original_code:
void qr_bitmap_render(const struct qr_bitmap * bmp,
                      void * buffer,
                      int mod_bits,
                      long line_stride,
                      int line_repeat,
                      unsigned long mark,
                      unsigned long space)
{
        unsigned char * out;
        const unsigned char * in;
        size_t n, dim;
        int pack;
        pack = (mod_bits < 8);
        ((!pack || (8 % mod_bits == 0)) ? (void) (0) : __assert_fail ("!pack || (CHAR_BIT % mod_bits == 0)", "bitmap.c", 181, __extension__ __PRETTY_FUNCTION__));
        ((pack || (mod_bits % 8 == 0)) ? (void) (0) : __assert_fail ("pack || (mod_bits % CHAR_BIT == 0)", "bitmap.c", 182, __extension__ __PRETTY_FUNCTION__));
        in = bmp->bits;
        out = buffer;
        dim = bmp->width;
        if (pack) {
                mark &= (1 << mod_bits) - 1;
                space &= (1 << mod_bits) - 1;
        }
        n = dim;
        while (n-- > 0) {
                size_t rpt;
                unsigned char * next;
                if (pack)
                        render_line_2(out, in, mod_bits, dim, mark, space);
                else
                        render_line_1(out, in, mod_bits, dim, mark, space);
                rpt = line_repeat;
                next = out + line_stride;
                while (--rpt > 0) {
                        size_t bits = dim * mod_bits;
                        size_t bytes = bits / 8 + !!(bits % 8);
                        memcpy(next, out, bytes);
                        next += line_stride;
                }
                in += bmp->stride;
                out = next;
        }
}

canonical_original_code:
void qr_bitmap_render(struct qr_bitmap *bmp, void *buffer, int mod_bits, long line_stride, int line_repeat, unsigned long mark, unsigned long space)
{
        unsigned char *out;
        unsigned char *in;
        unsigned long n; unsigned long dim;
        int pack;
        pack = (mod_bits < 8);
        ((!pack || (8 % mod_bits == 0)) ? (void) (0) : __assert_fail ("!pack || (CHAR_BIT % mod_bits == 0)", "bitmap.c", 181, __extension__ __PRETTY_FUNCTION__));
        ((pack || (mod_bits % 8 == 0)) ? (void) (0) : __assert_fail ("pack || (mod_bits % CHAR_BIT == 0)", "bitmap.c", 182, __extension__ __PRETTY_FUNCTION__));
        in = bmp->bits;
        out = buffer;
        dim = bmp->width;
        if (pack) {
                mark &= (1 << mod_bits) - 1;
                space &= (1 << mod_bits) - 1;
        }
        n = dim;
        while (n-- > 0) {
                unsigned long rpt;
                unsigned char *next;
                if (pack)
                        render_line_2(out, in, mod_bits, dim, mark, space);
                else
                        render_line_1(out, in, mod_bits, dim, mark, space);
                rpt = line_repeat;
                next = out + line_stride;
                while (--rpt > 0) {
                        unsigned long bits = dim * mod_bits;
                        unsigned long bytes = bits / 8 + !!(bits % 8);
                        memcpy(next, out, bytes);
                        next += line_stride;
                }
                in += bmp->stride;
                out = next;
        }
}

variable_types:
buffer
void *

variable_types: pack int

variable_types:
rpt
unsigned long

variable_types:
next
unsigned char *

variable_types:
bits
unsigned long

variable_types:
bytes
unsigned long


repo: norisys/libqr

decompiled_code:
void __fastcall qr_code_destroy(void *ptr)
{
if ( ptr )
{
qr_bitmap_destroy(*((_QWORD *)ptr + 1));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
if ( ptr )
{
qr_bitmap_destroy(*((_QWORD *)ptr + 1));
free(ptr);
}
}

original_code:
void qr_code_destroy(struct qr_code * code)
{
        if (code) {
                qr_bitmap_destroy(code->modules);
                free(code);
        }
}

canonical_original_code:
void qr_code_destroy(struct qr_code *code)
{
        if (code) {
                qr_bitmap_destroy(code->modules);
                free(code);
        }
}

variable_types: struct qr_code { int version; struct qr_bitmap { unsigned char *bits; unsigned char *mask; unsigned long stride; unsigned long width; } *modules; } *

return_type: void

user_defined_types: struct qr_code { int version; struct qr_bitmap *modules; }


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

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

original_code:
int qr_code_width(const struct qr_code * code)
{
        return code->version * 4 + 17;
}

canonical_original_code:
int qr_code_width(struct qr_code *code)
{
        return code->version * 4 + 17;
}

return_type: int

user_defined_types: struct qr_bitmap { unsigned char *bits; unsigned char *mask; unsigned long stride; unsigned long width; }


original_code:
size_t qr_code_total_capacity(int version)
{
 int side = version * 4 + 17;
 int alignment_side = version > 1 ? (version / 7) + 2 : 0;
 int alignment_count = alignment_side >= 2 ?
  alignment_side * alignment_side - 3 : 0;
 int locator_bits = 8*8*3;
 int format_bits = 8*4 - 1 + (version >= 7 ? 6*3*2 : 0);
 int timing_bits = 2 * (side - 8*2 -
  (alignment_side > 2 ? (alignment_side - 2) * 5 : 0));
 int function_bits = timing_bits + format_bits + locator_bits
  + alignment_count * 5*5;
 return side * side - function_bits;
}

canonical_original_code:
unsigned long qr_code_total_capacity(int version)
{
 int side = version * 4 + 17;
 int alignment_side = version > 1 ? (version / 7) + 2 : 0;
 int alignment_count = alignment_side >= 2 ?
  alignment_side * alignment_side - 3 : 0;
 int locator_bits = 8*8*3;
 int format_bits = 8*4 - 1 + (version >= 7 ? 6*3*2 : 0);
 int timing_bits = 2 * (side - 8*2 -
  (alignment_side > 2 ? (alignment_side - 2) * 5 : 0));
 int function_bits = timing_bits + format_bits + locator_bits
  + alignment_count * 5*5;
 return side * side - function_bits;
}

variable_types: version int

variable_types: side int

return_type: unsigned long


decompiled_code:
__int64 __fastcall qr_get_rs_block_sizes(int a1, int a2, __int64 a3, int *a4, int *a5)
{
unsigned __int64 v7; // rax
_DWORD *v8; // r11
__int64 v9; // r10
__int64 v10; // rdx
int v11; // edi
int v12; // ecx
int v13; // esi
int v14; // esi
int v15; // ecx
__int64 result; // rax

v7 = qr_code_total_capacity(a1);
v10 = (a2 ^ 1u) + 4 * v9;
v11 = *((_DWORD *)&QR_DATA_WORD_COUNT + v10);
v12 = *((_DWORD *)&QR_RS_BLOCK_COUNT + 2 * v10);
v13 = v7 >> 3;
LODWORD(v7) = *((_DWORD *)&QR_RS_BLOCK_COUNT + 2 * v10 + 1);
*v8 = v12;
v14 = v13 - v11;
v8[1] = v7;
v15 = v7 + v12;
LODWORD(v10) = v11 / v15 + 1;
*a4 = v11 / v15;
a4[1] = v10;
if ( v8[1] * (_DWORD)v10 + *v8 * (v11 / v15) != v11 )
_assert_fail(
"data_length[0] * block_count[0] + data_length[1] * block_count[1] == data_words",
"code-common.c",
0x43u,
"qr_get_rs_block_sizes");
a5[1] = v14 / v15;
*a5 = v14 / v15;
result = (unsigned int)(v14 / v15 * *v8 + v8[1] * (v14 / v15));
if ( (_DWORD)result != v14 )
_assert_fail(
"ec_length[0] * block_count[0] + ec_length[1] * block_count[1] == ec_words",
"code-common.c",
0x48u,
"qr_get_rs_block_sizes");
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2, __int64 a3, int *a4, int *a5)
{
unsigned __int64 v7; // rax
_DWORD *v8; // r11
__int64 v9; // r10
__int64 v10; // rdx
int v11; // edi
int v12; // ecx
int v13; // esi
int v14; // esi
int v15; // ecx
__int64 result; // rax

v7 = func2(a1);
v10 = (a2 ^ 1u) + 4 * v9;
v11 = *((_DWORD *)&QR_DATA_WORD_COUNT + v10);
v12 = *((_DWORD *)&QR_RS_BLOCK_COUNT + 2 * v10);
v13 = v7 >> 3;
LODWORD(v7) = *((_DWORD *)&QR_RS_BLOCK_COUNT + 2 * v10 + 1);
*v8 = v12;
v14 = v13 - v11;
v8[1] = v7;
v15 = v7 + v12;
LODWORD(v10) = v11 / v15 + 1;
*a4 = v11 / v15;
a4[1] = v10;
if ( v8[1] * (_DWORD)v10 + *v8 * (v11 / v15) != v11 )
_assert_fail(
"data_length[0] * block_count[0] + data_length[1] * block_count[1] == data_words",
"code-common.c",
0x43u,
"qr_get_rs_block_sizes");
a5[1] = v14 / v15;
*a5 = v14 / v15;
result = (unsigned int)(v14 / v15 * *v8 + v8[1] * (v14 / v15));
if ( (_DWORD)result != v14 )
_assert_fail(
"ec_length[0] * block_count[0] + ec_length[1] * block_count[1] == ec_words",
"code-common.c",
0x48u,
"qr_get_rs_block_sizes");
return result;
}

original_code:
void qr_get_rs_block_sizes(int version,
                           enum qr_ec_level ec,
                           int block_count[2],
                           int data_length[2],
                           int ec_length[2])
{
        int total_words = qr_code_total_capacity(version) / 8;
        int data_words = QR_DATA_WORD_COUNT[version - 1][ec ^ 0x1];
        int ec_words = total_words - data_words;
        int total_blocks;
        block_count[0] = QR_RS_BLOCK_COUNT[version - 1][ec ^ 0x1][0];
        block_count[1] = QR_RS_BLOCK_COUNT[version - 1][ec ^ 0x1][1];
        total_blocks = block_count[0] + block_count[1];
        data_length[0] = data_words / total_blocks;
        data_length[1] = data_length[0] + 1;
        ((data_length[0] * block_count[0] + data_length[1] * block_count[1] == data_words) ? (void) (0) : __assert_fail ("data_length[0] * block_count[0] + data_length[1] * block_count[1] == data_words", "code-common.c", 67, __extension__ __PRETTY_FUNCTION__));
        ec_length[0] = ec_length[1] = ec_words / total_blocks;
        ((ec_length[0] * block_count[0] + ec_length[1] * block_count[1] == ec_words) ? (void) (0) : __assert_fail ("ec_length[0] * block_count[0] + ec_length[1] * block_count[1] == ec_words", "code-common.c", 72, __extension__ __PRETTY_FUNCTION__));
        ((data_words + ec_words == total_words) ? (void) (0) : __assert_fail ("data_words + ec_words == total_words", "code-common.c", 74, __extension__ __PRETTY_FUNCTION__));
}

variable_types: enum qr_ec_level { QR_EC_LEVEL_L=1, QR_EC_LEVEL_M=0, QR_EC_LEVEL_Q=3, QR_EC_LEVEL_H=2 }


decompiled_code:
void __fastcall qr_mask_apply(__int64 *a1, int a2)
{
unsigned __int64 i; // r11
__int64 v5; // r10
__int64 v6; // rbp
unsigned __int64 v7; // r9
unsigned __int64 v8; // r8
unsigned __int64 v9; // rsi
char v10; // cl
unsigned __int64 v11; // rax
unsigned __int64 v12; // rdi

if ( (a2 & 0xFFFFFFF8) != 0 )
_assert_fail("(mask & 0x7) == mask", "code-common.c", 0x51u, "qr_mask_apply");
for ( i = 0LL; a1[4] > i; ++i )
{
v5 = *a1;
v6 = i * a1[2];
if ( a1[3] )
{
v7 = i;
v8 = 0LL;
v9 = 0LL;
do
{
v10 = v9 & 7;
switch ( a2 )
{
case 1:
LODWORD(v11) = i & 1;
break;
case 2:
LODWORD(v11) = v9 - (v9 / 3 + (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)v9) >> 64) & 0xFFFFFFFE));
break;
case 3:
LODWORD(v11) = v7 - (v7 / 3 + (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)v7) >> 64) & 0xFFFFFFFE));
break;
case 4:
LODWORD(v11) = ((unsigned __int8)(i >> 1) + (unsigned __int8)(v9 / 3)) & 1;
break;
case 5:
LODWORD(v11) = (v8 & 1)
+ v8
- (v8 / 3
+ (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)v8) >> 64) & 0xFFFFFFFE));
break;
case 6:
v11 = (v8 / 3) & 1;
break;
case 7:
LODWORD(v11) = ((_BYTE)v7
+ (_BYTE)v8
- ((unsigned __int8)(v8 / 3)
+ (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)v8) >> 64) & 0xFE))) & 1;
break;
default:
LODWORD(v11) = v7 & 1;
break;
}
v12 = v6 + (v9++ >> 3);
v8 += i;
++v7;
*(_BYTE *)(v5 + v12) ^= ((_DWORD)v11 == 0) << v10;
}
while ( a1[3] > v9 );
}
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 *a1, int a2)
{
unsigned __int64 i; // r11
__int64 v5; // r10
__int64 v6; // rbp
unsigned __int64 v7; // r9
unsigned __int64 v8; // r8
unsigned __int64 v9; // rsi
char v10; // cl
unsigned __int64 v11; // rax
unsigned __int64 v12; // rdi

if ( (a2 & 0xFFFFFFF8) != 0 )
_assert_fail("(mask & 0x7) == mask", "code-common.c", 0x51u, "qr_mask_apply");
for ( i = 0LL; a1[4] > i; ++i )
{
v5 = *a1;
v6 = i * a1[2];
if ( a1[3] )
{
v7 = i;
v8 = 0LL;
v9 = 0LL;
do
{
v10 = v9 & 7;
switch ( a2 )
{
case 1:
LODWORD(v11) = i & 1;
break;
case 2:
LODWORD(v11) = v9 - (v9 / 3 + (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)v9) >> 64) & 0xFFFFFFFE));
break;
case 3:
LODWORD(v11) = v7 - (v7 / 3 + (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)v7) >> 64) & 0xFFFFFFFE));
break;
case 4:
LODWORD(v11) = ((unsigned __int8)(i >> 1) + (unsigned __int8)(v9 / 3)) & 1;
break;
case 5:
LODWORD(v11) = (v8 & 1)
+ v8
- (v8 / 3
+ (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)v8) >> 64) & 0xFFFFFFFE));
break;
case 6:
v11 = (v8 / 3) & 1;
break;
case 7:
LODWORD(v11) = ((_BYTE)v7
+ (_BYTE)v8
- ((unsigned __int8)(v8 / 3)
+ (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)v8) >> 64) & 0xFE))) & 1;
break;
default:
LODWORD(v11) = v7 & 1;
break;
}
v12 = v6 + (v9++ >> 3);
v8 += i;
++v7;
*(_BYTE *)(v5 + v12) ^= ((_DWORD)v11 == 0) << v10;
}
while ( a1[3] > v9 );
}
}
}

