#include <stdint.h>
#include <stdlib.h>
#include "el_runtime.h"

el_val_t str_ends(el_val_t s, el_val_t suf);
el_val_t str_last_char(el_val_t s);
el_val_t str_last2(el_val_t s);
el_val_t str_last3(el_val_t s);
el_val_t str_drop_last(el_val_t s, el_val_t n);
el_val_t is_vowel(el_val_t c);
el_val_t morph_apply_suffix(el_val_t base, el_val_t suffix);
el_val_t en_irregular_plural(el_val_t word);
el_val_t en_irregular_singular(el_val_t word);
el_val_t en_irregular_verb(el_val_t base);
el_val_t en_verb_3sg(el_val_t base);
el_val_t en_should_double_final(el_val_t base);
el_val_t en_verb_past(el_val_t base);
el_val_t en_verb_gerund(el_val_t base);
el_val_t en_pluralize_regular(el_val_t singular);
el_val_t en_verb_form(el_val_t base, el_val_t tense, el_val_t person, el_val_t number);
el_val_t agree_determiner(el_val_t det, el_val_t noun);
el_val_t morph_pluralize(el_val_t noun, el_val_t profile);
el_val_t morph_map_canonical(el_val_t verb, el_val_t code);
el_val_t morph_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number, el_val_t profile);
el_val_t morph_inflect(el_val_t word, el_val_t features, el_val_t profile);
el_val_t pluralize(el_val_t singular);
el_val_t singularize(el_val_t plural);
el_val_t verb_form(el_val_t base, el_val_t tense, el_val_t person, el_val_t number);
el_val_t irregular_plural(el_val_t word);
el_val_t irregular_singular(el_val_t word);
el_val_t goh_str_ends(el_val_t s, el_val_t suf);
el_val_t goh_drop(el_val_t s, el_val_t n);
el_val_t goh_slot(el_val_t person, el_val_t number);
el_val_t goh_map_canonical(el_val_t verb);
el_val_t goh_wesan_present(el_val_t slot);
el_val_t goh_wesan_past(el_val_t slot);
el_val_t goh_haben_present(el_val_t slot);
el_val_t goh_haben_past(el_val_t slot);
el_val_t goh_gan_present(el_val_t slot);
el_val_t goh_gan_past(el_val_t slot);
el_val_t goh_sehan_present(el_val_t slot);
el_val_t goh_sehan_past(el_val_t slot);
el_val_t goh_quethan_present(el_val_t slot);
el_val_t goh_quethan_past(el_val_t slot);
el_val_t goh_tuon_present(el_val_t slot);
el_val_t goh_tuon_past(el_val_t slot);
el_val_t goh_weak_present(el_val_t stem, el_val_t slot);
el_val_t goh_weak_past(el_val_t stem, el_val_t slot);
el_val_t goh_verb_stem(el_val_t verb);
el_val_t goh_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
el_val_t goh_stem_type(el_val_t noun);
el_val_t goh_extract_stem(el_val_t noun, el_val_t stype);
el_val_t goh_decline_masc_a_sg(el_val_t stem, el_val_t gram_case);
el_val_t goh_decline_masc_a_pl(el_val_t stem, el_val_t gram_case);
el_val_t goh_decline_fem_o_sg(el_val_t stem, el_val_t gram_case);
el_val_t goh_decline_fem_o_pl(el_val_t stem, el_val_t gram_case);
el_val_t goh_decline_neut_a_sg(el_val_t stem, el_val_t gram_case);
el_val_t goh_decline_neut_a_pl(el_val_t stem, el_val_t gram_case);
el_val_t goh_decline_masc_n_sg(el_val_t stem, el_val_t gram_case);
el_val_t goh_decline_masc_n_pl(el_val_t stem, el_val_t gram_case);
el_val_t goh_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
el_val_t goh_demo_article(el_val_t stype, el_val_t number);
el_val_t goh_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite);

el_val_t goh_str_ends(el_val_t s, el_val_t suf) {
  return str_ends_with(s, suf);
  return 0;
}

el_val_t goh_drop(el_val_t s, el_val_t n) {
  el_val_t len = str_len(s);
  if (n >= len) {
    return EL_STR("");
  }
  return str_slice(s, 0, (len - n));
  return 0;
}

