decompiled_code:
__int64 __fastcall tiny_hash_resize(__int64 a1, unsigned int a2)
{
unsigned int v2; // ebx
int v3; // er13
_QWORD *v4; // r12
char *v5; // rax
char *v6; // r14
_QWORD *v7; // rbx
__int64 v8; // r13
__int64 result; // rax

if ( a2 > 0x40000000 )
return 0xFFFFFFFFLL;
v2 = a2;
if ( !a2 )
v2 = 64;
v3 = *(_DWORD *)(a1 + 16);
v4 = *(_QWORD **)a1;
v5 = (char *)malloc(32LL * v2);
v6 = v5;
*(_QWORD *)a1 = v5;
if ( v5 )
{
memset(v5, 0, 32LL * v2);
*(_DWORD *)(a1 + 16) = v2;
*(_QWORD *)(a1 + 8) = &v6[32 * v2];
*(_DWORD *)(a1 + 20) = 0;
*(_DWORD *)(a1 + 28) = (int)(float)((float)(int)v2 * *(float *)(a1 + 24));
if ( v3 )
{
v7 = v4;
v8 = (__int64)&v4[4 * (unsigned int)(v3 - 1) + 4];
do
{
if ( *((_BYTE *)v7 + 28) )
tiny_hash_insert(a1, *((unsigned int *)v7 + 6), *v7, v7[1]);
v7 += 4;
}
while ( v7 != (_QWORD *)v8 );
}
free(v4);
result = 0LL;
}
else
{
*(_QWORD *)a1 = v4;
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
tiny_hash_resize(TinyHash *t, uint32_t size)
{
    uint32_t old_size;
    Slot *old_slot;
    uint32_t i;
    if (size > 1073741824) {
        return -1;
    }
    if (size == 0) {
        size = 64;
    }
    old_size = t->size;
    old_slot = t->slot;
    t->slot = (Slot *) malloc(size * sizeof(Slot));
    if (t->slot == ((void *)0)) {
        t->slot = old_slot;
        return -1;
    }
    memset(t->slot, 0, size * sizeof(Slot));
    t->size = size;
    t->free_slot = t->slot + size;
    t->count = 0;
    t->max_count = size * t->max_full;
    for (i = 0; i < old_size; i++) {
        Slot *slot = old_slot + i;
        if (slot->taken) {
            tiny_hash_insert(t, slot->hash, slot->key, slot->value);
        }
    }
    free(old_slot);
    return 0;
}

canonical_original_code:
int tiny_hash_resize(struct TinyHash *t, unsigned int size)
{
    unsigned int old_size;
    struct Slot *old_slot;
    unsigned int i;
    if (size > 1073741824) {
        return -1;
    }
    if (size == 0) {
        size = 64;
    }
    old_size = t->size;
    old_slot = t->slot;
    t->slot = (struct Slot *) malloc(size * sizeof(Slot));
    if (t->slot == ((void *)0)) {
        t->slot = old_slot;
        return -1;
    }
    memset(t->slot, 0, size * sizeof(Slot));
    t->size = size;
    t->free_slot = t->slot + size;
    t->count = 0;
    t->max_count = size * t->max_full;
    for (i = 0; i < old_size; i++) {
        struct Slot *slot = old_slot + i;
        if (slot->taken) {
            tiny_hash_insert(t, slot->hash, slot->key, slot->value);
        }
    }
    free(old_slot);
    return 0;
}

variable_types:
size
unsigned int

variable_types:
i
unsigned int


repo: nodeleap/tinyhash

original_code:
static int
tiny_hash_insert(TinyHash *t, HASH h, const void *key, const void *value)
{
    Slot *m;
    if (t->count >= t->max_count) {
        if (tiny_hash_resize(t, 2 * t->size) != 0) {
            return -1;
        }
    }
    m = (&(t)->slot[(h) % (t)->size]);
    if (m->taken) {
        Slot *n = ((void *)0);
        Slot *mm = (&(t)->slot[(m->hash) % (t)->size]);
        for (n = ((void *)0); t->free_slot > t->slot;) {
            n = --t->free_slot;
            if (!n->taken) {
                break;
            }
        }
        ((void) sizeof ((n && !n->taken) ? 1 : 0), __extension__ ({ if (n && !n->taken) ; else __assert_fail ("n && !n->taken", "tinyhash.c", 178, __extension__ __PRETTY_FUNCTION__); }));
        if (mm == m) {
            n->next = m->next;
            m->next = n;
            n->taken = 1;
            m = n;
        } else {
            while (mm->next != m) {
                mm = mm->next;
            }
            mm->next = n;
            *n = *m;
            m->next = ((void *)0);
        }
    }
    else {
        m->next = ((void *)0);
        m->taken = 1;
    }
    m->key = key;
    m->value = value;
    m->hash = h;
    t->count++;
    return 0;
}

canonical_original_code:
static int tiny_hash_insert(struct TinyHash *t, unsigned int h, void *key, void *value)
{
    struct Slot *m;
    if (t->count >= t->max_count) {
        if (tiny_hash_resize(t, 2 * t->size) != 0) {
            return -1;
        }
    }
    m = (&(t)->slot[(h) % (t)->size]);
    if (m->taken) {
        struct Slot *n = ((void *)0);
        struct Slot *mm = (&(t)->slot[(m->hash) % (t)->size]);
        for (n = ((void *)0); t->free_slot > t->slot;) {
            n = --t->free_slot;
            if (!n->taken) {
                break;
            }
        }
        ((void) sizeof ((n && !n->taken) ? 1 : 0), __extension__ ({ if (n && !n->taken) ; else __assert_fail ("n && !n->taken", "tinyhash.c", 178, __extension__ __PRETTY_FUNCTION__); }));
        if (mm == m) {
            n->next = m->next;
            m->next = n;
            n->taken = 1;
            m = n;
        } else {
            while (mm->next != m) {
                mm = mm->next;
            }
            mm->next = n;
            *n = *m;
            m->next = ((void *)0);
        }
    }
    else {
        m->next = ((void *)0);
        m->taken = 1;
    }
    m->key = key;
    m->value = value;
    m->hash = h;
    t->count++;
    return 0;
}

variable_types: struct TinyHash { struct Slot { void *key; void *value; struct Slot *next; unsigned int hash; unsigned char taken; } *slot; struct Slot *free_slot; unsigned int size; unsigned int count; float max_full; unsigned int max_count; unsigned int (*hasher)(void *); int (*tester)(void *, void *); } *

variable_types:
h
unsigned int

variable_types:
key
void *

variable_types:
value
void *

return_type: int

user_defined_types: struct TinyHash { struct Slot *slot; struct Slot *free_slot; unsigned int size; unsigned int count; float max_full; unsigned int max_count; unsigned int (*hasher)(void *); int (*tester)(void *, void *); }


decompiled_code:
__int64 __fastcall tiny_hash_put(__int64 a1, __int64 a2, __int64 a3)
{
unsigned int v4; // er12
__int64 v5; // rax

v4 = (*(__int64 (__fastcall **)(__int64))(a1 + 32))(a2);
v5 = tiny_hash_locate(a1, v4, a2);
if ( !v5 )
return tiny_hash_insert(a1, v4, a2, a3);
*(_QWORD *)(v5 + 8) = a3;
return 0LL;
}

original_code:
int
tiny_hash_put(TinyHash *t, const void *key, const void *value) {
    HASH h = t->hasher(key);
    Slot *n = tiny_hash_locate(t, h, key);
    if (n) {
        n->value = value;
        return 0;
    }
    return tiny_hash_insert(t, h, key, value);
}

canonical_original_code:
int tiny_hash_put(struct TinyHash *t, void *key, void *value) {
    unsigned int h = t->hasher(key);
    struct Slot *n = tiny_hash_locate(t, h, key);
    if (n) {
        n->value = value;
        return 0;
    }
    return tiny_hash_insert(t, h, key, value);
}

variable_types: struct Slot { void *key; void *value; struct Slot *next; unsigned int hash; unsigned char taken; } *


decompiled_code:
unsigned __int64 __fastcall tiny_hash_first(__int64 a1)
{
unsigned __int64 result; // rax
unsigned __int64 v2; // rdx

result = *(_QWORD *)a1;
v2 = *(_QWORD *)a1 + 32LL * *(unsigned int *)(a1 + 16);
if ( *(_QWORD *)a1 >= v2 )
return 0LL;
while ( !*(_BYTE *)(result + 28) )
{
result += 32LL;
if ( result >= v2 )
return 0LL;
}
if ( v2 <= result )
return 0LL;
if ( !*(_BYTE *)(result + 28) )
result = 0LL;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func16(__int64 a1)
{
unsigned __int64 result; // rax
unsigned __int64 v2; // rdx

result = *(_QWORD *)a1;
v2 = *(_QWORD *)a1 + 32LL * *(unsigned int *)(a1 + 16);
if ( *(_QWORD *)a1 >= v2 )
return 0LL;
while ( !*(_BYTE *)(result + 28) )
{
result += 32LL;
if ( result >= v2 )
return 0LL;
}
if ( v2 <= result )
return 0LL;
if ( !*(_BYTE *)(result + 28) )
result = 0LL;
return result;
}

original_code:
const TinyHashIterator *tiny_hash_first(TinyHash *t) {
    Slot *slot = t->slot;
    while (slot < t->slot + t->size && !slot->taken) {
        slot++;
    }
    if (slot < t->slot + t->size && slot->taken) {
        return (const TinyHashIterator *) slot;
    }
    return ((void *)0);
}

canonical_original_code:
struct TinyHashIterator *tiny_hash_first(struct TinyHash *t) {
    struct Slot *slot = t->slot;
    while (slot < t->slot + t->size && !slot->taken) {
        slot++;
    }
    if (slot < t->slot + t->size && slot->taken) {
        return (struct TinyHashIterator *) slot;
    }
    return ((void *)0);
}

return_type: struct TinyHashIterator { void *key; void *value; } *

user_defined_types: struct Slot { void *key; void *value; struct Slot *next; unsigned int hash; unsigned char taken; }


decompiled_code:
unsigned __int64 __fastcall tiny_hash_next(__int64 a1, __int64 a2)
{
unsigned __int64 result; // rax
unsigned __int64 v3; // rdx

result = a2 + 32;
v3 = *(_QWORD *)a1 + 32LL * *(unsigned int *)(a1 + 16);
if ( a2 + 32 >= v3 )
return 0LL;
while ( !*(_BYTE *)(result + 28) )
{
result += 32LL;
if ( result >= v3 )
return 0LL;
}
if ( v3 <= result )
return 0LL;
if ( !*(_BYTE *)(result + 28) )
result = 0LL;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func17(__int64 a1, __int64 a2)
{
unsigned __int64 result; // rax
unsigned __int64 v3; // rdx

result = a2 + 32;
v3 = *(_QWORD *)a1 + 32LL * *(unsigned int *)(a1 + 16);
if ( a2 + 32 >= v3 )
return 0LL;
while ( !*(_BYTE *)(result + 28) )
{
result += 32LL;
if ( result >= v3 )
return 0LL;
}
if ( v3 <= result )
return 0LL;
if ( !*(_BYTE *)(result + 28) )
result = 0LL;
return result;
}

original_code:
const TinyHashIterator *tiny_hash_next(TinyHash *t, const TinyHashIterator *p) {
    Slot *slot = (Slot *) p;
    ++slot;
    while (slot < t->slot + t->size && !slot->taken) {
        slot++;
    }
    if (slot < t->slot + t->size && slot->taken) {
        return (const TinyHashIterator *) slot;
    }
    return ((void *)0);
}

canonical_original_code:
struct TinyHashIterator *tiny_hash_next(struct TinyHash *t, struct TinyHashIterator *p) {
    struct Slot *slot = (Slot *) p;
    ++slot;
    while (slot < t->slot + t->size && !slot->taken) {
        slot++;
    }
    if (slot < t->slot + t->size && slot->taken) {
        return (struct TinyHashIterator *) slot;
    }
    return ((void *)0);
}

variable_types:
p
struct TinyHashIterator *

user_defined_types: struct TinyHashIterator { void *key; void *value; }


original_code:
void tiny_hash_dump(TinyHash *t, char *buf, int length) {
    uint32_t i, so;
    *buf = '\0';
    for (i = 0, so = 0; i < t->size; i++) {
        Slot *slot = &t->slot[i];
        if (slot->taken) {
            so += snprintf(buf + so, length - so, "(%d,%d,%d)", i,
                    slot->hash, slot->next ? (int)(slot->next - t->slot) : -1);
        }
    }
}

canonical_original_code:
void tiny_hash_dump(struct TinyHash *t, char *buf, int length) {
    unsigned int i; unsigned int so;
    *buf = '\0';
    for (i = 0, so = 0; i < t->size; i++) {
        struct Slot *slot = &t->slot[i];
        if (slot->taken) {
            so += snprintf(buf + so, length - so, "(%d,%d,%d)", i,
                    slot->hash, slot->next ? (int)(slot->next - t->slot) : -1);
        }
    }
}

variable_types:
buf
char *

variable_types: length int

variable_types:
so
unsigned int

return_type: void


decompiled_code:
__int64 __fastcall test_hasher(char *a1)
{
char *v1; // rbx
char v2; // bp
const unsigned __int16 *v3; // rdx
const unsigned __int16 **v4; // rax
const unsigned __int16 *v5; // rsi
unsigned int i; // edx
char v7; // al

v1 = a1;
v2 = *a1;
if ( *a1 )
{
v3 = *_ctype_b_loc();
do
{
if ( (v3[v2] & 0x800) != 0 )
break;
v2 = *++v1;
}
while ( *v1 );
}
v4 = _ctype_b_loc();
v5 = *v4;
i = -1;
if ( ((*v4)[v2] & 0x800) != 0 )
{
v7 = *v1;
for ( i = 0; *v1; v7 = *++v1 )
{
if ( (v5[v7] & 0x800) == 0 )
break;
i = v7 + 10 * i - 48;
}
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char *v1; // rbx
char v2; // bp
const unsigned __int16 *v3; // rdx
const unsigned __int16 **v4; // rax
const unsigned __int16 *v5; // rsi
unsigned int i; // edx
char v7; // al

v1 = a1;
v2 = *a1;
if ( *a1 )
{
v3 = *_ctype_b_loc();
do
{
if ( (v3[v2] & 0x800) != 0 )
break;
v2 = *++v1;
}
while ( *v1 );
}
v4 = _ctype_b_loc();
v5 = *v4;
i = -1;
if ( ((*v4)[v2] & 0x800) != 0 )
{
v7 = *v1;
for ( i = 0; *v1; v7 = *++v1 )
{
if ( (v5[v7] & 0x800) == 0 )
break;
i = v7 + 10 * i - 48;
}
}
return i;
}

original_code:
static uint32_t test_hasher (const void *k) {
    const char *s = (const char *) k;
    uint32_t n;
    while (*s && !((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISdigit)) {
        s++;
    }
    if (!((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISdigit)) return (uint32_t) -1;
    for (n = 0; *s && ((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISdigit); s++) {
        n = n * 10 + (*s - '0');
    }
    return n;
}

canonical_original_code:
static unsigned int test_hasher(void *k) {
    char *s = (const char *) k;
    unsigned int n;
    while (*s && !((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISdigit)) {
        s++;
    }
    if (!((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISdigit)) return (unsigned int) -1;
    for (n = 0; *s && ((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISdigit); s++) {
        n = n * 10 + (*s - '0');
    }
    return n;
}

variable_types:
k
void *

variable_types:
s
char *

variable_types:
n
unsigned int

return_type: unsigned int


repo: noelaye/siggen

decompiled_code:
__int64 find_main()
{
const char **v0; // rdx
int v1; // er8

if ( n_procs <= 0 )
{
v1 = 0;
}
else
{
v0 = (const char **)(procs + 152LL);
v1 = 1;
while ( strcmp(*v0, "main") )
{
++v1;
v0 += 16;
if ( v1 > n_procs )
return 0;
}
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 func1()
{
const char **v0; // rdx
int v1; // er8

if ( n_procs <= 0 )
{
v1 = 0;
}
else
{
v0 = (const char **)(procs + 152LL);
v1 = 1;
while ( strcmp(*v0, "main") )
{
++v1;
v0 += 16;
if ( v1 > n_procs )
return 0;
}
}
return (unsigned int)v1;
}

original_code:
int find_main()
{
 int p;
 for (p = 1; p <= n_procs; p++){
  if (strcmp(procs[p].proc_name,"main") == 0)
   return p;
 }
 return 0;
}

variable_types: p int


decompiled_code:
__int64 current_chain()
{
return (unsigned int)max_chain;
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)max_chain;
}

original_code: int current_chain() {return max_chain;}

canonical_original_code: int current_chain() {return max_chain;}


repo: noonat/hashtable

decompiled_code:
void *__fastcall table_init(__int64 a1, int a2, void *a3, void *a4)
{
void *result; // rax

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x10Eu, "table_init");
if ( !a2 )
_assert_fail("type != H_NULL", "table.c", 0x10Fu, "table_init");
*(_QWORD *)a1 = &dummy_node;
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)(a1 + 16) = &dummy_node;
*(_DWORD *)(a1 + 24) = a2;
if ( !a3 )
a3 = &hash_code;
*(_QWORD *)(a1 + 32) = a3;
result = &hash_equal;
if ( !a4 )
a4 = &hash_equal;
*(_QWORD *)(a1 + 40) = a4;
return result;
}

canonical_decompiled_code:
void *__fastcall func8(__int64 a1, int a2, void *a3, void *a4)
{
void *result; // rax

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x10Eu, "table_init");
if ( !a2 )
_assert_fail("type != H_NULL", "table.c", 0x10Fu, "table_init");
*(_QWORD *)a1 = &dummy_node;
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)(a1 + 16) = &dummy_node;
*(_DWORD *)(a1 + 24) = a2;
if ( !a3 )
a3 = &hash_code;
*(_QWORD *)(a1 + 32) = a3;
result = &hash_equal;
if ( !a4 )
a4 = &hash_equal;
*(_QWORD *)(a1 + 40) = a4;
return result;
}

original_code:
void table_init(table_t * const table, const htype_t type, hash_func_t fn,
                hash_equal_func_t eq_fn) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 270, __extension__ __PRETTY_FUNCTION__));
  ((type != H_NULL) ? (void) (0) : __assert_fail ("type != H_NULL", "table.c", 271, __extension__ __PRETTY_FUNCTION__));
  table->nodes = ((table_node_t *)&_dummy_node);
  table->nodes_size_log = 0;
  table->inactive_node = ((table_node_t *)&_dummy_node);
  table->hash_type = type;
  table->hash_func = fn ? fn : hash_code;
  table->hash_equal_func = eq_fn ? eq_fn : hash_equal;
}

canonical_original_code:
void table_init(struct _table *table, enum _htype type, unsigned int (*fn)(enum _htype, unsigned long), int (*eq_fn)(enum _htype, unsigned long, unsigned long)) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 270, __extension__ __PRETTY_FUNCTION__));
  ((type != H_NULL) ? (void) (0) : __assert_fail ("type != H_NULL", "table.c", 271, __extension__ __PRETTY_FUNCTION__));
  table->nodes = ((struct _table_node *)&_dummy_node);
  table->nodes_size_log = 0;
  table->inactive_node = ((struct _table_node *)&_dummy_node);
  table->hash_type = type;
  table->hash_func = fn ? fn : hash_code;
  table->hash_equal_func = eq_fn ? eq_fn : hash_equal;
}

variable_types: struct _table { struct _table_node { unsigned long key; unsigned long value; struct _table_node *next; char active; } *nodes; int nodes_size_log; struct _table_node *inactive_node; enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 } hash_type; unsigned int (*hash_func)(enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }, unsigned long); int (*hash_equal_func)(enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }, unsigned long, unsigned long); enum _table_error { TABLE_ERROR_NONE=0, TABLE_ERROR_MEMORY=1, TABLE_ERROR_OVERFLOW=2, MAX_TABLE_ERROR=3 } error; } *

