decompiled_code:
__int64 refl_call(__int64 a1, ...)
{
__int64 v1; // rax
char *v2; // rsi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = refl_hash_find();
if ( !v1 )
_assert_fail("e", "lib/call.c", 0x70u, "refl_call");
v2 = *(char **)(v1 + 56);
if ( !v2 )
_assert_fail("sig", "lib/call.c", 0x73u, "refl_call");
return raw_call(v1, v2, (int *)va);
}

canonical_decompiled_code:
__int64 func1(__int64 a1, ...)
{
__int64 v1; // rax
char *v2; // rsi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = refl_hash_find();
if ( !v1 )
_assert_fail("e", "lib/call.c", 0x70u, "refl_call");
v2 = *(char **)(v1 + 56);
if ( !v2 )
_assert_fail("sig", "lib/call.c", 0x73u, "refl_call");
return func0(v1, v2, (int *)va);
}

original_code:
void refl_call(func_t func, ...)
{
    va_list args;
    __builtin_va_start(args,func);
    const struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 112, __extension__ __PRETTY_FUNCTION__));
    const char *sig = e->signature;
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 115, __extension__ __PRETTY_FUNCTION__));
    raw_call(e, sig, args);
    __builtin_va_end(args);
}

canonical_original_code:
void refl_call(unsigned long func, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,func);
    struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 112, __extension__ __PRETTY_FUNCTION__));
    char *sig = e->signature;
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 115, __extension__ __PRETTY_FUNCTION__));
    raw_call(e, sig, args);
    __builtin_va_end(args);
}

variable_types:
sig
char *

user_defined_types: struct virt_trampoline { unsigned long code[8]; unsigned long scratch_ret; unsigned long target; unsigned long prologue[2]; unsigned long trampoline[2]; }

user_defined_types: enum refl_annotation_type { REFL_ANNOTATION_FUNCSIG_PAR_TYPE=32768, REFL_ANNOTATION_FUNCSIG_RET_TYPE=expr }


repo: XanClic/shiny-c

decompiled_code:
__int64 refl_call_verify(__int64 a1, const char *a2, ...)
{
__int64 v2; // rax
__int64 v3; // rbp
char *v4; // r13
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

va_start(va, a2);
v2 = refl_hash_find();
v3 = v2;
if ( !v2 )
_assert_fail("e", "lib/call.c", 0x81u, "refl_call_verify");
v4 = *(char **)(v2 + 56);
if ( !v4 )
_assert_fail("sig", "lib/call.c", 0x84u, "refl_call_verify");
if ( strcmp(*(const char **)(v2 + 56), a2) )
{
fprintf(stderr, "Cannot dynamically call function: Expected signature (%s) differs from actual one (%s)\n", a2, v4);
_assert_fail("0", "lib/call.c", 0x88u, "refl_call_verify");
}
return raw_call(v3, v4, (int *)va);
}

canonical_decompiled_code:
__int64 func2(__int64 a1, const char *a2, ...)
{
__int64 v2; // rax
__int64 v3; // rbp
char *v4; // r13
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

va_start(va, a2);
v2 = refl_hash_find();
v3 = v2;
if ( !v2 )
_assert_fail("e", "lib/call.c", 0x81u, "refl_call_verify");
v4 = *(char **)(v2 + 56);
if ( !v4 )
_assert_fail("sig", "lib/call.c", 0x84u, "refl_call_verify");
if ( strcmp(*(const char **)(v2 + 56), a2) )
{
fprintf(stderr, "Cannot dynamically call function: Expected signature (%s) differs from actual one (%s)\n", a2, v4);
_assert_fail("0", "lib/call.c", 0x88u, "refl_call_verify");
}
return func0(v3, v4, (int *)va);
}

original_code:
void refl_call_verify(func_t func, const char *expsig, ...)
{
    va_list args;
    __builtin_va_start(args,expsig);
    const struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 129, __extension__ __PRETTY_FUNCTION__));
    const char *sig = e->signature;
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 132, __extension__ __PRETTY_FUNCTION__));
    if (strcmp(sig, expsig))
    {
        fprintf(stderr, "Cannot dynamically call function: Expected signature (%s) differs from actual one (%s)\n", expsig, sig);
        ((0) ? (void) (0) : __assert_fail ("0", "lib/call.c", 136, __extension__ __PRETTY_FUNCTION__));
    }
    raw_call(e, sig, args);
    __builtin_va_end(args);
}

canonical_original_code:
void refl_call_verify(unsigned long func, char *expsig, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,expsig);
    struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 129, __extension__ __PRETTY_FUNCTION__));
    char *sig = e->signature;
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 132, __extension__ __PRETTY_FUNCTION__));
    if (strcmp(sig, expsig))
    {
        fprintf(stderr, "Cannot dynamically call function: Expected signature (%s) differs from actual one (%s)\n", expsig, sig);
        ((0) ? (void) (0) : __assert_fail ("0", "lib/call.c", 136, __extension__ __PRETTY_FUNCTION__));
    }
    raw_call(e, sig, args);
    __builtin_va_end(args);
}

variable_types:
func
unsigned long

variable_types:
expsig
char *

variable_types: struct vf_ht_entry { struct vf_ht_entry *next; unsigned long vf; int symtab_idx; unsigned long prologue[2]; struct virt_trampoline { unsigned long code[8]; unsigned long scratch_ret; unsigned long target; unsigned long prologue[2]; unsigned long trampoline[2]; } *trampoline; struct annotation_entry { struct refl_annotation { unsigned long func; enum refl_annotation_type { REFL_ANNOTATION_FUNCSIG_PAR_TYPE=32768, REFL_ANNOTATION_FUNCSIG_RET_TYPE=expr } type; char *data; } annotation; struct annotation_entry *next; } *annotation_list; char *signature; } *

return_type: void

user_defined_types: struct vf_ht_entry { struct vf_ht_entry *next; unsigned long vf; int symtab_idx; unsigned long prologue[2]; struct virt_trampoline *trampoline; struct annotation_entry *annotation_list; char *signature; }

user_defined_types: struct annotation_entry { struct refl_annotation annotation; struct annotation_entry *next; }

user_defined_types: struct refl_annotation { unsigned long func; enum refl_annotation_type type; char *data; }


decompiled_code:
__int64 refl_call_sig(__int64 a1, char *a2, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
v2 = refl_hash_find();
if ( !v2 )
_assert_fail("e", "lib/call.c", 0x97u, "refl_call_sig");
if ( !a2 )
_assert_fail("sig", "lib/call.c", 0x98u, "refl_call_sig");
return raw_call(v2, a2, (int *)va);
}

canonical_decompiled_code:
__int64 func3(__int64 a1, char *a2, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
v2 = refl_hash_find();
if ( !v2 )
_assert_fail("e", "lib/call.c", 0x97u, "refl_call_sig");
if ( !a2 )
_assert_fail("sig", "lib/call.c", 0x98u, "refl_call_sig");
return func0(v2, a2, (int *)va);
}

original_code:
void refl_call_sig(func_t func, const char *sig, ...)
{
    va_list args;
    __builtin_va_start(args,sig);
    const struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 151, __extension__ __PRETTY_FUNCTION__));
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 152, __extension__ __PRETTY_FUNCTION__));
    raw_call(e, sig, args);
    __builtin_va_end(args);
}

canonical_original_code:
void refl_call_sig(unsigned long func, char *sig, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,sig);
    struct vf_ht_entry *e = refl_hash_find(func);
    ((e) ? (void) (0) : __assert_fail ("e", "lib/call.c", 151, __extension__ __PRETTY_FUNCTION__));
    ((sig) ? (void) (0) : __assert_fail ("sig", "lib/call.c", 152, __extension__ __PRETTY_FUNCTION__));
    raw_call(e, sig, args);
    __builtin_va_end(args);
}


decompiled_code:
__int64 __fastcall refl_get_annotation(__int64 a1, int a2)
{
__int64 result; // rax

result = refl_hash_find();
if ( result )
{
result = *(_QWORD *)(result + 48);
if ( result )
{
while ( *(_DWORD *)(result + 8) != a2 )
{
result = *(_QWORD *)(result + 24);
if ( !result )
return result;
}
result = *(_QWORD *)(result + 16);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
__int64 result; // rax

result = refl_hash_find();
if ( result )
{
result = *(_QWORD *)(result + 48);
if ( result )
{
while ( *(_DWORD *)(result + 8) != a2 )
{
result = *(_QWORD *)(result + 24);
if ( !result )
return result;
}
result = *(_QWORD *)(result + 16);
}
}
return result;
}

original_code:
const char *refl_get_annotation(func_t func, enum refl_annotation_type type)
{
    const struct vf_ht_entry *e = refl_hash_find(func);
    if (!e)
        return ((void *)0);
    for (struct annotation_entry *ae = e->annotation_list; ae != ((void *)0); ae = ae->next)
        if (ae->annotation.type == type)
            return ae->annotation.data;
    return ((void *)0);
}

canonical_original_code:
char *refl_get_annotation(unsigned long func, enum refl_annotation_type type)
{
    struct vf_ht_entry *e = refl_hash_find(func);
    if (!e)
        return ((void *)0);
    for (struct annotation_entry *ae = e->annotation_list; ae != ((void *)0); ae = ae->next)
        if (ae->annotation.type == type)
            return ae->annotation.data;
    return ((void *)0);
}

variable_types: struct annotation_entry { struct refl_annotation { unsigned long func; enum refl_annotation_type { REFL_ANNOTATION_FUNCSIG_PAR_TYPE=32768, REFL_ANNOTATION_FUNCSIG_RET_TYPE=expr } type; char *data; } annotation; struct annotation_entry *next; } *

return_type: char *


decompiled_code:
bool __fastcall refl_each_annotation_next(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rax

v2 = refl_hash_find();
if ( !v2 )
return 0;
if ( *(_QWORD *)a2 )
v3 = *(_QWORD *)(*(_QWORD *)a2 + 24LL);
else
v3 = *(_QWORD *)(v2 + 48);
*(_QWORD *)a2 = v3;
return v3 != 0;
}

canonical_decompiled_code:
bool __fastcall func1(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rax

v2 = refl_hash_find();
if ( !v2 )
return 0;
if ( *(_QWORD *)a2 )
v3 = *(_QWORD *)(*(_QWORD *)a2 + 24LL);
else
v3 = *(_QWORD *)(v2 + 48);
*(_QWORD *)a2 = v3;
return v3 != 0;
}

original_code:
_Bool refl_each_annotation_next(func_t func, const struct refl_annotation **anno)
{
    const struct vf_ht_entry *e = refl_hash_find(func);
    if (!e)
        return 0;
    if (!*anno)
        *anno = &e->annotation_list->annotation;
    else
        *anno = &((const struct annotation_entry *)*anno)->next->annotation;
    return *anno != ((void *)0);
}

canonical_original_code:
_Bool refl_each_annotation_next(unsigned long func, struct refl_annotation **anno)
{
    struct vf_ht_entry *e = refl_hash_find(func);
    if (!e)
        return 0;
    if (!*anno)
        *anno = &e->annotation_list->annotation;
    else
        *anno = &((struct annotation_entry *)*anno)->next->annotation;
    return *anno != ((void *)0);
}

variable_types: struct refl_annotation { unsigned long func; enum refl_annotation_type { REFL_ANNOTATION_FUNCSIG_PAR_TYPE=32768, REFL_ANNOTATION_FUNCSIG_RET_TYPE=expr } type; char *data; } **


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

result = refl_hash_find();
if ( result )
result = _strtab + *(unsigned int *)(_symtab + 24LL * *(int *)(result + 16));
return result;
}

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

result = refl_hash_find();
if ( result )
result = _strtab + *(unsigned int *)(_symtab + 24LL * *(int *)(result + 16));
return result;
}

original_code:
const char *refl_get_name(func_t func)
{
    const struct vf_ht_entry *e = refl_hash_find(func);
    return e ? __strtab + __symtab[e->symtab_idx].st_name : ((void *)0);
}

canonical_original_code:
char *refl_get_name(unsigned long func)
{
    struct vf_ht_entry *e = refl_hash_find(func);
    return e ? __strtab + __symtab[e->symtab_idx].st_name : ((void *)0);
}


decompiled_code:
__int64 refl_get_target()
{
__int64 v0; // rax
__int64 v1; // r8

v0 = refl_hash_find();
v1 = 0LL;
if ( v0 )
v1 = *(_QWORD *)(*(_QWORD *)(v0 + 40) + 72LL);
return v1;
}

original_code:
func_t refl_get_target(func_t func)
{
    const struct vf_ht_entry *e = refl_hash_find(func);
    return e ? e->trampoline->target : 0;
}

canonical_original_code:
unsigned long refl_get_target(unsigned long func)
{
    struct vf_ht_entry *e = refl_hash_find(func);
    return e ? e->trampoline->target : 0;
}

return_type: unsigned long


main

canonical_name: main

repo: XanClic/standard

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int result; // eax
const char **v5; // rbx
__int64 v6; // rax
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

