decompiled_code:
__int64 __fastcall alloc_add_to_free(__int64 a1, __int64 a2)
{
__int64 result; // rax
unsigned int v4; // edi
__int64 v5; // rdx
char v6; // r8
__int64 v7; // rcx

result = *(_QWORD *)(a1 + 8);
*(_DWORD *)(a2 + 12) = 0;
if ( result )
{
v4 = *(_DWORD *)(a2 + 8);
v5 = qword_BB8;
v6 = 0;
if ( *(_DWORD *)(result + 8) > v4 )
{
++qword_BA8;
*(_QWORD *)(a1 + 8) = a2;
*(_QWORD *)(a2 + 16) = 0LL;
*(_QWORD *)(a2 + 24) = result;
*(_QWORD *)(result + 16) = a2;
}
else
{
while ( 1 )
{
v7 = result;
result = *(_QWORD *)(result + 24);
if ( !result )
break;
++v5;
v6 = 1;
if ( v4 < *(_DWORD *)(result + 8) )
{
qword_BB8 = v5;
++qword_BB0;
++qword_BC0;
*(_QWORD *)(result + 16) = a2;
goto LABEL_6;
}
}
if ( v6 )
qword_BB8 = v5;
++qword_BC8;
LABEL_6:
*(_QWORD *)(a2 + 16) = v7;
result = *(_QWORD *)(v7 + 24);
*(_QWORD *)(a2 + 24) = result;
*(_QWORD *)(v7 + 24) = a2;
}
}
else
{
++_gcov0_alloc_add_to_free;
*(_QWORD *)(a1 + 8) = a2;
*(_QWORD *)(a2 + 16) = 0LL;
*(_QWORD *)(a2 + 24) = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 result; // rax
unsigned int v4; // edi
__int64 v5; // rdx
char v6; // r8
__int64 v7; // rcx

result = *(_QWORD *)(a1 + 8);
*(_DWORD *)(a2 + 12) = 0;
if ( result )
{
v4 = *(_DWORD *)(a2 + 8);
v5 = qword_BB8;
v6 = 0;
if ( *(_DWORD *)(result + 8) > v4 )
{
++qword_BA8;
*(_QWORD *)(a1 + 8) = a2;
*(_QWORD *)(a2 + 16) = 0LL;
*(_QWORD *)(a2 + 24) = result;
*(_QWORD *)(result + 16) = a2;
}
else
{
while ( 1 )
{
v7 = result;
result = *(_QWORD *)(result + 24);
if ( !result )
break;
++v5;
v6 = 1;
if ( v4 < *(_DWORD *)(result + 8) )
{
qword_BB8 = v5;
++qword_BB0;
++qword_BC0;
*(_QWORD *)(result + 16) = a2;
goto LABEL_6;
}
}
if ( v6 )
qword_BB8 = v5;
++qword_BC8;
LABEL_6:
*(_QWORD *)(a2 + 16) = v7;
result = *(_QWORD *)(v7 + 24);
*(_QWORD *)(a2 + 24) = result;
*(_QWORD *)(v7 + 24) = a2;
}
}
else
{
++_gcov0_alloc_add_to_free;
*(_QWORD *)(a1 + 8) = a2;
*(_QWORD *)(a2 + 16) = 0LL;
*(_QWORD *)(a2 + 24) = 0LL;
}
return result;
}

original_code:
static void alloc_add_to_free (xHeapArea_t* heap, xHeapChunk_t* chunk)
{
    xHeapChunk_t* curs = heap->free_list;
    chunk->is_used = 0;
    if ( !curs ) {
        heap->free_list = chunk;
        chunk->prev_chunk = 0;
        chunk->next_chunk = 0;
        return;
    }
    if ( curs->chunk_size > chunk->chunk_size ) {
        heap->free_list = chunk;
        chunk->prev_chunk = 0;
        chunk->next_chunk = curs;
        curs->prev_chunk = chunk;
        return;
    }
    while ( curs->next_chunk ) {
        if ( curs->next_chunk->chunk_size > chunk->chunk_size ) {
            break;
        }
        curs = curs->next_chunk;
    }
    if ( curs->next_chunk ) {
        curs->next_chunk->prev_chunk = chunk;
    }
    chunk->prev_chunk = curs;
    chunk->next_chunk = curs->next_chunk;
    curs->next_chunk = chunk;
}

canonical_original_code:
static void alloc_add_to_free(struct xHeapArea *heap, struct xHeapChunk *chunk)
{
    struct xHeapChunk *curs = heap->free_list;
    chunk->is_used = 0;
    if ( !curs ) {
        heap->free_list = chunk;
        chunk->prev_chunk = 0;
        chunk->next_chunk = 0;
        return;
    }
    if ( curs->chunk_size > chunk->chunk_size ) {
        heap->free_list = chunk;
        chunk->prev_chunk = 0;
        chunk->next_chunk = curs;
        curs->prev_chunk = chunk;
        return;
    }
    while ( curs->next_chunk ) {
        if ( curs->next_chunk->chunk_size > chunk->chunk_size ) {
            break;
        }
        curs = curs->next_chunk;
    }
    if ( curs->next_chunk ) {
        curs->next_chunk->prev_chunk = chunk;
    }
    chunk->prev_chunk = curs;
    chunk->next_chunk = curs->next_chunk;
    curs->next_chunk = chunk;
}

variable_types: struct xHeapArea { struct xHeapChunk { unsigned int prev_size; unsigned int reserved; unsigned int chunk_size; unsigned int is_used; union { struct { struct xHeapChunk *prev_chunk; struct xHeapChunk *next_chunk; }; unsigned int data[4]; }; } *start; struct xHeapChunk *free_list; unsigned long max; unsigned long available; char lock; long flags; } *

variable_types:
chunk
struct xHeapChunk *


repo: AxFab/axlibc

original_code:
static void alloc_rem_of_free (xHeapArea_t* heap, xHeapChunk_t* chunk)
{
    if ( chunk->next_chunk ) {
        chunk->next_chunk->prev_chunk = chunk->prev_chunk;
    }
    if ( chunk->prev_chunk ) {
        chunk->prev_chunk->next_chunk = chunk->next_chunk;
    } else {
        heap->free_list = chunk->next_chunk;
    }
    chunk->prev_chunk = (xHeapChunk_t*)0xcccccccc;
    chunk->next_chunk = (xHeapChunk_t*)0xcccccccc;
    chunk->is_used |= 0x01;
}

canonical_original_code:
static void alloc_rem_of_free(struct xHeapArea *heap, struct xHeapChunk *chunk)
{
    if ( chunk->next_chunk ) {
        chunk->next_chunk->prev_chunk = chunk->prev_chunk;
    }
    if ( chunk->prev_chunk ) {
        chunk->prev_chunk->next_chunk = chunk->next_chunk;
    } else {
        heap->free_list = chunk->next_chunk;
    }
    chunk->prev_chunk = (struct xHeapChunk *)0xcccccccc;
    chunk->next_chunk = (struct xHeapChunk *)0xcccccccc;
    chunk->is_used |= 0x01;
}

return_type: void

user_defined_types: struct xHeapArea { struct xHeapChunk *start; struct xHeapChunk *free_list; unsigned long max; unsigned long available; char lock; long flags; }

user_defined_types: struct xHeapChunk { unsigned int prev_size; unsigned int reserved; unsigned int chunk_size; unsigned int is_used; union { struct { struct xHeapChunk *prev_chunk; struct xHeapChunk *next_chunk; }; unsigned int data[4]; }; }


decompiled_code:
__int64 __fastcall meminit_r(__int64 *a1, __int64 a2, __int64 a3)
{
_DWORD *v3; // rax
unsigned __int64 v4; // rsi
__int64 result; // rax

++_gcov0_meminit_r;
a1[5] |= 4uLL;
v3 = (_DWORD *)((a2 + 31) & 0xFFFFFFFFFFFFFFE0LL);
*a1 = (__int64)v3;
a1[1] = 0LL;
v4 = (a3 + a2 - (_QWORD)v3) & 0xFFFFFFFFFFFFFFE0LL;
a1[3] = v4;
a1[2] = (__int64)v3 + v4;
*v3 = 0;
*(_DWORD *)(*a1 + 8) = a1[3];
a1[5] |= 1uLL;
*((_BYTE *)a1 + 32) = 0;
result = alloc_add_to_free((__int64)a1, *a1);
++qword_BF8;
return result;
}

original_code:
void meminit_r(xHeapArea_t* heap, void* base, size_t length)
{
    heap->flags |= 0x04;
    heap->start = ( xHeapChunk_t* ) ((((uintptr_t)base)+(sizeof(xHeapChunk_t)-1))&(~(sizeof(xHeapChunk_t)-1)));
    heap->free_list = 0;
    heap->available = (size_t)heap->start - (size_t)base;
    heap->available = length - heap->available;
    heap->available = heap->available & ~(sizeof(xHeapChunk_t) - 1);
    heap->max = (size_t)heap->start + heap->available;
    heap->start->prev_size = 0;
    heap->start->chunk_size = heap->available;
    heap->flags |= 0x01;
    heap->lock = 0;
    alloc_add_to_free (heap, heap->start);
}

canonical_original_code:
void meminit_r(struct xHeapArea *heap, void *base, unsigned long length)
{
    heap->flags |= 0x04;
    heap->start = ( struct xHeapChunk * ) ((((unsigned long long)base)+(sizeof(xHeapChunk_t)-1))&(~(sizeof(xHeapChunk_t)-1)));
    heap->free_list = 0;
    heap->available = (unsigned long)heap->start - (unsigned long)base;
    heap->available = length - heap->available;
    heap->available = heap->available & ~(sizeof(xHeapChunk_t) - 1);
    heap->max = (unsigned long)heap->start + heap->available;
    heap->start->prev_size = 0;
    heap->start->chunk_size = heap->available;
    heap->flags |= 0x01;
    heap->lock = 0;
    alloc_add_to_free (heap, heap->start);
}

variable_types:
base
void *

variable_types:
length
unsigned long


original_code:
int memcorrupt_r (xHeapArea_t* heap)
{
    int err = 0;
    int free_chunks = 0;
    int total_chunks = 0;
    uintmax_t lsize = 0;
    xHeapChunk_t* chunk = heap->free_list;
    xHeapChunk_t* prev = 0;
    while (chunk != 0) {
        free_chunks++;
        if (chunk->is_used) {
            ((void)(0));
            err++;
        }
        if (chunk->prev_chunk != prev) {
            ((void)(0));
            err++;
        }
        if (chunk->chunk_size < lsize) {
            ((void)(0));
            err++;
        }
        lsize = chunk->chunk_size;
        prev = chunk;
        chunk = chunk->next_chunk;
    }
    chunk = heap->start;
    lsize = 0;
    while ((size_t)chunk < heap->max) {
        total_chunks++;
        if (chunk->is_used) {
        } else {
            free_chunks--;
            if ((uint32_t)chunk->next_chunk & 7)
                err++;
            if ((uint32_t)chunk->prev_chunk & 7)
                err++;
        }
        if (chunk->prev_size != lsize) {
            ((void)(0));
            err++;
        }
        lsize = chunk->chunk_size;
        chunk = (xHeapChunk_t*)((size_t)chunk + (size_t)chunk->chunk_size);
    }
    if (free_chunks != 0) {
        ((void)(0));
        err++;
    }
    if ((size_t)chunk != heap->max) {
        ((void)(0));
        err++;
    }
    return (err);
}

canonical_original_code:
int memcorrupt_r(struct xHeapArea *heap)
{
    int err = 0;
    int free_chunks = 0;
    int total_chunks = 0;
    unsigned long long lsize = 0;
    struct xHeapChunk *chunk = heap->free_list;
    struct xHeapChunk *prev = 0;
    while (chunk != 0) {
        free_chunks++;
        if (chunk->is_used) {
            ((void)(0));
            err++;
        }
        if (chunk->prev_chunk != prev) {
            ((void)(0));
            err++;
        }
        if (chunk->chunk_size < lsize) {
            ((void)(0));
            err++;
        }
        lsize = chunk->chunk_size;
        prev = chunk;
        chunk = chunk->next_chunk;
    }
    chunk = heap->start;
    lsize = 0;
    while ((unsigned long)chunk < heap->max) {
        total_chunks++;
        if (chunk->is_used) {
        } else {
            free_chunks--;
            if ((unsigned int)chunk->next_chunk & 7)
                err++;
            if ((unsigned int)chunk->prev_chunk & 7)
                err++;
        }
        if (chunk->prev_size != lsize) {
            ((void)(0));
            err++;
        }
        lsize = chunk->chunk_size;
        chunk = (struct xHeapChunk *)((unsigned long)chunk + (unsigned long)chunk->chunk_size);
    }
    if (free_chunks != 0) {
        ((void)(0));
        err++;
    }
    if ((unsigned long)chunk != heap->max) {
        ((void)(0));
        err++;
    }
    return (err);
}

variable_types: err int

variable_types:
lsize
unsigned long long

variable_types: struct xHeapChunk { unsigned int prev_size; unsigned int reserved; unsigned int chunk_size; unsigned int is_used; union { struct { struct xHeapChunk *prev_chunk; struct xHeapChunk *next_chunk; }; unsigned int data[4]; }; } *

return_type: int


original_code:
void free_r(xHeapArea_t* heap, void* ptr)
{
    xHeapChunk_t* chunk = ((xHeapChunk_t*)((uint32_t)(ptr) - (sizeof (uint32_t)*4)));
    xHeapChunk_t* prev = 0;
    xHeapChunk_t* next = 0;
    if (heap->flags & 0x04) {
        if (memcorrupt_r(heap)) {
            ((void)(0));
        }
    }
    if ((size_t)chunk < (size_t)heap->start || (size_t)chunk > heap->max) {
        return;
    }
    ((void)(0));
    prev = (xHeapChunk_t*)((size_t)chunk - (size_t)chunk->prev_size);
    next = (xHeapChunk_t*)((size_t)chunk + (size_t)chunk->chunk_size);
    if (heap->flags & 0x01) {
        if (!chunk->is_used)
            heap->flags |= 0x02;
        if (prev != chunk && prev->chunk_size != chunk->prev_size)
            heap->flags |= 0x02;
        if ((size_t)next < heap->max && next->prev_size != chunk->chunk_size)
            heap->flags |= 0x02;
        if (heap->flags & 0x02) {
            ((void)(0));
            if (heap->flags & 0x04) {
                if (memcorrupt_r(heap)) {
                    ((void)(0));
                }
            }
            return;
        }
    }
    heap->available += chunk->chunk_size;
    if (prev != chunk && !prev->is_used) {
        alloc_rem_of_free(heap, prev);
        prev->chunk_size += chunk->chunk_size;
        if ((size_t)next < heap->max)
            next->prev_size = prev->chunk_size;
        chunk = prev;
    }
    if ((size_t)next < heap->max && !next->is_used) {
        alloc_rem_of_free(heap, next);
        chunk->chunk_size += next->chunk_size;
        next = (xHeapChunk_t*)((size_t)chunk + (size_t)chunk->chunk_size);
        if ((size_t)next < heap->max)
            next->prev_size = chunk->chunk_size;
    }
    alloc_add_to_free (heap, chunk);
    if (heap->flags & 0x04) {
        if (memcorrupt_r(heap)) {
            ((void)(0));
        }
    }
    ((void)(0));
}

canonical_original_code:
void free_r(struct xHeapArea *heap, void *ptr)
{
    struct xHeapChunk *chunk = ((xHeapChunk_t*)((uint32_t)(ptr) - (sizeof (uint32_t)*4)));
    struct xHeapChunk *prev = 0;
    struct xHeapChunk *next = 0;
    if (heap->flags & 0x04) {
        if (memcorrupt_r(heap)) {
            ((void)(0));
        }
    }
    if ((unsigned long)chunk < (unsigned long)heap->start || (unsigned long)chunk > heap->max) {
        return;
    }
    ((void)(0));
    prev = (struct xHeapChunk *)((unsigned long)chunk - (unsigned long)chunk->prev_size);
    next = (struct xHeapChunk *)((unsigned long)chunk + (unsigned long)chunk->chunk_size);
    if (heap->flags & 0x01) {
        if (!chunk->is_used)
            heap->flags |= 0x02;
        if (prev != chunk && prev->chunk_size != chunk->prev_size)
            heap->flags |= 0x02;
        if ((unsigned long)next < heap->max && next->prev_size != chunk->chunk_size)
            heap->flags |= 0x02;
        if (heap->flags & 0x02) {
            ((void)(0));
            if (heap->flags & 0x04) {
                if (memcorrupt_r(heap)) {
                    ((void)(0));
                }
            }
            return;
        }
    }
    heap->available += chunk->chunk_size;
    if (prev != chunk && !prev->is_used) {
        alloc_rem_of_free(heap, prev);
        prev->chunk_size += chunk->chunk_size;
        if ((unsigned long)next < heap->max)
            next->prev_size = prev->chunk_size;
        chunk = prev;
    }
    if ((unsigned long)next < heap->max && !next->is_used) {
        alloc_rem_of_free(heap, next);
        chunk->chunk_size += next->chunk_size;
        next = (struct xHeapChunk *)((unsigned long)chunk + (unsigned long)chunk->chunk_size);
        if ((unsigned long)next < heap->max)
            next->prev_size = chunk->chunk_size;
    }
    alloc_add_to_free (heap, chunk);
    if (heap->flags & 0x04) {
        if (memcorrupt_r(heap)) {
            ((void)(0));
        }
    }
    ((void)(0));
}

variable_types:
ptr
void *


original_code:
void* malloc_r(xHeapArea_t* heap, size_t size)
{
    xHeapChunk_t* chunk = heap->free_list;
    xHeapChunk_t* split = 0;
    xHeapChunk_t* prev = 0;
    xHeapChunk_t* next = 0;
    size_t lsize = 0;
    ((void)(0));
    if (heap->flags & 0x04) {
        if (memcorrupt_r(heap)) {
            ((void)(0));
        }
    }
    if (size > 0x30000000 - sizeof(xHeapChunk_t)) {
        ((void)(0));
        return 0;
    }
    size = (((size + (sizeof (uint32_t)*4))+(sizeof(xHeapChunk_t)-1))&(~(sizeof(xHeapChunk_t)-1)));
    if (size < sizeof(xHeapChunk_t)) size = sizeof(xHeapChunk_t);
    while ( chunk ) {
        if (heap->flags & 0x01) {
            if (chunk->is_used)
                heap->flags |= 0x02;
            if (chunk->prev_chunk != prev)
                heap->flags |= 0x02;
            if (chunk->chunk_size < lsize)
                heap->flags |= 0x02;
            if (heap->flags & 0x02) {
                ((void)(0));
                return 0;
            }
            lsize = chunk->chunk_size;
            prev = chunk;
        }
        if ( chunk->chunk_size >= size) {
            alloc_rem_of_free ( heap, chunk );
            if (chunk->chunk_size >= size + sizeof(xHeapChunk_t) ) {
                split = ( xHeapChunk_t* ) ((( size_t ) chunk ) + size);
                split->chunk_size = chunk->chunk_size - size;
                split->prev_size = size;
                next = (xHeapChunk_t*)((size_t)split + (size_t)split->chunk_size);
                if ((size_t)next < heap->max)
                    next->prev_size = split->chunk_size;
                else if ((heap->flags & 0x01) && (size_t)next != heap->max) {
                    heap->flags |= 0x02;
                    ((void)(0));
                    return 0;
                }
                alloc_add_to_free ( heap, split );
                chunk->chunk_size = size;
                chunk->is_used |= 0x01;
            }
            heap->available -= size;
            ((void)(0));
            if (heap->flags & 0x04) {
                if (memcorrupt_r(heap)) {
                    ((void)(0));
                }
            }
            return (chunk->data);
        }
        chunk = chunk->next_chunk;
    }
    ((void)(0));
    (*_geterrno()) = 12;
    if (heap->flags & 0x04) {
        if (memcorrupt_r(heap)) {
            ((void)(0));
        }
    }
    return 0;
}

canonical_original_code:
void *malloc_r(struct xHeapArea *heap, unsigned long size)
{
    struct xHeapChunk *chunk = heap->free_list;
    struct xHeapChunk *split = 0;
    struct xHeapChunk *prev = 0;
    struct xHeapChunk *next = 0;
    unsigned long lsize = 0;
    ((void)(0));
    if (heap->flags & 0x04) {
        if (memcorrupt_r(heap)) {
            ((void)(0));
        }
    }
    if (size > 0x30000000 - sizeof(xHeapChunk_t)) {
        ((void)(0));
        return 0;
    }
    size = (((size + (sizeof (unsigned int)*4))+(sizeof(xHeapChunk_t)-1))&(~(sizeof(xHeapChunk_t)-1)));
    if (size < sizeof(xHeapChunk_t)) size = sizeof(xHeapChunk_t);
    while ( chunk ) {
        if (heap->flags & 0x01) {
            if (chunk->is_used)
                heap->flags |= 0x02;
            if (chunk->prev_chunk != prev)
                heap->flags |= 0x02;
            if (chunk->chunk_size < lsize)
                heap->flags |= 0x02;
            if (heap->flags & 0x02) {
                ((void)(0));
                return 0;
            }
            lsize = chunk->chunk_size;
            prev = chunk;
        }
        if ( chunk->chunk_size >= size) {
            alloc_rem_of_free ( heap, chunk );
            if (chunk->chunk_size >= size + sizeof(xHeapChunk_t) ) {
                split = ( struct xHeapChunk * ) ((( unsigned long ) chunk ) + size);
                split->chunk_size = chunk->chunk_size - size;
                split->prev_size = size;
                next = (struct xHeapChunk *)((unsigned long)split + (unsigned long)split->chunk_size);
                if ((unsigned long)next < heap->max)
                    next->prev_size = split->chunk_size;
                else if ((heap->flags & 0x01) && (unsigned long)next != heap->max) {
                    heap->flags |= 0x02;
                    ((void)(0));
                    return 0;
                }
                alloc_add_to_free ( heap, split );
                chunk->chunk_size = size;
                chunk->is_used |= 0x01;
            }
            heap->available -= size;
            ((void)(0));
            if (heap->flags & 0x04) {
                if (memcorrupt_r(heap)) {
                    ((void)(0));
                }
            }
            return (chunk->data);
        }
        chunk = chunk->next_chunk;
    }
    ((void)(0));
    (*_geterrno()) = 12;
    if (heap->flags & 0x04) {
        if (memcorrupt_r(heap)) {
            ((void)(0));
        }
    }
    return 0;
}

variable_types:
size
unsigned long

variable_types:
lsize
unsigned long

return_type: void *


vfscanf

decompiled_code:
__int64 vfscanf()
{
_DWORD *v0; // rax

++_gcov0_vfscanf;
v0 = (_DWORD *)geterrno();
++qword_138;
*v0 = 38;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
_DWORD *v0; // rax

++_gcov0_vfscanf;
v0 = (_DWORD *)geterrno();
++qword_138;
*v0 = 38;
return 0xFFFFFFFFLL;
}

original_code:
int vfscanf(FILE * f, const char * format, va_list ap)
{
  (*_geterrno()) = 38;
  return -1;
}

canonical_original_code:
int vfscanf(struct axFile *f, char *format, char *ap)
{
  (*_geterrno()) = 38;
  return -1;
}

variable_types: struct axFile { int fd_; int oflags_; struct axFile *next_; int lock_; int count_; char lbuf_; char *buf_; long off_; long lpos_; unsigned long bsize_; char *rpos_; char *rend_; char *wpos_; char *wend_; int (*write_)(struct axFile *fp, char *buf, unsigned long length); int (*read_)(struct axFile *fp, char *buf, unsigned long length); } *

variable_types:
format
char *

variable_types:
ap
char *


original_code:
int axDigest_print (axDigestContext_t* ctx, char* str, size_t lg)
{
  uint8_t* ptr = (uint8_t*)ctx->_digest;
  unsigned i, j = 0;
  if (lg == 0) return -1;
  for (i = 0; i < ctx->_digestLg; i++) {
    if (lg < 3) {
      str[j] = '\0';
      return -1;
    }
    str[j] = ((ptr[i] >> 4) & 0xf);
    str[j] += (str[j] < 10) ? '0' : 'a'-10;
    j++;
    str[j] = ((ptr[i] >> 0) & 0xf);
    str[j] += (str[j] < 10) ? '0' : 'a'-10;
    j++;
    lg-=2;
  }
  str[j] = '\0';
  return ctx->_digestLg;
}

canonical_original_code:
int axDigest_print(struct axDigestContext *ctx, char *str, unsigned long lg)
{
  unsigned char *ptr = (uint8_t*)ctx->_digest;
  unsigned int i; unsigned int j = 0;
  if (lg == 0) return -1;
  for (i = 0; i < ctx->_digestLg; i++) {
    if (lg < 3) {
      str[j] = '\0';
      return -1;
    }
    str[j] = ((ptr[i] >> 4) & 0xf);
    str[j] += (str[j] < 10) ? '0' : 'a'-10;
    j++;
    str[j] = ((ptr[i] >> 0) & 0xf);
    str[j] += (str[j] < 10) ? '0' : 'a'-10;
    j++;
    lg-=2;
  }
  str[j] = '\0';
  return ctx->_digestLg;
}

variable_types: struct axDigestContext { unsigned int _digestLg; unsigned int _digest[]; } *

variable_types:
str
char *

variable_types:
lg
unsigned long

variable_types:
ptr
unsigned char *

variable_types:
i
unsigned int

variable_types:
j
unsigned int


fclose

decompiled_code:
__int64 __fastcall fclose(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // r12
__int64 result; // rax
bool v4; // zf

if ( OFP_HEAD == a1 )
{
if ( !a1 )
{
LABEL_6:
*(_DWORD *)geterrno() = 22;
return 0xFFFFFFFFLL;
}
v1 = a1;
while ( 1 )
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 8);
if ( a1 != v1 )
break;
if ( !v1 )
goto LABEL_6;
}
flockfile((FILE *)v1);
v4 = (*(_DWORD *)(v1 + 4) & 3) == 0;
*(_DWORD *)(v1 + 4) &= 3u;
if ( v4 || (result = flush_cache(v1), (_DWORD)result != -1) )
{
close(*(_DWORD *)v1);
*(_QWORD *)(v2 + 8) = *(_QWORD *)(v1 + 8);
LABEL_9:
mtx_destroy(v1 + 16);
free((void *)v1);
return 0LL;
}
}
else
{
flockfile((FILE *)a1);
v4 = (*(_DWORD *)(a1 + 4) & 3) == 0;
*(_DWORD *)(a1 + 4) &= 3u;
if ( v4 || (result = flush_cache(a1), (_DWORD)result != -1) )
{
close(*(_DWORD *)a1);
OFP_HEAD = *(_QWORD *)(a1 + 8);
v1 = a1;
goto LABEL_9;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // r12
__int64 result; // rax
bool v4; // zf

if ( OFP_HEAD == a1 )
{
if ( !a1 )
{
LABEL_6:
*(_DWORD *)geterrno() = 22;
return 0xFFFFFFFFLL;
}
v1 = a1;
while ( 1 )
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 8);
if ( a1 != v1 )
break;
if ( !v1 )
goto LABEL_6;
}
flockfile((FILE *)v1);
v4 = (*(_DWORD *)(v1 + 4) & 3) == 0;
*(_DWORD *)(v1 + 4) &= 3u;
if ( v4 || (result = flush_cache(v1), (_DWORD)result != -1) )
{
close(*(_DWORD *)v1);
*(_QWORD *)(v2 + 8) = *(_QWORD *)(v1 + 8);
LABEL_9:
mtx_destroy(v1 + 16);
free((void *)v1);
return 0LL;
}
}
else
{
flockfile((FILE *)a1);
v4 = (*(_DWORD *)(a1 + 4) & 3) == 0;
*(_DWORD *)(a1 + 4) &= 3u;
if ( v4 || (result = flush_cache(a1), (_DWORD)result != -1) )
{
close(*(_DWORD *)a1);
OFP_HEAD = *(_QWORD *)(a1 + 8);
v1 = a1;
goto LABEL_9;
}
}
return result;
}