variable_types: unsigned int (*)(enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }, unsigned long)

variable_types: int (*)(enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }, unsigned long, unsigned long)

user_defined_types: struct _table { struct _table_node *nodes; int nodes_size_log; struct _table_node *inactive_node; enum _htype hash_type; unsigned int (*hash_func)(enum _htype, unsigned long); int (*hash_equal_func)(enum _htype, unsigned long, unsigned long); enum _table_error error; }


decompiled_code:
void __fastcall table_destroy(void **a1)
{
if ( a1 )
{
table_nodes_free(*a1);
*a1 = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func9(void **a1)
{
if ( a1 )
{
func6(*a1);
*a1 = 0LL;
}
}

original_code:
void table_destroy(table_t * const table) {
  if (table != ((void *)0)) {
    _table_nodes_free(table->nodes);
    table->nodes = ((void *)0);
  }
}

canonical_original_code:
void table_destroy(struct _table *table) {
  if (table != ((void *)0)) {
    _table_nodes_free(table->nodes);
    table->nodes = ((void *)0);
  }
}

user_defined_types: enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }

user_defined_types: enum _table_error { TABLE_ERROR_NONE=0, TABLE_ERROR_MEMORY=1, TABLE_ERROR_OVERFLOW=2, MAX_TABLE_ERROR=3 }

user_defined_types: struct _table_node { unsigned long key; unsigned long value; struct _table_node *next; char active; }


decompiled_code:
__int64 __fastcall table_get(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x120u, "table_get");
v2 = table_node_find_key(a1, a2, 0LL);
v3 = 0LL;
if ( v2 )
v3 = v2[1];
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x120u, "table_get");
v2 = table_node_find_key(a1, a2, 0LL);
v3 = 0LL;
if ( v2 )
v3 = v2[1];
return v3;
}