el_val_t goh_slot(el_val_t person, el_val_t number) {
  if (str_eq(person, EL_STR("first"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return 0;
    }
    return 3;
  }
  if (str_eq(person, EL_STR("second"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return 1;
    }
    return 4;
  }
  if (str_eq(number, EL_STR("singular"))) {
    return 2;
  }
  return 5;
  return 0;
}

el_val_t goh_map_canonical(el_val_t verb) {
  if (str_eq(verb, EL_STR("be"))) {
    return EL_STR("wesan");
  }
  if (str_eq(verb, EL_STR("have"))) {
    return EL_STR("haben");
  }
  if (str_eq(verb, EL_STR("go"))) {
    return EL_STR("gan");
  }
  if (str_eq(verb, EL_STR("see"))) {
    return EL_STR("sehan");
  }
  if (str_eq(verb, EL_STR("say"))) {
    return EL_STR("quethan");
  }
  if (str_eq(verb, EL_STR("do"))) {
    return EL_STR("tuon");
  }
  if (str_eq(verb, EL_STR("make"))) {
    return EL_STR("tuon");
  }
  if (str_eq(verb, EL_STR("come"))) {
    return EL_STR("queman");
  }
  if (str_eq(verb, EL_STR("give"))) {
    return EL_STR("geban");
  }
  if (str_eq(verb, EL_STR("know"))) {
    return EL_STR("wizzan");
  }
  if (str_eq(verb, EL_STR("want"))) {
    return EL_STR("wellan");
  }
  return verb;
  return 0;
}

el_val_t goh_wesan_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("bim");
  }
  if (slot == 1) {
    return EL_STR("bist");
  }
  if (slot == 2) {
    return EL_STR("ist");
  }
  if (slot == 3) {
    return EL_STR("birum");
  }
  if (slot == 4) {
    return EL_STR("birut");
  }
  return EL_STR("sint");
  return 0;
}

el_val_t goh_wesan_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("was");
  }
  if (slot == 1) {
    return EL_STR("wari");
  }
  if (slot == 2) {
    return EL_STR("was");
  }
  if (slot == 3) {
    return EL_STR("warum");
  }
  if (slot == 4) {
    return EL_STR("warut");
  }
  return EL_STR("warun");
  return 0;
}

el_val_t goh_haben_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("habem");
  }
  if (slot == 1) {
    return EL_STR("habest");
  }
  if (slot == 2) {
    return EL_STR("habet");
  }
  if (slot == 3) {
    return EL_STR("habemes");
  }
  if (slot == 4) {
    return EL_STR("habet");
  }
  return EL_STR("habent");
  return 0;
}

el_val_t goh_haben_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("habeta");
  }
  if (slot == 1) {
    return EL_STR("habetos");
  }
  if (slot == 2) {
    return EL_STR("habeta");
  }
  if (slot == 3) {
    return EL_STR("habetom");
  }
  if (slot == 4) {
    return EL_STR("habetot");
  }
  return EL_STR("habeton");
  return 0;
}

el_val_t goh_gan_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("gan");
  }
  if (slot == 1) {
    return EL_STR("gest");
  }
  if (slot == 2) {
    return EL_STR("get");
  }
  if (slot == 3) {
    return EL_STR("games");
  }
  if (slot == 4) {
    return EL_STR("gat");
  }
  return EL_STR("gant");
  return 0;
}

el_val_t goh_gan_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("giang");
  }
  if (slot == 1) {
    return EL_STR("giangi");
  }
  if (slot == 2) {
    return EL_STR("giang");
  }
  if (slot == 3) {
    return EL_STR("giangum");
  }
  if (slot == 4) {
    return EL_STR("giangun");
  }
  return EL_STR("giangun");
  return 0;
}

el_val_t goh_sehan_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("sihu");
  }
  if (slot == 1) {
    return EL_STR("sihist");
  }
  if (slot == 2) {
    return EL_STR("sihit");
  }
  if (slot == 3) {
    return EL_STR("sehemes");
  }
  if (slot == 4) {
    return EL_STR("sehet");
  }
  return EL_STR("sehent");
  return 0;
}

el_val_t goh_sehan_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("sah");
  }
  if (slot == 1) {
    return EL_STR("sahi");
  }
  if (slot == 2) {
    return EL_STR("sah");
  }
  if (slot == 3) {
    return EL_STR("sahum");
  }
  if (slot == 4) {
    return EL_STR("sahut");
  }
  return EL_STR("sahun");
  return 0;
}

el_val_t goh_quethan_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("quidu");
  }
  if (slot == 1) {
    return EL_STR("quidist");
  }
  if (slot == 2) {
    return EL_STR("quidit");
  }
  if (slot == 3) {
    return EL_STR("quethumes");
  }
  if (slot == 4) {
    return EL_STR("quethet");
  }
  return EL_STR("quethent");
  return 0;
}

el_val_t goh_quethan_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("quad");
  }
  if (slot == 1) {
    return EL_STR("quadi");
  }
  if (slot == 2) {
    return EL_STR("quad");
  }
  if (slot == 3) {
    return EL_STR("quadum");
  }
  if (slot == 4) {
    return EL_STR("quadut");
  }
  return EL_STR("quadun");
  return 0;
}