original_code:
int fclose (FILE* fp)
{
  FILE* hp = OFP_HEAD;
  FILE* pp = 0;
  while (fp == hp) {
    if (fp == 0) {
      (*_geterrno()) = 22;
      return (-1);
    }
    pp = fp;
    fp = fp->next_;
  }
  flockfile (fp);
  if (fp->oflags_ &= (1 | 2)) {
    if (flush_cache (fp) == (-1)) {
      return (-1);
    }
  }
  close (fp->fd_);
  if (pp != 0) {
    pp->next_ = fp->next_;
  } else {
    OFP_HEAD = fp->next_;
  }
  mtx_destroy( &fp->lock_ );
  free(fp);
  return 0;
}

canonical_original_code:
int fclose(struct axFile *fp)
{
  struct axFile *hp = OFP_HEAD;
  struct axFile *pp = 0;
  while (fp == hp) {
    if (fp == 0) {
      (*_geterrno()) = 22;
      return (-1);
    }
    pp = fp;
    fp = fp->next_;
  }
  flockfile (fp);
  if (fp->oflags_ &= (1 | 2)) {
    if (flush_cache (fp) == (-1)) {
      return (-1);
    }
  }
  close (fp->fd_);
  if (pp != 0) {
    pp->next_ = fp->next_;
  } else {
    OFP_HEAD = fp->next_;
  }
  mtx_destroy( &fp->lock_ );
  free(fp);
  return 0;
}

