xconfigAlloc

repo: NVIDIA/nvidia-xconfig

decompiled_code:
void *__fastcall xconfigAlloc(size_t a1)
{
void *result; // rax
int *v2; // rax
char *v3; // rax

result = calloc(a1, 1uLL);
if ( !result )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "memory allocation failure (%s)! \n", v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1)
{
void *result; // rax
int *v2; // rax
char *v3; // rax

result = calloc(a1, 1uLL);
if ( !result )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "memory allocation failure (%s)! \n", v3);
exit(1);
}
return result;
}

original_code:
void *xconfigAlloc(size_t size)
{
    void *m = malloc(size);
    if (!m) {
        fprintf(stderr, "memory allocation failure (%s)! \n", strerror((*__errno_location ())));
        exit(1);
    }
    memset((char *) m, 0, size);
    return m;
}

canonical_original_code:
void *xconfigAlloc(unsigned long size)
{
    void *m = malloc(size);
    if (!m) {
        fprintf(stderr, "memory allocation failure (%s)! \n", strerror((*__errno_location ())));
        exit(1);
    }
    memset((char *) m, 0, size);
    return m;
}

variable_types:
m
void *


xconfigStrdup

decompiled_code:
char *__fastcall xconfigStrdup(const char *a1)
{
char *result; // rax
int *v2; // rax
char *v3; // rax

if ( !a1 )
return 0LL;
result = strdup(a1);
if ( !result )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "memory allocation failure during strdup (%s)! \n", v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func1(const char *a1)
{
char *result; // rax
int *v2; // rax
char *v3; // rax

if ( !a1 )
return 0LL;
result = strdup(a1);
if ( !result )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "memory allocation failure during strdup (%s)! \n", v3);
exit(1);
}
return result;
}

original_code:
char *xconfigStrdup(const char *s)
{
    char *m;
    if (!s) return ((void *)0);
    m = strdup(s);
    if (!m) {
        fprintf(stderr, "memory allocation failure during strdup (%s)! \n",
                strerror((*__errno_location ())));
        exit(1);
    }
    return m;
}

canonical_original_code:
char *xconfigStrdup(char *s)
{
    char *m;
    if (!s) return ((void *)0);
    m = strdup(s);
    if (!m) {
        fprintf(stderr, "memory allocation failure during strdup (%s)! \n",
                strerror((*__errno_location ())));
        exit(1);
    }
    return m;
}

variable_types:
s
char *

variable_types:
m
char *

return_type: char *


xconfigErrorMsg

decompiled_code:
void xconfigErrorMsg(unsigned int a1, const char *a2, ...)
{
double v2; // xmm0_8
double v3; // xmm1_8
double v4; // xmm2_8
double v5; // xmm3_8
double v6; // xmm6_8
double v7; // xmm7_8
int v8; // ebx
size_t v9; // r15
char *i; // r14
int v11; // eax
double v12; // xmm4_8
double v13; // xmm5_8
char *v14; // r12
double v15; // xmm4_8
double v16; // xmm5_8
char *v17; // r12
double v18; // xmm4_8
double v19; // xmm5_8
char *v20; // r15
double v21; // xmm4_8
double v22; // xmm5_8
gcc_va_list arg; // [rsp+8h] [rbp-138h] BYREF
char s[64]; // [rsp+20h] [rbp-120h] BYREF
__int128 v25; // [rsp+90h] [rbp-B0h]
__int128 v26; // [rsp+A0h] [rbp-A0h]
__int128 v27; // [rsp+B0h] [rbp-90h]
__int128 v28; // [rsp+C0h] [rbp-80h]
__int128 v29; // [rsp+D0h] [rbp-70h]
__int128 v30; // [rsp+E0h] [rbp-60h]
__int128 v31; // [rsp+F0h] [rbp-50h]
__int128 v32; // [rsp+100h] [rbp-40h]

va_start(arg, a2);
*(double *)&v25 = va_arg(arg, double);
v2 = *(double *)&v25;
*(double *)&v26 = va_arg(arg, double);
v3 = *(double *)&v26;
*(double *)&v27 = va_arg(arg, double);
v4 = *(double *)&v27;
*(double *)&v28 = va_arg(arg, double);
v5 = *(double *)&v28;
*(double *)&v29 = va_arg(arg, double);
*(double *)&v30 = va_arg(arg, double);
*(double *)&v31 = va_arg(arg, double);
v6 = *(double *)&v31;
*(double *)&v32 = va_arg(arg, double);
v7 = *(double *)&v32;
va_end(arg);
v8 = 64;
v9 = 64LL;
for ( i = (char *)xconfigAlloc(0x40uLL); ; i = (char *)xconfigAlloc(v8) )
{
va_start(arg, a2);
v11 = vsnprintf(i, v9, a2, arg);
if ( v11 < (unsigned int)v8 )
break;
v8 += 64;
if ( v11 >= 0 )
v8 = v11 + 1;
free(i);
v9 = v8;
}
if ( a1 == 1 )
{
sprintf(s, "%d", configLineNo);
v17 = xconfigStrcat(
"Parse warning on line ",
(__int64)s,
v2,
v3,
v4,
v5,
v15,
v16,
v6,
v7,
" of section ",
configSection,
" in file ",
configPath,
".\n",
0LL);
}
else if ( a1 == 2 )
{
v17 = xconfigStrcat("Data incomplete in file ", configPath, v2, v3, v4, v5, v12, v13, v6, v7, ".\n", 0LL);
}
else
{
if ( a1 )
{
LABEL_9:
v14 = strdup(i);
xconfigPrint(a1, v14);
free(i);
free(v14);
return;
}
sprintf(s, "%d", configLineNo);
v17 = xconfigStrcat(
"Parse error on line ",
(__int64)s,
v2,
v3,
v4,
v5,
v21,
v22,
v6,
v7,
" of section ",
configSection,
" in file ",
configPath,
".\n");
}
if ( !v17 )
goto LABEL_9;
v20 = xconfigStrcat(v17, (__int64)i, v2, v3, v4, v5, v18, v19, v6, v7, 0LL);
xconfigPrint(a1, v20);
free(i);
free(v20);
free(v17);
}

canonical_decompiled_code:
void func3(unsigned int a1, const char *a2, ...)
{
double v2; // xmm0_8
double v3; // xmm1_8
double v4; // xmm2_8
double v5; // xmm3_8
double v6; // xmm6_8
double v7; // xmm7_8
int v8; // ebx
size_t v9; // r15
char *i; // r14
int v11; // eax
double v12; // xmm4_8
double v13; // xmm5_8
char *v14; // r12
double v15; // xmm4_8
double v16; // xmm5_8
char *v17; // r12
double v18; // xmm4_8
double v19; // xmm5_8
char *v20; // r15
double v21; // xmm4_8
double v22; // xmm5_8
gcc_va_list arg; // [rsp+8h] [rbp-138h] BYREF
char s[64]; // [rsp+20h] [rbp-120h] BYREF
__int128 v25; // [rsp+90h] [rbp-B0h]
__int128 v26; // [rsp+A0h] [rbp-A0h]
__int128 v27; // [rsp+B0h] [rbp-90h]
__int128 v28; // [rsp+C0h] [rbp-80h]
__int128 v29; // [rsp+D0h] [rbp-70h]
__int128 v30; // [rsp+E0h] [rbp-60h]
__int128 v31; // [rsp+F0h] [rbp-50h]
__int128 v32; // [rsp+100h] [rbp-40h]

va_start(arg, a2);
*(double *)&v25 = va_arg(arg, double);
v2 = *(double *)&v25;
*(double *)&v26 = va_arg(arg, double);
v3 = *(double *)&v26;
*(double *)&v27 = va_arg(arg, double);
v4 = *(double *)&v27;
*(double *)&v28 = va_arg(arg, double);
v5 = *(double *)&v28;
*(double *)&v29 = va_arg(arg, double);
*(double *)&v30 = va_arg(arg, double);
*(double *)&v31 = va_arg(arg, double);
v6 = *(double *)&v31;
*(double *)&v32 = va_arg(arg, double);
v7 = *(double *)&v32;
va_end(arg);
v8 = 64;
v9 = 64LL;
for ( i = (char *)func0(0x40uLL); ; i = (char *)func0(v8) )
{
va_start(arg, a2);
v11 = vsnprintf(i, v9, a2, arg);
if ( v11 < (unsigned int)v8 )
break;
v8 += 64;
if ( v11 >= 0 )
v8 = v11 + 1;
free(i);
v9 = v8;
}
if ( a1 == 1 )
{
sprintf(s, "%d", configLineNo);
v17 = func2(
"Parse warning on line ",
(__int64)s,
v2,
v3,
v4,
v5,
v15,
v16,
v6,
v7,
" of section ",
configSection,
" in file ",
configPath,
".\n",
0LL);
}
else if ( a1 == 2 )
{
v17 = func2("Data incomplete in file ", configPath, v2, v3, v4, v5, v12, v13, v6, v7, ".\n", 0LL);
}
else
{
if ( a1 )
{
LABEL_9:
v14 = strdup(i);
xconfigPrint(a1, v14);
free(i);
free(v14);
return;
}
sprintf(s, "%d", configLineNo);
v17 = func2(
"Parse error on line ",
(__int64)s,
v2,
v3,
v4,
v5,
v21,
v22,
v6,
v7,
" of section ",
configSection,
" in file ",
configPath,
".\n");
}
if ( !v17 )
goto LABEL_9;
v20 = func2(v17, (__int64)i, v2, v3, v4, v5, v18, v19, v6, v7, 0LL);
xconfigPrint(a1, v20);
free(i);
free(v20);
free(v17);
}

original_code:
void xconfigErrorMsg(MsgType t, char *fmt, ...)
{
    va_list ap;
    int len, current_len = 64;
    char *b, *pre = ((void *)0), *msg;
    char scratch[64];
    b = xconfigAlloc(current_len);
    while (1) {
        __builtin_va_start(ap,fmt);
        len = vsnprintf(b, current_len, fmt, ap);
        __builtin_va_end(ap);
        if ((len > -1) && (len < current_len)) {
            break;
        } else if (len > -1) {
            current_len = len + 1;
        } else {
            current_len += 64;
        }
        free(b);
        b = xconfigAlloc(current_len);
    }
    switch (t) {
    case ParseErrorMsg:
        sprintf(scratch, "%d", configLineNo);
        pre = xconfigStrcat("Parse error on line ", scratch, " of section ",
                         configSection, " in file ", configPath, ".\n", ((void *)0));
        break;
    case ParseWarningMsg:
        sprintf(scratch, "%d", configLineNo);
        pre = xconfigStrcat("Parse warning on line ", scratch, " of section ",
                         configSection, " in file ", configPath, ".\n", ((void *)0));
        break;
    case ValidationErrorMsg:
        pre = xconfigStrcat("Data incomplete in file ", configPath, ".\n", ((void *)0));
        break;
    case InternalErrorMsg: break;
    case WriteErrorMsg: break;
    case WarnMsg: break;
    case ErrorMsg: break;
    case DebugMsg: break;
    case UnknownMsg: break;
    }
    if (pre) {
        msg = xconfigStrcat(pre, b, ((void *)0));
    } else {
        msg = strdup(b);
    }
    xconfigPrint(t, msg);
    free(b);
    free(msg);
    if (pre) free(pre);
}

canonical_original_code:
void xconfigErrorMsg(enum MsgType t, char *fmt, ...)
{
    __builtin_va_list ap;
    int len; int current_len = 64;
    char *b; char *pre = ((void *)0); char *msg;
    char scratch[64];
    b = xconfigAlloc(current_len);
    while (1) {
        __builtin_va_start(ap,fmt);
        len = vsnprintf(b, current_len, fmt, ap);
        __builtin_va_end(ap);
        if ((len > -1) && (len < current_len)) {
            break;
        } else if (len > -1) {
            current_len = len + 1;
        } else {
            current_len += 64;
        }
        free(b);
        b = xconfigAlloc(current_len);
    }
    switch (t) {
    case ParseErrorMsg:
        sprintf(scratch, "%d", configLineNo);
        pre = xconfigStrcat("Parse error on line ", scratch, " of section ",
                         configSection, " in file ", configPath, ".\n", ((void *)0));
        break;
    case ParseWarningMsg:
        sprintf(scratch, "%d", configLineNo);
        pre = xconfigStrcat("Parse warning on line ", scratch, " of section ",
                         configSection, " in file ", configPath, ".\n", ((void *)0));
        break;
    case ValidationErrorMsg:
        pre = xconfigStrcat("Data incomplete in file ", configPath, ".\n", ((void *)0));
        break;
    case InternalErrorMsg: break;
    case WriteErrorMsg: break;
    case WarnMsg: break;
    case ErrorMsg: break;
    case DebugMsg: break;
    case UnknownMsg: break;
    }
    if (pre) {
        msg = xconfigStrcat(pre, b, ((void *)0));
    } else {
        msg = strdup(b);
    }
    xconfigPrint(t, msg);
    free(b);
    free(msg);
    if (pre) free(pre);
}

variable_types: enum MsgType { ParseErrorMsg=0, ParseWarningMsg=1, ValidationErrorMsg=2, InternalErrorMsg=3, WriteErrorMsg=4, WarnMsg=5, ErrorMsg=6, DebugMsg=7, UnknownMsg=8 }

variable_types:
fmt
char *

variable_types: len int

variable_types:
b
char *

variable_types:
pre
char *

variable_types:
msg
char *

variable_types:
scratch
char [64]

return_type: void


decompiled_code:
__int64 update_scf_depth()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 1LL;
}

original_code:
int update_scf_depth(int depth)
{
    return 1;
}

variable_types: depth int

return_type: int


decompiled_code:
__int64 read_scf_depth()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 0LL;
}

original_code:
int read_scf_depth(int *depth)
{
    return 0;
}

variable_types:
depth
int *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
gen_manpage_opts_helper(&_options, argv, envp);
return 0;
}

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

original_code:
int main(void)
{
    gen_manpage_opts_helper(__options);
    return 0;
}

canonical_original_code:
int main()
{
    gen_manpage_opts_helper(__options);
    return 0;
}


