#include "cdefs.h"
#include "mem.h"
extern cptr object_methods[meth_max];
extern cptr boolean_methods[meth_max];
extern cptr integer_methods[meth_max];
extern cptr string_methods[meth_max];
extern cptr sstring_methods[meth_max];
extern cptr list_methods[meth_max];
extern cptr slist_methods[meth_max];
#include "cfile.h"
#include "cint.h"
#include "cprocess.h"
#include "cstring.h"
#include "cobarray.h"
#include "cdir.h"
extern cptr abstract_methods[meth_max];

extern cint boolean_True;
extern cint object_Undefined;
extern cint boolean_False;
extern cint abstract_counter;

cint clib_Trace;
cint clib_ArgV;
cint clib_ArgC;
cint clib_Command_line;
cint clib_Myself;
cint clib_Stderr;
cint clib_Stdin;
cint clib_Stdout;
cint clib_stde;
cint clib_stdi;
cint clib_stdo;

cptr clib_methods[meth_max];

cint clib_init(){
  cint clib_ste, clib_sti, clib_sto, clib_i, clib_avp, clib_av, clib_avd;
  cint t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t15, t16, t17, t18, t19, t20;
  t2 = mem_malloc(28); ((cptr)t2)[0] = integer_methods;
  t3 = integer_ialloc((cint)t2, (cint)0);
  clib_Trace = t3;
  t4 = mem_malloc(28); ((cptr)t4)[0] = abstract_methods;
  t5 = abstract_alloc((cint)t4);
  clib_Myself = t5;
  clib_ste = stderr;
  clib_sti = stdin;
  clib_sto = stdout;
  t6 = mem_malloc(28); ((cptr)t6)[0] = abstract_methods;
  t7 = abstract_alloc((cint)t6);
  clib_Stderr = t7;
  ((cptr)clib_Stderr)[2] /* hashval*/ = clib_ste;
  t8 = mem_malloc(28); ((cptr)t8)[0] = abstract_methods;
  t9 = abstract_alloc((cint)t8);
  clib_Stdin = t9;
  ((cptr)clib_Stdin)[2] /* hashval*/ = clib_sti;
  t10 = mem_malloc(28); ((cptr)t10)[0] = abstract_methods;
  t11 = abstract_alloc((cint)t10);
  clib_Stdout = t11;
  ((cptr)clib_Stdout)[2] /* hashval*/ = clib_sto;
  clib_stde = clib_ste;
  clib_stdi = clib_sti;
  clib_stdo = clib_sto;
  t12 = mem_malloc(40); ((cptr)t12)[0] = slist_methods;
  t13 = slist_alloc((cint)t12, (cint)clib_ArgC);
  clib_Command_line = t13;
  for (clib_i = 1; clib_i <= clib_ArgC; clib_i++){
    t15 = clib_i - 1;
    clib_avp = ((cptr)(clib_ArgV))[t15];
    clib_av = clib_avp;
    clib_avd = cstring_strdupl((cint)clib_av);
    t16 = mem_malloc(40); ((cptr)t16)[0] = sstring_methods;
    t17 = sstring_alloc((cint)t16, (cint)clib_avd);
    t18 = ((cptr)(((cptr)clib_Command_line)[0]))[7];
    t19 = ((f0)t18)((cint)clib_Command_line, (cint)t17); /* Append*/
    clib_Command_line = t19;
  };t14:;
  t1_label: return t1;
}

cint clib_fini(){
  cint t21;
  object_Dec_ref(clib_Trace);
  object_Dec_ref(clib_Command_line);
  object_Dec_ref(clib_Myself);
  object_Dec_ref(clib_Stderr);
  object_Dec_ref(clib_Stdin);
  object_Dec_ref(clib_Stdout);
  t21_label: return t21;
}

cint clib_as_cstring(cint clib_str){
  cint clib_result, clib_siz, clib_r, clib_i;
  cint t22, t23, t25, t26, t27, t28;
  clib_siz = ((cptr)clib_str)[3] /* len*/;
  t23 = clib_siz + 1;
  clib_r = cstring_mem_malloc((cint)t23);
  for (clib_i = 1; clib_i <= clib_siz; clib_i++){
    t25 = clib_i - 1;
    t26 = ((cptr)(((cptr)clib_str)[0]))[62];
    t27 = ((f0)t26)((cint)clib_str, (cint)clib_i); /* lookup*/
    cstring_update_char((cint)clib_r, (cint)t25, (cint)((cptr)t27)[2] /* hashval*/);
  };t24:;
  cstring_update_char((cint)clib_r, (cint)clib_siz, (cint)0);
  t22 = clib_r;
  goto t22_label;
  t22_label: return t22;
}

