original_code:
void write_item(item *i, int clientID, int shardID, int op, char *key, char *value, KV_t *pair, int file_position)
{
    if(i != ((void *)0)){
        if(clientID != -1){
            i->clientID = clientID;
        }
        if(shardID != -1){
            i->shardID = shardID;
        }
        if(op != -1){
            i->op = op;
        }
        if(file_position != -1) {
            i->file_position = file_position;
        }
        if(key != ((void *)0)){
            strncpy(i->key, key, 20);
        }
        if(value != ((void *)0)){
            if(i->value == ((void *)0)) {
                i->value = calloc(20, sizeof(char));
                if(i->value==((void *)0)) {
                    fprintf(stderr, "Dynamic memory allocation failed\n");
                    exit(1);
                }
            }
            strncpy(i->value, value, 20);
        }
        else {
            if(i->value != ((void *)0)){
                free(i->value);
                i->value = ((void *)0);
            }
        }
        if(pair != ((void *)0)){
            i->pair = pair;
        }
    }
}

canonical_original_code:
void write_item(struct _item *i, int clientID, int shardID, int op, char *key, char *value, struct KV_t *pair, int file_position)
{
    if(i != ((void *)0)){
        if(clientID != -1){
            i->clientID = clientID;
        }
        if(shardID != -1){
            i->shardID = shardID;
        }
        if(op != -1){
            i->op = op;
        }
        if(file_position != -1) {
            i->file_position = file_position;
        }
        if(key != ((void *)0)){
            strncpy(i->key, key, 20);
        }
        if(value != ((void *)0)){
            if(i->value == ((void *)0)) {
                i->value = calloc(20, sizeof(char));
                if(i->value==((void *)0)) {
                    fprintf(stderr, "Dynamic memory allocation failed\n");
                    exit(1);
                }
            }
            strncpy(i->value, value, 20);
        }
        else {
            if(i->value != ((void *)0)){
                free(i->value);
                i->value = ((void *)0);
            }
        }
        if(pair != ((void *)0)){
            i->pair = pair;
        }
    }
}

variable_types: clientID int

variable_types: shardID int

variable_types: op int

variable_types:
key
char *

variable_types:
value
char *

variable_types:
pair
struct KV_t *


populate

decompiled_code:
__int64 __fastcall populate(int a1)
{
++*(_DWORD *)(file_pos + 4LL * a1);
return add(*(_QWORD *)(shards + 8LL * a1));
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
++*(_DWORD *)(file_pos + 4LL * a1);
return add(*(_QWORD *)(shards + 8LL * a1));
}

original_code:
void populate(int shardId, char* key, char* value)
{
    int file_position = -1;
    file_position = file_pos[shardId];
    file_pos[shardId] += 1;
    add(shards[shardId], key, value, file_position);
}

canonical_original_code:
void populate(int shardId, char *key, char *value)
{
    int file_position = -1;
    file_position = file_pos[shardId];
    file_pos[shardId] += 1;
    add(shards[shardId], key, value, file_position);
}

variable_types: shardId int

return_type: void


writeToFile

decompiled_code:
int __fastcall writeToFile(unsigned int a1, const char *a2, const char *a3, int a4)
{
__int64 v5; // rbp
__int64 v6; // rbx
int v7; // eax
__int64 v8; // rdx
int v9; // eax
__int64 v10; // rdx
__int64 v11; // rdi
char v13[32]; // [rsp+0h] [rbp-88h] BYREF
char dest[32]; // [rsp+20h] [rbp-68h] BYREF
char v15[72]; // [rsp+40h] [rbp-48h] BYREF

v5 = a4;
v6 = (int)a1;
snprintf(v13, 0x14uLL, "fshard%d", a1);
strncpy(dest, a2, 0x14uLL);
strncpy(v15, a3, 0x14uLL);
v7 = strlen(a2);
if ( v7 > 18 )
{
v8 = v7;
}
else
{
memset(&dest[v7], 32, (unsigned int)(18 - v7) + 1LL);
v8 = 19LL;
}
dest[v8] = 0;
v9 = strlen(a3);
v10 = v9;
if ( v9 <= 18 )
{
memset(&v15[v9], 32, (unsigned int)(18 - v9) + 1LL);
v10 = 19LL;
}
v11 = mutexFiles;
v15[v10] = 0;
pthread_mutex_lock((pthread_mutex_t *)(40 * v6 + v11));
if ( (_DWORD)v5 != -1 )
fseek(*(FILE **)(files + 8 * v6), 40 * v5, 0);
fprintf(*(FILE **)(files + 8 * v6), "%s %s\n", dest, v15);
fflush(*(FILE **)(files + 8 * v6));
return pthread_mutex_unlock((pthread_mutex_t *)(40 * v6 + mutexFiles));
}

canonical_decompiled_code:
int __fastcall func2(unsigned int a1, const char *a2, const char *a3, int a4)
{
__int64 v5; // rbp
__int64 v6; // rbx
int v7; // eax
__int64 v8; // rdx
int v9; // eax
__int64 v10; // rdx
__int64 v11; // rdi
char v13[32]; // [rsp+0h] [rbp-88h] BYREF
char dest[32]; // [rsp+20h] [rbp-68h] BYREF
char v15[72]; // [rsp+40h] [rbp-48h] BYREF

v5 = a4;
v6 = (int)a1;
snprintf(v13, 0x14uLL, "fshard%d", a1);
strncpy(dest, a2, 0x14uLL);
strncpy(v15, a3, 0x14uLL);
v7 = strlen(a2);
if ( v7 > 18 )
{
v8 = v7;
}
else
{
memset(&dest[v7], 32, (unsigned int)(18 - v7) + 1LL);
v8 = 19LL;
}
dest[v8] = 0;
v9 = strlen(a3);
v10 = v9;
if ( v9 <= 18 )
{
memset(&v15[v9], 32, (unsigned int)(18 - v9) + 1LL);
v10 = 19LL;
}
v11 = mutexFiles;
v15[v10] = 0;
pthread_mutex_lock((pthread_mutex_t *)(40 * v6 + v11));
if ( (_DWORD)v5 != -1 )
fseek(*(FILE **)(files + 8 * v6), 40 * v5, 0);
fprintf(*(FILE **)(files + 8 * v6), "%s %s\n", dest, v15);
fflush(*(FILE **)(files + 8 * v6));
return pthread_mutex_unlock((pthread_mutex_t *)(40 * v6 + mutexFiles));
}

original_code:
void writeToFile(int shardId, char* key, char* value, int position)
{
    char name[20], key_copy[20], value_copy[20];
    int ix, spaces;
    snprintf(name, 20, "fshard%d", shardId);
    strncpy(key_copy, key, 20);
    strncpy(value_copy, value, 20);
    spaces = 20 - 1;
    for(ix = strlen(key); ix < spaces; ++ix) {
        key_copy[ix] = ' ';
    }
    key_copy[ix] = '\0';
    for(ix = strlen(value); ix < spaces; ++ix) {
        value_copy[ix] = ' ';
    }
    value_copy[ix] = '\0';
    pthread_mutex_lock(&mutexFiles[shardId]);
    if(position != -1) {
        fseek(files[shardId], 40 * sizeof(char) * position, 0);
    }
    fprintf(files[shardId], "%s %s\n", key_copy, value_copy);
    fflush(files[shardId]);
    pthread_mutex_unlock(&mutexFiles[shardId]);
}

canonical_original_code:
void writeToFile(int shardId, char *key, char *value, int position)
{
    char name[20]; char key_copy[20]; char value_copy[20];
    int ix; int spaces;
    snprintf(name, 20, "fshard%d", shardId);
    strncpy(key_copy, key, 20);
    strncpy(value_copy, value, 20);
    spaces = 20 - 1;
    for(ix = strlen(key); ix < spaces; ++ix) {
        key_copy[ix] = ' ';
    }
    key_copy[ix] = '\0';
    for(ix = strlen(value); ix < spaces; ++ix) {
        value_copy[ix] = ' ';
    }
    value_copy[ix] = '\0';
    pthread_mutex_lock(&mutexFiles[shardId]);
    if(position != -1) {
        fseek(files[shardId], 40 * sizeof(char) * position, 0);
    }
    fprintf(files[shardId], "%s %s\n", key_copy, value_copy);
    fflush(files[shardId]);
    pthread_mutex_unlock(&mutexFiles[shardId]);
}

variable_types: position int

variable_types:
name
char [20]

variable_types: ix int

variable_types: spaces int


producer

decompiled_code:
int __fastcall producer(__int64 a1)
{
__int64 v1; // rcx
__int64 v2; // rax

sem_wait(&semCanProd);
pthread_mutex_lock(&mutex);
v1 = index_producer;
v2 = *(_QWORD *)(b + 8);
index_producer = (index_producer + 1) % *(_DWORD *)b;
*(_QWORD *)(v2 + 8 * v1) = a1;
pthread_mutex_unlock(&mutex);
sem_post(&semCanCons);
return sem_wait((sem_t *)(a1 + 56));
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
__int64 v1; // rcx
__int64 v2; // rax

sem_wait(&semCanProd);
pthread_mutex_lock(&mutex);
v1 = index_producer;
v2 = *(_QWORD *)(b + 8);
index_producer = (index_producer + 1) % *(_DWORD *)b;
*(_QWORD *)(v2 + 8 * v1) = a1;
pthread_mutex_unlock(&mutex);
sem_post(&semCanCons);
return sem_wait((sem_t *)(a1 + 56));
}

original_code:
void producer(item *ip)
{
    int pos;
    sem_wait(&semCanProd);
    pthread_mutex_lock(&mutex);
    pos = index_producer++;
    index_producer %= b->size;
    b->items[pos] = ip;
    pthread_mutex_unlock(&mutex);
    sem_post(&semCanCons);
    sem_wait(&(ip->waiting));
}

canonical_original_code:
void producer(struct _item *ip)
{
    int pos;
    sem_wait(&semCanProd);
    pthread_mutex_lock(&mutex);
    pos = index_producer++;
    index_producer %= b->size;
    b->items[pos] = ip;
    pthread_mutex_unlock(&mutex);
    sem_post(&semCanCons);
    sem_wait(&(ip->waiting));
}

variable_types: struct _item { int clientID; int shardID; int dimension; int op; int file_position; char key[20]; char *value; struct KV_t { char key[20]; char value[20]; } *pair; union sem_t { char __size[32]; long __align; } waiting; } *

variable_types: pos int

user_defined_types: struct _item { int clientID; int shardID; int dimension; int op; int file_position; char key[20]; char *value; struct KV_t *pair; union sem_t waiting; }


