deleteToken

decompiled_code:
void __fastcall deleteToken(_DWORD *a1)
{
if ( a1 && ((*a1 & 0xFFFFFFF7) == 18 || *a1 == 21) )
deleteString((__int64)(a1 + 2));
}

original_code:
void deleteToken(Token *pt)
{
    if (pt != ((void *)0)) {
        if ((pt->type == STT_Variable)
            || (pt->type == STT_Identifier)
            || (pt->type == STT_String)) {
            deleteString(&pt->str);
        }
    }
}

canonical_original_code:
void deleteToken(struct Token *pt)
{
    if (pt != ((void *)0)) {
        if ((pt->type == STT_Variable)
            || (pt->type == STT_Identifier)
            || (pt->type == STT_String)) {
            deleteString(&pt->str);
        }
    }
}

user_defined_types: enum ScannerTokenType { STT_Plus=0, STT_Minus=1, STT_Multiply=2, STT_Divide=3, STT_Dot=4, STT_Less=5, STT_Greater=6, STT_LessEqual=7, STT_GreaterEqual=8, STT_Equal=9, STT_NotEqual=10, STT_And=11, STT_Or=12, STT_AndLow=13, STT_OrLow=14, STT_Not=15, STT_LeftBracket=16, STT_RightBracket=17, STT_Identifier=18, STT_Comma=19, STT_EOF=20, STT_Variable=21, STT_Number=22, STT_Double=23, STT_Bool=24, STT_Null=25, STT_String=26, STT_Semicolon=27, STT_LeftCurlyBracket=28, STT_RightCurlyBracket=29, STT_Empty=30, STT_Keyword=31, STT_Assignment=32, STT_Php=33 }

user_defined_types: enum KeywordTokenType { KTT_None=0, KTT_If=1, KTT_Else=2, KTT_Elseif=3, KTT_Continue=4, KTT_Break=5, KTT_While=6, KTT_For=7, KTT_Return=8, KTT_Function=9, KTT_And=10, KTT_Or=11, KTT_True=12, KTT_False=13, KTT_Null=14 }


freeToken

repo: metthal/IFJ-Projekt

decompiled_code:
void __fastcall freeToken(void **a1)
{
_DWORD *v2; // rdi

if ( a1 )
{
v2 = *a1;
if ( v2 && ((*v2 & 0xFFFFFFF7) == 18 || *v2 == 21) )
deleteString((__int64)(v2 + 2));
free(*a1);
*a1 = 0LL;
}
}

original_code:
void freeToken(Token **ppt)
{
    if (ppt != ((void *)0)) {
        if (*ppt != ((void *)0)) {
            if (((*ppt)->type == STT_Variable)
                || ((*ppt)->type == STT_Identifier)
                || ((*ppt)->type == STT_String)) {
                deleteString(&(*ppt)->str);
            }
        }
        free(*ppt);
        *ppt = ((void *)0);
    }
}

canonical_original_code:
void freeToken(struct Token **ppt)
{
    if (ppt != ((void *)0)) {
        if (*ppt != ((void *)0)) {
            if (((*ppt)->type == STT_Variable)
                || ((*ppt)->type == STT_Identifier)
                || ((*ppt)->type == STT_String)) {
                deleteString(&(*ppt)->str);
            }
        }
        free(*ppt);
        *ppt = ((void *)0);
    }
}

variable_types: struct Token { enum ScannerTokenType { STT_Plus=0, STT_Minus=1, STT_Multiply=2, STT_Divide=3, STT_Dot=4, STT_Less=5, STT_Greater=6, STT_LessEqual=7, STT_GreaterEqual=8, STT_Equal=9, STT_NotEqual=10, STT_And=11, STT_Or=12, STT_AndLow=13, STT_OrLow=14, STT_Not=15, STT_LeftBracket=16, STT_RightBracket=17, STT_Identifier=18, STT_Comma=19, STT_EOF=20, STT_Variable=21, STT_Number=22, STT_Double=23, STT_Bool=24, STT_Null=25, STT_String=26, STT_Semicolon=27, STT_LeftCurlyBracket=28, STT_RightCurlyBracket=29, STT_Empty=30, STT_Keyword=31, STT_Assignment=32, STT_Php=33 } type; union { double d; int n; enum KeywordTokenType { KTT_None=0, KTT_If=1, KTT_Else=2, KTT_Elseif=3, KTT_Continue=4, KTT_Break=5, KTT_While=6, KTT_For=7, KTT_Return=8, KTT_Function=9, KTT_And=10, KTT_Or=11, KTT_True=12, KTT_False=13, KTT_Null=14 } keywordType; struct String { unsigned int size; unsigned int length; char *data; } str; }; } **

return_type: void

user_defined_types: struct Token { enum ScannerTokenType type; union { double d; int n; enum KeywordTokenType keywordType; struct String str; }; }


copyToken