original_code:
hvalue_t table_get(const table_t * const table, const hvalue_t key) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 288, __extension__ __PRETTY_FUNCTION__));
  const table_node_t * const node = _table_node_find_key(table, key, ((void *)0));
  return node != ((void *)0) ? node->value : 0;
}

canonical_original_code:
unsigned long table_get(struct _table *table, unsigned long key) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 288, __extension__ __PRETTY_FUNCTION__));
  struct _table_node *node = _table_node_find_key(table, key, ((void *)0));
  return node != ((void *)0) ? node->value : 0;
}

variable_types:
key
unsigned long

variable_types: struct _table_node { unsigned long key; unsigned long value; struct _table_node *next; char active; } *

return_type: unsigned long


decompiled_code:
_BOOL8 __fastcall table_contains(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_BOOL4 v3; // edx

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x134u, "table_contains");
v2 = table_node_find_key(a1, a2, 0LL);
v3 = 0;
if ( v2 )
v3 = *((_BYTE *)v2 + 24) != 0;
return v3;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_BOOL4 v3; // edx

if ( !a1 )
_assert_fail("table != NULL", "table.c", 0x134u, "table_contains");
v2 = table_node_find_key(a1, a2, 0LL);
v3 = 0;
if ( v2 )
v3 = *((_BYTE *)v2 + 24) != 0;
return v3;
}

original_code:
int32_t table_contains(const table_t * const table, const hvalue_t key) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 308, __extension__ __PRETTY_FUNCTION__));
  const table_node_t * const node = _table_node_find_key(table, key, ((void *)0));
  return node != ((void *)0) && node->active;
}

canonical_original_code:
int table_contains(struct _table *table, unsigned long key) {
  ((table != ((void *)0)) ? (void) (0) : __assert_fail ("table != NULL", "table.c", 308, __extension__ __PRETTY_FUNCTION__));
  struct _table_node *node = _table_node_find_key(table, key, ((void *)0));
  return node != ((void *)0) && node->active;
}


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

result = "out of memory";
if ( a1 != 1 )
{
result = "table size too large";
if ( a1 != 2 )
{
result = "unknown error";
if ( !a1 )
result = "none";
}
}
return result;
}

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

result = "out of memory";
if ( a1 != 1 )
{
result = "table size too large";
if ( a1 != 2 )
{
result = "unknown error";
if ( !a1 )
result = "none";
}
}
return result;
}

original_code:
const char *table_error_string(const table_error_t error) {
  switch (error) {
    case TABLE_ERROR_NONE:
      return "none";
    case TABLE_ERROR_MEMORY:
      return "out of memory";
    case TABLE_ERROR_OVERFLOW:
      return "table size too large";
    default:
      return "unknown error";
  }
}

canonical_original_code:
char *table_error_string(enum _table_error error) {
  switch (error) {
    case TABLE_ERROR_NONE:
      return "none";
    case TABLE_ERROR_MEMORY:
      return "out of memory";
    case TABLE_ERROR_OVERFLOW:
      return "table size too large";
    default:
      return "unknown error";
  }
}

variable_types: enum _table_error { TABLE_ERROR_NONE=0, TABLE_ERROR_MEMORY=1, TABLE_ERROR_OVERFLOW=2, MAX_TABLE_ERROR=3 }

return_type: char *