decompiled_code:
char *__fastcall kos_get(unsigned int a1, unsigned int a2, __int64 a3)
{
__int64 v4; // rbp
const char *v5; // r13
char *v6; // rax
char *v7; // r12

v4 = init_item();
write_item(v4, a1, a2, 2LL, a3, 0LL, 0LL, -1LL);
producer(v4);
v5 = *(const char **)(v4 + 40);
if ( v5 )
{
v6 = (char *)calloc(0x14uLL, 1uLL);
v7 = v6;
if ( !v6 )
{
fwrite("Dynamic memory allocation failed\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
strncpy(v6, v5, 0x14uLL);
}
else
{
v7 = 0LL;
}
destroy_item(v4);
return v7;
}

canonical_decompiled_code:
char *__fastcall func4(unsigned int a1, unsigned int a2, __int64 a3)
{
__int64 v4; // rbp
const char *v5; // r13
char *v6; // rax
char *v7; // r12

v4 = init_item();
write_item(v4, a1, a2, 2LL, a3, 0LL, 0LL, -1LL);
func3(v4);
v5 = *(const char **)(v4 + 40);
if ( v5 )
{
v6 = (char *)calloc(0x14uLL, 1uLL);
v7 = v6;
if ( !v6 )
{
fwrite("Dynamic memory allocation failed\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
strncpy(v6, v5, 0x14uLL);
}
else
{
v7 = 0LL;
}
destroy_item(v4);
return v7;
}

original_code:
char* kos_get(int clientid, int shardId, char* key)
{
    char *ret = ((void *)0);
    item *i = ((void *)0);
    i = init_item();
    write_item(i, clientid, shardId, 2, key, ((void *)0), ((void *)0), -1);
    producer(i);
    if(i->value != ((void *)0)) {
        ret = calloc(20, sizeof(char));
        if(ret==((void *)0)) {
            fprintf(stderr, "Dynamic memory allocation failed\n");
            exit(1);
        }
        strncpy(ret, i->value, 20);
    }
    destroy_item(i);
    return ret;
}

canonical_original_code:
char *kos_get(int clientid, int shardId, char *key)
{
    char *ret = ((void *)0);
    struct _item *i = ((void *)0);
    i = init_item();
    write_item(i, clientid, shardId, 2, key, ((void *)0), ((void *)0), -1);
    producer(i);
    if(i->value != ((void *)0)) {
        ret = calloc(20, sizeof(char));
        if(ret==((void *)0)) {
            fprintf(stderr, "Dynamic memory allocation failed\n");
            exit(1);
        }
        strncpy(ret, i->value, 20);
    }
    destroy_item(i);
    return ret;
}

variable_types: clientid int

variable_types:
ret
char *

return_type: char *

user_defined_types: union sem_t { char __size[32]; long __align; }

user_defined_types: struct KV_t { char key[20]; char value[20]; }


decompiled_code:
char *__fastcall kos_put(unsigned int a1, signed int a2, const char *a3, const char *a4)
{
__int64 v6; // rbx
__int64 v8; // rbp
_DWORD *v9; // rax
__int64 v10; // r15
char *v11; // rax
const char *v12; // rsi
char *v13; // r8
char *src; // [rsp+8h] [rbp-40h]
char *srca; // [rsp+8h] [rbp-40h]
char *srcb; // [rsp+8h] [rbp-40h]

v6 = a2;
v8 = init_item();
pthread_mutex_lock(&mutex_filepos);
if ( (unsigned int)lst_size(*(_QWORD *)(invalids + 8LL * a2)) )
{
v10 = (unsigned int)lst_remove_pos(*(_QWORD *)(invalids + 8LL * a2));
}
else
{
v9 = (_DWORD *)(file_pos + 4LL * a2);
v10 = (unsigned int)*v9;
*v9 = v10 + 1;
}
pthread_mutex_unlock(&mutex_filepos);
write_item(v8, a1, (unsigned int)a2, 1LL, a3, a4, 0LL, v10);
producer(v8);
if ( *(_QWORD *)(v8 + 40) )
{
src = *(char **)(v8 + 40);
v11 = (char *)calloc(0x14uLL, 1uLL);
if ( !v11 )
{
fwrite("Dynamic memory allocation failed\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
v12 = src;
srca = v11;
strncpy(v11, v12, 0x14uLL);
writeToFile(a2, a3, a4, *(_DWORD *)(v8 + 16));
lst_insert_pos(*(_QWORD *)(invalids + 8 * v6), (unsigned int)v10);
v13 = srca;
}
else
{
writeToFile(a2, a3, a4, v10);
v13 = 0LL;
}
srcb = v13;
destroy_item(v8);
return srcb;
}

canonical_decompiled_code:
char *__fastcall func5(unsigned int a1, signed int a2, const char *a3, const char *a4)
{
__int64 v6; // rbx
__int64 v8; // rbp
_DWORD *v9; // rax
__int64 v10; // r15
char *v11; // rax
const char *v12; // rsi
char *v13; // r8
char *src; // [rsp+8h] [rbp-40h]
char *srca; // [rsp+8h] [rbp-40h]
char *srcb; // [rsp+8h] [rbp-40h]

v6 = a2;
v8 = init_item();
pthread_mutex_lock(&mutex_filepos);
if ( (unsigned int)lst_size(*(_QWORD *)(invalids + 8LL * a2)) )
{
v10 = (unsigned int)lst_remove_pos(*(_QWORD *)(invalids + 8LL * a2));
}
else
{
v9 = (_DWORD *)(file_pos + 4LL * a2);
v10 = (unsigned int)*v9;
*v9 = v10 + 1;
}
pthread_mutex_unlock(&mutex_filepos);
write_item(v8, a1, (unsigned int)a2, 1LL, a3, a4, 0LL, v10);
func3(v8);
if ( *(_QWORD *)(v8 + 40) )
{
src = *(char **)(v8 + 40);
v11 = (char *)calloc(0x14uLL, 1uLL);
if ( !v11 )
{
fwrite("Dynamic memory allocation failed\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
v12 = src;
srca = v11;
strncpy(v11, v12, 0x14uLL);
func2(a2, a3, a4, *(_DWORD *)(v8 + 16));
lst_insert_pos(*(_QWORD *)(invalids + 8 * v6), (unsigned int)v10);
v13 = srca;
}
else
{
func2(a2, a3, a4, v10);
v13 = 0LL;
}
srcb = v13;
destroy_item(v8);
return srcb;
}

original_code:
char* kos_put(int clientid, int shardId, char* key, char* value)
{
    int file_position = -1;
    char *ret = ((void *)0);
    item *i = ((void *)0);
    i = init_item();
    pthread_mutex_lock(&mutex_filepos);
    if(lst_size(invalids[shardId]) == 0) {
        file_position = file_pos[shardId];
        file_pos[shardId] += 1;
    }
    else {
        file_position = lst_remove_pos(invalids[shardId]);
    }
    pthread_mutex_unlock(&mutex_filepos);
    write_item(i, clientid, shardId, 1, key, value, ((void *)0), file_position);
    producer(i);
    if(i->value != ((void *)0)) {
        ret = calloc(20, sizeof(char));
        if(ret==((void *)0)) {
            fprintf(stderr, "Dynamic memory allocation failed\n");
            exit(1);
        }
        strncpy(ret, i->value, 20);
    }
    if(ret != ((void *)0)) {
        writeToFile(shardId, key, value, i->file_position);
        lst_insert_pos(invalids[shardId], file_position);
    }
    else {
        writeToFile(shardId, key, value, file_position);
    }
    destroy_item(i);
    return ret;
}

canonical_original_code:
char *kos_put(int clientid, int shardId, char *key, char *value)
{
    int file_position = -1;
    char *ret = ((void *)0);
    struct _item *i = ((void *)0);
    i = init_item();
    pthread_mutex_lock(&mutex_filepos);
    if(lst_size(invalids[shardId]) == 0) {
        file_position = file_pos[shardId];
        file_pos[shardId] += 1;
    }
    else {
        file_position = lst_remove_pos(invalids[shardId]);
    }
    pthread_mutex_unlock(&mutex_filepos);
    write_item(i, clientid, shardId, 1, key, value, ((void *)0), file_position);
    producer(i);
    if(i->value != ((void *)0)) {
        ret = calloc(20, sizeof(char));
        if(ret==((void *)0)) {
            fprintf(stderr, "Dynamic memory allocation failed\n");
            exit(1);
        }
        strncpy(ret, i->value, 20);
    }
    if(ret != ((void *)0)) {
        writeToFile(shardId, key, value, i->file_position);
        lst_insert_pos(invalids[shardId], file_position);
    }
    else {
        writeToFile(shardId, key, value, file_position);
    }
    destroy_item(i);
    return ret;
}


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

v4 = init_item();
write_item(v4, a1, a2, 3LL, a3, 0LL, 0LL, -1LL);
producer(v4);
v5 = invalids;
v6 = *(_QWORD *)(v4 + 40);
*(_QWORD *)(v4 + 40) = 0LL;
lst_insert_pos(*(_QWORD *)(v5 + 8LL * (int)a2), *(unsigned int *)(v4 + 16));
destroy_item(v4);
return v6;
}

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

v4 = init_item();
write_item(v4, a1, a2, 3LL, a3, 0LL, 0LL, -1LL);
func3(v4);
v5 = invalids;
v6 = *(_QWORD *)(v4 + 40);
*(_QWORD *)(v4 + 40) = 0LL;
lst_insert_pos(*(_QWORD *)(v5 + 8LL * (int)a2), *(unsigned int *)(v4 + 16));
destroy_item(v4);
return v6;
}

original_code:
char* kos_remove(int clientid, int shardId, char* key)
{
    char *ret = ((void *)0);
    item *i = ((void *)0);
    i = init_item();
    write_item(i, clientid, shardId, 3, key, ((void *)0), ((void *)0), -1);
    producer(i);
    ret = i->value;
    i->value = ((void *)0);
    lst_insert_pos(invalids[shardId], i->file_position);
    destroy_item(i);
    return ret;
}

canonical_original_code:
char *kos_remove(int clientid, int shardId, char *key)
{
    char *ret = ((void *)0);
    struct _item *i = ((void *)0);
    i = init_item();
    write_item(i, clientid, shardId, 3, key, ((void *)0), ((void *)0), -1);
    producer(i);
    ret = i->value;
    i->value = ((void *)0);
    lst_insert_pos(invalids[shardId], i->file_position);
    destroy_item(i);
    return ret;
}


decompiled_code:
__int64 __fastcall kos_getAllKeys(unsigned int a1, unsigned int a2, _DWORD *a3)
{
__int64 v4; // rbp
__int64 v5; // r12

v4 = init_item();
write_item(v4, a1, a2, 4LL, 0LL, 0LL, 0LL, -1LL);
producer(v4);
v5 = *(_QWORD *)(v4 + 48);
*a3 = *(_DWORD *)(v4 + 8);
*(_QWORD *)(v4 + 48) = 0LL;
destroy_item(v4);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, unsigned int a2, _DWORD *a3)
{
__int64 v4; // rbp
__int64 v5; // r12

v4 = init_item();
write_item(v4, a1, a2, 4LL, 0LL, 0LL, 0LL, -1LL);
func3(v4);
v5 = *(_QWORD *)(v4 + 48);
*a3 = *(_DWORD *)(v4 + 8);
*(_QWORD *)(v4 + 48) = 0LL;
destroy_item(v4);
return v5;
}

original_code:
KV_t* kos_getAllKeys(int clientid, int shardId, int* dim)
{
    item *i = ((void *)0);
    KV_t *pair;
    i = init_item();
    write_item(i, clientid, shardId, 4, ((void *)0), ((void *)0), ((void *)0), -1);
    producer(i);
    *dim = i->dimension;
    pair = i->pair;
    i->pair = ((void *)0);
    destroy_item(i);
    return pair;
}

canonical_original_code:
struct KV_t *kos_getAllKeys(int clientid, int shardId, int *dim)
{
    struct _item *i = ((void *)0);
    struct KV_t *pair;
    i = init_item();
    write_item(i, clientid, shardId, 4, ((void *)0), ((void *)0), ((void *)0), -1);
    producer(i);
    *dim = i->dimension;
    pair = i->pair;
    i->pair = ((void *)0);
    destroy_item(i);
    return pair;
}

variable_types:
dim
int *

variable_types: struct KV_t { char key[20]; char value[20]; } *


decompiled_code:
__int64 __fastcall op_handler(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // [rsp-10h] [rbp-18h]
__int64 v5; // [rsp-10h] [rbp-18h]

result = *(unsigned int *)(a1 + 12);
if ( (_DWORD)result != 3 )
{
if ( (int)result > 3 )
{
if ( (_DWORD)result != 4 )
return result;
v5 = getAllKeys(*(_QWORD *)(shards + 8LL * *(int *)(a1 + 4)), a1 + 8);
goto LABEL_9;
}
if ( (_DWORD)result == 1 )
{
v3 = add(*(_QWORD *)(shards + 8LL * *(int *)(a1 + 4)));
write_item(a1, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0LL, *(_QWORD *)v3, 0LL, *(unsigned int *)(v3 + 8));
return v4;
}
if ( (_DWORD)result != 2 )
return result;
v2 = get(*(_QWORD *)(shards + 8LL * *(int *)(a1 + 4)), a1 + 20);
if ( v2 )
goto LABEL_6;
LABEL_14:
v5 = 0LL;
LABEL_9:
write_item(a1, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0LL, 0LL, v5, -1LL);
return v4;
}
v2 = ht_remove(*(_QWORD *)(shards + 8LL * *(int *)(a1 + 4)), a1 + 20);
if ( !v2 )
goto LABEL_14;
LABEL_6:
write_item(a1, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0LL, *(_QWORD *)v2, 0LL, *(unsigned int *)(v2 + 8));
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // [rsp-10h] [rbp-18h]
__int64 v5; // [rsp-10h] [rbp-18h]

result = *(unsigned int *)(a1 + 12);
if ( (_DWORD)result != 3 )
{
if ( (int)result > 3 )
{
if ( (_DWORD)result != 4 )
return result;
v5 = getAllKeys(*(_QWORD *)(shards + 8LL * *(int *)(a1 + 4)), a1 + 8);
goto LABEL_9;
}
if ( (_DWORD)result == 1 )
{
v3 = add(*(_QWORD *)(shards + 8LL * *(int *)(a1 + 4)));
write_item(a1, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0LL, *(_QWORD *)v3, 0LL, *(unsigned int *)(v3 + 8));
return v4;
}
if ( (_DWORD)result != 2 )
return result;
v2 = get(*(_QWORD *)(shards + 8LL * *(int *)(a1 + 4)), a1 + 20);
if ( v2 )
goto LABEL_6;
LABEL_14:
v5 = 0LL;
LABEL_9:
write_item(a1, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0LL, 0LL, v5, -1LL);
return v4;
}
v2 = ht_remove(*(_QWORD *)(shards + 8LL * *(int *)(a1 + 4)), a1 + 20);
if ( !v2 )
goto LABEL_14;
LABEL_6:
write_item(a1, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0LL, *(_QWORD *)v2, 0LL, *(unsigned int *)(v2 + 8));
return v4;
}

original_code:
void op_handler(item *i)
{
    lst_ret_t *oldvalue = ((void *)0);
    KV_t *pair = ((void *)0);
        switch(i->op) {
            case 1:
                oldvalue = add(shards[i->shardID], i->key, i->value, i->file_position);
                write_item(i, -1, -1, -1, ((void *)0), oldvalue->value, ((void *)0), oldvalue->position);
                break;
            case 3:
                oldvalue = ht_remove(shards[i->shardID], i->key);
                if(oldvalue == ((void *)0)) {
                    write_item(i, -1, -1, -1, ((void *)0), ((void *)0), ((void *)0), -1);
                }
                else {
                    write_item(i, -1, -1, -1, ((void *)0), oldvalue->value, ((void *)0), oldvalue->position);
                }
                break;
            case 2:
                oldvalue = get(shards[i->shardID], i->key);
                if(oldvalue == ((void *)0)) {
                    write_item(i, -1, -1, -1, ((void *)0), ((void *)0), ((void *)0), -1);
                }
                else {
                    write_item(i, -1, -1, -1, ((void *)0), oldvalue->value, ((void *)0), oldvalue->position);
                }
                break;
            case 4:
                pair = getAllKeys(shards[i->shardID], &(i->dimension));
                write_item(i, -1, -1, -1, ((void *)0), ((void *)0), pair, -1);
                break;
            default:
                break;
        }
}

canonical_original_code:
void op_handler(struct _item *i)
{
    struct lst_ret_t *oldvalue = ((void *)0);
    struct KV_t *pair = ((void *)0);
        switch(i->op) {
            case 1:
                oldvalue = add(shards[i->shardID], i->key, i->value, i->file_position);
                write_item(i, -1, -1, -1, ((void *)0), oldvalue->value, ((void *)0), oldvalue->position);
                break;
            case 3:
                oldvalue = ht_remove(shards[i->shardID], i->key);
                if(oldvalue == ((void *)0)) {
                    write_item(i, -1, -1, -1, ((void *)0), ((void *)0), ((void *)0), -1);
                }
                else {
                    write_item(i, -1, -1, -1, ((void *)0), oldvalue->value, ((void *)0), oldvalue->position);
                }
                break;
            case 2:
                oldvalue = get(shards[i->shardID], i->key);
                if(oldvalue == ((void *)0)) {
                    write_item(i, -1, -1, -1, ((void *)0), ((void *)0), ((void *)0), -1);
                }
                else {
                    write_item(i, -1, -1, -1, ((void *)0), oldvalue->value, ((void *)0), oldvalue->position);
                }
                break;
            case 4:
                pair = getAllKeys(shards[i->shardID], &(i->dimension));
                write_item(i, -1, -1, -1, ((void *)0), ((void *)0), pair, -1);
                break;
            default:
                break;
        }
}

variable_types: struct lst_ret_t { char *value; int position; } *


consumer

decompiled_code:
int consumer()
{
__int64 *v0; // rax
__int64 v1; // rbx

sem_wait(&semCanCons);
pthread_mutex_lock(&mutex);
v0 = (__int64 *)(*(_QWORD *)(b + 8) + 8LL * index_consumer);
v1 = *v0;
index_consumer = (index_consumer + 1) % *(_DWORD *)b;
*v0 = 0LL;
pthread_mutex_unlock(&mutex);
sem_post(&semCanProd);
op_handler(v1);
return sem_post((sem_t *)(v1 + 56));
}

canonical_decompiled_code:
int func9()
{
__int64 *v0; // rax
__int64 v1; // rbx

sem_wait(&semCanCons);
pthread_mutex_lock(&mutex);
v0 = (__int64 *)(*(_QWORD *)(b + 8) + 8LL * index_consumer);
v1 = *v0;
index_consumer = (index_consumer + 1) % *(_DWORD *)b;
*v0 = 0LL;
pthread_mutex_unlock(&mutex);
sem_post(&semCanProd);
func8(v1);
return sem_post((sem_t *)(v1 + 56));
}

original_code:
void consumer()
{
    int pos;
    item *i = ((void *)0);
    sem_wait(&semCanCons);
    pthread_mutex_lock(&mutex);
    pos = index_consumer++;
    index_consumer %= b->size;
    i = b->items[pos];
    b->items[pos] = ((void *)0);
    pthread_mutex_unlock(&mutex);
    sem_post(&semCanProd);
    op_handler(i);
    sem_post(&(i->waiting));
}

canonical_original_code:
void consumer()
{
    int pos;
    struct _item *i = ((void *)0);
    sem_wait(&semCanCons);
    pthread_mutex_lock(&mutex);
    pos = index_consumer++;
    index_consumer %= b->size;
    i = b->items[pos];
    b->items[pos] = ((void *)0);
    pthread_mutex_unlock(&mutex);
    sem_post(&semCanProd);
    op_handler(i);
    sem_post(&(i->waiting));
}


decompiled_code:
void __fastcall __noreturn server_thread(void *a1)
{
while ( 1 )
consumer();
}

canonical_decompiled_code:
void __fastcall __noreturn func10(void *a1)
{
while ( 1 )
func9();
}

original_code:
void *server_thread(void *arg)
{
    while(1) {
        consumer();
    }
}

variable_types:
arg
void *

return_type: void *


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

result = malloc(0x10uLL);
*(_QWORD *)result = 0LL;
result[2] = 0;
return result;
}

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

result = malloc(0x10uLL);
*(_QWORD *)result = 0LL;
result[2] = 0;
return result;
}

original_code:
list_t* lst_new()
{
   list_t *list;
   list = (list_t*) malloc(sizeof(list_t));
   list->first = ((void *)0);
   list->size = 0;
   return list;
}

canonical_original_code:
struct list_t *lst_new()
{
   struct list_t *list;
   list = (struct list_t *) malloc(sizeof(list_t));
   list->first = ((void *)0);
   list->size = 0;
   return list;
}

variable_types: struct list_t { struct lst_iitem { struct KV_t { char key[20]; char value[20]; } *item; int file_position; struct lst_iitem *next; } *first; int size; } *

user_defined_types: struct list_t { struct lst_iitem *first; int size; }

user_defined_types: struct lst_iitem { struct KV_t *item; int file_position; struct lst_iitem *next; }


decompiled_code:
void __fastcall lst_destroy(void *ptr)
{
void **v1; // rax
void **i; // rbx

v1 = *(void ***)ptr;
if ( *(_QWORD *)ptr )
{
for ( i = (void **)v1[2]; ; i = (void **)i[2] )
{
free(*v1);
free(*(void **)ptr);
*(_QWORD *)ptr = i;
if ( !i )
break;
v1 = i;
}
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void **v1; // rax
void **i; // rbx

v1 = *(void ***)ptr;
if ( *(_QWORD *)ptr )
{
for ( i = (void **)v1[2]; ; i = (void **)i[2] )
{
free(*v1);
free(*(void **)ptr);
*(_QWORD *)ptr = i;
if ( !i )
break;
v1 = i;
}
}
free(ptr);
}

original_code:
void lst_destroy(list_t *list)
{
    if(list->first != ((void *)0)) {
        lst_iitem_t *tempA;
        tempA = list->first->next;
        while(list->first != ((void *)0)) {
            free(list->first->item);
            free(list->first);
            list->first = tempA;
            if(tempA != ((void *)0)) {
                tempA = list->first->next;
            }
        }
    }
    free(list);
}

canonical_original_code:
void lst_destroy(struct list_t *list)
{
    if(list->first != ((void *)0)) {
        struct lst_iitem *tempA;
        tempA = list->first->next;
        while(list->first != ((void *)0)) {
            free(list->first->item);
            free(list->first);
            list->first = tempA;
            if(tempA != ((void *)0)) {
                tempA = list->first->next;
            }
        }
    }
    free(list);
}

variable_types: struct lst_iitem { struct KV_t { char key[20]; char value[20]; } *item; int file_position; struct lst_iitem *next; } *


decompiled_code:
__int64 __fastcall lst_insert_pos(__int64 *a1, int a2)
{
_DWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax

if ( !a1 )
return 0LL;
v2 = calloc(1uLL, 0x18uLL);
if ( !v2 )
{
fwrite("\ndynamic memory allocation failed\n", 1uLL, 0x22uLL, stderr);
exit(1);
}
v3 = *a1;
v2[2] = a2;
*a1 = (__int64)v2;
*((_QWORD *)v2 + 2) = v3;
result = 0LL;
++*((_DWORD *)a1 + 2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1, int a2)
{
_DWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax

if ( !a1 )
return 0LL;
v2 = calloc(1uLL, 0x18uLL);
if ( !v2 )
{
fwrite("\ndynamic memory allocation failed\n", 1uLL, 0x22uLL, stderr);
exit(1);
}
v3 = *a1;
v2[2] = a2;
*a1 = (__int64)v2;
*((_QWORD *)v2 + 2) = v3;
result = 0LL;
++*((_DWORD *)a1 + 2);
return result;
}

original_code:
int lst_insert_pos(list_t *list, int file_position)
{
    if(list != ((void *)0)) {
        lst_iitem_t* item = ((void *)0);
        item = calloc(1, sizeof(lst_iitem_t));
        if(item==((void *)0)) {
            fprintf(stderr, "\ndynamic memory allocation failed\n");
            exit(1);
        }
        item->item = ((void *)0);
        item->file_position = file_position;
        item->next = list->first;
        list->first = item;
        list->size += 1;
    }
    return 0;
}

canonical_original_code:
int lst_insert_pos(struct list_t *list, int file_position)
{
    if(list != ((void *)0)) {
        struct lst_iitem *item = ((void *)0);
        item = calloc(1, sizeof(lst_iitem_t));
        if(item==((void *)0)) {
            fprintf(stderr, "\ndynamic memory allocation failed\n");
            exit(1);
        }
        item->item = ((void *)0);
        item->file_position = file_position;
        item->next = list->first;
        list->first = item;
        list->size += 1;
    }
    return 0;
}

return_type: int


decompiled_code:
__int64 __fastcall lst_remove_pos(__int64 *a1)
{
unsigned int v1; // er12
__int64 v3; // rdi

if ( !a1 )
return v1;
v3 = *a1;
if ( v3 )
{
v1 = *(_DWORD *)(v3 + 8);
*a1 = *(_QWORD *)(v3 + 16);
free((void *)v3);
--*((_DWORD *)a1 + 2);
}
return v1;
}

original_code:
int lst_remove_pos(list_t *list)
{
    int ret;
    lst_iitem_t *iterA = ((void *)0), *iterB = ((void *)0);
    if(list != ((void *)0)) {
        iterA = list->first;
        if(iterA != ((void *)0)) {
            ret = iterA->file_position;
            iterB = iterA->next;
            list->first = iterB;
            free(iterA);
            list->size -= 1;
        }
    }
    return ret;
}

canonical_original_code:
int lst_remove_pos(struct list_t *list)
{
    int ret;
    struct lst_iitem *iterA = ((void *)0); struct lst_iitem *iterB = ((void *)0);
    if(list != ((void *)0)) {
        iterA = list->first;
        if(iterA != ((void *)0)) {
            ret = iterA->file_position;
            iterB = iterA->next;
            list->first = iterB;
            free(iterA);
            list->size -= 1;
        }
    }
    return ret;
}

variable_types: ret int


decompiled_code:
int __fastcall lst_print(__int64 *a1)
{
__int64 i; // rbx
int result; // eax

for ( i = *a1; i; i = *(_QWORD *)(i + 16) )
result = printf("key: %s, value: %s\n", *(const char **)i, (const char *)(*(_QWORD *)i + 20LL));
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 *a1)
{
__int64 i; // rbx
int result; // eax

for ( i = *a1; i; i = *(_QWORD *)(i + 16) )
result = printf("key: %s, value: %s\n", *(const char **)i, (const char *)(*(_QWORD *)i + 20LL));
return result;
}

original_code:
void lst_print(list_t *list)
{
    lst_iitem_t *temp;
    for(temp = list->first; temp != ((void *)0); temp = temp->next) {
        printf("key: %s, value: %s\n", temp->item->key, temp->item->value);
    }
}

canonical_original_code:
void lst_print(struct list_t *list)
{
    struct lst_iitem *temp;
    for(temp = list->first; temp != ((void *)0); temp = temp->next) {
        printf("key: %s, value: %s\n", temp->item->key, temp->item->value);
    }
}


decompiled_code:
__int64 __fastcall lst_size(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int lst_size(list_t *list)
{
    return list->size;
}

canonical_original_code:
int lst_size(struct list_t *list)
{
    return list->size;
}


decompiled_code:
__int64 __fastcall delete_hashtable(void *ptr)
{
__int64 v2; // r12
pthread_rwlock_t *v3; // r14
int v4; // ebx
pthread_rwlock_t *v5; // rdi
__int64 v6; // rbx
__int64 v7; // r12
__int64 v8; // rdi

if ( !ptr )
return 0LL;
v2 = *(int *)ptr;
if ( (int)v2 > 0 )
{
v3 = (pthread_rwlock_t *)((char *)ptr + 16);
v4 = 0;
do
{
v5 = v3;
++v4;
++v3;
pthread_rwlock_destroy(v5);
}
while ( *(_DWORD *)ptr > v4 );
}
v6 = 8LL * ((int)v2 - 1);
v7 = 8 * (v2 - (unsigned int)(v2 - 1)) - 16;
do
{
v8 = *(_QWORD *)(*((_QWORD *)ptr + 1) + v6);
v6 -= 8LL;
lst_destroy(v8);
}
while ( v7 != v6 );
free(*((void **)ptr + 1));
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *ptr)
{
__int64 v2; // r12
pthread_rwlock_t *v3; // r14
int v4; // ebx
pthread_rwlock_t *v5; // rdi
__int64 v6; // rbx
__int64 v7; // r12
__int64 v8; // rdi

if ( !ptr )
return 0LL;
v2 = *(int *)ptr;
if ( (int)v2 > 0 )
{
v3 = (pthread_rwlock_t *)((char *)ptr + 16);
v4 = 0;
do
{
v5 = v3;
++v4;
++v3;
pthread_rwlock_destroy(v5);
}
while ( *(_DWORD *)ptr > v4 );
}
v6 = 8LL * ((int)v2 - 1);
v7 = 8 * (v2 - (unsigned int)(v2 - 1)) - 16;
do
{
v8 = *(_QWORD *)(*((_QWORD *)ptr + 1) + v6);
v6 -= 8LL;
lst_destroy(v8);
}
while ( v7 != v6 );
free(*((void **)ptr + 1));
free(ptr);
return 0LL;
}

original_code:
int delete_hashtable(hashtable *h)
{
    if(h != ((void *)0)) {
        int size = h->size, ix;
        --size;
        for(ix = 0; ix < h->size; ix += 1) {
            pthread_rwlock_destroy(&(h->rwlock[ix]));
        }
        do {
            lst_destroy(h->lists[size]);
        } while(size--);
        free(h->lists);
        h->lists = ((void *)0);
        free(h);
        h = ((void *)0);
    }
    return 0;
}

canonical_original_code:
int delete_hashtable(struct hashtable *h)
{
    if(h != ((void *)0)) {
        int size = h->size; int ix;
        --size;
        for(ix = 0; ix < h->size; ix += 1) {
            pthread_rwlock_destroy(&(h->rwlock[ix]));
        }
        do {
            lst_destroy(h->lists[size]);
        } while(size--);
        free(h->lists);
        h->lists = ((void *)0);
        free(h);
        h = ((void *)0);
    }
    return 0;
}

variable_types: struct hashtable { int size; struct list_t { struct lst_iitem { struct KV_t { char key[20]; char value[20]; } *item; int file_position; struct lst_iitem *next; } *first; int size; } **lists; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } rwlock[10]; } *

variable_types: size int

user_defined_types: struct hashtable { int size; struct list_t **lists; union pthread_rwlock_t rwlock[10]; }

user_defined_types: union pthread_rwlock_t { struct __pthread_rwlock_arch_t __data; char __size[56]; long __align; }


hash

decompiled_code:
__int64 __fastcall hash(_BYTE *a1)
{
int v1; // eax
int v2; // edx

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = (char)*a1;
if ( !*a1 )
return 0LL;
v2 = 0;
do
{
++a1;
v2 += v1;
v1 = (char)*a1;
}
while ( *a1 );
return (unsigned int)(v2 % 10);
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1)
{
int v1; // eax
int v2; // edx

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = (char)*a1;
if ( !*a1 )
return 0LL;
v2 = 0;
do
{
++a1;
v2 += v1;
v1 = (char)*a1;
}
while ( *a1 );
return (unsigned int)(v2 % 10);
}

original_code:
int hash(char* key) {
    int i=0;
    if (key == ((void *)0))
        return -1;
    while (*key != '\0') {
        i+=(int) *key;
        key++;
    }
    i=i % 10;
    return i;
}

canonical_original_code:
int hash(char *key) {
    int i = 0;
    if (key == ((void *)0))
        return -1;
    while (*key != '\0') {
        i+=(int) *key;
        key++;
    }
    i=i % 10;
    return i;
}

variable_types:
key
char *

variable_types: i int


get

decompiled_code:
__int64 __fastcall get(__int64 a1, _BYTE *a2)
{
__int64 v2; // r12
pthread_rwlock_t *v3; // r13
__int64 v4; // r12
__int64 result; // rax

if ( a1 && a2 )
{
v2 = (int)hash(a2);
v3 = (pthread_rwlock_t *)(a1 + 56 * v2 + 16);
pthread_rwlock_rdlock(v3);
v4 = lst_get(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v2), a2);
pthread_rwlock_unlock(v3);
result = v4;
}
else
{
puts("error: received NULL pointer");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, _BYTE *a2)
{
__int64 v2; // r12
pthread_rwlock_t *v3; // r13
__int64 v4; // r12
__int64 result; // rax

if ( a1 && a2 )
{
v2 = (int)func1(a2);
v3 = (pthread_rwlock_t *)(a1 + 56 * v2 + 16);
pthread_rwlock_rdlock(v3);
v4 = lst_get(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v2), a2);
pthread_rwlock_unlock(v3);
result = v4;
}
else
{
puts("error: received NULL pointer");
result = 0LL;
}
return result;
}

original_code:
lst_ret_t *get(hashtable *h, char *key)
{
    int bucket;
    lst_ret_t* value = ((void *)0);
    if((h != ((void *)0)) && (key != ((void *)0))) {
        bucket = hash(key);
        pthread_rwlock_rdlock(&h->rwlock[bucket]);
        value = lst_get(h->lists[bucket], key);
        pthread_rwlock_unlock(&h->rwlock[bucket]);
    }
    else {
        puts("error: received NULL pointer");
    }
    return value;
}

canonical_original_code:
struct lst_ret_t *get(struct hashtable *h, char *key)
{
    int bucket;
    struct lst_ret_t *value = ((void *)0);
    if((h != ((void *)0)) && (key != ((void *)0))) {
        bucket = hash(key);
        pthread_rwlock_rdlock(&h->rwlock[bucket]);
        value = lst_get(h->lists[bucket], key);
        pthread_rwlock_unlock(&h->rwlock[bucket]);
    }
    else {
        puts("error: received NULL pointer");
    }
    return value;
}

variable_types: bucket int

return_type: struct lst_ret_t { char *value; int position; } *

user_defined_types: struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; }


decompiled_code:
__int64 __fastcall ht_remove(__int64 a1, _BYTE *a2)
{
__int64 v2; // r12
pthread_rwlock_t *v3; // r13
__int64 v4; // r12
__int64 result; // rax

if ( a1 && a2 )
{
v2 = (int)hash(a2);
v3 = (pthread_rwlock_t *)(a1 + 56 * v2 + 16);
pthread_rwlock_wrlock(v3);
v4 = lst_remove(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v2), a2);
pthread_rwlock_unlock(v3);
result = v4;
}
else
{
puts("error: received NULL pointer");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _BYTE *a2)
{
__int64 v2; // r12
pthread_rwlock_t *v3; // r13
__int64 v4; // r12
__int64 result; // rax

if ( a1 && a2 )
{
v2 = (int)func1(a2);
v3 = (pthread_rwlock_t *)(a1 + 56 * v2 + 16);
pthread_rwlock_wrlock(v3);
v4 = lst_remove(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v2), a2);
pthread_rwlock_unlock(v3);
result = v4;
}
else
{
puts("error: received NULL pointer");
result = 0LL;
}
return result;
}

original_code:
lst_ret_t *ht_remove(hashtable *h, char *key)
{
    int bucket;
    lst_ret_t *ret = ((void *)0);
    if((h != ((void *)0)) && (key != ((void *)0))) {
        bucket = hash(key);
        pthread_rwlock_wrlock(&h->rwlock[bucket]);
        ret = lst_remove(h->lists[bucket], key);
        pthread_rwlock_unlock(&h->rwlock[bucket]);
    }
    else {
        puts("error: received NULL pointer");
    }
    return ret;
}

canonical_original_code:
struct lst_ret_t *ht_remove(struct hashtable *h, char *key)
{
    int bucket;
    struct lst_ret_t *ret = ((void *)0);
    if((h != ((void *)0)) && (key != ((void *)0))) {
        bucket = hash(key);
        pthread_rwlock_wrlock(&h->rwlock[bucket]);
        ret = lst_remove(h->lists[bucket], key);
        pthread_rwlock_unlock(&h->rwlock[bucket]);
    }
    else {
        puts("error: received NULL pointer");
    }
    return ret;
}

user_defined_types: struct lst_ret_t { char *value; int position; }


add

decompiled_code:
__int64 __fastcall add(__int64 a1, _BYTE *a2, __int64 a3, unsigned int a4)
{
__int64 v6; // r12
pthread_rwlock_t *v7; // r15
__int64 v8; // r12

if ( a1 && a2 )
{
v6 = (int)hash(a2);
v7 = (pthread_rwlock_t *)(a1 + 56 * v6 + 16);
pthread_rwlock_wrlock(v7);
v8 = lst_insert(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v6), a2, a3, a4);
pthread_rwlock_unlock(v7);
}
else
{
v8 = 0LL;
puts("error: received NULL pointer");
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _BYTE *a2, __int64 a3, unsigned int a4)
{
__int64 v6; // r12
pthread_rwlock_t *v7; // r15
__int64 v8; // r12

if ( a1 && a2 )
{
v6 = (int)func1(a2);
v7 = (pthread_rwlock_t *)(a1 + 56 * v6 + 16);
pthread_rwlock_wrlock(v7);
v8 = lst_insert(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v6), a2, a3, a4);
pthread_rwlock_unlock(v7);
}
else
{
v8 = 0LL;
puts("error: received NULL pointer");
}
return v8;
}

original_code:
lst_ret_t *add(hashtable *h, char *key, char *value, int file_position)
{
    int bucket;
    lst_ret_t *ret = ((void *)0);
    if((h != ((void *)0)) && (key != ((void *)0))) {
        bucket = hash(key);
        pthread_rwlock_wrlock(&h->rwlock[bucket]);
        ret = lst_insert(h->lists[bucket], key, value, file_position);
        pthread_rwlock_unlock(&h->rwlock[bucket]);
    }
    else {
        puts("error: received NULL pointer");
    }
    return ret;
}

canonical_original_code:
struct lst_ret_t *add(struct hashtable *h, char *key, char *value, int file_position)
{
    int bucket;
    struct lst_ret_t *ret = ((void *)0);
    if((h != ((void *)0)) && (key != ((void *)0))) {
        bucket = hash(key);
        pthread_rwlock_wrlock(&h->rwlock[bucket]);
        ret = lst_insert(h->lists[bucket], key, value, file_position);
        pthread_rwlock_unlock(&h->rwlock[bucket]);
    }
    else {
        puts("error: received NULL pointer");
    }
    return ret;
}

variable_types:
value
char *


getAllKeys

decompiled_code:
char *__fastcall getAllKeys(__int64 a1, _DWORD *a2)
{
pthread_rwlock_t *v3; // rbp
__int64 v4; // rbx
__int64 v5; // rdi
int v6; // eax
pthread_rwlock_t *v7; // rdi
size_t v8; // rdi
pthread_rwlock_t *v9; // rbp
__int64 v10; // r14
int i; // er12
__int64 v12; // r15
char *v13; // rbx
char *v14; // rdi
int v16; // [rsp+Ch] [rbp-4Ch]
char *v17; // [rsp+10h] [rbp-48h]

if ( !a1 )
return 0LL;
if ( *(int *)a1 <= 0 )
{
v16 = 0;
v8 = 0LL;
}
else
{
v16 = 0;
v3 = (pthread_rwlock_t *)(a1 + 16);
v4 = 0LL;
do
{
pthread_rwlock_rdlock(v3);
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v4++);
v6 = lst_size(v5);
v7 = v3;
v16 += v6;
++v3;
pthread_rwlock_unlock(v7);
}
while ( *(_DWORD *)a1 > (int)v4 );
v8 = v16;
}
v17 = (char *)calloc(v8, 0x28uLL);
if ( !v17 )
{
fwrite("Dynamic memory allocation failed\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
v9 = (pthread_rwlock_t *)(a1 + 16);
v10 = 0LL;
for ( i = 0; *(_DWORD *)a1 > (int)v10; ++v9 )
{
v12 = **(_QWORD **)(*(_QWORD *)(a1 + 8) + 8 * v10);
if ( v12 )
{
v13 = &v17[40 * i];
do
{
++i;
pthread_rwlock_rdlock(v9);
strncpy(v13, *(const char **)v12, 0x14uLL);
v14 = v13 + 20;
v13 += 40;
strncpy(v14, (const char *)(*(_QWORD *)v12 + 20LL), 0x14uLL);
pthread_rwlock_unlock(v9);
v12 = *(_QWORD *)(v12 + 16);
}
while ( v12 );
}
++v10;
}
*a2 = v16;
return v17;
}

canonical_decompiled_code:
char *__fastcall func5(__int64 a1, _DWORD *a2)
{
pthread_rwlock_t *v3; // rbp
__int64 v4; // rbx
__int64 v5; // rdi
int v6; // eax
pthread_rwlock_t *v7; // rdi
size_t v8; // rdi
pthread_rwlock_t *v9; // rbp
__int64 v10; // r14
int i; // er12
__int64 v12; // r15
char *v13; // rbx
char *v14; // rdi
int v16; // [rsp+Ch] [rbp-4Ch]
char *v17; // [rsp+10h] [rbp-48h]

if ( !a1 )
return 0LL;
if ( *(int *)a1 <= 0 )
{
v16 = 0;
v8 = 0LL;
}
else
{
v16 = 0;
v3 = (pthread_rwlock_t *)(a1 + 16);
v4 = 0LL;
do
{
pthread_rwlock_rdlock(v3);
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v4++);
v6 = lst_size(v5);
v7 = v3;
v16 += v6;
++v3;
pthread_rwlock_unlock(v7);
}
while ( *(_DWORD *)a1 > (int)v4 );
v8 = v16;
}
v17 = (char *)calloc(v8, 0x28uLL);
if ( !v17 )
{
fwrite("Dynamic memory allocation failed\n", 1uLL, 0x21uLL, stderr);
exit(1);
}
v9 = (pthread_rwlock_t *)(a1 + 16);
v10 = 0LL;
for ( i = 0; *(_DWORD *)a1 > (int)v10; ++v9 )
{
v12 = **(_QWORD **)(*(_QWORD *)(a1 + 8) + 8 * v10);
if ( v12 )
{
v13 = &v17[40 * i];
do
{
++i;
pthread_rwlock_rdlock(v9);
strncpy(v13, *(const char **)v12, 0x14uLL);
v14 = v13 + 20;
v13 += 40;
strncpy(v14, (const char *)(*(_QWORD *)v12 + 20LL), 0x14uLL);
pthread_rwlock_unlock(v9);
v12 = *(_QWORD *)(v12 + 16);
}
while ( v12 );
}
++v10;
}
*a2 = v16;
return v17;
}

original_code:
KV_t* getAllKeys(hashtable *h, int* dim)
{
    if(h != ((void *)0)) {
        KV_t* pairs = ((void *)0);
        lst_iitem_t *lst_it = ((void *)0);
        int counter = 0, ix, jx;
        for(ix = 0; ix < h->size; ix += 1) {
            pthread_rwlock_rdlock(&h->rwlock[ix]);
            counter += lst_size(h->lists[ix]);
            pthread_rwlock_unlock(&h->rwlock[ix]);
        }
        pairs = calloc((size_t) counter, sizeof(KV_t));
        if(pairs==((void *)0)) {
            fprintf(stderr, "Dynamic memory allocation failed\n");
            exit(1);
        }
        for(ix = 0, jx = 0; ix < h->size; ix += 1) {
            for(lst_it = h->lists[ix]->first; lst_it != ((void *)0); ++jx, lst_it = lst_it->next) {
                pthread_rwlock_rdlock(&h->rwlock[ix]);
                strncpy(pairs[jx].key, lst_it->item->key, 20);
                strncpy(pairs[jx].value, lst_it->item->value, 20);
                pthread_rwlock_unlock(&h->rwlock[ix]);
            }
        }
        *dim = counter;
        return pairs;
    }
    return ((void *)0);
}

canonical_original_code:
struct KV_t *getAllKeys(struct hashtable *h, int *dim)
{
    if(h != ((void *)0)) {
        struct KV_t *pairs = ((void *)0);
        struct lst_iitem *lst_it = ((void *)0);
        int counter = 0; int ix; int jx;
        for(ix = 0; ix < h->size; ix += 1) {
            pthread_rwlock_rdlock(&h->rwlock[ix]);
            counter += lst_size(h->lists[ix]);
            pthread_rwlock_unlock(&h->rwlock[ix]);
        }
        pairs = calloc((unsigned long) counter, sizeof(KV_t));
        if(pairs==((void *)0)) {
            fprintf(stderr, "Dynamic memory allocation failed\n");
            exit(1);
        }
        for(ix = 0, jx = 0; ix < h->size; ix += 1) {
            for(lst_it = h->lists[ix]->first; lst_it != ((void *)0); ++jx, lst_it = lst_it->next) {
                pthread_rwlock_rdlock(&h->rwlock[ix]);
                strncpy(pairs[jx].key, lst_it->item->key, 20);
                strncpy(pairs[jx].value, lst_it->item->value, 20);
                pthread_rwlock_unlock(&h->rwlock[ix]);
            }
        }
        *dim = counter;
        return pairs;
    }
    return ((void *)0);
}

variable_types: struct hashtable { int size; struct list_t { struct lst_iitem { struct KV_t *item; int file_position; struct lst_iitem *next; } *first; int size; } **lists; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } rwlock[10]; } *

variable_types: counter int

variable_types: ix int

variable_types: jx int

return_type: struct KV_t { char key[20]; char value[20]; } *


delay

decompiled_code:
unsigned int delay()
{
return sleep(1u);
}

canonical_decompiled_code:
unsigned int func0()
{
return sleep(1u);
}

original_code:
void delay() {
  sleep(1);
}

canonical_original_code:
void delay() {
  sleep(1);
}


crearColaP

repo: zeroscarface/Simulacion-Con-Sockets

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

result = malloc(0x10uLL);
*(_QWORD *)result = 0LL;
result[2] = 0;
return result;
}