original_code:
void qr_mask_apply(struct qr_bitmap * bmp, int mask)
{
        size_t i, j;
        (((mask & 0x7) == mask) ? (void) (0) : __assert_fail ("(mask & 0x7) == mask", "code-common.c", 81, __extension__ __PRETTY_FUNCTION__));
        mask &= 0x7;
        for (i = 0; i < bmp->height; ++i) {
                unsigned char * p = bmp->bits + i * bmp->stride;
                for (j = 0; j < bmp->width; ++j) {
                        int bit = j % 8;
                        size_t off = j / 8;
                        int t;
                        switch (mask) {
                        case 0: t = (i + j) % 2; break;
                        case 1: t = i % 2; break;
                        case 2: t = j % 3; break;
                        case 3: t = (i + j) % 3; break;
                        case 4: t = (i/2 + j/3) % 2; break;
                        case 5: t = ((i*j) % 2) + ((i*j) % 3); break;
                        case 6: t = (((i*j) % 2) + ((i*j) % 3)) % 2; break;
                        case 7: t = (((i*j) % 3) + ((i+j) % 2)) % 2; break;
                        }
                        p[off] ^= (t == 0) << bit;
                }
        }
}

canonical_original_code:
void qr_mask_apply(struct qr_bitmap *bmp, int mask)
{
        unsigned long i; unsigned long j;
        (((mask & 0x7) == mask) ? (void) (0) : __assert_fail ("(mask & 0x7) == mask", "code-common.c", 81, __extension__ __PRETTY_FUNCTION__));
        mask &= 0x7;
        for (i = 0; i < bmp->height; ++i) {
                unsigned char *p = bmp->bits + i * bmp->stride;
                for (j = 0; j < bmp->width; ++j) {
                        int bit = j % 8;
                        unsigned long off = j / 8;
                        int t;
                        switch (mask) {
                        case 0: t = (i + j) % 2; break;
                        case 1: t = i % 2; break;
                        case 2: t = j % 3; break;
                        case 3: t = (i + j) % 3; break;
                        case 4: t = (i/2 + j/3) % 2; break;
                        case 5: t = ((i*j) % 2) + ((i*j) % 3); break;
                        case 6: t = (((i*j) % 2) + ((i*j) % 3)) % 2; break;
                        case 7: t = (((i*j) % 3) + ((i+j) % 2)) % 2; break;
                        }
                        p[off] ^= (t == 0) << bit;
                }
        }
}

variable_types: struct qr_bitmap { unsigned char *bits; unsigned char *mask; unsigned long stride; unsigned long width; } *

variable_types: mask int

variable_types:
i
unsigned long

variable_types:
j
unsigned long

variable_types:
p
unsigned char *

variable_types: bit int

variable_types:
off
unsigned long

variable_types: t int


decompiled_code:
__int64 __fastcall gf_mult(int a1, unsigned int a2)
{
int v2; // edx
__int64 result; // rax
int v4; // er8
bool v5; // zf

v2 = 8;
LOBYTE(result) = 0;
do
{
if ( (a2 & 1) != 0 )
LOBYTE(result) = a1 ^ result;
v4 = (2 * a1) ^ 0x11D;
v5 = (a1 & 0x80) == 0;
a1 *= 2;
if ( !v5 )
a1 = v4;
a2 >>= 1;
--v2;
}
while ( v2 );
return (unsigned __int8)result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, unsigned int a2)
{
int v2; // edx
__int64 result; // rax
int v4; // er8
bool v5; // zf

v2 = 8;
LOBYTE(result) = 0;
do
{
if ( (a2 & 1) != 0 )
LOBYTE(result) = a1 ^ result;
v4 = (2 * a1) ^ 0x11D;
v5 = (a1 & 0x80) == 0;
a1 *= 2;
if ( !v5 )
a1 = v4;
a2 >>= 1;
--v2;
}
while ( v2 );
return (unsigned __int8)result;
}

original_code:
static unsigned int gf_mult(unsigned int a, unsigned int b)
{
        const unsigned int m = 0x11D;
        unsigned int x = 0;
        int i;
        for (i = 0; i < 8; ++i) {
                x ^= (b & 0x1) ? a : 0;
                a = (a << 1) ^ ((a & 0x80) ? m : 0);
                b >>= 1;
        }
        return x & 0xFF;
}

canonical_original_code:
static unsigned int gf_mult(unsigned int a, unsigned int b)
{
        unsigned int m = 0x11D;
        unsigned int x = 0;
        int i;
        for (i = 0; i < 8; ++i) {
                x ^= (b & 0x1) ? a : 0;
                a = (a << 1) ^ ((a & 0x80) ? m : 0);
                b >>= 1;
        }
        return x & 0xFF;
}

variable_types:
a
unsigned int

variable_types:
b
unsigned int

variable_types:
m
unsigned int

variable_types:
x
unsigned int

variable_types: i int

return_type: unsigned int


original_code:
unsigned long gf_residue(unsigned long a, unsigned long m)
{
        unsigned long o = 1;
        int n = 1;
        while (m & ~(o - 1))
                o <<= 1;
        while (a & ~(o - 1)) {
                o <<= 1;
                ++n;
        }
        while (n--) {
                o >>= 1;
                if (a & o)
                        a ^= m << n;
        }
        return a;
}

variable_types:
a
unsigned long

variable_types:
m
unsigned long

variable_types:
o
unsigned long

variable_types: n int


original_code:
void qr_layout_init_mask(struct qr_code * code)
{
        size_t x, y;
        size_t dim = qr_code_width(code);
        struct qr_bitmap * bmp = code->modules;
        const int * am_pos = QR_ALIGNMENT_LOCATION[code->version - 1];
        size_t am_side;
        if (!bmp->mask)
                qr_bitmap_add_mask(bmp);
        ((bmp->mask) ? (void) (0) : __assert_fail ("bmp->mask", "code-layout.c", 33, __extension__ __PRETTY_FUNCTION__));
        memset(bmp->mask, 0, bmp->height * bmp->stride);
        for (y = 0; y < bmp->height; ++y) {
                unsigned char * row = bmp->mask + y * bmp->stride;
                for (x = 0; x < bmp->width; ++x) {
                        unsigned char bit = 1 << (x % 8);
                        int off = x / 8;
                        if (x == 6 || y == 6)
                                continue;
                        if (x < 9 && y < 9)
                                continue;
                        if (x >= dim - 8 && y < 9)
                                continue;
                        if (x < 9 && y >= dim - 8)
                                continue;
                        if (code->version >= 7) {
                                if (y < 6 && x >= dim - 11)
                                        continue;
                                if (x < 6 && y >= dim - 11)
                                        continue;
                        }
                        row[off] |= bit;
                }
        }
        am_side = code->version > 1 ? (code->version / 7) + 2 : 0;
        for (y = 0; y < am_side; ++y) {
                for (x = 0; x < am_side; ++x) {
                        int i, j;
                        if ((x == 0 && y == 0) ||
                            (x == 0 && y == am_side - 1) ||
                            (x == am_side - 1 && y == 0))
                                continue;
                        for (j = -2; j <= 2; ++j) {
                                unsigned char * row = bmp->mask + (am_pos[y]+j) * bmp->stride;
                                for (i = -2; i <= 2; ++i) {
                                        unsigned char bit = 1 << ((am_pos[x]+i) % 8);
                                        int off = (am_pos[x]+i) / 8;
                                        row[off] &= ~bit;
                                }
                        }
                }
        }
}

canonical_original_code:
void qr_layout_init_mask(struct qr_code *code)
{
        unsigned long x; unsigned long y;
        unsigned long dim = qr_code_width(code);
        struct qr_bitmap *bmp = code->modules;
        int *am_pos = QR_ALIGNMENT_LOCATION[code->version - 1];
        unsigned long am_side;
        if (!bmp->mask)
                qr_bitmap_add_mask(bmp);
        ((bmp->mask) ? (void) (0) : __assert_fail ("bmp->mask", "code-layout.c", 33, __extension__ __PRETTY_FUNCTION__));
        memset(bmp->mask, 0, bmp->height * bmp->stride);
        for (y = 0; y < bmp->height; ++y) {
                unsigned char *row = bmp->mask + y * bmp->stride;
                for (x = 0; x < bmp->width; ++x) {
                        unsigned char bit = 1 << (x % 8);
                        int off = x / 8;
                        if (x == 6 || y == 6)
                                continue;
                        if (x < 9 && y < 9)
                                continue;
                        if (x >= dim - 8 && y < 9)
                                continue;
                        if (x < 9 && y >= dim - 8)
                                continue;
                        if (code->version >= 7) {
                                if (y < 6 && x >= dim - 11)
                                        continue;
                                if (x < 6 && y >= dim - 11)
                                        continue;
                        }
                        row[off] |= bit;
                }
        }
        am_side = code->version > 1 ? (code->version / 7) + 2 : 0;
        for (y = 0; y < am_side; ++y) {
                for (x = 0; x < am_side; ++x) {
                        int i; int j;
                        if ((x == 0 && y == 0) ||
                            (x == 0 && y == am_side - 1) ||
                            (x == am_side - 1 && y == 0))
                                continue;
                        for (j = -2; j <= 2; ++j) {
                                unsigned char *row = bmp->mask + (am_pos[y]+j) * bmp->stride;
                                for (i = -2; i <= 2; ++i) {
                                        unsigned char bit = 1 << ((am_pos[x]+i) % 8);
                                        int off = (am_pos[x]+i) / 8;
                                        row[off] &= ~bit;
                                }
                        }
                }
        }
}

variable_types: struct qr_code { int version; struct qr_bitmap { unsigned char *bits; unsigned char *mask; unsigned long stride; unsigned long width; } *modules; } *

variable_types:
x
unsigned long

variable_types:
y
unsigned long

variable_types:
dim
unsigned long

variable_types:
row
unsigned char *

variable_types:
bit
unsigned char

variable_types: off int

variable_types: j int


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

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

original_code:
void qr_layout_end(struct qr_iterator * i)
{
        free(i);
}

canonical_original_code:
void qr_layout_end(struct qr_iterator *i)
{
        free(i);
}

variable_types: struct qr_iterator { struct qr_code { int version; struct qr_bitmap { unsigned char *bits; unsigned char *mask; unsigned long stride; unsigned long width; } *modules; } *code; int dim; int column; int row; int up; int mask; unsigned char *p; } *

user_defined_types: struct qr_iterator { struct qr_code *code; int dim; int column; int row; int up; int mask; unsigned char *p; }


decompiled_code:
__int64 __fastcall qr_layout_read(__int64 a1)
{
int v2; // ebx
unsigned int v3; // er10

v2 = 8;
do
{
advance(a1);
--v2;
}
while ( v2 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
int v2; // ebx
unsigned int v3; // er10

v2 = 8;
do
{
func2(a1);
--v2;
}
while ( v2 );
return v3;
}

original_code:
unsigned int qr_layout_read(struct qr_iterator * i)
{
        unsigned int x = 0;
        int b;
        for (b = 0; b < QR_WORD_BITS; ++b) {
                x = (x << 1) | ((*i->p & i->mask) ? 1 : 0);
                advance(i);
        }
        return x;
}

canonical_original_code:
unsigned int qr_layout_read(struct qr_iterator *i)
{
        unsigned int x = 0;
        int b;
        for (b = 0; b < QR_WORD_BITS; ++b) {
                x = (x << 1) | ((*i->p & i->mask) ? 1 : 0);
                advance(i);
        }
        return x;
}

variable_types: b int


decompiled_code:
__int64 __fastcall qr_layout_write(__int64 a1, char a2)
{
char *v3; // rax
char v4; // dl
__int64 result; // rax
int v6; // er11

do
{
v3 = *(char **)(a1 + 32);
v4 = *v3;
if ( a2 < 0 )
v4 |= *(_BYTE *)(a1 + 24);
*v3 = v4;
result = advance(a1);
}
while ( v6 != 1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, char a2)
{
char *v3; // rax
char v4; // dl
__int64 result; // rax
int v6; // er11

do
{
v3 = *(char **)(a1 + 32);
v4 = *v3;
if ( a2 < 0 )
v4 |= *(_BYTE *)(a1 + 24);
*v3 = v4;
result = func2(a1);
}
while ( v6 != 1 );
return result;
}

original_code:
void qr_layout_write(struct qr_iterator * i, unsigned int x)
{
        int b;
        for (b = 0; b < QR_WORD_BITS; ++b) {
                *i->p |= (x & 0x80) ? i->mask : 0;
                advance(i);
                x <<= 1;
        }
}

canonical_original_code:
void qr_layout_write(struct qr_iterator *i, unsigned int x)
{
        int b;
        for (b = 0; b < QR_WORD_BITS; ++b) {
                *i->p |= (x & 0x80) ? i->mask : 0;
                advance(i);
                x <<= 1;
        }
}


decompiled_code:
__int64 __fastcall get_alpha_code(char a1)
{
unsigned __int16 v1; // ax
__int64 result; // rax

v1 = (*_ctype_b_loc())[a1];
if ( (v1 & 0x800) != 0 )
return (unsigned int)(a1 - 48);
if ( (v1 & 0x400) != 0 )
return (unsigned int)((*_ctype_toupper_loc())[a1] - 55);
result = 0xFFFFFFFFLL;
if ( (unsigned __int8)(a1 - 32) <= 0x1Au )
result = (unsigned int)CSWTCH_11[(unsigned __int8)(a1 - 32)];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
unsigned __int16 v1; // ax
__int64 result; // rax

v1 = (*_ctype_b_loc())[a1];
if ( (v1 & 0x800) != 0 )
return (unsigned int)(a1 - 48);
if ( (v1 & 0x400) != 0 )
return (unsigned int)((*_ctype_toupper_loc())[a1] - 55);
result = 0xFFFFFFFFLL;
if ( (unsigned __int8)(a1 - 32) <= 0x1Au )
result = (unsigned int)CSWTCH_11[(unsigned __int8)(a1 - 32)];
return result;
}

original_code:
static int get_alpha_code(char c)
{
        if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit))
                return c - '0';
        else if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha))
                return toupper(c) - 'A' + 10;
        switch (c) {
        case ' ': return 36;
        case '$': return 37;
        case '%': return 38;
        case '*': return 39;
        case '+': return 40;
        case '-': return 41;
        case '.': return 42;
        case '/': return 43;
        case ':': return 44;
        default: return -1;
        }
}