user_defined_types: struct axFile { int fd_; int oflags_; struct axFile *next_; int lock_; int count_; char lbuf_; char *buf_; long off_; long lpos_; unsigned long bsize_; char *rpos_; char *rend_; char *wpos_; char *wend_; int (*write_)(struct axFile *fp, char *buf, unsigned long length); int (*read_)(struct axFile *fp, char *buf, unsigned long length); }


atexit

decompiled_code:
__int64 __fastcall atexit(__int64 a1)
{
if ( !atexitPtr )
return 0xFFFFFFFFLL;
atexitStack[--atexitPtr] = a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
if ( !atexitPtr )
return 0xFFFFFFFFLL;
atexitStack[--atexitPtr] = a1;
return 0LL;
}

original_code:
int atexit( void (*func)( void ) )
{
  if (_atexitPtr == 0) {
    return -1;
  }
  _atexitStack[ --_atexitPtr ] = func;
  return 0;
}

canonical_original_code:
int atexit(void (*func)())
{
  if (_atexitPtr == 0) {
    return -1;
  }
  _atexitStack[ --_atexitPtr ] = func;
  return 0;
}

variable_types:
func
void (*)()


exit

decompiled_code:
void __fastcall __noreturn exit(int status)
{
__int64 i; // rax

for ( i = atexitPtr; atexitPtr <= 0x1FuLL; i = atexitPtr )
{
atexitPtr = i + 1;
((void (*)(void))atexitStack[i])();
}
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(int status)
{
__int64 i; // rax

for ( i = atexitPtr; atexitPtr <= 0x1FuLL; i = atexitPtr )
{
atexitPtr = i + 1;
((void (*)(void))atexitStack[i])();
}
func1(status);
}

original_code:
void exit (int status)
{
  while (_atexitPtr < 32)
  {
      _atexitStack[_atexitPtr++]();
  }
  _exit(status);
}

canonical_original_code:
void exit(int status)
{
  while (_atexitPtr < 32)
  {
      _atexitStack[_atexitPtr++]();
  }
  _exit(status);
}

variable_types: status int


abort

decompiled_code:
void __noreturn abort()
{
raise(6);
exit(-1);
}

canonical_decompiled_code:
void __noreturn func2()
{
raise(6);
func1(-1);
}

original_code:
void abort (void)
{
  raise(6);
  exit((-1));
}

canonical_original_code:
void abort()
{
  raise(6);
  exit((-1));
}


repo: BHYCHIK/ftor

decompiled_code:
__sighandler_t set_signals()
{
signal(13, (__sighandler_t)((char *)set_signals + 1));
signal(2, (__sighandler_t)&stop_server);
return signal(15, (__sighandler_t)&stop_server);
}

canonical_decompiled_code:
__sighandler_t func0()
{
signal(13, (__sighandler_t)((char *)set_signals + 1));
signal(2, (__sighandler_t)&stop_server);
return signal(15, (__sighandler_t)&stop_server);
}

original_code:
static void set_signals() {
    signal(13, ((__sighandler_t) 1));
    signal(2, stop_server);
    signal(15, stop_server);
}

canonical_original_code:
static void set_signals() {
    signal(13, ((void (*)(int)) 1));
    signal(2, stop_server);
    signal(15, stop_server);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
set_signals();
ftor_reactor_init(argc, argv);
ftor_start_server();
ftor_reactor();
rsa_cleanup();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func0();
ftor_reactor_init(argc, argv);
ftor_start_server();
ftor_reactor();
rsa_cleanup();
return 0;
}

original_code:
int main() {
    set_signals();
    ftor_reactor_init();
    ftor_start_server();
    ftor_reactor();
    rsa_cleanup();
    return 0;
}


decompiled_code:
__int64 get_total_events()
{
return (unsigned int)total_events;
}

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

original_code:
int get_total_events() {
    return total_events;
}


original_code:
struct ftor_context *ftor_create_context() {
    struct ftor_context *context = malloc(sizeof(struct ftor_context));
    context->state = conn_none_state;
    context->incoming_fd = -1;
    context->terminated = 0;
    context->pool = ftor_pool_get();
    context->client_recv_buffer = malloc(1);
    context->client_recv_buffer_size = 1;
    context->client_recv_buffer_pos = 0;
    context->chain_recv_buffer = malloc(1);
    context->chain_recv_buffer_size = 1;
    context->chain_recv_buffer_pos = 0;
    context->client_addr_len = sizeof(context->client_addr);
    context->client_event = ((void *)0);
    context->chain_domain_name1 = ((void *)0);
    context->chain_domain_name2 = ((void *)0);
    context->chain_pubkey1 = ((void *)0);
    context->chain_pubkey2 = ((void *)0);
    context->chain_ip1 = 0;
    context->chain_ip2 = 0;
    context->events_num = 0;
    context->client_eof = 0;
    context->chain_eof = 0;
    context->next_ip = 0;
    context->next_port = 0;
    context->node_flags = 0;
    context->header_bytes_received = 0;
    context->forward_cipher_offset = 0;
    context->backward_cipher_offset = 0;
    memset(&context->client_addr, 0, context->client_addr_len);
    memset(&context->sesskey1, 0, sizeof(context->sesskey1));
    memset(&context->sesskey2, 0, sizeof(context->sesskey2));
    memset(&context->node_sesskey, 0, sizeof(context->node_sesskey));
    return context;
}

variable_types: struct ftor_context { int incoming_fd; _Bool terminated; struct mem_pool { void *memory_start; void *current_pos; unsigned long space_left; struct mem_pool *next; } *pool; enum ftor_state { conn_none_state=0, socks_header_received_state=1 } state; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client_addr; unsigned int client_addr_len; unsigned char *client_recv_buffer; unsigned long client_recv_buffer_size; unsigned long client_recv_buffer_pos; unsigned char *chain_recv_buffer; unsigned long chain_recv_buffer_size; unsigned long chain_recv_buffer_pos; unsigned short peer_port; unsigned int peer_address; struct ftor_event { struct ftor_context *context; int (*read_handler)(struct ftor_event *event); int (*write_handler)(struct ftor_event *event); int (*destuction_handler)(struct ftor_event *event); int socket_fd; unsigned char *recv_buffer; unsigned long recv_buffer_size; unsigned long recv_buffer_pos; unsigned char *send_buffer; unsigned long send_buffer_size; unsigned long send_buffer_pos; } *client_event; char *chain_domain_name1; char *chain_domain_name2; char *chain_pubkey1; char *chain_pubkey2; unsigned int chain_ip1; unsigned int chain_ip2; unsigned char sesskey1[128]; unsigned char sesskey2[128]; int events_num; _Bool client_eof; _Bool chain_eof; unsigned char node_sesskey[128]; unsigned int next_ip; unsigned short next_port; unsigned int node_flags; int header_bytes_received; int forward_cipher_offset; int backward_cipher_offset; } *

user_defined_types: struct ftor_context { int incoming_fd; _Bool terminated; struct mem_pool *pool; enum ftor_state state; struct sockaddr_in client_addr; unsigned int client_addr_len; unsigned char *client_recv_buffer; unsigned long client_recv_buffer_size; unsigned long client_recv_buffer_pos; unsigned char *chain_recv_buffer; unsigned long chain_recv_buffer_size; unsigned long chain_recv_buffer_pos; unsigned short peer_port; unsigned int peer_address; struct ftor_event *client_event; char *chain_domain_name1; char *chain_domain_name2; char *chain_pubkey1; char *chain_pubkey2; unsigned int chain_ip1; unsigned int chain_ip2; unsigned char sesskey1[128]; unsigned char sesskey2[128]; int events_num; _Bool client_eof; _Bool chain_eof; unsigned char node_sesskey[128]; unsigned int next_ip; unsigned short next_port; unsigned int node_flags; int header_bytes_received; int forward_cipher_offset; int backward_cipher_offset; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }


decompiled_code:
void __fastcall ftor_del_context(void *ptr)
{
puts("Context deleted");
ftor_free(*((_QWORD *)ptr + 1));
free(*((void **)ptr + 5));
free(*((void **)ptr + 8));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
puts("Context deleted");
ftor_free(*((_QWORD *)ptr + 1));
free(*((void **)ptr + 5));
free(*((void **)ptr + 8));
free(ptr);
}

original_code:
void ftor_del_context(struct ftor_context *context) {
    printf("Context deleted\n");
    ftor_free(context->pool);
    free(context->client_recv_buffer);
    free(context->chain_recv_buffer);
    free(context);
}

user_defined_types: enum ftor_state { conn_none_state=0, socks_header_received_state=1 }

user_defined_types: struct mem_pool { void *memory_start; void *current_pos; unsigned long space_left; struct mem_pool *next; }

user_defined_types: struct ftor_event { struct ftor_context *context; int (*read_handler)(struct ftor_event *event); int (*write_handler)(struct ftor_event *event); int (*destuction_handler)(struct ftor_event *event); int socket_fd; unsigned char *recv_buffer; unsigned long recv_buffer_size; unsigned long recv_buffer_pos; unsigned char *send_buffer; unsigned long send_buffer_size; unsigned long send_buffer_pos; }


decompiled_code:
void __fastcall ftor_del_event(void *ptr)
{
void (*v2)(void); // rax
int v3; // edi
_DWORD *v4; // rdx
int v5; // eax

v2 = (void (*)(void))*((_QWORD *)ptr + 3);
if ( v2 )
v2();
--total_events;
v3 = *((_DWORD *)ptr + 8);
if ( v3 >= 0 )
close(v3);
v4 = *(_DWORD **)ptr;
if ( *(_QWORD *)ptr )
{
v5 = v4[100] - 1;
v4[100] = v5;
if ( !v5 )
ftor_del_context(*(void **)ptr);
}
free(*((void **)ptr + 8));
free(*((void **)ptr + 5));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
void (*v2)(void); // rax
int v3; // edi
_DWORD *v4; // rdx
int v5; // eax

v2 = (void (*)(void))*((_QWORD *)ptr + 3);
if ( v2 )
v2();
--total_events;
v3 = *((_DWORD *)ptr + 8);
if ( v3 >= 0 )
close(v3);
v4 = *(_DWORD **)ptr;
if ( *(_QWORD *)ptr )
{
v5 = v4[100] - 1;
v4[100] = v5;
if ( !v5 )
func2(*(void **)ptr);
}
free(*((void **)ptr + 8));
free(*((void **)ptr + 5));
free(ptr);
}

original_code:
void ftor_del_event(struct ftor_event *event) {
    if (event->destuction_handler) event->destuction_handler(event);
    --total_events;
    if (event->socket_fd > -1) close(event->socket_fd);
    if (event->context) {
        if ((--event->context->events_num) == 0) {
            ftor_del_context(event->context);
        }
    }
    free(event->send_buffer);
    free(event->recv_buffer);
    free(event);
}

variable_types: struct ftor_event { struct ftor_context { int incoming_fd; _Bool terminated; struct mem_pool { void *memory_start; void *current_pos; unsigned long space_left; struct mem_pool *next; } *pool; enum ftor_state { conn_none_state=0, socks_header_received_state=1 } state; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client_addr; unsigned int client_addr_len; unsigned char *client_recv_buffer; unsigned long client_recv_buffer_size; unsigned long client_recv_buffer_pos; unsigned char *chain_recv_buffer; unsigned long chain_recv_buffer_size; unsigned long chain_recv_buffer_pos; unsigned short peer_port; unsigned int peer_address; struct ftor_event *client_event; char *chain_domain_name1; char *chain_domain_name2; char *chain_pubkey1; char *chain_pubkey2; unsigned int chain_ip1; unsigned int chain_ip2; unsigned char sesskey1[128]; unsigned char sesskey2[128]; int events_num; _Bool client_eof; _Bool chain_eof; unsigned char node_sesskey[128]; unsigned int next_ip; unsigned short next_port; unsigned int node_flags; int header_bytes_received; int forward_cipher_offset; int backward_cipher_offset; } *context; int (*read_handler)(struct ftor_event *event); int (*write_handler)(struct ftor_event *event); int (*destuction_handler)(struct ftor_event *event); int socket_fd; unsigned char *recv_buffer; unsigned long recv_buffer_size; unsigned long recv_buffer_pos; unsigned char *send_buffer; unsigned long send_buffer_size; unsigned long send_buffer_pos; } *


original_code:
ssize_t ftor_read_all(int fd, unsigned char **buf, size_t *pos, size_t *alloced, _Bool *eof, _Bool *error) {
    *eof = 0;
    *error = 0;
    char tmp_buf[1];
    ssize_t bytes_readed = 0;
    ssize_t total_readed = 0;
    do {
        bytes_readed = recv(fd, tmp_buf, sizeof(tmp_buf), MSG_DONTWAIT);
        if (bytes_readed == 0) {
            *eof = 1;
            break;
        }
        if (bytes_readed < 0) {
            if ((*__errno_location ()) != 11) {
                *error = 1;
            }
            break;
        }
        if ((ssize_t)*alloced < (*buf +(int)(*pos)) - *buf + bytes_readed) {
            *alloced *= 2;
            *buf = realloc(*buf, *alloced);
        }
        memcpy(*buf + *pos, tmp_buf, bytes_readed);
        *pos += bytes_readed;
        total_readed += bytes_readed;
    } while (bytes_readed > 0);
    return total_readed;
}

canonical_original_code:
long ftor_read_all(int fd, unsigned char **buf, unsigned long *pos, unsigned long *alloced, _Bool *eof, _Bool *error) {
    *eof = 0;
    *error = 0;
    char tmp_buf[1];
    long bytes_readed = 0;
    long total_readed = 0;
    do {
        bytes_readed = recv(fd, tmp_buf, sizeof(tmp_buf), MSG_DONTWAIT);
        if (bytes_readed == 0) {
            *eof = 1;
            break;
        }
        if (bytes_readed < 0) {
            if ((*__errno_location ()) != 11) {
                *error = 1;
            }
            break;
        }
        if ((long)*alloced < (*buf +(int)(*pos)) - *buf + bytes_readed) {
            *alloced *= 2;
            *buf = realloc(*buf, *alloced);
        }
        memcpy(*buf + *pos, tmp_buf, bytes_readed);
        *pos += bytes_readed;
        total_readed += bytes_readed;
    } while (bytes_readed > 0);
    return total_readed;
}

variable_types: fd int

variable_types:
buf
unsigned char **

variable_types:
pos
unsigned long *

variable_types:
alloced
unsigned long *

variable_types:
eof
_Bool *

variable_types:
error
_Bool *

return_type: long


decompiled_code:
ssize_t __fastcall ftor_read_data_to_buffer(int a1, __int64 a2, int *a3, size_t a4, _DWORD *a5, char a6)
{
ssize_t result; // rax

if ( a6 )
result = read(a1, (void *)((int)*(_QWORD *)a3 + a2), a4 - *(_QWORD *)a3);
else
result = read(a1, (void *)(*a3 + a2), a4);
if ( a5 && result == -1 || a5 )
*a5 = 0;
*(_QWORD *)a3 += result;
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func5(int a1, __int64 a2, int *a3, size_t a4, _DWORD *a5, char a6)
{
ssize_t result; // rax

if ( a6 )
result = read(a1, (void *)((int)*(_QWORD *)a3 + a2), a4 - *(_QWORD *)a3);
else
result = read(a1, (void *)(*a3 + a2), a4);
if ( a5 && result == -1 || a5 )
*a5 = 0;
*(_QWORD *)a3 += result;
return result;
}

original_code:
ssize_t ftor_read_data_to_buffer(int fd, unsigned char *buf, size_t *pos, size_t size, enum read_result *eval, _Bool read_all) {
    ssize_t readed = 0;
    if (!read_all) {
        readed = read(fd, buf + (int)(*pos), size);
    }
    else {
        readed = read(fd, buf + (int)(*pos), read_all ? size - *pos : size);
    }
    if (readed == -1) {
        if (eval) *eval = read_result_err;
    }
    if (eval) *eval = read_result_ok;
    *pos += (size_t)readed;
    return readed;
}

canonical_original_code:
long ftor_read_data_to_buffer(int fd, unsigned char *buf, unsigned long *pos, unsigned long size, enum read_result *eval, _Bool read_all) {
    long readed = 0;
    if (!read_all) {
        readed = read(fd, buf + (int)(*pos), size);
    }
    else {
        readed = read(fd, buf + (int)(*pos), read_all ? size - *pos : size);
    }
    if (readed == -1) {
        if (eval) *eval = read_result_err;
    }
    if (eval) *eval = read_result_ok;
    *pos += (unsigned long)readed;
    return readed;
}

variable_types:
buf
unsigned char *

variable_types:
size
unsigned long

variable_types: enum read_result { read_result_ok=0, read_result_err=1 } *

variable_types: readed long


decompiled_code:
__int64 __fastcall incomming_connection_event_destructor(__int64 a1)
{
incomming_connection_event = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
incomming_connection_event = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
return 0LL;
}

original_code:
static int incomming_connection_event_destructor(struct ftor_event *event) {
    incomming_connection_event = ((void *)0);
    event->destuction_handler = ((void *)0);
    return 0;
}


decompiled_code:
__int64 __fastcall node_connection_event_destructor(__int64 a1)
{
node_connection_event = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
node_connection_event = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
return 0LL;
}

original_code:
static int node_connection_event_destructor(struct ftor_event *event) {
    node_connection_event = ((void *)0);
    event->destuction_handler = ((void *)0);
    return 0;
}


decompiled_code:
void stop_server()
{
running_server = 0;
}

canonical_decompiled_code:
void func4()
{
running_server = 0;
}


decompiled_code:
int ftor_reactor()
{
__int64 v0; // r14
void *v1; // rsp
int result; // eax
int v3; // eax
struct epoll_event *v4; // rbx
struct epoll_event *v5; // r12
struct epoll_event v6[4]; // [rsp+0h] [rbp-30h] BYREF

v0 = get_conf();
v1 = alloca(12LL * *(int *)(v0 + 24588));
while ( 1 )
{
if ( !running_server )
{
result = get_total_events();
if ( !result )
break;
}
v3 = epoll_wait(epoll_fd, v6, *(_DWORD *)(v0 + 24588), 20);
if ( v3 > 0 )
{
v4 = v6;
v5 = &v6[(unsigned int)(v3 - 1) + 1];
do
{
process_event((__int64 *)v4->data.ptr, v4->events);
++v4;
}
while ( v4 != v5 );
}
if ( !running_server )
{
if ( !incomming_connection_event || (ftor_del_event(incomming_connection_event), !running_server) )
{
if ( node_connection_event )
ftor_del_event(node_connection_event);
}
}
}
if ( random_fd != -1 )
result = close(random_fd);
return result;
}

canonical_decompiled_code:
int func5()
{
__int64 v0; // r14
void *v1; // rsp
int result; // eax
int v3; // eax
struct epoll_event *v4; // rbx
struct epoll_event *v5; // r12
struct epoll_event v6[4]; // [rsp+0h] [rbp-30h] BYREF

v0 = get_conf();
v1 = alloca(12LL * *(int *)(v0 + 24588));
while ( 1 )
{
if ( !running_server )
{
result = get_total_events();
if ( !result )
break;
}
v3 = epoll_wait(epoll_fd, v6, *(_DWORD *)(v0 + 24588), 20);
if ( v3 > 0 )
{
v4 = v6;
v5 = &v6[(unsigned int)(v3 - 1) + 1];
do
{
func2((__int64 *)v4->data.ptr, v4->events);
++v4;
}
while ( v4 != v5 );
}
if ( !running_server )
{
if ( !incomming_connection_event || (ftor_del_event(incomming_connection_event), !running_server) )
{
if ( node_connection_event )
ftor_del_event(node_connection_event);
}
}
}
if ( random_fd != -1 )
result = close(random_fd);
return result;
}

original_code:
void ftor_reactor() {
    struct conf *config = get_conf();
    struct epoll_event events[config->max_epoll_queue];
    while (running_server || get_total_events()) {
        int nfds = epoll_wait(epoll_fd, events, config->max_epoll_queue, 20);
        for (int i = 0; i < nfds; ++i) {
            process_event((struct ftor_event *)events[i].data.ptr, events[i].events);
        }
        if (!running_server && incomming_connection_event) {
            ftor_del_event(incomming_connection_event);
        }
        if (!running_server && node_connection_event) {
            ftor_del_event(node_connection_event);
        }
    }
    if (random_fd != -1) close(random_fd);
}

canonical_original_code:
void ftor_reactor() {
    struct conf *config = get_conf();
    struct epoll_event events[-1];
    while (running_server || get_total_events()) {
        int nfds = epoll_wait(epoll_fd, events, config->max_epoll_queue, 20);
        for (int i = 0; i < nfds; ++i) {
            process_event((struct ftor_event *)events[i].data.ptr, events[i].events);
        }
        if (!running_server && incomming_connection_event) {
            ftor_del_event(incomming_connection_event);
        }
        if (!running_server && node_connection_event) {
            ftor_del_event(node_connection_event);
        }
    }
    if (random_fd != -1) close(random_fd);
}

variable_types: struct conf { int listening_port; char listening_ip_addr[8192]; int designator_port; char designator_ip_addr[8192]; int resolver_port; char resolver_ip_addr[8192]; int max_epoll_queue; int node_port; int enable_cipher; unsigned char private_key[8192]; } *

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } [-1]

variable_types: nfds int

variable_types: i int

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }


decompiled_code:
__int64 __fastcall add_event_to_reactor(epoll_data_t a1)
{
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

event.events = 5;
event.data = a1;
if ( epoll_ctl(epoll_fd, 1, *(_DWORD *)(a1.u64 + 32), &event) >= 0 )
return 0LL;
puts("Epoll fd add");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func6(epoll_data_t a1)
{
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

event.events = 5;
event.data = a1;
if ( epoll_ctl(epoll_fd, 1, *(_DWORD *)(a1.u64 + 32), &event) >= 0 )
return 0LL;
puts("Epoll fd add");
return 0xFFFFFFFFLL;
}

original_code:
int add_event_to_reactor(struct ftor_event *event_to_add) {
    struct epoll_event listenev;
    listenev.events = EPOLLIN | EPOLLOUT;
    listenev.data.ptr = event_to_add;
    if (epoll_ctl(epoll_fd, 1, event_to_add->socket_fd, &listenev) < 0) {
        printf("Epoll fd add\n");
        return -1;
    }
    return 0;
}

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; }


decompiled_code:
int ftor_reactor_init()
{
__int64 v0; // rax
int result; // eax

v0 = get_conf();
result = epoll_create(*(_DWORD *)(v0 + 24588));
epoll_fd = result;
return result;
}

canonical_decompiled_code:
int func7()
{
__int64 v0; // rax
int result; // eax

v0 = get_conf();
result = epoll_create(*(_DWORD *)(v0 + 24588));
epoll_fd = result;
return result;
}

