#include "cdefs.h"
#include "mem.h"
extern cptr object_methods[meth_max];
#include "cint.h"
extern cptr scope_methods[meth_max];
extern cptr entset_methods[meth_max];
extern cptr boolean_methods[meth_max];
extern cptr strpool_methods[meth_max];
extern cptr filter_methods[meth_max];
extern cptr niterator_methods[meth_max];

extern cint entset_predef;
extern cint object_Undefined;
extern cint strpool_str_74;
extern cint scope_curr;


cptr iterator_methods[meth_max];

cint iterator_alloc(cint iterator_self, cint iterator_t, cint iterator_s){
  cint iterator_result;
  cint t1, t2, t3, t4, t5, t6, t7;
  t2 = object_alloc((cint)iterator_self);
  iterator_result = t2;
  ((cptr)iterator_self)[4] /* class_of*/ = strpool_str_74;
  t3 = mem_malloc(60); ((cptr)t3)[0] = scope_methods;
  t4 = scope_alloc((cint)t3, (cint)scope_curr, (cint)0);
  scope_curr = t4;
  ((cptr)entset_predef)[1] += 1;
  t5 = ((cptr)(((cptr)iterator_t)[0]))[27];
  t6 = ((f0)t5)((cint)iterator_t, (cint)entset_predef, (cint)1); /* Declare_as_var*/
  ((cptr)iterator_self)[7] /* target*/ = t6;
  ((cptr)iterator_self)[8] /* source*/ = iterator_s;
  ((cptr)iterator_self)[11] /* cursor*/ = 0;
  ((cptr)iterator_self)[9] /* elem1*/ = 0;
  ((cptr)iterator_self)[10] /* elem2*/ = 0;
  ((cptr)iterator_self)[12] /* ecursor*/ = 1;
  ((cptr)iterator_self)[3] /* len*/ = 1;
  t1 = iterator_result;
  goto t1_label;
  t1_label: return t1;
}

cint iterator_reclaim(cint iterator_self){
  cint iterator_e;
  cint t8;
  iterator_e = ((cptr)scope_curr)[7] /* enclosing*/;
  object_Dec_ref(scope_curr);
  scope_curr = iterator_e;
  object_Dec_ref(((cptr)iterator_self)[7] /* target*/);
  object_Dec_ref(((cptr)iterator_self)[8] /* source*/);
  object_reclaim((cint)iterator_self);
  t8_label: return t8;
}

cint iterator_eval_target(cint iterator_self){
  cint iterator_result;
  cint t9, t10, t11, t12;
  ((cptr)((cptr)iterator_self)[7] /* target*/)[1] += 1;
  t10 = ((cptr)(((cptr)((cptr)iterator_self)[7] /* target*/)[0]))[38];
  t11 = ((f0)t10)((cint)((cptr)iterator_self)[7] /* target*/); /* Eval*/
  t9 = t11;
  goto t9_label;
  t9_label: return t9;
}

cint iterator_iterate(cint iterator_self){
  cint iterator_result;
  cint t13, t14, t15, t16, t17, t18, t19, t20;
  t14 = ((cptr)iterator_self)[11] /* cursor*/ == ((cptr)((cptr)iterator_self)[8] /* source*/)[3] /* len*/;
  if (t14){
    ((cptr)iterator_self)[11] /* cursor*/ = 0;
    t13 = 0;
    goto t13_label;
  };
  t15 = ((cptr)iterator_self)[11] /* cursor*/ + 1;
  ((cptr)iterator_self)[11] /* cursor*/ = t15;
  t16 = ((cptr)(((cptr)((cptr)iterator_self)[8] /* source*/)[0]))[62];
  t17 = ((f0)t16)((cint)((cptr)iterator_self)[8] /* source*/, (cint)((cptr)iterator_self)[11] /* cursor*/); /* lookup*/
  ((cptr)iterator_self)[10] /* elem2*/ = t17;
  t19 = ((cptr)((cptr)iterator_self)[8] /* source*/)[3] /* len*/ <= 0;
  if (t19){
    t13 = 0;
    goto t13_label;
  };
  ((cptr)((cptr)iterator_self)[7] /* target*/)[1] += 1;
  ((cptr)((cptr)iterator_self)[10] /* elem2*/)[1] += 1;
  t20 = (cptr)(((cptr)(((cptr)((cptr)iterator_self)[7] /* target*/)[0]))[13]); /* Assign*/
  ((f0)t20)((cint)((cptr)iterator_self)[7] /* target*/, (cint)((cptr)iterator_self)[10] /* elem2*/);
  t13 = 1;
  goto t13_label;
  t13_label: return t13;
}

