decompiled_code:
__int64 __fastcall get_concurrency(char *nptr)
{
int *v1; // rax
int *v2; // rbx
__int64 result; // rax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
result = strtol(nptr, 0LL, 10);
if ( *v2 )
{
perror("Error converting string to integer");
exit(1);
}
if ( (unsigned int)(result - 1) > 0x31 )
{
puts("Error: Concurrency level (-c) is not in valid range (1 to 50).");
exit(0);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *nptr)
{
int *v1; // rax
int *v2; // rbx
__int64 result; // rax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
result = strtol(nptr, 0LL, 10);
if ( *v2 )
{
perror("Error converting string to integer");
exit(1);
}
if ( (unsigned int)(result - 1) > 0x31 )
{
puts("Error: Concurrency level (-c) is not in valid range (1 to 50).");
exit(0);
}
return result;
}

original_code:
int get_concurrency(char * concr_str) {
    int concr;
    (*__errno_location ()) = 0;
    concr = (int) strtol(concr_str, ((void *)0), 10);
    if((*__errno_location ()) != 0) {
        perror("Error converting string to integer");
        exit(1);
    }
    if((concr < 1) || (concr > 50)) {
        printf("Error: Concurrency level (-c) is not in valid range (1 to 50).\n");
        exit(0);
    }
    return concr;
}

canonical_original_code:
int get_concurrency(char *concr_str) {
    int concr;
    (*__errno_location ()) = 0;
    concr = (int) strtol(concr_str, ((void *)0), 10);
    if((*__errno_location ()) != 0) {
        perror("Error converting string to integer");
        exit(1);
    }
    if((concr < 1) || (concr > 50)) {
        printf("Error: Concurrency level (-c) is not in valid range (1 to 50).\n");
        exit(0);
    }
    return concr;
}

variable_types: concr int


decompiled_code:
__int64 __fastcall get_max_prime(char *nptr)
{
int *v1; // rax
int *v2; // rbx
__int64 result; // rax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
result = strtol(nptr, 0LL, 10);
if ( *v2 )
{
perror("Error converting string to integer");
exit(1);
}
if ( (unsigned __int64)(result - 2) > 0xFFFFFFFD )
{
printf("Error: Max prime (-m) is not in valid range (2 to %u).\n", 0xFFFFFFFFLL);
exit(0);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *nptr)
{
int *v1; // rax
int *v2; // rbx
__int64 result; // rax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
result = strtol(nptr, 0LL, 10);
if ( *v2 )
{
perror("Error converting string to integer");
exit(1);
}
if ( (unsigned __int64)(result - 2) > 0xFFFFFFFD )
{
printf("Error: Max prime (-m) is not in valid range (2 to %u).\n", 0xFFFFFFFFLL);
exit(0);
}
return result;
}

original_code:
unsigned int get_max_prime(char * max_str) {
    long max;
    (*__errno_location ()) = 0;
    max = strtol(max_str, ((void *)0), 10);
    if((*__errno_location ()) != 0) {
        perror("Error converting string to integer");
        exit(1);
    }
    if((max < 2) || (max > (0x7fffffff * 2U + 1U))) {
        printf("Error: Max prime (-m) is not in valid range (2 to %u).\n", (0x7fffffff * 2U + 1U));
        exit(0);
    }
    return (unsigned int) max;
}

canonical_original_code:
unsigned int get_max_prime(char *max_str) {
    long max;
    (*__errno_location ()) = 0;
    max = strtol(max_str, ((void *)0), 10);
    if((*__errno_location ()) != 0) {
        perror("Error converting string to integer");
        exit(1);
    }
    if((max < 2) || (max > (0x7fffffff * 2U + 1U))) {
        printf("Error: Max prime (-m) is not in valid range (2 to %u).\n", (0x7fffffff * 2U + 1U));
        exit(0);
    }
    return (unsigned int) max;
}

variable_types: max long

return_type: unsigned int


decompiled_code:
int __fastcall get_options(int argc, char **argv, __int64 a3)
{
int result; // eax

*(_QWORD *)a3 = 0x1FFFFFFFFLL;
*(_DWORD *)(a3 + 8) = 0;
while ( 1 )
{
result = getopt(argc, argv, "qc:m:");
if ( (_BYTE)result == 0xFF )
break;
switch ( (_BYTE)result )
{
case 'm':
*(_DWORD *)a3 = get_max_prime(optarg);
break;
case 'q':
*(_DWORD *)(a3 + 8) = 1;
break;
case 'c':
*(_DWORD *)(a3 + 4) = get_concurrency(optarg);
break;
default:
printf("Usage: %s [-q (quiet mode)] [-c concurrency] [-m max prime]\n", *argv);
exit(0);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int argc, char **argv, __int64 a3)
{
int result; // eax

*(_QWORD *)a3 = 0x1FFFFFFFFLL;
*(_DWORD *)(a3 + 8) = 0;
while ( 1 )
{
result = getopt(argc, argv, "qc:m:");
if ( (_BYTE)result == 0xFF )
break;
switch ( (_BYTE)result )
{
case 'm':
*(_DWORD *)a3 = func1(optarg);
break;
case 'q':
*(_DWORD *)(a3 + 8) = 1;
break;
case 'c':
*(_DWORD *)(a3 + 4) = func0(optarg);
break;
default:
printf("Usage: %s [-q (quiet mode)] [-c concurrency] [-m max prime]\n", *argv);
exit(0);
}
}
return result;
}

original_code:
void get_options(int argc, char ** argv, struct options * opts) {
    char opt;
    opts->max = (0x7fffffff * 2U + 1U);
    opts->concurrency = 1;
    opts->q_set = 0;
    while((opt = getopt(argc, argv, "qc:m:")) != -1) {
        switch (opt) {
            case 'q':
                opts->q_set = 1;
                break;
            case 'c':
                opts->concurrency = get_concurrency(optarg);
                break;
            case 'm':
                opts->max = get_max_prime(optarg);
                break;
            default:
                printf("Usage: %s [-q (quiet mode)] [-c concurrency] [-m max prime]\n", argv[0]);
                exit(0);
                break;
        }
    }
}

canonical_original_code:
void get_options(int argc, char **argv, struct options *opts) {
    char opt;
    opts->max = (0x7fffffff * 2U + 1U);
    opts->concurrency = 1;
    opts->q_set = 0;
    while((opt = getopt(argc, argv, "qc:m:")) != -1) {
        switch (opt) {
            case 'q':
                opts->q_set = 1;
                break;
            case 'c':
                opts->concurrency = get_concurrency(optarg);
                break;
            case 'm':
                opts->max = get_max_prime(optarg);
                break;
            default:
                printf("Usage: %s [-q (quiet mode)] [-c concurrency] [-m max prime]\n", argv[0]);
                exit(0);
                break;
        }
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct options { unsigned int max; int concurrency; int q_set; } *

variable_types: opt char

return_type: void


decompiled_code:
__int64 __fastcall get_sqrt(int a1)
{
double v1; // xmm0_8
__int64 result; // rax

v1 = (double)a1;
if ( (double)a1 < 0.0 )
result = (unsigned int)(int)sqrt(v1);
else
result = (unsigned int)(int)sqrt(v1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
double v1; // xmm0_8
__int64 result; // rax

v1 = (double)a1;
if ( (double)a1 < 0.0 )
result = (unsigned int)(int)sqrt(v1);
else
result = (unsigned int)(int)sqrt(v1);
return result;
}

original_code:
unsigned int get_sqrt(unsigned int max) {
    return (unsigned int)(sqrt((double)max));
}

variable_types:
max
unsigned int


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

result = get_sqrt(a1);
if ( (result & 0x1F) != 0 )
result = ((unsigned int)result & 0xFFFFFFE0) + 32;
return result;
}

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

result = func3(a1);
if ( (result & 0x1F) != 0 )
result = ((unsigned int)result & 0xFFFFFFE0) + 32;
return result;
}

original_code:
unsigned int get_sqrt32(unsigned int max) {
    unsigned int sqr;
    sqr = get_sqrt(max);
    if((sqr % 32) == 0) {
        return sqr;
    }
    else {
        return ((sqr/32) + 1) * 32;
    }
}

variable_types:
sqr
unsigned int


decompiled_code:
__int64 __fastcall get_interval(unsigned int a1, int a2)
{
return (((a2 - (unsigned int)get_sqrt32(a2)) / a1) & 0xFFFFFFE0) + 32;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, int a2)
{
return (((a2 - (unsigned int)func4(a2)) / a1) & 0xFFFFFFE0) + 32;
}

original_code:
unsigned int get_interval(int num_threads, unsigned int max) {
    unsigned int intvl;
    intvl = ((max - get_sqrt32(max))/num_threads);
    intvl = (intvl/32 + 1) * 32;
    return intvl;
}

variable_types:
intvl
unsigned int


decompiled_code:
__int64 __fastcall get_first_multiple_gt(unsigned int a1, unsigned int a2)
{
return a1 * (a2 / a1 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, unsigned int a2)
{
return a1 * (a2 / a1 + 1);
}

original_code:
unsigned int get_first_multiple_gt(unsigned int factor, unsigned int low) {
    unsigned int i = (low/factor) + 1;
    return i*factor;
}

variable_types:
factor
unsigned int

variable_types:
low
unsigned int

variable_types:
i
unsigned int


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

result = a2;
if ( a1 <= a2 )
result = a1;
return result;
}

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

result = a2;
if ( a1 <= a2 )
result = a1;
return result;
}

original_code:
unsigned long get_min(unsigned long a, unsigned long b) {
    if(a<b)
        return a;
    else
        return b;
}

variable_types:
a
unsigned long

variable_types:
b
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall get_low(unsigned int a1, unsigned int a2, int a3)
{
__int64 v5; // r12
unsigned int v6; // eax

if ( !a3 )
return get_sqrt(a2);
v5 = (unsigned int)get_sqrt32(a2);
v6 = get_interval(a1, a2);
return get_min(v5 + v6 * (__int64)a3, a2);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, unsigned int a2, int a3)
{
__int64 v5; // r12
unsigned int v6; // eax

if ( !a3 )
return func3(a2);
v5 = (unsigned int)func4(a2);
v6 = func5(a1, a2);
return func7(v5 + v6 * (__int64)a3, a2);
}

original_code:
unsigned int get_low(int num_threads, unsigned int max, int thread_num) {
    unsigned long intvl, sqr;
    if(thread_num == 0) {
        sqr = get_sqrt(max);
        return sqr;
    }
    else {
        sqr = get_sqrt32(max);
        intvl = get_interval(num_threads, max);
        return get_min((sqr + (thread_num * intvl)), max);
    }
}

canonical_original_code:
unsigned int get_low(int num_threads, unsigned int max, int thread_num) {
    unsigned long intvl; unsigned long sqr;
    if(thread_num == 0) {
        sqr = get_sqrt(max);
        return sqr;
    }
    else {
        sqr = get_sqrt32(max);
        intvl = get_interval(num_threads, max);
        return get_min((sqr + (thread_num * intvl)), max);
    }
}

variable_types:
intvl
unsigned long

variable_types:
sqr
unsigned long


decompiled_code:
unsigned __int64 __fastcall get_high(unsigned int a1, unsigned int a2, int a3)
{
__int64 v4; // r12
unsigned int v5; // eax

v4 = (unsigned int)get_interval(a1, a2);
v5 = get_sqrt32(a2);
return get_min(v5 + v4 * (a3 + 1), a2);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(unsigned int a1, unsigned int a2, int a3)
{
__int64 v4; // r12
unsigned int v5; // eax

v4 = (unsigned int)func5(a1, a2);
v5 = func4(a2);
return func7(v5 + v4 * (a3 + 1), a2);
}

original_code:
unsigned int get_high(int num_threads, unsigned int max, int thread_num) {
    unsigned long intvl, sqr;
    intvl = get_interval(num_threads, max);
    sqr = get_sqrt32(max);
    return get_min(sqr + ((thread_num+1) * intvl), max);
}

canonical_original_code:
unsigned int get_high(int num_threads, unsigned int max, int thread_num) {
    unsigned long intvl; unsigned long sqr;
    intvl = get_interval(num_threads, max);
    sqr = get_sqrt32(max);
    return get_min(sqr + ((thread_num+1) * intvl), max);
}


decompiled_code:
__int64 __fastcall sieve_multiples(__int64 a1, int a2, unsigned int a3)
{
unsigned int v3; // esi
__int64 result; // rax
int v5; // er9
unsigned int v6; // er8

v3 = 2 * a2;
if ( v3 <= a3 )
{
do
{
result = set_bit(a1, v3);
v3 += v5;
}
while ( v6 >= v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, int a2, unsigned int a3)
{
unsigned int v3; // esi
__int64 result; // rax
int v5; // er9
unsigned int v6; // er8

v3 = 2 * a2;
if ( v3 <= a3 )
{
do
{
result = func10(a1, v3);
v3 += v5;
}
while ( v6 >= v3 );
}
return result;
}

original_code:
void sieve_multiples(int * psbl_primes, unsigned int factor, unsigned int max) {
    unsigned int i;
    for(i = (2*factor); i <= max; i += factor) {
        set_bit(psbl_primes, i);
    }
}

canonical_original_code:
void sieve_multiples(int *psbl_primes, unsigned int factor, unsigned int max) {
    unsigned int i;
    for(i = (2*factor); i <= max; i += factor) {
        set_bit(psbl_primes, i);
    }
}


decompiled_code:
__int64 __fastcall sieve_multiples_in_range(__int64 a1, unsigned int a2, unsigned int a3)
{
__int64 result; // rax
__int64 v4; // r11
unsigned __int64 v5; // r8
unsigned __int64 v6; // r9
unsigned __int64 v7; // r9

result = get_first_multiple_gt(a2, a3);
LODWORD(v5) = result;
if ( (unsigned int)result <= v6 )
{
do
result = set_bit(v4, v5);
while ( v5 <= v7 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, unsigned int a2, unsigned int a3)
{
__int64 result; // rax
__int64 v4; // r11
unsigned __int64 v5; // r8
unsigned __int64 v6; // r9
unsigned __int64 v7; // r9

result = func6(a2, a3);
LODWORD(v5) = result;
if ( (unsigned int)result <= v6 )
{
do
result = func10(v4, v5);
while ( v5 <= v7 );
}
return result;
}

original_code:
void sieve_multiples_in_range(int * psbl_primes, unsigned int factor, unsigned int low, unsigned int high) {
    unsigned long i;
    for(i = get_first_multiple_gt(factor, low); i <= high; i += factor) {
        set_bit(psbl_primes, i);
    }
}

canonical_original_code:
void sieve_multiples_in_range(int *psbl_primes, unsigned int factor, unsigned int low, unsigned int high) {
    unsigned long i;
    for(i = get_first_multiple_gt(factor, low); i <= high; i += factor) {
        set_bit(psbl_primes, i);
    }
}

variable_types:
high
unsigned int

variable_types:
i
unsigned long


decompiled_code:
__int64 __fastcall check_bit(__int64 a1, unsigned int a2)
{
return (*(int *)(a1 + 4LL * (a2 >> 5)) >> a2) & 1;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, unsigned int a2)
{
return (*(int *)(a1 + 4LL * (a2 >> 5)) >> a2) & 1;
}

original_code:
int check_bit(int * array, unsigned int pos) {
    return ((array[pos/32] & (1 << (pos%32))) != 0);
}

canonical_original_code:
int check_bit(int *array, unsigned int pos) {
    return ((array[pos/32] & (1 << (pos%32))) != 0);
}

variable_types:
array
int *

variable_types:
pos
unsigned int

return_type: int


decompiled_code:
int __fastcall print_primes(__int64 a1, unsigned int a2)
{
int v2; // er13
unsigned int i; // ebx

if ( a2 )
{
v2 = 0;
for ( i = 1; i <= a2; ++i )
{
while ( 1 )
{
if ( !(unsigned int)check_bit(a1, i) )
{
++v2;
printf("%10u", i);
if ( __ROR4__(-858993459 * v2, 1) <= 0x19999999u )
break;
}
if ( ++i > a2 )
return putchar(10);
}
putchar(10);
}
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func15(__int64 a1, unsigned int a2)
{
int v2; // er13
unsigned int i; // ebx

if ( a2 )
{
v2 = 0;
for ( i = 1; i <= a2; ++i )
{
while ( 1 )
{
if ( !(unsigned int)func14(a1, i) )
{
++v2;
printf("%10u", i);
if ( __ROR4__(-858993459 * v2, 1) <= 0x19999999u )
break;
}
if ( ++i > a2 )
return putchar(10);
}
putchar(10);
}
}
return putchar(10);
}

original_code:
void print_primes(int * primes, int max) {
    unsigned int i;
    unsigned int cnt = 0;
    for(i=1; i<=max; i++) {
        if(!check_bit(primes, i)) {
            cnt++;
            printf("%10u", i);
            if(cnt % 10 == 0) {
                printf("\n");
            }
        }
    }
    printf("\n");
}

canonical_original_code:
void print_primes(int *primes, int max) {
    unsigned int i;
    unsigned int cnt = 0;
    for(i=1; i<=max; i++) {
        if(!check_bit(primes, i)) {
            cnt++;
            printf("%10u", i);
            if(cnt % 10 == 0) {
                printf("\n");
            }
        }
    }
    printf("\n");
}

variable_types:
primes
int *

variable_types: max int

variable_types:
cnt
unsigned int


decompiled_code:
__int64 __fastcall pre_sieve(__int64 a1, int a2)
{
signed int v2; // ebp
double v3; // xmm0_8
unsigned int v4; // er10
int v5; // esi
__int64 result; // rax
int v7; // er10
unsigned int v8; // er11
unsigned int v9; // er11

v2 = get_sqrt(a2);
v3 = (float)v2;
if ( v3 < 0.0 )
sqrt(v3);
v4 = 0;
do
{
while ( 1 )
{
v5 = v4;
result = check_bit(a1, v4);
if ( !(_DWORD)result )
break;
v4 = v7 + 1;
if ( v8 < v4 )
return result;
}
result = sieve_multiples(a1, v5, v2);
}
while ( v9 >= v4 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, int a2)
{
signed int v2; // ebp
double v3; // xmm0_8
unsigned int v4; // er10
int v5; // esi
__int64 result; // rax
int v7; // er10
unsigned int v8; // er11
unsigned int v9; // er11

v2 = func3(a2);
v3 = (float)v2;
if ( v3 < 0.0 )
sqrt(v3);
v4 = 0;
do
{
while ( 1 )
{
v5 = v4;
result = func14(a1, v4);
if ( !(_DWORD)result )
break;
v4 = v7 + 1;
if ( v8 < v4 )
return result;
}
result = func11(a1, v5, v2);
}
while ( v9 >= v4 );
return result;
}

original_code:
void pre_sieve(int * psbl_primes, unsigned int max) {
    unsigned int i, sqr, sqr2;
    sqr = get_sqrt(max);
    sqr2 = (unsigned int)(sqrt((float) sqr));
    for(i=0; i<=sqr2; i++) {
        if(!check_bit(psbl_primes, i)) {
            sieve_multiples(psbl_primes, i, sqr);
        }
    }
}

canonical_original_code:
void pre_sieve(int *psbl_primes, unsigned int max) {
    unsigned int i; unsigned int sqr; unsigned int sqr2;
    sqr = get_sqrt(max);
    sqr2 = (unsigned int)(sqrt((float) sqr));
    for(i=0; i<=sqr2; i++) {
        if(!check_bit(psbl_primes, i)) {
            sieve_multiples(psbl_primes, i, sqr);
        }
    }
}

variable_types:
max
unsigned int


decompiled_code:
void __fastcall thread_sieve(void *ptr)
{
unsigned int v2; // ebx
unsigned int v3; // er12
__int64 v4; // rdi
unsigned int v5; // esi

v2 = 0;
v3 = get_sqrt(*((_DWORD *)ptr + 2));
do
{
while ( 1 )
{
v4 = *(_QWORD *)ptr;
v5 = v2;
if ( !(unsigned int)check_bit(*(_QWORD *)ptr, v2) )
break;
if ( v3 < ++v2 )
goto LABEL_5;
}
++v2;
sieve_multiples_in_range(v4, v5, *((_DWORD *)ptr + 3));
}
while ( v3 >= v2 );
LABEL_5:
free(ptr);
}

canonical_decompiled_code:
void __fastcall func17(void *ptr)
{
unsigned int v2; // ebx
unsigned int v3; // er12
__int64 v4; // rdi
unsigned int v5; // esi

v2 = 0;
v3 = func3(*((_DWORD *)ptr + 2));
do
{
while ( 1 )
{
v4 = *(_QWORD *)ptr;
v5 = v2;
if ( !(unsigned int)func14(*(_QWORD *)ptr, v2) )
break;
if ( v3 < ++v2 )
goto LABEL_5;
}
++v2;
func12(v4, v5, *((_DWORD *)ptr + 3));
}
while ( v3 >= v2 );
LABEL_5:
free(ptr);
}

original_code:
void thread_sieve(struct sieve_args * sa) {
    unsigned int i, sqr;
    sqr = get_sqrt(sa->max);
    for(i=0; i<=sqr; i++) {
        if(!check_bit(sa->psbl_primes, i)) {
            sieve_multiples_in_range(sa->psbl_primes, i, sa->low, sa->high);
        }
    }
    free(sa);
}

canonical_original_code:
void thread_sieve(struct sieve_args *sa) {
    unsigned int i; unsigned int sqr;
    sqr = get_sqrt(sa->max);
    for(i=0; i<=sqr; i++) {
        if(!check_bit(sa->psbl_primes, i)) {
            sieve_multiples_in_range(sa->psbl_primes, i, sa->low, sa->high);
        }
    }
    free(sa);
}

variable_types: struct sieve_args { int *psbl_primes; unsigned int max; unsigned int low; unsigned int high; } *

variable_types:
sqr
unsigned int


original_code:
long nano_diff(struct timespec * begin, struct timespec * end) {
    return (1000000000L * (end->tv_sec - begin->tv_sec) + (end->tv_nsec - begin->tv_nsec));
}

canonical_original_code:
long nano_diff(struct timespec *begin, struct timespec *end) {
    return (1000000000L * (end->tv_sec - begin->tv_sec) + (end->tv_nsec - begin->tv_nsec));
}

variable_types: struct timespec { long tv_sec; long tv_nsec; } *

variable_types:
end
struct timespec *

return_type: long


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

result = shm_open("prime_shm", 194, 0x180u);
if ( result == -1 )
{
perror("Error creating shared memory object");
exit(1);
}
return result;
}

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

result = shm_open("prime_shm", 194, 0x180u);
if ( result == -1 )
{
perror("Error creating shared memory object");
exit(1);
}
return result;
}

original_code:
int create_prime_shm(void) {
    int shm_fd;
    if((shm_fd = shm_open("prime_shm", 0100 | 0200 | 02, 0400 | 0200)) == -1) {
        perror("Error creating shared memory object");
        exit(1);
    }
    return shm_fd;
}

canonical_original_code:
int create_prime_shm() {
    int shm_fd;
    if((shm_fd = shm_open("prime_shm", 0100 | 0200 | 02, 0400 | 0200)) == -1) {
        perror("Error creating shared memory object");
        exit(1);
    }
    return shm_fd;
}


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

result = shm_open("prime_shm", 2, 0x180u);
if ( result == -1 )
{
perror("Error opening shared memory object");
exit(1);
}
return result;
}

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

result = shm_open("prime_shm", 2, 0x180u);
if ( result == -1 )
{
perror("Error opening shared memory object");
exit(1);
}
return result;
}

original_code:
int open_prime_shm(unsigned int max) {
    int shm_fd;
    if((shm_fd = shm_open("prime_shm", 02, 0400 | 0200)) == -1) {
        perror("Error opening shared memory object");
        exit(1);
    }
    return shm_fd;;
}


decompiled_code:
void *__fastcall map_prime_shm(int fd, unsigned int a2)
{
void *result; // rax

if ( ftruncate(fd, (a2 >> 3) + 1) == -1 )
{
perror("Error truncating shared memory object");
exit(1);
}
result = mmap(0LL, (a2 >> 3) + 1, 3, 1, fd, 0LL);
if ( result == (void *)-1LL )
{
perror("Error mapping shared memory object");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func3(int fd, unsigned int a2)
{
void *result; // rax

if ( ftruncate(fd, (a2 >> 3) + 1) == -1 )
{
perror("Error truncating shared memory object");
exit(1);
}
result = mmap(0LL, (a2 >> 3) + 1, 3, 1, fd, 0LL);
if ( result == (void *)-1LL )
{
perror("Error mapping shared memory object");
exit(1);
}
return result;
}

original_code:
int * map_prime_shm(int shm_fd, unsigned int max) {
    int size;
    int * addr;
    size = (max/8) + 1;
    if(ftruncate(shm_fd, size) == -1) {
        perror("Error truncating shared memory object");
        exit(1);
    }
    if((addr = mmap(((void *)0), size, 0x1 | 0x2, 0x01, shm_fd, 0)) == ((void *) -1)) {
        perror("Error mapping shared memory object");
        exit(1);
    }
    return addr;
}

canonical_original_code:
int *map_prime_shm(int shm_fd, unsigned int max) {
    int size;
    int *addr;
    size = (max/8) + 1;
    if(ftruncate(shm_fd, size) == -1) {
        perror("Error truncating shared memory object");
        exit(1);
    }
    if((addr = mmap(((void *)0), size, 0x1 | 0x2, 0x01, shm_fd, 0)) == ((void *) -1)) {
        perror("Error mapping shared memory object");
        exit(1);
    }
    return addr;
}

variable_types: size int

variable_types:
addr
int *

return_type: int *


decompiled_code:
int __fastcall unmap_prime_shm(void *a1, unsigned int a2)
{
int result; // eax

result = munmap(a1, (a2 >> 3) + 1);
if ( result == -1 )
{
perror("Error unmapping shared memory region");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(void *a1, unsigned int a2)
{
int result; // eax

result = munmap(a1, (a2 >> 3) + 1);
if ( result == -1 )
{
perror("Error unmapping shared memory region");
exit(1);
}
return result;
}

original_code:
void unmap_prime_shm(void * addr, unsigned int max) {
    int size;
    size = (max/8) + 1;
    if(munmap(addr, size) == -1) {
        perror("Error unmapping shared memory region");
        exit(1);
    }
}

canonical_original_code:
void unmap_prime_shm(void *addr, unsigned int max) {
    int size;
    size = (max/8) + 1;
    if(munmap(addr, size) == -1) {
        perror("Error unmapping shared memory region");
        exit(1);
    }
}

variable_types:
addr
void *


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

result = shm_unlink("prime_shm");
if ( result == -1 )
{
perror("Error unlinking shared memory object");
exit(1);
}
return result;
}

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

result = shm_unlink("prime_shm");
if ( result == -1 )
{
perror("Error unlinking shared memory object");
exit(1);
}
return result;
}

original_code:
void unlink_prime_shm(void) {
    if(shm_unlink("prime_shm") == -1) {
        perror("Error unlinking shared memory object");
        exit(1);
    }
}

canonical_original_code:
void unlink_prime_shm() {
    if(shm_unlink("prime_shm") == -1) {
        perror("Error unlinking shared memory object");
        exit(1);
    }
}


decompiled_code:
void __noreturn sigint_handler()
{
unlink_prime_shm();
exit(0);
}

canonical_decompiled_code:
void __noreturn func6()
{
func5();
exit(0);
}

original_code:
void sigint_handler(int sig) {
    unlink_prime_shm();
    exit(0);
}

variable_types: sig int


decompiled_code:
__pid_t __fastcall create_child(int a1, int a2, unsigned int a3)
{
__pid_t result; // eax
_DWORD *v5; // rbp
int v6; // eax
void *v7; // rax

result = fork();
if ( result == -1 )
{
perror("Error forking child process");
exit(1);
}
if ( !result )
{
v5 = malloc(0x18uLL);
v6 = open_prime_shm();
v7 = map_prime_shm(v6, a3);
v5[3] = a1;
*(_QWORD *)v5 = v7;
v5[4] = a2;
v5[2] = a3;
thread_sieve(v5);
exit(0);
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func7(int a1, int a2, unsigned int a3)
{
__pid_t result; // eax
_DWORD *v5; // rbp
int v6; // eax
void *v7; // rax

result = fork();
if ( result == -1 )
{
perror("Error forking child process");
exit(1);
}
if ( !result )
{
v5 = malloc(0x18uLL);
v6 = func2();
v7 = func3(v6, a3);
v5[3] = a1;
*(_QWORD *)v5 = v7;
v5[4] = a2;
v5[2] = a3;
thread_sieve(v5);
exit(0);
}
return result;
}

original_code:
pid_t create_child(int low, int high, unsigned int max) {
    int shm_fd;
    pid_t pid;
    struct sieve_args * sa;
    switch (pid = fork()) {
        case -1:
            perror("Error forking child process");
            exit(1);
        case 0:
            sa = malloc(sizeof(struct sieve_args));
            shm_fd = open_prime_shm(max);
            sa->psbl_primes = map_prime_shm(shm_fd, max);
            sa->low = low;
            sa->high = high;
            sa->max = max;
            thread_sieve(sa);
            exit(0);
        default:
            return pid;
    }
}

canonical_original_code:
int create_child(int low, int high, unsigned int max) {
    int shm_fd;
    int pid;
    struct sieve_args *sa;
    switch (pid = fork()) {
        case -1:
            perror("Error forking child process");
            exit(1);
        case 0:
            sa = malloc(sizeof(struct sieve_args));
            shm_fd = open_prime_shm(max);
            sa->psbl_primes = map_prime_shm(shm_fd, max);
            sa->low = low;
            sa->high = high;
            sa->max = max;
            thread_sieve(sa);
            exit(0);
        default:
            return pid;
    }
}

variable_types: low int

variable_types: high int

variable_types: pid int

user_defined_types: struct sieve_args { int *psbl_primes; unsigned int max; unsigned int low; unsigned int high; }


decompiled_code:
void __fastcall wait_on_children(int a1, __pid_t *a2)
{
__pid_t *v2; // rbx

if ( a1 > 0 )
{
v2 = a2;
do
{
if ( waitpid(*v2, 0LL, 0) == -1 )
{
perror("Error waiting for child process");
exit(1);
}
++v2;
}
while ( v2 != &a2[a1 - 1 + 1] );
}
free(a2);
}

canonical_decompiled_code:
void __fastcall func8(int a1, __pid_t *a2)
{
__pid_t *v2; // rbx

if ( a1 > 0 )
{
v2 = a2;
do
{
if ( waitpid(*v2, 0LL, 0) == -1 )
{
perror("Error waiting for child process");
exit(1);
}
++v2;
}
while ( v2 != &a2[a1 - 1 + 1] );
}
free(a2);
}

original_code:
void wait_on_children(int num_procs, pid_t * pids) {
    int i;
    for(i=0; i<num_procs; i++) {
        if(waitpid(pids[i], ((void *)0), 0) == -1) {
            perror("Error waiting for child process");
            exit(1);
        }
    }
    free(pids);
}

canonical_original_code:
void wait_on_children(int num_procs, int *pids) {
    int i;
    for(i=0; i<num_procs; i++) {
        if(waitpid(pids[i], ((void *)0), 0) == -1) {
            perror("Error waiting for child process");
            exit(1);
        }
    }
    free(pids);
}

variable_types:
pids
int *

variable_types: i int


decompiled_code:
int install_sigint_handler()
{
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)sigint_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
return sigaction(2, &v1, 0LL);
}

canonical_decompiled_code:
int func9()
{
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)sigint_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
return sigaction(2, &v1, 0LL);
}

original_code:
void install_sigint_handler(void) {
    struct sigaction sigint;
    sigint.__sigaction_handler.sa_handler = sigint_handler;
    sigemptyset(&sigint.sa_mask);
    sigint.sa_flags = 0;
    sigaction(2, &sigint, ((void *)0));
}

canonical_original_code:
void install_sigint_handler() {
    struct sigaction sigint;
    sigint.__sigaction_handler.sa_handler = sigint_handler;
    sigemptyset(&sigint.sa_mask);
    sigint.sa_flags = 0;
    sigaction(2, &sigint, ((void *)0));
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


repo: NathanJCochran/ftp

decompiled_code:
ssize_t __fastcall send_message(int fd, void *buf)
{
size_t v2; // rbx
ssize_t result; // rax

v2 = (int)strlen((const char *)buf);
result = write(fd, buf, v2);
if ( result != v2 )
{
perror("Error writing message");
close(fd);
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func1(int fd, void *buf)
{
size_t v2; // rbx
ssize_t result; // rax

v2 = (int)strlen((const char *)buf);
result = write(fd, buf, v2);
if ( result != v2 )
{
perror("Error writing message");
close(fd);
exit(1);
}
return result;
}

original_code:
void send_message(int socket_fd, char * message) {
    int length = strlen(message);
    if(write(socket_fd, message, length) != length) {
        perror("Error writing message");
        close(socket_fd);
        exit(1);
    }
}

canonical_original_code:
void send_message(int socket_fd, char *message) {
    int length = strlen(message);
    if(write(socket_fd, message, length) != length) {
        perror("Error writing message");
        close(socket_fd);
        exit(1);
    }
}

variable_types:
message
char *

variable_types: length int


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

result = socket(2, 1, 0);
if ( result == -1 )
{
perror("Error creating socket");
exit(1);
}
return result;
}

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

result = socket(2, 1, 0);
if ( result == -1 )
{
perror("Error creating socket");
exit(1);
}
return result;
}

original_code:
int create_socket(void) {
    int socket_fd;
    if((socket_fd = socket(2, SOCK_STREAM, 0)) == -1) {
        perror("Error creating socket");
        exit(1);
    }
    return socket_fd;
}

canonical_original_code:
int create_socket() {
    int socket_fd;
    if((socket_fd = socket(2, SOCK_STREAM, 0)) == -1) {
        perror("Error creating socket");
        exit(1);
    }
    return socket_fd;
}


decompiled_code:
int __fastcall bind_socket(int fd, __int16 a2)
{
socklen_t v2; // edx
int result; // eax
struct sockaddr v4; // [rsp+0h] [rbp-18h] BYREF

v4.sa_family = 2;
*(_DWORD *)&v4.sa_data[2] = 0;
*(_WORD *)v4.sa_data = _bswap_16(a2);
result = bind(fd, &v4, v2);
if ( result == -1 )
{
perror("Error binding socket to address");
close(fd);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(int fd, __int16 a2)
{
socklen_t v2; // edx
int result; // eax
struct sockaddr v4; // [rsp+0h] [rbp-18h] BYREF

v4.sa_family = 2;
*(_DWORD *)&v4.sa_data[2] = 0;
*(_WORD *)v4.sa_data = func0(a2);
result = bind(fd, &v4, v2);
if ( result == -1 )
{
perror("Error binding socket to address");
close(fd);
exit(1);
}
return result;
}

original_code:
void bind_socket(int socket_fd, unsigned short port) {
    struct sockaddr_in address;
    address.sin_family = 2;
    address.sin_addr.s_addr = ((in_addr_t) 0x00000000);
    address.sin_port = htons(port);
    if(bind(socket_fd, (struct sockaddr *) &address, sizeof(address)) == -1) {
        perror("Error binding socket to address");
        close(socket_fd);
        exit(1);
    }
}

canonical_original_code:
void bind_socket(int socket_fd, unsigned short port) {
    struct sockaddr_in address;
    address.sin_family = 2;
    address.sin_addr.s_addr = ((unsigned int) 0x00000000);
    address.sin_port = htons(port);
    if(bind(socket_fd, (struct sockaddr *) &address, sizeof(address)) == -1) {
        perror("Error binding socket to address");
        close(socket_fd);
        exit(1);
    }
}

variable_types:
port
unsigned short

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
int __fastcall listen_socket(int fd)
{
int result; // eax

result = listen(fd, 5);
if ( result == -1 )
{
perror("Error listening for incoming connections");
close(fd);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(int fd)
{
int result; // eax

result = listen(fd, 5);
if ( result == -1 )
{
perror("Error listening for incoming connections");
close(fd);
exit(1);
}
return result;
}

original_code:
void listen_socket(int socket_fd) {
    if(listen(socket_fd, 5) == -1) {
        perror("Error listening for incoming connections");
        close(socket_fd);
        exit(1);
    }
}


original_code:
int parse_command(char * buffer, char * arg) {
    int command = -1;
    while(*buffer == ' ' || *buffer == '\t') {
        buffer++;
    }
    if(strncmp(buffer, "list ", 5) == 0 ||
        strncmp(buffer, "list\t", 5) == 0 ||
        strncmp(buffer, "list\n", 5) == 0) {
        buffer = buffer + 4;
        command = 1;
    }
    else if(strncmp(buffer, "get ", 4) == 0 ||
            strncmp(buffer, "get\t", 4) == 0 ||
            strncmp(buffer, "get\n", 4) == 0) {
        buffer = buffer + 3;
        command = 2;
    }
    else if(strncmp(buffer, "cd ", 3) == 0 ||
            strncmp(buffer, "cd\t", 3) == 0 ||
            strncmp(buffer, "cd\n", 3) == 0) {
        buffer = buffer + 2;
        command = 3;
    }
    else if(strncmp(buffer, "pwd ", 4) == 0 ||
            strncmp(buffer, "pwd\t", 4) == 0 ||
            strncmp(buffer, "pwd\n", 4) == 0) {
        buffer = buffer + 3;
        command = 4;
    }
    else if(strncmp(buffer, "exit ", 5) == 0 ||
            strncmp(buffer, "exit\t", 5) == 0 ||
            strncmp(buffer, "exit\n", 5) == 0) {
        buffer = buffer + 4;
        command = 0;
    }
    if(arg != ((void *)0)) {
        while(*buffer == ' ' || *buffer == '\t') {
            buffer++;
        }
        while((*buffer != ' ') && (*buffer != '\n') && (*buffer != '\r') && (*buffer != '\0')) {
            *arg = *buffer;
            arg++;
            buffer++;
        }
        *arg = '\0';
    }
    return command;
}

canonical_original_code:
int parse_command(char *buffer, char *arg) {
    int command = -1;
    while(*buffer == ' ' || *buffer == '\t') {
        buffer++;
    }
    if(strncmp(buffer, "list ", 5) == 0 ||
        strncmp(buffer, "list\t", 5) == 0 ||
        strncmp(buffer, "list\n", 5) == 0) {
        buffer = buffer + 4;
        command = 1;
    }
    else if(strncmp(buffer, "get ", 4) == 0 ||
            strncmp(buffer, "get\t", 4) == 0 ||
            strncmp(buffer, "get\n", 4) == 0) {
        buffer = buffer + 3;
        command = 2;
    }
    else if(strncmp(buffer, "cd ", 3) == 0 ||
            strncmp(buffer, "cd\t", 3) == 0 ||
            strncmp(buffer, "cd\n", 3) == 0) {
        buffer = buffer + 2;
        command = 3;
    }
    else if(strncmp(buffer, "pwd ", 4) == 0 ||
            strncmp(buffer, "pwd\t", 4) == 0 ||
            strncmp(buffer, "pwd\n", 4) == 0) {
        buffer = buffer + 3;
        command = 4;
    }
    else if(strncmp(buffer, "exit ", 5) == 0 ||
            strncmp(buffer, "exit\t", 5) == 0 ||
            strncmp(buffer, "exit\n", 5) == 0) {
        buffer = buffer + 4;
        command = 0;
    }
    if(arg != ((void *)0)) {
        while(*buffer == ' ' || *buffer == '\t') {
            buffer++;
        }
        while((*buffer != ' ') && (*buffer != '\n') && (*buffer != '\r') && (*buffer != '\0')) {
            *arg = *buffer;
            arg++;
            buffer++;
        }
        *arg = '\0';
    }
    return command;
}

variable_types:
buffer
char *

variable_types:
arg
char *

variable_types: command int


original_code:
int input_yn(char * prompt) {
    char buf[256];
    printf("%s", prompt);
    while(1) {
        fgets(buf, 256, stdin);
        if(strncmp(buf, "yes\n", 4) == 0 ||
            strncmp(buf, "yes ", 4) == 0 ||
            strncmp(buf, "y\n", 2) == 0 ||
            strncmp(buf, "y ", 2) == 0) {
            return 1;
        }
        if(strncmp(buf, "no\n", 3) == 0 ||
            strncmp(buf, "no ", 3) == 0 ||
             strncmp(buf, "n\n", 2) == 0 ||
             strncmp(buf, "n ", 2) == 0) {
            return 0;
        }
        printf("Invalid input.  Please try again.\n");
        printf("%s", prompt);
    }
    return -1;
}

canonical_original_code:
int input_yn(char *prompt) {
    char buf[256];
    printf("%s", prompt);
    while(1) {
        fgets(buf, 256, stdin);
        if(strncmp(buf, "yes\n", 4) == 0 ||
            strncmp(buf, "yes ", 4) == 0 ||
            strncmp(buf, "y\n", 2) == 0 ||
            strncmp(buf, "y ", 2) == 0) {
            return 1;
        }
        if(strncmp(buf, "no\n", 3) == 0 ||
            strncmp(buf, "no ", 3) == 0 ||
             strncmp(buf, "n\n", 2) == 0 ||
             strncmp(buf, "n ", 2) == 0) {
            return 0;
        }
        printf("Invalid input.  Please try again.\n");
        printf("%s", prompt);
    }
    return -1;
}

variable_types:
prompt
char *

variable_types:
buf
char [256]


decompiled_code:
void __fastcall control_connect(int fd, char *name)
{
struct addrinfo *v2; // rbx
struct addrinfo *pai; // [rsp+0h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+8h] [rbp-48h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_QWORD *)&req.ai_family = 0x100000002LL;
*(_OWORD *)&req.ai_canonname = 0LL;
if ( getaddrinfo(name, "30021", &req, &pai) )
{
perror("Error getting server address info");
puts("Please check that the server hostname is correct");
exit(1);
}
v2 = pai;
if ( !pai )
{
LABEL_7:
perror("Unable to open control connection with specified host");
close(fd);
exit(1);
}
while ( connect(fd, v2->ai_addr, v2->ai_addrlen) == -1 )
{
v2 = v2->ai_next;
if ( !v2 )
goto LABEL_7;
}
freeaddrinfo(pai);
}

canonical_decompiled_code:
void __fastcall func1(int fd, char *name)
{
struct addrinfo *v2; // rbx
struct addrinfo *pai; // [rsp+0h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+8h] [rbp-48h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_QWORD *)&req.ai_family = 0x100000002LL;
*(_OWORD *)&req.ai_canonname = 0LL;
if ( getaddrinfo(name, "30021", &req, &pai) )
{
perror("Error getting server address info");
puts("Please check that the server hostname is correct");
exit(1);
}
v2 = pai;
if ( !pai )
{
LABEL_7:
perror("Unable to open control connection with specified host");
close(fd);
exit(1);
}
while ( connect(fd, v2->ai_addr, v2->ai_addrlen) == -1 )
{
v2 = v2->ai_next;
if ( !v2 )
goto LABEL_7;
}
freeaddrinfo(pai);
}

original_code:
void control_connect(int ctrl_fd, char * host) {
    struct addrinfo hints, *results, *p;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = 2;
    hints.ai_socktype = SOCK_STREAM;
    if(getaddrinfo(host, "30021", &hints, &results) != 0) {
        perror("Error getting server address info");
        printf("Please check that the server hostname is correct\n");
        exit(1);
    }
    for(p = results; p != ((void *)0); p = p->ai_next) {
        if(connect(ctrl_fd, p->ai_addr, p->ai_addrlen) != -1) {
            freeaddrinfo(results);
            return;
        }
    }
    perror("Unable to open control connection with specified host");
    close(ctrl_fd);
    exit(1);
}

canonical_original_code:
void control_connect(int ctrl_fd, char *host) {
    struct addrinfo hints; struct addrinfo *results; struct addrinfo *p;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = 2;
    hints.ai_socktype = SOCK_STREAM;
    if(getaddrinfo(host, "30021", &hints, &results) != 0) {
        perror("Error getting server address info");
        printf("Please check that the server hostname is correct\n");
        exit(1);
    }
    for(p = results; p != ((void *)0); p = p->ai_next) {
        if(connect(ctrl_fd, p->ai_addr, p->ai_addrlen) != -1) {
            freeaddrinfo(results);
            return;
        }
    }
    perror("Unable to open control connection with specified host");
    close(ctrl_fd);
    exit(1);
}

variable_types:
host
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
int __fastcall receive_message(int fd)
{
char *v1; // rbx
int v2; // eax
int v3; // eax
char vars0[255]; // [rsp+0h] [rbp+0h] BYREF
char varsFF; // [rsp+FFh] [rbp+FFh] BYREF

v1 = vars0;
while ( 1 )
{
v3 = read(fd, v1, 1uLL);
if ( v3 == -1 )
{
perror("Error reading from control socket");
close(fd);
exit(1);
}
if ( !v3 )
{
printf("%s", vars0);
close(fd);
exit(0);
}
v2 = (unsigned __int8)*(v1 - 1) - 62;
if ( *(v1 - 1) == 62 )
v2 = (unsigned __int8)*v1 - 62;
if ( !v2 )
break;
if ( &varsFF == ++v1 )
return printf("%s", vars0);
}
vars0[(_DWORD)v1 + 1 - (unsigned int)vars0] = 0;
return printf("%s", vars0);
}

canonical_decompiled_code:
int __fastcall func2(int fd)
{
char *v1; // rbx
int v2; // eax
int v3; // eax
char vars0[255]; // [rsp+0h] [rbp+0h] BYREF
char varsFF; // [rsp+FFh] [rbp+FFh] BYREF

v1 = vars0;
while ( 1 )
{
v3 = read(fd, v1, 1uLL);
if ( v3 == -1 )
{
perror("Error reading from control socket");
close(fd);
exit(1);
}
if ( !v3 )
{
printf("%s", vars0);
close(fd);
exit(0);
}
v2 = (unsigned __int8)*(v1 - 1) - 62;
if ( *(v1 - 1) == 62 )
v2 = (unsigned __int8)*v1 - 62;
if ( !v2 )
break;
if ( &varsFF == ++v1 )
return printf("%s", vars0);
}
vars0[(_DWORD)v1 + 1 - (unsigned int)vars0] = 0;
return printf("%s", vars0);
}

original_code:
void receive_message(int ctrl_fd) {
    int i, num_read;
    char buffer[256];
    for(i=0; i<256 -1; i++) {
        if((num_read = read(ctrl_fd, &buffer[i], 1)) == -1) {
            perror("Error reading from control socket");
            close(ctrl_fd);
            exit(1);
        }
        if(num_read == 0)
            break;
        if(strncmp(&buffer[i-(strlen(">>")-1)], ">>", strlen(">>")) == 0) {
            buffer[i+1] = '\0';
            break;
        }
    }
    printf("%s", buffer);
    if(num_read == 0) {
        close(ctrl_fd);
        exit(0);
    }
}

canonical_original_code:
void receive_message(int ctrl_fd) {
    int i; int num_read;
    char buffer[256];
    for(i=0; i<256 -1; i++) {
        if((num_read = read(ctrl_fd, &buffer[i], 1)) == -1) {
            perror("Error reading from control socket");
            close(ctrl_fd);
            exit(1);
        }
        if(num_read == 0)
            break;
        if(strncmp(&buffer[i-(strlen(">>")-1)], ">>", strlen(">>")) == 0) {
            buffer[i+1] = '\0';
            break;
        }
    }
    printf("%s", buffer);
    if(num_read == 0) {
        close(ctrl_fd);
        exit(0);
    }
}

variable_types:
buffer
char [256]


decompiled_code:
char *__fastcall get_request(int fd, char *s)
{
char *result; // rax

result = fgets(s, 256, stdin);
if ( !result )
{
perror("Error reading user input");
close(fd);
exit(1);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func3(int fd, char *s)
{
char *result; // rax

result = fgets(s, 256, stdin);
if ( !result )
{
perror("Error reading user input");
close(fd);
exit(1);
}
return result;
}

original_code:
void get_request(int ctrl_fd, char * response) {
    if(fgets(response, 256, stdin) == ((void *)0)) {
        perror("Error reading user input");
        close(ctrl_fd);
        exit(1);
    }
}

canonical_original_code:
void get_request(int ctrl_fd, char *response) {
    if(fgets(response, 256, stdin) == ((void *)0)) {
        perror("Error reading user input");
        close(ctrl_fd);
        exit(1);
    }
}

variable_types:
response
char *


decompiled_code:
__int64 __fastcall open_data_connection(int fd)
{
__int64 v1; // r12
unsigned int v2; // ebp
socklen_t len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr v6; // [rsp+20h] [rbp-28h] BYREF

len = 16;
if ( getpeername(fd, &addr, &len) == -1 )
{
perror("Error getting peer's address");
close(fd);
exit(1);
}
v1 = (unsigned int)create_socket();
bind_socket(v1, 30020LL);
listen_socket((unsigned int)v1);
while ( 1 )
{
len = 16;
v2 = accept(v1, &v6, &len);
if ( v2 == -1 )
{
perror("Error accepting incoming data connection");
close(fd);
close(v1);
exit(1);
}
if ( *(_DWORD *)&addr.sa_data[2] == *(_DWORD *)&v6.sa_data[2] )
break;
close(v2);
}
close(v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd)
{
__int64 v1; // r12
unsigned int v2; // ebp
socklen_t len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr v6; // [rsp+20h] [rbp-28h] BYREF

len = 16;
if ( getpeername(fd, &addr, &len) == -1 )
{
perror("Error getting peer's address");
close(fd);
exit(1);
}
v1 = (unsigned int)create_socket();
bind_socket(v1, 30020LL);
listen_socket((unsigned int)v1);
while ( 1 )
{
len = 16;
v2 = accept(v1, &v6, &len);
if ( v2 == -1 )
{
perror("Error accepting incoming data connection");
close(fd);
close(v1);
exit(1);
}
if ( *(_DWORD *)&addr.sa_data[2] == *(_DWORD *)&v6.sa_data[2] )
break;
close(v2);
}
close(v1);
return v2;
}

original_code:
int open_data_connection(int ctrl_fd) {
    struct sockaddr_in ctrl_address, data_address;
    int passive_fd, data_fd;
    unsigned int length;
    length = sizeof(ctrl_address);
    if(getpeername(ctrl_fd, (struct sockaddr *) &ctrl_address, &length) == -1) {
        perror("Error getting peer's address");
        close(ctrl_fd);
        exit(1);
    }
    passive_fd = create_socket();
    bind_socket(passive_fd, 30020);
    listen_socket(passive_fd);
    while(1) {
        length = sizeof(data_address);
        if((data_fd = accept(passive_fd, (struct sockaddr *) &data_address, &length)) == -1) {
            perror("Error accepting incoming data connection");
            close(ctrl_fd);
            close(passive_fd);
            exit(1);
        }
        if(ctrl_address.sin_addr.s_addr == data_address.sin_addr.s_addr) {
            break;
        }
        close(data_fd);
    }
    close(passive_fd);
    return data_fd;
}

canonical_original_code:
int open_data_connection(int ctrl_fd) {
    struct sockaddr_in ctrl_address; struct sockaddr_in data_address;
    int passive_fd; int data_fd;
    unsigned int length;
    length = sizeof(ctrl_address);
    if(getpeername(ctrl_fd, (struct sockaddr *) &ctrl_address, &length) == -1) {
        perror("Error getting peer's address");
        close(ctrl_fd);
        exit(1);
    }
    passive_fd = create_socket();
    bind_socket(passive_fd, 30020);
    listen_socket(passive_fd);
    while(1) {
        length = sizeof(data_address);
        if((data_fd = accept(passive_fd, (struct sockaddr *) &data_address, &length)) == -1) {
            perror("Error accepting incoming data connection");
            close(ctrl_fd);
            close(passive_fd);
            exit(1);
        }
        if(ctrl_address.sin_addr.s_addr == data_address.sin_addr.s_addr) {
            break;
        }
        close(data_fd);
    }
    close(passive_fd);
    return data_fd;
}

variable_types:
length
unsigned int


decompiled_code:
ssize_t __fastcall receive_listing(int fd)
{
ssize_t result; // rax
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

while ( 1 )
{
result = read(fd, v2, 0x100uLL);
if ( (int)result <= 0 )
break;
printf("%s", v2);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func5(int fd)
{
ssize_t result; // rax
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

while ( 1 )
{
result = read(fd, v2, 0x100uLL);
if ( (int)result <= 0 )
break;
printf("%s", v2);
}
return result;
}

original_code:
void receive_listing(int data_fd) {
    char buffer[256];
    int num_read;
    while((num_read = read(data_fd, buffer, 256)) > 0) {
        printf("%s", buffer);
    }
}


decompiled_code:
int __fastcall receive_file(int fd, char *file)
{
int result; // eax
int v4; // ebx
int v5; // er12
int v6; // eax
const char *v7; // rdi
_BYTE v8[4136]; // [rsp+0h] [rbp-1028h] BYREF

result = read(fd, v8, 0x1000uLL);
if ( result > 0 )
{
v4 = result;
v5 = open(file, 193, 432LL);
if ( v5 != -1 )
goto LABEL_5;
if ( *_errno_location() == 17 )
{
if ( !(unsigned int)input_yn("File already exists. Overwrite? ") )
{
printf("File not received: %s\n", file);
return close(fd);
}
v5 = open(file, 1, 438LL);
if ( v5 != -1 )
{
LABEL_5:
while ( write(v5, v8, v4) != -1 )
{
v6 = read(fd, v8, 0x1000uLL);
v4 = v6;
if ( v6 <= 0 )
{
if ( v6 != -1 )
return printf("File received: %s\n", file);
LABEL_16:
v7 = "Error reading file from data connection";
LABEL_15:
perror(v7);
close(fd);
exit(1);
}
}
v7 = "Error writing to file";
goto LABEL_15;
}
}
v7 = "Error creating file";
goto LABEL_15;
}
if ( result == -1 )
goto LABEL_16;
return result;
}

canonical_decompiled_code:
int __fastcall func6(int fd, char *file)
{
int result; // eax
int v4; // ebx
int v5; // er12
int v6; // eax
const char *v7; // rdi
_BYTE v8[4136]; // [rsp+0h] [rbp-1028h] BYREF

result = read(fd, v8, 0x1000uLL);
if ( result > 0 )
{
v4 = result;
v5 = open(file, 193, 432LL);
if ( v5 != -1 )
goto LABEL_5;
if ( *_errno_location() == 17 )
{
if ( !(unsigned int)input_yn("File already exists. Overwrite? ") )
{
printf("File not received: %s\n", file);
return close(fd);
}
v5 = open(file, 1, 438LL);
if ( v5 != -1 )
{
LABEL_5:
while ( write(v5, v8, v4) != -1 )
{
v6 = read(fd, v8, 0x1000uLL);
v4 = v6;
if ( v6 <= 0 )
{
if ( v6 != -1 )
return printf("File received: %s\n", file);
LABEL_16:
v7 = "Error reading file from data connection";
LABEL_15:
perror(v7);
close(fd);
exit(1);
}
}
v7 = "Error writing to file";
goto LABEL_15;
}
}
v7 = "Error creating file";
goto LABEL_15;
}
if ( result == -1 )
goto LABEL_16;
return result;
}

original_code:
void receive_file(int data_fd, char * filename) {
    int file_fd, num_read;
    char buffer[4096];
    if((num_read=read(data_fd, buffer, 4096)) > 0) {
        if((file_fd = open(filename, 0100 | 0200 | 01, 0660)) == -1) {
            if((*__errno_location ()) == 17) {
                if(input_yn("File already exists. Overwrite? ")) {
                    if((file_fd = open(filename, 01, 0666)) == -1) {
                        perror("Error creating file");
                        close(data_fd);
                        exit(1);
                    }
                }
                else {
                    printf("File not received: %s\n", filename);
                    close(data_fd);
                    return;
                }
            }
            else {
                perror("Error creating file");
                close(data_fd);
                exit(1);
            }
        }
        do {
            if(write(file_fd, buffer, num_read) == -1) {
                perror("Error writing to file");
                close(data_fd);
                exit(1);
            }
        } while((num_read = read(data_fd, buffer, 4096)) > 0);
        if(num_read == -1) {
            perror("Error reading file from data connection");
            close(data_fd);
            exit(1);
        }
        printf("File received: %s\n", filename);
    }
    if(num_read == -1) {
        perror("Error reading file from data connection");
        close(data_fd);
        exit(1);
    }
}

canonical_original_code:
void receive_file(int data_fd, char *filename) {
    int file_fd; int num_read;
    char buffer[4096];
    if((num_read=read(data_fd, buffer, 4096)) > 0) {
        if((file_fd = open(filename, 0100 | 0200 | 01, 0660)) == -1) {
            if((*__errno_location ()) == 17) {
                if(input_yn("File already exists. Overwrite? ")) {
                    if((file_fd = open(filename, 01, 0666)) == -1) {
                        perror("Error creating file");
                        close(data_fd);
                        exit(1);
                    }
                }
                else {
                    printf("File not received: %s\n", filename);
                    close(data_fd);
                    return;
                }
            }
            else {
                perror("Error creating file");
                close(data_fd);
                exit(1);
            }
        }
        do {
            if(write(file_fd, buffer, num_read) == -1) {
                perror("Error writing to file");
                close(data_fd);
                exit(1);
            }
        } while((num_read = read(data_fd, buffer, 4096)) > 0);
        if(num_read == -1) {
            perror("Error reading file from data connection");
            close(data_fd);
            exit(1);
        }
        printf("File received: %s\n", filename);
    }
    if(num_read == -1) {
        perror("Error reading file from data connection");
        close(data_fd);
        exit(1);
    }
}

variable_types:
filename
char *

variable_types:
buffer
char [4096]


decompiled_code:
int __fastcall make_request(int fd, __int64 a2)
{
int result; // eax
int v3; // ebp
int v4; // ebp
char v5[280]; // [rsp+0h] [rbp-118h] BYREF

send_message((unsigned int)control_fd, a2);
result = parse_command(a2, v5);
if ( result == 2 )
{
v3 = open_data_connection(fd);
receive_file(v3, v5);
result = close(v3);
}
else if ( result == 1 )
{
v4 = open_data_connection(fd);
receive_listing(v4);
result = close(v4);
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(int fd, __int64 a2)
{
int result; // eax
int v3; // ebp
int v4; // ebp
char v5[280]; // [rsp+0h] [rbp-118h] BYREF

send_message((unsigned int)control_fd, a2);
result = parse_command(a2, v5);
if ( result == 2 )
{
v3 = func4(fd);
func6(v3, v5);
result = close(v3);
}
else if ( result == 1 )
{
v4 = func4(fd);
func5(v4);
result = close(v4);
}
return result;
}

original_code:
void make_request(int ctrl_fd, char * request) {
    int data_fd, command;
    char arg[256];
    send_message(control_fd, request);
    command = parse_command(request, arg);
    if(command == 2) {
        data_fd = open_data_connection(ctrl_fd);
        receive_file(data_fd, arg);
        close(data_fd);
    }
    else if(command == 1) {
        data_fd = open_data_connection(ctrl_fd);
        receive_listing(data_fd);
        close(data_fd);
    }
}

canonical_original_code:
void make_request(int ctrl_fd, char *request) {
    int data_fd; int command;
    char arg[256];
    send_message(control_fd, request);
    command = parse_command(request, arg);
    if(command == 2) {
        data_fd = open_data_connection(ctrl_fd);
        receive_file(data_fd, arg);
        close(data_fd);
    }
    else if(command == 1) {
        data_fd = open_data_connection(ctrl_fd);
        receive_listing(data_fd);
        close(data_fd);
    }
}

variable_types:
request
char *

variable_types:
arg
char [256]


decompiled_code:
int install_signal_handlers()
{
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)signal_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
sigaction(2, &v1, 0LL);
return sigaction(15, &v1, 0LL);
}

canonical_decompiled_code:
int func8()
{
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)signal_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
sigaction(2, &v1, 0LL);
return sigaction(15, &v1, 0LL);
}

original_code:
void install_signal_handlers(void) {
    struct sigaction sig;
    sig.__sigaction_handler.sa_handler = signal_handler;
    sigemptyset(&sig.sa_mask);
    sig.sa_flags = 0;
    sigaction(2, &sig, ((void *)0));
    sigaction(15, &sig, ((void *)0));
}

canonical_original_code:
void install_signal_handlers() {
    struct sigaction sig;
    sig.__sigaction_handler.sa_handler = signal_handler;
    sigemptyset(&sig.sa_mask);
    sig.sa_flags = 0;
    sigaction(2, &sig, ((void *)0));
    sigaction(15, &sig, ((void *)0));
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }


decompiled_code:
void __noreturn signal_handler()
{
puts("\nClosing connection to server...");
send_message((unsigned int)control_fd, "exit\n");
close(control_fd);
puts("Connection closed");
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
puts("\nClosing connection to server...");
send_message((unsigned int)control_fd, "exit\n");
close(control_fd);
puts("Connection closed");
exit(0);
}

original_code:
void signal_handler(int sig) {
    printf("\nClosing connection to server...\n");
    send_message(control_fd, "exit\n");
    close(control_fd);
    printf("Connection closed\n");
    exit(0);
}

variable_types: sig int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
char *v4; // rsi
char v6[264]; // [rsp+0h] [rbp-108h] BYREF

if ( argc != 2 )
{
printf("Usage:\n\t%s <server hostname>\n", *argv);
exit(0);
}
install_signal_handlers();
v3 = create_socket();
v4 = (char *)argv[1];
control_fd = v3;
control_connect(v3, v4);
do
{
receive_message(control_fd);
get_request(control_fd, v6);
make_request(control_fd, (__int64)v6);
}
while ( (unsigned int)parse_command(v6, 0LL) );
close(control_fd);
puts("Connection closed");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
char *v4; // rsi
char v6[264]; // [rsp+0h] [rbp-108h] BYREF

if ( argc != 2 )
{
printf("Usage:\n\t%s <server hostname>\n", *argv);
exit(0);
}
func8();
v3 = create_socket();
v4 = (char *)argv[1];
control_fd = v3;
func1(v3, v4);
do
{
func2(control_fd);
func3(control_fd, v6);
func7(control_fd, (__int64)v6);
}
while ( (unsigned int)parse_command(v6, 0LL) );
close(control_fd);
puts("Connection closed");
return 0;
}

original_code:
int main(int argc, char * argv[]) {
    char request[256];
    if(argc != 2) {
        printf("Usage:\n\t%s <server hostname>\n", argv[0]);
        exit(0);
    }
    install_signal_handlers();
    control_fd = create_socket();
    control_connect(control_fd, argv[1]);
    do {
        receive_message(control_fd);
        get_request(control_fd, request);
        make_request(control_fd, request);
    } while(parse_command(request, ((void *)0)) != 0);
    close(control_fd);
    printf("Connection closed\n");
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    char request[256];
    if(argc != 2) {
        printf("Usage:\n\t%s <server hostname>\n", argv[0]);
        exit(0);
    }
    install_signal_handlers();
    control_fd = create_socket();
    control_connect(control_fd, argv[1]);
    do {
        receive_message(control_fd);
        get_request(control_fd, request);
        make_request(control_fd, request);
    } while(parse_command(request, ((void *)0)) != 0);
    close(control_fd);
    printf("Connection closed\n");
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
request
char [256]


decompiled_code:
__int64 start_server()
{
__int64 v0; // r12

v0 = (unsigned int)create_socket();
bind_socket(v0, 30021LL);
listen_socket((unsigned int)v0);
puts("Server started");
puts("Listening for incoming connections...");
return (unsigned int)v0;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // r12

v0 = (unsigned int)create_socket();
bind_socket(v0, 30021LL);
listen_socket((unsigned int)v0);
puts("Server started");
puts("Listening for incoming connections...");
return (unsigned int)v0;
}

original_code:
int start_server(void) {
    int fd;
    fd = create_socket();
    bind_socket(fd, 30021);
    listen_socket(fd);
    printf("Server started\n");
    printf("Listening for incoming connections...\n");
    return fd;
}

canonical_original_code:
int start_server() {
    int fd;
    fd = create_socket();
    bind_socket(fd, 30021);
    listen_socket(fd);
    printf("Server started\n");
    printf("Listening for incoming connections...\n");
    return fd;
}

variable_types: fd int


decompiled_code:
__int64 __fastcall accept_connection(int fd)
{
int v2; // eax
unsigned int v3; // er12
const char *v5; // rdi
socklen_t addr_len; // [rsp+Ch] [rbp-12Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-128h] BYREF
char buf[280]; // [rsp+20h] [rbp-118h] BYREF

addr_len = 16;
v2 = accept(fd, &addr, &addr_len);
if ( v2 == -1 )
{
v5 = "Error accepting incoming connection";
goto LABEL_5;
}
v3 = v2;
if ( !inet_ntop(2, &addr.sa_data[2], buf, 0x100u) )
{
v5 = "Error converting ip address to string";
LABEL_5:
perror(v5);
close(fd);
exit(1);
}
printf("Connection accepted: %s\n", buf);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd)
{
int v2; // eax
unsigned int v3; // er12
const char *v5; // rdi
socklen_t addr_len; // [rsp+Ch] [rbp-12Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-128h] BYREF
char buf[280]; // [rsp+20h] [rbp-118h] BYREF

addr_len = 16;
v2 = accept(fd, &addr, &addr_len);
if ( v2 == -1 )
{
v5 = "Error accepting incoming connection";
goto LABEL_5;
}
v3 = v2;
if ( !inet_ntop(2, &addr.sa_data[2], buf, 0x100u) )
{
v5 = "Error converting ip address to string";
LABEL_5:
perror(v5);
close(fd);
exit(1);
}
printf("Connection accepted: %s\n", buf);
return v3;
}

original_code:
int accept_connection(int socket_fd) {
    struct sockaddr_in address;
    char address_str[256];
    int connection_fd;
    unsigned int length;
    length = sizeof(address);
    if((connection_fd = accept(socket_fd, (struct sockaddr *) &address, &length)) == -1) {
        perror("Error accepting incoming connection");
        close(socket_fd);
        exit(1);
    }
    if(inet_ntop(2, &address.sin_addr, address_str, 256) == ((void *)0)) {
        perror("Error converting ip address to string");
        close(socket_fd);
        exit(1);
    }
    printf("Connection accepted: %s\n", address_str);
    return connection_fd;
}

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }


decompiled_code:
__int64 __fastcall get_command(int fd, __int64 a2)
{
int v2; // ebx
char v3; // al
char buf[296]; // [rsp+0h] [rbp-128h] BYREF

v2 = 0;
LABEL_2:
if ( !v2 )
goto LABEL_9;
while ( 1 )
{
if ( read(fd, &buf[v2], 1uLL) == -1 )
{
perror("Error reading from socket");
exit(1);
}
v3 = buf[v2];
if ( v3 != 10 && v3 != 13 )
{
if ( ++v2 == 256 )
return parse_command(buf, a2);
goto LABEL_2;
}
if ( v2 )
return parse_command(buf, a2);
LABEL_9:
v2 = 0;
send_message((unsigned int)fd, ">>");
}
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, __int64 a2)
{
int v2; // ebx
char v3; // al
char buf[296]; // [rsp+0h] [rbp-128h] BYREF

v2 = 0;
LABEL_2:
if ( !v2 )
goto LABEL_9;
while ( 1 )
{
if ( read(fd, &buf[v2], 1uLL) == -1 )
{
perror("Error reading from socket");
exit(1);
}
v3 = buf[v2];
if ( v3 != 10 && v3 != 13 )
{
if ( ++v2 == 256 )
return parse_command(buf, a2);
goto LABEL_2;
}
if ( v2 )
return parse_command(buf, a2);
LABEL_9:
v2 = 0;
send_message((unsigned int)fd, ">>");
}
}

original_code:
int get_command(int ctrl_fd, char * arg) {
    int i;
    char buffer[256];
    for(i=0; i<256; i++) {
        if(i==0) {
            send_message(ctrl_fd, ">>");
        }
        if(read(ctrl_fd, &buffer[i], 1) == -1) {
            perror("Error reading from socket");
            exit(1);
        };
        if((buffer[i] == '\n') || (buffer[i] == '\r')) {
            if(i==0) {
                i--;
                continue;
            }
            else {
                break;
            }
        }
    }
    return parse_command(buffer, arg);
}

canonical_original_code:
int get_command(int ctrl_fd, char *arg) {
    int i;
    char buffer[256];
    for(i=0; i<256; i++) {
        if(i==0) {
            send_message(ctrl_fd, ">>");
        }
        if(read(ctrl_fd, &buffer[i], 1) == -1) {
            perror("Error reading from socket");
            exit(1);
        };
        if((buffer[i] == '\n') || (buffer[i] == '\r')) {
            if(i==0) {
                i--;
                continue;
            }
            else {
                break;
            }
        }
    }
    return parse_command(buffer, arg);
}

variable_types:
arg
char *

variable_types: i int


decompiled_code:
__int64 __fastcall data_connect(int fd)
{
unsigned int v2; // er12
const char *v4; // rdi
socklen_t len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

len = 16;
if ( getpeername(fd, &addr, &len) == -1 )
{
v4 = "Error getting peer's address";
goto LABEL_5;
}
*(_WORD *)addr.sa_data = _bswap_16_constprop_0();
v2 = create_socket();
if ( connect(v2, &addr, 0x10u) == -1 )
{
v4 = "Error opening data connection";
LABEL_5:
perror(v4);
close(fd);
exit(1);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd)
{
unsigned int v2; // er12
const char *v4; // rdi
socklen_t len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

len = 16;
if ( getpeername(fd, &addr, &len) == -1 )
{
v4 = "Error getting peer's address";
goto LABEL_5;
}
*(_WORD *)addr.sa_data = func1();
v2 = create_socket();
if ( connect(v2, &addr, 0x10u) == -1 )
{
v4 = "Error opening data connection";
LABEL_5:
perror(v4);
close(fd);
exit(1);
}
return v2;
}

original_code:
int data_connect(int ctrl_fd) {
    struct sockaddr_in address;
    int data_fd;
    unsigned int length;
    length = sizeof(address);
    if(getpeername(ctrl_fd, (struct sockaddr *) &address, &length) == -1) {
        perror("Error getting peer's address");
        close(ctrl_fd);
        exit(1);
    }
    address.sin_port = htons(30020);
    data_fd = create_socket();
    if(connect(data_fd, (struct sockaddr *) &address, sizeof(address)) == -1) {
        perror("Error opening data connection");
        close(ctrl_fd);
        exit(1);
    }
    return data_fd;
}


decompiled_code:
int __fastcall send_file(unsigned int a1, const char *a2)
{
int v2; // er12
int v3; // ebp
int v4; // eax
int result; // eax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v2 = data_connect(a1);
v3 = open(a2, 0);
if ( v3 == -1 )
{
if ( *_errno_location() != 2 )
{
perror("Error opening file");
exit(1);
}
send_message(a1, "Invalid filename: file does not exist\n");
result = close(v2);
}
else
{
while ( 1 )
{
v4 = read(v3, buf, 1uLL);
if ( v4 <= 0 )
break;
if ( write(v2, buf, 1uLL) != 1 )
{
perror("Error writing to data socket");
exit(1);
}
}
if ( v4 == -1 )
{
perror("Error reading from file");
exit(1);
}
close(v3);
result = close(v2);
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(unsigned int a1, const char *a2)
{
int v2; // er12
int v3; // ebp
int v4; // eax
int result; // eax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v2 = func5(a1);
v3 = open(a2, 0);
if ( v3 == -1 )
{
if ( *_errno_location() != 2 )
{
perror("Error opening file");
exit(1);
}
send_message(a1, "Invalid filename: file does not exist\n");
result = close(v2);
}
else
{
while ( 1 )
{
v4 = read(v3, buf, 1uLL);
if ( v4 <= 0 )
break;
if ( write(v2, buf, 1uLL) != 1 )
{
perror("Error writing to data socket");
exit(1);
}
}
if ( v4 == -1 )
{
perror("Error reading from file");
exit(1);
}
close(v3);
result = close(v2);
}
return result;
}

original_code:
void send_file(int ctrl_fd, char * filename) {
    int data_fd, file_fd, num_read;
    char c;
    data_fd = data_connect(ctrl_fd);
    if((file_fd = open(filename, 00)) == -1) {
        if((*__errno_location ()) == 2) {
            send_message(ctrl_fd, "Invalid filename: file does not exist\n");
            close(data_fd);
            return;
        }
        perror("Error opening file");
        exit(1);
    }
    while((num_read = read(file_fd, &c, 1)) > 0) {
        if(write(data_fd, &c, 1) != 1) {
            perror("Error writing to data socket");
            exit(1);
        }
    }
    if(num_read == -1) {
        perror("Error reading from file");
        exit(1);
    }
    close(file_fd);
    close(data_fd);
}

canonical_original_code:
void send_file(int ctrl_fd, char *filename) {
    int data_fd; int file_fd; int num_read;
    char c;
    data_fd = data_connect(ctrl_fd);
    if((file_fd = open(filename, 00)) == -1) {
        if((*__errno_location ()) == 2) {
            send_message(ctrl_fd, "Invalid filename: file does not exist\n");
            close(data_fd);
            return;
        }
        perror("Error opening file");
        exit(1);
    }
    while((num_read = read(file_fd, &c, 1)) > 0) {
        if(write(data_fd, &c, 1) != 1) {
            perror("Error writing to data socket");
            exit(1);
        }
    }
    if(num_read == -1) {
        perror("Error reading from file");
        exit(1);
    }
    close(file_fd);
    close(data_fd);
}

variable_types: c char


decompiled_code:
__int64 __fastcall show_cwd(int fd)
{
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

if ( !getcwd(v2, 0x100uLL) )
{
perror("Error getting the current working directory");
close(fd);
exit(1);
}
send_message((unsigned int)fd, "Remote working directory: ");
send_message((unsigned int)fd, v2);
return send_message((unsigned int)fd, "\n");
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd)
{
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

if ( !getcwd(v2, 0x100uLL) )
{
perror("Error getting the current working directory");
close(fd);
exit(1);
}
send_message((unsigned int)fd, "Remote working directory: ");
send_message((unsigned int)fd, v2);
return send_message((unsigned int)fd, "\n");
}

original_code:
void show_cwd(int ctrl_fd) {
    char buf[256];
    if(getcwd(buf, 256) == ((void *)0)) {
        perror("Error getting the current working directory");
        close(ctrl_fd);
        exit(1);
    }
    send_message(ctrl_fd, "Remote working directory: ");
    send_message(ctrl_fd, buf);
    send_message(ctrl_fd, "\n");
}

variable_types:
buf
char [256]


decompiled_code:
__int64 __fastcall change_directory(int fd, char *path)
{
int v2; // eax

if ( chdir(path) != -1 )
return show_cwd(fd);
v2 = *_errno_location();
if ( v2 == 13 )
return send_message((unsigned int)fd, "Error: permission denied\n");
if ( v2 == 20 || v2 == 2 )
return send_message((unsigned int)fd, "Error: invalid directory\n");
return send_message((unsigned int)fd, "Error: could not change directories\n");
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, char *path)
{
int v2; // eax

if ( chdir(path) != -1 )
return func8(fd);
v2 = *_errno_location();
if ( v2 == 13 )
return send_message((unsigned int)fd, "Error: permission denied\n");
if ( v2 == 20 || v2 == 2 )
return send_message((unsigned int)fd, "Error: invalid directory\n");
return send_message((unsigned int)fd, "Error: could not change directories\n");
}

original_code:
void change_directory(int ctrl_fd, char * directory) {
    if(chdir(directory) == -1) {
        if((*__errno_location ()) == 13) {
            send_message(ctrl_fd, "Error: permission denied\n");
        }
        else if((*__errno_location ()) == 20 || (*__errno_location ()) == 2) {
            send_message(ctrl_fd, "Error: invalid directory\n");
        }
        else {
            send_message(ctrl_fd, "Error: could not change directories\n");
        }
    }
    else {
        show_cwd(ctrl_fd);
    }
}

canonical_original_code:
void change_directory(int ctrl_fd, char *directory) {
    if(chdir(directory) == -1) {
        if((*__errno_location ()) == 13) {
            send_message(ctrl_fd, "Error: permission denied\n");
        }
        else if((*__errno_location ()) == 20 || (*__errno_location ()) == 2) {
            send_message(ctrl_fd, "Error: invalid directory\n");
        }
        else {
            send_message(ctrl_fd, "Error: could not change directories\n");
        }
    }
    else {
        show_cwd(ctrl_fd);
    }
}

variable_types:
directory
char *


decompiled_code:
__int64 __fastcall handle_request(__int64 fd)
{
__int64 result; // rax
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

send_message(fd, "Welcome to Nathan's File Transfer Program\nCommands:\n\t");
send_message((unsigned int)fd, "exit\t- end the ftp session\n\t");
send_message((unsigned int)fd, "pwd\t- print working directory\n\t");
send_message((unsigned int)fd, "list\t- view files in current directory\n\t");
send_message((unsigned int)fd, "cd <directory>\t- change directory\n\t");
send_message((unsigned int)fd, "get <filename>\t- get the specified file\n");
while ( 2 )
{
result = get_command(fd, (__int64)v2);
if ( (_DWORD)result )
{
LABEL_3:
switch ( (int)result )
{
case -1:
send_message((unsigned int)fd, "Invalid command\n");
continue;
case 1:
list_directories(fd);
continue;
case 2:
send_file(fd, v2);
continue;
case 3:
change_directory(fd, v2);
continue;
case 4:
show_cwd(fd);
result = get_command(fd, (__int64)v2);
if ( (_DWORD)result )
goto LABEL_3;
break;
default:
continue;
}
}
return result;
}
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 fd)
{
__int64 result; // rax
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

send_message(fd, "Welcome to Nathan's File Transfer Program\nCommands:\n\t");
send_message((unsigned int)fd, "exit\t- end the ftp session\n\t");
send_message((unsigned int)fd, "pwd\t- print working directory\n\t");
send_message((unsigned int)fd, "list\t- view files in current directory\n\t");
send_message((unsigned int)fd, "cd <directory>\t- change directory\n\t");
send_message((unsigned int)fd, "get <filename>\t- get the specified file\n");
while ( 2 )
{
result = func4(fd, (__int64)v2);
if ( (_DWORD)result )
{
LABEL_3:
switch ( (int)result )
{
case -1:
send_message((unsigned int)fd, "Invalid command\n");
continue;
case 1:
func6(fd);
continue;
case 2:
func7(fd, v2);
continue;
case 3:
func9(fd, v2);
continue;
case 4:
func8(fd);
result = func4(fd, (__int64)v2);
if ( (_DWORD)result )
goto LABEL_3;
break;
default:
continue;
}
}
return result;
}
}

original_code:
void handle_request(int ctrl_fd) {
    int command;
    char arg[256];
    send_message(ctrl_fd, "Welcome to Nathan's File Transfer Program\nCommands:\n\t");
    send_message(ctrl_fd, "exit\t- end the ftp session\n\t");
    send_message(ctrl_fd, "pwd\t- print working directory\n\t");
    send_message(ctrl_fd, "list\t- view files in current directory\n\t");
    send_message(ctrl_fd, "cd <directory>\t- change directory\n\t");
    send_message(ctrl_fd, "get <filename>\t- get the specified file\n");
    while((command = get_command(ctrl_fd, arg)) != 0) {
        switch(command) {
            case -1:
                send_message(ctrl_fd, "Invalid command\n");
                break;
            case 1:
                list_directories(ctrl_fd);
                break;
            case 2:
                send_file(ctrl_fd, arg);
                break;
            case 3:
                change_directory(ctrl_fd, arg);
                break;
            case 4:
                show_cwd(ctrl_fd);
                break;
        }
    }
}

variable_types: command int


decompiled_code:
int install_sigint_handler()
{
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)signal_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
sigaction(2, &v1, 0LL);
return sigaction(15, &v1, 0LL);
}

canonical_decompiled_code:
int func11()
{
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)signal_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
sigaction(2, &v1, 0LL);
return sigaction(15, &v1, 0LL);
}

original_code:
void install_sigint_handler(void) {
    struct sigaction sig;
    sig.__sigaction_handler.sa_handler = signal_handler;
    sigemptyset(&sig.sa_mask);
    sig.sa_flags = 0;
    sigaction(2, &sig, ((void *)0));
    sigaction(15, &sig, ((void *)0));
}

canonical_original_code:
void install_sigint_handler() {
    struct sigaction sig;
    sig.__sigaction_handler.sa_handler = signal_handler;
    sigemptyset(&sig.sa_mask);
    sig.sa_flags = 0;
    sigaction(2, &sig, ((void *)0));
    sigaction(15, &sig, ((void *)0));
}

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }

user_defined_types: union sigval { int sival_int; void *sival_ptr; }


decompiled_code:
void __noreturn signal_handler()
{
if ( control_fd != -1 )
{
puts("Closing client connection...");
send_message((unsigned int)control_fd, "Server closed connection.\n");
close(control_fd);
control_fd = -1;
puts("Client connection closed");
}
close(server_fd);
puts("Server shut down");
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
if ( control_fd != -1 )
{
puts("Closing client connection...");
send_message((unsigned int)control_fd, "Server closed connection.\n");
close(control_fd);
control_fd = -1;
puts("Client connection closed");
}
close(server_fd);
puts("Server shut down");
exit(0);
}

original_code:
void signal_handler(int sig) {
    if(control_fd != -1) {
        printf("Closing client connection...\n");
        send_message(control_fd, "Server closed connection.\n");
        close(control_fd);
        control_fd = -1;
        printf("Client connection closed\n");
    }
    close(server_fd);
    printf("Server shut down\n");
    exit(0);
}


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int i; // edi

install_sigint_handler();
server_fd = start_server(argc, argv);
for ( i = server_fd; ; i = server_fd )
{
control_fd = accept_connection(i);
handle_request((unsigned int)control_fd);
close(control_fd);
control_fd = -1;
puts("Connection closed by client");
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int i; // edi

func11();
server_fd = func2(argc, argv);
for ( i = server_fd; ; i = server_fd )
{
control_fd = func3(i);
func10((unsigned int)control_fd);
close(control_fd);
control_fd = -1;
puts("Connection closed by client");
}
}

original_code:
int main(int argc, char * argv[]) {
    install_sigint_handler();
    server_fd = start_server();
    while(1) {
        control_fd = accept_connection(server_fd);
        handle_request(control_fd);
        close(control_fd);
        control_fd = -1;
        printf("Connection closed by client\n");
    }
    close(server_fd);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    install_sigint_handler();
    server_fd = start_server();
    while(1) {
        control_fd = accept_connection(server_fd);
        handle_request(control_fd);
        close(control_fd);
        control_fd = -1;
        printf("Connection closed by client\n");
    }
    close(server_fd);
    return 0;
}


repo: NathanJCochran/myar

decompiled_code:
int __fastcall d_delete(char *name, __int64 a2, unsigned int a3)
{
__int64 v4; // r12
int v5; // eax
unsigned int v6; // er14
int v7; // eax
__int64 v8; // rdi
__int64 v9; // rbp

v4 = create_arch_itr();
if ( unlink(name) == -1 )
{
perror("Error unlinking file");
exit(0);
}
v5 = open(name, 65, 438LL);
v6 = v5;
if ( v5 == -1 )
{
perror("Error creating new archive file");
exit(0);
}
v7 = write(v5, "!<arch>\n", 8uLL);
if ( v7 != 8 )
{
if ( v7 == -1 )
perror("Error writing archive header to file");
else
printf("Error writing full archive header to file: %s", name);
exit(0);
}
while ( 1 )
{
v8 = v4;
if ( !(unsigned __int8)hasNextFile(v4) )
break;
while ( 1 )
{
v9 = nextFile(v8);
if ( !(unsigned __int8)is_member(v9, a2, a3) )
break;
remove_member(v9, a2, a3);
v8 = v4;
if ( !(unsigned __int8)hasNextFile(v4) )
goto LABEL_12;
}
write_file_header_to(v4, v6);
write_file_contents_to(v4, v6);
if ( strtol((const char *)(v9 + 53), 0LL, 10) % 2 == 1 )
write(v6, "\n", 1uLL);
}
LABEL_12:
free_arch_itr(v8);
return close(v6);
}

canonical_decompiled_code:
int __fastcall func1(char *name, __int64 a2, unsigned int a3)
{
__int64 v4; // r12
int v5; // eax
unsigned int v6; // er14
int v7; // eax
__int64 v8; // rdi
__int64 v9; // rbp

v4 = create_arch_itr();
if ( unlink(name) == -1 )
{
perror("Error unlinking file");
exit(0);
}
v5 = open(name, 65, 438LL);
v6 = v5;
if ( v5 == -1 )
{
perror("Error creating new archive file");
exit(0);
}
v7 = write(v5, "!<arch>\n", 8uLL);
if ( v7 != 8 )
{
if ( v7 == -1 )
perror("Error writing archive header to file");
else
printf("Error writing full archive header to file: %s", name);
exit(0);
}
while ( 1 )
{
v8 = v4;
if ( !(unsigned __int8)hasNextFile(v4) )
break;
while ( 1 )
{
v9 = nextFile(v8);
if ( !(unsigned __int8)is_member(v9, a2, a3) )
break;
remove_member(v9, a2, a3);
v8 = v4;
if ( !(unsigned __int8)hasNextFile(v4) )
goto LABEL_12;
}
write_file_header_to(v4, v6);
write_file_contents_to(v4, v6);
if ( strtol((const char *)(v9 + 53), 0LL, 10) % 2 == 1 )
write(v6, "\n", 1uLL);
}
LABEL_12:
free_arch_itr(v8);
return close(v6);
}

original_code:
void d_delete(char * arch_name, char * members[], int mem_len) {
 int fd, num_written;
 struct ar_file * archfile;
 struct arch_itr * itr = create_arch_itr(arch_name);
 if(unlink(arch_name) == -1) {
  perror("Error unlinking file");
  exit(0);
 }
 if((fd = open(arch_name, 01 | 0100, 0666)) == -1) {
  perror("Error creating new archive file");
  exit(0);
 }
 if ((num_written = write(fd, "!<arch>\n", 8)) != 8) {
  if (num_written == -1)
   perror("Error writing archive header to file");
  else
   printf("Error writing full archive header to file: %s", arch_name);
  exit(0);
 }
 while (hasNextFile(itr)) {
  archfile = nextFile(itr);
  if(is_member(archfile->name, members, mem_len)) {
   remove_member(archfile->name, members, mem_len);
  }
  else {
   write_file_header_to(itr, fd);
   write_file_contents_to(itr, fd);
   if(strtol(archfile->size, ((void *)0), 10)%2 ==1) {
    write(fd, "\n", 1);
   }
  }
 }
 free_arch_itr(itr);
 close(fd);
}

canonical_original_code:
void d_delete(char *arch_name, char *members[], int mem_len) {
 int fd; int num_written;
 struct ar_file *archfile;
 struct arch_itr *itr = create_arch_itr(arch_name);
 if(unlink(arch_name) == -1) {
  perror("Error unlinking file");
  exit(0);
 }
 if((fd = open(arch_name, 01 | 0100, 0666)) == -1) {
  perror("Error creating new archive file");
  exit(0);
 }
 if ((num_written = write(fd, "!<arch>\n", 8)) != 8) {
  if (num_written == -1)
   perror("Error writing archive header to file");
  else
   printf("Error writing full archive header to file: %s", arch_name);
  exit(0);
 }
 while (hasNextFile(itr)) {
  archfile = nextFile(itr);
  if(is_member(archfile->name, members, mem_len)) {
   remove_member(archfile->name, members, mem_len);
  }
  else {
   write_file_header_to(itr, fd);
   write_file_contents_to(itr, fd);
   if(strtol(archfile->size, ((void *)0), 10)%2 ==1) {
    write(fd, "\n", 1);
   }
  }
 }
 free_arch_itr(itr);
 close(fd);
}

variable_types:
members
char *[]

variable_types: struct arch_itr { int fd; struct ar_hdr { char ar_name[16]; char ar_date[12]; char ar_uid[6]; char ar_mode[8]; char ar_size[10]; char ar_fmag[2]; } *header; struct ar_file { char name[17]; char date[13]; char uid[7]; char mode[9]; char size[11]; char fmag[3]; } *archfile; } *

user_defined_types: struct arch_itr { int fd; struct ar_hdr *header; struct ar_file *archfile; }


decompiled_code:
__int64 __fastcall x_extract(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rbp
__int64 v6; // rax

v4 = create_arch_itr();
if ( a3 )
{
while ( (unsigned __int8)hasNextFile(v4) )
{
v6 = nextFile(v4);
if ( (unsigned __int8)is_member(v6, a2, a3) )
extractFile(v4);
}
}
else
{
while ( (unsigned __int8)hasNextFile(v4) )
extractFile(v4);
}
return free_arch_itr(v4);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rbp
__int64 v6; // rax

v4 = create_arch_itr();
if ( a3 )
{
while ( (unsigned __int8)hasNextFile(v4) )
{
v6 = nextFile(v4);
if ( (unsigned __int8)is_member(v6, a2, a3) )
extractFile(v4);
}
}
else
{
while ( (unsigned __int8)hasNextFile(v4) )
extractFile(v4);
}
return free_arch_itr(v4);
}

original_code:
void x_extract(char * arch_name, char * members[], int mem_len) {
 struct ar_file * archfile;
 struct arch_itr * itr = create_arch_itr(arch_name);
 if (mem_len == 0) {
  while (hasNextFile(itr)) {
   extractFile(itr);
  }
 }
 else {
  while (hasNextFile(itr)) {
   archfile = nextFile(itr);
   if(is_member(archfile->name, members, mem_len)) {
    extractFile(itr);
   }
  }
 }
 free_arch_itr(itr);
}

canonical_original_code:
void x_extract(char *arch_name, char *members[], int mem_len) {
 struct ar_file *archfile;
 struct arch_itr *itr = create_arch_itr(arch_name);
 if (mem_len == 0) {
  while (hasNextFile(itr)) {
   extractFile(itr);
  }
 }
 else {
  while (hasNextFile(itr)) {
   archfile = nextFile(itr);
   if(is_member(archfile->name, members, mem_len)) {
    extractFile(itr);
   }
  }
 }
 free_arch_itr(itr);
}

variable_types: struct ar_file { char name[17]; char date[13]; char uid[7]; char mode[9]; char size[11]; char fmag[3]; } *

user_defined_types: struct ar_hdr { char ar_name[16]; char ar_date[12]; char ar_uid[6]; char ar_mode[8]; char ar_size[10]; char ar_fmag[2]; }


decompiled_code:
__int64 v_contents()
{
__int64 v0; // r12
const char *v1; // rbp

v0 = create_arch_itr();
while ( (unsigned __int8)hasNextFile(v0) )
{
v1 = (const char *)nextFile(v0);
print_perms(v1 + 44);
printf("%s/%s ", v1 + 30, v1 + 37);
printf("%6s ", v1 + 53);
print_date(v1 + 17);
printf("%.16s\n", v1);
}
return free_arch_itr(v0);
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // r12
const char *v1; // rbp

v0 = create_arch_itr();
while ( (unsigned __int8)hasNextFile(v0) )
{
v1 = (const char *)nextFile(v0);
print_perms(v1 + 44);
printf("%s/%s ", v1 + 30, v1 + 37);
printf("%6s ", v1 + 53);
print_date(v1 + 17);
printf("%.16s\n", v1);
}
return free_arch_itr(v0);
}

original_code:
void v_contents(char * arch_name) {
 struct ar_file * archfile;
 struct arch_itr * itr = create_arch_itr(arch_name);
 while (hasNextFile(itr)) {
  archfile = nextFile(itr);
  print_perms(archfile->mode);
  printf("%s/%s ", archfile->uid, archfile->gid);
  printf("%6s ", archfile->size);
  print_date(archfile->date);
  printf("%.16s\n", archfile->name);
 }
 free_arch_itr(itr);
}

canonical_original_code:
void v_contents(char *arch_name) {
 struct ar_file *archfile;
 struct arch_itr *itr = create_arch_itr(arch_name);
 while (hasNextFile(itr)) {
  archfile = nextFile(itr);
  print_perms(archfile->mode);
  printf("%s/%s ", archfile->uid, archfile->gid);
  printf("%6s ", archfile->size);
  print_date(archfile->date);
  printf("%.16s\n", archfile->name);
 }
 free_arch_itr(itr);
}

user_defined_types: struct ar_file { char name[17]; char date[13]; char uid[7]; char mode[9]; char size[11]; char fmag[3]; }


decompiled_code:
__int64 t_contents()
{
__int64 v0; // rbp
const char *v1; // rax

v0 = create_arch_itr();
while ( (unsigned __int8)hasNextFile(v0) )
{
v1 = (const char *)nextFile(v0);
puts(v1);
}
return free_arch_itr(v0);
}

canonical_decompiled_code:
__int64 func4()
{
__int64 v0; // rbp
const char *v1; // rax

v0 = create_arch_itr();
while ( (unsigned __int8)hasNextFile(v0) )
{
v1 = (const char *)nextFile(v0);
puts(v1);
}
return free_arch_itr(v0);
}

original_code:
void t_contents(char * arch_name) {
 struct ar_file * archfile;
 struct arch_itr * itr = create_arch_itr(arch_name);
 while (hasNextFile(itr)) {
  archfile = nextFile(itr);
  printf("%s\n", archfile->name);
 }
 free_arch_itr(itr);
}

canonical_original_code:
void t_contents(char *arch_name) {
 struct ar_file *archfile;
 struct arch_itr *itr = create_arch_itr(arch_name);
 while (hasNextFile(itr)) {
  archfile = nextFile(itr);
  printf("%s\n", archfile->name);
 }
 free_arch_itr(itr);
}


decompiled_code:
int __fastcall q_append(const char *a1, __int64 *a2, int a3)
{
__int64 *v4; // rbx
int v5; // eax
int *v6; // rax
__int64 v7; // r12
__int64 v8; // rsi
int v9; // er12
int v10; // eax

v4 = a2;
v5 = open(a1, 193, 438LL);
if ( v5 == -1 )
{
v6 = _errno_location();
if ( *v6 == 17 )
goto LABEL_3;
perror("Error creating new file");
LABEL_8:
exit(0);
}
v9 = v5;
printf("myar: creating %s\n", a1);
v10 = write(v9, "!<arch>\n", 8uLL);
if ( v10 != 8 )
{
if ( v10 == -1 )
perror("Error writing archive header to file");
else
printf("Error writing full archive header to file: %s", a1);
goto LABEL_8;
}
LODWORD(v6) = close(v9);
LABEL_3:
if ( a3 > 0 )
{
v7 = (__int64)&a2[(unsigned int)(a3 - 1) + 1];
do
{
v8 = *v4++;
LODWORD(v6) = append_to_arch(a1, v8);
}
while ( v4 != (__int64 *)v7 );
}
return (int)v6;
}

canonical_decompiled_code:
int __fastcall func5(const char *a1, __int64 *a2, int a3)
{
__int64 *v4; // rbx
int v5; // eax
int *v6; // rax
__int64 v7; // r12
__int64 v8; // rsi
int v9; // er12
int v10; // eax

v4 = a2;
v5 = open(a1, 193, 438LL);
if ( v5 == -1 )
{
v6 = _errno_location();
if ( *v6 == 17 )
goto LABEL_3;
perror("Error creating new file");
LABEL_8:
exit(0);
}
v9 = v5;
printf("myar: creating %s\n", a1);
v10 = write(v9, "!<arch>\n", 8uLL);
if ( v10 != 8 )
{
if ( v10 == -1 )
perror("Error writing archive header to file");
else
printf("Error writing full archive header to file: %s", a1);
goto LABEL_8;
}
LODWORD(v6) = close(v9);
LABEL_3:
if ( a3 > 0 )
{
v7 = (__int64)&a2[(unsigned int)(a3 - 1) + 1];
do
{
v8 = *v4++;
LODWORD(v6) = append_to_arch(a1, v8);
}
while ( v4 != (__int64 *)v7 );
}
return (int)v6;
}

original_code:
void q_append(char * arch_name, char * members[], int mem_len) {
 int fd, num_written, i;
 if((fd = open(arch_name, 01 | 0100 | 0200, 0666)) != -1 ) {
  printf("myar: creating %s\n", arch_name);
  if ((num_written = write(fd, "!<arch>\n", 8)) != 8) {
   if (num_written == -1)
    perror("Error writing archive header to file");
   else
    printf("Error writing full archive header to file: %s", arch_name);
   exit(0);
  }
  close(fd);
 }
 else if((*__errno_location ()) != 17) {
  perror("Error creating new file");
  exit(0);
 }
 for (i=0; i<mem_len; i++) {
  append_to_arch(arch_name, members[i]);
 }
}

canonical_original_code:
void q_append(char *arch_name, char *members[], int mem_len) {
 int fd; int num_written; int i;
 if((fd = open(arch_name, 01 | 0100 | 0200, 0666)) != -1 ) {
  printf("myar: creating %s\n", arch_name);
  if ((num_written = write(fd, "!<arch>\n", 8)) != 8) {
   if (num_written == -1)
    perror("Error writing archive header to file");
   else
    printf("Error writing full archive header to file: %s", arch_name);
   exit(0);
  }
  close(fd);
 }
 else if((*__errno_location ()) != 17) {
  perror("Error creating new file");
  exit(0);
 }
 for (i=0; i<mem_len; i++) {
  append_to_arch(arch_name, members[i]);
 }
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er14
void *v4; // rsp
char v5; // al
__int64 s1[8]; // [rsp+0h] [rbp-40h] BYREF

v3 = argc - 3;
v4 = alloca(8LL * (argc - 3));
get_arch_name(argc, argv, s1);
get_members((unsigned int)argc, argv, s1);
v5 = get_arch_opt((unsigned int)argc, argv);
if ( v5 == 65 )
{
a_append((char *)s1);
}
else
{
switch ( v5 )
{
case 'd':
d_delete((char *)s1, (__int64)s1, v3);
break;
case 'q':
q_append((const char *)s1, s1, v3);
break;
case 't':
t_contents(s1);
break;
case 'v':
v_contents(s1);
break;
case 'x':
x_extract((__int64)s1, (__int64)s1, v3);
break;
default:
print_usage(*argv);
exit(0);
}
}
free_members(s1, v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er14
void *v4; // rsp
char v5; // al
__int64 s1[8]; // [rsp+0h] [rbp-40h] BYREF

v3 = argc - 3;
v4 = alloca(8LL * (argc - 3));
get_arch_name(argc, argv, s1);
get_members((unsigned int)argc, argv, s1);
v5 = get_arch_opt((unsigned int)argc, argv);
if ( v5 == 65 )
{
func0((char *)s1);
}
else
{
switch ( v5 )
{
case 'd':
func1((char *)s1, (__int64)s1, v3);
break;
case 'q':
func5((const char *)s1, s1, v3);
break;
case 't':
func4(s1);
break;
case 'v':
func3(s1);
break;
case 'x':
func2((__int64)s1, (__int64)s1, v3);
break;
default:
print_usage(*argv);
exit(0);
}
}
free_members(s1, v3);
return 0;
}

original_code:
int main(int argc, char * argv[]) {
 int mem_len = argc-3;
 char * members[mem_len];
 char arch_name[32];
 get_arch_name(argc, argv, arch_name);
 get_members(argc, argv, members);
 switch(get_arch_opt(argc, argv)){
  case 'q':
   q_append(arch_name, members, mem_len);
   break;
  case 'x':
   x_extract(arch_name, members, mem_len);
   break;
  case 't':
   t_contents(arch_name);
   break;
  case 'v':
   v_contents(arch_name);
   break;
  case 'd':
   d_delete(arch_name, members, mem_len);
   break;
  case 'A':
   a_append(arch_name);
   break;
        default:
            print_usage(argv[0]);
            exit(0);
 }
 free_members(members, mem_len);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int mem_len = argc-3;
 char *members[-1];
 char arch_name[32];
 get_arch_name(argc, argv, arch_name);
 get_members(argc, argv, members);
 switch(get_arch_opt(argc, argv)){
  case 'q':
   q_append(arch_name, members, mem_len);
   break;
  case 'x':
   x_extract(arch_name, members, mem_len);
   break;
  case 't':
   t_contents(arch_name);
   break;
  case 'v':
   v_contents(arch_name);
   break;
  case 'd':
   d_delete(arch_name, members, mem_len);
   break;
  case 'A':
   a_append(arch_name);
   break;
        default:
            print_usage(argv[0]);
            exit(0);
 }
 free_members(members, mem_len);
 return 0;
}

variable_types:
members
char *[-1]