original_code:
ColaP* crearColaP(){
 ColaP* cp;
 if((cp = (ColaP*)malloc(sizeof(ColaP))) < 0){
  perror("error al pedir memoria");
  exit(1);
 }
 cp->pri = ((void *)0);
 cp->tam = 0;
 return cp;
}

canonical_original_code:
struct colaP *crearColaP(){
 struct colaP *cp;
 if((cp = (struct colaP *)malloc(sizeof(ColaP))) < 0){
  perror("error al pedir memoria");
  exit(1);
 }
 cp->pri = ((void *)0);
 cp->tam = 0;
 return cp;
}

variable_types: struct colaP { struct nodo { struct nodo *sig; struct nodo *ant; int prior; char *info; } *pri; int tam; } *

user_defined_types: struct colaP { struct nodo *pri; int tam; }


agregarElemP

decompiled_code:
unsigned __int64 __fastcall agregarElemP(__int64 a1, __int64 a2, int a3)
{
unsigned __int64 result; // rax
int v6; // ecx
_QWORD *v7; // rsi
int v8; // edx
_QWORD *v9; // rdi
_QWORD *v10; // rdx
_QWORD *v11; // rdx

result = (unsigned __int64)crearNodo(a2, a3);
v6 = *(_DWORD *)(a1 + 8);
v7 = (_QWORD *)result;
if ( !v6 )
{
*(_QWORD *)a1 = result;
*(_DWORD *)(a1 + 8) = 1;
return result;
}
result = *(_QWORD *)a1;
v8 = 0;
v9 = *(_QWORD **)a1;
if ( v6 > 0 )
{
while ( *(_DWORD *)(result + 16) < a3 )
{
++v8;
result = *(_QWORD *)result;
if ( v6 == v8 )
{
if ( result )
goto LABEL_6;
result = 0LL;
while ( 1 )
{
v11 = v9;
v9 = (_QWORD *)*v9;
if ( !v9 )
goto LABEL_14;
result = (unsigned int)(result + 1);
if ( v6 == (_DWORD)result )
{
v11 = v9;
goto LABEL_14;
}
}
}
}
goto LABEL_6;
}
if ( result )
{
LABEL_6:
v10 = *(_QWORD **)(result + 8);
if ( v10 )
*v10 = v7;
else
*(_QWORD *)a1 = v7;
*v7 = result;
v7[1] = v10;
*(_QWORD *)(result + 8) = v7;
*(_DWORD *)(a1 + 8) = v6 + 1;
return result;
}
v11 = 0LL;
LABEL_14:
*v11 = v7;
v7[1] = v11;
*(_DWORD *)(a1 + 8) = v6 + 1;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(__int64 a1, __int64 a2, int a3)
{
unsigned __int64 result; // rax
int v6; // ecx
_QWORD *v7; // rsi
int v8; // edx
_QWORD *v9; // rdi
_QWORD *v10; // rdx
_QWORD *v11; // rdx

result = (unsigned __int64)crearNodo(a2, a3);
v6 = *(_DWORD *)(a1 + 8);
v7 = (_QWORD *)result;
if ( !v6 )
{
*(_QWORD *)a1 = result;
*(_DWORD *)(a1 + 8) = 1;
return result;
}
result = *(_QWORD *)a1;
v8 = 0;
v9 = *(_QWORD **)a1;
if ( v6 > 0 )
{
while ( *(_DWORD *)(result + 16) < a3 )
{
++v8;
result = *(_QWORD *)result;
if ( v6 == v8 )
{
if ( result )
goto LABEL_6;
result = 0LL;
while ( 1 )
{
v11 = v9;
v9 = (_QWORD *)*v9;
if ( !v9 )
goto LABEL_14;
result = (unsigned int)(result + 1);
if ( v6 == (_DWORD)result )
{
v11 = v9;
goto LABEL_14;
}
}
}
}
goto LABEL_6;
}
if ( result )
{
LABEL_6:
v10 = *(_QWORD **)(result + 8);
if ( v10 )
*v10 = v7;
else
*(_QWORD *)a1 = v7;
*v7 = result;
v7[1] = v10;
*(_QWORD *)(result + 8) = v7;
*(_DWORD *)(a1 + 8) = v6 + 1;
return result;
}
v11 = 0LL;
LABEL_14:
*v11 = v7;
v7[1] = v11;
*(_DWORD *)(a1 + 8) = v6 + 1;
return result;
}

original_code:
void agregarElemP(ColaP* cp, char* info, int prior){
 Nodo* np = crearNodo(info,prior);
 if (cp->tam == 0){
  cp->pri = np;
  cp->tam = 1;
  return;
 }
 Nodo* aux = cp->pri;
 int i = 0;
 for (i = 0; i < cp->tam && aux->prior < prior; i++)
  aux = aux->sig;
 if (aux == ((void *)0)){
  aux = cp->pri;
  for (i=0; i < cp->tam && aux->sig != ((void *)0); i++)
   aux = aux->sig;
  aux->sig = np;
  np->ant = aux;
  cp->tam += 1;
  return;
 }
 if (aux->ant == ((void *)0)){
  cp->pri = np;
 } else {
  aux->ant->sig = np;
 }
 np->sig = aux;
 np->ant = aux->ant;
 aux->ant = np;
 cp->tam += 1;
}

canonical_original_code:
void agregarElemP(struct colaP *cp, char *info, int prior){
 struct nodo *np = crearNodo(info,prior);
 if (cp->tam == 0){
  cp->pri = np;
  cp->tam = 1;
  return;
 }
 struct nodo *aux = cp->pri;
 int i = 0;
 for (i = 0; i < cp->tam && aux->prior < prior; i++)
  aux = aux->sig;
 if (aux == ((void *)0)){
  aux = cp->pri;
  for (i=0; i < cp->tam && aux->sig != ((void *)0); i++)
   aux = aux->sig;
  aux->sig = np;
  np->ant = aux;
  cp->tam += 1;
  return;
 }
 if (aux->ant == ((void *)0)){
  cp->pri = np;
 } else {
  aux->ant->sig = np;
 }
 np->sig = aux;
 np->ant = aux->ant;
 aux->ant = np;
 cp->tam += 1;
}

variable_types:
info
char *

variable_types: prior int

variable_types: struct nodo { struct nodo *sig; struct nodo *ant; int prior; char *info; } *


darElemP

decompiled_code:
__int64 __fastcall darElemP(__int64 a1, int a2)
{
_QWORD *v2; // rdx
int i; // eax

v2 = *(_QWORD **)a1;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
if ( *(_DWORD *)(a1 + 8) <= i )
break;
v2 = (_QWORD *)*v2;
}
}
return v2[3];
}