canonical_original_code:
static int get_alpha_code(char c)
{
        if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit))
                return c - '0';
        else if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha))
                return toupper(c) - 'A' + 10;
        switch (c) {
        case ' ': return 36;
        case '$': return 37;
        case '%': return 38;
        case '*': return 39;
        case '+': return 40;
        case '-': return 41;
        case '.': return 42;
        case '/': return 43;
        case ':': return 44;
        default: return -1;
        }
}

variable_types: c char


decompiled_code:
__int64 __fastcall qr_data_dpart_length(int a1, unsigned __int64 a2)
{
__int64 result; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rsi

if ( a1 == 2 )
return 11 * (a2 >> 1) + 6 * (a2 & 1);
result = 8 * a2;
if ( a1 != 4 )
{
result = 0LL;
if ( a1 == 1 )
{
v3 = 5 * (a2 / 3);
v4 = a2 % 3;
result = 2 * v3;
if ( v4 == 1 )
{
result += 4LL;
}
else if ( v4 == 2 )
{
result += 7LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, unsigned __int64 a2)
{
__int64 result; // rax
unsigned __int64 v3; // rax
unsigned __int64 v4; // rsi

if ( a1 == 2 )
return 11 * (a2 >> 1) + 6 * (a2 & 1);
result = 8 * a2;
if ( a1 != 4 )
{
result = 0LL;
if ( a1 == 1 )
{
v3 = 5 * (a2 / 3);
v4 = a2 % 3;
result = 2 * v3;
if ( v4 == 1 )
{
result += 4LL;
}
else if ( v4 == 2 )
{
result += 7LL;
}
}
}
return result;
}

original_code:
size_t qr_data_dpart_length(enum qr_data_type type, size_t length)
{
        size_t bits;
        switch (type) {
        case QR_DATA_NUMERIC:
                bits = 10 * (length / 3);
                if (length % 3 == 1)
                        bits += 4;
                else if (length % 3 == 2)
                        bits += 7;
                break;
        case QR_DATA_ALPHA:
                bits = 11 * (length / 2)
                        + 6 * (length % 2);
                break;
        case QR_DATA_8BIT:
                bits = 8 * length;
                break;
        case QR_DATA_KANJI:
        default:
                bits = 0;
        }
        return bits;
}

canonical_original_code:
unsigned long qr_data_dpart_length(enum qr_data_type type, unsigned long length)
{
        unsigned long bits;
        switch (type) {
        case QR_DATA_NUMERIC:
                bits = 10 * (length / 3);
                if (length % 3 == 1)
                        bits += 4;
                else if (length % 3 == 2)
                        bits += 7;
                break;
        case QR_DATA_ALPHA:
                bits = 11 * (length / 2)
                        + 6 * (length % 2);
                break;
        case QR_DATA_8BIT:
                bits = 8 * length;
                break;
        case QR_DATA_KANJI:
        default:
                bits = 0;
        }
        return bits;
}

variable_types: enum qr_data_type { QR_DATA_INVALID=-1, QR_DATA_ECI=7, QR_DATA_NUMERIC=1, QR_DATA_ALPHA=2, QR_DATA_8BIT=4, QR_DATA_KANJI=8, QR_DATA_MIXED=3, QR_DATA_FNC1=9 }

variable_types:
length
unsigned long

variable_types:
bits
unsigned long


decompiled_code:
__int64 __fastcall calc_min_version(unsigned int a1, int a2, unsigned __int64 a3)
{
unsigned int v3; // er12
int *v4; // r13
__int64 v5; // rbp

v3 = 1;
v4 = (int *)((char *)&QR_DATA_WORD_COUNT + 4 * (a2 ^ 1u));
v5 = qr_data_dpart_length(a1, a3) + 4;
do
{
if ( qr_data_size_field_length(v3, a1) + v5 < (unsigned __int64)(8LL * *v4) )
return v3;
++v3;
v4 += 4;
}
while ( v3 != 41 );
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, int a2, unsigned __int64 a3)
{
unsigned int v3; // er12
int *v4; // r13
__int64 v5; // rbp

v3 = 1;
v4 = (int *)((char *)&QR_DATA_WORD_COUNT + 4 * (a2 ^ 1u));
v5 = func3(a1, a3) + 4;
do
{
if ( qr_data_size_field_length(v3, a1) + v5 < (unsigned __int64)(8LL * *v4) )
return v3;
++v3;
v4 += 4;
}
while ( v3 != 41 );
return 0xFFFFFFFFLL;
}

original_code:
static int calc_min_version(enum qr_data_type type,
                            enum qr_ec_level ec,
                            size_t length)
{
        size_t dbits;
        int version;
        dbits = qr_data_dpart_length(type, length);
        for (version = 1; version <= 40; ++version) {
                if (4 + dbits + qr_data_size_field_length(version, type)
                    < 8 * (size_t) QR_DATA_WORD_COUNT[version - 1][ec ^ 0x1])
                        return version;
        }
        return -1;
}

canonical_original_code:
static int calc_min_version(enum qr_data_type type, enum qr_ec_level ec, unsigned long length)
{
        unsigned long dbits;
        int version;
        dbits = qr_data_dpart_length(type, length);
        for (version = 1; version <= 40; ++version) {
                if (4 + dbits + qr_data_size_field_length(version, type)
                    < 8 * (unsigned long) QR_DATA_WORD_COUNT[version - 1][ec ^ 0x1])
                        return version;
        }
        return -1;
}

variable_types: enum qr_ec_level { QR_EC_LEVEL_L=1, QR_EC_LEVEL_M=0, QR_EC_LEVEL_Q=3, QR_EC_LEVEL_H=2 }

variable_types:
dbits
unsigned long

variable_types: version int

user_defined_types: enum qr_data_type { QR_DATA_INVALID=-1, QR_DATA_ECI=7, QR_DATA_NUMERIC=1, QR_DATA_ALPHA=2, QR_DATA_8BIT=4, QR_DATA_KANJI=8, QR_DATA_MIXED=3, QR_DATA_FNC1=9 }


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

if ( a2 == 4 )
{
v2 = 2LL;
goto LABEL_6;
}
if ( a2 <= 4 )
{
if ( a2 == 1 )
{
v2 = 0LL;
}
else
{
if ( a2 != 2 )
return 0LL;
v2 = 1LL;
}
LABEL_6:
if ( a1 <= 9 )
return QR_SIZE_LENGTHS_0[v2];
return QR_SIZE_LENGTHS_0[4 * (a1 > 26) + 4 + v2];
}
result = 0LL;
if ( a2 == 8 )
{
v2 = 3LL;
if ( a1 <= 9 )
return QR_SIZE_LENGTHS_0[v2];
return QR_SIZE_LENGTHS_0[4 * (a1 > 26) + 4 + v2];
}
return result;
}

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

if ( a2 == 4 )
{
v2 = 2LL;
goto LABEL_6;
}
if ( a2 <= 4 )
{
if ( a2 == 1 )
{
v2 = 0LL;
}
else
{
if ( a2 != 2 )
return 0LL;
v2 = 1LL;
}
LABEL_6:
if ( a1 <= 9 )
return QR_SIZE_LENGTHS_0[v2];
return QR_SIZE_LENGTHS_0[4 * (a1 > 26) + 4 + v2];
}
result = 0LL;
if ( a2 == 8 )
{
v2 = 3LL;
if ( a1 <= 9 )
return QR_SIZE_LENGTHS_0[v2];
return QR_SIZE_LENGTHS_0[4 * (a1 > 26) + 4 + v2];
}
return result;
}

original_code:
size_t qr_data_size_field_length(int version, enum qr_data_type type)
{
        static const size_t QR_SIZE_LENGTHS[3][4] = {
                { 10, 9, 8, 8 },
                { 12, 11, 16, 10 },
                { 14, 13, 16, 12 }
        };
        int row, col;
        switch (type) {
        case QR_DATA_NUMERIC: col = 0; break;
        case QR_DATA_ALPHA: col = 1; break;
        case QR_DATA_8BIT: col = 2; break;
        case QR_DATA_KANJI: col = 3; break;
        default: return 0;
        }
        if (version < 10)
                row = 0;
        else if (version < 27)
                row = 1;
        else
                row = 2;
        return QR_SIZE_LENGTHS[row][col];
}

canonical_original_code:
unsigned long qr_data_size_field_length(int version, enum qr_data_type type)
{
        static unsigned long QR_SIZE_LENGTHS[3][4] = {
                { 10, 9, 8, 8 },
                { 12, 11, 16, 10 },
                { 14, 13, 16, 12 }
        };
        int row; int col;
        switch (type) {
        case QR_DATA_NUMERIC: col = 0; break;
        case QR_DATA_ALPHA: col = 1; break;
        case QR_DATA_8BIT: col = 2; break;
        case QR_DATA_KANJI: col = 3; break;
        default: return 0;
        }
        if (version < 10)
                row = 0;
        else if (version < 27)
                row = 1;
        else
                row = 2;
        return QR_SIZE_LENGTHS[row][col];
}

variable_types: row int

variable_types: col int


PerDown

repo: norlanliu/data-structure

original_code:
static void PerDown(int *array, int size, int hole)
{
 int temp = array[hole];
 int child;
 for(; (2 * hole + 1) < size; hole = child)
 {
  child = 2 * hole + 1;
  if(child != size-1 && array[child] < array[child+1])
   ++child;
  if(array[child] < temp)
   break;
  else
   array[hole] = array[child];
 }
 array[hole] = temp;
}

variable_types:
array
int *

variable_types: size int

variable_types: hole int

variable_types: temp int

variable_types: child int


BuildHeap

decompiled_code:
__int64 __fastcall BuildHeap(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // ebx

result = (unsigned int)(a2 - 2);
v3 = (a2 - 2) / 2;
if ( a2 > 0 )
{
do
result = PerDown(a1, a2, v3);
while ( v3-- != 0 );
}
return result;
}

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

result = (unsigned int)(a2 - 2);
v3 = (a2 - 2) / 2;
if ( a2 > 0 )
{
do
result = func0(a1, a2, v3);
while ( v3-- != 0 );
}
return result;
}

original_code:
static void BuildHeap(int *array, int size)
{
 int i = (size-2) / 2;
 for(; i >= 0; --i)
  PerDown(array, size, i);
}

variable_types: i int


counterSorting

original_code:
void counterSorting(unsigned *org, unsigned *des, int count, int order)
{
 if(org == ((void *)0) || count == 0)
  return;
 int max = 0;
 int i;
 for(i = 0; i < count; i++)
  if(org[i] > max)
   max = org[i];
 max += 1;
 int *bucket = (int *)malloc(max * sizeof(int));
 memset(bucket, 0, sizeof(int)*max);
 for(i = 0; i < count; i++)
  bucket[org[i]]++;
 if(order == 0)
  for(i = 1; i < max; i++)
   bucket[i] += bucket[i-1];
 else
  for(i = max-2; i >= 0; i--)
   bucket[i] += bucket[i+1];
 for(i = count-1; i >= 0; i--)
 {
  des[bucket[org[i]]-1] = org[i];
  bucket[org[i]] -= 1;
 }
}

canonical_original_code:
void counterSorting(unsigned int *org, unsigned int *des, int count, int order)
{
 if(org == ((void *)0) || count == 0)
  return;
 int max = 0;
 int i;
 for(i = 0; i < count; i++)
  if(org[i] > max)
   max = org[i];
 max += 1;
 int *bucket = (int *)malloc(max * sizeof(int));
 memset(bucket, 0, sizeof(int)*max);
 for(i = 0; i < count; i++)
  bucket[org[i]]++;
 if(order == 0)
  for(i = 1; i < max; i++)
   bucket[i] += bucket[i-1];
 else
  for(i = max-2; i >= 0; i--)
   bucket[i] += bucket[i+1];
 for(i = count-1; i >= 0; i--)
 {
  des[bucket[org[i]]-1] = org[i];
  bucket[org[i]] -= 1;
 }
}

variable_types:
org
unsigned int *

variable_types:
des
unsigned int *

variable_types: count int

variable_types: order int

variable_types: max int

variable_types:
bucket
int *


bucketSorting

original_code:
void bucketSorting(unsigned *org, unsigned *des, int length)
{
 int max = 0;
 int i,j, k = 0;
 for(i = 0; i<length; i++)
  if(org[i] > max)
   max = org[i];
 if(max <= 0)
  return;
 unsigned m = max / 128 + 1;
 list_t **buckets = (list_t **)malloc(128 * sizeof(list_t *));
 memset(buckets, 0, 128*sizeof(list_t*));
 list_t *temp, *temp2;
 for(i = 0; i < length; i++)
 {
  k = org[i] / m;
  temp = (list_t *)malloc(sizeof(list_t));
  temp->item = org[i];
  temp->next = buckets[k];
  buckets[k] = temp;
 }
 for(i=j=0; i<128; i++)
 {
  temp = buckets[i];
  while(temp != ((void *)0))
  {
   des[j++] = temp->item;
   temp2 = temp;
   temp = temp->next;
   free(temp2);
  }
 }
 free(buckets);
 insertSorting(des, 0, length);
}