cint clib_Assign_file(cint clib_port, cint clib_filename, cint clib_mode){
  cint clib_result, clib_fn, clib_md, clib_fi;
  cint t29, t30, t31, t32, t33, t34, t35;
  t30 = clib_as_cstring((cint)clib_filename);
  clib_fn = t30;
  t31 = clib_as_cstring((cint)clib_mode);
  clib_md = t31;
  clib_fi = cfile_fopen((cint)clib_fn, (cint)clib_md);
  t32 = clib_fi == 0;
  if (t32){
    clib_result = boolean_False;
  }else{
    t33 = clib_port == clib_Stderr;
    if (t33){
      clib_stde = clib_fi;
    };
    t34 = clib_port == clib_Stdin;
    if (t34){
      clib_stdi = clib_fi;
    };
    t35 = clib_port == clib_Stdout;
    if (t35){
      clib_stdo = clib_fi;
    };
    ((cptr)clib_port)[2] /* hashval*/ = clib_fi;
    clib_result = boolean_True;
  };
  cstring_mem_free((cint)clib_fn);
  cstring_mem_free((cint)clib_md);
  object_Dec_ref(clib_port);
  object_Dec_ref(clib_filename);
  object_Dec_ref(clib_mode);
  t29 = clib_result;
  goto t29_label;
  t29_label: return t29;
}

cint clib_Chdir(cint clib_name){
  cint clib_dn;
  cint t36, t37;
  t37 = clib_as_cstring((cint)clib_name);
  clib_dn = t37;
  cdir_chdir((cint)clib_dn);
  cstring_mem_free((cint)clib_dn);
  object_Dec_ref(clib_name);
  t36_label: return t36;
}

cint clib_Close(cint clib_port){
  cint clib_fi;
  cint t38;
  clib_fi = ((cptr)clib_port)[2] /* hashval*/;
  cfile_fclose((cint)clib_fi);
  object_Dec_ref(clib_port);
  t38_label: return t38;
}

cint clib_Delete_directory(cint clib_name){
  cint clib_dn;
  cint t39, t40;
  t40 = clib_as_cstring((cint)clib_name);
  clib_dn = t40;
  cdir_rmdir((cint)clib_dn);
  cstring_mem_free((cint)clib_dn);
  object_Dec_ref(clib_name);
  t39_label: return t39;
}

cint clib_Delete_file(cint clib_name){
  cint clib_fn;
  cint t41, t42;
  t42 = clib_as_cstring((cint)clib_name);
  clib_fn = t42;
  cfile_remove((cint)clib_fn);
  cstring_mem_free((cint)clib_fn);
  object_Dec_ref(clib_name);
  t41_label: return t41;
}

cint clib_Exit(cint clib_code){
  cint clib_c;
  cint t43;
  clib_c = ((cptr)clib_code)[2] /* hashval*/;
  cprocess_exitt((cint)clib_c);
  object_Dec_ref(clib_code);
  t43_label: return t43;
}

cint clib_Fgets(cint clib_strmax, cint clib_file){
  cint clib_result, clib_b, clib_fi, clib_s;
  cint t44, t45, t46, t47;
  clib_b = cstring_mem_malloc((cint)((cptr)clib_strmax)[2] /* hashval*/);
  clib_fi = ((cptr)clib_file)[2] /* hashval*/;
  t45 = cfile_fgets((cint)clib_b, (cint)((cptr)clib_strmax)[2] /* hashval*/, (cint)clib_fi) == 0;
  if (t45){
    clib_result = object_Undefined;
  }else{
    clib_s = cstring_strdupl((cint)clib_b);
    t46 = mem_malloc(40); ((cptr)t46)[0] = sstring_methods;
    t47 = sstring_alloc((cint)t46, (cint)clib_s);
    clib_result = t47;
  };
  cstring_mem_free((cint)clib_b);
  object_Dec_ref(clib_strmax);
  object_Dec_ref(clib_file);
  t44 = clib_result;
  goto t44_label;
  t44_label: return t44;
}