decompiled_code:
__int64 __fastcall hash_code(__int64 a1, char *a2)
{
__int64 result; // rax

if ( (_DWORD)a1 == 9 )
return hash_func_string(9LL, a2);
if ( (unsigned int)a1 > 9 )
{
result = (unsigned int)a2;
if ( (_DWORD)a1 != 10 )
return (unsigned int)a2;
}
else
{
if ( (((_DWORD)a1 - 4) & 0xFFFFFFFB) != 0 )
return (unsigned int)a2;
result = hash_func_int64(a1, (__int64)a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, char *a2)
{
__int64 result; // rax

if ( (_DWORD)a1 == 9 )
return func1(9LL, a2);
if ( (unsigned int)a1 > 9 )
{
result = (unsigned int)a2;
if ( (_DWORD)a1 != 10 )
return (unsigned int)a2;
}
else
{
if ( (((_DWORD)a1 - 4) & 0xFFFFFFFB) != 0 )
return (unsigned int)a2;
result = func0(a1, (__int64)a2);
}
return result;
}

original_code:
hcode_t hash_code(const htype_t type, const hvalue_t value) {
  switch (type) {
    case H_INT64:
    case H_UINT64:
      return hash_func_int64(type, value);
    case H_STRING:
      return hash_func_string(type, value);
    case H_VOID:
      return (hcode_t)(size_t)value;
    default:
      return (hcode_t)value;
  }
}

canonical_original_code:
unsigned int hash_code(enum _htype type, unsigned long value) {
  switch (type) {
    case H_INT64:
    case H_UINT64:
      return hash_func_int64(type, value);
    case H_STRING:
      return hash_func_string(type, value);
    case H_VOID:
      return (unsigned int)(unsigned long)value;
    default:
      return (unsigned int)value;
  }
}

variable_types: enum _htype { H_NULL=0, H_INT8=1, H_INT16=2, H_INT32=3, H_INT64=4, H_UINT8=5, H_UINT16=6, H_UINT32=7, H_UINT64=8, H_STRING=9, H_VOID=10 }

variable_types:
value
unsigned long


decompiled_code:
_BOOL8 __fastcall hash_equal(int a1, const char *a2, const char *a3)
{
_BOOL8 result; // rax

result = a2 == a3;
if ( a1 == 9 )
{
if ( a3 != 0LL && a2 != 0LL || (result = 0LL, a2 == a3) )
result = strcmp(a2, a3) == 0;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(int a1, const char *a2, const char *a3)
{
_BOOL8 result; // rax

result = a2 == a3;
if ( a1 == 9 )
{
if ( a3 != 0LL && a2 != 0LL || (result = 0LL, a2 == a3) )
result = strcmp(a2, a3) == 0;
}
return result;
}

original_code:
int32_t hash_equal(const htype_t type, const hvalue_t a, const hvalue_t b) {
  switch (type) {
    case H_STRING: {
      const char *sa = (const char *)(size_t)a;
      const char *sb = (const char *)(size_t)b;
      if ((sa == ((void *)0) || sb == ((void *)0)) && sa != sb) {
        return 0;
      } else{
        return strcmp(sa, sb) == 0;
      }
    }
    default:
      return a == b;
  }
}

canonical_original_code:
int hash_equal(enum _htype type, unsigned long a, unsigned long b) {
  switch (type) {
    case H_STRING: {
      char *sa = (const char *)(size_t)a;
      char *sb = (const char *)(size_t)b;
      if ((sa == ((void *)0) || sb == ((void *)0)) && sa != sb) {
        return 0;
      } else{
        return strcmp(sa, sb) == 0;
      }
    }
    default:
      return a == b;
  }
}

variable_types:
a
unsigned long

variable_types:
b
unsigned long

variable_types:
sa
char *

variable_types:
sb
char *


repo: noqsi/NIPP

decompiled_code:
char __fastcall nipp_check_message(_BYTE *a1)
{
__int64 v1; // rdx
char result; // al

v1 = (__int64)&a1[((unsigned __int8)a1[5] | ((unsigned __int8)a1[4] << 8)) + 7];
result = -1;
do
result ^= *a1++;
while ( a1 != (_BYTE *)v1 );
return result;
}

canonical_decompiled_code:
char __fastcall func0(_BYTE *a1)
{
__int64 v1; // rdx
char result; // al

v1 = (__int64)&a1[((unsigned __int8)a1[5] | ((unsigned __int8)a1[4] << 8)) + 7];
result = -1;
do
result ^= *a1++;
while ( a1 != (_BYTE *)v1 );
return result;
}

original_code:
uint8_t nipp_check_message( nipp_message_t *m )
{
 unsigned len = ((unsigned)(((*m)[4]<<8)|(*m)[5])+1) + 6;
 unsigned i;
 uint8_t parity = 0xff;
 for( i = 0; i < len; i += 1 )
  parity ^= (*m)[i];
 return parity;
}

canonical_original_code:
unsigned char nipp_check_message(unsigned char (*m)[-1])
{
 unsigned int len = ((unsigned)(((*m)[4]<<8)|(*m)[5])+1) + 6;
 unsigned int i;
 unsigned char parity = 0xff;
 for( i = 0; i < len; i += 1 )
  parity ^= (*m)[i];
 return parity;
}

variable_types:
m
unsigned char (*)[-1]

variable_types:
len
unsigned int

variable_types:
i
unsigned int

variable_types:
parity
unsigned char

return_type: unsigned char


decompiled_code:
char __fastcall nipp_add_checksum(_BYTE *a1)
{
char result; // al

a1[7] = 0;
result = nipp_check_message(a1);
a1[7] = result;
return result;
}

canonical_decompiled_code:
char __fastcall func1(_BYTE *a1)
{
char result; // al

a1[7] = 0;
result = func0(a1);
a1[7] = result;
return result;
}

original_code:
void
nipp_add_checksum( nipp_message_t *m)
{
 (*m)[7] = 0;
 (*m)[7] = nipp_check_message( m );
}

canonical_original_code:
void nipp_add_checksum(unsigned char (*m)[-1])
{
 (*m)[7] = 0;
 (*m)[7] = nipp_check_message( m );
}


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

if ( a2 > 0x3FF )
{
nipp_errno = 2;
result = 0xFFFFFFFFLL;
}
else
{
*(_BYTE *)(a1 + 4) = (unsigned __int16)(a2 - 1) >> 8;
*(_BYTE *)(a1 + 5) = a2 - 1;
result = 0LL;
}
return result;
}

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

if ( a2 > 0x3FF )
{
nipp_errno = 2;
result = 0xFFFFFFFFLL;
}
else
{
*(_BYTE *)(a1 + 4) = (unsigned __int16)(a2 - 1) >> 8;
*(_BYTE *)(a1 + 5) = a2 - 1;
result = 0LL;
}
return result;
}

original_code:
int nipp_truncate( nipp_message_t *m, unsigned length )
{
 if( length >= 1024 ) {
  nipp_errno = 2;
  return -1;
 }
 length -= 1;
 (*m)[4] = length>>8;
 (*m)[5] = length;
 return 0;
}

canonical_original_code:
int nipp_truncate(unsigned char (*m)[-1], unsigned int length)
{
 if( length >= 1024 ) {
  nipp_errno = 2;
  return -1;
 }
 length -= 1;
 (*m)[4] = length>>8;
 (*m)[5] = length;
 return 0;
}

variable_types:
length
unsigned int


decompiled_code:
__int64 __fastcall nipp_send(__int64 a1)
{
int v1; // ebx
__int64 result; // rax

v1 = *(unsigned __int8 *)(a1 + 5) | (*(unsigned __int8 *)(a1 + 4) << 8);
if ( (unsigned int)(v1 + 1) > 0x400 )
{
nipp_errno = 2;
nipp_abort_tx();
result = 0xFFFFFFFFLL;
}
else if ( (int)nipp_send_sync() < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
result = nipp_send_buffer(a1, (unsigned int)(v1 + 7));
}
return result;
}

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

v1 = *(unsigned __int8 *)(a1 + 5) | (*(unsigned __int8 *)(a1 + 4) << 8);
if ( (unsigned int)(v1 + 1) > 0x400 )
{
nipp_errno = 2;
nipp_abort_tx();
result = 0xFFFFFFFFLL;
}
else if ( (int)nipp_send_sync() < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
result = nipp_send_buffer(a1, (unsigned int)(v1 + 7));
}
return result;
}

original_code:
int nipp_send( nipp_message_t *m )
{
 unsigned length = ((unsigned)(((*m)[4]<<8)|(*m)[5])+1);
 if( length > 1024 ) {
  nipp_errno = 2;
  nipp_abort_tx( m );
  return -1;
 }
 if( nipp_send_sync() < 0 ) return -1;
 return nipp_send_buffer( m, length + 6);
}

canonical_original_code:
int nipp_send(unsigned char (*m)[-1])
{
 unsigned int length = ((unsigned)(((*m)[4]<<8)|(*m)[5])+1);
 if( length > 1024 ) {
  nipp_errno = 2;
  nipp_abort_tx( m );
  return -1;
 }
 if( nipp_send_sync() < 0 ) return -1;
 return nipp_send_buffer( m, length + 6);
}


decompiled_code:
void *__fastcall nipp_get_message(int a1)
{
unsigned int v1; // eax
int v2; // eax
int v3; // ebx
unsigned int v4; // ebx
unsigned int v5; // eax
int v6; // eax
void *result; // rax
int v8[3]; // [rsp+Ch] [rbp-Ch] BYREF

v8[0] = a1;
if ( !found_sync_2 )
{
if ( (int)nipp_find_sync(v8) < 0 )
return 0LL;
found_sync_2 = 1;
}
v1 = bytes_1;
if ( (unsigned int)bytes_1 > 5 )
{
LABEL_5:
v3 = (unsigned __int8)byte_425 | ((unsigned __int8)byte_424 << 8);
if ( (unsigned int)(v3 + 1) > 0x400 )
{
nipp_errno = 2;
nipp_abort_rx();
bytes_1 = 0;
result = 0LL;
}
else
{
v4 = v3 + 7;
v5 = bytes_1;
if ( v4 <= bytes_1 )
{
LABEL_9:
found_sync_2 = 0;
bytes_1 = 0;
result = &b_0;
}
else
{
while ( 1 )
{
v6 = nipp_get_bytes((char *)&b_0 + v5, v4 - v5, v8);
if ( !v6 )
break;
v5 = bytes_1 + v6;
bytes_1 = v5;
if ( v5 >= v4 )
goto LABEL_9;
}
result = 0LL;
}
}
}
else
{
while ( 1 )
{
v2 = nipp_get_bytes((char *)&b_0 + v1, 6 - v1, v8);
if ( !v2 )
break;
v1 = bytes_1 + v2;
bytes_1 = v1;
if ( v1 > 5 )
goto LABEL_5;
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func4(int a1)
{
unsigned int v1; // eax
int v2; // eax
int v3; // ebx
unsigned int v4; // ebx
unsigned int v5; // eax
int v6; // eax
void *result; // rax
int v8[3]; // [rsp+Ch] [rbp-Ch] BYREF

v8[0] = a1;
if ( !found_sync_2 )
{
if ( (int)nipp_find_sync(v8) < 0 )
return 0LL;
found_sync_2 = 1;
}
v1 = bytes_1;
if ( (unsigned int)bytes_1 > 5 )
{
LABEL_5:
v3 = (unsigned __int8)byte_425 | ((unsigned __int8)byte_424 << 8);
if ( (unsigned int)(v3 + 1) > 0x400 )
{
nipp_errno = 2;
nipp_abort_rx();
bytes_1 = 0;
result = 0LL;
}
else
{
v4 = v3 + 7;
v5 = bytes_1;
if ( v4 <= bytes_1 )
{
LABEL_9:
found_sync_2 = 0;
bytes_1 = 0;
result = &b_0;
}
else
{
while ( 1 )
{
v6 = nipp_get_bytes((char *)&b_0 + v5, v4 - v5, v8);
if ( !v6 )
break;
v5 = bytes_1 + v6;
bytes_1 = v5;
if ( v5 >= v4 )
goto LABEL_9;
}
result = 0LL;
}
}
}
else
{
while ( 1 )
{
v2 = nipp_get_bytes((char *)&b_0 + v1, 6 - v1, v8);
if ( !v2 )
break;
v1 = bytes_1 + v2;
bytes_1 = v1;
if ( v1 > 5 )
goto LABEL_5;
}
result = 0LL;
}
return result;
}

original_code:
nipp_message_t *nipp_get_message( unsigned timeout )
{
 static nipp_message_t b;
 static unsigned bytes = 0;
 static int found_sync = 0;
 unsigned c, t;
 int n;
 int hdrlen;
 if( !found_sync ) {
  n = nipp_find_sync( &timeout );
  if( n < 0 ) return 0;
  found_sync = 1;
 }
 hdrlen = 6;
 while( bytes < hdrlen )
 {
  c = nipp_get_bytes( b + bytes, hdrlen - bytes,
       &timeout );
  if( !c ) return 0;
  bytes += c;
 }
 t = ((unsigned)(((*&b)[4]<<8)|(*&b)[5])+1);
 if( t > 1024 ) {
  nipp_errno = 2;
  nipp_abort_rx();
  bytes = 0;
  return 0;
 }
 t += 6;
 while( bytes < t ) {
  c = nipp_get_bytes( b + bytes, t - bytes, &timeout );
  if( !c ) return 0;
  bytes += c;
 }
 bytes = found_sync = 0;
 return &b;
}

canonical_original_code:
unsigned char (*nipp_get_message(unsigned int timeout))[-1]
{
 static unsigned char b[-1];
 static unsigned int bytes = 0;
 static int found_sync = 0;
 unsigned int c; unsigned int t;
 int n;
 int hdrlen;
 if( !found_sync ) {
  n = nipp_find_sync( &timeout );
  if( n < 0 ) return 0;
  found_sync = 1;
 }
 hdrlen = 6;
 while( bytes < hdrlen )
 {
  c = nipp_get_bytes( b + bytes, hdrlen - bytes,
       &timeout );
  if( !c ) return 0;
  bytes += c;
 }
 t = ((unsigned int)(((*&b)[4]<<8)|(*&b)[5])+1);
 if( t > 1024 ) {
  nipp_errno = 2;
  nipp_abort_rx();
  bytes = 0;
  return 0;
 }
 t += 6;
 while( bytes < t ) {
  c = nipp_get_bytes( b + bytes, t - bytes, &timeout );
  if( !c ) return 0;
  bytes += c;
 }
 bytes = found_sync = 0;
 return &b;
}

variable_types:
timeout
unsigned int

variable_types:
b
unsigned char [-1]

variable_types:
bytes
unsigned int

variable_types:
c
unsigned int

variable_types:
t
unsigned int

variable_types: n int

variable_types: hdrlen int

return_type: unsigned char (*)[-1]


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

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

original_code:
int nipp_default_handler( nipp_message_t *msg )
{
 return 0;
}

canonical_original_code:
int nipp_default_handler(unsigned char (*msg)[-1])
{
 return 0;
}

variable_types:
msg
unsigned char (*)[-1]


original_code:
unsigned int
nipp_unpack(uint8_t *m, int offset, int lenval)
{
 int out=0;
 int outlen = 0;
 int byte, toget, offbit, offbyte;
 while (outlen < lenval)
 {
        offbit = (offset + outlen) % 8;
        offbyte = (offset + outlen) / 8;
  byte = m[offbyte];
  if (lenval + offbit - outlen < 8)
   toget = lenval - outlen;
  else
   toget = 8 - offbit;
  offbit = 8 - (offbit + toget);
  out = (out << toget) | ((byte >> offbit) & ((1<<toget)-1));
  outlen += toget;
 }
 return out;
}

canonical_original_code:
unsigned int nipp_unpack(unsigned char *m, int offset, int lenval)
{
 int out = 0;
 int outlen = 0;
 int byte; int toget; int offbit; int offbyte;
 while (outlen < lenval)
 {
        offbit = (offset + outlen) % 8;
        offbyte = (offset + outlen) / 8;
  byte = m[offbyte];
  if (lenval + offbit - outlen < 8)
   toget = lenval - outlen;
  else
   toget = 8 - offbit;
  offbit = 8 - (offbit + toget);
  out = (out << toget) | ((byte >> offbit) & ((1<<toget)-1));
  outlen += toget;
 }
 return out;
}

variable_types:
m
unsigned char *

variable_types: offset int

variable_types: lenval int

variable_types: out int

variable_types: outlen int

variable_types: byte int

variable_types: toget int

variable_types: offbit int

variable_types: offbyte int


original_code:
void
nipp_pack(uint8_t *m, int offset, int lenval, unsigned int val)
{
 int endoffset, mask, toset, val2set;
 int offbit, offbyte;
    endoffset = offset + lenval;
    while (offset < endoffset)
 {
        offbit = offset % 8;
        offbyte = offset / 8;
        mask=0;
        toset = 8-offbit;
        if (offset + toset > endoffset)
            toset = endoffset - offset;
        val2set = val >> (endoffset - offset - toset);
        offbit = 8 - (offbit + toset);
        if (offbit > 0)
            mask = (1<<offbit) - 1;
        if (toset + offbit != 8)
            mask = mask | (~((1<< (toset + offbit)) - 1));
        m[offbyte] = (m[offbyte] & mask) | ((val2set << offbit) & (~mask & 0xFF));
        offset = offset + toset;
 }
}

canonical_original_code:
void nipp_pack(unsigned char *m, int offset, int lenval, unsigned int val)
{
 int endoffset; int mask; int toset; int val2set;
 int offbit; int offbyte;
    endoffset = offset + lenval;
    while (offset < endoffset)
 {
        offbit = offset % 8;
        offbyte = offset / 8;
        mask=0;
        toset = 8-offbit;
        if (offset + toset > endoffset)
            toset = endoffset - offset;
        val2set = val >> (endoffset - offset - toset);
        offbit = 8 - (offbit + toset);
        if (offbit > 0)
            mask = (1<<offbit) - 1;
        if (toset + offbit != 8)
            mask = mask | (~((1<< (toset + offbit)) - 1));
        m[offbyte] = (m[offbyte] & mask) | ((val2set << offbit) & (~mask & 0xFF));
        offset = offset + toset;
 }
}

variable_types:
val
unsigned int

variable_types: endoffset int

variable_types: mask int

variable_types: toset int


repo: norayr/oric-tools

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

result = (unsigned int)plotter;
if ( (int)plotter <= 479 )
{
result = (unsigned int)(plotter + 1);
LODWORD(plotter) = plotter + 1;
}
return result;
}

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

result = (unsigned int)plotter;
if ( (int)plotter <= 479 )
{
result = (unsigned int)(plotter + 1);
LODWORD(plotter) = plotter + 1;
}
return result;
}

original_code:
void inc_x()
{
 if (plotter.pos.x < 480)
  plotter.pos.x++;
}


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

result = (unsigned int)plotter;
if ( (int)plotter > 0 )
{
result = (unsigned int)(plotter - 1);
LODWORD(plotter) = plotter - 1;
}
return result;
}

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

result = (unsigned int)plotter;
if ( (int)plotter > 0 )
{
result = (unsigned int)(plotter - 1);
LODWORD(plotter) = plotter - 1;
}
return result;
}