original_code:
char* darElemP(ColaP* cp, int pos){
 Nodo* aux = cp->pri;
 int i;
 for (i=0; i < pos && i < cp->tam; i++)
  aux = aux->sig;
 return aux->info;
}

canonical_original_code:
char *darElemP(struct colaP *cp, int pos){
 struct nodo *aux = cp->pri;
 int i;
 for (i=0; i < pos && i < cp->tam; i++)
  aux = aux->sig;
 return aux->info;
}

variable_types: pos int

user_defined_types: struct nodo { struct nodo *sig; struct nodo *ant; int prior; char *info; }


sboxPrint

decompiled_code:
int __fastcall sboxPrint(__int64 a1)
{
unsigned __int8 *v1; // rbp
unsigned __int8 *v2; // rbx
__int64 v3; // rsi
int result; // eax

v1 = (unsigned __int8 *)(a1 + 16);
do
{
v2 = v1 - 16;
do
{
v3 = *v2++;
printf("0x%02x, ", v3);
}
while ( v2 != v1 );
v1 = v2 + 16;
putchar(10);
result = putchar(10);
}
while ( v2 + 16 != (unsigned __int8 *)(a1 + 272) );
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
unsigned __int8 *v1; // rbp
unsigned __int8 *v2; // rbx
__int64 v3; // rsi
int result; // eax

v1 = (unsigned __int8 *)(a1 + 16);
do
{
v2 = v1 - 16;
do
{
v3 = *v2++;
printf("0x%02x, ", v3);
}
while ( v2 != v1 );
v1 = v2 + 16;
putchar(10);
result = putchar(10);
}
while ( v2 + 16 != (unsigned __int8 *)(a1 + 272) );
return result;
}