canonical_original_code:
void bucketSorting(unsigned int *org, unsigned int *des, int length)
{
 int max = 0;
 int i; int j; int k = 0;
 for(i = 0; i<length; i++)
  if(org[i] > max)
   max = org[i];
 if(max <= 0)
  return;
 unsigned int m = max / 128 + 1;
 struct _list **buckets = (list_t **)malloc(128 * sizeof(list_t *));
 memset(buckets, 0, 128*sizeof(struct _list *));
 struct _list *temp; struct _list *temp2;
 for(i = 0; i < length; i++)
 {
  k = org[i] / m;
  temp = (struct _list *)malloc(sizeof(list_t));
  temp->item = org[i];
  temp->next = buckets[k];
  buckets[k] = temp;
 }
 for(i=j=0; i<128; i++)
 {
  temp = buckets[i];
  while(temp != ((void *)0))
  {
   des[j++] = temp->item;
   temp2 = temp;
   temp = temp->next;
   free(temp2);
  }
 }
 free(buckets);
 insertSorting(des, 0, length);
}

variable_types: length int

variable_types: j int

variable_types: k int

variable_types:
m
unsigned int

variable_types: struct _list { int item; struct _list *next; } **


radixSorting

original_code:
void radixSorting(unsigned *org, int length)
{
 int max = 0;
 int i,j, k = 0, bucket_size = 10,exp = 1;
 for(i = 0; i<length; i++)
  if(org[i] > max)
   max = org[i];
 if(max <= 0)
  return;
 list_t **buckets = (list_t **)malloc(bucket_size * sizeof(list_t *));
 memset(buckets, 0, bucket_size* sizeof(list_t*));
 list_t *temp, *temp2;
 while(max / exp != 0)
 {
  for(i = 0; i < length; i++)
  {
   k = (org[i] / exp) % bucket_size;
   temp = (list_t *)malloc(sizeof(list_t));
   temp->item = org[i];
   temp->next = buckets[k];
   buckets[k] = temp;
  }
  for(i=j=0; i<bucket_size; i++)
  {
   temp = buckets[i];
   while(temp != ((void *)0))
   {
    temp = temp->next;
    j++;
   }
   k = 0;
   temp = buckets[i];
   while(temp != ((void *)0))
   {
    org[j-k-1] = temp->item;
    temp2 = temp;
    temp = temp->next;
    free(temp2);
    k++;
   }
   buckets[i] = ((void *)0);
  }
  exp *= 10;
 }
 free(buckets);
}

canonical_original_code:
void radixSorting(unsigned int *org, int length)
{
 int max = 0;
 int i; int j; int k = 0; int bucket_size = 10; int exp = 1;
 for(i = 0; i<length; i++)
  if(org[i] > max)
   max = org[i];
 if(max <= 0)
  return;
 struct _list **buckets = (list_t **)malloc(bucket_size * sizeof(list_t *));
 memset(buckets, 0, bucket_size* sizeof(struct _list *));
 struct _list *temp; struct _list *temp2;
 while(max / exp != 0)
 {
  for(i = 0; i < length; i++)
  {
   k = (org[i] / exp) % bucket_size;
   temp = (struct _list *)malloc(sizeof(list_t));
   temp->item = org[i];
   temp->next = buckets[k];
   buckets[k] = temp;
  }
  for(i=j=0; i<bucket_size; i++)
  {
   temp = buckets[i];
   while(temp != ((void *)0))
   {
    temp = temp->next;
    j++;
   }
   k = 0;
   temp = buckets[i];
   while(temp != ((void *)0))
   {
    org[j-k-1] = temp->item;
    temp2 = temp;
    temp = temp->next;
    free(temp2);
    k++;
   }
   buckets[i] = ((void *)0);
  }
  exp *= 10;
 }
 free(buckets);
}

variable_types: exp int

variable_types: struct _list { int item; struct _list *next; } *


bitSorting

original_code:
void bitSorting(unsigned *org, int length)
{
 int i,j;
 for(i = 31; i >= 1; i--)
 {
  int preCB = org[0] >> i;
  int theS = 0;
  int theI = theS - 1;
  int theBase = 1 << (i-1);
  int theAxBit = 0;
  for(j = 0; j < length; j++)
  {
   int temPreCB = org[j]>>i;
   if(temPreCB != preCB)
   {
    theS = j;
    theI = theS - 1;
    theAxBit = 0;
    preCB = temPreCB;
    j--;
    continue;
   }
   int theAJ = (org[j] & theBase) > 0 ? 1:0;
   if(theI < theS)
   {
    if(theAJ == 0)
     continue;
    theI = j;
    continue;
   }
   if(theAJ <= theAxBit)
   {
    int temp = org[j];
    org[j] = org[theI];
    org[theI]=temp;
    theI++;
   }
  }
 }
}

canonical_original_code:
void bitSorting(unsigned int *org, int length)
{
 int i; int j;
 for(i = 31; i >= 1; i--)
 {
  int preCB = org[0] >> i;
  int theS = 0;
  int theI = theS - 1;
  int theBase = 1 << (i-1);
  int theAxBit = 0;
  for(j = 0; j < length; j++)
  {
   int temPreCB = org[j]>>i;
   if(temPreCB != preCB)
   {
    theS = j;
    theI = theS - 1;
    theAxBit = 0;
    preCB = temPreCB;
    j--;
    continue;
   }
   int theAJ = (org[j] & theBase) > 0 ? 1:0;
   if(theI < theS)
   {
    if(theAJ == 0)
     continue;
    theI = j;
    continue;
   }
   if(theAJ <= theAxBit)
   {
    int temp = org[j];
    org[j] = org[theI];
    org[theI]=temp;
    theI++;
   }
  }
 }
}

variable_types: preCB int

variable_types: theS int

variable_types: theI int

variable_types: theBase int

variable_types: theAxBit int

variable_types: temPreCB int

variable_types: theAJ int


HeapSorting

decompiled_code:
__int64 __fastcall HeapSorting(__int64 a1, int a2)
{
int v2; // ebx
__int64 result; // rax
__int64 v4; // rbp
__int64 v5; // rsi

v2 = a2 - 1;
result = BuildHeap(a1, a2);
if ( a2 - 1 > 0 )
{
v4 = a1 + 4LL * v2;
do
{
v5 = v4;
v4 -= 4LL;
Swap(a1, v5);
result = PerDown(a1, v2--, 0);
}
while ( v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
int v2; // ebx
__int64 result; // rax
__int64 v4; // rbp
__int64 v5; // rsi

v2 = a2 - 1;
result = func1(a1, a2);
if ( a2 - 1 > 0 )
{
v4 = a1 + 4LL * v2;
do
{
v5 = v4;
v4 -= 4LL;
Swap(a1, v5);
result = func0(a1, v2--, 0);
}
while ( v2 );
}
return result;
}

original_code:
void HeapSorting(int *org, int length)
{
 BuildHeap(org, length);
 int i;
 for(i = length-1; i > 0; --i)
 {
  Swap(&org[0], &org[i]);
  PerDown(org, i, 0);
 }
}

variable_types:
org
int *

variable_types: i int


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

for ( result = a1; result && *(_QWORD *)(result + 16); result = *(_QWORD *)(result + 16) )
;
return result;
}

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

for ( result = a1; result && *(_QWORD *)(result + 16); result = *(_QWORD *)(result + 16) )
;
return result;
}

original_code:
BStree *find_min(BStree *tree)
{
 if(tree == ((void *)0))
  return ((void *)0);
 if(tree->left != ((void *)0))
  return find_min(tree->left);
 return
  tree;
}

canonical_original_code:
struct bstree *find_min(struct bstree *tree)
{
 if(tree == ((void *)0))
  return ((void *)0);
 if(tree->left != ((void *)0))
  return find_min(tree->left);
 return
  tree;
}

variable_types:
tree
struct bstree *

return_type: struct bstree { int element; struct bstree *parent; struct bstree *left; struct bstree *right; } *


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

for ( result = a1; result && *(_QWORD *)(result + 24); result = *(_QWORD *)(result + 24) )
;
return result;
}

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

for ( result = a1; result && *(_QWORD *)(result + 24); result = *(_QWORD *)(result + 24) )
;
return result;
}

original_code:
BStree *find_max(BStree *tree)
{
 if(tree == ((void *)0))
  return ((void *)0);
 if(tree->right != ((void *)0))
  return find_max(tree->right);
 else
  return tree;
}

canonical_original_code:
struct bstree *find_max(struct bstree *tree)
{
 if(tree == ((void *)0))
  return ((void *)0);
 if(tree->right != ((void *)0))
  return find_max(tree->right);
 else
  return tree;
}

user_defined_types: struct bstree { int element; struct bstree *parent; struct bstree *left; struct bstree *right; }


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

result = 0LL;
if ( !*(_QWORD *)(a1 + 16) )
result = *(_QWORD *)(a1 + 24) == 0LL;
return result;
}

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

result = 0LL;
if ( !*(_QWORD *)(a1 + 16) )
result = *(_QWORD *)(a1 + 24) == 0LL;
return result;
}

original_code:
short is_leaf(BStree *tree)
{
 if(tree->left == ((void *)0) && tree->right == ((void *)0))
  return 1;
 return 0;
}

canonical_original_code:
short is_leaf(struct bstree *tree)
{
 if(tree->left == ((void *)0) && tree->right == ((void *)0))
  return 1;
 return 0;
}

variable_types: struct bstree { int element; struct bstree *parent; struct bstree *left; struct bstree *right; } *

return_type: short


decompiled_code:
__int64 __fastcall replace_with_max(__int64 a1)
{
_DWORD *v2; // rax
_DWORD *v3; // r8
__int64 v4; // r8
__int64 v5; // rdi

while ( !is_leaf(a1) )
{
v5 = *(_QWORD *)(v4 + 16);
if ( v5 )
v2 = (_DWORD *)find_max(v5);
else
v2 = (_DWORD *)find_min(*(_QWORD *)(v4 + 24));
*v3 = *v2;
a1 = (__int64)v2;
}
return v4;
}

original_code:
BStree *replace_with_max(BStree *tree)
{
 if(is_leaf(tree))
  return tree;
 else if(tree->left != ((void *)0))
 {
  BStree *tmp = find_max(tree->left);
  tree->element = tmp->element;
  return replace_with_max(tmp);
 }
 else
 {
  BStree *tmp = find_min(tree->right);
  tree->element = tmp->element;
  return replace_with_max(tmp);
 }
}

canonical_original_code:
struct bstree *replace_with_max(struct bstree *tree)
{
 if(is_leaf(tree))
  return tree;
 else if(tree->left != ((void *)0))
 {
  struct bstree *tmp = find_max(tree->left);
  tree->element = tmp->element;
  return replace_with_max(tmp);
 }
 else
 {
  struct bstree *tmp = find_min(tree->right);
  tree->element = tmp->element;
  return replace_with_max(tmp);
 }
}


original_code:
void delete_tree(BStree *tree, item_type element)
{
 if(tree == ((void *)0))
  return;
 else if(element == tree->element)
 {
  BStree *tmp = replace_with_max(tree);
  if(tmp != ((void *)0) && tmp->parent != ((void *)0))
  {
   if(tmp->parent->left == tmp)
    tmp->parent->left = ((void *)0);
   else
    tmp->parent->right = ((void *)0);
  }
  free(tmp);
  tmp = ((void *)0);
 }
 else if(element < tree->element)
  delete_tree(tree->left, element);
 else
  delete_tree(tree->right, element);
}

canonical_original_code:
void delete_tree(struct bstree *tree, int element)
{
 if(tree == ((void *)0))
  return;
 else if(element == tree->element)
 {
  struct bstree *tmp = replace_with_max(tree);
  if(tmp != ((void *)0) && tmp->parent != ((void *)0))
  {
   if(tmp->parent->left == tmp)
    tmp->parent->left = ((void *)0);
   else
    tmp->parent->right = ((void *)0);
  }
  free(tmp);
  tmp = ((void *)0);
 }
 else if(element < tree->element)
  delete_tree(tree->left, element);
 else
  delete_tree(tree->right, element);
}

variable_types: element int