original_code:
void dec_x()
{
 if (plotter.pos.x > 0)
  plotter.pos.x--;
}


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

result = roll_sheet((unsigned int)(HIDWORD(plotter) + 1));
if ( (_DWORD)result )
++HIDWORD(plotter);
return result;
}

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

result = roll_sheet((unsigned int)(HIDWORD(plotter) + 1));
if ( (_DWORD)result )
++HIDWORD(plotter);
return result;
}

original_code:
void inc_y()
{
 if (roll_sheet(plotter.pos.y+1))
  plotter.pos.y++;
}


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

result = roll_sheet((unsigned int)(HIDWORD(plotter) - 1));
if ( (_DWORD)result )
--HIDWORD(plotter);
return result;
}

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

result = roll_sheet((unsigned int)(HIDWORD(plotter) - 1));
if ( (_DWORD)result )
--HIDWORD(plotter);
return result;
}

original_code:
void dec_y()
{
 if (roll_sheet(plotter.pos.y-1))
  plotter.pos.y--;
}


decompiled_code:
void __fastcall change_color(unsigned int a1)
{
if ( a1 <= 3 )
dword_64C = a1;
}

canonical_decompiled_code:
void __fastcall func4(unsigned int a1)
{
if ( a1 <= 3 )
dword_64C = a1;
}

original_code:
void change_color(int pen)
{
 if (pen>=0 && pen<=3)
  plotter.pen=pen;
}

variable_types: pen int


decompiled_code:
void __fastcall set_dir(unsigned int a1)
{
if ( a1 <= 3 )
dword_650 = a1;
}

canonical_decompiled_code:
void __fastcall func5(unsigned int a1)
{
if ( a1 <= 3 )
dword_650 = a1;
}

original_code:
void set_dir(int dir)
{
 if (dir>=0 && dir<=3)
  plotter.direction=dir;
}

variable_types: dir int


home

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

result = qword_65C;
plotter = qword_65C;
return result;
}

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

result = qword_65C;
plotter = qword_65C;
return result;
}

original_code: void home() { plotter.pos=plotter.origin; }


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

result = plotter;
qword_65C = plotter;
return result;
}

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

result = plotter;
qword_65C = plotter;
return result;
}

original_code: void set_origin() { plotter.origin=plotter.pos; }


decompiled_code:
__int64 set_graphic_mode()
{
dword_658 = 1;
return set_origin();
}

canonical_decompiled_code:
__int64 func8()
{
dword_658 = 1;
return func7();
}

original_code: void set_graphic_mode() { plotter.graphics=true; set_origin(); }


decompiled_code:
void set_text_mode()
{
dword_658 = 0;
}

canonical_decompiled_code:
void func9()
{
dword_658 = 0;
}

original_code: void set_text_mode() { plotter.graphics=false; }


decompiled_code:
void __fastcall set_line_type(unsigned int a1)
{
if ( a1 <= 0xF )
dword_664 = a1;
}

canonical_decompiled_code:
void __fastcall func10(unsigned int a1)
{
if ( a1 <= 0xF )
dword_664 = a1;
}

original_code:
void set_line_type(int line_type)
{
 if (line_type>=0 && line_type<=15)
  plotter.line_type=line_type;
}


decompiled_code:
void __fastcall set_char_size(unsigned int a1)
{
if ( a1 <= 0x3F )
dword_654 = a1;
}

canonical_decompiled_code:
void __fastcall func11(unsigned int a1)
{
if ( a1 <= 0x3F )
dword_654 = a1;
}