original_code:
void sboxPrint(byte_t * s)
{
 int i,j;
 for(i=0;i<16;i++){
  for(j=0;j<16;j++)
   printf("0x%02x, ",s[i*16+j]);
  putchar('\n');
  putchar('\n');
 }
}

canonical_original_code:
void sboxPrint(unsigned char *s)
{
 int i; int j;
 for(i=0;i<16;i++){
  for(j=0;j<16;j++)
   printf("0x%02x, ",s[i*16+j]);
  putchar('\n');
  putchar('\n');
 }
}

variable_types:
s
unsigned char *

variable_types: j int


decompiled_code:
int __fastcall print4x(unsigned __int8 *a1)
{
unsigned __int8 *v1; // rbx
__int64 v2; // rsi

v1 = a1;
do
{
v2 = *v1++;
printf("%02x", v2);
}
while ( v1 != a1 + 16 );
putchar(10);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func1(unsigned __int8 *a1)
{
unsigned __int8 *v1; // rbx
__int64 v2; // rsi

v1 = a1;
do
{
v2 = *v1++;
printf("%02x", v2);
}
while ( v1 != a1 + 16 );
putchar(10);
return putchar(10);
}

original_code:
void print4x(byte_t * box)
{
 int i;
 for(i=0;i<16;i++)
 {
  printf("%02x",box[i]);
 }
 putchar('\n');
 putchar('\n');
}

canonical_original_code:
void print4x(unsigned char *box)
{
 int i;
 for(i=0;i<16;i++)
 {
  printf("%02x",box[i]);
 }
 putchar('\n');
 putchar('\n');
}

variable_types:
box
unsigned char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int8 *v3; // rbp
unsigned __int8 *v4; // r12
unsigned int v6; // [rsp+Ch] [rbp-3Ch] BYREF
__m128i v7; // [rsp+10h] [rbp-38h] BYREF
__int128 v8[2]; // [rsp+20h] [rbp-28h] BYREF

v7 = _mm_load_si128((const __m128i *)&LC3);
v8[0] = (__int128)_mm_load_si128((const __m128i *)&LC4);
v3 = (unsigned __int8 *)calloc(0x11uLL, 1uLL);
Aes128cbc_Pkcs7_Enc(&v7, 16LL, v8, 16LL, v3, &IV);
if ( v3 )
print4x(v3);
v6 = 0;
v4 = (unsigned __int8 *)calloc(0x11uLL, 1uLL);
Aes128cbc_Pkcs7_Dec(v3, 16LL, v8, 16LL, v4, &v6, &IV);
print4x(v4);
printf("ptlen=%d\n", v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int8 *v3; // rbp
unsigned __int8 *v4; // r12
unsigned int v6; // [rsp+Ch] [rbp-3Ch] BYREF
__m128i v7; // [rsp+10h] [rbp-38h] BYREF
__int128 v8[2]; // [rsp+20h] [rbp-28h] BYREF

v7 = _mm_load_si128((const __m128i *)&LC3);
v8[0] = (__int128)_mm_load_si128((const __m128i *)&LC4);
v3 = (unsigned __int8 *)calloc(0x11uLL, 1uLL);
Aes128cbc_Pkcs7_Enc(&v7, 16LL, v8, 16LL, v3, &IV);
if ( v3 )
func1(v3);
v6 = 0;
v4 = (unsigned __int8 *)calloc(0x11uLL, 1uLL);
Aes128cbc_Pkcs7_Dec(v3, 16LL, v8, 16LL, v4, &v6, &IV);
func1(v4);
printf("ptlen=%d\n", v6);
return 0;
}

original_code:
int main(void)
{
 int i;
 byte_t input[16]={0x32,0x43,0xf6,0xa8,0x88,0x5a,0x30,0x8d,0x31,0x31,0x98,0xa2,0xe0,0x37,0x07,0x34};
 byte_t k[16]={0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c};
 byte_t * S=calloc(16+1,sizeof(byte_t));
 Aes128cbc_Pkcs7_Enc(input,sizeof(input),k,16,S,IV);
 if(S){
  print4x(S);
 }
 byte_t * plain=calloc(16+1,sizeof(byte_t));
 int ptlen=0;
 Aes128cbc_Pkcs7_Dec(S,16,k,16,plain,&ptlen,IV);
 print4x(plain);
 printf("ptlen=%d\n",ptlen);
 return 0;
}

canonical_original_code:
int main()
{
 int i;
 unsigned char input[16] = {0x32,0x43,0xf6,0xa8,0x88,0x5a,0x30,0x8d,0x31,0x31,0x98,0xa2,0xe0,0x37,0x07,0x34};
 unsigned char k[16] = {0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c};
 unsigned char *S = calloc(16+1,sizeof(byte_t));
 Aes128cbc_Pkcs7_Enc(input,sizeof(input),k,16,S,IV);
 if(S){
  print4x(S);
 }
 unsigned char *plain = calloc(16+1,sizeof(byte_t));
 int ptlen = 0;
 Aes128cbc_Pkcs7_Dec(S,16,k,16,plain,&ptlen,IV);
 print4x(plain);
 printf("ptlen=%d\n",ptlen);
 return 0;
}

variable_types:
input
unsigned char [16]

variable_types:
k
unsigned char [16]

variable_types:
S
unsigned char *

variable_types:
plain
unsigned char *

variable_types: ptlen int


decompiled_code:
__int64 __fastcall state_mix_columns(unsigned __int8 *a1)
{
unsigned __int8 *v1; // r13
unsigned __int8 *v2; // r12
__int64 v3; // rbp
__int64 i; // rdi
int v5; // ebx
int v6; // ebx
int v7; // ebx
__int64 result; // rax
char v9[3]; // [rsp+Ch] [rbp-2Ch]
unsigned __int8 v10; // [rsp+Fh] [rbp-29h]

v1 = a1 + 4;
v2 = a1;
do
{
v3 = 0LL;
for ( i = 2LL; ; i = (unsigned __int8)MixC[4 * v3] )
{
v5 = GF2sup8_mul8(i, *v2);
v6 = GF2sup8_mul8((unsigned __int8)byte_611[4 * v3], v2[4]) ^ v5;
v7 = GF2sup8_mul8((unsigned __int8)byte_612[4 * v3], v2[8]) ^ v6;
v9[v3] = GF2sup8_mul8((unsigned __int8)byte_613[4 * v3], v2[12]) ^ v7;
if ( ++v3 == 4 )
break;
}
*v2++ = v9[0];
v2[3] = v9[1];
v2[7] = v9[2];
result = v10;
v2[11] = v10;
}
while ( v2 != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
unsigned __int8 *v1; // r13
unsigned __int8 *v2; // r12
__int64 v3; // rbp
__int64 i; // rdi
int v5; // ebx
int v6; // ebx
int v7; // ebx
__int64 result; // rax
char v9[3]; // [rsp+Ch] [rbp-2Ch]
unsigned __int8 v10; // [rsp+Fh] [rbp-29h]

v1 = a1 + 4;
v2 = a1;
do
{
v3 = 0LL;
for ( i = 2LL; ; i = (unsigned __int8)MixC[4 * v3] )
{
v5 = GF2sup8_mul8(i, *v2);
v6 = GF2sup8_mul8((unsigned __int8)byte_611[4 * v3], v2[4]) ^ v5;
v7 = GF2sup8_mul8((unsigned __int8)byte_612[4 * v3], v2[8]) ^ v6;
v9[v3] = GF2sup8_mul8((unsigned __int8)byte_613[4 * v3], v2[12]) ^ v7;
if ( ++v3 == 4 )
break;
}
*v2++ = v9[0];
v2[3] = v9[1];
v2[7] = v9[2];
result = v10;
v2[11] = v10;
}
while ( v2 != v1 );
return result;
}

original_code:
static void state_mix_columns(byte_t * state)
{
 int i,j,ret;
 byte_t tr[4];
 for(i=0;i<4;i++){
  for(j=0;j<4;j++){
   tr[j]=GF2sup8_mul8(MixC[j][0],state[0*4+i])^
    GF2sup8_mul8(MixC[j][1],state[1*4+i])^
    GF2sup8_mul8(MixC[j][2],state[2*4+i])^
    GF2sup8_mul8(MixC[j][3],state[3*4+i]);
  }
  state[0*4+i]=tr[0];
  state[1*4+i]=tr[1];
  state[2*4+i]=tr[2];
  state[3*4+i]=tr[3];
 }
}

canonical_original_code:
static void state_mix_columns(unsigned char *state)
{
 int i; int j; int ret;
 unsigned char tr[4];
 for(i=0;i<4;i++){
  for(j=0;j<4;j++){
   tr[j]=GF2sup8_mul8(MixC[j][0],state[0*4+i])^
    GF2sup8_mul8(MixC[j][1],state[1*4+i])^
    GF2sup8_mul8(MixC[j][2],state[2*4+i])^
    GF2sup8_mul8(MixC[j][3],state[3*4+i]);
  }
  state[0*4+i]=tr[0];
  state[1*4+i]=tr[1];
  state[2*4+i]=tr[2];
  state[3*4+i]=tr[3];
 }
}

variable_types:
state
unsigned char *

variable_types: i int

variable_types: ret int

variable_types:
tr
unsigned char [4]


decompiled_code:
__int64 __fastcall state_shift_row_left(int *a1, unsigned int a2)
{
unsigned int v2; // er12
__int64 result; // rax
unsigned int v4; // ebp
int v5; // eax
char v6; // cl
int v7; // er8
char v8; // al
unsigned int v9; // edx
unsigned int v10; // ebp

if ( a2 > 3 || !a1 )
return (unsigned int)-1;
v2 = a2;
if ( !a2 )
return v2;
v4 = *a1;
v5 = is_big_endian();
v6 = 8 * a2;
v7 = v5;
v8 = 8 * (4 - a2);
if ( v7 )
{
v9 = v4 << v6;
v10 = v4 >> v8;
}
else
{
v9 = v4 >> v6;
v10 = v4 << v8;
}
result = 0LL;
*a1 = v9 | v10;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, unsigned int a2)
{
unsigned int v2; // er12
__int64 result; // rax
unsigned int v4; // ebp
int v5; // eax
char v6; // cl
int v7; // er8
char v8; // al
unsigned int v9; // edx
unsigned int v10; // ebp

if ( a2 > 3 || !a1 )
return (unsigned int)-1;
v2 = a2;
if ( !a2 )
return v2;
v4 = *a1;
v5 = is_big_endian();
v6 = 8 * a2;
v7 = v5;
v8 = 8 * (4 - a2);
if ( v7 )
{
v9 = v4 << v6;
v10 = v4 >> v8;
}
else
{
v9 = v4 >> v6;
v10 = v4 << v8;
}
result = 0LL;
*a1 = v9 | v10;
return result;
}

original_code:
static int state_shift_row_left(byte_t * state,int n)
{
 if(n>3 || n<0 || !state)
  return -1;
 if(n==0) return 0;
 word_t * swt = (word_t*)state;
 word_t sw=*swt;
 if(is_big_endian()){
  *swt=((sw) << (n*8))|((sw) >> ((32) - (n*8)));
 }else{
  *swt=((sw) >> (n*8))|((sw) << ((32) - (n*8)));
 }
 return 0;
}

canonical_original_code:
static int state_shift_row_left(unsigned char *state, int n)
{
 if(n>3 || n<0 || !state)
  return -1;
 if(n==0) return 0;
 unsigned int *swt = (word_t*)state;
 unsigned int sw = *swt;
 if(is_big_endian()){
  *swt=((sw) << (n*8))|((sw) >> ((32) - (n*8)));
 }else{
  *swt=((sw) >> (n*8))|((sw) << ((32) - (n*8)));
 }
 return 0;
}

variable_types: n int

variable_types:
swt
unsigned int *

variable_types:
sw
unsigned int


decompiled_code:
__int64 __fastcall state_bvary_lshift(__int64 a1)
{
unsigned int v1; // er12
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // rdi
__int64 result; // rax

v1 = 0;
v2 = a1 + 4;
while ( 1 )
{
v3 = v2 - 4;
do
{
v4 = v3++;
subbyte(v4, &sbox);
}
while ( v3 != v2 );
result = state_shift_row_left((int *)(v2 - 4), v1);
if ( (int)result < 0 )
break;
++v1;
v2 = v3 + 4;
if ( v1 == 4 )
return 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned int v1; // er12
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // rdi
__int64 result; // rax

v1 = 0;
v2 = a1 + 4;
while ( 1 )
{
v3 = v2 - 4;
do
{
v4 = v3++;
subbyte(v4, &sbox);
}
while ( v3 != v2 );
result = func1((int *)(v2 - 4), v1);
if ( (int)result < 0 )
break;
++v1;
v2 = v3 + 4;
if ( v1 == 4 )
return 0LL;
}
return result;
}

original_code:
static int state_bvary_lshift(byte_t * state)
{
 int i,j,ret;
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   subbyte(&state[i*4+j],sbox);
  }
  ret = state_shift_row_left(&state[i*4],i);
  if(ret < 0)
   return ret;
 }
 return 0;
}

canonical_original_code:
static int state_bvary_lshift(unsigned char *state)
{
 int i; int j; int ret;
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   subbyte(&state[i*4+j],sbox);
  }
  ret = state_shift_row_left(&state[i*4],i);
  if(ret < 0)
   return ret;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall Aes128_Enc(__int64 a1, int a2, __int64 a3, int a4, unsigned __int8 *a5)
{
unsigned __int64 v7; // rbx

if ( !a1 || a2 != 16 )
return 0xFFFFFFFFLL;
if ( a4 == 16 && a3 != 0 && a5 )
{
v7 = (unsigned int)((_DWORD)&W + 16);
state_put(a1, a5);
key_expansion(a3, &W);
state_add_rou_key(a5, &W);
while ( (int)state_bvary_lshift((__int64)a5) >= 0 )
{
state_mix_columns(a5);
if ( (int)state_add_rou_key(a5, v7) < 0 )
break;
v7 += 16LL;
if ( (_UNKNOWN *)v7 == (_UNKNOWN *)((char *)&W + 160) )
{
if ( (int)state_bvary_lshift((__int64)a5) < 0 )
return 0xFFFFFFFFLL;
return (unsigned int)((int)state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160)) >> 31);
}
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, __int64 a3, int a4, unsigned __int8 *a5)
{
unsigned __int64 v7; // rbx

if ( !a1 || a2 != 16 )
return 0xFFFFFFFFLL;
if ( a4 == 16 && a3 != 0 && a5 )
{
v7 = (unsigned int)((_DWORD)&W + 16);
state_put(a1, a5);
key_expansion(a3, &W);
state_add_rou_key(a5, &W);
while ( (int)func2((__int64)a5) >= 0 )
{
func0(a5);
if ( (int)state_add_rou_key(a5, v7) < 0 )
break;
v7 += 16LL;
if ( (_UNKNOWN *)v7 == (_UNKNOWN *)((char *)&W + 160) )
{
if ( (int)func2((__int64)a5) < 0 )
return 0xFFFFFFFFLL;
return (unsigned int)((int)state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160)) >> 31);
}
}
}
return 0xFFFFFFFFLL;
}

