decompiled_code:
__int64 __fastcall isLegalUTF8(unsigned __int8 *a1, int a2, __int64 a3, __int64 a4, unsigned int a5)
{
unsigned __int8 *v5; // rax
char v6; // dl
int v7; // eax

v5 = &a1[a2];
if ( a2 == 3 )
goto LABEL_16;
if ( a2 > 3 )
{
if ( a2 != 4 )
return 0LL;
a5 = 0;
if ( (unsigned __int8)(*(v5 - 1) + 0x80) > 0x3Fu )
return a5;
--v5;
LABEL_16:
a5 = 0;
if ( (unsigned __int8)(*(v5 - 1) + 0x80) > 0x3Fu )
return a5;
--v5;
goto LABEL_5;
}
if ( a2 == 1 )
{
v7 = *a1;
goto LABEL_11;
}
if ( a2 == 2 )
{
LABEL_5:
v6 = *(v5 - 1);
a5 = 0;
if ( (unsigned __int8)v6 > 0xBFu )
return a5;
v7 = *a1;
if ( (_BYTE)v7 == 0xF0 )
{
if ( (unsigned __int8)v6 <= 0x8Fu )
return a5;
goto LABEL_11;
}
if ( (unsigned __int8)v7 > 0xF0u )
{
if ( (_BYTE)v7 == 0xF4 )
{
if ( (unsigned __int8)v6 > 0x8Fu )
return a5;
goto LABEL_11;
}
}
else
{
if ( (_BYTE)v7 == 0xE0 )
{
if ( (unsigned __int8)v6 <= 0x9Fu )
return a5;
goto LABEL_11;
}
if ( (_BYTE)v7 == 0xED )
{
if ( (unsigned __int8)v6 <= 0x9Fu )
{
LABEL_11:
LOBYTE(a5) = (unsigned __int8)(v7 + 0x80) <= 0x41u;
LOBYTE(v7) = (unsigned __int8)v7 > 0xF4u;
a5 = (v7 | a5) ^ 1;
}
return a5;
}
}
a5 = 0;
if ( v6 >= 0 )
return a5;
goto LABEL_11;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1, int a2, __int64 a3, __int64 a4, unsigned int a5)
{
unsigned __int8 *v5; // rax
char v6; // dl
int v7; // eax

v5 = &a1[a2];
if ( a2 == 3 )
goto LABEL_16;
if ( a2 > 3 )
{
if ( a2 != 4 )
return 0LL;
a5 = 0;
if ( (unsigned __int8)(*(v5 - 1) + 0x80) > 0x3Fu )
return a5;
--v5;
LABEL_16:
a5 = 0;
if ( (unsigned __int8)(*(v5 - 1) + 0x80) > 0x3Fu )
return a5;
--v5;
goto LABEL_5;
}
if ( a2 == 1 )
{
v7 = *a1;
goto LABEL_11;
}
if ( a2 == 2 )
{
LABEL_5:
v6 = *(v5 - 1);
a5 = 0;
if ( (unsigned __int8)v6 > 0xBFu )
return a5;
v7 = *a1;
if ( (_BYTE)v7 == 0xF0 )
{
if ( (unsigned __int8)v6 <= 0x8Fu )
return a5;
goto LABEL_11;
}
if ( (unsigned __int8)v7 > 0xF0u )
{
if ( (_BYTE)v7 == 0xF4 )
{
if ( (unsigned __int8)v6 > 0x8Fu )
return a5;
goto LABEL_11;
}
}
else
{
if ( (_BYTE)v7 == 0xE0 )
{
if ( (unsigned __int8)v6 <= 0x9Fu )
return a5;
goto LABEL_11;
}
if ( (_BYTE)v7 == 0xED )
{
if ( (unsigned __int8)v6 <= 0x9Fu )
{
LABEL_11:
LOBYTE(a5) = (unsigned __int8)(v7 + 0x80) <= 0x41u;
LOBYTE(v7) = (unsigned __int8)v7 > 0xF4u;
a5 = (v7 | a5) ^ 1;
}
return a5;
}
}
a5 = 0;
if ( v6 >= 0 )
return a5;
goto LABEL_11;
}
return 0LL;
}

original_code:
inline static unsigned char isLegalUTF8(const UTF8 *source, int length)
{
    UTF8 a;
    const UTF8 *srcptr = source+length;
    switch (length) {
        default: return 0;
        case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
        case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
        case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
                    switch (*source) {
                        case 0xE0: if (a < 0xA0) return 0; break;
                        case 0xED: if (a > 0x9F) return 0; break;
                        case 0xF0: if (a < 0x90) return 0; break;
                        case 0xF4: if (a > 0x8F) return 0; break;
                        default: if (a < 0x80) return 0;
                    }
        case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
    }
    if (*source > 0xF4) return 0;
    return 1;
}

canonical_original_code:
inline static unsigned char isLegalUTF8(unsigned char *source, int length)
{
    unsigned char a;
    unsigned char *srcptr = source+length;
    switch (length) {
        default: return 0;
        case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
        case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
        case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
                    switch (*source) {
                        case 0xE0: if (a < 0xA0) return 0; break;
                        case 0xED: if (a > 0x9F) return 0; break;
                        case 0xF0: if (a < 0x90) return 0; break;
                        case 0xF4: if (a > 0x8F) return 0; break;
                        default: if (a < 0x80) return 0;
                    }
        case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
    }
    if (*source > 0xF4) return 0;
    return 1;
}

variable_types:
source
unsigned char *

variable_types: length int

variable_types:
a
unsigned char

variable_types:
srcptr
unsigned char *

return_type: unsigned char


decompiled_code:
void __fastcall JSON_convert_UTF8_to_JSON(__int64 a1, _QWORD *a2, int a3, __int64 a4, unsigned int a5)
{
char *v5; // r12
unsigned __int8 *v6; // r14
__int64 v7; // rbx
unsigned __int8 *v8; // rbx
unsigned __int16 v9; // ax
unsigned __int64 v10; // rdx
unsigned __int8 v11; // r9
int v12; // er10
char v13; // r11
unsigned __int64 v14; // r9
__int64 v15; // rcx
unsigned __int64 v16; // rcx
unsigned __int8 *v17; // rdx
unsigned __int64 v18; // rcx
unsigned __int64 v19; // rax
unsigned __int8 *v20; // rdx
__int64 v21; // rax
unsigned __int64 v22; // rcx
__int64 v23; // rax
__int64 v24; // rax
__int16 v26; // [rsp+8h] [rbp-50h]
char s; // [rsp+19h] [rbp-3Fh] BYREF

if ( (*a2 & 0x2000LL) != 0 )
{
v6 = (unsigned __int8 *)a2[3];
v7 = a2[2];
}
else
{
v6 = (unsigned __int8 *)(a2 + 2);
v7 = (*a2 >> 14) & 0x1FLL;
}
v8 = &v6[v7];
if ( v8 > v6 )
{
v9 = trailingBytesForUTF8[*v6];
v10 = (unsigned __int64)&v6[v9];
if ( (unsigned __int64)v8 <= v10 )
goto LABEL_27;
v5 = &s;
LABEL_6:
if ( !(unsigned __int8)isLegalUTF8(v6, (unsigned int)v9 + 1, v10, a4, a5) )
{
LABEL_48:
v24 = rb_path2class("JSON::GeneratorError");
rb_raise(v24, "source sequence is illegal/malformed");
JUMPOUT(0x508LL);
}
switch ( v13 )
{
case 0:
v20 = v6;
v21 = 0LL;
goto LABEL_13;
case 1:
v19 = 0LL;
goto LABEL_12;
case 2:
v17 = v6;
v18 = 0LL;
goto LABEL_11;
case 3:
goto LABEL_28;
case 4:
v14 = 0LL;
goto LABEL_9;
case 5:
++v6;
v14 = (unsigned __int64)v11 << 6;
LABEL_9:
v15 = *v6++;
v16 = (v14 + v15) << 6;
break;
default:
JUMPOUT(0x595LL);
}
while ( 1 )
{
v17 = v6 + 1;
v18 = (v16 + *v6) << 6;
LABEL_11:
v6 = v17 + 1;
v19 = (v18 + *v17) << 6;
LABEL_12:
v20 = v6 + 1;
v21 = (v19 + *v6) << 6;
LABEL_13:
v6 = v20 + 1;
v22 = v21 + *v20 - offsetsFromUTF8[v12];
if ( v22 > 0xFFFF )
break;
if ( v22 - 55296 <= 0x7FF )
goto LABEL_34;
if ( v22 != 34 )
{
if ( v22 == 92 )
{
rb_str_buf_cat(a1, "\\\\", 2LL);
}
else if ( v22 == 47 )
{
rb_str_buf_cat(a1, "\\/", 2LL);
}
else if ( v22 - 32 <= 0x5F )
{
rb_str_buf_cat(a1, v20, 1LL);
}
else
{
switch ( v22 )
{
case 0xAuLL:
rb_str_buf_cat(a1, "\\n", 2LL);
break;
case 0xDuLL:
rb_str_buf_cat(a1, "\\r", 2LL);
break;
case 9uLL:
rb_str_buf_cat(a1, "\\t", 2LL);
break;
case 0xCuLL:
rb_str_buf_cat(a1, "\\f", 2LL);
break;
case 8uLL:
rb_str_buf_cat(a1, "\\b", 2LL);
break;
default:
goto LABEL_24;
}
}
goto LABEL_25;
}
rb_str_buf_cat(a1, "\\\"", 2LL);
if ( v6 >= v8 )
return;
LABEL_26:
v9 = trailingBytesForUTF8[*v6];
v10 = (unsigned __int64)&v6[v9];
if ( (unsigned __int64)v8 > v10 )
goto LABEL_6;
LABEL_27:
v23 = rb_path2class("JSON::GeneratorError");
rb_raise(v23, "partial character in source, but hit end");
LABEL_28:
v16 = 0LL;
}
if ( v22 <= 0x10FFFF )
{
v26 = v21 + *v20 - LOWORD(offsetsFromUTF8[v12]);
snprintf(v5, 7uLL, "\\u%04x", (unsigned __int16)(((v22 - 0x10000) >> 10) - 10240));
rb_str_buf_cat(a1, v5, 6LL);
v22 = (unsigned __int16)((v26 & 0x3FF) - 9216);
}
else
{
LABEL_34:
if ( !a3 )
goto LABEL_48;
v22 = 65533LL;
}
LABEL_24:
snprintf(v5, 7uLL, "\\u%04x", v22);
rb_str_buf_cat(a1, v5, 6LL);
LABEL_25:
if ( v6 >= v8 )
return;
goto LABEL_26;
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, _QWORD *a2, int a3, __int64 a4, unsigned int a5)
{
char *v5; // r12
unsigned __int8 *v6; // r14
__int64 v7; // rbx
unsigned __int8 *v8; // rbx
unsigned __int16 v9; // ax
unsigned __int64 v10; // rdx
unsigned __int8 v11; // r9
int v12; // er10
char v13; // r11
unsigned __int64 v14; // r9
__int64 v15; // rcx
unsigned __int64 v16; // rcx
unsigned __int8 *v17; // rdx
unsigned __int64 v18; // rcx
unsigned __int64 v19; // rax
unsigned __int8 *v20; // rdx
__int64 v21; // rax
unsigned __int64 v22; // rcx
__int64 v23; // rax
__int64 v24; // rax
__int16 v26; // [rsp+8h] [rbp-50h]
char s; // [rsp+19h] [rbp-3Fh] BYREF

if ( (*a2 & 0x2000LL) != 0 )
{
v6 = (unsigned __int8 *)a2[3];
v7 = a2[2];
}
else
{
v6 = (unsigned __int8 *)(a2 + 2);
v7 = (*a2 >> 14) & 0x1FLL;
}
v8 = &v6[v7];
if ( v8 > v6 )
{
v9 = trailingBytesForUTF8[*v6];
v10 = (unsigned __int64)&v6[v9];
if ( (unsigned __int64)v8 <= v10 )
goto LABEL_27;
v5 = &s;
LABEL_6:
if ( !(unsigned __int8)func0(v6, (unsigned int)v9 + 1, v10, a4, a5) )
{
LABEL_48:
v24 = rb_path2class("JSON::GeneratorError");
rb_raise(v24, "source sequence is illegal/malformed");
JUMPOUT(0x508LL);
}
switch ( v13 )
{
case 0:
v20 = v6;
v21 = 0LL;
goto LABEL_13;
case 1:
v19 = 0LL;
goto LABEL_12;
case 2:
v17 = v6;
v18 = 0LL;
goto LABEL_11;
case 3:
goto LABEL_28;
case 4:
v14 = 0LL;
goto LABEL_9;
case 5:
++v6;
v14 = (unsigned __int64)v11 << 6;
LABEL_9:
v15 = *v6++;
v16 = (v14 + v15) << 6;
break;
default:
JUMPOUT(0x595LL);
}
while ( 1 )
{
v17 = v6 + 1;
v18 = (v16 + *v6) << 6;
LABEL_11:
v6 = v17 + 1;
v19 = (v18 + *v17) << 6;
LABEL_12:
v20 = v6 + 1;
v21 = (v19 + *v6) << 6;
LABEL_13:
v6 = v20 + 1;
v22 = v21 + *v20 - offsetsFromUTF8[v12];
if ( v22 > 0xFFFF )
break;
if ( v22 - 55296 <= 0x7FF )
goto LABEL_34;
if ( v22 != 34 )
{
if ( v22 == 92 )
{
rb_str_buf_cat(a1, "\\\\", 2LL);
}
else if ( v22 == 47 )
{
rb_str_buf_cat(a1, "\\/", 2LL);
}
else if ( v22 - 32 <= 0x5F )
{
rb_str_buf_cat(a1, v20, 1LL);
}
else
{
switch ( v22 )
{
case 0xAuLL:
rb_str_buf_cat(a1, "\\n", 2LL);
break;
case 0xDuLL:
rb_str_buf_cat(a1, "\\r", 2LL);
break;
case 9uLL:
rb_str_buf_cat(a1, "\\t", 2LL);
break;
case 0xCuLL:
rb_str_buf_cat(a1, "\\f", 2LL);
break;
case 8uLL:
rb_str_buf_cat(a1, "\\b", 2LL);
break;
default:
goto LABEL_24;
}
}
goto LABEL_25;
}
rb_str_buf_cat(a1, "\\\"", 2LL);
if ( v6 >= v8 )
return;
LABEL_26:
v9 = trailingBytesForUTF8[*v6];
v10 = (unsigned __int64)&v6[v9];
if ( (unsigned __int64)v8 > v10 )
goto LABEL_6;
LABEL_27:
v23 = rb_path2class("JSON::GeneratorError");
rb_raise(v23, "partial character in source, but hit end");
LABEL_28:
v16 = 0LL;
}
if ( v22 <= 0x10FFFF )
{
v26 = v21 + *v20 - LOWORD(offsetsFromUTF8[v12]);
snprintf(v5, 7uLL, "\\u%04x", (unsigned __int16)(((v22 - 0x10000) >> 10) - 10240));
rb_str_buf_cat(a1, v5, 6LL);
v22 = (unsigned __int16)((v26 & 0x3FF) - 9216);
}
else
{
LABEL_34:
if ( !a3 )
goto LABEL_48;
v22 = 65533LL;
}
LABEL_24:
snprintf(v5, 7uLL, "\\u%04x", v22);
rb_str_buf_cat(a1, v5, 6LL);
LABEL_25:
if ( v6 >= v8 )
return;
goto LABEL_26;
}
}

original_code:
void JSON_convert_UTF8_to_JSON(VALUE buffer, VALUE string, ConversionFlags flags)
{
    char buf[7];
    const UTF8* source = (UTF8 *) (!(((struct RBasic*)(string))->flags & (((VALUE)1)<<(12 +1))) ? ((struct RString*)(string))->as.ary : ((struct RString*)(string))->as.heap.ptr);
    const UTF8* sourceEnd = source + (!(((struct RBasic*)(string))->flags & (((VALUE)1)<<(12 +1))) ? (long)((((struct RBasic*)(string))->flags >> (12 +2)) & (((((VALUE)1)<<(12 +2))|(((VALUE)1)<<(12 +3))|(((VALUE)1)<<(12 +4))|(((VALUE)1)<<(12 +5))|(((VALUE)1)<<(12 +6))) >> (12 +2))) : ((struct RString*)(string))->as.heap.len);
    while (source < sourceEnd) {
        UTF32 ch = 0;
        unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
        if (source + extraBytesToRead >= sourceEnd) {
            rb_raise(rb_path2class("JSON::GeneratorError"),
                    "partial character in source, but hit end");
        }
        if (!isLegalUTF8(source, extraBytesToRead+1)) {
            rb_raise(rb_path2class("JSON::GeneratorError"),
                    "source sequence is illegal/malformed");
        }
        switch (extraBytesToRead) {
            case 5: ch += *source++; ch <<= 6;
            case 4: ch += *source++; ch <<= 6;
            case 3: ch += *source++; ch <<= 6;
            case 2: ch += *source++; ch <<= 6;
            case 1: ch += *source++; ch <<= 6;
            case 0: ch += *source++;
        }
        ch -= offsetsFromUTF8[extraBytesToRead];
        if (ch <= (UTF32)0x0000FFFF) {
            if (ch >= (UTF32)0xD800 && ch <= (UTF32)0xDFFF) {
                if (flags == strictConversion) {
                    source -= (extraBytesToRead+1);
                    rb_raise(rb_path2class("JSON::GeneratorError"),
                        "source sequence is illegal/malformed");
                } else {
                    snprintf(buf, 7, "\\u%04x", (unsigned int) ((UTF32)0x0000FFFD)); rb_str_buf_cat(buffer, buf, 6);;
                }
            } else {
                if (ch == '"') {
                    __extension__ ( { (__builtin_constant_p("\\\"")) ? rb_str_buf_cat(buffer, "\\\"", strlen("\\\"")) : rb_str_buf_cat2(buffer, "\\\""); });
                } else if (ch == '\\') {
                    __extension__ ( { (__builtin_constant_p("\\\\")) ? rb_str_buf_cat(buffer, "\\\\", strlen("\\\\")) : rb_str_buf_cat2(buffer, "\\\\"); });
                } else if (ch == '/') {
                    __extension__ ( { (__builtin_constant_p("\\/")) ? rb_str_buf_cat(buffer, "\\/", strlen("\\/")) : rb_str_buf_cat2(buffer, "\\/"); });
                } else if (ch >= 0x20 && ch <= 0x7f) {
                    rb_str_buf_cat(buffer, (char *) source - 1, 1);
                } else if (ch == '\n') {
                    __extension__ ( { (__builtin_constant_p("\\n")) ? rb_str_buf_cat(buffer, "\\n", strlen("\\n")) : rb_str_buf_cat2(buffer, "\\n"); });
                } else if (ch == '\r') {
                    __extension__ ( { (__builtin_constant_p("\\r")) ? rb_str_buf_cat(buffer, "\\r", strlen("\\r")) : rb_str_buf_cat2(buffer, "\\r"); });
                } else if (ch == '\t') {
                    __extension__ ( { (__builtin_constant_p("\\t")) ? rb_str_buf_cat(buffer, "\\t", strlen("\\t")) : rb_str_buf_cat2(buffer, "\\t"); });
                } else if (ch == '\f') {
                    __extension__ ( { (__builtin_constant_p("\\f")) ? rb_str_buf_cat(buffer, "\\f", strlen("\\f")) : rb_str_buf_cat2(buffer, "\\f"); });
                } else if (ch == '\b') {
                    __extension__ ( { (__builtin_constant_p("\\b")) ? rb_str_buf_cat(buffer, "\\b", strlen("\\b")) : rb_str_buf_cat2(buffer, "\\b"); });
                } else if (ch < 0x20) {
                    snprintf(buf, 7, "\\u%04x", (unsigned int) ((UTF16) ch)); rb_str_buf_cat(buffer, buf, 6);;
                } else {
                    snprintf(buf, 7, "\\u%04x", (unsigned int) ((UTF16) ch)); rb_str_buf_cat(buffer, buf, 6);;
                }
            }
        } else if (ch > (UTF32)0x0010FFFF) {
            if (flags == strictConversion) {
                source -= (extraBytesToRead+1);
                rb_raise(rb_path2class("JSON::GeneratorError"),
                        "source sequence is illegal/malformed");
            } else {
                snprintf(buf, 7, "\\u%04x", (unsigned int) ((UTF32)0x0000FFFD)); rb_str_buf_cat(buffer, buf, 6);;
            }
        } else {
            ch -= halfBase;
            snprintf(buf, 7, "\\u%04x", (unsigned int) ((UTF16)((ch >> halfShift) + (UTF32)0xD800))); rb_str_buf_cat(buffer, buf, 6);;
            snprintf(buf, 7, "\\u%04x", (unsigned int) ((UTF16)((ch & halfMask) + (UTF32)0xDC00))); rb_str_buf_cat(buffer, buf, 6);;
        }
    }
}

canonical_original_code:
void JSON_convert_UTF8_to_JSON(unsigned long buffer, unsigned long string, enum ConversionFlags flags)
{
    char buf[7];
    unsigned char *source = (UTF8 *) (!(((struct RBasic*)(string))->flags & (((VALUE)1)<<(12 +1))) ? ((struct RString*)(string))->as.ary : ((struct RString*)(string))->as.heap.ptr);
    unsigned char *sourceEnd = source + (!(((struct RBasic*)(string))->flags & (((VALUE)1)<<(12 +1))) ? (long)((((struct RBasic*)(string))->flags >> (12 +2)) & (((((VALUE)1)<<(12 +2))|(((VALUE)1)<<(12 +3))|(((VALUE)1)<<(12 +4))|(((VALUE)1)<<(12 +5))|(((VALUE)1)<<(12 +6))) >> (12 +2))) : ((struct RString*)(string))->as.heap.len);
    while (source < sourceEnd) {
        unsigned long ch = 0;
        unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
        if (source + extraBytesToRead >= sourceEnd) {
            rb_raise(rb_path2class("JSON::GeneratorError"),
                    "partial character in source, but hit end");
        }
        if (!isLegalUTF8(source, extraBytesToRead+1)) {
            rb_raise(rb_path2class("JSON::GeneratorError"),
                    "source sequence is illegal/malformed");
        }
        switch (extraBytesToRead) {
            case 5: ch += *source++; ch <<= 6;
            case 4: ch += *source++; ch <<= 6;
            case 3: ch += *source++; ch <<= 6;
            case 2: ch += *source++; ch <<= 6;
            case 1: ch += *source++; ch <<= 6;
            case 0: ch += *source++;
        }
        ch -= offsetsFromUTF8[extraBytesToRead];
        if (ch <= (unsigned long)0x0000FFFF) {
            if (ch >= (unsigned long)0xD800 && ch <= (unsigned long)0xDFFF) {
                if (flags == strictConversion) {
                    source -= (extraBytesToRead+1);
                    rb_raise(rb_path2class("JSON::GeneratorError"),
                        "source sequence is illegal/malformed");
                } else {
                    snprintf(buf, 7, "\\u%04x", (unsigned int) ((unsigned long)0x0000FFFD)); rb_str_buf_cat(buffer, buf, 6);;
                }
            } else {
                if (ch == '"') {
                    __extension__ ( { (__builtin_constant_p("\\\"")) ? rb_str_buf_cat(buffer, "\\\"", strlen("\\\"")) : rb_str_buf_cat2(buffer, "\\\""); });
                } else if (ch == '\\') {
                    __extension__ ( { (__builtin_constant_p("\\\\")) ? rb_str_buf_cat(buffer, "\\\\", strlen("\\\\")) : rb_str_buf_cat2(buffer, "\\\\"); });
                } else if (ch == '/') {
                    __extension__ ( { (__builtin_constant_p("\\/")) ? rb_str_buf_cat(buffer, "\\/", strlen("\\/")) : rb_str_buf_cat2(buffer, "\\/"); });
                } else if (ch >= 0x20 && ch <= 0x7f) {
                    rb_str_buf_cat(buffer, (char *) source - 1, 1);
                } else if (ch == '\n') {
                    __extension__ ( { (__builtin_constant_p("\\n")) ? rb_str_buf_cat(buffer, "\\n", strlen("\\n")) : rb_str_buf_cat2(buffer, "\\n"); });
                } else if (ch == '\r') {
                    __extension__ ( { (__builtin_constant_p("\\r")) ? rb_str_buf_cat(buffer, "\\r", strlen("\\r")) : rb_str_buf_cat2(buffer, "\\r"); });
                } else if (ch == '\t') {
                    __extension__ ( { (__builtin_constant_p("\\t")) ? rb_str_buf_cat(buffer, "\\t", strlen("\\t")) : rb_str_buf_cat2(buffer, "\\t"); });
                } else if (ch == '\f') {
                    __extension__ ( { (__builtin_constant_p("\\f")) ? rb_str_buf_cat(buffer, "\\f", strlen("\\f")) : rb_str_buf_cat2(buffer, "\\f"); });
                } else if (ch == '\b') {
                    __extension__ ( { (__builtin_constant_p("\\b")) ? rb_str_buf_cat(buffer, "\\b", strlen("\\b")) : rb_str_buf_cat2(buffer, "\\b"); });
                } else if (ch < 0x20) {
                    snprintf(buf, 7, "\\u%04x", (unsigned int) ((unsigned short) ch)); rb_str_buf_cat(buffer, buf, 6);;
                } else {
                    snprintf(buf, 7, "\\u%04x", (unsigned int) ((unsigned short) ch)); rb_str_buf_cat(buffer, buf, 6);;
                }
            }
        } else if (ch > (unsigned long)0x0010FFFF) {
            if (flags == strictConversion) {
                source -= (extraBytesToRead+1);
                rb_raise(rb_path2class("JSON::GeneratorError"),
                        "source sequence is illegal/malformed");
            } else {
                snprintf(buf, 7, "\\u%04x", (unsigned int) ((unsigned long)0x0000FFFD)); rb_str_buf_cat(buffer, buf, 6);;
            }
        } else {
            ch -= halfBase;
            snprintf(buf, 7, "\\u%04x", (unsigned int) ((UTF16)((ch >> halfShift) + (unsigned long)0xD800))); rb_str_buf_cat(buffer, buf, 6);;
            snprintf(buf, 7, "\\u%04x", (unsigned int) ((UTF16)((ch & halfMask) + (unsigned long)0xDC00))); rb_str_buf_cat(buffer, buf, 6);;
        }
    }
}

variable_types:
buffer
unsigned long

variable_types:
string
unsigned long

variable_types: enum ConversionFlags { strictConversion=0, lenientConversion=1 }

variable_types:
buf
char [7]

variable_types:
sourceEnd
unsigned char *

variable_types:
ch
unsigned long

variable_types:
extraBytesToRead
unsigned short

return_type: void


decompiled_code:
__int64 Init_sha2()
{
__int64 v0; // rbp
__int64 v1; // r12
__int64 v2; // r13
__int64 v3; // r14
__int64 v4; // rax
__int64 v5; // r14
__int64 v6; // rax
__int64 v7; // r12
__int64 v8; // rax

rb_require("digest");
v0 = rb_intern_id_cache_0;
if ( !rb_intern_id_cache_0 )
{
rb_intern_id_cache_0 = rb_intern2("metadata", 8LL);
v0 = rb_intern_id_cache_0;
}
v1 = rb_path2class("Digest");
v2 = rb_path2class("Digest::Base");
v3 = rb_define_class_under(v1, "SHA256", v2);
v4 = rb_data_object_alloc(rb_cObject, &sha256, 0LL, 0LL);
rb_ivar_set(v3, v0, v4);
v5 = rb_define_class_under(v1, "SHA384", v2);
v6 = rb_data_object_alloc(rb_cObject, &sha384, 0LL, 0LL);
rb_ivar_set(v5, v0, v6);
v7 = rb_define_class_under(v1, "SHA512", v2);
v8 = rb_data_object_alloc(rb_cObject, &sha512, 0LL, 0LL);
return rb_ivar_set(v7, v0, v8);
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rbp
__int64 v1; // r12
__int64 v2; // r13
__int64 v3; // r14
__int64 v4; // rax
__int64 v5; // r14
__int64 v6; // rax
__int64 v7; // r12
__int64 v8; // rax

rb_require("digest");
v0 = rb_intern_id_cache_0;
if ( !rb_intern_id_cache_0 )
{
rb_intern_id_cache_0 = rb_intern2("metadata", 8LL);
v0 = rb_intern_id_cache_0;
}
v1 = rb_path2class("Digest");
v2 = rb_path2class("Digest::Base");
v3 = rb_define_class_under(v1, "SHA256", v2);
v4 = rb_data_object_alloc(rb_cObject, &sha256, 0LL, 0LL);
rb_ivar_set(v3, v0, v4);
v5 = rb_define_class_under(v1, "SHA384", v2);
v6 = rb_data_object_alloc(rb_cObject, &sha384, 0LL, 0LL);
rb_ivar_set(v5, v0, v6);
v7 = rb_define_class_under(v1, "SHA512", v2);
v8 = rb_data_object_alloc(rb_cObject, &sha512, 0LL, 0LL);
return rb_ivar_set(v7, v0, v8);
}

original_code:
void
Init_sha2()
{
    VALUE mDigest, cDigest_Base;
    ID id_metadata;
    VALUE cDigest_SHA256; VALUE cDigest_SHA384; VALUE cDigest_SHA512;
    rb_require("digest");
    id_metadata = (__builtin_constant_p("metadata") ? __extension__ ({ static ID rb_intern_id_cache; if (!rb_intern_id_cache) rb_intern_id_cache = rb_intern2("metadata", strlen("metadata")); rb_intern_id_cache; }) : rb_intern("metadata"));
    mDigest = rb_path2class("Digest");
    cDigest_Base = rb_path2class("Digest::Base");
    cDigest_SHA256 = rb_define_class_under(mDigest, "SHA" "256", cDigest_Base); rb_ivar_set(cDigest_SHA256, id_metadata, rb_data_object_alloc(rb_cObject,(void *)&sha256,(RUBY_DATA_FUNC)0,(RUBY_DATA_FUNC)0)); cDigest_SHA384 = rb_define_class_under(mDigest, "SHA" "384", cDigest_Base); rb_ivar_set(cDigest_SHA384, id_metadata, rb_data_object_alloc(rb_cObject,(void *)&sha384,(RUBY_DATA_FUNC)0,(RUBY_DATA_FUNC)0)); cDigest_SHA512 = rb_define_class_under(mDigest, "SHA" "512", cDigest_Base); rb_ivar_set(cDigest_SHA512, id_metadata, rb_data_object_alloc(rb_cObject,(void *)&sha512,(RUBY_DATA_FUNC)0,(RUBY_DATA_FUNC)0));
}

canonical_original_code:
void Init_sha2()
{
    unsigned long mDigest; unsigned long cDigest_Base;
    unsigned long id_metadata;
    unsigned long cDigest_SHA256; unsigned long cDigest_SHA384; unsigned long cDigest_SHA512;
    rb_require("digest");
    id_metadata = (__builtin_constant_p("metadata") ? __extension__ ({ static unsigned long rb_intern_id_cache; if (!rb_intern_id_cache) rb_intern_id_cache = rb_intern2("metadata", strlen("metadata")); rb_intern_id_cache; }) : rb_intern("metadata"));
    mDigest = rb_path2class("Digest");
    cDigest_Base = rb_path2class("Digest::Base");
    cDigest_SHA256 = rb_define_class_under(mDigest, "SHA" "256", cDigest_Base); rb_ivar_set(cDigest_SHA256, id_metadata, rb_data_object_alloc(rb_cObject,(void *)&sha256,(void (*)(void *))0,(void (*)(void *))0)); cDigest_SHA384 = rb_define_class_under(mDigest, "SHA" "384", cDigest_Base); rb_ivar_set(cDigest_SHA384, id_metadata, rb_data_object_alloc(rb_cObject,(void *)&sha384,(void (*)(void *))0,(void (*)(void *))0)); cDigest_SHA512 = rb_define_class_under(mDigest, "SHA" "512", cDigest_Base); rb_ivar_set(cDigest_SHA512, id_metadata, rb_data_object_alloc(rb_cObject,(void *)&sha512,(void (*)(void *))0,(void (*)(void *))0));
}

variable_types:
mDigest
unsigned long


decompiled_code:
__int64 Init_rational()
{
return nurat_canonicalization(1LL);
}

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

original_code:
void
Init_rational(void)
{
    nurat_canonicalization(1);
}

canonical_original_code:
void Init_rational()
{
    nurat_canonicalization(1);
}


decompiled_code:
__int64 Init_bubblebabble()
{
__int64 v0; // r13
__int64 v1; // rbp
__int64 v2; // r12
__int64 result; // rax

rb_require("digest");
v0 = rb_path2class("Digest");
v1 = rb_path2class("Digest::Instance");
v2 = rb_path2class("Digest::Class");
rb_define_module_function(v0, "bubblebabble", rb_digest_s_bubblebabble, 1LL);
rb_define_singleton_method(v2, "bubblebabble", rb_digest_class_s_bubblebabble, 0xFFFFFFFFLL);
rb_define_method(v1, "bubblebabble", rb_digest_instance_bubblebabble, 0LL);
result = rb_intern_id_cache_2;
if ( rb_intern_id_cache_2 )
{
id_digest = rb_intern_id_cache_2;
}
else
{
result = rb_intern2("digest", 6LL);
rb_intern_id_cache_2 = result;
id_digest = result;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // r13
__int64 v1; // rbp
__int64 v2; // r12
__int64 result; // rax

rb_require("digest");
v0 = rb_path2class("Digest");
v1 = rb_path2class("Digest::Instance");
v2 = rb_path2class("Digest::Class");
rb_define_module_function(v0, "bubblebabble", rb_digest_s_bubblebabble, 1LL);
rb_define_singleton_method(v2, "bubblebabble", rb_digest_class_s_bubblebabble, 0xFFFFFFFFLL);
rb_define_method(v1, "bubblebabble", rb_digest_instance_bubblebabble, 0LL);
result = rb_intern_id_cache_2;
if ( rb_intern_id_cache_2 )
{
id_digest = rb_intern_id_cache_2;
}
else
{
result = rb_intern2("digest", 6LL);
rb_intern_id_cache_2 = result;
id_digest = result;
}
return result;
}

original_code:
void
Init_bubblebabble(void)
{
    VALUE mDigest, mDigest_Instance, cDigest_Class;
    rb_require("digest");
    mDigest = rb_path2class("Digest");
    mDigest_Instance = rb_path2class("Digest::Instance");
    cDigest_Class = rb_path2class("Digest::Class");
    rb_define_module_function(mDigest, "bubblebabble", rb_digest_s_bubblebabble, 1);
    rb_define_singleton_method(cDigest_Class, "bubblebabble", rb_digest_class_s_bubblebabble, -1);
    rb_define_method(mDigest_Instance, "bubblebabble", rb_digest_instance_bubblebabble, 0);
    id_digest = (__builtin_constant_p("digest") ? __extension__ ({ static ID rb_intern_id_cache; if (!rb_intern_id_cache) rb_intern_id_cache = rb_intern2("digest", strlen("digest")); rb_intern_id_cache; }) : rb_intern("digest"));
}

canonical_original_code:
void Init_bubblebabble()
{
    unsigned long mDigest; unsigned long mDigest_Instance; unsigned long cDigest_Class;
    rb_require("digest");
    mDigest = rb_path2class("Digest");
    mDigest_Instance = rb_path2class("Digest::Instance");
    cDigest_Class = rb_path2class("Digest::Class");
    rb_define_module_function(mDigest, "bubblebabble", rb_digest_s_bubblebabble, 1);
    rb_define_singleton_method(cDigest_Class, "bubblebabble", rb_digest_class_s_bubblebabble, -1);
    rb_define_method(mDigest_Instance, "bubblebabble", rb_digest_instance_bubblebabble, 0);
    id_digest = (__builtin_constant_p("digest") ? __extension__ ({ static unsigned long rb_intern_id_cache; if (!rb_intern_id_cache) rb_intern_id_cache = rb_intern2("digest", strlen("digest")); rb_intern_id_cache; }) : rb_intern("digest"));
}


decompiled_code:
__int64 Init_complex()
{
return nucomp_canonicalization(1LL);
}

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

original_code:
void
Init_complex(void)
{
    nucomp_canonicalization(1);
}

canonical_original_code:
void Init_complex()
{
    nucomp_canonicalization(1);
}


original_code:
void
rb_Digest_MD5_Update(MD5_CTX *pms, const uint8_t *data, size_t nbytes)
{
    const uint8_t *p = data;
    size_t left = nbytes;
    size_t offset = (pms->count[0] >> 3) & 63;
    uint32_t nbits = (uint32_t)(nbytes << 3);
    if (nbytes <= 0)
 return;
    pms->count[1] += nbytes >> 29;
    pms->count[0] += nbits;
    if (pms->count[0] < nbits)
 pms->count[1]++;
    if (offset) {
 size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
 memcpy(pms->buffer + offset, p, copy);
 if (offset + copy < 64)
     return;
 p += copy;
 left -= copy;
 md5_process(pms, pms->buffer);
    }
    for (; left >= 64; p += 64, left -= 64)
 md5_process(pms, p);
    if (left)
 memcpy(pms->buffer, p, left);
}

canonical_original_code:
void rb_Digest_MD5_Update(struct md5_state_s *pms, unsigned char *data, unsigned long nbytes)
{
    unsigned char *p = data;
    unsigned long left = nbytes;
    unsigned long offset = (pms->count[0] >> 3) & 63;
    unsigned int nbits = (uint32_t)(nbytes << 3);
    if (nbytes <= 0)
 return;
    pms->count[1] += nbytes >> 29;
    pms->count[0] += nbits;
    if (pms->count[0] < nbits)
 pms->count[1]++;
    if (offset) {
 unsigned long copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
 memcpy(pms->buffer + offset, p, copy);
 if (offset + copy < 64)
     return;
 p += copy;
 left -= copy;
 md5_process(pms, pms->buffer);
    }
    for (; left >= 64; p += 64, left -= 64)
 md5_process(pms, p);
    if (left)
 memcpy(pms->buffer, p, left);
}

variable_types: struct md5_state_s { unsigned int count[2]; unsigned int state[4]; unsigned char buffer[64]; } *

variable_types:
data
unsigned char *

variable_types:
nbytes
unsigned long

variable_types:
p
unsigned char *

variable_types:
left
unsigned long

variable_types:
offset
unsigned long

variable_types:
nbits
unsigned int

variable_types:
copy
unsigned long


decompiled_code:
unsigned __int64 __fastcall rb_Digest_MD5_Finish(unsigned int *a1, __int64 a2)
{
unsigned __int64 i; // rax
unsigned __int64 result; // rax
char v4[32]; // [rsp+8h] [rbp-20h] BYREF

for ( i = 0LL; i != 8; ++i )
v4[i] = a1[i >> 2] >> ((8 * i) & 0x18);
rb_Digest_MD5_Update(a1, pad_0, ((55 - (unsigned __int8)(*a1 >> 3)) & 0x3F) + 1LL);
rb_Digest_MD5_Update(a1, v4, 8uLL);
for ( result = 0LL; result != 16; ++result )
*(_BYTE *)(a2 + result) = a1[(result >> 2) + 2] >> ((8 * result) & 0x18);
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(unsigned int *a1, __int64 a2)
{
unsigned __int64 i; // rax
unsigned __int64 result; // rax
char v4[32]; // [rsp+8h] [rbp-20h] BYREF

for ( i = 0LL; i != 8; ++i )
v4[i] = a1[i >> 2] >> ((8 * i) & 0x18);
func2(a1, pad_0, ((55 - (unsigned __int8)(*a1 >> 3)) & 0x3F) + 1LL);
func2(a1, v4, 8uLL);
for ( result = 0LL; result != 16; ++result )
*(_BYTE *)(a2 + result) = a1[(result >> 2) + 2] >> ((8 * result) & 0x18);
return result;
}

variable_types:
digest
unsigned char *

variable_types:
pad
unsigned char [64]

variable_types:
data
unsigned char [8]

variable_types:
i
unsigned long

user_defined_types: struct md5_state_s { unsigned int count[2]; unsigned int state[4]; unsigned char buffer[64]; }


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

if ( (a1 & 3) != 0 )
{
result = 21LL;
if ( (a1 & 1) == 0 )
{
if ( a1 == 2 )
{
result = 18LL;
}
else if ( (_BYTE)a1 == 14 )
{
result = 20LL;
}
else
{
if ( a1 != 6 )
return *(_QWORD *)a1 & 0x1FLL;
result = 27LL;
}
}
}
else
{
if ( (a1 & 0xFFFFFFFFFFFFFFFBLL) != 0 )
return *(_QWORD *)a1 & 0x1FLL;
if ( a1 == 4 )
{
result = 17LL;
}
else
{
result = 19LL;
if ( a1 )
return *(_QWORD *)a1 & 0x1FLL;
}
}
return result;
}

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

if ( (a1 & 3) != 0 )
{
result = 21LL;
if ( (a1 & 1) == 0 )
{
if ( a1 == 2 )
{
result = 18LL;
}
else if ( (_BYTE)a1 == 14 )
{
result = 20LL;
}
else
{
if ( a1 != 6 )
return *(_QWORD *)a1 & 0x1FLL;
result = 27LL;
}
}
}
else
{
if ( (a1 & 0xFFFFFFFFFFFFFFFBLL) != 0 )
return *(_QWORD *)a1 & 0x1FLL;
if ( a1 == 4 )
{
result = 17LL;
}
else
{
result = 19LL;
if ( a1 )
return *(_QWORD *)a1 & 0x1FLL;
}
}
return result;
}

original_code:
static inline int
rb_type(VALUE obj)
{
    if (((VALUE)(obj) & RUBY_IMMEDIATE_MASK)) {
 if ((((long)(obj))&RUBY_FIXNUM_FLAG)) return RUBY_T_FIXNUM;
 if (obj == ((VALUE)RUBY_Qtrue)) return RUBY_T_TRUE;
 if ((((VALUE)(obj)&~(~(VALUE)0<<RUBY_SPECIAL_SHIFT))==RUBY_SYMBOL_FLAG)) return RUBY_T_SYMBOL;
 if (obj == ((VALUE)RUBY_Qundef)) return RUBY_T_UNDEF;
    }
    else if (!(((VALUE)(obj) & ~((VALUE)RUBY_Qnil)) != 0)) {
 if (obj == ((VALUE)RUBY_Qnil)) return RUBY_T_NIL;
 if (obj == ((VALUE)RUBY_Qfalse)) return RUBY_T_FALSE;
    }
    return (((struct RBasic*)(obj))->flags & RUBY_T_MASK);
}

canonical_original_code:
static inline int rb_type(unsigned long obj)
{
    if (((VALUE)(obj) & RUBY_IMMEDIATE_MASK)) {
 if ((((long)(obj))&RUBY_FIXNUM_FLAG)) return RUBY_T_FIXNUM;
 if (obj == ((unsigned long)RUBY_Qtrue)) return RUBY_T_TRUE;
 if ((((VALUE)(obj)&~(~(unsigned long)0<<RUBY_SPECIAL_SHIFT))==RUBY_SYMBOL_FLAG)) return RUBY_T_SYMBOL;
 if (obj == ((unsigned long)RUBY_Qundef)) return RUBY_T_UNDEF;
    }
    else if (!(((VALUE)(obj) & ~((unsigned long)RUBY_Qnil)) != 0)) {
 if (obj == ((unsigned long)RUBY_Qnil)) return RUBY_T_NIL;
 if (obj == ((unsigned long)RUBY_Qfalse)) return RUBY_T_FALSE;
    }
    return (((struct RBasic *)(obj))->flags & RUBY_T_MASK);
}

variable_types:
obj
unsigned long

return_type: int

user_defined_types: struct RBasic { unsigned long flags; unsigned long klass; }


repo: masak/git-learning

decompiled_code:
__int64 __fastcall verify_path(char *a1)
{
char v1; // al
char *v2; // rdi
char *v4; // rdx
char v5; // al

v1 = *a1;
if ( (unsigned __int8)(*a1 - 46) <= 1u )
return 0LL;
v2 = a1 + 1;
if ( !v1 )
return 0LL;
while ( 1 )
{
v4 = v2;
v5 = *v2++;
if ( !v5 )
break;
if ( v5 == 47 )
{
if ( (unsigned __int8)(*v2 - 46) <= 1u || !*v2 )
return 0LL;
v2 = v4 + 2;
}
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char v1; // al
char *v2; // rdi
char *v4; // rdx
char v5; // al

v1 = *a1;
if ( (unsigned __int8)(*a1 - 46) <= 1u )
return 0LL;
v2 = a1 + 1;
if ( !v1 )
return 0LL;
while ( 1 )
{
v4 = v2;
v5 = *v2++;
if ( !v5 )
break;
if ( v5 == 47 )
{
if ( (unsigned __int8)(*v2 - 46) <= 1u || !*v2 )
return 0LL;
v2 = v4 + 2;
}
}
return 1LL;
}

original_code:
static int verify_path(char *path)
{
 char c;
 goto inside;
 for (;;) {
  if (!c)
   return 1;
  if (c == '/') {
inside:
   c = *path++;
   if (c != '/' && c != '.' && c != '\0')
    continue;
   return 0;
  }
  c = *path++;
 }
}

variable_types:
path
char *

variable_types: c char


decompiled_code:
int __fastcall cache_name_compare(const void *a1, signed int a2, const void *a3, signed int a4)
{
size_t v7; // rdx
int result; // eax

v7 = (unsigned int)a2;
if ( a4 <= a2 )
v7 = (unsigned int)a4;
result = memcmp(a1, a3, v7);
if ( !result )
{
if ( a4 > a2 )
result = -1;
else
result = a4 < a2;
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(const void *a1, signed int a2, const void *a3, signed int a4)
{
size_t v7; // rdx
int result; // eax

v7 = (unsigned int)a2;
if ( a4 <= a2 )
v7 = (unsigned int)a4;
result = memcmp(a1, a3, v7);
if ( !result )
{
if ( a4 > a2 )
result = -1;
else
result = a4 < a2;
}
return result;
}

original_code:
static int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
{
 int len = len1 < len2 ? len1 : len2;
 int cmp;
 cmp = memcmp(name1, name2, len);
 if (cmp)
  return cmp;
 if (len1 < len2)
  return -1;
 if (len1 > len2)
  return 1;
 return 0;
}

canonical_original_code:
static int cache_name_compare(char *name1, int len1, char *name2, int len2)
{
 int len = len1 < len2 ? len1 : len2;
 int cmp;
 cmp = memcmp(name1, name2, len);
 if (cmp)
  return cmp;
 if (len1 < len2)
  return -1;
 if (len1 > len2)
  return 1;
 return 0;
}

variable_types: len int

variable_types: cmp int


decompiled_code:
__int64 __fastcall cache_name_pos(const void *a1, signed int a2)
{
unsigned int v2; // er14
int v3; // er15
_QWORD *v4; // r13
int v5; // ebx
int v6; // eax

v2 = 0;
v3 = active_nr;
v4 = active_cache;
LABEL_2:
if ( v3 > (int)v2 )
{
while ( 1 )
{
v5 = (int)(v3 + v2) >> 1;
v6 = cache_name_compare(a1, a2, (const void *)(v4[v5] + 62LL), *(unsigned __int16 *)(v4[v5] + 60LL));
if ( !v6 )
break;
if ( v6 < 0 )
{
v3 = (int)(v3 + v2) >> 1;
goto LABEL_2;
}
v2 = v5 + 1;
if ( v3 <= v5 + 1 )
return v2;
}
v2 = ~v5;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(const void *a1, signed int a2)
{
unsigned int v2; // er14
int v3; // er15
_QWORD *v4; // r13
int v5; // ebx
int v6; // eax

v2 = 0;
v3 = active_nr;
v4 = active_cache;
LABEL_2:
if ( v3 > (int)v2 )
{
while ( 1 )
{
v5 = (int)(v3 + v2) >> 1;
v6 = func1(a1, a2, (const void *)(v4[v5] + 62LL), *(unsigned __int16 *)(v4[v5] + 60LL));
if ( !v6 )
break;
if ( v6 < 0 )
{
v3 = (int)(v3 + v2) >> 1;
goto LABEL_2;
}
v2 = v5 + 1;
if ( v3 <= v5 + 1 )
return v2;
}
v2 = ~v5;
}
return v2;
}

original_code:
static int cache_name_pos(const char *name, int namelen)
{
 int first, last;
 first = 0;
 last = active_nr;
 while (last > first) {
  int next = (last + first) >> 1;
  struct cache_entry *ce = active_cache[next];
  int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
  if (!cmp)
   return -next-1;
  if (cmp < 0) {
   last = next;
   continue;
  }
  first = next+1;
 }
 return first;
}

canonical_original_code:
static int cache_name_pos(char *name, int namelen)
{
 int first; int last;
 first = 0;
 last = active_nr;
 while (last > first) {
  int next = (last + first) >> 1;
  struct cache_entry *ce = active_cache[next];
  int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
  if (!cmp)
   return -next-1;
  if (cmp < 0) {
   last = next;
   continue;
  }
  first = next+1;
 }
 return first;
}

variable_types:
name
char *

variable_types: namelen int

variable_types: first int

variable_types: last int

variable_types: next int

variable_types: struct cache_entry { struct cache_time { unsigned int sec; unsigned int nsec; } ctime; struct cache_time mtime; unsigned int st_dev; unsigned int st_ino; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; unsigned int st_size; unsigned char sha1[20]; unsigned short namelen; unsigned char name[]; } *

user_defined_types: struct cache_entry { struct cache_time ctime; struct cache_time mtime; unsigned int st_dev; unsigned int st_ino; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; unsigned int st_size; unsigned char sha1[20]; unsigned short namelen; unsigned char name[]; }


decompiled_code:
__int64 __fastcall remove_file_from_cache(const char *a1)
{
signed int v1; // eax
__int64 result; // rax
int v3; // edx

v1 = strlen(a1);
result = cache_name_pos(a1, v1);
if ( (int)result < 0 )
{
v3 = active_nr;
result = (unsigned int)~(_DWORD)result;
if ( --active_nr > (unsigned int)result )
result = (__int64)memmove(
(char *)active_cache + 8 * (int)result,
(char *)active_cache + 8 * (int)result + 8,
8LL * (unsigned int)(v3 - 2 - result));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
signed int v1; // eax
__int64 result; // rax
int v3; // edx

v1 = strlen(a1);
result = func2(a1, v1);
if ( (int)result < 0 )
{
v3 = active_nr;
result = (unsigned int)~(_DWORD)result;
if ( --active_nr > (unsigned int)result )
result = (__int64)memmove(
(char *)active_cache + 8 * (int)result,
(char *)active_cache + 8 * (int)result + 8,
8LL * (unsigned int)(v3 - 2 - result));
}
return result;
}

original_code:
static int remove_file_from_cache(char *path)
{
 int pos = cache_name_pos(path, strlen(path));
 if (pos < 0) {
  pos = -pos-1;
  active_nr--;
  if (pos < active_nr)
   memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos - 1) * sizeof(struct cache_entry *));
 }
}

variable_types: pos int

user_defined_types: struct cache_time { unsigned int sec; unsigned int nsec; }


decompiled_code:
__int64 __fastcall add_cache_entry(__int64 a1)
{
int v2; // eax
_QWORD *v3; // rdi
signed int v4; // ebx
int v5; // edx
__int64 result; // rax
void *v7; // rax

v2 = cache_name_pos((const void *)(a1 + 62), *(unsigned __int16 *)(a1 + 60));
v3 = active_cache;
v4 = v2;
if ( v2 < 0 )
{
result = 0LL;
*((_QWORD *)active_cache - v4 - 1) = a1;
}
else
{
v5 = active_nr;
if ( active_nr == active_alloc )
{
active_alloc = (unsigned int)(3 * active_nr + 48) >> 1;
v7 = realloc(active_cache, 8LL * ((unsigned int)(3 * active_nr + 48) >> 1));
v5 = active_nr;
active_cache = v7;
v3 = v7;
}
active_nr = v5 + 1;
if ( v5 + 1 > (unsigned int)v4 )
{
memmove(&v3[v4 + 1], &v3[v4], 8LL * (unsigned int)(v5 - v4));
v3 = active_cache;
}
v3[v4] = a1;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
int v2; // eax
_QWORD *v3; // rdi
signed int v4; // ebx
int v5; // edx
__int64 result; // rax
void *v7; // rax

v2 = func2((const void *)(a1 + 62), *(unsigned __int16 *)(a1 + 60));
v3 = active_cache;
v4 = v2;
if ( v2 < 0 )
{
result = 0LL;
*((_QWORD *)active_cache - v4 - 1) = a1;
}
else
{
v5 = active_nr;
if ( active_nr == active_alloc )
{
active_alloc = (unsigned int)(3 * active_nr + 48) >> 1;
v7 = realloc(active_cache, 8LL * ((unsigned int)(3 * active_nr + 48) >> 1));
v5 = active_nr;
active_cache = v7;
v3 = v7;
}
active_nr = v5 + 1;
if ( v5 + 1 > (unsigned int)v4 )
{
memmove(&v3[v4 + 1], &v3[v4], 8LL * (unsigned int)(v5 - v4));
v3 = active_cache;
}
v3[v4] = a1;
result = 0LL;
}
return result;
}

original_code:
static int add_cache_entry(struct cache_entry *ce)
{
 int pos;
 pos = cache_name_pos(ce->name, ce->namelen);
 if (pos < 0) {
  active_cache[-pos-1] = ce;
  return 0;
 }
 if (active_nr == active_alloc) {
  active_alloc = (((active_alloc)+16)*3/2);
  active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
 }
 active_nr++;
 if (active_nr > pos)
  memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
 active_cache[pos] = ce;
 return 0;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
char **v4; // rbx
const char **v5; // rbp
char *v6; // r12
int result; // eax

if ( (int)read_cache(argc, argv, envp) < 0 )
{
perror("cache corrupted");
result = -1;
}
else
{
v3 = open(".dircache/index.lock", 194, 384LL);
if ( v3 < 0 )
{
perror("unable to create new cachefile");
result = -1;
}
else
{
if ( argc > 1 )
{
v4 = (char **)(argv + 1);
v5 = &argv[(unsigned int)(argc - 2) + 2];
while ( 1 )
{
while ( 1 )
{
v6 = *v4;
if ( (unsigned int)verify_path(*v4) )
break;
++v4;
fprintf(stderr, "Ignoring path %s\n", v6);
if ( v4 == (char **)v5 )
goto LABEL_9;
}
if ( (unsigned int)add_file_to_cache(v6) )
break;
if ( ++v4 == (char **)v5 )
goto LABEL_9;
}
fprintf(stderr, "Unable to add %s to database\n", v6);
goto LABEL_10;
}
LABEL_9:
if ( (unsigned int)write_cache(v3, (const void **)active_cache, active_nr)
|| (result = rename(".dircache/index.lock", ".dircache/index")) != 0 )
{
LABEL_10:
unlink(".dircache/index.lock");
return 0;
}
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
char **v4; // rbx
const char **v5; // rbp
char *v6; // r12
int result; // eax

if ( (int)read_cache(argc, argv, envp) < 0 )
{
perror("cache corrupted");
result = -1;
}
else
{
v3 = open(".dircache/index.lock", 194, 384LL);
if ( v3 < 0 )
{
perror("unable to create new cachefile");
result = -1;
}
else
{
if ( argc > 1 )
{
v4 = (char **)(argv + 1);
v5 = &argv[(unsigned int)(argc - 2) + 2];
while ( 1 )
{
while ( 1 )
{
v6 = *v4;
if ( (unsigned int)func0(*v4) )
break;
++v4;
fprintf(stderr, "Ignoring path %s\n", v6);
if ( v4 == (char **)v5 )
goto LABEL_9;
}
if ( (unsigned int)func7(v6) )
break;
if ( ++v4 == (char **)v5 )
goto LABEL_9;
}
fprintf(stderr, "Unable to add %s to database\n", v6);
goto LABEL_10;
}
LABEL_9:
if ( (unsigned int)func5(v3, (const void **)active_cache, active_nr)
|| (result = rename(".dircache/index.lock", ".dircache/index")) != 0 )
{
LABEL_10:
unlink(".dircache/index.lock");
return 0;
}
}
}
return result;
}

original_code:
int main(int argc, char **argv)
{
 int i, newfd, entries;
 entries = read_cache();
 if (entries < 0) {
  perror("cache corrupted");
  return -1;
 }
 newfd = open(".dircache/index.lock", 02 | 0100 | 0200, 0600);
 if (newfd < 0) {
  perror("unable to create new cachefile");
  return -1;
 }
 for (i = 1 ; i < argc; i++) {
  char *path = argv[i];
  if (!verify_path(path)) {
   fprintf(stderr, "Ignoring path %s\n", argv[i]);
   continue;
  }
  if (add_file_to_cache(path)) {
   fprintf(stderr, "Unable to add %s to database\n", path);
   goto out;
  }
 }
 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
  return 0;
out:
 unlink(".dircache/index.lock");
}

canonical_original_code:
int main(int argc, char **argv)
{
 int i; int newfd; int entries;
 entries = read_cache();
 if (entries < 0) {
  perror("cache corrupted");
  return -1;
 }
 newfd = open(".dircache/index.lock", 02 | 0100 | 0200, 0600);
 if (newfd < 0) {
  perror("unable to create new cachefile");
  return -1;
 }
 for (i = 1 ; i < argc; i++) {
  char *path = argv[i];
  if (!verify_path(path)) {
   fprintf(stderr, "Ignoring path %s\n", argv[i]);
   continue;
  }
  if (add_file_to_cache(path)) {
   fprintf(stderr, "Unable to add %s to database\n", path);
   goto out;
  }
 }
 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
  return 0;
out:
 unlink(".dircache/index.lock");
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types: newfd int

variable_types: entries int


hexval

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

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)(char)(a1 - 48);
if ( (unsigned __int8)(a1 - 97) <= 5u )
return (unsigned int)(char)(a1 - 87);
result = (unsigned int)(char)(a1 - 55);
if ( (unsigned __int8)(a1 - 65) >= 6u )
result = 0xFFFFFFFFLL;
return result;
}

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

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)(char)(a1 - 48);
if ( (unsigned __int8)(a1 - 97) <= 5u )
return (unsigned int)(char)(a1 - 87);
result = (unsigned int)(char)(a1 - 55);
if ( (unsigned __int8)(a1 - 65) >= 6u )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
static unsigned hexval(char c)
{
 if (c >= '0' && c <= '9')
  return c - '0';
 if (c >= 'a' && c <= 'f')
  return c - 'a' + 10;
 if (c >= 'A' && c <= 'F')
  return c - 'A' + 10;
 return ~0;
}

canonical_original_code:
static unsigned int hexval(char c)
{
 if (c >= '0' && c <= '9')
  return c - '0';
 if (c >= 'a' && c <= 'f')
  return c - 'a' + 10;
 if (c >= 'A' && c <= 'F')
  return c - 'A' + 10;
 return ~0;
}

return_type: unsigned int


error

decompiled_code:
__int64 __fastcall error(const char *a1)
{
fprintf(stderr, "error: %s\n", a1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
fprintf(stderr, "error: %s\n", a1);
return 0xFFFFFFFFLL;
}

original_code:
static int error(const char * string)
{
 fprintf(stderr, "error: %s\n", string);
 return -1;
}

canonical_original_code:
static int error(char *string)
{
 fprintf(stderr, "error: %s\n", string);
 return -1;
}

variable_types:
string
char *


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
fprintf(stderr, "read-tree: %s\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(const char *a1)
{
fprintf(stderr, "read-tree: %s\n", a1);
exit(1);
}

original_code:
void usage(const char *err)
{
 fprintf(stderr, "read-tree: %s\n", err);
 exit(1);
}

canonical_original_code:
void usage(char *err)
{
 fprintf(stderr, "read-tree: %s\n", err);
 exit(1);
}

variable_types:
err
char *


original_code:
int get_sha1_hex(char *hex, unsigned char *sha1)
{
 int i;
 for (i = 0; i < 20; i++) {
  unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
  if (val & ~0xff)
   return -1;
  *sha1++ = val;
  hex += 2;
 }
 return 0;
}

variable_types:
hex
char *

variable_types:
val
unsigned int


decompiled_code:
__int64 __fastcall write_sha1_buffer(__int64 a1, const void *a2, unsigned int a3)
{
const char *v4; // rax
int v5; // eax
int v6; // ebp

v4 = sha1_file_name(a1);
v5 = open(v4, 193, 438LL);
if ( v5 < 0 )
return (unsigned int)-(*_errno_location() != 17);
v6 = v5;
write(v5, a2, a3);
close(v6);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const void *a2, unsigned int a3)
{
const char *v4; // rax
int v5; // eax
int v6; // ebp

v4 = sha1_file_name(a1);
v5 = open(v4, 193, 438LL);
if ( v5 < 0 )
return (unsigned int)-(*_errno_location() != 17);
v6 = v5;
write(v5, a2, a3);
close(v6);
return 0LL;
}

original_code:
int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
{
 char *filename = sha1_file_name(sha1);
 int i, fd;
 fd = open(filename, 01 | 0100 | 0200, 0666);
 if (fd < 0)
  return ((*__errno_location ()) == 17) ? 0 : -1;
 write(fd, buf, size);
 close(fd);
 return 0;
}

canonical_original_code:
int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
{
 char *filename = sha1_file_name(sha1);
 int i; int fd;
 fd = open(filename, 01 | 0100 | 0200, 0666);
 if (fd < 0)
  return ((*__errno_location ()) == 17) ? 0 : -1;
 write(fd, buf, size);
 close(fd);
 return 0;
}

variable_types:
buf
void *

variable_types:
size
unsigned int

variable_types:
filename
char *

variable_types: fd int


decompiled_code:
int check_valid_sha1()
{
const char *v0; // rbp
int result; // eax
int v2; // [rsp+Ch] [rbp-Ch]

v0 = (const char *)sha1_file_name();
result = access(v0, 4);
if ( result )
{
v2 = result;
perror(v0);
result = v2;
}
return result;
}

canonical_decompiled_code:
int func0()
{
const char *v0; // rbp
int result; // eax
int v2; // [rsp+Ch] [rbp-Ch]

v0 = (const char *)sha1_file_name();
result = access(v0, 4);
if ( result )
{
v2 = result;
perror(v0);
result = v2;
}
return result;
}

original_code:
static int check_valid_sha1(unsigned char *sha1)
{
 char *filename = sha1_file_name(sha1);
 int ret;
 ret = access(filename, 4);
 if (ret)
  perror(filename);
 return ret;
}

variable_types: ret int


decompiled_code:
__int64 __fastcall match_stat(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = 1LL;
if ( a1[2] == a2[22] )
result = a1[3] != a2[24];
if ( *a1 != a2[26] || a1[1] != a2[28] )
{
result = (unsigned int)result | 2;
if ( a1[7] == a2[7] )
goto LABEL_15;
LABEL_5:
result = (unsigned int)result | 4;
goto LABEL_6;
}
if ( a1[7] != a2[7] )
goto LABEL_5;
LABEL_15:
if ( a1[8] != a2[8] )
goto LABEL_5;
LABEL_6:
if ( a1[6] != a2[6] )
result = (unsigned int)result | 8;
if ( a1[4] != *a2 || a1[5] != a2[2] )
result = (unsigned int)result | 0x10;
if ( a1[9] != a2[12] )
result = (unsigned int)result | 0x20;
return result;
}

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

result = 1LL;
if ( a1[2] == a2[22] )
result = a1[3] != a2[24];
if ( *a1 != a2[26] || a1[1] != a2[28] )
{
result = (unsigned int)result | 2;
if ( a1[7] == a2[7] )
goto LABEL_15;
LABEL_5:
result = (unsigned int)result | 4;
goto LABEL_6;
}
if ( a1[7] != a2[7] )
goto LABEL_5;
LABEL_15:
if ( a1[8] != a2[8] )
goto LABEL_5;
LABEL_6:
if ( a1[6] != a2[6] )
result = (unsigned int)result | 8;
if ( a1[4] != *a2 || a1[5] != a2[2] )
result = (unsigned int)result | 0x10;
if ( a1[9] != a2[12] )
result = (unsigned int)result | 0x20;
return result;
}

original_code:
static int match_stat(struct cache_entry *ce, struct stat *st)
{
 unsigned int changed = 0;
 if (ce->mtime.sec != (unsigned int)st->st_mtim.tv_sec ||
     ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec)
  changed |= 0x0001;
 if (ce->ctime.sec != (unsigned int)st->st_ctim.tv_sec ||
     ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec)
  changed |= 0x0002;
 if (ce->st_uid != (unsigned int)st->st_uid ||
     ce->st_gid != (unsigned int)st->st_gid)
  changed |= 0x0004;
 if (ce->st_mode != (unsigned int)st->st_mode)
  changed |= 0x0008;
 if (ce->st_dev != (unsigned int)st->st_dev ||
     ce->st_ino != (unsigned int)st->st_ino)
  changed |= 0x0010;
 if (ce->st_size != (unsigned int)st->st_size)
  changed |= 0x0020;
 return changed;
}

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

variable_types:
changed
unsigned int

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


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // r13
__int64 v5; // r14
__int64 v6; // r12
unsigned __int8 *v7; // rbx
__int64 v8; // rsi
void *v9; // rbp
int *v11; // rax
unsigned int v12; // eax
size_t v13; // [rsp+8h] [rbp-F0h] BYREF
char v14[32]; // [rsp+10h] [rbp-E8h] BYREF
struct stat v15; // [rsp+30h] [rbp-C8h] BYREF

v3 = read_cache(argc, argv, envp);
if ( v3 < 0 )
{
perror("read_cache");
exit(1);
}
if ( v3 )
{
v4 = 0LL;
v5 = 8LL * v3;
while ( 1 )
{
v6 = *(_QWORD *)(active_cache + v4);
if ( stat((const char *)(v6 + 62), &v15) < 0 )
break;
if ( (unsigned int)match_stat((_DWORD *)v6, &v15) )
{
printf("%.*s: ", *(unsigned __int16 *)(v6 + 60), (const char *)(v6 + 62));
v7 = (unsigned __int8 *)(v6 + 40);
do
{
v8 = *v7++;
printf("%02x", v8);
}
while ( (unsigned __int8 *)(v6 + 60) != v7 );
v4 += 8LL;
putchar(10);
v9 = (void *)read_sha1_file(v6 + 40, v14, &v13);
show_differences_constprop_0(v6, v9, v13);
free(v9);
if ( v4 == v5 )
return 0;
}
else
{
printf("%s: ok\n", (const char *)(v6 + 62));
LABEL_5:
v4 += 8LL;
if ( v4 == v5 )
return 0;
}
}
v11 = _errno_location();
v12 = (unsigned int)strerror(*v11);
printf("%s: %s\n", (const char *)(v6 + 62), (const char *)v12);
goto LABEL_5;
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // r13
__int64 v5; // r14
__int64 v6; // r12
unsigned __int8 *v7; // rbx
__int64 v8; // rsi
void *v9; // rbp
int *v11; // rax
unsigned int v12; // eax
size_t v13; // [rsp+8h] [rbp-F0h] BYREF
char v14[32]; // [rsp+10h] [rbp-E8h] BYREF
struct stat v15; // [rsp+30h] [rbp-C8h] BYREF

v3 = read_cache(argc, argv, envp);
if ( v3 < 0 )
{
perror("read_cache");
exit(1);
}
if ( v3 )
{
v4 = 0LL;
v5 = 8LL * v3;
while ( 1 )
{
v6 = *(_QWORD *)(active_cache + v4);
if ( stat((const char *)(v6 + 62), &v15) < 0 )
break;
if ( (unsigned int)func0((_DWORD *)v6, &v15) )
{
printf("%.*s: ", *(unsigned __int16 *)(v6 + 60), (const char *)(v6 + 62));
v7 = (unsigned __int8 *)(v6 + 40);
do
{
v8 = *v7++;
printf("%02x", v8);
}
while ( (unsigned __int8 *)(v6 + 60) != v7 );
v4 += 8LL;
putchar(10);
v9 = (void *)read_sha1_file(v6 + 40, v14, &v13);
func1(v6, v9, v13);
free(v9);
if ( v4 == v5 )
return 0;
}
else
{
printf("%s: ok\n", (const char *)(v6 + 62));
LABEL_5:
v4 += 8LL;
if ( v4 == v5 )
return 0;
}
}
v11 = _errno_location();
v12 = (unsigned int)strerror(*v11);
printf("%s: %s\n", (const char *)(v6 + 62), (const char *)v12);
goto LABEL_5;
}
return 0;
}

original_code:
int main(int argc, char **argv)
{
 int entries = read_cache();
 int i;
 if (entries < 0) {
  perror("read_cache");
  exit(1);
 }
 for (i = 0; i < entries; i++) {
  struct stat st;
  struct cache_entry *ce = active_cache[i];
  int n, changed;
  unsigned int mode;
  unsigned long size;
  char type[20];
  void *new;
  if (stat(ce->name, &st) < 0) {
   printf("%s: %s\n", ce->name, strerror((*__errno_location ())));
   continue;
  }
  changed = match_stat(ce, &st);
  if (!changed) {
   printf("%s: ok\n", ce->name);
   continue;
  }
  printf("%.*s:  ", ce->namelen, ce->name);
  for (n = 0; n < 20; n++)
   printf("%02x", ce->sha1[n]);
  printf("\n");
  new = read_sha1_file(ce->sha1, type, &size);
  show_differences(ce, &st, new, size);
  free(new);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int entries = read_cache();
 int i;
 if (entries < 0) {
  perror("read_cache");
  exit(1);
 }
 for (i = 0; i < entries; i++) {
  struct stat st;
  struct cache_entry *ce = active_cache[i];
  int n; int changed;
  unsigned int mode;
  unsigned long size;
  char type[20];
  void *new;
  if (stat(ce->name, &st) < 0) {
   printf("%s: %s\n", ce->name, strerror((*__errno_location ())));
   continue;
  }
  changed = match_stat(ce, &st);
  if (!changed) {
   printf("%s: ok\n", ce->name);
   continue;
  }
  printf("%.*s:  ", ce->namelen, ce->name);
  for (n = 0; n < 20; n++)
   printf("%02x", ce->sha1[n]);
  printf("\n");
  new = read_sha1_file(ce->sha1, type, &size);
  show_differences(ce, &st, new, size);
  free(new);
 }
 return 0;
}

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

variable_types: struct cache_entry { struct cache_time { unsigned int sec; unsigned int nsec; } ctime; struct cache_time mtime; unsigned int st_dev; unsigned int st_ino; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; unsigned int st_size; unsigned char sha1[20]; unsigned short namelen; unsigned char name[]; } *

variable_types: n int

variable_types: changed int

variable_types:
mode
unsigned int

variable_types:
size
unsigned long

variable_types:
type
char [20]

variable_types:
new
void *


decompiled_code:
unsigned __int8 *__fastcall remove_special(unsigned __int8 *a1)
{
unsigned __int8 *result; // rax
unsigned __int8 v2; // cl

result = a1;
LABEL_2:
v2 = *a1++;
if ( v2 > 0x3Eu )
goto LABEL_4;
while ( 1 )
{
if ( ((1LL << v2) & 0x5000000000000400LL) != 0 )
goto LABEL_2;
LABEL_4:
*result = v2;
if ( !v2 )
return result;
while ( 1 )
{
v2 = *a1;
++result;
++a1;
if ( v2 <= 0x3Eu )
break;
*result = v2;
}
}
}

canonical_decompiled_code:
unsigned __int8 *__fastcall func0(unsigned __int8 *a1)
{
unsigned __int8 *result; // rax
unsigned __int8 v2; // cl

result = a1;
LABEL_2:
v2 = *a1++;
if ( v2 > 0x3Eu )
goto LABEL_4;
while ( 1 )
{
if ( ((1LL << v2) & 0x5000000000000400LL) != 0 )
goto LABEL_2;
LABEL_4:
*result = v2;
if ( !v2 )
return result;
while ( 1 )
{
v2 = *a1;
++result;
++a1;
if ( v2 <= 0x3Eu )
break;
*result = v2;
}
}
}

original_code:
static void remove_special(char *p)
{
 char c;
 char *dst = p;
 for (;;) {
  c = *p;
  p++;
  switch(c) {
  case '\n': case '<': case '>':
   continue;
  }
  *dst++ = c;
  if (!c)
   break;
 }
}

variable_types:
p
char *

variable_types: c char

variable_types:
dst
char *


decompiled_code:
void *add_buffer(void **a1, _DWORD *a2, const char *a3, ...)
{
int v4; // eax
__int64 v5; // r13
char *v6; // rdi
size_t v7; // r12
unsigned __int64 v8; // r14
char *v10; // rax
gcc_va_list arg; // [rsp+8h] [rbp-8F0h] BYREF
char s[2048]; // [rsp+20h] [rbp-8D8h] BYREF

va_start(arg, a3);
v4 = vsnprintf(s, 0x800uLL, a3, arg);
v5 = (unsigned int)*a2;
v6 = (char *)*a1;
v7 = v4;
v8 = v4 + v5;
if ( v8 > ((v5 + 0x7FFF) & 0xFFFFFFFFFFFF8000LL) )
{
v10 = (char *)realloc(v6, (v8 + 0x7FFF) & 0xFFFFFFFFFFFF8000LL);
*a1 = v10;
v6 = v10;
}
*a2 = v8;
return memcpy(&v6[v5], s, v7);
}

canonical_decompiled_code:
void *func1(void **a1, _DWORD *a2, const char *a3, ...)
{
int v4; // eax
__int64 v5; // r13
char *v6; // rdi
size_t v7; // r12
unsigned __int64 v8; // r14
char *v10; // rax
gcc_va_list arg; // [rsp+8h] [rbp-8F0h] BYREF
char s[2048]; // [rsp+20h] [rbp-8D8h] BYREF

va_start(arg, a3);
v4 = vsnprintf(s, 0x800uLL, a3, arg);
v5 = (unsigned int)*a2;
v6 = (char *)*a1;
v7 = v4;
v8 = v4 + v5;
if ( v8 > ((v5 + 0x7FFF) & 0xFFFFFFFFFFFF8000LL) )
{
v10 = (char *)realloc(v6, (v8 + 0x7FFF) & 0xFFFFFFFFFFFF8000LL);
*a1 = v10;
v6 = v10;
}
*a2 = v8;
return memcpy(&v6[v5], s, v7);
}

original_code:
static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
{
 char one_line[2048];
 va_list args;
 int len;
 unsigned long alloc, size, newsize;
 char *buf;
 __builtin_va_start(args,fmt);
 len = vsnprintf(one_line, sizeof(one_line), fmt, args);
 __builtin_va_end(args);
 size = *sizep;
 newsize = size + len;
 alloc = (size + 32767) & ~32767;
 buf = *bufp;
 if (newsize > alloc) {
  alloc = (newsize + 32767) & ~32767;
  buf = realloc(buf, alloc);
  *bufp = buf;
 }
 *sizep = newsize;
 memcpy(buf + size, one_line, len);
}

canonical_original_code:
static void add_buffer(char **bufp, unsigned int *sizep, char *fmt, ...)
{
 char one_line[2048];
 __builtin_va_list args;
 int len;
 unsigned long alloc; unsigned long size; unsigned long newsize;
 char *buf;
 __builtin_va_start(args,fmt);
 len = vsnprintf(one_line, sizeof(one_line), fmt, args);
 __builtin_va_end(args);
 size = *sizep;
 newsize = size + len;
 alloc = (size + 32767) & ~32767;
 buf = *bufp;
 if (newsize > alloc) {
  alloc = (newsize + 32767) & ~32767;
  buf = realloc(buf, alloc);
  *bufp = buf;
 }
 *sizep = newsize;
 memcpy(buf + size, one_line, len);
}

variable_types:
bufp
char **

variable_types:
sizep
unsigned int *

variable_types:
fmt
char *

variable_types: len int

variable_types:
alloc
unsigned long

variable_types:
newsize
unsigned long

variable_types:
buf
char *


unpack

decompiled_code:
__int64 __fastcall unpack(__int64 a1)
{
const char *v1; // r13
int v2; // ebx
int v3; // ebp
unsigned __int64 v4; // rbx
const char *v5; // rbp
const char *v6; // r12
const char *v7; // rax
unsigned __int64 v8; // r14
unsigned int v10; // [rsp+4h] [rbp-54h] BYREF
unsigned __int64 v11; // [rsp+8h] [rbp-50h] BYREF
int v12; // [rsp+10h] [rbp-48h] BYREF
char v13; // [rsp+14h] [rbp-44h]

v1 = (const char *)read_sha1_file(a1, &v12, &v11);
if ( !v1 )
usage("unable to read sha1 file");
if ( v12 != 1701147252 || v13 )
usage("expected a 'tree' node");
while ( 1 )
{
v8 = v11;
if ( !v11 )
break;
v2 = strlen(v1);
v3 = v2 + 1;
v4 = v2 + 21;
v5 = &v1[v3];
v6 = strchr(v1, 32) + 1;
if ( v4 > v8 || (unsigned int)_isoc99_sscanf(v1, "%o", &v10) != 1 )
usage("corrupt 'tree' file");
v11 -= v4;
v1 = v5 + 20;
v7 = (const char *)sha1_to_hex(v5);
printf("%o %s (%s)\n", v10, v6, v7);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
const char *v1; // r13
int v2; // ebx
int v3; // ebp
unsigned __int64 v4; // rbx
const char *v5; // rbp
const char *v6; // r12
const char *v7; // rax
unsigned __int64 v8; // r14
unsigned int v10; // [rsp+4h] [rbp-54h] BYREF
unsigned __int64 v11; // [rsp+8h] [rbp-50h] BYREF
int v12; // [rsp+10h] [rbp-48h] BYREF
char v13; // [rsp+14h] [rbp-44h]

v1 = (const char *)read_sha1_file(a1, &v12, &v11);
if ( !v1 )
usage("unable to read sha1 file");
if ( v12 != 1701147252 || v13 )
usage("expected a 'tree' node");
while ( 1 )
{
v8 = v11;
if ( !v11 )
break;
v2 = strlen(v1);
v3 = v2 + 1;
v4 = v2 + 21;
v5 = &v1[v3];
v6 = strchr(v1, 32) + 1;
if ( v4 > v8 || (unsigned int)_isoc99_sscanf(v1, "%o", &v10) != 1 )
usage("corrupt 'tree' file");
v11 -= v4;
v1 = v5 + 20;
v7 = (const char *)sha1_to_hex(v5);
printf("%o %s (%s)\n", v10, v6, v7);
}
return 0LL;
}

original_code:
static int unpack(unsigned char *sha1)
{
 void *buffer;
 unsigned long size;
 char type[20];
 buffer = read_sha1_file(sha1, type, &size);
 if (!buffer)
  usage("unable to read sha1 file");
 if (strcmp(type, "tree"))
  usage("expected a 'tree' node");
 while (size) {
  int len = strlen(buffer)+1;
  unsigned char *sha1 = buffer + len;
  char *path = strchr(buffer, ' ')+1;
  unsigned int mode;
  if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
   usage("corrupt 'tree' file");
  buffer = sha1 + 20;
  size -= len + 20;
  printf("%o %s (%s)\n", mode, path, sha1_to_hex(sha1));
 }
 return 0;
}

variable_types:
buffer
void *

variable_types:
path
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
_BYTE v5[40]; // [rsp+0h] [rbp-28h] BYREF

if ( argc != 2 )
usage("read-tree <key>");
if ( (int)get_sha1_hex(argv[1], v5, envp) < 0 )
usage("read-tree <key>");
v3 = getenv("SHA1_FILE_DIRECTORY");
if ( !v3 )
v3 = ".dircache/objects";
sha1_file_directory = (__int64)v3;
if ( (int)unpack((__int64)v5) < 0 )
usage("unpack failed");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
_BYTE v5[40]; // [rsp+0h] [rbp-28h] BYREF

if ( argc != 2 )
usage("read-tree <key>");
if ( (int)get_sha1_hex(argv[1], v5, envp) < 0 )
usage("read-tree <key>");
v3 = getenv("SHA1_FILE_DIRECTORY");
if ( !v3 )
v3 = ".dircache/objects";
sha1_file_directory = (__int64)v3;
if ( (int)func0((__int64)v5) < 0 )
usage("unpack failed");
return 0;
}

original_code:
int main(int argc, char **argv)
{
 int fd;
 unsigned char sha1[20];
 if (argc != 2)
  usage("read-tree <key>");
 if (get_sha1_hex(argv[1], sha1) < 0)
  usage("read-tree <key>");
 sha1_file_directory = getenv("SHA1_FILE_DIRECTORY");
 if (!sha1_file_directory)
  sha1_file_directory = ".dircache/objects";
 if (unpack(sha1) < 0)
  usage("unpack failed");
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: fd int


decompiled_code:
_BOOL8 __fastcall get_pixel(int a1, int a2, int a3)
{
return ((unsigned int)read_mem(a1 + (((a3 << 7) + a2 / 8) & 0xFFFFFFFC)) & (1 << ~(_BYTE)a2)) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(int a1, int a2, int a3)
{
return ((unsigned int)read_mem(a1 + (((a3 << 7) + a2 / 8) & 0xFFFFFFFC)) & (1 << ~(_BYTE)a2)) != 0;
}

original_code:
int get_pixel(uint32_t base_addr, int x, int y) {
 uint32_t addr = base_addr +
  (((x / 8) + (y * (1024 / 8))) & (-sizeof(uint32_t)));
 int bit = 31 - x % (sizeof(uint32_t) * 8);
 uint32_t data = read_mem(addr);
 return (((data) & (1 << (bit))) != 0);
}

canonical_original_code:
int get_pixel(unsigned int base_addr, int x, int y) {
 unsigned int addr = base_addr +
  (((x / 8) + (y * (1024 / 8))) & (-sizeof(uint32_t)));
 int bit = 31 - x % (sizeof(uint32_t) * 8);
 unsigned int data = read_mem(addr);
 return (((data) & (1 << (bit))) != 0);
}

variable_types: x int

variable_types: y int

variable_types:
addr
unsigned int

variable_types: bit int

variable_types:
data
unsigned int


decompiled_code:
__int64 __fastcall set_pixel(int a1, int a2, int a3, int a4)
{
int v4; // eax
unsigned int v6; // ebp
int v7; // eax
unsigned int v8; // edx
__int64 v9; // rsi

v4 = a2 + 7;
if ( a2 >= 0 )
v4 = a2;
v6 = (((a3 << 7) + (v4 >> 3)) & 0xFFFFFFFC) + a1;
v7 = read_mem(v6);
v8 = v7 | (1 << ~(_BYTE)a2);
v9 = v7 & (unsigned int)~(1 << ~(_BYTE)a2);
if ( a4 )
v9 = v8;
return write_mem(v6, v9);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, int a3, int a4)
{
int v4; // eax
unsigned int v6; // ebp
int v7; // eax
unsigned int v8; // edx
__int64 v9; // rsi

v4 = a2 + 7;
if ( a2 >= 0 )
v4 = a2;
v6 = (((a3 << 7) + (v4 >> 3)) & 0xFFFFFFFC) + a1;
v7 = read_mem(v6);
v8 = v7 | (1 << ~(_BYTE)a2);
v9 = v7 & (unsigned int)~(1 << ~(_BYTE)a2);
if ( a4 )
v9 = v8;
return write_mem(v6, v9);
}

original_code:
void set_pixel(uint32_t base_addr, int x, int y, int value) {
 uint32_t addr = base_addr +
  (((x / 8) + (y * (1024 / 8))) & (-sizeof(uint32_t)));
 int bit = 31 - x % (sizeof(uint32_t) * 8);
 uint32_t data = read_mem(addr);
 if(value) {
  do { (data) |= (1 << (bit)); } while(0);
 }
 else {
  do { (data) &= ~(1 << (bit)); } while(0);
 }
 write_mem(addr, data);
}

canonical_original_code:
void set_pixel(unsigned int base_addr, int x, int y, int value) {
 unsigned int addr = base_addr +
  (((x / 8) + (y * (1024 / 8))) & (-sizeof(uint32_t)));
 int bit = 31 - x % (sizeof(uint32_t) * 8);
 unsigned int data = read_mem(addr);
 if(value) {
  do { (data) |= (1 << (bit)); } while(0);
 }
 else {
  do { (data) &= ~(1 << (bit)); } while(0);
 }
 write_mem(addr, data);
}

variable_types: value int


decompiled_code:
__int64 __fastcall clr_screen(unsigned int a1)
{
unsigned int v1; // ebp
unsigned int v2; // ebx
__int64 v3; // rdi
__int64 result; // rax

v1 = a1 + 61440;
v2 = a1;
do
{
v3 = v2;
v2 += 4;
result = write_mem(v3, 0LL);
}
while ( v2 != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
unsigned int v1; // ebp
unsigned int v2; // ebx
__int64 v3; // rdi
__int64 result; // rax

v1 = a1 + 61440;
v2 = a1;
do
{
v3 = v2;
v2 += 4;
result = write_mem(v3, 0LL);
}
while ( v2 != v1 );
return result;
}

original_code:
void clr_screen(uint32_t base_addr) {
 int i;
 int nbytes = (1024 * 480) / 8;
 for(i = 0; i < nbytes; i += sizeof(uint32_t)) {
  write_mem(base_addr + i, 0);
 }
}

canonical_original_code:
void clr_screen(unsigned int base_addr) {
 int i;
 int nbytes = (1024 * 480) / 8;
 for(i = 0; i < nbytes; i += sizeof(unsigned int)) {
  write_mem(base_addr + i, 0);
 }
}

variable_types: i int

variable_types: nbytes int


decompiled_code:
__int64 __fastcall blank_screen(unsigned int a1)
{
unsigned int v1; // ebp
unsigned int v2; // ebx
__int64 v3; // rdi
__int64 result; // rax

v1 = a1 + 61440;
v2 = a1;
do
{
v3 = v2;
v2 += 4;
result = write_mem(v3, 0xFFFFFFFFLL);
}
while ( v2 != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
unsigned int v1; // ebp
unsigned int v2; // ebx
__int64 v3; // rdi
__int64 result; // rax

v1 = a1 + 61440;
v2 = a1;
do
{
v3 = v2;
v2 += 4;
result = write_mem(v3, 0xFFFFFFFFLL);
}
while ( v2 != v1 );
return result;
}

original_code:
void blank_screen(uint32_t base_addr) {
 int i;
 int nbytes = (1024 * 480) / 8;
 for(i = 0; i < nbytes; i += sizeof(uint32_t)) {
  write_mem(base_addr + i, 0xFFFFFFFF);
 }
}

canonical_original_code:
void blank_screen(unsigned int base_addr) {
 int i;
 int nbytes = (1024 * 480) / 8;
 for(i = 0; i < nbytes; i += sizeof(unsigned int)) {
  write_mem(base_addr + i, 0xFFFFFFFF);
 }
}


neighbors

original_code:
int neighbors(uint32_t base_addr, int x, int y) {
 int n = 0;
 if(x > 0) {
  if(y > 0) {
   n += get_pixel(base_addr, x - 1, y - 1);
  }
  n += get_pixel(base_addr, x - 1, y);
  if(y < 480/4 -1) {
   n += get_pixel(base_addr, x - 1, y + 1);
  }
 }
 if(y > 0) {
  n += get_pixel(base_addr, x, y - 1);
 }
 if(y < 480/4 -1) {
  n += get_pixel(base_addr, x, y + 1);
 }
 if(x < 640/4 -1) {
  if(y > 0) {
   n += get_pixel(base_addr, x + 1, y - 1);
  }
  n += get_pixel(base_addr, x + 1, y);
  if(y < 480/4 -1) {
   n += get_pixel(base_addr, x + 1, y + 1);
  }
 }
 return n;
}

canonical_original_code:
int neighbors(unsigned int base_addr, int x, int y) {
 int n = 0;
 if(x > 0) {
  if(y > 0) {
   n += get_pixel(base_addr, x - 1, y - 1);
  }
  n += get_pixel(base_addr, x - 1, y);
  if(y < 480/4 -1) {
   n += get_pixel(base_addr, x - 1, y + 1);
  }
 }
 if(y > 0) {
  n += get_pixel(base_addr, x, y - 1);
 }
 if(y < 480/4 -1) {
  n += get_pixel(base_addr, x, y + 1);
 }
 if(x < 640/4 -1) {
  if(y > 0) {
   n += get_pixel(base_addr, x + 1, y - 1);
  }
  n += get_pixel(base_addr, x + 1, y);
  if(y < 480/4 -1) {
   n += get_pixel(base_addr, x + 1, y + 1);
  }
 }
 return n;
}

variable_types: n int


decompiled_code:
__int64 game_of_life()
{
int i; // er12
int j; // ebx
int v2; // edx
__int64 result; // rax
int v4; // ebp
int v5; // edx

for ( i = 0; i != 160; ++i )
{
for ( j = 0; j != 120; ++j )
{
while ( 1 )
{
v4 = neighbors(old_img_addr, i, j);
if ( !get_pixel(old_img_addr, i, j) )
break;
if ( (unsigned int)(v4 - 2) <= 1 )
goto LABEL_7;
LABEL_4:
v2 = j++;
result = set_pixel(new_img_addr, i, v2, 0);
if ( j == 120 )
goto LABEL_8;
}
if ( v4 != 3 )
goto LABEL_4;
LABEL_7:
v5 = j;
result = set_pixel(new_img_addr, i, v5, 1);
}
LABEL_8:
;
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
int i; // er12
int j; // ebx
int v2; // edx
__int64 result; // rax
int v4; // ebp
int v5; // edx

for ( i = 0; i != 160; ++i )
{
for ( j = 0; j != 120; ++j )
{
while ( 1 )
{
v4 = func4(old_img_addr, i, j);
if ( !func0(old_img_addr, i, j) )
break;
if ( (unsigned int)(v4 - 2) <= 1 )
goto LABEL_7;
LABEL_4:
v2 = j++;
result = func1(new_img_addr, i, v2, 0);
if ( j == 120 )
goto LABEL_8;
}
if ( v4 != 3 )
goto LABEL_4;
LABEL_7:
v5 = j;
result = func1(new_img_addr, i, v5, 1);
}
LABEL_8:
;
}
return result;
}

original_code:
void game_of_life() {
 int x;
 int y;
 for(x = 0; x < 640/4; ++x) {
  for(y = 0; y < 480/4; ++y) {
   int n = neighbors(old_img_addr, x, y);
   if(get_pixel(old_img_addr, x, y)) {
    if((n < 2) || (n > 3)) {
     set_pixel(new_img_addr, x, y, 0);
    }
    else {
     set_pixel(new_img_addr, x, y, 1);
    }
   }
   else {
    if(n == 3) {
     set_pixel(new_img_addr, x, y, 1);
    }
    else {
     set_pixel(new_img_addr, x, y, 0);
    }
   }
  }
 }
}


decompiled_code:
__int64 __fastcall draw_gun(int a1, int a2)
{
set_pixel(old_img_addr, a1, a2 + 5, 1);
set_pixel(old_img_addr, a1, a2 + 6, 1);
set_pixel(old_img_addr, a1 + 1, a2 + 5, 1);
set_pixel(old_img_addr, a1 + 1, a2 + 6, 1);
set_pixel(old_img_addr, a1 + 11, a2 + 5, 1);
set_pixel(old_img_addr, a1 + 11, a2 + 6, 1);
set_pixel(old_img_addr, a1 + 11, a2 + 7, 1);
set_pixel(old_img_addr, a1 + 12, a2 + 4, 1);
set_pixel(old_img_addr, a1 + 12, a2 + 8, 1);
set_pixel(old_img_addr, a1 + 13, a2 + 3, 1);
set_pixel(old_img_addr, a1 + 13, a2 + 9, 1);
set_pixel(old_img_addr, a1 + 14, a2 + 3, 1);
set_pixel(old_img_addr, a1 + 14, a2 + 9, 1);
set_pixel(old_img_addr, a1 + 15, a2 + 6, 1);
set_pixel(old_img_addr, a1 + 16, a2 + 4, 1);
set_pixel(old_img_addr, a1 + 16, a2 + 8, 1);
set_pixel(old_img_addr, a1 + 17, a2 + 5, 1);
set_pixel(old_img_addr, a1 + 17, a2 + 6, 1);
set_pixel(old_img_addr, a1 + 17, a2 + 7, 1);
set_pixel(old_img_addr, a1 + 18, a2 + 6, 1);
set_pixel(old_img_addr, a1 + 21, a2 + 3, 1);
set_pixel(old_img_addr, a1 + 21, a2 + 4, 1);
set_pixel(old_img_addr, a1 + 21, a2 + 5, 1);
set_pixel(old_img_addr, a1 + 22, a2 + 3, 1);
set_pixel(old_img_addr, a1 + 22, a2 + 4, 1);
set_pixel(old_img_addr, a1 + 22, a2 + 5, 1);
set_pixel(old_img_addr, a1 + 23, a2 + 2, 1);
set_pixel(old_img_addr, a1 + 23, a2 + 6, 1);
set_pixel(old_img_addr, a1 + 25, a2 + 1, 1);
set_pixel(old_img_addr, a1 + 25, a2 + 2, 1);
set_pixel(old_img_addr, a1 + 25, a2 + 6, 1);
set_pixel(old_img_addr, a1 + 25, a2 + 7, 1);
set_pixel(old_img_addr, a1 + 35, a2 + 3, 1);
set_pixel(old_img_addr, a1 + 35, a2 + 4, 1);
set_pixel(old_img_addr, a1 + 36, a2 + 3, 1);
return set_pixel(old_img_addr, a1 + 36, a2 + 4, 1);
}

original_code:
void draw_gun(int x, int y) {
 set_pixel(old_img_addr, x + 0, y + 5, 1);
 set_pixel(old_img_addr, x + 0, y + 6, 1);
 set_pixel(old_img_addr, x + 1, y + 5, 1);
 set_pixel(old_img_addr, x + 1, y + 6, 1);
 set_pixel(old_img_addr, x + 11, y + 5, 1);
 set_pixel(old_img_addr, x + 11, y + 6, 1);
 set_pixel(old_img_addr, x + 11, y + 7, 1);
 set_pixel(old_img_addr, x + 12, y + 4, 1);
 set_pixel(old_img_addr, x + 12, y + 8, 1);
 set_pixel(old_img_addr, x + 13, y + 3, 1);
 set_pixel(old_img_addr, x + 13, y + 9, 1);
 set_pixel(old_img_addr, x + 14, y + 3, 1);
 set_pixel(old_img_addr, x + 14, y + 9, 1);
 set_pixel(old_img_addr, x + 15, y + 6, 1);
 set_pixel(old_img_addr, x + 16, y + 4, 1);
 set_pixel(old_img_addr, x + 16, y + 8, 1);
 set_pixel(old_img_addr, x + 17, y + 5, 1);
 set_pixel(old_img_addr, x + 17, y + 6, 1);
 set_pixel(old_img_addr, x + 17, y + 7, 1);
 set_pixel(old_img_addr, x + 18, y + 6, 1);
 set_pixel(old_img_addr, x + 21, y + 3, 1);
 set_pixel(old_img_addr, x + 21, y + 4, 1);
 set_pixel(old_img_addr, x + 21, y + 5, 1);
 set_pixel(old_img_addr, x + 22, y + 3, 1);
 set_pixel(old_img_addr, x + 22, y + 4, 1);
 set_pixel(old_img_addr, x + 22, y + 5, 1);
 set_pixel(old_img_addr, x + 23, y + 2, 1);
 set_pixel(old_img_addr, x + 23, y + 6, 1);
 set_pixel(old_img_addr, x + 25, y + 1, 1);
 set_pixel(old_img_addr, x + 25, y + 2, 1);
 set_pixel(old_img_addr, x + 25, y + 6, 1);
 set_pixel(old_img_addr, x + 25, y + 7, 1);
 set_pixel(old_img_addr, x + 35, y + 3, 1);
 set_pixel(old_img_addr, x + 35, y + 4, 1);
 set_pixel(old_img_addr, x + 36, y + 3, 1);
 set_pixel(old_img_addr, x + 36, y + 4, 1);
}


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

result = (unsigned int)refresh;
if ( refresh )
{
refresh = 0;
puts("vga_isr: double buffer flip\r");
result = write_mem(1939865600LL, (unsigned int)old_img_addr);
}
return result;
}

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

result = (unsigned int)refresh;
if ( refresh )
{
refresh = 0;
puts("vga_isr: double buffer flip\r");
result = write_mem(1939865600LL, (unsigned int)old_img_addr);
}
return result;
}

original_code:
void vga_isr() {
 if(refresh) {
  refresh = 0;
  printf("vga_isr: double buffer flip\r\n");
  write_mem(0x73A00000 + 0x0, old_img_addr);
 }
}


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

puts("timer_0_isr\r");
game_of_life();
result = (unsigned int)old_img_addr;
refresh = 1;
old_img_addr = new_img_addr;
new_img_addr = result;
return result;
}

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

puts("timer_0_isr\r");
func5();
result = (unsigned int)old_img_addr;
refresh = 1;
old_img_addr = new_img_addr;
new_img_addr = result;
return result;
}

original_code:
void timer_0_isr() {
 printf("timer_0_isr\r\n");
 game_of_life();
 uint32_t tmp = old_img_addr;
 old_img_addr = new_img_addr;
 new_img_addr = tmp;
 refresh = 1;
}

canonical_original_code:
void timer_0_isr() {
 printf("timer_0_isr\r\n");
 game_of_life();
 unsigned int tmp = old_img_addr;
 old_img_addr = new_img_addr;
 new_img_addr = tmp;
 refresh = 1;
}

variable_types:
tmp
unsigned int


decompiled_code:
int timer_1_isr()
{
return puts("timer_1_isr");
}

canonical_decompiled_code:
int func10()
{
return puts("timer_1_isr");
}

original_code:
void timer_1_isr() {
 printf("timer_1_isr\n");
}


main

repo: masriamir/vect

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // r12
unsigned int v4; // ebx
__int64 v5; // rdx
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // eax
unsigned int v9; // eax
unsigned int v10; // eax
unsigned int v11; // eax
const char *v12; // rax

v3 = (void *)new(argc, argv, envp);
debug(v3);
push(v3, "e1");
push(v3, "e2");
debug(v3);
pop(v3);
debug(v3);
push(v3, "e3");
push(v3, "e4");
debug(v3);
clear(v3);
debug(v3);
push(v3, "e5");
push(v3, "e6");
debug(v3);
push(v3, "e7");
push(v3, "e8");
push(v3, "e9");
debug(v3);
push(v3, "e10");
push(v3, "e11");
push(v3, "e12");
debug(v3);
pop(v3);
pop(v3);
push(v3, "e13");
debug(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
debug(v3);
v4 = 0;
clear(v3);
debug(v3);
do
{
v5 = v4++;
sprintf(0LL, "e%d", v5);
push(v3, 0LL);
}
while ( v4 != 36 );
debug(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
pop(v3);
debug(v3);
v6 = find(v3, "e6");
replace(v3, v6, "e93");
v7 = find(v3, "e11");
replace(v3, v7, "");
replace(v3, 0LL, "e44");
replace(v3, 4LL, "e44");
replace(v3, 12LL, "e44");
debug(v3);
v8 = find_n(v3, "e881");
printf("# of 'e881': %d\n", v8);
v9 = find_n(v3, "e12");
printf("# of 'e12': %d\n", v9);
v10 = find_n(v3, "");
printf("# of '': %d\n", v10);
v11 = find_n(v3, "e44");
printf("# of 'e44': %d\n\n", v11);
v12 = (const char *)get(v3, 7LL);
printf("v[7]: %s\n", v12);
free(v3);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
 Vector *v = new();
 debug(v);
 push(v, "e1");
 push(v, "e2");
 debug(v);
 pop(v);
 debug(v);
 push(v, "e3");
 push(v, "e4");
 debug(v);
 clear(v);
 debug(v);
 push(v, "e5");
 push(v, "e6");
 debug(v);
 push(v, "e7");
 push(v, "e8");
 push(v, "e9");
 debug(v);
 push(v, "e10");
 push(v, "e11");
 push(v, "e12");
 debug(v);
 pop(v);
 pop(v);
 push(v, "e13");
 debug(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 debug(v);
 clear(v);
 debug(v);
 for (int i = 0; i < 128 / 4 + 4; ++ i) {
  char *buf;
  sprintf(buf, "e%d", i);
  push(v, buf);
 }
 debug(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 debug(v);
 replace(v, find(v, "e6"), "e93");
 replace(v, find(v, "e11"), "");
 replace(v, 0, "e44");
 replace(v, 4, "e44");
 replace(v, 12, "e44");
 debug(v);
 printf("# of 'e881': %d\n", find_n(v, "e881"));
 printf("# of 'e12': %d\n", find_n(v, "e12"));
 printf("# of '': %d\n", find_n(v, ""));
 printf("# of 'e44': %d\n\n", find_n(v, "e44"));
 char *e7 = get(v, 7);
 printf("v[7]: %s\n", e7);
 free(v);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 struct Vector *v = new();
 debug(v);
 push(v, "e1");
 push(v, "e2");
 debug(v);
 pop(v);
 debug(v);
 push(v, "e3");
 push(v, "e4");
 debug(v);
 clear(v);
 debug(v);
 push(v, "e5");
 push(v, "e6");
 debug(v);
 push(v, "e7");
 push(v, "e8");
 push(v, "e9");
 debug(v);
 push(v, "e10");
 push(v, "e11");
 push(v, "e12");
 debug(v);
 pop(v);
 pop(v);
 push(v, "e13");
 debug(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 debug(v);
 clear(v);
 debug(v);
 for (int i = 0; i < 128 / 4 + 4; ++ i) {
  char *buf;
  sprintf(buf, "e%d", i);
  push(v, buf);
 }
 debug(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 pop(v);
 debug(v);
 replace(v, find(v, "e6"), "e93");
 replace(v, find(v, "e11"), "");
 replace(v, 0, "e44");
 replace(v, 4, "e44");
 replace(v, 12, "e44");
 debug(v);
 printf("# of 'e881': %d\n", find_n(v, "e881"));
 printf("# of 'e12': %d\n", find_n(v, "e12"));
 printf("# of '': %d\n", find_n(v, ""));
 printf("# of 'e44': %d\n\n", find_n(v, "e44"));
 char *e7 = get(v, 7);
 printf("v[7]: %s\n", e7);
 free(v);
 return 0;
}

variable_types:
argv
char *[]

variable_types: struct Vector { int size; int total; char **elements; } *


expand

decompiled_code:
char __fastcall expand(__int64 a1)
{
int v1; // edx
char result; // al
void *v3; // rbp

v1 = *(_DWORD *)(a1 + 4);
result = (double)*(int *)a1 == (double)v1 * 0.75;
if ( (double)*(int *)a1 == (double)v1 * 0.75 && v1 <= 127 )
{
v3 = realloc(*(void **)(a1 + 8), 8LL * (v1 + 8));
result = check_null(v3);
*(_DWORD *)(a1 + 4) += 8;
*(_QWORD *)(a1 + 8) = v3;
}
return result;
}

canonical_decompiled_code:
char __fastcall func7(__int64 a1)
{
int v1; // edx
char result; // al
void *v3; // rbp

v1 = *(_DWORD *)(a1 + 4);
result = (double)*(int *)a1 == (double)v1 * 0.75;
if ( (double)*(int *)a1 == (double)v1 * 0.75 && v1 <= 127 )
{
v3 = realloc(*(void **)(a1 + 8), 8LL * (v1 + 8));
result = func23(v3);
*(_DWORD *)(a1 + 4) += 8;
*(_QWORD *)(a1 + 8) = v3;
}
return result;
}

original_code:
static void expand(Vector *v) {
 if (v->size == v->total * 0.75 && v->total < 128) {
  char **new_elements = realloc(v->elements, (v->total + 8) * sizeof(char *));
  check_null(new_elements);
  v->elements = new_elements;
  v->total += 8;
 }
}

canonical_original_code:
static void expand(struct Vector *v) {
 if (v->size == v->total * 0.75 && v->total < 128) {
  char **new_elements = realloc(v->elements, (v->total + 8) * sizeof(char *));
  check_null(new_elements);
  v->elements = new_elements;
  v->total += 8;
 }
}

user_defined_types: struct Vector { int size; int total; char **elements; }


shrink

decompiled_code:
char __fastcall shrink(__int64 a1)
{
int v1; // edx
char result; // al
void *v3; // rbp

v1 = *(_DWORD *)(a1 + 4);
result = (double)*(int *)a1 == (double)v1 * 0.5;
if ( (double)*(int *)a1 == (double)v1 * 0.5 && v1 > 8 )
{
v3 = realloc(*(void **)(a1 + 8), 8LL * (v1 - 8));
result = check_null(v3);
*(_DWORD *)(a1 + 4) -= 8;
*(_QWORD *)(a1 + 8) = v3;
}
return result;
}

canonical_decompiled_code:
char __fastcall func8(__int64 a1)
{
int v1; // edx
char result; // al
void *v3; // rbp

v1 = *(_DWORD *)(a1 + 4);
result = (double)*(int *)a1 == (double)v1 * 0.5;
if ( (double)*(int *)a1 == (double)v1 * 0.5 && v1 > 8 )
{
v3 = realloc(*(void **)(a1 + 8), 8LL * (v1 - 8));
result = func23(v3);
*(_DWORD *)(a1 + 4) -= 8;
*(_QWORD *)(a1 + 8) = v3;
}
return result;
}

original_code:
static void shrink(Vector *v) {
 if (v->size == v->total * 0.50 && v->total > 8) {
  char **new_elements = realloc(v->elements, (v->total - 8) * sizeof(char *));
  check_null(new_elements);
  v->elements = new_elements;
  v->total -= 8;
 }
}

canonical_original_code:
static void shrink(struct Vector *v) {
 if (v->size == v->total * 0.50 && v->total > 8) {
  char **new_elements = realloc(v->elements, (v->total - 8) * sizeof(char *));
  check_null(new_elements);
  v->elements = new_elements;
  v->total -= 8;
 }
}


new

decompiled_code:
_QWORD *new()
{
_QWORD *v0; // r12
void *v1; // rbx

v0 = calloc(1uLL, 0x10uLL);
check_null(v0);
v1 = calloc(8uLL, 8uLL);
check_null(v1);
v0[1] = v1;
*v0 = 0x800000000LL;
return v0;
}

canonical_decompiled_code:
_QWORD *func9()
{
_QWORD *v0; // r12
void *v1; // rbx

v0 = calloc(1uLL, 0x10uLL);
func23(v0);
v1 = calloc(8uLL, 8uLL);
func23(v1);
v0[1] = v1;
*v0 = 0x800000000LL;
return v0;
}

original_code:
Vector *new() {
 Vector *v = calloc(1, sizeof(Vector));
 check_null(v);
 char **elements = calloc(8, sizeof(char *));
 check_null(elements);
 v->size = 0;
 v->total = 8;
 v->elements = elements;
 return v;
}

canonical_original_code:
struct Vector *new() {
 struct Vector *v = calloc(1, sizeof(Vector));
 check_null(v);
 char **elements = calloc(8, sizeof(char *));
 check_null(elements);
 v->size = 0;
 v->total = 8;
 v->elements = elements;
 return v;
}

variable_types:
elements
char **

return_type: struct Vector { int size; int total; char **elements; } *


decompiled_code:
bool __fastcall is_empty(_DWORD *a1)
{
return *a1 <= 0;
}

canonical_decompiled_code:
bool __fastcall func10(_DWORD *a1)
{
return *a1 <= 0;
}

original_code:
_Bool is_empty(const Vector *v) {
 return v->size <= 0;
}

canonical_original_code:
_Bool is_empty(struct Vector *v) {
 return v->size <= 0;
}


decompiled_code:
bool __fastcall is_full(_DWORD *a1)
{
return *a1 == 128;
}

canonical_decompiled_code:
bool __fastcall func11(_DWORD *a1)
{
return *a1 == 128;
}

original_code:
_Bool is_full(const Vector *v) {
 return v->size == 128;
}

canonical_original_code:
_Bool is_full(struct Vector *v) {
 return v->size == 128;
}


push

decompiled_code:
char __fastcall push(int *a1, const char *a2)
{
char result; // al
char *v3; // rbx
int v4; // eax

check_null(a1);
result = is_full(a1);
if ( !result )
{
v3 = (char *)calloc(8uLL, 1uLL);
check_null(v3);
strcpy(v3, a2);
v4 = *a1;
*(_QWORD *)(*((_QWORD *)a1 + 1) + 8LL * *a1) = v3;
*a1 = v4 + 1;
result = expand((__int64)a1);
}
return result;
}

canonical_decompiled_code:
char __fastcall func12(int *a1, const char *a2)
{
char result; // al
char *v3; // rbx
int v4; // eax

func23(a1);
result = func11(a1);
if ( !result )
{
v3 = (char *)calloc(8uLL, 1uLL);
func23(v3);
strcpy(v3, a2);
v4 = *a1;
*(_QWORD *)(*((_QWORD *)a1 + 1) + 8LL * *a1) = v3;
*a1 = v4 + 1;
result = func7((__int64)a1);
}
return result;
}

original_code:
void push(Vector *v, const char *data) {
 check_null(v);
 if (!is_full(v)) {
  char *data_ptr = calloc(sizeof(data), sizeof(char));
  check_null(data_ptr);
  strcpy(data_ptr, data);
  v->elements[v->size] = data_ptr;
  v->size ++;
  expand(v);
 }
}

canonical_original_code:
void push(struct Vector *v, char *data) {
 check_null(v);
 if (!is_full(v)) {
  char *data_ptr = calloc(sizeof(data), sizeof(char));
  check_null(data_ptr);
  strcpy(data_ptr, data);
  v->elements[v->size] = data_ptr;
  v->size ++;
  expand(v);
 }
}

variable_types:
data
char *


pop

decompiled_code:
char __fastcall pop(int *a1)
{
char result; // al
__int64 v2; // rcx

check_null(a1);
result = is_empty(a1);
if ( !result )
{
v2 = *a1;
*(_QWORD *)(*((_QWORD *)a1 + 1) + 8 * v2 - 8) = 0LL;
*a1 = v2 - 1;
result = shrink((__int64)a1);
}
return result;
}

canonical_decompiled_code:
char __fastcall func13(int *a1)
{
char result; // al
__int64 v2; // rcx

func23(a1);
result = func10(a1);
if ( !result )
{
v2 = *a1;
*(_QWORD *)(*((_QWORD *)a1 + 1) + 8 * v2 - 8) = 0LL;
*a1 = v2 - 1;
result = func8((__int64)a1);
}
return result;
}

original_code:
void pop(Vector *v) {
 check_null(v);
 if (!is_empty(v)) {
  v->elements[v->size - 1] = '\0';
  v->size --;
  shrink(v);
 }
}

canonical_original_code:
void pop(struct Vector *v) {
 check_null(v);
 if (!is_empty(v)) {
  v->elements[v->size - 1] = '\0';
  v->size --;
  shrink(v);
 }
}


get

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

check_null(a1);
check_index(*a1, a2);
if ( is_empty(a1) )
result = 0LL;
else
result = *(_QWORD *)(*((_QWORD *)a1 + 1) + 8LL * (int)a2);
return result;
}

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

func23(a1);
func24(*a1, a2);
if ( func10(a1) )
result = 0LL;
else
result = *(_QWORD *)(*((_QWORD *)a1 + 1) + 8LL * (int)a2);
return result;
}

original_code:
char *get(const Vector *v, const int index) {
 check_null(v);
 check_index(v->size, index);
 if (is_empty(v)) {
  return ((void *)0);
 }
 return v->elements[index];
}

canonical_original_code:
char *get(struct Vector *v, int index) {
 check_null(v);
 check_index(v->size, index);
 if (is_empty(v)) {
  return ((void *)0);
 }
 return v->elements[index];
}

variable_types: index int

return_type: char *


find

decompiled_code:
__int64 __fastcall find(int *a1, const char *a2)
{
__int64 v2; // r12
__int64 v3; // r14
__int64 v4; // rbx

check_null(a1);
check_null(a2);
v2 = *a1;
if ( (int)v2 <= 0 )
return 0xFFFFFFFFLL;
v3 = *((_QWORD *)a1 + 1);
v4 = 0LL;
while ( strcmp(*(const char **)(v3 + 8 * v4), a2) )
{
if ( ++v4 == v2 )
return 0xFFFFFFFFLL;
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func15(int *a1, const char *a2)
{
__int64 v2; // r12
__int64 v3; // r14
__int64 v4; // rbx

func23(a1);
func23(a2);
v2 = *a1;
if ( (int)v2 <= 0 )
return 0xFFFFFFFFLL;
v3 = *((_QWORD *)a1 + 1);
v4 = 0LL;
while ( strcmp(*(const char **)(v3 + 8 * v4), a2) )
{
if ( ++v4 == v2 )
return 0xFFFFFFFFLL;
}
return (unsigned int)v4;
}

original_code:
int find(const Vector *v, const char *data) {
 check_null(v);
 check_null(data);
 for (int i = 0; i < v->size; ++ i) {
  if (!strcmp(v->elements[i], data)) {
   return i;
  }
 }
 return -1;
}

canonical_original_code:
int find(struct Vector *v, char *data) {
 check_null(v);
 check_null(data);
 for (int i = 0; i < v->size; ++ i) {
  if (!strcmp(v->elements[i], data)) {
   return i;
  }
 }
 return -1;
}


decompiled_code:
__int64 __fastcall find_n(__int64 a1, const char *a2)
{
const char **v2; // rax
unsigned int v3; // er12
const char **v4; // rbx
__int64 v5; // r13
bool v6; // cf

check_null(a1);
check_null(a2);
if ( *(int *)a1 <= 0 )
return 0LL;
v2 = *(const char ***)(a1 + 8);
v3 = 0;
v4 = v2 + 1;
v5 = (__int64)&v2[(unsigned int)(*(_DWORD *)a1 - 1) + 1];
while ( 1 )
{
v6 = strcmp(*v2, a2) == 0;
v2 = v4;
v3 += v6;
if ( v4 == (const char **)v5 )
break;
++v4;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, const char *a2)
{
const char **v2; // rax
unsigned int v3; // er12
const char **v4; // rbx
__int64 v5; // r13
bool v6; // cf

func23(a1);
func23(a2);
if ( *(int *)a1 <= 0 )
return 0LL;
v2 = *(const char ***)(a1 + 8);
v3 = 0;
v4 = v2 + 1;
v5 = (__int64)&v2[(unsigned int)(*(_DWORD *)a1 - 1) + 1];
while ( 1 )
{
v6 = strcmp(*v2, a2) == 0;
v2 = v4;
v3 += v6;
if ( v4 == (const char **)v5 )
break;
++v4;
}
return v3;
}

original_code:
int find_n(const Vector *v, const char *data) {
 check_null(v);
 check_null(data);
 int count = 0;
 for (int i = 0; i < v->size; ++ i) {
  if (!strcmp(v->elements[i], data)) {
   count ++;;
  }
 }
 return count;
}

canonical_original_code:
int find_n(struct Vector *v, char *data) {
 check_null(v);
 check_null(data);
 int count = 0;
 for (int i = 0; i < v->size; ++ i) {
  if (!strcmp(v->elements[i], data)) {
   count ++;;
  }
 }
 return count;
}

variable_types: count int


replace

decompiled_code:
__int64 __fastcall replace(unsigned int *a1, unsigned int a2, const char *a3)
{
char *v4; // r12
__int64 result; // rax

check_null(a1);
check_null(a3);
check_index(*a1, a2);
v4 = (char *)calloc(8uLL, 1uLL);
check_null(v4);
strcpy(v4, a3);
free(*(void **)(*((_QWORD *)a1 + 1) + 8LL * (int)a2));
result = *((_QWORD *)a1 + 1);
*(_QWORD *)(result + 8LL * (int)a2) = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned int *a1, unsigned int a2, const char *a3)
{
char *v4; // r12
__int64 result; // rax

func23(a1);
func23(a3);
func24(*a1, a2);
v4 = (char *)calloc(8uLL, 1uLL);
func23(v4);
strcpy(v4, a3);
free(*(void **)(*((_QWORD *)a1 + 1) + 8LL * (int)a2));
result = *((_QWORD *)a1 + 1);
*(_QWORD *)(result + 8LL * (int)a2) = v4;
return result;
}

original_code:
void replace(Vector *v, const int index, const char *data) {
 check_null(v);
 check_null(data);
 check_index(v->size, index);
 char *data_ptr = calloc(sizeof(data), sizeof(char));
 check_null(data_ptr);
 strcpy(data_ptr, data);
 free(v->elements[index]);
 v->elements[index] = data_ptr;
}

canonical_original_code:
void replace(struct Vector *v, int index, char *data) {
 check_null(v);
 check_null(data);
 check_index(v->size, index);
 char *data_ptr = calloc(sizeof(data), sizeof(char));
 check_null(data_ptr);
 strcpy(data_ptr, data);
 free(v->elements[index]);
 v->elements[index] = data_ptr;
}


print

decompiled_code:
int __fastcall print(unsigned int *a1)
{
signed int v1; // ebx
const char *v2; // rax
signed int v3; // eax

check_null(a1);
if ( is_empty(a1) )
return puts("[]");
v1 = 0;
printf("[ ");
if ( (int)*a1 > 0 )
{
do
{
v2 = (const char *)get(a1, v1);
printf("%s", v2);
v3 = *a1;
if ( *a1 - 1 != v1 )
{
printf(", ");
v3 = *a1;
}
++v1;
}
while ( v1 < v3 );
}
return puts(" ]");
}

canonical_decompiled_code:
int __fastcall func19(unsigned int *a1)
{
signed int v1; // ebx
const char *v2; // rax
signed int v3; // eax

func23(a1);
if ( func10(a1) )
return puts("[]");
v1 = 0;
printf("[ ");
if ( (int)*a1 > 0 )
{
do
{
v2 = (const char *)func14(a1, v1);
printf("%s", v2);
v3 = *a1;
if ( *a1 - 1 != v1 )
{
printf(", ");
v3 = *a1;
}
++v1;
}
while ( v1 < v3 );
}
return puts(" ]");
}

original_code:
void print(const Vector *v) {
 check_null(v);
 if (!is_empty(v)) {
  printf("[ ");
  for (int i = 0; i < v->size; ++ i) {
   printf("%s", get(v, i));
   if (i != v->size - 1) {
    printf(", ");
   }
  }
  printf(" ]\n");
 } else {
  printf("[]\n");
 }
}

canonical_original_code:
void print(struct Vector *v) {
 check_null(v);
 if (!is_empty(v)) {
  printf("[ ");
  for (int i = 0; i < v->size; ++ i) {
   printf("%s", get(v, i));
   if (i != v->size - 1) {
    printf(", ");
   }
  }
  printf(" ]\n");
 } else {
  printf("[]\n");
 }
}


decompiled_code:
bool __fastcall is_null(__int64 a1)
{
return a1 == 0;
}

canonical_decompiled_code:
bool __fastcall func20(__int64 a1)
{
return a1 == 0;
}

original_code:
static _Bool is_null(const void *ptr) {
 return !ptr;
}

canonical_original_code:
static _Bool is_null(void *ptr) {
 return !ptr;
}

variable_types:
ptr
void *


decompiled_code:
__int64 __fastcall is_valid_index(int a1, int a2)
{
return (a2 >= 0) & (unsigned __int8)(a2 < a1);
}

canonical_decompiled_code:
__int64 __fastcall func21(int a1, int a2)
{
return (a2 >= 0) & (unsigned __int8)(a2 < a1);
}

original_code:
static _Bool is_valid_index(const int size, const int index) {
 return !(index < 0 || index >= size);
}

canonical_original_code:
static _Bool is_valid_index(int size, int index) {
 return !(index < 0 || index >= size);
}

variable_types: size int


error

decompiled_code:
void __fastcall __noreturn error(int a1)
{
const char *v1; // rdx

v1 = "Null pointer reference.";
if ( a1 == 1 )
v1 = "Index out of bounds.";
fprintf(stderr, "%s\n", v1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func22(int a1)
{
const char *v1; // rdx

v1 = "Null pointer reference.";
if ( a1 == 1 )
v1 = "Index out of bounds.";
fprintf(stderr, "%s\n", v1);
exit(1);
}

original_code:
static void error(const error_code e) {
 char *msg;
 switch (e) {
  case E_NULL:
   msg = "Null pointer reference.";
   break;
  case E_INDEX:
   msg = "Index out of bounds.";
   break;
  default:
   msg = "Unhandled error.";
 }
 fprintf(stderr, "%s\n", msg);
 exit(1);
}

canonical_original_code:
static void error(enum error_code e) {
 char *msg;
 switch (e) {
  case E_NULL:
   msg = "Null pointer reference.";
   break;
  case E_INDEX:
   msg = "Index out of bounds.";
   break;
  default:
   msg = "Unhandled error.";
 }
 fprintf(stderr, "%s\n", msg);
 exit(1);
}

variable_types:
e
enum error_code { E_NULL=0, E_INDEX=1 }

variable_types:
msg
char *


decompiled_code:
bool __fastcall check_null(__int64 a1)
{
bool result; // al

result = is_null(a1);
if ( result )
error(0);
return result;
}

canonical_decompiled_code:
bool __fastcall func23(__int64 a1)
{
bool result; // al

result = func20(a1);
if ( result )
func22(0);
return result;
}

original_code:
void check_null(const void *ptr) {
 if (is_null(ptr)) {
  error(E_NULL);
 }
}

canonical_original_code:
void check_null(void *ptr) {
 if (is_null(ptr)) {
  error(E_NULL);
 }
}


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

result = is_valid_index(a1, a2);
if ( !(_BYTE)result )
error(1);
return result;
}

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

result = func21(a1, a2);
if ( !(_BYTE)result )
func22(1);
return result;
}

original_code:
void check_index(const int size, const int index) {
 if (!is_valid_index(size, index)) {
  error(E_INDEX);
 }
}

canonical_original_code:
void check_index(int size, int index) {
 if (!is_valid_index(size, index)) {
  error(E_INDEX);
 }
}


debug

decompiled_code:
int __fastcall debug(unsigned int *a1)
{
print(a1);
return printf("Size: %d, Total: %d\n\n", *a1, a1[1]);
}

canonical_decompiled_code:
int __fastcall func25(unsigned int *a1)
{
func19(a1);
return printf("Size: %d, Total: %d\n\n", *a1, a1[1]);
}

original_code:
void debug(const Vector *v) {
 print(v);
 printf("Size: %d, Total: %d\n\n", v->size, v->total);
}

canonical_original_code:
void debug(struct Vector *v) {
 print(v);
 printf("Size: %d, Total: %d\n\n", v->size, v->total);
}


repo: massar/ecmh

decompiled_code:
void *listnode_new()
{
return calloc(1uLL, 0x18uLL);
}

canonical_decompiled_code:
void *func0()
{
return calloc(1uLL, 0x18uLL);
}

original_code:
static struct listnode *listnode_new(void)
{
 struct listnode *node;
 node = calloc(1, sizeof(*node));
 if (!node) return ((void *)0);
 return node;
}

canonical_original_code:
static struct listnode *listnode_new()
{
 struct listnode *node;
 node = calloc(1, sizeof(*node));
 if (!node) return ((void *)0);
 return node;
}

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


decompiled_code:
// attributes: thunk
void listnode_free(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func1(void *ptr)
{
free(ptr);
}

original_code:
static void listnode_free(struct listnode *node)
{
 free(node);
}

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