original_code:
void set_char_size(int char_size)
{
 if (char_size>=0 && char_size<=63)
  plotter.char_size=char_size;
}


plot

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

result = roll_sheet(a1 >> 32);
if ( (_DWORD)result )
result = mark_point((unsigned int)a1, (unsigned int)(a2 + 1));
return result;
}

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

result = roll_sheet(a1 >> 32);
if ( (_DWORD)result )
result = mark_point((unsigned int)a1, (unsigned int)(a2 + 1));
return result;
}

original_code:
void plot(position pos, int colour)
{
 if (roll_sheet(pos.y))
  mark_point(pos.x,colour+1);
}

canonical_original_code:
void plot(struct position pos, int colour)
{
 if (roll_sheet(pos.y))
  mark_point(pos.x,colour+1);
}

variable_types:
pos
struct position { int x; }

variable_types: colour int


decompiled_code:
__int64 pen_down()
{
dword_648 = 1;
return plot(plotter, dword_64C);
}

canonical_decompiled_code:
__int64 func13()
{
dword_648 = 1;
return func12(plotter, dword_64C);
}

original_code:
void pen_down()
{
 plotter.pen_down=true;
 plot(plotter.pos,plotter.pen);
}


decompiled_code:
void pen_down_no_mark()
{
dword_648 = 1;
}

canonical_decompiled_code:
void func14()
{
dword_648 = 1;
}

original_code:
void pen_down_no_mark()
{
 plotter.pen_down=true;
}


decompiled_code:
void pen_up()
{
dword_648 = 0;
}

canonical_decompiled_code:
void func15()
{
dword_648 = 0;
}

original_code: void pen_up() { plotter.pen_down=false; }


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

result = (unsigned int)((dword_64C + 1) % 4);
dword_64C = (dword_64C + 1) % 4;
return result;
}

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

result = (unsigned int)((dword_64C + 1) % 4);
dword_64C = (dword_64C + 1) % 4;
return result;
}

original_code: void rotate_color() { plotter.pen=(plotter.pen+1)%4; }


decompiled_code:
__int64 get_dir()
{
return (unsigned int)dword_650;
}

canonical_decompiled_code:
__int64 func17()
{
return (unsigned int)dword_650;
}

original_code: int get_dir() { return plotter.direction; }


decompiled_code:
__int64 get_char_size()
{
return (unsigned int)dword_654;
}

canonical_decompiled_code:
__int64 func18()
{
return (unsigned int)dword_654;
}

original_code: int get_char_size() { return plotter.char_size; }


decompiled_code:
__int64 get_pen_pos()
{
return plotter;
}

canonical_decompiled_code:
__int64 func19()
{
return plotter;
}

canonical_original_code: struct position get_pen_pos() { return plotter.pos; }

return_type: struct position { int x; }


decompiled_code:
__int64 get_origin()
{
return qword_65C;
}

canonical_decompiled_code:
__int64 func20()
{
return qword_65C;
}

canonical_original_code: struct position get_origin() { return plotter.origin; }


decompiled_code:
void __fastcall set_pen_pos(__int64 a1)
{
plotter = a1;
}

canonical_decompiled_code:
void __fastcall func21(__int64 a1)
{
plotter = a1;
}

original_code: void set_pen_pos(position newpos) { plotter.pos=newpos; }

canonical_original_code: void set_pen_pos(struct position newpos) { plotter.pos=newpos; }

variable_types:
newpos
struct position { int x; }


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

switch ( a1 )
{
case 0:
goto LABEL_3;
case 1:
dec_y();
LABEL_3:
if ( dword_648 )
plot(plotter, dword_64C);
return inc_x();
case 2:
result = dec_y();
if ( dword_648 )
return plot(plotter, dword_64C);
return result;
case 3:
dec_y();
goto LABEL_10;
case 4:
LABEL_10:
result = dec_x();
if ( dword_648 )
return plot(plotter, dword_64C);
return result;
case 5:
dec_x();
goto LABEL_13;
case 6:
LABEL_13:
if ( dword_648 )
plot(plotter, dword_64C);
result = inc_y();
break;
case 7:
if ( dword_648 )
plot(plotter, dword_64C);
inc_y();
result = inc_x();
break;
default:
return result;
}
return result;
}

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

switch ( a1 )
{
case 0:
goto LABEL_3;
case 1:
func3();
LABEL_3:
if ( dword_648 )
func12(plotter, dword_64C);
return func0();
case 2:
result = func3();
if ( dword_648 )
return func12(plotter, dword_64C);
return result;
case 3:
func3();
goto LABEL_10;
case 4:
LABEL_10:
result = func1();
if ( dword_648 )
return func12(plotter, dword_64C);
return result;
case 5:
func1();
goto LABEL_13;
case 6:
LABEL_13:
if ( dword_648 )
func12(plotter, dword_64C);
result = func2();
break;
case 7:
if ( dword_648 )
func12(plotter, dword_64C);
func2();
result = func0();
break;
default:
return result;
}
return result;
}

original_code:
void pen_step(int absolute_dir)
{
 switch (absolute_dir) {
 case 1:
  dec_y();
 case 0:
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
  inc_x();
  break;
 case 2:
  dec_y();
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
  break;
 case 3:
  dec_y();
 case 4:
  dec_x();
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
  break;
 case 5:
  dec_x();
 case 6:
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
  inc_y();
  break;
 case 7:
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
  inc_y();
  inc_x();
  break;
 }
}


decompiled_code:
__int64 __fastcall move_pen(int a1, int a2)
{
int v2; // eax
char v3; // bp
__int64 result; // rax
unsigned int v5; // ebp
int i; // ebx

v2 = get_dir();
v3 = a1 + 2 * (4 - v2);
result = (unsigned int)((a1 + 2 * (4 - v2)) >> 31) >> 29;
v5 = (((_BYTE)result + v3) & 7) - result;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
result = v5;
switch ( v5 )
{
case 0u:
goto LABEL_4;
case 1u:
dec_y();
LABEL_4:
result = inc_x();
break;
case 2u:
result = dec_y();
break;
case 3u:
dec_y();
goto LABEL_11;
case 4u:
LABEL_11:
result = dec_x();
break;
case 5u:
dec_x();
goto LABEL_13;
case 6u:
LABEL_13:
result = inc_y();
break;
case 7u:
inc_y();
result = inc_x();
break;
default:
break;
}
if ( *((_DWORD *)&plotter + 2) )
result = plot(plotter, *((_DWORD *)&plotter + 3));
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func24(int a1, int a2)
{
int v2; // eax
char v3; // bp
__int64 result; // rax
unsigned int v5; // ebp
int i; // ebx

v2 = func17();
v3 = a1 + 2 * (4 - v2);
result = (unsigned int)((a1 + 2 * (4 - v2)) >> 31) >> 29;
v5 = (((_BYTE)result + v3) & 7) - result;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
result = v5;
switch ( v5 )
{
case 0u:
goto LABEL_4;
case 1u:
func3();
LABEL_4:
result = func0();
break;
case 2u:
result = func3();
break;
case 3u:
func3();
goto LABEL_11;
case 4u:
LABEL_11:
result = func1();
break;
case 5u:
func1();
goto LABEL_13;
case 6u:
LABEL_13:
result = func2();
break;
case 7u:
func2();
result = func0();
break;
default:
break;
}
if ( *((_DWORD *)&plotter + 2) )
result = func12(plotter, *((_DWORD *)&plotter + 3));
}
}
return result;
}

original_code:
void move_pen(int relative_dir,int length)
{
 int i;
 int absolute_dir = (8-2*get_dir()+relative_dir)%8;
 for (i=0;i<length;i++) {
  switch (absolute_dir) {
  case 1: dec_y();
  case 0: inc_x(); break;
  case 2: dec_y(); break;
  case 3:dec_y();
  case 4: dec_x(); break;
  case 5:dec_x();
  case 6: inc_y(); break;
  case 7:inc_y(); inc_x(); break;
  }
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
 }
}

canonical_original_code:
void move_pen(int relative_dir, int length)
{
 int i;
 int absolute_dir = (8-2*get_dir()+relative_dir)%8;
 for (i=0;i<length;i++) {
  switch (absolute_dir) {
  case 1: dec_y();
  case 0: inc_x(); break;
  case 2: dec_y(); break;
  case 3:dec_y();
  case 4: dec_x(); break;
  case 5:dec_x();
  case 6: inc_y(); break;
  case 7:inc_y(); inc_x(); break;
  }
  if (plotter.pen_down)
   plot(plotter.pos,plotter.pen);
 }
}

variable_types: length int

variable_types: i int


decompiled_code:
void __fastcall draw_to(int a1, int a2)
{
int v2; // edi
int v3; // esi
int v4; // er14
int v5; // er15
double v6; // rbx
int v7; // er13
double v8; // xmm2_8
double v9; // rbx
int i; // er13
double v11; // xmm3_8

v2 = a1 - plotter;
v3 = a2 - HIDWORD(plotter);
if ( v3 | v2 )
{
v4 = abs32(v2);
v5 = abs32(v3);
if ( v4 >= v5 )
{
if ( v4 > 0 )
{
v9 = *(double *)&LC0;
for ( i = 0; i != v4; ++i )
{
dot_count();
v11 = v9 + (double)v5 / (double)v4;
v9 = v11;
if ( v11 <= 0.5 )
{
if ( v2 <= 0 )
pen_step(4);
else
pen_step(0);
}
else
{
v9 = v11 - 1.0;
if ( v3 <= 0 )
{
if ( v2 <= 0 )
pen_step(3);
else
pen_step(1);
}
else if ( v2 <= 0 )
{
pen_step(5);
}
else
{
pen_step(7);
}
}
}
}
}
else
{
v6 = *(double *)&LC0;
v7 = 0;
do
{
dot_count();
v8 = v6 + (double)v4 / (double)v5;
v6 = v8;
if ( v8 <= 0.5 )
{
if ( v3 <= 0 )
pen_step(2);
else
pen_step(6);
}
else
{
v6 = v8 - 1.0;
if ( v2 <= 0 )
{
if ( v3 <= 0 )
pen_step(3);
else
pen_step(5);
}
else if ( v3 <= 0 )
{
pen_step(1);
}
else
{
pen_step(7);
}
}
++v7;
}
while ( v5 != v7 );
}
pen_up();
}
}