original_code:
void ftor_reactor_init() {
    struct conf *config = get_conf();
    epoll_fd = epoll_create(config->max_epoll_queue);
}

user_defined_types: struct conf { int listening_port; char listening_ip_addr[8192]; int designator_port; char designator_ip_addr[8192]; int resolver_port; char resolver_ip_addr[8192]; int max_epoll_queue; int node_port; int enable_cipher; unsigned char private_key[8192]; }


setnonblock

decompiled_code:
int __fastcall setnonblock(int fd)
{
unsigned int v1; // eax
int result; // eax

v1 = fcntl(fd, 3, 0LL);
if ( v1 == -1 || (BYTE1(v1) |= 8u, result = fcntl(fd, 4, v1), result == -1) )
exit(1);
return result;
}

canonical_decompiled_code:
int __fastcall func8(int fd)
{
unsigned int v1; // eax
int result; // eax

v1 = fcntl(fd, 3, 0LL);
if ( v1 == -1 || (BYTE1(v1) |= 8u, result = fcntl(fd, 4, v1), result == -1) )
exit(1);
return result;
}

original_code:
void setnonblock(int fd) {
    int f;
    if ((f = fcntl(fd, 3, 0)) == -1 || fcntl(fd, 4, f | 04000) == -1) {
        exit(1);
    }
}

variable_types: f int


decompiled_code:
__int64 __fastcall accept_connection(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
int v3; // eax
unsigned int v4; // ebx
epoll_data_t v5; // rax
unsigned int v7; // er12
char *v8; // rax

v2 = ftor_create_context();
v3 = accept(*(_DWORD *)(a1 + 32), (struct sockaddr *)(v2 + 20), (socklen_t *)(v2 + 36));
v4 = v3;
if ( v3 == -1 )
{
v7 = *_errno_location();
v8 = strerror(v7);
printf("BAD CONNECTION err=%s %d\n", v8, v7);
ftor_del_context(v2);
}
else
{
setnonblock(v3);
*(_DWORD *)v2 = v4;
v5.u64 = ftor_create_event(v4, v2);
*(_QWORD *)(v5.u64 + 8) = a2;
*(_QWORD *)(v5.u64 + 16) = 0LL;
*(epoll_data_t *)(v2 + 96) = v5;
add_event_to_reactor(v5);
v4 = 0;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
int v3; // eax
unsigned int v4; // ebx
epoll_data_t v5; // rax
unsigned int v7; // er12
char *v8; // rax

v2 = ftor_create_context();
v3 = accept(*(_DWORD *)(a1 + 32), (struct sockaddr *)(v2 + 20), (socklen_t *)(v2 + 36));
v4 = v3;
if ( v3 == -1 )
{
v7 = *_errno_location();
v8 = strerror(v7);
printf("BAD CONNECTION err=%s %d\n", v8, v7);
ftor_del_context(v2);
}
else
{
func8(v3);
*(_DWORD *)v2 = v4;
v5.u64 = ftor_create_event(v4, v2);
*(_QWORD *)(v5.u64 + 8) = a2;
*(_QWORD *)(v5.u64 + 16) = 0LL;
*(epoll_data_t *)(v2 + 96) = v5;
func6(v5);
v4 = 0;
}
return v4;
}

original_code:
static int accept_connection(struct ftor_event *event, ftor_handler read_handler) {
    struct ftor_context *context = ftor_create_context();
    int f = accept(event->socket_fd, (struct sockaddr *)&context->client_addr, &context->client_addr_len);
    if (f == -1) {
        printf("BAD CONNECTION err=%s %d\n", strerror((*__errno_location ())), (*__errno_location ()));
        ftor_del_context(context);
        return -1;
    }
    setnonblock(f);
    context->incoming_fd = f;
    struct ftor_event *client_event = ftor_create_event(f, context);
    client_event->read_handler = read_handler;
    client_event->write_handler = ((void *)0);
    context->client_event = client_event;
    add_event_to_reactor(client_event);
    return 0;
}

canonical_original_code:
static int accept_connection(struct ftor_event *event, int (*read_handler)(struct ftor_event *event)) {
    struct ftor_context *context = ftor_create_context();
    int f = accept(event->socket_fd, (struct sockaddr *)&context->client_addr, &context->client_addr_len);
    if (f == -1) {
        printf("BAD CONNECTION err=%s %d\n", strerror((*__errno_location ())), (*__errno_location ()));
        ftor_del_context(context);
        return -1;
    }
    setnonblock(f);
    context->incoming_fd = f;
    struct ftor_event *client_event = ftor_create_event(f, context);
    client_event->read_handler = read_handler;
    client_event->write_handler = ((void *)0);
    context->client_event = client_event;
    add_event_to_reactor(client_event);
    return 0;
}

variable_types: struct ftor_event { struct ftor_context { int incoming_fd; _Bool terminated; struct mem_pool { void *memory_start; void *current_pos; unsigned long space_left; struct mem_pool *next; } *pool; enum ftor_state { conn_none_state=0, socks_header_received_state=1 } state; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client_addr; unsigned int client_addr_len; unsigned char *client_recv_buffer; unsigned long client_recv_buffer_size; unsigned long client_recv_buffer_pos; unsigned char *chain_recv_buffer; unsigned long chain_recv_buffer_size; unsigned long chain_recv_buffer_pos; unsigned short peer_port; unsigned int peer_address; struct ftor_event *client_event; char *chain_domain_name1; char *chain_domain_name2; char *chain_pubkey1; char *chain_pubkey2; unsigned int chain_ip1; unsigned int chain_ip2; unsigned char sesskey1[128]; unsigned char sesskey2[128]; int events_num; _Bool client_eof; _Bool chain_eof; unsigned char node_sesskey[128]; unsigned int next_ip; unsigned short next_port; unsigned int node_flags; int header_bytes_received; int forward_cipher_offset; int backward_cipher_offset; } *context; int (*read_handler)(struct ftor_event *event); int (*write_handler)(struct ftor_event *event); int (*destuction_handler)(struct ftor_event *event); int socket_fd; unsigned char *recv_buffer; unsigned long recv_buffer_size; unsigned long recv_buffer_pos; unsigned char *send_buffer; unsigned long send_buffer_size; unsigned long send_buffer_pos; } *

variable_types: struct ftor_context { int incoming_fd; _Bool terminated; struct mem_pool { void *memory_start; void *current_pos; unsigned long space_left; struct mem_pool *next; } *pool; enum ftor_state { conn_none_state=0, socks_header_received_state=1 } state; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client_addr; unsigned int client_addr_len; unsigned char *client_recv_buffer; unsigned long client_recv_buffer_size; unsigned long client_recv_buffer_pos; unsigned char *chain_recv_buffer; unsigned long chain_recv_buffer_size; unsigned long chain_recv_buffer_pos; unsigned short peer_port; unsigned int peer_address; struct ftor_event { struct ftor_context *context; int (*read_handler)(struct ftor_event *event); int (*write_handler)(struct ftor_event *event); int (*destuction_handler)(struct ftor_event *event); int socket_fd; unsigned char *recv_buffer; unsigned long recv_buffer_size; unsigned long recv_buffer_pos; unsigned char *send_buffer; unsigned long send_buffer_size; unsigned long send_buffer_pos; } *client_event; char *chain_domain_name1; char *chain_domain_name2; char *chain_pubkey1; char *chain_pubkey2; unsigned int chain_ip1; unsigned int chain_ip2; unsigned char sesskey1[128]; unsigned char sesskey2[128]; int events_num; _Bool client_eof; _Bool chain_eof; unsigned char node_sesskey[128]; unsigned int next_ip; unsigned short next_port; unsigned int node_flags; int header_bytes_received; int forward_cipher_offset; int backward_cipher_offset; } *


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

v1 = accept_connection(a1, (__int64)&ftor_node_get_header);
puts("node connection accepted");
return v1;
}

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

v1 = func9(a1, (__int64)&ftor_node_get_header);
puts("node connection accepted");
return v1;
}

original_code:
static int node_connecton_accepter(struct ftor_event *event) {
    int rc = accept_connection(event, ftor_node_get_header);
    printf("node connection accepted\n");
    return rc;
}

variable_types: rc int


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

puts("In client_connecton_accepter");
v1 = accept_connection(a1, (__int64)&ftor_socks_get_header);
puts("client connection accepted");
return v1;
}

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

puts("In client_connecton_accepter");
v1 = func9(a1, (__int64)&ftor_socks_get_header);
puts("client connection accepted");
return v1;
}

original_code:
static int client_connecton_accepter(struct ftor_event *event) {
    printf("In client_connecton_accepter\n");
    int rc = accept_connection(event, ftor_socks_get_header);
    printf("client connection accepted\n");
    return rc;
}


decompiled_code:
__int64 __fastcall get_listening_sock(char *cp, unsigned int a2)
{
unsigned int v2; // ebx
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
struct sockaddr v8[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
inet_aton(cp, (struct in_addr *)&v8[0].sa_data[2]);
v8[0].sa_family = 2;
*(_WORD *)v8[0].sa_data = _bswap_16(a2);
setnonblock(v2);
if ( bind(v2, v8, 0x10u) )
{
v4 = _errno_location();
v5 = strerror(*v4);
printf("Cannot bind %s\n", v5);
_assert_fail("0", "network.c", 0xA8u, "get_listening_sock");
}
if ( listen(v2, 0) < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
printf("Cannot listen %s\n", v7);
_assert_fail("0", "network.c", 0xACu, "get_listening_sock");
}
printf("listening %d\n", a2);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func12(char *cp, unsigned int a2)
{
unsigned int v2; // ebx
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
struct sockaddr v8[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
inet_aton(cp, (struct in_addr *)&v8[0].sa_data[2]);
v8[0].sa_family = 2;
*(_WORD *)v8[0].sa_data = func3(a2);
func8(v2);
if ( bind(v2, v8, 0x10u) )
{
v4 = _errno_location();
v5 = strerror(*v4);
printf("Cannot bind %s\n", v5);
_assert_fail("0", "network.c", 0xA8u, "get_listening_sock");
}
if ( listen(v2, 0) < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
printf("Cannot listen %s\n", v7);
_assert_fail("0", "network.c", 0xACu, "get_listening_sock");
}
printf("listening %d\n", a2);
return v2;
}

original_code:
int get_listening_sock(const char *ip_addr, int port) {
    struct sockaddr_in listening_addr;
    int sock = socket(2, SOCK_STREAM, 0);
    inet_aton(ip_addr, &listening_addr.sin_addr);
    listening_addr.sin_family = 2;
    listening_addr.sin_port = htons(port);
    setnonblock(sock);
    if (bind(sock, (struct sockaddr *)&listening_addr, sizeof(listening_addr))) {
        printf("Cannot bind %s\n", strerror((*__errno_location ())));
        ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "network.c", 168, __extension__ __PRETTY_FUNCTION__); }));
    }
    if (listen(sock, 0) < 0) {
        printf("Cannot listen %s\n", strerror((*__errno_location ())));
        ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "network.c", 172, __extension__ __PRETTY_FUNCTION__); }));
    }
    printf("listening %d\n", port);
    return sock;
}

canonical_original_code:
int get_listening_sock(char *ip_addr, int port) {
    struct sockaddr_in listening_addr;
    int sock = socket(2, SOCK_STREAM, 0);
    inet_aton(ip_addr, &listening_addr.sin_addr);
    listening_addr.sin_family = 2;
    listening_addr.sin_port = htons(port);
    setnonblock(sock);
    if (bind(sock, (struct sockaddr *)&listening_addr, sizeof(listening_addr))) {
        printf("Cannot bind %s\n", strerror((*__errno_location ())));
        ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "network.c", 168, __extension__ __PRETTY_FUNCTION__); }));
    }
    if (listen(sock, 0) < 0) {
        printf("Cannot listen %s\n", strerror((*__errno_location ())));
        ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "network.c", 172, __extension__ __PRETTY_FUNCTION__); }));
    }
    printf("listening %d\n", port);
    return sock;
}

variable_types: port int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: sock int

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int64 ftor_start_server()
{
__int64 v0; // rbx
unsigned int v1; // eax
_QWORD *v2; // rax
unsigned int v3; // eax
_QWORD *v4; // rax

v0 = get_conf();
v1 = get_listening_sock((char *)(v0 + 4), *(_DWORD *)v0);
v2 = (_QWORD *)ftor_create_event(v1, 0LL);
incomming_connection_event = (__int64)v2;
v2[1] = client_connecton_accepter;
v2[2] = 0LL;
v2[3] = incomming_connection_event_destructor;
v3 = get_listening_sock((char *)(v0 + 4), *(_DWORD *)(v0 + 24592));
v4 = (_QWORD *)ftor_create_event(v3, 0LL);
node_connection_event = (__int64)v4;
v4[1] = node_connecton_accepter;
v4[2] = 0LL;
v4[3] = node_connection_event_destructor;
add_event_to_reactor((epoll_data_t)incomming_connection_event);
return add_event_to_reactor((epoll_data_t)node_connection_event);
}