decompiled_code:
__int64 __fastcall print_tree(unsigned int *a1)
{
unsigned int *v1; // rbx
__int64 result; // rax

if ( a1 )
{
v1 = a1;
do
{
printf("%d\n", *v1);
result = print_tree(*((_QWORD *)v1 + 2));
v1 = (unsigned int *)*((_QWORD *)v1 + 3);
}
while ( v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned int *a1)
{
unsigned int *v1; // rbx
__int64 result; // rax

if ( a1 )
{
v1 = a1;
do
{
printf("%d\n", *v1);
result = func13(*((_QWORD *)v1 + 2));
v1 = (unsigned int *)*((_QWORD *)v1 + 3);
}
while ( v1 );
}
return result;
}

original_code:
void print_tree(BStree *tree)
{
 if(tree == ((void *)0))
  return;
 else
  printf("%d\n", tree->element);
 print_tree(tree->left);
 print_tree(tree->right);
}

canonical_original_code:
void print_tree(struct bstree *tree)
{
 if(tree == ((void *)0))
  return;
 else
  printf("%d\n", tree->element);
 print_tree(tree->left);
 print_tree(tree->right);
}


clear

decompiled_code:
void __fastcall clear(void *ptr)
{
__int64 v1; // rax

if ( ptr )
{
if ( is_leaf((__int64)ptr) )
{
v1 = *((_QWORD *)ptr + 1);
if ( !v1 )
{
LABEL_6:
free(ptr);
return;
}
if ( *(void **)(v1 + 16) != ptr )
{
*(_QWORD *)(v1 + 24) = 0LL;
goto LABEL_6;
}
*(_QWORD *)(v1 + 16) = 0LL;
free(ptr);
}
else
{
clear(*((void **)ptr + 2));
clear(*((void **)ptr + 3));
free(ptr);
}
}
}

canonical_decompiled_code:
void __fastcall func14(void *ptr)
{
__int64 v1; // rax

if ( ptr )
{
if ( func9((__int64)ptr) )
{
v1 = *((_QWORD *)ptr + 1);
if ( !v1 )
{
LABEL_6:
free(ptr);
return;
}
if ( *(void **)(v1 + 16) != ptr )
{
*(_QWORD *)(v1 + 24) = 0LL;
goto LABEL_6;
}
*(_QWORD *)(v1 + 16) = 0LL;
free(ptr);
}
else
{
func14(*((void **)ptr + 2));
func14(*((void **)ptr + 3));
free(ptr);
}
}
}

original_code:
void clear(BStree *tree)
{
 if(tree == ((void *)0))
  return;
 else if(is_leaf(tree))
 {
  if(tree->parent == ((void *)0))
   ;
  else if(tree->parent->left == tree)
   tree->parent->left = ((void *)0);
  else
   tree->parent->right = ((void *)0);
  free(tree);
  tree = ((void *)0);
 }
 else
 {
  clear(tree->left);
  clear(tree->right);
  free(tree);
  tree = ((void *)0);
 }
}

canonical_original_code:
void clear(struct bstree *tree)
{
 if(tree == ((void *)0))
  return;
 else if(is_leaf(tree))
 {
  if(tree->parent == ((void *)0))
   ;
  else if(tree->parent->left == tree)
   tree->parent->left = ((void *)0);
  else
   tree->parent->right = ((void *)0);
  free(tree);
  tree = ((void *)0);
 }
 else
 {
  clear(tree->left);
  clear(tree->right);
  free(tree);
  tree = ((void *)0);
 }
}


hash

original_code:
static int hash(int hash_code, int hash_base)
{
 int answer = hash_base;
 answer ^= hash_code;
 answer ^= (answer >> 20) ^ (answer >> 12);
 answer ^= (answer >> 7) ^ (answer >> 4);
 return answer;
}

variable_types: answer int


indexForTable

decompiled_code:
__int64 __fastcall indexForTable(int a1, int a2)
{
return a1 & (unsigned int)(a2 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
return a1 & (unsigned int)(a2 - 1);
}

original_code:
static int indexForTable(int hash, int capability)
{
 return hash & (capability-1);
}

variable_types: hash int

variable_types: capability int


getHashBase

original_code:
static int getHashBase()
{
 int distance = sizeof(int)*8-15;
 int hash_base = 0;
 int data[2] = {time(((void *)0)), rand() >> 16};
 int i;
 for(i = 0; i < 2; i++)
 {
  data[i] *= 0xcc9e2d51;
  data[i] = (data[i]>>15) | (data[i]<<distance);
  data[i] *= 0x1b873593;
  hash_base ^= data[i];
  hash_base = (hash_base >> 15) | (hash_base << distance);
  hash_base = hash_base * 5 + 0xe6546b64;
 }
 hash_base ^= hash_base >> 16;
 hash_base *= 0x85ebca6b;
 hash_base ^= hash_base >> 13;
 hash_base *= 0xc2b2ae35;
 hash_base ^= hash_base >> 16;
 return hash_base;
}

variable_types: distance int

variable_types:
data
int [2]


resize

original_code:
static void resize(hashmap_t *map, int new_capability)
{
 if(map->table_capability >= MAX_CAPABILITY)
 {
  map->threshold = map->table_capability;
 }
 hashmap_list_t **new_table = (hashmap_list_t **)malloc(sizeof(hashmap_list_t *) * new_capability);
 memset(new_table, 0, sizeof(hashmap_list_t *)*new_capability);
 int i, index;
 hashmap_list_t *temp, *temp_while;
 for(i = 0; i < map->table_capability; i++)
 {
  temp = map->table[i];
  temp_while = map->table[i];
  while(temp_while != ((void *)0))
  {
   index = indexForTable(temp->hash, new_capability);
   temp_while = temp->next;
   temp->next = new_table[index];
   new_table[index] = temp;
   temp = temp_while;
  }
 }
 temp = temp_while = ((void *)0);
 free(map->table);
 map->table = new_table;
 map->table_capability = new_capability;
 map->threshold = new_capability * DEFAULT_LOAD_FACTOR;
}

canonical_original_code:
static void resize(struct _hashmap *map, int new_capability)
{
 if(map->table_capability >= MAX_CAPABILITY)
 {
  map->threshold = map->table_capability;
 }
 struct _linked_list **new_table = (hashmap_list_t **)malloc(sizeof(hashmap_list_t *) * new_capability);
 memset(new_table, 0, sizeof(struct _linked_list *)*new_capability);
 int i; int index;
 struct _linked_list *temp; struct _linked_list *temp_while;
 for(i = 0; i < map->table_capability; i++)
 {
  temp = map->table[i];
  temp_while = map->table[i];
  while(temp_while != ((void *)0))
  {
   index = indexForTable(temp->hash, new_capability);
   temp_while = temp->next;
   temp->next = new_table[index];
   new_table[index] = temp;
   temp = temp_while;
  }
 }
 temp = temp_while = ((void *)0);
 free(map->table);
 map->table = new_table;
 map->table_capability = new_capability;
 map->threshold = new_capability * DEFAULT_LOAD_FACTOR;
}

variable_types: struct _hashmap { int size; int threshold; int hash_base; int table_capability; int (*hash_code)(int); struct _linked_list { int hash; int key; int value; struct _linked_list *next; } **table; } *

variable_types: index int

user_defined_types: struct _hashmap { int size; int threshold; int hash_base; int table_capability; int (*hash_code)(int); struct _linked_list **table; }


original_code:
int hashmap_put(hashmap_t *map, int key, int value)
{
 if(map == ((void *)0) || map->table == ((void *)0))
  return -2;
 if(map->size >= (map->threshold-1))
  resize(map, 2*map->table_capability);
 int hash_num = hash((*(map->hash_code))(key), map->hash_base);
 int index = indexForTable(hash_num, map->table_capability);
 printf("key : %d, index : %d\n", key, index);
 if(map->table[index] != ((void *)0))
 {
  hashmap_list_t *temp;
  for(temp = map->table[index]; temp != ((void *)0); temp = temp->next)
  {
   if(temp->hash == hash_num && temp->key == key)
   {
    temp->value = value;
    return -1;
   }
  }
 }
 hashmap_list_t * newNode = (hashmap_list_t *)malloc(sizeof(hashmap_list_t));
 newNode->hash = hash_num;
 newNode->key = key;
 newNode->value = value;
 newNode->next = map->table[index];
 map->table[index] = newNode;
 map->size++;
 return index;
}

canonical_original_code:
int hashmap_put(struct _hashmap *map, int key, int value)
{
 if(map == ((void *)0) || map->table == ((void *)0))
  return -2;
 if(map->size >= (map->threshold-1))
  resize(map, 2*map->table_capability);
 int hash_num = hash((*(map->hash_code))(key), map->hash_base);
 int index = indexForTable(hash_num, map->table_capability);
 printf("key : %d, index : %d\n", key, index);
 if(map->table[index] != ((void *)0))
 {
  struct _linked_list *temp;
  for(temp = map->table[index]; temp != ((void *)0); temp = temp->next)
  {
   if(temp->hash == hash_num && temp->key == key)
   {
    temp->value = value;
    return -1;
   }
  }
 }
 struct _linked_list *newNode = (hashmap_list_t *)malloc(sizeof(hashmap_list_t));
 newNode->hash = hash_num;
 newNode->key = key;
 newNode->value = value;
 newNode->next = map->table[index];
 map->table[index] = newNode;
 map->size++;
 return index;
}

variable_types: key int

variable_types: value int

variable_types: struct _linked_list { int hash; int key; int value; struct _linked_list *next; } *


decompiled_code:
__int64 __fastcall hashmap_contains(__int64 a1, unsigned int a2, int a3)
{
int v3; // er13
int v5; // eax
int v6; // eax
__int64 v7; // rax
int v8; // edx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !*(_QWORD *)(a1 + 24) )
return 0xFFFFFFFFLL;
v3 = *(_DWORD *)(a1 + 8);
v5 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 16))(a2);
v6 = hash(v5, v3);
v7 = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * (int)indexForTable(v6, *(_DWORD *)(a1 + 12)));
if ( !v7 )
return 1LL;
while ( *(_DWORD *)v7 != v8 || *(_DWORD *)(v7 + 4) != a2 || *(_DWORD *)(v7 + 8) != a3 )
{
v7 = *(_QWORD *)(v7 + 16);
if ( !v7 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, unsigned int a2, int a3)
{
int v3; // er13
int v5; // eax
int v6; // eax
__int64 v7; // rax
int v8; // edx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !*(_QWORD *)(a1 + 24) )
return 0xFFFFFFFFLL;
v3 = *(_DWORD *)(a1 + 8);
v5 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 16))(a2);
v6 = func6(v5, v3);
v7 = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * (int)func7(v6, *(_DWORD *)(a1 + 12)));
if ( !v7 )
return 1LL;
while ( *(_DWORD *)v7 != v8 || *(_DWORD *)(v7 + 4) != a2 || *(_DWORD *)(v7 + 8) != a3 )
{
v7 = *(_QWORD *)(v7 + 16);
if ( !v7 )
return 1LL;
}
return 0LL;
}

original_code:
int hashmap_contains(hashmap_t *map, int key, int value)
{
 if(map == ((void *)0) || map->table == ((void *)0))
  return -1;
 int hash_num = hash((*(map->hash_code))(key), map->hash_base);
 int index = indexForTable(hash_num, map->table_capability);
 hashmap_list_t *temp;
 for(temp = map->table[index]; temp != ((void *)0); temp = temp->next)
  if(temp->hash == hash_num && temp->key == key && temp->value == value)
   return 0;
 return 1;
}

canonical_original_code:
int hashmap_contains(struct _hashmap *map, int key, int value)
{
 if(map == ((void *)0) || map->table == ((void *)0))
  return -1;
 int hash_num = hash((*(map->hash_code))(key), map->hash_base);
 int index = indexForTable(hash_num, map->table_capability);
 struct _linked_list *temp;
 for(temp = map->table[index]; temp != ((void *)0); temp = temp->next)
  if(temp->hash == hash_num && temp->key == key && temp->value == value)
   return 0;
 return 1;
}

user_defined_types: struct _linked_list { int hash; int key; int value; struct _linked_list *next; }


original_code:
void hashmap_remove(hashmap_t *map, int key)
{
 if(map == ((void *)0) || map->table == ((void *)0))
  return;
 int hash_num = hash((*(map->hash_code))(key), map->hash_base);
 int index = indexForTable(hash_num, map->table_capability);
 hashmap_list_t *temp = map->table[index], *prev = map->table[index];
 if(temp != ((void *)0) && temp->hash == hash_num && temp->key == key)
 {
  map->table[index] = temp->next;
  free(temp);
 }
 else
 {
  for(temp = map->table[index]->next; temp != ((void *)0); temp = temp->next)
  {
   if(temp->hash == hash_num && temp->key == key)
   {
    prev->next = temp->next;
    free(temp);
    break;
   }
   prev = prev->next;
  }
 }
}

canonical_original_code:
void hashmap_remove(struct _hashmap *map, int key)
{
 if(map == ((void *)0) || map->table == ((void *)0))
  return;
 int hash_num = hash((*(map->hash_code))(key), map->hash_base);
 int index = indexForTable(hash_num, map->table_capability);
 struct _linked_list *temp = map->table[index]; struct _linked_list *prev = map->table[index];
 if(temp != ((void *)0) && temp->hash == hash_num && temp->key == key)
 {
  map->table[index] = temp->next;
  free(temp);
 }
 else
 {
  for(temp = map->table[index]->next; temp != ((void *)0); temp = temp->next)
  {
   if(temp->hash == hash_num && temp->key == key)
   {
    prev->next = temp->next;
    free(temp);
    break;
   }
   prev = prev->next;
  }
 }
}


decompiled_code:
__int64 __fastcall hashmap_get(__int64 a1, unsigned int a2)
{
int v2; // er12
int v3; // eax
int v4; // eax
__int64 v5; // rax
int v6; // edx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !*(_QWORD *)(a1 + 24) )
return 0xFFFFFFFFLL;
v2 = *(_DWORD *)(a1 + 8);
v3 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 16))(a2);
v4 = hash(v3, v2);
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * (int)indexForTable(v4, *(_DWORD *)(a1 + 12)));
if ( !v5 )
return 0xFFFFFFFFLL;
while ( *(_DWORD *)v5 != v6 || *(_DWORD *)(v5 + 4) != a2 )
{
v5 = *(_QWORD *)(v5 + 16);
if ( !v5 )
return 0xFFFFFFFFLL;
}
return *(unsigned int *)(v5 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, unsigned int a2)
{
int v2; // er12
int v3; // eax
int v4; // eax
__int64 v5; // rax
int v6; // edx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !*(_QWORD *)(a1 + 24) )
return 0xFFFFFFFFLL;
v2 = *(_DWORD *)(a1 + 8);
v3 = (*(__int64 (__fastcall **)(_QWORD))(a1 + 16))(a2);
v4 = func6(v3, v2);
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * (int)func7(v4, *(_DWORD *)(a1 + 12)));
if ( !v5 )
return 0xFFFFFFFFLL;
while ( *(_DWORD *)v5 != v6 || *(_DWORD *)(v5 + 4) != a2 )
{
v5 = *(_QWORD *)(v5 + 16);
if ( !v5 )
return 0xFFFFFFFFLL;
}
return *(unsigned int *)(v5 + 8);
}

original_code:
int hashmap_get(hashmap_t *map, int key)
{
 if(map == ((void *)0) || map->table == ((void *)0))
  return -1;
 int hash_num = hash((*(map->hash_code))(key), map->hash_base);
 int index = indexForTable(hash_num, map->table_capability);
 if(map->table[index] != ((void *)0))
 {
  hashmap_list_t *temp;
  for(temp = map->table[index]; temp != ((void *)0); temp = temp->next)
   if(temp->hash == hash_num && temp->key == key)
    return temp->value;
 }
 return -1;
}