el_val_t goh_tuon_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("tuom");
  }
  if (slot == 1) {
    return EL_STR("tuost");
  }
  if (slot == 2) {
    return EL_STR("tuot");
  }
  if (slot == 3) {
    return EL_STR("tuomes");
  }
  if (slot == 4) {
    return EL_STR("tuot");
  }
  return EL_STR("tuont");
  return 0;
}

el_val_t goh_tuon_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("teta");
  }
  if (slot == 1) {
    return EL_STR("tetos");
  }
  if (slot == 2) {
    return EL_STR("teta");
  }
  if (slot == 3) {
    return EL_STR("tetom");
  }
  if (slot == 4) {
    return EL_STR("tetot");
  }
  return EL_STR("teton");
  return 0;
}

el_val_t goh_weak_present(el_val_t stem, el_val_t slot) {
  if (slot == 0) {
    return el_str_concat(stem, EL_STR("u"));
  }
  if (slot == 1) {
    return el_str_concat(stem, EL_STR("ist"));
  }
  if (slot == 2) {
    return el_str_concat(stem, EL_STR("it"));
  }
  if (slot == 3) {
    return el_str_concat(stem, EL_STR("emes"));
  }
  if (slot == 4) {
    return el_str_concat(stem, EL_STR("et"));
  }
  return el_str_concat(stem, EL_STR("ent"));
  return 0;
}

el_val_t goh_weak_past(el_val_t stem, el_val_t slot) {
  if (slot == 0) {
    return el_str_concat(stem, EL_STR("ta"));
  }
  if (slot == 1) {
    return el_str_concat(stem, EL_STR("tos"));
  }
  if (slot == 2) {
    return el_str_concat(stem, EL_STR("ta"));
  }
  if (slot == 3) {
    return el_str_concat(stem, EL_STR("tom"));
  }
  if (slot == 4) {
    return el_str_concat(stem, EL_STR("tot"));
  }
  return el_str_concat(stem, EL_STR("ton"));
  return 0;
}

el_val_t goh_verb_stem(el_val_t verb) {
  return goh_drop(verb, 2);
  return 0;
}