decompiled_code:
__int64 __fastcall copyToken(unsigned int *a1, _QWORD *a2)
{
__int64 result; // rax

*a2 = 0LL;
a2[1] = 0LL;
a2[2] = 0LL;
result = *a1;
*(_DWORD *)a2 = result;
if ( (result & 0xFFFFFFF7) == 18 || (_DWORD)result == 21 )
return copyString(a1 + 2, a2 + 1);
switch ( (_DWORD)result )
{
case 0x17:
a2[1] = *((_QWORD *)a1 + 1);
break;
case 0x16:
result = a1[2];
*((_DWORD *)a2 + 2) = result;
break;
case 0x1F:
result = a1[2];
*((_DWORD *)a2 + 2) = result;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, _QWORD *a2)
{
__int64 result; // rax

*a2 = 0LL;
a2[1] = 0LL;
a2[2] = 0LL;
result = *a1;
*(_DWORD *)a2 = result;
if ( (result & 0xFFFFFFF7) == 18 || (_DWORD)result == 21 )
return copyString(a1 + 2, a2 + 1);
switch ( (_DWORD)result )
{
case 0x17:
a2[1] = *((_QWORD *)a1 + 1);
break;
case 0x16:
result = a1[2];
*((_DWORD *)a2 + 2) = result;
break;
case 0x1F:
result = a1[2];
*((_DWORD *)a2 + 2) = result;
break;
}
return result;
}

original_code:
void copyToken(const Token *src, Token *dest)
{
    memset(dest, 0, sizeof(Token));
    dest->type = src->type;
    if(src->type == STT_Variable ||
       src->type == STT_Identifier ||
       src->type == STT_String){
        copyString(&src->str, &dest->str);
    }
    else if(src->type == STT_Double)
        dest->d = src->d;
    else if(src->type == STT_Number)
        dest->n = src->n;
    else if(src->type == STT_Keyword)
        dest->keywordType = src->keywordType;
}

canonical_original_code:
void copyToken(struct Token *src, struct Token *dest)
{
    memset(dest, 0, sizeof(Token));
    dest->type = src->type;
    if(src->type == STT_Variable ||
       src->type == STT_Identifier ||
       src->type == STT_String){
        copyString(&src->str, &dest->str);
    }
    else if(src->type == STT_Double)
        dest->d = src->d;
    else if(src->type == STT_Number)
        dest->n = src->n;
    else if(src->type == STT_Keyword)
        dest->keywordType = src->keywordType;
}

variable_types: struct Token { enum ScannerTokenType { STT_Plus=0, STT_Minus=1, STT_Multiply=2, STT_Divide=3, STT_Dot=4, STT_Less=5, STT_Greater=6, STT_LessEqual=7, STT_GreaterEqual=8, STT_Equal=9, STT_NotEqual=10, STT_And=11, STT_Or=12, STT_AndLow=13, STT_OrLow=14, STT_Not=15, STT_LeftBracket=16, STT_RightBracket=17, STT_Identifier=18, STT_Comma=19, STT_EOF=20, STT_Variable=21, STT_Number=22, STT_Double=23, STT_Bool=24, STT_Null=25, STT_String=26, STT_Semicolon=27, STT_LeftCurlyBracket=28, STT_RightCurlyBracket=29, STT_Empty=30, STT_Keyword=31, STT_Assignment=32, STT_Php=33 } type; union { double d; int n; enum KeywordTokenType { KTT_None=0, KTT_If=1, KTT_Else=2, KTT_Elseif=3, KTT_Continue=4, KTT_Break=5, KTT_While=6, KTT_For=7, KTT_Return=8, KTT_Function=9, KTT_And=10, KTT_Or=11, KTT_True=12, KTT_False=13, KTT_Null=14 } keywordType; struct String { unsigned int size; unsigned int length; char *data; } str; }; } *

variable_types:
dest
struct Token *


vectorReserve

decompiled_code:
void __fastcall vectorReserve(unsigned int *a1, unsigned int a2)
{
unsigned int v2; // eax
char *v4; // rax
int v5; // ecx
__int64 v6; // rdx
char *v7; // rdi

v2 = a1[1];
if ( v2 < a2 )
{
if ( v2 )
v4 = (char *)realloc(*((void **)a1 + 2), *((_QWORD *)a1 + 1) * a2);
else
v4 = (char *)malloc(*((_QWORD *)a1 + 1) * a2);
if ( v4 )
{
*((_QWORD *)a1 + 2) = v4;
v5 = *a1;
v6 = *((_QWORD *)a1 + 1);
v7 = &v4[v6 * *a1];
*((_QWORD *)a1 + 3) = v7;
a1[1] = a2;
memset(v7, 0, (a2 - v5) * v6);
}
else
{
setErrorExp(3, 41, (__int64)"vector.c", (__int64)"vectorReserve");
}
}
}

canonical_decompiled_code:
void __fastcall func1(unsigned int *a1, unsigned int a2)
{
unsigned int v2; // eax
char *v4; // rax
int v5; // ecx
__int64 v6; // rdx
char *v7; // rdi

v2 = a1[1];
if ( v2 < a2 )
{
if ( v2 )
v4 = (char *)realloc(*((void **)a1 + 2), *((_QWORD *)a1 + 1) * a2);
else
v4 = (char *)malloc(*((_QWORD *)a1 + 1) * a2);
if ( v4 )
{
*((_QWORD *)a1 + 2) = v4;
v5 = *a1;
v6 = *((_QWORD *)a1 + 1);
v7 = &v4[v6 * *a1];
*((_QWORD *)a1 + 3) = v7;
a1[1] = a2;
memset(v7, 0, (a2 - v5) * v6);
}
else
{
func0(3, 41, (__int64)"vector.c", (__int64)"vectorReserve");
}
}
}

original_code:
void vectorReserve(Vector *vec, uint32_t capacity)
{
    if (capacity <= vec->capacity)
        return;
    void *tmp;
    if (vec->capacity > 0)
        tmp = realloc(vec->data, vec->itemSize * capacity);
    else
        tmp = malloc(vec->itemSize * capacity);
    if (tmp == ((void *)0)) {
        setErrorExp(ERR_Allocation, 41, "vector.c", __func__);
        return;
    }
    vec->data = tmp;
    vec->end = vec->data + vec->size * vec->itemSize;
    vec->capacity = capacity;
    memset(vec->end, 0, (vec->capacity - vec->size) * vec->itemSize);
}

canonical_original_code:
void vectorReserve(struct Vector *vec, unsigned int capacity)
{
    if (capacity <= vec->capacity)
        return;
    void *tmp;
    if (vec->capacity > 0)
        tmp = realloc(vec->data, vec->itemSize * capacity);
    else
        tmp = malloc(vec->itemSize * capacity);
    if (tmp == ((void *)0)) {
        setErrorExp(ERR_Allocation, 41, "vector.c", __func__);
        return;
    }
    vec->data = tmp;
    vec->end = vec->data + vec->size * vec->itemSize;
    vec->capacity = capacity;
    memset(vec->end, 0, (vec->capacity - vec->size) * vec->itemSize);
}

variable_types: struct Vector { unsigned int size; unsigned int capacity; unsigned long itemSize; unsigned char *data; unsigned char *end; } *

variable_types:
capacity
unsigned int

variable_types:
tmp
void *


vectorShrinkToFit

decompiled_code:
void __fastcall vectorShrinkToFit(unsigned int *a1)
{
char *v1; // rax
unsigned int v2; // ecx

if ( *a1 )
{
v1 = (char *)realloc(*((void **)a1 + 2), *((_QWORD *)a1 + 1) * *a1);
if ( v1 )
{
*((_QWORD *)a1 + 2) = v1;
v2 = *a1;
*((_QWORD *)a1 + 3) = &v1[*((_QWORD *)a1 + 1) * *a1];
a1[1] = v2;
}
else
{
setErrorExp(3, 61, (__int64)"vector.c", (__int64)"vectorShrinkToFit");
}
}
else
{
free(*((void **)a1 + 2));
*((_QWORD *)a1 + 2) = 0LL;
*((_QWORD *)a1 + 3) = 0LL;
a1[1] = 0;
}
}

original_code:
void vectorShrinkToFit(Vector *vec)
{
    if (vec->size == 0) {
        free(vec->data);
        vec->end = vec->data = ((void *)0);
        vec->capacity = 0;
    }
    else {
        void *tmp = realloc(vec->data, vec->itemSize * vec->size);
        if (tmp == ((void *)0)) {
            setErrorExp(ERR_Allocation, 61, "vector.c", __func__);
            return;
        }
        vec->data = tmp;
        vec->end = vec->data + vec->size * vec->itemSize;
        vec->capacity = vec->size;
    }
}

canonical_original_code:
void vectorShrinkToFit(struct Vector *vec)
{
    if (vec->size == 0) {
        free(vec->data);
        vec->end = vec->data = ((void *)0);
        vec->capacity = 0;
    }
    else {
        void *tmp = realloc(vec->data, vec->itemSize * vec->size);
        if (tmp == ((void *)0)) {
            setErrorExp(ERR_Allocation, 61, "vector.c", __func__);
            return;
        }
        vec->data = tmp;
        vec->end = vec->data + vec->size * vec->itemSize;
        vec->capacity = vec->size;
    }
}

user_defined_types: struct Vector { unsigned int size; unsigned int capacity; unsigned long itemSize; unsigned char *data; unsigned char *end; }


vectorCapacity

decompiled_code:
__int64 __fastcall vectorCapacity(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

original_code:
uint32_t vectorCapacity(const Vector *vec)
{
    return vec->capacity;
}

canonical_original_code:
unsigned int vectorCapacity(struct Vector *vec)
{
    return vec->capacity;
}

return_type: unsigned int


vectorAt

decompiled_code:
__int64 __fastcall vectorAt(__int64 a1, unsigned int a2)
{
if ( *(_DWORD *)a1 > a2 )
return *(_QWORD *)(a1 + 16) + *(_QWORD *)(a1 + 8) * a2;
setErrorExp(4, 79, (__int64)"vector.c", (__int64)"vectorAt");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned int a2)
{
if ( *(_DWORD *)a1 > a2 )
return *(_QWORD *)(a1 + 16) + *(_QWORD *)(a1 + 8) * a2;
func0(4, 79, (__int64)"vector.c", (__int64)"vectorAt");
return 0LL;
}

original_code:
void* vectorAt(Vector *vec, uint32_t index)
{
    if (vec->size <= index) {
        setErrorExp(ERR_Range, 79, "vector.c", __func__);
        return ((void *)0);
    }
    return vec->data + vec->itemSize * index;
}

canonical_original_code:
void *vectorAt(struct Vector *vec, unsigned int index)
{
    if (vec->size <= index) {
        setErrorExp(ERR_Range, 79, "vector.c", __func__);
        return ((void *)0);
    }
    return vec->data + vec->itemSize * index;
}

variable_types:
index
unsigned int

return_type: void *


vectorAtConst

decompiled_code:
__int64 __fastcall vectorAtConst(__int64 a1, unsigned int a2)
{
if ( *(_DWORD *)a1 > a2 )
return *(_QWORD *)(a1 + 16) + *(_QWORD *)(a1 + 8) * a2;
setErrorExp(4, 88, (__int64)"vector.c", (__int64)"vectorAtConst");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned int a2)
{
if ( *(_DWORD *)a1 > a2 )
return *(_QWORD *)(a1 + 16) + *(_QWORD *)(a1 + 8) * a2;
func0(4, 88, (__int64)"vector.c", (__int64)"vectorAtConst");
return 0LL;
}

original_code:
const void* vectorAtConst(const Vector *vec, uint32_t index)
{
    if (vec->size <= index) {
        setErrorExp(ERR_Range, 88, "vector.c", __func__);
        return ((void *)0);
    }
    return vec->data + vec->itemSize * index;
}

canonical_original_code:
void *vectorAtConst(struct Vector *vec, unsigned int index)
{
    if (vec->size <= index) {
        setErrorExp(ERR_Range, 88, "vector.c", __func__);
        return ((void *)0);
    }
    return vec->data + vec->itemSize * index;
}


vectorFront

decompiled_code:
__int64 __fastcall vectorFront(__int64 a1)
{
if ( *(_DWORD *)a1 )
return *(_QWORD *)(a1 + 16);
setErrorExp(4, 97, (__int64)"vector.c", (__int64)"vectorFront");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
if ( *(_DWORD *)a1 )
return *(_QWORD *)(a1 + 16);
func0(4, 97, (__int64)"vector.c", (__int64)"vectorFront");
return 0LL;
}

original_code:
void* vectorFront(Vector *vec)
{
    if (vec->size == 0) {
        setErrorExp(ERR_Range, 97, "vector.c", __func__);
        return ((void *)0);
    }
    return vec->data;
}

canonical_original_code:
void *vectorFront(struct Vector *vec)
{
    if (vec->size == 0) {
        setErrorExp(ERR_Range, 97, "vector.c", __func__);
        return ((void *)0);
    }
    return vec->data;
}


vectorBack

decompiled_code:
__int64 __fastcall vectorBack(__int64 a1)
{
if ( *(_DWORD *)a1 )
return *(_QWORD *)(a1 + 24) - *(_QWORD *)(a1 + 8);
setErrorExp(4, 106, (__int64)"vector.c", (__int64)"vectorBack");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
if ( *(_DWORD *)a1 )
return *(_QWORD *)(a1 + 24) - *(_QWORD *)(a1 + 8);
func0(4, 106, (__int64)"vector.c", (__int64)"vectorBack");
return 0LL;
}

original_code:
void* vectorBack(Vector *vec)
{
    if (vec->size == 0) {
        setErrorExp(ERR_Range, 106, "vector.c", __func__);
        return ((void *)0);
    }
    return vec->end - vec->itemSize;
}

canonical_original_code:
void *vectorBack(struct Vector *vec)
{
    if (vec->size == 0) {
        setErrorExp(ERR_Range, 106, "vector.c", __func__);
        return ((void *)0);
    }
    return vec->end - vec->itemSize;
}


copyValue

decompiled_code:
__int64 __fastcall copyValue(const __m128i *a1, __m128i *a2)
{
__int64 result; // rax

result = a1[1].m128i_u32[0];
a2[1].m128i_i32[0] = result;
if ( (_DWORD)result == 3 )
return copyString();
if ( (_DWORD)result != 5 )
{
if ( (_DWORD)result )
*a2 = _mm_loadu_si128(a1);
}
return result;
}

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

result = a1[1].m128i_u32[0];
a2[1].m128i_i32[0] = result;
if ( (_DWORD)result == 3 )
return copyString();
if ( (_DWORD)result != 5 )
{
if ( (_DWORD)result )
*a2 = _mm_loadu_si128(a1);
}
return result;
}

original_code:
void copyValue(const Value *src, Value *dest)
{
    dest->type = src->type;
    switch(src->type) {
        case VT_Null:
        case VT_Undefined:
            return;
        case VT_String:
            copyString(&(src->data.s), &(dest->data.s));
            break;
        default:
            dest->data = src->data;
    }
}

canonical_original_code:
void copyValue(struct Value *src, struct Value *dest)
{
    dest->type = src->type;
    switch(src->type) {
        case VT_Null:
        case VT_Undefined:
            return;
        case VT_String:
            copyString(&(src->data.s), &(dest->data.s));
            break;
        default:
            dest->data = src->data;
    }
}

variable_types: struct Value { union ValueData { struct String { unsigned int size; unsigned int length; char *data; } s; double d; int i; void *ip; unsigned int sp; long ref; char b; } data; enum ValueType { VT_Undefined=0, VT_Integer=1, VT_Double=2, VT_String=3, VT_Bool=4, VT_Null=5, VT_StrongReference=6, VT_WeakReference=7, VT_ConstReference=8, VT_InstructionPtr=9, VT_StackPtr=10 } type; } *

variable_types:
dest
struct Value *

user_defined_types: struct Value { union ValueData data; enum ValueType type; }

user_defined_types: union ValueData { struct String s; double d; int i; void *ip; unsigned int sp; long ref; char b; }


tokenToValue

decompiled_code:
void __fastcall tokenToValue(_DWORD *a1, _DWORD *a2)
{
switch ( *a1 )
{
case 0x16:
a2[4] = 1;
*a2 = a1[2];
break;
case 0x17:
a2[4] = 2;
*(_QWORD *)a2 = *((_QWORD *)a1 + 1);
break;
case 0x18:
a2[4] = 4;
*(_BYTE *)a2 = a1[2];
break;
case 0x19:
a2[4] = 5;
break;
case 0x1A:
a2[4] = 3;
initString(a2);
copyString(a1 + 2, a2);
break;
default:
setErrorExp(6, 73, (__int64)"symbol.c", (__int64)"tokenToValue");
break;
}
}

canonical_decompiled_code:
void __fastcall func2(_DWORD *a1, _DWORD *a2)
{
switch ( *a1 )
{
case 0x16:
a2[4] = 1;
*a2 = a1[2];
break;
case 0x17:
a2[4] = 2;
*(_QWORD *)a2 = *((_QWORD *)a1 + 1);
break;
case 0x18:
a2[4] = 4;
*(_BYTE *)a2 = a1[2];
break;
case 0x19:
a2[4] = 5;
break;
case 0x1A:
a2[4] = 3;
initString(a2);
copyString(a1 + 2, a2);
break;
default:
func0(6, 73, (__int64)"symbol.c", (__int64)"tokenToValue");
break;
}
}

original_code:
void tokenToValue(const Token *src, Value *dest)
{
    switch (src->type) {
        case STT_Number:
            dest->type = VT_Integer;
            dest->data.i = src->n;
            break;
        case STT_Double:
            dest->type = VT_Double;
            dest->data.d = src->d;
            break;
        case STT_String:
            dest->type = VT_String;
            initString(&(dest->data.s));
            copyString(&(src->str), &(dest->data.s));
            break;
        case STT_Bool:
            dest->type = VT_Bool;
            dest->data.b = src->n;
            break;
        case STT_Null:
            dest->type = VT_Null;
            break;
        default:
            setErrorExp(ERR_Convert, 73, "symbol.c", __func__);
            return;
    }
}

canonical_original_code:
void tokenToValue(struct Token *src, struct Value *dest)
{
    switch (src->type) {
        case STT_Number:
            dest->type = VT_Integer;
            dest->data.i = src->n;
            break;
        case STT_Double:
            dest->type = VT_Double;
            dest->data.d = src->d;
            break;
        case STT_String:
            dest->type = VT_String;
            initString(&(dest->data.s));
            copyString(&(src->str), &(dest->data.s));
            break;
        case STT_Bool:
            dest->type = VT_Bool;
            dest->data.b = src->n;
            break;
        case STT_Null:
            dest->type = VT_Null;
            break;
        default:
            setErrorExp(ERR_Convert, 73, "symbol.c", __func__);
            return;
    }
}

variable_types: struct Value { union ValueData { struct String s; double d; int i; void *ip; unsigned int sp; long ref; char b; } data; enum ValueType { VT_Undefined=0, VT_Integer=1, VT_Double=2, VT_String=3, VT_Bool=4, VT_Null=5, VT_StrongReference=6, VT_WeakReference=7, VT_ConstReference=8, VT_InstructionPtr=9, VT_StackPtr=10 } type; } *

user_defined_types: struct String { unsigned int size; unsigned int length; char *data; }


newVariable

decompiled_code:
_DWORD *newVariable()
{
_DWORD *result; // rax

result = malloc(4uLL);
*result = 0;
return result;
}

canonical_decompiled_code:
_DWORD *func6()
{
_DWORD *result; // rax

result = malloc(4uLL);
*result = 0;
return result;
}

original_code:
Variable* newVariable()
{
    Variable *tmp = malloc(sizeof(Variable));
    memset(tmp, 0, sizeof(Variable));
    return tmp;
}

canonical_original_code:
struct Variable *newVariable()
{
    struct Variable *tmp = malloc(sizeof(Variable));
    memset(tmp, 0, sizeof(Variable));
    return tmp;
}

variable_types: struct Variable { int relativeIndex; } *


freeVariable

decompiled_code:
void __fastcall freeVariable(void **a1)
{
if ( a1 )
{
free(*a1);
*a1 = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func7(void **a1)
{
if ( a1 )
{
free(*a1);
*a1 = 0LL;
}
}

original_code:
void freeVariable(Variable **ppt)
{
    if (ppt != ((void *)0)) {
        free(*ppt);
        *ppt = ((void *)0);
    }
}

canonical_original_code:
void freeVariable(struct Variable **ppt)
{
    if (ppt != ((void *)0)) {
        free(*ppt);
        *ppt = ((void *)0);
    }
}

variable_types: struct Variable { int relativeIndex; } **


initContext

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

*a1 = 0LL;
a1[1] = 0LL;
a1[2] = 0LL;
result = newSymbolTable();
*a1 = result;
return result;
}

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

*a1 = 0LL;
a1[1] = 0LL;
a1[2] = 0LL;
result = newSymbolTable();
*a1 = result;
return result;
}

original_code:
void initContext(Context *pt)
{
    memset(pt, 0, sizeof(Context));
    pt->localTable = newSymbolTable();
}

canonical_original_code:
void initContext(struct Context *pt)
{
    memset(pt, 0, sizeof(Context));
    pt->localTable = newSymbolTable();
}

variable_types: struct Context { struct SymbolTable { unsigned int *table; struct Vector { unsigned int size; unsigned int capacity; unsigned long itemSize; unsigned char *data; unsigned char *end; } *vec; unsigned int size; unsigned int count; } *localTable; unsigned int defaultStart; unsigned int exprStart; unsigned short argumentCount; unsigned short localVariableCount; unsigned short defaultCount; } *

user_defined_types: struct Context { struct SymbolTable *localTable; unsigned int defaultStart; unsigned int exprStart; unsigned short argumentCount; unsigned short localVariableCount; unsigned short defaultCount; }

user_defined_types: struct SymbolTable { unsigned int *table; struct Vector *vec; unsigned int size; unsigned int count; }


deleteContext

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

if ( a1 )
result = freeSymbolTable();
return result;
}

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

if ( a1 )
result = freeSymbolTable();
return result;
}

original_code:
void deleteContext(Context *pt)
{
    if (pt != ((void *)0)) {
        freeSymbolTable(&pt->localTable);
    }
}

canonical_original_code:
void deleteContext(struct Context *pt)
{
    if (pt != ((void *)0)) {
        freeSymbolTable(&pt->localTable);
    }
}


repo: mewfork/gorilla-audio

decompiled_code:
void __fastcall gcX_defaultFreeFunc(void *a1)
{
free(a1);
}

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

original_code:
static void gcX_defaultFreeFunc(void* in_ptr)
{
  free(in_ptr);
}

canonical_original_code:
static void gcX_defaultFreeFunc(void *in_ptr)
{
  free(in_ptr);
}


decompiled_code:
void *__fastcall gcX_defaultReallocFunc(void *a1, unsigned int a2)
{
return realloc(a1, a2);
}

canonical_decompiled_code:
void *__fastcall func1(void *a1, unsigned int a2)
{
return realloc(a1, a2);
}

original_code:
static void* gcX_defaultReallocFunc(void* in_ptr, gc_uint32 in_size)
{
  return realloc(in_ptr, in_size);
}

canonical_original_code:
static void *gcX_defaultReallocFunc(void *in_ptr, unsigned int in_size)
{
  return realloc(in_ptr, in_size);
}


decompiled_code:
void *__fastcall gcX_defaultAllocFunc(size_t size)
{
return malloc((unsigned int)size);
}

canonical_decompiled_code:
void *__fastcall func2(size_t size)
{
return malloc((unsigned int)size);
}

original_code:
static void* gcX_defaultAllocFunc(gc_uint32 in_size)
{
  return malloc(in_size);
}

canonical_original_code:
static void *gcX_defaultAllocFunc(unsigned int in_size)
{
  return malloc(in_size);
}


decompiled_code:
__int64 __fastcall gc_initialize(__int64 *a1)
{
s_defaultCallbacks = (__int64)gcX_defaultAllocFunc;
qword_3E8 = (__int64)gcX_defaultReallocFunc;
qword_3F0 = (__int64)gcX_defaultFreeFunc;
if ( !a1 )
a1 = &s_defaultCallbacks;
gcX_ops = a1;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1)
{
s_defaultCallbacks = (__int64)gcX_defaultAllocFunc;
qword_3E8 = (__int64)gcX_defaultReallocFunc;
qword_3F0 = (__int64)gcX_defaultFreeFunc;
if ( !a1 )
a1 = &s_defaultCallbacks;
gcX_ops = a1;
return 1LL;
}

original_code:
gc_result gc_initialize(gc_SystemOps* in_callbacks)
{
  s_defaultCallbacks.allocFunc = &gcX_defaultAllocFunc;
  s_defaultCallbacks.reallocFunc = &gcX_defaultReallocFunc;
  s_defaultCallbacks.freeFunc = &gcX_defaultFreeFunc;
  gcX_ops = in_callbacks ? in_callbacks : &s_defaultCallbacks;
  return 1;
}