canonical_original_code:
int hashmap_get(struct _hashmap *map, int key)
{
 if(map == ((void *)0) || map->table == ((void *)0))
  return -1;
 int hash_num = hash((*(map->hash_code))(key), map->hash_base);
 int index = indexForTable(hash_num, map->table_capability);
 if(map->table[index] != ((void *)0))
 {
  struct _linked_list *temp;
  for(temp = map->table[index]; temp != ((void *)0); temp = temp->next)
   if(temp->hash == hash_num && temp->key == key)
    return temp->value;
 }
 return -1;
}


decompiled_code:
void __fastcall hashmap_free(void *ptr)
{
__int64 v2; // rdx
__int64 i; // rbp
_QWORD *v4; // rax
_QWORD *v5; // rbx
void *v6; // rdi

if ( ptr )
{
v2 = *((_QWORD *)ptr + 3);
if ( v2 )
{
for ( i = 0LL; *((_DWORD *)ptr + 3) > (int)i; ++i )
{
v4 = (_QWORD *)(v2 + 8 * i);
v5 = (_QWORD *)*v4;
if ( *v4 )
{
do
{
v6 = v5;
v5 = (_QWORD *)v5[2];
free(v6);
}
while ( v5 );
v2 = *((_QWORD *)ptr + 3);
v4 = (_QWORD *)(v2 + 8 * i);
}
*v4 = 0LL;
}
free(ptr);
}
}
}

original_code:
void hashmap_free(hashmap_t *map)
{
 if(map == ((void *)0) || map->table == ((void *)0))
  return;
 int i;
 hashmap_list_t *temp1, *temp2;
 for(i = 0; i < map->table_capability; i++)
 {
  temp1 = map->table[i];
  temp2 = temp1;
  while(temp2 != ((void *)0))
  {
   temp2 = temp2->next;
   free(temp1);
   temp1 = temp2;
  }
  map->table[i] = ((void *)0);
 }
 free(map);
 map = ((void *)0);
}

canonical_original_code:
void hashmap_free(struct _hashmap *map)
{
 if(map == ((void *)0) || map->table == ((void *)0))
  return;
 int i;
 struct _linked_list *temp1; struct _linked_list *temp2;
 for(i = 0; i < map->table_capability; i++)
 {
  temp1 = map->table[i];
  temp2 = temp1;
  while(temp2 != ((void *)0))
  {
   temp2 = temp2->next;
   free(temp1);
   temp1 = temp2;
  }
  map->table[i] = ((void *)0);
 }
 free(map);
 map = ((void *)0);
}


hashCode

decompiled_code:
__int64 __fastcall hashCode(int a1)
{
return (unsigned int)(a1 + 31);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return (unsigned int)(a1 + 31);
}

original_code:
static int hashCode(item_type item)
{
 return 31 + item;
}

canonical_original_code:
static int hashCode(int item)
{
 return 31 + item;
}

variable_types: item int


insert

original_code:
void insert(list *l, item_type item)
{
 if(l == ((void *)0))
  return;
 list * newItem = (list *)malloc(sizeof(list));
 newItem->item = item;
 newItem->next = l->next;
 l->next = newItem;
}

canonical_original_code:
void insert(struct _list *l, int item)
{
 if(l == ((void *)0))
  return;
 struct _list *newItem = (list *)malloc(sizeof(list));
 newItem->item = item;
 newItem->next = l->next;
 l->next = newItem;
}

variable_types: struct _list { int item; struct _list *next; } *


findItem

decompiled_code:
__int64 __fastcall findItem(__int64 a1, int a2)
{
while ( 1 )
{
if ( !a1 )
return 0LL;
if ( *(_DWORD *)a1 == a2 )
break;
a1 = *(_QWORD *)(a1 + 8);
}
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
while ( 1 )
{
if ( !a1 )
return 0LL;
if ( *(_DWORD *)a1 == a2 )
break;
a1 = *(_QWORD *)(a1 + 8);
}
return a1;
}

original_code:
list *findItem(list *l, item_type item)
{
 if(l == ((void *)0))
  return ((void *)0);
 list *p = l;
 while(p != ((void *)0))
 {
  if(p->item == item)
   return p;
  else
   p = p->next;
 }
 return ((void *)0);
}

canonical_original_code:
struct _list *findItem(struct _list *l, int item)
{
 if(l == ((void *)0))
  return ((void *)0);
 struct _list *p = l;
 while(p != ((void *)0))
 {
  if(p->item == item)
   return p;
  else
   p = p->next;
 }
 return ((void *)0);
}

variable_types:
l
struct _list *

return_type: struct _list { int item; struct _list *next; } *


findPredecessor

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

if ( a1 )
{
while ( 1 )
{
result = a1;
a1 = *(_QWORD *)(a1 + 8);
if ( !a1 )
break;
if ( *(_DWORD *)a1 == a2 )
return result;
}
}
return 0LL;
}

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

if ( a1 )
{
while ( 1 )
{
result = a1;
a1 = *(_QWORD *)(a1 + 8);
if ( !a1 )
break;
if ( *(_DWORD *)a1 == a2 )
return result;
}
}
return 0LL;
}

original_code:
list *findPredecessor(list *l, item_type x)
{
 if(l == ((void *)0))
  return ((void *)0);
 list * p = l;
 while(p->next != ((void *)0))
 {
  if(p->next->item == x)
   return p;
  else
   p = p->next;
 }
 return ((void *)0);
}

canonical_original_code:
struct _list *findPredecessor(struct _list *l, int x)
{
 if(l == ((void *)0))
  return ((void *)0);
 struct _list *p = l;
 while(p->next != ((void *)0))
 {
  if(p->next->item == x)
   return p;
  else
   p = p->next;
 }
 return ((void *)0);
}

variable_types: x int

user_defined_types: struct _list { int item; struct _list *next; }


deleteItem

decompiled_code:
void __fastcall deleteItem(__int64 a1, int a2)
{
__int64 v2; // rax
_QWORD *v3; // rdi

if ( a1 )
{
v2 = findPredecessor(a1, a2);
if ( v2 )
{
v3 = *(_QWORD **)(v2 + 8);
*(_QWORD *)(v2 + 8) = v3[1];
free(v3);
}
}
}

original_code:
void deleteItem(list *l, item_type x)
{
 if(l == ((void *)0))
  return;
 list *p = findPredecessor(l,x);
 if(p != ((void *)0))
 {
  list *old = p->next;
  p->next = old->next;
  free(old);
  old = ((void *)0);
 }
}

canonical_original_code:
void deleteItem(struct _list *l, int x)
{
 if(l == ((void *)0))
  return;
 struct _list *p = findPredecessor(l,x);
 if(p != ((void *)0))
 {
  struct _list *old = p->next;
  p->next = old->next;
  free(old);
  old = ((void *)0);
 }
}


freeList

original_code:
void freeList(list *l)
{
 if(l != ((void *)0))
 {
  list *temp = ((void *)0);
  while(l->next != ((void *)0))
  {
   temp = l->next;
   l->next = temp->next;
   free(temp);
   temp = ((void *)0);
  }
 }
}

canonical_original_code:
void freeList(struct _list *l)
{
 if(l != ((void *)0))
 {
  struct _list *temp = ((void *)0);
  while(l->next != ((void *)0))
  {
   temp = l->next;
   l->next = temp->next;
   free(temp);
   temp = ((void *)0);
  }
 }
}


printList