cint clib_Fwrite(cint clib_buffer, cint clib_elem_size, cint clib_num_elems, cint clib_file){
  cint clib_es, clib_ne, clib_siz, clib_fi, clib_b, clib_i;
  cint t48, t49, t50, t52, t53, t54, t55, t56;
  clib_es = ((cptr)clib_elem_size)[2] /* hashval*/;
  clib_ne = ((cptr)clib_num_elems)[2] /* hashval*/;
  t49 = clib_es * clib_ne;
  clib_siz = t49;
  clib_fi = ((cptr)clib_file)[2] /* hashval*/;
  t50 = clib_siz + 2;
  clib_b = cstring_mem_malloc((cint)t50);
  for (clib_i = 1; clib_i <= clib_siz; clib_i++){
    t52 = clib_i - 1;
    t53 = ((cptr)(((cptr)clib_buffer)[0]))[62];
    t54 = ((f0)t53)((cint)clib_buffer, (cint)clib_i); /* lookup*/
    cstring_update_char((cint)clib_b, (cint)t52, (cint)((cptr)t54)[2] /* hashval*/);
  };t51:;
  cstring_update_char((cint)clib_b, (cint)clib_siz, (cint)10);
  t56 = clib_siz + 1;
  cstring_update_char((cint)clib_b, (cint)t56, (cint)0);
  cfile_fwrite((cint)clib_b, (cint)clib_es, (cint)clib_ne, (cint)clib_fi);
  cstring_mem_free((cint)clib_b);
  object_Dec_ref(clib_buffer);
  object_Dec_ref(clib_elem_size);
  object_Dec_ref(clib_num_elems);
  object_Dec_ref(clib_file);
  t48_label: return t48;
}

cint clib_Get_closed_io_port(){
  cint clib_result;
  cint t57, t58, t59;
  t58 = mem_malloc(28); ((cptr)t58)[0] = abstract_methods;
  t59 = abstract_alloc((cint)t58);
  clib_result = t59;
  t57 = clib_result;
  goto t57_label;
  t57_label: return t57;
}

cint clib_Getcwd(){
  cint clib_result, clib_cwd;
  cint t60, t61, t62;
  clib_cwd = cdir_getcwd((cint)0);
  t61 = mem_malloc(40); ((cptr)t61)[0] = sstring_methods;
  t62 = sstring_stalloc((cint)t61, (cint)clib_cwd);
  t60 = t62;
  goto t60_label;
  t60_label: return t60;
}

cint clib_Mkdir(cint clib_dirname){
  cint clib_dn;
  cint t63, t64;
  t64 = clib_as_cstring((cint)clib_dirname);
  clib_dn = t64;
  cdir_mkdir((cint)clib_dn, (cint)448);
  cstring_mem_free((cint)clib_dn);
  object_Dec_ref(clib_dirname);
  t63_label: return t63;
}

cint clib_Open(cint clib_filename, cint clib_mode){
  cint clib_result, clib_fn, clib_mo, clib_fi;
  cint t65, t66, t67, t68, t69, t70;
  t66 = clib_as_cstring((cint)clib_filename);
  clib_fn = t66;
  t67 = clib_as_cstring((cint)clib_mode);
  clib_mo = t67;
  clib_fi = cfile_fopen((cint)clib_fn, (cint)clib_mo);
  t68 = clib_fi == 0;
  if (t68){
    clib_result = object_Undefined;
  }else{
    t69 = mem_malloc(28); ((cptr)t69)[0] = abstract_methods;
    t70 = abstract_alloc((cint)t69);
    clib_result = t70;
    ((cptr)clib_result)[2] /* hashval*/ = cfile_fopen((cint)clib_fn, (cint)clib_mo);
  };
  cstring_mem_free((cint)clib_fn);
  cstring_mem_free((cint)clib_mo);
  object_Dec_ref(clib_filename);
  object_Dec_ref(clib_mode);
  t65 = clib_result;
  goto t65_label;
  t65_label: return t65;
}