original_code:
void draw_to(int xdest, int ydest)
{
 int i;
 int dx=xdest-plotter.pos.x;
 int dy=ydest-plotter.pos.y;
 if (dx==0 && dy==0) return;
 if ( abs(dx) < abs(dy) ) {
  double frac=0.0;
  double step=((double)abs(dx))/((double)abs(dy));
  for (i=0;i<abs(dy);i++) {
   dot_count();
   frac+=step;
   if (frac>0.5) {
    frac-=1.0;
    if (dx>0) {
     if (dy>0) pen_step(7);
     else pen_step(1);
    } else {
     if (dy>0) pen_step(5);
     else pen_step(3);
    }
   } else {
    if (dy>0) pen_step(6);
    else pen_step(2);
   }
  }
 } else {
  double frac=0.0;
  double step=((double)abs(dy))/((double)abs(dx));
  for (i=0;i<abs(dx);i++) {
   dot_count();
   frac+=step;
   if (frac>0.5) {
    frac-=1.0;
    if (dy>0) {
     if (dx>0) pen_step(7);
     else pen_step(5);
    } else {
     if (dx>0) pen_step(1);
     else pen_step(3);
    }
   } else {
    if (dx>0) pen_step(0);
    else pen_step(4);
   }
  }
 }
 pen_up();
}

canonical_original_code:
void draw_to(int xdest, int ydest)
{
 int i;
 int dx = xdest-plotter.pos.x;
 int dy = ydest-plotter.pos.y;
 if (dx==0 && dy==0) return;
 if ( abs(dx) < abs(dy) ) {
  double frac = 0.0;
  double step = ((double)abs(dx))/((double)abs(dy));
  for (i=0;i<abs(dy);i++) {
   dot_count();
   frac+=step;
   if (frac>0.5) {
    frac-=1.0;
    if (dx>0) {
     if (dy>0) pen_step(7);
     else pen_step(1);
    } else {
     if (dy>0) pen_step(5);
     else pen_step(3);
    }
   } else {
    if (dy>0) pen_step(6);
    else pen_step(2);
   }
  }
 } else {
  double frac = 0.0;
  double step = ((double)abs(dy))/((double)abs(dx));
  for (i=0;i<abs(dx);i++) {
   dot_count();
   frac+=step;
   if (frac>0.5) {
    frac-=1.0;
    if (dy>0) {
     if (dx>0) pen_step(7);
     else pen_step(5);
    } else {
     if (dx>0) pen_step(1);
     else pen_step(3);
    }
   } else {
    if (dx>0) pen_step(0);
    else pen_step(4);
   }
  }
 }
 pen_up();
}

variable_types: xdest int

variable_types: ydest int

variable_types: dx int

variable_types: dy int

variable_types: frac double

variable_types: step double


decompiled_code:
__int64 init_plotter()
{
int v0; // ebx
__int64 result; // rax

dword_654 = 1;
HIDWORD(plotter) = 999;
HIDWORD(qword_65C) = 999;
v0 = 24;
do
{
result = pen_step(6);
--v0;
}
while ( v0 );
return result;
}

canonical_decompiled_code:
__int64 func27()
{
int v0; // ebx
__int64 result; // rax

dword_654 = 1;
HIDWORD(plotter) = 999;
HIDWORD(qword_65C) = 999;
v0 = 24;
do
{
result = func23(6);
--v0;
}
while ( v0 );
return result;
}

original_code:
void init_plotter()
{
 int i;
 plotter.char_size=1;
 plotter.origin.y = plotter.pos.y = 999;
 for (i=0;i<24;i++)
  pen_step(6);
}


repo: norisys/libqr

original_code:
static void render_line_1(unsigned char * out,
                          const unsigned char * in,
                          size_t mod_bits,
                          size_t dim,
                          unsigned long mark,
                          unsigned long space)
{
        unsigned char in_mask;
        size_t n, b;
        in_mask = 1;
        n = dim;
        while (n-- > 0) {
                unsigned long v = (*in & in_mask) ? mark : space;
                if ((in_mask <<= 1) == 0) {
                        in_mask = 1;
                        ++in;
                }
                b = mod_bits / 8;
                while (b-- > 0) {
                        *out++ = (unsigned char) v;
                        v >>= 8;
                }
        }
}

canonical_original_code:
static void render_line_1(unsigned char *out, unsigned char *in, unsigned long mod_bits, unsigned long dim, unsigned long mark, unsigned long space)
{
        unsigned char in_mask;
        unsigned long n; unsigned long b;
        in_mask = 1;
        n = dim;
        while (n-- > 0) {
                unsigned long v = (*in & in_mask) ? mark : space;
                if ((in_mask <<= 1) == 0) {
                        in_mask = 1;
                        ++in;
                }
                b = mod_bits / 8;
                while (b-- > 0) {
                        *out++ = (unsigned char) v;
                        v >>= 8;
                }
        }
}

variable_types:
out
unsigned char *

variable_types:
in
unsigned char *

variable_types:
dim
unsigned long

variable_types:
mark
unsigned long

variable_types:
space
unsigned long

variable_types:
n
unsigned long

variable_types:
b
unsigned long

variable_types:
v
unsigned long


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

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

original_code:
void qr_bitmap_destroy(struct qr_bitmap * bmp)
{
        if (bmp) {
                free(bmp->bits);
                free(bmp->mask);
                free(bmp);
        }
}

canonical_original_code:
void qr_bitmap_destroy(struct qr_bitmap *bmp)
{
        if (bmp) {
                free(bmp->bits);
                free(bmp->mask);
                free(bmp);
        }
}

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


decompiled_code:
__int64 __fastcall qr_bitmap_add_mask(_QWORD *a1)
{
size_t v1; // rbp
void *v2; // rax

v1 = a1[3] * a1[2];
v2 = malloc(v1);
a1[1] = v2;
if ( !v2 )
return 0xFFFFFFFFLL;
memset(v2, 255, v1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1)
{
size_t v1; // rbp
void *v2; // rax

v1 = a1[3] * a1[2];
v2 = malloc(v1);
a1[1] = v2;
if ( !v2 )
return 0xFFFFFFFFLL;
memset(v2, 255, v1);
return 0LL;
}

original_code:
int qr_bitmap_add_mask(struct qr_bitmap * bmp)
{
        size_t size = bmp->stride * bmp->width;
        bmp->mask = malloc(size);
        if (!bmp->mask)
                return -1;
        memset(bmp->mask, 0xFF, size);
        return 0;
}

canonical_original_code:
int qr_bitmap_add_mask(struct qr_bitmap *bmp)
{
        unsigned long size = bmp->stride * bmp->width;
        bmp->mask = malloc(size);
        if (!bmp->mask)
                return -1;
        memset(bmp->mask, 0xFF, size);
        return 0;
}

variable_types:
size
unsigned long

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


decompiled_code:
void **__fastcall qr_bitmap_clone(__int64 a1)
{
void **v2; // rax
void **v3; // rbx
void *v4; // rax
size_t v5; // r12
void *v6; // rdi

v2 = (void **)qr_bitmap_create(*(_QWORD *)(a1 + 24), *(_QWORD *)(a1 + 32), *(_QWORD *)(a1 + 8) != 0LL);
v3 = v2;
if ( v2 )
{
v4 = v2[2];
if ( v4 != *(void **)(a1 + 16) )
_assert_fail("bmp->stride == src->stride", "bitmap.c", 0x47u, "qr_bitmap_clone");
v5 = (_QWORD)v3[3] * (_QWORD)v4;
memcpy(*v3, *(const void **)a1, v5);
v6 = v3[1];
if ( v6 )
memcpy(v6, *(const void **)(a1 + 8), v5);
}
return v3;
}

canonical_decompiled_code:
void **__fastcall func4(__int64 a1)
{
void **v2; // rax
void **v3; // rbx
void *v4; // rax
size_t v5; // r12
void *v6; // rdi

v2 = (void **)qr_bitmap_create(*(_QWORD *)(a1 + 24), *(_QWORD *)(a1 + 32), *(_QWORD *)(a1 + 8) != 0LL);
v3 = v2;
if ( v2 )
{
v4 = v2[2];
if ( v4 != *(void **)(a1 + 16) )
_assert_fail("bmp->stride == src->stride", "bitmap.c", 0x47u, "qr_bitmap_clone");
v5 = (_QWORD)v3[3] * (_QWORD)v4;
memcpy(*v3, *(const void **)a1, v5);
v6 = v3[1];
if ( v6 )
memcpy(v6, *(const void **)(a1 + 8), v5);
}
return v3;
}

original_code:
struct qr_bitmap * qr_bitmap_clone(const struct qr_bitmap * src)
{
        struct qr_bitmap * bmp;
        size_t size;
        bmp = qr_bitmap_create(src->width, src->height, !!src->mask);
        if (!bmp)
                return 0;
        ((bmp->stride == src->stride) ? (void) (0) : __assert_fail ("bmp->stride == src->stride", "bitmap.c", 71, __extension__ __PRETTY_FUNCTION__));
        size = bmp->width * bmp->stride;
        memcpy(bmp->bits, src->bits, size);
        if (bmp->mask)
                memcpy(bmp->mask, src->mask, size);
        return bmp;
}

canonical_original_code:
struct qr_bitmap *qr_bitmap_clone(struct qr_bitmap *src)
{
        struct qr_bitmap *bmp;
        unsigned long size;
        bmp = qr_bitmap_create(src->width, src->height, !!src->mask);
        if (!bmp)
                return 0;
        ((bmp->stride == src->stride) ? (void) (0) : __assert_fail ("bmp->stride == src->stride", "bitmap.c", 71, __extension__ __PRETTY_FUNCTION__));
        size = bmp->width * bmp->stride;
        memcpy(bmp->bits, src->bits, size);
        if (bmp->mask)
                memcpy(bmp->mask, src->mask, size);
        return bmp;
}

variable_types:
src
struct qr_bitmap *

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


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


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; } *

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;
}


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


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