canonical_decompiled_code:
__int64 func13()
{
__int64 v0; // rbx
unsigned int v1; // eax
_QWORD *v2; // rax
unsigned int v3; // eax
_QWORD *v4; // rax

v0 = get_conf();
v1 = func12((char *)(v0 + 4), *(_DWORD *)v0);
v2 = (_QWORD *)ftor_create_event(v1, 0LL);
incomming_connection_event = (__int64)v2;
v2[1] = client_connecton_accepter;
v2[2] = 0LL;
v2[3] = incomming_connection_event_destructor;
v3 = func12((char *)(v0 + 4), *(_DWORD *)(v0 + 24592));
v4 = (_QWORD *)ftor_create_event(v3, 0LL);
node_connection_event = (__int64)v4;
v4[1] = node_connecton_accepter;
v4[2] = 0LL;
v4[3] = node_connection_event_destructor;
func6((epoll_data_t)incomming_connection_event);
return func6((epoll_data_t)node_connection_event);
}

original_code:
void ftor_start_server() {
    struct conf *config = get_conf();
    incomming_connection_event = ftor_create_event(get_listening_sock(config->listening_ip_addr, config->listening_port), ((void *)0));
    incomming_connection_event->read_handler = client_connecton_accepter;
    incomming_connection_event->write_handler = ((void *)0);
    incomming_connection_event->destuction_handler = incomming_connection_event_destructor;
    node_connection_event = ftor_create_event(get_listening_sock(config->listening_ip_addr, config->node_port), ((void *)0));
    node_connection_event->read_handler = node_connecton_accepter;
    node_connection_event->write_handler = ((void *)0);
    node_connection_event->destuction_handler = node_connection_event_destructor;
    add_event_to_reactor(incomming_connection_event);
    add_event_to_reactor(node_connection_event);
}