original_code:
int Aes128_Enc(byte_t * input,int inlen,byte_t * key,int keylen,byte_t * output)
{
 if(!input || inlen!=16 || !key || keylen!=16 || !output)
  return -1;
 int i=0,ret=0;
 state_put(input,output);
 key_expansion(key,W);
 i=0;
 state_add_rou_key(output,W+i*4);
 i++;
 while(i<10){
  if(state_bvary_lshift(output)<0)
   return -1;
  state_mix_columns(output);
  if(state_add_rou_key(output,W+i*4)<0)
   return -1;
  i++;
 }
 if(state_bvary_lshift(output)<0)
  return -1;
 if(state_add_rou_key(output,W+i*4)<0)
  return -1;
 return 0;
}

canonical_original_code:
int Aes128_Enc(unsigned char *input, int inlen, unsigned char *key, int keylen, unsigned char *output)
{
 if(!input || inlen!=16 || !key || keylen!=16 || !output)
  return -1;
 int i = 0; int ret = 0;
 state_put(input,output);
 key_expansion(key,W);
 i=0;
 state_add_rou_key(output,W+i*4);
 i++;
 while(i<10){
  if(state_bvary_lshift(output)<0)
   return -1;
  state_mix_columns(output);
  if(state_add_rou_key(output,W+i*4)<0)
   return -1;
  i++;
 }
 if(state_bvary_lshift(output)<0)
  return -1;
 if(state_add_rou_key(output,W+i*4)<0)
  return -1;
 return 0;
}

variable_types:
input
unsigned char *

variable_types: inlen int

variable_types:
key
unsigned char *

variable_types: keylen int

variable_types:
output
unsigned char *


decompiled_code:
__int64 __fastcall Aes128cbc_Enc(unsigned __int8 *a1, int a2, __int64 a3, int a4)
{
unsigned __int64 v5; // rbx
unsigned __int64 v6; // rsi

if ( !a1 || a2 != 16 )
return 0xFFFFFFFFLL;
if ( a3 && a4 == 16 )
{
v5 = (unsigned int)((_DWORD)&W + 16);
((void (*)(void))reverse4x)();
key_expansion(a3, &W);
state_add_rou_key(a1, &W);
while ( (int)state_bvary_lshift((__int64)a1) >= 0 )
{
state_mix_columns(a1);
v6 = v5;
v5 += 16LL;
state_add_rou_key(a1, v6);
if ( (_UNKNOWN *)v5 == (_UNKNOWN *)((char *)&W + 160) )
{
if ( (int)state_bvary_lshift((__int64)a1) < 0 )
return 0xFFFFFFFFLL;
state_add_rou_key(a1, (unsigned int)((_DWORD)&W + 160));
reverse4x(a1);
return 0LL;
}
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 *a1, int a2, __int64 a3, int a4)
{
unsigned __int64 v5; // rbx
unsigned __int64 v6; // rsi

if ( !a1 || a2 != 16 )
return 0xFFFFFFFFLL;
if ( a3 && a4 == 16 )
{
v5 = (unsigned int)((_DWORD)&W + 16);
((void (*)(void))reverse4x)();
key_expansion(a3, &W);
state_add_rou_key(a1, &W);
while ( (int)func2((__int64)a1) >= 0 )
{
func0(a1);
v6 = v5;
v5 += 16LL;
state_add_rou_key(a1, v6);
if ( (_UNKNOWN *)v5 == (_UNKNOWN *)((char *)&W + 160) )
{
if ( (int)func2((__int64)a1) < 0 )
return 0xFFFFFFFFLL;
state_add_rou_key(a1, (unsigned int)((_DWORD)&W + 160));
reverse4x(a1);
return 0LL;
}
}
}
return 0xFFFFFFFFLL;
}

original_code:
int Aes128cbc_Enc(byte_t * state,int slen,byte_t * key,int keylen,const byte_t * iv)
{
 if(!state || slen!=16 || !key || keylen!=16 )
  return -1;
 int i=0;
 reverse4x(state);
 key_expansion(key,W);
 i=0;
 state_add_rou_key(state,W+i*4);
 i++;
 while(i<10){
  if(state_bvary_lshift(state)<0)
   return -1;
  state_mix_columns(state);
  state_add_rou_key(state,W+i*4);
  i++;
 }
 if(state_bvary_lshift(state)<0)
  return -1;
 state_add_rou_key(state,W+i*4);
 reverse4x(state);
 return 0;
}

canonical_original_code:
int Aes128cbc_Enc(unsigned char *state, int slen, unsigned char *key, int keylen, unsigned char *iv)
{
 if(!state || slen!=16 || !key || keylen!=16 )
  return -1;
 int i = 0;
 reverse4x(state);
 key_expansion(key,W);
 i=0;
 state_add_rou_key(state,W+i*4);
 i++;
 while(i<10){
  if(state_bvary_lshift(state)<0)
   return -1;
  state_mix_columns(state);
  state_add_rou_key(state,W+i*4);
  i++;
 }
 if(state_bvary_lshift(state)<0)
  return -1;
 state_add_rou_key(state,W+i*4);
 reverse4x(state);
 return 0;
}

variable_types: slen int

variable_types:
iv
unsigned char *


decompiled_code:
__int64 __fastcall Aes128cbc_Pkcs7_Enc(__int64 a1, __int64 a2, __int64 a3, int a4, unsigned __int8 *a5, __int64 a6)
{
__int64 v9; // rax
__int64 v10; // rbx
__int64 v11; // rsi
__int64 i; // rax

if ( !a1 || (int)a2 <= 0 )
return 0xFFFFFFFFLL;
if ( a4 == 16 && a3 != 0 && a5 )
{
v9 = 0LL;
if ( (int)a2 <= 15 )
{
PKCS7_Pad(a1, a2, 16LL, a5);
for ( i = 0LL; i != 16; ++i )
a5[i] ^= *(_BYTE *)(a6 + i);
}
else
{
do
{
a5[v9] = *(_BYTE *)(a6 + v9) ^ *(_BYTE *)(a1 + v9);
++v9;
}
while ( v9 != 16 );
}
v10 = (unsigned int)((_DWORD)&W + 16);
reverse4x(a5);
key_expansion(a3, &W);
state_add_rou_key(a5, &W);
while ( (int)state_bvary_lshift((__int64)a5) >= 0 )
{
state_mix_columns(a5);
v11 = v10;
v10 += 16LL;
state_add_rou_key(a5, v11);
if ( (_DWORD)&W + 160 == v10 )
{
if ( (int)state_bvary_lshift((__int64)a5) < 0 )
return 0xFFFFFFFFLL;
state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160));
reverse4x(a5);
return 0LL;
}
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, int a4, unsigned __int8 *a5, __int64 a6)
{
__int64 v9; // rax
__int64 v10; // rbx
__int64 v11; // rsi
__int64 i; // rax

if ( !a1 || (int)a2 <= 0 )
return 0xFFFFFFFFLL;
if ( a4 == 16 && a3 != 0 && a5 )
{
v9 = 0LL;
if ( (int)a2 <= 15 )
{
PKCS7_Pad(a1, a2, 16LL, a5);
for ( i = 0LL; i != 16; ++i )
a5[i] ^= *(_BYTE *)(a6 + i);
}
else
{
do
{
a5[v9] = *(_BYTE *)(a6 + v9) ^ *(_BYTE *)(a1 + v9);
++v9;
}
while ( v9 != 16 );
}
v10 = (unsigned int)((_DWORD)&W + 16);
reverse4x(a5);
key_expansion(a3, &W);
state_add_rou_key(a5, &W);
while ( (int)func2((__int64)a5) >= 0 )
{
func0(a5);
v11 = v10;
v10 += 16LL;
state_add_rou_key(a5, v11);
if ( (_DWORD)&W + 160 == v10 )
{
if ( (int)func2((__int64)a5) < 0 )
return 0xFFFFFFFFLL;
state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160));
reverse4x(a5);
return 0LL;
}
}
}
return 0xFFFFFFFFLL;
}

original_code:
int Aes128cbc_Pkcs7_Enc(byte_t * input,int inlen,byte_t * key,int keylen,byte_t * output,const byte_t * iv)
{
 if(!input || inlen<=0 || !key || keylen!=16 || !output)
  return -1;
 int i=0,ret=0;
 if(inlen<16){
  PKCS7_Pad(input,inlen,16,output);
  for(i=0;i<16;i++) output[i]^=iv[i];
 }else{
  for(i=0;i<16;i++) output[i]=input[i]^iv[i];
 }
 reverse4x(output);
 key_expansion(key,W);
 i=0;
 state_add_rou_key(output,W+i*4);
 i++;
 while(i<10){
  if(state_bvary_lshift(output)<0)
   return -1;
  state_mix_columns(output);
  state_add_rou_key(output,W+i*4);
  i++;
 }
 if(state_bvary_lshift(output)<0)
  return -1;
 state_add_rou_key(output,W+i*4);
 reverse4x(output);
 return 0;
}

canonical_original_code:
int Aes128cbc_Pkcs7_Enc(unsigned char *input, int inlen, unsigned char *key, int keylen, unsigned char *output, unsigned char *iv)
{
 if(!input || inlen<=0 || !key || keylen!=16 || !output)
  return -1;
 int i = 0; int ret = 0;
 if(inlen<16){
  PKCS7_Pad(input,inlen,16,output);
  for(i=0;i<16;i++) output[i]^=iv[i];
 }else{
  for(i=0;i<16;i++) output[i]=input[i]^iv[i];
 }
 reverse4x(output);
 key_expansion(key,W);
 i=0;
 state_add_rou_key(output,W+i*4);
 i++;
 while(i<10){
  if(state_bvary_lshift(output)<0)
   return -1;
  state_mix_columns(output);
  state_add_rou_key(output,W+i*4);
  i++;
 }
 if(state_bvary_lshift(output)<0)
  return -1;
 state_add_rou_key(output,W+i*4);
 reverse4x(output);
 return 0;
}


original_code:
static int move_left_by(const int s,const int b)
{
 if(s>b || s<=0)
  return -1;
 int i,si=-1,bi=-1;
 for(i=0;i<16;i++)
 {
  if(s&bit_vector[i] && si==-1)
   si=i;
  if(b&bit_vector[i] && bi==-1)
   bi=i;
  if(si>0&&bi>0)
   break;
 }
 int m_n=si-bi;
 return m_n;
}

canonical_original_code:
static int move_left_by(int s, int b)
{
 if(s>b || s<=0)
  return -1;
 int i; int si = -1; int bi = -1;
 for(i=0;i<16;i++)
 {
  if(s&bit_vector[i] && si==-1)
   si=i;
  if(b&bit_vector[i] && bi==-1)
   bi=i;
  if(si>0&&bi>0)
   break;
 }
 int m_n = si-bi;
 return m_n;
}

variable_types: s int

variable_types: b int

variable_types: si int

variable_types: bi int


original_code:
int GF2sup8_mul8(const int a,const int b)
{
 if(!a || !b) return 0;
 int BX[8];
 BX[0]=a;
 int i;
 for(i=1;i<8;i++){
  if(BX[i-1] & 0x80){
   BX[i]=BX[i-1]<<1;
   BX[i]^=0x001B;
  }else
   BX[i]=BX[i-1]<<1;
 }
 i=0;
 int n=1;
 int ret=0;
 while(n){
  if(n&b) ret^=BX[i];
  i++;
  n<<=1;
 }
 return ret;
}

canonical_original_code:
int GF2sup8_mul8(int a, int b)
{
 if(!a || !b) return 0;
 int BX[8];
 BX[0]=a;
 int i;
 for(i=1;i<8;i++){
  if(BX[i-1] & 0x80){
   BX[i]=BX[i-1]<<1;
   BX[i]^=0x001B;
  }else
   BX[i]=BX[i-1]<<1;
 }
 i=0;
 int n = 1;
 int ret = 0;
 while(n){
  if(n&b) ret^=BX[i];
  i++;
  n<<=1;
 }
 return ret;
}

variable_types: a int

variable_types:
BX
int [8]

variable_types: i int


original_code:
int GF2sup8_mul16(const int a,const int b)
{
 int i=0;
 int n=1;
 if(!a || !b) return 0;
 int ret=0;
 while(n){
  if(n&b) ret^=(a<<i);
  i++;
  n<<=1;
 }
 return ret;
}

canonical_original_code:
int GF2sup8_mul16(int a, int b)
{
 int i = 0;
 int n = 1;
 if(!a || !b) return 0;
 int ret = 0;
 while(n){
  if(n&b) ret^=(a<<i);
  i++;
  n<<=1;
 }
 return ret;
}

variable_types: n int

variable_types: ret int


decompiled_code:
__int64 __fastcall GF2sup8_divid(int a1, int a2, int *a3)
{
int v3; // er10
int v6; // eax
__int64 result; // rax
unsigned int v8[5]; // [rsp+Ch] [rbp-14h]

v3 = a1;
v8[0] = 0;
if ( a1 <= a2 )
{
result = 0LL;
}
else
{
do
{
v6 = move_left_by(a2, v3);
if ( v6 < 0 )
break;
v8[v6 >> 3] |= 1 << (v6 & 7);
v3 ^= a2 << v6;
}
while ( a2 < v3 );
result = v8[0];
}
if ( a3 )
*a3 = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2, int *a3)
{
int v3; // er10
int v6; // eax
__int64 result; // rax
unsigned int v8[5]; // [rsp+Ch] [rbp-14h]

v3 = a1;
v8[0] = 0;
if ( a1 <= a2 )
{
result = 0LL;
}
else
{
do
{
v6 = func0(a2, v3);
if ( v6 < 0 )
break;
v8[v6 >> 3] |= 1 << (v6 & 7);
v3 ^= a2 << v6;
}
while ( a2 < v3 );
result = v8[0];
}
if ( a3 )
*a3 = v3;
return result;
}

original_code:
int GF2sup8_divid(const int a,const int b,int * mod)
{
 int ret = 0;
 int c=0;
 int A=a;
 while(A>b){
  c=move_left_by(b,A);
  if(c<0) break;
  ((&ret)[(c)/8] |= 1<<((c)%8));
  A=(b<<c)^A;
 }
 if(mod)
  *mod=A;
 return ret;
}

canonical_original_code:
int GF2sup8_divid(int a, int b, int *mod)
{
 int ret = 0;
 int c = 0;
 int A = a;
 while(A>b){
  c=move_left_by(b,A);
  if(c<0) break;
  ((&ret)[(c)/8] |= 1<<((c)%8));
  A=(b<<c)^A;
 }
 if(mod)
  *mod=A;
 return ret;
}

variable_types:
mod
int *

variable_types: c int

variable_types: A int


decompiled_code:
__int64 __fastcall GF2sup8_mod(int a1, int a2)
{
int i; // er10
int v4; // eax

for ( i = a1; a2 < i; i ^= a2 << v4 )
{
v4 = move_left_by(a2, i);
if ( v4 < 0 )
break;
}
return (unsigned int)i;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, int a2)
{
int i; // er10
int v4; // eax

for ( i = a1; a2 < i; i ^= a2 << v4 )
{
v4 = func0(a2, i);
if ( v4 < 0 )
break;
}
return (unsigned int)i;
}

original_code:
int GF2sup8_mod(const int a,const int b)
{
 int c=0;
 int A=a;
 while(A>b)
 {
  c=move_left_by(b,A);
  if(c<0) break;
  A=(b<<c)^A;
 }
 return A;
}

canonical_original_code:
int GF2sup8_mod(int a, int b)
{
 int c = 0;
 int A = a;
 while(A>b)
 {
  c=move_left_by(b,A);
  if(c<0) break;
  A=(b<<c)^A;
 }
 return A;
}


decompiled_code:
__int64 __fastcall euclid_gcb(int a1, int a2)
{
__int64 result; // rax
int v3; // edx
int v4; // ecx
unsigned int v5; // er8

LODWORD(result) = abs32(a1);
v3 = abs32(a2);
if ( v3 >= (int)result )
{
v4 = result;
LODWORD(result) = v3;
v3 = v4;
}
if ( !v3 )
return (unsigned int)result;
do
{
v5 = v3;
v3 = (int)result % v3;
LODWORD(result) = v5;
}
while ( v3 );
return v5;
}

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