decompiled_code:
void __fastcall printList(__int64 a1)
{
__int64 v1; // rbx
unsigned int *v2; // rax

if ( a1 )
{
v1 = a1;
do
{
v2 = *(unsigned int **)(v1 + 8);
if ( !v2 )
break;
printf("%d\n", *v2);
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1)
{
__int64 v1; // rbx
unsigned int *v2; // rax

if ( a1 )
{
v1 = a1;
do
{
v2 = *(unsigned int **)(v1 + 8);
if ( !v2 )
break;
printf("%d\n", *v2);
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
}
}

original_code:
void printList(list *l)
{
 if(l == ((void *)0) || l->next == ((void *)0))
  return;
 printf("%d\n", l->next->item);
 printList(l->next);
}

canonical_original_code:
void printList(struct _list *l)
{
 if(l == ((void *)0) || l->next == ((void *)0))
  return;
 printf("%d\n", l->next->item);
 printList(l->next);
}


reverseList

original_code:
void reverseList(list *l)
{
 if(l == ((void *)0))
  return;
 list *p1 = l->next;
 list *p2 = p1->next;
 p1->next = ((void *)0);
 list *pTemp = p2;
 while(pTemp != ((void *)0))
 {
  pTemp = p2->next;
  p2->next = p1;
  p1 = p2;
  p2 = pTemp;
 }
 l->next = p1;
}

canonical_original_code:
void reverseList(struct _list *l)
{
 if(l == ((void *)0))
  return;
 struct _list *p1 = l->next;
 struct _list *p2 = p1->next;
 p1->next = ((void *)0);
 struct _list *pTemp = p2;
 while(pTemp != ((void *)0))
 {
  pTemp = p2->next;
  p2->next = p1;
  p1 = p2;
  p2 = pTemp;
 }
 l->next = p1;
}


decompiled_code:
int __fastcall remove_duplication(__int64 a1)
{
int v2; // er12
__int64 v3; // rbp
unsigned int *v4; // rax
int v5; // eax

v2 = clock();
v3 = hashmap_initialize(13LL, hashCode);
while ( 1 )
{
v4 = *(unsigned int **)(a1 + 8);
if ( !v4 )
break;
while ( (int)hashmap_put(v3, *v4, 0LL) >= 0 )
{
a1 = *(_QWORD *)(a1 + 8);
v4 = *(unsigned int **)(a1 + 8);
if ( !v4 )
goto LABEL_5;
}
deleteItem(a1, **(_DWORD **)(a1 + 8));
}
LABEL_5:
v5 = clock() - v2;
return printf("Total time taken by CPU is %f\n", (double)v5 / 1000000.0);
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1)
{
int v2; // er12
__int64 v3; // rbp
unsigned int *v4; // rax
int v5; // eax

v2 = clock();
v3 = hashmap_initialize(13LL, hashCode);
while ( 1 )
{
v4 = *(unsigned int **)(a1 + 8);
if ( !v4 )
break;
while ( (int)hashmap_put(v3, *v4, 0LL) >= 0 )
{
a1 = *(_QWORD *)(a1 + 8);
v4 = *(unsigned int **)(a1 + 8);
if ( !v4 )
goto LABEL_5;
}
func4(a1, **(_DWORD **)(a1 + 8));
}
LABEL_5:
v5 = clock() - v2;
return printf("Total time taken by CPU is %f\n", (double)v5 / 1000000.0);
}

original_code:
void remove_duplication(list *l)
{
 clock_t start = clock();
 hashmap_t *map = hashmap_initialize(13, &hashCode);
 while(l->next != ((void *)0))
 {
  if(hashmap_put(map, l->next->item, 0) < 0 )
   deleteItem(l, l->next->item);
  else
   l = l->next;
 }
 clock_t end = clock();
 printf("Total time taken by CPU is %f\n", (double)(end-start)/((__clock_t) 1000000));
}

canonical_original_code:
void remove_duplication(struct _list *l)
{
 long start = clock();
 struct _hashmap *map = hashmap_initialize(13, &hashCode);
 while(l->next != ((void *)0))
 {
  if(hashmap_put(map, l->next->item, 0) < 0 )
   deleteItem(l, l->next->item);
  else
   l = l->next;
 }
 long end = clock();
 printf("Total time taken by CPU is %f\n", (double)(end-start)/((long) 1000000));
}

variable_types: start long

variable_types: end long


decompiled_code:
void __fastcall free_queue(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void free_queue(priority_queue *queue)
{
 free(queue->item);
 free(queue);
 queue = ((void *)0);
}

canonical_original_code:
void free_queue(struct priority_queue *queue)
{
 free(queue->item);
 free(queue);
 queue = ((void *)0);
}

variable_types: struct priority_queue { int *item; int length; int capability; } *


dequeue

decompiled_code:
__int64 __fastcall dequeue(int *a1)
{
__int64 v1; // rax
__int64 v2; // r8
unsigned int v3; // er12

v1 = a1[2];
if ( !(_DWORD)v1 )
return (unsigned int)-1;
v2 = *(_QWORD *)a1;
v3 = *(_DWORD *)(*(_QWORD *)a1 + 4LL);
a1[2] = v1 - 1;
*(_DWORD *)(v2 + 4) = *(_DWORD *)(v2 + 4 * v1);
percolate_down_constprop_0_isra_0(v2, a1[2]);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1)
{
__int64 v1; // rax
__int64 v2; // r8
unsigned int v3; // er12

v1 = a1[2];
if ( !(_DWORD)v1 )
return (unsigned int)-1;
v2 = *(_QWORD *)a1;
v3 = *(_DWORD *)(*(_QWORD *)a1 + 4LL);
a1[2] = v1 - 1;
*(_DWORD *)(v2 + 4) = *(_DWORD *)(v2 + 4 * v1);
percolate_down_constprop_0_isra_0(v2, a1[2]);
return v3;
}

original_code:
item_type dequeue(priority_queue *queue)
{
 if(queue->length == 0)
  return -1;
 int answer = queue->item[1];
 queue->item[1] = queue->item[queue->length--];
 percolate_down(queue, 1);
 return answer;
}

canonical_original_code:
int dequeue(struct priority_queue *queue)
{
 if(queue->length == 0)
  return -1;
 int answer = queue->item[1];
 queue->item[1] = queue->item[queue->length--];
 percolate_down(queue, 1);
 return answer;
}

variable_types: answer int

user_defined_types: struct priority_queue { int *item; int length; int capability; }


subFindByName

repo: norly/centaur

decompiled_code:
__int64 __fastcall subFindByName(__int64 a1, __int64 a2, const char *a3)
{
const char *v4; // rax
__int64 result; // rax

if ( ((__int64 (*)(void))elfu_mScnName)() && (v4 = (const char *)elfu_mScnName(a1, a2), !strcmp(v4, a3)) )
result = a2;
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, const char *a3)
{
const char *v4; // rax
__int64 result; // rax

if ( ((__int64 (*)(void))elfu_mScnName)() && (v4 = (const char *)elfu_mScnName(a1, a2), !strcmp(v4, a3)) )
result = a2;
else
result = 0LL;
return result;
}

original_code:
static void* subFindByName(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
{
  char *name = (char*)aux1;
  (void)aux2;
  if (elfu_mScnName(me, ms)) {
    if (!strcmp(elfu_mScnName(me, ms), name)) {
      return ms;
    }
  }
  return ((void *)0);
}

canonical_original_code:
static void *subFindByName(struct ElfuElf *me, struct ElfuScn *ms, void *aux1, void *aux2)
{
  char *name = (char*)aux1;
  (void)aux2;
  if (elfu_mScnName(me, ms)) {
    if (!strcmp(elfu_mScnName(me, ms), name)) {
      return ms;
    }
  }
  return ((void *)0);
}

variable_types: struct ElfuElf { int elfclass; struct Elf64_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long e_entry; unsigned long e_phoff; unsigned long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } ehdr; struct PhdrList { struct ElfuPhdr { struct Elf64_Phdr { unsigned int p_type; unsigned int p_flags; unsigned long p_offset; unsigned long p_vaddr; unsigned long p_paddr; unsigned long p_filesz; unsigned long p_memsz; unsigned long p_align; } phdr; struct ChildScnList { struct ElfuScn { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } shdr; char *databuf; struct ElfuScn *linkptr; struct ElfuScn *infoptr; struct ElfuScn *oldptr; struct ElfuSymtab { struct Syms { struct ElfuSym { unsigned int name; unsigned long value; unsigned int size; unsigned char bind; unsigned char type; unsigned char other; struct ElfuScn *scnptr; int shndx; struct { struct ElfuSym *cqe_next; struct ElfuSym *cqe_prev; } elem; } *cqh_first; struct ElfuSym *cqh_last; } syms; } symtab; struct ElfuReltab { struct Rels { struct ElfuRel { unsigned long offset; unsigned int sym; unsigned char type; int addendUsed; int addend; struct { struct ElfuRel *cqe_next; struct ElfuRel *cqe_prev; } elem; } *cqh_first; struct ElfuRel *cqh_last; } rels; } reltab; struct { struct ElfuScn *cqe_next; struct ElfuScn *cqe_prev; } elemChildScn; } *cqh_first; struct ElfuScn *cqh_last; } childScnList; struct ChildPhdrList { struct ElfuPhdr *cqh_first; struct ElfuPhdr *cqh_last; } childPhdrList; struct { struct ElfuPhdr *cqe_next; struct ElfuPhdr *cqe_prev; } elemChildPhdr; struct { struct ElfuPhdr *cqe_next; struct ElfuPhdr *cqe_prev; } elem; } *cqh_first; struct ElfuPhdr *cqh_last; } phdrList; struct OrphanScnList { struct ElfuScn *cqh_first; struct ElfuScn *cqh_last; } orphanScnList; struct ElfuScn *shstrtab; struct ElfuScn *symtab; } *

variable_types:
ms
struct ElfuScn *

variable_types:
name
char *

return_type: void *

user_defined_types: struct ElfuElf { int elfclass; struct Elf64_Ehdr ehdr; struct PhdrList phdrList; struct OrphanScnList orphanScnList; struct ElfuScn *shstrtab; struct ElfuScn *symtab; }

user_defined_types: struct PhdrList { struct ElfuPhdr *cqh_first; struct ElfuPhdr *cqh_last; }

user_defined_types: struct ElfuPhdr { struct Elf64_Phdr phdr; struct ChildScnList childScnList; struct ChildPhdrList childPhdrList; struct { struct ElfuPhdr *cqe_next; struct ElfuPhdr *cqe_prev; } elemChildPhdr; struct { struct ElfuPhdr *cqe_next; struct ElfuPhdr *cqe_prev; } elem; }

user_defined_types: struct ChildScnList { struct ElfuScn *cqh_first; struct ElfuScn *cqh_last; }

user_defined_types: struct ElfuScn { struct Elf64_Shdr shdr; char *databuf; struct ElfuScn *linkptr; struct ElfuScn *infoptr; struct ElfuScn *oldptr; struct ElfuSymtab symtab; struct ElfuReltab reltab; struct { struct ElfuScn *cqe_next; struct ElfuScn *cqe_prev; } elemChildScn; }

user_defined_types: struct Syms { struct ElfuSym *cqh_first; struct ElfuSym *cqh_last; }

user_defined_types: struct Rels { struct ElfuRel *cqh_first; struct ElfuRel *cqh_last; }


decompiled_code:
__int64 __fastcall elfu_mDynLookupPltAddrByName(_DWORD *a1, const char *a2, _QWORD *a3)
{
__int64 v3; // rbx
__int64 v4; // r13
__int64 v5; // r15
__int64 v6; // rbx
unsigned int v7; // er14
unsigned int v8; // ebp
unsigned int v9; // ecx
unsigned int *v10; // rdx
int i; // eax
__int64 v12; // rbx
unsigned int v13; // er9
__int64 v15; // [rsp+8h] [rbp-50h]

v3 = elfu_mScnForall(a1, subFindByName, ".rel.plt", 0LL);
if ( v3 || (v3 = elfu_mScnForall(a1, subFindByName, ".rela.plt", 0LL)) != 0 )
{
v15 = elfu_mScnForall(a1, subFindByName, ".plt", 0LL);
if ( v15 )
{
v4 = *(_QWORD *)(v3 + 72);
if ( !v4 )
_assert_fail("relplt->linkptr", "src/libelfu/modelops/dynlookup.c", 0x43u, "elfu_mDynLookupPltAddrByName");
v5 = *(_QWORD *)(v3 + 112);
v6 = v3 + 112;
if ( v6 == v5 )
{
LABEL_14:
fprintf(stderr, "elfu_mDynLookupPltAddr: Could not find symbol '%s' in destination ELF.\n", a2);
v13 = -1;
}
else
{
v7 = 0;
v8 = *a1 - 1;
while ( 1 )
{
++v7;
if ( v8 > 1 || *(_BYTE *)(v5 + 12) == 7 )
{
v9 = *(_DWORD *)(v5 + 8);
v10 = *(unsigned int **)(v4 + 96);
if ( v9 > 1 )
{
for ( i = 1; i != v9; ++i )
v10 = (unsigned int *)*((_QWORD *)v10 + 5);
}
if ( !strcmp((const char *)(*(_QWORD *)(*(_QWORD *)(v4 + 72) + 64LL) + *v10), a2) )
break;
}
v5 = *(_QWORD *)(v5 + 24);
if ( v6 == v5 )
goto LABEL_14;
}
v12 = *(_QWORD *)(v15 + 16) + 16 * v7;
fprintf(
stdout,
"elfu_mDynLookupPltAddr: Guessing symbol '%s' is in destination memory at %x (PLT entry #%u).\n",
a2,
*(_DWORD *)(v15 + 16) + 16 * v7,
v7);
v13 = 0;
*a3 = v12;
}
}
else
{
fwrite("elfu_mDynLookupPltAddr: Could not find .plt section in destination ELF.\n", 1uLL, 0x48uLL, stderr);
v13 = -1;
}
}
else
{
fwrite("elfu_mDynLookupPltAddr: Could not find .rel.plt section in destination ELF.\n", 1uLL, 0x4CuLL, stderr);
v13 = -1;
}
return v13;
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1, const char *a2, _QWORD *a3)
{
__int64 v3; // rbx
__int64 v4; // r13
__int64 v5; // r15
__int64 v6; // rbx
unsigned int v7; // er14
unsigned int v8; // ebp
unsigned int v9; // ecx
unsigned int *v10; // rdx
int i; // eax
__int64 v12; // rbx
unsigned int v13; // er9
__int64 v15; // [rsp+8h] [rbp-50h]

v3 = elfu_mScnForall(a1, subFindByName, ".rel.plt", 0LL);
if ( v3 || (v3 = elfu_mScnForall(a1, subFindByName, ".rela.plt", 0LL)) != 0 )
{
v15 = elfu_mScnForall(a1, subFindByName, ".plt", 0LL);
if ( v15 )
{
v4 = *(_QWORD *)(v3 + 72);
if ( !v4 )
_assert_fail("relplt->linkptr", "src/libelfu/modelops/dynlookup.c", 0x43u, "elfu_mDynLookupPltAddrByName");
v5 = *(_QWORD *)(v3 + 112);
v6 = v3 + 112;
if ( v6 == v5 )
{
LABEL_14:
fprintf(stderr, "elfu_mDynLookupPltAddr: Could not find symbol '%s' in destination ELF.\n", a2);
v13 = -1;
}
else
{
v7 = 0;
v8 = *a1 - 1;
while ( 1 )
{
++v7;
if ( v8 > 1 || *(_BYTE *)(v5 + 12) == 7 )
{
v9 = *(_DWORD *)(v5 + 8);
v10 = *(unsigned int **)(v4 + 96);
if ( v9 > 1 )
{
for ( i = 1; i != v9; ++i )
v10 = (unsigned int *)*((_QWORD *)v10 + 5);
}
if ( !strcmp((const char *)(*(_QWORD *)(*(_QWORD *)(v4 + 72) + 64LL) + *v10), a2) )
break;
}
v5 = *(_QWORD *)(v5 + 24);
if ( v6 == v5 )
goto LABEL_14;
}
v12 = *(_QWORD *)(v15 + 16) + 16 * v7;
fprintf(
stdout,
"elfu_mDynLookupPltAddr: Guessing symbol '%s' is in destination memory at %x (PLT entry #%u).\n",
a2,
*(_DWORD *)(v15 + 16) + 16 * v7,
v7);
v13 = 0;
*a3 = v12;
}
}
else
{
fwrite("elfu_mDynLookupPltAddr: Could not find .plt section in destination ELF.\n", 1uLL, 0x48uLL, stderr);
v13 = -1;
}
}
else
{
fwrite("elfu_mDynLookupPltAddr: Could not find .rel.plt section in destination ELF.\n", 1uLL, 0x4CuLL, stderr);
v13 = -1;
}
return v13;
}

original_code:
int elfu_mDynLookupPltAddrByName(ElfuElf *me, char *name, GElf_Addr *result)
{
  ElfuScn *relplt;
  ElfuScn *plt;
  ElfuRel *rel;
  GElf_Word j;
  relplt = elfu_mScnForall(me, subFindByName, ".rel.plt", ((void *)0));
  if (!relplt) {
    relplt = elfu_mScnForall(me, subFindByName, ".rela.plt", ((void *)0));
  }
  if (!relplt) {
    do { fprintf(stderr, "elfu_mDynLookupPltAddr: Could not find .rel.plt section in destination ELF.\n"); } while(0);
    return -1;
  }
  plt = elfu_mScnForall(me, subFindByName, ".plt", ((void *)0));
  if (!plt) {
    do { fprintf(stderr, "elfu_mDynLookupPltAddr: Could not find .plt section in destination ELF.\n"); } while(0);
    return -1;
  }
  ((void) sizeof ((relplt->linkptr) ? 1 : 0), __extension__ ({ if (relplt->linkptr) ; else __assert_fail ("relplt->linkptr", "src/libelfu/modelops/dynlookup.c", 67, __extension__ __PRETTY_FUNCTION__); }));
  j = 0;
  for ((rel) = ((&relplt->reltab.rels)->cqh_first); (rel) != (const void *)(&relplt->reltab.rels); (rel) = ((rel)->elem.cqe_next)) {
    GElf_Word i;
    ElfuSym *sym;
    char *symname;
    j++;
    if ((me->elfclass == 1 && rel->type != 7)
        || (me->elfclass == 2 && rel->type != 7)) {
      continue;
    }
    sym = ((&relplt->linkptr->symtab.syms)->cqh_first);
    for (i = 1; i < rel->sym; i++) {
      sym = ((sym)->elem.cqe_next);
    }
    symname = ((relplt->linkptr)->linkptr->databuf + (sym->name));
    if (!strcmp(symname, name)) {
      GElf_Addr addr = plt->shdr.sh_addr + (16 * j);
      do { fprintf(stdout, "elfu_mDynLookupPltAddr: Guessing symbol '%s' is in destination memory at %x (PLT entry #%u).\n", name, (unsigned)addr, j); } while(0);
      *result = addr;
      return 0;
    }
  }
  do { fprintf(stderr, "elfu_mDynLookupPltAddr: Could not find symbol '%s' in destination ELF.\n", name); } while(0);
  return -1;
}

canonical_original_code:
int elfu_mDynLookupPltAddrByName(struct ElfuElf *me, char *name, unsigned long *result)
{
  struct ElfuScn *relplt;
  struct ElfuScn *plt;
  struct ElfuRel *rel;
  unsigned int j;
  relplt = elfu_mScnForall(me, subFindByName, ".rel.plt", ((void *)0));
  if (!relplt) {
    relplt = elfu_mScnForall(me, subFindByName, ".rela.plt", ((void *)0));
  }
  if (!relplt) {
    do { fprintf(stderr, "elfu_mDynLookupPltAddr: Could not find .rel.plt section in destination ELF.\n"); } while(0);
    return -1;
  }
  plt = elfu_mScnForall(me, subFindByName, ".plt", ((void *)0));
  if (!plt) {
    do { fprintf(stderr, "elfu_mDynLookupPltAddr: Could not find .plt section in destination ELF.\n"); } while(0);
    return -1;
  }
  ((void) sizeof ((relplt->linkptr) ? 1 : 0), __extension__ ({ if (relplt->linkptr) ; else __assert_fail ("relplt->linkptr", "src/libelfu/modelops/dynlookup.c", 67, __extension__ __PRETTY_FUNCTION__); }));
  j = 0;
  for ((rel) = ((&relplt->reltab.rels)->cqh_first); (rel) != (void *)(&relplt->reltab.rels); (rel) = ((rel)->elem.cqe_next)) {
    unsigned int i;
    struct ElfuSym *sym;
    char *symname;
    j++;
    if ((me->elfclass == 1 && rel->type != 7)
        || (me->elfclass == 2 && rel->type != 7)) {
      continue;
    }
    sym = ((&relplt->linkptr->symtab.syms)->cqh_first);
    for (i = 1; i < rel->sym; i++) {
      sym = ((sym)->elem.cqe_next);
    }
    symname = ((relplt->linkptr)->linkptr->databuf + (sym->name));
    if (!strcmp(symname, name)) {
      unsigned long addr = plt->shdr.sh_addr + (16 * j);
      do { fprintf(stdout, "elfu_mDynLookupPltAddr: Guessing symbol '%s' is in destination memory at %x (PLT entry #%u).\n", name, (unsigned int)addr, j); } while(0);
      *result = addr;
      return 0;
    }
  }
  do { fprintf(stderr, "elfu_mDynLookupPltAddr: Could not find symbol '%s' in destination ELF.\n", name); } while(0);
  return -1;
}

variable_types:
result
unsigned long *

variable_types: struct ElfuScn { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } shdr; char *databuf; struct ElfuScn *linkptr; struct ElfuScn *infoptr; struct ElfuScn *oldptr; struct ElfuSymtab { struct Syms { struct ElfuSym { unsigned int name; unsigned long value; unsigned int size; unsigned char bind; unsigned char type; unsigned char other; struct ElfuScn *scnptr; int shndx; struct { struct ElfuSym *cqe_next; struct ElfuSym *cqe_prev; } elem; } *cqh_first; struct ElfuSym *cqh_last; } syms; } symtab; struct ElfuReltab { struct Rels { struct ElfuRel { unsigned long offset; unsigned int sym; unsigned char type; int addendUsed; int addend; struct { struct ElfuRel *cqe_next; struct ElfuRel *cqe_prev; } elem; } *cqh_first; struct ElfuRel *cqh_last; } rels; } reltab; struct { struct ElfuScn *cqe_next; struct ElfuScn *cqe_prev; } elemChildScn; } *

variable_types:
j
unsigned int

variable_types:
i
unsigned int

variable_types: struct ElfuSym { unsigned int name; unsigned long value; unsigned int size; unsigned char bind; unsigned char type; unsigned char other; struct ElfuScn { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } shdr; char *databuf; struct ElfuScn *linkptr; struct ElfuScn *infoptr; struct ElfuScn *oldptr; struct ElfuSymtab { struct Syms { struct ElfuSym *cqh_first; struct ElfuSym *cqh_last; } syms; } symtab; struct ElfuReltab { struct Rels { struct ElfuRel { unsigned long offset; unsigned int sym; unsigned char type; int addendUsed; int addend; struct { struct ElfuRel *cqe_next; struct ElfuRel *cqe_prev; } elem; } *cqh_first; struct ElfuRel *cqh_last; } rels; } reltab; struct { struct ElfuScn *cqe_next; struct ElfuScn *cqe_prev; } elemChildScn; } *scnptr; int shndx; struct { struct ElfuSym *cqe_next; struct ElfuSym *cqe_prev; } elem; } *

variable_types:
symname
char *

variable_types:
addr
unsigned long

user_defined_types: struct Elf64_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long e_entry; unsigned long e_phoff; unsigned long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; }

