decompiled_code:
__int64 __fastcall opts_next_char(unsigned int *a1)
{
unsigned int v1; // edx
__int64 v2; // rcx
__int64 v3; // rax
__int64 result; // rax

a1[6] = -1;
v1 = *a1;
if ( *a1 >= a1[2] )
return 0xFFFFFFFFLL;
v2 = *(_QWORD *)(*((_QWORD *)a1 + 2) + 8LL * v1);
v3 = a1[1] + 1;
a1[1] = v3;
result = *(unsigned __int8 *)(v2 + v3);
if ( (_BYTE)result )
{
if ( (_BYTE)result == 61 )
{
result = 32LL;
a1[6] = 32;
}
else
{
a1[6] = (char)result;
}
}
else
{
a1[1] = -1;
result = 32LL;
*a1 = v1 + 1;
a1[6] = 32;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1)
{
unsigned int v1; // edx
__int64 v2; // rcx
__int64 v3; // rax
__int64 result; // rax

a1[6] = -1;
v1 = *a1;
if ( *a1 >= a1[2] )
return 0xFFFFFFFFLL;
v2 = *(_QWORD *)(*((_QWORD *)a1 + 2) + 8LL * v1);
v3 = a1[1] + 1;
a1[1] = v3;
result = *(unsigned __int8 *)(v2 + v3);
if ( (_BYTE)result )
{
if ( (_BYTE)result == 61 )
{
result = 32LL;
a1[6] = 32;
}
else
{
a1[6] = (char)result;
}
}
else
{
a1[1] = -1;
result = 32LL;
*a1 = v1 + 1;
a1[6] = 32;
}
return result;
}

original_code:
static char opts_next_char( stream_ctx_t* ctx ) {
    char current = (-1);
    ctx->current = (-1);
    if (ctx->line_idx < ctx->arg_count) {
        ctx->col_idx++;
        if (ctx->line_idx < ctx->arg_count) {
            char temp = ctx->arg_vect[ ctx->line_idx ][ ctx->col_idx ];
            if (temp == '\0') {
                ctx->col_idx = -1;
                ctx->line_idx++;
                current = ' ';
                ctx->current = current;
            } else {
                temp = (temp == '=') ? ' ' : temp;
                current = temp;
                ctx->current = current;
            }
        }
    }
    return current;
}

canonical_original_code:
static char opts_next_char(struct stream_ctx_t *ctx) {
    char current = (-1);
    ctx->current = (-1);
    if (ctx->line_idx < ctx->arg_count) {
        ctx->col_idx++;
        if (ctx->line_idx < ctx->arg_count) {
            char temp = ctx->arg_vect[ ctx->line_idx ][ ctx->col_idx ];
            if (temp == '\0') {
                ctx->col_idx = -1;
                ctx->line_idx++;
                current = ' ';
                ctx->current = current;
            } else {
                temp = (temp == '=') ? ' ' : temp;
                current = temp;
                ctx->current = current;
            }
        }
    }
    return current;
}

variable_types: struct stream_ctx_t { unsigned int line_idx; unsigned int col_idx; unsigned int arg_count; char **arg_vect; int current; struct opts_cfg_t { char *name; _Bool has_arg; char *tag; char *desc; } *options; } *

variable_types: current char

variable_types: temp char

user_defined_types: struct stream_ctx_t { unsigned int line_idx; unsigned int col_idx; unsigned int arg_count; char **arg_vect; int current; struct opts_cfg_t *options; }


repo: mikedlowis/opts

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

while ( a1[6] == 32 )
result = opts_next_char(a1);
return result;
}

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

while ( a1[6] == 32 )
result = func0(a1);
return result;
}

original_code:
static void opts_consume_ws( stream_ctx_t* ctx ) {
    while (' ' == ctx->current)
        (void)opts_next_char( ctx );
}

canonical_original_code:
static void opts_consume_ws(struct stream_ctx_t *ctx) {
    while (' ' == ctx->current)
        (void)opts_next_char( ctx );
}

return_type: void

user_defined_types: struct opts_cfg_t { char *name; _Bool has_arg; char *tag; char *desc; }


decompiled_code:
__int64 __fastcall opts_calc_padding(const char **a1)
{
const char **v1; // rbx
const char *v2; // rdi
size_t v3; // rbp
char v4; // r12
size_t v5; // rax
__int64 result; // rax
unsigned int v7; // ebp

v1 = a1;
v2 = *a1;
if ( !v2 )
return 4LL;
v3 = 0LL;
v4 = 0;
do
{
v5 = strlen(v2);
if ( v3 < v5 )
v3 = v5;
if ( *((_BYTE *)v1 + 8) )
v4 = *((_BYTE *)v1 + 8);
v2 = v1[4];
v1 += 4;
}
while ( v2 );
result = (unsigned int)(v3 + 8);
v7 = v3 + 4;
if ( !v4 )
result = v7;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char **a1)
{
const char **v1; // rbx
const char *v2; // rdi
size_t v3; // rbp
char v4; // r12
size_t v5; // rax
__int64 result; // rax
unsigned int v7; // ebp

v1 = a1;
v2 = *a1;
if ( !v2 )
return 4LL;
v3 = 0LL;
v4 = 0;
do
{
v5 = strlen(v2);
if ( v3 < v5 )
v3 = v5;
if ( *((_BYTE *)v1 + 8) )
v4 = *((_BYTE *)v1 + 8);
v2 = v1[4];
v1 += 4;
}
while ( v2 );
result = (unsigned int)(v3 + 8);
v7 = v3 + 4;
if ( !v4 )
result = v7;
return result;
}

original_code:
static int opts_calc_padding(opts_cfg_t* opts) {
    _Bool opts_have_args = 0;
    size_t sz = 0;
    while (((void *)0) != opts->name) {
        size_t name_sz = strlen(opts->name);
        if (name_sz > sz) {
            sz = name_sz;
        }
        if (opts->has_arg) {
            opts_have_args = 1;
        }
        opts++;
    }
    return sz + 4 + ((opts_have_args) ? 4 : 0);
}

canonical_original_code:
static int opts_calc_padding(struct opts_cfg_t *opts) {
    _Bool opts_have_args = 0;
    unsigned long sz = 0;
    while (((void *)0) != opts->name) {
        unsigned long name_sz = strlen(opts->name);
        if (name_sz > sz) {
            sz = name_sz;
        }
        if (opts->has_arg) {
            opts_have_args = 1;
        }
        opts++;
    }
    return sz + 4 + ((opts_have_args) ? 4 : 0);
}

variable_types: struct opts_cfg_t { char *name; _Bool has_arg; char *tag; char *desc; } *

variable_types:
sz
unsigned long

return_type: int


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

result = opts_next_token(a1);
if ( result )
result = opts_add_argument((__int64)result);
return result;
}

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

result = func3(a1);
if ( result )
result = opts_add_argument((__int64)result);
return result;
}

original_code:
static void opts_parse_argument( stream_ctx_t* ctx ) {
    char* arg_val = opts_next_token( ctx );
    if (((void *)0) != arg_val)
        opts_add_argument(arg_val);
}

canonical_original_code:
static void opts_parse_argument(struct stream_ctx_t *ctx) {
    char *arg_val = opts_next_token( ctx );
    if (((void *)0) != arg_val)
        opts_add_argument(arg_val);
}


decompiled_code:
const char *__fastcall opts_get_option_config(const char **a1, int a2, const char *a3)
{
const char *v3; // rbp
const char **v4; // rbx

v3 = *a1;
if ( *a1 )
{
v4 = a1;
while ( strlen(v3) <= 1 != a2 || strcmp(v3, a3) )
{
v3 = v4[4];
v4 += 4;
if ( !v3 )
return v3;
}
v3 = (const char *)v4;
}
return v3;
}

canonical_decompiled_code:
const char *__fastcall func6(const char **a1, int a2, const char *a3)
{
const char *v3; // rbp
const char **v4; // rbx

v3 = *a1;
if ( *a1 )
{
v4 = a1;
while ( strlen(v3) <= 1 != a2 || strcmp(v3, a3) )
{
v3 = v4[4];
v4 += 4;
if ( !v3 )
return v3;
}
v3 = (const char *)v4;
}
return v3;
}

original_code:
static opts_cfg_t* opts_get_option_config( opts_cfg_t* opts, opt_type_t type, char* name ) {
    opts_cfg_t* cfg = ((void *)0);
    int i = 0;
    while( opts[i].name != ((void *)0) ) {
        opt_type_t curr_type = (strlen(opts[i].name) > 1) ? LONG : SHORT;
        if ((curr_type == type) && (0 == strcmp(opts[i].name, name))) {
            cfg = &(opts[i]);
            break;
        }
        i++;
    }
    return cfg;
}

canonical_original_code:
static struct opts_cfg_t *opts_get_option_config(struct opts_cfg_t *opts, enum opt_type_t type, char *name) {
    struct opts_cfg_t *cfg = ((void *)0);
    int i = 0;
    while( opts[i].name != ((void *)0) ) {
        enum opt_type_t curr_type = (strlen(opts[i].name) > 1) ? LONG : SHORT;
        if ((curr_type == type) && (0 == strcmp(opts[i].name, name))) {
            cfg = &(opts[i]);
            break;
        }
        i++;
    }
    return cfg;
}

variable_types:
opts
struct opts_cfg_t *

variable_types:
type
enum opt_type_t { LONG=0, SHORT=1 }

variable_types:
name
char *

variable_types: i int

return_type: struct opts_cfg_t { char *name; _Bool has_arg; char *tag; char *desc; } *


decompiled_code:
const char **__fastcall find_option(char *s1, char *a2)
{
const char ***v2; // rbx
const char **v3; // r12

v2 = (const char ***)Options;
if ( !Options )
return 0LL;
while ( 1 )
{
v3 = *v2;
if ( (!s1 || !strcmp(s1, *v3)) && (!a2 || !strcmp(a2, v3[1])) )
break;
v2 = (const char ***)v2[1];
if ( !v2 )
return 0LL;
}
return v3;
}

canonical_decompiled_code:
const char **__fastcall func7(char *s1, char *a2)
{
const char ***v2; // rbx
const char **v3; // r12

v2 = (const char ***)Options;
if ( !Options )
return 0LL;
while ( 1 )
{
v3 = *v2;
if ( (!s1 || !strcmp(s1, *v3)) && (!a2 || !strcmp(a2, v3[1])) )
break;
v2 = (const char ***)v2[1];
if ( !v2 )
return 0LL;
}
return v3;
}

original_code:
static option_t* find_option(const char* name, const char* tag) {
    option_t* p_opt = ((void *)0);
    entry_t* current = Options;
    while (current != ((void *)0)) {
        option_t* curr_opt = (option_t*)current->value;
        if (((((void *)0) == name) || (0 == strcmp(name, curr_opt->name))) &&
            ((((void *)0) == tag) || (0 == strcmp(tag, curr_opt->tag)))) {
            p_opt = curr_opt;
            break;
        }
        current = current->next;
    }
    return p_opt;
}

canonical_original_code:
static struct option_t *find_option(char *name, char *tag) {
    struct option_t *p_opt = ((void *)0);
    struct entry_t *current = Options;
    while (current != ((void *)0)) {
        struct option_t *curr_opt = (option_t*)current->value;
        if (((((void *)0) == name) || (0 == strcmp(name, curr_opt->name))) &&
            ((((void *)0) == tag) || (0 == strcmp(tag, curr_opt->tag)))) {
            p_opt = curr_opt;
            break;
        }
        current = current->next;
    }
    return p_opt;
}

variable_types:
tag
char *

variable_types: struct option_t { char *name; char *tag; char *value; } *

variable_types: struct entry_t { void *value; struct entry_t *next; } *