cint iterator_lookup(cint iterator_self, cint iterator_i){
  cint iterator_result;
  cint t21, t22, t23, t24, t25, t26, t27, t28, t29, t30, t31, t32, t33, t34, t35, t36, t37;
  t22 = ((cptr)iterator_self)[9] /* elem1*/ == 0;
  if (t22){
    t23 = ((cptr)(((cptr)iterator_self)[0]))[55];
    t24 = ((f0)t23)((cint)iterator_self); /* iterate*/
    t25 = t24 == 0;
    if (t25){
      ((cptr)iterator_self)[3] /* len*/ = 0;
      t21 = object_Undefined;
      goto t21_label;
    };
    t26 = (cptr)(((cptr)(((cptr)iterator_self)[0]))[260]); /* get_a_new_elem1*/
    ((f0)t26)((cint)iterator_self);
  };
  t27 = ((cptr)iterator_self)[12] /* ecursor*/ > ((cptr)((cptr)iterator_self)[9] /* elem1*/)[3] /* len*/;
  if (t27){
    t28 = (cptr)(((cptr)(((cptr)iterator_self)[0]))[260]); /* get_a_new_elem1*/
    ((f0)t28)((cint)iterator_self);
  };
  t29 = ((cptr)(((cptr)((cptr)iterator_self)[9] /* elem1*/)[0]))[62];
  t30 = ((f0)t29)((cint)((cptr)iterator_self)[9] /* elem1*/, (cint)((cptr)iterator_self)[12] /* ecursor*/); /* lookup*/
  iterator_result = t30;
  t32 = ((cptr)iterator_self)[12] /* ecursor*/ + 1;
  ((cptr)iterator_self)[12] /* ecursor*/ = t32;
  t33 = ((cptr)((cptr)iterator_self)[9] /* elem1*/)[3] /* len*/ <= 0;
  if (t33){
    t34 = ((cptr)iterator_self)[11] /* cursor*/ == 0;
    if (t34){
      ((cptr)iterator_self)[3] /* len*/ = 0;
      t21 = object_Undefined;
      goto t21_label;
    }else{
      t35 = ((cptr)(((cptr)iterator_self)[0]))[62];
      t36 = ((f0)t35)((cint)iterator_self, (cint)iterator_i); /* lookup*/
      t21 = t36;
      goto t21_label;
    };
  };
  t21 = iterator_result;
  goto t21_label;
  t21_label: return t21;
}

cint iterator_get_a_new_elem1(cint iterator_self){
  cint t38, t39, t40, t41, t42, t43, t44;
  ((cptr)iterator_self)[9] /* elem1*/ = ((cptr)iterator_self)[10] /* elem2*/;
  t39 = ((cptr)iterator_self)[3] /* len*/ + ((cptr)((cptr)iterator_self)[9] /* elem1*/)[3] /* len*/;
  ((cptr)iterator_self)[3] /* len*/ = t39;
  t40 = ((cptr)(((cptr)iterator_self)[0]))[55];
  t41 = ((f0)t40)((cint)iterator_self); /* iterate*/
  t42 = t41 == 0;
  if (t42){
    t43 = ((cptr)iterator_self)[3] /* len*/ - 1;
    ((cptr)iterator_self)[3] /* len*/ = t43;
  };
  ((cptr)iterator_self)[12] /* ecursor*/ = 1;
  ((cptr)((cptr)iterator_self)[7] /* target*/)[1] += 1;
  ((cptr)((cptr)iterator_self)[9] /* elem1*/)[1] += 1;
  t44 = (cptr)(((cptr)(((cptr)((cptr)iterator_self)[7] /* target*/)[0]))[13]); /* Assign*/
  ((f0)t44)((cint)((cptr)iterator_self)[7] /* target*/, (cint)((cptr)iterator_self)[9] /* elem1*/);
  t38_label: return t38;
}

cint iterator_Make_filter(cint iterator_self, cint iterator_le){
  cint iterator_result;
  cint t45, t46, t47;
  t46 = mem_malloc(36); ((cptr)t46)[0] = filter_methods;
  t47 = filter_alloc((cint)t46, (cint)iterator_self, (cint)iterator_le);
  t45 = t47;
  goto t45_label;
  t45_label: return t45;
}

cint iterator_Make_list(cint iterator_self){
  cint iterator_result;
  cint t48, t49, t50, t51;
  ((cptr)((cptr)iterator_self)[8] /* source*/)[1] += 1;
  t49 = ((cptr)(((cptr)((cptr)iterator_self)[8] /* source*/)[0]))[68];
  t50 = ((f0)t49)((cint)((cptr)iterator_self)[8] /* source*/); /* Make_list*/
  iterator_result = t50;
  object_Dec_ref(iterator_self);
  t48 = iterator_result;
  goto t48_label;
  t48_label: return t48;
}

cint iterator_Make_set(cint iterator_self){
  cint iterator_result;
  cint t52, t53, t54, t55;
  ((cptr)((cptr)iterator_self)[8] /* source*/)[1] += 1;
  t53 = ((cptr)(((cptr)((cptr)iterator_self)[8] /* source*/)[0]))[74];
  t54 = ((f0)t53)((cint)((cptr)iterator_self)[8] /* source*/); /* Make_set*/
  iterator_result = t54;
  object_Dec_ref(iterator_self);
  t52 = iterator_result;
  goto t52_label;
  t52_label: return t52;
}

cint iterator_Make_nested_iterator(cint iterator_self, cint iterator_t, cint iterator_s){
  cint iterator_result;
  cint t56, t57, t58;
  t57 = mem_malloc(48); ((cptr)t57)[0] = niterator_methods;
  t58 = niterator_alloc((cint)t57, (cint)iterator_self, (cint)iterator_t, (cint)iterator_s);
  t56 = t58;
  goto t56_label;
  t56_label: return t56;
}

void init_iterator(){
  copy_meth_table(iterator_methods, object_methods);
  iterator_methods[0] = 52;
  iterator_methods[3] = iterator_alloc;
  iterator_methods[4] = iterator_reclaim;
  iterator_methods[40] = iterator_eval_target;
  iterator_methods[55] = iterator_iterate;
  iterator_methods[62] = iterator_lookup;
  iterator_methods[260] = iterator_get_a_new_elem1;
  iterator_methods[66] = iterator_Make_filter;
  iterator_methods[68] = iterator_Make_list;
  iterator_methods[74] = iterator_Make_set;
  iterator_methods[71] = iterator_Make_nested_iterator;
}