longind[0] = 0;
v3 = getopt_long(argc, (char *const *)argv, "hv", &long_options, longind);
switch ( v3 )
{
case -1:
term_init();
load_config();
if ( argc <= 1 )
{
new_buffer();
LABEL_11:
editor();
return 0;
}
v5 = argv + 1;
while ( 1 )
{
v6 = new_buffer();
if ( !(unsigned __int8)buffer_load(v6, *v5) )
break;
if ( ++v5 == &argv[(unsigned int)(argc - 2) + 2] )
goto LABEL_11;
}
term_release();
fprintf(stderr, format, *v5);
result = 1;
break;
case 104:
puts("Usage: std FILE\n--help, -h Show this help\n--version, -v Show current version");
result = 0;
break;
case 118:
puts(s);
result = 0;
break;
default:
return 0;
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
    int c;
    int option_index = 0;
    while ((c = getopt_long(argc, argv, "hv", long_options, &option_index)) != -1)
    {
        switch (c)
        {
            case 'h':
                printf("Usage: std FILE\n"
                        "--help, -h     Show this help\n"
                        "--version, -v  Show current version\n");
                return 0;
            case 'v':
                printf("standard " "0.0.元気" ": The standard text editor.\n"
                       "No need for ed anymore.\n");
                return 0;
            default:
                return 0;
        }
    }
    term_init();
    load_config();
    if (argc < 2)
        new_buffer();
    else
    {
        for (int i = 1; i < argc; i++)
        {
            buffer_t *buf = new_buffer();
            if (!buffer_load(buf, argv[i]))
            {
                term_release();
                fprintf(stderr, "Could not load “%s”.\n", argv[i]);
                return 1;
            }
        }
    }
    editor();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int c;
    int option_index = 0;
    while ((c = getopt_long(argc, argv, "hv", long_options, &option_index)) != -1)
    {
        switch (c)
        {
            case 'h':
                printf("Usage: std FILE\n"
                        "--help, -h     Show this help\n"
                        "--version, -v  Show current version\n");
                return 0;
            case 'v':
                printf("standard " "0.0.元気" ": The standard text editor.\n"
                       "No need for ed anymore.\n");
                return 0;
            default:
                return 0;
        }
    }
    term_init();
    load_config();
    if (argc < 2)
        new_buffer();
    else
    {
        for (int i = 1; i < argc; i++)
        {
            struct buffer *buf = new_buffer();
            if (!buffer_load(buf, argv[i]))
            {
                term_release();
                fprintf(stderr, "Could not load “%s”.\n", argv[i]);
                return 1;
            }
        }
    }
    editor();
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: c int

variable_types: i int

variable_types: struct buffer { char *name; char *location; int line_count; char **lines; int *line_screen_pos; int x; int ys; int linenr_width; _Bool modified; int oll_unused_lines; } *

return_type: int


print

decompiled_code:
size_t __fastcall print(void *ptr)
{
size_t v1; // rax

v1 = strlen((const char *)ptr);
return fwrite(ptr, 1uLL, v1, stdout);
}

canonical_decompiled_code:
size_t __fastcall func0(void *ptr)
{
size_t v1; // rax

v1 = strlen((const char *)ptr);
return fwrite(ptr, 1uLL, v1, stdout);
}

original_code:
void print(const char *s)
{
    fwrite(s, 1, strlen(s), stdout);
}

canonical_original_code:
void print(char *s)
{
    fwrite(s, 1, strlen(s), stdout);
}

variable_types:
s
char *


decompiled_code:
int __fastcall print_flushed(void *a1)
{
print(a1);
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func1(void *a1)
{
func0(a1);
return fflush(stdout);
}

original_code:
void print_flushed(const char *s)
{
    print(s);
    fflush(stdout);
}

canonical_original_code:
void print_flushed(char *s)
{
    print(s);
    fflush(stdout);
}


decompiled_code:
size_t term_clear()
{
return print("\x1B[2J\x1B[H");
}

canonical_decompiled_code:
size_t func2()
{
return func0("\x1B[2J\x1B[H");
}

original_code:
void term_clear(void)
{
    print("\033[2J\033[H");
}


decompiled_code:
size_t __fastcall term_underline(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[4m";
if ( v1 )
v2 = "\x1B[24m";
return print(v2);
}

canonical_decompiled_code:
size_t __fastcall func3(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[4m";
if ( v1 )
v2 = "\x1B[24m";
return func0(v2);
}

original_code:
void term_underline(_Bool ul)
{
    print(ul ? "\033[4m" : "\033[24m");
}


decompiled_code:
size_t __fastcall term_bold(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[1m";
if ( v1 )
v2 = "\x1B[22m";
return print(v2);
}

canonical_decompiled_code:
size_t __fastcall func4(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[1m";
if ( v1 )
v2 = "\x1B[22m";
return func0(v2);
}

original_code:
void term_bold(_Bool bold)
{
    print(bold ? "\033[1m" : "\033[22m");
}


decompiled_code:
int __fastcall term_set_color(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rbx
int result; // eax
bool v4; // cc
__int64 v5; // rsi

v2 = HIDWORD(a2);
switch ( (int)a1 )
{
case 0:
print("\x1B[22;39m");
goto LABEL_3;
case 1:
printf("\x1B[3%im", BYTE4(a1) & 7);
goto LABEL_3;
case 2:
v5 = BYTE4(a1) & 7;
if ( (a1 & 0x800000000LL) != 0 )
printf("\x1B[1;3%im", v5);
else
printf("\x1B[22;3%im", v5);
goto LABEL_3;
case 3:
printf("\x1B[38;5;%im", BYTE4(a1));
result = a2;
v4 = (unsigned int)a2 <= 3;
if ( (_DWORD)a2 == 3 )
return printf("\x1B[48;5;%im", (unsigned __int8)v2);
goto LABEL_4;
case 4:
printf("\x1B[38;2;%i;%i;%im", BYTE4(a1), BYTE5(a1), BYTE6(a1));
goto LABEL_3;
default:
LABEL_3:
result = a2;
v4 = (unsigned int)a2 <= 3;
if ( (_DWORD)a2 == 3 )
return printf("\x1B[48;5;%im", (unsigned __int8)v2);
LABEL_4:
if ( v4 )
{
if ( result )
result = printf("\x1B[4%im", v2 & 7);
else
result = print("\x1B[49m");
}
else if ( result == 4 )
{
result = printf("\x1B[48;2;%i;%i;%im", (unsigned __int8)v2, BYTE5(a2), BYTE6(a2));
}
return result;
}
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rbx
int result; // eax
bool v4; // cc
__int64 v5; // rsi

v2 = HIDWORD(a2);
switch ( (int)a1 )
{
case 0:
func0("\x1B[22;39m");
goto LABEL_3;
case 1:
printf("\x1B[3%im", BYTE4(a1) & 7);
goto LABEL_3;
case 2:
v5 = BYTE4(a1) & 7;
if ( (a1 & 0x800000000LL) != 0 )
printf("\x1B[1;3%im", v5);
else
printf("\x1B[22;3%im", v5);
goto LABEL_3;
case 3:
printf("\x1B[38;5;%im", BYTE4(a1));
result = a2;
v4 = (unsigned int)a2 <= 3;
if ( (_DWORD)a2 == 3 )
return printf("\x1B[48;5;%im", (unsigned __int8)v2);
goto LABEL_4;
case 4:
printf("\x1B[38;2;%i;%i;%im", BYTE4(a1), BYTE5(a1), BYTE6(a1));
goto LABEL_3;
default:
LABEL_3:
result = a2;
v4 = (unsigned int)a2 <= 3;
if ( (_DWORD)a2 == 3 )
return printf("\x1B[48;5;%im", (unsigned __int8)v2);
LABEL_4:
if ( v4 )
{
if ( result )
result = printf("\x1B[4%im", v2 & 7);
else
result = func0("\x1B[49m");
}
else if ( result == 4 )
{
result = printf("\x1B[48;2;%i;%i;%im", (unsigned __int8)v2, BYTE5(a2), BYTE6(a2));
}
return result;
}
}

original_code:
void term_set_color(color_t fg, color_t bg)
{
    switch (fg.type)
    {
        case COL_DEFAULT:
            print("\033[22;39m");
            break;
        case COL_8:
            printf("\033[3%im", fg.color & 7);
            break;
        case COL_16:
            if (fg.color & 8)
                printf("\033[1;3%im", fg.color & 7);
            else
                printf("\033[22;3%im", fg.color & 7);
            break;
        case COL_256:
            printf("\033[38;5;%im", fg.color);
            break;
        case COL_RGB:
            printf("\033[38;2;%i;%i;%im", fg.r, fg.g, fg.b);
    }
    switch (bg.type)
    {
        case COL_DEFAULT:
            print("\033[49m");
            break;
        case COL_8:
        case COL_16:
            printf("\033[4%im", bg.color & 7);
            break;
        case COL_256:
            printf("\033[48;5;%im", bg.color);
            break;
        case COL_RGB:
            printf("\033[48;2;%i;%i;%im", bg.r, bg.g, bg.b);
    }
}

canonical_original_code:
void term_set_color(struct color fg, struct color bg)
{
    switch (fg.type)
    {
        case COL_DEFAULT:
            print("\033[22;39m");
            break;
        case COL_8:
            printf("\033[3%im", fg.color & 7);
            break;
        case COL_16:
            if (fg.color & 8)
                printf("\033[1;3%im", fg.color & 7);
            else
                printf("\033[22;3%im", fg.color & 7);
            break;
        case COL_256:
            printf("\033[38;5;%im", fg.color);
            break;
        case COL_RGB:
            printf("\033[38;2;%i;%i;%im", fg.r, fg.g, fg.b);
    }
    switch (bg.type)
    {
        case COL_DEFAULT:
            print("\033[49m");
            break;
        case COL_8:
        case COL_16:
            printf("\033[4%im", bg.color & 7);
            break;
        case COL_256:
            printf("\033[48;5;%im", bg.color);
            break;
        case COL_RGB:
            printf("\033[48;2;%i;%i;%im", bg.r, bg.g, bg.b);
    }
}

variable_types: struct color { enum color_type { COL_DEFAULT=0, COL_8=1, COL_16=2, COL_256=3, COL_RGB=4 } type; union { unsigned char color; struct { unsigned char r; }; }; }

variable_types:
bg
struct color

user_defined_types: struct color { enum color_type type; union { unsigned char color; struct { unsigned char r; }; }; }


decompiled_code:
size_t __fastcall term_invert(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[7m";
if ( v1 )
v2 = "\x1B[27m";
return print(v2);
}

canonical_decompiled_code:
size_t __fastcall func6(char a1)
{
bool v1; // zf
char *v2; // rdi

v1 = a1 == 0;
v2 = "\x1B[7m";
if ( v1 )
v2 = "\x1B[27m";
return func0(v2);
}

original_code:
void term_invert(_Bool invert)
{
    print(invert ? "\033[7m" : "\033[27m");
}


decompiled_code:
int __fastcall term_cursor_pos(int a1, int a2)
{
printf("\x1B[%i;%iH", (unsigned int)(a1 / term_width + a2 + 1), (unsigned int)(a1 % term_width + 1));
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func7(int a1, int a2)
{
printf("\x1B[%i;%iH", (unsigned int)(a1 / term_width + a2 + 1), (unsigned int)(a1 % term_width + 1));
return fflush(stdout);
}

original_code:
void term_cursor_pos(int x, int y)
{
    y += x / term_width;
    x %= term_width;
    printf("\033[%i;%iH", y + 1, x + 1);
    fflush(stdout);
}

variable_types: x int

variable_types: y int


decompiled_code:
int __fastcall term_show_cursor(char a1)
{
return printf("\x1B[?25%c", a1 == 0 ? 108 : 104);
}

canonical_decompiled_code:
int __fastcall func8(char a1)
{
return printf("\x1B[?25%c", a1 == 0 ? 108 : 104);
}

original_code:
void term_show_cursor(_Bool show)
{
    printf("\033[?25%c", show ? 'h' : 'l');
}


decompiled_code:
void term_release(void)
{
if ( claimed )
{
if ( !cleared_0 )
{
term_clear();
fflush(stdout);
cleared_0 = 1;
}
term_show_cursor(1);
tcsetattr(0, 0, &initial_tios);
print_flushed("\x1B[?1000;1002;1006;1015l");
}
}

canonical_decompiled_code:
void func9(void)
{
if ( claimed )
{
if ( !cleared_0 )
{
func2();
fflush(stdout);
cleared_0 = 1;
}
func8(1);
tcsetattr(0, 0, &initial_tios);
func1("\x1B[?1000;1002;1006;1015l");
}
}

original_code:
void term_release(void)
{
    static _Bool cleared = 0;
    if (!claimed)
        return;
    if (!cleared)
    {
        term_clear();
        fflush(stdout);
        cleared = 1;
    }
    term_show_cursor(1);
    tcsetattr(0, 0, &initial_tios);
    print_flushed("\033[?1000;1002;1006;1015l");
}

canonical_original_code:
void term_release()
{
    static _Bool cleared = 0;
    if (!claimed)
        return;
    if (!cleared)
    {
        term_clear();
        fflush(stdout);
        cleared = 1;
    }
    term_show_cursor(1);
    tcsetattr(0, 0, &initial_tios);
    print_flushed("\033[?1000;1002;1006;1015l");
}


decompiled_code:
int term_init()
{
__int16 v1[4]; // [rsp+8h] [rbp-50h] BYREF
struct termios termios_p; // [rsp+10h] [rbp-48h] BYREF

terminfo_load();
ioctl(1, 0x5413uLL, v1);
term_width = (unsigned __int16)v1[1];
term_height = (unsigned __int16)v1[0];
tcgetattr(0, &initial_tios);
claimed = 1;
atexit(term_release);
*(_QWORD *)&termios_p.c_cc[31] = *(_QWORD *)&initial_tios.c_cc[31];
*(__m128i *)&termios_p.c_iflag = _mm_load_si128((const __m128i *)&initial_tios);
termios_p.c_ospeed = initial_tios.c_ospeed;
*(__m128i *)&termios_p.c_line = _mm_load_si128((const __m128i *)&initial_tios.c_line);
*(__m128i *)&termios_p.c_cc[15] = _mm_load_si128((const __m128i *)&initial_tios.c_cc[15]);
termios_p.c_lflag &= 0xFFFFFF85;
*(_WORD *)&termios_p.c_cc[5] = 257;
tcsetattr(0, 0, &termios_p);
setbuf(stdin, 0LL);
setvbuf(stdout, 0LL, 0, 0LL);
print_flushed("\x1B[?1000;1002;1006;1015h");
init_mouse_input_regex();
term_show_cursor(0);
return fflush(stdout);
}

canonical_decompiled_code:
int func10()
{
__int16 v1[4]; // [rsp+8h] [rbp-50h] BYREF
struct termios termios_p; // [rsp+10h] [rbp-48h] BYREF

terminfo_load();
ioctl(1, 0x5413uLL, v1);
term_width = (unsigned __int16)v1[1];
term_height = (unsigned __int16)v1[0];
tcgetattr(0, &initial_tios);
claimed = 1;
atexit(term_release);
*(_QWORD *)&termios_p.c_cc[31] = *(_QWORD *)&initial_tios.c_cc[31];
*(__m128i *)&termios_p.c_iflag = _mm_load_si128((const __m128i *)&initial_tios);
termios_p.c_ospeed = initial_tios.c_ospeed;
*(__m128i *)&termios_p.c_line = _mm_load_si128((const __m128i *)&initial_tios.c_line);
*(__m128i *)&termios_p.c_cc[15] = _mm_load_si128((const __m128i *)&initial_tios.c_cc[15]);
termios_p.c_lflag &= 0xFFFFFF85;
*(_WORD *)&termios_p.c_cc[5] = 257;
tcsetattr(0, 0, &termios_p);
setbuf(stdin, 0LL);
setvbuf(stdout, 0LL, 0, 0LL);
func1("\x1B[?1000;1002;1006;1015h");
init_mouse_input_regex();
func8(0);
return fflush(stdout);
}

original_code:
void term_init(void)
{
    terminfo_load();
    struct winsize ws;
    ioctl(1, 0x5413, &ws);
    term_width = ws.ws_col;
    term_height = ws.ws_row;
    tcgetattr(0, &initial_tios);
    claimed = 1;
    atexit(term_release);
    struct termios tios;
    memcpy(&tios, &initial_tios, sizeof(tios));
    tios.c_lflag &= ~(0000010 | 0000020 | 0000040 | 0000100 | 0000002);
    tios.c_cc[5] = 1;
    tios.c_cc[6] = 1;
    tcsetattr(0, 0, &tios);
    setbuf(stdin, ((void *)0));
    setvbuf(stdout, ((void *)0), 0, 0);
    print_flushed("\033[?1000;1002;1006;1015h");
    init_mouse_input_regex();
    term_show_cursor(0);
    fflush(stdout);
}

canonical_original_code:
void term_init()
{
    terminfo_load();
    struct winsize ws;
    ioctl(1, 0x5413, &ws);
    term_width = ws.ws_col;
    term_height = ws.ws_row;
    tcgetattr(0, &initial_tios);
    claimed = 1;
    atexit(term_release);
    struct termios tios;
    memcpy(&tios, &initial_tios, sizeof(tios));
    tios.c_lflag &= ~(0000010 | 0000020 | 0000040 | 0000100 | 0000002);
    tios.c_cc[5] = 1;
    tios.c_cc[6] = 1;
    tcsetattr(0, 0, &tios);
    setbuf(stdin, ((void *)0));
    setvbuf(stdout, ((void *)0), 0, 0);
    print_flushed("\033[?1000;1002;1006;1015h");
    init_mouse_input_regex();
    term_show_cursor(0);
    fflush(stdout);
}

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
void __fastcall buf_edit(__int64 a1, __int64 a2, const char *a3)
{
__int64 v3; // rsi
const char *v4; // rdx

v3 = *(_QWORD *)(a1 + 8);
if ( v3 )
{
if ( *(_QWORD *)(a1 + 16) )
{
error((int)"Only one file path allowed.", v3, a3);
}
else if ( *(_BYTE *)(active_buffer + 60LL) )
{
error((int)"%s has been modified.", *active_buffer, a3);
}
else if ( (unsigned __int8)buffer_load() )
{
update_active_buffer();
}
else
{
error((int)&unk_519, *(_QWORD *)(a1 + 8), v4);
}
}
else
{
error((int)"Expected a file path.", 0, a3);
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, __int64 a2, const char *a3)
{
__int64 v3; // rsi
const char *v4; // rdx

v3 = *(_QWORD *)(a1 + 8);
if ( v3 )
{
if ( *(_QWORD *)(a1 + 16) )
{
error((int)"Only one file path allowed.", v3, a3);
}
else if ( *(_BYTE *)(active_buffer + 60LL) )
{
error((int)"%s has been modified.", *active_buffer, a3);
}
else if ( (unsigned __int8)buffer_load() )
{
update_active_buffer();
}
else
{
error((int)&unk_519, *(_QWORD *)(a1 + 8), v4);
}
}
else
{
error((int)"Expected a file path.", 0, a3);
}
}

original_code:
static void buf_edit(char **cmd_line)
{
    if (!(cmd_line[1])) { error("Expected a file path."); return; };
    if (!(!cmd_line[2])) { error("Only one file path allowed."); return; };
    if (!(!active_buffer->modified)) { error("%s has been modified.", active_buffer->name); return; };
    if (!(buffer_load(active_buffer, cmd_line[1]))) { error("Could not load “%s”.", cmd_line[1]); return; };
    update_active_buffer();
}


decompiled_code:
__int64 __fastcall raw_write(_QWORD *a1, __int64 a2, const char *a3)
{
_QWORD *v3; // rax
unsigned int v4; // ebp
_QWORD *v5; // rbx
__int64 v6; // r13
unsigned int v7; // er12
const char *v9; // rdx

if ( !*a1 )
goto LABEL_18;
v3 = a1 + 1;
v4 = 0;
do
{
++v3;
a3 = (const char *)v4++;
}
while ( *(v3 - 1) );
if ( (_DWORD)a3 )
{
v5 = &a1[(int)a3];
v6 = (__int64)&v5[-((_DWORD)a3 - 1) - 1];
while ( 1 )
{
v7 = buffer_write(active_buffer, *v5);
if ( !(_BYTE)v7 )
break;
if ( --v5 == (_QWORD *)v6 )
goto LABEL_10;
}
error((int)&unk_532, *v5, a3);
return v7;
}
LABEL_10:
if ( v4 == 1 )
{
LABEL_18:
if ( *(_QWORD *)(active_buffer + 8LL) )
{
v7 = buffer_write(active_buffer, 0LL);
if ( (_BYTE)v7 )
return 1LL;
error((int)&unk_532, *(_QWORD *)(active_buffer + 8LL), v9);
}
else
{
v7 = 0;
error((int)"No file associated with %s.", *active_buffer, a3);
}
return v7;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 a2, const char *a3)
{
_QWORD *v3; // rax
unsigned int v4; // ebp
_QWORD *v5; // rbx
__int64 v6; // r13
unsigned int v7; // er12
const char *v9; // rdx

if ( !*a1 )
goto LABEL_18;
v3 = a1 + 1;
v4 = 0;
do
{
++v3;
a3 = (const char *)v4++;
}
while ( *(v3 - 1) );
if ( (_DWORD)a3 )
{
v5 = &a1[(int)a3];
v6 = (__int64)&v5[-((_DWORD)a3 - 1) - 1];
while ( 1 )
{
v7 = buffer_write(active_buffer, *v5);
if ( !(_BYTE)v7 )
break;
if ( --v5 == (_QWORD *)v6 )
goto LABEL_10;
}
error((int)&unk_532, *v5, a3);
return v7;
}
LABEL_10:
if ( v4 == 1 )
{
LABEL_18:
if ( *(_QWORD *)(active_buffer + 8LL) )
{
v7 = buffer_write(active_buffer, 0LL);
if ( (_BYTE)v7 )
return 1LL;
error((int)&unk_532, *(_QWORD *)(active_buffer + 8LL), v9);
}
else
{
v7 = 0;
error((int)"No file associated with %s.", *active_buffer, a3);
}
return v7;
}
return 1LL;
}

original_code:
static _Bool raw_write(char **cmd_line)
{
    int parcount;
    for (parcount = 0; cmd_line[parcount]; parcount++);
    for (int i = parcount - 1; i > 0; i--)
    {
        if (!buffer_write(active_buffer, cmd_line[i]))
        {
            error("Could not write to “%s”.", cmd_line[i]);
            return 0;
        }
    }
    if (parcount <= 1)
    {
        if (!active_buffer->location)
        {
            error("No file associated with %s.", active_buffer->name);
            return 0;
        }
        else if (!buffer_write(active_buffer, ((void *)0)))
        {
            error("Could not write to “%s”.", active_buffer->location);
            return 0;
        }
    }
    return 1;
}

variable_types: parcount int


decompiled_code:
// attributes: thunk
__int64 __fastcall buf_write(_QWORD *a1, __int64 a2, const char *a3)
{
return raw_write(a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, __int64 a2, const char *a3)
{
return func1(a1, a2, a3);
}

original_code:
static void buf_write(char **cmd_line)
{
    raw_write(cmd_line);
}


decompiled_code:
void __fastcall write_and_quit_all(__int64 a1, int a2, const char *a3)
{
_QWORD *v3; // rbx
__int64 v4; // rdi

if ( *(_QWORD *)(a1 + 8) )
{
error((int)"Unexpected parameter.", a2, a3);
}
else
{
v3 = (_QWORD *)buffer_list;
if ( !buffer_list )
LABEL_7:
exit(0);
while ( 1 )
{
v4 = v3[1];
if ( *(_BYTE *)(v4 + 60) )
{
if ( !*(_QWORD *)(v4 + 8) )
{
error((int)"No file associated with %s.", *(_QWORD *)v4, a3);
return;
}
if ( !(unsigned __int8)buffer_write(v4, 0LL) )
break;
}
v3 = (_QWORD *)*v3;
if ( !v3 )
goto LABEL_7;
}
error((int)&unk_532, *(_QWORD *)(v3[1] + 8LL), a3);
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, int a2, const char *a3)
{
_QWORD *v3; // rbx
__int64 v4; // rdi

if ( *(_QWORD *)(a1 + 8) )
{
error((int)"Unexpected parameter.", a2, a3);
}
else
{
v3 = (_QWORD *)buffer_list;
if ( !buffer_list )
LABEL_7:
exit(0);
while ( 1 )
{
v4 = v3[1];
if ( *(_BYTE *)(v4 + 60) )
{
if ( !*(_QWORD *)(v4 + 8) )
{
error((int)"No file associated with %s.", *(_QWORD *)v4, a3);
return;
}
if ( !(unsigned __int8)buffer_write(v4, 0LL) )
break;
}
v3 = (_QWORD *)*v3;
if ( !v3 )
goto LABEL_7;
}
error((int)&unk_532, *(_QWORD *)(v3[1] + 8LL), a3);
}
}

original_code:
static void write_and_quit_all(char **cmd_line)
{
    if (cmd_line[1])
    {
        error("Unexpected parameter.");
        return;
    }
    for (buffer_list_t *bl = buffer_list; bl != ((void *)0); bl = bl->next)
    {
        if (bl->buffer->modified)
        {
            if (!bl->buffer->location)
            {
                error("No file associated with %s.", bl->buffer->name);
                return;
            }
            else if (!buffer_write(bl->buffer, ((void *)0)))
            {
                error("Could not write to “%s”.", bl->buffer->location);
                return;
            }
        }
    }
    exit(0);
}

canonical_original_code:
static void write_and_quit_all(char **cmd_line)
{
    if (cmd_line[1])
    {
        error("Unexpected parameter.");
        return;
    }
    for (struct buffer_list *bl = buffer_list; bl != ((void *)0); bl = bl->next)
    {
        if (bl->buffer->modified)
        {
            if (!bl->buffer->location)
            {
                error("No file associated with %s.", bl->buffer->name);
                return;
            }
            else if (!buffer_write(bl->buffer, ((void *)0)))
            {
                error("Could not write to “%s”.", bl->buffer->location);
                return;
            }
        }
    }
    exit(0);
}

variable_types: struct buffer_list { struct buffer_list *next; struct buffer { char *name; char *location; int line_count; char **lines; int *line_screen_pos; int x; int ys; int linenr_width; _Bool modified; int oll_unused_lines; } *buffer; } *

user_defined_types: struct buffer_list { struct buffer_list *next; struct buffer *buffer; }


decompiled_code:
void __fastcall force_quit_all(__int64 a1, int a2, const char *a3)
{
if ( !*(_QWORD *)(a1 + 8) )
exit(0);
error((int)"Unexpected parameter.", a2, a3);
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, int a2, const char *a3)
{
if ( !*(_QWORD *)(a1 + 8) )
exit(0);
error((int)"Unexpected parameter.", a2, a3);
}

original_code:
static void force_quit_all(char **cmd_line)
{
    if (!(!cmd_line[1])) { error("Unexpected parameter."); return; };
    exit(0);
}


decompiled_code:
void __fastcall quit_all(__int64 a1, int a2, const char *a3)
{
_QWORD *v3; // rax
__int64 v4; // rdx

if ( *(_QWORD *)(a1 + 8) )
{
error((int)"Unexpected parameter.", a2, a3);
}
else
{
v3 = (_QWORD *)buffer_list;
if ( !buffer_list )
LABEL_8:
exit(0);
while ( 1 )
{
v4 = v3[1];
if ( *(_BYTE *)(v4 + 60) )
break;
v3 = (_QWORD *)*v3;
if ( !v3 )
goto LABEL_8;
}
error((int)"%s has been modified.", *(_QWORD *)v4, (const char *)v4);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, int a2, const char *a3)
{
_QWORD *v3; // rax
__int64 v4; // rdx

if ( *(_QWORD *)(a1 + 8) )
{
error((int)"Unexpected parameter.", a2, a3);
}
else
{
v3 = (_QWORD *)buffer_list;
if ( !buffer_list )
LABEL_8:
exit(0);
while ( 1 )
{
v4 = v3[1];
if ( *(_BYTE *)(v4 + 60) )
break;
v3 = (_QWORD *)*v3;
if ( !v3 )
goto LABEL_8;
}
error((int)"%s has been modified.", *(_QWORD *)v4, (const char *)v4);
}
}

original_code:
static void quit_all(char **cmd_line)
{
    if (!(!cmd_line[1])) { error("Unexpected parameter."); return; };
    for (buffer_list_t *bl = buffer_list; bl != ((void *)0); bl = bl->next)
        if (!(!bl->buffer->modified)) { error("%s has been modified.", bl->buffer->name); return; };
    exit(0);
}

canonical_original_code:
static void quit_all(char **cmd_line)
{
    if (!(!cmd_line[1])) { error("Unexpected parameter."); return; };
    for (struct buffer_list *bl = buffer_list; bl != ((void *)0); bl = bl->next)
        if (!(!bl->buffer->modified)) { error("%s has been modified.", bl->buffer->name); return; };
    exit(0);
}

user_defined_types: struct buffer { char *name; char *location; int line_count; char **lines; int *line_screen_pos; int x; int ys; int linenr_width; _Bool modified; int oll_unused_lines; }


decompiled_code:
__int64 __fastcall write_and_quit(_QWORD *a1, __int64 a2, const char *a3)
{
__int64 result; // rax

if ( !a1[1] && !*(_BYTE *)(active_buffer + 60LL) || (result = raw_write(a1, a2, a3), (_BYTE)result) )
result = buffer_destroy(active_buffer);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1, __int64 a2, const char *a3)
{
__int64 result; // rax

if ( !a1[1] && !*(_BYTE *)(active_buffer + 60LL) || (result = func1(a1, a2, a3), (_BYTE)result) )
result = buffer_destroy(active_buffer);
return result;
}

original_code:
static void write_and_quit(char **cmd_line)
{
    if ((!cmd_line[1] && !active_buffer->modified) || raw_write(cmd_line))
        buffer_destroy(active_buffer);
}


decompiled_code:
void __fastcall force_quit(__int64 a1, int a2, const char *a3)
{
if ( *(_QWORD *)(a1 + 8) )
error((int)"Unexpected parameter.", a2, a3);
else
buffer_destroy(active_buffer);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, int a2, const char *a3)
{
if ( *(_QWORD *)(a1 + 8) )
error((int)"Unexpected parameter.", a2, a3);
else
buffer_destroy(active_buffer);
}

original_code:
static void force_quit(char **cmd_line)
{
    if (!(!cmd_line[1])) { error("Unexpected parameter."); return; };
    buffer_destroy(active_buffer);
}


quit

decompiled_code:
void __fastcall quit(__int64 a1, int a2, const char *a3)
{
if ( *(_QWORD *)(a1 + 8) )
{
error((int)"Unexpected parameter.", a2, a3);
}
else if ( *(_BYTE *)(active_buffer + 60LL) )
{
error((int)"%s has been modified.", *active_buffer, a3);
}
else
{
buffer_destroy(active_buffer);
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, int a2, const char *a3)
{
if ( *(_QWORD *)(a1 + 8) )
{
error((int)"Unexpected parameter.", a2, a3);
}
else if ( *(_BYTE *)(active_buffer + 60LL) )
{
error((int)"%s has been modified.", *active_buffer, a3);
}
else
{
buffer_destroy(active_buffer);
}
}

original_code:
static void quit(char **cmd_line)
{
    if (!(!cmd_line[1])) { error("Unexpected parameter."); return; };
    if (!(!active_buffer->modified)) { error("%s has been modified.", active_buffer->name); return; };
    buffer_destroy(active_buffer);
}


tabnew

decompiled_code:
void __fastcall tabnew(__int64 a1, int a2, const char *a3)
{
if ( *(_QWORD *)(a1 + 8) )
{
error((int)"Unexpected parameter.", a2, a3);
}
else
{
new_buffer();
buffer_activate_next();
}
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1, int a2, const char *a3)
{
if ( *(_QWORD *)(a1 + 8) )
{
error((int)"Unexpected parameter.", a2, a3);
}
else
{
new_buffer();
buffer_activate_next();
}
}

original_code:
static void tabnew(char **cmd_line)
{
    if (!(!cmd_line[1])) { error("Unexpected parameter."); return; };
    new_buffer();
    buffer_activate_next();
}


tabnext

decompiled_code:
void __fastcall tabnext(__int64 a1, int a2, const char *a3)
{
if ( *(_QWORD *)(a1 + 8) )
error((int)"Unexpected parameter.", a2, a3);
else
buffer_activate_next();
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1, int a2, const char *a3)
{
if ( *(_QWORD *)(a1 + 8) )
error((int)"Unexpected parameter.", a2, a3);
else
buffer_activate_next();
}

original_code:
static void tabnext(char **cmd_line)
{
    if (!(!cmd_line[1])) { error("Unexpected parameter."); return; };
    buffer_activate_next();
}


tabprevious

decompiled_code:
void __fastcall tabprevious(__int64 a1, int a2, const char *a3)
{
if ( *(_QWORD *)(a1 + 8) )
error((int)"Unexpected parameter.", a2, a3);
else
buffer_activate_prev();
}

canonical_decompiled_code:
void __fastcall func11(__int64 a1, int a2, const char *a3)
{
if ( *(_QWORD *)(a1 + 8) )
error((int)"Unexpected parameter.", a2, a3);
else
buffer_activate_prev();
}

original_code:
static void tabprevious(char **cmd_line)
{
    if (!(!cmd_line[1])) { error("Unexpected parameter."); return; };
    buffer_activate_prev();
}


decompiled_code:
__int64 __fastcall hash_eseq(char *a1)
{
char v1; // dl
_BYTE *v2; // rcx
__int64 result; // rax

v1 = *a1;
v2 = a1 + 1;
if ( !*a1 )
return 5LL;
LOBYTE(result) = 5;
do
{
++v2;
LOBYTE(result) = v1 ^ (33 * result);
v1 = *(v2 - 1);
}
while ( v1 );
return (unsigned __int8)result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char v1; // dl
_BYTE *v2; // rcx
__int64 result; // rax

v1 = *a1;
v2 = a1 + 1;
if ( !*a1 )
return 5LL;
LOBYTE(result) = 5;
do
{
++v2;
LOBYTE(result) = v1 ^ (33 * result);
v1 = *(v2 - 1);
}
while ( v1 );
return (unsigned __int8)result;
}

original_code:
static int hash_eseq(const char *sequence)
{
    uint32_t hash = 5381;
    uint8_t c;
    while ((c = *(sequence++)))
        hash = ((hash << 5) + hash) ^ c;
    return hash & ((1 << 8) - 1);
}

canonical_original_code:
static int hash_eseq(char *sequence)
{
    unsigned int hash = 5381;
    unsigned char c;
    while ((c = *(sequence++)))
        hash = ((hash << 5) + hash) ^ c;
    return hash & ((1 << 8) - 1);
}

variable_types:
sequence
char *

variable_types:
hash
unsigned int

variable_types:
c
unsigned char


decompiled_code:
__int64 read_escape_sequence()
{
int v0; // ebx
__int64 result; // rax
__int64 v2; // rbx
const char *v3; // rdi
__int64 v4; // rdx
int v5; // ebp
char s2[8]; // [rsp+20h] [rbp-68h] BYREF
__int64 v7; // [rsp+28h] [rbp-60h]
__int128 v8; // [rsp+30h] [rbp-58h]
regmatch_t v9; // [rsp+40h] [rbp-48h] BYREF
int v10; // [rsp+48h] [rbp-40h]
int v11; // [rsp+50h] [rbp-38h]
int v12; // [rsp+58h] [rbp-30h]
int v13; // [rsp+60h] [rbp-28h]

fcntl(0, 4, 2048LL);
v8 = 0LL;
*(_QWORD *)s2 = 27LL;
v7 = 0LL;
v0 = fread(&s2[1], 1uLL, 0x1EuLL, stdin);
fcntl(0, 4, 0LL);
result = 27LL;
if ( v0 > 0 )
{
v2 = eseq_trans_list[(int)hash_eseq(s2)];
if ( v2 )
{
while ( strcmp(*(const char **)(v2 + 8), s2) )
{
v2 = *(_QWORD *)v2;
if ( !v2 )
goto LABEL_8;
}
result = *(unsigned int *)(v2 + 16);
}
else
{
LABEL_8:
if ( !regexec(&mouse_input_regex_1006, s2, 5uLL, &v9, 0) )
{
atoi(&s2[v10]);
atoi(&s2[v11]);
v3 = &s2[v12];
atoi(v3);
v4 = (unsigned int)(s2[v13] != 77) + 2;
LABEL_10:
trigger_event(v3, s2, v4);
return 0LL;
}
v3 = (const char *)&mouse_input_regex_1015;
if ( !regexec(&mouse_input_regex_1015, s2, 4uLL, &v9, 0) )
{
v5 = atoi(&s2[v10]) - 32;
atoi(&s2[v11]);
v3 = &s2[v12];
atoi(v3);
LABEL_16:
if ( v5 > 2 )
{
v4 = 2LL;
if ( v5 == 3 )
v4 = 3LL;
}
else
{
last_down_0 = v5;
v4 = 2LL;
}
goto LABEL_10;
}
result = 0LL;
if ( s2[1] == 91 && s2[2] == 77 )
{
v5 = (unsigned __int8)s2[3] - 32;
goto LABEL_16;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // ebx
__int64 result; // rax
__int64 v2; // rbx
const char *v3; // rdi
__int64 v4; // rdx
int v5; // ebp
char s2[8]; // [rsp+20h] [rbp-68h] BYREF
__int64 v7; // [rsp+28h] [rbp-60h]
__int128 v8; // [rsp+30h] [rbp-58h]
regmatch_t v9; // [rsp+40h] [rbp-48h] BYREF
int v10; // [rsp+48h] [rbp-40h]
int v11; // [rsp+50h] [rbp-38h]
int v12; // [rsp+58h] [rbp-30h]
int v13; // [rsp+60h] [rbp-28h]

fcntl(0, 4, 2048LL);
v8 = 0LL;
*(_QWORD *)s2 = 27LL;
v7 = 0LL;
v0 = fread(&s2[1], 1uLL, 0x1EuLL, stdin);
fcntl(0, 4, 0LL);
result = 27LL;
if ( v0 > 0 )
{
v2 = eseq_trans_list[(int)func0(s2)];
if ( v2 )
{
while ( strcmp(*(const char **)(v2 + 8), s2) )
{
v2 = *(_QWORD *)v2;
if ( !v2 )
goto LABEL_8;
}
result = *(unsigned int *)(v2 + 16);
}
else
{
LABEL_8:
if ( !regexec(&mouse_input_regex_1006, s2, 5uLL, &v9, 0) )
{
atoi(&s2[v10]);
atoi(&s2[v11]);
v3 = &s2[v12];
atoi(v3);
v4 = (unsigned int)(s2[v13] != 77) + 2;
LABEL_10:
trigger_event(v3, s2, v4);
return 0LL;
}
v3 = (const char *)&mouse_input_regex_1015;
if ( !regexec(&mouse_input_regex_1015, s2, 4uLL, &v9, 0) )
{
v5 = atoi(&s2[v10]) - 32;
atoi(&s2[v11]);
v3 = &s2[v12];
atoi(v3);
LABEL_16:
if ( v5 > 2 )
{
v4 = 2LL;
if ( v5 == 3 )
v4 = 3LL;
}
else
{
last_down_0 = v5;
v4 = 2LL;
}
goto LABEL_10;
}
result = 0LL;
if ( s2[1] == 91 && s2[2] == 77 )
{
v5 = (unsigned __int8)s2[3] - 32;
goto LABEL_16;
}
}
}
return result;
}

original_code:
static int read_escape_sequence(void)
{
    fcntl(0, 4, 04000);
    char escape_sequence[32] = { '\033' };
    int eseq_len = fread(&escape_sequence[1], 1, 30, stdin);
    fcntl(0, 4, 0);
    if (eseq_len <= 0)
        return '\033';
    int hash = hash_eseq(escape_sequence);
    for (struct eseq_trans_list *etl = eseq_trans_list[hash]; etl != ((void *)0); etl = etl->next)
        if (!strcmp(etl->sequence, escape_sequence))
            return etl->keycode;
    int match_type;
    regmatch_t matches[5];
    if (!regexec(&mouse_input_regex_1006, escape_sequence, 5, matches, 0))
        match_type = 1006;
    else if (!regexec(&mouse_input_regex_1015, escape_sequence, 4, matches, 0))
        match_type = 1015;
    else if ((escape_sequence[1] == '[') && (escape_sequence[2] == 'M'))
        match_type = 1000;
    else
        return 0;
    int button = (match_type == 1000) ? escape_sequence[3] : atoi(&escape_sequence[matches[1].rm_so]);
    int x = (match_type == 1000) ? escape_sequence[4] : atoi(&escape_sequence[matches[2].rm_so]);
    int y = (match_type == 1000) ? escape_sequence[5] : atoi(&escape_sequence[matches[3].rm_so]);
    int event_type = EVENT_MBUTTON_DOWN;
    if (match_type == 1006)
        event_type = (escape_sequence[matches[4].rm_so] == 'M') ? EVENT_MBUTTON_DOWN : EVENT_MBUTTON_UP;
    else if ((match_type == 1015) || (match_type == 1000))
    {
        button -= ' ';
        if (match_type == 1000)
        {
            x -= ' ';
            y -= ' ';
        }
        static int last_down = 0;
        if (button < 3)
            last_down = button;
        else if (button == 3)
        {
            button = last_down;
            event_type = EVENT_MBUTTON_UP;
        }
    }
    trigger_event((event_t){ event_type, .code = button, .mbutton = { x - 1, y - 1 } });
    return 0;
}

canonical_original_code:
static int read_escape_sequence()
{
    fcntl(0, 4, 04000);
    char escape_sequence[32] = { '\033' };
    int eseq_len = fread(&escape_sequence[1], 1, 30, stdin);
    fcntl(0, 4, 0);
    if (eseq_len <= 0)
        return '\033';
    int hash = hash_eseq(escape_sequence);
    for (struct eseq_trans_list *etl = eseq_trans_list[hash]; etl != ((void *)0); etl = etl->next)
        if (!strcmp(etl->sequence, escape_sequence))
            return etl->keycode;
    int match_type;
    struct regmatch_t matches[5];
    if (!regexec(&mouse_input_regex_1006, escape_sequence, 5, matches, 0))
        match_type = 1006;
    else if (!regexec(&mouse_input_regex_1015, escape_sequence, 4, matches, 0))
        match_type = 1015;
    else if ((escape_sequence[1] == '[') && (escape_sequence[2] == 'M'))
        match_type = 1000;
    else
        return 0;
    int button = (match_type == 1000) ? escape_sequence[3] : atoi(&escape_sequence[matches[1].rm_so]);
    int x = (match_type == 1000) ? escape_sequence[4] : atoi(&escape_sequence[matches[2].rm_so]);
    int y = (match_type == 1000) ? escape_sequence[5] : atoi(&escape_sequence[matches[3].rm_so]);
    int event_type = EVENT_MBUTTON_DOWN;
    if (match_type == 1006)
        event_type = (escape_sequence[matches[4].rm_so] == 'M') ? EVENT_MBUTTON_DOWN : EVENT_MBUTTON_UP;
    else if ((match_type == 1015) || (match_type == 1000))
    {
        button -= ' ';
        if (match_type == 1000)
        {
            x -= ' ';
            y -= ' ';
        }
        static int last_down = 0;
        if (button < 3)
            last_down = button;
        else if (button == 3)
        {
            button = last_down;
            event_type = EVENT_MBUTTON_UP;
        }
    }
    trigger_event((struct event){ event_type, .code = button, .mbutton = { x - 1, y - 1 } });
    return 0;
}

variable_types: hash int

variable_types: struct eseq_trans_list { struct eseq_trans_list *next; char *sequence; int keycode; } *

variable_types: struct regmatch_t { int rm_so; int rm_eo; } [5]

variable_types: button int

user_defined_types: struct event { enum event_type type; union { int *key_seq; int code; }; struct { int x; } mbutton; }

user_defined_types: enum event_type { EVENT_NORMAL_KEY_SEQ=0, EVENT_INSERT_KEY=1, EVENT_MBUTTON_DOWN=2, EVENT_MBUTTON_UP=3 }


decompiled_code:
__int64 input_read()
{
__int64 v0; // rdx
unsigned int v1; // er12
__int64 result; // rax
int v3; // eax
int v4; // eax

if ( fifo_content )
{
v0 = fifo_content - 1;
fifo_content = v0;
v1 = *(_DWORD *)fifo;
if ( !v0 )
return v1;
memmove(fifo, (char *)fifo + 4, 4 * v0);
result = v1;
}
else
{
v3 = getchar();
v1 = v3;
if ( v3 == 27 )
{
result = read_escape_sequence();
}
else
{
if ( (unsigned int)(v3 - 1) > 0x19 )
return v1;
v4 = v3 + 96;
if ( v1 != 10 )
{
BYTE1(v4) = ((unsigned __int16)(v1 + 96) >> 8) | 8;
v1 = v4;
}
result = v1;
}
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rdx
unsigned int v1; // er12
__int64 result; // rax
int v3; // eax
int v4; // eax

if ( fifo_content )
{
v0 = fifo_content - 1;
fifo_content = v0;
v1 = *(_DWORD *)fifo;
if ( !v0 )
return v1;
memmove(fifo, (char *)fifo + 4, 4 * v0);
result = v1;
}
else
{
v3 = getchar();
v1 = v3;
if ( v3 == 27 )
{
result = func1();
}
else
{
if ( (unsigned int)(v3 - 1) > 0x19 )
return v1;
v4 = v3 + 96;
if ( v1 != 10 )
{
BYTE1(v4) = ((unsigned __int16)(v1 + 96) >> 8) | 8;
v1 = v4;
}
result = v1;
}
}
return result;
}

original_code:
int input_read(void)
{
    if (fifo_content)
    {
        int v = fifo[0];
        if (--fifo_content)
            memmove(&fifo[0], &fifo[1], fifo_content * sizeof(fifo[0]));
        return v;
    }
    int inp = getchar();
    if (inp == '\e')
        inp = read_escape_sequence();
    else if ((inp >= 1) && (inp <= 26) && (inp != '\n'))
        inp = (inp + 96) | 0x0800;
    return inp;
}

canonical_original_code:
int input_read()
{
    if (fifo_content)
    {
        int v = fifo[0];
        if (--fifo_content)
            memmove(&fifo[0], &fifo[1], fifo_content * sizeof(fifo[0]));
        return v;
    }
    int inp = getchar();
    if (inp == '\e')
        inp = read_escape_sequence();
    else if ((inp >= 1) && (inp <= 26) && (inp != '\n'))
        inp = (inp + 96) | 0x0800;
    return inp;
}

variable_types: v int

variable_types: inp int


decompiled_code:
void __fastcall sim_input(int a1)
{
__int64 v1; // rbx
__int64 v2; // r12
void *v3; // rax

v1 = fifo_content;
v2 = fifo_content + 1;
if ( fifo_content == -1 )
{
*((_DWORD *)fifo + fifo_content) = a1;
fifo_content = v2;
}
else
{
v3 = realloc(fifo, 0x40uLL);
fifo_content = v2;
fifo = v3;
*((_DWORD *)v3 + v1) = a1;
}
}

canonical_decompiled_code:
void __fastcall func4(int a1)
{
__int64 v1; // rbx
__int64 v2; // r12
void *v3; // rax

v1 = fifo_content;
v2 = fifo_content + 1;
if ( fifo_content == -1 )
{
*((_DWORD *)fifo + fifo_content) = a1;
fifo_content = v2;
}
else
{
v3 = realloc(fifo, 0x40uLL);
fifo_content = v2;
fifo = v3;
*((_DWORD *)v3 + v1) = a1;
}
}

original_code:
void sim_input(int val)
{
    if (fifo_content + 1 > fifo_size)
        fifo = realloc(fifo, fifo_size + 16 * sizeof(fifo[0]));
    fifo[fifo_content++] = val;
}

variable_types: val int


decompiled_code:
__int64 __fastcall update_buffer_name(void **a1)
{
const char *v2; // rbp
size_t v3; // rdi
char *v4; // rax
char v5; // r14
char *v6; // rbx
int i; // er13
char *v8; // rcx
int v9; // esi
int v10; // edx
char *v11; // rax
char *v12; // rdi

free(*a1);
v2 = (const char *)a1[1];
v3 = strlen(v2) + 1;
v4 = (char *)malloc(v3);
*a1 = v4;
v5 = *v2;
if ( *v2 )
{
v6 = v4;
for ( i = 0; ; i = v9 )
{
v11 = strchr(v2, 47);
v12 = &v6[i];
if ( !v11 )
break;
*v12 = v5;
v8 = (char *)a1[1];
v9 = i + 1;
v10 = (_DWORD)v11 - (_DWORD)v8;
if ( (_DWORD)v11 != (_DWORD)v8 )
{
*((_BYTE *)*a1 + v9) = 47;
v8 = (char *)a1[1];
v9 = i + 2;
}
v2 = &v8[v10 + 1];
v5 = *v2;
if ( !*v2 )
return full_redraw();
v6 = (char *)*a1;
}
strcpy(v12, v2);
}
return full_redraw();
}

canonical_decompiled_code:
__int64 __fastcall func0(void **a1)
{
const char *v2; // rbp
size_t v3; // rdi
char *v4; // rax
char v5; // r14
char *v6; // rbx
int i; // er13
char *v8; // rcx
int v9; // esi
int v10; // edx
char *v11; // rax
char *v12; // rdi

free(*a1);
v2 = (const char *)a1[1];
v3 = strlen(v2) + 1;
v4 = (char *)malloc(v3);
*a1 = v4;
v5 = *v2;
if ( *v2 )
{
v6 = v4;
for ( i = 0; ; i = v9 )
{
v11 = strchr(v2, 47);
v12 = &v6[i];
if ( !v11 )
break;
*v12 = v5;
v8 = (char *)a1[1];
v9 = i + 1;
v10 = (_DWORD)v11 - (_DWORD)v8;
if ( (_DWORD)v11 != (_DWORD)v8 )
{
*((_BYTE *)*a1 + v9) = 47;
v8 = (char *)a1[1];
v9 = i + 2;
}
v2 = &v8[v10 + 1];
v5 = *v2;
if ( !*v2 )
return full_redraw();
v6 = (char *)*a1;
}
strcpy(v12, v2);
}
return full_redraw();
}

original_code:
static void update_buffer_name(buffer_t *buf)
{
    free(buf->name);
    buf->name = malloc(strlen(buf->location) + 1);
    for (int i = 0, oi = 0; buf->location[i]; i++)
    {
        char *slash = strchr(&buf->location[i], '/');
        if (slash)
        {
            buf->name[oi++] = buf->location[i];
            if ((i = slash - buf->location))
                buf->name[oi++] = '/';
        }
        else
        {
            strcpy(&buf->name[oi], &buf->location[i]);
            break;
        }
    }
    full_redraw();
}

canonical_original_code:
static void update_buffer_name(struct buffer *buf)
{
    free(buf->name);
    buf->name = malloc(strlen(buf->location) + 1);
    for (int i = 0; int oi = 0; buf->location[i]; i++)
    {
        char *slash = strchr(&buf->location[i], '/');
        if (slash)
        {
            buf->name[oi++] = buf->location[i];
            if ((i = slash - buf->location))
                buf->name[oi++] = '/';
        }
        else
        {
            strcpy(&buf->name[oi], &buf->location[i]);
            break;
        }
    }
    full_redraw();
}

variable_types: struct buffer { char *name; char *location; int line_count; char **lines; int *line_screen_pos; int x; int ys; int linenr_width; _Bool modified; int oll_unused_lines; } *

variable_types: i int

variable_types: oi int

variable_types:
slash
char *


decompiled_code:
void *__fastcall ensure_line_size(void **a1, __int64 a2)
{
void *result; // rax

result = realloc(*a1, a2 + 1);
*a1 = result;
return result;
}

canonical_decompiled_code:
void *__fastcall func1(void **a1, __int64 a2)
{
void *result; // rax

result = realloc(*a1, a2 + 1);
*a1 = result;
return result;
}

original_code:
static void ensure_line_size(char **lineptr, size_t len)
{
    *lineptr = realloc(*lineptr, len + 1);
}

canonical_original_code:
static void ensure_line_size(char **lineptr, unsigned long len)
{
    *lineptr = realloc(*lineptr, len + 1);
}

variable_types:
lineptr
char **

variable_types:
len
unsigned long


decompiled_code:
void __fastcall buffer_destroy(void *ptr)
{
void **v1; // r12
void **v3; // rbx
void *v4; // rax
void **v5; // rdi
bool v6; // zf
char v7; // r12
__int64 v8; // rbx
void *v9; // rdi
__int64 v10; // rax

v1 = &buffer_list;
v3 = (void **)buffer_list;
if ( !buffer_list )
goto LABEL_6;
while ( 1 )
{
v4 = v3[1];
v5 = v3;
v3 = (void **)*v3;
if ( v4 == ptr )
break;
v1 = v5;
if ( !v3 )
goto LABEL_6;
}
free(v5);
v6 = active_buffer == (_QWORD)ptr;
*v1 = v3;
if ( v6 )
{
if ( v3 )
{
v7 = 1;
active_buffer = (__int64)v3[1];
}
else
{
if ( v1 == &buffer_list )
exit(0);
v10 = (__int64)v1[1];
v7 = 1;
active_buffer = v10;
}
}
else
{
LABEL_6:
v7 = 0;
}
v8 = 0LL;
free(*((void **)ptr + 1));
free(*(void **)ptr);
if ( *((int *)ptr + 4) > 0 )
{
do
{
v9 = *(void **)(*((_QWORD *)ptr + 3) + 8 * v8++);
free(v9);
}
while ( *((_DWORD *)ptr + 4) > (int)v8 );
}
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
free(ptr);
if ( v7 )
update_active_buffer();
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
void **v1; // r12
void **v3; // rbx
void *v4; // rax
void **v5; // rdi
bool v6; // zf
char v7; // r12
__int64 v8; // rbx
void *v9; // rdi
__int64 v10; // rax

v1 = &buffer_list;
v3 = (void **)buffer_list;
if ( !buffer_list )
goto LABEL_6;
while ( 1 )
{
v4 = v3[1];
v5 = v3;
v3 = (void **)*v3;
if ( v4 == ptr )
break;
v1 = v5;
if ( !v3 )
goto LABEL_6;
}
free(v5);
v6 = active_buffer == (_QWORD)ptr;
*v1 = v3;
if ( v6 )
{
if ( v3 )
{
v7 = 1;
active_buffer = (__int64)v3[1];
}
else
{
if ( v1 == &buffer_list )
exit(0);
v10 = (__int64)v1[1];
v7 = 1;
active_buffer = v10;
}
}
else
{
LABEL_6:
v7 = 0;
}
v8 = 0LL;
free(*((void **)ptr + 1));
free(*(void **)ptr);
if ( *((int *)ptr + 4) > 0 )
{
do
{
v9 = *(void **)(*((_QWORD *)ptr + 3) + 8 * v8++);
free(v9);
}
while ( *((_DWORD *)ptr + 4) > (int)v8 );
}
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
free(ptr);
if ( v7 )
update_active_buffer();
}

original_code:
void buffer_destroy(buffer_t *buf)
{
    _Bool active_buffer_changed = 0;
    buffer_list_t **blp;
    for (blp = &buffer_list; (*blp != ((void *)0)) && ((*blp)->buffer != buf); blp = &(*blp)->next);
    if (*blp != ((void *)0))
    {
        buffer_list_t *blp_next = (*blp)->next;
        free(*blp);
        *blp = blp_next;
        if (active_buffer == buf)
        {
            if (blp_next != ((void *)0))
                active_buffer = blp_next->buffer;
            else
            {
                if (blp == &buffer_list)
                    exit(0);
                else
                {
                    active_buffer = *(buffer_t **)((uintptr_t)blp - __builtin_offsetof (buffer_list_t, next) + __builtin_offsetof (buffer_list_t, buffer));
                }
            }
            active_buffer_changed = 1;
        }
    }
    free(buf->location);
    free(buf->name);
    for (int i = 0; i < buf->line_count; i++)
        free(buf->lines[i]);
    free(buf->lines);
    free(buf->line_screen_pos);
    free(buf);
    if (active_buffer_changed)
        update_active_buffer();
}

canonical_original_code:
void buffer_destroy(struct buffer *buf)
{
    _Bool active_buffer_changed = 0;
    struct buffer_list **blp;
    for (blp = &buffer_list; (*blp != ((void *)0)) && ((*blp)->buffer != buf); blp = &(*blp)->next);
    if (*blp != ((void *)0))
    {
        struct buffer_list *blp_next = (*blp)->next;
        free(*blp);
        *blp = blp_next;
        if (active_buffer == buf)
        {
            if (blp_next != ((void *)0))
                active_buffer = blp_next->buffer;
            else
            {
                if (blp == &buffer_list)
                    exit(0);
                else
                {
                    active_buffer = *(struct buffer **)((unsigned long)blp - __builtin_offsetof (buffer_list_t, next) + __builtin_offsetof (buffer_list_t, buffer));
                }
            }
            active_buffer_changed = 1;
        }
    }
    free(buf->location);
    free(buf->name);
    for (int i = 0; i < buf->line_count; i++)
        free(buf->lines[i]);
    free(buf->lines);
    free(buf->line_screen_pos);
    free(buf);
    if (active_buffer_changed)
        update_active_buffer();
}

variable_types: struct buffer_list { struct buffer_list *next; struct buffer { char *name; char *location; int line_count; char **lines; int *line_screen_pos; int x; int ys; int linenr_width; _Bool modified; int oll_unused_lines; } *buffer; } **


original_code:
void buffer_insert(buffer_t *buf, const char *string)
{
    buf->modified = 1;
    int ofs = utf8_byte_offset(buf->lines[buf->y], buf->x);
    size_t line_len = strlen(buf->lines[buf->y]);
    const char *nl = strchr(string, '\n');
    if (!nl)
    {
        size_t str_len = strlen(string);
        ensure_line_size(&buf->lines[buf->y], line_len + str_len);
        memmove(&buf->lines[buf->y][ofs + str_len], &buf->lines[buf->y][ofs], line_len - ofs + 1);
        memcpy(&buf->lines[buf->y][ofs], string, str_len);
        buf->x += utf8_strlen(string);
        return;
    }
    size_t str_len = nl - string;
    buf->lines = realloc(buf->lines, ++buf->line_count * sizeof(*buf->lines));
    buf->line_screen_pos = realloc(buf->line_screen_pos, buf->line_count * sizeof(*buf->line_screen_pos));
    buf->linenr_width = get_decimal_length(buf->line_count);
    memmove(&buf->lines[buf->y + 2], &buf->lines[buf->y + 1], (buf->line_count - buf->y - 2) * sizeof(*buf->lines));
    buf->lines[buf->y + 1] = malloc(line_len - ofs + 1);
    strcpy(buf->lines[buf->y + 1], &buf->lines[buf->y][ofs]);
    ensure_line_size(&buf->lines[buf->y], ofs + str_len);
    memcpy(&buf->lines[buf->y][ofs], string, str_len);
    buf->lines[buf->y][ofs + str_len] = 0;
    buf->x = 0;
    buf->y++;
    if (nl[1])
        buffer_insert(buf, nl + 1);
}

canonical_original_code:
void buffer_insert(struct buffer *buf, char *string)
{
    buf->modified = 1;
    int ofs = utf8_byte_offset(buf->lines[buf->y], buf->x);
    unsigned long line_len = strlen(buf->lines[buf->y]);
    char *nl = strchr(string, '\n');
    if (!nl)
    {
        unsigned long str_len = strlen(string);
        ensure_line_size(&buf->lines[buf->y], line_len + str_len);
        memmove(&buf->lines[buf->y][ofs + str_len], &buf->lines[buf->y][ofs], line_len - ofs + 1);
        memcpy(&buf->lines[buf->y][ofs], string, str_len);
        buf->x += utf8_strlen(string);
        return;
    }
    unsigned long str_len = nl - string;
    buf->lines = realloc(buf->lines, ++buf->line_count * sizeof(*buf->lines));
    buf->line_screen_pos = realloc(buf->line_screen_pos, buf->line_count * sizeof(*buf->line_screen_pos));
    buf->linenr_width = get_decimal_length(buf->line_count);
    memmove(&buf->lines[buf->y + 2], &buf->lines[buf->y + 1], (buf->line_count - buf->y - 2) * sizeof(*buf->lines));
    buf->lines[buf->y + 1] = malloc(line_len - ofs + 1);
    strcpy(buf->lines[buf->y + 1], &buf->lines[buf->y][ofs]);
    ensure_line_size(&buf->lines[buf->y], ofs + str_len);
    memcpy(&buf->lines[buf->y][ofs], string, str_len);
    buf->lines[buf->y][ofs + str_len] = 0;
    buf->x = 0;
    buf->y++;
    if (nl[1])
        buffer_insert(buf, nl + 1);
}

variable_types:
string
char *

variable_types: ofs int

variable_types:
nl
char *


original_code:
void buffer_delete(buffer_t *buf, int char_count)
{
    while (char_count > 0)
    {
        int remaining = utf8_strlen(buf->lines[buf->y]) - buf->x;
        int x_offset = utf8_byte_offset(buf->lines[buf->y], buf->x);
        if (remaining == char_count)
            buf->lines[buf->y][x_offset] = 0;
        else if (remaining > char_count)
        {
            int bytes = utf8_byte_offset(&buf->lines[buf->y][x_offset], char_count);
            memmove(&buf->lines[buf->y][x_offset], &buf->lines[buf->y][x_offset + bytes], strlen(&buf->lines[buf->y][x_offset + bytes]) + 1);
        }
        else
        {
            if (buf->line_count <= buf->y + 1)
            {
                buf->lines[buf->y][x_offset] = 0;
                break;
            }
            size_t next_line_length = strlen(buf->lines[buf->y + 1]);
            buf->lines[buf->y] = realloc(buf->lines[buf->y], x_offset + next_line_length + 1);
            memmove(&buf->lines[buf->y][x_offset], buf->lines[buf->y + 1], next_line_length + 1);
            free(buf->lines[buf->y + 1]);
            memmove(&buf->lines[buf->y + 1], &buf->lines[buf->y + 2], (--buf->line_count - buf->y - 1) * sizeof(buf->lines[0]));
            buf->linenr_width = get_decimal_length(buf->line_count);
            remaining++;
        }
        char_count -= remaining;
        buf->modified = 1;
    }
    int current_line_length = utf8_strlen(buf->lines[buf->y]);
    if (buf->x >= current_line_length)
        buf->x = (input_mode == MODE_INSERT) ? current_line_length : (current_line_length ? (current_line_length - 1) : 0);
}

canonical_original_code:
void buffer_delete(struct buffer *buf, int char_count)
{
    while (char_count > 0)
    {
        int remaining = utf8_strlen(buf->lines[buf->y]) - buf->x;
        int x_offset = utf8_byte_offset(buf->lines[buf->y], buf->x);
        if (remaining == char_count)
            buf->lines[buf->y][x_offset] = 0;
        else if (remaining > char_count)
        {
            int bytes = utf8_byte_offset(&buf->lines[buf->y][x_offset], char_count);
            memmove(&buf->lines[buf->y][x_offset], &buf->lines[buf->y][x_offset + bytes], strlen(&buf->lines[buf->y][x_offset + bytes]) + 1);
        }
        else
        {
            if (buf->line_count <= buf->y + 1)
            {
                buf->lines[buf->y][x_offset] = 0;
                break;
            }
            unsigned long next_line_length = strlen(buf->lines[buf->y + 1]);
            buf->lines[buf->y] = realloc(buf->lines[buf->y], x_offset + next_line_length + 1);
            memmove(&buf->lines[buf->y][x_offset], buf->lines[buf->y + 1], next_line_length + 1);
            free(buf->lines[buf->y + 1]);
            memmove(&buf->lines[buf->y + 1], &buf->lines[buf->y + 2], (--buf->line_count - buf->y - 1) * sizeof(buf->lines[0]));
            buf->linenr_width = get_decimal_length(buf->line_count);
            remaining++;
        }
        char_count -= remaining;
        buf->modified = 1;
    }
    int current_line_length = utf8_strlen(buf->lines[buf->y]);
    if (buf->x >= current_line_length)
        buf->x = (input_mode == MODE_INSERT) ? current_line_length : (current_line_length ? (current_line_length - 1) : 0);
}

variable_types: remaining int

variable_types: bytes int


original_code:
buffer_t *new_buffer(void)
{
    buffer_t *buf = malloc(sizeof(*buf));
    buf->location = ((void *)0);
    buf->x = buf->y = buf->ys = 0;
    buf->modified = 0;
    buf->line_count = 1;
    buf->linenr_width = 1;
    buf->lines = malloc(sizeof(*buf->lines));
    buf->line_screen_pos = malloc(sizeof(*buf->line_screen_pos));
    buf->lines[0] = malloc(1);
    buf->lines[0][0] = 0;
    buf->name = strdup("[unnamed]");
    buffer_list_append(buf);
    return buf;
}

canonical_original_code:
struct buffer *new_buffer()
{
    struct buffer *buf = malloc(sizeof(*buf));
    buf->location = ((void *)0);
    buf->x = buf->y = buf->ys = 0;
    buf->modified = 0;
    buf->line_count = 1;
    buf->linenr_width = 1;
    buf->lines = malloc(sizeof(*buf->lines));
    buf->line_screen_pos = malloc(sizeof(*buf->line_screen_pos));
    buf->lines[0] = malloc(1);
    buf->lines[0][0] = 0;
    buf->name = strdup("[unnamed]");
    buffer_list_append(buf);
    return buf;
}

return_type: struct buffer { char *name; char *location; int line_count; char **lines; int *line_screen_pos; int x; int ys; int linenr_width; _Bool modified; int oll_unused_lines; } *


decompiled_code:
__int64 __fastcall buffer_activate_next(__int64 a1)
{
__int64 v1; // rcx
_QWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax

if ( !buffer_list )
goto LABEL_7;
v1 = active_buffer;
v2 = buffer_list;
while ( 1 )
{
v3 = v2[1];
v2 = (_QWORD *)*v2;
if ( v3 == active_buffer )
break;
if ( !v2 )
goto LABEL_7;
}
if ( v2 )
{
active_buffer = v2[1];
result = update_active_buffer(a1, buffer_list, v3, v1);
}
else
{
LABEL_7:
active_buffer = *((_QWORD *)buffer_list + 1);
result = ((__int64 (*)(void))update_active_buffer)();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 v1; // rcx
_QWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax

if ( !buffer_list )
goto LABEL_7;
v1 = active_buffer;
v2 = buffer_list;
while ( 1 )
{
v3 = v2[1];
v2 = (_QWORD *)*v2;
if ( v3 == active_buffer )
break;
if ( !v2 )
goto LABEL_7;
}
if ( v2 )
{
active_buffer = v2[1];
result = update_active_buffer(a1, buffer_list, v3, v1);
}
else
{
LABEL_7:
active_buffer = *((_QWORD *)buffer_list + 1);
result = ((__int64 (*)(void))update_active_buffer)();
}
return result;
}

original_code:
void buffer_activate_next(void)
{
    buffer_list_t *bl;
    for (bl = buffer_list; (bl != ((void *)0)) && (bl->buffer != active_buffer); bl = bl->next);
    if ((bl != ((void *)0)) && (bl->next != ((void *)0)))
        active_buffer = bl->next->buffer;
    else
        active_buffer = buffer_list->buffer;
    update_active_buffer();
}

canonical_original_code:
void buffer_activate_next()
{
    struct buffer_list *bl;
    for (bl = buffer_list; (bl != ((void *)0)) && (bl->buffer != active_buffer); bl = bl->next);
    if ((bl != ((void *)0)) && (bl->next != ((void *)0)))
        active_buffer = bl->next->buffer;
    else
        active_buffer = buffer_list->buffer;
    update_active_buffer();
}

variable_types: struct buffer_list { struct buffer_list *next; struct buffer { char *name; char *location; int line_count; char **lines; int *line_screen_pos; int x; int ys; int linenr_width; _Bool modified; int oll_unused_lines; } *buffer; } *


decompiled_code:
__int64 __fastcall buffer_activate_prev(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rcx
_QWORD *v4; // rdx

v2 = buffer_list;
v3 = active_buffer;
do
{
v4 = v2;
v2 = (_QWORD *)*v2;
}
while ( v2 && v2[1] != active_buffer );
active_buffer = v4[1];
return update_active_buffer(a1, a2, v4, v3);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rcx
_QWORD *v4; // rdx

v2 = buffer_list;
v3 = active_buffer;
do
{
v4 = v2;
v2 = (_QWORD *)*v2;
}
while ( v2 && v2[1] != active_buffer );
active_buffer = v4[1];
return update_active_buffer(a1, a2, v4, v3);
}

original_code:
void buffer_activate_prev(void)
{
    buffer_list_t *bl;
    for (bl = buffer_list; (bl->next != ((void *)0)) && (bl->next->buffer != active_buffer); bl = bl->next);
    active_buffer = bl->buffer;
    update_active_buffer();
}

canonical_original_code:
void buffer_activate_prev()
{
    struct buffer_list *bl;
    for (bl = buffer_list; (bl->next != ((void *)0)) && (bl->next->buffer != active_buffer); bl = bl->next);
    active_buffer = bl->buffer;
    update_active_buffer();
}


decompiled_code:
__int64 __fastcall hash_event(__int64 a1)
{
unsigned int *v1; // rdx
unsigned __int8 v2; // cl
__int64 result; // rax
unsigned int *v4; // rdx

if ( *(_DWORD *)a1 )
return (unsigned __int8)(*(_BYTE *)(a1 + 8) + *(_DWORD *)a1);
v1 = *(unsigned int **)(a1 + 8);
v2 = 0;
result = *v1;
v4 = v1 + 1;
if ( (_DWORD)result )
{
do
{
++v4;
v2 += result;
LODWORD(result) = *(v4 - 1);
}
while ( (_DWORD)result );
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int *v1; // rdx
unsigned __int8 v2; // cl
__int64 result; // rax
unsigned int *v4; // rdx

if ( *(_DWORD *)a1 )
return (unsigned __int8)(*(_BYTE *)(a1 + 8) + *(_DWORD *)a1);
v1 = *(unsigned int **)(a1 + 8);
v2 = 0;
result = *v1;
v4 = v1 + 1;
if ( (_DWORD)result )
{
do
{
++v4;
v2 += result;
LODWORD(result) = *(v4 - 1);
}
while ( (_DWORD)result );
result = v2;
}
return result;
}

original_code:
static int hash_event(const event_t *event)
{
    if (event->type != EVENT_NORMAL_KEY_SEQ)
        return (event->type + event->code) & ((1 << 8) - 1);
    else
    {
        int sum = event->type;
        for (int i = 0; event->key_seq[i]; i++)
            sum += event->key_seq[i];
        return sum & ((1 << 8) - 1);
    }
}

canonical_original_code:
static int hash_event(struct event *event)
{
    if (event->type != EVENT_NORMAL_KEY_SEQ)
        return (event->type + event->code) & ((1 << 8) - 1);
    else
    {
        int sum = event->type;
        for (int i = 0; event->key_seq[i]; i++)
            sum += event->key_seq[i];
        return sum & ((1 << 8) - 1);
    }
}

variable_types: struct event { enum event_type { EVENT_NORMAL_KEY_SEQ=0, EVENT_INSERT_KEY=1, EVENT_MBUTTON_DOWN=2, EVENT_MBUTTON_UP=3 } type; union { int *key_seq; int code; }; struct { int x; } mbutton; } *

variable_types: sum int


decompiled_code:
bool __fastcall key_seq_cmp(__int64 a1, __int64 a2)
{
__int64 v2; // rax
int v3; // edx
int v4; // ecx

v2 = 0LL;
do
{
v3 = *(_DWORD *)(a1 + v2);
v4 = *(_DWORD *)(a2 + v2);
if ( !v3 )
return v4 == 0;
v2 += 4LL;
}
while ( v3 == v4 );
return 0;
}

canonical_decompiled_code:
bool __fastcall func1(__int64 a1, __int64 a2)
{
__int64 v2; // rax
int v3; // edx
int v4; // ecx

v2 = 0LL;
do
{
v3 = *(_DWORD *)(a1 + v2);
v4 = *(_DWORD *)(a2 + v2);
if ( !v3 )
return v4 == 0;
v2 += 4LL;
}
while ( v3 == v4 );
return 0;
}

original_code:
static _Bool key_seq_cmp(const int *ks1, const int *ks2)
{
    for (int i = 0; ks1[i] || ks2[i]; i++)
        if (ks1[i] != ks2[i])
            return 0;
    return 1;
}

canonical_original_code:
static _Bool key_seq_cmp(int *ks1, int *ks2)
{
    for (int i = 0; ks1[i] || ks2[i]; i++)
        if (ks1[i] != ks2[i])
            return 0;
    return 1;
}


original_code:
_Bool trigger_event(event_t event)
{
    int hash = hash_event(&event);
    if (event.type == EVENT_NORMAL_KEY_SEQ)
    {
        for (struct event_handler_list *ehl = event_handlers[hash]; ehl != ((void *)0); ehl = ehl->next)
            if ((ehl->handler.event.type == EVENT_NORMAL_KEY_SEQ) && key_seq_cmp(ehl->handler.event.key_seq, event.key_seq))
                return ehl->handler.handler(&event, ehl->handler.info);
    }
    else
    {
        for (struct event_handler_list *ehl = event_handlers[hash]; ehl != ((void *)0); ehl = ehl->next)
            if ((ehl->handler.event.code == event.code) && (ehl->handler.event.type == event.type))
                return ehl->handler.handler(&event, ehl->handler.info);
    }
    return 0;
}

canonical_original_code:
_Bool trigger_event(struct event event)
{
    int hash = hash_event(&event);
    if (event.type == EVENT_NORMAL_KEY_SEQ)
    {
        for (struct event_handler_list *ehl = event_handlers[hash]; ehl != ((void *)0); ehl = ehl->next)
            if ((ehl->handler.event.type == EVENT_NORMAL_KEY_SEQ) && key_seq_cmp(ehl->handler.event.key_seq, event.key_seq))
                return ehl->handler.handler(&event, ehl->handler.info);
    }
    else
    {
        for (struct event_handler_list *ehl = event_handlers[hash]; ehl != ((void *)0); ehl = ehl->next)
            if ((ehl->handler.event.code == event.code) && (ehl->handler.event.type == event.type))
                return ehl->handler.handler(&event, ehl->handler.info);
    }
    return 0;
}

variable_types: hash int

variable_types: struct event_handler_list { struct event_handler_list *next; struct event_handler { struct event { enum event_type { EVENT_NORMAL_KEY_SEQ=0, EVENT_INSERT_KEY=1, EVENT_MBUTTON_DOWN=2, EVENT_MBUTTON_UP=3 } type; union { int *key_seq; int code; }; struct { int x; } mbutton; } event; _Bool (*handler)(struct event *event, void *info); void *info; } handler; } *

user_defined_types: struct event_handler_list { struct event_handler_list *next; struct event_handler handler; }

user_defined_types: struct event_handler { struct event event; _Bool (*handler)(struct event *event, void *info); void *info; }


original_code:
void register_event_handler(event_t event, _Bool (*handler)(const event_t *event, void *info), void *info)
{
    int hash = hash_event(&event);
    for (struct event_handler_list *ehl = event_handlers[hash]; ehl != ((void *)0); ehl = ehl->next)
    {
        if ((ehl->handler.event.type == event.type) &&
           (((event.type == EVENT_NORMAL_KEY_SEQ) && key_seq_cmp(ehl->handler.event.key_seq, event.key_seq)) ||
            ((event.type != EVENT_NORMAL_KEY_SEQ) && (ehl->handler.event.code == event.code))))
        {
            if (event.type == EVENT_NORMAL_KEY_SEQ)
                free((void *)event.key_seq);
            ehl->handler.handler = handler;
            ehl->handler.info = info;
            return;
        }
    }
    struct event_handler_list *ehl = malloc(sizeof(*ehl));
    memcpy(&ehl->handler.event, &event, sizeof(event));
    ehl->handler.handler = handler;
    ehl->handler.info = info;
    ehl->next = event_handlers[hash];
    event_handlers[hash] = ehl;
}

canonical_original_code:
void register_event_handler(struct event event, _Bool (*handler)(struct event *event, void *info), void *info)
{
    int hash = hash_event(&event);
    for (struct event_handler_list *ehl = event_handlers[hash]; ehl != ((void *)0); ehl = ehl->next)
    {
        if ((ehl->handler.event.type == event.type) &&
           (((event.type == EVENT_NORMAL_KEY_SEQ) && key_seq_cmp(ehl->handler.event.key_seq, event.key_seq)) ||
            ((event.type != EVENT_NORMAL_KEY_SEQ) && (ehl->handler.event.code == event.code))))
        {
            if (event.type == EVENT_NORMAL_KEY_SEQ)
                free((void *)event.key_seq);
            ehl->handler.handler = handler;
            ehl->handler.info = info;
            return;
        }
    }
    struct event_handler_list *ehl = malloc(sizeof(*ehl));
    memcpy(&ehl->handler.event, &event, sizeof(event));
    ehl->handler.handler = handler;
    ehl->handler.info = info;
    ehl->next = event_handlers[hash];
    event_handlers[hash] = ehl;
}

variable_types: struct event { enum event_type { EVENT_NORMAL_KEY_SEQ=0, EVENT_INSERT_KEY=1, EVENT_MBUTTON_DOWN=2, EVENT_MBUTTON_UP=3 } type; union { int *key_seq; int code; }; struct { int x; } mbutton; }

variable_types: _Bool (*)(struct event *event, void *info)

variable_types:
info
void *


decompiled_code:
__int64 __fastcall syntax_region(int a1)
{
term_set_color(syntax_fg[a1], syntax_bg[a1]);
term_underline((unsigned __int8)syntax_underline[a1]);
return term_bold((unsigned __int8)syntax_bold[a1]);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
term_set_color(syntax_fg[a1], syntax_bg[a1]);
term_underline((unsigned __int8)syntax_underline[a1]);
return term_bold((unsigned __int8)syntax_bold[a1]);
}

original_code:
void syntax_region(enum syntax_region region)
{
    term_set_color(syntax_fg[region], syntax_bg[region]);
    term_underline(syntax_underline[region]);
    term_bold(syntax_bold[region]);
}

variable_types: enum syntax_region { SYNREG_DEFAULT=0, SYNREG_ERROR=1, SYNREG_TABBAR=2, SYNREG_TAB_ACTIVE_INNER=3, SYNREG_TAB_ACTIVE_OUTER=4, SYNREG_TAB_INACTIVE_INNER=5, SYNREG_TAB_INACTIVE_OUTER=6, SYNREG_LINENR=7, SYNREG_PLACEHOLDER_EMPTY=8, SYNREG_PLACEHOLDER_LINE=9, SYNREG_STATUSBAR=10, SYNREG_MODEBAR=11, SYNREG_NORMAL_COMMAND_COMPLETED=12, SYNREG_NORMAL_COMMAND_TYPING=13, SYNREG_COUNT=14 }


main

repo: XanClic/transalign-killer

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r14
__int64 v4; // rbx
__int64 v5; // rax
__int64 v6; // rdx
__int64 i; // rbp
__int64 v8; // r13
__int64 v9; // rax
__int64 v10; // r12
char *v11; // r15
__int64 v12; // rdx
__int64 v13; // rax
unsigned int v14; // ebx
int v15; // er14
__int64 v16; // rbx
__int64 v17; // rbp
__int64 v18; // rax
__int64 v19; // rdx
int v20; // er11
__int64 v21; // rcx
int v22; // er8
__int64 j; // r9
int v24; // er8
__int64 v25; // rdx
unsigned __int64 v26; // rsi
int v27; // eax
__int64 v28; // rdi
int v29; // er12
__int64 v31; // r8
unsigned int k; // er12
__int64 v33; // rax
__int64 v34; // [rsp-8h] [rbp-D0h]
_BYTE *v35; // [rsp+0h] [rbp-C8h]
__int64 v36; // [rsp+8h] [rbp-C0h]
__int64 v37; // [rsp+10h] [rbp-B8h]
signed __int64 v38; // [rsp+18h] [rbp-B0h]
__int64 v39; // [rsp+18h] [rbp-B0h]
__int64 v40; // [rsp+20h] [rbp-A8h]
__int64 v41; // [rsp+20h] [rbp-A8h]
__int64 v42; // [rsp+28h] [rbp-A0h]
int v43; // [rsp+30h] [rbp-98h]
__int64 v44; // [rsp+38h] [rbp-90h]
int v45; // [rsp+4Ch] [rbp-7Ch] BYREF
int v46; // [rsp+50h] [rbp-78h] BYREF
int v47; // [rsp+54h] [rbp-74h] BYREF
__int64 v48; // [rsp+58h] [rbp-70h] BYREF
__int64 v49; // [rsp+60h] [rbp-68h] BYREF
void *ptr; // [rsp+68h] [rbp-60h] BYREF
__int64 v51; // [rsp+70h] [rbp-58h] BYREF
__int64 v52; // [rsp+78h] [rbp-50h] BYREF
__int64 v53; // [rsp+80h] [rbp-48h] BYREF
__int64 v54[8]; // [rsp+88h] [rbp-40h] BYREF

if ( argc <= 1 )
{
v29 = 1;
fwrite("Usage: transalign_killer [--cldev=x.y] <input file>\n", 1uLL, 0x34uLL, stderr);
fwrite(" --cldev=x.y: x specifies the platform index, y the device index.\n", 1uLL, 0x43uLL, stderr);
}
else
{
v35 = load_text(argv[argc - 1], (size_t *)&v48);
if ( !v35 )
goto LABEL_22;
v3 = 0LL;
v38 = --v48;
v4 = round_up_to_power_of_two_constprop_0(v48);
v5 = v4 / 16;
v6 = v4 % 16;
for ( i = v4 / 16; v5; v5 /= 16LL )
{
v3 += v5;
v6 = v5 + 15;
}
v49 = select_device((unsigned int)(argc - 1), argv, v6);
if ( v49
&& (v37 = clCreateContext(0LL, 1LL, &v49, 0LL, 0LL, 0LL),
v8 = clCreateCommandQueue(v37, v49, 0LL, 0LL),
(ptr = load_text("trans.cl", 0LL)) != 0LL) )
{
v42 = clCreateProgramWithSource(v37, 1LL, &ptr, 0LL, 0LL);
free(ptr);
clBuildProgram(v42, 0LL, 0LL, 0LL, 0LL, 0LL);
v36 = clCreateKernel(v42, "k_iadd", 0LL);
v9 = clCreateKernel(v42, "k_cadd", 0LL);
v10 = v9;
if ( !v36 )
_assert_fail("k_iadd", "main.c", 0x8Du, "main");
if ( !v9 )
_assert_fail("k_cadd", "main.c", 0x8Eu, "main");
v40 = 4 * v3;
v11 = (char *)malloc(4 * v3);
v51 = clCreateBuffer(v37, 9LL, 4 * v3, v11, 0LL);
clock_start();
v52 = clCreateBuffer(v37, 9LL, v48, v35, 0LL);
v44 = ((__int64 (*)(void))clock_delta)();
clSetKernelArg(v36, 0LL, 8LL, &v51);
clSetKernelArg(v36, 1LL, 8LL, &v52);
v45 = v48;
clSetKernelArg(v36, 2LL, 4LL, &v45);
v53 = v4 / 16;
clEnqueueNDRangeKernel(v8, v36, 1LL, 0LL, &v53, 0LL, 0LL, 0LL, 0LL, v12);
v13 = v4 / 256;
v14 = v4 / 256;
if ( (_DWORD)v13 )
{
v43 = v3;
v15 = 0;
do
{
clSetKernelArg(v10, 0LL, 8LL, &v51);
v46 = i;
clSetKernelArg(v10, 1LL, 4LL, &v46);
v47 = v15;
v15 = i;
LODWORD(i) = v14 + i;
clSetKernelArg(v10, 2LL, 4LL, &v47);
clFinish(v8);
v54[0] = v14;
clEnqueueNDRangeKernel(v8, v10, 1LL, 0LL, v54, 0LL, 0LL, 0LL, 0LL, v34);
v14 >>= 4;
}
while ( v14 );
LODWORD(v3) = v43;
}
v16 = v38 / 2;
clFinish(v8);
v17 = ((__int64 (*)(void))clock_delta)();
clEnqueueReadBuffer(v8, v51, 1LL, 0LL, v40, v11, 0LL, 0LL, 0LL, v17);
v18 = ((__int64 (*)(void))clock_delta)();
v19 = *(unsigned int *)&v11[v40 - 4];
v39 = v18;
if ( (unsigned int)v19 < (unsigned int)v16 )
{
v29 = 1;
fprintf(stderr, "Logical index out of bounds (last index: %u).\n", v19);
}
else if ( (_DWORD)v16 )
{
v20 = v3 - 17;
v21 = (unsigned int)(v16 - 1);
v22 = 0;
for ( j = 16LL; ; v20 -= j )
{
v24 = 16 * v22;
v25 = *(unsigned int *)&v11[4 * (v24 + v20)];
v26 = (unsigned int)(v24 + v20);
if ( (unsigned int)v21 < (unsigned int)v25 )
{
v28 = 0LL;
}
else
{
v27 = 0;
do
{
++v27;
v21 = (unsigned int)(v21 - v25);
v28 = (unsigned int)v27;
v25 = *(unsigned int *)&v11[4 * (unsigned int)(v26 + v27)];
}
while ( (unsigned int)v25 <= (unsigned int)v21 );
if ( v27 > 15 )
_assert_fail("subtree < BASE", "main.c", 0x133u, "main");
}
v22 = v28 + v24;
if ( !v20 )
break;
j = (unsigned int)(16 * j);
}
v31 = (unsigned int)(16 * v22);
for ( k = v31; (_DWORD)v21; v21 = (unsigned int)(v21 - 1) )
{
do
{
v26 = (unsigned __int64)v35;
v33 = k++;
}
while ( v35[v33] == 45 );
}
v41 = clock_delta(v28, v26, v25, v21, v31, j);
printf("%li us elapsed total\n", v41);
printf(" - %li us on bandwidth forth\n", v44);
printf(" - %li us on GPU\n", v17 - v44);
printf(" - %li us on bandwidth back\n", v39 - v17);
printf(" - %li us on CPU\n", v41 - v39);
printf("Index for %u: %u\n", (unsigned int)v16, k);
printf("cnt = %u (index + 1)\n", k + 1);
v29 = 0;
}
else
{
v29 = 1;
fwrite("Please used 1-based indexing (for whatever reason).\n", 1uLL, 0x34uLL, stderr);
}
free(v35);
free(v11);
clReleaseMemObject(v51);
clReleaseMemObject(v52);
clReleaseKernel(v36);
clReleaseProgram(v42);
clReleaseCommandQueue(v8);
clReleaseContext(v37);
}
else
{
LABEL_22:
v29 = 1;
}
}
return v29;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r14
__int64 v4; // rbx
__int64 v5; // rax
__int64 v6; // rdx
__int64 i; // rbp
__int64 v8; // r13
__int64 v9; // rax
__int64 v10; // r12
char *v11; // r15
__int64 v12; // rdx
__int64 v13; // rax
unsigned int v14; // ebx
int v15; // er14
__int64 v16; // rbx
__int64 v17; // rbp
__int64 v18; // rax
__int64 v19; // rdx
int v20; // er11
__int64 v21; // rcx
int v22; // er8
__int64 j; // r9
int v24; // er8
__int64 v25; // rdx
unsigned __int64 v26; // rsi
int v27; // eax
__int64 v28; // rdi
int v29; // er12
__int64 v31; // r8
unsigned int k; // er12
__int64 v33; // rax
__int64 v34; // [rsp-8h] [rbp-D0h]
_BYTE *v35; // [rsp+0h] [rbp-C8h]
__int64 v36; // [rsp+8h] [rbp-C0h]
__int64 v37; // [rsp+10h] [rbp-B8h]
signed __int64 v38; // [rsp+18h] [rbp-B0h]
__int64 v39; // [rsp+18h] [rbp-B0h]
__int64 v40; // [rsp+20h] [rbp-A8h]
__int64 v41; // [rsp+20h] [rbp-A8h]
__int64 v42; // [rsp+28h] [rbp-A0h]
int v43; // [rsp+30h] [rbp-98h]
__int64 v44; // [rsp+38h] [rbp-90h]
int v45; // [rsp+4Ch] [rbp-7Ch] BYREF
int v46; // [rsp+50h] [rbp-78h] BYREF
int v47; // [rsp+54h] [rbp-74h] BYREF
__int64 v48; // [rsp+58h] [rbp-70h] BYREF
__int64 v49; // [rsp+60h] [rbp-68h] BYREF
void *ptr; // [rsp+68h] [rbp-60h] BYREF
__int64 v51; // [rsp+70h] [rbp-58h] BYREF
__int64 v52; // [rsp+78h] [rbp-50h] BYREF
__int64 v53; // [rsp+80h] [rbp-48h] BYREF
__int64 v54[8]; // [rsp+88h] [rbp-40h] BYREF

if ( argc <= 1 )
{
v29 = 1;
fwrite("Usage: transalign_killer [--cldev=x.y] <input file>\n", 1uLL, 0x34uLL, stderr);
fwrite(" --cldev=x.y: x specifies the platform index, y the device index.\n", 1uLL, 0x43uLL, stderr);
}
else
{
v35 = func0(argv[argc - 1], (size_t *)&v48);
if ( !v35 )
goto LABEL_22;
v3 = 0LL;
v38 = --v48;
v4 = func1(v48);
v5 = v4 / 16;
v6 = v4 % 16;
for ( i = v4 / 16; v5; v5 /= 16LL )
{
v3 += v5;
v6 = v5 + 15;
}
v49 = select_device((unsigned int)(argc - 1), argv, v6);
if ( v49
&& (v37 = clCreateContext(0LL, 1LL, &v49, 0LL, 0LL, 0LL),
v8 = clCreateCommandQueue(v37, v49, 0LL, 0LL),
(ptr = func0("trans.cl", 0LL)) != 0LL) )
{
v42 = clCreateProgramWithSource(v37, 1LL, &ptr, 0LL, 0LL);
free(ptr);
clBuildProgram(v42, 0LL, 0LL, 0LL, 0LL, 0LL);
v36 = clCreateKernel(v42, "k_iadd", 0LL);
v9 = clCreateKernel(v42, "k_cadd", 0LL);
v10 = v9;
if ( !v36 )
_assert_fail("k_iadd", "main.c", 0x8Du, "main");
if ( !v9 )
_assert_fail("k_cadd", "main.c", 0x8Eu, "main");
v40 = 4 * v3;
v11 = (char *)malloc(4 * v3);
v51 = clCreateBuffer(v37, 9LL, 4 * v3, v11, 0LL);
clock_start();
v52 = clCreateBuffer(v37, 9LL, v48, v35, 0LL);
v44 = ((__int64 (*)(void))clock_delta)();
clSetKernelArg(v36, 0LL, 8LL, &v51);
clSetKernelArg(v36, 1LL, 8LL, &v52);
v45 = v48;
clSetKernelArg(v36, 2LL, 4LL, &v45);
v53 = v4 / 16;
clEnqueueNDRangeKernel(v8, v36, 1LL, 0LL, &v53, 0LL, 0LL, 0LL, 0LL, v12);
v13 = v4 / 256;
v14 = v4 / 256;
if ( (_DWORD)v13 )
{
v43 = v3;
v15 = 0;
do
{
clSetKernelArg(v10, 0LL, 8LL, &v51);
v46 = i;
clSetKernelArg(v10, 1LL, 4LL, &v46);
v47 = v15;
v15 = i;
LODWORD(i) = v14 + i;
clSetKernelArg(v10, 2LL, 4LL, &v47);
clFinish(v8);
v54[0] = v14;
clEnqueueNDRangeKernel(v8, v10, 1LL, 0LL, v54, 0LL, 0LL, 0LL, 0LL, v34);
v14 >>= 4;
}
while ( v14 );
LODWORD(v3) = v43;
}
v16 = v38 / 2;
clFinish(v8);
v17 = ((__int64 (*)(void))clock_delta)();
clEnqueueReadBuffer(v8, v51, 1LL, 0LL, v40, v11, 0LL, 0LL, 0LL, v17);
v18 = ((__int64 (*)(void))clock_delta)();
v19 = *(unsigned int *)&v11[v40 - 4];
v39 = v18;
if ( (unsigned int)v19 < (unsigned int)v16 )
{
v29 = 1;
fprintf(stderr, "Logical index out of bounds (last index: %u).\n", v19);
}
else if ( (_DWORD)v16 )
{
v20 = v3 - 17;
v21 = (unsigned int)(v16 - 1);
v22 = 0;
for ( j = 16LL; ; v20 -= j )
{
v24 = 16 * v22;
v25 = *(unsigned int *)&v11[4 * (v24 + v20)];
v26 = (unsigned int)(v24 + v20);
if ( (unsigned int)v21 < (unsigned int)v25 )
{
v28 = 0LL;
}
else
{
v27 = 0;
do
{
++v27;
v21 = (unsigned int)(v21 - v25);
v28 = (unsigned int)v27;
v25 = *(unsigned int *)&v11[4 * (unsigned int)(v26 + v27)];
}
while ( (unsigned int)v25 <= (unsigned int)v21 );
if ( v27 > 15 )
_assert_fail("subtree < BASE", "main.c", 0x133u, "main");
}
v22 = v28 + v24;
if ( !v20 )
break;
j = (unsigned int)(16 * j);
}
v31 = (unsigned int)(16 * v22);
for ( k = v31; (_DWORD)v21; v21 = (unsigned int)(v21 - 1) )
{
do
{
v26 = (unsigned __int64)v35;
v33 = k++;
}
while ( v35[v33] == 45 );
}
v41 = clock_delta(v28, v26, v25, v21, v31, j);
printf("%li us elapsed total\n", v41);
printf(" - %li us on bandwidth forth\n", v44);
printf(" - %li us on GPU\n", v17 - v44);
printf(" - %li us on bandwidth back\n", v39 - v17);
printf(" - %li us on CPU\n", v41 - v39);
printf("Index for %u: %u\n", (unsigned int)v16, k);
printf("cnt = %u (index + 1)\n", k + 1);
v29 = 0;
}
else
{
v29 = 1;
fwrite("Please used 1-based indexing (for whatever reason).\n", 1uLL, 0x34uLL, stderr);
}
free(v35);
free(v11);
clReleaseMemObject(v51);
clReleaseMemObject(v52);
clReleaseKernel(v36);
clReleaseProgram(v42);
clReleaseCommandQueue(v8);
clReleaseContext(v37);
}
else
{
LABEL_22:
v29 = 1;
}
}
return v29;
}

original_code:
int main(int argc, char *argv[])
{
    int ret = 0;
    if (argc < 2)
    {
        fprintf(stderr, "Usage: transalign_killer <input file>\n");
        return 1;
    }
    long seq_length;
    char *sequence = load_text(argv[argc - 1], &seq_length);
    if (!sequence)
        return 1;
    seq_length--;
    unsigned long letter_index = seq_length / 2;
    clock_start();
    unsigned long index = find_index(sequence, letter_index - 1);
    long delta_time = clock_delta();
    printf("%li us elapsed total\n\n", delta_time);
    printf("Index for %lu: %lu\n", letter_index, index);
    printf("cnt = %lu (index + 1)\n", index + 1);
    free(sequence);
    return ret;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int

variable_types:
sequence
char *

variable_types:
index
unsigned long


repo: Xangis/ModernMUDConverter

decompiled_code:
__int64 __fastcall is_name(__int64 a1, __int64 a2)
{
char v4[1224]; // [rsp+0h] [rbp-4C8h] BYREF

do
{
a2 = one_argument(a2, v4);
if ( !v4[0] )
return 0LL;
}
while ( (unsigned __int8)str_cmp(a1, v4) );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
char v4[1224]; // [rsp+0h] [rbp-4C8h] BYREF

do
{
a2 = one_argument(a2, v4);
if ( !v4[0] )
return 0LL;
}
while ( (unsigned __int8)str_cmp(a1, v4) );
return 1LL;
}

original_code:
bool is_name( const char *str, char *namelist )
{
    char name [299 * 4];
    for ( ; ; )
    {
 namelist = one_argument( namelist, name );
 if ( name[0] == '\0' )
     return 0;
 if ( !str_cmp( str, name ) )
     return 1;
    }
}

canonical_original_code:
unsigned char is_name(char *str, char *namelist)
{
    char name[-1];
    for ( ; ; )
    {
 namelist = one_argument( namelist, name );
 if ( name[0] == '\0' )
     return 0;
 if ( !str_cmp( str, name ) )
     return 1;
    }
}

variable_types:
str
char *

variable_types:
namelist
char *

variable_types:
name
char [-1]

return_type: unsigned char