v8 = qr_code_total_capacity(a1);
v9 = (a2 ^ 1u) + 4LL * (a1 - 1);
v10 = *((_DWORD *)&QR_DATA_WORD_COUNT + v9);
v11 = (v8 >> 3) - v10;
v12 = *((_DWORD *)&QR_RS_BLOCK_COUNT + 2 * v9);
*a3 = v12;
LODWORD(v8) = *((_DWORD *)&QR_RS_BLOCK_COUNT + 2 * v9 + 1);
a3[1] = v8;
v13 = v8 + v12;
*a4 = v10 / v13;
LODWORD(v9) = v10 / v13 + 1;
a4[1] = v9;
if ( a3[1] * (_DWORD)v9 + *a3 * (v10 / v13) != v10 )
_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] = v11 / v13;
*a5 = v11 / v13;
result = (unsigned int)(*a3 * (v11 / v13) + a3[1] * (v11 / v13));
if ( (_DWORD)result != v11 )
_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, _DWORD *a3, int *a4, int *a5)
{
unsigned __int64 v8; // rax
__int64 v9; // rdx
int v10; // edi
int v11; // esi
int v12; // ecx
int v13; // ecx
__int64 result; // rax

v8 = func2(a1);
v9 = (a2 ^ 1u) + 4LL * (a1 - 1);
v10 = *((_DWORD *)&QR_DATA_WORD_COUNT + v9);
v11 = (v8 >> 3) - v10;
v12 = *((_DWORD *)&QR_RS_BLOCK_COUNT + 2 * v9);
*a3 = v12;
LODWORD(v8) = *((_DWORD *)&QR_RS_BLOCK_COUNT + 2 * v9 + 1);
a3[1] = v8;
v13 = v8 + v12;
*a4 = v10 / v13;
LODWORD(v9) = v10 / v13 + 1;
a4[1] = v9;
if ( a3[1] * (_DWORD)v9 + *a3 * (v10 / v13) != v10 )
_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] = v11 / v13;
*a5 = v11 / v13;
result = (unsigned int)(*a3 * (v11 / v13) + a3[1] * (v11 / v13));
if ( (_DWORD)result != v11 )
_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:
unsigned __int64 __fastcall qr_mask_apply(__int64 *a1, int a2, __int64 a3, __int64 a4, unsigned __int64 a5)
{
unsigned __int64 i; // r11
unsigned __int64 result; // rax
unsigned __int64 v9; // rsi
unsigned __int64 v10; // r9
unsigned __int64 v11; // r10
unsigned __int64 v12; // rdi
__int64 v13; // r13
__int64 v14; // r15

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

canonical_decompiled_code:
unsigned __int64 __fastcall func4(__int64 *a1, int a2, __int64 a3, __int64 a4, unsigned __int64 a5)
{
unsigned __int64 i; // r11
unsigned __int64 result; // rax
unsigned __int64 v9; // rsi
unsigned __int64 v10; // r9
unsigned __int64 v11; // r10
unsigned __int64 v12; // rdi
__int64 v13; // r13
__int64 v14; // r15

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

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: 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


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


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:
void __fastcall qr_layout_end(void *a1)
{
free(a1);
}

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

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 v1; // er12
unsigned int v2; // ebx

v1 = 8;
v2 = 0;
do
{
v2 = ((*(_DWORD *)(a1 + 24) & **(unsigned __int8 **)(a1 + 32)) != 0) | (2 * v2);
advance((_DWORD *)a1);
--v1;
}
while ( v1 );
return v2;
}

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)
{
int v3; // ebp
__int64 result; // rax
char v5; // cl

v3 = 8;
do
{
v5 = 0;
if ( a2 < 0 )
v5 = *(_BYTE *)(a1 + 24);
**(_BYTE **)(a1 + 32) |= v5;
result = advance((_DWORD *)a1);
a2 *= 2;
--v3;
}
while ( v3 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, char a2)
{
int v3; // ebp
__int64 result; // rax
char v5; // cl

v3 = 8;
do
{
v5 = 0;
if ( a2 < 0 )
v5 = *(_BYTE *)(a1 + 24);
**(_BYTE **)(a1 + 32) |= v5;
result = func2((_DWORD *)a1);
a2 *= 2;
--v3;
}
while ( v3 );
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);
switch ( a1 )
{
case ' ':
result = 36LL;
break;
case '$':
result = 37LL;
break;
case '%':
result = 38LL;
break;
case '*':
result = 39LL;
break;
case '+':
result = 40LL;
break;
case '-':
result = 41LL;
break;
case '.':
result = 42LL;
break;
case '/':
result = 43LL;
break;
case ':':
result = 44LL;
break;
default:
result = 0xFFFFFFFFLL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(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);
switch ( a1 )
{
case ' ':
result = 36LL;
break;
case '$':
result = 37LL;
break;
case '%':
result = 38LL;
break;
case '*':
result = 39LL;
break;
case '+':
result = 40LL;
break;
case '-':
result = 41LL;
break;
case '.':
result = 42LL;
break;
case '/':
result = 43LL;
break;
case ':':
result = 44LL;
break;
default:
result = 0xFFFFFFFFLL;
break;
}
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; // rcx
unsigned __int64 v4; // rsi
__int64 v5; // rcx

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

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

if ( a1 == 2 )
return 11 * (a2 >> 1) + 6 * (a2 & 1);
result = 8 * a2;
if ( a1 != 4 )
{
result = 0LL;
if ( a1 == 1 )
{
v3 = 10 * (a2 / 3);
result = v3;
v4 = a2 % 3;
if ( v4 == 1 )
{
result = v3 + 4;
}
else
{
v5 = v3 + 7;
if ( v4 == 2 )
result = v5;
}
}
}
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)
{
__int64 v3; // r13
unsigned int v4; // ebx

v3 = qr_data_dpart_length(a1, a3);
v4 = 1;
while ( v3 + qr_data_size_field_length(v4, a1) + 4 >= (unsigned __int64)(8LL
* QR_DATA_WORD_COUNT[4 * (v4 - 1) + (a2 ^ 1)]) )
{
if ( ++v4 == 41 )
return (unsigned int)-1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, int a2, unsigned __int64 a3)
{
__int64 v3; // r13
unsigned int v4; // ebx

v3 = func3(a1, a3);
v4 = 1;
while ( v3 + qr_data_size_field_length(v4, a1) + 4 >= (unsigned __int64)(8LL
* QR_DATA_WORD_COUNT[4 * (v4 - 1) + (a2 ^ 1)]) )
{
if ( ++v4 == 41 )
return (unsigned int)-1;
}
return v4;
}

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)
{
int v2; // eax
int v3; // edx
__int64 result; // rax

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

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

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

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

decompiled_code:
__int64 __fastcall PerDown(__int64 a1, int a2, int a3)
{
int v3; // er9
__int64 result; // rax
int v5; // ecx

v3 = *(_DWORD *)(a1 + 4LL * a3);
result = (unsigned int)(2 * a3 + 1);
if ( a2 > (int)result )
{
while ( 1 )
{
v5 = result;
if ( a2 - 1 != (_DWORD)result && *(_DWORD *)(a1 + 4LL * (int)result) < *(_DWORD *)(a1 + 4LL * (int)result + 4) )
v5 = 2 * a3 + 2;
result = *(unsigned int *)(a1 + 4LL * v5);
if ( (int)result < v3 )
break;
*(_DWORD *)(a1 + 4LL * a3) = result;
result = (unsigned int)(2 * v5 + 1);
if ( (int)result >= a2 )
{
a3 = v5;
break;
}
a3 = v5;
}
}
*(_DWORD *)(a1 + 4LL * a3) = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, int a3)
{
int v3; // er9
__int64 result; // rax
int v5; // ecx

v3 = *(_DWORD *)(a1 + 4LL * a3);
result = (unsigned int)(2 * a3 + 1);
if ( a2 > (int)result )
{
while ( 1 )
{
v5 = result;
if ( a2 - 1 != (_DWORD)result && *(_DWORD *)(a1 + 4LL * (int)result) < *(_DWORD *)(a1 + 4LL * (int)result + 4) )
v5 = 2 * a3 + 2;
result = *(unsigned int *)(a1 + 4LL * v5);
if ( (int)result < v3 )
break;
*(_DWORD *)(a1 + 4LL * a3) = result;
result = (unsigned int)(2 * v5 + 1);
if ( (int)result >= a2 )
{
a3 = v5;
break;
}
a3 = v5;
}
}
*(_DWORD *)(a1 + 4LL * a3) = v3;
return result;
}

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; } **