canonical_original_code:
int gc_initialize(struct gc_SystemOps *in_callbacks)
{
  s_defaultCallbacks.allocFunc = &gcX_defaultAllocFunc;
  s_defaultCallbacks.reallocFunc = &gcX_defaultReallocFunc;
  s_defaultCallbacks.freeFunc = &gcX_defaultFreeFunc;
  gcX_ops = in_callbacks ? in_callbacks : &s_defaultCallbacks;
  return 1;
}

variable_types: struct gc_SystemOps { void *(*allocFunc)(unsigned int in_size); void *(*reallocFunc)(void *in_ptr, unsigned int in_size); void (*freeFunc)(void *in_ptr); } *

return_type: int


decompiled_code:
__int64 gc_shutdown()
{
gcX_ops = 0LL;
return 1LL;
}

canonical_decompiled_code:
__int64 func4()
{
gcX_ops = 0LL;
return 1LL;
}

original_code:
gc_result gc_shutdown()
{
  gcX_ops = 0;
  return 1;
}

canonical_original_code:
int gc_shutdown()
{
  gcX_ops = 0;
  return 1;
}


decompiled_code:
__int64 __fastcall gc_buffer_create(unsigned int a1)
{
__int64 v1; // rbp

if ( !a1 )
return 0LL;
if ( (a1 & (a1 - 1)) != 0 )
return 0LL;
v1 = ((__int64 (__fastcall *)(__int64))*gcX_ops)(24LL);
*(_QWORD *)v1 = ((__int64 (__fastcall *)(_QWORD))*gcX_ops)(a1);
*(_DWORD *)(v1 + 8) = a1;
*(_DWORD *)(v1 + 12) = 0;
*(_DWORD *)(v1 + 16) = 0;
return v1;
}

original_code:
gc_CircBuffer* gc_buffer_create(gc_uint32 in_size)
{
  gc_CircBuffer* ret;
  if(!in_size || (in_size & (in_size - 1)))
    return 0;
  ret = gcX_ops->allocFunc(sizeof(gc_CircBuffer));
  ret->data = gcX_ops->allocFunc(in_size);
  ret->dataSize = in_size;
  ret->nextAvail = 0;
  ret->nextFree = 0;
  return ret;
}

canonical_original_code:
struct gc_CircBuffer *gc_buffer_create(unsigned int in_size)
{
  struct gc_CircBuffer *ret;
  if(!in_size || (in_size & (in_size - 1)))
    return 0;
  ret = gcX_ops->allocFunc(sizeof(gc_CircBuffer));
  ret->data = gcX_ops->allocFunc(in_size);
  ret->dataSize = in_size;
  ret->nextAvail = 0;
  ret->nextFree = 0;
  return ret;
}

variable_types: struct gc_CircBuffer { unsigned char *data; unsigned int dataSize; unsigned int nextAvail; unsigned int nextFree; } *


decompiled_code:
__int64 __fastcall gc_buffer_destroy(_QWORD *a1)
{
(*(void (__fastcall **)(_QWORD))(gcX_ops + 16LL))(*a1);
(*(void (__fastcall **)(_QWORD *))(gcX_ops + 16LL))(a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
(*(void (__fastcall **)(_QWORD))(gcX_ops + 16LL))(*a1);
(*(void (__fastcall **)(_QWORD *))(gcX_ops + 16LL))(a1);
return 1LL;
}

original_code:
gc_result gc_buffer_destroy(gc_CircBuffer* in_buffer)
{
  gcX_ops->freeFunc(in_buffer->data);
  gcX_ops->freeFunc(in_buffer);
  return 1;
}

canonical_original_code:
int gc_buffer_destroy(struct gc_CircBuffer *in_buffer)
{
  gcX_ops->freeFunc(in_buffer->data);
  gcX_ops->freeFunc(in_buffer);
  return 1;
}

user_defined_types: struct gc_CircBuffer { unsigned char *data; unsigned int dataSize; unsigned int nextAvail; unsigned int nextFree; }


decompiled_code:
__int64 __fastcall gc_buffer_bytesAvail(__int64 a1)
{
return (unsigned int)(*(_DWORD *)(a1 + 16) - *(_DWORD *)(a1 + 12));
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return (unsigned int)(*(_DWORD *)(a1 + 16) - *(_DWORD *)(a1 + 12));
}

original_code:
gc_uint32 gc_buffer_bytesAvail(gc_CircBuffer* in_buffer)
{
  return in_buffer->nextFree - in_buffer->nextAvail;
}

canonical_original_code:
unsigned int gc_buffer_bytesAvail(struct gc_CircBuffer *in_buffer)
{
  return in_buffer->nextFree - in_buffer->nextAvail;
}


decompiled_code:
__int64 __fastcall gc_buffer_bytesFree(__int64 a1)
{
int v1; // ebx

v1 = *(_DWORD *)(a1 + 8);
return v1 - (unsigned int)gc_buffer_bytesAvail(a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
int v1; // ebx

v1 = *(_DWORD *)(a1 + 8);
return v1 - (unsigned int)func7(a1);
}

original_code:
gc_uint32 gc_buffer_bytesFree(gc_CircBuffer* in_buffer)
{
  return in_buffer->dataSize - gc_buffer_bytesAvail(in_buffer);
}

canonical_original_code:
unsigned int gc_buffer_bytesFree(struct gc_CircBuffer *in_buffer)
{
  return in_buffer->dataSize - gc_buffer_bytesAvail(in_buffer);
}


decompiled_code:
__int64 __fastcall gc_buffer_getFree(__int64 a1, unsigned int a2, _QWORD *a3, unsigned int *a4, _QWORD *a5, _DWORD *a6)
{
unsigned int v7; // er13
unsigned int v8; // ebp
unsigned int v9; // er15
__int64 result; // rax

v7 = *(_DWORD *)(a1 + 8);
v8 = *(_DWORD *)(a1 + 16) % v7;
v9 = v7 - v8;
if ( (unsigned int)gc_buffer_bytesFree(a1) < a2 )
return 0xFFFFFFFFLL;
*a3 = *(_QWORD *)a1 + v8;
if ( v9 < a2 )
{
*a4 = v9;
*a5 = *(_QWORD *)a1;
*a6 = v8 - v7 + a2;
result = 2LL;
}
else
{
*a4 = a2;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned int a2, _QWORD *a3, unsigned int *a4, _QWORD *a5, _DWORD *a6)
{
unsigned int v7; // er13
unsigned int v8; // ebp
unsigned int v9; // er15
__int64 result; // rax

v7 = *(_DWORD *)(a1 + 8);
v8 = *(_DWORD *)(a1 + 16) % v7;
v9 = v7 - v8;
if ( (unsigned int)func8(a1) < a2 )
return 0xFFFFFFFFLL;
*a3 = *(_QWORD *)a1 + v8;
if ( v9 < a2 )
{
*a4 = v9;
*a5 = *(_QWORD *)a1;
*a6 = v8 - v7 + a2;
result = 2LL;
}
else
{
*a4 = a2;
result = 1LL;
}
return result;
}

original_code:
gc_int32 gc_buffer_getFree(gc_CircBuffer* in_buffer, gc_uint32 in_numBytes,
                           void** out_dataA, gc_uint32* out_sizeA,
                           void** out_dataB, gc_uint32* out_sizeB)
{
  gc_CircBuffer* b = in_buffer;
  gc_uint32 size = b->dataSize;
  gc_uint32 nextFree = b->nextFree % size;
  gc_uint32 nextAvail = b->nextAvail % size;
  gc_uint32 maxBytes = size - nextFree;
  if(in_numBytes > gc_buffer_bytesFree(b))
    return -1;
  if(maxBytes >= in_numBytes)
  {
    *out_dataA = &b->data[nextFree];
    *out_sizeA = in_numBytes;
    return 1;
  }
  else
  {
    *out_dataA = &b->data[nextFree];
    *out_sizeA = maxBytes;
    *out_dataB = &b->data[0];
    *out_sizeB = in_numBytes - maxBytes;
    return 2;
  }
}

canonical_original_code:
int gc_buffer_getFree(struct gc_CircBuffer *in_buffer, unsigned int in_numBytes, void **out_dataA, unsigned int *out_sizeA, void **out_dataB, unsigned int *out_sizeB)
{
  struct gc_CircBuffer *b = in_buffer;
  unsigned int size = b->dataSize;
  unsigned int nextFree = b->nextFree % size;
  unsigned int nextAvail = b->nextAvail % size;
  unsigned int maxBytes = size - nextFree;
  if(in_numBytes > gc_buffer_bytesFree(b))
    return -1;
  if(maxBytes >= in_numBytes)
  {
    *out_dataA = &b->data[nextFree];
    *out_sizeA = in_numBytes;
    return 1;
  }
  else
  {
    *out_dataA = &b->data[nextFree];
    *out_sizeA = maxBytes;
    *out_dataB = &b->data[0];
    *out_sizeB = in_numBytes - maxBytes;
    return 2;
  }
}

variable_types:
size
unsigned int

variable_types:
nextFree
unsigned int

variable_types:
nextAvail
unsigned int

variable_types:
maxBytes
unsigned int


decompiled_code:
__int64 __fastcall gc_buffer_write(__int64 a1, char *a2, unsigned int a3)
{
unsigned int v4; // er14
unsigned int v5; // er13
unsigned int v6; // edx
__int64 result; // rax
unsigned int v8; // er13
unsigned int v9; // er15

v4 = *(_DWORD *)(a1 + 8);
v5 = *(_DWORD *)(a1 + 16);
v6 = gc_buffer_bytesFree(a1);
result = 0xFFFFFFFFLL;
if ( v6 >= a3 )
{
v8 = v5 % v4;
v9 = v4 - v8;
if ( v4 - v8 < a3 )
{
memcpy((void *)(*(_QWORD *)a1 + v8), a2, v9);
memcpy(*(void **)a1, &a2[v9], v8 - v4 + a3);
}
else
{
memcpy((void *)(*(_QWORD *)a1 + v8), a2, a3);
}
*(_DWORD *)(a1 + 16) += a3;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, char *a2, unsigned int a3)
{
unsigned int v4; // er14
unsigned int v5; // er13
unsigned int v6; // edx
__int64 result; // rax
unsigned int v8; // er13
unsigned int v9; // er15

v4 = *(_DWORD *)(a1 + 8);
v5 = *(_DWORD *)(a1 + 16);
v6 = func8(a1);
result = 0xFFFFFFFFLL;
if ( v6 >= a3 )
{
v8 = v5 % v4;
v9 = v4 - v8;
if ( v4 - v8 < a3 )
{
memcpy((void *)(*(_QWORD *)a1 + v8), a2, v9);
memcpy(*(void **)a1, &a2[v9], v8 - v4 + a3);
}
else
{
memcpy((void *)(*(_QWORD *)a1 + v8), a2, a3);
}
*(_DWORD *)(a1 + 16) += a3;
result = 1LL;
}
return result;
}

original_code:
gc_result gc_buffer_write(gc_CircBuffer* in_buffer, void* in_data,
                          gc_uint32 in_numBytes)
{
  gc_CircBuffer* b = in_buffer;
  gc_uint32 size = b->dataSize;
  gc_uint32 nextFree = b->nextFree % size;
  gc_uint32 nextAvail = b->nextAvail % size;
  gc_uint32 maxBytes = size - nextFree;
  if(in_numBytes > gc_buffer_bytesFree(b))
    return -1;
  if(maxBytes >= in_numBytes)
    memcpy(&b->data[nextFree], in_data, in_numBytes);
  else
  {
    memcpy(&b->data[nextFree], in_data, maxBytes);
    memcpy(&b->data[0], (char*)in_data + maxBytes, in_numBytes - maxBytes);
  }
  b->nextFree += in_numBytes;
  return 1;
}

canonical_original_code:
int gc_buffer_write(struct gc_CircBuffer *in_buffer, void *in_data, unsigned int in_numBytes)
{
  struct gc_CircBuffer *b = in_buffer;
  unsigned int size = b->dataSize;
  unsigned int nextFree = b->nextFree % size;
  unsigned int nextAvail = b->nextAvail % size;
  unsigned int maxBytes = size - nextFree;
  if(in_numBytes > gc_buffer_bytesFree(b))
    return -1;
  if(maxBytes >= in_numBytes)
    memcpy(&b->data[nextFree], in_data, in_numBytes);
  else
  {
    memcpy(&b->data[nextFree], in_data, maxBytes);
    memcpy(&b->data[0], (char *)in_data + maxBytes, in_numBytes - maxBytes);
  }
  b->nextFree += in_numBytes;
  return 1;
}


decompiled_code:
__int64 __fastcall gc_buffer_getAvail(__int64 a1, unsigned int a2, _QWORD *a3, unsigned int *a4, _QWORD *a5, _DWORD *a6)
{
unsigned int v11; // esi
unsigned int v12; // ecx
unsigned int v13; // edx
unsigned int v14; // eax
__int64 result; // rax

v11 = gc_buffer_bytesAvail(a1);
v12 = *(_DWORD *)(a1 + 8);
v13 = *(_DWORD *)(a1 + 12) % v12;
v14 = v12 - v13;
if ( v11 < a2 )
return 0xFFFFFFFFLL;
*a3 = *(_QWORD *)a1 + v13;
if ( v14 < a2 )
{
*a4 = v14;
*a5 = *(_QWORD *)a1;
*a6 = v13 - v12 + a2;
result = 2LL;
}
else
{
*a4 = a2;
*a5 = 0LL;
*a6 = 0;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, unsigned int a2, _QWORD *a3, unsigned int *a4, _QWORD *a5, _DWORD *a6)
{
unsigned int v11; // esi
unsigned int v12; // ecx
unsigned int v13; // edx
unsigned int v14; // eax
__int64 result; // rax

v11 = func7(a1);
v12 = *(_DWORD *)(a1 + 8);
v13 = *(_DWORD *)(a1 + 12) % v12;
v14 = v12 - v13;
if ( v11 < a2 )
return 0xFFFFFFFFLL;
*a3 = *(_QWORD *)a1 + v13;
if ( v14 < a2 )
{
*a4 = v14;
*a5 = *(_QWORD *)a1;
*a6 = v13 - v12 + a2;
result = 2LL;
}
else
{
*a4 = a2;
*a5 = 0LL;
*a6 = 0;
result = 1LL;
}
return result;
}

original_code:
gc_int32 gc_buffer_getAvail(gc_CircBuffer* in_buffer, gc_uint32 in_numBytes,
                            void** out_dataA, gc_uint32* out_sizeA,
                            void** out_dataB, gc_uint32* out_sizeB)
{
  gc_CircBuffer* b = in_buffer;
  gc_uint32 bytesAvailable = gc_buffer_bytesAvail(in_buffer);
  gc_uint32 size = b->dataSize;
  gc_uint32 nextFree = b->nextFree % size;
  gc_uint32 nextAvail = b->nextAvail % size;
  gc_uint32 maxBytes = size - nextAvail;
  if(bytesAvailable < in_numBytes)
    return -1;
  if(maxBytes >= in_numBytes)
  {
    *out_dataA = &b->data[nextAvail];
    *out_sizeA = in_numBytes;
    *out_dataB = 0;
    *out_sizeB = 0;
    return 1;
  }
  else
  {
    *out_dataA = &b->data[nextAvail];
    *out_sizeA = maxBytes;
    *out_dataB = &b->data[0];
    *out_sizeB = in_numBytes - maxBytes;
    return 2;
  }
}

canonical_original_code:
int gc_buffer_getAvail(struct gc_CircBuffer *in_buffer, unsigned int in_numBytes, void **out_dataA, unsigned int *out_sizeA, void **out_dataB, unsigned int *out_sizeB)
{
  struct gc_CircBuffer *b = in_buffer;
  unsigned int bytesAvailable = gc_buffer_bytesAvail(in_buffer);
  unsigned int size = b->dataSize;
  unsigned int nextFree = b->nextFree % size;
  unsigned int nextAvail = b->nextAvail % size;
  unsigned int maxBytes = size - nextAvail;
  if(bytesAvailable < in_numBytes)
    return -1;
  if(maxBytes >= in_numBytes)
  {
    *out_dataA = &b->data[nextAvail];
    *out_sizeA = in_numBytes;
    *out_dataB = 0;
    *out_sizeB = 0;
    return 1;
  }
  else
  {
    *out_dataA = &b->data[nextAvail];
    *out_sizeA = maxBytes;
    *out_dataB = &b->data[0];
    *out_sizeB = in_numBytes - maxBytes;
    return 2;
  }
}

variable_types:
bytesAvailable
unsigned int


decompiled_code:
__int64 __fastcall gc_buffer_read(__int64 a1, char *a2, unsigned int a3)
{
__int64 result; // rax
int v4; // ebx
__int64 v5; // r12
size_t n; // [rsp+8h] [rbp-30h] BYREF
void *src; // [rsp+10h] [rbp-28h] BYREF
void *v8; // [rsp+18h] [rbp-20h] BYREF

result = gc_buffer_getAvail(a1, a3, &src, (unsigned int *)&n, &v8, (_DWORD *)&n + 1);
if ( (int)result > 0 )
{
v4 = result;
v5 = (unsigned int)n;
result = (__int64)memcpy(a2, src, (unsigned int)n);
if ( v4 == 2 )
result = (__int64)memcpy(&a2[v5], v8, HIDWORD(n));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, char *a2, unsigned int a3)
{
__int64 result; // rax
int v4; // ebx
__int64 v5; // r12
size_t n; // [rsp+8h] [rbp-30h] BYREF
void *src; // [rsp+10h] [rbp-28h] BYREF
void *v8; // [rsp+18h] [rbp-20h] BYREF

result = func11(a1, a3, &src, (unsigned int *)&n, &v8, (_DWORD *)&n + 1);
if ( (int)result > 0 )
{
v4 = result;
v5 = (unsigned int)n;
result = (__int64)memcpy(a2, src, (unsigned int)n);
if ( v4 == 2 )
result = (__int64)memcpy(&a2[v5], v8, HIDWORD(n));
}
return result;
}

original_code:
void gc_buffer_read(gc_CircBuffer* in_buffer, void* in_data,
                    gc_uint32 in_numBytes)
{
  void* data[2];
  gc_uint32 size[2];
  gc_int32 ret = gc_buffer_getAvail(in_buffer, in_numBytes,
                                   &data[0], &size[0],
                                   &data[1], &size[1]);
  if(ret >= 1)
  {
    memcpy(in_data, data[0], size[0]);
    if(ret == 2)
      memcpy((char*)in_data + size[0], data[1], size[1]);
  }
}

canonical_original_code:
void gc_buffer_read(struct gc_CircBuffer *in_buffer, void *in_data, unsigned int in_numBytes)
{
  void *data[2];
  unsigned int size[2];
  int ret = gc_buffer_getAvail(in_buffer, in_numBytes,
                                   &data[0], &size[0],
                                   &data[1], &size[1]);
  if(ret >= 1)
  {
    memcpy(in_data, data[0], size[0]);
    if(ret == 2)
      memcpy((char *)in_data + size[0], data[1], size[1]);
  }
}

variable_types:
data
void *[2]

variable_types:
size
unsigned int [2]

variable_types: ret int


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

result = (unsigned int)(a2 + *(_DWORD *)(a1 + 16));
*(_DWORD *)(a1 + 16) = result;
return result;
}

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

result = (unsigned int)(a2 + *(_DWORD *)(a1 + 16));
*(_DWORD *)(a1 + 16) = result;
return result;
}

original_code:
void gc_buffer_produce(gc_CircBuffer* in_buffer, gc_uint32 in_numBytes)
{
  in_buffer->nextFree += in_numBytes;
}

canonical_original_code:
void gc_buffer_produce(struct gc_CircBuffer *in_buffer, unsigned int in_numBytes)
{
  in_buffer->nextFree += in_numBytes;
}


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

result = (unsigned int)(a2 + *(_DWORD *)(a1 + 12));
*(_DWORD *)(a1 + 12) = result;
return result;
}

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

result = (unsigned int)(a2 + *(_DWORD *)(a1 + 12));
*(_DWORD *)(a1 + 12) = result;
return result;
}

original_code:
void gc_buffer_consume(gc_CircBuffer* in_buffer, gc_uint32 in_numBytes)
{
  in_buffer->nextAvail += in_numBytes;
}

canonical_original_code:
void gc_buffer_consume(struct gc_CircBuffer *in_buffer, unsigned int in_numBytes)
{
  in_buffer->nextAvail += in_numBytes;
}


original_code:
void gc_list_link(gc_Link* in_head, gc_Link* in_link, void* in_data)
{
  in_link->data = in_data;
  in_link->prev = in_head;
  in_link->next = in_head->next;
  in_head->next->prev = in_link;
  in_head->next = in_link;
}

canonical_original_code:
void gc_list_link(struct gc_Link *in_head, struct gc_Link *in_link, void *in_data)
{
  in_link->data = in_data;
  in_link->prev = in_head;
  in_link->next = in_head->next;
  in_head->next->prev = in_link;
  in_head->next = in_link;
}

variable_types: struct gc_Link { struct gc_Link *next; struct gc_Link *prev; void *data; } *


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

result = *a1;
*(_QWORD *)a1[1] = *a1;
*(_QWORD *)(result + 8) = a1[1];
a1[1] = 0LL;
*a1 = 0LL;
a1[2] = 0LL;
return result;
}

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

result = *a1;
*(_QWORD *)a1[1] = *a1;
*(_QWORD *)(result + 8) = a1[1];
a1[1] = 0LL;
*a1 = 0LL;
a1[2] = 0LL;
return result;
}