user_defined_types: struct OrphanScnList { struct ElfuScn *cqh_first; struct ElfuScn *cqh_last; }

user_defined_types: struct Elf64_Phdr { unsigned int p_type; unsigned int p_flags; unsigned long p_offset; unsigned long p_vaddr; unsigned long p_paddr; unsigned long p_filesz; unsigned long p_memsz; unsigned long p_align; }

user_defined_types: struct ChildPhdrList { struct ElfuPhdr *cqh_first; struct ElfuPhdr *cqh_last; }


decompiled_code:
__int64 __fastcall elfu_mDynLookupReldynAddrByName(_DWORD *a1, const char *a2, _QWORD *a3)
{
__int64 v4; // r15
__int64 *v5; // rbx
unsigned int v6; // ebp
__int64 v7; // rsi
const char *v8; // rax
unsigned int v9; // er9
__int64 v11; // rbx

v4 = elfu_mScnForall(a1, subFindByName, ".rel.dyn", 0LL);
if ( v4 || (v4 = elfu_mScnForall(a1, subFindByName, ".rela.dyn", 0LL)) != 0 )
{
if ( !*(_QWORD *)(v4 + 72) )
_assert_fail("reldyn->linkptr", "src/libelfu/modelops/dynlookup.c", 0x83u, "elfu_mDynLookupReldynAddrByName");
v5 = *(__int64 **)(v4 + 112);
if ( v5 == (__int64 *)(v4 + 112) )
{
LABEL_10:
fprintf(stderr, "elfu_mDynLookupReldynAddrByName: Could not find or use symbol '%s' in destination ELF.\n", a2);
fwrite(" NOTE: Only R_*_COPY relocations are resolved to global variables.\n", 1uLL, 0x48uLL, stderr);
v9 = -1;
}
else
{
v6 = 0;
while ( 1 )
{
++v6;
if ( (unsigned int)(*a1 - 1) > 1 || *((_BYTE *)v5 + 12) == 5 )
{
v7 = elfu_mSymtabIndexToSym(*(_QWORD *)(v4 + 72), *((unsigned int *)v5 + 2));
if ( !v7 )
_assert_fail("sym", "src/libelfu/modelops/dynlookup.c", 0x96u, "elfu_mDynLookupReldynAddrByName");
v8 = (const char *)elfu_mSymtabSymToName(*(_QWORD *)(v4 + 72), v7);
if ( !strcmp(v8, a2) )
break;
}
v5 = (__int64 *)v5[3];
if ( v5 == (__int64 *)(v4 + 112) )
goto LABEL_10;
}
v11 = *v5;
fprintf(
stdout,
"elfu_mDynLookupReldynAddrByName: Guessing symbol '%s' is in destination memory at %x (PLT entry #%u).\n",
a2,
(unsigned int)v11,
v6);
v9 = 0;
*a3 = v11;
}
}
else
{
fwrite(
"elfu_mDynLookupReldynAddrByName: Could not find .rel.dyn section in destination ELF.\n",
1uLL,
0x55uLL,
stderr);
v9 = -1;
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1, const char *a2, _QWORD *a3)
{
__int64 v4; // r15
__int64 *v5; // rbx
unsigned int v6; // ebp
__int64 v7; // rsi
const char *v8; // rax
unsigned int v9; // er9
__int64 v11; // rbx

v4 = elfu_mScnForall(a1, subFindByName, ".rel.dyn", 0LL);
if ( v4 || (v4 = elfu_mScnForall(a1, subFindByName, ".rela.dyn", 0LL)) != 0 )
{
if ( !*(_QWORD *)(v4 + 72) )
_assert_fail("reldyn->linkptr", "src/libelfu/modelops/dynlookup.c", 0x83u, "elfu_mDynLookupReldynAddrByName");
v5 = *(__int64 **)(v4 + 112);
if ( v5 == (__int64 *)(v4 + 112) )
{
LABEL_10:
fprintf(stderr, "elfu_mDynLookupReldynAddrByName: Could not find or use symbol '%s' in destination ELF.\n", a2);
fwrite(" NOTE: Only R_*_COPY relocations are resolved to global variables.\n", 1uLL, 0x48uLL, stderr);
v9 = -1;
}
else
{
v6 = 0;
while ( 1 )
{
++v6;
if ( (unsigned int)(*a1 - 1) > 1 || *((_BYTE *)v5 + 12) == 5 )
{
v7 = elfu_mSymtabIndexToSym(*(_QWORD *)(v4 + 72), *((unsigned int *)v5 + 2));
if ( !v7 )
_assert_fail("sym", "src/libelfu/modelops/dynlookup.c", 0x96u, "elfu_mDynLookupReldynAddrByName");
v8 = (const char *)elfu_mSymtabSymToName(*(_QWORD *)(v4 + 72), v7);
if ( !strcmp(v8, a2) )
break;
}
v5 = (__int64 *)v5[3];
if ( v5 == (__int64 *)(v4 + 112) )
goto LABEL_10;
}
v11 = *v5;
fprintf(
stdout,
"elfu_mDynLookupReldynAddrByName: Guessing symbol '%s' is in destination memory at %x (PLT entry #%u).\n",
a2,
(unsigned int)v11,
v6);
v9 = 0;
*a3 = v11;
}
}
else
{
fwrite(
"elfu_mDynLookupReldynAddrByName: Could not find .rel.dyn section in destination ELF.\n",
1uLL,
0x55uLL,
stderr);
v9 = -1;
}
return v9;
}

original_code:
int elfu_mDynLookupReldynAddrByName(ElfuElf *me, char *name, GElf_Addr *result)
{
  ElfuScn *reldyn;
  ElfuRel *rel;
  GElf_Word j;
  reldyn = elfu_mScnForall(me, subFindByName, ".rel.dyn", ((void *)0));
  if (!reldyn) {
    reldyn = elfu_mScnForall(me, subFindByName, ".rela.dyn", ((void *)0));
  }
  if (!reldyn) {
    do { fprintf(stderr, "elfu_mDynLookupReldynAddrByName: Could not find .rel.dyn section in destination ELF.\n"); } while(0);
    return -1;
  }
  ((void) sizeof ((reldyn->linkptr) ? 1 : 0), __extension__ ({ if (reldyn->linkptr) ; else __assert_fail ("reldyn->linkptr", "src/libelfu/modelops/dynlookup.c", 131, __extension__ __PRETTY_FUNCTION__); }));
  j = 0;
  for ((rel) = ((&reldyn->reltab.rels)->cqh_first); (rel) != (const void *)(&reldyn->reltab.rels); (rel) = ((rel)->elem.cqe_next)) {
    ElfuSym *sym;
    char *symname;
    j++;
    if ((me->elfclass == 1 && rel->type != 5)
        || (me->elfclass == 2 && rel->type != 5)) {
      continue;
    }
    sym = elfu_mSymtabIndexToSym(reldyn->linkptr, rel->sym);
    ((void) sizeof ((sym) ? 1 : 0), __extension__ ({ if (sym) ; else __assert_fail ("sym", "src/libelfu/modelops/dynlookup.c", 150, __extension__ __PRETTY_FUNCTION__); }));
    symname = elfu_mSymtabSymToName(reldyn->linkptr, sym);
    if (!strcmp(symname, name)) {
      GElf_Addr addr = rel->offset;
      do { fprintf(stdout, "elfu_mDynLookupReldynAddrByName: Guessing symbol '%s' is in destination memory at %x (PLT entry #%u).\n", name, (unsigned)addr, j); } while(0);
      *result = addr;
      return 0;
    }
  }
  do { fprintf(stderr, "elfu_mDynLookupReldynAddrByName: Could not find or use symbol '%s' in destination ELF.\n", name); } while(0);
  do { fprintf(stderr, "      NOTE: Only R_*_COPY relocations are resolved to global variables.\n"); } while(0);
  return -1;
}

canonical_original_code:
int elfu_mDynLookupReldynAddrByName(struct ElfuElf *me, char *name, unsigned long *result)
{
  struct ElfuScn *reldyn;
  struct ElfuRel *rel;
  unsigned int j;
  reldyn = elfu_mScnForall(me, subFindByName, ".rel.dyn", ((void *)0));
  if (!reldyn) {
    reldyn = elfu_mScnForall(me, subFindByName, ".rela.dyn", ((void *)0));
  }
  if (!reldyn) {
    do { fprintf(stderr, "elfu_mDynLookupReldynAddrByName: Could not find .rel.dyn section in destination ELF.\n"); } while(0);
    return -1;
  }
  ((void) sizeof ((reldyn->linkptr) ? 1 : 0), __extension__ ({ if (reldyn->linkptr) ; else __assert_fail ("reldyn->linkptr", "src/libelfu/modelops/dynlookup.c", 131, __extension__ __PRETTY_FUNCTION__); }));
  j = 0;
  for ((rel) = ((&reldyn->reltab.rels)->cqh_first); (rel) != (void *)(&reldyn->reltab.rels); (rel) = ((rel)->elem.cqe_next)) {
    struct ElfuSym *sym;
    char *symname;
    j++;
    if ((me->elfclass == 1 && rel->type != 5)
        || (me->elfclass == 2 && rel->type != 5)) {
      continue;
    }
    sym = elfu_mSymtabIndexToSym(reldyn->linkptr, rel->sym);
    ((void) sizeof ((sym) ? 1 : 0), __extension__ ({ if (sym) ; else __assert_fail ("sym", "src/libelfu/modelops/dynlookup.c", 150, __extension__ __PRETTY_FUNCTION__); }));
    symname = elfu_mSymtabSymToName(reldyn->linkptr, sym);
    if (!strcmp(symname, name)) {
      unsigned long addr = rel->offset;
      do { fprintf(stdout, "elfu_mDynLookupReldynAddrByName: Guessing symbol '%s' is in destination memory at %x (PLT entry #%u).\n", name, (unsigned int)addr, j); } while(0);
      *result = addr;
      return 0;
    }
  }
  do { fprintf(stderr, "elfu_mDynLookupReldynAddrByName: Could not find or use symbol '%s' in destination ELF.\n", name); } while(0);
  do { fprintf(stderr, "      NOTE: Only R_*_COPY relocations are resolved to global variables.\n"); } while(0);
  return -1;
}

variable_types: struct ElfuRel { unsigned long offset; unsigned int sym; unsigned char type; int addendUsed; int addend; struct { struct ElfuRel *cqe_next; struct ElfuRel *cqe_prev; } elem; } *

user_defined_types: struct ElfuSym { unsigned int name; unsigned long value; unsigned int size; unsigned char bind; unsigned char type; unsigned char other; struct ElfuScn *scnptr; int shndx; struct { struct ElfuSym *cqe_next; struct ElfuSym *cqe_prev; } elem; }

user_defined_types: struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; }