decompiled_code:
_BOOL8 use_alternative()
{
return (unsigned int)non_zero_on_stack() == 0;
}

canonical_decompiled_code:
_BOOL8 func1()
{
return (unsigned int)non_zero_on_stack() == 0;
}

original_code:
static int use_alternative(void)
{
    return !non_zero_on_stack();
}

canonical_original_code:
static int use_alternative()
{
    return !non_zero_on_stack();
}


repo: jff/mathspad

decompiled_code:
__int64 seq_empty()
{
__int64 result; // rax

result = mm_malloc(32LL);
if ( result )
{
*(_BYTE *)result = 1;
*(_BYTE *)(result + 1) = 1;
*(_QWORD *)(result + 8) = 0LL;
*(_QWORD *)(result + 24) = 0LL;
*(_QWORD *)(result + 16) = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax

result = mm_malloc(32LL);
if ( result )
{
*(_BYTE *)result = 1;
*(_BYTE *)(result + 1) = 1;
*(_QWORD *)(result + 8) = 0LL;
*(_QWORD *)(result + 24) = 0LL;
*(_QWORD *)(result + 16) = 0LL;
}
return result;
}

original_code:
FuncSequenceRef *seq_empty(void)
{
    FuncSequenceRef *res;
    res = (FuncSequenceRef *) mm_malloc(sizeof(FuncSequenceRef));
    if (res) {
 res->free_next=1;
        res->free_alt=1;
        res->arglist=0;
        res->next=res->alternative=0;
    }
    return res;
}

canonical_original_code:
struct FuncSequence *seq_empty()
{
    struct FuncSequence *res;
    res = (struct FuncSequence *) mm_malloc(sizeof(FuncSequenceRef));
    if (res) {
 res->free_next=1;
        res->free_alt=1;
        res->arglist=0;
        res->next=res->alternative=0;
    }
    return res;
}

variable_types: struct FuncSequence { char free_next; char free_alt; struct Expression { char type; char delta; unsigned int restype; union { int op; int arg; struct Expression *le; struct FuncDef { char *name; char *description; char nr_args; char functype; unsigned int rettype; void *prototype; } *ifunc; struct FuncDef *ufunc; } val; struct Expression *next; } *arglist; struct FuncSequence *next; struct FuncSequence *alternative; } *

user_defined_types: struct FuncSequence { char free_next; char free_alt; struct Expression *arglist; struct FuncSequence *next; struct FuncSequence *alternative; }

user_defined_types: struct Expression { char type; char delta; unsigned int restype; union { int op; int arg; struct Expression *le; struct FuncDef *ifunc; struct FuncDef *ufunc; } val; struct Expression *next; }


decompiled_code:
__int64 __fastcall make_ifcheck(__int64 a1)
{
__int64 result; // rax

if ( a1 && (unsigned int)left_on_stack() != 1 )
fwrite("Warning: incorrect expression in guard.\n", 1uLL, 0x28uLL, stderr);
result = seq_empty();
if ( result )
*(_QWORD *)(result + 8) = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 result; // rax

if ( a1 && (unsigned int)left_on_stack() != 1 )
fwrite("Warning: incorrect expression in guard.\n", 1uLL, 0x28uLL, stderr);
result = func3();
if ( result )
*(_QWORD *)(result + 8) = a1;
return result;
}

original_code:
static FuncSequenceRef *make_ifcheck(Expression *expr)
{
    FuncSequenceRef *res;
    if (expr && left_on_stack(expr) != 1) {
      fprintf(stderr, "Warning: incorrect expression in guard.\n");
    }
    res = seq_empty();
    if (res) {
        res->arglist=expr;
    }
    return res;
}

canonical_original_code:
static struct FuncSequence *make_ifcheck(struct Expression *expr)
{
    struct FuncSequence *res;
    if (expr && left_on_stack(expr) != 1) {
      fprintf(stderr, "Warning: incorrect expression in guard.\n");
    }
    res = seq_empty();
    if (res) {
        res->arglist=expr;
    }
    return res;
}

variable_types:
expr
struct Expression *

return_type: struct FuncSequence { char free_next; char free_alt; struct Expression { char type; char delta; unsigned int restype; union { int op; int arg; struct Expression *le; struct FuncDef { char *name; char *description; char nr_args; char functype; unsigned int rettype; void *prototype; } *ifunc; struct FuncDef *ufunc; } val; struct Expression *next; } *arglist; struct FuncSequence *next; struct FuncSequence *alternative; } *


decompiled_code:
__int64 __fastcall seq_expression(__int64 a1)
{
__int64 result; // rax

result = seq_empty();
if ( result )
*(_QWORD *)(result + 8) = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 result; // rax

result = func3();
if ( result )
*(_QWORD *)(result + 8) = a1;
return result;
}

original_code:
FuncSequenceRef *seq_expression(Expression *expr)
{
    FuncSequenceRef *res;
    res=seq_empty();
    if (res) {
 res->arglist=expr;
    }
    return res;
}

canonical_original_code:
struct FuncSequence *seq_expression(struct Expression *expr)
{
    struct FuncSequence *res;
    res=seq_empty();
    if (res) {
 res->arglist=expr;
    }
    return res;
}

user_defined_types: struct FuncDef { char *name; char *description; char nr_args; char functype; unsigned int rettype; void *prototype; }


decompiled_code:
__int64 seq_assign()
{
__int64 v0; // rax

v0 = assign_expression();
return seq_expression(v0);
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v0; // rax

v0 = assign_expression();
return func5(v0);
}

original_code:
FuncSequenceRef *seq_assign(int nr, Argument *alist, Expression *elist)
{
    return seq_expression(assign_expression(nr, alist, elist));
}

canonical_original_code:
struct FuncSequence *seq_assign(int nr, int *alist, struct Expression *elist)
{
    return seq_expression(assign_expression(nr, alist, elist));
}

variable_types: nr int

variable_types:
alist
int *

variable_types:
elist
struct Expression *


original_code:
FuncSequenceRef *seq_if_statement(Expression *expr,
      FuncSequenceRef *trueps, FuncSequenceRef *truepe,
      FuncSequenceRef *falseps, FuncSequenceRef *falsepe,
      FuncSequenceRef **endret)
{
    FuncSequenceRef *start,*end;
    start = make_ifcheck(expr);
    end = seq_empty();
    if (trueps) {
 start->next=trueps;
 truepe->next=end;
    } else {
 start->next=end;
    }
    if (falseps) {
 start->alternative=falseps;
 falsepe->next=end;
 falsepe->free_next=0;
    } else {
 start->alternative=end;
 start->free_alt=0;
    }
    *endret=end;
    return start;
}

canonical_original_code:
struct FuncSequence *seq_if_statement(struct Expression *expr, struct FuncSequence *trueps, struct FuncSequence *truepe, struct FuncSequence *falseps, struct FuncSequence *falsepe, struct FuncSequence **endret)
{
    struct FuncSequence *start; struct FuncSequence *end;
    start = make_ifcheck(expr);
    end = seq_empty();
    if (trueps) {
 start->next=trueps;
 truepe->next=end;
    } else {
 start->next=end;
    }
    if (falseps) {
 start->alternative=falseps;
 falsepe->next=end;
 falsepe->free_next=0;
    } else {
 start->alternative=end;
 start->free_alt=0;
    }
    *endret=end;
    return start;
}

variable_types:
trueps
struct FuncSequence *

variable_types:
truepe
struct FuncSequence *

variable_types:
falseps
struct FuncSequence *

variable_types:
falsepe
struct FuncSequence *

variable_types:
endret
struct FuncSequence **


original_code:
FuncSequenceRef *seq_do_statement(Expression *expr,
      FuncSequenceRef *trueps, FuncSequenceRef *truepe,
      FuncSequenceRef *falseps, FuncSequenceRef *falsepe,
      FuncSequenceRef **endret)
{
    FuncSequenceRef *start, *end;
    FuncSequenceRef *ifchk;
    start=ifchk=make_ifcheck(expr);
    end = seq_empty();
    if (trueps) {
 start->next=trueps;
 truepe->next=start;
 truepe->free_next=0;
    } else {
 if (falseps) start->next=falseps;
 else start->next=end;
 start->free_next=0;
    }
    if (falseps) {
 FuncSequenceRef *h;
 start->alternative=falseps;
 falsepe->next = start;
 falsepe->free_next = 0;
 h=falseps;
 while (h->alternative != falsepe) {
     if (h->next == falsepe) {
  h->next = h->alternative;
  h->free_next=0;
     }
     h=h->alternative;
 }
 h->alternative=end;
 h->free_alt=1;
    } else {
 start->alternative=end;
 start->free_alt=1;
    }
    *endret=end;
    return start;
}

canonical_original_code:
struct FuncSequence *seq_do_statement(struct Expression *expr, struct FuncSequence *trueps, struct FuncSequence *truepe, struct FuncSequence *falseps, struct FuncSequence *falsepe, struct FuncSequence **endret)
{
    struct FuncSequence *start; struct FuncSequence *end;
    struct FuncSequence *ifchk;
    start=ifchk=make_ifcheck(expr);
    end = seq_empty();
    if (trueps) {
 start->next=trueps;
 truepe->next=start;
 truepe->free_next=0;
    } else {
 if (falseps) start->next=falseps;
 else start->next=end;
 start->free_next=0;
    }
    if (falseps) {
 struct FuncSequence *h;
 start->alternative=falseps;
 falsepe->next = start;
 falsepe->free_next = 0;
 h=falseps;
 while (h->alternative != falsepe) {
     if (h->next == falsepe) {
  h->next = h->alternative;
  h->free_next=0;
     }
     h=h->alternative;
 }
 h->alternative=end;
 h->free_alt=1;
    } else {
 start->alternative=end;
 start->free_alt=1;
    }
    *endret=end;
    return start;
}


decompiled_code:
__int64 __fastcall run_sequence(_QWORD *a1)
{
_QWORD *v1; // rbx

v1 = a1;
while ( v1 )
{
calculate_expression(v1[1]);
if ( v1[3] && use_alternative() )
{
clear_use_alternative();
v1 = (_QWORD *)v1[3];
}
else
{
v1 = (_QWORD *)v1[2];
}
clear_expr_stack();
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(_QWORD *a1)
{
_QWORD *v1; // rbx

v1 = a1;
while ( v1 )
{
calculate_expression(v1[1]);
if ( v1[3] && func1() )
{
func0();
v1 = (_QWORD *)v1[3];
}
else
{
v1 = (_QWORD *)v1[2];
}
clear_expr_stack();
}
return 0LL;
}

original_code:
int run_sequence(FuncSequenceRef *seq)
{
    while (seq && !interupted) {
 calculate_expression(seq->arglist);
 if (seq->alternative && use_alternative()) {
     clear_use_alternative();
     seq=seq->alternative;
 } else {
     seq=seq->next;
 }
 clear_expr_stack();
    }
    return interupted;
}

canonical_original_code:
int run_sequence(struct FuncSequence *seq)
{
    while (seq && !interupted) {
 calculate_expression(seq->arglist);
 if (seq->alternative && use_alternative()) {
     clear_use_alternative();
     seq=seq->alternative;
 } else {
     seq=seq->next;
 }
 clear_expr_stack();
    }
    return interupted;
}

return_type: int


decompiled_code:
__int64 __fastcall eval_sequence(_QWORD *a1)
{
unsigned int v1; // ebx

push_eval_stack();
v1 = run_sequence(a1);
clear_expr_stack();
pop_eval_stack();
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1)
{
unsigned int v1; // ebx

push_eval_stack();
v1 = func11(a1);
clear_expr_stack();
pop_eval_stack();
return v1;
}

original_code:
int eval_sequence(FuncSequenceRef *seq)
{
    int i;
    push_eval_stack();
    i=run_sequence(seq);
    clear_expr_stack();
    pop_eval_stack();
    return i;
}

canonical_original_code:
int eval_sequence(struct FuncSequence *seq)
{
    int i;
    push_eval_stack();
    i=run_sequence(seq);
    clear_expr_stack();
    pop_eval_stack();
    return i;
}

variable_types: i int


repo: jflemer/lotr

decompiled_code:
void lotr_close(void)
{
int v0; // eax
int v1; // ecx
int v2; // edi
int v3; // er9
int v4; // er11
int v5; // ecx

v0 = initlevel--;
if ( v0 > 5 )
characters_close();
v1 = initlevel--;
if ( v1 > 4 )
shapes_close();
v2 = initlevel--;
if ( v2 > 3 )
map_close();
v3 = initlevel--;
if ( v3 > 2 )
gui_close();
v4 = initlevel--;
if ( v4 > 1 )
sound_close();
v5 = initlevel--;
if ( v5 > 0 )
lotr_system_close();
}

canonical_decompiled_code:
void func0(void)
{
int v0; // eax
int v1; // ecx
int v2; // edi
int v3; // er9
int v4; // er11
int v5; // ecx

v0 = initlevel--;
if ( v0 > 5 )
characters_close();
v1 = initlevel--;
if ( v1 > 4 )
shapes_close();
v2 = initlevel--;
if ( v2 > 3 )
map_close();
v3 = initlevel--;
if ( v3 > 2 )
gui_close();
v4 = initlevel--;
if ( v4 > 1 )
sound_close();
v5 = initlevel--;
if ( v5 > 0 )
lotr_system_close();
}

original_code:
void
lotr_close(void)
{
    if (initlevel-- >= 6)
        characters_close();
    if (initlevel-- >= 5)
        shapes_close();
    if (initlevel-- >= 4)
        map_close();
    if (initlevel-- >= 3)
        gui_close();
    if (initlevel-- >= 2)
        sound_close();
    if (initlevel-- >= 1)
        lotr_system_close();
}

canonical_original_code:
void lotr_close()
{
    if (initlevel-- >= 6)
        characters_close();
    if (initlevel-- >= 5)
        shapes_close();
    if (initlevel-- >= 4)
        map_close();
    if (initlevel-- >= 3)
        gui_close();
    if (initlevel-- >= 2)
        sound_close();
    if (initlevel-- >= 1)
        lotr_system_close();
}

return_type: void


decompiled_code:
__int64 lotr_init()
{
__int64 result; // rax

initlevel = 0;
atexit(lotr_close);
lotr_system_init();
++initlevel;
sound_init();
++initlevel;
gui_init();
++initlevel;
map_init();
++initlevel;
shapes_init();
++initlevel;
result = characters_init();
++initlevel;
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax

initlevel = 0;
atexit(lotr_close);
lotr_system_init();
++initlevel;
sound_init();
++initlevel;
gui_init();
++initlevel;
map_init();
++initlevel;
shapes_init();
++initlevel;
result = characters_init();
++initlevel;
return result;
}

original_code:
void
lotr_init(void)
{
    initlevel = 0;
    atexit(lotr_close);
    lotr_system_init();
    initlevel++;
    sound_init();
    initlevel++;
    gui_init();
    initlevel++;
    map_init();
    initlevel++;
    shapes_init();
    initlevel++;
    characters_init();
    initlevel++;
}

canonical_original_code:
void lotr_init()
{
    initlevel = 0;
    atexit(lotr_close);
    lotr_system_init();
    initlevel++;
    sound_init();
    initlevel++;
    gui_init();
    initlevel++;
    map_init();
    initlevel++;
    shapes_init();
    initlevel++;
    characters_init();
    initlevel++;
}


main

canonical_name: main

original_code:
int
main(int argc, char *argv[])
{
    int game_num = 0;
    lotr_init();
    game_new();
    if (game_num)
        game_load(game_num);
    cartoon_show_credits(1);
    if (game_num == 0)
        cartoon_play("opening");
    game_init_graphics();
    lotr_reset_timer();
    while (!lotr_key_esc()) {
        int frame_time = 50;
        lotr_poll_events();
        game_next_frame();
        if (lotr_key_shift())
            frame_time /= 4;
        lotr_timer(frame_time);
    }
    lotr_close();
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
void *__fastcall object_name(unsigned int a1)
{
void *result; // rax

result = &unk_D41;
if ( a1 <= 0xF6 )
result = *(void **)&object_names[8 * a1];
return result;
}

canonical_decompiled_code:
void *__fastcall func0(unsigned int a1)
{
void *result; // rax

result = &unk_D41;
if ( a1 <= 0xF6 )
result = *(void **)&object_names[8 * a1];
return result;
}

original_code:
char *
object_name(int index)
{
    if (index < 0 || index > 0xf6)
        return "";
    return object_names[index];
}

canonical_original_code:
char *object_name(int index)
{
    if (index < 0 || index > 0xf6)
        return "";
    return object_names[index];
}

variable_types: index int

return_type: char *


decompiled_code:
__int64 __fastcall object_price(int a1)
{
__int64 result; // rax

result = 255LL;
if ( (unsigned int)(a1 - 96) <= 0x9F )
result = (unsigned int)object_prices[a1 - 96];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
__int64 result; // rax

result = 255LL;
if ( (unsigned int)(a1 - 96) <= 0x9F )
result = (unsigned int)object_prices[a1 - 96];
return result;
}

original_code:
int
object_price(int index)
{
    if (index < 0x60 || index >= 0x100)
        return 255;
    return object_prices[index - 0x60];
}


decompiled_code:
_BOOL8 __fastcall object_is_ring(int a1)
{
return a1 == 148;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(int a1)
{
return a1 == 148;
}

original_code:
int
object_is_ring(int index)
{
    return index == 0x94;
}


decompiled_code:
_BOOL8 __fastcall object_is_armour(int a1)
{
return (unsigned int)(a1 - 128) <= 7;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(int a1)
{
return (unsigned int)(a1 - 128) <= 7;
}

original_code:
int
object_is_armour(int index)
{
    return index >= 0x80 && index < 0x88;
}


decompiled_code:
_BOOL8 __fastcall object_is_shield(int a1)
{
return (unsigned int)(a1 - 136) <= 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(int a1)
{
return (unsigned int)(a1 - 136) <= 1;
}

original_code:
int
object_is_shield(int index)
{
    return index == 0x88 || index == 0x89;
}


decompiled_code:
_BOOL8 __fastcall object_is_weapon(int a1)
{
return (unsigned int)(a1 - 96) <= 0x1F;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(int a1)
{
return (unsigned int)(a1 - 96) <= 0x1F;
}

original_code:
int
object_is_weapon(int index)
{
    return index >= 0x60 && index < 0x80;
}


decompiled_code:
__int64 __fastcall object_weapon_class(int a1)
{
unsigned __int64 v1; // rdx
__int64 result; // rax
__int64 v3; // rcx
unsigned int v4; // esi
unsigned int v5; // edi

if ( (unsigned int)(a1 - 96) <= 1 || a1 == 79 )
return 1LL;
v1 = (unsigned int)(a1 - 98);
if ( (unsigned int)v1 > 8 || (result = 2LL, v3 = 407LL, !_bittest64(&v3, v1)) )
{
if ( a1 == 103 )
{
result = 4LL;
}
else if ( a1 == 101 )
{
result = 8LL;
}
else
{
v4 = a1 - 112;
v5 = a1 - 111;
result = 0LL;
if ( v4 < 9 )
result = v5;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
unsigned __int64 v1; // rdx
__int64 result; // rax
__int64 v3; // rcx
unsigned int v4; // esi
unsigned int v5; // edi

if ( (unsigned int)(a1 - 96) <= 1 || a1 == 79 )
return 1LL;
v1 = (unsigned int)(a1 - 98);
if ( (unsigned int)v1 > 8 || (result = 2LL, v3 = 407LL, !_bittest64(&v3, v1)) )
{
if ( a1 == 103 )
{
result = 4LL;
}
else if ( a1 == 101 )
{
result = 8LL;
}
else
{
v4 = a1 - 112;
v5 = a1 - 111;
result = 0LL;
if ( v4 < 9 )
result = v5;
}
}
return result;
}

original_code:
int
object_weapon_class(int index)
{
    if (index == 0x60 || index == 0x61 || index == 79)
        return 1;
    if (index == 0x62 || index == 0x63 || index == 0x64 || index == 0x66
        || index == 0x69 || index == 0x6a)
        return 2;
    if (index == 0x67)
        return 4;
    if (index == 0x65)
        return 8;
    if (index >= 0x70 && index <= 0x78)
        return index - 0x70 + 1;
    return 0;
}


decompiled_code:
__int64 __fastcall object_weapon_skill(int a1)
{
__int64 result; // rax

switch ( (unsigned int)object_weapon_class(a1) )
{
case 2u:
result = 65LL;
break;
case 4u:
result = 66LL;
break;
case 5u:
result = 67LL;
break;
case 7u:
result = 68LL;
break;
case 8u:
result = 69LL;
break;
default:
result = 0LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
__int64 result; // rax

switch ( (unsigned int)func6(a1) )
{
case 2u:
result = 65LL;
break;
case 4u:
result = 66LL;
break;
case 5u:
result = 67LL;
break;
case 7u:
result = 68LL;
break;
case 8u:
result = 69LL;
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
int
object_weapon_skill(int index)
{
    int class;
    class = object_weapon_class(index);
    switch (class) {
        case 2:
            return 0x41;
        case 4:
            return 0x42;
        case 5:
            return 0x43;
        case 7:
            return 0x44;
        case 8:
            return 0x45;
        default:
            return 0;
    }
    return 0;
}

variable_types: class int


decompiled_code:
_BOOL8 __fastcall object_is_skill(int a1)
{
return (unsigned int)(a1 - 64) <= 0x1F;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(int a1)
{
return (unsigned int)(a1 - 64) <= 0x1F;
}

original_code:
int
object_is_skill(int index)
{
    return index >= 0x40 && index < 0x60;
}


decompiled_code:
_BOOL8 __fastcall object_is_spell(int a1)
{
return (unsigned int)(a1 - 1) <= 0x1E;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(int a1)
{
return (unsigned int)(a1 - 1) <= 0x1E;
}

original_code:
int
object_is_spell(int index)
{
    return index >= 1 && index < 0x20;
}


decompiled_code:
_BOOL8 __fastcall object_is_item(int a1)
{
return a1 > 95;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(int a1)
{
return a1 > 95;
}

original_code:
int
object_is_item(int index)
{
    return index >= 0x60;
}


decompiled_code:
__int64 __fastcall object_food_value(int a1)
{
__int64 result; // rax

result = 1LL;
if ( a1 != 172 )
{
switch ( a1 )
{
case 236:
case 164:
case 184:
result = 2LL;
break;
case 225:
case 181:
result = 3LL;
break;
case 183:
case 190:
result = 4LL;
break;
default:
result = 0LL;
if ( a1 == 245 )
result = 6LL;
break;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1)
{
__int64 result; // rax

result = 1LL;
if ( a1 != 172 )
{
switch ( a1 )
{
case 236:
case 164:
case 184:
result = 2LL;
break;
case 225:
case 181:
result = 3LL;
break;
case 183:
case 190:
result = 4LL;
break;
default:
result = 0LL;
if ( a1 == 245 )
result = 6LL;
break;
}
}
return result;
}

original_code:
int
object_food_value(int index)
{
    if (index == 0xac)
        return 1;
    if (index == 0xa4 || index == 0xec || index == 0xb8)
        return 2;
    if (index == 0xe1 || index == 0xb5)
        return 3;
    if (index == 0xb7 || index == 0xbe)
        return 4;
    if (index == 0xf5)
        return 6;
    return 0;
}


decompiled_code:
__int64 __fastcall object_weapon_weight(int a1)
{
__int64 result; // rax

result = object_weapon_class(a1);
if ( (_DWORD)result )
{
switch ( (int)result )
{
case 2:
case 7:
result = 16LL;
break;
case 3:
result = 7LL;
break;
case 4:
result = 20LL;
break;
case 5:
result = 13LL;
break;
case 6:
result = 8LL;
break;
case 8:
result = 7LL;
break;
default:
result = 0LL;
break;
}
if ( a1 <= 111 )
result = (unsigned int)(7 * (int)result / 10);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1)
{
__int64 result; // rax

result = func6(a1);
if ( (_DWORD)result )
{
switch ( (int)result )
{
case 2:
case 7:
result = 16LL;
break;
case 3:
result = 7LL;
break;
case 4:
result = 20LL;
break;
case 5:
result = 13LL;
break;
case 6:
result = 8LL;
break;
case 8:
result = 7LL;
break;
default:
result = 0LL;
break;
}
if ( a1 <= 111 )
result = (unsigned int)(7 * (int)result / 10);
}
return result;
}

original_code:
int
object_weapon_weight(int index)
{
    int class, w;
    class = object_weapon_class(index);
    if (class == 0)
        return 0;
    switch (class) {
        case 1:
            w = 0;
            break;
        case 2:
            w = 16;
            break;
        case 3:
            w = 7;
            break;
        case 4:
            w = 20;
            break;
        case 5:
            w = 13;
            break;
        case 6:
            w = 8;
            break;
        case 7:
            w = 16;
            break;
        case 8:
            w = 7;
            break;
        case 9:
            w = 0;
            break;
        default:
            w = 0;
            break;
    }
    if (index < 0x70)
        w = w * 7 / 10;
    return w;
}

canonical_original_code:
int object_weapon_weight(int index)
{
    int class; int w;
    class = object_weapon_class(index);
    if (class == 0)
        return 0;
    switch (class) {
        case 1:
            w = 0;
            break;
        case 2:
            w = 16;
            break;
        case 3:
            w = 7;
            break;
        case 4:
            w = 20;
            break;
        case 5:
            w = 13;
            break;
        case 6:
            w = 8;
            break;
        case 7:
            w = 16;
            break;
        case 8:
            w = 7;
            break;
        case 9:
            w = 0;
            break;
        default:
            w = 0;
            break;
    }
    if (index < 0x70)
        w = w * 7 / 10;
    return w;
}

variable_types: w int


decompiled_code:
__int64 __fastcall object_weapon_damage(int a1)
{
__int64 result; // rax

result = object_weapon_class(a1);
if ( (_DWORD)result )
{
switch ( (int)result )
{
case 1:
case 3:
case 6:
case 9:
result = 6LL;
break;
case 2:
case 5:
case 8:
result = 8LL;
break;
case 4:
result = 10LL;
break;
case 7:
result = 10LL;
break;
default:
result = 5LL;
break;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(int a1)
{
__int64 result; // rax

result = func6(a1);
if ( (_DWORD)result )
{
switch ( (int)result )
{
case 1:
case 3:
case 6:
case 9:
result = 6LL;
break;
case 2:
case 5:
case 8:
result = 8LL;
break;
case 4:
result = 10LL;
break;
case 7:
result = 10LL;
break;
default:
result = 5LL;
break;
}
}
return result;
}

original_code:
int
object_weapon_damage(int index)
{
    int class, result;
    class = object_weapon_class(index);
    if (class == 0)
        return 0;
    switch (class) {
        case 1:
            result = 6;
            break;
        case 2:
            result = 8;
            break;
        case 3:
            result = 6;
            break;
        case 4:
            result = 10;
            break;
        case 5:
            result = 8;
            break;
        case 6:
            result = 6;
            break;
        case 7:
            result = 10;
            break;
        case 8:
            result = 8;
            break;
        case 9:
            result = 6;
            break;
        default:
            result = 5;
            break;
    }
    return result;
}

canonical_original_code:
int object_weapon_damage(int index)
{
    int class; int result;
    class = object_weapon_class(index);
    if (class == 0)
        return 0;
    switch (class) {
        case 1:
            result = 6;
            break;
        case 2:
            result = 8;
            break;
        case 3:
            result = 6;
            break;
        case 4:
            result = 10;
            break;
        case 5:
            result = 8;
            break;
        case 6:
            result = 6;
            break;
        case 7:
            result = 10;
            break;
        case 8:
            result = 8;
            break;
        case 9:
            result = 6;
            break;
        default:
            result = 5;
            break;
    }
    return result;
}

variable_types: result int


decompiled_code:
__int64 __fastcall object_weapon_to_hit(int a1)
{
__int64 result; // rax
unsigned int v2; // ecx

result = object_weapon_class(a1);
if ( (_DWORD)result )
{
v2 = 2 * ((unsigned int)(result - 1) < 3);
result = v2 + 2;
if ( a1 >= 112 )
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1)
{
__int64 result; // rax
unsigned int v2; // ecx

result = func6(a1);
if ( (_DWORD)result )
{
v2 = 2 * ((unsigned int)(result - 1) < 3);
result = v2 + 2;
if ( a1 >= 112 )
result = v2;
}
return result;
}

original_code:
int
object_weapon_to_hit(int index)
{
    int class, result;
    class = object_weapon_class(index);
    if (class == 0)
        return 0;
    result = 0;
    if (class > 0 && class < 4)
        result = 2;
    if (index < 0x70)
        result += 2;
    return result;
}

canonical_original_code:
int object_weapon_to_hit(int index)
{
    int class; int result;
    class = object_weapon_class(index);
    if (class == 0)
        return 0;
    result = 0;
    if (class > 0 && class < 4)
        result = 2;
    if (index < 0x70)
        result += 2;
    return result;
}


decompiled_code:
__int64 __fastcall object_armour_reduced(int a1)
{
unsigned int v1; // eax
unsigned int v2; // edi
bool v3; // cf
__int64 result; // rax

v1 = a1 - 129;
v2 = a1 - 127;
v3 = v1 < 4;
result = 0LL;
if ( v3 )
result = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1)
{
unsigned int v1; // eax
unsigned int v2; // edi
bool v3; // cf
__int64 result; // rax

v1 = a1 - 129;
v2 = a1 - 127;
v3 = v1 < 4;
result = 0LL;
if ( v3 )
result = v2;
return result;
}

original_code:
int
object_armour_reduced(int index)
{
    if (index <= 0x80 || index > 0x84)
        return 0;
    return index - 0x80 + 1;
}


decompiled_code:
__int64 __fastcall object_armour_to_hit(int a1)
{
__int64 result; // rax

result = 1LL;
if ( (a1 & 0xFFFFFFFD) != 129 )
result = 2 * (unsigned int)(a1 == 130);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1)
{
__int64 result; // rax

result = 1LL;
if ( (a1 & 0xFFFFFFFD) != 129 )
result = 2 * (unsigned int)(a1 == 130);
return result;
}

original_code:
int
object_armour_to_hit(int index)
{
    if (index == 0x81 || index == 0x83)
        return 1;
    if (index == 0x82)
        return 2;
    return 0;
}


decompiled_code:
__int64 __fastcall object_shield_to_hit(int a1)
{
__int64 result; // rax

if ( a1 == 136 )
result = 2LL;
else
result = 4 * (unsigned int)(a1 == 137);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(int a1)
{
__int64 result; // rax

if ( a1 == 136 )
result = 2LL;
else
result = 4 * (unsigned int)(a1 == 137);
return result;
}

original_code:
int
object_shield_to_hit(int index)
{
    if (index == 0x88)
        return 2;
    if (index == 0x89)
        return 4;
    return 0;
}


decompiled_code:
int hook_music_finished()
{
int result; // eax

result = unlink(miditmpname);
music_running = 0;
return result;
}

canonical_decompiled_code:
int func0()
{
int result; // eax

result = unlink(miditmpname);
music_running = 0;
return result;
}

original_code:
void
hook_music_finished()
{
    unlink(miditmpname);
    music_running = 0;
}


decompiled_code:
__int64 stop_midi()
{
unsigned int v0; // ebx

v0 = 0;
if ( music )
{
v0 = music_running;
Mix_HaltMusic();
Mix_FreeMusic(music);
music = 0LL;
hook_music_finished();
}
return v0;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebx

v0 = 0;
if ( music )
{
v0 = music_running;
Mix_HaltMusic();
Mix_FreeMusic(music);
music = 0LL;
func0();
}
return v0;
}

original_code:
int
stop_midi(void)
{
    int was_running = music_running;
    if (!music)
        return 0;
    Mix_HaltMusic();
    Mix_FreeMusic(music);
    music = 0;
    hook_music_finished();
    return was_running;
}

canonical_original_code:
int stop_midi()
{
    int was_running = music_running;
    if (!music)
        return 0;
    Mix_HaltMusic();
    Mix_FreeMusic(music);
    music = 0;
    hook_music_finished();
    return was_running;
}


decompiled_code:
void __fastcall play_midi(void *buf, int a2, int a3)
{
int v4; // eax
int v5; // er12
__int64 v6; // rdi
const char *v7; // rax
const char *v8; // rax

if ( !midi_disabled )
{
if ( !buf || !a2 )
{
fwrite("lotr: trying to play an empty midi file\n", 1uLL, 0x28uLL, stderr);
exit(1);
}
stop_midi();
miditmpname[14] = 88;
miditmpname[13] = 88;
miditmpname[12] = 88;
miditmpname[11] = 88;
miditmpname[10] = 88;
miditmpname[9] = 88;
v4 = mkstemp(miditmpname);
v5 = v4;
if ( v4 < 0 || write(v4, buf, a2) != a2 )
{
fwrite("lotr: can not create temporary midifile\n", 1uLL, 0x28uLL, stderr);
exit(1);
}
close(v5);
Mix_HookMusicFinished(hook_music_finished);
v6 = Mix_LoadMUS(miditmpname);
music = v6;
if ( v6 )
{
music_running = 1;
if ( (int)Mix_PlayMusic(v6, a3 == 0 ? 1 : -1) < 0 )
{
v8 = (const char *)SDL_GetError();
printf("Can't play midifile: %s\n", v8);
hook_music_finished();
}
}
else
{
v7 = (const char *)SDL_GetError();
fprintf(stderr, "Can't start midi file %s: %s\n", miditmpname, v7);
}
}
}

canonical_decompiled_code:
void __fastcall func2(void *buf, int a2, int a3)
{
int v4; // eax
int v5; // er12
__int64 v6; // rdi
const char *v7; // rax
const char *v8; // rax

if ( !midi_disabled )
{
if ( !buf || !a2 )
{
fwrite("lotr: trying to play an empty midi file\n", 1uLL, 0x28uLL, stderr);
exit(1);
}
func1();
miditmpname[14] = 88;
miditmpname[13] = 88;
miditmpname[12] = 88;
miditmpname[11] = 88;
miditmpname[10] = 88;
miditmpname[9] = 88;
v4 = mkstemp(miditmpname);
v5 = v4;
if ( v4 < 0 || write(v4, buf, a2) != a2 )
{
fwrite("lotr: can not create temporary midifile\n", 1uLL, 0x28uLL, stderr);
exit(1);
}
close(v5);
Mix_HookMusicFinished(hook_music_finished);
v6 = Mix_LoadMUS(miditmpname);
music = v6;
if ( v6 )
{
music_running = 1;
if ( (int)Mix_PlayMusic(v6, a3 == 0 ? 1 : -1) < 0 )
{
v8 = (const char *)SDL_GetError();
printf("Can't play midifile: %s\n", v8);
func0();
}
}
else
{
v7 = (const char *)SDL_GetError();
fprintf(stderr, "Can't start midi file %s: %s\n", miditmpname, v7);
}
}
}

original_code:
void
play_midi(Uint8 *data, int size, int loop)
{
    int midifile = -1;
    if (midi_disabled)
        return;
    if (data == ((void *)0) || size == 0) {
        fprintf(stderr, "lotr: trying to play an empty midi file\n");
        exit(1);
    }
    stop_midi();
    miditmpname[9] = miditmpname[10] = miditmpname[11] = miditmpname[12] =
        miditmpname[13] = miditmpname[14] = 'X';
    if ((midifile = mkstemp(miditmpname)) < 0
        || write(midifile, data, size) != size) {
        fprintf(stderr, "lotr: can not create temporary midifile\n");
        exit(1);
    }
    close(midifile);
    Mix_HookMusicFinished(hook_music_finished);
    music = Mix_LoadMUS(miditmpname);
    if (!music) {
        fprintf(stderr, "Can't start midi file %s: %s\n", miditmpname,
                SDL_GetError());
        return;
    }
    music_running = 1;
    if (Mix_PlayMusic(music, loop ? -1 : 1) < 0) {
        printf("Can't play midifile: %s\n", SDL_GetError());
        hook_music_finished();
    }
}

canonical_original_code:
void play_midi(unsigned char *data, int size, int loop)
{
    int midifile = -1;
    if (midi_disabled)
        return;
    if (data == ((void *)0) || size == 0) {
        fprintf(stderr, "lotr: trying to play an empty midi file\n");
        exit(1);
    }
    stop_midi();
    miditmpname[9] = miditmpname[10] = miditmpname[11] = miditmpname[12] =
        miditmpname[13] = miditmpname[14] = 'X';
    if ((midifile = mkstemp(miditmpname)) < 0
        || write(midifile, data, size) != size) {
        fprintf(stderr, "lotr: can not create temporary midifile\n");
        exit(1);
    }
    close(midifile);
    Mix_HookMusicFinished(hook_music_finished);
    music = Mix_LoadMUS(miditmpname);
    if (!music) {
        fprintf(stderr, "Can't start midi file %s: %s\n", miditmpname,
                SDL_GetError());
        return;
    }
    music_running = 1;
    if (Mix_PlayMusic(music, loop ? -1 : 1) < 0) {
        printf("Can't play midifile: %s\n", SDL_GetError());
        hook_music_finished();
    }
}

variable_types:
data
unsigned char *

variable_types: size int

variable_types: loop int

variable_types: midifile int


decompiled_code:
void sound_close()
{
__int64 *v0; // r12
__int64 *v1; // rbx
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi
void *v6; // rdi
void *v7; // rdi
void *v8; // rdi

stop_midi();
if ( sound_data[0] )
free((void *)sound_data[0]);
v0 = &sound_data[1];
do
{
if ( *v0 )
free((void *)*v0);
v1 = v0 + 1;
v2 = (void *)v0[1];
if ( v2 )
free(v2);
v3 = (void *)v0[2];
if ( v3 )
free(v3);
v4 = (void *)v0[3];
if ( v4 )
free(v4);
v5 = (void *)v0[4];
if ( v5 )
free(v5);
v6 = (void *)v0[5];
if ( v6 )
free(v6);
v7 = (void *)v0[6];
if ( v7 )
free(v7);
v8 = (void *)v0[7];
if ( v8 )
free(v8);
v0 += 8;
}
while ( v1 + 7 != (__int64 *)(unsigned int)((_DWORD)sound_data + 200) );
}

canonical_decompiled_code:
void func1()
{
__int64 *v0; // r12
__int64 *v1; // rbx
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi
void *v6; // rdi
void *v7; // rdi
void *v8; // rdi

stop_midi();
if ( sound_data[0] )
free((void *)sound_data[0]);
v0 = &sound_data[1];
do
{
if ( *v0 )
free((void *)*v0);
v1 = v0 + 1;
v2 = (void *)v0[1];
if ( v2 )
free(v2);
v3 = (void *)v0[2];
if ( v3 )
free(v3);
v4 = (void *)v0[3];
if ( v4 )
free(v4);
v5 = (void *)v0[4];
if ( v5 )
free(v5);
v6 = (void *)v0[5];
if ( v6 )
free(v6);
v7 = (void *)v0[6];
if ( v7 )
free(v7);
v8 = (void *)v0[7];
if ( v8 )
free(v8);
v0 += 8;
}
while ( v1 + 7 != (__int64 *)(unsigned int)((_DWORD)sound_data + 200) );
}

original_code:
void
sound_close(void)
{
    int i;
    stop_midi();
    for (i = 0; i < 25; ++i)
        if (sound_data[i])
            free(sound_data[i]);
}

canonical_original_code:
void sound_close()
{
    int i;
    stop_midi();
    for (i = 0; i < 25; ++i)
        if (sound_data[i])
            free(sound_data[i]);
}


decompiled_code:
__int64 __fastcall sound_play(int a1)
{
__int64 result; // rax

if ( a1 <= 8 )
result = play_midi(sound_data[a1 + 8], (unsigned int)sound_sizes[a1 + 8], 0LL);
else
result = play_sample(sound_data[a1 - 8], (unsigned int)sound_sizes[a1 - 8]);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
__int64 result; // rax

if ( a1 <= 8 )
result = play_midi(sound_data[a1 + 8], (unsigned int)sound_sizes[a1 + 8], 0LL);
else
result = play_sample(sound_data[a1 - 8], (unsigned int)sound_sizes[a1 - 8]);
return result;
}

original_code:
void
sound_play(int index)
{
    if (index > 8)
        play_sample(sound_data[index - 8], sound_sizes[index - 8]);
    else
        play_midi(sound_data[index + 8], sound_sizes[index + 8], 0);
}


decompiled_code:
__int64 play_music()
{
return play_midi(music_data, (unsigned int)music_size, 1LL);
}

canonical_decompiled_code:
__int64 func3()
{
return play_midi(music_data, (unsigned int)music_size, 1LL);
}

original_code:
void
play_music(void)
{
    play_midi(music_data, music_size, 1);
}

canonical_original_code:
void play_music()
{
    play_midi(music_data, music_size, 1);
}


decompiled_code:
__int64 toggle_music()
{
__int64 result; // rax

result = stop_midi();
if ( !(_DWORD)result )
result = play_music();
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax

result = stop_midi();
if ( !(_DWORD)result )
result = func3();
return result;
}

original_code:
void
toggle_music(void)
{
    if (!stop_midi())
        play_music();
}

canonical_original_code:
void toggle_music()
{
    if (!stop_midi())
        play_music();
}


decompiled_code:
__int64 music_spell_performed()
{
return play_midi(sound_data[15], (unsigned int)sound_sizes[15], 0LL);
}

canonical_decompiled_code:
__int64 func5()
{
return play_midi(sound_data[15], (unsigned int)sound_sizes[15], 0LL);
}

original_code:
void
music_spell_performed(void)
{
    play_midi(sound_data[15], sound_sizes[15], 0);
}

canonical_original_code:
void music_spell_performed()
{
    play_midi(sound_data[15], sound_sizes[15], 0);
}


decompiled_code:
__int64 music_spell_failed()
{
return play_midi(sound_data[16], (unsigned int)sound_sizes[16], 0LL);
}

canonical_decompiled_code:
__int64 func6()
{
return play_midi(sound_data[16], (unsigned int)sound_sizes[16], 0LL);
}

original_code:
void
music_spell_failed(void)
{
    play_midi(sound_data[16], sound_sizes[16], 0);
}

canonical_original_code:
void music_spell_failed()
{
    play_midi(sound_data[16], sound_sizes[16], 0);
}


decompiled_code:
__int64 music_combat_started()
{
return play_midi(sound_data[21], (unsigned int)sound_sizes[21], 0LL);
}

canonical_decompiled_code:
__int64 func7()
{
return play_midi(sound_data[21], (unsigned int)sound_sizes[21], 0LL);
}

original_code:
void
music_combat_started(void)
{
    play_midi(sound_data[21], sound_sizes[21], 0);
}

canonical_original_code:
void music_combat_started()
{
    play_midi(sound_data[21], sound_sizes[21], 0);
}


decompiled_code:
__int64 music_combat_won()
{
return play_midi(sound_data[22], (unsigned int)sound_sizes[22], 0LL);
}

canonical_decompiled_code:
__int64 func8()
{
return play_midi(sound_data[22], (unsigned int)sound_sizes[22], 0LL);
}

original_code:
void
music_combat_won(void)
{
    play_midi(sound_data[22], sound_sizes[22], 0);
}

canonical_original_code:
void music_combat_won()
{
    play_midi(sound_data[22], sound_sizes[22], 0);
}


decompiled_code:
__int64 lotr_sleep()
{
return SDL_Delay();
}

canonical_decompiled_code:
__int64 func0()
{
return SDL_Delay();
}

original_code:
void
lotr_sleep(int delay)
{
    SDL_Delay(delay);
}

variable_types: delay int


decompiled_code:
__int64 lotr_reset_timer()
{
__int64 result; // rax

result = SDL_GetTicks();
lotr_ticks = result;
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax

result = SDL_GetTicks();
lotr_ticks = result;
return result;
}

original_code:
void
lotr_reset_timer(void)
{
    lotr_ticks = SDL_GetTicks();
}

canonical_original_code:
void lotr_reset_timer()
{
    lotr_ticks = SDL_GetTicks();
}


decompiled_code:
__int64 __fastcall lotr_timer(int a1)
{
__int64 result; // rax

result = SDL_GetTicks();
if ( lotr_ticks + a1 > (int)result )
{
result = SDL_Delay((unsigned int)(lotr_ticks + a1 - result));
lotr_ticks += a1;
}
else
{
lotr_ticks = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
__int64 result; // rax

result = SDL_GetTicks();
if ( lotr_ticks + a1 > (int)result )
{
result = SDL_Delay((unsigned int)(lotr_ticks + a1 - result));
lotr_ticks += a1;
}
else
{
lotr_ticks = result;
}
return result;
}

original_code:
void
lotr_timer(int delay)
{
    int timer;
    timer = SDL_GetTicks();
    if (lotr_ticks + delay > timer) {
        SDL_Delay(lotr_ticks + delay - timer);
        lotr_ticks = lotr_ticks + delay;
    } else {
        lotr_ticks = timer;
    }
}

variable_types: timer int


original_code:
void
shapes_close(void)
{
    int i, j;
    for (i = 0; i < 700; ++i)
        if (shapes_cache[i] != ((void *)0)) {
            for (j = 0; j < 4; ++j)
                if (shapes_cache[i]->pixmaps[j] != ((void *)0))
                    pixmap_free(shapes_cache[i]->pixmaps[j]);
            free(shapes_cache[i]);
        }
    for (i = 0; i < 46; ++i) {
        if (portraits_cache[i] == ((void *)0))
            continue;
        if (portraits_cache[i]->freepalette)
            free(portraits_cache[i]->palette);
        free(portraits_cache[i]->pixmap);
        free(portraits_cache[i]);
        portraits_cache[i] = ((void *)0);
    }
}

canonical_original_code:
void shapes_close()
{
    int i; int j;
    for (i = 0; i < 700; ++i)
        if (shapes_cache[i] != ((void *)0)) {
            for (j = 0; j < 4; ++j)
                if (shapes_cache[i]->pixmaps[j] != ((void *)0))
                    pixmap_free(shapes_cache[i]->pixmaps[j]);
            free(shapes_cache[i]);
        }
    for (i = 0; i < 46; ++i) {
        if (portraits_cache[i] == ((void *)0))
            continue;
        if (portraits_cache[i]->freepalette)
            free(portraits_cache[i]->palette);
        free(portraits_cache[i]->pixmap);
        free(portraits_cache[i]);
        portraits_cache[i] = ((void *)0);
    }
}

variable_types: j int


decompiled_code:
__int64 shapes_set_palette()
{
return graphics_set_palette(shapes_palette, 128LL, 64LL);
}

canonical_decompiled_code:
__int64 func1()
{
return graphics_set_palette(shapes_palette, 128LL, 64LL);
}

original_code:
void
shapes_set_palette(void)
{
    graphics_set_palette(shapes_palette, 0x80, 0x40);
}

canonical_original_code:
void shapes_set_palette()
{
    graphics_set_palette(shapes_palette, 0x80, 0x40);
}


decompiled_code:
__int64 __fastcall shape_get(unsigned int a1)
{
__int64 result; // rax

if ( a1 > 0x2BB )
result = 0LL;
else
result = shapes_cache[a1];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
__int64 result; // rax

if ( a1 > 0x2BB )
result = 0LL;
else
result = shapes_cache[a1];
return result;
}

original_code:
Shape *
shape_get(int i)
{
    if (i < 0 || i >= 700)
        return ((void *)0);
    return shapes_cache[i];
}

canonical_original_code:
struct Shape *shape_get(int i)
{
    if (i < 0 || i >= 700)
        return ((void *)0);
    return shapes_cache[i];
}

return_type: struct Shape { struct Pixmap { int width; int height; int datasize; int hasalpha; unsigned char alpha; unsigned char *data; } *pixmaps[4]; int pixmaps_num; } *

user_defined_types: struct Shape { struct Pixmap *pixmaps[4]; int pixmaps_num; }


decompiled_code:
void __fastcall shape_draw(_QWORD *a1, int a2, int a3, int a4)
{
_DWORD *v4; // rax
_DWORD *v5; // rdi

if ( a1 )
{
v4 = (_DWORD *)*a1;
if ( *a1 )
{
v5 = (_DWORD *)a1[a2];
if ( !v5 )
v5 = v4;
pixmap_draw(v5, (unsigned int)(a3 - *v5), (unsigned int)(a4 - v5[1]));
}
}
}

canonical_decompiled_code:
void __fastcall func4(_QWORD *a1, int a2, int a3, int a4)
{
_DWORD *v4; // rax
_DWORD *v5; // rdi

if ( a1 )
{
v4 = (_DWORD *)*a1;
if ( *a1 )
{
v5 = (_DWORD *)a1[a2];
if ( !v5 )
v5 = v4;
pixmap_draw(v5, (unsigned int)(a3 - *v5), (unsigned int)(a4 - v5[1]));
}
}
}

original_code:
void
shape_draw(Shape *shape, int state, int x, int y)
{
    Pixmap *pixmap;
    if (shape == ((void *)0))
        return;
    if (shape->pixmaps[0] == ((void *)0))
        return;
    if (shape->pixmaps[state] == ((void *)0))
        pixmap = shape->pixmaps[0];
    else
        pixmap = shape->pixmaps[state];
    pixmap_draw(pixmap, x - pixmap->width, y - pixmap->height);
}

canonical_original_code:
void shape_draw(struct Shape *shape, int state, int x, int y)
{
    struct Pixmap *pixmap;
    if (shape == ((void *)0))
        return;
    if (shape->pixmaps[0] == ((void *)0))
        return;
    if (shape->pixmaps[state] == ((void *)0))
        pixmap = shape->pixmaps[0];
    else
        pixmap = shape->pixmaps[state];
    pixmap_draw(pixmap, x - pixmap->width, y - pixmap->height);
}

variable_types: struct Shape { struct Pixmap { int width; int height; int datasize; int hasalpha; unsigned char alpha; unsigned char *data; } *pixmaps[4]; int pixmaps_num; } *

variable_types: state int

variable_types: x int

variable_types: y int


decompiled_code:
int __fastcall portrait_draw(unsigned int a1, unsigned int a2, unsigned int a3)
{
__int64 v4; // rdx

if ( a1 > 0x2D )
return fprintf(stderr, "lotr: invalid portrait num %d\n", a1);
v4 = portraits_cache[a1];
if ( !v4 )
return fprintf(stderr, "lotr: invalid portrait num %d\n", a1);
graphics_set_palette(*(_QWORD *)(v4 + 8), 96LL, 32LL);
return pixmap_draw(*(_QWORD *)portraits_cache[a1], a2, a3);
}

canonical_decompiled_code:
int __fastcall func5(unsigned int a1, unsigned int a2, unsigned int a3)
{
__int64 v4; // rdx

if ( a1 > 0x2D )
return fprintf(stderr, "lotr: invalid portrait num %d\n", a1);
v4 = portraits_cache[a1];
if ( !v4 )
return fprintf(stderr, "lotr: invalid portrait num %d\n", a1);
graphics_set_palette(*(_QWORD *)(v4 + 8), 96LL, 32LL);
return pixmap_draw(*(_QWORD *)portraits_cache[a1], a2, a3);
}

original_code:
void
portrait_draw(int index, int x, int y)
{
    if (index < 0 || index >= 46 || portraits_cache[index] == ((void *)0)) {
        fprintf(stderr, "lotr: invalid portrait num %d\n", index);
        return;
    }
    graphics_set_palette(portraits_cache[index]->palette, 0x60, 0x20);
    pixmap_draw(portraits_cache[index]->pixmap, x, y);
}


decompiled_code:
__int64 __fastcall shape_get_weapon_shape_id(unsigned __int64 a1, int a2)
{
__int64 result; // rax
__int64 v3; // rdx
unsigned __int64 v4; // r8

if ( !shapes_cache[6 * (int)a1 + 4] )
return 0xFFFFFFFFLL;
if ( !(_DWORD)a1 || (_DWORD)a1 == 3 )
{
result = 222LL;
}
else if ( (unsigned int)a1 > 0x23 || (result = 222LL, v3 = 0x902433030LL, !_bittest64(&v3, a1)) )
{
result = 0xFFFFFFFFLL;
if ( (unsigned int)(a1 - 98) <= 0x11 )
{
v4 = (0x21009uLL >> ((unsigned __int8)a1 - 98)) & 1;
result = (unsigned int)-(v4 == 0);
LOBYTE(result) = v4 == 0 ? -1 : 0xDE;
}
}
switch ( (_DWORD)a1 )
{
case 0x13:
result = 336LL;
break;
case 0x15:
LODWORD(result) = 456;
break;
case 0x17:
LODWORD(result) = 396;
break;
case 0x22:
LODWORD(result) = 516;
break;
default:
if ( (_DWORD)result == -1 )
return result;
break;
}
result = (unsigned int)(result + 6 * a2);
if ( !shapes_cache[(int)result] )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int64 a1, int a2)
{
__int64 result; // rax
__int64 v3; // rdx
unsigned __int64 v4; // r8

if ( !shapes_cache[6 * (int)a1 + 4] )
return 0xFFFFFFFFLL;
if ( !(_DWORD)a1 || (_DWORD)a1 == 3 )
{
result = 222LL;
}
else if ( (unsigned int)a1 > 0x23 || (result = 222LL, v3 = 0x902433030LL, !_bittest64(&v3, a1)) )
{
result = 0xFFFFFFFFLL;
if ( (unsigned int)(a1 - 98) <= 0x11 )
{
v4 = (0x21009uLL >> ((unsigned __int8)a1 - 98)) & 1;
result = (unsigned int)-(v4 == 0);
LOBYTE(result) = v4 == 0 ? -1 : 0xDE;
}
}
switch ( (_DWORD)a1 )
{
case 0x13:
result = 336LL;
break;
case 0x15:
LODWORD(result) = 456;
break;
case 0x17:
LODWORD(result) = 396;
break;
case 0x22:
LODWORD(result) = 516;
break;
default:
if ( (_DWORD)result == -1 )
return result;
break;
}
result = (unsigned int)(result + 6 * a2);
if ( !shapes_cache[(int)result] )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int
shape_get_weapon_shape_id(int character_shape_id, int weapon_class)
{
    int weapon_bank = -1;
    if (shapes_cache[6 * character_shape_id + 4] == ((void *)0))
        return -1;
    if (character_shape_id == 0 ||
        character_shape_id == 3 ||
        character_shape_id == 4 ||
        character_shape_id == 5 ||
        character_shape_id == 12 ||
        character_shape_id == 13 ||
        character_shape_id == 16 ||
        character_shape_id == 17 ||
        character_shape_id == 22 ||
        character_shape_id == 25 ||
        character_shape_id == 32 ||
        character_shape_id == 35 ||
        character_shape_id == 98 ||
        character_shape_id == 101 ||
        character_shape_id == 110 ||
        character_shape_id == 115 ||
        0)
        weapon_bank = 222;
    if (character_shape_id == 19)
        weapon_bank = 336;
    if (character_shape_id == 21)
        weapon_bank = 456;
    if (character_shape_id == 23)
        weapon_bank = 396;
    if (character_shape_id == 34)
        weapon_bank = 516;
    if (weapon_bank != -1
        && shapes_cache[weapon_bank + 6 * weapon_class] != ((void *)0))
        return weapon_bank + 6 * weapon_class;
    return -1;
}


merge

original_code:
void merge(void *starts, void *midd, size_t num_elems, Compare_fn cmp, size_t size_elem){
  char *place = calloc(num_elems, size_elem);
  size_t mid = num_elems/2;
  size_t i = 0, k = 0;
  char *jp = place;
  while ((i < mid) && (k < num_elems - mid)){
    if (cmp(starts+(i*size_elem), midd+(k*size_elem)) <= 0){
      memcpy(jp, starts+(i*size_elem), size_elem);
      i++;
    }
    else {
      memcpy(jp, midd+(k*size_elem), size_elem);
      k++;
    }
    jp += size_elem;
  }
  while (i < mid){
    memcpy(jp, starts+(i*size_elem), size_elem);
    jp += size_elem;
    i++;
  }
  while (k < (num_elems - mid)){
    memcpy(jp, midd+(k*size_elem), size_elem);
    jp += size_elem;
    k++;
  }
  memcpy(starts, place, size_elem*num_elems);
  free(place);
}

canonical_original_code:
void merge(void *starts, void *midd, unsigned long num_elems, int (*cmp)(void *, void *), unsigned long size_elem){
  char *place = calloc(num_elems, size_elem);
  unsigned long mid = num_elems/2;
  unsigned long i = 0; unsigned long k = 0;
  char *jp = place;
  while ((i < mid) && (k < num_elems - mid)){
    if (cmp(starts+(i*size_elem), midd+(k*size_elem)) <= 0){
      memcpy(jp, starts+(i*size_elem), size_elem);
      i++;
    }
    else {
      memcpy(jp, midd+(k*size_elem), size_elem);
      k++;
    }
    jp += size_elem;
  }
  while (i < mid){
    memcpy(jp, starts+(i*size_elem), size_elem);
    jp += size_elem;
    i++;
  }
  while (k < (num_elems - mid)){
    memcpy(jp, midd+(k*size_elem), size_elem);
    jp += size_elem;
    k++;
  }
  memcpy(starts, place, size_elem*num_elems);
  free(place);
}

variable_types:
starts
void *

variable_types:
midd
void *

variable_types:
cmp
int (*)(void *, void *)

variable_types:
place
char *

variable_types:
mid
unsigned long

variable_types:
i
unsigned long

variable_types:
k
unsigned long

variable_types:
jp
char *


decompiled_code:
void __fastcall st_mergesort(char *a1, size_t a2, size_t a3, int (__fastcall *a4)(char *, size_t))
{
__int64 v6; // r14

if ( a4 != 0LL && a1 != 0LL && a2 > 1 )
{
v6 = (__int64)&a1[a3 * (a2 >> 1)];
st_mergesort(a1, a2 >> 1, a3, a4);
st_mergesort(v6, a2 - (a2 >> 1), a3, a4);
merge(a1, v6, a2, a4, a3);
}
}

original_code:
void st_mergesort(void *arr, size_t num_elem, size_t elem_size, Compare_fn cmp){
  void *middle = arr + elem_size*(num_elem/2);
  if (arr == ((void *)0) || cmp == ((void *)0)){
    return;
  }
  if (num_elem <= 1){
    return;
  }
  st_mergesort(arr, num_elem/2, elem_size, cmp);
  st_mergesort(middle, num_elem - num_elem/2, elem_size, cmp);
  merge(arr, middle, num_elem, cmp, elem_size);
  return;
}

canonical_original_code:
void st_mergesort(void *arr, unsigned long num_elem, unsigned long elem_size, int (*cmp)(void *, void *)){
  void *middle = arr + elem_size*(num_elem/2);
  if (arr == ((void *)0) || cmp == ((void *)0)){
    return;
  }
  if (num_elem <= 1){
    return;
  }
  st_mergesort(arr, num_elem/2, elem_size, cmp);
  st_mergesort(middle, num_elem - num_elem/2, elem_size, cmp);
  merge(arr, middle, num_elem, cmp, elem_size);
  return;
}

variable_types:
arr
void *

variable_types:
middle
void *


decompiled_code:
void *__fastcall mt_thread(void *a1)
{
char *v1; // rbx
size_t v2; // rbp
size_t v3; // r12
int (__fastcall *v4)(char *, size_t); // r13

if ( a1 )
{
v1 = *(char **)a1;
v2 = *((_QWORD *)a1 + 1);
v3 = *((_QWORD *)a1 + 2);
v4 = (int (__fastcall *)(char *, size_t))*((_QWORD *)a1 + 3);
pthread_mutex_lock(&mutex);
st_mergesort(v1, v2, v3, v4);
pthread_mutex_unlock(&mutex);
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func10(void *a1)
{
char *v1; // rbx
size_t v2; // rbp
size_t v3; // r12
int (__fastcall *v4)(char *, size_t); // r13

if ( a1 )
{
v1 = *(char **)a1;
v2 = *((_QWORD *)a1 + 1);
v3 = *((_QWORD *)a1 + 2);
v4 = (int (__fastcall *)(char *, size_t))*((_QWORD *)a1 + 3);
pthread_mutex_lock(&mutex);
func9(v1, v2, v3, v4);
pthread_mutex_unlock(&mutex);
}
return 0LL;
}

original_code:
void *mt_thread(void * arg){
  struct info info;
  if (arg == ((void *)0)){
    return ((void *)0);
  }
  info = * (struct info *) arg;
  pthread_mutex_lock(&mutex);
  st_mergesort(info.arr, info.num_elem, info.elem_size, info.cmp);
  pthread_mutex_unlock(&mutex);
  return ((void *)0);
}

canonical_original_code:
void *mt_thread(void *arg){
  struct info info;
  if (arg == ((void *)0)){
    return ((void *)0);
  }
  info = * (struct info *) arg;
  pthread_mutex_lock(&mutex);
  st_mergesort(info.arr, info.num_elem, info.elem_size, info.cmp);
  pthread_mutex_unlock(&mutex);
  return ((void *)0);
}

variable_types:
arg
void *

variable_types: struct info { void *arr; unsigned long num_elem; unsigned long elem_size; int (*cmp)(void *, void *); }

return_type: void *


original_code:
void mt_mergesort(void *arr, size_t num_elem, int chunk_size, size_t elem_size, Compare_fn cmp, int num_threads){
  pthread_t tids[num_threads];
  struct info info[num_threads];
  int i;
  if (num_threads < 1 || num_threads > 1024 || chunk_size < 1 || chunk_size > (2^30)){
    return;
  }
  if (arr == ((void *)0) || cmp == ((void *)0)){
    return;
  }
  for (i = 0; i < num_threads; i++){
    info[i].arr = (char *)arr + i*((num_elem/chunk_size)*elem_size);
    info[i].num_elem = num_elem/2;
    info[i].elem_size = elem_size;
    info[i].cmp = cmp;
  }
  for(i = 0; i < num_threads; i++) {
    pthread_create(&tids[i], ((void *)0), mt_thread, &info[i]);
  }
  for(i = 0; i < num_threads; i++) {
    pthread_join(tids[i], ((void *)0));
  }
  pthread_mutex_lock(&mutex);
  st_mergesort(arr, num_elem, elem_size, cmp);
  pthread_mutex_unlock(&mutex);
  return;
}

canonical_original_code:
void mt_mergesort(void *arr, unsigned long num_elem, int chunk_size, unsigned long elem_size, int (*cmp)(void *, void *), int num_threads){
  unsigned long tids[-1];
  struct info info[-1];
  int i;
  if (num_threads < 1 || num_threads > 1024 || chunk_size < 1 || chunk_size > (2^30)){
    return;
  }
  if (arr == ((void *)0) || cmp == ((void *)0)){
    return;
  }
  for (i = 0; i < num_threads; i++){
    info[i].arr = (char *)arr + i*((num_elem/chunk_size)*elem_size);
    info[i].num_elem = num_elem/2;
    info[i].elem_size = elem_size;
    info[i].cmp = cmp;
  }
  for(i = 0; i < num_threads; i++) {
    pthread_create(&tids[i], ((void *)0), mt_thread, &info[i]);
  }
  for(i = 0; i < num_threads; i++) {
    pthread_join(tids[i], ((void *)0));
  }
  pthread_mutex_lock(&mutex);
  st_mergesort(arr, num_elem, elem_size, cmp);
  pthread_mutex_unlock(&mutex);
  return;
}

variable_types:
tids
unsigned long [-1]

variable_types: struct info { void *arr; unsigned long num_elem; unsigned long elem_size; int (*cmp)(void *, void *); } [-1]

variable_types: i int


repo: jgaeddert/liquid-fpm

decompiled_code:
float __fastcall q32_fixed_to_float(int a1)
{
return (float)a1 * 0.000000029802322;
}

canonical_decompiled_code:
float __fastcall func0(int a1)
{
return (float)a1 * 0.000000029802322;
}

original_code: static inline float q32_fixed_to_float(q32_t _x) { return (float) (_x) / (float)(1 << 25); }

canonical_original_code: static inline float q32_fixed_to_float(int _x) { return (float) (_x) / (float)(1 << 25); }

return_type: float


decompiled_code:
__int64 __fastcall q32_float_to_fixed(float a1)
{
return (unsigned int)(int)(float)((float)(a1 * 33554432.0) + 0.5);
}

canonical_decompiled_code:
__int64 __fastcall func1(float a1)
{
return (unsigned int)(int)(float)((float)(a1 * 33554432.0) + 0.5);
}

original_code: static inline q32_t q32_float_to_fixed(float _x) { return (q32_t) (_x * (float)(1 << 25) + 0.5f); }

canonical_original_code: static inline int q32_float_to_fixed(float _x) { return (q32_t) (_x * (float)(1 << 25) + 0.5f); }


decompiled_code:
float __fastcall q32_angle_fixed_to_float(int a1)
{
return q32_fixed_to_float(a1) * 0.098174773;
}

canonical_decompiled_code:
float __fastcall func2(int a1)
{
return func0(a1) * 0.098174773;
}

original_code: static inline float q32_angle_fixed_to_float(q32_t _x) { return q32_fixed_to_float(_x) * q32_angle_scalarf; }

canonical_original_code: static inline float q32_angle_fixed_to_float(int _x) { return q32_fixed_to_float(_x) * q32_angle_scalarf; }


decompiled_code:
float __fastcall loglogbesseli0(float a1)
{
float v1; // xmm0_4
float v2; // xmm2_4
int v3; // xmm0_4
int *v4; // rax

if ( a1 <= 0.0 )
{
puts("error: _z must be greater than zero");
exit(0);
}
v1 = log2f(a1);
v2 = v1;
if ( *(float *)&r1 <= v1 )
{
if ( *(float *)&r2 <= v1 )
{
v3 = pr3;
v4 = &pr3;
}
else
{
v3 = pr2;
v4 = &pr2;
}
}
else
{
v3 = pr1;
v4 = &pr1;
}
return (float)((float)((float)((float)(v2 * *((float *)v4 + 1)) * v2)
+ (float)((float)((float)(*(float *)&v3 * v2) * v2) * v2))
+ (float)(v2 * *((float *)v4 + 2)))
+ *((float *)v4 + 3);
}

canonical_decompiled_code:
float __fastcall func0(float a1)
{
float v1; // xmm0_4
float v2; // xmm2_4
int v3; // xmm0_4
int *v4; // rax

if ( a1 <= 0.0 )
{
puts("error: _z must be greater than zero");
exit(0);
}
v1 = log2f(a1);
v2 = v1;
if ( *(float *)&r1 <= v1 )
{
if ( *(float *)&r2 <= v1 )
{
v3 = pr3;
v4 = &pr3;
}
else
{
v3 = pr2;
v4 = &pr2;
}
}
else
{
v3 = pr1;
v4 = &pr1;
}
return (float)((float)((float)((float)(v2 * *((float *)v4 + 1)) * v2)
+ (float)((float)((float)(*(float *)&v3 * v2) * v2) * v2))
+ (float)(v2 * *((float *)v4 + 2)))
+ *((float *)v4 + 3);
}

original_code:
float loglogbesseli0(float _z)
{
    if (_z <= 0) {
        printf("error: _z must be greater than zero\n");
        exit(0);
    }
    float t = log2f(_z);
    float y;
    float * p = ((void *)0);
    if (t < r1) {
        p = pr1;
    } else if (t < r2) {
        p = pr2;
    } else {
        p = pr3;
    }
    y = p[0]*t*t*t + p[1]*t*t + p[2]*t + p[3];
    return y;
}

canonical_original_code:
float loglogbesseli0(float _z)
{
    if (_z <= 0) {
        printf("error: _z must be greater than zero\n");
        exit(0);
    }
    float t = log2f(_z);
    float y;
    float *p = ((void *)0);
    if (t < r1) {
        p = pr1;
    } else if (t < r2) {
        p = pr2;
    } else {
        p = pr3;
    }
    y = p[0]*t*t*t + p[1]*t*t + p[2]*t + p[3];
    return y;
}

variable_types: t float

variable_types: y float

variable_types:
p
float *


kaiser

decompiled_code:
float __fastcall kaiser(int a1, int a2, float a3)
{
float v3; // xmm0_4
float v4; // xmm1_4
float v5; // xmm1_4
float v6; // xmm0_4
float v7; // xmm0_4
float v8; // xmm1_4
float v11; // [rsp+Ch] [rbp-Ch]

v3 = (float)((float)((float)a1 - (float)((float)(a2 - 1) * 0.5)) + (float)((float)a1 - (float)((float)(a2 - 1) * 0.5)))
/ (float)a2;
v4 = 1.0 - (float)(v3 * v3);
if ( v4 < 0.0 )
v5 = sqrtf(v4);
else
v5 = fsqrt(v4);
v6 = loglogbesseli0(v5 * a3);
v11 = powf(2.0, v6);
v7 = loglogbesseli0(a3);
v8 = v11 - powf(2.0, v7);
return powf(2.0, v8);
}

canonical_decompiled_code:
float __fastcall func1(int a1, int a2, float a3)
{
float v3; // xmm0_4
float v4; // xmm1_4
float v5; // xmm1_4
float v6; // xmm0_4
float v7; // xmm0_4
float v8; // xmm1_4
float v11; // [rsp+Ch] [rbp-Ch]

v3 = (float)((float)((float)a1 - (float)((float)(a2 - 1) * 0.5)) + (float)((float)a1 - (float)((float)(a2 - 1) * 0.5)))
/ (float)a2;
v4 = 1.0 - (float)(v3 * v3);
if ( v4 < 0.0 )
v5 = sqrtf(v4);
else
v5 = fsqrt(v4);
v6 = func0(v5 * a3);
v11 = powf(2.0, v6);
v7 = func0(a3);
v8 = v11 - powf(2.0, v7);
return powf(2.0, v8);
}

original_code:
float kaiser(unsigned int _n, unsigned int _N, float _beta)
{
    float t = (float)_n - (float)(_N-1)/2;
    float r = 2.0f*t / (float)(_N);
    float log2a = powf(2.0f, loglogbesseli0(_beta*sqrtf(1-r*r)));
    float log2b = powf(2.0f, loglogbesseli0(_beta));
    float w = powf(2.0f, log2a - log2b);
    return w;
}

variable_types: r float

variable_types: w float


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int i; // ebx
float v4; // xmm0_4

for ( i = 0; i != 21; printf("w(%3u) = %12.8f;\n", i, v4) )
v4 = kaiser(i++, 21, 7.0);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int i; // ebx
float v4; // xmm0_4

for ( i = 0; i != 21; printf("w(%3u) = %12.8f;\n", i, v4) )
v4 = func1(i++, 21, 7.0);
return 0;
}

original_code:
int main() {
    unsigned int n = 21;
    float beta = 7.0f;
    unsigned int k;
    float wk;
    for (k=0; k<n; k++) {
        wk = kaiser(k,n,beta);
        printf("w(%3u) = %12.8f;\n", k+1, wk);
    }
    return 0;
}

variable_types:
n
unsigned int

variable_types: beta float

variable_types:
k
unsigned int

variable_types: wk float


repo: jgavert/skiplist

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r13
unsigned int v4; // eax
unsigned int v5; // ebp
unsigned int v6; // er12
unsigned int v7; // ebx
unsigned int *v8; // rax
int *v9; // rbp
int v10; // ebp
__int64 v11; // r14
int v12; // ebx
int v13; // ebp
__int64 v14; // rbx
int v15; // er15
_DWORD *v16; // rax
unsigned int *v17; // rax
__int64 v18; // rsi

v3 = SK_createList(10LL, argv, envp);
v4 = time(0LL);
srand(v4);
v5 = 0;
v6 = 0;
do
{
v7 = rand() % 2000 - 1000;
if ( (int)v6 < (int)v7 )
v6 = v7;
printf("Adding %d. node, value %d\n", v5, v7);
if ( !(unsigned int)SK_insert(v3, v7) )
puts("Already added");
++v5;
}
while ( v5 != 15 );
SK_print(v3);
v8 = (unsigned int *)SK_find(v3, v7);
v9 = (int *)v8;
if ( v8 )
{
printf("Found: %d\n", *v8);
v10 = *v9;
printf("Deleting %d...\n", v7);
SK_delete(v3, v7);
printf("Inserting: %d\n", (unsigned int)(int)((double)v10 * 0.5));
SK_insert(v3, (unsigned int)(int)((double)v10 * 0.5));
}
else
{
puts("Didn't find");
}
SK_print2(v3);
v11 = SK_innerfind(v3, v6);
printf("found %d by going from ", v6);
v12 = *(_DWORD *)(v3 + 4) - 1;
if ( v12 >= 0 )
{
v13 = *(_DWORD *)(v3 + 4) - 2;
v14 = 8LL * v12;
v15 = 0;
while ( 1 )
{
v17 = *(unsigned int **)(v11 + v14);
if ( v17 == (unsigned int *)v3 )
goto LABEL_12;
v18 = *v17;
if ( (_DWORD)v18 == v15 )
goto LABEL_14;
printf("%d", v18);
v15 = **(_DWORD **)(v11 + v14);
if ( v13 + 1 <= 0 )
{
LABEL_12:
v16 = *(_DWORD **)(*(_QWORD *)(*(_QWORD *)(v11 + v14) + 8LL) + v14);
if ( !v16 )
goto LABEL_14;
}
else
{
printf(" -> ");
v16 = *(_DWORD **)(*(_QWORD *)(*(_QWORD *)(v11 + v14) + 8LL) + v14);
if ( !v16 )
goto LABEL_15;
}
if ( v6 == *v16 )
break;
LABEL_14:
if ( v13 < 0 )
break;
LABEL_15:
--v13;
v14 -= 8LL;
}
}
putchar(10);
SK_free(v3);
return 1;
}

original_code:
int main()
{
  Node* skiplist = SK_createList(10);
  srand(time(0));
  int luku;
  int max = 0;
  for (int i=0;i<15;i++) {
    luku = (rand()%2000)-1000;
    if (luku > max)
      max = luku;
    printf("Adding %d. node, value %d\n", i, luku);
    if (!SK_insert(skiplist, luku))
      printf("Already added\n");
  }
  SK_print(skiplist);
  Node* tmp = SK_find(skiplist, luku);
  if (tmp != ((void *)0))
  {
    printf("Found: %d\n", tmp->value);
    int luku2 = tmp->value;
    printf("Deleting %d...\n", luku);
    SK_delete(skiplist, luku);
    printf("Inserting: %d\n", (int)(luku2/2.0));
    SK_insert(skiplist, (int)(luku2/2.0));
  }
  else
    printf("Didn't find\n");
  SK_print2(skiplist);
  Node** list = SK_innerfind(skiplist, max);
  printf("found %d by going from ", max);
  int last = 0;
  for (int i=skiplist->height-1;i>=0;i--)
  {
    if (list[i] != skiplist)
    {
      if (last == list[i]->value)
        continue;
      printf("%d", list[i]->value);
      last = list[i]->value;
      if (i>0)
        printf(" -> ", list[i]->value);
    }
    if (list[i]->list[i] != ((void *)0) && list[i]->list[i]->value == max)
      break;
  }
  printf("\n");
  SK_free(skiplist);
  return 1;
}

canonical_original_code:
int main()
{
  struct _node *skiplist = SK_createList(10);
  srand(time(0));
  int luku;
  int max = 0;
  for (int i = 0;i<15;i++) {
    luku = (rand()%2000)-1000;
    if (luku > max)
      max = luku;
    printf("Adding %d. node, value %d\n", i, luku);
    if (!SK_insert(skiplist, luku))
      printf("Already added\n");
  }
  SK_print(skiplist);
  struct _node *tmp = SK_find(skiplist, luku);
  if (tmp != ((void *)0))
  {
    printf("Found: %d\n", tmp->value);
    int luku2 = tmp->value;
    printf("Deleting %d...\n", luku);
    SK_delete(skiplist, luku);
    printf("Inserting: %d\n", (int)(luku2/2.0));
    SK_insert(skiplist, (int)(luku2/2.0));
  }
  else
    printf("Didn't find\n");
  SK_print2(skiplist);
  struct _node **list = SK_innerfind(skiplist, max);
  printf("found %d by going from ", max);
  int last = 0;
  for (int i = skiplist->height-1;i>=0;i--)
  {
    if (list[i] != skiplist)
    {
      if (last == list[i]->value)
        continue;
      printf("%d", list[i]->value);
      last = list[i]->value;
      if (i>0)
        printf(" -> ", list[i]->value);
    }
    if (list[i]->list[i] != ((void *)0) && list[i]->list[i]->value == max)
      break;
  }
  printf("\n");
  SK_free(skiplist);
  return 1;
}

variable_types: luku int

variable_types: max int

variable_types: struct _node { int value; unsigned int height; struct _node **list; } **

variable_types: last int


repo: jgor/combogen

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
char *v4; // r13
char *v5; // rax
__int64 i; // rcx
int v7; // ebp
int result; // eax
char *v9; // rbx
__int64 v10; // rdx
unsigned int v11; // [rsp+Ch] [rbp-9Ch]
int v12[10]; // [rsp+10h] [rbp-98h] BYREF
char v13; // [rsp+38h] [rbp-70h] BYREF
int v14[10]; // [rsp+40h] [rbp-68h] BYREF
char v15[64]; // [rsp+68h] [rbp-40h] BYREF

switch ( argc )
{
case 2:
v11 = atoi(argv[1]);
v3 = 4;
break;
case 3:
v11 = atoi(argv[1]);
v3 = atoi(argv[2]);
break;
case 1:
printf("Last Digit: ");
__isoc99_scanf();
v3 = 4;
break;
default:
fwrite("Too many arguments.\n", 1uLL, 0x14uLL, _bss_start);
exit(1);
}
if ( v11 > 0x27 )
{
puts("Last Digit must be 0-39.");
result = 1;
}
else
{
puts("Possible Combinations:");
v4 = (char *)v14;
v5 = (char *)v14;
do
{
*(_DWORD *)v5 = (int)v11 % 4 - (unsigned int)v14 + (_DWORD)v5;
v5 += 4;
}
while ( v5 != v15 );
for ( i = 0LL; i != 10; ++i )
v12[i] = (v14[i] + 2) % 40;
v7 = 0;
do
{
v9 = (char *)v12;
do
{
v10 = *(unsigned int *)v9;
if ( (_DWORD)v10 != v11
&& v11 != ((int)v10 + 38) % 40
&& v11 != ((int)v10 + 39) % 40
&& v11 != ((int)v10 + 41) % 40
&& v11 != ((int)v10 + 42) % 40 )
{
printf("%02d-%02d-%02d ", *(unsigned int *)v4, v10, v11);
if ( !(++v7 % v3) )
putchar(10);
}
v9 += 4;
}
while ( &v13 != v9 );
v4 += 4;
}
while ( v4 != v15 );
putchar(10);
result = 0;
}
return result;
}

original_code:
int main(int argc, char **argv) {
  int first[10], middle[10], last, columns, count, i, j;
  switch(argc-1) {
    case 0:
      columns = 4;
      printf("Last Digit: ");
      scanf("%d", &last);
      break;
    case 1:
      last = atoi(argv[1]);
      columns = 4;
      break;
    case 2:
      last = atoi(argv[1]);
      columns = atoi(argv[2]);
      break;
    default:
      fprintf(stderr, "Too many arguments.\n");
      exit(1);
  }
  if (last < 0 || last > 39) {
    printf("Last Digit must be 0-39.\n");
    return 1;
  }
  printf("Possible Combinations:\n");
  count = 0;
  for (i = 0; i < 10; ++i) {
    first[i] = (4 * i) + (last % 4);
  }
  for (i = 0; i < 10; ++i) {
    middle[i] = (first[i] + 2) % 40;
  }
  int skip = 0;
  for (i = 0; i < 10; ++i) {
    for (j = 0; j < 10; ++j) {
      if (middle[j] == last ||
          (40+middle[j]-2)%40 == last || (40+middle[j]-1)%40 == last ||
          (40+middle[j]+1)%40 == last || (40+middle[j]+2)%40 == last) {
        skip++;
        continue;
      }
      printf("%02d-%02d-%02d  ", first[i], middle[j], last);
      count++;
      if (count % columns == 0) {
        printf("\n");
      }
    }
  }
  printf("\n");
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  int first[10]; int middle[10]; int last; int columns; int count; int i; int j;
  switch(argc-1) {
    case 0:
      columns = 4;
      printf("Last Digit: ");
      scanf("%d", &last);
      break;
    case 1:
      last = atoi(argv[1]);
      columns = 4;
      break;
    case 2:
      last = atoi(argv[1]);
      columns = atoi(argv[2]);
      break;
    default:
      fprintf(stderr, "Too many arguments.\n");
      exit(1);
  }
  if (last < 0 || last > 39) {
    printf("Last Digit must be 0-39.\n");
    return 1;
  }
  printf("Possible Combinations:\n");
  count = 0;
  for (i = 0; i < 10; ++i) {
    first[i] = (4 * i) + (last % 4);
  }
  for (i = 0; i < 10; ++i) {
    middle[i] = (first[i] + 2) % 40;
  }
  int skip = 0;
  for (i = 0; i < 10; ++i) {
    for (j = 0; j < 10; ++j) {
      if (middle[j] == last ||
          (40+middle[j]-2)%40 == last || (40+middle[j]-1)%40 == last ||
          (40+middle[j]+1)%40 == last || (40+middle[j]+2)%40 == last) {
        skip++;
        continue;
      }
      printf("%02d-%02d-%02d  ", first[i], middle[j], last);
      count++;
      if (count % columns == 0) {
        printf("\n");
      }
    }
  }
  printf("\n");
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
first
int [10]

variable_types:
middle
int [10]

variable_types: columns int

variable_types: count int

variable_types: j int

variable_types: skip int


repo: jgottula/cowsync

decompiled_code:
int print_stats()
{
float v0; // xmm1_4
float v1; // xmm2_4
float v2; // xmm2_4
float v3; // xmm2_4
float v5; // [rsp+4h] [rbp-14h]
float v6; // [rsp+8h] [rbp-10h]
float v7; // [rsp+Ch] [rbp-Ch]

if ( is_first_0 )
is_first_0 = 0;
else
fwrite("\x1B[4A", 1uLL, 4uLL, stderr);
v0 = (float)(int)off;
if ( off )
{
if ( b_written < 0 )
v1 = (float)(b_written & 1 | (unsigned int)((unsigned __int64)b_written >> 1))
+ (float)(b_written & 1 | (unsigned int)((unsigned __int64)b_written >> 1));
else
v1 = (float)(int)b_written;
v5 = (float)(v1 / v0) * 100.0;
if ( b_zeroed < 0 )
v2 = (float)(b_zeroed & 1 | (unsigned int)((unsigned __int64)b_zeroed >> 1))
+ (float)(b_zeroed & 1 | (unsigned int)((unsigned __int64)b_zeroed >> 1));
else
v2 = (float)(int)b_zeroed;
v6 = (float)(v2 / v0) * 100.0;
if ( b_punched < 0 )
v3 = (float)(b_punched & 1 | (unsigned int)((unsigned __int64)b_punched >> 1))
+ (float)(b_punched & 1 | (unsigned int)((unsigned __int64)b_punched >> 1));
else
v3 = (float)(int)b_punched;
v7 = (float)(v3 / v0) * 100.0;
}
else
{
v7 = 0.0;
v6 = 0.0;
v5 = 0.0;
}
fprintf(
stderr,
"\x1B[K read: %9ldK %5.1f%%\n",
off / 1024,
(float)((float)((float)(int)off / (float)(int)len_src) * 100.0));
fprintf(stderr, "\x1B[Kwritten: %9ldK %5.1f%%\n", (unsigned __int64)b_written >> 10, v5);
fprintf(stderr, "\x1B[K zeroed: %9ldK %5.1f%%\n", (unsigned __int64)b_zeroed >> 10, v6);
return fprintf(stderr, "\x1B[Kpunched: %9ldK %5.1f%%\n", (unsigned __int64)b_punched >> 10, v7);
}

canonical_decompiled_code:
int func0()
{
float v0; // xmm1_4
float v1; // xmm2_4
float v2; // xmm2_4
float v3; // xmm2_4
float v5; // [rsp+4h] [rbp-14h]
float v6; // [rsp+8h] [rbp-10h]
float v7; // [rsp+Ch] [rbp-Ch]

if ( is_first_0 )
is_first_0 = 0;
else
fwrite("\x1B[4A", 1uLL, 4uLL, stderr);
v0 = (float)(int)off;
if ( off )
{
if ( b_written < 0 )
v1 = (float)(b_written & 1 | (unsigned int)((unsigned __int64)b_written >> 1))
+ (float)(b_written & 1 | (unsigned int)((unsigned __int64)b_written >> 1));
else
v1 = (float)(int)b_written;
v5 = (float)(v1 / v0) * 100.0;
if ( b_zeroed < 0 )
v2 = (float)(b_zeroed & 1 | (unsigned int)((unsigned __int64)b_zeroed >> 1))
+ (float)(b_zeroed & 1 | (unsigned int)((unsigned __int64)b_zeroed >> 1));
else
v2 = (float)(int)b_zeroed;
v6 = (float)(v2 / v0) * 100.0;
if ( b_punched < 0 )
v3 = (float)(b_punched & 1 | (unsigned int)((unsigned __int64)b_punched >> 1))
+ (float)(b_punched & 1 | (unsigned int)((unsigned __int64)b_punched >> 1));
else
v3 = (float)(int)b_punched;
v7 = (float)(v3 / v0) * 100.0;
}
else
{
v7 = 0.0;
v6 = 0.0;
v5 = 0.0;
}
fprintf(
stderr,
"\x1B[K read: %9ldK %5.1f%%\n",
off / 1024,
(float)((float)((float)(int)off / (float)(int)len_src) * 100.0));
fprintf(stderr, "\x1B[Kwritten: %9ldK %5.1f%%\n", (unsigned __int64)b_written >> 10, v5);
fprintf(stderr, "\x1B[K zeroed: %9ldK %5.1f%%\n", (unsigned __int64)b_zeroed >> 10, v6);
return fprintf(stderr, "\x1B[Kpunched: %9ldK %5.1f%%\n", (unsigned __int64)b_punched >> 10, v7);
}

original_code:
void print_stats(void) {
 static _Bool is_first = 1;
 if (!is_first) {
  fputs("\e[4A", stderr);
 } else {
  is_first = 0;
 }
 float pct_read = ((float)off / (float)len_src) * 100.;
 float pct_written = ((float)b_written / (float)off) * 100.;
 float pct_zeroed = ((float)b_zeroed / (float)off) * 100.;
 float pct_punched = ((float)b_punched / (float)off) * 100.;
 if (off == 0) {
  pct_written = 0.;
  pct_zeroed = 0.;
  pct_punched = 0.;
 }
 fprintf(stderr, "\e[K   read: %9ldK %5.1f%%\n",
  off / 1024, pct_read);
 fprintf(stderr, "\e[Kwritten: %9ldK %5.1f%%\n",
  b_written / 1024, pct_written);
 fprintf(stderr, "\e[K zeroed: %9ldK %5.1f%%\n",
  b_zeroed / 1024, pct_zeroed);
 fprintf(stderr, "\e[Kpunched: %9ldK %5.1f%%\n",
  b_punched / 1024, pct_punched);
}

canonical_original_code:
void print_stats() {
 static _Bool is_first = 1;
 if (!is_first) {
  fputs("\e[4A", stderr);
 } else {
  is_first = 0;
 }
 float pct_read = ((float)off / (float)len_src) * 100.;
 float pct_written = ((float)b_written / (float)off) * 100.;
 float pct_zeroed = ((float)b_zeroed / (float)off) * 100.;
 float pct_punched = ((float)b_punched / (float)off) * 100.;
 if (off == 0) {
  pct_written = 0.;
  pct_zeroed = 0.;
  pct_punched = 0.;
 }
 fprintf(stderr, "\e[K   read: %9ldK %5.1f%%\n",
  off / 1024, pct_read);
 fprintf(stderr, "\e[Kwritten: %9ldK %5.1f%%\n",
  b_written / 1024, pct_written);
 fprintf(stderr, "\e[K zeroed: %9ldK %5.1f%%\n",
  b_zeroed / 1024, pct_zeroed);
 fprintf(stderr, "\e[Kpunched: %9ldK %5.1f%%\n",
  b_punched / 1024, pct_punched);
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__off64_t v3; // rbx
__off64_t v4; // rbx
char *v5; // rbp
char *v6; // r13
int v7; // eax
double v8; // rbx
double v10; // [rsp+8h] [rbp-40h]

if ( argc != 3 )
errx(1, "usage: %s <src> <dst>", *argv);
mem_zero = mmap64(0LL, 0x1000uLL, 1, 34, -1, 0LL);
if ( mem_zero == (void *)-1LL )
err(1, "anonymous mmap failed");
path_src = (char *)argv[1];
path_dst = (char *)argv[2];
warnx("src: %s", path_src);
warnx("dst: %s", path_dst);
fd_src = open64(path_src, 0);
if ( fd_src < 0 )
err(1, "open failed: %s", path_src);
fd_dst = open64(path_dst, 2);
if ( fd_dst < 0 )
err(1, "open failed: %s", path_dst);
len_src = lseek64(fd_src, 0LL, 2);
lseek64(fd_src, 0LL, 0);
len_dst = lseek64(fd_dst, 0LL, 2);
lseek64(fd_dst, 0LL, 0);
warnx("src len: %ldK", len_src / 1024);
warnx("dst len: %ldK", (__int64)len_dst / 1024);
if ( len_src != len_dst )
{
warnx("truncating dst");
if ( ftruncate64(fd_dst, len_src) < 0 )
err(1, "ftruncate failed");
len_dst = lseek64(fd_dst, 0LL, 2);
lseek64(fd_dst, 0LL, 0);
if ( len_src != len_dst )
{
warnx("src and dst lens differ");
abort();
}
}
mem_src = mmap64(0LL, len_src, 1, 1, fd_src, 0LL);
if ( mem_src == (void *)-1LL )
err(1, "mmap on src failed");
mem_dst = mmap64(0LL, len_dst, 3, 1, fd_dst, 0LL);
if ( mem_dst == (void *)-1LL )
err(1, "mmap on dst failed");
if ( madvise(mem_src, len_src, 2) < 0 )
warn("madvise on src failed");
if ( madvise(mem_dst, len_dst, 2) < 0 )
warn("madvise on dst failed");
if ( clock_gettime(1, &time_before) < 0 )
time_ok = 0;
warnx("copying now");
LODWORD(v3) = off;
if ( off < len_src )
{
while ( 1 )
{
if ( (v3 & 0xFFFFF) == 0 )
print_stats();
v4 = len_src - off;
if ( len_src - off >= 4096 )
v4 = 4096LL;
v5 = (char *)mem_src + off;
v6 = (char *)mem_dst + off;
if ( off == 104857600 * (off / 104857600) && off )
{
if ( munlock(v5 - 104857600, 0x6400000uLL) < 0 )
err(1, "munlock on src failed @ %ldK", off / 1024);
if ( munlock(v6 - 104857600, 0x6400000uLL) < 0 )
err(1, "munlock on dst failed @ %ldK", off / 1024);
madvise(v5 - 104857600, 0x6400000uLL, 4);
madvise(v6 - 104857600, 0x6400000uLL, 4);
}
if ( !memcmp(v5, v6, v4) )
goto LABEL_33;
if ( memcmp(v5, mem_zero, v4) )
{
memcpy(v6, v5, v4);
b_written += v4;
goto LABEL_33;
}
if ( !falloc_ok )
goto LABEL_43;
if ( fallocate64(fd_dst, 3, off, v4) )
break;
b_punched += v4;
if ( !falloc_ok )
goto LABEL_43;
LABEL_33:
v3 = off + v4;
off = v3;
if ( v3 >= len_src )
goto LABEL_49;
}
v7 = *_errno_location();
if ( v7 != 38 && v7 != 95 && v7 != 19 )
err(1, "fallocate failed: dst @ %ldK", off / 1024);
falloc_ok = 0;
LABEL_43:
memcpy(v6, mem_zero, v4);
b_zeroed += v4;
goto LABEL_33;
}
LABEL_49:
print_stats();
warnx("syncing pages");
if ( munlockall() < 0 )
warn("munlockall failed");
if ( munmap(mem_src, len_src) < 0 )
warn("munmap on src failed");
if ( munmap(mem_dst, len_dst) < 0 )
warn("munmap on dst failed");
if ( close(fd_src) < 0 )
warn("close failed: %s", path_src);
if ( close(fd_dst) < 0 )
warn("close failed: %s", path_dst);
if ( clock_gettime(1, &time_after) < 0 )
{
time_ok = 0;
}
else if ( time_ok )
{
v8 = (double)(int)len_src * 0.00000095367431640625;
v10 = (double)(int)(1000 * (LODWORD(time_after.tv_sec) - LODWORD(time_before.tv_sec))
+ time_after.tv_nsec / 1000000
- time_before.tv_nsec / 1000000)
/ 1000.0;
warnx("elapsed time: %.1fs", 1000000LL, time_before.tv_nsec % 1000000, v10);
warnx("avg rate: %.1fM/s", v8 / v10);
}
warnx("done");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__off64_t v3; // rbx
__off64_t v4; // rbx
char *v5; // rbp
char *v6; // r13
int v7; // eax
double v8; // rbx
double v10; // [rsp+8h] [rbp-40h]

if ( argc != 3 )
errx(1, "usage: %s <src> <dst>", *argv);
mem_zero = mmap64(0LL, 0x1000uLL, 1, 34, -1, 0LL);
if ( mem_zero == (void *)-1LL )
err(1, "anonymous mmap failed");
path_src = (char *)argv[1];
path_dst = (char *)argv[2];
warnx("src: %s", path_src);
warnx("dst: %s", path_dst);
fd_src = open64(path_src, 0);
if ( fd_src < 0 )
err(1, "open failed: %s", path_src);
fd_dst = open64(path_dst, 2);
if ( fd_dst < 0 )
err(1, "open failed: %s", path_dst);
len_src = lseek64(fd_src, 0LL, 2);
lseek64(fd_src, 0LL, 0);
len_dst = lseek64(fd_dst, 0LL, 2);
lseek64(fd_dst, 0LL, 0);
warnx("src len: %ldK", len_src / 1024);
warnx("dst len: %ldK", (__int64)len_dst / 1024);
if ( len_src != len_dst )
{
warnx("truncating dst");
if ( ftruncate64(fd_dst, len_src) < 0 )
err(1, "ftruncate failed");
len_dst = lseek64(fd_dst, 0LL, 2);
lseek64(fd_dst, 0LL, 0);
if ( len_src != len_dst )
{
warnx("src and dst lens differ");
abort();
}
}
mem_src = mmap64(0LL, len_src, 1, 1, fd_src, 0LL);
if ( mem_src == (void *)-1LL )
err(1, "mmap on src failed");
mem_dst = mmap64(0LL, len_dst, 3, 1, fd_dst, 0LL);
if ( mem_dst == (void *)-1LL )
err(1, "mmap on dst failed");
if ( madvise(mem_src, len_src, 2) < 0 )
warn("madvise on src failed");
if ( madvise(mem_dst, len_dst, 2) < 0 )
warn("madvise on dst failed");
if ( clock_gettime(1, &time_before) < 0 )
time_ok = 0;
warnx("copying now");
LODWORD(v3) = off;
if ( off < len_src )
{
while ( 1 )
{
if ( (v3 & 0xFFFFF) == 0 )
func0();
v4 = len_src - off;
if ( len_src - off >= 4096 )
v4 = 4096LL;
v5 = (char *)mem_src + off;
v6 = (char *)mem_dst + off;
if ( off == 104857600 * (off / 104857600) && off )
{
if ( munlock(v5 - 104857600, 0x6400000uLL) < 0 )
err(1, "munlock on src failed @ %ldK", off / 1024);
if ( munlock(v6 - 104857600, 0x6400000uLL) < 0 )
err(1, "munlock on dst failed @ %ldK", off / 1024);
madvise(v5 - 104857600, 0x6400000uLL, 4);
madvise(v6 - 104857600, 0x6400000uLL, 4);
}
if ( !memcmp(v5, v6, v4) )
goto LABEL_33;
if ( memcmp(v5, mem_zero, v4) )
{
memcpy(v6, v5, v4);
b_written += v4;
goto LABEL_33;
}
if ( !falloc_ok )
goto LABEL_43;
if ( fallocate64(fd_dst, 3, off, v4) )
break;
b_punched += v4;
if ( !falloc_ok )
goto LABEL_43;
LABEL_33:
v3 = off + v4;
off = v3;
if ( v3 >= len_src )
goto LABEL_49;
}
v7 = *_errno_location();
if ( v7 != 38 && v7 != 95 && v7 != 19 )
err(1, "fallocate failed: dst @ %ldK", off / 1024);
falloc_ok = 0;
LABEL_43:
memcpy(v6, mem_zero, v4);
b_zeroed += v4;
goto LABEL_33;
}
LABEL_49:
func0();
warnx("syncing pages");
if ( munlockall() < 0 )
warn("munlockall failed");
if ( munmap(mem_src, len_src) < 0 )
warn("munmap on src failed");
if ( munmap(mem_dst, len_dst) < 0 )
warn("munmap on dst failed");
if ( close(fd_src) < 0 )
warn("close failed: %s", path_src);
if ( close(fd_dst) < 0 )
warn("close failed: %s", path_dst);
if ( clock_gettime(1, &time_after) < 0 )
{
time_ok = 0;
}
else if ( time_ok )
{
v8 = (double)(int)len_src * 0.00000095367431640625;
v10 = (double)(int)(1000 * (LODWORD(time_after.tv_sec) - LODWORD(time_before.tv_sec))
+ time_after.tv_nsec / 1000000
- time_before.tv_nsec / 1000000)
/ 1000.0;
warnx("elapsed time: %.1fs", 1000000LL, time_before.tv_nsec % 1000000, v10);
warnx("avg rate: %.1fM/s", v8 / v10);
}
warnx("done");
return 0;
}

original_code:
int main(int argc, char **argv) {
 if (argc != 3) {
  errx(1, "usage: %s <src> <dst>", argv[0]);
 }
 if ((mem_zero = mmap(((void *)0), 4096, 0x1,
  0x02 | 0x20, -1, 0)) == ((void *) -1)) {
  err(1, "anonymous mmap failed");
 }
 path_src = argv[1];
 path_dst = argv[2];
 warnx("src: %s", path_src);
 warnx("dst: %s", path_dst);
 if ((fd_src = open(path_src, 00)) < 0) {
  err(1, "open failed: %s", path_src);
 }
 if ((fd_dst = open(path_dst, 02)) < 0) {
  err(1, "open failed: %s", path_dst);
 }
 len_src = lseek(fd_src, 0, 2);
 lseek(fd_src, 0, 0);
 len_dst = lseek(fd_dst, 0, 2);
 lseek(fd_dst, 0, 0);
 warnx("src len: %ldK", len_src / 1024);
 warnx("dst len: %ldK", len_dst / 1024);
 if (len_src != len_dst) {
  warnx("truncating dst");
  if (ftruncate(fd_dst, len_src) < 0) {
   err(1, "ftruncate failed");
  }
  len_dst = lseek(fd_dst, 0, 2);
  lseek(fd_dst, 0, 0);
  if (len_src != len_dst) {
   warnx("src and dst lens differ");
   abort();
  }
 }
 if ((mem_src = mmap(((void *)0), len_src, 0x1, 0x01,
  fd_src, 0)) == ((void *) -1)) {
  err(1, "mmap on src failed");
 }
 if ((mem_dst = mmap(((void *)0), len_dst, 0x1 | 0x2, 0x01,
  fd_dst, 0)) == ((void *) -1)) {
  err(1, "mmap on dst failed");
 }
 if (madvise(mem_src, len_src, 2) < 0) {
  warn("madvise on src failed");
 }
 if (madvise(mem_dst, len_dst, 2) < 0) {
  warn("madvise on dst failed");
 }
 if (clock_gettime(1, &time_before) < 0) {
  time_ok = 0;
 }
 warnx("copying now");
 while (off < len_src) {
  if ((off % (1024 * 1024)) == 0) {
   print_stats();
  }
  ssize_t count = 4096;
  if ((len_src - off) < 4096) {
   count = (len_src - off);
  }
  char *ptr_src = mem_src + off;
  char *ptr_dst = mem_dst + off;
  if ((off % (100 * 1024 * 1024)) == 0) {
   size_t len_chunk = (100 * 1024 * 1024);
   if ((len_src - off) < (100 * 1024 * 1024)) {
    len_chunk = (len_src - off);
   }
   if (off != 0) {
    if (munlock(ptr_src - (100 * 1024 * 1024), (100 * 1024 * 1024)) < 0) {
     err(1, "munlock on src failed @ %ldK", off / 1024);
    }
    if (munlock(ptr_dst - (100 * 1024 * 1024), (100 * 1024 * 1024)) < 0) {
     err(1, "munlock on dst failed @ %ldK", off / 1024);
    }
    madvise(ptr_src - (100 * 1024 * 1024), (100 * 1024 * 1024), 4);
    madvise(ptr_dst - (100 * 1024 * 1024), (100 * 1024 * 1024), 4);
   }
  }
  if (memcmp(ptr_src, ptr_dst, count) != 0) {
   if (memcmp(ptr_src, mem_zero, count) == 0) {
    if (falloc_ok) {
     if (fallocate(fd_dst, 0x01 |
      0x02, off, count) == 0) {
      b_punched += count;
     } else {
      if ((*__errno_location ()) == 95 ||
       (*__errno_location ()) == 38 ||
       (*__errno_location ()) == 19) {
       falloc_ok = 0;
      } else {
       err(1, "fallocate failed: dst @ %ldK", off / 1024);
      }
     }
    }
    if (!falloc_ok) {
     memcpy(ptr_dst, mem_zero, count);
     b_zeroed += count;
    }
   } else {
    memcpy(ptr_dst, ptr_src, count);
    b_written += count;
   }
  }
  off += count;
 }
 print_stats();
 warnx("syncing pages");
 if (munlockall() < 0) {
  warn("munlockall failed");
 }
 if (munmap(mem_src, len_src) < 0) {
  warn("munmap on src failed");
 }
 if (munmap(mem_dst, len_dst) < 0) {
  warn("munmap on dst failed");
 }
 if (close(fd_src) < 0) {
  warn("close failed: %s", path_src);
 }
 if (close(fd_dst) < 0) {
  warn("close failed: %s", path_dst);
 }
 if (clock_gettime(1, &time_after) < 0) {
  time_ok = 0;
 }
 if (time_ok) {
  time_t msec_before = (time_before.tv_nsec / (1000 * 1000));
  time_t msec_after = (time_after.tv_nsec / (1000 * 1000));
  time_t msec_diff = ((time_after.tv_sec - time_before.tv_sec) * 1000) +
   (msec_after - msec_before);
  double sec_diff = (double)msec_diff / 1000.;
  double mb_total = (double)len_src / (1024. * 1024.);
  warnx("elapsed time: %.1fs", sec_diff);
  warnx("avg rate: %.1fM/s", mb_total / sec_diff);
 }
 warnx("done");
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 if (argc != 3) {
  errx(1, "usage: %s <src> <dst>", argv[0]);
 }
 if ((mem_zero = mmap(((void *)0), 4096, 0x1,
  0x02 | 0x20, -1, 0)) == ((void *) -1)) {
  err(1, "anonymous mmap failed");
 }
 path_src = argv[1];
 path_dst = argv[2];
 warnx("src: %s", path_src);
 warnx("dst: %s", path_dst);
 if ((fd_src = open(path_src, 00)) < 0) {
  err(1, "open failed: %s", path_src);
 }
 if ((fd_dst = open(path_dst, 02)) < 0) {
  err(1, "open failed: %s", path_dst);
 }
 len_src = lseek(fd_src, 0, 2);
 lseek(fd_src, 0, 0);
 len_dst = lseek(fd_dst, 0, 2);
 lseek(fd_dst, 0, 0);
 warnx("src len: %ldK", len_src / 1024);
 warnx("dst len: %ldK", len_dst / 1024);
 if (len_src != len_dst) {
  warnx("truncating dst");
  if (ftruncate(fd_dst, len_src) < 0) {
   err(1, "ftruncate failed");
  }
  len_dst = lseek(fd_dst, 0, 2);
  lseek(fd_dst, 0, 0);
  if (len_src != len_dst) {
   warnx("src and dst lens differ");
   abort();
  }
 }
 if ((mem_src = mmap(((void *)0), len_src, 0x1, 0x01,
  fd_src, 0)) == ((void *) -1)) {
  err(1, "mmap on src failed");
 }
 if ((mem_dst = mmap(((void *)0), len_dst, 0x1 | 0x2, 0x01,
  fd_dst, 0)) == ((void *) -1)) {
  err(1, "mmap on dst failed");
 }
 if (madvise(mem_src, len_src, 2) < 0) {
  warn("madvise on src failed");
 }
 if (madvise(mem_dst, len_dst, 2) < 0) {
  warn("madvise on dst failed");
 }
 if (clock_gettime(1, &time_before) < 0) {
  time_ok = 0;
 }
 warnx("copying now");
 while (off < len_src) {
  if ((off % (1024 * 1024)) == 0) {
   print_stats();
  }
  long count = 4096;
  if ((len_src - off) < 4096) {
   count = (len_src - off);
  }
  char *ptr_src = mem_src + off;
  char *ptr_dst = mem_dst + off;
  if ((off % (100 * 1024 * 1024)) == 0) {
   unsigned long len_chunk = (100 * 1024 * 1024);
   if ((len_src - off) < (100 * 1024 * 1024)) {
    len_chunk = (len_src - off);
   }
   if (off != 0) {
    if (munlock(ptr_src - (100 * 1024 * 1024), (100 * 1024 * 1024)) < 0) {
     err(1, "munlock on src failed @ %ldK", off / 1024);
    }
    if (munlock(ptr_dst - (100 * 1024 * 1024), (100 * 1024 * 1024)) < 0) {
     err(1, "munlock on dst failed @ %ldK", off / 1024);
    }
    madvise(ptr_src - (100 * 1024 * 1024), (100 * 1024 * 1024), 4);
    madvise(ptr_dst - (100 * 1024 * 1024), (100 * 1024 * 1024), 4);
   }
  }
  if (memcmp(ptr_src, ptr_dst, count) != 0) {
   if (memcmp(ptr_src, mem_zero, count) == 0) {
    if (falloc_ok) {
     if (fallocate(fd_dst, 0x01 |
      0x02, off, count) == 0) {
      b_punched += count;
     } else {
      if ((*__errno_location ()) == 95 ||
       (*__errno_location ()) == 38 ||
       (*__errno_location ()) == 19) {
       falloc_ok = 0;
      } else {
       err(1, "fallocate failed: dst @ %ldK", off / 1024);
      }
     }
    }
    if (!falloc_ok) {
     memcpy(ptr_dst, mem_zero, count);
     b_zeroed += count;
    }
   } else {
    memcpy(ptr_dst, ptr_src, count);
    b_written += count;
   }
  }
  off += count;
 }
 print_stats();
 warnx("syncing pages");
 if (munlockall() < 0) {
  warn("munlockall failed");
 }
 if (munmap(mem_src, len_src) < 0) {
  warn("munmap on src failed");
 }
 if (munmap(mem_dst, len_dst) < 0) {
  warn("munmap on dst failed");
 }
 if (close(fd_src) < 0) {
  warn("close failed: %s", path_src);
 }
 if (close(fd_dst) < 0) {
  warn("close failed: %s", path_dst);
 }
 if (clock_gettime(1, &time_after) < 0) {
  time_ok = 0;
 }
 if (time_ok) {
  long msec_before = (time_before.tv_nsec / (1000 * 1000));
  long msec_after = (time_after.tv_nsec / (1000 * 1000));
  long msec_diff = ((time_after.tv_sec - time_before.tv_sec) * 1000) +
   (msec_after - msec_before);
  double sec_diff = (double)msec_diff / 1000.;
  double mb_total = (double)len_src / (1024. * 1024.);
  warnx("elapsed time: %.1fs", sec_diff);
  warnx("avg rate: %.1fM/s", mb_total / sec_diff);
 }
 warnx("done");
 return 0;
}

variable_types: count long


repo: jgreco/vindication

decompiled_code:
void __noreturn sigchld_handler()
{
tcsetattr(master_controlling_tty, 0, &orig_settings);
exit(0);
}

canonical_decompiled_code:
void __noreturn func7()
{
tcsetattr(master_controlling_tty, 0, &orig_settings);
exit(0);
}

original_code:
void sigchld_handler(int sig_num)
{
 tcsetattr(master_controlling_tty, 0, &orig_settings);
 exit(0);
}


decompiled_code:
int sigwinch_handler()
{
ioctl(master_controlling_tty, 0x5413uLL, &term_size);
ioctl(slave_pty, 0x5414uLL, &term_size);
return kill(pid, 28);
}

canonical_decompiled_code:
int func8()
{
ioctl(master_controlling_tty, 0x5413uLL, &term_size);
ioctl(slave_pty, 0x5414uLL, &term_size);
return kill(pid, 28);
}

original_code:
void sigwinch_handler(int sig_num)
{
 ioctl(master_controlling_tty, 0x5413, &term_size);
 ioctl(slave_pty, 0x5414, &term_size);
 kill(pid, 28);
}


decompiled_code:
__int64 __fastcall get_pty(int *a1, int *a2)
{
int v2; // eax
char *v3; // rax

v2 = getpt();
*a1 = v2;
grantpt(v2);
unlockpt(*a1);
v3 = ptsname(*a1);
*a2 = open(v3, 258, 0LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(int *a1, int *a2)
{
int v2; // eax
char *v3; // rax

v2 = getpt();
*a1 = v2;
grantpt(v2);
unlockpt(*a1);
v3 = ptsname(*a1);
*a2 = open(v3, 258, 0LL);
return 0LL;
}

original_code:
int get_pty(int *master, int *slave)
{
 int num;
 char *name;
 *master = getpt();
 grantpt(*master);
 unlockpt(*master);
 num = *master;
 name = ptsname(num);
 *slave = open(name, 02 | 0400, 0);
 return 0;
}

variable_types:
master
int *

variable_types:
slave
int *

variable_types: num int

variable_types:
name
char *


decompiled_code:
__int64 pty_fork()
{
__pid_t v0; // eax
unsigned int v1; // ebx
int v3; // ebp
int v4; // [rsp+8h] [rbp-20h] BYREF
int fd[7]; // [rsp+Ch] [rbp-1Ch] BYREF

get_pty(fd, &v4);
v0 = fork();
if ( v0 < 0 )
{
fwrite("FORK FAILED\n", 1uLL, 0xCuLL, stderr);
exit(1);
}
v1 = v0;
if ( !v0 )
{
close(fd[0]);
setsid();
v3 = open("/dev/tty", 258);
ioctl(v3, 0x5422uLL, 0LL);
close(v3);
ioctl(v4, 0x540EuLL, 0LL);
ioctl(v4, 0x5414uLL, &term_size);
dup2(v4, 0);
dup2(v4, 1);
dup2(v4, 2);
}
master_pty = fd[0];
slave_pty = v4;
return v1;
}

canonical_decompiled_code:
__int64 func10()
{
__pid_t v0; // eax
unsigned int v1; // ebx
int v3; // ebp
int v4; // [rsp+8h] [rbp-20h] BYREF
int fd[7]; // [rsp+Ch] [rbp-1Ch] BYREF

func9(fd, &v4);
v0 = fork();
if ( v0 < 0 )
{
fwrite("FORK FAILED\n", 1uLL, 0xCuLL, stderr);
exit(1);
}
v1 = v0;
if ( !v0 )
{
close(fd[0]);
setsid();
v3 = open("/dev/tty", 258);
ioctl(v3, 0x5422uLL, 0LL);
close(v3);
ioctl(v4, 0x540EuLL, 0LL);
ioctl(v4, 0x5414uLL, &term_size);
dup2(v4, 0);
dup2(v4, 1);
dup2(v4, 2);
}
master_pty = fd[0];
slave_pty = v4;
return v1;
}

original_code:
int pty_fork()
{
 int n_pid;
 int master, slave;
 int fd;
 get_pty(&master, &slave);
 if((n_pid= fork()) < 0) {
  fprintf(stderr, "FORK FAILED\n");
  exit(1);
 } else if(n_pid == 0) {
  close(master);
  setsid();
  fd = open("/dev/tty", 02 | 0400);
  ioctl(fd, 0x5422, ((void *)0));
  close(fd);
  fd = ioctl(slave, 0x540E, ((void *)0));
  ioctl(slave, 0x5414, &term_size);
  dup2(slave, 0);
  dup2(slave, 1);
  dup2(slave, 2);
 } else {
 }
 master_pty = master;
 slave_pty = slave;
 return n_pid;
}

canonical_original_code:
int pty_fork()
{
 int n_pid;
 int master; int slave;
 int fd;
 get_pty(&master, &slave);
 if((n_pid= fork()) < 0) {
  fprintf(stderr, "FORK FAILED\n");
  exit(1);
 } else if(n_pid == 0) {
  close(master);
  setsid();
  fd = open("/dev/tty", 02 | 0400);
  ioctl(fd, 0x5422, ((void *)0));
  close(fd);
  fd = ioctl(slave, 0x540E, ((void *)0));
  ioctl(slave, 0x5414, &term_size);
  dup2(slave, 0);
  dup2(slave, 1);
  dup2(slave, 2);
 } else {
 }
 master_pty = master;
 slave_pty = slave;
 return n_pid;
}

variable_types: master int

variable_types: slave int

variable_types: fd int


decompiled_code:
void __fastcall input_mangle(_BYTE *a1, int a2)
{
__int64 v2; // rbx
int v3; // eax
const unsigned __int16 **v4; // rdx
char v5; // al
int v6; // eax
int v7; // er14
int v8; // ebp
int v9; // er15

mangled_len = 0;
if ( a2 == 1 )
{
v2 = 0LL;
if ( *a1 == 27 )
{
if ( !state )
{
mangled_in[0] = 27;
byte_404161 = 91;
mangled_len = 3;
byte_404162 = 68;
state = 1;
return;
}
v2 = 0LL;
if ( state == 1 )
{
state = 0;
mangled_len = 1;
mangled_in[0] = 27;
return;
}
}
}
else
{
v2 = 0LL;
if ( a2 <= 0 )
return;
}
do
{
if ( state )
{
if ( state == 1 )
{
v4 = __ctype_b_loc();
v5 = a1[v2];
if ( ((*v4)[v5] & 0x800) != 0 )
{
if ( command_count == 1 )
v6 = v5 - 48;
else
v6 = v5 + 10 * command_count - 48;
command_count = v6;
}
else
{
v7 = command_count;
v8 = 0;
if ( command_count > 0 )
{
do
{
if ( a1[v2] == 13 )
{
v9 = mangled_len;
strcpy((char *)(mangled_len + 4211040LL), K_ENTER);
mangled_len = S_ENTER + v9;
}
++v8;
}
while ( v8 != v7 );
}
command_count = 1;
}
}
}
else
{
v3 = mangled_len++;
mangled_in[v3] = a1[v2];
}
++v2;
}
while ( a2 > (int)v2 );
}

canonical_decompiled_code:
void __fastcall func11(_BYTE *a1, int a2)
{
__int64 v2; // rbx
int v3; // eax
const unsigned __int16 **v4; // rdx
char v5; // al
int v6; // eax
int v7; // er14
int v8; // ebp
int v9; // er15

mangled_len = 0;
if ( a2 == 1 )
{
v2 = 0LL;
if ( *a1 == 27 )
{
if ( !state )
{
mangled_in[0] = 27;
byte_404161 = 91;
mangled_len = 3;
byte_404162 = 68;
state = 1;
return;
}
v2 = 0LL;
if ( state == 1 )
{
state = 0;
mangled_len = 1;
mangled_in[0] = 27;
return;
}
}
}
else
{
v2 = 0LL;
if ( a2 <= 0 )
return;
}
do
{
if ( state )
{
if ( state == 1 )
{
v4 = __ctype_b_loc();
v5 = a1[v2];
if ( ((*v4)[v5] & 0x800) != 0 )
{
if ( command_count == 1 )
v6 = v5 - 48;
else
v6 = v5 + 10 * command_count - 48;
command_count = v6;
}
else
{
v7 = command_count;
v8 = 0;
if ( command_count > 0 )
{
do
{
if ( a1[v2] == 13 )
{
v9 = mangled_len;
strcpy((char *)(mangled_len + 4211040LL), K_ENTER);
mangled_len = S_ENTER + v9;
}
++v8;
}
while ( v8 != v7 );
}
command_count = 1;
}
}
}
else
{
v3 = mangled_len++;
mangled_in[v3] = a1[v2];
}
++v2;
}
while ( a2 > (int)v2 );
}

original_code:
void input_mangle(char *in, int num)
{
 int i, k;
 mangled_len = 0;
 if(num == 1 && in[0] == 27) {
  if(state == INSERT) {
   mangled_in[mangled_len++] = 27;
   mangled_in[mangled_len++] = 91;
   mangled_in[mangled_len++] = 68;
   mangled_len = 3;
   state = NORMAL;
   return;
  }
  else if(state == NORMAL) {
   state = INSERT;
   mangled_len = 1;
   mangled_in[0] = 27;
   return;
  }
 }
 for(i=0; i<num; i++) {
  if(state == INSERT) {
   mangled_in[mangled_len++] = in[i];
  }
  else if(state == NORMAL) {
   if(((*__ctype_b_loc ())[(int) ((in[i]))] & (unsigned short int) _ISdigit)) {
    if(command_count == 1)
     command_count = in[i] - '0';
    else
     command_count = command_count * 10 + in[i] - '0';
    continue;
   }
   for(k=0; k<command_count; k++) {
   switch(in[i]) {
    case 13:
     strcpy(mangled_in+mangled_len, K_ENTER);
     mangled_len += S_ENTER;
     break;
    case 'i':
     state = INSERT;
     break;
    case 'A':
     state = INSERT;
     strcpy(mangled_in+mangled_len, K_END);
     mangled_len += S_END;
     break;
    case 'h':
     strcpy(mangled_in+mangled_len, K_LEFT);
     mangled_len += S_LEFT;
     break;
    case 'j':
     strcpy(mangled_in+mangled_len, K_DOWN);
     mangled_len += S_DOWN;
     break;
    case 'k':
     strcpy(mangled_in+mangled_len, K_UP);
     mangled_len += S_UP;
     break;
    case 'a':
     state = INSERT;
    case 'l':
     strcpy(mangled_in+mangled_len, K_RIGHT);
     mangled_len += S_RIGHT;
     break;
    case 'x':
     strcpy(mangled_in+mangled_len, K_DELETE);
     mangled_len += S_DELETE;
     break;
   }
   }
   command_count = 1;
  }
 }
 return;
}

canonical_original_code:
void input_mangle(char *in, int num)
{
 int i; int k;
 mangled_len = 0;
 if(num == 1 && in[0] == 27) {
  if(state == INSERT) {
   mangled_in[mangled_len++] = 27;
   mangled_in[mangled_len++] = 91;
   mangled_in[mangled_len++] = 68;
   mangled_len = 3;
   state = NORMAL;
   return;
  }
  else if(state == NORMAL) {
   state = INSERT;
   mangled_len = 1;
   mangled_in[0] = 27;
   return;
  }
 }
 for(i=0; i<num; i++) {
  if(state == INSERT) {
   mangled_in[mangled_len++] = in[i];
  }
  else if(state == NORMAL) {
   if(((*__ctype_b_loc ())[(int) ((in[i]))] & (unsigned short) _ISdigit)) {
    if(command_count == 1)
     command_count = in[i] - '0';
    else
     command_count = command_count * 10 + in[i] - '0';
    continue;
   }
   for(k=0; k<command_count; k++) {
   switch(in[i]) {
    case 13:
     strcpy(mangled_in+mangled_len, K_ENTER);
     mangled_len += S_ENTER;
     break;
    case 'i':
     state = INSERT;
     break;
    case 'A':
     state = INSERT;
     strcpy(mangled_in+mangled_len, K_END);
     mangled_len += S_END;
     break;
    case 'h':
     strcpy(mangled_in+mangled_len, K_LEFT);
     mangled_len += S_LEFT;
     break;
    case 'j':
     strcpy(mangled_in+mangled_len, K_DOWN);
     mangled_len += S_DOWN;
     break;
    case 'k':
     strcpy(mangled_in+mangled_len, K_UP);
     mangled_len += S_UP;
     break;
    case 'a':
     state = INSERT;
    case 'l':
     strcpy(mangled_in+mangled_len, K_RIGHT);
     mangled_len += S_RIGHT;
     break;
    case 'x':
     strcpy(mangled_in+mangled_len, K_DELETE);
     mangled_len += S_DELETE;
     break;
   }
   }
   command_count = 1;
  }
 }
 return;
}

variable_types:
in
char *

variable_types: i int

variable_types: k int