original_code:
void gc_list_unlink(gc_Link* in_link)
{
  in_link->prev->next = in_link->next;
  in_link->next->prev = in_link->prev;
  in_link->prev = 0;
  in_link->next = 0;
  in_link->data = 0;
}

canonical_original_code:
void gc_list_unlink(struct gc_Link *in_link)
{
  in_link->prev->next = in_link->next;
  in_link->next->prev = in_link->prev;
  in_link->prev = 0;
  in_link->next = 0;
  in_link->data = 0;
}

user_defined_types: struct gc_Link { struct gc_Link *next; struct gc_Link *prev; void *data; }


StaticThreadWrapper

decompiled_code:
void *__fastcall StaticThreadWrapper(void *a1)
{
pthread_mutex_lock((pthread_mutex_t *)((char *)a1 + 64));
(*((void (__fastcall **)(_QWORD))a1 + 13))(*((_QWORD *)a1 + 14));
pthread_mutex_unlock((pthread_mutex_t *)((char *)a1 + 64));
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
pthread_mutex_lock((pthread_mutex_t *)((char *)a1 + 64));
(*((void (__fastcall **)(_QWORD))a1 + 13))(*((_QWORD *)a1 + 14));
pthread_mutex_unlock((pthread_mutex_t *)((char *)a1 + 64));
return 0LL;
}

original_code:
static void* StaticThreadWrapper(void* in_context)
{
  LinuxThreadData* threadData = (LinuxThreadData*)in_context;
  pthread_mutex_lock(&threadData->suspendMutex);
  threadData->threadFunc(threadData->context);
  pthread_mutex_unlock(&threadData->suspendMutex);
  return 0;
}

canonical_original_code:
static void *StaticThreadWrapper(void *in_context)
{
  struct LinuxThreadData *threadData = (LinuxThreadData*)in_context;
  pthread_mutex_lock(&threadData->suspendMutex);
  threadData->threadFunc(threadData->context);
  pthread_mutex_unlock(&threadData->suspendMutex);
  return 0;
}

variable_types: struct LinuxThreadData { unsigned long thread; union pthread_attr_t { char __size[56]; long __align; } attr; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } suspendMutex; int (*threadFunc)(void *in_context); void *context; } *