LODWORD(result) = abs32(a1);
v3 = abs32(a2);
if ( v3 >= (int)result )
{
v4 = result;
LODWORD(result) = v3;
v3 = v4;
}
if ( !v3 )
return (unsigned int)result;
do
{
v5 = v3;
v3 = (int)result % v3;
LODWORD(result) = v5;
}
while ( v3 );
return v5;
}

original_code:
int euclid_gcb(int a,int b)
{
    int A,B,R;
    if(a<0) a=-a;
    if(b<0) b=-b;
    if(a>b) A=a,B=b;
    else A=b,B=a;
    while(B){
        R=A%B;
        A=B;
        B=R;
    }
    return A;
}

canonical_original_code:
int euclid_gcb(int a, int b)
{
    int A; int B; int R;
    if(a<0) a=-a;
    if(b<0) b=-b;
    if(a>b) A=a,B=b;
    else A=b,B=a;
    while(B){
        R=A%B;
        A=B;
        B=R;
    }
    return A;
}

variable_types: B int

variable_types: R int


decompiled_code:
__int64 __fastcall euclid_gcb_ext(unsigned int a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // edi
unsigned int v4; // er8
unsigned int v5; // ecx
unsigned int v6; // edx
int v7; // eax
unsigned int v8; // edi

result = a1;
if ( (int)a1 <= (int)a2 )
{
result = a2;
a2 = a1;
}
v3 = 1;
if ( a2 > 1 )
{
v4 = 0;
do
{
v5 = v3;
v6 = (int)result % (int)a2;
v7 = v3 * ((int)result / (int)a2);
v8 = v4;
v4 = v5;
v3 = v8 - v7;
result = a2;
a2 = v6;
}
while ( v6 > 1 );
}
if ( a2 )
result = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // edi
unsigned int v4; // er8
unsigned int v5; // ecx
unsigned int v6; // edx
int v7; // eax
unsigned int v8; // edi

result = a1;
if ( (int)a1 <= (int)a2 )
{
result = a2;
a2 = a1;
}
v3 = 1;
if ( a2 > 1 )
{
v4 = 0;
do
{
v5 = v3;
v6 = (int)result % (int)a2;
v7 = v3 * ((int)result / (int)a2);
v8 = v4;
v4 = v5;
v3 = v8 - v7;
result = a2;
a2 = v6;
}
while ( v6 > 1 );
}
if ( a2 )
result = v3;
return result;
}

canonical_original_code:
int euclid_gcb_ext(int a, int b)
{
    int A; int B;
    int A1; int A2; int A3; int B1; int B2; int B3;
    if(a>b)A=a,B=b;
    else A=b,B=a;
    A1=1,A2=0,A3=A;
    B1=0,B2=1,B3=B;
    int Q; int T1; int T2; int T3;
    while(B3!=1 && B3!=0){
        Q=A3/B3;
        T1=A1-Q*B1;
        T2=A2-Q*B2;
        T3=A3-Q*B3;
        A1=B1,A2=B2,A3=B3;
        B1=T1,B2=T2,B3=T3;
    }
    if(B3==0) return A3;
    if(B3==1) return B2;
}

variable_types: Q int


decompiled_code:
__int64 __fastcall euclid_gcb_GF2sup8_ext(unsigned int a1, unsigned int a2, unsigned int *a3)
{
unsigned int v3; // er15
unsigned int v4; // er14
unsigned int v5; // ebx
unsigned int v6; // ebp
unsigned int v7; // er13
__int64 v8; // r12
int v9; // er9
unsigned int v10; // eax
unsigned int v11; // er9
int v13; // [rsp+Ch] [rbp-4Ch]
unsigned int v14; // [rsp+10h] [rbp-48h]
int v15; // [rsp+14h] [rbp-44h]

v3 = a1;
v4 = a2;
if ( a2 <= 1 )
{
v10 = a1;
v5 = 1;
}
else
{
v13 = 1;
v5 = 1;
v6 = 0;
v7 = 0;
do
{
v8 = (unsigned int)GF2sup8_divid(v3, v4, 0LL);
v15 = v6;
v6 = GF2sup8_mul16(v8, v6) ^ v13;
v14 = v5;
v5 = v7 ^ GF2sup8_mul16((unsigned int)v8, v5);
v7 = v14;
v9 = GF2sup8_mul16((unsigned int)v8, v4);
v10 = v4;
v11 = v3 ^ v9;
v13 = v15;
v3 = v4;
v4 = v11;
}
while ( v11 > 1 );
}
if ( !v4 )
{
v4 = v10;
v5 = -1;
}
*a3 = v5;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, unsigned int a2, unsigned int *a3)
{
unsigned int v3; // er15
unsigned int v4; // er14
unsigned int v5; // ebx
unsigned int v6; // ebp
unsigned int v7; // er13
__int64 v8; // r12
int v9; // er9
unsigned int v10; // eax
unsigned int v11; // er9
int v13; // [rsp+Ch] [rbp-4Ch]
unsigned int v14; // [rsp+10h] [rbp-48h]
int v15; // [rsp+14h] [rbp-44h]

v3 = a1;
v4 = a2;
if ( a2 <= 1 )
{
v10 = a1;
v5 = 1;
}
else
{
v13 = 1;
v5 = 1;
v6 = 0;
v7 = 0;
do
{
v8 = (unsigned int)GF2sup8_divid(v3, v4, 0LL);
v15 = v6;
v6 = GF2sup8_mul16(v8, v6) ^ v13;
v14 = v5;
v5 = v7 ^ GF2sup8_mul16((unsigned int)v8, v5);
v7 = v14;
v9 = GF2sup8_mul16((unsigned int)v8, v4);
v10 = v4;
v11 = v3 ^ v9;
v13 = v15;
v3 = v4;
v4 = v11;
}
while ( v11 > 1 );
}
if ( !v4 )
{
v4 = v10;
v5 = -1;
}
*a3 = v5;
return v4;
}

original_code:
int euclid_gcb_GF2sup8_ext(int mx,int bx,int * inverse)
{
    int a1x=1,a2x=0,a3x=mx;
    int b1x=0,b2x=1,b3x=bx;
 int t1x=0,t2x=0,t3x=0;
    int qx;
    while(b3x!=0 && b3x!=1)
    {
  qx=GF2sup8_divid(a3x,b3x,((void *)0));
  t1x=((a1x)^(GF2sup8_mul16(qx,b1x)));
  t2x=((a2x)^(GF2sup8_mul16(qx,b2x)));
  t3x=((a3x)^(GF2sup8_mul16(qx,b3x)));
  a1x=b1x,a2x=b2x,a3x=b3x;
  b1x=t1x,b2x=t2x,b3x=t3x;
    }
 if(b3x){
  *inverse=b2x;
  return b3x;
 }else{
  *inverse=-1;
  return a3x;
 }
}

canonical_original_code:
int euclid_gcb_GF2sup8_ext(int mx, int bx, int *inverse)
{
    int a1x = 1; int a2x = 0; int a3x = mx;
    int b1x = 0; int b2x = 1; int b3x = bx;
 int t1x = 0; int t2x = 0; int t3x = 0;
    int qx;
    while(b3x!=0 && b3x!=1)
    {
  qx=GF2sup8_divid(a3x,b3x,((void *)0));
  t1x=((a1x)^(GF2sup8_mul16(qx,b1x)));
  t2x=((a2x)^(GF2sup8_mul16(qx,b2x)));
  t3x=((a3x)^(GF2sup8_mul16(qx,b3x)));
  a1x=b1x,a2x=b2x,a3x=b3x;
  b1x=t1x,b2x=t2x,b3x=t3x;
    }
 if(b3x){
  *inverse=b2x;
  return b3x;
 }else{
  *inverse=-1;
  return a3x;
 }
}

variable_types: mx int

variable_types: bx int

variable_types:
inverse
int *

variable_types: qx int


decompiled_code:
__int64 __fastcall istate_mix_columns(unsigned __int8 *a1)
{
unsigned __int8 *v1; // r13
unsigned __int8 *v2; // r12
__int64 v3; // rbp
__int64 i; // rdi
int v5; // ebx
int v6; // ebx
int v7; // ebx
__int64 result; // rax
char v9[3]; // [rsp+Ch] [rbp-2Ch]
unsigned __int8 v10; // [rsp+Fh] [rbp-29h]

v1 = a1 + 4;
v2 = a1;
do
{
v3 = 0LL;
for ( i = 14LL; ; i = (unsigned __int8)IMixC[4 * v3] )
{
v5 = GF2sup8_mul8(i, *v2);
v6 = GF2sup8_mul8((unsigned __int8)byte_551[4 * v3], v2[4]) ^ v5;
v7 = GF2sup8_mul8((unsigned __int8)byte_552[4 * v3], v2[8]) ^ v6;
v9[v3] = GF2sup8_mul8((unsigned __int8)byte_553[4 * v3], v2[12]) ^ v7;
if ( ++v3 == 4 )
break;
}
*v2++ = v9[0];
v2[3] = v9[1];
v2[7] = v9[2];
result = v10;
v2[11] = v10;
}
while ( v2 != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
unsigned __int8 *v1; // r13
unsigned __int8 *v2; // r12
__int64 v3; // rbp
__int64 i; // rdi
int v5; // ebx
int v6; // ebx
int v7; // ebx
__int64 result; // rax
char v9[3]; // [rsp+Ch] [rbp-2Ch]
unsigned __int8 v10; // [rsp+Fh] [rbp-29h]

v1 = a1 + 4;
v2 = a1;
do
{
v3 = 0LL;
for ( i = 14LL; ; i = (unsigned __int8)IMixC[4 * v3] )
{
v5 = GF2sup8_mul8(i, *v2);
v6 = GF2sup8_mul8((unsigned __int8)byte_551[4 * v3], v2[4]) ^ v5;
v7 = GF2sup8_mul8((unsigned __int8)byte_552[4 * v3], v2[8]) ^ v6;
v9[v3] = GF2sup8_mul8((unsigned __int8)byte_553[4 * v3], v2[12]) ^ v7;
if ( ++v3 == 4 )
break;
}
*v2++ = v9[0];
v2[3] = v9[1];
v2[7] = v9[2];
result = v10;
v2[11] = v10;
}
while ( v2 != v1 );
return result;
}

original_code:
static void istate_mix_columns(byte_t * state)
{
 int i,j,ret;
 byte_t tr[4];
 for(i=0;i<4;i++){
  for(j=0;j<4;j++){
   tr[j]=GF2sup8_mul8(IMixC[j][0],state[0*4+i])^
    GF2sup8_mul8(IMixC[j][1],state[1*4+i])^
    GF2sup8_mul8(IMixC[j][2],state[2*4+i])^
    GF2sup8_mul8(IMixC[j][3],state[3*4+i]);
  }
  state[0*4+i]=tr[0];
  state[1*4+i]=tr[1];
  state[2*4+i]=tr[2];
  state[3*4+i]=tr[3];
 }
}

canonical_original_code:
static void istate_mix_columns(unsigned char *state)
{
 int i; int j; int ret;
 unsigned char tr[4];
 for(i=0;i<4;i++){
  for(j=0;j<4;j++){
   tr[j]=GF2sup8_mul8(IMixC[j][0],state[0*4+i])^
    GF2sup8_mul8(IMixC[j][1],state[1*4+i])^
    GF2sup8_mul8(IMixC[j][2],state[2*4+i])^
    GF2sup8_mul8(IMixC[j][3],state[3*4+i]);
  }
  state[0*4+i]=tr[0];
  state[1*4+i]=tr[1];
  state[2*4+i]=tr[2];
  state[3*4+i]=tr[3];
 }
}

variable_types:
state
unsigned char *

variable_types: j int

variable_types:
tr
unsigned char [4]


decompiled_code:
__int64 __fastcall state_shift_row_right(int *a1, unsigned int a2)
{
unsigned int v2; // er12
__int64 result; // rax
unsigned int v4; // ebp
int v5; // eax
char v6; // cl
int v7; // er8
char v8; // al
unsigned int v9; // edx
unsigned int v10; // ebp

if ( a2 > 3 || !a1 )
return (unsigned int)-1;
v2 = a2;
if ( !a2 )
return v2;
v4 = *a1;
v5 = is_big_endian();
v6 = 8 * a2;
v7 = v5;
v8 = 8 * (4 - a2);
if ( v7 )
{
v9 = v4 >> v6;
v10 = v4 << v8;
}
else
{
v9 = v4 << v6;
v10 = v4 >> v8;
}
result = 0LL;
*a1 = v9 | v10;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, unsigned int a2)
{
unsigned int v2; // er12
__int64 result; // rax
unsigned int v4; // ebp
int v5; // eax
char v6; // cl
int v7; // er8
char v8; // al
unsigned int v9; // edx
unsigned int v10; // ebp

if ( a2 > 3 || !a1 )
return (unsigned int)-1;
v2 = a2;
if ( !a2 )
return v2;
v4 = *a1;
v5 = is_big_endian();
v6 = 8 * a2;
v7 = v5;
v8 = 8 * (4 - a2);
if ( v7 )
{
v9 = v4 >> v6;
v10 = v4 << v8;
}
else
{
v9 = v4 << v6;
v10 = v4 >> v8;
}
result = 0LL;
*a1 = v9 | v10;
return result;
}

original_code:
static int state_shift_row_right(byte_t * state,int n)
{
 if(n>3 || n<0 || !state)
  return -1;
 if(n==0) return 0;
 word_t * swt = (word_t*)state;
 word_t sw=*swt;
 if(is_big_endian()){
  *swt=((sw) >> (n*8))|((sw) << ((32) - (n*8)));
 }else{
  *swt=((sw) << (n*8))|((sw) >> ((32) - (n*8)));
 }
 return 0;
}

canonical_original_code:
static int state_shift_row_right(unsigned char *state, int n)
{
 if(n>3 || n<0 || !state)
  return -1;
 if(n==0) return 0;
 unsigned int *swt = (word_t*)state;
 unsigned int sw = *swt;
 if(is_big_endian()){
  *swt=((sw) >> (n*8))|((sw) << ((32) - (n*8)));
 }else{
  *swt=((sw) << (n*8))|((sw) >> ((32) - (n*8)));
 }
 return 0;
}

variable_types:
swt
unsigned int *

variable_types:
sw
unsigned int


decompiled_code:
__int64 __fastcall istate_rshift_bvary(__int64 a1)
{
unsigned int v1; // er12
__int64 v2; // rbp
__int64 v3; // rbx
__int64 result; // rax
__int64 v5; // rdi

v1 = 0;
v2 = a1 + 4;
while ( 1 )
{
v3 = v2 - 4;
result = state_shift_row_right((int *)(v2 - 4), v1);
if ( (int)result < 0 )
break;
do
{
v5 = v3++;
subbyte(v5, &isbox);
}
while ( v3 != v2 );
++v1;
v2 = v3 + 4;
if ( v1 == 4 )
return 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned int v1; // er12
__int64 v2; // rbp
__int64 v3; // rbx
__int64 result; // rax
__int64 v5; // rdi

v1 = 0;
v2 = a1 + 4;
while ( 1 )
{
v3 = v2 - 4;
result = func1((int *)(v2 - 4), v1);
if ( (int)result < 0 )
break;
do
{
v5 = v3++;
subbyte(v5, &isbox);
}
while ( v3 != v2 );
++v1;
v2 = v3 + 4;
if ( v1 == 4 )
return 0LL;
}
return result;
}

original_code:
static int istate_rshift_bvary(byte_t * state)
{
 int i,j,ret;
 for(i=0;i<4;i++)
 {
  ret = state_shift_row_right(&state[i*4],i);
  if(ret < 0)
   return ret;
  for(j=0;j<4;j++)
   subbyte(&state[i*4+j],isbox);
 }
 return 0;
}

canonical_original_code:
static int istate_rshift_bvary(unsigned char *state)
{
 int i; int j; int ret;
 for(i=0;i<4;i++)
 {
  ret = state_shift_row_right(&state[i*4],i);
  if(ret < 0)
   return ret;
  for(j=0;j<4;j++)
   subbyte(&state[i*4+j],isbox);
 }
 return 0;
}

variable_types: i int


decompiled_code:
__int64 __fastcall Aes128_Dec(__int64 a1, int a2, __int64 a3, int a4, unsigned __int8 *a5)
{
unsigned __int64 v7; // rbx

if ( !a1 || a2 <= 15 )
return 0xFFFFFFFFLL;
if ( a4 == 16 && a3 != 0 && a5 )
{
v7 = (unsigned int)((_DWORD)&W + 144);
state_put(a1, a5);
key_expansion(a3, &W);
state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160));
while ( (int)istate_rshift_bvary((__int64)a5) >= 0 && (int)state_add_rou_key(a5, v7) >= 0 )
{
v7 -= 16LL;
istate_mix_columns(a5);
if ( (_UNKNOWN *)v7 == &W )
{
if ( (int)istate_rshift_bvary((__int64)a5) < 0 )
return 0xFFFFFFFFLL;
return (unsigned int)((int)state_add_rou_key(a5, &W) >> 31);
}
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, __int64 a3, int a4, unsigned __int8 *a5)
{
unsigned __int64 v7; // rbx

if ( !a1 || a2 <= 15 )
return 0xFFFFFFFFLL;
if ( a4 == 16 && a3 != 0 && a5 )
{
v7 = (unsigned int)((_DWORD)&W + 144);
state_put(a1, a5);
key_expansion(a3, &W);
state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160));
while ( (int)func2((__int64)a5) >= 0 && (int)state_add_rou_key(a5, v7) >= 0 )
{
v7 -= 16LL;
func0(a5);
if ( (_UNKNOWN *)v7 == &W )
{
if ( (int)func2((__int64)a5) < 0 )
return 0xFFFFFFFFLL;
return (unsigned int)((int)state_add_rou_key(a5, &W) >> 31);
}
}
}
return 0xFFFFFFFFLL;
}