cint clib_Read_dir(cint clib_dirname){
  cint clib_result, clib_dn, clib_success, clib_fn, clib_fndup, clib_filename;
  cint t71, t72, t73, t74, t75, t78, t79, t80, t81, t82, t83;
  t72 = mem_malloc(40); ((cptr)t72)[0] = slist_methods;
  t73 = slist_alloc((cint)t72, (cint)64);
  clib_result = t73;
  t74 = clib_as_cstring((cint)clib_dirname);
  clib_dn = t74;
  clib_success = cdir_open_dir((cint)clib_dn);
  t75 = clib_success == 0;
  if (t75){
    t71 = clib_result;
    goto t71_label;
  };
  t76:
    clib_fn = cdir_next_fname();
    t78 = clib_fn == 0;
    if (t78){
      t71 = clib_result;
      goto t71_label;
    };
    clib_fndup = cstring_strdupl((cint)clib_fn);
    t79 = mem_malloc(40); ((cptr)t79)[0] = sstring_methods;
    t80 = sstring_stalloc((cint)t79, (cint)clib_fndup);
    clib_filename = t80;
    t81 = ((cptr)(((cptr)clib_result)[0]))[7];
    t82 = ((f0)t81)((cint)clib_result, (cint)clib_filename); /* Append*/
    clib_result = t82;
  goto t76;
  t77:;
  cstring_mem_free((cint)clib_dn);
  object_Dec_ref(clib_dirname);
  t71 = clib_result;
  goto t71_label;
  t71_label: return t71;
}

cint clib_Read_entity_count(){
  cint clib_result;
  cint t84, t85, t86;
  t85 = mem_malloc(28); ((cptr)t85)[0] = integer_methods;
  t86 = integer_ialloc((cint)t85, (cint)abstract_counter);
  t84 = t86;
  goto t84_label;
  t84_label: return t84;
}

cint clib_Rename_file(cint clib_old_name, cint clib_new_name){
  cint clib_on, clib_nn;
  cint t87, t88, t89;
  t88 = clib_as_cstring((cint)clib_old_name);
  clib_on = t88;
  t89 = clib_as_cstring((cint)clib_new_name);
  clib_nn = t89;
  cfile_rename((cint)clib_on, (cint)clib_nn);
  cstring_mem_free((cint)clib_on);
  cstring_mem_free((cint)clib_nn);
  object_Dec_ref(clib_old_name);
  object_Dec_ref(clib_new_name);
  t87_label: return t87;
}

cint clib_Restore_stdin(){
  cint t90;
  ((cptr)clib_Stdin)[2] /* hashval*/ = stdin;
  clib_stdi = stdin;
  t90_label: return t90;
}

cint clib_Set_entity_count(cint clib_cntr){
  cint t91;
  abstract_counter = ((cptr)clib_cntr)[2] /* hashval*/;
  object_Dec_ref(clib_cntr);
  t91_label: return t91;
}

cint clib_Set_trace(cint clib_level){
  cint t92;
  object_Dec_ref(clib_Trace);
  clib_Trace = clib_level;
  t92_label: return t92;
}

void init_clib(){
  copy_meth_table(clib_methods, entity_methods);
  clib_methods[0] = 0;
  clib_methods[1] = clib_init;
  clib_methods[2] = clib_fini;
  clib_methods[131] = clib_as_cstring;
  clib_methods[270] = clib_Assign_file;
  clib_methods[271] = clib_Chdir;
  clib_methods[132] = clib_Close;
  clib_methods[272] = clib_Delete_directory;
  clib_methods[133] = clib_Delete_file;
  clib_methods[134] = clib_Exit;
  clib_methods[135] = clib_Fgets;
  clib_methods[136] = clib_Fwrite;
  clib_methods[137] = clib_Get_closed_io_port;
  clib_methods[273] = clib_Getcwd;
  clib_methods[274] = clib_Mkdir;
  clib_methods[138] = clib_Open;
  clib_methods[275] = clib_Read_dir;
  clib_methods[139] = clib_Read_entity_count;
  clib_methods[140] = clib_Rename_file;
  clib_methods[141] = clib_Restore_stdin;
  clib_methods[142] = clib_Set_entity_count;
  clib_methods[143] = clib_Set_trace;
}