decompiled_code:
__int64 __fastcall rsa_get_last_error(__int64 a1)
{
__int64 v1; // rax

OPENSSL_init_crypto(2LL, 0LL);
v1 = ERR_get_error();
return ERR_error_string(v1, a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 v1; // rax

OPENSSL_init_crypto(2LL, 0LL);
v1 = ERR_get_error();
return ERR_error_string(v1, a1);
}

original_code:
void rsa_get_last_error(char *msg) {
    OPENSSL_init_crypto(0x00000002L, ((void *)0));
    ERR_error_string(ERR_get_error(), msg);
}

variable_types:
msg
char *


decompiled_code:
__int64 rsa_cleanup()
{
return ERR_remove_state(0LL);
}

canonical_decompiled_code:
__int64 func6()
{
return ERR_remove_state(0LL);
}

original_code:
void rsa_cleanup() {
    while(0) continue;
    while(0) continue;
    ERR_remove_state(0);
    while(0) continue;
}


decompiled_code:
_QWORD *ftor_pool_get()
{
return ftor_pool_get_with_size(0x100000uLL);
}

canonical_decompiled_code:
_QWORD *func0()
{
return ftor_pool_get_with_size(0x100000uLL);
}

original_code:
struct mem_pool *ftor_pool_get() {
    return ftor_pool_get_with_size((1 * 1024 * 1024));
}

return_type: struct mem_pool { void *memory_start; void *current_pos; unsigned long space_left; struct mem_pool *next; } *


decompiled_code:
void __fastcall ftor_free(void *ptr)
{
void **v1; // rbx
void **v2; // rbp

v1 = (void **)ptr;
if ( ptr )
{
do
{
v2 = v1;
v1 = (void **)v1[3];
free(*v2);
free(v2);
}
while ( v1 );
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void **v1; // rbx
void **v2; // rbp

v1 = (void **)ptr;
if ( ptr )
{
do
{
v2 = v1;
v1 = (void **)v1[3];
free(*v2);
free(v2);
}
while ( v1 );
}
}

original_code:
void ftor_free(struct mem_pool *pool) {
    struct mem_pool *prev_part = ((void *)0);
    while(pool) {
        prev_part = pool;
        pool = pool->next;
        free(prev_part->memory_start);
        free(prev_part);
    }
}

variable_types: struct mem_pool { void *memory_start; void *current_pos; unsigned long space_left; struct mem_pool *next; } *


decompiled_code:
int __fastcall parse_config_line(char *s1, char *file)
{
char **v2; // rbx
int result; // eax
int *v4; // r12
int v5; // er15
char *v6; // r12

if ( !strcasecmp("include", s1) )
read_config(file);
v2 = &parser;
do
{
result = strcasecmp(s1, *v2);
if ( !result )
{
result = *((_DWORD *)v2 + 2);
switch ( result )
{
case 1:
result = (unsigned int)strncpy(v2[2], file, 0x2000uLL);
break;
case 2:
v5 = open(file, 0);
v6 = v2[2];
v6[(int)read(v5, v6, 0x2000uLL)] = 0;
result = close(v5);
break;
case 0:
v4 = (int *)v2[2];
result = atoi(file);
*v4 = result;
break;
}
}
v2 += 3;
}
while ( v2 != (char **)&unk_8890 );
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *s1, char *file)
{
char **v2; // rbx
int result; // eax
int *v4; // r12
int v5; // er15
char *v6; // r12

if ( !strcasecmp("include", s1) )
func1(file);
v2 = &parser;
do
{
result = strcasecmp(s1, *v2);
if ( !result )
{
result = *((_DWORD *)v2 + 2);
switch ( result )
{
case 1:
result = (unsigned int)strncpy(v2[2], file, 0x2000uLL);
break;
case 2:
v5 = open(file, 0);
v6 = v2[2];
v6[(int)read(v5, v6, 0x2000uLL)] = 0;
result = close(v5);
break;
case 0:
v4 = (int *)v2[2];
result = atoi(file);
*v4 = result;
break;
}
}
v2 += 3;
}
while ( v2 != (char **)&unk_8890 );
return result;
}

original_code:
static void parse_config_line(const char *key, const char *value) {
    if (!strcasecmp("include", key)) read_config(value);
    int options_num = sizeof(parser) / sizeof(struct config_parser);
    for (int i = 0; i < options_num; ++i) {
        if (!strcasecmp(key, parser[i].opt_name)) {
            switch (parser[i].opt_type) {
            case ct_int:
                *((int *)parser[i].opt_val) = atoi(value);
                break;
            case ct_string:
                strncpy(parser[i].opt_val, value, 8192);
                break;
            case ct_file:
                {
                int fd = open(value, 00);
                int readed = read(fd, parser[i].opt_val, 8192);
                *(((unsigned char *)parser[i].opt_val) + readed) = '\0';
                close(fd);
                break;
                }
            }
        }
    }
}

canonical_original_code:
static void parse_config_line(char *key, char *value) {
    if (!strcasecmp("include", key)) read_config(value);
    int options_num = sizeof(parser) / sizeof(struct config_parser);
    for (int i = 0; i < options_num; ++i) {
        if (!strcasecmp(key, parser[i].opt_name)) {
            switch (parser[i].opt_type) {
            case ct_int:
                *((int *)parser[i].opt_val) = atoi(value);
                break;
            case ct_string:
                strncpy(parser[i].opt_val, value, 8192);
                break;
            case ct_file:
                {
                int fd = open(value, 00);
                int readed = read(fd, parser[i].opt_val, 8192);
                *(((unsigned char *)parser[i].opt_val) + readed) = '\0';
                close(fd);
                break;
                }
            }
        }
    }
}

variable_types:
key
char *

variable_types:
value
char *

variable_types: fd int

variable_types: readed int


decompiled_code:
__int64 __fastcall set_config_file(const char *a1)
{
const char *v1; // rcx
int v2; // edx
__int64 result; // rax

v1 = a1;
if ( !a1 )
v1 = "/etc/ftor/ftor.conf";
v2 = snprintf(config_file, 0x402uLL, "%s", v1);
result = 0LL;
if ( v2 <= 1025 )
result = read_config(config_file);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
const char *v1; // rcx
int v2; // edx
__int64 result; // rax

v1 = a1;
if ( !a1 )
v1 = "/etc/ftor/ftor.conf";
v2 = snprintf(config_file, 0x402uLL, "%s", v1);
result = 0LL;
if ( v2 <= 1025 )
result = func1(config_file);
return result;
}

original_code:
_Bool set_config_file(const char *cfg) {
    if (snprintf(config_file, sizeof(config_file), "%s", cfg ? cfg : "/etc/ftor/ftor.conf" ) >= (int)sizeof(config_file))
        return 0;
    return read_config(config_file);
}

canonical_original_code:
_Bool set_config_file(char *cfg) {
    if (snprintf(config_file, sizeof(config_file), "%s", cfg ? cfg : "/etc/ftor/ftor.conf" ) >= (int)sizeof(config_file))
        return 0;
    return read_config(config_file);
}

variable_types:
cfg
char *


decompiled_code:
void **get_conf()
{
void **result; // rax

result = &config;
if ( !configured )
{
if ( (unsigned __int8)read_config(config_file) )
{
configured = 1;
result = &config;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
void **func3()
{
void **result; // rax

result = &config;
if ( !configured )
{
if ( (unsigned __int8)func1(config_file) )
{
configured = 1;
result = &config;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
struct conf *get_conf() {
    if (!configured) {
        if(!read_config(config_file)) {
            return ((void *)0);
        }
        configured = 1;
    }
    return &config;
}

return_type: struct conf { int listening_port; char listening_ip_addr[8192]; int designator_port; char designator_ip_addr[8192]; int resolver_port; char resolver_ip_addr[8192]; int max_epoll_queue; int node_port; int enable_cipher; unsigned char private_key[8192]; } *


getch

repo: BJIADOKC/samp-plugin-logger

decompiled_code:
__int64 getch()
{
unsigned int buf; // [rsp+Ch] [rbp-8Ch] BYREF
struct termios v2; // [rsp+10h] [rbp-88h] BYREF
struct termios termios_p; // [rsp+50h] [rbp-48h] BYREF

buf = 0;
if ( !isatty(0) )
return 0xFFFFFFFFLL;
if ( tcgetattr(0, &termios_p) < 0 )
return 0xFFFFFFFFLL;
*(__m128i *)&v2.c_iflag = _mm_loadu_si128((const __m128i *)&termios_p);
*(__m128i *)&v2.c_line = _mm_loadu_si128((const __m128i *)&termios_p.c_line);
*(__m128i *)&v2.c_cc[15] = _mm_loadu_si128((const __m128i *)&termios_p.c_cc[15]);
*(_QWORD *)&v2.c_cc[31] = *(_QWORD *)&termios_p.c_cc[31];
v2.c_ospeed = termios_p.c_ospeed;
v2.c_lflag &= 0xFFFFFFF4;
v2.c_cc[6] = 1;
v2.c_cc[5] = 0;
if ( tcsetattr(0, 0, &v2) < 0 )
return 0xFFFFFFFFLL;
if ( read(0, &buf, 1uLL) != 1 )
buf = -1;
tcsetattr(0, 0, &termios_p);
return buf;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int buf; // [rsp+Ch] [rbp-8Ch] BYREF
struct termios v2; // [rsp+10h] [rbp-88h] BYREF
struct termios termios_p; // [rsp+50h] [rbp-48h] BYREF

buf = 0;
if ( !isatty(0) )
return 0xFFFFFFFFLL;
if ( tcgetattr(0, &termios_p) < 0 )
return 0xFFFFFFFFLL;
*(__m128i *)&v2.c_iflag = _mm_loadu_si128((const __m128i *)&termios_p);
*(__m128i *)&v2.c_line = _mm_loadu_si128((const __m128i *)&termios_p.c_line);
*(__m128i *)&v2.c_cc[15] = _mm_loadu_si128((const __m128i *)&termios_p.c_cc[15]);
*(_QWORD *)&v2.c_cc[31] = *(_QWORD *)&termios_p.c_cc[31];
v2.c_ospeed = termios_p.c_ospeed;
v2.c_lflag &= 0xFFFFFFF4;
v2.c_cc[6] = 1;
v2.c_cc[5] = 0;
if ( tcsetattr(0, 0, &v2) < 0 )
return 0xFFFFFFFFLL;
if ( read(0, &buf, 1uLL) != 1 )
buf = -1;
tcsetattr(0, 0, &termios_p);
return buf;
}

original_code:
int
getch (void)
{
  struct termios save_termios;
  struct termios ios;
  int c = 0;
  if (!isatty (0))
    return (-1);
  if (tcgetattr (0, &save_termios) < 0)
    return (-1);
  ios = save_termios;
  ios.c_lflag &= ~(0000002 | 0000010 | 0000001);
  ios.c_cc[6] = 1;
  ios.c_cc[5] = 0;
  if (tcsetattr (0, 0, &ios) < 0)
    return (-1);
  if (read (0, &c, 1) != 1)
    c = (-1);
  tcsetattr (0, 0, &save_termios);
  return c;
}

canonical_original_code:
int getch()
{
  struct termios save_termios;
  struct termios ios;
  int c = 0;
  if (!isatty (0))
    return (-1);
  if (tcgetattr (0, &save_termios) < 0)
    return (-1);
  ios = save_termios;
  ios.c_lflag &= ~(0000002 | 0000010 | 0000001);
  ios.c_cc[6] = 1;
  ios.c_cc[5] = 0;
  if (tcsetattr (0, 0, &ios) < 0)
    return (-1);
  if (read (0, &c, 1) != 1)
    c = (-1);
  tcsetattr (0, 0, &save_termios);
  return c;
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types: c int


kbhit

decompiled_code:
__int64 kbhit()
{
unsigned int v0; // ebx
struct timeval v2; // [rsp+0h] [rbp-118h] BYREF
fd_set readfds; // [rsp+10h] [rbp-108h] BYREF
struct termios v4; // [rsp+90h] [rbp-88h] BYREF
struct termios termios_p; // [rsp+D0h] [rbp-48h] BYREF

v2.tv_sec = 0LL;
v2.tv_usec = 0LL;
v0 = isatty(0);
if ( v0 )
{
v0 = 0;
if ( tcgetattr(0, &termios_p) >= 0 )
{
*(__m128i *)&v4.c_iflag = _mm_loadu_si128((const __m128i *)&termios_p);
*(__m128i *)&v4.c_line = _mm_loadu_si128((const __m128i *)&termios_p.c_line);
*(__m128i *)&v4.c_cc[15] = _mm_loadu_si128((const __m128i *)&termios_p.c_cc[15]);
*(_QWORD *)&v4.c_cc[31] = *(_QWORD *)&termios_p.c_cc[31];
v4.c_ospeed = termios_p.c_ospeed;
v4.c_lflag &= 0xFFFFFFF4;
v4.c_cc[6] = 1;
v4.c_cc[5] = 0;
if ( tcsetattr(0, 0, &v4) >= 0 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
v0 = select(1, &readfds, 0LL, 0LL, &v2) == 1;
tcsetattr(0, 0, &termios_p);
}
}
}
return v0;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebx
struct timeval v2; // [rsp+0h] [rbp-118h] BYREF
fd_set readfds; // [rsp+10h] [rbp-108h] BYREF
struct termios v4; // [rsp+90h] [rbp-88h] BYREF
struct termios termios_p; // [rsp+D0h] [rbp-48h] BYREF

v2.tv_sec = 0LL;
v2.tv_usec = 0LL;
v0 = isatty(0);
if ( v0 )
{
v0 = 0;
if ( tcgetattr(0, &termios_p) >= 0 )
{
*(__m128i *)&v4.c_iflag = _mm_loadu_si128((const __m128i *)&termios_p);
*(__m128i *)&v4.c_line = _mm_loadu_si128((const __m128i *)&termios_p.c_line);
*(__m128i *)&v4.c_cc[15] = _mm_loadu_si128((const __m128i *)&termios_p.c_cc[15]);
*(_QWORD *)&v4.c_cc[31] = *(_QWORD *)&termios_p.c_cc[31];
v4.c_ospeed = termios_p.c_ospeed;
v4.c_lflag &= 0xFFFFFFF4;
v4.c_cc[6] = 1;
v4.c_cc[5] = 0;
if ( tcsetattr(0, 0, &v4) >= 0 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
v0 = select(1, &readfds, 0LL, 0LL, &v2) == 1;
tcsetattr(0, 0, &termios_p);
}
}
}
return v0;
}

original_code:
int
kbhit (void)
{
  struct termios save_termios;
  struct termios ios;
  fd_set inp;
  struct timeval timeout = {0, 0};
  int result;
  if (!isatty (0))
    return 0;
  if (tcgetattr (0, &save_termios) < 0)
    return 0;
  ios = save_termios;
  ios.c_lflag &= ~(0000002 | 0000010 | 0000001);
  ios.c_cc[6] = 1;
  ios.c_cc[5] = 0;
  if (tcsetattr (0, 0, &ios) < 0)
    return 0;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&inp)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&inp)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
  result = select (0 +1, &inp, ((void *)0), ((void *)0), &timeout) == 1;
  tcsetattr (0, 0, &save_termios);
  return result;
}

canonical_original_code:
int kbhit()
{
  struct termios save_termios;
  struct termios ios;
  struct fd_set inp;
  struct timeval timeout = {0, 0};
  int result;
  if (!isatty (0))
    return 0;
  if (tcgetattr (0, &save_termios) < 0)
    return 0;
  ios = save_termios;
  ios.c_lflag &= ~(0000002 | 0000010 | 0000001);
  ios.c_cc[6] = 1;
  ios.c_cc[5] = 0;
  if (tcsetattr (0, 0, &ios) < 0)
    return 0;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&inp)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&inp)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
  result = select (0 +1, &inp, ((void *)0), ((void *)0), &timeout) == 1;
  tcsetattr (0, 0, &save_termios);
  return result;
}

variable_types:
inp
struct fd_set { long __fds_bits[-1]; }

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types: result int

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


user_defined_types: struct timeval { long tv_sec; long tv_usec; }


strnew

original_code:
static _Bool strnew(elfpart *part)
{
    part->shtype = 3;
    part->shname = ".strtab";
    _resizepart(part, 1);
    *(char*)part->part = '\0';
    return 1;
}

canonical_original_code:
static _Bool strnew(struct elfpart *part)
{
    part->shtype = 3;
    part->shname = ".strtab";
    _resizepart(part, 1);
    *(char *)part->part = '\0';
    return 1;
}

variable_types: struct elfpart { _Bool (*new)(struct elfpart *); _Bool (*init)(struct elfpart *, struct blueprint { int filetype; int partcount; struct elfpart *parts; } *); _Bool (*fill)(struct elfpart *, struct blueprint *); _Bool (*complete)(struct elfpart *, struct blueprint *); int shtype; int entsize; int count; int size; void *part; char *shname; unsigned long offset; long addr; struct elfpart *link; int info; short flags; _Bool done; } *

user_defined_types: struct elfpart { _Bool (*new)(struct elfpart *); _Bool (*init)(struct elfpart *, struct blueprint *); _Bool (*fill)(struct elfpart *, struct blueprint *); _Bool (*complete)(struct elfpart *, struct blueprint *); int shtype; int entsize; int count; int size; void *part; char *shname; unsigned long offset; long addr; struct elfpart *link; int info; short flags; _Bool done; }


shnew

decompiled_code:
__int64 __fastcall shnew(__int64 a1)
{
strnew(a1);
*(_QWORD *)(a1 + 56) = ".shstrtab";
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
func0(a1);
*(_QWORD *)(a1 + 56) = ".shstrtab";
return 1LL;
}

original_code:
static _Bool shnew(elfpart *part)
{
    strnew(part);
    part->shname = ".shstrtab";
    return 1;
}

canonical_original_code:
static _Bool shnew(struct elfpart *part)
{
    strnew(part);
    part->shname = ".shstrtab";
    return 1;
}

user_defined_types: struct blueprint { int filetype; int partcount; struct elfpart *parts; }


addtostrtab

decompiled_code:
__int64 __fastcall addtostrtab(__int64 a1, const char *a2)
{
int v2; // er12
size_t v4; // rax
size_t v5; // r13
__int64 v6; // rax

if ( (unsigned int)validate(*(_DWORD *)(a1 + 32) == 3, "not a string table") )
{
if ( a2 )
{
v2 = 0;
if ( *a2 )
{
v4 = strlen(a2);
v5 = v4 + 1;
v2 = *(_DWORD *)(a1 + 44);
v6 = resizepart(a1, (unsigned int)(v2 + v4 + 1));
if ( v6 )
memcpy((void *)(v6 + v2), a2, v5);
else
v2 = -1;
}
}
else
{
v2 = 0;
}
}
else
{
v2 = -1;
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2)
{
int v2; // er12
size_t v4; // rax
size_t v5; // r13
__int64 v6; // rax

if ( (unsigned int)validate(*(_DWORD *)(a1 + 32) == 3, "not a string table") )
{
if ( a2 )
{
v2 = 0;
if ( *a2 )
{
v4 = strlen(a2);
v5 = v4 + 1;
v2 = *(_DWORD *)(a1 + 44);
v6 = resizepart(a1, (unsigned int)(v2 + v4 + 1));
if ( v6 )
memcpy((void *)(v6 + v2), a2, v5);
else
v2 = -1;
}
}
else
{
v2 = 0;
}
}
else
{
v2 = -1;
}
return (unsigned int)v2;
}

original_code:
int addtostrtab(elfpart *part, char const *str)
{
    char *strtab;
    size_t pos, len;
    if (!_validate(part->shtype == 3, "not a string table"))
        return -1;
    if (!str || !*str)
 return 0;
    len = strlen(str) + 1;
    pos = part->size;
    strtab = _resizepart(part, part->size + len);
    if (!strtab)
        return -1;
    memcpy(strtab + pos, str, len);
    return pos;
}

canonical_original_code:
int addtostrtab(struct elfpart *part, char *str)
{
    char *strtab;
    unsigned long pos; unsigned long len;
    if (!_validate(part->shtype == 3, "not a string table"))
        return -1;
    if (!str || !*str)
 return 0;
    len = strlen(str) + 1;
    pos = part->size;
    strtab = _resizepart(part, part->size + len);
    if (!strtab)
        return -1;
    memcpy(strtab + pos, str, len);
    return pos;
}

variable_types:
str
char *

variable_types:
strtab
char *

variable_types:
pos
unsigned long

variable_types:
len
unsigned long


dynnew

decompiled_code:
__int64 __fastcall dynnew(__int64 a1)
{
strnew(a1);
*(_QWORD *)(a1 + 56) = ".dynstr";
*(_WORD *)(a1 + 92) = 4;
return 1LL;
}

original_code:
static _Bool dynnew(elfpart *part)
{
    strnew(part);
    part->shname = ".dynstr";
    part->flags = (1 << 2);
    return 1;
}

canonical_original_code:
static _Bool dynnew(struct elfpart *part)
{
    strnew(part);
    part->shname = ".dynstr";
    part->flags = (1 << 2);
    return 1;
}


getstringptr

decompiled_code:
__int64 __fastcall getstringptr(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rcx
_BYTE *v4; // rax
_BYTE *v5; // rsi

if ( !a1 || a2 <= 0 )
return 0LL;
if ( a3 < 0 || a2 < a3 )
return 0LL;
if ( a2 <= a3 )
{
LABEL_9:
warn("string in string table at offset %ld is unterminated.", a3);
return 0LL;
}
v3 = a1 + a3;
v4 = (_BYTE *)(a1 + a3);
v5 = (_BYTE *)(a1 + a2);
while ( *v4 )
{
if ( ++v4 == v5 )
goto LABEL_9;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rcx
_BYTE *v4; // rax
_BYTE *v5; // rsi

if ( !a1 || a2 <= 0 )
return 0LL;
if ( a3 < 0 || a2 < a3 )
return 0LL;
if ( a2 <= a3 )
{
LABEL_9:
warn("string in string table at offset %ld is unterminated.", a3);
return 0LL;
}
v3 = a1 + a3;
v4 = (_BYTE *)(a1 + a3);
v5 = (_BYTE *)(a1 + a2);
while ( *v4 )
{
if ( ++v4 == v5 )
goto LABEL_9;
}
return v3;
}

original_code:
static char const *getstringptr(char const *strtab, long strsize, long offset)
{
    long i;
    if (!strtab || strsize <= 0 || offset < 0 || offset > strsize)
        return ((void *)0);
    for (i = offset ; i < strsize ; ++i)
        if (strtab[i] == '\0')
            return strtab + offset;
    warn("string in string table at offset %ld is unterminated.", offset);
    return ((void *)0);
}

canonical_original_code:
static char *getstringptr(char *strtab, long strsize, long offset)
{
    long i;
    if (!strtab || strsize <= 0 || offset < 0 || offset > strsize)
        return ((void *)0);
    for (i = offset ; i < strsize ; ++i)
        if (strtab[i] == '\0')
            return strtab + offset;
    warn("string in string table at offset %ld is unterminated.", offset);
    return ((void *)0);
}

variable_types: strsize long

variable_types: offset long

variable_types: i long

return_type: char *


copytable

original_code:
static Elf64_Shdr const *copytable(char const *ptr, int count, int entsize)
{
    Elf64_Shdr *shdrs;
    Elf32_Shdr sh32;
    int i;
    if (iself64() && entsize == sizeof *shdrs)
 return (Elf64_Shdr const*)ptr;
    shdrs = allocate(count * sizeof *shdrs);
    for (i = 0 ; i < count ; ++i) {
 if (iself64()) {
     if (entsize >= (int)(sizeof *shdrs)) {
  memcpy(shdrs + i, ptr + i * entsize, sizeof *shdrs);
     } else {
  memcpy(shdrs + i, ptr + i * entsize, entsize);
  memset((char*)(shdrs + i) + entsize, 0,
         sizeof *shdrs - entsize);
     }
 } else {
     if (entsize >= (int)(sizeof sh32)) {
  memcpy(&sh32, ptr + i * entsize, sizeof sh32);
     } else {
  memcpy(&sh32, ptr + i * entsize, entsize);
  memset((char*)&sh32 + entsize, 0, sizeof sh32 - entsize);
     }
     shdrs[i].sh_name = sh32.sh_name;
     shdrs[i].sh_type = sh32.sh_type;
     shdrs[i].sh_flags = sh32.sh_flags;
     shdrs[i].sh_addr = sh32.sh_addr;
     shdrs[i].sh_offset = sh32.sh_offset;
     shdrs[i].sh_size = sh32.sh_size;
     shdrs[i].sh_link = sh32.sh_link;
     shdrs[i].sh_info = sh32.sh_info;
     shdrs[i].sh_addralign = sh32.sh_addralign;
     shdrs[i].sh_entsize = sh32.sh_entsize;
 }
    }
    return shdrs;
}

canonical_original_code:
static struct Elf64_Shdr *copytable(char *ptr, int count, int entsize)
{
    struct Elf64_Shdr *shdrs;
    struct Elf32_Shdr sh32;
    int i;
    if (iself64() && entsize == sizeof *shdrs)
 return (struct Elf64_Shdr *)ptr;
    shdrs = allocate(count * sizeof *shdrs);
    for (i = 0 ; i < count ; ++i) {
 if (iself64()) {
     if (entsize >= (int)(sizeof *shdrs)) {
  memcpy(shdrs + i, ptr + i * entsize, sizeof *shdrs);
     } else {
  memcpy(shdrs + i, ptr + i * entsize, entsize);
  memset((char *)(shdrs + i) + entsize, 0,
         sizeof *shdrs - entsize);
     }
 } else {
     if (entsize >= (int)(sizeof sh32)) {
  memcpy(&sh32, ptr + i * entsize, sizeof sh32);
     } else {
  memcpy(&sh32, ptr + i * entsize, entsize);
  memset((char *)&sh32 + entsize, 0, sizeof sh32 - entsize);
     }
     shdrs[i].sh_name = sh32.sh_name;
     shdrs[i].sh_type = sh32.sh_type;
     shdrs[i].sh_flags = sh32.sh_flags;
     shdrs[i].sh_addr = sh32.sh_addr;
     shdrs[i].sh_offset = sh32.sh_offset;
     shdrs[i].sh_size = sh32.sh_size;
     shdrs[i].sh_link = sh32.sh_link;
     shdrs[i].sh_info = sh32.sh_info;
     shdrs[i].sh_addralign = sh32.sh_addralign;
     shdrs[i].sh_entsize = sh32.sh_entsize;
 }
    }
    return shdrs;
}

variable_types:
ptr
char *

variable_types: count int

variable_types: entsize int

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

variable_types: struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; }

variable_types: i int


selectshtype

original_code:
static int selectshtype(Elf64_Shdr const *shdr, char const *name)
{
    if (!strcmp(name, ".got"))
 return P_ADDRS;
    if (!strncmp(name, ".got.", 5))
 return P_ADDRS;
    if (!strncmp(name, ".ctors", 6))
 return P_ADDRS;
    if (!strncmp(name, ".dtors", 6))
 return P_ADDRS;
    if (shdr->sh_flags & (1 << 5))
 return P_STRINGS;
    if (shdr->sh_flags & (1 << 2))
 return P_BYTES;
    switch (shdr->sh_entsize) {
      case 8: return P_XWORDS;
      case 4: return P_WORDS;
      case 2: return P_HALVES;
      case 1: return P_BYTES;
    }
    return P_SECTION;
}

canonical_original_code:
static int selectshtype(struct Elf64_Shdr *shdr, char *name)
{
    if (!strcmp(name, ".got"))
 return P_ADDRS;
    if (!strncmp(name, ".got.", 5))
 return P_ADDRS;
    if (!strncmp(name, ".ctors", 6))
 return P_ADDRS;
    if (!strncmp(name, ".dtors", 6))
 return P_ADDRS;
    if (shdr->sh_flags & (1 << 5))
 return P_STRINGS;
    if (shdr->sh_flags & (1 << 2))
 return P_BYTES;
    switch (shdr->sh_entsize) {
      case 8: return P_XWORDS;
      case 4: return P_WORDS;
      case 2: return P_HALVES;
      case 1: return P_BYTES;
    }
    return P_SECTION;
}

variable_types:
name
char *

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


enumfinalize

decompiled_code:
void enumfinalize()
{
__int64 i; // rbp
__int64 v1; // rbx
const char *v2; // r12
size_t v3; // rax
const char *v4; // r13
const char **v5; // r12
__int64 v6; // r14

if ( sectionenumcount > 0 )
{
for ( i = 0LL; sectionenumcount > (int)i; ++i )
{
if ( !(unsigned int)lookupname(*(_QWORD *)(sectionenums + 16 * i), 0LL) )
{
v4 = *(const char **)(sectionenums + 16 * i);
if ( strcmp(v4, "SHN_COUNT") )
{
if ( (int)i <= 0 )
continue;
v5 = (const char **)sectionenums;
v6 = sectionenums + 16LL * (unsigned int)(i - 1) + 16;
while ( strcmp(v4, *v5) )
{
v5 += 2;
if ( v5 == (const char **)v6 )
goto LABEL_4;
}
}
}
v1 = sectionenums + 16 * i;
v2 = *(const char **)v1;
v3 = strlen(*(const char **)v1);
sprintf((char *)&v2[v3], "_%d", *(unsigned int *)(v1 + 8));
LABEL_4:
;
}
}
}

canonical_decompiled_code:
void func3()
{
__int64 i; // rbp
__int64 v1; // rbx
const char *v2; // r12
size_t v3; // rax
const char *v4; // r13
const char **v5; // r12
__int64 v6; // r14

if ( sectionenumcount > 0 )
{
for ( i = 0LL; sectionenumcount > (int)i; ++i )
{
if ( !(unsigned int)lookupname(*(_QWORD *)(sectionenums + 16 * i), 0LL) )
{
v4 = *(const char **)(sectionenums + 16 * i);
if ( strcmp(v4, "SHN_COUNT") )
{
if ( (int)i <= 0 )
continue;
v5 = (const char **)sectionenums;
v6 = sectionenums + 16LL * (unsigned int)(i - 1) + 16;
while ( strcmp(v4, *v5) )
{
v5 += 2;
if ( v5 == (const char **)v6 )
goto LABEL_4;
}
}
}
v1 = sectionenums + 16 * i;
v2 = *(const char **)v1;
v3 = strlen(*(const char **)v1);
sprintf((char *)&v2[v3], "_%d", *(unsigned int *)(v1 + 8));
LABEL_4:
;
}
}
}

original_code:
static void enumfinalize(void)
{
    int i, j;
    for (i = 0 ; i < sectionenumcount ; ++i) {
 if (lookupname(sectionenums[i].name, ((void *)0)))
     goto conflict;
 if (!strcmp(sectionenums[i].name, "SHN_" "COUNT"))
     goto conflict;
 for (j = 0 ; j < i ; ++j) {
     if (!strcmp(sectionenums[i].name, sectionenums[j].name))
  goto conflict;
 }
 continue;
      conflict:
 sprintf(sectionenums[i].name + strlen(sectionenums[i].name),
  "_%d", sectionenums[i].ndx);
    }
}

canonical_original_code:
static void enumfinalize()
{
    int i; int j;
    for (i = 0 ; i < sectionenumcount ; ++i) {
 if (lookupname(sectionenums[i].name, ((void *)0)))
     goto conflict;
 if (!strcmp(sectionenums[i].name, "SHN_" "COUNT"))
     goto conflict;
 for (j = 0 ; j < i ; ++j) {
     if (!strcmp(sectionenums[i].name, sectionenums[j].name))
  goto conflict;
 }
 continue;
      conflict:
 sprintf(sectionenums[i].name + strlen(sectionenums[i].name),
  "_%d", sectionenums[i].ndx);
    }
}

variable_types: j int


addenumname

decompiled_code:
__int64 __fastcall addenumname(unsigned int a1, const char *a2)
{
const char *v2; // rbx
int v3; // eax
char *v4; // r13
char v5; // bp
const unsigned __int16 **v6; // r14
char v7; // bp
__int64 v8; // r12
int v9; // eax
char v10; // al
int v11; // edx
__int64 result; // rax

v2 = a2;
v3 = strlen(a2);
v4 = (char *)allocate((unsigned int)(v3 + 16));
v5 = *a2;
if ( *a2 )
{
v6 = _ctype_b_loc();
while ( ((*v6)[v5] & 8) == 0 )
{
v5 = *++v2;
if ( !*v2 )
goto LABEL_5;
}
*(_DWORD *)v4 = 1598965843;
v7 = *v2;
if ( *v2 )
{
v8 = 1LL;
do
{
v10 = 95;
if ( ((*v6)[v7] & 8) != 0 )
v10 = (*_ctype_toupper_loc())[v7];
v4[v8 + 3] = v10;
v9 = v8++;
v7 = v2[v8 - 1];
}
while ( v7 );
}
else
{
v9 = 0;
}
v4[v9 + 4] = 0;
}
else
{
LABEL_5:
sprintf(v4, "SHN_%d", a1);
}
sectionenums = reallocate(sectionenums, (unsigned int)(16 * (sectionenumcount + 1)));
v11 = sectionenumcount;
result = 16LL * sectionenumcount + sectionenums;
*(_QWORD *)result = v4;
*(_DWORD *)(result + 8) = a1;
sectionenumcount = v11 + 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, const char *a2)
{
const char *v2; // rbx
int v3; // eax
char *v4; // r13
char v5; // bp
const unsigned __int16 **v6; // r14
char v7; // bp
__int64 v8; // r12
int v9; // eax
char v10; // al
int v11; // edx
__int64 result; // rax

v2 = a2;
v3 = strlen(a2);
v4 = (char *)allocate((unsigned int)(v3 + 16));
v5 = *a2;
if ( *a2 )
{
v6 = _ctype_b_loc();
while ( ((*v6)[v5] & 8) == 0 )
{
v5 = *++v2;
if ( !*v2 )
goto LABEL_5;
}
*(_DWORD *)v4 = 1598965843;
v7 = *v2;
if ( *v2 )
{
v8 = 1LL;
do
{
v10 = 95;
if ( ((*v6)[v7] & 8) != 0 )
v10 = (*_ctype_toupper_loc())[v7];
v4[v8 + 3] = v10;
v9 = v8++;
v7 = v2[v8 - 1];
}
while ( v7 );
}
else
{
v9 = 0;
}
v4[v9 + 4] = 0;
}
else
{
LABEL_5:
sprintf(v4, "SHN_%d", a1);
}
sectionenums = reallocate(sectionenums, (unsigned int)(16 * (sectionenumcount + 1)));
v11 = sectionenumcount;
result = 16LL * sectionenumcount + sectionenums;
*(_QWORD *)result = v4;
*(_DWORD *)(result + 8) = a1;
sectionenumcount = v11 + 1;
return result;
}

original_code:
static void addenumname(int ndx, char const *name)
{
    char const *src;
    char *dest;
    int i;
    dest = allocate(16 + strlen(name));
    for (src = name ; *src ; ++src)
 if (((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short int) _ISalnum))
     break;
    if (*src) {
 memcpy(dest, "SHN_", 4);
 for (i = 0 ; src[i] ; ++i)
     dest[4 + i] = ((*__ctype_b_loc ())[(int) ((src[i]))] & (unsigned short int) _ISalnum) ? toupper(src[i]) : '_';
 dest[4 + i] = '\0';
    } else {
 sprintf(dest, "SHN_%d", ndx);
    }
    sectionenums = reallocate(sectionenums,
         (sectionenumcount + 1) * sizeof *sectionenums);
    sectionenums[sectionenumcount].name = dest;
    sectionenums[sectionenumcount].ndx = ndx;
    ++sectionenumcount;
}

canonical_original_code:
static void addenumname(int ndx, char *name)
{
    char *src;
    char *dest;
    int i;
    dest = allocate(16 + strlen(name));
    for (src = name ; *src ; ++src)
 if (((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short) _ISalnum))
     break;
    if (*src) {
 memcpy(dest, "SHN_", 4);
 for (i = 0 ; src[i] ; ++i)
     dest[4 + i] = ((*__ctype_b_loc ())[(int) ((src[i]))] & (unsigned short) _ISalnum) ? toupper(src[i]) : '_';
 dest[4 + i] = '\0';
    } else {
 sprintf(dest, "SHN_%d", ndx);
    }
    sectionenums = reallocate(sectionenums,
         (sectionenumcount + 1) * sizeof *sectionenums);
    sectionenums[sectionenumcount].name = dest;
    sectionenums[sectionenumcount].ndx = ndx;
    ++sectionenumcount;
}

variable_types: ndx int

variable_types:
src
char *

variable_types:
dest
char *