decompiled_code:
void *__fastcall opts_parse(__int64 a1, __int64 a2, int a3, __int64 a4)
{
void **v4; // r8
__int64 (__fastcall *v5)(_QWORD, _QWORD); // r9
void *result; // rax
__int64 v7; // [rsp+0h] [rbp-38h] BYREF
int v8; // [rsp+8h] [rbp-30h]
__int64 v9; // [rsp+10h] [rbp-28h]
int v10; // [rsp+18h] [rbp-20h]
__int64 v11; // [rsp+20h] [rbp-18h]

v11 = a1;
v7 = 0xFFFFFFFF00000000LL;
v8 = a3 - 1;
v9 = a4 + 8;
opts_next_char((unsigned int *)&v7);
if ( v5 )
Error_Callback = v5;
result = *v4;
Program_Name = (__int64)*v4;
while ( v10 != -1 )
{
while ( 1 )
{
opts_consume_ws((unsigned int *)&v7);
if ( v10 == 45 )
break;
result = opts_parse_argument((unsigned int *)&v7);
if ( v10 == -1 )
return result;
}
if ( (unsigned __int8)opts_next_char((unsigned int *)&v7) == 45 )
{
opts_next_char((unsigned int *)&v7);
result = opts_parse_long_option((__int64)&v7);
}
else
{
result = opts_parse_short_option((__int64)&v7);
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func8(__int64 a1, __int64 a2, int a3, __int64 a4)
{
void **v4; // r8
__int64 (__fastcall *v5)(_QWORD, _QWORD); // r9
void *result; // rax
__int64 v7; // [rsp+0h] [rbp-38h] BYREF
int v8; // [rsp+8h] [rbp-30h]
__int64 v9; // [rsp+10h] [rbp-28h]
int v10; // [rsp+18h] [rbp-20h]
__int64 v11; // [rsp+20h] [rbp-18h]

v11 = a1;
v7 = 0xFFFFFFFF00000000LL;
v8 = a3 - 1;
v9 = a4 + 8;
func0((unsigned int *)&v7);
if ( v5 )
Error_Callback = v5;
result = *v4;
Program_Name = (__int64)*v4;
while ( v10 != -1 )
{
while ( 1 )
{
func1((unsigned int *)&v7);
if ( v10 == 45 )
break;
result = func5((unsigned int *)&v7);
if ( v10 == -1 )
return result;
}
if ( (unsigned __int8)func0((unsigned int *)&v7) == 45 )
{
func0((unsigned int *)&v7);
result = opts_parse_long_option((__int64)&v7);
}
else
{
result = opts_parse_short_option((__int64)&v7);
}
}
return result;
}

original_code:
void opts_parse(opts_cfg_t* opts, opts_err_cbfn_t err_cb, int argc, char** argv) {
    stream_ctx_t ctx;
    ctx.line_idx = 0;
    ctx.col_idx = -1;
    ctx.arg_count = argc-1;
    ctx.arg_vect = &argv[1];
    ctx.options = opts;
    (void)opts_next_char( &ctx );
    if (((void *)0) != err_cb)
        Error_Callback = err_cb;
    Program_Name = argv[0];
    while (ctx.current != (-1)) {
        opts_consume_ws( &ctx );
        if ('-' == ctx.current) {
            if ('-' == opts_next_char( &ctx )) {
                opts_next_char( &ctx );
                opts_parse_long_option( &ctx );
            } else {
                opts_parse_short_option( &ctx );
            }
        } else {
            opts_parse_argument( &ctx );
        }
    }
}

canonical_original_code:
void opts_parse(struct opts_cfg_t *opts, void (*err_cb)(char *msg, char *opt_name), int argc, char **argv) {
    struct stream_ctx_t ctx;
    ctx.line_idx = 0;
    ctx.col_idx = -1;
    ctx.arg_count = argc-1;
    ctx.arg_vect = &argv[1];
    ctx.options = opts;
    (void)opts_next_char( &ctx );
    if (((void *)0) != err_cb)
        Error_Callback = err_cb;
    Program_Name = argv[0];
    while (ctx.current != (-1)) {
        opts_consume_ws( &ctx );
        if ('-' == ctx.current) {
            if ('-' == opts_next_char( &ctx )) {
                opts_next_char( &ctx );
                opts_parse_long_option( &ctx );
            } else {
                opts_parse_short_option( &ctx );
            }
        } else {
            opts_parse_argument( &ctx );
        }
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct stream_ctx_t { unsigned int line_idx; unsigned int col_idx; unsigned int arg_count; char **arg_vect; int current; struct opts_cfg_t { char *name; _Bool has_arg; char *tag; char *desc; } *options; }


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

v0 = (void **)Options;
if ( Options )
{
do
{
v1 = (void **)*v0;
v2 = v0;
v0 = (void **)v0[1];
v3 = *v1;
Options = v0;
free(v3);
free(v1[1]);
v4 = v1[2];
if ( *v1 != v4 )
free(v4);
free(v1);
free(v2);
}
while ( v0 );
}
v5 = (void **)Arguments;
if ( Arguments )
{
do
{
v6 = *v5;
v7 = v5;
v5 = (void **)v5[1];
Arguments = v5;
free(v6);
free(v7);
}
while ( v5 );
}
}

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

v0 = (void **)Options;
if ( Options )
{
do
{
v1 = (void **)*v0;
v2 = v0;
v0 = (void **)v0[1];
v3 = *v1;
Options = v0;
free(v3);
free(v1[1]);
v4 = v1[2];
if ( *v1 != v4 )
free(v4);
free(v1);
free(v2);
}
while ( v0 );
}
v5 = (void **)Arguments;
if ( Arguments )
{
do
{
v6 = *v5;
v7 = v5;
v5 = (void **)v5[1];
Arguments = v5;
free(v6);
free(v7);
}
while ( v5 );
}
}

original_code:
void opts_reset(void) {
    while (Options != ((void *)0)) {
        entry_t* entry = Options;
        option_t* opt = (option_t*)entry->value;
        Options = entry->next;
        free(opt->name);
        free(opt->tag);
        if(opt->name != opt->value)
            free(opt->value);
        free(opt);
        free(entry);
    }
    while (Arguments != ((void *)0)) {
        entry_t* entry = Arguments;
        char* arg = (char*)entry->value;
        Arguments = entry->next;
        free(arg);
        free(entry);
    }
}

canonical_original_code:
void opts_reset() {
    while (Options != ((void *)0)) {
        struct entry_t *entry = Options;
        struct option_t *opt = (option_t*)entry->value;
        Options = entry->next;
        free(opt->name);
        free(opt->tag);
        if(opt->name != opt->value)
            free(opt->value);
        free(opt);
        free(entry);
    }
    while (Arguments != ((void *)0)) {
        struct entry_t *entry = Arguments;
        char *arg = (char*)entry->value;
        Arguments = entry->next;
        free(arg);
        free(entry);
    }
}

variable_types:
arg
char *

user_defined_types: struct entry_t { void *value; struct entry_t *next; }

user_defined_types: struct option_t { char *name; char *tag; char *value; }


decompiled_code:
void __fastcall __noreturn opts_parse_error(const char *a1, char *a2)
{
fprintf(stderr, "Option '%s' : %s\n", a2, a1);
free(a2);
opts_reset();
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func10(const char *a1, char *a2)
{
fprintf(stderr, "Option '%s' : %s\n", a2, a1);
free(a2);
func9();
exit(1);
}

original_code:
static void opts_parse_error(const char* msg, char* opt_name) {
    fprintf(stderr, "Option '%s' : %s\n", opt_name, msg);
    free(opt_name);
    opts_reset();
    exit(1);
}

canonical_original_code:
static void opts_parse_error(char *msg, char *opt_name) {
    fprintf(stderr, "Option '%s' : %s\n", opt_name, msg);
    free(opt_name);
    opts_reset();
    exit(1);
}

variable_types:
msg
char *


decompiled_code:
bool __fastcall opts_is_set(char *a1, char *a2)
{
return find_option(a1, a2) != 0LL;
}

canonical_decompiled_code:
bool __fastcall func11(char *a1, char *a2)
{
return func7(a1, a2) != 0LL;
}

original_code:
_Bool opts_is_set(const char* name, const char* tag) {
    return (((void *)0) != find_option(name,tag));
}

canonical_original_code:
_Bool opts_is_set(char *name, char *tag) {
    return (((void *)0) != find_option(name,tag));
}


decompiled_code:
const char **__fastcall opts_get_value(char *a1, char *a2)
{
const char **result; // rax

result = find_option(a1, a2);
if ( result )
result = (const char **)result[2];
return result;
}

canonical_decompiled_code:
const char **__fastcall func12(char *a1, char *a2)
{
const char **result; // rax

result = func7(a1, a2);
if ( result )
result = (const char **)result[2];
return result;
}

original_code:
const char* opts_get_value(const char* name, const char* tag) {
    option_t* p_opt = find_option(name,tag);
    return (((void *)0) == p_opt) ? ((void *)0) : p_opt->value;
}

canonical_original_code:
char *opts_get_value(char *name, char *tag) {
    struct option_t *p_opt = find_option(name,tag);
    return (((void *)0) == p_opt) ? ((void *)0) : p_opt->value;
}

return_type: char *


decompiled_code:
bool __fastcall opts_equal(char *a1, char *a2, const char *a3)
{
const char **v4; // rax

v4 = opts_get_value(a1, a2);
return strcmp(a3, (const char *)v4) == 0;
}

canonical_decompiled_code:
bool __fastcall func13(char *a1, char *a2, const char *a3)
{
const char **v4; // rax

v4 = func12(a1, a2);
return strcmp(a3, (const char *)v4) == 0;
}

original_code:
_Bool opts_equal(const char* name, const char* tag, const char* value) {
    return (0 == strcmp(value, opts_get_value(name,tag)));
}

canonical_original_code:
_Bool opts_equal(char *name, char *tag, char *value) {
    return (0 == strcmp(value, opts_get_value(name,tag)));
}

variable_types:
value
char *


decompiled_code:
char *opts_arguments()
{
char *v0; // rax
__int64 *v1; // rbp
char *v2; // rdi
size_t v3; // rbx
__int64 v4; // rax

v0 = (char *)malloc(8uLL);
v1 = (__int64 *)Arguments;
*(_QWORD *)v0 = 0LL;
v2 = v0;
if ( v1 )
{
v3 = 16LL;
do
{
v2 = (char *)realloc(v2, v3);
v4 = *v1;
v1 = (__int64 *)v1[1];
*(_QWORD *)&v2[v3 - 8] = 0LL;
*(_QWORD *)&v2[v3 - 16] = v4;
v3 += 8LL;
}
while ( v1 );
}
return v2;
}

original_code:
const char** opts_arguments(void) {
    size_t index = 0;
    const char** ret = (const char**)malloc(sizeof(const char*));
    ret[0] = ((void *)0);
    entry_t* entry = Arguments;
    while (((void *)0) != entry) {
        ret = (const char**)realloc(ret, (index+2)*sizeof(const char*));
        ret[index++] = (const char*)entry->value;
        ret[index] = ((void *)0);
        entry = entry->next;
    }
    return ret;
}

canonical_original_code:
char **opts_arguments() {
    unsigned long index = 0;
    char **ret = (const char**)malloc(sizeof(const char*));
    ret[0] = ((void *)0);
    struct entry_t *entry = Arguments;
    while (((void *)0) != entry) {
        ret = (char **)realloc(ret, (index+2)*sizeof(char *));
        ret[index++] = (char *)entry->value;
        ret[index] = ((void *)0);
        entry = entry->next;
    }
    return ret;
}

variable_types:
index
unsigned long

variable_types:
ret
char **

return_type: char **


decompiled_code:
__int64 opts_prog_name()
{
return Program_Name;
}

canonical_decompiled_code:
__int64 func16()
{
return Program_Name;
}

original_code:
const char* opts_prog_name(void) {
    return Program_Name;
}

canonical_original_code:
char *opts_prog_name() {
    return Program_Name;
}


decompiled_code:
__int64 __fastcall atf_run_suite(__int64 (*a1)(void))
{
return a1();
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 (*a1)(void))
{
return a1();
}

original_code:
void atf_run_suite(suite_t suite) {
    suite();
}

canonical_original_code:
void atf_run_suite(void (*suite)()) {
    suite();
}

variable_types:
suite
void (*)()


decompiled_code:
void __fastcall atf_test_start(__int64 a1)
{
++Total;
Curr_Test = a1;
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
++Total;
Curr_Test = a1;
}

original_code:
void atf_test_start(char* p_test_name) {
    Curr_Test = p_test_name;
    Total++;
}

canonical_original_code:
void atf_test_start(char *p_test_name) {
    Curr_Test = p_test_name;
    Total++;
}


decompiled_code:
int __fastcall atf_test_fail(const char *a1, const char *a2, __int64 a3)
{
++Failed;
return printf("%s:%d:0:%s:FAIL\n\t%s\n", a2, a3, (const char *)Curr_Test, a1);
}

canonical_decompiled_code:
int __fastcall func2(const char *a1, const char *a2, __int64 a3)
{
++Failed;
return printf("%s:%d:0:%s:FAIL\n\t%s\n", a2, a3, (const char *)Curr_Test, a1);
}

original_code:
void atf_test_fail(char* expr, char* file, int line) {
    Failed++;
    printf("%s:%d:0:%s:FAIL\n\t%s\n", file, line, Curr_Test, expr); }

canonical_original_code:
void atf_test_fail(char *expr, char *file, int line) {
    Failed++;
    printf("%s:%d:0:%s:FAIL\n\t%s\n", file, line, Curr_Test, expr); }

variable_types:
expr
char *

variable_types:
file
char *

variable_types: line int


decompiled_code:
__int64 atf_print_results()
{
printf(
"\nUnit Test Summary\n-----------------\nTotal: %d\nPassed: %d\nFailed: %d\n\n",
(unsigned int)Total,
(unsigned int)(Total - Failed),
(unsigned int)Failed);
return (unsigned int)Failed;
}

canonical_decompiled_code:
__int64 func3()
{
printf(
"\nUnit Test Summary\n-----------------\nTotal: %d\nPassed: %d\nFailed: %d\n\n",
(unsigned int)Total,
(unsigned int)(Total - Failed),
(unsigned int)Failed);
return (unsigned int)Failed;
}

original_code:
int atf_print_results(void) {
    static const char* results_string =
    "\nUnit Test Summary"
    "\n-----------------"
    "\nTotal:  %d"
    "\nPassed: %d"
    "\nFailed: %d"
    "\n\n";
    printf(results_string, Total, Total - Failed, Failed);
    return Failed;
}

canonical_original_code:
int atf_print_results() {
    static char *results_string = "\nUnit Test Summary"
    "\n-----------------"
    "\nTotal:  %d"
    "\nPassed: %d"
    "\nFailed: %d"
    "\n\n";
    printf(results_string, Total, Total - Failed, Failed);
    return Failed;
}


repo: mikelward/root

decompiled_code:
struct group *__fastcall get_group_name(__gid_t a1)
{
struct group *result; // rax

result = getgrgid(a1);
if ( result )
result = (struct group *)result->gr_name;
return result;
}

canonical_decompiled_code:
struct group *__fastcall func0(__gid_t a1)
{
struct group *result; // rax

result = getgrgid(a1);
if ( result )
result = (struct group *)result->gr_name;
return result;
}

original_code:
char *get_group_name(gid_t gid)
{
    struct group *gp = getgrgid(gid);
    if (gp != ((void *)0) && gp->gr_name != ((void *)0)) {
        return gp->gr_name;
    }
    else {
        return ((void *)0);
    }
}

canonical_original_code:
char *get_group_name(unsigned int gid)
{
    struct group *gp = getgrgid(gid);
    if (gp != ((void *)0) && gp->gr_name != ((void *)0)) {
        return gp->gr_name;
    }
    else {
        return ((void *)0);
    }
}

variable_types:
gid
unsigned int

variable_types: struct group { char *gr_name; char *gr_passwd; unsigned int gr_gid; char **gr_mem; } *


decompiled_code:
__int64 __fastcall in_group(int a1)
{
int *v1; // rax
int *v2; // r13
__int64 v3; // rax
void *v4; // rsp
int v5; // eax
int v6; // edx
__gid_t *v7; // rax
__gid_t *v8; // rdx
int v10; // eax
const char *v11; // rdx
int v12; // eax
const char *v13; // rdx
__gid_t v14[8]; // [rsp+0h] [rbp-20h] BYREF

if ( getgid() == a1 )
return 1LL;
v1 = _errno_location();
*v1 = 0;
v2 = v1;
v3 = sysconf(3);
if ( v3 == -1 )
{
v10 = (unsigned int)strerror(*v2);
error((int)"Cannot determine maximum number of groups: %s", v10, v11);
exit(124);
}
*v2 = 0;
v4 = alloca(4 * v3);
v5 = getgroups(v3, v14);
v6 = v5;
if ( v5 == -1 )
{
v12 = (unsigned int)strerror(*v2);
error((int)"Cannot get group list: %s", v12, v13);
exit(124);
}
if ( v5 <= 0 )
return 0LL;
v7 = v14;
v8 = &v14[v6 - 1 + 1];
while ( *v7 != a1 )
{
if ( ++v7 == v8 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int *v1; // rax
int *v2; // r13
__int64 v3; // rax
void *v4; // rsp
int v5; // eax
int v6; // edx
__gid_t *v7; // rax
__gid_t *v8; // rdx
int v10; // eax
const char *v11; // rdx
int v12; // eax
const char *v13; // rdx
__gid_t v14[8]; // [rsp+0h] [rbp-20h] BYREF

if ( getgid() == a1 )
return 1LL;
v1 = _errno_location();
*v1 = 0;
v2 = v1;
v3 = sysconf(3);
if ( v3 == -1 )
{
v10 = (unsigned int)strerror(*v2);
error((int)"Cannot determine maximum number of groups: %s", v10, v11);
exit(124);
}
*v2 = 0;
v4 = alloca(4 * v3);
v5 = getgroups(v3, v14);
v6 = v5;
if ( v5 == -1 )
{
v12 = (unsigned int)strerror(*v2);
error((int)"Cannot get group list: %s", v12, v13);
exit(124);
}
if ( v5 <= 0 )
return 0LL;
v7 = v14;
v8 = &v14[v6 - 1 + 1];
while ( *v7 != a1 )
{
if ( ++v7 == v8 )
return 0LL;
}
return 1LL;
}

original_code:
int in_group(gid_t root_gid)
{
    gid_t gid;
    gid = getgid();
    if (gid == root_gid) {
        return 1;
    }
    else {
        long ngroups_max;
        (*__errno_location ()) = 0;
        ngroups_max = sysconf(_SC_NGROUPS_MAX);
        if (ngroups_max == -1) {
            error("Cannot determine maximum number of groups: %s",
                  strerror((*__errno_location ())));
            exit(124);
        }
        else {
            int ngroups;
            gid_t grouplist[ngroups_max];
            (*__errno_location ()) = 0;
            ngroups = getgroups(ngroups_max, grouplist);
            if (ngroups == -1) {
                error("Cannot get group list: %s", strerror((*__errno_location ())));
                exit(124);
            }
            for (int i = 0; i < ngroups; i++) {
                if (grouplist[i] == root_gid) {
                    return 1;
                }
            }
            return 0;
        }
    }
}

canonical_original_code:
int in_group(unsigned int root_gid)
{
    unsigned int gid;
    gid = getgid();
    if (gid == root_gid) {
        return 1;
    }
    else {
        long ngroups_max;
        (*__errno_location ()) = 0;
        ngroups_max = sysconf(_SC_NGROUPS_MAX);
        if (ngroups_max == -1) {
            error("Cannot determine maximum number of groups: %s",
                  strerror((*__errno_location ())));
            exit(124);
        }
        else {
            int ngroups;
            unsigned int grouplist[-1];
            (*__errno_location ()) = 0;
            ngroups = getgroups(ngroups_max, grouplist);
            if (ngroups == -1) {
                error("Cannot get group list: %s", strerror((*__errno_location ())));
                exit(124);
            }
            for (int i = 0; i < ngroups; i++) {
                if (grouplist[i] == root_gid) {
                    return 1;
                }
            }
            return 0;
        }
    }
}

variable_types: ngroups int

variable_types:
grouplist
unsigned int [-1]

variable_types: i int


decompiled_code:
__int64 __fastcall setup_groups(__uid_t uid)
{
int *v1; // rax
int *v2; // rbp
struct passwd *v3; // rax
struct passwd *v4; // rbx
char *v6; // rax
char *v7; // rax
char *v8; // rax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
v3 = getpwuid(uid);
if ( !v3 )
{
v6 = strerror(*v2);
error((int)"Cannot get passwd info for uid %d: %s", uid, v6);
exit(124);
}
*v2 = 0;
v4 = v3;
if ( setgid(v3->pw_gid) == -1 )
{
v8 = strerror(*v2);
error((int)"Cannot setgid %d: %s", v4->pw_gid, v8);
exit(124);
}
*v2 = 0;
if ( initgroups(v4->pw_name, v4->pw_gid) == -1 )
{
v7 = strerror(*v2);
error((int)"Cannot initgroups for %s: %s", (int)v4->pw_name, v7);
exit(124);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__uid_t uid)
{
int *v1; // rax
int *v2; // rbp
struct passwd *v3; // rax
struct passwd *v4; // rbx
char *v6; // rax
char *v7; // rax
char *v8; // rax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
v3 = getpwuid(uid);
if ( !v3 )
{
v6 = strerror(*v2);
error((int)"Cannot get passwd info for uid %d: %s", uid, v6);
exit(124);
}
*v2 = 0;
v4 = v3;
if ( setgid(v3->pw_gid) == -1 )
{
v8 = strerror(*v2);
error((int)"Cannot setgid %d: %s", v4->pw_gid, v8);
exit(124);
}
*v2 = 0;
if ( initgroups(v4->pw_name, v4->pw_gid) == -1 )
{
v7 = strerror(*v2);
error((int)"Cannot initgroups for %s: %s", (int)v4->pw_name, v7);
exit(124);
}
return 0LL;
}

original_code:
int setup_groups(uid_t uid)
{
    struct passwd *ps;
    int result;
    (*__errno_location ()) = 0;
    ps = getpwuid(uid);
    if (ps == ((void *)0)) {
        error("Cannot get passwd info for uid %d: %s", uid, strerror((*__errno_location ())));
        exit(124);
    }
    (*__errno_location ()) = 0;
    result = setgid(ps->pw_gid);
    if (result == -1) {
        error("Cannot setgid %d: %s", ps->pw_gid, strerror((*__errno_location ())));
        exit(124);
    }
    (*__errno_location ()) = 0;
    result = initgroups(ps->pw_name, ps->pw_gid);
    if (result == -1) {
        error("Cannot initgroups for %s: %s", ps->pw_name, strerror((*__errno_location ())));
        exit(124);
    }
    else {
        return 0;
    }
}

canonical_original_code:
int setup_groups(unsigned int uid)
{
    struct passwd *ps;
    int result;
    (*__errno_location ()) = 0;
    ps = getpwuid(uid);
    if (ps == ((void *)0)) {
        error("Cannot get passwd info for uid %d: %s", uid, strerror((*__errno_location ())));
        exit(124);
    }
    (*__errno_location ()) = 0;
    result = setgid(ps->pw_gid);
    if (result == -1) {
        error("Cannot setgid %d: %s", ps->pw_gid, strerror((*__errno_location ())));
        exit(124);
    }
    (*__errno_location ()) = 0;
    result = initgroups(ps->pw_name, ps->pw_gid);
    if (result == -1) {
        error("Cannot initgroups for %s: %s", ps->pw_name, strerror((*__errno_location ())));
        exit(124);
    }
    else {
        return 0;
    }
}

variable_types:
uid
unsigned int

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

variable_types: result int


decompiled_code:
_BOOL8 __fastcall set_home_dir(__uid_t uid)
{
int *v1; // rax
int *v2; // rbx
struct passwd *v3; // rax
char *v5; // rax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
v3 = getpwuid(uid);
if ( !v3 )
{
v5 = strerror(*v2);
error((int)"Cannot get passwd info for uid %d: %s", uid, v5);
exit(124);
}
return setenv("HOME", v3->pw_dir, 1) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__uid_t uid)
{
int *v1; // rax
int *v2; // rbx
struct passwd *v3; // rax
char *v5; // rax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
v3 = getpwuid(uid);
if ( !v3 )
{
v5 = strerror(*v2);
error((int)"Cannot get passwd info for uid %d: %s", uid, v5);
exit(124);
}
return setenv("HOME", v3->pw_dir, 1) == 0;
}

original_code:
int set_home_dir(uid_t uid)
{
    struct passwd *ps;
    (*__errno_location ()) = 0;
    ps = getpwuid(uid);
    if (ps == ((void *)0)) {
        error("Cannot get passwd info for uid %d: %s", uid, strerror((*__errno_location ())));
        exit(124);
    }
    return setenv("HOME", ps->pw_dir, 1) == 0;
}

canonical_original_code:
int set_home_dir(unsigned int uid)
{
    struct passwd *ps;
    (*__errno_location ()) = 0;
    ps = getpwuid(uid);
    if (ps == ((void *)0)) {
        error("Cannot get passwd info for uid %d: %s", uid, strerror((*__errno_location ())));
        exit(124);
    }
    return setenv("HOME", ps->pw_dir, 1) == 0;
}

user_defined_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; }


decompiled_code:
__int64 __fastcall become_user(int a1, int a2, const char *a3)
{
int *v3; // rax
int *v4; // rbx
int v5; // er8
__int64 result; // rax
char *v7; // rax

if ( a1 )
{
error((int)"Becoming non-root user has not been tested", a2, a3);
result = 0LL;
}
else
{
v3 = _errno_location();
*v3 = 0;
v4 = v3;
v5 = setuid(0);
result = 1LL;
if ( v5 == -1 )
{
v7 = strerror(*v4);
error((int)"Cannot setuid %u: %s", 0, v7);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, int a2, const char *a3)
{
int *v3; // rax
int *v4; // rbx
int v5; // er8
__int64 result; // rax
char *v7; // rax

if ( a1 )
{
error((int)"Becoming non-root user has not been tested", a2, a3);
result = 0LL;
}
else
{
v3 = _errno_location();
*v3 = 0;
v4 = v3;
v5 = setuid(0);
result = 1LL;
if ( v5 == -1 )
{
v7 = strerror(*v4);
error((int)"Cannot setuid %u: %s", 0, v7);
result = 0LL;
}
}
return result;
}

original_code:
int become_user(uid_t uid)
{
    if (uid != 0) {
        error("Becoming non-root user has not been tested");
        return 0;
    }
    (*__errno_location ()) = 0;
    if (setuid(0) == -1) {
        error("Cannot setuid %u: %s", (unsigned)0, strerror((*__errno_location ())));
        return 0;
    }
    return 1;
}

canonical_original_code:
int become_user(unsigned int uid)
{
    if (uid != 0) {
        error("Becoming non-root user has not been tested");
        return 0;
    }
    (*__errno_location ()) = 0;
    if (setuid(0) == -1) {
        error("Cannot setuid %u: %s", (unsigned int)0, strerror((*__errno_location ())));
        return 0;
    }
    return 1;
}


decompiled_code:
char *__fastcall get_command_path(char *src, char *s)
{
size_t v2; // r12
char *v3; // rax
const char *v4; // rdx
char *v5; // r14
size_t v6; // r12
char *v7; // r15
size_t v8; // rbx
char *v9; // rax
const char *v10; // rdx
char *v11; // rbp
char sa[58]; // [rsp+Eh] [rbp-3Ah] BYREF

v2 = strlen(src);
v3 = strdup(s);
if ( v3 )
{
v5 = v3;
v6 = v2 + 1;
v7 = strtok(v3, ":");
if ( v7 )
{
while ( 1 )
{
v8 = strlen(v7);
v9 = (char *)malloc(v6 + (int)v8 + 1);
v11 = v9;
if ( !v9 )
break;
memcpy(v9, v7, v8 + 1);
if ( *v11 && v11[(int)v8 - 1] != 47 )
{
sprintf(sa, "%c", 47LL);
strcat(v11, sa);
}
strcat(v11, src);
if ( !access(v11, 0) )
{
free(v5);
return v11;
}
free(v11);
v7 = strtok(0LL, ":");
if ( !v7 )
goto LABEL_11;
}
error((int)"Cannot allocate memory to hold path", (int)":", v10);
}
else
{
LABEL_11:
v11 = 0LL;
debug("%s not found in PATH", src);
free(v5);
}
}
else
{
v11 = 0LL;
error((int)"Cannot allocate memory to hold pathenv\n", (int)s, v4);
}
return v11;
}

canonical_decompiled_code:
char *__fastcall func0(char *src, char *s)
{
size_t v2; // r12
char *v3; // rax
const char *v4; // rdx
char *v5; // r14
size_t v6; // r12
char *v7; // r15
size_t v8; // rbx
char *v9; // rax
const char *v10; // rdx
char *v11; // rbp
char sa[58]; // [rsp+Eh] [rbp-3Ah] BYREF

v2 = strlen(src);
v3 = strdup(s);
if ( v3 )
{
v5 = v3;
v6 = v2 + 1;
v7 = strtok(v3, ":");
if ( v7 )
{
while ( 1 )
{
v8 = strlen(v7);
v9 = (char *)malloc(v6 + (int)v8 + 1);
v11 = v9;
if ( !v9 )
break;
memcpy(v9, v7, v8 + 1);
if ( *v11 && v11[(int)v8 - 1] != 47 )
{
sprintf(sa, "%c", 47LL);
strcat(v11, sa);
}
strcat(v11, src);
if ( !access(v11, 0) )
{
free(v5);
return v11;
}
free(v11);
v7 = strtok(0LL, ":");
if ( !v7 )
goto LABEL_11;
}
error((int)"Cannot allocate memory to hold path", (int)":", v10);
}
else
{
LABEL_11:
v11 = 0LL;
debug("%s not found in PATH", src);
free(v5);
}
}
else
{
v11 = 0LL;
error((int)"Cannot allocate memory to hold pathenv\n", (int)s, v4);
}
return v11;
}

original_code:
char *get_command_path(const char *command, const char *pathenv)
{
    size_t commandlen = strlen(command);
    char *pathenvcopy = strdup(pathenv);
    if (pathenvcopy == ((void *)0)) {
        error("Cannot allocate memory to hold pathenv\n");
        return ((void *)0);
    }
    for (char *dir = strtok(pathenvcopy, ":");
         dir != ((void *)0);
         dir = strtok(((void *)0), ":")) {
        int dirlen = strlen(dir);
        char *path = malloc(dirlen+1+commandlen+1);
        if (path == ((void *)0)) {
            error("Cannot allocate memory to hold path");
            return ((void *)0);
        }
        strcpy(path, dir);
        if (strcmp(path, "") != 0 && path[dirlen-1] != '/') {
            char dirsepstr[2];
            sprintf(dirsepstr, "%c", '/');
            strcat(path, dirsepstr);
            dirlen++;
        }
        strcat(path, command);
        if (access(path, 0) == 0) {
            free(pathenvcopy);
            return path;
        }
        else {
            free(path);
            path = ((void *)0);
        }
    }
    debug("%s not found in PATH", command);
    if (pathenvcopy != ((void *)0)) {
        free(pathenvcopy);
    }
    return ((void *)0);
}

canonical_original_code:
char *get_command_path(char *command, char *pathenv)
{
    unsigned long commandlen = strlen(command);
    char *pathenvcopy = strdup(pathenv);
    if (pathenvcopy == ((void *)0)) {
        error("Cannot allocate memory to hold pathenv\n");
        return ((void *)0);
    }
    for (char *dir = strtok(pathenvcopy, ":");
         dir != ((void *)0);
         dir = strtok(((void *)0), ":")) {
        int dirlen = strlen(dir);
        char *path = malloc(dirlen+1+commandlen+1);
        if (path == ((void *)0)) {
            error("Cannot allocate memory to hold path");
            return ((void *)0);
        }
        strcpy(path, dir);
        if (strcmp(path, "") != 0 && path[dirlen-1] != '/') {
            char dirsepstr[2];
            sprintf(dirsepstr, "%c", '/');
            strcat(path, dirsepstr);
            dirlen++;
        }
        strcat(path, command);
        if (access(path, 0) == 0) {
            free(pathenvcopy);
            return path;
        }
        else {
            free(path);
            path = ((void *)0);
        }
    }
    debug("%s not found in PATH", command);
    if (pathenvcopy != ((void *)0)) {
        free(pathenvcopy);
    }
    return ((void *)0);
}

variable_types:
command
char *

variable_types:
pathenv
char *

variable_types:
commandlen
unsigned long

variable_types:
pathenvcopy
char *

variable_types:
dir
char *

variable_types: dirlen int

variable_types:
path
char *

variable_types:
dirsepstr
char [2]


decompiled_code:
_BOOL8 __fastcall is_qualified_path(const char *a1)
{
_BOOL8 result; // rax

if ( a1 )
result = strchr(a1, 47) != 0LL;
else
result = 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(const char *a1)
{
_BOOL8 result; // rax

if ( a1 )
result = strchr(a1, 47) != 0LL;
else
result = 0LL;
return result;
}

original_code:
int is_qualified_path(const char *path)
{
    return path && strchr(path, '/') != ((void *)0);
}

canonical_original_code:
int is_qualified_path(char *path)
{
    return path && strchr(path, '/') != ((void *)0);
}


decompiled_code:
_BOOL8 __fastcall is_unqualified_path(const char *a1)
{
return !is_qualified_path(a1);
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(const char *a1)
{
return !func1(a1);
}

original_code:
int is_unqualified_path(const char *path)
{
    return !is_qualified_path(path);
}

canonical_original_code:
int is_unqualified_path(char *path)
{
    return !is_qualified_path(path);
}


decompiled_code:
_BOOL8 __fastcall is_absolute_path(_BYTE *a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 )
result = *a1 == 47;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(_BYTE *a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 )
result = *a1 == 47;
return result;
}

original_code:
int is_absolute_path(const char *path)
{
    return path && path[0] == '/';
}

canonical_original_code:
int is_absolute_path(char *path)
{
    return path && path[0] == '/';
}


decompiled_code:
void __fastcall pathenv_each(const char *a1, void (__fastcall *a2)(char *), const char *a3)
{
char *v3; // rax
char *i; // rdi

if ( a1 )
{
v3 = strdup(a1);
for ( i = strtok(v3, ":"); i; i = strtok(0LL, ":") )
a2(i);
}
else
{
error((int)"pathenv_each: pathenv is NULL\n", (int)a2, a3);
}
}

canonical_decompiled_code:
void __fastcall func4(const char *a1, void (__fastcall *a2)(char *), const char *a3)
{
char *v3; // rax
char *i; // rdi

if ( a1 )
{
v3 = strdup(a1);
for ( i = strtok(v3, ":"); i; i = strtok(0LL, ":") )
a2(i);
}
else
{
error((int)"pathenv_each: pathenv is NULL\n", (int)a2, a3);
}
}

original_code:
void pathenv_each(const char *pathenv, void (*func)(const char *pathentry))
{
    if (pathenv == ((void *)0)) {
        error("pathenv_each: pathenv is NULL\n");
        return;
    }
    char *pathenvcopy = strdup(pathenv);
    for (char *dir = strtok(pathenvcopy, ":");
         dir != ((void *)0);
         dir = strtok(((void *)0), ":")) {
        (*func)(dir);
    }
}

canonical_original_code:
void pathenv_each(char *pathenv, void (*func)(char *pathentry))
{
    if (pathenv == ((void *)0)) {
        error("pathenv_each: pathenv is NULL\n");
        return;
    }
    char *pathenvcopy = strdup(pathenv);
    for (char *dir = strtok(pathenvcopy, ":");
         dir != ((void *)0);
         dir = strtok(((void *)0), ":")) {
        (*func)(dir);
    }
}

variable_types:
func
void (*)(char *pathentry)


decompiled_code:
bool testescape1()
{
bool v0; // zf
bool result; // al

printf("Running %s\n", "testescape1");
v0 = strcmp((const char *)escape_percents("mikel"), "mikel") == 0;
result = !v0;
if ( !v0 )
__assert_fail("strcmp(actual, expected) == 0", "loggingtest.c", 0x1Eu, "testescape1");
return result;
}

canonical_decompiled_code:
bool func7()
{
bool v0; // zf
bool result; // al

printf("Running %s\n", "testescape1");
v0 = strcmp((const char *)escape_percents("mikel"), "mikel") == 0;
result = !v0;
if ( !v0 )
__assert_fail("strcmp(actual, expected) == 0", "loggingtest.c", 0x1Eu, "testescape1");
return result;
}

original_code:
void testescape1(void)
{
    char *input = "mikel";
    char *expected = "mikel";
    char *actual;
    printf("Running %s\n", __func__);
    actual = escape_percents(input);
    ((void) sizeof ((strcmp(actual, expected) == 0) ? 1 : 0), __extension__ ({ if (strcmp(actual, expected) == 0) ; else __assert_fail ("strcmp(actual, expected) == 0", "loggingtest.c", 30, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void testescape1()
{
    char *input = "mikel";
    char *expected = "mikel";
    char *actual;
    printf("Running %s\n", __func__);
    actual = escape_percents(input);
    ((void) sizeof ((strcmp(actual, expected) == 0) ? 1 : 0), __extension__ ({ if (strcmp(actual, expected) == 0) ; else __assert_fail ("strcmp(actual, expected) == 0", "loggingtest.c", 30, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types:
input
char *

variable_types:
expected
char *

variable_types:
actual
char *


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

printf("Running %s\n", "testescape2");
result = escape_percents(0LL);
if ( result )
__assert_fail("actual == NULL", "loggingtest.c", 0x29u, "testescape2");
return result;
}

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

printf("Running %s\n", "testescape2");
result = escape_percents(0LL);
if ( result )
__assert_fail("actual == NULL", "loggingtest.c", 0x29u, "testescape2");
return result;
}

original_code:
void testescape2(void)
{
    char *input = ((void *)0);
    char *actual;
    printf("Running %s\n", __func__);
    actual = escape_percents(input);
    ((void) sizeof ((actual == ((void *)0)) ? 1 : 0), __extension__ ({ if (actual == ((void *)0)) ; else __assert_fail ("actual == NULL", "loggingtest.c", 41, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void testescape2()
{
    char *input = ((void *)0);
    char *actual;
    printf("Running %s\n", __func__);
    actual = escape_percents(input);
    ((void) sizeof ((actual == ((void *)0)) ? 1 : 0), __extension__ ({ if (actual == ((void *)0)) ; else __assert_fail ("actual == NULL", "loggingtest.c", 41, __extension__ __PRETTY_FUNCTION__); }));
}


decompiled_code:
bool testescape3()
{
bool v0; // zf
bool result; // al

printf("Running %s\n", "testescape3");
v0 = strcmp((const char *)escape_percents("%sally", "testescape3"), "%%sally") == 0;
result = !v0;
if ( !v0 )
__assert_fail("strcmp(actual, expected) == 0", "loggingtest.c", 0x35u, "testescape3");
return result;
}

canonical_decompiled_code:
bool func9()
{
bool v0; // zf
bool result; // al

printf("Running %s\n", "testescape3");
v0 = strcmp((const char *)escape_percents("%sally", "testescape3"), "%%sally") == 0;
result = !v0;
if ( !v0 )
__assert_fail("strcmp(actual, expected) == 0", "loggingtest.c", 0x35u, "testescape3");
return result;
}

original_code:
void testescape3(void)
{
    char *input = "%sally";
    char *expected = "%%sally";
    char *actual;
    printf("Running %s\n", __func__);
    actual = escape_percents(input);
    ((void) sizeof ((strcmp(actual, expected) == 0) ? 1 : 0), __extension__ ({ if (strcmp(actual, expected) == 0) ; else __assert_fail ("strcmp(actual, expected) == 0", "loggingtest.c", 53, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void testescape3()
{
    char *input = "%sally";
    char *expected = "%%sally";
    char *actual;
    printf("Running %s\n", __func__);
    actual = escape_percents(input);
    ((void) sizeof ((strcmp(actual, expected) == 0) ? 1 : 0), __extension__ ({ if (strcmp(actual, expected) == 0) ; else __assert_fail ("strcmp(actual, expected) == 0", "loggingtest.c", 53, __extension__ __PRETTY_FUNCTION__); }));
}


initlog

decompiled_code:
char *__fastcall initlog(char *s)
{
char *result; // rax

openlog(s, 3, 80);
result = strdup(s);
g_progname = (__int64)result;
return result;
}

canonical_decompiled_code:
char *__fastcall func10(char *s)
{
char *result; // rax

openlog(s, 3, 80);
result = strdup(s);
g_progname = (__int64)result;
return result;
}

original_code:
void initlog(const char *name)
{
    openlog(name, 0x02|0x01, (10<<3));
    g_progname = strdup(name);
}

canonical_original_code:
void initlog(char *name)
{
    openlog(name, 0x02|0x01, (10<<3));
    g_progname = strdup(name);
}

variable_types:
name
char *


makeformat

decompiled_code:
char *__fastcall makeformat(const char *a1, const char *a2, const char *a3)
{
size_t v4; // r15
size_t v5; // r12
size_t v6; // rbx
char *v7; // rax
char *v8; // r12

v4 = strlen(a1);
v5 = strlen(a2);
v6 = strlen(a3) + v4 + v5 + 3;
v7 = (char *)malloc(v6);
v8 = v7;
if ( v7 && v6 <= snprintf(v7, v6, "%s: %s%s", a1, a2, a3) )
{
fwrite("root: Unable to make log format\n", 1uLL, 0x20uLL, stderr);
syslog(2, "root: Unable to make log format");
exit(1);
}
return v8;
}

canonical_decompiled_code:
char *__fastcall func11(const char *a1, const char *a2, const char *a3)
{
size_t v4; // r15
size_t v5; // r12
size_t v6; // rbx
char *v7; // rax
char *v8; // r12

v4 = strlen(a1);
v5 = strlen(a2);
v6 = strlen(a3) + v4 + v5 + 3;
v7 = (char *)malloc(v6);
v8 = v7;
if ( v7 && v6 <= snprintf(v7, v6, "%s: %s%s", a1, a2, a3) )
{
fwrite("root: Unable to make log format\n", 1uLL, 0x20uLL, stderr);
syslog(2, "root: Unable to make log format");
exit(1);
}
return v8;
}

original_code:
char *makeformat(const char *tag, const char *format, const char *suffix)
{
    char *fmt = ((void *)0); size_t fmtmax, fmtlen;
    fmtmax = strlen(tag) + strlen(": ") + strlen(format) + strlen(suffix) + 1;
    fmt = malloc(fmtmax * sizeof *format);
    if (fmt == ((void *)0)) {
        return ((void *)0);
    }
    fmtlen = snprintf(fmt, fmtmax, "%s: %s%s", tag, format, suffix);
    if (fmtlen >= fmtmax) {
        fprintf(stderr, "root: Unable to make log format\n");
        syslog(2, "root: Unable to make log format");
        exit(1);
    }
    return fmt;
}

canonical_original_code:
char *makeformat(char *tag, char *format, char *suffix)
{
    char *fmt = ((void *)0); unsigned long fmtmax; unsigned long fmtlen;
    fmtmax = strlen(tag) + strlen(": ") + strlen(format) + strlen(suffix) + 1;
    fmt = malloc(fmtmax * sizeof *format);
    if (fmt == ((void *)0)) {
        return ((void *)0);
    }
    fmtlen = snprintf(fmt, fmtmax, "%s: %s%s", tag, format, suffix);
    if (fmtlen >= fmtmax) {
        fprintf(stderr, "root: Unable to make log format\n");
        syslog(2, "root: Unable to make log format");
        exit(1);
    }
    return fmt;
}

variable_types:
tag
char *

variable_types:
format
char *

variable_types:
suffix
char *

variable_types:
fmt
char *

variable_types:
fmtmax
unsigned long

variable_types:
fmtlen
unsigned long


print

decompiled_code:
int print(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func12(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stderr, format, arg);
}

original_code:
void print(const char *format, ...)
{
    va_list ap;
    __builtin_va_start(ap,format);
    vfprintf(stderr, format, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void print(char *format, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    vfprintf(stderr, format, ap);
    __builtin_va_end(ap);
}


decompiled_code:
const char *__fastcall get_username(__uid_t a1)
{
struct passwd *v1; // rax
const char *result; // rax

v1 = getpwuid(a1);
if ( v1 )
result = v1->pw_name;
else
result = "Unknown user";
return result;
}

canonical_decompiled_code:
const char *__fastcall func13(__uid_t a1)
{
struct passwd *v1; // rax
const char *result; // rax

v1 = getpwuid(a1);
if ( v1 )
result = v1->pw_name;
else
result = "Unknown user";
return result;
}

original_code:
char *get_username(uid_t uid)
{
    struct passwd *ppw = getpwuid(uid);
    if (ppw == ((void *)0)) {
        return "Unknown user";
    }
    return ppw->pw_name;
}

canonical_original_code:
char *get_username(unsigned int uid)
{
    struct passwd *ppw = getpwuid(uid);
    if (ppw == ((void *)0)) {
        return "Unknown user";
    }
    return ppw->pw_name;
}

variable_types:
uid
unsigned int

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *


writelog

decompiled_code:
void __fastcall writelog(int pri, const char *a2, void *a3)
{
__uid_t v4; // eax
const char *v5; // rax
char *v6; // r12
char *v7; // rbp

v4 = getuid();
v5 = get_username(v4);
v6 = escape_percents(v5);
v7 = makeformat(v6, a2, "");
vsyslog(pri, v7, a3);
free(v6);
free(v7);
}

canonical_decompiled_code:
void __fastcall func14(int pri, const char *a2, void *a3)
{
__uid_t v4; // eax
const char *v5; // rax
char *v6; // r12
char *v7; // rbp

v4 = getuid();
v5 = func13(v4);
v6 = escape_percents(v5);
v7 = func11(v6, a2, "");
vsyslog(pri, v7, a3);
free(v6);
free(v7);
}

original_code:
void writelog(int priority, const char *format, va_list ap)
{
    char *logformat = ((void *)0);
    char *escapedusername = ((void *)0);
    uid_t ruid;
    ruid = getuid();
    escapedusername = escape_percents(get_username(ruid));
    logformat = makeformat(escapedusername, format, "");
    vsyslog(priority, logformat, ap);
    free(escapedusername);
    free(logformat);
}

canonical_original_code:
void writelog(int priority, char *format, __builtin_va_list ap)
{
    char *logformat = ((void *)0);
    char *escapedusername = ((void *)0);
    unsigned int ruid;
    ruid = getuid();
    escapedusername = escape_percents(get_username(ruid));
    logformat = makeformat(escapedusername, format, "");
    vsyslog(priority, logformat, ap);
    free(escapedusername);
    free(logformat);
}

variable_types: priority int

variable_types:
logformat
char *

variable_types:
escapedusername
char *

variable_types:
ruid
unsigned int


writescreen

decompiled_code:
void __fastcall writescreen(int a1, const char *a2, void *a3)
{
char *v4; // r13
char *v5; // rbp

if ( loglevel >= a1 )
{
v4 = escape_percents((const char *)g_progname);
v5 = makeformat(v4, a2, "\n");
vfprintf(stderr, v5, a3);
free(v4);
free(v5);
}
}

canonical_decompiled_code:
void __fastcall func15(int a1, const char *a2, void *a3)
{
char *v4; // r13
char *v5; // rbp

if ( loglevel >= a1 )
{
v4 = escape_percents((const char *)g_progname);
v5 = func11(v4, a2, "\n");
vfprintf(stderr, v5, a3);
free(v4);
free(v5);
}
}

original_code:
void writescreen(int priority, const char *format, va_list ap)
{
    char *screenformat = ((void *)0);
    char *escapedprogname = ((void *)0);
    if (priority > loglevel) {
        return;
    }
    escapedprogname = escape_percents(g_progname);
    screenformat = makeformat(escapedprogname, format, "\n");
    vfprintf(stderr, screenformat, ap);
    free(escapedprogname);
    free(screenformat);
}

canonical_original_code:
void writescreen(int priority, char *format, __builtin_va_list ap)
{
    char *screenformat = ((void *)0);
    char *escapedprogname = ((void *)0);
    if (priority > loglevel) {
        return;
    }
    escapedprogname = escape_percents(g_progname);
    screenformat = makeformat(escapedprogname, format, "\n");
    vfprintf(stderr, screenformat, ap);
    free(escapedprogname);
    free(screenformat);
}

variable_types:
screenformat
char *

variable_types:
escapedprogname
char *


debug

decompiled_code:
void debug(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
writelog(7, a1, va);
va_end(va);
va_start(va, a1);
writescreen(7, a1, va);
}

canonical_decompiled_code:
void func16(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func14(7, a1, va);
va_end(va);
va_start(va, a1);
func15(7, a1, va);
}

original_code:
void debug(const char *format, ...)
{
    va_list ap;
    __builtin_va_start(ap,format);
    writelog(7, format, ap);
    __builtin_va_end(ap);
    __builtin_va_start(ap,format);
    writescreen(7, format, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void debug(char *format, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    writelog(7, format, ap);
    __builtin_va_end(ap);
    __builtin_va_start(ap,format);
    writescreen(7, format, ap);
    __builtin_va_end(ap);
}


error

decompiled_code:
void error(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
writelog(3, a1, va);
va_end(va);
va_start(va, a1);
writescreen(3, a1, va);
}

canonical_decompiled_code:
void func17(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func14(3, a1, va);
va_end(va);
va_start(va, a1);
func15(3, a1, va);
}

original_code:
void error(const char *format, ...)
{
    va_list ap;
    __builtin_va_start(ap,format);
    writelog(3, format, ap);
    __builtin_va_end(ap);
    __builtin_va_start(ap,format);
    writescreen(3, format, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void error(char *format, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    writelog(3, format, ap);
    __builtin_va_end(ap);
    __builtin_va_start(ap,format);
    writescreen(3, format, ap);
    __builtin_va_end(ap);
}


info

decompiled_code:
void info(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
writelog(6, a1, va);
va_end(va);
va_start(va, a1);
writescreen(6, a1, va);
}

canonical_decompiled_code:
void func18(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func14(6, a1, va);
va_end(va);
va_start(va, a1);
func15(6, a1, va);
}

original_code:
void info(const char *format, ...)
{
    va_list ap;
    __builtin_va_start(ap,format);
    writelog(6, format, ap);
    __builtin_va_end(ap);
    __builtin_va_start(ap,format);
    writescreen(6, format, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void info(char *format, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    writelog(6, format, ap);
    __builtin_va_end(ap);
    __builtin_va_start(ap,format);
    writescreen(6, format, ap);
    __builtin_va_end(ap);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
testescape1(argc, argv, envp);
testescape2();
testescape3();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func7(argc, argv, envp);
func8();
func9();
return 0;
}

original_code:
int main(int argc, const char *argv[])
{
    testescape1();
    testescape2();
    testescape3();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    testescape1();
    testescape2();
    testescape3();
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
__int64 setup_logging()
{
return initlog("root");
}

canonical_decompiled_code:
__int64 func0()
{
return initlog("root");
}

original_code:
void setup_logging()
{
    initlog("root");
}


usage

decompiled_code:
__int64 usage()
{
return print("Usage: root <command> [<argument>]...\n");
}

canonical_decompiled_code:
__int64 func1()
{
return print("Usage: root <command> [<argument>]...\n");
}

original_code:
void usage(void)
{
    print("Usage: root <command> [<argument>]...\n");
}

canonical_original_code:
void usage()
{
    print("Usage: root <command> [<argument>]...\n");
}


decompiled_code:
// attributes: thunk
__int64 command_is_safe()
{
return is_absolute_path();
}

canonical_decompiled_code:
__int64 func2()
{
return is_absolute_path();
}

original_code:
int command_is_safe(const char *path_command)
{
    return is_absolute_path(path_command);
}

canonical_original_code:
int command_is_safe(char *path_command)
{
    return is_absolute_path(path_command);
}


decompiled_code:
__int64 __fastcall print_if_unsafe(const char *a1)
{
__int64 result; // rax

result = command_is_safe();
if ( !(_DWORD)result )
result = print(" \"%s\"", a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
__int64 result; // rax

result = func2();
if ( !(_DWORD)result )
result = print(" \"%s\"", a1);
return result;
}

original_code:
void print_if_unsafe(const char *dir)
{
    if (!command_is_safe(dir)) {
        print(" \"%s\"", dir);
    }
}

canonical_original_code:
void print_if_unsafe(char *dir)
{
    if (!command_is_safe(dir)) {
        print(" \"%s\"", dir);
    }
}

variable_types:
dir
char *


decompiled_code:
__int64 __fastcall print_unsafe_path_entries(__int64 a1)
{
pathenv_each(a1, print_if_unsafe);
return print("\n");
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
pathenv_each(a1, print_if_unsafe);
return print("\n");
}

original_code:
void print_unsafe_path_entries(const char *pathenv)
{
    pathenv_each(pathenv, &print_if_unsafe);
    print("\n");
}

canonical_original_code:
void print_unsafe_path_entries(char *pathenv)
{
    pathenv_each(pathenv, &print_if_unsafe);
    print("\n");
}

variable_types:
pathenv
char *


decompiled_code:
char *__fastcall get_absolute_command(const char *errnum, char **a2, const char *a3)
{
char *result; // rax
const char *v4; // rdx

if ( !errnum )
{
error((int)"get_absolute_command: qualified_command is NULL", (int)a2, a3);
exit(121);
}
result = realpath(errnum, 0LL);
if ( !result )
{
error((int)"Cannot determine real path to %s", (int)errnum, v4);
exit(127);
}
*a2 = result;
return result;
}

canonical_decompiled_code:
char *__fastcall func5(const char *errnum, char **a2, const char *a3)
{
char *result; // rax
const char *v4; // rdx

if ( !errnum )
{
error((int)"get_absolute_command: qualified_command is NULL", (int)a2, a3);
exit(121);
}
result = realpath(errnum, 0LL);
if ( !result )
{
error((int)"Cannot determine real path to %s", (int)errnum, v4);
exit(127);
}
*a2 = result;
return result;
}

original_code:
void get_absolute_command(const char *qualified_command,
                          char **absolute_commandp)
{
    if (qualified_command == ((void *)0)) {
        error("get_absolute_command: qualified_command is NULL");
        exit(121);
    }
    if (absolute_commandp == ((void *)0)) {
        error("get_absolute_command: absolute_commandp is NULL");
        exit(121);
    }
    char *absolute_command = realpath(qualified_command, ((void *)0));
    if (absolute_command == ((void *)0)) {
        error("Cannot determine real path to %s", qualified_command);
        exit(127);
    }
    *absolute_commandp = absolute_command;
}

canonical_original_code:
void get_absolute_command(char *qualified_command, char **absolute_commandp)
{
    if (qualified_command == ((void *)0)) {
        error("get_absolute_command: qualified_command is NULL");
        exit(121);
    }
    if (absolute_commandp == ((void *)0)) {
        error("get_absolute_command: absolute_commandp is NULL");
        exit(121);
    }
    char *absolute_command = realpath(qualified_command, ((void *)0));
    if (absolute_command == ((void *)0)) {
        error("Cannot determine real path to %s", qualified_command);
        exit(127);
    }
    *absolute_commandp = absolute_command;
}


decompiled_code:
__int64 ensure_permitted()
{
__int64 result; // rax
const char *v1; // rdx
__int64 v2; // rsi

result = in_group(0LL);
if ( !(_DWORD)result )
{
v2 = get_group_name(0LL);
if ( v2 )
error((int)"You must be in the %s group to run root", v2, v1);
else
error((int)"You must be in group %u to run root", 0, v1);
exit(123);
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 result; // rax
const char *v1; // rdx
__int64 v2; // rsi

result = in_group(0LL);
if ( !(_DWORD)result )
{
v2 = get_group_name(0LL);
if ( v2 )
error((int)"You must be in the %s group to run root", v2, v1);
else
error((int)"You must be in group %u to run root", 0, v1);
exit(123);
}
return result;
}

original_code:
void ensure_permitted(void)
{
    if (!in_group(0)) {
        const char *groupname = get_group_name(0);
        if (groupname != ((void *)0)) {
            error("You must be in the %s group to run root", groupname);
        }
        else {
            error("You must be in group %u to run root", (unsigned)0);
        }
        exit(123);
    }
}

canonical_original_code:
void ensure_permitted()
{
    if (!in_group(0)) {
        char *groupname = get_group_name(0);
        if (groupname != ((void *)0)) {
            error("You must be in the %s group to run root", groupname);
        }
        else {
            error("You must be in group %u to run root", (unsigned int)0);
        }
        exit(123);
    }
}

variable_types:
groupname
char *


decompiled_code:
__int64 __fastcall become_root(__int64 a1, int a2)
{
__int64 result; // rax
const char *v3; // rdx

set_home_dir(0LL);
setup_groups(0LL);
result = become_user(0LL);
if ( !(_DWORD)result )
{
error((int)"Cannot become root", a2, v3);
exit(124);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2)
{
__int64 result; // rax
const char *v3; // rdx

set_home_dir(0LL);
setup_groups(0LL);
result = become_user(0LL);
if ( !(_DWORD)result )
{
error((int)"Cannot become root", a2, v3);
exit(124);
}
return result;
}

original_code:
void become_root(void)
{
    set_home_dir(0);
    setup_groups(0);
    if (!become_user(0)) {
        error("Cannot become root");
        exit(124);
    }
}

canonical_original_code:
void become_root()
{
    set_home_dir(0);
    setup_groups(0);
    if (!become_user(0)) {
        error("Cannot become root");
        exit(124);
    }
}


decompiled_code:
int __fastcall run_command(const char *errnum, char *const *a2)
{
int result; // eax
int *v3; // rax
char *v4; // rax

result = execv(errnum, a2);
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
error((int)"Cannot exec '%s': %s", (int)errnum, v4);
exit(126);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(const char *errnum, char *const *a2)
{
int result; // eax
int *v3; // rax
char *v4; // rax

result = execv(errnum, a2);
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
error((int)"Cannot exec '%s': %s", (int)errnum, v4);
exit(126);
}
return result;
}

original_code:
void run_command(const char *absolute_command, const char *const *args)
{
    if (execv(absolute_command, (char *const *)args) == -1) {
        error("Cannot exec '%s': %s", absolute_command, strerror((*__errno_location ())));
        exit(126);
    }
}

canonical_original_code:
void run_command(char *absolute_command, char **args)
{
    if (execv(absolute_command, (char **)args) == -1) {
        error("Cannot exec '%s': %s", absolute_command, strerror((*__errno_location ())));
        exit(126);
    }
}

variable_types:
args
char **


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // esi
int errnum[2]; // [rsp+0h] [rbp-28h] BYREF
char *const *v6; // [rsp+8h] [rbp-20h] BYREF

*(_QWORD *)errnum = 0LL;
v6 = 0LL;
setup_logging(argc, argv, envp);
process_args(argc, (__int64)argv, (char *)errnum, &v6);
ensure_permitted();
v3 = errnum[0];
info("Running %s", *(const char **)errnum);
become_root((__int64)"Running %s", v3);
run_command(*(const char **)errnum, v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // esi
int errnum[2]; // [rsp+0h] [rbp-28h] BYREF
char *const *v6; // [rsp+8h] [rbp-20h] BYREF

*(_QWORD *)errnum = 0LL;
v6 = 0LL;
func0(argc, argv, envp);
func11(argc, (__int64)argv, (char *)errnum, &v6);
func6();
v3 = errnum[0];
info("Running %s", *(const char **)errnum);
func7((__int64)"Running %s", v3);
func8(*(const char **)errnum, v6);
return 0;
}

original_code:
int main(int argc, const char *const *argv)
{
    char *absolute_command = ((void *)0);
    const char *const *args = ((void *)0);
    setup_logging();
    process_args(argc, argv, &absolute_command, &args);
    ensure_permitted();
    info("Running %s", absolute_command);
    become_root();
    run_command(absolute_command, args);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    char *absolute_command = ((void *)0);
    char **args = ((void *)0);
    setup_logging();
    process_args(argc, argv, &absolute_command, &args);
    ensure_permitted();
    info("Running %s", absolute_command);
    become_root();
    run_command(absolute_command, args);
    return 0;
}

variable_types:
argv
char **


repo: mikemelia/algorithms

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
__int64 v4; // rax
unsigned int i; // ebx
__int64 v6; // rsi
__int64 v8[4]; // [rsp+8h] [rbp-20h] BYREF

v3 = strlen(text);
v4 = allocate(32LL, argv);
v8[0] = v4;
*(_QWORD *)v4 = text;
*(_DWORD *)(v4 + 8) = 1;
if ( v3 > 1 )
{
for ( i = 1; i != v3; ++i )
{
v6 = i;
add_next_tree(v8, v6, (unsigned int)v3);
}
}
print_tree(v8);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
__int64 v4; // rax
unsigned int i; // ebx
__int64 v6; // rsi
__int64 v8[4]; // [rsp+8h] [rbp-20h] BYREF

v3 = strlen(text);
v4 = func7(32LL, argv);
v8[0] = v4;
*(_QWORD *)v4 = text;
*(_DWORD *)(v4 + 8) = 1;
if ( v3 > 1 )
{
for ( i = 1; i != v3; ++i )
{
v6 = i;
add_next_tree(v8, v6, (unsigned int)v3);
}
}
func9(v8);
return 0;
}

original_code:
int main (int argc, char const *argv[]) {
  int length = strlen(text);
  Tree suffixTree;
  suffixTree.firstChild = allocate(sizeof(Node));
  suffixTree.firstChild->edge = text;
  suffixTree.firstChild->length = 1;
  int i;
  for (i = 1; i < length; i++) {
    add_next_tree(&suffixTree, i, length);
  }
  print_tree(&suffixTree);
}

canonical_original_code:
int main(int argc, char *argv[]) {
  int length = strlen(text);
  struct tree suffixTree;
  suffixTree.firstChild = allocate(sizeof(Node));
  suffixTree.firstChild->edge = text;
  suffixTree.firstChild->length = 1;
  int i;
  for (i = 1; i < length; i++) {
    add_next_tree(&suffixTree, i, length);
  }
  print_tree(&suffixTree);
}

variable_types: length int

variable_types: struct tree { struct node { char *edge; int length; struct node *child; struct node *nextSibling; } *firstChild; }

variable_types: i int

user_defined_types: struct tree { struct node *firstChild; }


allocate

decompiled_code:
void *__fastcall allocate(size_t a1)
{
void *result; // rax
int *v2; // rax
char *v3; // r8
int *v4; // rbx

result = malloc(a1);
if ( !result )
{
v2 = __errno_location();
v3 = "None";
v4 = v2;
if ( *v2 )
v3 = strerror(*v2);
fprintf(stdout, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "ukkonen.c", 22LL, v3);
*v4 = 0;
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func7(size_t a1)
{
void *result; // rax
int *v2; // rax
char *v3; // r8
int *v4; // rbx

result = malloc(a1);
if ( !result )
{
v2 = __errno_location();
v3 = "None";
v4 = v2;
if ( *v2 )
v3 = strerror(*v2);
fprintf(stdout, "[ERROR] (%s:%d: errno: %s) Out of memory.\n", "ukkonen.c", 22LL, v3);
*v4 = 0;
exit(1);
}
return result;
}

original_code:
void *allocate(size_t amount) {
  void *location = malloc(amount);
  if(!((location))) { fprintf(stdout, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "ukkonen.c", 22, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  return location;
 error:
  exit(1);
}

canonical_original_code:
void *allocate(unsigned long amount) {
  void *location = malloc(amount);
  if(!((location))) { fprintf(stdout, "[ERROR] (%s:%d: errno: %s) " "Out of memory." "\n", "ukkonen.c", 22, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ())))); (*__errno_location ())=0; goto error; };
  return location;
 error:
  exit(1);
}

variable_types:
amount
unsigned long

variable_types:
location
void *

return_type: void *


decompiled_code:
void __fastcall print_node(__int64 a1, unsigned int a2)
{
__int64 v3; // rbx
__int64 v4; // r12
const char *v5; // rax
__int64 v6; // r8
__int64 v7; // rdi
const char *v8; // rax

if ( a1 )
{
v3 = a1;
do
{
v4 = *(unsigned int *)(v3 + 8);
v5 = edge_text_from(v3);
v6 = a2++;
fprintf(stderr, "[DEBUG] (%s:%d) %d: %s %d \n\n", "ukkonen.c", 38LL, v6, v5, v4);
v7 = *(_QWORD *)(v3 + 16);
if ( v7 )
((void (__fastcall *)(__int64, _QWORD))print_node)(v7, a2);
v8 = edge_text_from(v3);
fprintf(stderr, "[DEBUG] (%s:%d) New Sibling for %s\n\n", "ukkonen.c", 42LL, v8);
v3 = *(_QWORD *)(v3 + 24);
}
while ( v3 );
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, unsigned int a2)
{
__int64 v3; // rbx
__int64 v4; // r12
const char *v5; // rax
__int64 v6; // r8
__int64 v7; // rdi
const char *v8; // rax

if ( a1 )
{
v3 = a1;
do
{
v4 = *(unsigned int *)(v3 + 8);
v5 = edge_text_from(v3);
v6 = a2++;
fprintf(stderr, "[DEBUG] (%s:%d) %d: %s %d \n\n", "ukkonen.c", 38LL, v6, v5, v4);
v7 = *(_QWORD *)(v3 + 16);
if ( v7 )
((void (__fastcall *)(__int64, _QWORD))print_node)(v7, a2);
v8 = edge_text_from(v3);
fprintf(stderr, "[DEBUG] (%s:%d) New Sibling for %s\n\n", "ukkonen.c", 42LL, v8);
v3 = *(_QWORD *)(v3 + 24);
}
while ( v3 );
}
}

original_code:
void print_node(Node *node, int counter) {
  while (node != 0) {
    fprintf(stderr, "[DEBUG] (%s:%d) " "%d: %s %d \n" "\n", "ukkonen.c", 38, counter++, edge_text_from(node), node->length);
    if (node->child != 0) {
      print_node(node->child, counter);
    }
    fprintf(stderr, "[DEBUG] (%s:%d) " "New Sibling for %s\n" "\n", "ukkonen.c", 42, edge_text_from(node));
    node = node->nextSibling;
  }
}

canonical_original_code:
void print_node(struct node *node, int counter) {
  while (node != 0) {
    fprintf(stderr, "[DEBUG] (%s:%d) " "%d: %s %d \n" "\n", "ukkonen.c", 38, counter++, edge_text_from(node), node->length);
    if (node->child != 0) {
      print_node(node->child, counter);
    }
    fprintf(stderr, "[DEBUG] (%s:%d) " "New Sibling for %s\n" "\n", "ukkonen.c", 42, edge_text_from(node));
    node = node->nextSibling;
  }
}

variable_types: struct node { char *edge; int length; struct node *child; struct node *nextSibling; } *

variable_types: counter int


decompiled_code:
void __fastcall print_tree(__int64 *a1)
{
print_node(*a1, 0);
}

canonical_decompiled_code:
void __fastcall func9(__int64 *a1)
{
func8(*a1, 0);
}

original_code:
void print_tree(Tree *tree) {
  Node *child = tree->firstChild;
  int i = 0;
  print_node(child, i);
}

canonical_original_code:
void print_tree(struct tree *tree) {
  struct node *child = tree->firstChild;
  int i = 0;
  print_node(child, i);
}

variable_types: struct tree { struct node { char *edge; int length; struct node *child; struct node *nextSibling; } *firstChild; } *


decompiled_code:
_BOOL8 __fastcall compare_chars(_BYTE *a1, _BYTE *a2)
{
return *a1 == *a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(_BYTE *a1, _BYTE *a2)
{
return *a1 == *a2;
}

original_code:
int compare_chars(char *first, char *second) {
  return *first == *second;
}

variable_types:
first
char *

variable_types:
second
char *


decompiled_code:
_BOOL8 __fastcall has_next_sibling(__int64 a1)
{
return *(_QWORD *)(a1 + 24) != 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(__int64 a1)
{
return *(_QWORD *)(a1 + 24) != 0LL;
}

original_code:
int has_next_sibling(Node *node) {
  return node->nextSibling != 0;
}

canonical_original_code:
int has_next_sibling(struct node *node) {
  return node->nextSibling != 0;
}

user_defined_types: struct node { char *edge; int length; struct node *child; struct node *nextSibling; }


decompiled_code:
__int64 __fastcall extend_all_nodes(__int64 *a1, int a2)
{
__int64 result; // rax
char *v3; // rsi
int v4; // edx

result = *a1;
if ( *a1 )
{
v3 = &text[a2];
do
{
while ( 1 )
{
v4 = *(_DWORD *)(result + 8);
if ( v4 )
break;
*(_DWORD *)(result + 8) = 1;
*(_QWORD *)result = v3;
result = *(_QWORD *)(result + 24);
if ( !result )
return result;
}
*(_DWORD *)(result + 8) = v4 + 1;
result = *(_QWORD *)(result + 24);
}
while ( result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 *a1, int a2)
{
__int64 result; // rax
char *v3; // rsi
int v4; // edx

result = *a1;
if ( *a1 )
{
v3 = &text[a2];
do
{
while ( 1 )
{
v4 = *(_DWORD *)(result + 8);
if ( v4 )
break;
*(_DWORD *)(result + 8) = 1;
*(_QWORD *)result = v3;
result = *(_QWORD *)(result + 24);
if ( !result )
return result;
}
*(_DWORD *)(result + 8) = v4 + 1;
result = *(_QWORD *)(result + 24);
}
while ( result );
}
return result;
}

original_code:
void extend_all_nodes(Tree *tree, int offset) {
  Node *node = tree->firstChild;
  while (node != 0) {
    if (node->length == 0) {
      node->length = 1;
      node->edge = (text + offset);
    } else {
      node->length += 1;
    }
    node = node->nextSibling;
  }
}

canonical_original_code:
void extend_all_nodes(struct tree *tree, int offset) {
  struct node *node = tree->firstChild;
  while (node != 0) {
    if (node->length == 0) {
      node->length = 1;
      node->edge = (text + offset);
    } else {
      node->length += 1;
    }
    node = node->nextSibling;
  }
}

variable_types: offset int


main

repo: mikenawrocki/clockme

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // rbp

charge_num_list_head = 0LL;
initialize_rl_completion(argc, argv, envp);
read_numbers();
open_log();
atexit((void (*)(void))&clear_history);
while ( 1 )
{
v3 = (void *)readline("(clockme) ");
cmd_parse(v3);
add_history(v3);
free(v3);
}
}

original_code:
int main(void)
{
 char *input;
 charge_num_list_head = ((void *)0);
 initialize_rl_completion();
 read_numbers();
 open_log();
 atexit(clear_history);
 for(;;) {
  input = readline("(clockme) ");
  cmd_parse(input);
  add_history(input);
  free(input);
 }
 fclose(logfile);
 return 0;
}

canonical_original_code:
int main()
{
 char *input;
 charge_num_list_head = ((void *)0);
 initialize_rl_completion();
 read_numbers();
 open_log();
 atexit(clear_history);
 for(;;) {
  input = readline("(clockme) ");
  cmd_parse(input);
  add_history(input);
  free(input);
 }
 fclose(logfile);
 return 0;
}

variable_types:
input
char *


decompiled_code:
int activate_paused()
{
__int64 v0; // rbp

v0 = charge_num_list_head;
if ( !charge_num_list_head )
return fwrite("Error! No charge numbers entered.", 1uLL, 0x21uLL, stderr);
do
{
if ( *(_DWORD *)(v0 + 88) == 2 )
{
*(_DWORD *)(v0 + 88) = 1;
*(_QWORD *)(v0 + 80) = time(0LL);
return printf("Resuming charge number: %s\n", (const char *)v0);
}
v0 = *(_QWORD *)(v0 + 96);
}
while ( v0 );
return fwrite("ERROR! No paused charge number.\n", 1uLL, 0x20uLL, stderr);
}

canonical_decompiled_code:
int func0()
{
__int64 v0; // rbp

v0 = charge_num_list_head;
if ( !charge_num_list_head )
return fwrite("Error! No charge numbers entered.", 1uLL, 0x21uLL, stderr);
do
{
if ( *(_DWORD *)(v0 + 88) == 2 )
{
*(_DWORD *)(v0 + 88) = 1;
*(_QWORD *)(v0 + 80) = time(0LL);
return printf("Resuming charge number: %s\n", (const char *)v0);
}
v0 = *(_QWORD *)(v0 + 96);
}
while ( v0 );
return fwrite("ERROR! No paused charge number.\n", 1uLL, 0x20uLL, stderr);
}

original_code:
static void activate_paused()
{
 struct node *tmp = charge_num_list_head;
 struct charge_num *data;
 if(!tmp) {
  fprintf(stderr, "Error! No charge numbers entered.");
  return;
 }
 do {
  data = &(tmp->data);
  if(data->status == status_paused) {
   data->status = status_active;
   data->start_time = time(((void *)0));
   printf("Resuming charge number: %s\n",data->num);
   return;
  }
  } while(tmp->next && (tmp = tmp->next));
 fprintf(stderr, "ERROR! No paused charge number.\n");
}

variable_types: struct node { struct charge_num { char num[72]; long seconds_elapsed; long start_time; int status; } data; struct node *next; } *

variable_types: struct charge_num { char num[72]; long seconds_elapsed; long start_time; int status; } *

user_defined_types: struct node { struct charge_num data; struct node *next; }


decompiled_code:
int __fastcall activate_num(const char *a1)
{
__int64 v1; // rbx
__int64 v2; // rax
char v3; // r12
int v4; // ebp
bool v5; // zf
time_t v6; // r13
time_t v7; // rax

v1 = charge_num_list_head;
if ( charge_num_list_head )
{
LODWORD(v2) = atoi(a1);
v3 = 0;
v4 = v2;
do
{
while ( !v4 )
{
*(_DWORD *)(v1 + 88) = 1;
v3 = 1;
v4 = -1;
*(_QWORD *)(v1 + 80) = time(0LL);
LODWORD(v2) = printf("Resuming charge number: %s\n", (const char *)v1);
v1 = *(_QWORD *)(v1 + 96);
if ( !v1 )
goto LABEL_8;
}
v5 = *(_DWORD *)(v1 + 88) == 1;
*(_DWORD *)(v1 + 88) = 0;
if ( v5 )
{
v6 = *(_QWORD *)(v1 + 80);
v7 = time(0LL);
v2 = (unsigned int)(int)difftime(v7, v6);
*(_QWORD *)(v1 + 72) += v2;
}
v1 = *(_QWORD *)(v1 + 96);
--v4;
}
while ( v1 );
LABEL_8:
if ( !v3 )
LODWORD(v2) = fwrite("ERROR! Invalid charge number.\n", 1uLL, 0x1EuLL, stderr);
}
else
{
LODWORD(v2) = fwrite("Error! No charge numbers entered.", 1uLL, 0x21uLL, stderr);
}
return v2;
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
__int64 v1; // rbx
__int64 v2; // rax
char v3; // r12
int v4; // ebp
bool v5; // zf
time_t v6; // r13
time_t v7; // rax

v1 = charge_num_list_head;
if ( charge_num_list_head )
{
LODWORD(v2) = atoi(a1);
v3 = 0;
v4 = v2;
do
{
while ( !v4 )
{
*(_DWORD *)(v1 + 88) = 1;
v3 = 1;
v4 = -1;
*(_QWORD *)(v1 + 80) = time(0LL);
LODWORD(v2) = printf("Resuming charge number: %s\n", (const char *)v1);
v1 = *(_QWORD *)(v1 + 96);
if ( !v1 )
goto LABEL_8;
}
v5 = *(_DWORD *)(v1 + 88) == 1;
*(_DWORD *)(v1 + 88) = 0;
if ( v5 )
{
v6 = *(_QWORD *)(v1 + 80);
v7 = time(0LL);
v2 = (unsigned int)(int)difftime(v7, v6);
*(_QWORD *)(v1 + 72) += v2;
}
v1 = *(_QWORD *)(v1 + 96);
--v4;
}
while ( v1 );
LABEL_8:
if ( !v3 )
LODWORD(v2) = fwrite("ERROR! Invalid charge number.\n", 1uLL, 0x1EuLL, stderr);
}
else
{
LODWORD(v2) = fwrite("Error! No charge numbers entered.", 1uLL, 0x21uLL, stderr);
}
return v2;
}

original_code:
static void activate_num(char *num)
{
 struct node *tmp = charge_num_list_head;
 struct charge_num *data;
 char found = 0;
 int order = atoi(num);
 if(!tmp || !num) {
  fprintf(stderr, "Error! No charge numbers entered.");
  return;
 }
 do {
  data = &(tmp->data);
  if(!order--) {
   data->status = status_active;
   data->start_time = time(((void *)0));
   printf("Resuming charge number: %s\n",data->num);
   found = 1;
  }
  else if(data->status == status_active) {
   data->status = status_inactive;
   data->seconds_elapsed += (long)difftime(time(((void *)0)),
     data->start_time);
  }
  else {
   data->status = status_inactive;
  }
 } while(tmp->next && (tmp = tmp->next));
 if(!found)
  fprintf(stderr, "ERROR! Invalid charge number.\n");
}

variable_types:
num
char *

variable_types: found char

variable_types: order int

user_defined_types: struct charge_num { char num[72]; long seconds_elapsed; long start_time; int status; }


decompiled_code:
int write_log()
{
return write_times_formatted(logfile, 1);
}

canonical_decompiled_code:
int func4()
{
return func3(logfile, 1);
}

original_code:
static void write_log(void)
{
 write_times_formatted(logfile, 1);
}

canonical_original_code:
static void write_log()
{
 write_times_formatted(logfile, 1);
}


pause

decompiled_code:
int pause()
{
__int64 v0; // rbp
time_t v1; // r12
time_t v2; // rax

v0 = charge_num_list_head;
if ( !charge_num_list_head )
return fwrite("Error! No charge numbers entered.", 1uLL, 0x21uLL, stderr);
do
{
if ( *(_DWORD *)(v0 + 88) == 1 )
{
*(_DWORD *)(v0 + 88) = 2;
v1 = *(_QWORD *)(v0 + 80);
v2 = time(0LL);
*(_QWORD *)(v0 + 72) += (unsigned int)(int)difftime(v2, v1);
return printf("Pausing charge number %s\n", (const char *)v0);
}
v0 = *(_QWORD *)(v0 + 96);
}
while ( v0 );
return puts("No Active Charge Number!");
}

canonical_decompiled_code:
int func5()
{
__int64 v0; // rbp
time_t v1; // r12
time_t v2; // rax

v0 = charge_num_list_head;
if ( !charge_num_list_head )
return fwrite("Error! No charge numbers entered.", 1uLL, 0x21uLL, stderr);
do
{
if ( *(_DWORD *)(v0 + 88) == 1 )
{
*(_DWORD *)(v0 + 88) = 2;
v1 = *(_QWORD *)(v0 + 80);
v2 = time(0LL);
*(_QWORD *)(v0 + 72) += (unsigned int)(int)difftime(v2, v1);
return printf("Pausing charge number %s\n", (const char *)v0);
}
v0 = *(_QWORD *)(v0 + 96);
}
while ( v0 );
return puts("No Active Charge Number!");
}

original_code:
void pause(int argc, char **argv)
{
 struct node *tmp = charge_num_list_head;
 if(!tmp) {
  fprintf(stderr, "Error! No charge numbers entered.");
  return;
 }
 do {
  if(tmp->data.status == status_active) {
   tmp->data.status = status_paused;
   tmp->data.seconds_elapsed += (long)difftime(time(((void *)0)),
    tmp->data.start_time);
   printf("Pausing charge number %s\n", tmp->data.num);
   return;
  }
 } while(tmp->next && (tmp = tmp->next));
 printf("No Active Charge Number!\n");
}

variable_types: argc int

variable_types:
argv
char **


resume

decompiled_code:
int __fastcall resume(int a1, __int64 a2)
{
int result; // eax

if ( a1 <= 1 )
result = activate_paused();
else
result = activate_num(*(const char **)(a2 + 8));
return result;
}

canonical_decompiled_code:
int __fastcall func6(int a1, __int64 a2)
{
int result; // eax

if ( a1 <= 1 )
result = func0();
else
result = func1(*(const char **)(a2 + 8));
return result;
}

original_code:
void resume(int argc, char **argv)
{
 if(argc < 2) {
  activate_paused();
 }
 else {
  activate_num(argv[1]);
 }
}


decompiled_code:
__int64 __fastcall add_number(char *src)
{
char *v1; // rax
char *v2; // rax
char *v3; // rcx
__int64 result; // rax

v1 = (char *)malloc(0x68uLL);
v2 = strncpy(v1, src, 0x48uLL);
v2[49] = 0;
v3 = v2;
*((_QWORD *)v2 + 9) = 0LL;
*((_QWORD *)v2 + 10) = 0LL;
*((_DWORD *)v2 + 22) = 0;
result = charge_num_list_head;
charge_num_list_head = v3;
*((_QWORD *)v3 + 12) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *src)
{
char *v1; // rax
char *v2; // rax
char *v3; // rcx
__int64 result; // rax

v1 = (char *)malloc(0x68uLL);
v2 = strncpy(v1, src, 0x48uLL);
v2[49] = 0;
v3 = v2;
*((_QWORD *)v2 + 9) = 0LL;
*((_QWORD *)v2 + 10) = 0LL;
*((_DWORD *)v2 + 22) = 0;
result = charge_num_list_head;
charge_num_list_head = v3;
*((_QWORD *)v3 + 12) = result;
return result;
}

original_code:
void add_number(char *num)
{
 struct node *new_node = malloc(sizeof(struct node));
 strncpy(new_node->data.num, num, 72);
 new_node->data.num[49] = '\0';
 new_node->data.seconds_elapsed = 0;
 new_node->data.start_time = 0;
 new_node->data.status = status_inactive;
 new_node->next = charge_num_list_head;
 charge_num_list_head = new_node;
}


add

decompiled_code:
int __fastcall add(int a1, __int64 a2)
{
if ( (unsigned int)(a1 - 2) > 1 )
return puts("Invalid Arguments for charge number add.");
add_number(*(char **)(a2 + 8));
return write_number(*(_QWORD *)(a2 + 8));
}

canonical_decompiled_code:
int __fastcall func8(int a1, __int64 a2)
{
if ( (unsigned int)(a1 - 2) > 1 )
return puts("Invalid Arguments for charge number add.");
func7(*(char **)(a2 + 8));
return write_number(*(_QWORD *)(a2 + 8));
}

original_code:
void add(int argc, char **argv)
{
 if(argc < 2 || argc > 3) {
  printf("Invalid Arguments for charge number add.\n");
  return;
 }
 add_number(argv[1]);
 write_number(argv[1]);
}


list

decompiled_code:
int list()
{
return write_times_formatted(stdout, 0);
}

canonical_decompiled_code:
int func9()
{
return func3(stdout, 0);
}

original_code:
void list(int argc, char **argv)
{
 write_times_formatted(stdout, 0);
}


quit

decompiled_code:
void __noreturn quit()
{
list();
write_log();
fclose(logfile);
exit(0);
}

canonical_decompiled_code:
void __noreturn func10()
{
func9();
func4();
fclose(logfile);
exit(0);
}

original_code:
void quit(int argc, char **argv)
{
 list(0, ((void *)0));
 write_log();
 fclose(logfile);
 exit(0);
}


decompiled_code:
char *__fastcall make_args(int *a1, void **a2, char *a3)
{
char *result; // rax
char *i; // rdx
_QWORD *v6; // rcx

*a1 = 0;
*a2 = malloc(0x28uLL);
result = strtok(a3, " ");
for ( i = result; result; i = result )
{
result = (char *)*a1;
if ( (int)result > 4 )
break;
v6 = *a2;
*a1 = (_DWORD)result + 1;
v6[(_QWORD)result] = i;
result = strtok(0LL, " ");
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(int *a1, void **a2, char *a3)
{
char *result; // rax
char *i; // rdx
_QWORD *v6; // rcx

*a1 = 0;
*a2 = malloc(0x28uLL);
result = strtok(a3, " ");
for ( i = result; result; i = result )
{
result = (char *)*a1;
if ( (int)result > 4 )
break;
v6 = *a2;
*a1 = (_DWORD)result + 1;
v6[(_QWORD)result] = i;
result = strtok(0LL, " ");
}
return result;
}

original_code:
static void make_args(int *argc, char ***argv, char *args)
{
 *argc = 0;
 argv[*argc] = malloc(sizeof(char *) * 5);
 char *str_arg = strtok(args, " ");
 while(str_arg && *argc < 5) {
  (*argv)[(*argc)++] = str_arg;
  str_arg = strtok(((void *)0), " ");
 }
}

variable_types:
argc
int *

variable_types:
argv
char ***

variable_types:
args
char *


decompiled_code:
char *__fastcall find_cmd(char *s2)
{
__int64 v1; // rbx

v1 = 0LL;
while ( strcmp((&commands)[2 * v1], s2) )
{
if ( ++v1 == 10 )
return 0LL;
}
return (&commands)[2 * (int)v1 + 1];
}

canonical_decompiled_code:
char *__fastcall func1(char *s2)
{
__int64 v1; // rbx

v1 = 0LL;
while ( strcmp((&commands)[2 * v1], s2) )
{
if ( ++v1 == 10 )
return 0LL;
}
return (&commands)[2 * (int)v1 + 1];
}

original_code:
static void (*find_cmd(char *cmd))(int, char**)
{
 int i;
 for(i = 0; i < (sizeof(commands) / sizeof(commands[0])); i++) {
  if(!strcmp(commands[i].cmd_str, cmd)) {
   return commands[i].func;
  }
 }
 return ((void *)0);
}

canonical_original_code:
static void (*find_cmd(char *cmd))(int, char **)
{
 int i;
 for(i = 0; i < (sizeof(commands) / sizeof(commands[0])); i++) {
  if(!strcmp(commands[i].cmd_str, cmd)) {
   return commands[i].func;
  }
 }
 return ((void *)0);
}

variable_types:
cmd
char *

variable_types: i int

return_type: void (*)(int, char **)


decompiled_code:
char *__fastcall command_gen(char *s2, int a2)
{
__int64 v2; // rbx
size_t v3; // r13
const char *v4; // rbp
int v5; // eax

v2 = a2;
if ( !a2 )
{
len_0 = strlen(s2);
v3 = len_0;
goto LABEL_4;
}
v2 = ndx_1;
v3 = len_0;
if ( (unsigned int)ndx_1 > 9 )
return 0LL;
LABEL_4:
while ( 1 )
{
v4 = (&commands)[2 * v2];
v5 = strncmp(v4, s2, v3);
ndx_1 = v2 + 1;
if ( !v5 )
break;
if ( (unsigned int)++v2 > 9 )
return 0LL;
}
return strdup(v4);
}

canonical_decompiled_code:
char *__fastcall func2(char *s2, int a2)
{
__int64 v2; // rbx
size_t v3; // r13
const char *v4; // rbp
int v5; // eax

v2 = a2;
if ( !a2 )
{
len_0 = strlen(s2);
v3 = len_0;
goto LABEL_4;
}
v2 = ndx_1;
v3 = len_0;
if ( (unsigned int)ndx_1 > 9 )
return 0LL;
LABEL_4:
while ( 1 )
{
v4 = (&commands)[2 * v2];
v5 = strncmp(v4, s2, v3);
ndx_1 = v2 + 1;
if ( !v5 )
break;
if ( (unsigned int)++v2 > 9 )
return 0LL;
}
return strdup(v4);
}

original_code:
char *command_gen(const char *text, int state)
{
 static int ndx, len;
 if(!state) {
  ndx = 0;
  len = strlen(text);
 }
 for(; ndx < (sizeof(commands) / sizeof(commands[0])); ndx++) {
  if(!strncmp(commands[ndx].cmd_str, text, len))
   return strdup(commands[ndx++].cmd_str);
 }
 return ((void *)0);
}

canonical_original_code:
char *command_gen(char *text, int state)
{
 static int ndx; static int len;
 if(!state) {
  ndx = 0;
  len = strlen(text);
 }
 for(; ndx < (sizeof(commands) / sizeof(commands[0])); ndx++) {
  if(!strncmp(commands[ndx].cmd_str, text, len))
   return strdup(commands[ndx++].cmd_str);
 }
 return ((void *)0);
}

variable_types:
text
char *

variable_types: state int

variable_types: ndx int

variable_types: len int


decompiled_code:
void __fastcall cmd_parse(char *a1)
{
char *v1; // rdx
unsigned int v2; // ebp
void *v3; // r12
const char *v4; // r13
char *v5; // rax
unsigned int v6; // [rsp+14h] [rbp-24h] BYREF
void *ptr; // [rsp+18h] [rbp-20h] BYREF

v1 = a1;
if ( !a1 )
{
puts("quit");
quit(0LL, 0LL);
v1 = 0LL;
}
make_args((int *)&v6, &ptr, v1);
v2 = v6;
if ( v6 )
{
v3 = ptr;
v4 = *(const char **)ptr;
v5 = find_cmd(*(char **)ptr);
if ( v5 )
((void (__fastcall *)(_QWORD, void *))v5)(v2, v3);
else
printf("%s : Command not found!\n", v4);
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(char *a1)
{
char *v1; // rdx
unsigned int v2; // ebp
void *v3; // r12
const char *v4; // r13
char *v5; // rax
unsigned int v6; // [rsp+14h] [rbp-24h] BYREF
void *ptr; // [rsp+18h] [rbp-20h] BYREF

v1 = a1;
if ( !a1 )
{
puts("quit");
quit(0LL, 0LL);
v1 = 0LL;
}
func0((int *)&v6, &ptr, v1);
v2 = v6;
if ( v6 )
{
v3 = ptr;
v4 = *(const char **)ptr;
v5 = func1(*(char **)ptr);
if ( v5 )
((void (__fastcall *)(_QWORD, void *))v5)(v2, v3);
else
printf("%s : Command not found!\n", v4);
}
free(ptr);
}

original_code:
void cmd_parse(char *cmd)
{
 int argc;
 char **argv;
 if(!cmd) {
  printf("quit\n");
  quit(0, ((void *)0));
 }
 make_args(&argc,&argv, cmd);
 if(argc == 0) {
  free(argv);
  return;
 }
 cmd_func command = find_cmd(argv[0]);
 if(command)
  command(argc, argv);
 else
  printf("%s : Command not found!\n", argv[0]);
 free(argv);
}

canonical_original_code:
void cmd_parse(char *cmd)
{
 int argc;
 char **argv;
 if(!cmd) {
  printf("quit\n");
  quit(0, ((void *)0));
 }
 make_args(&argc,&argv, cmd);
 if(argc == 0) {
  free(argv);
  return;
 }
 void (*command)(int, char **) = find_cmd(argv[0]);
 if(command)
  command(argc, argv);
 else
  printf("%s : Command not found!\n", argv[0]);
 free(argv);
}

variable_types:
command
void (*)(int, char **)


decompiled_code:
void initialize_rl_completion()
{
rl_completion_entry_function = command_gen;
}

canonical_decompiled_code:
void func4()
{
rl_completion_entry_function = command_gen;
}


decompiled_code:
FILE *open_numfile()
{
char *v0; // rax
int *v1; // rax
FILE *result; // rax
int *v3; // [rsp+8h] [rbp-1010h]
char dest[4104]; // [rsp+10h] [rbp-1008h] BYREF

v0 = getenv("HOME");
strncpy(dest, v0, 0x1000uLL);
strcat(dest, "/.clockme/");
if ( mkdir(dest, 0x1E8u) < 0 )
{
v1 = _errno_location();
if ( *v1 != 17 )
{
v3 = v1;
perror("logging directory");
exit(-*v3);
}
}
*(_QWORD *)&dest[strlen(dest)] = 0x737265626D756ELL;
result = fopen(dest, "a+");
numfile = result;
if ( !result )
{
perror("numfile");
exit(-4);
}
return result;
}

canonical_decompiled_code:
FILE *func0()
{
char *v0; // rax
int *v1; // rax
FILE *result; // rax
int *v3; // [rsp+8h] [rbp-1010h]
char dest[4104]; // [rsp+10h] [rbp-1008h] BYREF

v0 = getenv("HOME");
strncpy(dest, v0, 0x1000uLL);
strcat(dest, "/.clockme/");
if ( mkdir(dest, 0x1E8u) < 0 )
{
v1 = _errno_location();
if ( *v1 != 17 )
{
v3 = v1;
perror("logging directory");
exit(-*v3);
}
}
*(_QWORD *)&dest[strlen(dest)] = 0x737265626D756ELL;
result = fopen(dest, "a+");
numfile = result;
if ( !result )
{
perror("numfile");
exit(-4);
}
return result;
}

original_code:
void open_numfile()
{
 char numfile_name[4096];
 strncpy(numfile_name, getenv("HOME"), 4096);
 strcat(numfile_name, "/.clockme/");
 if(mkdir(numfile_name, 0750) < 0) {
  if((*__errno_location ()) != 17) {
   perror("logging directory");
   exit(-(*__errno_location ()));
  }
 }
 strcat(numfile_name, "numbers");
 numfile = fopen(numfile_name, "a+");
 if(!numfile) {
  perror("numfile");
  exit(-4);
 }
}


decompiled_code:
int read_numbers()
{
char v1[89]; // [rsp+1h] [rbp-59h]
char v2[88]; // [rsp+2h] [rbp-58h] BYREF

open_numfile();
while ( fgets(v2, 72, numfile) )
{
v1[strnlen(v2, 0x48uLL)] = 0;
add_number(v2);
}
return fclose(numfile);
}

canonical_decompiled_code:
int func1()
{
char v1[89]; // [rsp+1h] [rbp-59h]
char v2[88]; // [rsp+2h] [rbp-58h] BYREF

func0();
while ( fgets(v2, 72, numfile) )
{
v1[strnlen(v2, 0x48uLL)] = 0;
add_number(v2);
}
return fclose(numfile);
}

original_code:
void read_numbers()
{
 char chargenum[72];
 open_numfile();
 while(fgets(chargenum, 72, numfile)) {
  chargenum[strnlen(chargenum,72)-1] = '\0';
  add_number(chargenum);
 }
 fclose(numfile);
}

variable_types:
chargenum
char [72]


decompiled_code:
int __fastcall write_number(char *s)
{
open_numfile();
fseek(numfile, 0LL, 2);
fputs(s, numfile);
fputc(10, numfile);
return fclose(numfile);
}

canonical_decompiled_code:
int __fastcall func2(char *s)
{
func0();
fseek(numfile, 0LL, 2);
fputs(s, numfile);
fputc(10, numfile);
return fclose(numfile);
}

original_code:
void write_number(char *num)
{
 open_numfile();
 fseek(numfile, 0, 2);
 fputs(num, numfile);
 fputs("\n", numfile);
 fclose(numfile);
}


decompiled_code:
char *__fastcall iso8601_date(time_t a1)
{
char *v1; // r12
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = (char *)malloc(0xCuLL);
v2 = localtime(&timer);
if ( !v2 )
{
perror("Time");
exit(-2);
}
strftime(v1, 0xCuLL, "%F", v2);
return v1;
}

canonical_decompiled_code:
char *__fastcall func0(time_t a1)
{
char *v1; // r12
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = (char *)malloc(0xCuLL);
v2 = localtime(&timer);
if ( !v2 )
{
perror("Time");
exit(-2);
}
strftime(v1, 0xCuLL, "%F", v2);
return v1;
}

original_code:
static char *iso8601_date(time_t convtime)
{
 char *datestr = malloc(12);
 struct tm *loctime = localtime(&convtime);
 if(loctime)
  strftime(datestr, 12, "%F", loctime);
 else {
  perror("Time");
  exit(-2);
 }
 return datestr;
}

canonical_original_code:
static char *iso8601_date(long convtime)
{
 char *datestr = malloc(12);
 struct tm *loctime = localtime(&convtime);
 if(loctime)
  strftime(datestr, 12, "%F", loctime);
 else {
  perror("Time");
  exit(-2);
 }
 return datestr;
}

variable_types: convtime long

variable_types:
datestr
char *

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


decompiled_code:
FILE *open_log()
{
time_t v0; // rax
char *v1; // rbp
char *v2; // rax
FILE *result; // rax
char v4[4104]; // [rsp+0h] [rbp-1008h] BYREF

v0 = time(0LL);
v1 = iso8601_date(v0);
v2 = getenv("HOME");
strncpy(v4, v2, 0x1000uLL);
strcat(v4, "/.clockme/");
strncat(v4, v1, 0xCuLL);
strcat(v4, ".log");
result = fopen(v4, "w+");
logfile = (__int64)result;
if ( !result )
{
perror("Logging");
exit(-3);
}
return result;
}

canonical_decompiled_code:
FILE *func1()
{
time_t v0; // rax
char *v1; // rbp
char *v2; // rax
FILE *result; // rax
char v4[4104]; // [rsp+0h] [rbp-1008h] BYREF

v0 = time(0LL);
v1 = func0(v0);
v2 = getenv("HOME");
strncpy(v4, v2, 0x1000uLL);
strcat(v4, "/.clockme/");
strncat(v4, v1, 0xCuLL);
strcat(v4, ".log");
result = fopen(v4, "w+");
logfile = (__int64)result;
if ( !result )
{
perror("Logging");
exit(-3);
}
return result;
}

original_code:
void open_log()
{
 char *isodate;
 char logname[4096];
 time_t curtime = time(((void *)0));
 isodate = iso8601_date(curtime);
 strncpy(logname, getenv("HOME"), 4096);
 strcat(logname, "/.clockme/");
 strncat(logname, isodate, 12);
 strcat(logname, ".log");
 logfile = fopen(logname, "w+");
 if(!logfile) {
  perror("Logging");
  exit(-3);
 }
}

canonical_original_code:
void open_log()
{
 char *isodate;
 char logname[4096];
 long curtime = time(((void *)0));
 isodate = iso8601_date(curtime);
 strncpy(logname, getenv("HOME"), 4096);
 strcat(logname, "/.clockme/");
 strncat(logname, isodate, 12);
 strcat(logname, ".log");
 logfile = fopen(logname, "w+");
 if(!logfile) {
  perror("Logging");
  exit(-3);
 }
}

variable_types:
isodate
char *

variable_types:
logname
char [4096]

variable_types: curtime long