cast simplification, fixes, compiles to the end (no indirect)
[ia32rtools.git] / tools / translate.c
index ef38d73..2ba4031 100644 (file)
@@ -38,6 +38,7 @@ enum op_flags {
   OPF_REPZ   = (1 << 8), /* rep is repe/repz */
   OPF_REPNZ  = (1 << 9), /* rep is repne/repnz */
   OPF_FARG   = (1 << 10), /* push collected as func arg (no reuse) */
+  OPF_EBP_S  = (1 << 11), /* ebp used as scratch, not BP */
 };
 
 enum op_op {
@@ -120,6 +121,7 @@ struct parsed_opr {
   enum opr_lenmod lmod;
   unsigned int is_ptr:1;   // pointer in C
   unsigned int is_array:1; // array in C
+  unsigned int type_from_var:1; // .. in header, sometimes wrong
   unsigned int size_mismatch:1; // type override differs from C
   unsigned int size_lt:1;  // type override is larger than C
   int reg;
@@ -195,6 +197,7 @@ static char g_func[256];
 static char g_comment[256];
 static int g_bp_frame;
 static int g_sp_frame;
+static int g_stack_frame_used;
 static int g_stack_fsz;
 static int g_ida_func_attr;
 #define ferr(op_, fmt, ...) do { \
@@ -468,8 +471,9 @@ static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
   const struct parsed_type *c_type)
 {
   static const char *dword_types[] = {
-    "int", "_DWORD", "DWORD", "HANDLE", "HWND", "HMODULE",
-    "WPARAM", "UINT",
+    "int", "_DWORD", "UINT_PTR",
+    "DWORD", "HANDLE", "HWND", "HMODULE",
+    "WPARAM", "LPARAM", "UINT",
   };
   static const char *word_types[] = {
     "uint16_t", "int16_t",
@@ -478,6 +482,7 @@ static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
   static const char *byte_types[] = {
     "uint8_t", "int8_t", "char",
     "unsigned __int8", "__int8", "BYTE",
+    "_UNKNOWN",
   };
   const char *n;
   int i;
@@ -680,12 +685,15 @@ static int parse_operand(struct parsed_opr *opr,
       opr->is_ptr = 1;
     }
     else {
+      tmplmod = OPLM_UNSPEC;
       if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
         anote("unhandled C type '%s' for '%s'\n",
           pp->type.name, opr->name);
       
-      if (opr->lmod == OPLM_UNSPEC)
+      if (opr->lmod == OPLM_UNSPEC) {
         opr->lmod = tmplmod;
+        opr->type_from_var = 1;
+      }
       else if (opr->lmod != tmplmod) {
         opr->size_mismatch = 1;
         if (tmplmod < opr->lmod)
@@ -759,7 +767,7 @@ static const struct {
   { "idiv", OP_IDIV,   1, 1, OPF_DATA|OPF_FLAGS },
   { "test", OP_TEST,   2, 2, OPF_FLAGS },
   { "cmp",  OP_CMP,    2, 2, OPF_FLAGS },
-  { "retn", OP_RET,    0, 1, OPF_JMP|OPF_TAIL },
+  { "retn", OP_RET,    0, 1, OPF_TAIL },
   { "call", OP_CALL,   1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
   { "jmp",  OP_JMP,    1, 1, OPF_JMP },
   { "jo",   OP_JO,     1, 1, OPF_JMP|OPF_CC }, // 70 OF=1
@@ -1135,6 +1143,46 @@ static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
   return regs_r32[popr->reg];
 }
 
+// cast1 is the "final" cast
+static const char *simplify_cast(const char *cast1, const char *cast2)
+{
+  static char buf[256];
+
+  if (cast1[0] == 0)
+    return cast2;
+  if (cast2[0] == 0)
+    return cast1;
+  if (IS(cast1, cast2))
+    return cast1;
+  if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
+    return cast1;
+  if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
+    return cast1;
+  if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
+    return cast2;
+  if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
+    return cast2;
+
+  snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
+  return buf;
+}
+
+static const char *simplify_cast_num(const char *cast, unsigned int val)
+{
+  if (IS(cast, "(u8)") && val < 0x100)
+    return "";
+  if (IS(cast, "(s8)") && val < 0x80)
+    return "";
+  if (IS(cast, "(u16)") && val < 0x10000)
+    return "";
+  if (IS(cast, "(s16)") && val < 0x8000)
+    return "";
+  if (IS(cast, "(s32)") && val < 0x80000000)
+    return "";
+
+  return cast;
+}
+
 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
   int *extra_offs)
 {
@@ -1184,12 +1232,28 @@ static void stack_frame_access(struct parsed_op *po,
   const char *p;
   char *endp = NULL;
   int i, arg_i, arg_s;
+  int unaligned = 0;
   int stack_ra = 0;
   int offset = 0;
   int sf_ofs;
   int lim;
 
-  if (!IS_START(name, "ebp-")) {
+  if (po->flags & OPF_EBP_S)
+    ferr(po, "stack_frame_access while ebp is scratch\n");
+
+  if (IS_START(name, "ebp-")
+   || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
+  {
+    p = name + 4;
+    if (IS_START(p, "0x"))
+      p += 2;
+    offset = strtoul(p, &endp, 16);
+    if (name[3] == '-')
+      offset = -offset;
+    if (*endp != 0)
+      ferr(po, "ebp- parse of '%s' failed\n", name);
+  }
+  else {
     bp_arg = parse_stack_el(name, ofs_reg);
     snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
     eq = equ_find(po, bp_arg, &offset);
@@ -1197,19 +1261,11 @@ static void stack_frame_access(struct parsed_op *po,
       ferr(po, "detected but missing eq\n");
     offset += eq->offset;
   }
-  else {
-    p = name + 4;
-    if (IS_START(p, "0x"))
-      p += 2;
-    offset = -strtoul(p, &endp, 16);
-    if (*endp != 0)
-      ferr(po, "ebp- parse of '%s' failed\n", name);
-  }
 
   if (!strncmp(name, "ebp", 3))
     stack_ra = 4;
 
-  if (stack_ra <= offset && offset < stack_ra + 4)
+  if (ofs_reg[0] == 0 && stack_ra <= offset && offset < stack_ra + 4)
     ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
 
   if (offset > stack_ra)
@@ -1250,21 +1306,33 @@ static void stack_frame_access(struct parsed_op *po,
       if (is_lea)
         ferr(po, "lea/byte to arg?\n");
       if (is_src && (offset & 3) == 0)
-        snprintf(buf, buf_size, "(u8)a%d", i + 1);
+        snprintf(buf, buf_size, "%sa%d",
+          simplify_cast(cast, "(u8)"), i + 1);
       else
-        snprintf(buf, buf_size, "BYTE%d(a%d)", offset & 3, i + 1);
+        snprintf(buf, buf_size, "%sBYTE%d(a%d)",
+          cast, offset & 3, i + 1);
       break;
 
     case OPLM_WORD:
       if (is_lea)
         ferr(po, "lea/word to arg?\n");
-      if (offset & 1)
-        ferr(po, "unaligned arg access\n");
-      if (is_src && (offset & 2) == 0)
-        snprintf(buf, buf_size, "(u16)a%d", i + 1);
+      if (offset & 1) {
+        unaligned = 1;
+        if (!is_src) {
+          if (offset & 2)
+            ferr(po, "problematic arg store\n");
+          snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
+            simplify_cast(cast, "*(u16 *)"), i + 1);
+        }
+        else
+          ferr(po, "unaligned arg word load\n");
+      }
+      else if (is_src && (offset & 2) == 0)
+        snprintf(buf, buf_size, "%sa%d",
+          simplify_cast(cast, "(u16)"), i + 1);
       else
-        snprintf(buf, buf_size, "%sWORD(a%d)",
-          (offset & 2) ? "HI" : "LO", i + 1);
+        snprintf(buf, buf_size, "%s%sWORD(a%d)",
+          cast, (offset & 2) ? "HI" : "LO", i + 1);
       break;
 
     case OPLM_DWORD:
@@ -1272,14 +1340,19 @@ static void stack_frame_access(struct parsed_op *po,
         prefix = cast;
       else if (is_src)
         prefix = "(u32)";
+
       if (offset & 3) {
-        snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
+        unaligned = 1;
         if (is_lea)
           snprintf(buf, buf_size, "(u32)&a%d + %d",
             i + 1, offset & 3);
-        else
+        else if (!is_src)
+          ferr(po, "unaligned arg store\n");
+        else {
+          // mov edx, [ebp+arg_4+2]; movsx ecx, dx
           snprintf(buf, buf_size, "%s(a%d >> %d)",
             prefix, i + 1, (offset & 3) * 8);
+        }
       }
       else {
         snprintf(buf, buf_size, "%s%sa%d",
@@ -1291,16 +1364,23 @@ static void stack_frame_access(struct parsed_op *po,
       ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
     }
 
+    if (unaligned)
+      snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
+
     // common problem
     guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
-    if ((offset & 3) && tmp_lmod != OPLM_DWORD)
-      ferr(po, "bp_arg arg/w offset %d and type '%s'\n",
-        offset, g_func_pp->arg[i].type.name);
+    if (tmp_lmod != OPLM_DWORD
+      && (unaligned || (!is_src && tmp_lmod < popr->lmod)))
+    {
+      ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
+        i + 1, offset, g_func_pp->arg[i].type.name);
+    }
   }
   else
   {
     if (g_stack_fsz == 0)
       ferr(po, "stack var access without stackframe\n");
+    g_stack_frame_used = 1;
 
     sf_ofs = g_stack_fsz + offset;
     lim = (ofs_reg[0] != 0) ? -4 : 0;
@@ -1353,8 +1433,15 @@ static void stack_frame_access(struct parsed_op *po,
 
 static void check_label_read_ref(struct parsed_op *po, const char *name)
 {
-  if (IS_START(name, "sub_"))
-    ferr(po, "func reference?\n");
+  const struct parsed_proto *pp;
+
+  pp = proto_parse(g_fhdr, name);
+  if (pp == NULL)
+    ferr(po, "proto_parse failed for ref '%s'\n", name);
+
+  // currently we can take __cdecl and __stdcall
+  if (pp->is_func && pp->argc_reg != 0)
+    ferr(po, "reg-arg func reference?\n");
 }
 
 static char *out_src_opr(char *buf, size_t buf_size,
@@ -1363,6 +1450,7 @@ static char *out_src_opr(char *buf, size_t buf_size,
 {
   char tmp1[256], tmp2[256];
   char expr[256];
+  char *p;
   int ret;
 
   if (cast == NULL)
@@ -1378,13 +1466,16 @@ static char *out_src_opr(char *buf, size_t buf_size,
       snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
       break;
     case OPLM_WORD:
-      snprintf(buf, buf_size, "(u16)%s", opr_reg_p(po, popr));
+      snprintf(buf, buf_size, "%s%s",
+        simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
       break;
     case OPLM_BYTE:
       if (popr->name[1] == 'h') // XXX..
-        snprintf(buf, buf_size, "(u8)(%s >> 8)", opr_reg_p(po, popr));
+        snprintf(buf, buf_size, "%s(%s >> 8)",
+          simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
       else
-        snprintf(buf, buf_size, "(u8)%s", opr_reg_p(po, popr));
+        snprintf(buf, buf_size, "%s%s",
+          simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
       break;
     default:
       ferr(po, "invalid src lmod: %d\n", popr->lmod);
@@ -1393,7 +1484,8 @@ static char *out_src_opr(char *buf, size_t buf_size,
 
   case OPT_REGMEM:
     if (parse_stack_el(popr->name, NULL)
-      || (g_bp_frame && IS_START(popr->name, "ebp-")))
+      || (g_bp_frame && !(po->flags & OPF_EBP_S)
+          && IS_START(popr->name, "ebp")))
     {
       stack_frame_access(po, popr, buf, buf_size,
         popr->name, cast, 1, is_lea);
@@ -1406,6 +1498,14 @@ static char *out_src_opr(char *buf, size_t buf_size,
       ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
       if (ret != 2)
         ferr(po, "parse failure for '%s'\n", expr);
+      if (tmp1[0] == '(') {
+        // (off_4FFF50+3)[eax]
+        p = strchr(tmp1 + 1, ')');
+        if (p == NULL || p[1] != 0)
+          ferr(po, "parse failure (2) for '%s'\n", expr);
+        *p = 0;
+        memmove(tmp1, tmp1 + 1, strlen(tmp1));
+      }
       snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
     }
 
@@ -1415,9 +1515,8 @@ static char *out_src_opr(char *buf, size_t buf_size,
       break;
     }
 
-    if (cast[0] == 0)
-      cast = lmod_cast_u_ptr(po, popr->lmod);
-    snprintf(buf, buf_size, "%s(%s)", cast, expr);
+    snprintf(buf, buf_size, "%s(%s)",
+      simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
     break;
 
   case OPT_LABEL:
@@ -1450,9 +1549,9 @@ static char *out_src_opr(char *buf, size_t buf_size,
     if (is_lea)
       ferr(po, "lea from const?\n");
 
-    snprintf(buf, buf_size, "%s", cast);
-    ret = strlen(buf);
-    printf_number(buf + ret, buf_size - ret, popr->val);
+    printf_number(tmp1, sizeof(tmp1), popr->val);
+    snprintf(buf, buf_size, "%s%s",
+      simplify_cast_num(cast, popr->val), tmp1);
     break;
 
   default:
@@ -1490,7 +1589,8 @@ static char *out_dst_opr(char *buf, size_t buf_size,
 
   case OPT_REGMEM:
     if (parse_stack_el(popr->name, NULL)
-      || (g_bp_frame && IS_START(popr->name, "ebp-")))
+      || (g_bp_frame && !(po->flags & OPF_EBP_S)
+          && IS_START(popr->name, "ebp")))
     {
       stack_frame_access(po, popr, buf, buf_size,
         popr->name, "", 0, 0);
@@ -1613,50 +1713,85 @@ static void out_test_for_cc(char *buf, size_t buf_size,
 }
 
 static void out_cmp_for_cc(char *buf, size_t buf_size,
-  struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
-  enum opr_lenmod lmod, const char *expr1, const char *expr2)
+  struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
 {
-  const char *cast, *scast;
+  const char *cast, *scast, *cast_use;
+  char buf1[256], buf2[256];
+  enum opr_lenmod lmod;
+
+  if (po->operand[0].lmod != po->operand[1].lmod)
+    ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
+      po->operand[0].lmod, po->operand[1].lmod);
+  lmod = po->operand[0].lmod;
 
   cast = lmod_cast_u(po, lmod);
   scast = lmod_cast_s(po, lmod);
 
+  switch (pfo) {
+  case PFO_C:
+  case PFO_Z:
+  case PFO_BE: // !a
+    cast_use = cast;
+    break;
+
+  case PFO_S:
+  case PFO_L: // !ge
+  case PFO_LE:
+    cast_use = scast;
+    break;
+
+  default:
+    ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
+  }
+
+  out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
+  out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
+
   switch (pfo) {
   case PFO_C:
     // note: must be unsigned compare
-    snprintf(buf, buf_size, "(%s%s %s %s%s)",
-      cast, expr1, is_inv ? ">=" : "<", cast, expr2);
+    snprintf(buf, buf_size, "(%s %s %s)",
+      buf1, is_inv ? ">=" : "<", buf2);
     break;
 
   case PFO_Z:
-    snprintf(buf, buf_size, "(%s%s %s %s%s)",
-      cast, expr1, is_inv ? "!=" : "==", cast, expr2);
+    snprintf(buf, buf_size, "(%s %s %s)",
+      buf1, is_inv ? "!=" : "==", buf2);
     break;
 
   case PFO_BE: // !a
     // note: must be unsigned compare
-    snprintf(buf, buf_size, "(%s%s %s %s%s)",
-      cast, expr1, is_inv ? ">" : "<=", cast, expr2);
+    snprintf(buf, buf_size, "(%s %s %s)",
+      buf1, is_inv ? ">" : "<=", buf2);
+
+    // annoying case
+    if (is_inv && lmod == OPLM_BYTE
+      && po->operand[1].type == OPT_CONST
+      && po->operand[1].val == 0xff)
+    {
+      snprintf(g_comment, sizeof(g_comment), "if %s", buf);
+      snprintf(buf, buf_size, "(0)");
+    }
     break;
 
   // note: must be signed compare
   case PFO_S:
     snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
-      scast, expr1, expr2, is_inv ? ">=" : "<");
+      scast, buf1, buf2, is_inv ? ">=" : "<");
     break;
 
   case PFO_L: // !ge
-    snprintf(buf, buf_size, "(%s%s %s %s%s)",
-      scast, expr1, is_inv ? ">=" : "<", scast, expr2);
+    snprintf(buf, buf_size, "(%s %s %s)",
+      buf1, is_inv ? ">=" : "<", buf2);
     break;
 
   case PFO_LE:
-    snprintf(buf, buf_size, "(%s%s %s %s%s)",
-      scast, expr1, is_inv ? ">" : "<=", scast, expr2);
+    snprintf(buf, buf_size, "(%s %s %s)",
+      buf1, is_inv ? ">" : "<=", buf2);
     break;
 
   default:
-    ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
+    break;
   }
 }
 
@@ -1678,10 +1813,7 @@ static void out_cmp_test(char *buf, size_t buf_size,
       po->operand[0].lmod, buf3);
   }
   else if (po->op == OP_CMP) {
-    out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
-    out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[1]);
-    out_cmp_for_cc(buf, buf_size, po, pfo, is_inv,
-      po->operand[0].lmod, buf2, buf3);
+    out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
   }
   else
     ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
@@ -1697,8 +1829,23 @@ static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
     popr1->lmod = popr2->lmod;
   else if (popr2->lmod == OPLM_UNSPEC)
     popr2->lmod = popr1->lmod;
-  else if (popr1->lmod != popr2->lmod)
-    ferr(po, "conflicting lmods: %d vs %d\n", popr1->lmod, popr2->lmod);
+  else if (popr1->lmod != popr2->lmod) {
+    if (popr1->type_from_var) {
+      popr1->size_mismatch = 1;
+      if (popr1->lmod < popr2->lmod)
+        popr1->size_lt = 1;
+      popr1->lmod = popr2->lmod;
+    }
+    else if (popr2->type_from_var) {
+      popr2->size_mismatch = 1;
+      if (popr2->lmod < popr1->lmod)
+        popr2->size_lt = 1;
+      popr2->lmod = popr1->lmod;
+    }
+    else
+      ferr(po, "conflicting lmods: %d vs %d\n",
+        popr1->lmod, popr2->lmod);
+  }
 }
 
 static const char *op_to_c(struct parsed_op *po)
@@ -1729,15 +1876,11 @@ static const char *op_to_c(struct parsed_op *po)
   }
 }
 
-static void set_flag_no_dup(struct parsed_op *po, enum op_flags flag,
-  enum op_flags flag_check)
+static void op_set_clear_flag(struct parsed_op *po,
+  enum op_flags flag_set, enum op_flags flag_clear)
 {
-  if (po->flags & flag)
-    ferr(po, "flag %x already set\n", flag);
-  if (po->flags & flag_check)
-    ferr(po, "flag_check %x already set\n", flag_check);
-
-  po->flags |= flag;
+  po->flags |= flag_set;
+  po->flags &= ~flag_clear;
 }
 
 // last op in stream - unconditional branch or ret
@@ -1804,11 +1947,11 @@ static int scan_for_pop(int i, int opcnt, const char *reg,
         if (depth > *maxdepth)
           *maxdepth = depth;
         if (do_flags)
-          set_flag_no_dup(po, OPF_RSAVE, OPF_RMD);
+          op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
       }
       else if (depth == 0) {
         if (do_flags)
-          set_flag_no_dup(po, OPF_RMD, OPF_RSAVE);
+          op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
         return 1;
       }
       else {
@@ -1816,7 +1959,7 @@ static int scan_for_pop(int i, int opcnt, const char *reg,
         if (depth < 0) // should not happen
           ferr(po, "fail with depth\n");
         if (do_flags)
-          set_flag_no_dup(po, OPF_RSAVE, OPF_RMD);
+          op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
       }
     }
   }
@@ -1929,22 +2072,36 @@ static int scan_for_mod_opr0(struct parsed_op *po_test,
   return -1;
 }
 
-static int scan_for_flag_set(int i, int *branched, int *setters,
-  int *setter_cnt)
+static int scan_for_flag_set(int i, int magic, int *branched,
+  int *setters, int *setter_cnt)
 {
+  struct label_ref *lr;
   int ret;
 
   while (i >= 0) {
+    if (ops[i].cc_scratch == magic) {
+      ferr(&ops[i], "%s looped\n", __func__);
+      return -1;
+    }
+    ops[i].cc_scratch = magic;
+
     if (g_labels[i][0] != 0) {
       *branched = 1;
-      if (g_label_refs[i].next != NULL)
-        return -1;
+
+      lr = &g_label_refs[i];
+      for (; lr->next; lr = lr->next) {
+        ret = scan_for_flag_set(lr->i, magic,
+                branched, setters, setter_cnt);
+        if (ret < 0)
+          return ret;
+      }
+
       if (i > 0 && LAST_OP(i - 1)) {
-        i = g_label_refs[i].i;
+        i = lr->i;
         continue;
       }
-      ret = scan_for_flag_set(g_label_refs[i].i, branched,
-              setters, setter_cnt);
+      ret = scan_for_flag_set(lr->i, magic,
+              branched, setters, setter_cnt);
       if (ret < 0)
         return ret;
     }
@@ -1966,7 +2123,18 @@ static int scan_for_flag_set(int i, int *branched, int *setters,
 // scan back for cdq, if anything modifies edx, fail
 static int scan_for_cdq_edx(int i)
 {
-  for (; i >= 0; i--) {
+  while (i >= 0) {
+    if (g_labels[i][0] != 0) {
+      if (g_label_refs[i].next != NULL)
+        return -1;
+      if (i > 0 && LAST_OP(i - 1)) {
+        i = g_label_refs[i].i;
+        continue;
+      }
+      return -1;
+    }
+    i--;
+
     if (ops[i].op == OP_CDQ)
       return i;
 
@@ -1981,7 +2149,18 @@ static int scan_for_cdq_edx(int i)
 
 static int scan_for_reg_clear(int i, int reg)
 {
-  for (; i >= 0; i--) {
+  while (i >= 0) {
+    if (g_labels[i][0] != 0) {
+      if (g_label_refs[i].next != NULL)
+        return -1;
+      if (i > 0 && LAST_OP(i - 1)) {
+        i = g_label_refs[i].i;
+        continue;
+      }
+      return -1;
+    }
+    i--;
+
     if (ops[i].op == OP_XOR
      && ops[i].operand[0].lmod == OPLM_DWORD
      && ops[i].operand[0].reg == ops[i].operand[1].reg
@@ -2001,11 +2180,15 @@ static int scan_for_reg_clear(int i, int reg)
 static int scan_for_esp_adjust(int i, int opcnt, int *adj)
 {
   struct parsed_op *po;
+  int i_first = i;
   *adj = 0;
 
   for (; i < opcnt; i++) {
     po = &ops[i];
 
+    if (g_labels[i][0] != 0)
+      break;
+
     if (po->op == OP_ADD && po->operand[0].reg == xSP) {
       if (po->operand[1].type != OPT_CONST)
         ferr(&ops[i], "non-const esp adjust?\n");
@@ -2020,22 +2203,59 @@ static int scan_for_esp_adjust(int i, int opcnt, int *adj)
       *adj += lmod_bytes(po, po->operand[0].lmod);
     else if (po->flags & (OPF_JMP|OPF_TAIL)) {
       if (po->op != OP_CALL)
-        return -1;
+        break;
       if (po->operand[0].type != OPT_LABEL)
-        return -1;
+        break;
       // TODO: should only allow combining __cdecl calls..
     }
+  }
 
-    if (g_labels[i][0] != 0)
-      return -1;
+  if (*adj == 4 && ops[i_first].op == OP_POP
+    && ops[i_first].operand[0].type == OPT_REG
+    && ops[i_first].operand[0].reg == xCX)
+  {
+    // probably 'pop ecx' was used..
+    return i_first;
   }
 
   return -1;
 }
 
+static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
+{
+  struct parsed_op *po;
+  int j;
+
+  if (i < 0)
+    ferr(ops, "%s: followed bad branch?\n", __func__);
+
+  for (; i < opcnt; i++) {
+    po = &ops[i];
+    if (po->cc_scratch == magic)
+      return;
+    po->cc_scratch = magic;
+    po->flags |= flags;
+
+    if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
+      if (po->btj != NULL) {
+        // jumptable
+        for (j = 0; j < po->btj->count; j++)
+          scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
+        return;
+      }
+
+      scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
+      if (!(po->flags & OPF_CC))
+        return;
+    }
+    if (po->flags & OPF_TAIL)
+      return;
+  }
+}
+
 static int collect_call_args(struct parsed_op *po, int i,
   struct parsed_proto *pp, int *save_arg_vars, int arg,
-  int need_op_saving, int may_reuse)
+  int magic, int need_op_saving, int may_reuse)
 {
   struct parsed_proto *pp_tmp;
   struct label_ref *lr;
@@ -2043,24 +2263,40 @@ static int collect_call_args(struct parsed_op *po, int i,
   int ret = 0;
   int j;
 
-  if (i < 0)
-    ferr(po, "no refs for '%s'?\n", g_labels[i]);
+  if (i < 0) {
+    ferr(po, "dead label encountered\n");
+    return -1;
+  }
 
   for (; arg < pp->argc; arg++)
     if (pp->arg[arg].reg == NULL)
       break;
+  magic = (magic & 0xffffff) | (arg << 24);
 
   for (j = i; j >= 0 && arg < pp->argc; )
   {
-    if (g_labels[j][0] != 0) {
+    if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
+      if (ops[j].cc_scratch != magic) {
+        ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
+           pp->name);
+        return -1;
+      }
+      // ok: have already been here
+      return 0;
+    }
+    ops[j].cc_scratch = magic;
+
+    if (g_labels[j][0] != 0 && g_label_refs[j].i != -1) {
       lr = &g_label_refs[j];
       if (lr->next != NULL)
         need_op_saving = 1;
       for (; lr->next; lr = lr->next) {
         if ((ops[lr->i].flags & (OPF_JMP|OPF_CC)) != OPF_JMP)
           may_reuse = 1;
-        ret |= collect_call_args(po, lr->i, pp, save_arg_vars,
-                 arg, need_op_saving, may_reuse);
+        ret = collect_call_args(po, lr->i, pp, save_arg_vars,
+                arg, magic, need_op_saving, may_reuse);
+        if (ret < 0)
+          return ret;
       }
 
       if ((ops[lr->i].flags & (OPF_JMP|OPF_CC)) != OPF_JMP)
@@ -2071,8 +2307,10 @@ static int collect_call_args(struct parsed_op *po, int i,
         continue;
       }
       need_op_saving = 1;
-      ret |= collect_call_args(po, lr->i, pp, save_arg_vars,
-               arg, need_op_saving, may_reuse);
+      ret = collect_call_args(po, lr->i, pp, save_arg_vars,
+               arg, magic, need_op_saving, may_reuse);
+      if (ret < 0)
+        return ret;
     }
     j--;
 
@@ -2080,7 +2318,8 @@ static int collect_call_args(struct parsed_op *po, int i,
     {
       pp_tmp = ops[j].datap;
       if (pp_tmp == NULL)
-        ferr(po, "arg collect hit unparsed call\n");
+        ferr(po, "arg collect hit unparsed call '%s'\n",
+          ops[j].operand[0].name);
       if (may_reuse && pp_tmp->argc_stack > 0)
         ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
           arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
@@ -2128,6 +2367,7 @@ static int collect_call_args(struct parsed_op *po, int i,
       for (arg++; arg < pp->argc; arg++)
         if (pp->arg[arg].reg == NULL)
           break;
+      magic = (magic & 0xffffff) | (arg << 24);
     }
   }
 
@@ -2154,6 +2394,21 @@ static void add_label_ref(struct label_ref *lr, int op_i)
   lr->next = lr_new;
 }
 
+static void output_std_flags(FILE *fout, struct parsed_op *po,
+  int *pfomask, const char *dst_opr_text)
+{
+  if (*pfomask & (1 << PFO_Z)) {
+    fprintf(fout, "\n  cond_z = (%s%s == 0);",
+      lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
+    *pfomask &= ~(1 << PFO_Z);
+  }
+  if (*pfomask & (1 << PFO_S)) {
+    fprintf(fout, "\n  cond_s = (%s%s < 0);",
+      lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
+    *pfomask &= ~(1 << PFO_S);
+  }
+}
+
 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
 {
   struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
@@ -2167,10 +2422,13 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
   int save_arg_vars = 0;
   int cmp_result_vars = 0;
   int need_mul_var = 0;
+  int have_func_ret = 0;
+  int have_normal_ret = 0;
   int had_decl = 0;
   int label_pending = 0;
   int regmask_save = 0;
   int regmask_arg = 0;
+  int regmask_now = 0;
   int regmask = 0;
   int pfomask = 0;
   int found = 0;
@@ -2183,25 +2441,48 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
   int ret;
 
   g_bp_frame = g_sp_frame = g_stack_fsz = 0;
+  g_stack_frame_used = 0;
 
   g_func_pp = proto_parse(fhdr, funcn);
   if (g_func_pp == NULL)
     ferr(ops, "proto_parse failed for '%s'\n", funcn);
 
   fprintf(fout, "%s ", g_func_pp->ret_type.name);
+  if (g_func_pp->is_stdcall && g_func_pp->argc_reg == 0)
+    fprintf(fout, "__stdcall ");
   if (g_ida_func_attr & IDAFA_NORETURN)
     fprintf(fout, "noreturn ");
   fprintf(fout, "%s(", funcn);
+
   for (i = 0; i < g_func_pp->argc; i++) {
     if (i > 0)
       fprintf(fout, ", ");
-    fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1);
+    if (g_func_pp->arg[i].fptr != NULL) {
+      // func pointer..
+      pp = g_func_pp->arg[i].fptr;
+      fprintf(fout, "%s (", pp->ret_type.name);
+      if (pp->is_stdcall && pp->argc_reg == 0)
+        fprintf(fout, "__stdcall ");
+      fprintf(fout, "*a%d)(", i + 1);
+      for (j = 0; j < pp->argc; j++) {
+        if (j > 0)
+          fprintf(fout, ", ");
+        if (pp->arg[j].fptr)
+          ferr(ops, "nested fptr\n");
+        fprintf(fout, "%s", pp->arg[j].type.name);
+      }
+      fprintf(fout, ")");
+    }
+    else {
+      fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1);
+    }
   }
   if (g_func_pp->is_vararg) {
     if (i > 0)
       fprintf(fout, ", ");
     fprintf(fout, "...");
   }
+
   fprintf(fout, ")\n{\n");
 
   // pass1:
@@ -2315,6 +2596,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
   }
 
   // pass2:
+  // - parse calls with labels
   // - resolve all branches
   for (i = 0; i < opcnt; i++)
   {
@@ -2322,8 +2604,25 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
     po->bt_i = -1;
     po->btj = NULL;
 
-    if ((po->flags & OPF_RMD) || !(po->flags & OPF_JMP)
-        || po->op == OP_CALL || po->op == OP_RET)
+    if (po->flags & OPF_RMD)
+      continue;
+
+    if (po->op == OP_CALL) {
+      if (po->operand[0].type == OPT_LABEL) {
+        tmpname = opr_name(po, 0);
+        pp_c = proto_parse(fhdr, tmpname);
+        if (pp_c == NULL)
+          ferr(po, "proto_parse failed for call '%s'\n", tmpname);
+        if (pp_c->is_fptr && pp_c->argc_reg != 0)
+          ferr(po, "fptr call with reg arg\n");
+        pp = proto_clone(pp_c);
+        my_assert_not(pp, NULL);
+        po->datap = pp;
+      }
+      continue;
+    }
+
+    if (!(po->flags & OPF_JMP) || po->op == OP_RET)
       continue;
 
     if (po->operand[0].type == OPT_REGMEM) {
@@ -2341,7 +2640,8 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         }
       }
       if (pd == NULL)
-        ferr(po, "label '%s' not parsed?\n", buf1);
+        //ferr(po, "label '%s' not parsed?\n", buf1);
+        goto tailcall;
       if (pd->type != OPT_OFFSET)
         ferr(po, "label '%s' with non-offset data?\n", buf1);
 
@@ -2371,31 +2671,39 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
     if (po->bt_i != -1)
       continue;
 
-    if (po->operand[0].type == OPT_LABEL) {
+    if (po->operand[0].type == OPT_LABEL)
       // assume tail call
-      po->op = OP_CALL;
-      po->flags |= OPF_TAIL;
-      continue;
-    }
+      goto tailcall;
 
     ferr(po, "unhandled branch\n");
+
+tailcall:
+    po->op = OP_CALL;
+    po->flags |= OPF_TAIL;
+    i--; // reprocess
   }
 
   // pass3:
+  // - remove dead labels
   // - process calls
   for (i = 0; i < opcnt; i++)
   {
+    if (g_labels[i][0] != 0 && g_label_refs[i].i == -1)
+      g_labels[i][0] = 0;
+
     po = &ops[i];
     if (po->flags & OPF_RMD)
       continue;
 
     if (po->op == OP_CALL)
     {
-      pp = calloc(1, sizeof(*pp));
-      my_assert_not(pp, NULL);
       tmpname = opr_name(po, 0);
-      if (po->operand[0].type != OPT_LABEL)
+      pp = po->datap;
+      if (pp == NULL)
       {
+        // indirect call
+        pp = calloc(1, sizeof(*pp));
+        my_assert_not(pp, NULL);
         ret = scan_for_esp_adjust(i + 1, opcnt, &j);
         if (ret < 0)
           ferr(po, "non-__cdecl indirect call unhandled yet\n");
@@ -2406,12 +2714,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         pp->argc = pp->argc_stack = j;
         for (arg = 0; arg < pp->argc; arg++)
           pp->arg[arg].type.name = strdup("int");
-      }
-      else {
-        pp_c = proto_parse(fhdr, tmpname);
-        if (pp_c == NULL)
-          ferr(po, "proto_parse failed for call '%s'\n", tmpname);
-        pp = proto_clone(pp_c);
+        po->datap = pp;
       }
 
       // look for and make use of esp adjust
@@ -2455,11 +2758,13 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         }
       }
 
-      collect_call_args(po, i, pp, &save_arg_vars, 0, 0, 0);
+      collect_call_args(po, i, pp, &save_arg_vars,
+        0, i + opcnt * 2, 0, 0);
 
       if (strstr(pp->ret_type.name, "int64"))
         need_mul_var = 1;
-      po->datap = pp;
+      if (!(po->flags & OPF_TAIL) && !IS(pp->ret_type.name, "void"))
+        have_func_ret = 1;
     }
   }
 
@@ -2473,6 +2778,15 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
     if (po->flags & OPF_RMD)
       continue;
 
+    if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
+      reg = po->operand[0].reg;
+      if (!(regmask & (1 << reg)))
+        // not a reg save after all, rerun scan_for_pop
+        po->flags &= ~OPF_RSAVE;
+      else
+        regmask_save |= 1 << reg;
+    }
+
     if (po->op == OP_PUSH
         && po->argnum == 0 && !(po->flags & OPF_RSAVE)
         && po->operand[0].type == OPT_REG)
@@ -2483,16 +2797,14 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
 
       depth = 0;
       ret = scan_for_pop(i + 1, opcnt,
-              po->operand[0].name, i + opcnt, 0, &depth, 0);
+              po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
       if (ret == 1) {
         if (depth > 1)
           ferr(po, "too much depth: %d\n", depth);
-        if (depth > 0)
-          regmask_save |= 1 << reg;
 
         po->flags |= OPF_RMD;
         scan_for_pop(i + 1, opcnt, po->operand[0].name,
-          i + opcnt * 2, 0, &depth, 1);
+          i + opcnt * 4, 0, &depth, 1);
         continue;
       }
       ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
@@ -2509,13 +2821,25 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
       }
     }
 
-    regmask |= po->regmask_src | po->regmask_dst;
+    regmask_now = po->regmask_src | po->regmask_dst;
+    if (regmask_now & (1 << xBP)) {
+      if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
+        if (po->regmask_dst & (1 << xBP))
+          // compiler decided to drop bp frame and use ebp as scratch
+          scan_fwd_set_flags(i, opcnt, i + opcnt * 5, OPF_EBP_S);
+        else
+          regmask_now &= ~(1 << xBP);
+      }
+    }
+
+    regmask |= regmask_now;
 
     if (po->flags & OPF_CC)
     {
       int setters[16], cnt = 0, branched = 0;
 
-      ret = scan_for_flag_set(i, &branched, setters, &cnt);
+      ret = scan_for_flag_set(i, i + opcnt * 6,
+              &branched, setters, &cnt);
       if (ret < 0 || cnt <= 0)
         ferr(po, "unable to trace flag setter(s)\n");
       if (cnt > ARRAY_SIZE(setters))
@@ -2538,16 +2862,19 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
           pfomask = 1 << PFO_Z;
         }
         else {
-          if ((pfo != PFO_Z && pfo != PFO_S && pfo != PFO_P)
+          // see if we'll be able to handle based on op result
+          if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
+               && pfo != PFO_Z && pfo != PFO_S && pfo != PFO_P)
+              || branched
               || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
             pfomask = 1 << pfo;
         }
         if (pfomask) {
           tmp_op->pfomask |= pfomask;
           cmp_result_vars |= pfomask;
-          // note: may overwrite, currently not a problem
-          po->datap = tmp_op;
         }
+        // note: may overwrite, currently not a problem
+        po->datap = tmp_op;
       }
 
       if (po->op == OP_ADC || po->op == OP_SBB)
@@ -2569,6 +2896,8 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
       }
       fprintf(fout, ");\n");
     }
+    else if (po->op == OP_RET)
+      have_normal_ret = 1;
   }
 
   // output LUTs/jumptables
@@ -2622,6 +2951,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
 
   // declare other regs - special case for eax
   if (!((regmask | regmask_arg) & 1)
+   && (have_func_ret || have_normal_ret)
    && !IS(g_func_pp->ret_type.name, "void"))
   {
     fprintf(fout, "  u32 eax = 0;\n");
@@ -2630,8 +2960,6 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
 
   regmask &= ~regmask_arg;
   regmask &= ~(1 << xSP);
-  if (g_bp_frame)
-    regmask &= ~(1 << xBP);
   if (regmask) {
     for (reg = 0; reg < 8; reg++) {
       if (regmask & (1 << reg)) {
@@ -2685,7 +3013,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
   // output ops
   for (i = 0; i < opcnt; i++)
   {
-    if (g_labels[i][0] != 0 && g_label_refs[i].i != -1) {
+    if (g_labels[i][0] != 0) {
       fprintf(fout, "\n%s:\n", g_labels[i]);
       label_pending = 1;
 
@@ -2710,6 +3038,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
       int is_inv = 0;
 
       pfo = split_cond(po, po->op, &is_inv);
+      tmp_op = po->datap;
 
       // we go through all this trouble to avoid using parsed_flag_op,
       // which makes generated code much nicer
@@ -2719,17 +3048,18 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         is_delayed = 1;
       }
       else if (last_arith_dst != NULL
-        && (pfo == PFO_Z || pfo == PFO_S || pfo == PFO_P))
+        && (pfo == PFO_Z || pfo == PFO_S || pfo == PFO_P
+           || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
+           ))
       {
         out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
         out_test_for_cc(buf1, sizeof(buf1), po, pfo, is_inv,
           last_arith_dst->lmod, buf3);
         is_delayed = 1;
       }
-      else if (po->datap != NULL) {
+      else if (tmp_op != NULL) {
         // use preprocessed flag calc results
-        tmp_op = po->datap;
-        if (!tmp_op || !(tmp_op->pfomask & (1 << pfo)))
+        if (!(tmp_op->pfomask & (1 << pfo)))
           ferr(po, "not prepared for pfo %d\n", pfo);
 
         // note: is_inv was not yet applied
@@ -2798,10 +3128,10 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         default:
           ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
         }
-        fprintf(fout, "  %s = %s%s;",
+        fprintf(fout, "  %s = %s;",
             out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
-            buf3,
-            out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
+            out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
+              buf3, 0));
         break;
 
       case OP_NOT:
@@ -2892,14 +3222,44 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
       case OP_OR:
         propagate_lmod(po, &po->operand[0], &po->operand[1]);
         // fallthrough
-      case OP_SHL:
-      case OP_SHR:
       dualop_arith:
         assert_operand_cnt(2);
         fprintf(fout, "  %s %s= %s;",
             out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
             op_to_c(po),
             out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
+        output_std_flags(fout, po, &pfomask, buf1);
+        last_arith_dst = &po->operand[0];
+        delayed_flag_op = NULL;
+        break;
+
+      case OP_SHL:
+      case OP_SHR:
+        assert_operand_cnt(2);
+        out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+        if (pfomask & (1 << PFO_C)) {
+          if (po->operand[1].type == OPT_CONST) {
+            l = lmod_bytes(po, po->operand[0].lmod) * 8;
+            j = po->operand[1].val;
+            j %= l;
+            if (j != 0) {
+              if (po->op == OP_SHL)
+                j = l - j;
+              else
+                j -= 1;
+              fprintf(fout, "  cond_c = (%s & 0x%02x) ? 1 : 0;\n",
+                buf1, 1 << j);
+            }
+            else
+              ferr(po, "zero shift?\n");
+          }
+          else
+            ferr(po, "TODO\n");
+          pfomask &= ~(1 << PFO_C);
+        }
+        fprintf(fout, "  %s %s= %s;", buf1, op_to_c(po),
+            out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
+        output_std_flags(fout, po, &pfomask, buf1);
         last_arith_dst = &po->operand[0];
         delayed_flag_op = NULL;
         break;
@@ -2910,6 +3270,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         fprintf(fout, "  %s = %s%s >> %s;", buf1,
           lmod_cast_s(po, po->operand[0].lmod), buf1,
           out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
+        output_std_flags(fout, po, &pfomask, buf1);
         last_arith_dst = &po->operand[0];
         delayed_flag_op = NULL;
         break;
@@ -2929,6 +3290,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         }
         else
           ferr(po, "TODO\n");
+        output_std_flags(fout, po, &pfomask, buf1);
         last_arith_dst = &po->operand[0];
         delayed_flag_op = NULL;
         break;
@@ -2950,10 +3312,20 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
       case OP_SBB:
         assert_operand_cnt(2);
         propagate_lmod(po, &po->operand[0], &po->operand[1]);
-        fprintf(fout, "  %s %s= %s + cond_c;",
-            out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
-            op_to_c(po),
+        out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+        if (po->op == OP_SBB
+          && IS(po->operand[0].name, po->operand[1].name))
+        {
+          // avoid use of unitialized var
+          fprintf(fout, "  %s = -cond_c;", buf1);
+          // carry remains what it was
+          pfomask &= ~(1 << PFO_C);
+        }
+        else {
+          fprintf(fout, "  %s %s= %s + cond_c;", buf1, op_to_c(po),
             out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
+        }
+        output_std_flags(fout, po, &pfomask, buf1);
         last_arith_dst = &po->operand[0];
         delayed_flag_op = NULL;
         break;
@@ -2969,6 +3341,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
           strcpy(buf2, po->op == OP_INC ? "+" : "-");
           fprintf(fout, "  %s %s= 1;", buf1, buf2);
         }
+        output_std_flags(fout, po, &pfomask, buf1);
         last_arith_dst = &po->operand[0];
         delayed_flag_op = NULL;
         break;
@@ -3013,9 +3386,9 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
 
         // 32bit division is common, look for it
         if (po->op == OP_DIV)
-          ret = scan_for_reg_clear(i - 1, xDX);
+          ret = scan_for_reg_clear(i, xDX);
         else
-          ret = scan_for_cdq_edx(i - 1);
+          ret = scan_for_cdq_edx(i);
         if (ret >= 0) {
           out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
           strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
@@ -3093,9 +3466,11 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         }
         else if (!IS(pp->ret_type.name, "void")) {
           if (po->flags & OPF_TAIL) {
-            fprintf(fout, "return ");
-            if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
-              fprintf(fout, "(%s)", g_func_pp->ret_type.name);
+            if (!IS(g_func_pp->ret_type.name, "void")) {
+              fprintf(fout, "return ");
+              if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
+                fprintf(fout, "(%s)", g_func_pp->ret_type.name);
+            }
           }
           else {
             fprintf(fout, "eax = ");
@@ -3150,9 +3525,18 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
 
         if (po->flags & OPF_TAIL) {
           strcpy(g_comment, "tailcall");
-          if (IS(pp->ret_type.name, "void")
-           && !(g_ida_func_attr & IDAFA_NORETURN))
-          {
+          ret = 0;
+          if (i == opcnt - 1)
+            ret = 0;
+          else if (IS(pp->ret_type.name, "void"))
+            ret = 1;
+          else if (IS(g_func_pp->ret_type.name, "void"))
+            ret = 1;
+          // else already handled as 'return f()'
+
+          if (ret) {
+            if (!IS(g_func_pp->ret_type.name, "void"))
+              ferr(po, "int func -> void func tailcall?\n");
             fprintf(fout, "\n  return;");
             strcpy(g_comment, "^ tailcall");
           }
@@ -3249,6 +3633,9 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
     label_pending = 0;
   }
 
+  if (g_stack_fsz && !g_stack_frame_used)
+    fprintf(fout, "  (void)sf;\n");
+
   fprintf(fout, "}\n\n");
 
   // cleanup
@@ -3317,6 +3704,24 @@ struct chunk_item {
   int asmln;
 };
 
+static struct chunk_item *func_chunks;
+static int func_chunk_cnt;
+static int func_chunk_alloc;
+
+static void add_func_chunk(FILE *fasm, const char *name, int line)
+{
+  if (func_chunk_cnt >= func_chunk_alloc) {
+    func_chunk_alloc *= 2;
+    func_chunks = realloc(func_chunks,
+      func_chunk_alloc * sizeof(func_chunks[0]));
+    my_assert_not(func_chunks, NULL);
+  }
+  func_chunks[func_chunk_cnt].fptr = ftell(fasm);
+  func_chunks[func_chunk_cnt].name = strdup(name);
+  func_chunks[func_chunk_cnt].asmln = line;
+  func_chunk_cnt++;
+}
+
 static int cmp_chunks(const void *p1, const void *p2)
 {
   const struct chunk_item *c1 = p1, *c2 = p2;
@@ -3328,6 +3733,64 @@ static int cmpstringp(const void *p1, const void *p2)
   return strcmp(*(char * const *)p1, *(char * const *)p2);
 }
 
+static void scan_ahead(FILE *fasm)
+{
+  char words[2][256];
+  char line[256];
+  long oldpos;
+  int oldasmln;
+  int wordc;
+  char *p;
+  int i;
+
+  oldpos = ftell(fasm);
+  oldasmln = asmln;
+
+  while (fgets(line, sizeof(line), fasm))
+  {
+    wordc = 0;
+    asmln++;
+
+    p = sskip(line);
+    if (*p == 0)
+      continue;
+
+    if (*p == ';')
+    {
+      // get rid of random tabs
+      for (i = 0; line[i] != 0; i++)
+        if (line[i] == '\t')
+          line[i] = ' ';
+
+      if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
+      {
+        p += 30;
+        next_word(words[0], sizeof(words[0]), p);
+        if (words[0][0] == 0)
+          aerr("missing name for func chunk?\n");
+
+        add_func_chunk(fasm, words[0], asmln);
+      }
+      continue;
+    } // *p == ';'
+
+    for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
+      words[wordc][0] = 0;
+      p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
+      if (*p == 0 || *p == ';') {
+        wordc++;
+        break;
+      }
+    }
+
+    if (wordc == 2 && IS(words[1], "ends"))
+      break;
+  }
+
+  fseek(fasm, oldpos, SEEK_SET);
+  asmln = oldasmln;
+}
+
 int main(int argc, char *argv[])
 {
   FILE *fout, *fasm, *frlist;
@@ -3336,16 +3799,14 @@ int main(int argc, char *argv[])
   char **rlist = NULL;
   int rlist_len = 0;
   int rlist_alloc = 0;
-  struct chunk_item *func_chunks;
   int func_chunks_used = 0;
   int func_chunks_sorted = 0;
-  int func_chunk_cnt = 0;
-  int func_chunk_alloc;
   int func_chunk_i = -1;
   long func_chunk_ret = 0;
   int func_chunk_ret_ln = 0;
+  int scanned_ahead = 0;
   char line[256];
-  char words[16][256];
+  char words[20][256];
   enum opr_lenmod lmod;
   int in_func = 0;
   int pending_endp = 0;
@@ -3392,6 +3853,8 @@ int main(int argc, char *argv[])
   func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
   my_assert_not(func_chunks, NULL);
 
+  memset(words, 0, sizeof(words));
+
   for (; arg < argc; arg++) {
     frlist = fopen(argv[arg], "r");
     my_assert_not(frlist, NULL);
@@ -3489,18 +3952,12 @@ int main(int argc, char *argv[])
         p += 30;
         next_word(words[0], sizeof(words[0]), p);
         if (words[0][0] == 0)
-          aerr("missing nam for func chunk?\n");
-        if (func_chunk_cnt >= func_chunk_alloc) {
-          func_chunk_alloc *= 2;
-          func_chunks = realloc(func_chunks,
-            func_chunk_alloc * sizeof(func_chunks[0]));
-          my_assert_not(func_chunks, NULL);
+          aerr("missing name for func chunk?\n");
+
+        if (!scanned_ahead) {
+          add_func_chunk(fasm, words[0], asmln);
+          func_chunks_sorted = 0;
         }
-        func_chunks[func_chunk_cnt].fptr = ftell(fasm);
-        func_chunks[func_chunk_cnt].name = strdup(words[0]);
-        func_chunks[func_chunk_cnt].asmln = asmln;
-        func_chunk_cnt++;
-        func_chunks_sorted = 0;
       }
       else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
       {
@@ -3532,22 +3989,30 @@ int main(int argc, char *argv[])
         if (IS_START(g_func, "sub_")) {
           unsigned long addr = strtoul(p, NULL, 16);
           unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
-          if (addr > f_addr)
-            aerr("need a chunk %lX that is after %s\n", addr, g_func);
+          if (addr > f_addr && !scanned_ahead) {
+            anote("scan_ahead caused by '%s', addr %lx\n",
+              g_func, addr);
+            scan_ahead(fasm);
+            scanned_ahead = 1;
+            func_chunks_sorted = 0;
+          }
         }
       }
       continue;
     } // *p == ';'
 
 parse_words:
-    memset(words, 0, sizeof(words));
-    for (wordc = 0; wordc < 16; wordc++) {
+    for (i = wordc; i < ARRAY_SIZE(words); i++)
+      words[i][0] = 0;
+    for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
       p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
       if (*p == 0 || *p == ';') {
         wordc++;
         break;
       }
     }
+    if (*p != 0 && *p != ';')
+      aerr("too many words\n");
 
     // alow asm patches in comments
     if (*p == ';' && IS_START(p, "; sctpatch:")) {
@@ -3710,6 +4175,9 @@ do_pending_endp:
       continue;
     }
 
+    if (wordc == 2 && IS(words[1], "ends"))
+      break;
+
     p = strchr(words[0], ':');
     if (p != NULL) {
       set_label(pi, words[0]);