original_code:
int Aes128_Dec(byte_t * cipherText,int cilen,byte_t * key,int keylen,byte_t * plainText)
{
 if(!cipherText || cilen<=0 || !key || keylen!=16 || !plainText)
  return -1;
 int i=0,ret=0;
 if(cilen<16)
  return -1;
 state_put(cipherText,plainText);
 key_expansion(key,W);
 i=10;
 state_add_rou_key(plainText,W+i*4);
 i--;
 while(i>0){
  if(istate_rshift_bvary(plainText)<0)
   return -1;
  if(state_add_rou_key(plainText,W+i*4)<0)
   return -1;
  istate_mix_columns(plainText);
  i--;
 }
 if(istate_rshift_bvary(plainText)<0)
  return -1;
 if(state_add_rou_key(plainText,W+i*4)<0)
  return -1;
 return 0;
}

canonical_original_code:
int Aes128_Dec(unsigned char *cipherText, int cilen, unsigned char *key, int keylen, unsigned char *plainText)
{
 if(!cipherText || cilen<=0 || !key || keylen!=16 || !plainText)
  return -1;
 int i = 0; int ret = 0;
 if(cilen<16)
  return -1;
 state_put(cipherText,plainText);
 key_expansion(key,W);
 i=10;
 state_add_rou_key(plainText,W+i*4);
 i--;
 while(i>0){
  if(istate_rshift_bvary(plainText)<0)
   return -1;
  if(state_add_rou_key(plainText,W+i*4)<0)
   return -1;
  istate_mix_columns(plainText);
  i--;
 }
 if(istate_rshift_bvary(plainText)<0)
  return -1;
 if(state_add_rou_key(plainText,W+i*4)<0)
  return -1;
 return 0;
}

variable_types:
cipherText
unsigned char *

variable_types: cilen int

variable_types:
key
unsigned char *

variable_types: keylen int

variable_types:
plainText
unsigned char *

variable_types: ret int


decompiled_code:
__int64 __fastcall Aes128cbc_Pkcs7_Dec(__int64 a1, int a2, __int64 a3, int a4, unsigned __int8 *a5, __int64 a6, __int64 a7)
{
unsigned __int64 v10; // rbx
unsigned __int8 *v11; // rsi
__int64 i; // rdi
unsigned __int8 *v13; // rax
_BYTE *v14; // rdx

if ( a1 && a2 > 0 && a4 == 16 && a3 != 0 && a5 )
{
v10 = (unsigned int)((_DWORD)&W + 144);
state_put(a1, a5);
key_expansion(a3, &W);
state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160));
while ( (int)istate_rshift_bvary((__int64)a5) >= 0 && (int)state_add_rou_key(a5, v10) >= 0 )
{
v10 -= 16LL;
istate_mix_columns(a5);
if ( (_UNKNOWN *)v10 == &W )
{
if ( (int)istate_rshift_bvary((__int64)a5) < 0 || (int)state_add_rou_key(a5, &W) < 0 )
return 0xFFFFFFFFLL;
v11 = a5 + 4;
for ( i = 0LL; i != 4; ++i )
{
v13 = v11 - 4;
v14 = (_BYTE *)(a7 + i);
do
{
*v13++ ^= *v14;
v14 += 4;
}
while ( v13 != v11 );
v11 = v13 + 4;
}
reverse4x(a5, v11, v14);
PKCS7_UnPad(a5, 16LL, 16LL, a6);
return 0LL;
}
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, __int64 a3, int a4, unsigned __int8 *a5, __int64 a6, __int64 a7)
{
unsigned __int64 v10; // rbx
unsigned __int8 *v11; // rsi
__int64 i; // rdi
unsigned __int8 *v13; // rax
_BYTE *v14; // rdx

if ( a1 && a2 > 0 && a4 == 16 && a3 != 0 && a5 )
{
v10 = (unsigned int)((_DWORD)&W + 144);
state_put(a1, a5);
key_expansion(a3, &W);
state_add_rou_key(a5, (unsigned int)((_DWORD)&W + 160));
while ( (int)func2((__int64)a5) >= 0 && (int)state_add_rou_key(a5, v10) >= 0 )
{
v10 -= 16LL;
func0(a5);
if ( (_UNKNOWN *)v10 == &W )
{
if ( (int)func2((__int64)a5) < 0 || (int)state_add_rou_key(a5, &W) < 0 )
return 0xFFFFFFFFLL;
v11 = a5 + 4;
for ( i = 0LL; i != 4; ++i )
{
v13 = v11 - 4;
v14 = (_BYTE *)(a7 + i);
do
{
*v13++ ^= *v14;
v14 += 4;
}
while ( v13 != v11 );
v11 = v13 + 4;
}
reverse4x(a5, v11, v14);
PKCS7_UnPad(a5, 16LL, 16LL, a6);
return 0LL;
}
}
}
return 0xFFFFFFFFLL;
}

original_code:
int Aes128cbc_Pkcs7_Dec(byte_t * cipherText,int cilen,byte_t * key,int keylen,byte_t * plainText,int * ptlen,const byte_t * iv)
{
 if(!cipherText || cilen<=0 || !key || keylen!=16 || !plainText)
  return -1;
 int i=0,ret=0;
 byte_t inPadBuf[16];
 state_put(cipherText,plainText);
 key_expansion(key,W);
 i=10;
 state_add_rou_key(plainText,W+i*4);
 i--;
 while(i>0){
  if(istate_rshift_bvary(plainText)<0)
   return -1;
  if(state_add_rou_key(plainText,W+i*4)<0)
   return -1;
  istate_mix_columns(plainText);
  i--;
 }
 if(istate_rshift_bvary(plainText)<0)
  return -1;
 if(state_add_rou_key(plainText,W+i*4)<0)
  return -1;
 int j;
 for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   plainText[i*4+j]=plainText[i*4+j]^iv[j*4+i];
 reverse4x(plainText);
 PKCS7_UnPad(plainText,16,16,ptlen);
 return 0;
}

canonical_original_code:
int Aes128cbc_Pkcs7_Dec(unsigned char *cipherText, int cilen, unsigned char *key, int keylen, unsigned char *plainText, int *ptlen, unsigned char *iv)
{
 if(!cipherText || cilen<=0 || !key || keylen!=16 || !plainText)
  return -1;
 int i = 0; int ret = 0;
 unsigned char inPadBuf[16];
 state_put(cipherText,plainText);
 key_expansion(key,W);
 i=10;
 state_add_rou_key(plainText,W+i*4);
 i--;
 while(i>0){
  if(istate_rshift_bvary(plainText)<0)
   return -1;
  if(state_add_rou_key(plainText,W+i*4)<0)
   return -1;
  istate_mix_columns(plainText);
  i--;
 }
 if(istate_rshift_bvary(plainText)<0)
  return -1;
 if(state_add_rou_key(plainText,W+i*4)<0)
  return -1;
 int j;
 for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   plainText[i*4+j]=plainText[i*4+j]^iv[j*4+i];
 reverse4x(plainText);
 PKCS7_UnPad(plainText,16,16,ptlen);
 return 0;
}

variable_types:
ptlen
int *

variable_types:
iv
unsigned char *

variable_types:
inPadBuf
unsigned char [16]


decompiled_code:
__int64 __fastcall bit_add_8(char a1)
{
int v1; // ecx
int v2; // edx
__int64 result; // rax

v1 = 7;
v2 = 1;
result = a1 & 1;
do
{
v2 *= 2;
if ( (v2 & a1) != 0 )
result = (unsigned int)result ^ 1;
--v1;
}
while ( v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1)
{
int v1; // ecx
int v2; // edx
__int64 result; // rax

v1 = 7;
v2 = 1;
result = a1 & 1;
do
{
v2 *= 2;
if ( (v2 & a1) != 0 )
result = (unsigned int)result ^ 1;
--v1;
}
while ( v1 );
return result;
}

original_code:
static int bit_add_8(int8_t bx)
{
 int i=1;
 int ret=bx&i;
 while(i!=0x80) {
  i<<=1;
  if(bx&i)
   ret=((ret)^(1));
  else
   ret=((ret)^(0));
 }
 return ret;
}

canonical_original_code:
static int bit_add_8(char bx)
{
 int i = 1;
 int ret = bx&i;
 while(i!=0x80) {
  i<<=1;
  if(bx&i)
   ret=((ret)^(1));
  else
   ret=((ret)^(0));
 }
 return ret;
}

variable_types: bx char


decompiled_code:
__int64 __fastcall sbox_init(__int64 a1)
{
__int64 v1; // r8
char v2; // si
__int64 result; // rax

v1 = a1 + 256;
v2 = 0;
do
{
for ( result = 0LL; result != 16; ++result )
*(_BYTE *)(a1 + result) = result | v2;
a1 += 16LL;
v2 += 16;
}
while ( a1 != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // r8
char v2; // si
__int64 result; // rax

v1 = a1 + 256;
v2 = 0;
do
{
for ( result = 0LL; result != 16; ++result )
*(_BYTE *)(a1 + result) = result | v2;
a1 += 16LL;
v2 += 16;
}
while ( a1 != v1 );
return result;
}

original_code:
static void sbox_init(byte_t * s)
{
 int8_t i,j;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++)
   s[i*16+j]=i<<4|j;
}

canonical_original_code:
static void sbox_init(unsigned char *s)
{
 char i; char j;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++)
   s[i*16+j]=i<<4|j;
}

variable_types:
s
unsigned char *

variable_types: i char

variable_types: j char


original_code:
static void sbox_bit_column_vector(byte_t * s)
{
 int i,j,k;
 int vary,sxy;
 int bx=0,bt=0;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++){
   bx=0;
   sxy = s[i*16+j];
   for(k=0;k<8;k++) {
    bt=bit_add_8(sxy&vary_matrix[k]);
    if(bt)
     ((&bx)[(k)/8] |= 1<<((k)%8));
   }
   vary=((bx)^(0x63));
   s[i*16+j]=vary;
  }
}

canonical_original_code:
static void sbox_bit_column_vector(unsigned char *s)
{
 int i; int j; int k;
 int vary; int sxy;
 int bx = 0; int bt = 0;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++){
   bx=0;
   sxy = s[i*16+j];
   for(k=0;k<8;k++) {
    bt=bit_add_8(sxy&vary_matrix[k]);
    if(bt)
     ((&bx)[(k)/8] |= 1<<((k)%8));
   }
   vary=((bx)^(0x63));
   s[i*16+j]=vary;
  }
}

variable_types: j int

variable_types: k int

variable_types: vary int

variable_types: sxy int

variable_types: bx int

variable_types: bt int


original_code:
static void isbox_bit_column_vector(byte_t * s)
{
 int i,j,k;
 int vary,sxy;
 int bx=0,bt=0;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++){
   bx=0;
   sxy = s[i*16+j];
   for(k=0;k<8;k++) {
    bt=bit_add_8(sxy&ivary_matrix[k]);
    if(bt)
     ((&bx)[(k)/8] |= 1<<((k)%8));
   }
   vary=((bx)^(0x05));
   s[i*16+j]=vary;
  }
}

canonical_original_code:
static void isbox_bit_column_vector(unsigned char *s)
{
 int i; int j; int k;
 int vary; int sxy;
 int bx = 0; int bt = 0;
 for(i=0;i<16;i++)
  for(j=0;j<16;j++){
   bx=0;
   sxy = s[i*16+j];
   for(k=0;k<8;k++) {
    bt=bit_add_8(sxy&ivary_matrix[k]);
    if(bt)
     ((&bx)[(k)/8] |= 1<<((k)%8));
   }
   vary=((bx)^(0x05));
   s[i*16+j]=vary;
  }
}

variable_types: i int


decompiled_code:
__int64 __fastcall gen_sbox(__int64 a1)
{
sbox_init(a1);
sbox_inverse_gf28_isra_0(a1);
return sbox_bit_column_vector(a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
func1(a1);
func4(a1);
return func2(a1);
}

original_code:
void gen_sbox(byte_t * s)
{
 sbox_init(s);
 sbox_inverse_gf28(s);
 sbox_bit_column_vector(s);
}

canonical_original_code:
void gen_sbox(unsigned char *s)
{
 sbox_init(s);
 sbox_inverse_gf28(s);
 sbox_bit_column_vector(s);
}


decompiled_code:
void __fastcall gen_isbox(__int64 a1)
{
sbox_init(a1);
isbox_bit_column_vector(a1);
sbox_inverse_gf28_isra_0(a1);
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1)
{
func1(a1);
func3(a1);
func4(a1);
}

original_code:
void gen_isbox(byte_t * is)
{
 sbox_init(is);
 isbox_bit_column_vector(is);
 sbox_inverse_gf28(is);
}

canonical_original_code:
void gen_isbox(unsigned char *is)
{
 sbox_init(is);
 isbox_bit_column_vector(is);
 sbox_inverse_gf28(is);
}

variable_types:
is
unsigned char *


variable_types:
state
unsigned char *

variable_types:
key
unsigned int *


decompiled_code:
__int64 __fastcall key_expansion(__int64 a1, __int64 a2)
{
__int64 i; // rdx
__int64 j; // rcx
__int64 result; // rax
int v6; // eax
int v7; // [rsp+Ch] [rbp-4h] BYREF

for ( i = 0LL; i != 16; i += 4LL )
*(_DWORD *)(a2 + i) = _byteswap_ulong(*(_DWORD *)(a1 + i));
for ( j = 4LL; j != 44; ++j )
{
v6 = *(_DWORD *)(a2 + 4 * j - 4);
if ( (j & 3) == 0 )
{
v7 = __ROL4__(v6, 8);
subword((unsigned __int8 *)&v7, (__int64)&sbox);
v6 = v7 ^ ((unsigned __int8)RC[((int)j >> 2) - 1] << 24);
}
result = *(_DWORD *)(a2 + 4 * j - 16) ^ (unsigned int)v6;
*(_DWORD *)(a2 + 4 * j) = result;
}
return result;
}

variable_types:
key
unsigned char *

variable_types:
w
unsigned int *

variable_types:
temp
unsigned int