user_defined_types: struct LinuxThreadData { unsigned long thread; union pthread_attr_t attr; union pthread_mutex_t suspendMutex; int (*threadFunc)(void *in_context); void *context; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall gc_thread_create(__int64 a1, __int64 a2, int a3, int a4)
{
__int64 v6; // rbp
__int64 v7; // rbx
struct sched_param param; // [rsp+Ch] [rbp-3Ch] BYREF

v6 = ((__int64 (__fastcall *)(__int64))*gcX_ops)(40LL);
v7 = ((__int64 (__fastcall *)(__int64))*gcX_ops)(120LL);
*(_QWORD *)(v7 + 104) = a1;
*(_QWORD *)(v7 + 112) = a2;
*(_QWORD *)(v6 + 8) = v7;
*(_QWORD *)v6 = a1;
*(_QWORD *)(v6 + 16) = a2;
*(_DWORD *)(v6 + 28) = a3;
*(_DWORD *)(v6 + 32) = a4;
pthread_mutex_init((pthread_mutex_t *)(v7 + 64), 0LL);
pthread_mutex_lock((pthread_mutex_t *)(v7 + 64));
pthread_attr_init((pthread_attr_t *)(v7 + 8));
param.__sched_priority = priorityLut[a3];
pthread_attr_setschedparam((pthread_attr_t *)(v7 + 8), &param);
pthread_create((pthread_t *)v7, (const pthread_attr_t *)(v7 + 8), StaticThreadWrapper, (void *)v7);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, int a3, int a4)
{
__int64 v6; // rbp
__int64 v7; // rbx
struct sched_param param; // [rsp+Ch] [rbp-3Ch] BYREF

v6 = ((__int64 (__fastcall *)(__int64))*gcX_ops)(40LL);
v7 = ((__int64 (__fastcall *)(__int64))*gcX_ops)(120LL);
*(_QWORD *)(v7 + 104) = a1;
*(_QWORD *)(v7 + 112) = a2;
*(_QWORD *)(v6 + 8) = v7;
*(_QWORD *)v6 = a1;
*(_QWORD *)(v6 + 16) = a2;
*(_DWORD *)(v6 + 28) = a3;
*(_DWORD *)(v6 + 32) = a4;
pthread_mutex_init((pthread_mutex_t *)(v7 + 64), 0LL);
pthread_mutex_lock((pthread_mutex_t *)(v7 + 64));
pthread_attr_init((pthread_attr_t *)(v7 + 8));
param.__sched_priority = priorityLut[a3];
pthread_attr_setschedparam((pthread_attr_t *)(v7 + 8), &param);
pthread_create((pthread_t *)v7, (const pthread_attr_t *)(v7 + 8), StaticThreadWrapper, (void *)v7);
return v6;
}

original_code:
gc_Thread* gc_thread_create(gc_ThreadFunc in_threadFunc, void* in_context,
                            gc_int32 in_priority, gc_int32 in_stackSize)
{
  int result = 0;
  struct sched_param param;
  gc_Thread* ret = gcX_ops->allocFunc(sizeof(gc_Thread));
  LinuxThreadData* threadData = (LinuxThreadData*)gcX_ops->allocFunc(sizeof(LinuxThreadData));
  threadData->threadFunc = in_threadFunc;
  threadData->context = in_context;
  ret->threadObj = threadData;
  ret->threadFunc = in_threadFunc;
  ret->context = in_context;
  ret->priority = in_priority;
  ret->stackSize = in_stackSize;
  pthread_mutex_init(&threadData->suspendMutex, ((void *)0));
  pthread_mutex_lock(&threadData->suspendMutex);
  result = pthread_attr_init(&threadData->attr);
  param.sched_priority = priorityLut[in_priority];
  pthread_attr_setschedparam(&threadData->attr, &param);
  pthread_create(&threadData->thread, &threadData->attr, StaticThreadWrapper, threadData);
  return ret;
}

canonical_original_code:
struct gc_Thread *gc_thread_create(int (*in_threadFunc)(void *in_context), void *in_context, int in_priority, int in_stackSize)
{
  int result = 0;
  struct sched_param param;
  struct gc_Thread *ret = gcX_ops->allocFunc(sizeof(gc_Thread));
  struct LinuxThreadData *threadData = (LinuxThreadData*)gcX_ops->allocFunc(sizeof(LinuxThreadData));
  threadData->threadFunc = in_threadFunc;
  threadData->context = in_context;
  ret->threadObj = threadData;
  ret->threadFunc = in_threadFunc;
  ret->context = in_context;
  ret->priority = in_priority;
  ret->stackSize = in_stackSize;
  pthread_mutex_init(&threadData->suspendMutex, ((void *)0));
  pthread_mutex_lock(&threadData->suspendMutex);
  result = pthread_attr_init(&threadData->attr);
  param.sched_priority = priorityLut[in_priority];
  pthread_attr_setschedparam(&threadData->attr, &param);
  pthread_create(&threadData->thread, &threadData->attr, StaticThreadWrapper, threadData);
  return ret;
}

variable_types: result int

variable_types: struct sched_param { int sched_priority; }

variable_types: struct gc_Thread { int (*threadFunc)(void *in_context); void *threadObj; void *context; int id; int priority; int stackSize; } *

user_defined_types: union pthread_attr_t { char __size[56]; long __align; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
int __fastcall gc_thread_run(__int64 a1)
{
return pthread_mutex_unlock((pthread_mutex_t *)(*(_QWORD *)(a1 + 8) + 64LL));
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
return pthread_mutex_unlock((pthread_mutex_t *)(*(_QWORD *)(a1 + 8) + 64LL));
}

original_code:
void gc_thread_run(gc_Thread* in_thread)
{
  LinuxThreadData* threadData = (LinuxThreadData*)in_thread->threadObj;
  pthread_mutex_unlock(&threadData->suspendMutex);
}

canonical_original_code:
void gc_thread_run(struct gc_Thread *in_thread)
{
  struct LinuxThreadData *threadData = (LinuxThreadData*)in_thread->threadObj;
  pthread_mutex_unlock(&threadData->suspendMutex);
}

user_defined_types: struct gc_Thread { int (*threadFunc)(void *in_context); void *threadObj; void *context; int id; int priority; int stackSize; }


decompiled_code:
int __fastcall gc_thread_join(__int64 a1)
{
return pthread_join(**(_QWORD **)(a1 + 8), 0LL);
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
return pthread_join(**(_QWORD **)(a1 + 8), 0LL);
}

original_code:
void gc_thread_join(gc_Thread* in_thread)
{
  LinuxThreadData* threadData = (LinuxThreadData*)in_thread->threadObj;
  pthread_join(threadData->thread, 0);
}

canonical_original_code:
void gc_thread_join(struct gc_Thread *in_thread)
{
  struct LinuxThreadData *threadData = (LinuxThreadData*)in_thread->threadObj;
  pthread_join(threadData->thread, 0);
}


decompiled_code:
int __fastcall gc_thread_sleep(int a1)
{
return usleep(1000 * a1);
}

canonical_decompiled_code:
int __fastcall func4(int a1)
{
return usleep(1000 * a1);
}

original_code:
void gc_thread_sleep(gc_uint32 in_ms)
{
  usleep(in_ms * 1000);
}

canonical_original_code:
void gc_thread_sleep(unsigned int in_ms)
{
  usleep(in_ms * 1000);
}


decompiled_code:
void __fastcall __noreturn gc_thread_destroy(__int64 a1)
{
__int64 v1; // rbx

v1 = *(_QWORD *)(a1 + 8);
pthread_mutex_destroy((pthread_mutex_t *)(v1 + 64));
pthread_exit((void *)v1);
}

canonical_decompiled_code:
void __fastcall __noreturn func5(__int64 a1)
{
__int64 v1; // rbx

v1 = *(_QWORD *)(a1 + 8);
pthread_mutex_destroy((pthread_mutex_t *)(v1 + 64));
pthread_exit((void *)v1);
}

original_code:
void gc_thread_destroy(gc_Thread* in_thread)
{
  LinuxThreadData* threadData = (LinuxThreadData*)in_thread->threadObj;
  pthread_mutex_destroy(&threadData->suspendMutex);
  pthread_exit(&threadData->thread);
  pthread_attr_destroy(&threadData->attr);
}

canonical_original_code:
void gc_thread_destroy(struct gc_Thread *in_thread)
{
  struct LinuxThreadData *threadData = (LinuxThreadData*)in_thread->threadObj;
  pthread_mutex_destroy(&threadData->suspendMutex);
  pthread_exit(&threadData->thread);
  pthread_attr_destroy(&threadData->attr);
}


decompiled_code:
pthread_mutex_t **gc_mutex_create()
{
pthread_mutex_t **v0; // rbx
pthread_mutex_t *v1; // rax

v0 = (pthread_mutex_t **)((__int64 (__fastcall *)(__int64))*gcX_ops)(8LL);
v1 = (pthread_mutex_t *)((__int64 (__fastcall *)(__int64))*gcX_ops)(40LL);
*v0 = v1;
pthread_mutex_init(v1, 0LL);
return v0;
}

canonical_decompiled_code:
pthread_mutex_t **func6()
{
pthread_mutex_t **v0; // rbx
pthread_mutex_t *v1; // rax

v0 = (pthread_mutex_t **)((__int64 (__fastcall *)(__int64))*gcX_ops)(8LL);
v1 = (pthread_mutex_t *)((__int64 (__fastcall *)(__int64))*gcX_ops)(40LL);
*v0 = v1;
pthread_mutex_init(v1, 0LL);
return v0;
}

original_code:
gc_Mutex* gc_mutex_create()
{
  gc_Mutex* ret = gcX_ops->allocFunc(sizeof(gc_Mutex));
  ret->mutex = gcX_ops->allocFunc(sizeof(pthread_mutex_t));
  pthread_mutex_init((pthread_mutex_t*)ret->mutex, ((void *)0));
  return ret;
}

canonical_original_code:
struct gc_Mutex *gc_mutex_create()
{
  struct gc_Mutex *ret = gcX_ops->allocFunc(sizeof(gc_Mutex));
  ret->mutex = gcX_ops->allocFunc(sizeof(pthread_mutex_t));
  pthread_mutex_init((union pthread_mutex_t *)ret->mutex, ((void *)0));
  return ret;
}

variable_types:
ret
struct gc_Mutex { void *mutex; } *

return_type: struct gc_Mutex { void *mutex; } *


decompiled_code:
__int64 __fastcall gc_mutex_destroy(pthread_mutex_t **a1)
{
pthread_mutex_destroy(*a1);
(*(void (__fastcall **)(pthread_mutex_t *))(gcX_ops + 16LL))(*a1);
return (*(__int64 (__fastcall **)(pthread_mutex_t **))(gcX_ops + 16LL))(a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(pthread_mutex_t **a1)
{
pthread_mutex_destroy(*a1);
(*(void (__fastcall **)(pthread_mutex_t *))(gcX_ops + 16LL))(*a1);
return (*(__int64 (__fastcall **)(pthread_mutex_t **))(gcX_ops + 16LL))(a1);
}

original_code:
void gc_mutex_destroy(gc_Mutex* in_mutex)
{
  pthread_mutex_destroy((pthread_mutex_t*)in_mutex->mutex);
  gcX_ops->freeFunc(in_mutex->mutex);
  gcX_ops->freeFunc(in_mutex);
}

canonical_original_code:
void gc_mutex_destroy(struct gc_Mutex *in_mutex)
{
  pthread_mutex_destroy((union pthread_mutex_t *)in_mutex->mutex);
  gcX_ops->freeFunc(in_mutex->mutex);
  gcX_ops->freeFunc(in_mutex);
}


decompiled_code:
int __fastcall gc_mutex_lock(pthread_mutex_t **a1)
{
return pthread_mutex_lock(*a1);
}

canonical_decompiled_code:
int __fastcall func8(pthread_mutex_t **a1)
{
return pthread_mutex_lock(*a1);
}

original_code:
void gc_mutex_lock(gc_Mutex* in_mutex)
{
  pthread_mutex_lock((pthread_mutex_t*)in_mutex->mutex);
}

canonical_original_code:
void gc_mutex_lock(struct gc_Mutex *in_mutex)
{
  pthread_mutex_lock((union pthread_mutex_t *)in_mutex->mutex);
}


decompiled_code:
int __fastcall gc_mutex_unlock(pthread_mutex_t **a1)
{
return pthread_mutex_unlock(*a1);
}

canonical_decompiled_code:
int __fastcall func9(pthread_mutex_t **a1)
{
return pthread_mutex_unlock(*a1);
}

original_code:
void gc_mutex_unlock(gc_Mutex* in_mutex)
{
  pthread_mutex_unlock((pthread_mutex_t*)in_mutex->mutex);
}

canonical_original_code:
void gc_mutex_unlock(struct gc_Mutex *in_mutex)
{
  pthread_mutex_unlock((union pthread_mutex_t *)in_mutex->mutex);
}


decompiled_code:
const char *__fastcall gaX_openAlErrorToString(int a1)
{
const char *result; // rax

result = "OpenAL error - None";
if ( a1 )
result = "OpenAL error - Unknown error.";
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(int a1)
{
const char *result; // rax

result = "OpenAL error - None";
if ( a1 )
result = "OpenAL error - Unknown error.";
return result;
}

original_code:
const char* gaX_openAlErrorToString(ALuint error)
{
  const char* errMsg = 0;
  switch(error)
  {
  case 0: errMsg = "OpenAL error - None"; break;
  case 0xA001: errMsg = "OpenAL error - Invalid name."; break;
  case 0xA002: errMsg = "OpenAL error - Invalid enum."; break;
  case 0xA003: errMsg = "OpenAL error - Invalid value."; break;
  case 0xA004: errMsg = "OpenAL error - Invalid op."; break;
  case 0xA005: errMsg = "OpenAL error - Out of memory."; break;
  default: errMsg = "OpenAL error - Unknown error."; break;
  }
  return errMsg;
}

canonical_original_code:
char *gaX_openAlErrorToString(unsigned int error)
{
  char *errMsg = 0;
  switch(error)
  {
  case 0: errMsg = "OpenAL error - None"; break;
  case 0xA001: errMsg = "OpenAL error - Invalid name."; break;
  case 0xA002: errMsg = "OpenAL error - Invalid enum."; break;
  case 0xA003: errMsg = "OpenAL error - Invalid value."; break;
  case 0xA004: errMsg = "OpenAL error - Invalid op."; break;
  case 0xA005: errMsg = "OpenAL error - Out of memory."; break;
  default: errMsg = "OpenAL error - Unknown error."; break;
  }
  return errMsg;
}

variable_types:
error
unsigned int

variable_types:
errMsg
char *

return_type: char *


mgetopt

repo: mfanone/libmutils

decompiled_code:
__int64 __fastcall mgetopt(int a1, __int64 a2, const char *a3)
{
int v3; // ebp
__int64 v4; // r15
__int64 v5; // rbx
unsigned int v6; // er14
char *v7; // rax

v3 = optind;
if ( optind >= a1 )
return (unsigned int)-1;
v4 = 8LL * optind;
v5 = *(_QWORD *)(a2 + v4);
if ( *(_BYTE *)v5 != 45 )
return (unsigned int)-1;
if ( !strcmp(*(const char **)(a2 + 8LL * optind), "-") )
return (unsigned int)-1;
if ( !strcmp((const char *)v5, "--") )
{
++optind;
v6 = -1;
}
else
{
v6 = *(char *)(v5 + 1);
v7 = strchr(a3, v6);
if ( v7 )
{
if ( v7[1] == 58 )
{
if ( *(_BYTE *)(v5 + 2) )
{
optarg = v5 + 2;
optind = v3 + 1;
}
else
{
optind = v3 + 1;
optarg = *(_QWORD *)(a2 + v4 + 8);
optind = v3 + 2;
if ( v3 + 2 > a1 )
{
optopt = v6;
v6 = 63;
}
}
}
else if ( *(_BYTE *)(v5 + 2) )
{
*(_BYTE *)(v5 + 1) = 45;
++*(_QWORD *)(a2 + 8LL * optind);
}
else
{
optind = v3 + 1;
}
}
else
{
optopt = v6;
v6 = 63;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, __int64 a2, const char *a3)
{
int v3; // ebp
__int64 v4; // r15
__int64 v5; // rbx
unsigned int v6; // er14
char *v7; // rax

v3 = optind;
if ( optind >= a1 )
return (unsigned int)-1;
v4 = 8LL * optind;
v5 = *(_QWORD *)(a2 + v4);
if ( *(_BYTE *)v5 != 45 )
return (unsigned int)-1;
if ( !strcmp(*(const char **)(a2 + 8LL * optind), "-") )
return (unsigned int)-1;
if ( !strcmp((const char *)v5, "--") )
{
++optind;
v6 = -1;
}
else
{
v6 = *(char *)(v5 + 1);
v7 = strchr(a3, v6);
if ( v7 )
{
if ( v7[1] == 58 )
{
if ( *(_BYTE *)(v5 + 2) )
{
optarg = v5 + 2;
optind = v3 + 1;
}
else
{
optind = v3 + 1;
optarg = *(_QWORD *)(a2 + v4 + 8);
optind = v3 + 2;
if ( v3 + 2 > a1 )
{
optopt = v6;
v6 = 63;
}
}
}
else if ( *(_BYTE *)(v5 + 2) )
{
*(_BYTE *)(v5 + 1) = 45;
++*(_QWORD *)(a2 + 8LL * optind);
}
else
{
optind = v3 + 1;
}
}
else
{
optopt = v6;
v6 = 63;
}
}
return v6;
}

original_code:
int mgetopt(int argc, char *argv[], const char *optstr)
{
    char *p;
    int c;
    if (optind >= argc || argv[optind][0] != '-'
        || strcmp(argv[optind], "-") == 0)
 return -1;
    if (strcmp(argv[optind], "--") == 0) {
        optind++;
        return -1;
    }
    if ((p = strchr(optstr, argv[optind][1])) == ((void *)0)) {
 optopt = argv[optind][1];
 return '?';
    }
    c = argv[optind][1];
    if (*++p == ':') {
        if (argv[optind][2] == '\0') {
            optarg = argv[++optind];
            if (++optind > argc) {
                optopt = c;
                c = '?';
            }
        } else {
     optarg = &argv[optind][2];
     optind++;
 }
    } else {
 if (argv[optind][2] != '\0') {
     argv[optind][1] = '-';
            argv[optind]++;
 } else {
            optind++;
        }
    }
    return c;
}

canonical_original_code:
int mgetopt(int argc, char *argv[], char *optstr)
{
    char *p;
    int c;
    if (optind >= argc || argv[optind][0] != '-'
        || strcmp(argv[optind], "-") == 0)
 return -1;
    if (strcmp(argv[optind], "--") == 0) {
        optind++;
        return -1;
    }
    if ((p = strchr(optstr, argv[optind][1])) == ((void *)0)) {
 optopt = argv[optind][1];
 return '?';
    }
    c = argv[optind][1];
    if (*++p == ':') {
        if (argv[optind][2] == '\0') {
            optarg = argv[++optind];
            if (++optind > argc) {
                optopt = c;
                c = '?';
            }
        } else {
     optarg = &argv[optind][2];
     optind++;
 }
    } else {
 if (argv[optind][2] != '\0') {
     argv[optind][1] = '-';
            argv[optind]++;
 } else {
            optind++;
        }
    }
    return c;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
optstr
char *

variable_types:
p
char *

variable_types: c int


repo: mgalgs/termship

decompiled_code:
int __fastcall sigsegv_handler(int sig)
{
__pid_t v1; // eax

cleanup_ncurses();
puts("Whoa, we just overwrote The Grid. Cora's dumping the core of whatever's left...");
fflush(stdout);
signal(sig, 0LL);
v1 = getpid();
return kill(v1, sig);
}

canonical_decompiled_code:
int __fastcall func0(int sig)
{
__pid_t v1; // eax

cleanup_ncurses();
puts("Whoa, we just overwrote The Grid. Cora's dumping the core of whatever's left...");
fflush(stdout);
signal(sig, 0LL);
v1 = getpid();
return kill(v1, sig);
}

original_code:
void sigsegv_handler(int sig)
{
  cleanup_ncurses();
  printf("Whoa, we just overwrote The Grid. Cora's dumping the core of whatever's left...\n");
  fflush(stdout);
  signal(sig, ((__sighandler_t) 0));
  kill(getpid(), sig);
}

canonical_original_code:
void sigsegv_handler(int sig)
{
  cleanup_ncurses();
  printf("Whoa, we just overwrote The Grid. Cora's dumping the core of whatever's left...\n");
  fflush(stdout);
  signal(sig, ((void (*)(int)) 0));
  kill(getpid(), sig);
}

variable_types: sig int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
struct sigaction v5; // [rsp+0h] [rbp-A8h] BYREF

v5.sa_flags = 4;
sigemptyset(&v5.sa_mask);
v5.sa_handler = (__sighandler_t)sigsegv_handler;
if ( sigaction(11, &v5, 0LL) == -1 )
{
perror("Couldn't set up signal handler...");
exit(1);
}
v3 = time(0LL);
srand(v3);
write_to_log("\n=======================\ntermship is starting!!!\n=======================\n");
initscr();
cbreak();
noecho();
start_color();
init_pair(2, 3, 0);
init_pair(3, 4, 0);
init_pair(4, 1, 0);
init_pair(5, 0, 7);
init_pair(6, 7, 0);
init_pair(7, 2, 0);
init_pair(8, 7, 1);
main_menu();
cleanup_ncurses();
return 0;
}

original_code:
int main() {
  struct sigaction sa;
  sa.sa_flags = 4;
  sigemptyset(&sa.sa_mask);
  sa.__sigaction_handler.sa_handler = sigsegv_handler;
  if (sigaction(11, &sa, ((void *)0)) == -1) {
    perror("Couldn't set up signal handler...");
    exit(1);
  }
  srand(time(((void *)0)));
  write_to_log("\n"
        "=======================\n"
        "termship is starting!!!\n"
        "=======================\n");
  initscr();
  cbreak();
  noecho();
  start_color();
  init_pair(2, 3, 0);
  init_pair(3, 4, 0);
  init_pair(4, 1, 0);
  init_pair(5, 0, 7);
  init_pair(6, 7, 0);
  init_pair(7, 2, 0);
  init_pair(8, 7, 1);
  main_menu();
  cleanup_ncurses();
  return 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; }


validMesg

decompiled_code:
_BOOL8 __fastcall validMesg(unsigned int a1)
{
return a1 <= 2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(unsigned int a1)
{
return a1 <= 2;
}

original_code:
int validMesg(int MESS)
{
  return ((MESS == BFIRE) || (MESS == BHIT) || (MESS == BMISS));
}

variable_types: MESS int


validCode

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

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

original_code:
int validCode(char *code)
{
  return 1;
}

canonical_original_code:
int validCode(char *code)
{
  return 1;
}

variable_types:
code
char *


CreateEmptyBMesg

decompiled_code:
char *CreateEmptyBMesg()
{
char *v0; // rax
char *v1; // rcx
char *v2; // rax

v0 = (char *)malloc(0x10uLL);
if ( !v0 )
{
perror("malloc error");
exit(1);
}
v1 = v0;
v2 = v0 + 4;
do
*v2++ = 0;
while ( v2 != v1 + 14 );
return v1;
}

canonical_decompiled_code:
char *func2()
{
char *v0; // rax
char *v1; // rcx
char *v2; // rax

v0 = (char *)malloc(0x10uLL);
if ( !v0 )
{
perror("malloc error");
exit(1);
}
v1 = v0;
v2 = v0 + 4;
do
*v2++ = 0;
while ( v2 != v1 + 14 );
return v1;
}

original_code:
BMesg *CreateEmptyBMesg()
{
  BMesg *newBMesg;
  int i;
  if ((newBMesg = (BMesg *) malloc(sizeof(BMesg))) == ((void *)0)) {
    perror("malloc error");
    exit(1);
  }
  for (i=0; i<10; i++)
    newBMesg->code[i]=0;
  return newBMesg;
}

canonical_original_code:
struct BMesg_ *CreateEmptyBMesg()
{
  struct BMesg_ *newBMesg;
  int i;
  if ((newBMesg = (struct BMesg_ *) malloc(sizeof(BMesg))) == ((void *)0)) {
    perror("malloc error");
    exit(1);
  }
  for (i=0; i<10; i++)
    newBMesg->code[i]=0;
  return newBMesg;
}

variable_types: struct BMesg_ { enum MESSAGE { BFIRE=0, BHIT=1, BMISS=2 } msg; char code[10]; } *

variable_types: i int

user_defined_types: struct BMesg_ { enum MESSAGE msg; char code[10]; }


CreateBMesg

decompiled_code:
char *__fastcall CreateBMesg(unsigned int a1, unsigned int a2, unsigned int a3)
{
char *v4; // rbx

v4 = CreateEmptyBMesg();
if ( !validMesg(a1) )
{
cleanup_ncurses();
perror("Invalide message passed to CreateBMesg");
exit(1);
}
*(_DWORD *)v4 = a1;
sprintf(v4 + 4, "%d,%d", a2, a3);
return v4;
}

canonical_decompiled_code:
char *__fastcall func3(unsigned int a1, unsigned int a2, unsigned int a3)
{
char *v4; // rbx

v4 = func2();
if ( !func0(a1) )
{
cleanup_ncurses();
perror("Invalide message passed to CreateBMesg");
exit(1);
}
*(_DWORD *)v4 = a1;
sprintf(v4 + 4, "%d,%d", a2, a3);
return v4;
}

original_code:
BMesg *CreateBMesg(int MESS, int x, int y)
{
  BMesg *newBMesg;
  newBMesg = CreateEmptyBMesg();
  if (!validMesg(MESS)) {
    cleanup_ncurses();
    perror("Invalide message passed to CreateBMesg");
    exit(1);
  }
  newBMesg->msg = MESS;
  sprintf(newBMesg->code, "%d,%d", x, y);
  return newBMesg;
}

canonical_original_code:
struct BMesg_ *CreateBMesg(int MESS, int x, int y)
{
  struct BMesg_ *newBMesg;
  newBMesg = CreateEmptyBMesg();
  if (!validMesg(MESS)) {
    cleanup_ncurses();
    perror("Invalide message passed to CreateBMesg");
    exit(1);
  }
  newBMesg->msg = MESS;
  sprintf(newBMesg->code, "%d,%d", x, y);
  return newBMesg;
}

variable_types: x int

variable_types: y int

return_type: struct BMesg_ { enum MESSAGE { BFIRE=0, BHIT=1, BMISS=2 } msg; char code[10]; } *


decompiled_code:
__int64 get_battleship_port()
{
return (unsigned int)(42 * (rand() % 10) + 4740);
}

original_code:
uint16_t get_battleship_port()
{
  return 4740 + ((rand() % 10) * 42);
}

canonical_original_code:
unsigned short get_battleship_port()
{
  return 4740 + ((rand() % 10) * 42);
}

return_type: unsigned short


decompiled_code:
__int64 __fastcall check_hit(__int64 a1)
{
const char *v1; // r12
char *v2; // rax
int v3; // ebx
int v4; // er14
unsigned int v5; // ebp
unsigned int v6; // er12
__int64 v7; // rdx
unsigned int v8; // er13
__int64 v9; // rax
unsigned int v10; // ebx
__int64 result; // rax
char dest[50]; // [rsp+6h] [rbp-32h] BYREF

wrefresh(stdscr);
v1 = (const char *)(a1 + 4);
v2 = index((const char *)(a1 + 4), 44);
if ( !v2 )
{
cleanup_ncurses();
perror("Invalid BFIRE message detected in check_hit");
exit(1);
}
v3 = (_DWORD)v2 - (_DWORD)v1;
v4 = strlen(v1);
strncpy(dest, v1, v3);
v5 = atoi(dest);
strncpy(dest, &v1[v3 + 1], ~v3 + v4);
v6 = atoi(dest);
wrefresh(stdscr);
v7 = 0LL;
v8 = 0;
while ( Shipset[v7 + 9] || !Shipset[v7 + 8] )
{
LABEL_15:
++v8;
v7 += 24LL;
if ( v8 == 5 )
{
place_hit_or_mis(opponent_win, 0LL, v5, v6, 1LL);
return 0LL;
}
}
v9 = 0LL;
while ( 1 )
{
v10 = v9;
if ( Shipset[v7 + 13 + v9] == 1 )
goto LABEL_5;
if ( Shipset[v7 + 12] == 1 )
break;
if ( (_DWORD)v9 + Shipset[v7 + 10] == v5 && Shipset[v7 + 11] == v6 )
{
wrefresh(stdscr);
setAsHit(v8, v10);
result = v8 + 1;
if ( Shipset[24 * v8 + 9] )
result = ~v8;
return result;
}
LABEL_5:
if ( Shipset[v7 + 8] <= (int)++v9 )
goto LABEL_15;
}
if ( Shipset[v7 + 10] != v5 || (_DWORD)v9 + Shipset[v7 + 11] != v6 )
goto LABEL_5;
wrefresh(stdscr);
setAsHit(v8, v10);
result = v8 + 1;
if ( Shipset[24 * v8 + 9] )
result = ~v8;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
const char *v1; // r12
char *v2; // rax
int v3; // ebx
int v4; // er14
unsigned int v5; // ebp
unsigned int v6; // er12
__int64 v7; // rdx
unsigned int v8; // er13
__int64 v9; // rax
unsigned int v10; // ebx
__int64 result; // rax
char dest[50]; // [rsp+6h] [rbp-32h] BYREF

wrefresh(stdscr);
v1 = (const char *)(a1 + 4);
v2 = index((const char *)(a1 + 4), 44);
if ( !v2 )
{
cleanup_ncurses();
perror("Invalid BFIRE message detected in check_hit");
exit(1);
}
v3 = (_DWORD)v2 - (_DWORD)v1;
v4 = strlen(v1);
strncpy(dest, v1, v3);
v5 = atoi(dest);
strncpy(dest, &v1[v3 + 1], ~v3 + v4);
v6 = atoi(dest);
wrefresh(stdscr);
v7 = 0LL;
v8 = 0;
while ( Shipset[v7 + 9] || !Shipset[v7 + 8] )
{
LABEL_15:
++v8;
v7 += 24LL;
if ( v8 == 5 )
{
place_hit_or_mis(opponent_win, 0LL, v5, v6, 1LL);
return 0LL;
}
}
v9 = 0LL;
while ( 1 )
{
v10 = v9;
if ( Shipset[v7 + 13 + v9] == 1 )
goto LABEL_5;
if ( Shipset[v7 + 12] == 1 )
break;
if ( (_DWORD)v9 + Shipset[v7 + 10] == v5 && Shipset[v7 + 11] == v6 )
{
wrefresh(stdscr);
setAsHit(v8, v10);
result = v8 + 1;
if ( Shipset[24 * v8 + 9] )
result = ~v8;
return result;
}
LABEL_5:
if ( Shipset[v7 + 8] <= (int)++v9 )
goto LABEL_15;
}
if ( Shipset[v7 + 10] != v5 || (_DWORD)v9 + Shipset[v7 + 11] != v6 )
goto LABEL_5;
wrefresh(stdscr);
setAsHit(v8, v10);
result = v8 + 1;
if ( Shipset[24 * v8 + 9] )
result = ~v8;
return result;
}

original_code:
int check_hit(const BMesg *buf)
{
  char *scratch, dest[10];
  int x,y,lenx,leny,i,j;
  wrefresh(stdscr);
  if ( (scratch = index(buf->code, ',')) == ((void *)0)) {
    cleanup_ncurses();
    perror("Invalid BFIRE message detected in check_hit");
    exit(1);
  } else {
    lenx = scratch - buf->code;
    leny = (strlen(buf->code)-1) - lenx;
    strncpy(dest, buf->code, lenx);
    x = atoi(dest);
    strncpy(dest, buf->code+lenx+1, leny);
    y = atoi(dest);
    wrefresh(stdscr);
  }
  for (i=0; i<5; i++) {
    if (Shipset[i].sunk)
      continue;
    for (j=0; j<Shipset[i].size; j++) {
      if (Shipset[i].slots[j] == 1) {
 continue;
      }
      if (Shipset[i].direction == 1) {
 if ( (Shipset[i].x == x) && (Shipset[i].y+j == y) ) {
   wrefresh(stdscr);
   setAsHit(i, j);
   return ((Shipset[i].sunk) ? -1*(i+1) : i+1);
 }
      } else {
 if ( (Shipset[i].x+j == x) && (Shipset[i].y == y) ) {
   wrefresh(stdscr);
   setAsHit(i, j);
   return ((Shipset[i].sunk) ? -1*(i+1) : i+1);
 }
      }
    }
  }
  place_hit_or_mis(opponent_win,0,x,y,1);
  return 0;
}

canonical_original_code:
int check_hit(struct BMesg_ *buf)
{
  char *scratch; char dest[10];
  int x; int y; int lenx; int leny; int i; int j;
  wrefresh(stdscr);
  if ( (scratch = index(buf->code, ',')) == ((void *)0)) {
    cleanup_ncurses();
    perror("Invalid BFIRE message detected in check_hit");
    exit(1);
  } else {
    lenx = scratch - buf->code;
    leny = (strlen(buf->code)-1) - lenx;
    strncpy(dest, buf->code, lenx);
    x = atoi(dest);
    strncpy(dest, buf->code+lenx+1, leny);
    y = atoi(dest);
    wrefresh(stdscr);
  }
  for (i=0; i<5; i++) {
    if (Shipset[i].sunk)
      continue;
    for (j=0; j<Shipset[i].size; j++) {
      if (Shipset[i].slots[j] == 1) {
 continue;
      }
      if (Shipset[i].direction == 1) {
 if ( (Shipset[i].x == x) && (Shipset[i].y+j == y) ) {
   wrefresh(stdscr);
   setAsHit(i, j);
   return ((Shipset[i].sunk) ? -1*(i+1) : i+1);
 }
      } else {
 if ( (Shipset[i].x+j == x) && (Shipset[i].y == y) ) {
   wrefresh(stdscr);
   setAsHit(i, j);
   return ((Shipset[i].sunk) ? -1*(i+1) : i+1);
 }
      }
    }
  }
  place_hit_or_mis(opponent_win,0,x,y,1);
  return 0;
}

variable_types:
scratch
char *

variable_types:
dest
char [10]

variable_types: lenx int

variable_types: leny int

variable_types: j int

user_defined_types: enum MESSAGE { BFIRE=0, BHIT=1, BMISS=2 }


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

v0 = 0LL;
while ( Shipset[v0 + 9] )
{
v0 += 24LL;
if ( v0 == 120 )
return 1LL;
}
return 0LL;
}

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

v0 = 0LL;
while ( Shipset[v0 + 9] )
{
v0 += 24LL;
if ( v0 == 120 )
return 1LL;
}
return 0LL;
}

original_code:
int check_game_over()
{
  int i;
  for (i=0; i<5; i++) {
    if (!Shipset[i].sunk) {
      return 0;
    } else {
    }
  }
  return 1;
}


decompiled_code:
ssize_t __fastcall send_hit(int fd, char *src)
{
__int64 v2; // rax
const void *v3; // rbx
ssize_t result; // rax

v2 = CreateEmptyBMesg();
v3 = (const void *)v2;
*(_DWORD *)v2 = 1;
if ( src )
strcpy((char *)(v2 + 4), src);
result = send(fd, v3, 0x10uLL, 0);
if ( (_DWORD)result == -1 )
{
cleanup_ncurses();
perror("send error");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func4(int fd, char *src)
{
__int64 v2; // rax
const void *v3; // rbx
ssize_t result; // rax

v2 = CreateEmptyBMesg();
v3 = (const void *)v2;
*(_DWORD *)v2 = 1;
if ( src )
strcpy((char *)(v2 + 4), src);
result = send(fd, v3, 0x10uLL, 0);
if ( (_DWORD)result == -1 )
{
cleanup_ncurses();
perror("send error");
exit(1);
}
return result;
}

original_code:
void send_hit(const int sock, const char *code)
{
  BMesg *buf;
  int nbytes;
  buf = CreateEmptyBMesg();
  buf->msg=BHIT;
  if (code)
    strcpy(buf->code, code);
  if ( (nbytes = send(sock, buf, sizeof(BMesg), 0)) == -1) {
    cleanup_ncurses();
    perror("send error");
    exit(1);
  }
}

canonical_original_code:
void send_hit(int sock, char *code)
{
  struct BMesg_ *buf;
  int nbytes;
  buf = CreateEmptyBMesg();
  buf->msg=BHIT;
  if (code)
    strcpy(buf->code, code);
  if ( (nbytes = send(sock, buf, sizeof(BMesg), 0)) == -1) {
    cleanup_ncurses();
    perror("send error");
    exit(1);
  }
}

variable_types: sock int

variable_types: nbytes int


decompiled_code:
ssize_t __fastcall send_miss(int fd)
{
_DWORD *v1; // rsi
ssize_t result; // rax

v1 = (_DWORD *)CreateEmptyBMesg();
*v1 = 2;
result = send(fd, v1, 0x10uLL, 0);
if ( (_DWORD)result == -1 )
{
cleanup_ncurses();
perror("send error");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func5(int fd)
{
_DWORD *v1; // rsi
ssize_t result; // rax

v1 = (_DWORD *)CreateEmptyBMesg();
*v1 = 2;
result = send(fd, v1, 0x10uLL, 0);
if ( (_DWORD)result == -1 )
{
cleanup_ncurses();
perror("send error");
exit(1);
}
return result;
}

original_code:
void send_miss(const int sock)
{
  BMesg *buf;
  int nbytes;
  buf = CreateEmptyBMesg();
  buf->msg=BMISS;
  if ( (nbytes = send(sock, buf, sizeof(BMesg), 0)) == -1) {
    cleanup_ncurses();
    perror("send error");
    exit(1);
  }
}

canonical_original_code:
void send_miss(int sock)
{
  struct BMesg_ *buf;
  int nbytes;
  buf = CreateEmptyBMesg();
  buf->msg=BMISS;
  if ( (nbytes = send(sock, buf, sizeof(BMesg), 0)) == -1) {
    cleanup_ncurses();
    perror("send error");
    exit(1);
  }
}


decompiled_code:
__int64 __fastcall do_receive(int fd)
{
void *v1; // rbx
int v2; // ebx

v1 = (void *)CreateEmptyBMesg();
if ( recv(fd, v1, 0x64uLL, 0) == -1 )
{
cleanup_ncurses();
perror("recv error");
exit(1);
}
wrefresh(stdscr);
v2 = check_hit((__int64)v1);
if ( v2 )
{
if ( (unsigned int)check_game_over() )
{
send_hit(fd, "1");
v2 = 100;
}
else if ( v2 < 0 )
{
send_hit(fd, "0");
}
else
{
send_hit(fd, 0LL);
}
}
else
{
send_miss(fd);
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd)
{
void *v1; // rbx
int v2; // ebx

v1 = (void *)CreateEmptyBMesg();
if ( recv(fd, v1, 0x64uLL, 0) == -1 )
{
cleanup_ncurses();
perror("recv error");
exit(1);
}
wrefresh(stdscr);
v2 = func2((__int64)v1);
if ( v2 )
{
if ( (unsigned int)func3() )
{
func4(fd, "1");
v2 = 100;
}
else if ( v2 < 0 )
{
func4(fd, "0");
}
else
{
func4(fd, 0LL);
}
}
else
{
func5(fd);
}
return (unsigned int)v2;
}

original_code:
int do_receive(const int sock)
{
  int nbytes,res;
  BMesg *buf;
  buf = CreateEmptyBMesg();
  if ( (nbytes = recv(sock, buf, 100, 0) == -1) ) {
    cleanup_ncurses();
    perror("recv error");
    exit(1);
  }
  wrefresh(stdscr);
  if ( (res = check_hit(buf)) != 0 ) {
    if (check_game_over()) {
      send_hit(sock, "1");
      return 100;
    } else {
      if (res < 0) {
 send_hit(sock, "0");
 return res;
      } else if (res > 0) {
 send_hit(sock, ((void *)0));
 return res;
      }
      else {
 cleanup_ncurses();
 perror("Unexpected case in do_receive");
 exit(1);
      }
    }
  } else {
    send_miss(sock);
    return res;
  }
  cleanup_ncurses();
  perror("Reached unexpected worlds....");
  exit(1);
  return -1;
}

canonical_original_code:
int do_receive(int sock)
{
  int nbytes; int res;
  struct BMesg_ *buf;
  buf = CreateEmptyBMesg();
  if ( (nbytes = recv(sock, buf, 100, 0) == -1) ) {
    cleanup_ncurses();
    perror("recv error");
    exit(1);
  }
  wrefresh(stdscr);
  if ( (res = check_hit(buf)) != 0 ) {
    if (check_game_over()) {
      send_hit(sock, "1");
      return 100;
    } else {
      if (res < 0) {
 send_hit(sock, "0");
 return res;
      } else if (res > 0) {
 send_hit(sock, ((void *)0));
 return res;
      }
      else {
 cleanup_ncurses();
 perror("Unexpected case in do_receive");
 exit(1);
      }
    }
  } else {
    send_miss(sock);
    return res;
  }
  cleanup_ncurses();
  perror("Reached unexpected worlds....");
  exit(1);
  return -1;
}

variable_types: res int


decompiled_code:
ssize_t __fastcall get_response(int a1, void *a2)
{
ssize_t result; // rax

result = recv(a1, a2, 0x64uLL, 0);
if ( result == -1 )
{
cleanup_ncurses();
perror("recv error");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func7(int a1, void *a2)
{
ssize_t result; // rax

result = recv(a1, a2, 0x64uLL, 0);
if ( result == -1 )
{
cleanup_ncurses();
perror("recv error");
exit(1);
}
return result;
}

original_code:
void get_response(const int sock, BMesg *buf)
{
  int nbytes;
  if ( (nbytes = recv(sock, buf, 100, 0) == -1) ) {
    cleanup_ncurses();
    perror("recv error");
    exit(1);
  }
}

canonical_original_code:
void get_response(int sock, struct BMesg_ *buf)
{
  int nbytes;
  if ( (nbytes = recv(sock, buf, 100, 0) == -1) ) {
    cleanup_ncurses();
    perror("recv error");
    exit(1);
  }
}


decompiled_code:
__int64 __fastcall do_fire(int fd)
{
char *v1; // rbx
__int64 result; // rax
const char *v3; // rbx

v1 = (char *)CreateBMesg(0LL);
if ( (unsigned int)send(fd, v1, 0x10uLL, 0) == -1 )
{
cleanup_ncurses();
perror("send error");
exit(1);
}
wrefresh(stdscr);
get_response(fd, v1);
result = 0LL;
if ( *(_DWORD *)v1 == 1 )
{
v3 = v1 + 4;
if ( !strcmp(v3, "0") )
result = 0xFFFFFFFFLL;
else
result = strcmp(v3, "1") == 0 ? -2 : 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd)
{
char *v1; // rbx
__int64 result; // rax
const char *v3; // rbx

v1 = (char *)CreateBMesg(0LL);
if ( (unsigned int)send(fd, v1, 0x10uLL, 0) == -1 )
{
cleanup_ncurses();
perror("send error");
exit(1);
}
wrefresh(stdscr);
func7(fd, v1);
result = 0LL;
if ( *(_DWORD *)v1 == 1 )
{
v3 = v1 + 4;
if ( !strcmp(v3, "0") )
result = 0xFFFFFFFFLL;
else
result = strcmp(v3, "1") == 0 ? -2 : 1;
}
return result;
}

original_code:
int do_fire(const int sock, const int x, const int y)
{
  BMesg *buf;
  int nbytes;
  buf = CreateBMesg(BFIRE, x, y);
  if ( (nbytes = send(sock, buf, sizeof(BMesg), 0)) == -1) {
    cleanup_ncurses();
    perror("send error");
    exit(1);
  }
  wrefresh(stdscr);
  get_response(sock, buf);
  if (buf->msg == BHIT) {
    if ( strcmp(buf->code,"0") == 0 ) {
      return -1;
    } else if ( strcmp(buf->code,"1") == 0){
      return -2;
    } else {
      return 1;
    }
    wrefresh(stdscr);
  } else {
    return 0;
  }
  return -100;
}

canonical_original_code:
int do_fire(int sock, int x, int y)
{
  struct BMesg_ *buf;
  int nbytes;
  buf = CreateBMesg(BFIRE, x, y);
  if ( (nbytes = send(sock, buf, sizeof(BMesg), 0)) == -1) {
    cleanup_ncurses();
    perror("send error");
    exit(1);
  }
  wrefresh(stdscr);
  get_response(sock, buf);
  if (buf->msg == BHIT) {
    if ( strcmp(buf->code,"0") == 0 ) {
      return -1;
    } else if ( strcmp(buf->code,"1") == 0){
      return -2;
    } else {
      return 1;
    }
    wrefresh(stdscr);
  } else {
    return 0;
  }
  return -100;
}


decompiled_code:
ssize_t __fastcall get_peer_user_name(int a1)
{
ssize_t result; // rax

result = recv(a1, &peer_user_name, 0x32uLL, 0);
if ( (_DWORD)result == -1 )
{
cleanup_ncurses();
perror("Error getting other user's name...");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func9(int a1)
{
ssize_t result; // rax

result = recv(a1, &peer_user_name, 0x32uLL, 0);
if ( (_DWORD)result == -1 )
{
cleanup_ncurses();
perror("Error getting other user's name...");
exit(1);
}
return result;
}

original_code:
void get_peer_user_name(const int sock)
{
  int nbytes;
  if ( (nbytes = recv(sock, peer_user_name, 50, 0)) == -1) {
    cleanup_ncurses();
    perror("Error getting other user's name...");
    exit(1);
  }
}

canonical_original_code:
void get_peer_user_name(int sock)
{
  int nbytes;
  if ( (nbytes = recv(sock, peer_user_name, 50, 0)) == -1) {
    cleanup_ncurses();
    perror("Error getting other user's name...");
    exit(1);
  }
}


decompiled_code:
ssize_t __fastcall send_user_name(int fd)
{
size_t v1; // rax
ssize_t result; // rax

v1 = strlen(&global_user_name);
result = send(fd, &global_user_name, v1 + 1, 0);
if ( (_DWORD)result == -1 )
{
cleanup_ncurses();
perror("send error");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func10(int fd)
{
size_t v1; // rax
ssize_t result; // rax

v1 = strlen(&global_user_name);
result = send(fd, &global_user_name, v1 + 1, 0);
if ( (_DWORD)result == -1 )
{
cleanup_ncurses();
perror("send error");
exit(1);
}
return result;
}

original_code:
void send_user_name(const int sock)
{
  int nbytes;
  if ( (nbytes = send(sock, global_user_name, strlen(global_user_name)+1 , 0)) == -1) {
    cleanup_ncurses();
    perror("send error");
    exit(1);
  }
}

canonical_original_code:
void send_user_name(int sock)
{
  int nbytes;
  if ( (nbytes = send(sock, global_user_name, strlen(global_user_name)+1 , 0)) == -1) {
    cleanup_ncurses();
    perror("send error");
    exit(1);
  }
}


decompiled_code:
ssize_t __fastcall exchange_names(int fd)
{
ssize_t result; // rax

if ( user_mode )
{
send_user_name(fd);
result = get_peer_user_name(fd);
}
else
{
get_peer_user_name(fd);
result = send_user_name(fd);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func11(int fd)
{
ssize_t result; // rax

if ( user_mode )
{
func10(fd);
result = func9(fd);
}
else
{
func9(fd);
result = func10(fd);
}
return result;
}

original_code:
void exchange_names(const int sock)
{
  if (user_mode == 0) {
    get_peer_user_name(sock);
    send_user_name(sock);
  } else {
    send_user_name(sock);
    get_peer_user_name(sock);
  }
}

canonical_original_code:
void exchange_names(int sock)
{
  if (user_mode == 0) {
    get_peer_user_name(sock);
    send_user_name(sock);
  } else {
    send_user_name(sock);
    get_peer_user_name(sock);
  }
}


decompiled_code:
ssize_t __fastcall send_byte(int a1, char a2)
{
ssize_t result; // rax
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a2;
result = send(a1, buf, 1uLL, 0);
if ( result == -1 )
{
cleanup_ncurses();
perror("Error sending a byte...");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func12(int a1, char a2)
{
ssize_t result; // rax
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a2;
result = send(a1, buf, 1uLL, 0);
if ( result == -1 )
{
cleanup_ncurses();
perror("Error sending a byte...");
exit(1);
}
return result;
}

original_code:
void send_byte(const int sock, uint8_t byte)
{
  if (-1 == send(sock,
   &byte,
   1, 0)) {
    cleanup_ncurses();
    perror("Error sending a byte...");
    exit(1);
  }
}

canonical_original_code:
void send_byte(int sock, unsigned char byte)
{
  if (-1 == send(sock,
   &byte,
   1, 0)) {
    cleanup_ncurses();
    perror("Error sending a byte...");
    exit(1);
  }
}

variable_types:
byte
unsigned char


decompiled_code:
ssize_t __fastcall send_shipset(int a1)
{
__int64 i; // rbx
ssize_t result; // rax

for ( i = 0LL; i != 120; i += 24LL )
{
send_byte(a1, Shipset[i + 10]);
send_byte(a1, Shipset[i + 11]);
result = send_byte(a1, Shipset[i + 12]);
}
return result;
}

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

for ( i = 0LL; i != 120; i += 24LL )
{
func12(a1, Shipset[i + 10]);
func12(a1, Shipset[i + 11]);
result = func12(a1, Shipset[i + 12]);
}
return result;
}

original_code:
void send_shipset(const int sock)
{
  for (int i=0; i<5; ++i) {
    send_byte(sock, Shipset[i].x);
    send_byte(sock, Shipset[i].y);
    send_byte(sock, Shipset[i].direction);
  }
}

canonical_original_code:
void send_shipset(int sock)
{
  for (int i = 0; i<5; ++i) {
    send_byte(sock, Shipset[i].x);
    send_byte(sock, Shipset[i].y);
    send_byte(sock, Shipset[i].direction);
  }
}


decompiled_code:
__int64 __fastcall recv_byte(int a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( recv(a1, buf, 1uLL, 0) == -1 )
{
cleanup_ncurses();
perror("Error recv'ing a byte...");
exit(1);
}
return (unsigned __int8)buf[0];
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( recv(a1, buf, 1uLL, 0) == -1 )
{
cleanup_ncurses();
perror("Error recv'ing a byte...");
exit(1);
}
return (unsigned __int8)buf[0];
}

original_code:
uint8_t recv_byte(const int sock)
{
  uint8_t byte;
  if ( -1 == recv(sock,
    &byte,
    1, 0)) {
    cleanup_ncurses();
    perror("Error recv'ing a byte...");
    exit(1);
  }
  return byte;
}

canonical_original_code:
unsigned char recv_byte(int sock)
{
  unsigned char byte;
  if ( -1 == recv(sock,
    &byte,
    1, 0)) {
    cleanup_ncurses();
    perror("Error recv'ing a byte...");
    exit(1);
  }
  return byte;
}

return_type: unsigned char


decompiled_code:
__int64 __fastcall get_peer_shipset(int a1)
{
__int64 i; // rbx
__int64 result; // rax

for ( i = 0LL; i != 120; i += 24LL )
{
PeerShipset[i + 10] = recv_byte(a1);
PeerShipset[i + 11] = recv_byte(a1);
result = recv_byte(a1);
PeerShipset[i + 12] = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1)
{
__int64 i; // rbx
__int64 result; // rax

for ( i = 0LL; i != 120; i += 24LL )
{
PeerShipset[i + 10] = func14(a1);
PeerShipset[i + 11] = func14(a1);
result = func14(a1);
PeerShipset[i + 12] = result;
}
return result;
}

original_code:
void get_peer_shipset(const int sock)
{
  for (int i=0; i<5; ++i) {
    PeerShipset[i].x = recv_byte(sock);
    PeerShipset[i].y = recv_byte(sock);
    PeerShipset[i].direction = recv_byte(sock);
  }
}

canonical_original_code:
void get_peer_shipset(int sock)
{
  for (int i = 0; i<5; ++i) {
    PeerShipset[i].x = recv_byte(sock);
    PeerShipset[i].y = recv_byte(sock);
    PeerShipset[i].direction = recv_byte(sock);
  }
}


decompiled_code:
__int64 __fastcall exchange_shipsets(int a1)
{
show_message_box("Waiting to exchange ships from opponent...");
if ( user_mode )
{
send_shipset(a1);
get_peer_shipset(a1);
}
else
{
get_peer_shipset(a1);
send_shipset(a1);
}
return hide_message_box();
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1)
{
show_message_box("Waiting to exchange ships from opponent...");
if ( user_mode )
{
func13(a1);
func15(a1);
}
else
{
func15(a1);
func13(a1);
}
return hide_message_box();
}

original_code:
void exchange_shipsets(const int sock)
{
  show_message_box("Waiting to exchange ships from opponent...");
  if (user_mode == 0) {
    get_peer_shipset(sock);
    send_shipset(sock);
  } else {
    send_shipset(sock);
    get_peer_shipset(sock);
  }
  hide_message_box();
}

canonical_original_code:
void exchange_shipsets(int sock)
{
  show_message_box("Waiting to exchange ships from opponent...");
  if (user_mode == 0) {
    get_peer_shipset(sock);
    send_shipset(sock);
  } else {
    send_shipset(sock);
    get_peer_shipset(sock);
  }
  hide_message_box();
}


decompiled_code:
__int64 __fastcall verify_server(int a1)
{
char v1; // dl
__int64 result; // rax

send_byte(a1, 42);
v1 = recv_byte(a1);
result = 0LL;
if ( v1 == 47 )
{
send_byte(a1, 49);
result = 1LL;
}
return result;
}

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

func12(a1, 42);
v1 = func14(a1);
result = 0LL;
if ( v1 == 47 )
{
func12(a1, 49);
result = 1LL;
}
return result;
}

original_code:
_Bool verify_server(const int sock)
{
  send_byte(sock, 42);
  if (recv_byte(sock) != 47) return 0;
  send_byte(sock, 49);
  return 1;
}

canonical_original_code:
_Bool verify_server(int sock)
{
  send_byte(sock, 42);
  if (recv_byte(sock) != 47) return 0;
  send_byte(sock, 49);
  return 1;
}


decompiled_code:
bool __fastcall verify_client(int a1)
{
char v1; // dl
bool result; // al

v1 = recv_byte(a1);
result = 0;
if ( v1 == 42 )
{
send_byte(a1, 47);
result = (unsigned __int8)recv_byte(a1) == 49;
}
return result;
}

canonical_decompiled_code:
bool __fastcall func18(int a1)
{
char v1; // dl
bool result; // al

v1 = func14(a1);
result = 0;
if ( v1 == 42 )
{
func12(a1, 47);
result = (unsigned __int8)func14(a1) == 49;
}
return result;
}

original_code:
_Bool verify_client(const int sock)
{
  if (recv_byte(sock) != 42) return 0;
  send_byte(sock, 47);
  if (recv_byte(sock) != 49) return 0;
  return 1;
}

canonical_original_code:
_Bool verify_client(int sock)
{
  if (recv_byte(sock) != 42) return 0;
  send_byte(sock, 47);
  if (recv_byte(sock) != 49) return 0;
  return 1;
}


decompiled_code:
char *__fastcall create_grid(__int64 a1, __int64 a2)
{
__int64 v3; // r10
_BYTE *v4; // rdx
_BYTE *v5; // rax
unsigned __int8 *v6; // rdi
unsigned __int8 *v7; // r11
__int64 v8; // r8
__int64 v9; // rsi
char *result; // rax
char *v11; // r8
char v12; // si
unsigned __int8 v13; // si
unsigned __int8 v14; // bl
__int64 v15; // r8
__int64 v16; // rsi
char *v17; // r8
char v18; // si

v3 = a1 + 10;
v4 = (_BYTE *)(a1 + 10);
do
{
v5 = v4 - 10;
do
*v5++ = 95;
while ( v5 != v4 );
v4 += 10;
}
while ( v4 != (_BYTE *)(a1 + 110) );
v6 = (unsigned __int8 *)(a2 + 8);
v7 = (unsigned __int8 *)(a2 + 128);
do
{
result = (char *)v6[2];
v13 = v6[3];
v14 = *v6;
if ( v6[4] )
{
if ( *v6 )
{
v8 = v13;
v9 = (unsigned __int8)result;
result = (char *)(a1 + (unsigned __int8)result + 10 * v8);
v11 = (char *)(v3 + v9 + 10 * ((unsigned int)v14 - 1 + v8));
do
{
v12 = 42;
if ( *result != 95 )
v12 = 120;
*result = v12;
result += 10;
}
while ( result != v11 );
}
}
else if ( *v6 )
{
v15 = a1 + 10LL * v13;
v16 = (unsigned __int8)result;
result = (char *)(v15 + (unsigned __int8)result);
v17 = (char *)(v16 + (unsigned int)v14 - 1 + 1 + v15);
do
{
v18 = 42;
if ( *result != 95 )
v18 = 120;
*result++ = v18;
}
while ( result != v17 );
}
v6 += 24;
}
while ( v6 != v7 );
return result;
}

original_code:
void create_grid(char grid[10][10], Ship ships[])
{
  int x, y, dir, size, i,j;
  for (i = 0; i< 10; i++) {
    for(j = 0; j< 10; j++) {
      grid[i][j]='_';
    }
  }
  for (i=0; i<5; i++) {
    x = ships[i].x;
    y = ships[i].y;
    dir = ships[i].direction;
    size = ships[i].size;
    if(dir) {
      for (j=0;j<size; j++) {
 if (grid[y+j][x] != '_')
   grid[y+j][x]='x';
 else
   grid[y+j][x]='*';
      }
    } else {
      for (j=0;j<size; j++) {
 if (grid[y][x+j] != '_')
   grid[y][x+j]='x';
 else
   grid[y][x+j]='*';
      }
    }
  }
}

canonical_original_code:
void create_grid(char grid[10][10], struct Ship_ ships[])
{
  int x; int y; int dir; int size; int i; int j;
  for (i = 0; i< 10; i++) {
    for(j = 0; j< 10; j++) {
      grid[i][j]='_';
    }
  }
  for (i=0; i<5; i++) {
    x = ships[i].x;
    y = ships[i].y;
    dir = ships[i].direction;
    size = ships[i].size;
    if(dir) {
      for (j=0;j<size; j++) {
 if (grid[y+j][x] != '_')
   grid[y+j][x]='x';
 else
   grid[y+j][x]='*';
      }
    } else {
      for (j=0;j<size; j++) {
 if (grid[y][x+j] != '_')
   grid[y][x+j]='x';
 else
   grid[y][x+j]='*';
      }
    }
  }
}

variable_types:
grid
char [10][10]

variable_types: struct Ship_ { char *name; unsigned char size; unsigned char sunk; unsigned char x; unsigned char y; unsigned char direction; unsigned char slots[5]; } []

variable_types: dir int

variable_types: size int


randNum

decompiled_code:
__int64 __fastcall randNum(int a1, int a2)
{
unsigned int v2; // eax

v2 = time(0LL);
srand(v2);
return (unsigned int)(rand() % (a1 - a2 + 1) + a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
unsigned int v2; // eax

v2 = time(0LL);
srand(v2);
return (unsigned int)(rand() % (a1 - a2 + 1) + a2);
}

original_code:
int randNum(const int high, const int low) {
  srand((unsigned)time(0));
  return ((rand() % (high - low +1)) + low);
}

canonical_original_code:
int randNum(int high, int low) {
  srand((unsigned int)time(0));
  return ((rand() % (high - low +1)) + low);
}

variable_types: high int

variable_types: low int


setAsHit

decompiled_code:
__int64 __fastcall setAsHit(int a1, int a2)
{
__int64 v3; // rdx
__int64 v4; // rax
__int64 v5; // rcx
unsigned int v6; // edi
__int64 v7; // rcx
__int64 v8; // rax
char v9; // dl
__int64 result; // rax

v3 = a1;
v4 = 2LL * a1;
v5 = 3LL * a1;
v6 = *((unsigned __int8 *)&Shipset + 24 * a1 + 10);
v7 = *((unsigned __int8 *)&Shipset + 8 * v5 + 11);
if ( *((_BYTE *)&Shipset + 8 * v3 + 8 * v4 + 12) )
v7 = (unsigned int)(a2 + v7);
else
v6 += a2;
*((_BYTE *)&Shipset + 24 * a1 + a2 + 13) = 1;
place_hit_or_mis(opponent_win, 0xFFFFFFFFLL, v6, v7, 1LL);
if ( LOBYTE((&Shipset)[3 * a1 + 1]) )
{
v8 = 0LL;
while ( *((_BYTE *)&Shipset + 24 * a1 + v8 + 13) )
{
if ( LOBYTE((&Shipset)[3 * a1 + 1]) <= (int)++v8 )
{
v9 = 1;
goto LABEL_11;
}
}
v9 = 0;
}
else
{
v9 = 1;
}
LABEL_11:
result = 3LL * a1;
*((_BYTE *)&Shipset + 24 * a1 + 9) = v9;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
__int64 v3; // rdx
__int64 v4; // rax
__int64 v5; // rcx
unsigned int v6; // edi
__int64 v7; // rcx
__int64 v8; // rax
char v9; // dl
__int64 result; // rax

v3 = a1;
v4 = 2LL * a1;
v5 = 3LL * a1;
v6 = *((unsigned __int8 *)&Shipset + 24 * a1 + 10);
v7 = *((unsigned __int8 *)&Shipset + 8 * v5 + 11);
if ( *((_BYTE *)&Shipset + 8 * v3 + 8 * v4 + 12) )
v7 = (unsigned int)(a2 + v7);
else
v6 += a2;
*((_BYTE *)&Shipset + 24 * a1 + a2 + 13) = 1;
place_hit_or_mis(opponent_win, 0xFFFFFFFFLL, v6, v7, 1LL);
if ( LOBYTE((&Shipset)[3 * a1 + 1]) )
{
v8 = 0LL;
while ( *((_BYTE *)&Shipset + 24 * a1 + v8 + 13) )
{
if ( LOBYTE((&Shipset)[3 * a1 + 1]) <= (int)++v8 )
{
v9 = 1;
goto LABEL_11;
}
}
v9 = 0;
}
else
{
v9 = 1;
}
LABEL_11:
result = 3LL * a1;
*((_BYTE *)&Shipset + 24 * a1 + 9) = v9;
return result;
}

original_code:
void setAsHit(int ship, int slot)
{
  int i;
  int sunk = 1;
  int x, y,dir;
  x = Shipset[ship].x;
  y = Shipset[ship].y;
  dir = Shipset[ship].direction;
  if (dir) y += slot;
  else x += slot;
  Shipset[ship].slots[slot] = 1;
  place_hit_or_mis(opponent_win, -1, x, y, 1);
  for (i=0; i<Shipset[ship].size; i++) {
    if (Shipset[ship].slots[i] == 0) {
      sunk = 0;
      break;
    }
  }
  Shipset[ship].sunk = sunk;
}

canonical_original_code:
void setAsHit(int ship, int slot)
{
  int i;
  int sunk = 1;
  int x; int y; int dir;
  x = Shipset[ship].x;
  y = Shipset[ship].y;
  dir = Shipset[ship].direction;
  if (dir) y += slot;
  else x += slot;
  Shipset[ship].slots[slot] = 1;
  place_hit_or_mis(opponent_win, -1, x, y, 1);
  for (i=0; i<Shipset[ship].size; i++) {
    if (Shipset[ship].slots[i] == 0) {
      sunk = 0;
      break;
    }
  }
  Shipset[ship].sunk = sunk;
}

variable_types: ship int

variable_types: slot int

variable_types: i int

variable_types: sunk int

variable_types: x int


original_code:
_Bool is_there_a_ship_here(Ship ships[], int x, int y)
{
  for (int i=0; i<5; i++) {
    for (int j=0; j<ships[i].size; j++) {
      if (ships[i].direction == 1) {
 if ( (ships[i].x == x) && (ships[i].y+j == y) ) {
   return 1;
 }
      } else {
 if ( (ships[i].x+j == x) && (ships[i].y == y) ) {
   return 1;
 }
      }
    }
  }
  return 0;
}

canonical_original_code:
_Bool is_there_a_ship_here(struct Ship_ ships[], int x, int y)
{
  for (int i = 0; i<5; i++) {
    for (int j = 0; j<ships[i].size; j++) {
      if (ships[i].direction == 1) {
 if ( (ships[i].x == x) && (ships[i].y+j == y) ) {
   return 1;
 }
      } else {
 if ( (ships[i].x+j == x) && (ships[i].y == y) ) {
   return 1;
 }
      }
    }
  }
  return 0;
}

variable_types: y int

user_defined_types: struct Ship_ { char *name; unsigned char size; unsigned char sunk; unsigned char x; unsigned char y; unsigned char direction; unsigned char slots[5]; }