el_val_t goh_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number) {
  el_val_t v = goh_map_canonical(verb);
  el_val_t slot = goh_slot(person, number);
  if (str_eq(v, EL_STR("wesan"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return goh_wesan_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return goh_wesan_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("haben"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return goh_haben_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return goh_haben_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("haben"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return goh_haben_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return goh_haben_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("gan"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return goh_gan_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return goh_gan_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("sehan"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return goh_sehan_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return goh_sehan_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("quethan"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return goh_quethan_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return goh_quethan_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("tuon"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return goh_tuon_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return goh_tuon_past(slot);
    }
    return v;
  }
  el_val_t stem = goh_verb_stem(v);
  if (str_eq(tense, EL_STR("present"))) {
    return goh_weak_present(stem, slot);
  }
  if (str_eq(tense, EL_STR("past"))) {
    return goh_weak_past(stem, slot);
  }
  return v;
  return 0;
}

el_val_t goh_stem_type(el_val_t noun) {
  if (goh_str_ends(noun, EL_STR("o"))) {
    return EL_STR("masc_n");
  }
  if (goh_str_ends(noun, EL_STR("a"))) {
    return EL_STR("fem_o");
  }
  if (goh_str_ends(noun, EL_STR("t"))) {
    return EL_STR("neut_a");
  }
  if (goh_str_ends(noun, EL_STR("d"))) {
    return EL_STR("neut_a");
  }
  if (goh_str_ends(noun, EL_STR("nd"))) {
    return EL_STR("neut_a");
  }
  return EL_STR("masc_a");
  return 0;
}

el_val_t goh_extract_stem(el_val_t noun, el_val_t stype) {
  if (str_eq(stype, EL_STR("fem_o"))) {
    return goh_drop(noun, 1);
  }
  if (str_eq(stype, EL_STR("masc_n"))) {
    return goh_drop(noun, 1);
  }
  return noun;
  return 0;
}

el_val_t goh_decline_masc_a_sg(el_val_t stem, el_val_t gram_case) {
  if (str_eq(gram_case, EL_STR("nominative"))) {
    return stem;
  }
  if (str_eq(gram_case, EL_STR("accusative"))) {
    return stem;
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return el_str_concat(stem, EL_STR("es"));
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return el_str_concat(stem, EL_STR("e"));
  }
  return stem;
  return 0;
}

el_val_t goh_decline_masc_a_pl(el_val_t stem, el_val_t gram_case) {
  if (str_eq(gram_case, EL_STR("nominative"))) {
    return el_str_concat(stem, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("accusative"))) {
    return el_str_concat(stem, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return el_str_concat(stem, EL_STR("o"));
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return el_str_concat(stem, EL_STR("um"));
  }
  return el_str_concat(stem, EL_STR("a"));
  return 0;
}

el_val_t goh_decline_fem_o_sg(el_val_t stem, el_val_t gram_case) {
  if (str_eq(gram_case, EL_STR("nominative"))) {
    return el_str_concat(stem, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("accusative"))) {
    return el_str_concat(stem, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return el_str_concat(stem, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return el_str_concat(stem, EL_STR("u"));
  }
  return el_str_concat(stem, EL_STR("a"));
  return 0;
}

el_val_t goh_decline_fem_o_pl(el_val_t stem, el_val_t gram_case) {
  if (str_eq(gram_case, EL_STR("nominative"))) {
    return el_str_concat(stem, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("accusative"))) {
    return el_str_concat(stem, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return el_str_concat(stem, EL_STR("ono"));
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return el_str_concat(stem, EL_STR("om"));
  }
  return el_str_concat(stem, EL_STR("a"));
  return 0;
}

el_val_t goh_decline_neut_a_sg(el_val_t stem, el_val_t gram_case) {
  if (str_eq(gram_case, EL_STR("nominative"))) {
    return stem;
  }
  if (str_eq(gram_case, EL_STR("accusative"))) {
    return stem;
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return el_str_concat(stem, EL_STR("es"));
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return el_str_concat(stem, EL_STR("e"));
  }
  return stem;
  return 0;
}

el_val_t goh_decline_neut_a_pl(el_val_t stem, el_val_t gram_case) {
  if (str_eq(gram_case, EL_STR("nominative"))) {
    return stem;
  }
  if (str_eq(gram_case, EL_STR("accusative"))) {
    return stem;
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return el_str_concat(stem, EL_STR("o"));
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return el_str_concat(stem, EL_STR("um"));
  }
  return stem;
  return 0;
}

el_val_t goh_decline_masc_n_sg(el_val_t stem, el_val_t gram_case) {
  if (str_eq(gram_case, EL_STR("nominative"))) {
    return el_str_concat(stem, EL_STR("o"));
  }
  if (str_eq(gram_case, EL_STR("accusative"))) {
    return el_str_concat(stem, EL_STR("on"));
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return el_str_concat(stem, EL_STR("on"));
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return el_str_concat(stem, EL_STR("on"));
  }
  return el_str_concat(stem, EL_STR("o"));
  return 0;
}

el_val_t goh_decline_masc_n_pl(el_val_t stem, el_val_t gram_case) {
  if (str_eq(gram_case, EL_STR("nominative"))) {
    return el_str_concat(stem, EL_STR("on"));
  }
  if (str_eq(gram_case, EL_STR("accusative"))) {
    return el_str_concat(stem, EL_STR("on"));
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return el_str_concat(stem, EL_STR("ono"));
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return el_str_concat(stem, EL_STR("om"));
  }
  return el_str_concat(stem, EL_STR("on"));
  return 0;
}

el_val_t goh_decline(el_val_t noun, el_val_t gram_case, el_val_t number) {
  el_val_t stype = goh_stem_type(noun);
  el_val_t stem = goh_extract_stem(noun, stype);
  if (str_eq(stype, EL_STR("masc_a"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return goh_decline_masc_a_sg(stem, gram_case);
    }
    return goh_decline_masc_a_pl(stem, gram_case);
  }
  if (str_eq(stype, EL_STR("fem_o"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return goh_decline_fem_o_sg(stem, gram_case);
    }
    return goh_decline_fem_o_pl(stem, gram_case);
  }
  if (str_eq(stype, EL_STR("neut_a"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return goh_decline_neut_a_sg(stem, gram_case);
    }
    return goh_decline_neut_a_pl(stem, gram_case);
  }
  if (str_eq(stype, EL_STR("masc_n"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return goh_decline_masc_n_sg(stem, gram_case);
    }
    return goh_decline_masc_n_pl(stem, gram_case);
  }
  return noun;
  return 0;
}

el_val_t goh_demo_article(el_val_t stype, el_val_t number) {
  if (str_eq(number, EL_STR("plural"))) {
    return EL_STR("die");
  }
  if (str_eq(stype, EL_STR("fem_o"))) {
    return EL_STR("diu");
  }
  if (str_eq(stype, EL_STR("neut_a"))) {
    return EL_STR("daz");
  }
  return EL_STR("der");
  return 0;
}

el_val_t goh_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite) {
  el_val_t stype = goh_stem_type(noun);
  el_val_t declined = goh_decline(noun, gram_case, number);
  if (str_eq(definite, EL_STR("true"))) {
    el_val_t art = goh_demo_article(stype, number);
    return el_str_concat(el_str_concat(art, EL_STR(" ")), declined);
  }
  return declined;
  return 0;
}