decompiled_code:
int __fastcall print_option(const char **a1)
{
const char *v2; // rsi
const unsigned __int16 **v3; // rax
__int64 v4; // rsi
int v5; // eax
const char *v6; // r14
int v7; // edi
int v8; // er12
int v9; // edx
int v10; // er15
int v11; // ebx
const char *v13; // rbx
int v14; // er12
const __int32_t *v15; // rsi
__int64 v16; // rax
__int64 v17; // rdx
char dest[112]; // [rsp+0h] [rbp-70h] BYREF

if ( (*((_BYTE *)a1 + 12) & 0xE) != 0 )
{
v2 = a1[2];
if ( v2 )
{
strcpy(dest, v2);
}
else
{
v13 = *a1;
v14 = strlen(*a1);
if ( v14 > 0 )
{
v15 = *__ctype_toupper_loc();
v16 = 0LL;
do
{
dest[v16] = v15[v13[v16]];
v17 = v16++;
}
while ( v17 != v14 - 1 );
}
dest[v14] = 0;
}
}
printf(".TP\n.BI \"");
v3 = __ctype_b_loc();
v4 = *((int *)a1 + 2);
if ( ((*v3)[v4] & 0x400) != 0 )
{
printf("\\-%c", v4);
if ( (*((_BYTE *)a1 + 12) & 0xE) != 0 )
printf(" \" \"%s\" \"", dest);
printf(", ");
}
printf("\\-\\-%s", *a1);
v5 = *((_DWORD *)a1 + 3);
if ( (v5 & 0xE) != 0 )
{
printf("=\" \"%s", dest);
v5 = *((_DWORD *)a1 + 3);
if ( (v5 & 0x11) != 0 )
{
printf("\" \"");
v5 = *((_DWORD *)a1 + 3);
}
}
if ( (v5 & 0xF) == 1 || (v5 & 0x10) != 0 )
printf(", \\-\\-no\\-%s", *a1);
puts("\"");
v6 = a1[3];
if ( v6 )
{
v7 = *v6;
v8 = 0;
v9 = 1;
v10 = 0;
v11 = 0;
if ( *v6 )
{
do
{
if ( (char)v7 > 46 )
{
if ( (_BYTE)v7 == 94 )
{
if ( v11 )
putchar(10);
else
printf("\n.B ");
v9 = v11;
v10 = v11;
v11 ^= 1u;
goto LABEL_15;
}
}
else if ( (char)v7 > 9 )
{
switch ( (char)v7 )
{
case 10:
v10 = 0;
putchar(10);
v9 = 1;
goto LABEL_15;
case 32:
if ( !v10 )
{
putchar(32);
v9 = 0;
}
goto LABEL_15;
case 38:
if ( v8 )
putchar(10);
else
printf("\n.I ");
v9 = v8;
v10 = v8;
v8 ^= 1u;
goto LABEL_15;
case 39:
if ( !v9 )
goto LABEL_14;
fprintf(
stderr,
"Error: *roff can't start a line with '''. If you started a line with ''' in the description of the '%s' "
"option, please add some text at the beginning of the sentence, so that a valid manpage can be generated.\n",
*a1);
break;
case 45:
v10 = 0;
printf("\\-");
v9 = 0;
goto LABEL_15;
case 46:
if ( !v9 )
goto LABEL_14;
fprintf(
stderr,
"Error: *roff can't start a line with '.' If you used '&' or '^' to format text in the description of the"
" '%s' option, please add some text before the end of the sentence, so that a valid manpage can be generated.\n",
*a1);
break;
default:
goto LABEL_14;
}
exit(1);
}
LABEL_14:
putchar(v7);
v10 = 0;
v9 = 0;
LABEL_15:
v7 = *++v6;
}
while ( (_BYTE)v7 );
}
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func7(const char **a1)
{
const char *v2; // rsi
const unsigned __int16 **v3; // rax
__int64 v4; // rsi
int v5; // eax
const char *v6; // r14
int v7; // edi
int v8; // er12
int v9; // edx
int v10; // er15
int v11; // ebx
const char *v13; // rbx
int v14; // er12
const __int32_t *v15; // rsi
__int64 v16; // rax
__int64 v17; // rdx
char dest[112]; // [rsp+0h] [rbp-70h] BYREF

if ( (*((_BYTE *)a1 + 12) & 0xE) != 0 )
{
v2 = a1[2];
if ( v2 )
{
strcpy(dest, v2);
}
else
{
v13 = *a1;
v14 = strlen(*a1);
if ( v14 > 0 )
{
v15 = *__ctype_toupper_loc();
v16 = 0LL;
do
{
dest[v16] = v15[v13[v16]];
v17 = v16++;
}
while ( v17 != v14 - 1 );
}
dest[v14] = 0;
}
}
printf(".TP\n.BI \"");
v3 = __ctype_b_loc();
v4 = *((int *)a1 + 2);
if ( ((*v3)[v4] & 0x400) != 0 )
{
printf("\\-%c", v4);
if ( (*((_BYTE *)a1 + 12) & 0xE) != 0 )
printf(" \" \"%s\" \"", dest);
printf(", ");
}
printf("\\-\\-%s", *a1);
v5 = *((_DWORD *)a1 + 3);
if ( (v5 & 0xE) != 0 )
{
printf("=\" \"%s", dest);
v5 = *((_DWORD *)a1 + 3);
if ( (v5 & 0x11) != 0 )
{
printf("\" \"");
v5 = *((_DWORD *)a1 + 3);
}
}
if ( (v5 & 0xF) == 1 || (v5 & 0x10) != 0 )
printf(", \\-\\-no\\-%s", *a1);
puts("\"");
v6 = a1[3];
if ( v6 )
{
v7 = *v6;
v8 = 0;
v9 = 1;
v10 = 0;
v11 = 0;
if ( *v6 )
{
do
{
if ( (char)v7 > 46 )
{
if ( (_BYTE)v7 == 94 )
{
if ( v11 )
putchar(10);
else
printf("\n.B ");
v9 = v11;
v10 = v11;
v11 ^= 1u;
goto LABEL_15;
}
}
else if ( (char)v7 > 9 )
{
switch ( (char)v7 )
{
case 10:
v10 = 0;
putchar(10);
v9 = 1;
goto LABEL_15;
case 32:
if ( !v10 )
{
putchar(32);
v9 = 0;
}
goto LABEL_15;
case 38:
if ( v8 )
putchar(10);
else
printf("\n.I ");
v9 = v8;
v10 = v8;
v8 ^= 1u;
goto LABEL_15;
case 39:
if ( !v9 )
goto LABEL_14;
fprintf(
stderr,
"Error: *roff can't start a line with '''. If you started a line with ''' in the description of the '%s' "
"option, please add some text at the beginning of the sentence, so that a valid manpage can be generated.\n",
*a1);
break;
case 45:
v10 = 0;
printf("\\-");
v9 = 0;
goto LABEL_15;
case 46:
if ( !v9 )
goto LABEL_14;
fprintf(
stderr,
"Error: *roff can't start a line with '.' If you used '&' or '^' to format text in the description of the"
" '%s' option, please add some text before the end of the sentence, so that a valid manpage can be generated.\n",
*a1);
break;
default:
goto LABEL_14;
}
exit(1);
}
LABEL_14:
putchar(v7);
v10 = 0;
v9 = 0;
LABEL_15:
v7 = *++v6;
}
while ( (_BYTE)v7 );
}
}
return putchar(10);
}

original_code:
static void print_option(const NVGetoptOption *o)
{
    char scratch[64];
    const char *s;
    int j, len;
    int italics, bold, omitWhiteSpace, firstchar;
    if (o->flags & (0x0002 | 0x0004 | 0x0008)) {
        if (o->arg_name) {
            strcpy(scratch, o->arg_name);
        } else {
            len = strlen(o->name);
            for (j = 0; j < len; j++) scratch[j] = toupper(o->name[j]);
            scratch[len] = '\0';
        }
    }
    printf(".TP\n.BI \"");
    if (((*__ctype_b_loc ())[(int) ((o->val))] & (unsigned short int) _ISalpha)) {
        printf("\\-%c", o->val);
        if (o->flags & (0x0002 | 0x0004 | 0x0008)) {
            printf(" \" \"%s\" \"", scratch);
        }
        printf(", ");
    }
    printf("\\-\\-%s", o->name);
    if (o->flags & (0x0002 | 0x0004 | 0x0008)) {
        printf("=\" \"%s", scratch);
        if ((o->flags & 0x0001) ||
            (o->flags & 0x0010)) {
            printf("\" \"");
        }
    }
    if (((o->flags & 0x0001) &&
         !(o->flags & (0x0002 | 0x0004 | 0x0008))) ||
        (o->flags & 0x0010)) {
        printf(", \\-\\-no\\-%s", o->name);
    }
    printf("\"\n");
    italics = 0;
    bold = 0;
    omitWhiteSpace = 0;
    firstchar = 1;
    for (s = o->description; s && *s; s++) {
        switch (*s) {
          case '&':
              if (italics) {
                  printf("\n");
              } else {
                  printf("\n.I ");
              }
              omitWhiteSpace = italics;
              firstchar = italics;
              italics = !italics;
              break;
          case '^':
              if (bold) {
                  printf("\n");
              } else {
                  printf("\n.B ");
              }
              omitWhiteSpace = bold;
              firstchar = bold;
              bold = !bold;
              break;
          case '-':
              printf("\\-");
              omitWhiteSpace = 0;
              firstchar = 0;
              break;
          case ' ':
              if (!omitWhiteSpace) {
                  printf(" ");
                  firstchar = 0;
              }
              break;
          case '\n':
              printf("\n");
              omitWhiteSpace = 0;
              firstchar = 1;
              break;
          case '.':
              if (firstchar) {
                  fprintf(stderr, "Error: *roff can't start a line with '.' "
                          "If you used '&' or '^' to format text in the "
                          "description of the '%s' option, please add some "
                          "text before the end of the sentence, so that a "
                          "valid manpage can be generated.\n", o->name);
                  exit(1);
              }
          case '\'':
              if (firstchar) {
                  fprintf(stderr, "Error: *roff can't start a line with '\''. "
                          "If you started a line with '\'' in the description "
                          "of the '%s' option, please add some text at the "
                          "beginning of the sentence, so that a valid manpage "
                          "can be generated.\n", o->name);
                  exit(1);
              }
          default:
              printf("%c", *s);
              omitWhiteSpace = 0;
              firstchar = 0;
              break;
        }
    }
    printf("\n");
}

canonical_original_code:
static void print_option(struct NVGetoptOption *o)
{
    char scratch[64];
    char *s;
    int j; int len;
    int italics; int bold; int omitWhiteSpace; int firstchar;
    if (o->flags & (0x0002 | 0x0004 | 0x0008)) {
        if (o->arg_name) {
            strcpy(scratch, o->arg_name);
        } else {
            len = strlen(o->name);
            for (j = 0; j < len; j++) scratch[j] = toupper(o->name[j]);
            scratch[len] = '\0';
        }
    }
    printf(".TP\n.BI \"");
    if (((*__ctype_b_loc ())[(int) ((o->val))] & (unsigned short) _ISalpha)) {
        printf("\\-%c", o->val);
        if (o->flags & (0x0002 | 0x0004 | 0x0008)) {
            printf(" \" \"%s\" \"", scratch);
        }
        printf(", ");
    }
    printf("\\-\\-%s", o->name);
    if (o->flags & (0x0002 | 0x0004 | 0x0008)) {
        printf("=\" \"%s", scratch);
        if ((o->flags & 0x0001) ||
            (o->flags & 0x0010)) {
            printf("\" \"");
        }
    }
    if (((o->flags & 0x0001) &&
         !(o->flags & (0x0002 | 0x0004 | 0x0008))) ||
        (o->flags & 0x0010)) {
        printf(", \\-\\-no\\-%s", o->name);
    }
    printf("\"\n");
    italics = 0;
    bold = 0;
    omitWhiteSpace = 0;
    firstchar = 1;
    for (s = o->description; s && *s; s++) {
        switch (*s) {
          case '&':
              if (italics) {
                  printf("\n");
              } else {
                  printf("\n.I ");
              }
              omitWhiteSpace = italics;
              firstchar = italics;
              italics = !italics;
              break;
          case '^':
              if (bold) {
                  printf("\n");
              } else {
                  printf("\n.B ");
              }
              omitWhiteSpace = bold;
              firstchar = bold;
              bold = !bold;
              break;
          case '-':
              printf("\\-");
              omitWhiteSpace = 0;
              firstchar = 0;
              break;
          case ' ':
              if (!omitWhiteSpace) {
                  printf(" ");
                  firstchar = 0;
              }
              break;
          case '\n':
              printf("\n");
              omitWhiteSpace = 0;
              firstchar = 1;
              break;
          case '.':
              if (firstchar) {
                  fprintf(stderr, "Error: *roff can't start a line with '.' "
                          "If you used '&' or '^' to format text in the "
                          "description of the '%s' option, please add some "
                          "text before the end of the sentence, so that a "
                          "valid manpage can be generated.\n", o->name);
                  exit(1);
              }
          case '\'':
              if (firstchar) {
                  fprintf(stderr, "Error: *roff can't start a line with '\''. "
                          "If you started a line with '\'' in the description "
                          "of the '%s' option, please add some text at the "
                          "beginning of the sentence, so that a valid manpage "
                          "can be generated.\n", o->name);
                  exit(1);
              }
          default:
              printf("%c", *s);
              omitWhiteSpace = 0;
              firstchar = 0;
              break;
        }
    }
    printf("\n");
}

variable_types: struct NVGetoptOption { char *name; int val; unsigned int flags; char *arg_name; char *description; } *

variable_types: j int

variable_types: italics int

variable_types: bold int

variable_types: omitWhiteSpace int

variable_types: firstchar int


decompiled_code:
int __fastcall gen_manpage_opts_helper(const char **a1)
{
const char **v1; // r12
int result; // eax
const char **v3; // rbx
int v4; // er13

v1 = a1;
result = puts(".SH OPTIONS");
if ( !*a1 )
return result;
v3 = a1;
v4 = 0;
do
{
while ( 1 )
{
if ( !v3[3] )
goto LABEL_4;
if ( (*((_BYTE *)v3 + 12) & 0x40) == 0 )
break;
result = print_option(v3);
LABEL_4:
v3 += 4;
if ( !*v3 )
goto LABEL_8;
}
v3 += 4;
v4 = 1;
}
while ( *v3 );
LABEL_8:
if ( v4 )
{
result = puts(".SH \"ADVANCED OPTIONS\"");
if ( *a1 )
{
do
{
if ( v1[3] )
{
if ( (*((_BYTE *)v1 + 12) & 0x40) == 0 )
result = print_option(v1);
}
v1 += 4;
}
while ( *v1 );
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(const char **a1)
{
const char **v1; // r12
int result; // eax
const char **v3; // rbx
int v4; // er13

v1 = a1;
result = puts(".SH OPTIONS");
if ( !*a1 )
return result;
v3 = a1;
v4 = 0;
do
{
while ( 1 )
{
if ( !v3[3] )
goto LABEL_4;
if ( (*((_BYTE *)v3 + 12) & 0x40) == 0 )
break;
result = func7(v3);
LABEL_4:
v3 += 4;
if ( !*v3 )
goto LABEL_8;
}
v3 += 4;
v4 = 1;
}
while ( *v3 );
LABEL_8:
if ( v4 )
{
result = puts(".SH \"ADVANCED OPTIONS\"");
if ( *a1 )
{
do
{
if ( v1[3] )
{
if ( (*((_BYTE *)v1 + 12) & 0x40) == 0 )
result = func7(v1);
}
v1 += 4;
}
while ( *v1 );
}
}
return result;
}

original_code:
void gen_manpage_opts_helper(const NVGetoptOption *options)
{
    int i;
    int has_advanced_options = 0;
    printf(".SH OPTIONS\n");
    for (i = 0; options[i].name; i++) {
        const NVGetoptOption *o = &options[i];
        if (!o->description) {
            continue;
        }
        if (!(o->flags & 0x0040)) {
            has_advanced_options = 1;
            continue;
        }
        print_option(o);
    }
    if (has_advanced_options) {
        printf(".SH \"ADVANCED OPTIONS\"\n");
        for (i = 0; options[i].name; i++) {
            const NVGetoptOption *o = &options[i];
            if (!o->description) {
                continue;
            }
            if (o->flags & 0x0040) {
                continue;
            }
            print_option(o);
        }
    }
}

canonical_original_code:
void gen_manpage_opts_helper(struct NVGetoptOption *options)
{
    int i;
    int has_advanced_options = 0;
    printf(".SH OPTIONS\n");
    for (i = 0; options[i].name; i++) {
        struct NVGetoptOption *o = &options[i];
        if (!o->description) {
            continue;
        }
        if (!(o->flags & 0x0040)) {
            has_advanced_options = 1;
            continue;
        }
        print_option(o);
    }
    if (has_advanced_options) {
        printf(".SH \"ADVANCED OPTIONS\"\n");
        for (i = 0; options[i].name; i++) {
            struct NVGetoptOption *o = &options[i];
            if (!o->description) {
                continue;
            }
            if (o->flags & 0x0040) {
                continue;
            }
            print_option(o);
        }
    }
}

variable_types: i int

user_defined_types: struct NVGetoptOption { char *name; int val; unsigned int flags; char *arg_name; char *description; }


decompiled_code:
int __fastcall print_range(float *a1, int a2)
{
float *v2; // rbx
double v3; // xmm0_8
double v4; // xmm1_8

if ( a2 > 0 )
{
v2 = a1;
while ( 1 )
{
v3 = v2[1];
v4 = *v2;
v2 += 2;
printf("%.1f-%.1f", v3, v4);
if ( v2 == &a1[2 * (a2 - 1) + 2] )
break;
printf(", ");
}
}
return putchar(10);
}

original_code:
static void print_range(parser_range* range, int n)
{
    int i;
    for (i = 0; i < n; i++) {
        if (i > 0) printf(", ");
        printf("%.1f-%.1f", range[i].lo, range[i].hi);
    }
    printf("\n");
}

canonical_original_code:
static void print_range(struct parser_range *range, int n)
{
    int i;
    for (i = 0; i < n; i++) {
        if (i > 0) printf(", ");
        printf("%.1f-%.1f", range[i].lo, range[i].hi);
    }
    printf("\n");
}

variable_types:
range
struct parser_range { float hi; } *

variable_types: n int


decompiled_code:
_BOOL8 __fastcall nvpci_dev_is_vga(__int64 a1)
{
return (*(_DWORD *)(a1 + 16) & 0xFFFF00) == 196608;
}

original_code:
int nvpci_dev_is_vga(struct pci_device *dev)
{
    return (dev->device_class & PCI_CLASS_SUBCLASS_MASK) ==
           PCI_CLASS_DISPLAY_VGA;
}

variable_types: struct pci_device { unsigned short domain_16; unsigned char bus; unsigned char dev; unsigned char func; unsigned short vendor_id; unsigned short device_id; unsigned short subvendor_id; unsigned short subdevice_id; unsigned int device_class; unsigned char revision; struct pci_mem_region { void *memory; unsigned long bus_addr; unsigned long base_addr; unsigned long size; unsigned int is_IO; unsigned int is_prefetchable; unsigned int is_64; } regions[6]; unsigned long rom_size; int irq; long user_data; int vgaarb_rsrc; unsigned int domain; } *

user_defined_types: struct pci_device { unsigned short domain_16; unsigned char bus; unsigned char dev; unsigned char func; unsigned short vendor_id; unsigned short device_id; unsigned short subvendor_id; unsigned short subdevice_id; unsigned int device_class; unsigned char revision; struct pci_mem_region regions[6]; unsigned long rom_size; int irq; long user_data; int vgaarb_rsrc; unsigned int domain; }


original_code:
static char *display_device_mask_to_display_device_name(unsigned int mask)
{
    char *s;
    int first = 1;
    unsigned int devcnt, devmask;
    char *display_device_name_string;
    display_device_name_string = nvalloc(256);
    s = display_device_name_string;
    devmask = 1 << 0;
    devcnt = 0;
    while (devmask & (0xff << 0)) {
        if (devmask & mask) {
            if (first) first = 0;
            else s += sprintf(s, ", ");
            s += sprintf(s, "CRT-%X", devcnt);
        }
        devmask <<= 1;
        devcnt++;
    }
    devmask = 1 << 16;
    devcnt = 0;
    while (devmask & (0xff << 16)) {
        if (devmask & mask) {
            if (first) first = 0;
            else s += sprintf(s, ", ");
            s += sprintf(s, "DFP-%X", devcnt);
        }
        devmask <<= 1;
        devcnt++;
    }
    devmask = 1 << 8;
    devcnt = 0;
    while (devmask & (0xff << 8)) {
        if (devmask & mask) {
            if (first) first = 0;
            else s += sprintf(s, ", ");
            s += sprintf(s, "TV-%X", devcnt);
        }
        devmask <<= 1;
        devcnt++;
    }
    *s = '\0';
    return (display_device_name_string);
}

canonical_original_code:
static char *display_device_mask_to_display_device_name(unsigned int mask)
{
    char *s;
    int first = 1;
    unsigned int devcnt; unsigned int devmask;
    char *display_device_name_string;
    display_device_name_string = nvalloc(256);
    s = display_device_name_string;
    devmask = 1 << 0;
    devcnt = 0;
    while (devmask & (0xff << 0)) {
        if (devmask & mask) {
            if (first) first = 0;
            else s += sprintf(s, ", ");
            s += sprintf(s, "CRT-%X", devcnt);
        }
        devmask <<= 1;
        devcnt++;
    }
    devmask = 1 << 16;
    devcnt = 0;
    while (devmask & (0xff << 16)) {
        if (devmask & mask) {
            if (first) first = 0;
            else s += sprintf(s, ", ");
            s += sprintf(s, "DFP-%X", devcnt);
        }
        devmask <<= 1;
        devcnt++;
    }
    devmask = 1 << 8;
    devcnt = 0;
    while (devmask & (0xff << 8)) {
        if (devmask & mask) {
            if (first) first = 0;
            else s += sprintf(s, ", ");
            s += sprintf(s, "TV-%X", devcnt);
        }
        devmask <<= 1;
        devcnt++;
    }
    *s = '\0';
    return (display_device_name_string);
}

variable_types:
mask
unsigned int

variable_types:
s
char *

variable_types: first int

variable_types:
devcnt
unsigned int

variable_types:
devmask
unsigned int


decompiled_code:
char *__fastcall cook_description(const char *a1)
{
const char *v1; // rbx
char *v2; // r8
char v3; // al
char *i; // rdx

if ( !a1 )
return 0LL;
v1 = a1;
v2 = strdup(a1);
if ( !v2 )
return 0LL;
v3 = *a1;
for ( i = v2; v3; ++v1 )
{
if ( v3 != 38 && v3 != 94 )
*i++ = v3;
v3 = v1[1];
}
*i = 0;
return v2;
}

canonical_decompiled_code:
char *__fastcall func0(const char *a1)
{
const char *v1; // rbx
char *v2; // r8
char v3; // al
char *i; // rdx

if ( !a1 )
return 0LL;
v1 = a1;
v2 = strdup(a1);
if ( !v2 )
return 0LL;
v3 = *a1;
for ( i = v2; v3; ++v1 )
{
if ( v3 != 38 && v3 != 94 )
*i++ = v3;
v3 = v1[1];
}
*i = 0;
return v2;
}

original_code:
static char *cook_description(const char *description)
{
    const char *src;
    char *s, *dst;
    if (!description) {
        return ((void *)0);
    }
    s = strdup(description);
    if (!s) {
        return ((void *)0);
    }
    for (src = description, dst = s; *src; src++) {
        if ((*src == '&') || (*src == '^')) {
            continue;
        }
        *dst = *src;
        dst++;
    }
    *dst = '\0';
    return s;
}

canonical_original_code:
static char *cook_description(char *description)
{
    char *src;
    char *s; char *dst;
    if (!description) {
        return ((void *)0);
    }
    s = strdup(description);
    if (!s) {
        return ((void *)0);
    }
    for (src = description, dst = s; *src; src++) {
        if ((*src == '&') || (*src == '^')) {
            continue;
        }
        *dst = *src;
        dst++;
    }
    *dst = '\0';
    return s;
}

variable_types:
description
char *

variable_types:
src
char *

variable_types:
dst
char *


nvgetopt

decompiled_code:
__int64 __fastcall nvgetopt(int a1, const char **a2, __int64 a3, char **a4, int *a5, _DWORD *a6, double *a7, int *a8)
{
int v9; // eax
const char *v10; // r15
char *v11; // rax
char *v12; // r13
char *v13; // rcx
const char *v14; // rsi
const char *v15; // rdx
int v16; // er15
unsigned int v17; // er12
int v19; // ecx
const char *v20; // r8
const char *v21; // r12
char v22; // al
const char *v23; // rdx
const char *v24; // r11
const char *v25; // rsi
const char *v26; // r15
__int64 v27; // r12
bool v28; // r13
int v29; // er14
const char *v30; // rdi
int v31; // ebx
__int64 v32; // rbx
int v33; // edx
int v34; // edx
const char *v35; // rsi
__int64 v36; // rax
const char *v37; // rcx
const char *v38; // r8
char v39; // al
char v40; // al
int v41; // eax
__int64 v42; // [rsp+0h] [rbp-A0h]
const char *v43; // [rsp+10h] [rbp-90h]
const char *v44; // [rsp+18h] [rbp-88h]
const char *v45; // [rsp+20h] [rbp-80h]
char *v46; // [rsp+28h] [rbp-78h]
int v48; // [rsp+38h] [rbp-68h]
const char *v49; // [rsp+38h] [rbp-68h]
char v50; // [rsp+42h] [rbp-5Eh]
char v51; // [rsp+43h] [rbp-5Dh]
char *endptr[7]; // [rsp+68h] [rbp-38h] BYREF

if ( a4 )
*a4 = 0LL;
if ( a5 )
*a5 = 0;
if ( a6 )
*a6 = 0;
if ( a7 )
*a7 = 0.0;
if ( a8 )
*a8 = 0;
v48 = argv_index_0;
v9 = argv_index_0 + 1;
argv_index_0 = v9;
if ( v9 >= a1 )
return (unsigned int)-1;
v10 = a2[v9];
v11 = strdup(v10);
v12 = v11;
if ( *v11 != 45 )
{
v13 = v11;
v14 = "%s: invalid option: \"%s\"\n";
v15 = *a2;
LABEL_14:
v16 = 0;
v17 = 0;
fprintf(stderr, v14, v15, v13);
goto LABEL_15;
}
v19 = v11[1];
if ( v11[1] == 45 )
{
v19 = v11[2];
v20 = v11 + 2;
if ( !v11[2] )
{
v16 = 0;
v17 = -1;
goto LABEL_15;
}
}
else
{
v20 = v11 + 1;
if ( !(_BYTE)v19 )
goto LABEL_68;
}
v21 = v20;
v22 = v19;
do
{
v23 = v21++;
if ( v22 == 61 )
{
*v23 = 0;
v19 = *v20;
if ( !*v20 )
goto LABEL_68;
v24 = *(const char **)a3;
v51 = v20[1];
if ( v51 )
goto LABEL_26;
LABEL_38:
if ( v24 )
{
v32 = a3;
do
{
if ( (char)v19 == *(_DWORD *)(v32 + 8) )
{
v33 = *(_DWORD *)(v32 + 12);
v16 = 0;
goto LABEL_43;
}
v32 += 32LL;
}
while ( *(_QWORD *)v32 );
LABEL_56:
v34 = *v20;
if ( *v20 )
{
v35 = v20;
do
{
v36 = a3 + 32;
while ( *(_DWORD *)(v36 - 24) != v34 )
{
v36 += 32LL;
if ( !*(_QWORD *)(v36 - 32) )
goto LABEL_66;
}
v34 = *++v35;
}
while ( (_BYTE)v34 );
v32 = a3;
while ( *(_DWORD *)(v32 + 8) != v19 )
{
v32 += 32LL;
if ( !*(_QWORD *)v32 )
goto LABEL_66;
}
v33 = *(_DWORD *)(v32 + 12);
if ( (v33 & 0xE) == 0 )
{
v39 = *v10;
if ( *v10 == 45 )
{
v39 = v10[1];
if ( v39 == 45 )
{
v39 = v10[2];
v10 += 2;
}
else
{
++v10;
}
}
if ( v39 == 43 )
v39 = *++v10;
if ( v39 )
{
do
{
v40 = *++v10;
*((_BYTE *)v10 - 1) = v40;
}
while ( v40 );
v33 = *(_DWORD *)(v32 + 12);
}
v16 = 0;
argv_index_0 = v48;
goto LABEL_43;
}
LABEL_66:
if ( a6 )
{
if ( v51 )
{
v21 = v20 + 1;
v49 = v20;
strtol(v20 + 1, endptr, 0);
if ( !*endptr[0] )
{
if ( *(_QWORD *)a3 )
{
while ( 1 )
{
v33 = *(_DWORD *)(a3 + 12);
if ( (v33 & 4) != 0 && *(_DWORD *)(a3 + 8) == *v49 )
break;
a3 += 32LL;
if ( !*(_QWORD *)a3 )
goto LABEL_68;
}
v32 = a3;
v16 = 0;
goto LABEL_43;
}
}
}
}
}
LABEL_68:
v13 = v12;
v14 = "%s: unrecognized option: \"%s\"\n";
v15 = *a2;
goto LABEL_14;
}
LABEL_104:
if ( *v20 )
goto LABEL_66;
goto LABEL_68;
}
v22 = *v21;
}
while ( *v21 );
v24 = *(const char **)a3;
v21 = 0LL;
v51 = v20[1];
if ( !v51 )
goto LABEL_38;
LABEL_26:
if ( !v24 )
goto LABEL_104;
v46 = v12;
v25 = v24;
v43 = v10;
v26 = v20;
v45 = v21;
v27 = a3;
v44 = v20 + 3;
v50 = v19;
v28 = v51 == 111 && (_BYTE)v19 == 110;
v42 = a3;
while ( 1 )
{
v29 = *(_DWORD *)(v27 + 12);
v30 = v26;
v31 = 0;
if ( (v29 & 0x11) != 0 && v28 )
{
if ( v26[2] == 45 )
v30 = v44;
if ( v26[2] == 45 )
v31 = 1;
}
if ( !strcmp(v30, v25) )
break;
v25 = *(const char **)(v27 + 32);
v27 += 32LL;
if ( !v25 )
{
v20 = v26;
v19 = v50;
v12 = v46;
v10 = v43;
v21 = v45;
a3 = v42;
goto LABEL_56;
}
}
v16 = v31;
v12 = v46;
v32 = v27;
LOBYTE(v33) = v29;
v21 = v45;
LABEL_43:
if ( a5 && (v33 & 1) != 0 )
{
*a5 = v16 ^ 1;
v33 = *(_DWORD *)(v32 + 12);
}
if ( (v33 & 0x10) != 0 )
{
if ( (v33 & 0xE) == 0 || v16 )
goto LABEL_75;
LABEL_48:
if ( v21 )
{
if ( *v21 )
{
LABEL_50:
if ( (v33 & 4) != 0 && a6 )
{
*a6 = strtol(v21, endptr, 0);
v16 = 0;
if ( !*endptr[0] )
goto LABEL_76;
}
else
{
if ( (v33 & 2) != 0 && a4 )
{
*a4 = strdup(v21);
v16 = 0;
goto LABEL_76;
}
if ( (v33 & 8) == 0 || !a7 )
{
v13 = v12;
v14 = "%s: error while assigning argument for option \"%s\".\n";
v15 = *a2;
goto LABEL_14;
}
v16 = 0;
*a7 = strtod(v21, endptr);
if ( !*endptr[0] )
goto LABEL_76;
}
v37 = v21;
v17 = 0;
fprintf(stderr, "%s: \"%s\" is not a valid argument for option \"%s\".\n", *a2, v37, v12);
goto LABEL_15;
}
}
else
{
if ( (v33 & 0x20) != 0 && (a1 - 1 == argv_index_0 || *a2[argv_index_0 + 1] == 45) )
{
v16 = 0;
goto LABEL_76;
}
v41 = argv_index_0 + 1;
argv_index_0 = v41;
if ( v41 < a1 )
{
v21 = a2[v41];
goto LABEL_50;
}
}
v13 = v12;
v14 = "%s: option \"%s\" requires an argument.\n";
v15 = *a2;
goto LABEL_14;
}
v16 = 0;
if ( (v33 & 0xE) != 0 )
goto LABEL_48;
LABEL_75:
if ( v21 )
{
v38 = v21;
v17 = 0;
fprintf(
stderr,
"%s: option \"%s\" does not take an argument, but was given an argument of \"%s\".\n",
*a2,
v12,
v38);
}
else
{
LABEL_76:
v17 = *(_DWORD *)(v32 + 8);
}
LABEL_15:
if ( a8 )
*a8 = v16;
free(v12);
return v17;
}

original_code:
int nvgetopt(int argc,
             char *argv[],
             const NVGetoptOption *options,
             char **strval,
             int *boolval,
             int *intval,
             double *doubleval,
             int *disable_val)
{
    char *c, *a, *arg, *name = ((void *)0), *argument=((void *)0);
    int i, found = 0;
    int ret = 0;
    int negate = 0;
    int disable = 0;
    int double_dash = 0;
    const NVGetoptOption *o = ((void *)0);
    static int argv_index = 0;
    if (strval) *strval = ((void *)0);
    if (boolval) *boolval = 0;
    if (intval) *intval = 0;
    if (doubleval) *doubleval = 0.0;
    if (disable_val) *disable_val = 0;
    argv_index++;
    if (argv_index >= argc) return -1;
    arg = strdup(argv[argv_index]);
    if ((arg[0] == '-') && (arg[1] == '-')) {
        name = arg + 2;
        double_dash = 1;
    } else if (arg[0] == '-') {
        name = arg + 1;
    } else {
        fprintf(stderr, "%s: invalid option: \"%s\"\n", argv[0], arg);
        goto done;
    }
    c = name;
    while (*c) {
        if (*c == '=') {
            argument = c + 1;
            *c = '\0';
            break;
        }
        c++;
    }
    if (name[0] == '\0') {
        if (double_dash && argument == ((void *)0)) {
            ret = -1;
            goto done;
        }
    } else if (name[1] == '\0') {
        for (i = 0; options[i].name; i++) {
            if (options[i].val == name[0]) {
                o = &options[i];
                break;
            }
        }
    } else {
        for (i = 0; options[i].name; i++) {
            const char *tmpname;
            int tmp_negate;
            if ((options[i].flags & (0x0001 |
                                     0x0010)) &&
                (name[0] == 'n') &&
                (name[1] == 'o') &&
                (name[2] == '-')) {
                tmpname = name + 3;
                tmp_negate = 1;
            } else {
                tmpname = name;
                tmp_negate = 0;
            }
            if (strcmp(tmpname, options[i].name) == 0) {
                o = &options[i];
                negate = tmp_negate;
                break;
            }
        }
    }
    if (!o) {
        for (c = name; *c; c++) {
            found = 0;
            for (i = 0; options[i].name; i++) {
                if (options[i].val == *c) {
                    found = 1;
                    break;
                }
            }
            if (!found) break;
        }
        if (found) {
            for (i = 0; options[i].name; i++) {
                if (options[i].val == name[0]) {
                    if (options[i].flags & (0x0002 | 0x0004 | 0x0008)) break;
                    a = argv[argv_index];
                    if (a[0] == '-') a++;
                    if (a[0] == '-') a++;
                    if (a[0] == '+') a++;
                    while (a[0]) { a[0] = a[1]; a++; }
                    argv_index--;
                    o = &options[i];
                    break;
                }
            }
        }
    }
    if (!o && intval) {
        int appendedInteger = 0;
        if ((name[0] != '\0') && (name[1] != '\0')) {
            char *endptr;
            strtol(name + 1, &endptr, 0);
            if (*endptr == '\0') {
                appendedInteger = 1;
            }
        }
        if (appendedInteger) {
            for (i = 0; options[i].name; i++) {
                if ((options[i].flags & 0x0004) == 0) {
                    continue;
                }
                if (options[i].val == name[0]) {
                    o = &options[i];
                    argument = name + 1;
                    break;
                }
            }
        }
    }
    if (!o) {
        fprintf(stderr, "%s: unrecognized option: \"%s\"\n", argv[0], arg);
        goto done;
    }
    if (o->flags & 0x0001) {
        if (boolval) *boolval = !negate;
    }
    if ((o->flags & 0x0010) && (negate == 1)) {
        disable = 1;
    }
    if ((o->flags & (0x0002 | 0x0004 | 0x0008)) && !disable) {
        if (argument) {
            if (!argument[0]) {
                fprintf(stderr, "%s: option \"%s\" requires an "
                        "argument.\n", argv[0], arg);
                goto done;
            }
        } else {
            if ((o->flags & 0x0020) &&
                ((argv_index == (argc - 1)) ||
                 (argv[argv_index + 1][0] == '-'))) {
                argument = ((void *)0);
                goto argument_processing_done;
            } else {
                argv_index++;
                if (argv_index >= argc) {
                    fprintf(stderr, "%s: option \"%s\" requires an "
                            "argument.\n", argv[0], arg);
                    goto done;
                }
                argument = argv[argv_index];
            }
        }
        if ((o->flags & 0x0004) && (intval)) {
            char *endptr;
            *intval = (int) strtol(argument, &endptr, 0);
            if (*endptr) {
                fprintf(stderr, "%s: \"%s\" is not a valid argument for "
                        "option \"%s\".\n", argv[0], argument, arg);
                goto done;
            }
        } else if ((o->flags & 0x0002) && (strval)) {
            *strval = strdup(argument);
        } else if ((o->flags & 0x0008) && (doubleval)) {
            char *endptr;
            *doubleval = (double) strtod(argument, &endptr);
            if (*endptr) {
                fprintf(stderr, "%s: \"%s\" is not a valid argument for "
                        "option \"%s\".\n", argv[0], argument, arg);
                goto done;
            }
        } else {
            fprintf(stderr, "%s: error while assigning argument for "
                    "option \"%s\".\n", argv[0], arg);
            goto done;
        }
    } else {
        if (argument) {
            fprintf(stderr, "%s: option \"%s\" does not take an argument, but "
                    "was given an argument of \"%s\".\n",
                    argv[0], arg, argument);
            goto done;
        }
    }
 argument_processing_done:
    ret = o->val;
 done:
    if (disable_val) *disable_val = disable;
    free(arg);
    return ret;
}

canonical_original_code:
int nvgetopt(int argc, char *argv[], struct NVGetoptOption *options, char **strval, int *boolval, int *intval, double *doubleval, int *disable_val)
{
    char *c; char *a; char *arg; char *name = ((void *)0); char *argument = ((void *)0);
    int i; int found = 0;
    int ret = 0;
    int negate = 0;
    int disable = 0;
    int double_dash = 0;
    struct NVGetoptOption *o = ((void *)0);
    static int argv_index = 0;
    if (strval) *strval = ((void *)0);
    if (boolval) *boolval = 0;
    if (intval) *intval = 0;
    if (doubleval) *doubleval = 0.0;
    if (disable_val) *disable_val = 0;
    argv_index++;
    if (argv_index >= argc) return -1;
    arg = strdup(argv[argv_index]);
    if ((arg[0] == '-') && (arg[1] == '-')) {
        name = arg + 2;
        double_dash = 1;
    } else if (arg[0] == '-') {
        name = arg + 1;
    } else {
        fprintf(stderr, "%s: invalid option: \"%s\"\n", argv[0], arg);
        goto done;
    }
    c = name;
    while (*c) {
        if (*c == '=') {
            argument = c + 1;
            *c = '\0';
            break;
        }
        c++;
    }
    if (name[0] == '\0') {
        if (double_dash && argument == ((void *)0)) {
            ret = -1;
            goto done;
        }
    } else if (name[1] == '\0') {
        for (i = 0; options[i].name; i++) {
            if (options[i].val == name[0]) {
                o = &options[i];
                break;
            }
        }
    } else {
        for (i = 0; options[i].name; i++) {
            char *tmpname;
            int tmp_negate;
            if ((options[i].flags & (0x0001 |
                                     0x0010)) &&
                (name[0] == 'n') &&
                (name[1] == 'o') &&
                (name[2] == '-')) {
                tmpname = name + 3;
                tmp_negate = 1;
            } else {
                tmpname = name;
                tmp_negate = 0;
            }
            if (strcmp(tmpname, options[i].name) == 0) {
                o = &options[i];
                negate = tmp_negate;
                break;
            }
        }
    }
    if (!o) {
        for (c = name; *c; c++) {
            found = 0;
            for (i = 0; options[i].name; i++) {
                if (options[i].val == *c) {
                    found = 1;
                    break;
                }
            }
            if (!found) break;
        }
        if (found) {
            for (i = 0; options[i].name; i++) {
                if (options[i].val == name[0]) {
                    if (options[i].flags & (0x0002 | 0x0004 | 0x0008)) break;
                    a = argv[argv_index];
                    if (a[0] == '-') a++;
                    if (a[0] == '-') a++;
                    if (a[0] == '+') a++;
                    while (a[0]) { a[0] = a[1]; a++; }
                    argv_index--;
                    o = &options[i];
                    break;
                }
            }
        }
    }
    if (!o && intval) {
        int appendedInteger = 0;
        if ((name[0] != '\0') && (name[1] != '\0')) {
            char *endptr;
            strtol(name + 1, &endptr, 0);
            if (*endptr == '\0') {
                appendedInteger = 1;
            }
        }
        if (appendedInteger) {
            for (i = 0; options[i].name; i++) {
                if ((options[i].flags & 0x0004) == 0) {
                    continue;
                }
                if (options[i].val == name[0]) {
                    o = &options[i];
                    argument = name + 1;
                    break;
                }
            }
        }
    }
    if (!o) {
        fprintf(stderr, "%s: unrecognized option: \"%s\"\n", argv[0], arg);
        goto done;
    }
    if (o->flags & 0x0001) {
        if (boolval) *boolval = !negate;
    }
    if ((o->flags & 0x0010) && (negate == 1)) {
        disable = 1;
    }
    if ((o->flags & (0x0002 | 0x0004 | 0x0008)) && !disable) {
        if (argument) {
            if (!argument[0]) {
                fprintf(stderr, "%s: option \"%s\" requires an "
                        "argument.\n", argv[0], arg);
                goto done;
            }
        } else {
            if ((o->flags & 0x0020) &&
                ((argv_index == (argc - 1)) ||
                 (argv[argv_index + 1][0] == '-'))) {
                argument = ((void *)0);
                goto argument_processing_done;
            } else {
                argv_index++;
                if (argv_index >= argc) {
                    fprintf(stderr, "%s: option \"%s\" requires an "
                            "argument.\n", argv[0], arg);
                    goto done;
                }
                argument = argv[argv_index];
            }
        }
        if ((o->flags & 0x0004) && (intval)) {
            char *endptr;
            *intval = (int) strtol(argument, &endptr, 0);
            if (*endptr) {
                fprintf(stderr, "%s: \"%s\" is not a valid argument for "
                        "option \"%s\".\n", argv[0], argument, arg);
                goto done;
            }
        } else if ((o->flags & 0x0002) && (strval)) {
            *strval = strdup(argument);
        } else if ((o->flags & 0x0008) && (doubleval)) {
            char *endptr;
            *doubleval = (double) strtod(argument, &endptr);
            if (*endptr) {
                fprintf(stderr, "%s: \"%s\" is not a valid argument for "
                        "option \"%s\".\n", argv[0], argument, arg);
                goto done;
            }
        } else {
            fprintf(stderr, "%s: error while assigning argument for "
                    "option \"%s\".\n", argv[0], arg);
            goto done;
        }
    } else {
        if (argument) {
            fprintf(stderr, "%s: option \"%s\" does not take an argument, but "
                    "was given an argument of \"%s\".\n",
                    argv[0], arg, argument);
            goto done;
        }
    }
 argument_processing_done:
    ret = o->val;
 done:
    if (disable_val) *disable_val = disable;
    free(arg);
    return ret;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct NVGetoptOption { char *name; int val; unsigned int flags; char *arg_name; char *description; } *

variable_types:
strval
char **

variable_types:
boolval
int *

variable_types:
intval
int *

variable_types:
doubleval
double *

variable_types:
c
char *

variable_types:
a
char *

variable_types:
arg
char *

variable_types:
name
char *

variable_types:
argument
char *

variable_types: found int

variable_types: ret int

variable_types: negate int

variable_types: disable int

variable_types:
tmpname
char *

variable_types: appendedInteger int

variable_types:
endptr
char *


original_code:
void nvgetopt_print_help(const NVGetoptOption *options,
                         unsigned int include_mask,
                         nvgetopt_print_help_callback_ptr callback)
{
    const NVGetoptOption *o;
    int i;
    for (i = 0; options[i].name; i++) {
        char *msg = ((void *)0), *arg = ((void *)0), *description = ((void *)0);
        o = &options[i];
        if (!o->description) {
            continue;
        }
        if ((o->flags & include_mask) != include_mask) {
            continue;
        }
        arg = ((void *)0);
        if (o->flags & (0x0002 | 0x0004 | 0x0008)) {
            if (o->arg_name) {
                arg = strdup(o->arg_name);
            } else {
                char *tmp;
                arg = strdup(o->name);
                for (tmp = arg; tmp && *tmp; tmp++) {
                    *tmp = toupper(*tmp);
                }
            }
        }
        msg = ((void *)0);
        if (arg) {
            msg = nvstrcat("--", o->name, "=", arg, ((void *)0));
        } else {
            msg = nvstrcat("--", o->name, ((void *)0));
        }
        if (o->val <= (0x7f * 2 + 1) && o->val >= 0 && ((*__ctype_b_loc ())[(int) ((o->val))] & (unsigned short int) _ISalpha)) {
            char scratch[16];
            char *tmp;
            snprintf(scratch, sizeof(scratch), "%c", o->val);
            if (arg) {
                tmp = nvstrcat("-", scratch, " ", arg, ", ", msg, ((void *)0));
            } else {
                tmp = nvstrcat("-", scratch, ", ", msg, ((void *)0));
            }
            free(msg);
            msg = tmp;
        }
        if (((o->flags & 0x0001) &&
             !(o->flags & (0x0002 | 0x0004 | 0x0008))) ||
            (o->flags & 0x0010)) {
            char *tmp = nvstrcat(msg, ", --no-", o->name, ((void *)0));
            free(msg);
            msg = tmp;
        }
        description = cook_description(o->description);
        callback(msg, description);
        free(msg);
        free(arg);
        free(description);
    }
}

canonical_original_code:
void nvgetopt_print_help(struct NVGetoptOption *options, unsigned int include_mask, void callback(char *name, char *description))
{
    struct NVGetoptOption *o;
    int i;
    for (i = 0; options[i].name; i++) {
        char *msg = ((void *)0); char *arg = ((void *)0); char *description = ((void *)0);
        o = &options[i];
        if (!o->description) {
            continue;
        }
        if ((o->flags & include_mask) != include_mask) {
            continue;
        }
        arg = ((void *)0);
        if (o->flags & (0x0002 | 0x0004 | 0x0008)) {
            if (o->arg_name) {
                arg = strdup(o->arg_name);
            } else {
                char *tmp;
                arg = strdup(o->name);
                for (tmp = arg; tmp && *tmp; tmp++) {
                    *tmp = toupper(*tmp);
                }
            }
        }
        msg = ((void *)0);
        if (arg) {
            msg = nvstrcat("--", o->name, "=", arg, ((void *)0));
        } else {
            msg = nvstrcat("--", o->name, ((void *)0));
        }
        if (o->val <= (0x7f * 2 + 1) && o->val >= 0 && ((*__ctype_b_loc ())[(int) ((o->val))] & (unsigned short) _ISalpha)) {
            char scratch[16];
            char *tmp;
            snprintf(scratch, sizeof(scratch), "%c", o->val);
            if (arg) {
                tmp = nvstrcat("-", scratch, " ", arg, ", ", msg, ((void *)0));
            } else {
                tmp = nvstrcat("-", scratch, ", ", msg, ((void *)0));
            }
            free(msg);
            msg = tmp;
        }
        if (((o->flags & 0x0001) &&
             !(o->flags & (0x0002 | 0x0004 | 0x0008))) ||
            (o->flags & 0x0010)) {
            char *tmp = nvstrcat(msg, ", --no-", o->name, ((void *)0));
            free(msg);
            msg = tmp;
        }
        description = cook_description(o->description);
        callback(msg, description);
        free(msg);
        free(arg);
        free(description);
    }
}

variable_types: struct NVGetoptOption { char *name; int val; unsigned int flags; char *arg_name; char *description; } *

variable_types:
callback
void (char *name, char *description)

variable_types: i int

variable_types:
msg
char *

variable_types:
description
char *

variable_types:
tmp
char *

variable_types:
scratch
char [16]


decompiled_code:
__int64 __fastcall copy_file(const char *a1, const char *a2, unsigned int a3)
{
int v4; // eax
int v5; // er12
int v6; // er13
unsigned int v7; // er14
void *v9; // rbx
void *v10; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int *v17; // rax
char *v18; // rax
int *v19; // rax
char *v20; // rax
int *v21; // rax
char *v22; // rax
int *v23; // rax
char *v24; // rax
int *v25; // rax
char *v26; // rax
int *v27; // rax
char *v28; // rax
void *addr; // [rsp+8h] [rbp-C8h]
struct stat buf; // [rsp+10h] [rbp-C0h] BYREF

v4 = open(a1, 0);
if ( v4 == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
v7 = 0;
nv_error_msg("Unable to open '%s' for copying (%s)", a1, v14);
return v7;
}
v5 = v4;
v6 = open(a2, 578, a3);
if ( v6 == -1 )
{
v17 = _errno_location();
v7 = 0;
v18 = strerror(*v17);
nv_error_msg("Unable to create '%s' for copying (%s)", a2, v18);
close(v5);
return v7;
}
if ( fstat(v5, &buf) == -1 )
{
v19 = _errno_location();
v20 = strerror(*v19);
v7 = 0;
nv_error_msg("Unable to determine size of '%s' (%s)", a1, v20);
goto LABEL_6;
}
if ( !buf.st_size )
goto LABEL_5;
if ( lseek(v6, buf.st_size - 1, 0) == -1 )
{
v21 = _errno_location();
v7 = 0;
v22 = strerror(*v21);
nv_error_msg("Unable to set file size for '%s' (%s)", a2, v22);
goto LABEL_6;
}
if ( write(v6, &unk_78F, 1uLL) != 1 )
{
v15 = _errno_location();
v7 = 0;
v16 = strerror(*v15);
nv_error_msg("Unable to write file size for '%s' (%s)", a2, v16);
goto LABEL_6;
}
v9 = mmap(0LL, buf.st_size, 1, 1, v5, 0LL);
if ( v9 == (void *)-1LL )
{
v23 = _errno_location();
v24 = strerror(*v23);
v7 = 0;
nv_error_msg("Unable to map source file '%s' for copying (%s)", a1, v24);
goto LABEL_6;
}
v10 = mmap(0LL, buf.st_size, 3, 1, v6, 0LL);
if ( v10 == (void *)-1LL )
{
v25 = _errno_location();
v7 = 0;
v26 = strerror(*v25);
nv_error_msg("Unable to map destination file '%s' for copying (%s)", a2, v26);
goto LABEL_6;
}
addr = v10;
memcpy(v10, v9, buf.st_size);
if ( munmap(v9, buf.st_size) == -1 )
{
v27 = _errno_location();
v28 = strerror(*v27);
v7 = 0;
nv_error_msg("Unable to unmap source file '%s' after copying (%s)", a1, v28);
goto LABEL_6;
}
if ( munmap(addr, buf.st_size) == -1 )
{
v11 = _errno_location();
v7 = 0;
v12 = strerror(*v11);
nv_error_msg("Unable to unmap destination file '%s' after copying (%s)", a2, v12);
}
else
{
LABEL_5:
v7 = 1;
}
LABEL_6:
close(v5);
close(v6);
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, const char *a2, unsigned int a3)
{
int v4; // eax
int v5; // er12
int v6; // er13
unsigned int v7; // er14
void *v9; // rbx
void *v10; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int *v17; // rax
char *v18; // rax
int *v19; // rax
char *v20; // rax
int *v21; // rax
char *v22; // rax
int *v23; // rax
char *v24; // rax
int *v25; // rax
char *v26; // rax
int *v27; // rax
char *v28; // rax
void *addr; // [rsp+8h] [rbp-C8h]
struct stat buf; // [rsp+10h] [rbp-C0h] BYREF

v4 = open(a1, 0);
if ( v4 == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
v7 = 0;
nv_error_msg("Unable to open '%s' for copying (%s)", a1, v14);
return v7;
}
v5 = v4;
v6 = open(a2, 578, a3);
if ( v6 == -1 )
{
v17 = _errno_location();
v7 = 0;
v18 = strerror(*v17);
nv_error_msg("Unable to create '%s' for copying (%s)", a2, v18);
close(v5);
return v7;
}
if ( fstat(v5, &buf) == -1 )
{
v19 = _errno_location();
v20 = strerror(*v19);
v7 = 0;
nv_error_msg("Unable to determine size of '%s' (%s)", a1, v20);
goto LABEL_6;
}
if ( !buf.st_size )
goto LABEL_5;
if ( lseek(v6, buf.st_size - 1, 0) == -1 )
{
v21 = _errno_location();
v7 = 0;
v22 = strerror(*v21);
nv_error_msg("Unable to set file size for '%s' (%s)", a2, v22);
goto LABEL_6;
}
if ( write(v6, &unk_78F, 1uLL) != 1 )
{
v15 = _errno_location();
v7 = 0;
v16 = strerror(*v15);
nv_error_msg("Unable to write file size for '%s' (%s)", a2, v16);
goto LABEL_6;
}
v9 = mmap(0LL, buf.st_size, 1, 1, v5, 0LL);
if ( v9 == (void *)-1LL )
{
v23 = _errno_location();
v24 = strerror(*v23);
v7 = 0;
nv_error_msg("Unable to map source file '%s' for copying (%s)", a1, v24);
goto LABEL_6;
}
v10 = mmap(0LL, buf.st_size, 3, 1, v6, 0LL);
if ( v10 == (void *)-1LL )
{
v25 = _errno_location();
v7 = 0;
v26 = strerror(*v25);
nv_error_msg("Unable to map destination file '%s' for copying (%s)", a2, v26);
goto LABEL_6;
}
addr = v10;
memcpy(v10, v9, buf.st_size);
if ( munmap(v9, buf.st_size) == -1 )
{
v27 = _errno_location();
v28 = strerror(*v27);
v7 = 0;
nv_error_msg("Unable to unmap source file '%s' after copying (%s)", a1, v28);
goto LABEL_6;
}
if ( munmap(addr, buf.st_size) == -1 )
{
v11 = _errno_location();
v7 = 0;
v12 = strerror(*v11);
nv_error_msg("Unable to unmap destination file '%s' after copying (%s)", a2, v12);
}
else
{
LABEL_5:
v7 = 1;
}
LABEL_6:
close(v5);
close(v6);
return v7;
}

original_code:
int copy_file(const char *srcfile, const char *dstfile, mode_t mode)
{
    int src_fd = -1, dst_fd = -1;
    struct stat stat_buf;
    char *src, *dst;
    int ret = 0;
    if ((src_fd = open(srcfile, 00)) == -1) {
        nv_error_msg("Unable to open '%s' for copying (%s)",
                     srcfile, strerror ((*__errno_location ())));
        goto done;
    }
    if ((dst_fd = open(dstfile, 02 | 0100 | 01000, mode)) == -1) {
        nv_error_msg("Unable to create '%s' for copying (%s)",
                     dstfile, strerror ((*__errno_location ())));
        goto done;
    }
    if (fstat(src_fd, &stat_buf) == -1) {
        nv_error_msg("Unable to determine size of '%s' (%s)",
                     srcfile, strerror ((*__errno_location ())));
        goto done;
    }
    if (stat_buf.st_size == 0) {
        ret = 1;
        goto done;
    }
    if (lseek(dst_fd, stat_buf.st_size - 1, 0) == -1) {
        nv_error_msg("Unable to set file size for '%s' (%s)",
                     dstfile, strerror ((*__errno_location ())));
        goto done;
    }
    if (write(dst_fd, "", 1) != 1) {
        nv_error_msg("Unable to write file size for '%s' (%s)",
                     dstfile, strerror ((*__errno_location ())));
        goto done;
    }
    if ((src = mmap(0, stat_buf.st_size, 0x1,
                    0x01, src_fd, 0)) == (void *) -1) {
        nv_error_msg("Unable to map source file '%s' for "
                     "copying (%s)", srcfile, strerror ((*__errno_location ())));
        goto done;
    }
    if ((dst = mmap(0, stat_buf.st_size, 0x1 | 0x2,
                    0x01, dst_fd, 0)) == (void *) -1) {
        nv_error_msg("Unable to map destination file '%s' for "
                     "copying (%s)", dstfile, strerror ((*__errno_location ())));
        goto done;
    }
    memcpy(dst, src, stat_buf.st_size);
    if (munmap (src, stat_buf.st_size) == -1) {
        nv_error_msg("Unable to unmap source file '%s' after "
                     "copying (%s)", srcfile, strerror ((*__errno_location ())));
        goto done;
    }
    if (munmap (dst, stat_buf.st_size) == -1) {
        nv_error_msg("Unable to unmap destination file '%s' after "
                     "copying (%s)", dstfile, strerror ((*__errno_location ())));
        goto done;
    }
    ret = 1;
 done:
    if (src_fd != -1) close(src_fd);
    if (dst_fd != -1) close(dst_fd);
    return ret;
}

canonical_original_code:
int copy_file(char *srcfile, char *dstfile, unsigned int mode)
{
    int src_fd = -1; int dst_fd = -1;
    struct stat stat_buf;
    char *src; char *dst;
    int ret = 0;
    if ((src_fd = open(srcfile, 00)) == -1) {
        nv_error_msg("Unable to open '%s' for copying (%s)",
                     srcfile, strerror ((*__errno_location ())));
        goto done;
    }
    if ((dst_fd = open(dstfile, 02 | 0100 | 01000, mode)) == -1) {
        nv_error_msg("Unable to create '%s' for copying (%s)",
                     dstfile, strerror ((*__errno_location ())));
        goto done;
    }
    if (fstat(src_fd, &stat_buf) == -1) {
        nv_error_msg("Unable to determine size of '%s' (%s)",
                     srcfile, strerror ((*__errno_location ())));
        goto done;
    }
    if (stat_buf.st_size == 0) {
        ret = 1;
        goto done;
    }
    if (lseek(dst_fd, stat_buf.st_size - 1, 0) == -1) {
        nv_error_msg("Unable to set file size for '%s' (%s)",
                     dstfile, strerror ((*__errno_location ())));
        goto done;
    }
    if (write(dst_fd, "", 1) != 1) {
        nv_error_msg("Unable to write file size for '%s' (%s)",
                     dstfile, strerror ((*__errno_location ())));
        goto done;
    }
    if ((src = mmap(0, stat_buf.st_size, 0x1,
                    0x01, src_fd, 0)) == (void *) -1) {
        nv_error_msg("Unable to map source file '%s' for "
                     "copying (%s)", srcfile, strerror ((*__errno_location ())));
        goto done;
    }
    if ((dst = mmap(0, stat_buf.st_size, 0x1 | 0x2,
                    0x01, dst_fd, 0)) == (void *) -1) {
        nv_error_msg("Unable to map destination file '%s' for "
                     "copying (%s)", dstfile, strerror ((*__errno_location ())));
        goto done;
    }
    memcpy(dst, src, stat_buf.st_size);
    if (munmap (src, stat_buf.st_size) == -1) {
        nv_error_msg("Unable to unmap source file '%s' after "
                     "copying (%s)", srcfile, strerror ((*__errno_location ())));
        goto done;
    }
    if (munmap (dst, stat_buf.st_size) == -1) {
        nv_error_msg("Unable to unmap destination file '%s' after "
                     "copying (%s)", dstfile, strerror ((*__errno_location ())));
        goto done;
    }
    ret = 1;
 done:
    if (src_fd != -1) close(src_fd);
    if (dst_fd != -1) close(dst_fd);
    return ret;
}

variable_types:
srcfile
char *

variable_types:
dstfile
char *

variable_types:
mode
unsigned int

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types:
src
char *

variable_types:
dst
char *

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


prependRoot

decompiled_code:
__int64 __fastcall prependRoot(__int64 a1)
{
return a1;
}

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

original_code:
static char *
prependRoot (char *pathname)
{
    return pathname;
}

canonical_original_code:
static char *prependRoot(char *pathname)
{
    return pathname;
}

variable_types:
pathname
char *


original_code:
int add_elem_in_list_with_pos(t_elem *elem_to_add)
{
  int counter;
  t_elem *elem_to_use;
  counter = 0;
  if ((elem_to_use = g_str->list_of_elems) == ((void *)0))
    return (-1);
  while (elem_to_use->next != ((void *)0))
    {
      elem_to_use = elem_to_use->next;
      counter = counter + 1;
    }
  if ((elem_to_add = my_sbrk(sizeof(*elem_to_add))) == (void*)-1)
    return (-1);
  elem_to_add->ptr_free = 0;
  elem_to_add->nbr_of_elem = counter + 1;
  elem_to_add->next = ((void *)0);
  elem_to_add->prev = elem_to_use;
  if (elem_to_use == g_str->list_of_elems)
    {
      g_str->list_of_elems->next = elem_to_add;
      printf("%p\n", g_str->list_of_elems->next);
    }
  else
    elem_to_use->next = elem_to_add;
  g_str->nb_of_elems = g_str->nb_of_elems + 1;
  return (0);
}

canonical_original_code:
int add_elem_in_list_with_pos(struct s_elem *elem_to_add)
{
  int counter;
  struct s_elem *elem_to_use;
  counter = 0;
  if ((elem_to_use = g_str->list_of_elems) == ((void *)0))
    return (-1);
  while (elem_to_use->next != ((void *)0))
    {
      elem_to_use = elem_to_use->next;
      counter = counter + 1;
    }
  if ((elem_to_add = my_sbrk(sizeof(*elem_to_add))) == (void *)-1)
    return (-1);
  elem_to_add->ptr_free = 0;
  elem_to_add->nbr_of_elem = counter + 1;
  elem_to_add->next = ((void *)0);
  elem_to_add->prev = elem_to_use;
  if (elem_to_use == g_str->list_of_elems)
    {
      g_str->list_of_elems->next = elem_to_add;
      printf("%p\n", g_str->list_of_elems->next);
    }
  else
    elem_to_use->next = elem_to_add;
  g_str->nb_of_elems = g_str->nb_of_elems + 1;
  return (0);
}

variable_types: struct s_elem { unsigned long size; int ptr_free; int nbr_of_elem; void *ptr_of_elem; struct s_elem *next; struct s_elem *prev; } *

variable_types: counter int


decompiled_code:
__int64 __fastcall add_elem_in_list(int a1)
{
__int64 *v1; // rbp
__int64 v2; // rax
_QWORD *v3; // rdx
_QWORD *v4; // rax
__int64 result; // rax

if ( a1 )
return add_elem_in_list_with_pos(0LL);
v1 = g_str;
v2 = my_sbrk(40LL);
*v1 = v2;
if ( v2 == -1 )
return 0xFFFFFFFFLL;
v3 = g_str;
v4 = (_QWORD *)*g_str;
v4[3] = 0LL;
v4[4] = 0LL;
*v4 = 0LL;
v4[1] = 0LL;
result = 0LL;
*((_DWORD *)v3 + 2) = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
__int64 *v1; // rbp
__int64 v2; // rax
_QWORD *v3; // rdx
_QWORD *v4; // rax
__int64 result; // rax

if ( a1 )
return func0(0LL);
v1 = g_str;
v2 = my_sbrk(40LL);
*v1 = v2;
if ( v2 == -1 )
return 0xFFFFFFFFLL;
v3 = g_str;
v4 = (_QWORD *)*g_str;
v4[3] = 0LL;
v4[4] = 0LL;
*v4 = 0LL;
v4[1] = 0LL;
result = 0LL;
*((_DWORD *)v3 + 2) = 1;
return result;
}

original_code:
int add_elem_in_list(int elem_position)
{
  t_elem *elem_to_add;
  elem_to_add = ((void *)0);
  if (elem_position == 0)
    {
      if ((g_str->list_of_elems = my_sbrk(sizeof(*g_str->list_of_elems))) == (void*)-1)
 return (-1);
      g_str->list_of_elems->next = ((void *)0);
      g_str->list_of_elems->prev = ((void *)0);
      g_str->list_of_elems->size = 0;
      g_str->list_of_elems->nbr_of_elem = 0;
      g_str->list_of_elems->ptr_free = 0;
      g_str->nb_of_elems = 1;
    }
  else
    return (add_elem_in_list_with_pos(elem_to_add));
  return (0);
}

canonical_original_code:
int add_elem_in_list(int elem_position)
{
  struct s_elem *elem_to_add;
  elem_to_add = ((void *)0);
  if (elem_position == 0)
    {
      if ((g_str->list_of_elems = my_sbrk(sizeof(*g_str->list_of_elems))) == (void *)-1)
 return (-1);
      g_str->list_of_elems->next = ((void *)0);
      g_str->list_of_elems->prev = ((void *)0);
      g_str->list_of_elems->size = 0;
      g_str->list_of_elems->nbr_of_elem = 0;
      g_str->list_of_elems->ptr_free = 0;
      g_str->nb_of_elems = 1;
    }
  else
    return (add_elem_in_list_with_pos(elem_to_add));
  return (0);
}

user_defined_types: struct s_elem { unsigned long size; int ptr_free; int nbr_of_elem; void *ptr_of_elem; struct s_elem *next; struct s_elem *prev; }


original_code:
t_elem *find_elem_in_list(int elem_position)
{
  int counter;
  t_elem *elem_to_use;
  if ((elem_to_use = g_str->list_of_elems) == ((void *)0))
    return (((void *)0));
  counter = 0;
  while (elem_to_use->next != ((void *)0) && counter < elem_position)
    {
      elem_to_use = elem_to_use->next;
      counter = counter + 1;
    }
  if (elem_to_use != ((void *)0) && counter == elem_position)
    return (elem_to_use);
  return (((void *)0));
}

canonical_original_code:
struct s_elem *find_elem_in_list(int elem_position)
{
  int counter;
  struct s_elem *elem_to_use;
  if ((elem_to_use = g_str->list_of_elems) == ((void *)0))
    return (((void *)0));
  counter = 0;
  while (elem_to_use->next != ((void *)0) && counter < elem_position)
    {
      elem_to_use = elem_to_use->next;
      counter = counter + 1;
    }
  if (elem_to_use != ((void *)0) && counter == elem_position)
    return (elem_to_use);
  return (((void *)0));
}

return_type: struct s_elem { unsigned long size; int ptr_free; int nbr_of_elem; void *ptr_of_elem; struct s_elem *next; struct s_elem *prev; } *


decompiled_code:
__int64 __fastcall fill_elem(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax

if ( !a2 || a1 == -1 )
return 0LL;
*(_QWORD *)a2 = a3;
result = a2;
*(_DWORD *)(a2 + 8) = 0;
*(_QWORD *)(a2 + 16) = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax

if ( !a2 || a1 == -1 )
return 0LL;
*(_QWORD *)a2 = a3;
result = a2;
*(_DWORD *)(a2 + 8) = 0;
*(_QWORD *)(a2 + 16) = a1;
return result;
}

original_code:
t_elem *fill_elem(void *ptr_of_elem_to_set,
      t_elem *elem_to_fill, size_t size_to_set)
{
  if (ptr_of_elem_to_set == (void*)-1)
      return (((void *)0));
  if (elem_to_fill == ((void *)0))
    return (((void *)0));
  elem_to_fill->size = size_to_set;
  elem_to_fill->ptr_free = 0;
  elem_to_fill->ptr_of_elem = ptr_of_elem_to_set;
  return (elem_to_fill);
}

canonical_original_code:
struct s_elem *fill_elem(void *ptr_of_elem_to_set, struct s_elem *elem_to_fill, unsigned long size_to_set)
{
  if (ptr_of_elem_to_set == (void *)-1)
      return (((void *)0));
  if (elem_to_fill == ((void *)0))
    return (((void *)0));
  elem_to_fill->size = size_to_set;
  elem_to_fill->ptr_free = 0;
  elem_to_fill->ptr_of_elem = ptr_of_elem_to_set;
  return (elem_to_fill);
}


decompiled_code:
__int64 show_list_of_elems()
{
_QWORD *v0; // rax
__int64 v1; // rbx
int v2; // ebp

v0 = g_str;
v1 = *g_str;
if ( !*g_str )
return 0xFFFFFFFFLL;
v2 = 0;
while ( *((_DWORD *)v0 + 2) > v2 )
{
if ( printf("Adresse du pointeur : %p .\nTaille du pointeur : %lu .\n", *(const void **)(v1 + 16), *(_QWORD *)v1) == -1
|| printf(
"Si le pointeur est free : %d .\nPlace de l'element : %d .\n",
*(unsigned int *)(v1 + 8),
*(unsigned int *)(v1 + 12)) == -1 )
{
return 0xFFFFFFFFLL;
}
v1 = *(_QWORD *)(v1 + 24);
++v2;
if ( !v1 )
break;
v0 = g_str;
}
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
_QWORD *v0; // rax
__int64 v1; // rbx
int v2; // ebp

v0 = g_str;
v1 = *g_str;
if ( !*g_str )
return 0xFFFFFFFFLL;
v2 = 0;
while ( *((_DWORD *)v0 + 2) > v2 )
{
if ( printf("Adresse du pointeur : %p .\nTaille du pointeur : %lu .\n", *(const void **)(v1 + 16), *(_QWORD *)v1) == -1
|| printf(
"Si le pointeur est free : %d .\nPlace de l'element : %d .\n",
*(unsigned int *)(v1 + 8),
*(unsigned int *)(v1 + 12)) == -1 )
{
return 0xFFFFFFFFLL;
}
v1 = *(_QWORD *)(v1 + 24);
++v2;
if ( !v1 )
break;
v0 = g_str;
}
return 0LL;
}

original_code:
int show_list_of_elems()
{
  int counter;
  t_elem *elem_to_use;
  counter = 0;
  if ((elem_to_use = g_str->list_of_elems) == ((void *)0))
    return (-1);
  while (elem_to_use != ((void *)0) && counter < g_str->nb_of_elems)
    {
      if (printf("Adresse du pointeur : %p .\nTaille du pointeur : %lu .\n",
   elem_to_use->ptr_of_elem, elem_to_use->size) == -1
   || printf("Si le pointeur est free : %d .\nPlace de l'element : %d .\n",
      elem_to_use->ptr_free, elem_to_use->nbr_of_elem) == -1)
    return (-1);
      elem_to_use = elem_to_use->next;
      counter = counter + 1;
    }
  return (0);
}

canonical_original_code:
int show_list_of_elems()
{
  int counter;
  struct s_elem *elem_to_use;
  counter = 0;
  if ((elem_to_use = g_str->list_of_elems) == ((void *)0))
    return (-1);
  while (elem_to_use != ((void *)0) && counter < g_str->nb_of_elems)
    {
      if (printf("Adresse du pointeur : %p .\nTaille du pointeur : %lu .\n",
   elem_to_use->ptr_of_elem, elem_to_use->size) == -1
   || printf("Si le pointeur est free : %d .\nPlace de l'element : %d .\n",
      elem_to_use->ptr_free, elem_to_use->nbr_of_elem) == -1)
    return (-1);
      elem_to_use = elem_to_use->next;
      counter = counter + 1;
    }
  return (0);
}


decompiled_code:
__int64 init_malloc()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx
void *v2; // rax

v0 = sbrk(40LL);
v0[1] = 0LL;
v1 = v0;
g_str = v0;
*v0 = 0LL;
*((_DWORD *)v0 + 4) = getpagesize();
v2 = sbrk(0LL);
v1[3] = v2;
if ( v2 == (void *)-1LL )
return 0xFFFFFFFFLL;
*(_QWORD *)(g_str + 32LL) = *(_QWORD *)(g_str + 24LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx
void *v2; // rax

v0 = sbrk(40LL);
v0[1] = 0LL;
v1 = v0;
g_str = v0;
*v0 = 0LL;
*((_DWORD *)v0 + 4) = getpagesize();
v2 = sbrk(0LL);
v1[3] = v2;
if ( v2 == (void *)-1LL )
return 0xFFFFFFFFLL;
*(_QWORD *)(g_str + 32LL) = *(_QWORD *)(g_str + 24LL);
return 0LL;
}

original_code:
int init_malloc()
{
  g_str = sbrk(sizeof(*g_str));
  g_str->list_of_elems = ((void *)0);
  g_str->nb_of_elems = 0;
  g_str->nb_of_pages = 0;
  g_str->pages_size = getpagesize();
  if ((g_str->begin_process_break_limit = sbrk(0)) == (void*)-1)
    return (-1);
  g_str->allocated_memory = g_str->begin_process_break_limit;
  return (0);
}

canonical_original_code:
int init_malloc()
{
  g_str = sbrk(sizeof(*g_str));
  g_str->list_of_elems = ((void *)0);
  g_str->nb_of_elems = 0;
  g_str->nb_of_pages = 0;
  g_str->pages_size = getpagesize();
  if ((g_str->begin_process_break_limit = sbrk(0)) == (void *)-1)
    return (-1);
  g_str->allocated_memory = g_str->begin_process_break_limit;
  return (0);
}


original_code:
void *my_sbrk(intptr_t increment)
{
  unsigned long local_mem_counter;
  void *ptr_to_allocate;
  unsigned long counter;
  int pages_counter;
  pages_counter = 0;
  g_str->allocated_memory = g_str->allocated_memory + (unsigned long)increment;
  local_mem_counter = (g_str->allocated_memory - g_str->begin_process_break_limit);
  counter = 0;
  while (counter < local_mem_counter)
    {
      counter = counter + g_str->pages_size;
      pages_counter = pages_counter + 1;
    }
  if ((pages_counter - g_str->nb_of_pages) > 0)
    {
      if ((sbrk((pages_counter - g_str->nb_of_pages) * g_str->pages_size)) == (void*)-1)
 return ((void*)-1);
    }
  g_str->nb_of_pages = pages_counter;
  ptr_to_allocate = g_str->allocated_memory - (unsigned long)increment;
  if (((unsigned long)g_str->allocated_memory % 4) != 0)
    g_str->allocated_memory = g_str->allocated_memory +
      (4 - ((unsigned long)g_str->allocated_memory % 4));
  return (ptr_to_allocate);
}

canonical_original_code:
void *my_sbrk(long increment)
{
  unsigned long local_mem_counter;
  void *ptr_to_allocate;
  unsigned long counter;
  int pages_counter;
  pages_counter = 0;
  g_str->allocated_memory = g_str->allocated_memory + (unsigned long)increment;
  local_mem_counter = (g_str->allocated_memory - g_str->begin_process_break_limit);
  counter = 0;
  while (counter < local_mem_counter)
    {
      counter = counter + g_str->pages_size;
      pages_counter = pages_counter + 1;
    }
  if ((pages_counter - g_str->nb_of_pages) > 0)
    {
      if ((sbrk((pages_counter - g_str->nb_of_pages) * g_str->pages_size)) == (void *)-1)
 return ((void *)-1);
    }
  g_str->nb_of_pages = pages_counter;
  ptr_to_allocate = g_str->allocated_memory - (unsigned long)increment;
  if (((unsigned long)g_str->allocated_memory % 4) != 0)
    g_str->allocated_memory = g_str->allocated_memory +
      (4 - ((unsigned long)g_str->allocated_memory % 4));
  return (ptr_to_allocate);
}

variable_types: increment long

variable_types:
counter
unsigned long

return_type: void *


decompiled_code:
__int64 __fastcall ret_free_good_elem(unsigned __int64 a1)
{
__int64 result; // rax
int v2; // edx

result = *g_str;
if ( *g_str )
{
v2 = 0;
while ( *((_DWORD *)g_str + 2) > v2 )
{
if ( *(_DWORD *)(result + 8) != 1 || *(_QWORD *)result < a1 )
{
result = *(_QWORD *)(result + 24);
++v2;
if ( result )
continue;
}
return result;
}
result = 0LL;
}
return result;
}

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

result = *g_str;
if ( *g_str )
{
v2 = 0;
while ( *((_DWORD *)g_str + 2) > v2 )
{
if ( *(_DWORD *)(result + 8) != 1 || *(_QWORD *)result < a1 )
{
result = *(_QWORD *)(result + 24);
++v2;
if ( result )
continue;
}
return result;
}
result = 0LL;
}
return result;
}

original_code:
t_elem *ret_free_good_elem(size_t size)
{
  int counter;
  t_elem *elem_to_use;
  elem_to_use = ((void *)0);
  if ((elem_to_use = g_str->list_of_elems) == ((void *)0))
    return (((void *)0));
  counter = 0;
  while (elem_to_use != ((void *)0) && counter < g_str->nb_of_elems)
    {
      if (elem_to_use->ptr_free == 1)
 if (size <= elem_to_use->size)
   return (elem_to_use);
      elem_to_use = elem_to_use->next;
      counter = counter + 1;
    }
  return (((void *)0));
}

canonical_original_code:
struct s_elem *ret_free_good_elem(unsigned long size)
{
  int counter;
  struct s_elem *elem_to_use;
  elem_to_use = ((void *)0);
  if ((elem_to_use = g_str->list_of_elems) == ((void *)0))
    return (((void *)0));
  counter = 0;
  while (elem_to_use != ((void *)0) && counter < g_str->nb_of_elems)
    {
      if (elem_to_use->ptr_free == 1)
 if (size <= elem_to_use->size)
   return (elem_to_use);
      elem_to_use = elem_to_use->next;
      counter = counter + 1;
    }
  return (((void *)0));
}

variable_types:
size
unsigned long


decompiled_code:
__int64 __fastcall ret_metadata_of_ptr(__int64 a1)
{
*(_DWORD *)(a1 - 32) = 1;
return a1 - 40;
}

original_code:
t_elem *ret_metadata_of_ptr(void *ptr_to_use)
{
  t_elem *elem_to_ret;
  elem_to_ret = ptr_to_use - (sizeof(*elem_to_ret));
  elem_to_ret->ptr_free = 1;
  return (elem_to_ret);
}

canonical_original_code:
struct s_elem *ret_metadata_of_ptr(void *ptr_to_use)
{
  struct s_elem *elem_to_ret;
  elem_to_ret = ptr_to_use - (sizeof(*elem_to_ret));
  elem_to_ret->ptr_free = 1;
  return (elem_to_ret);
}


decompiled_code:
__int64 __fastcall fusion_of_ptr(_QWORD *a1, _QWORD *a2)
{
__int64 v2; // rax
__int64 result; // rax

*a1 += *a2 + 40LL;
v2 = a2[3];
a1[3] = v2;
if ( v2 )
*(_QWORD *)(v2 + 32) = a1;
result = g_str;
--*(_DWORD *)(g_str + 8LL);
return result;
}

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

*a1 += *a2 + 40LL;
v2 = a2[3];
a1[3] = v2;
if ( v2 )
*(_QWORD *)(v2 + 32) = a1;
result = g_str;
--*(_DWORD *)(g_str + 8LL);
return result;
}

original_code:
void fusion_of_ptr(t_elem *elem_first, t_elem *elem_sec)
{
  elem_first->size = elem_first->size + sizeof(*elem_sec) + elem_sec->size;
  if (elem_sec == ((void *)0))
    elem_first->next = ((void *)0);
  else
    elem_first->next = elem_sec->next;
  if (elem_first != ((void *)0) && elem_first->next != ((void *)0))
    elem_first->next->prev = elem_first;
  g_str->nb_of_elems = g_str->nb_of_elems - 1;
}

canonical_original_code:
void fusion_of_ptr(struct s_elem *elem_first, struct s_elem *elem_sec)
{
  elem_first->size = elem_first->size + sizeof(*elem_sec) + elem_sec->size;
  if (elem_sec == ((void *)0))
    elem_first->next = ((void *)0);
  else
    elem_first->next = elem_sec->next;
  if (elem_first != ((void *)0) && elem_first->next != ((void *)0))
    elem_first->next->prev = elem_first;
  g_str->nb_of_elems = g_str->nb_of_elems - 1;
}


decompiled_code:
__int64 __fastcall fusion_of_datas(__int64 a1)
{
__int64 v1; // rsi
__int64 v2; // r12
__int64 result; // rax

if ( a1 )
{
v1 = *(_QWORD *)(a1 + 24);
*(_DWORD *)(a1 + 8) = 1;
v2 = *(_QWORD *)(a1 + 32);
if ( v1 && *(_DWORD *)(v1 + 8) == 1 )
result = fusion_of_ptr((_QWORD *)a1, (_QWORD *)v1);
if ( v2 )
{
if ( *(_DWORD *)(v2 + 8) == 1 )
result = fusion_of_ptr((_QWORD *)v2, (_QWORD *)a1);
}
}
return result;
}

original_code:
void fusion_of_datas(t_elem *elem_to_use)
{
  t_elem *next;
  t_elem *prev;
  elem_to_use->ptr_free = 1;
  next = elem_to_use->next;
  prev = elem_to_use->prev;
  if (elem_to_use != ((void *)0))
    {
      if (next != ((void *)0) && next->ptr_free == 1)
 fusion_of_ptr(elem_to_use, next);
      if (prev != ((void *)0) && prev->ptr_free == 1)
 fusion_of_ptr(prev, elem_to_use);
    }
}

canonical_original_code:
void fusion_of_datas(struct s_elem *elem_to_use)
{
  struct s_elem *next;
  struct s_elem *prev;
  elem_to_use->ptr_free = 1;
  next = elem_to_use->next;
  prev = elem_to_use->prev;
  if (elem_to_use != ((void *)0))
    {
      if (next != ((void *)0) && next->ptr_free == 1)
 fusion_of_ptr(elem_to_use, next);
      if (prev != ((void *)0) && prev->ptr_free == 1)
 fusion_of_ptr(prev, elem_to_use);
    }
}


free

decompiled_code:
void __fastcall free(__int64 a1)
{
__int64 v1; // rax

if ( a1 )
{
v1 = ret_metadata_of_ptr(a1);
fusion_of_datas(v1);
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
__int64 v1; // rax

if ( a1 )
{
v1 = func0(a1);
func2(v1);
}
}

original_code:
void free(void *ptr)
{
  t_elem *elem_to_use;
  if (ptr != ((void *)0))
    {
      elem_to_use = ret_metadata_of_ptr(ptr);
      fusion_of_datas(elem_to_use);
    }
}

canonical_original_code:
void free(void *ptr)
{
  struct s_elem *elem_to_use;
  if (ptr != ((void *)0))
    {
      elem_to_use = ret_metadata_of_ptr(ptr);
      fusion_of_datas(elem_to_use);
    }
}

variable_types:
ptr
void *


decompiled_code:
__int64 __fastcall refill_elem(_BYTE *a1, _BYTE *a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

if ( a1 == (_BYTE *)-1LL )
return 0LL;
*(_QWORD *)a3 = a4;
*(_DWORD *)(a3 + 8) = 0;
*a1 = *a2;
result = a3;
*(_QWORD *)(a3 + 16) = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, _BYTE *a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

if ( a1 == (_BYTE *)-1LL )
return 0LL;
*(_QWORD *)a3 = a4;
*(_DWORD *)(a3 + 8) = 0;
*a1 = *a2;
result = a3;
*(_QWORD *)(a3 + 16) = a1;
return result;
}

original_code:
t_elem *refill_elem(void *ptr_of_elem_to_set, void *ptr_to_use,
                             t_elem *elem_to_fill, size_t size_to_set)
{
  if (ptr_of_elem_to_set == (void*)-1)
    return (((void *)0));
  elem_to_fill->size = size_to_set;
  elem_to_fill->ptr_free = 0;
  if (memcpy(ptr_of_elem_to_set, ptr_to_use, sizeof(*ptr_to_use)) == ((void *)0))
    return (((void *)0));
  elem_to_fill->ptr_of_elem = ptr_of_elem_to_set;
  return (elem_to_fill);
}

canonical_original_code:
struct s_elem *refill_elem(void *ptr_of_elem_to_set, void *ptr_to_use, struct s_elem *elem_to_fill, unsigned long size_to_set)
{
  if (ptr_of_elem_to_set == (void *)-1)
    return (((void *)0));
  elem_to_fill->size = size_to_set;
  elem_to_fill->ptr_free = 0;
  if (memcpy(ptr_of_elem_to_set, ptr_to_use, sizeof(*ptr_to_use)) == ((void *)0))
    return (((void *)0));
  elem_to_fill->ptr_of_elem = ptr_of_elem_to_set;
  return (elem_to_fill);
}


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

if ( (unsigned int)add_elem_in_list(*(unsigned int *)(g_str + 8LL)) == -1 || (result = my_sbrk(a2), result == -1) )
result = 0LL;
return result;
}

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

if ( (unsigned int)add_elem_in_list(*(unsigned int *)(g_str + 8LL)) == -1 || (result = my_sbrk(a2), result == -1) )
result = 0LL;
return result;
}

original_code:
void *check_add_elem_and_sbrk(void *ptr_to_ret, size_t size)
{
  if (add_elem_in_list(g_str->nb_of_elems) == -1)
    return (((void *)0));
 if ((ptr_to_ret = my_sbrk(size)) == (void*)-1)
    return (((void *)0));
  return (ptr_to_ret);
}

canonical_original_code:
void *check_add_elem_and_sbrk(void *ptr_to_ret, unsigned long size)
{
  if (add_elem_in_list(g_str->nb_of_elems) == -1)
    return (((void *)0));
 if ((ptr_to_ret = my_sbrk(size)) == (void *)-1)
    return (((void *)0));
  return (ptr_to_ret);
}


main

repo: Nanolat/c-callstack

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v4; // er12

v3 = nanolat_message_queue(argc, argv, envp);
v4 = v3;
if ( v3 )
fprintf(stderr, "Error(code:%d) at : %s (%s:%d)\n", v3, "main", "sample.c", 47LL);
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v4; // er12

v3 = func9(argc, argv, envp);
v4 = v3;
if ( v3 )
fprintf(stderr, "Error(code:%d) at : %s (%s:%d)\n", v3, "main", "sample.c", 47LL);
return v4;
}

original_code:
int main(int argc, char * argv[]) {
    int ret = nanolat_message_queue();
    if (ret) { if ((ret)) { fprintf( stderr, "Error(code:%d) at : %s (%s:%d)\n", (ret), __FUNCTION__, "sample.c", 47); } return (ret); };
    { if ((0)) { fprintf( stderr, "Error(code:%d) at : %s (%s:%d)\n", (0), __FUNCTION__, "sample.c", 49); } return (0); };
}

canonical_original_code:
int main(int argc, char *argv[]) {
    int ret = nanolat_message_queue();
    if (ret) { if ((ret)) { fprintf( stderr, "Error(code:%d) at : %s (%s:%d)\n", (ret), __FUNCTION__, "sample.c", 47); } return (ret); };
    { if ((0)) { fprintf( stderr, "Error(code:%d) at : %s (%s:%d)\n", (0), __FUNCTION__, "sample.c", 49); } return (0); };
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int


decompiled_code:
__int64 so_topless_ranking_server()
{
puts("If you love me, you are in trouble, as I am married.");
fprintf(stderr, "Error(code:%d) at : %s (%s:%d)\n", 0xFFFFFFFFLL, "so_topless_ranking_server", "sample.c", 23LL);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func7()
{
puts("If you love me, you are in trouble, as I am married.");
fprintf(stderr, "Error(code:%d) at : %s (%s:%d)\n", 0xFFFFFFFFLL, "so_topless_ranking_server", "sample.c", 23LL);
return 0xFFFFFFFFLL;
}

original_code:
int so_topless_ranking_server() {
    printf("If you love me, you are in trouble, as I am married.\n");
    if ( 1 == 1 )
        { if ((-1)) { fprintf( stderr, "Error(code:%d) at : %s (%s:%d)\n", (-1), __FUNCTION__, "sample.c", 23); } return (-1); };
    { if ((0)) { fprintf( stderr, "Error(code:%d) at : %s (%s:%d)\n", (0), __FUNCTION__, "sample.c", 25); } return (0); };
}


decompiled_code:
__int64 nanolat_database()
{
unsigned int v0; // eax
unsigned int v1; // er12

v0 = so_topless_ranking_server();
v1 = v0;
if ( v0 )
fprintf(stderr, "Error(code:%d) at : %s (%s:%d)\n", v0, "nanolat_database", "sample.c", 31LL);
return v1;
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int v0; // eax
unsigned int v1; // er12

v0 = func7();
v1 = v0;
if ( v0 )
fprintf(stderr, "Error(code:%d) at : %s (%s:%d)\n", v0, "nanolat_database", "sample.c", 31LL);
return v1;
}

original_code:
int nanolat_database()
{
    int ret = so_topless_ranking_server();
    if (ret) { if ((ret)) { fprintf( stderr, "Error(code:%d) at : %s (%s:%d)\n", (ret), __FUNCTION__, "sample.c", 31); } return (ret); };
    { if ((0)) { fprintf( stderr, "Error(code:%d) at : %s (%s:%d)\n", (0), __FUNCTION__, "sample.c", 33); } return (0); };
}


decompiled_code:
__int64 nanolat_message_queue()
{
unsigned int v0; // eax
unsigned int v1; // er12

v0 = nanolat_database();
v1 = v0;
if ( v0 )
fprintf(stderr, "Error(code:%d) at : %s (%s:%d)\n", v0, "nanolat_message_queue", "sample.c", 39LL);
return v1;
}

canonical_decompiled_code:
__int64 func9()
{
unsigned int v0; // eax
unsigned int v1; // er12

v0 = func8();
v1 = v0;
if ( v0 )
fprintf(stderr, "Error(code:%d) at : %s (%s:%d)\n", v0, "nanolat_message_queue", "sample.c", 39LL);
return v1;
}

original_code:
int nanolat_message_queue()
{
    int ret = nanolat_database();
    if (ret) { if ((ret)) { fprintf( stderr, "Error(code:%d) at : %s (%s:%d)\n", (ret), __FUNCTION__, "sample.c", 39); } return (ret); };
    { if ((0)) { fprintf( stderr, "Error(code:%d) at : %s (%s:%d)\n", (0), __FUNCTION__, "sample.c", 41); } return (0); };
}


blur

repo: NatTuck/cakemark

original_code:
void
blur(byte* im0, size_t ww, size_t hh, int sigma)
{
    int rr = 3 * sigma;
    double *bvec = malloc((2 * rr + 1) * sizeof(double));
    double bsum = 0.0;
    for (int kk = -rr; kk <= rr; ++kk) {
        int ii = kk + rr;
        bvec[ii] = gauss(kk, 0.0, sigma);
        bsum += bvec[ii];
    }
    for (int ii = 0; ii < 2 * rr + 1; ++ii) {
        bvec[ii] *= 1.0 / bsum;
    }
    byte* im1 = malloc(ww * hh * sizeof(byte));
    for (int ii = 0; ii < hh; ++ii) {
        for (int jj = 0; jj < ww; ++jj) {
            double p1 = 0.0;
            for (int kk = -rr; kk <= rr; ++kk) {
                int jj0 = clamp(jj + kk, 0, ww - 1);
                p1 += bvec[kk + rr] * im0[ww*ii + jj0];
            }
            im1[ww*ii + jj] = clamp(round(p1), 0, 255);
        }
    }
    for (int jj = 0; jj < ww; ++jj) {
        for (int ii = 0; ii < hh; ++ii) {
            double p0 = 0.0;
            for (int kk = -rr; kk <= rr; ++kk) {
                int ii0 = clamp(ii + kk, 0, hh - 1);
                p0 += bvec[kk + rr] * im1[ww*ii0 + jj];
            }
            im0[ww*ii + jj] = clamp(round(p0), 0, 255);
        }
    }
    free(im1);
    free(bvec);
}

canonical_original_code:
void blur(unsigned char *im0, unsigned long ww, unsigned long hh, int sigma)
{
    int rr = 3 * sigma;
    double *bvec = malloc((2 * rr + 1) * sizeof(double));
    double bsum = 0.0;
    for (int kk = -rr; kk <= rr; ++kk) {
        int ii = kk + rr;
        bvec[ii] = gauss(kk, 0.0, sigma);
        bsum += bvec[ii];
    }
    for (int ii = 0; ii < 2 * rr + 1; ++ii) {
        bvec[ii] *= 1.0 / bsum;
    }
    unsigned char *im1 = malloc(ww * hh * sizeof(byte));
    for (int ii = 0; ii < hh; ++ii) {
        for (int jj = 0; jj < ww; ++jj) {
            double p1 = 0.0;
            for (int kk = -rr; kk <= rr; ++kk) {
                int jj0 = clamp(jj + kk, 0, ww - 1);
                p1 += bvec[kk + rr] * im0[ww*ii + jj0];
            }
            im1[ww*ii + jj] = clamp(round(p1), 0, 255);
        }
    }
    for (int jj = 0; jj < ww; ++jj) {
        for (int ii = 0; ii < hh; ++ii) {
            double p0 = 0.0;
            for (int kk = -rr; kk <= rr; ++kk) {
                int ii0 = clamp(ii + kk, 0, hh - 1);
                p0 += bvec[kk + rr] * im1[ww*ii0 + jj];
            }
            im0[ww*ii + jj] = clamp(round(p0), 0, 255);
        }
    }
    free(im1);
    free(bvec);
}

variable_types:
ww
unsigned long

variable_types:
hh
unsigned long

variable_types: sigma int

variable_types: rr int

variable_types:
bvec
double *

variable_types: bsum double

variable_types: kk int

variable_types: ii int

variable_types: jj int


streq

decompiled_code:
_BOOL8 __fastcall streq(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

original_code:
int
streq(const char* aa, const char* bb)
{
    return strcmp(aa, bb) == 0;
}

canonical_original_code:
int streq(char *aa, char *bb)
{
    return strcmp(aa, bb) == 0;
}

variable_types:
aa
char *

variable_types:
bb
char *


lstrdup

decompiled_code:
__int64 __fastcall lstrdup(const char *a1)
{
size_t v1; // r13
__int64 v2; // r12

v1 = strlen(a1) + 1;
v2 = GC_malloc_atomic(v1);
strlcpy(v2, a1, v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
size_t v1; // r13
__int64 v2; // r12

v1 = strlen(a1) + 1;
v2 = GC_malloc_atomic(v1);
strlcpy(v2, a1, v1);
return v2;
}

original_code:
char*
lstrdup(const char* aa)
{
    size_t size = strlen(aa) + 1;
    char* bb = (char*) GC_malloc_atomic(size);
    strlcpy(bb, aa, size);
    return bb;
}

canonical_original_code:
char *lstrdup(char *aa)
{
    unsigned long size = strlen(aa) + 1;
    char *bb = (char*) GC_malloc_atomic(size);
    strlcpy(bb, aa, size);
    return bb;
}


lsprintf

decompiled_code:
char *lsprintf(char *format, ...)
{
size_t v1; // rbp
char *v2; // r12
gcc_va_list va; // [rsp+0h] [rbp-108h] BYREF
__m128i arg; // [rsp+18h] [rbp-F0h] BYREF
void *v6; // [rsp+28h] [rbp-E0h]

va_start(va, format);
arg = _mm_loadu_si128((const __m128i *)va);
v6 = va[0].reg_save_area;
v1 = vsnprintf(0LL, 0LL, format, va) + 1;
v2 = (char *)GC_malloc_atomic(v1);
vsnprintf(v2, v1, format, &arg);
return v2;
}

canonical_decompiled_code:
char *func2(char *format, ...)
{
size_t v1; // rbp
char *v2; // r12
gcc_va_list va; // [rsp+0h] [rbp-108h] BYREF
__m128i arg; // [rsp+18h] [rbp-F0h] BYREF
void *v6; // [rsp+28h] [rbp-E0h]

va_start(va, format);
arg = _mm_loadu_si128((const __m128i *)va);
v6 = va[0].reg_save_area;
v1 = vsnprintf(0LL, 0LL, format, va) + 1;
v2 = (char *)GC_malloc_atomic(v1);
vsnprintf(v2, v1, format, &arg);
return v2;
}

original_code:
char*
lsprintf(const char* fmt, ...)
{
    va_list ap1;
    va_list ap2;
    __builtin_va_start(ap1,fmt);
    __builtin_va_copy(ap2,ap1);
    int size = 1 + vsnprintf(0, 0, fmt, ap1);
    char* outp = (char*) GC_malloc_atomic(size);
    vsnprintf(outp, size, fmt, ap2);
    __builtin_va_end(ap1);
    __builtin_va_end(ap2);
    return outp;
}

canonical_original_code:
char *lsprintf(char *fmt, ...)
{
    __builtin_va_list ap1;
    __builtin_va_list ap2;
    __builtin_va_start(ap1,fmt);
    __builtin_va_copy(ap2,ap1);
    int size = 1 + vsnprintf(0, 0, fmt, ap1);
    char *outp = (char*) GC_malloc_atomic(size);
    vsnprintf(outp, size, fmt, ap2);
    __builtin_va_end(ap1);
    __builtin_va_end(ap2);
    return outp;
}

variable_types:
fmt
char *

variable_types: size int

variable_types:
outp
char *


lchomp

decompiled_code:
const char *__fastcall lchomp(const char *a1)
{
const char *v1; // r12
__int64 v2; // rax
const char *v3; // rdx
__int64 v4; // rax

v1 = (const char *)lstrdup(a1);
v2 = (int)(strlen(v1) - 1);
v3 = &v1[v2];
if ( v1[v2] == 10 )
{
v4 = (__int64)&v1[v2 - 1];
do
{
*v3 = 0;
v3 = (const char *)v4--;
}
while ( *(_BYTE *)(v4 + 1) == 10 );
}
return v1;
}

canonical_decompiled_code:
const char *__fastcall func3(const char *a1)
{
const char *v1; // r12
__int64 v2; // rax
const char *v3; // rdx
__int64 v4; // rax

v1 = (const char *)func1(a1);
v2 = (int)(strlen(v1) - 1);
v3 = &v1[v2];
if ( v1[v2] == 10 )
{
v4 = (__int64)&v1[v2 - 1];
do
{
*v3 = 0;
v3 = (const char *)v4--;
}
while ( *(_BYTE *)(v4 + 1) == 10 );
}
return v1;
}

original_code:
char*
lchomp(const char* ss)
{
    char* yy = lstrdup(ss);
    int nn = strlen(yy);
    int ii = nn - 1;
    while (yy[ii] == '\n') {
        yy[ii] = '\0';
        ii -= 1;
    }
    return yy;
}

canonical_original_code:
char *lchomp(char *ss)
{
    char *yy = lstrdup(ss);
    int nn = strlen(yy);
    int ii = nn - 1;
    while (yy[ii] == '\n') {
        yy[ii] = '\0';
        ii -= 1;
    }
    return yy;
}

variable_types:
ss
char *

variable_types:
yy
char *

variable_types: nn int


lstripc

decompiled_code:
__int64 __fastcall lstripc(char a1, const char *a2)
{
size_t v4; // rax
int v5; // ecx
__int64 v6; // rdi
size_t v7; // r9
void *v8; // rsp
size_t i; // rax
char v10; // dl
char *v11; // rsi
char v13[15]; // [rsp+0h] [rbp-10h] BYREF

v4 = strlen(a2);
v5 = 0;
v6 = 0LL;
v7 = v4;
v8 = alloca(v4 + 10);
for ( i = 0LL; ; ++i )
{
v11 = &v13[v6];
if ( v7 <= i )
break;
v10 = a2[i];
if ( v10 != a1 )
{
*v11 = v10;
v6 = ++v5;
}
}
*v11 = 0;
return lstrdup(v13);
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1, const char *a2)
{
size_t v4; // rax
int v5; // ecx
__int64 v6; // rdi
size_t v7; // r9
void *v8; // rsp
size_t i; // rax
char v10; // dl
char *v11; // rsi
char v13[15]; // [rsp+0h] [rbp-10h] BYREF

v4 = strlen(a2);
v5 = 0;
v6 = 0LL;
v7 = v4;
v8 = alloca(v4 + 10);
for ( i = 0LL; ; ++i )
{
v11 = &v13[v6];
if ( v7 <= i )
break;
v10 = a2[i];
if ( v10 != a1 )
{
*v11 = v10;
v6 = ++v5;
}
}
*v11 = 0;
return func1(v13);
}

original_code:
char*
lstripc(const char cc, const char* ss)
{
    char* yy = __builtin_alloca (strlen(ss) + 2);
    int jj = 0;
    for (int ii = 0; ii < strlen(ss); ++ii) {
        if (ss[ii] != cc) {
            yy[jj] = ss[ii];
            jj += 1;
        }
    }
    yy[jj] = 0;
    return lstrdup(yy);
}

canonical_original_code:
char *lstripc(char cc, char *ss)
{
    char *yy = __builtin_alloca (strlen(ss) + 2);
    int jj = 0;
    for (int ii = 0; ii < strlen(ss); ++ii) {
        if (ss[ii] != cc) {
            yy[jj] = ss[ii];
            jj += 1;
        }
    }
    yy[jj] = 0;
    return lstrdup(yy);
}

variable_types: cc char