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

if ( *(__int64 *)((char *)&qword_1570 + _tls_get_addr(&nss_mcdb_st_tlsind) - 5232) )
{
if ( *(int *)((char *)&dword_1578 + _tls_get_addr(&nss_mcdb_st_tlsind) - 5232) )
{
v2 = _tls_get_addr(&nss_mcdb_st_tlsind);
result = (*(__int64 (__fastcall **)(char *, __int64))a1)((char *)&qword_1570 + v2 - 5232, a1);
}
else
{
*_errno_location() = 2;
**(_DWORD **)(a1 + 32) = 2;
result = 0LL;
}
}
else
{
**(_DWORD **)(a1 + 32) = *_errno_location();
result = 0xFFFFFFFFLL;
}
return result;
}

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

if ( *(__int64 *)((char *)&qword_1570 + _tls_get_addr(&nss_mcdb_st_tlsind) - 5232) )
{
if ( *(int *)((char *)&dword_1578 + _tls_get_addr(&nss_mcdb_st_tlsind) - 5232) )
{
v2 = _tls_get_addr(&nss_mcdb_st_tlsind);
result = (*(__int64 (__fastcall **)(char *, __int64))a1)((char *)&qword_1570 + v2 - 5232, a1);
}
else
{
*_errno_location() = 2;
**(_DWORD **)(a1 + 32) = 2;
result = 0LL;
}
}
else
{
**(_DWORD **)(a1 + 32) = *_errno_location();
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
__attribute__((__visibility__("internal"))) nss_status_t
nss_mcdb_getnetgrentnext(const struct nss_mcdb_vinfo * const restrict v)
{
    struct mcdb * const restrict m = &_nss_mcdb_st[NSS_DBTYPE_NETGROUP];
    if (__builtin_expect(m->map == ((void *)0), 0)) {
        *v->errnop = (*__errno_location ());
        return NSS_STATUS_UNAVAIL;
    }
    if (m->loop != 0) {
        return v->decode(m, v);
    }
    *v->errnop = (*__errno_location ()) = 2;
    return NSS_STATUS_NOTFOUND;
}

canonical_original_code:
enum nss_status nss_mcdb_getnetgrentnext(struct nss_mcdb_vinfo *v)
{
    struct mcdb *m = &_nss_mcdb_st[NSS_DBTYPE_NETGROUP];
    if (__builtin_expect(m->map == ((void *)0), 0)) {
        *v->errnop = (*__errno_location ());
        return NSS_STATUS_UNAVAIL;
    }
    if (m->loop != 0) {
        return v->decode(m, v);
    }
    *v->errnop = (*__errno_location ()) = 2;
    return NSS_STATUS_NOTFOUND;
}


repo: gstrauss/mcdb

decompiled_code:
__int64 __fastcall nss_mcdb_get_generic(unsigned int a1, __int64 a2)
{
unsigned int v2; // ebx
__int64 v4[11]; // [rsp+0h] [rbp-58h] BYREF

v4[0] = nss_mcdb_db_getshared(a1);
if ( v4[0] )
{
if ( (unsigned __int8)mcdb_findtagstart_h(
v4,
*(_QWORD *)(a2 + 40),
*(_QWORD *)(a2 + 48),
*(unsigned __int8 *)(a2 + 56))
&& (unsigned __int8)mcdb_findtagnext_h(
v4,
*(_QWORD *)(a2 + 40),
*(_QWORD *)(a2 + 48),
*(unsigned __int8 *)(a2 + 56)) )
{
v2 = (*(__int64 (__fastcall **)(__int64 *, __int64))a2)(v4, a2);
}
else
{
*_errno_location() = 2;
**(_DWORD **)(a2 + 32) = 2;
v2 = 0;
}
if ( !*(_QWORD *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind)) )
mcdb_mmap_thread_registration_h(v4, 0LL);
}
else
{
**(_DWORD **)(a2 + 32) = *_errno_location();
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, __int64 a2)
{
unsigned int v2; // ebx
__int64 v4[11]; // [rsp+0h] [rbp-58h] BYREF

v4[0] = func1(a1);
if ( v4[0] )
{
if ( (unsigned __int8)mcdb_findtagstart_h(
v4,
*(_QWORD *)(a2 + 40),
*(_QWORD *)(a2 + 48),
*(unsigned __int8 *)(a2 + 56))
&& (unsigned __int8)mcdb_findtagnext_h(
v4,
*(_QWORD *)(a2 + 40),
*(_QWORD *)(a2 + 48),
*(unsigned __int8 *)(a2 + 56)) )
{
v2 = (*(__int64 (__fastcall **)(__int64 *, __int64))a2)(v4, a2);
}
else
{
*_errno_location() = 2;
**(_DWORD **)(a2 + 32) = 2;
v2 = 0;
}
if ( !*(_QWORD *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind)) )
mcdb_mmap_thread_registration_h(v4, 0LL);
}
else
{
**(_DWORD **)(a2 + 32) = *_errno_location();
v2 = -1;
}
return v2;
}

original_code:
__attribute__((__visibility__("internal"))) nss_status_t
nss_mcdb_get_generic(const enum nss_dbtype dbtype,
                     const struct nss_mcdb_vinfo * const restrict v)
{
    struct mcdb m;
    nss_status_t status;
    m.map = _nss_mcdb_db_getshared(dbtype);
    if (__builtin_expect(m.map == ((void *)0), 0)) {
        *v->errnop = (*__errno_location ());
        return NSS_STATUS_UNAVAIL;
    }
    if ( __builtin_expect( mcdb_findtagstart_h(&m,v->key,v->klen,v->tagc), 1)
        && __builtin_expect( mcdb_findtagnext_h(&m,v->key,v->klen,v->tagc), 1))
        status = v->decode(&m, v);
    else {
        status = NSS_STATUS_NOTFOUND;
        *v->errnop = (*__errno_location ()) = 2;
    }
    if (_nss_mcdb_st[dbtype].map == ((void *)0))
        mcdb_mmap_thread_registration_h(&(m.map), MCDB_REGISTER_USE_DECR);
    return status;
}

canonical_original_code:
enum nss_status nss_mcdb_get_generic(enum nss_dbtype dbtype, struct nss_mcdb_vinfo *v)
{
    struct mcdb m;
    enum nss_status status;
    m.map = _nss_mcdb_db_getshared(dbtype);
    if (__builtin_expect(m.map == ((void *)0), 0)) {
        *v->errnop = (*__errno_location ());
        return NSS_STATUS_UNAVAIL;
    }
    if ( __builtin_expect( mcdb_findtagstart_h(&m,v->key,v->klen,v->tagc), 1)
        && __builtin_expect( mcdb_findtagnext_h(&m,v->key,v->klen,v->tagc), 1))
        status = v->decode(&m, v);
    else {
        status = NSS_STATUS_NOTFOUND;
        *v->errnop = (*__errno_location ()) = 2;
    }
    if (_nss_mcdb_st[dbtype].map == ((void *)0))
        mcdb_mmap_thread_registration_h(&(m.map), MCDB_REGISTER_USE_DECR);
    return status;
}

variable_types: enum nss_dbtype { NSS_DBTYPE_ALIASES=0, NSS_DBTYPE_ETHERS=1, NSS_DBTYPE_GROUP=2, NSS_DBTYPE_HOSTS=3, NSS_DBTYPE_NETGROUP=4, NSS_DBTYPE_NETWORKS=5, NSS_DBTYPE_PASSWD=6, NSS_DBTYPE_PROTOCOLS=7, NSS_DBTYPE_PUBLICKEY=8, NSS_DBTYPE_RPC=9, NSS_DBTYPE_SERVICES=10, NSS_DBTYPE_SHADOW=11, NSS_DBTYPE_SENTINEL=12 }

variable_types: struct nss_mcdb_vinfo { enum nss_status { NSS_STATUS_TRYAGAIN=-2, NSS_STATUS_UNAVAIL=-1, NSS_STATUS_NOTFOUND=0, NSS_STATUS_SUCCESS=1, NSS_STATUS_RETURN=2 } (*decode)(struct mcdb { struct mcdb_mmap { unsigned char *ptr; unsigned int b; unsigned int n; unsigned int hash_init; unsigned int hash_pad; unsigned int (*hash_fn)(unsigned int, void *, unsigned long); unsigned long size; long mtime; struct mcdb_mmap *next; void *(*fn_malloc)(unsigned long); void (*fn_free)(void *); char *fname; char fnamebuf[112]; int allocated; int dfd; unsigned int refcnt; } *map; unsigned int loop; unsigned int hslots; unsigned long kpos; unsigned long hpos; unsigned long dpos; unsigned int dlen; unsigned int klen; unsigned int khash; unsigned int pad0; void *vp; } *, struct nss_mcdb_vinfo *); void *vstruct; char *buf; unsigned long bufsz; int *errnop; char *key; unsigned long klen; unsigned char tagc; } *

user_defined_types: struct mcdb { struct mcdb_mmap *map; unsigned int loop; unsigned int hslots; unsigned long kpos; unsigned long hpos; unsigned long dpos; unsigned int dlen; unsigned int klen; unsigned int khash; unsigned int pad0; void *vp; }

user_defined_types: struct nss_mcdb_vinfo { enum nss_status (*decode)(struct mcdb *, struct nss_mcdb_vinfo *); void *vstruct; char *buf; unsigned long bufsz; int *errnop; char *key; unsigned long klen; unsigned char tagc; }


original_code:
__attribute__((__visibility__("internal"))) nss_status_t
nss_mcdb_buf_decode(struct mcdb * const restrict m,
                    const struct nss_mcdb_vinfo * const restrict v)
{
    const size_t dlen = ((m)->dlen);
    if (dlen < v->bufsz) {
        memcpy(v->buf, ((m)->map->ptr+(m)->dpos), dlen);
        v->buf[dlen] = '\0';
        return NSS_STATUS_SUCCESS;
    }
    *v->errnop = (*__errno_location ()) = 34;
    return NSS_STATUS_TRYAGAIN;
}

canonical_original_code:
enum nss_status nss_mcdb_buf_decode(struct mcdb *m, struct nss_mcdb_vinfo *v)
{
    unsigned long dlen = ((m)->dlen);
    if (dlen < v->bufsz) {
        memcpy(v->buf, ((m)->map->ptr+(m)->dpos), dlen);
        v->buf[dlen] = '\0';
        return NSS_STATUS_SUCCESS;
    }
    *v->errnop = (*__errno_location ()) = 34;
    return NSS_STATUS_TRYAGAIN;
}

variable_types: struct mcdb { struct mcdb_mmap { unsigned char *ptr; unsigned int b; unsigned int n; unsigned int hash_init; unsigned int hash_pad; unsigned int (*hash_fn)(unsigned int, void *, unsigned long); unsigned long size; long mtime; struct mcdb_mmap *next; void *(*fn_malloc)(unsigned long); void (*fn_free)(void *); char *fname; char fnamebuf[112]; int allocated; int dfd; unsigned int refcnt; } *map; unsigned int loop; unsigned int hslots; unsigned long kpos; unsigned long hpos; unsigned long dpos; unsigned int dlen; unsigned int klen; unsigned int khash; unsigned int pad0; void *vp; } *

variable_types: struct nss_mcdb_vinfo { enum nss_status { NSS_STATUS_TRYAGAIN=-2, NSS_STATUS_UNAVAIL=-1, NSS_STATUS_NOTFOUND=0, NSS_STATUS_SUCCESS=1, NSS_STATUS_RETURN=2 } (*decode)(struct mcdb *, struct nss_mcdb_vinfo *); void *vstruct; char *buf; unsigned long bufsz; int *errnop; char *key; unsigned long klen; unsigned char tagc; } *

variable_types:
dlen
unsigned long


decompiled_code:
__int64 __fastcall nss_mcdb_refresh_check(unsigned int a1)
{
__int64 v1; // rdi
__int64 result; // rax

if ( a1 <= 0xB && (v1 = nss_mcdb_mmap[a1]) != 0 )
result = mcdb_mmap_refresh_check_h(v1) & 1;
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1)
{
__int64 v1; // rdi
__int64 result; // rax

if ( a1 <= 0xB && (v1 = nss_mcdb_mmap[a1]) != 0 )
result = mcdb_mmap_refresh_check_h(v1) & 1;
else
result = 0LL;
return result;
}

original_code:
_Bool
nss_mcdb_refresh_check(const enum nss_dbtype dbtype)
{
    return (0 <= (int)dbtype && dbtype < NSS_DBTYPE_SENTINEL)
        && _nss_mcdb_mmap[dbtype] != ((void *)0)
        && mcdb_mmap_refresh_check_h(_nss_mcdb_mmap[dbtype]);
}

canonical_original_code:
_Bool nss_mcdb_refresh_check(enum nss_dbtype dbtype)
{
    return (0 <= (int)dbtype && dbtype < NSS_DBTYPE_SENTINEL)
        && _nss_mcdb_mmap[dbtype] != ((void *)0)
        && mcdb_mmap_refresh_check_h(_nss_mcdb_mmap[dbtype]);
}

user_defined_types: enum nss_dbtype { NSS_DBTYPE_ALIASES=0, NSS_DBTYPE_ETHERS=1, NSS_DBTYPE_GROUP=2, NSS_DBTYPE_HOSTS=3, NSS_DBTYPE_NETGROUP=4, NSS_DBTYPE_NETWORKS=5, NSS_DBTYPE_PASSWD=6, NSS_DBTYPE_PROTOCOLS=7, NSS_DBTYPE_PUBLICKEY=8, NSS_DBTYPE_RPC=9, NSS_DBTYPE_SERVICES=10, NSS_DBTYPE_SHADOW=11, NSS_DBTYPE_SENTINEL=12 }


decompiled_code:
__int64 __fastcall uint32_hash_djb(unsigned int a1, unsigned __int8 *a2, __int64 a3)
{
__int64 result; // rax
unsigned __int8 *v4; // rdx

result = a1;
v4 = &a2[a3];
if ( a2 < v4 )
{
do
result = *a2++ ^ (unsigned int)(33 * result);
while ( v4 != a2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, unsigned __int8 *a2, __int64 a3)
{
__int64 result; // rax
unsigned __int8 *v4; // rdx

result = a1;
v4 = &a2[a3];
if ( a2 < v4 )
{
do
result = *a2++ ^ (unsigned int)(33 * result);
while ( v4 != a2 );
}
return result;
}

original_code:
inline
uint32_t
uint32_hash_djb(uint32_t h, const void * const restrict vbuf, const size_t sz)
{
    const unsigned char * restrict buf = (const unsigned char *)vbuf;
    const unsigned char * const e = (const unsigned char *)vbuf + sz;
    for (; __builtin_expect( (buf < e), 1); ++buf)
        h = (((h) + ((h) << 5)) ^ (*buf));
    return h;
}

canonical_original_code:
inline unsigned int uint32_hash_djb(unsigned int h, void *vbuf, unsigned long sz)
{
    unsigned char *buf = (const unsigned char *)vbuf;
    unsigned char *e = (const unsigned char *)vbuf + sz;
    for (; __builtin_expect( (buf < e), 1); ++buf)
        h = (((h) + ((h) << 5)) ^ (*buf));
    return h;
}

variable_types:
h
unsigned int

variable_types:
vbuf
void *

variable_types:
sz
unsigned long

variable_types:
buf
unsigned char *

variable_types:
e
unsigned char *

return_type: unsigned int


decompiled_code:
__int64 __fastcall uint32_hash_identity(__int64 a1, unsigned int *a2)
{
return *a2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int *a2)
{
return *a2;
}

original_code:
inline
uint32_t
uint32_hash_identity(uint32_t h __attribute__((__unused__)),
                     const void * const restrict vbuf,
                     const size_t sz __attribute__((__unused__)))
{
    return *(uint32_t *)vbuf;
}

canonical_original_code:
inline unsigned int uint32_hash_identity(unsigned int h, void *vbuf, unsigned long sz)
{
    return *(unsigned int *)vbuf;
}


variable_types:
buf
char *

variable_types:
b
unsigned char *


return_type: unsigned short


decompiled_code:
__int64 __fastcall uint32_to_ascii_base10_loop(unsigned int a1, void *a2)
{
__int64 v2; // rdx
int v3; // er8
unsigned int v4; // ecx
unsigned int v5; // ebx
char src[12]; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned __int64 v8; // [rsp+18h] [rbp-10h]

v8 = __readfsqword(0x28u);
v2 = 11LL;
do
{
v3 = v2;
src[v2] = a1 % 0xA + 48;
v4 = a1;
a1 /= 0xAu;
--v2;
}
while ( v4 > 9 );
v5 = 12 - v3;
memcpy(a2, &src[v3], 12 - v3);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, void *a2)
{
__int64 v2; // rdx
int v3; // er8
unsigned int v4; // ecx
unsigned int v5; // ebx
char src[12]; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned __int64 v8; // [rsp+18h] [rbp-10h]

v8 = __readfsqword(0x28u);
v2 = 11LL;
do
{
v3 = v2;
src[v2] = a1 % 0xA + 48;
v4 = a1;
a1 /= 0xAu;
--v2;
}
while ( v4 > 9 );
v5 = 12 - v3;
memcpy(a2, &src[v3], 12 - v3);
return v5;
}

original_code:
uint32_t
uint32_to_ascii_base10_loop (uint32_t x, char * const restrict buf)
{
    int i = 12;
    char b[12];
    do { b[--i] = (char)((x % 10) + '0'); } while ((x /= 10));
    memcpy(buf, b+i, 12-i);
    return (uint32_t)(12-i);
}

canonical_original_code:
unsigned int uint32_to_ascii_base10_loop(unsigned int x, char *buf)
{
    int i = 12;
    char b[12];
    do { b[--i] = (char)((x % 10) + '0'); } while ((x /= 10));
    memcpy(buf, b+i, 12-i);
    return (unsigned int)(12-i);
}

variable_types:
x
unsigned int

variable_types: i int

variable_types:
b
char [12]


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

if ( a1 <= 9 )
{
*a2 = a1 + 48;
result = 1LL;
}
else if ( a1 > 0x63 )
{
result = uint32_to_ascii_base10_loop(a1, a2);
}
else
{
*a2 = a1 / 0xA + 48;
a2[1] = a1 % 0xA + 48;
result = 2LL;
}
return result;
}

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

if ( a1 <= 9 )
{
*a2 = a1 + 48;
result = 1LL;
}
else if ( a1 > 0x63 )
{
result = func8(a1, a2);
}
else
{
*a2 = a1 / 0xA + 48;
a2[1] = a1 % 0xA + 48;
result = 2LL;
}
return result;
}

original_code:
inline
uint32_t
uint32_to_ascii_base10 (const uint32_t x, char * const restrict buf)
{
    if (x < 10) {
        buf[0] = (char)(x + '0');
        return 1;
    }
    else if (x < 100) {
        buf[0] = (char)((x / 10) + '0');
        buf[1] = (char)((x % 10) + '0');
        return 2;
    }
    else {
        return uint32_to_ascii_base10_loop(x, buf);
    }
}

canonical_original_code:
inline unsigned int uint32_to_ascii_base10(unsigned int x, char *buf)
{
    if (x < 10) {
        buf[0] = (char)(x + '0');
        return 1;
    }
    else if (x < 100) {
        buf[0] = (char)((x / 10) + '0');
        buf[1] = (char)((x % 10) + '0');
        return 2;
    }
    else {
        return uint32_to_ascii_base10_loop(x, buf);
    }
}


decompiled_code:
__int64 __fastcall int32_to_ascii_base10(signed int a1, _BYTE *a2)
{
unsigned int v2; // ebx
__int64 result; // rax

v2 = (unsigned int)a1 >> 31;
if ( a1 >= 0 )
return v2 + (unsigned int)uint32_to_ascii_base10(a1, a2);
if ( a1 != 0x80000000 )
{
a1 = -a1;
*a2++ = 45;
return v2 + (unsigned int)uint32_to_ascii_base10(a1, a2);
}
for ( result = 0LL; result != 11; ++result )
a2[result] = LC0[result];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(signed int a1, _BYTE *a2)
{
unsigned int v2; // ebx
__int64 result; // rax

v2 = (unsigned int)a1 >> 31;
if ( a1 >= 0 )
return v2 + (unsigned int)func9(a1, a2);
if ( a1 != 0x80000000 )
{
a1 = -a1;
*a2++ = 45;
return v2 + (unsigned int)func9(a1, a2);
}
for ( result = 0LL; result != 11; ++result )
a2[result] = LC0[result];
return result;
}

original_code:
inline
uint32_t
int32_to_ascii_base10 (int32_t x, char * restrict buf)
{
    const uint32_t pre = (x < 0);
    if (pre) {
        if (__builtin_expect( (x != (int32_t)0x80000000u), 1)) {
            x = -x;
            *buf++ = '-';
        }
        else {

            int i; for (i=0; i < 11; ++i) buf[i] = ("-2147483648")[i];
            return 11;
        }
    }
    return pre + uint32_to_ascii_base10((uint32_t)x, buf);
}

canonical_original_code:
inline unsigned int int32_to_ascii_base10(int x, char *buf)
{
    unsigned int pre = (x < 0);
    if (pre) {
        if (__builtin_expect( (x != (int)0x80000000u), 1)) {
            x = -x;
            *buf++ = '-';
        }
        else {

            int i; for (i=0; i < 11; ++i) buf[i] = ("-2147483648")[i];
            return 11;
        }
    }
    return pre + uint32_to_ascii_base10((unsigned int)x, buf);
}

variable_types: x int

variable_types:
pre
unsigned int


decompiled_code:
__int64 __fastcall mcdb_bufread_number(__int64 *a1, unsigned __int64 *a2)
{
unsigned __int64 *v2; // rbx
__int64 v3; // r11
__int64 v4; // r8
__int64 v5; // r9
unsigned int v6; // eax
__int64 v7; // rdx
unsigned __int64 v8; // rcx

v2 = a2;
v3 = a1[1];
v4 = *a1;
v5 = a1[2];
if ( v3 == v5 || (v6 = *(char *)(v4 + v3) - 48, v6 > 9) )
{
*a2 = 0LL;
LODWORD(a2) = 0;
return (unsigned int)a2;
}
v7 = a1[1];
v8 = 0LL;
do
{
++v7;
v8 = (int)v6 + 10 * v8;
if ( v5 == v7 )
{
*v2 = v8;
LABEL_11:
a1[1] = v7;
LODWORD(a2) = 1;
return (unsigned int)a2;
}
v6 = *(char *)(v4 + v7) - 48;
LOBYTE(a2) = v6 <= 9 && v8 <= 0xCCCCCCB;
}
while ( (_BYTE)a2 );
*v2 = v8;
if ( v3 != v7 && (unsigned int)(*(char *)(v4 + v7) - 48) > 9 )
goto LABEL_11;
return (unsigned int)a2;
}

original_code:
static _Bool
mcdb_bufread_number (struct mcdb_input * const restrict b,
                     size_t * const restrict rv)
{
    size_t num = 0;
    size_t pos = b->pos;
    const char * const buf = b->buf;
    const size_t datasz = b->datasz;
    while (datasz != pos && ((uint32_t)(buf[pos]-'0'))<=9u && num <=214748363uL)
        num = num * 10 + (buf[pos++]-'0');
    *rv = num;
    if (b->pos != pos && (datasz == pos || ((uint32_t)(buf[pos]-'0')) > 9u)) {
        b->pos = pos;
        return 1;
    }
    return 0;
}

canonical_original_code:
static _Bool mcdb_bufread_number(struct mcdb_input *b, unsigned long *rv)
{
    unsigned long num = 0;
    unsigned long pos = b->pos;
    char *buf = b->buf;
    unsigned long datasz = b->datasz;
    while (datasz != pos && ((unsigned int)(buf[pos]-'0'))<=9u && num <=214748363uL)
        num = num * 10 + (buf[pos++]-'0');
    *rv = num;
    if (b->pos != pos && (datasz == pos || ((unsigned int)(buf[pos]-'0')) > 9u)) {
        b->pos = pos;
        return 1;
    }
    return 0;
}

variable_types: struct mcdb_input { char *buf; unsigned long pos; unsigned long datasz; unsigned long bufsz; int fd; } *

variable_types:
rv
unsigned long *

variable_types:
num
unsigned long

variable_types:
pos
unsigned long

variable_types:
datasz
unsigned long


original_code:
static ssize_t
mcdb_bufread_fd (struct mcdb_input * const restrict b)
{
    ssize_t r;
    if (b->fd == -1) return (ssize_t)-1;
    if (b->datasz - b->pos < 128 && b->pos != 0) {
        if ((b->datasz -= b->pos))
            memmove(b->buf, b->buf + b->pos, b->datasz);
        b->pos = 0;
    }
    do {((r = read(b->fd, b->buf + b->datasz, b->bufsz - b->datasz)));} while (__builtin_expect(((r == -1)),0) && __builtin_expect((*__errno_location ())==4, 1));
    if (r > 0) b->datasz += r;
    return r;
}

canonical_original_code:
static long mcdb_bufread_fd(struct mcdb_input *b)
{
    long r;
    if (b->fd == -1) return (long)-1;
    if (b->datasz - b->pos < 128 && b->pos != 0) {
        if ((b->datasz -= b->pos))
            memmove(b->buf, b->buf + b->pos, b->datasz);
        b->pos = 0;
    }
    do {((r = read(b->fd, b->buf + b->datasz, b->bufsz - b->datasz)));} while (__builtin_expect(((r == -1)),0) && __builtin_expect((*__errno_location ())==4, 1));
    if (r > 0) b->datasz += r;
    return r;
}

variable_types: r long

return_type: long

user_defined_types: struct mcdb_input { char *buf; unsigned long pos; unsigned long datasz; unsigned long bufsz; int fd; }


decompiled_code:
bool __fastcall mcdb_bufread_xchars(__int64 a1, unsigned __int64 a2)
{
while ( *(_QWORD *)(a1 + 16) - *(_QWORD *)(a1 + 8) < a2 && mcdb_bufread_fd(a1) > 0 )
;
return *(_QWORD *)(a1 + 16) - *(_QWORD *)(a1 + 8) >= a2;
}

original_code:
static _Bool
mcdb_bufread_xchars (struct mcdb_input * const restrict b, const size_t len)
{
    while (b->datasz - b->pos < len && mcdb_bufread_fd(b) > 0)
        ;
    return (b->datasz - b->pos >= len);
}

canonical_original_code:
static _Bool mcdb_bufread_xchars(struct mcdb_input *b, unsigned long len)
{
    while (b->datasz - b->pos < len && mcdb_bufread_fd(b) > 0)
        ;
    return (b->datasz - b->pos >= len);
}

variable_types:
len
unsigned long


original_code:
static ssize_t
mcdb_bufread_preamble_fill (struct mcdb_input * const restrict b)
{
    char * const buf = b->buf;
    ssize_t r = b->datasz - b->pos;
    while (memchr(buf + b->datasz - r, ':',(size_t)r) == ((void *)0)
           && (r == 0 || buf[b->pos] != '\n')
           && (r = mcdb_bufread_fd(b)) > 0
           && b->datasz - b->pos < 23)
        ;
    return r;
}

canonical_original_code:
static long mcdb_bufread_preamble_fill(struct mcdb_input *b)
{
    char *buf = b->buf;
    long r = b->datasz - b->pos;
    while (memchr(buf + b->datasz - r, ':',(unsigned long)r) == ((void *)0)
           && (r == 0 || buf[b->pos] != '\n')
           && (r = mcdb_bufread_fd(b)) > 0
           && b->datasz - b->pos < 23)
        ;
    return r;
}


decompiled_code:
__int64 __fastcall mcdb_bufread_preamble(__int64 *a1, unsigned __int64 *a2, unsigned __int64 *a3)
{
__int64 v4; // rdx
__int64 v5; // rax
char v6; // dl
__int64 result; // rax
__int64 v8; // rax
__int64 v9; // rdx
__int64 v10; // rax
__int64 v11; // rdx

if ( (unsigned __int64)(a1[2] - a1[1]) <= 0x16 && mcdb_bufread_preamble_fill(a1) <= 0 )
return (unsigned int)(*_errno_location() == 0) - 2;
v4 = *a1;
v5 = a1[1];
a1[1] = v5 + 1;
v6 = *(_BYTE *)(v4 + v5);
result = 0LL;
if ( v6 != 10 )
{
result = 0xFFFFFFFFLL;
if ( v6 == 43 )
{
if ( (unsigned __int8)mcdb_bufread_number(a1, a2) )
{
v8 = a1[1];
if ( a1[2] == v8 )
{
result = 0xFFFFFFFFLL;
}
else
{
v9 = *a1;
a1[1] = v8 + 1;
if ( *(_BYTE *)(v9 + v8) == 44 )
{
if ( (unsigned __int8)mcdb_bufread_number(a1, a3) )
{
v10 = a1[1];
if ( a1[2] == v10 )
{
result = 0xFFFFFFFFLL;
}
else
{
v11 = *a1;
a1[1] = v10 + 1;
result = 2 * (unsigned int)(*(_BYTE *)(v11 + v10) == 58) - 1;
}
}
else
{
result = 0xFFFFFFFFLL;
}
}
else
{
result = 0xFFFFFFFFLL;
}
}
}
else
{
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 *a1, unsigned __int64 *a2, unsigned __int64 *a3)
{
__int64 v4; // rdx
__int64 v5; // rax
char v6; // dl
__int64 result; // rax
__int64 v8; // rax
__int64 v9; // rdx
__int64 v10; // rax
__int64 v11; // rdx

if ( (unsigned __int64)(a1[2] - a1[1]) <= 0x16 && func5(a1) <= 0 )
return (unsigned int)(*_errno_location() == 0) - 2;
v4 = *a1;
v5 = a1[1];
a1[1] = v5 + 1;
v6 = *(_BYTE *)(v4 + v5);
result = 0LL;
if ( v6 != 10 )
{
result = 0xFFFFFFFFLL;
if ( v6 == 43 )
{
if ( (unsigned __int8)func0(a1, a2) )
{
v8 = a1[1];
if ( a1[2] == v8 )
{
result = 0xFFFFFFFFLL;
}
else
{
v9 = *a1;
a1[1] = v8 + 1;
if ( *(_BYTE *)(v9 + v8) == 44 )
{
if ( (unsigned __int8)func0(a1, a3) )
{
v10 = a1[1];
if ( a1[2] == v10 )
{
result = 0xFFFFFFFFLL;
}
else
{
v11 = *a1;
a1[1] = v10 + 1;
result = 2 * (unsigned int)(*(_BYTE *)(v11 + v10) == 58) - 1;
}
}
else
{
result = 0xFFFFFFFFLL;
}
}
else
{
result = 0xFFFFFFFFLL;
}
}
}
else
{
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

original_code:
static int
mcdb_bufread_preamble (struct mcdb_input * const restrict b,
                       size_t * const restrict klen,
                       size_t * const restrict dlen)
{
    if (b->datasz - b->pos < 23 && mcdb_bufread_preamble_fill(b) <= 0)
        return ((*__errno_location ()) == 0 ? MCDB_ERROR_READFORMAT : MCDB_ERROR_READ);
    switch (b->buf[b->pos++]) {
      case '+': break;
      case '\n': return 0;
      default: return MCDB_ERROR_READFORMAT;
    }
    return ( mcdb_bufread_number(b,klen)
            && b->datasz - b->pos != 0 && b->buf[b->pos++] == ','
            && mcdb_bufread_number(b,dlen)
            && b->datasz - b->pos != 0 && b->buf[b->pos++] == ':')
            ? 1
            : MCDB_ERROR_READFORMAT;
}

canonical_original_code:
static int mcdb_bufread_preamble(struct mcdb_input *b, unsigned long *klen, unsigned long *dlen)
{
    if (b->datasz - b->pos < 23 && mcdb_bufread_preamble_fill(b) <= 0)
        return ((*__errno_location ()) == 0 ? MCDB_ERROR_READFORMAT : MCDB_ERROR_READ);
    switch (b->buf[b->pos++]) {
      case '+': break;
      case '\n': return 0;
      default: return MCDB_ERROR_READFORMAT;
    }
    return ( mcdb_bufread_number(b,klen)
            && b->datasz - b->pos != 0 && b->buf[b->pos++] == ','
            && mcdb_bufread_number(b,dlen)
            && b->datasz - b->pos != 0 && b->buf[b->pos++] == ':')
            ? 1
            : MCDB_ERROR_READFORMAT;
}

variable_types:
klen
unsigned long *

variable_types:
dlen
unsigned long *

return_type: int


decompiled_code:
__int64 __fastcall mcdb_makefmt_fileintofile(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
int v6; // eax
unsigned int v7; // ebx
int *v8; // rbp
int v9; // er12
unsigned int v10; // ebx
void *v11; // r14
struct stat64 v13; // [rsp+0h] [rbp-B8h] BYREF

v6 = nointr_open(a1, 0LL, 0LL);
if ( v6 == -1 )
return (unsigned int)-2;
v7 = v6;
if ( fstat64(v6, &v13) == -1 )
goto LABEL_5;
if ( (v13.st_mode & 0xF000) != 0x8000 )
{
*_errno_location() = 22;
goto LABEL_5;
}
v11 = mmap64(0LL, v13.st_size, 1, 2, v7, 0LL);
if ( v11 == (void *)-1LL )
{
LABEL_5:
v8 = _errno_location();
v9 = *v8;
nointr_close(v7);
v10 = -2;
if ( v9 )
{
*v8 = v9;
v10 = -2;
}
return v10;
}
if ( (unsigned int)nointr_close(v7) )
{
v10 = -2;
}
else
{
posix_madvise(v11, v13.st_size, 3);
v10 = mcdb_makefmt_fdintofile(-1, (__int64)v11, v13.st_size, a2, a3, a4);
}
munmap(v11, v13.st_size);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
int v6; // eax
unsigned int v7; // ebx
int *v8; // rbp
int v9; // er12
unsigned int v10; // ebx
void *v11; // r14
struct stat64 v13; // [rsp+0h] [rbp-B8h] BYREF

v6 = nointr_open(a1, 0LL, 0LL);
if ( v6 == -1 )
return (unsigned int)-2;
v7 = v6;
if ( fstat64(v6, &v13) == -1 )
goto LABEL_5;
if ( (v13.st_mode & 0xF000) != 0x8000 )
{
*_errno_location() = 22;
goto LABEL_5;
}
v11 = mmap64(0LL, v13.st_size, 1, 2, v7, 0LL);
if ( v11 == (void *)-1LL )
{
LABEL_5:
v8 = _errno_location();
v9 = *v8;
nointr_close(v7);
v10 = -2;
if ( v9 )
{
*v8 = v9;
v10 = -2;
}
return v10;
}
if ( (unsigned int)nointr_close(v7) )
{
v10 = -2;
}
else
{
posix_madvise(v11, v13.st_size, 3);
v10 = func8(-1, (__int64)v11, v13.st_size, a2, a3, a4);
}
munmap(v11, v13.st_size);
return v10;
}

original_code:
__attribute__ ((__noinline__))
int
mcdb_makefmt_fileintofile (const char * const restrict infile,
                           const char * const restrict fname,
                           void * (* const fn_malloc)(size_t),
                           void (* const fn_free)(void *))
{
    void * restrict x = ((void *) -1);
    int rv = MCDB_ERROR_READ;
    int fd;
    struct stat st;
    int errsave = 0;
    if ((fd = nointr_open(infile,00,0)) == -1)
        return MCDB_ERROR_READ;
    if (fstat(fd, &st) == -1
        || (!((((st.st_mode)) & 0170000) == (0100000)) ? ((*__errno_location ()) = 22) : 0)
        || ((x = mmap(0, (size_t)st.st_size, 0x1, 0x02, fd, 0))
            == ((void *) -1)))
        errsave = (*__errno_location ());
    if (nointr_close(fd) == 0 && x != ((void *) -1)) {
        posix_madvise(x, (size_t)st.st_size, 3);
        rv = mcdb_makefmt_fdintofile(-1, x, (size_t)st.st_size,
                                     fname, fn_malloc, fn_free);
    }
    if (x != ((void *) -1))
        munmap(x, (size_t)st.st_size);
    else if (errsave != 0)
        (*__errno_location ()) = errsave;
    return rv;
}

canonical_original_code:
int mcdb_makefmt_fileintofile(char *infile, char *fname, void *(*fn_malloc)(unsigned long), void (*fn_free)(void *))
{
    void *x = ((void *) -1);
    int rv = MCDB_ERROR_READ;
    int fd;
    struct stat st;
    int errsave = 0;
    if ((fd = nointr_open(infile,00,0)) == -1)
        return MCDB_ERROR_READ;
    if (fstat(fd, &st) == -1
        || (!((((st.st_mode)) & 0170000) == (0100000)) ? ((*__errno_location ()) = 22) : 0)
        || ((x = mmap(0, (unsigned long)st.st_size, 0x1, 0x02, fd, 0))
            == ((void *) -1)))
        errsave = (*__errno_location ());
    if (nointr_close(fd) == 0 && x != ((void *) -1)) {
        posix_madvise(x, (unsigned long)st.st_size, 3);
        rv = mcdb_makefmt_fdintofile(-1, x, (unsigned long)st.st_size,
                                     fname, fn_malloc, fn_free);
    }
    if (x != ((void *) -1))
        munmap(x, (unsigned long)st.st_size);
    else if (errsave != 0)
        (*__errno_location ()) = errsave;
    return rv;
}

variable_types:
infile
char *

variable_types:
fname
char *

variable_types:
x
void *

variable_types: rv int

variable_types: fd int

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

variable_types: errsave int

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


decompiled_code:
__int64 __fastcall mcdb_error(unsigned int a1, const char *a2, const char *a3)
{
__int64 result; // rax
int *v4; // rax
int *v5; // rax
int *v6; // rax
char v7[128]; // [rsp+8h] [rbp-90h] BYREF
unsigned __int64 v8; // [rsp+88h] [rbp-10h]

v8 = __readfsqword(0x28u);
result = a1;
switch ( a1 )
{
case 0xFFFFFFFB:
_fprintf_chk(stderr, 1LL, "%s: %s", a2, a3);
result = 101LL;
break;
case 0xFFFFFFFC:
v6 = _errno_location();
if ( (unsigned int)_xpg_strerror_r((unsigned int)*v6, v7, 128LL) )
v7[0] = 0;
_fprintf_chk(stderr, 1LL, "%s: malloc: %s\n", a2, v7);
result = 111LL;
break;
case 0xFFFFFFFD:
v5 = _errno_location();
if ( (unsigned int)_xpg_strerror_r((unsigned int)*v5, v7, 128LL) )
v7[0] = 0;
_fprintf_chk(stderr, 1LL, "%s: write output: %s\n", a2, v7);
result = 111LL;
break;
case 0xFFFFFFFE:
v4 = _errno_location();
if ( (unsigned int)_xpg_strerror_r((unsigned int)*v4, v7, 128LL) )
v7[0] = 0;
_fprintf_chk(stderr, 1LL, "%s: read input: %s\n", a2, v7);
result = 111LL;
break;
case 0xFFFFFFFF:
_fprintf_chk(stderr, 1LL, "%s: read input: bad format\n", a2);
result = 111LL;
break;
case 0u:
return result;
default:
_fprintf_chk(stderr, 1LL, "%s: (unknown error)\n", a2);
result = 111LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, const char *a2, const char *a3)
{
__int64 result; // rax
int *v4; // rax
int *v5; // rax
int *v6; // rax
char v7[128]; // [rsp+8h] [rbp-90h] BYREF
unsigned __int64 v8; // [rsp+88h] [rbp-10h]

v8 = __readfsqword(0x28u);
result = a1;
switch ( a1 )
{
case 0xFFFFFFFB:
_fprintf_chk(stderr, 1LL, "%s: %s", a2, a3);
result = 101LL;
break;
case 0xFFFFFFFC:
v6 = _errno_location();
if ( (unsigned int)_xpg_strerror_r((unsigned int)*v6, v7, 128LL) )
v7[0] = 0;
_fprintf_chk(stderr, 1LL, "%s: malloc: %s\n", a2, v7);
result = 111LL;
break;
case 0xFFFFFFFD:
v5 = _errno_location();
if ( (unsigned int)_xpg_strerror_r((unsigned int)*v5, v7, 128LL) )
v7[0] = 0;
_fprintf_chk(stderr, 1LL, "%s: write output: %s\n", a2, v7);
result = 111LL;
break;
case 0xFFFFFFFE:
v4 = _errno_location();
if ( (unsigned int)_xpg_strerror_r((unsigned int)*v4, v7, 128LL) )
v7[0] = 0;
_fprintf_chk(stderr, 1LL, "%s: read input: %s\n", a2, v7);
result = 111LL;
break;
case 0xFFFFFFFF:
_fprintf_chk(stderr, 1LL, "%s: read input: bad format\n", a2);
result = 111LL;
break;
case 0u:
return result;
default:
_fprintf_chk(stderr, 1LL, "%s: (unknown error)\n", a2);
result = 111LL;
break;
}
return result;
}

original_code:
int
mcdb_error (const int rv, const char * const restrict prefix,
            const char * const restrict mcdb_usage)
{
    char errstr[128];
    switch (rv) {
      case 0:
        return 0;
      case MCDB_ERROR_READFORMAT:
        fprintf(stderr, "%s: read input: bad format\n", prefix);
        return 111;
      case MCDB_ERROR_READ:
        if (strerror_r((*__errno_location ()), errstr, sizeof(errstr)) != 0) errstr[0] = '\0';
        fprintf(stderr, "%s: read input: %s\n", prefix, errstr);
        return 111;
      case MCDB_ERROR_WRITE:
        if (strerror_r((*__errno_location ()), errstr, sizeof(errstr)) != 0) errstr[0] = '\0';
        fprintf(stderr, "%s: write output: %s\n", prefix, errstr);
        return 111;
      case MCDB_ERROR_MALLOC:
        if (strerror_r((*__errno_location ()), errstr, sizeof(errstr)) != 0) errstr[0] = '\0';
        fprintf(stderr, "%s: malloc: %s\n", prefix, errstr);
        return 111;
      case MCDB_ERROR_USAGE:
        fprintf(stderr, "%s: %s", prefix, mcdb_usage);
        return 101;
      default:
        fprintf(stderr, "%s: (unknown error)\n", prefix);
        return 111;
    }
}

canonical_original_code:
int mcdb_error(int rv, char *prefix, char *mcdb_usage)
{
    char errstr[128];
    switch (rv) {
      case 0:
        return 0;
      case MCDB_ERROR_READFORMAT:
        fprintf(stderr, "%s: read input: bad format\n", prefix);
        return 111;
      case MCDB_ERROR_READ:
        if (strerror_r((*__errno_location ()), errstr, sizeof(errstr)) != 0) errstr[0] = '\0';
        fprintf(stderr, "%s: read input: %s\n", prefix, errstr);
        return 111;
      case MCDB_ERROR_WRITE:
        if (strerror_r((*__errno_location ()), errstr, sizeof(errstr)) != 0) errstr[0] = '\0';
        fprintf(stderr, "%s: write output: %s\n", prefix, errstr);
        return 111;
      case MCDB_ERROR_MALLOC:
        if (strerror_r((*__errno_location ()), errstr, sizeof(errstr)) != 0) errstr[0] = '\0';
        fprintf(stderr, "%s: malloc: %s\n", prefix, errstr);
        return 111;
      case MCDB_ERROR_USAGE:
        fprintf(stderr, "%s: %s", prefix, mcdb_usage);
        return 101;
      default:
        fprintf(stderr, "%s: (unknown error)\n", prefix);
        return 111;
    }
}

variable_types:
prefix
char *

variable_types:
errstr
char [128]


decompiled_code:
__int64 __fastcall nointr_dup(int fd)
{
unsigned int v1; // ebx

do
v1 = dup(fd);
while ( v1 == -1 && *_errno_location() == 4 );
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd)
{
unsigned int v1; // ebx

do
v1 = dup(fd);
while ( v1 == -1 && *_errno_location() == 4 );
return v1;
}

original_code:
int
nointr_dup(const int fd)
{ int r; do {((r = dup(fd)));} while (__builtin_expect(((r == -1)),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

canonical_original_code:
int nointr_dup(int fd)
{ int r; do {((r = dup(fd)));} while (__builtin_expect(((r == -1)),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

variable_types: r int


decompiled_code:
__int64 __fastcall nointr_open(char *file, int oflag, unsigned int a3)
{
unsigned int v4; // ebx

do
v4 = open64(file, oflag, a3);
while ( v4 == -1 && *_errno_location() == 4 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *file, int oflag, unsigned int a3)
{
unsigned int v4; // ebx

do
v4 = open64(file, oflag, a3);
while ( v4 == -1 && *_errno_location() == 4 );
return v4;
}

original_code:
int
nointr_open(const char * const restrict fn, const int flags, const mode_t mode)
{ int r; do {((r = open(fn,flags,mode)));} while (__builtin_expect(((r == -1)),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

canonical_original_code:
int nointr_open(char *fn, int flags, unsigned int mode)
{ int r; do {((r = open(fn,flags,mode)));} while (__builtin_expect(((r == -1)),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

variable_types:
fn
char *

variable_types: flags int

variable_types:
mode
unsigned int


decompiled_code:
__int64 __fastcall nointr_close(int fd)
{
unsigned int v1; // ebx

do
v1 = close(fd);
while ( v1 && *_errno_location() == 4 );
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd)
{
unsigned int v1; // ebx

do
v1 = close(fd);
while ( v1 && *_errno_location() == 4 );
return v1;
}

original_code:
int
nointr_close(const int fd)
{ int r; do {((r = close(fd)));} while (__builtin_expect(((r != 0)),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

canonical_original_code:
int nointr_close(int fd)
{ int r; do {((r = close(fd)));} while (__builtin_expect(((r != 0)),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }


decompiled_code:
ssize_t __fastcall nointr_write(int fd, void *buf, size_t n)
{
ssize_t v5; // rax
ssize_t v6; // rbx

do
{
while ( 1 )
{
v5 = write(fd, buf, n);
v6 = v5;
if ( v5 != -1 )
break;
if ( *_errno_location() != 4 )
return v6;
}
buf = (char *)buf + v5;
n -= v5;
}
while ( n );
return v6;
}

canonical_decompiled_code:
ssize_t __fastcall func3(int fd, void *buf, size_t n)
{
ssize_t v5; // rax
ssize_t v6; // rbx

do
{
while ( 1 )
{
v5 = write(fd, buf, n);
v6 = v5;
if ( v5 != -1 )
break;
if ( *_errno_location() != 4 )
return v6;
}
buf = (char *)buf + v5;
n -= v5;
}
while ( n );
return v6;
}

original_code:
ssize_t
nointr_write(const int fd, const char * restrict buf, size_t sz)
{
    ssize_t w;
    do { w = write(fd,buf,sz); } while (w!=-1 ? (buf+=w,sz-=w) : (*__errno_location ())==4);
    return w;
}

canonical_original_code:
long nointr_write(int fd, char *buf, unsigned long sz)
{
    long w;
    do { w = write(fd,buf,sz); } while (w!=-1 ? (buf+=w,sz-=w) : (*__errno_location ())==4);
    return w;
}

variable_types:
buf
char *

variable_types:
sz
unsigned long

variable_types: w long


decompiled_code:
__int64 __fastcall nointr_ftruncate(int fd, __off64_t length)
{
unsigned int v2; // ebx

do
v2 = ftruncate64(fd, length);
while ( v2 && *_errno_location() == 4 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, __off64_t length)
{
unsigned int v2; // ebx

do
v2 = ftruncate64(fd, length);
while ( v2 && *_errno_location() == 4 );
return v2;
}

original_code:
int
nointr_ftruncate(const int fd, const off_t sz)
{ int r; do {((r = ftruncate(fd, sz)));} while (__builtin_expect(((r != 0)),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

canonical_original_code:
int nointr_ftruncate(int fd, long sz)
{ int r; do {((r = ftruncate(fd, sz)));} while (__builtin_expect(((r != 0)),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

variable_types: sz long


decompiled_code:
__int64 __fastcall nointr_openat(int fd, char *file, int oflag, unsigned int a4)
{
unsigned int v6; // ebx

do
v6 = openat64(fd, file, oflag, a4);
while ( v6 == -1 && *_errno_location() == 4 );
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, char *file, int oflag, unsigned int a4)
{
unsigned int v6; // ebx

do
v6 = openat64(fd, file, oflag, a4);
while ( v6 == -1 && *_errno_location() == 4 );
return v6;
}

original_code:
int
nointr_openat(const int dfd, const char * const restrict fn,
              const int flags, const mode_t mode)
{ int r; do {((r=openat(dfd,fn,flags,mode)));} while (__builtin_expect(((r==-1)),0) && __builtin_expect((*__errno_location ())==4, 1)); return r;}

canonical_original_code:
int nointr_openat(int dfd, char *fn, int flags, unsigned int mode)
{ int r; do {((r=openat(dfd,fn,flags,mode)));} while (__builtin_expect(((r==-1)),0) && __builtin_expect((*__errno_location ())==4, 1)); return r;}

variable_types: dfd int


decompiled_code:
char __fastcall nss_mcdb_nsswitch(char *s, char *a2, unsigned __int64 a3)
{
char *v3; // rbp
char *v6; // rbx
char *v7; // rax
size_t v8; // r12
int v9; // eax
unsigned int v10; // ebx
char result; // al
char v12; // al
char v13; // dl
bool v14; // di
char v15; // al
char *v16; // rdx
char v17; // al
unsigned __int64 v18; // rcx
char *v19; // rax
bool v20; // si
char v21; // al
__int64 v22; // rcx
struct stat64 v23; // [rsp+0h] [rbp-B8h] BYREF

v3 = s;
v6 = nsswitch_0;
if ( nsswitch_0 == (char *)-1LL )
{
v9 = nointr_open("/etc/nsswitch.conf", 526336LL, 0LL);
v10 = v9;
if ( v9 == -1 )
{
if ( *_errno_location() != 2 )
return 0;
}
else
{
if ( !fstat64(v9, &v23) )
nsswitch_0 = (char *)mmap64(0LL, v23.st_size, 1, 2, v10, 0LL);
nointr_close(v10);
}
v6 = nsswitch_0;
if ( nsswitch_0 == (char *)-1LL )
goto LABEL_13;
}
v7 = strrchr(s, 47);
if ( v7 )
v3 = v7 + 1;
v8 = strlen(v3);
if ( !*v6 )
{
LABEL_13:
result = 0;
if ( a3 > 9 )
{
if ( !strcmp(v3, "hosts") )
strcpy(a2, "mcdb dns");
else
strcpy(a2, "mcdb");
result = 1;
}
return result;
}
while ( 1 )
{
v17 = *v6;
if ( *v6 == 32 || v17 == 9 )
{
do
{
do
v17 = *++v6;
while ( *v6 == 32 );
}
while ( v17 == 9 );
}
if ( *v3 != v17 || strncmp(v6, v3, v8) )
goto LABEL_38;
v6 += v8;
v12 = *v6;
if ( *v6 == 32 || v12 == 9 )
{
do
{
do
v12 = *++v6;
while ( *v6 == 32 );
}
while ( v12 == 9 );
}
if ( v12 != 58 )
{
LABEL_38:
v15 = *v6;
if ( *v6 == 10 || !v15 )
{
v16 = v6;
}
else
{
LABEL_40:
v16 = v6;
do
{
v22 = 1LL;
if ( v15 == 92 )
v22 = (v16[1] == 10) + 1LL;
v16 += v22;
v15 = *v16;
}
while ( *v16 != 10 && v15 );
}
if ( !v15 )
goto LABEL_13;
goto LABEL_32;
}
do
{
v13 = *++v6;
result = v13 == 9 || v13 == 32;
}
while ( result );
if ( !v13 )
goto LABEL_13;
v14 = v13 != 10 && v13 != 35;
if ( v14 )
break;
v15 = *v6;
if ( *v6 != 10 && v15 )
goto LABEL_40;
v16 = v6;
LABEL_32:
v6 = v16 + 1;
if ( !v16[1] )
goto LABEL_13;
}
if ( !a3 )
return result;
v18 = 0LL;
while ( 2 )
{
if ( v13 == 92 )
{
v19 = v6 + 1;
if ( v6[1] == 10 )
goto LABEL_45;
goto LABEL_44;
}
if ( v13 != 32 && v13 != 9 )
{
LABEL_44:
a2[v18++] = v13;
v19 = v6;
goto LABEL_45;
}
v21 = v6[1];
if ( v21 == 32 || v21 == 9 )
{
v19 = v6;
}
else
{
a2[v18++] = 32;
v19 = v6;
}
LABEL_45:
v6 = v19 + 1;
v13 = v19[1];
v20 = v13 != 10 && v13 != 35;
result = v20 && v13 != 0 && v18 < a3;
if ( result )
continue;
break;
}
if ( !v20 || !v13 )
{
if ( a2[v18 - 1] == 32 )
--v18;
a2[v18] = 0;
result = v14;
}
return result;
}

canonical_decompiled_code:
char __fastcall func0(char *s, char *a2, unsigned __int64 a3)
{
char *v3; // rbp
char *v6; // rbx
char *v7; // rax
size_t v8; // r12
int v9; // eax
unsigned int v10; // ebx
char result; // al
char v12; // al
char v13; // dl
bool v14; // di
char v15; // al
char *v16; // rdx
char v17; // al
unsigned __int64 v18; // rcx
char *v19; // rax
bool v20; // si
char v21; // al
__int64 v22; // rcx
struct stat64 v23; // [rsp+0h] [rbp-B8h] BYREF

v3 = s;
v6 = nsswitch_0;
if ( nsswitch_0 == (char *)-1LL )
{
v9 = nointr_open("/etc/nsswitch.conf", 526336LL, 0LL);
v10 = v9;
if ( v9 == -1 )
{
if ( *_errno_location() != 2 )
return 0;
}
else
{
if ( !fstat64(v9, &v23) )
nsswitch_0 = (char *)mmap64(0LL, v23.st_size, 1, 2, v10, 0LL);
nointr_close(v10);
}
v6 = nsswitch_0;
if ( nsswitch_0 == (char *)-1LL )
goto LABEL_13;
}
v7 = strrchr(s, 47);
if ( v7 )
v3 = v7 + 1;
v8 = strlen(v3);
if ( !*v6 )
{
LABEL_13:
result = 0;
if ( a3 > 9 )
{
if ( !strcmp(v3, "hosts") )
strcpy(a2, "mcdb dns");
else
strcpy(a2, "mcdb");
result = 1;
}
return result;
}
while ( 1 )
{
v17 = *v6;
if ( *v6 == 32 || v17 == 9 )
{
do
{
do
v17 = *++v6;
while ( *v6 == 32 );
}
while ( v17 == 9 );
}
if ( *v3 != v17 || strncmp(v6, v3, v8) )
goto LABEL_38;
v6 += v8;
v12 = *v6;
if ( *v6 == 32 || v12 == 9 )
{
do
{
do
v12 = *++v6;
while ( *v6 == 32 );
}
while ( v12 == 9 );
}
if ( v12 != 58 )
{
LABEL_38:
v15 = *v6;
if ( *v6 == 10 || !v15 )
{
v16 = v6;
}
else
{
LABEL_40:
v16 = v6;
do
{
v22 = 1LL;
if ( v15 == 92 )
v22 = (v16[1] == 10) + 1LL;
v16 += v22;
v15 = *v16;
}
while ( *v16 != 10 && v15 );
}
if ( !v15 )
goto LABEL_13;
goto LABEL_32;
}
do
{
v13 = *++v6;
result = v13 == 9 || v13 == 32;
}
while ( result );
if ( !v13 )
goto LABEL_13;
v14 = v13 != 10 && v13 != 35;
if ( v14 )
break;
v15 = *v6;
if ( *v6 != 10 && v15 )
goto LABEL_40;
v16 = v6;
LABEL_32:
v6 = v16 + 1;
if ( !v16[1] )
goto LABEL_13;
}
if ( !a3 )
return result;
v18 = 0LL;
while ( 2 )
{
if ( v13 == 92 )
{
v19 = v6 + 1;
if ( v6[1] == 10 )
goto LABEL_45;
goto LABEL_44;
}
if ( v13 != 32 && v13 != 9 )
{
LABEL_44:
a2[v18++] = v13;
v19 = v6;
goto LABEL_45;
}
v21 = v6[1];
if ( v21 == 32 || v21 == 9 )
{
v19 = v6;
}
else
{
a2[v18++] = 32;
v19 = v6;
}
LABEL_45:
v6 = v19 + 1;
v13 = v19[1];
v20 = v13 != 10 && v13 != 35;
result = v20 && v13 != 0 && v18 < a3;
if ( result )
continue;
break;
}
if ( !v20 || !v13 )
{
if ( a2[v18 - 1] == 32 )
--v18;
a2[v18] = 0;
result = v14;
}
return result;
}

original_code:
static _Bool
nss_mcdb_nsswitch(const char * restrict svc,
                  char * const restrict data,
                  const size_t datasz)
{
    static void * restrict nsswitch = ((void *) -1);
    if (nsswitch == ((void *) -1)) {
        struct stat st;
        const int fd =
          nointr_open("/etc/nsswitch.conf", 00|04000|02000000, 0);
        if (fd != -1) {
            if (fstat(fd, &st) == 0) {
                    nsswitch = mmap(((void *)0), (size_t)st.st_size,
                                    0x1, 0x02, fd, 0);
            }
            (void) nointr_close(fd);
        }
        else if ((*__errno_location ()) != 2)
            return 0;
    }
    if (nsswitch != ((void *) -1)) {
        const char * restrict p = strrchr(svc, '/');
        const size_t svclen = strlen((p != ((void *)0)) ? (svc = p+1) : svc);
        for (p = (char *)nsswitch; *p != '\0'; ++p) {
            while (*p == ' ' || *p == '\t') ++p;
            if (*p == *svc && 0 == strncmp(p, svc, svclen)) {
                p += svclen;
                while (*p == ' ' || *p == '\t') ++p;
                if (*p == ':') {
                    do { ++p; } while (*p == ' ' || *p == '\t');
                    if (*p == '\0') break;
                    if (*p != '#' && *p != '\n') {
                        size_t i = 0;
                        for (;*p!='#' && *p!='\n' && *p!='\0' && i<datasz; ++p){
                            if (*p == '\\' && *(p+1) == '\n')
                                ++p;
                            else if (*p != ' ' && *p != '\t')
                                data[i++] = *p;
                            else if (*(p+1) != ' ' && *(p+1) != '\t')
                                data[i++] = ' ';
                        }
                        if (*p != '#' && *p != '\n' && *p != '\0')
                            return 0;
                        if (data[i-1] == ' ')
                            --i;
                        data[i] = '\0';
                        return 1;
                    }
                }
            }
            while (*p != '\n' && *p != '\0')
                p += (*p != '\\' || *(p+1) != '\n') ? 1 : 2;
            if (*p == '\0') break;
        }
    }
    if (sizeof("mcdb dns") >= datasz)
        return 0;
    if (0 != strcmp(svc, "hosts"))
        memcpy(data, "mcdb", sizeof("mcdb"));
    else
        memcpy(data, "mcdb dns", sizeof("mcdb dns"));
    return 1;
}

canonical_original_code:
static _Bool nss_mcdb_nsswitch(char *svc, char *data, unsigned long datasz)
{
    static void *nsswitch = ((void *) -1);
    if (nsswitch == ((void *) -1)) {
        struct stat st;
        int fd = nointr_open("/etc/nsswitch.conf", 00|04000|02000000, 0);
        if (fd != -1) {
            if (fstat(fd, &st) == 0) {
                    nsswitch = mmap(((void *)0), (unsigned long)st.st_size,
                                    0x1, 0x02, fd, 0);
            }
            (void) nointr_close(fd);
        }
        else if ((*__errno_location ()) != 2)
            return 0;
    }
    if (nsswitch != ((void *) -1)) {
        char *p = strrchr(svc, '/');
        unsigned long svclen = strlen((p != ((void *)0)) ? (svc = p+1) : svc);
        for (p = (char *)nsswitch; *p != '\0'; ++p) {
            while (*p == ' ' || *p == '\t') ++p;
            if (*p == *svc && 0 == strncmp(p, svc, svclen)) {
                p += svclen;
                while (*p == ' ' || *p == '\t') ++p;
                if (*p == ':') {
                    do { ++p; } while (*p == ' ' || *p == '\t');
                    if (*p == '\0') break;
                    if (*p != '#' && *p != '\n') {
                        unsigned long i = 0;
                        for (;*p!='#' && *p!='\n' && *p!='\0' && i<datasz; ++p){
                            if (*p == '\\' && *(p+1) == '\n')
                                ++p;
                            else if (*p != ' ' && *p != '\t')
                                data[i++] = *p;
                            else if (*(p+1) != ' ' && *(p+1) != '\t')
                                data[i++] = ' ';
                        }
                        if (*p != '#' && *p != '\n' && *p != '\0')
                            return 0;
                        if (data[i-1] == ' ')
                            --i;
                        data[i] = '\0';
                        return 1;
                    }
                }
            }
            while (*p != '\n' && *p != '\0')
                p += (*p != '\\' || *(p+1) != '\n') ? 1 : 2;
            if (*p == '\0') break;
        }
    }
    if (sizeof("mcdb dns") >= datasz)
        return 0;
    if (0 != strcmp(svc, "hosts"))
        memcpy(data, "mcdb", sizeof("mcdb"));
    else
        memcpy(data, "mcdb dns", sizeof("mcdb dns"));
    return 1;
}

variable_types:
svc
char *

variable_types:
data
char *

variable_types:
datasz
unsigned long

variable_types:
nsswitch
void *

variable_types:
p
char *

variable_types:
svclen
unsigned long

variable_types:
i
unsigned long

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
char *__fastcall nss_mcdb_authn_make_parse_long_int_colon(char *nptr, __int64 *a2)
{
unsigned __int64 v2; // rax
__int64 v3; // rdx
char *v4; // rbx
__int64 v5; // rax
__int64 v6; // rdx
unsigned __int64 v7; // rax
char *endptr; // [rsp+8h] [rbp-20h] BYREF

v2 = (unsigned __int8)*nptr;
if ( (unsigned __int8)v2 > 0x3Au )
{
v4 = nptr;
LABEL_6:
v6 = 0x400000000000401LL;
do
{
do
v7 = (unsigned __int8)*++v4;
while ( (unsigned __int8)v7 > 0x3Au );
}
while ( !_bittest64(&v6, v7) );
if ( (_BYTE)v7 != 58 )
return 0LL;
if ( nptr == v4 )
{
v5 = -1LL;
}
else
{
*v4 = 0;
*_errno_location() = 0;
v5 = strtol(nptr, &endptr, 10);
if ( *endptr )
return 0LL;
if ( (unsigned __int64)(v5 - 0x80000000LL) <= 0xFFFFFFFEFFFFFFFFLL )
return 0LL;
}
goto LABEL_13;
}
v3 = 0x400000000000401LL;
v4 = nptr;
if ( !_bittest64(&v3, v2) )
goto LABEL_6;
v4 = 0LL;
if ( (_BYTE)v2 == 58 )
{
v4 = nptr;
v5 = -1LL;
LABEL_13:
*a2 = v5;
}
return v4;
}

canonical_decompiled_code:
char *__fastcall func2(char *nptr, __int64 *a2)
{
unsigned __int64 v2; // rax
__int64 v3; // rdx
char *v4; // rbx
__int64 v5; // rax
__int64 v6; // rdx
unsigned __int64 v7; // rax
char *endptr; // [rsp+8h] [rbp-20h] BYREF

v2 = (unsigned __int8)*nptr;
if ( (unsigned __int8)v2 > 0x3Au )
{
v4 = nptr;
LABEL_6:
v6 = 0x400000000000401LL;
do
{
do
v7 = (unsigned __int8)*++v4;
while ( (unsigned __int8)v7 > 0x3Au );
}
while ( !_bittest64(&v6, v7) );
if ( (_BYTE)v7 != 58 )
return 0LL;
if ( nptr == v4 )
{
v5 = -1LL;
}
else
{
*v4 = 0;
*_errno_location() = 0;
v5 = strtol(nptr, &endptr, 10);
if ( *endptr )
return 0LL;
if ( (unsigned __int64)(v5 - 0x80000000LL) <= 0xFFFFFFFEFFFFFFFFLL )
return 0LL;
}
goto LABEL_13;
}
v3 = 0x400000000000401LL;
v4 = nptr;
if ( !_bittest64(&v3, v2) )
goto LABEL_6;
v4 = 0LL;
if ( (_BYTE)v2 == 58 )
{
v4 = nptr;
v5 = -1LL;
LABEL_13:
*a2 = v5;
}
return v4;
}

original_code:
static char *
nss_mcdb_authn_make_parse_long_int_colon(char * const restrict b,
                                         long *
                                                const restrict v)
{
    char *p = b;
    char *e;
    long n = -1;
    while (*(p)!=':' && *(p)!='\n' && *(p)!='\0') ++(p);
    if (*p != ':')
        return ((void *)0);
    if (b != p) {
        *p = '\0';
        (*__errno_location ()) = 0;
        n = strtol(b, &e, 10);
        if (*e != '\0' || ((n==(-0x7fffffffffffffffL - 1L) || n==0x7fffffffffffffffL) && (*__errno_location ()) == 34))
            return ((void *)0);
        if (0x7fffffffffffffffL != 0x7fffffff && (n < (-0x7fffffff - 1) || 0x7fffffff < n))
            return ((void *)0);
    }
    *v = n;
    return p;
}

canonical_original_code:
static char *nss_mcdb_authn_make_parse_long_int_colon(char *b, long *v)
{
    char *p = b;
    char *e;
    long n = -1;
    while (*(p)!=':' && *(p)!='\n' && *(p)!='\0') ++(p);
    if (*p != ':')
        return ((void *)0);
    if (b != p) {
        *p = '\0';
        (*__errno_location ()) = 0;
        n = strtol(b, &e, 10);
        if (*e != '\0' || ((n==(-0x7fffffffffffffffL - 1L) || n==0x7fffffffffffffffL) && (*__errno_location ()) == 34))
            return ((void *)0);
        if (0x7fffffffffffffffL != 0x7fffffff && (n < (-0x7fffffff - 1) || 0x7fffffff < n))
            return ((void *)0);
    }
    *v = n;
    return p;
}

variable_types:
b
char *

variable_types:
v
long *

variable_types:
e
char *

variable_types: n long

return_type: char *


usage

repo: gteissier/srtp-decrypt

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
fprintf(
stderr,
"usage: %s -k <base64 SDES key> [-d <rtp byte offset in packet>] [-t <srtp hmac tag length in bytes>]\n",
a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
fprintf(
stderr,
"usage: %s -k <base64 SDES key> [-d <rtp byte offset in packet>] [-t <srtp hmac tag length in bytes>]\n",
a1);
exit(1);
}

original_code:
static void usage(const char *arg0) {
  fprintf(stderr, "usage: %s -k <base64 SDES key> [-d <rtp byte offset in packet>] [-t <srtp hmac tag length in bytes>]\n", arg0);
  exit(1);
}

canonical_original_code:
static void usage(char *arg0) {
  fprintf(stderr, "usage: %s -k <base64 SDES key> [-d <rtp byte offset in packet>] [-t <srtp hmac tag length in bytes>]\n", arg0);
  exit(1);
}

return_type: void


hexdump

decompiled_code:
int __fastcall hexdump(__int64 a1, unsigned __int64 a2)
{
unsigned __int8 *v2; // rbp
unsigned __int64 i; // r13
int result; // eax
unsigned __int8 *v5; // rbx

if ( a2 )
{
v2 = (unsigned __int8 *)(a1 + 16);
for ( i = 0LL; i < a2; i += 16LL )
{
printf("%04x ", (unsigned int)i);
if ( a2 > i )
{
printf("%02x ", *(v2 - 16));
v5 = v2 - 15;
do
{
if ( v5 == (unsigned __int8 *)(a1 + a2) )
break;
printf("%02x ", *v5++);
}
while ( v5 != v2 );
}
result = putchar(10);
v2 += 16;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, unsigned __int64 a2)
{
unsigned __int8 *v2; // rbp
unsigned __int64 i; // r13
int result; // eax
unsigned __int8 *v5; // rbx

if ( a2 )
{
v2 = (unsigned __int8 *)(a1 + 16);
for ( i = 0LL; i < a2; i += 16LL )
{
printf("%04x ", (unsigned int)i);
if ( a2 > i )
{
printf("%02x ", *(v2 - 16));
v5 = v2 - 15;
do
{
if ( v5 == (unsigned __int8 *)(a1 + a2) )
break;
printf("%02x ", *v5++);
}
while ( v5 != v2 );
}
result = putchar(10);
v2 += 16;
}
}
return result;
}

original_code:
static void hexdump(const void *ptr, size_t size) {
  int i, j;
  const unsigned char *cptr = ptr;
  for (i = 0; i < size; i += 16) {
    printf("%04x ", i);
    for (j = 0; j < 16 && i+j < size; j++) {
      printf("%02x ", cptr[i+j]);
    }
    printf("\n");
  }
}

canonical_original_code:
static void hexdump(void *ptr, unsigned long size) {
  int i; int j;
  unsigned char *cptr = ptr;
  for (i = 0; i < size; i += 16) {
    printf("%04x ", i);
    for (j = 0; j < 16 && i+j < size; j++) {
      printf("%02x ", cptr[i+j]);
    }
    printf("\n");
  }
}

variable_types:
ptr
void *

variable_types:
size
unsigned long

variable_types: i int

variable_types: j int

variable_types:
cptr
unsigned char *


decompiled_code:
int __fastcall handle_pkt(__int64 a1, const __m128i *a2, __int64 a3)
{
unsigned int v3; // ebp
unsigned int v4; // er12
size_t v5; // r12
int v6; // eax
__int64 v7; // rcx
__int64 v8; // rax
__int64 v9; // rdi
int result; // eax
char *v11; // rax
unsigned __int64 v12; // [rsp+8h] [rbp-820h] BYREF
char dest[2072]; // [rsp+10h] [rbp-818h] BYREF

v3 = ++frame_nr;
v4 = a2[1].m128i_u32[0];
if ( v4 < rtp_offset )
return fprintf(stderr, "frame %d dropped: too short\n", v3);
v5 = v4 - rtp_offset;
memcpy(dest, (const void *)(a3 + rtp_offset), v5);
v12 = v5;
if ( !v3 )
start_tv = (__int128)_mm_loadu_si128(a2);
if ( !decoded_packets )
srtp_init_seq(s, dest);
v6 = srtp_recv(s, dest, &v12);
if ( v6 )
{
v11 = strerror(v6);
result = fprintf(stderr, "frame %d dropped: decoding failed '%s'\n", (unsigned int)frame_nr, v11);
}
else
{
++decoded_packets;
v7 = a2->m128i_i64[0] - start_tv;
v8 = a2->m128i_i64[1] - *((_QWORD *)&start_tv + 1);
v9 = v8;
if ( v8 < 0 )
{
--v7;
v9 = v8 + 1000000;
}
printf("%02ld:%02ld.%06lu\n", v7 / 60, v7 % 60, v9);
result = hexdump((__int64)dest, v12);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, const __m128i *a2, __int64 a3)
{
unsigned int v3; // ebp
unsigned int v4; // er12
size_t v5; // r12
int v6; // eax
__int64 v7; // rcx
__int64 v8; // rax
__int64 v9; // rdi
int result; // eax
char *v11; // rax
unsigned __int64 v12; // [rsp+8h] [rbp-820h] BYREF
char dest[2072]; // [rsp+10h] [rbp-818h] BYREF

v3 = ++frame_nr;
v4 = a2[1].m128i_u32[0];
if ( v4 < rtp_offset )
return fprintf(stderr, "frame %d dropped: too short\n", v3);
v5 = v4 - rtp_offset;
memcpy(dest, (const void *)(a3 + rtp_offset), v5);
v12 = v5;
if ( !v3 )
start_tv = (__int128)_mm_loadu_si128(a2);
if ( !decoded_packets )
srtp_init_seq(s, dest);
v6 = srtp_recv(s, dest, &v12);
if ( v6 )
{
v11 = strerror(v6);
result = fprintf(stderr, "frame %d dropped: decoding failed '%s'\n", (unsigned int)frame_nr, v11);
}
else
{
++decoded_packets;
v7 = a2->m128i_i64[0] - start_tv;
v8 = a2->m128i_i64[1] - *((_QWORD *)&start_tv + 1);
v9 = v8;
if ( v8 < 0 )
{
--v7;
v9 = v8 + 1000000;
}
printf("%02ld:%02ld.%06lu\n", v7 / 60, v7 % 60, v9);
result = func1((__int64)dest, v12);
}
return result;
}

original_code:
static void handle_pkt(u_char *arg, const struct pcap_pkthdr *hdr,
  const u_char *bytes) {
  unsigned char buffer[2048];
  size_t pktsize;
  int ret;
  struct timeval delta;
  frame_nr += 1;
  if (hdr->caplen < rtp_offset) {
    fprintf(stderr, "frame %d dropped: too short\n", frame_nr);
    return;
  }
  memcpy(buffer, bytes + rtp_offset, hdr->caplen - rtp_offset);
  pktsize = hdr->caplen - rtp_offset;
  if (frame_nr == 0) {
    start_tv = hdr->ts;
  }
  if (decoded_packets == 0) {
    srtp_init_seq (s, buffer);
  }
  ret = srtp_recv(s, buffer, &pktsize);
  if (ret != 0) {
    fprintf(stderr, "frame %d dropped: decoding failed '%s'\n", frame_nr,
      strerror(ret));
    return;
  }
  decoded_packets++;
  do { (&delta)->tv_sec = (&hdr->ts)->tv_sec - (&start_tv)->tv_sec; (&delta)->tv_usec = (&hdr->ts)->tv_usec - (&start_tv)->tv_usec; if ((&delta)->tv_usec < 0) { --(&delta)->tv_sec; (&delta)->tv_usec += 1000000; } } while (0);
  printf("%02ld:%02ld.%06lu\n", delta.tv_sec/60, delta.tv_sec%60, delta.tv_usec);
  hexdump(buffer, pktsize);
}

canonical_original_code:
static void handle_pkt(unsigned char *arg, struct pcap_pkthdr *hdr, unsigned char *bytes) {
  unsigned char buffer[2048];
  unsigned long pktsize;
  int ret;
  struct timeval delta;
  frame_nr += 1;
  if (hdr->caplen < rtp_offset) {
    fprintf(stderr, "frame %d dropped: too short\n", frame_nr);
    return;
  }
  memcpy(buffer, bytes + rtp_offset, hdr->caplen - rtp_offset);
  pktsize = hdr->caplen - rtp_offset;
  if (frame_nr == 0) {
    start_tv = hdr->ts;
  }
  if (decoded_packets == 0) {
    srtp_init_seq (s, buffer);
  }
  ret = srtp_recv(s, buffer, &pktsize);
  if (ret != 0) {
    fprintf(stderr, "frame %d dropped: decoding failed '%s'\n", frame_nr,
      strerror(ret));
    return;
  }
  decoded_packets++;
  do { (&delta)->tv_sec = (&hdr->ts)->tv_sec - (&start_tv)->tv_sec; (&delta)->tv_usec = (&hdr->ts)->tv_usec - (&start_tv)->tv_usec; if ((&delta)->tv_usec < 0) { --(&delta)->tv_sec; (&delta)->tv_usec += 1000000; } } while (0);
  printf("%02ld:%02ld.%06lu\n", delta.tv_sec/60, delta.tv_sec%60, delta.tv_usec);
  hexdump(buffer, pktsize);
}

variable_types:
arg
unsigned char *

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; } *

variable_types:
bytes
unsigned char *

variable_types:
buffer
unsigned char [2048]

variable_types:
pktsize
unsigned long

variable_types: ret int

user_defined_types: struct pcap_pkthdr { struct timeval ts; unsigned int caplen; unsigned int len; }


decompiled_code:
signed __int64 __fastcall shiftb64(unsigned __int8 a1)
{
char *v1; // rax

v1 = strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", a1);
if ( !v1 )
_assert_fail("p", "srtp-decrypt.c", 0x14u, "shiftb64");
return v1 - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}

canonical_decompiled_code:
signed __int64 __fastcall func3(unsigned __int8 a1)
{
char *v1; // rax

v1 = strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", a1);
if ( !v1 )
_assert_fail("p", "srtp-decrypt.c", 0x14u, "shiftb64");
return v1 - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}

original_code:
static unsigned char shiftb64(unsigned char c) {
  char *p = strchr(b64chars, c);
  ((void) sizeof ((p) ? 1 : 0), __extension__ ({ if (p) ; else __assert_fail ("p", "srtp-decrypt.c", 20, __extension__ __PRETTY_FUNCTION__); }));
  return p-b64chars;
}

variable_types:
c
unsigned char

variable_types:
p
char *

return_type: unsigned char


original_code:
static void decode_sdes(unsigned char *in,
  unsigned char *key, unsigned char *salt) {
  int i;
  size_t len = strlen((char *) in);
  ((void) sizeof ((len == 40) ? 1 : 0), __extension__ ({ if (len == 40) ; else __assert_fail ("len == 40", "srtp-decrypt.c", 41, __extension__ __PRETTY_FUNCTION__); }));
  unsigned char raw[30];
  for (i = 0; 4*i < len; i++) {
    decode_block(in+4*i, raw+3*i);
  }
  memcpy(key, raw, 16);
  memcpy(salt, raw+16, 14);
}

canonical_original_code:
static void decode_sdes(unsigned char *in, unsigned char *key, unsigned char *salt) {
  int i;
  unsigned long len = strlen((char *) in);
  ((void) sizeof ((len == 40) ? 1 : 0), __extension__ ({ if (len == 40) ; else __assert_fail ("len == 40", "srtp-decrypt.c", 41, __extension__ __PRETTY_FUNCTION__); }));
  unsigned char raw[30];
  for (i = 0; 4*i < len; i++) {
    decode_block(in+4*i, raw+3*i);
  }
  memcpy(key, raw, 16);
  memcpy(salt, raw+16, 14);
}

variable_types:
in
unsigned char *

variable_types:
key
unsigned char *

variable_types:
salt
unsigned char *

variable_types:
len
unsigned long

variable_types:
raw
unsigned char [30]


repo: gtill/bv

decompiled_code:
__int64 __fastcall bswap32(unsigned int a1)
{
return (a1 >> 8) & 0xFF00 | (a1 << 8) & 0xFF0000 | HIBYTE(a1) | (a1 << 24);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1)
{
return (a1 >> 8) & 0xFF00 | (a1 << 8) & 0xFF0000 | HIBYTE(a1) | (a1 << 24);
}

variable_types:
x
unsigned int


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

v1 = a1;
if ( (unsigned int)is_little_endian() )
v1 = bswap32(a1);
return v1;
}

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

v1 = a1;
if ( (unsigned int)func7() )
v1 = func8(a1);
return v1;
}

original_code:
static __inline__ uint32_t to_be32(uint32_t x)
{
 return is_little_endian() ? bswap32(x) : x;
}

canonical_original_code:
static __inline__ unsigned int to_be32(unsigned int x)
{
 return is_little_endian() ? bswap32(x) : x;
}


original_code:
static uint32_t *delete_id(struct um_id *um_id, uint32_t id)
{
 uint32_t *wid = ((void *)0);
 if ((um_id == ((void *)0)) || (id == 0)) {
  return ((void *)0);
 }
 wid = um_id->wid[id];
 um_id->wid[id] = ((void *)0);
 --um_id->noccupied;
 if (um_id->noccupied <= um_id->count / 4) {
 }
 return wid;
}

canonical_original_code:
static unsigned int *delete_id(struct um_id *um_id, unsigned int id)
{
 unsigned int *wid = ((void *)0);
 if ((um_id == ((void *)0)) || (id == 0)) {
  return ((void *)0);
 }
 wid = um_id->wid[id];
 um_id->wid[id] = ((void *)0);
 --um_id->noccupied;
 if (um_id->noccupied <= um_id->count / 4) {
 }
 return wid;
}

variable_types: struct um_id { unsigned int count; unsigned int noccupied; unsigned int **wid; } *

variable_types:
id
unsigned int

variable_types:
wid
unsigned int *

return_type: unsigned int *


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

if ( a1 && a2 )
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
else
result = 0LL;
return result;
}

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

if ( a1 && a2 )
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
else
result = 0LL;
return result;
}

original_code:
static uint32_t *get_wid(struct um_id *um_id, uint32_t id)
{
 if ((um_id == ((void *)0)) || (id == 0)) {
  return ((void *)0);
 }
 return um_id->wid[id];
}

canonical_original_code:
static unsigned int *get_wid(struct um_id *um_id, unsigned int id)
{
 if ((um_id == ((void *)0)) || (id == 0)) {
  return ((void *)0);
 }
 return um_id->wid[id];
}

user_defined_types: struct um_id { unsigned int count; unsigned int noccupied; unsigned int **wid; }


decompiled_code:
int __fastcall print_usage(const char *a1)
{
return fprintf(stderr, "Usage: %s scroll\n", a1);
}

canonical_decompiled_code:
int __fastcall func12(const char *a1)
{
return fprintf(stderr, "Usage: %s scroll\n", a1);
}

original_code:
static void print_usage(const char *name)
{
 fprintf(stderr, "Usage: %s scroll\n", name);
}

canonical_original_code:
static void print_usage(char *name)
{
 fprintf(stderr, "Usage: %s scroll\n", name);
}

variable_types:
name
char *


decompiled_code:
void __fastcall um_free(__int64 a1)
{
free((void *)(a1 - 4));
}

canonical_decompiled_code:
void __fastcall func13(__int64 a1)
{
free((void *)(a1 - 4));
}

original_code:
static void um_free(uint32_t *p)
{
 free(p - 1);
}

canonical_original_code:
static void um_free(unsigned int *p)
{
 free(p - 1);
}

variable_types:
p
unsigned int *


decompiled_code:
__int64 __fastcall free_wid(__int64 a1)
{
__int64 v2; // rbx
__int64 result; // rax
__int64 v4; // rdi

if ( *(_DWORD *)a1 > 1u )
{
v2 = 0LL;
do
{
v4 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v2 + 8);
if ( v4 )
um_free(v4);
result = (unsigned int)(++v2 + 1);
}
while ( *(_DWORD *)a1 > (unsigned int)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
__int64 v2; // rbx
__int64 result; // rax
__int64 v4; // rdi

if ( *(_DWORD *)a1 > 1u )
{
v2 = 0LL;
do
{
v4 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v2 + 8);
if ( v4 )
func13(v4);
result = (unsigned int)(++v2 + 1);
}
while ( *(_DWORD *)a1 > (unsigned int)result );
}
return result;
}

original_code:
static void free_wid(struct um_id *um_id)
{
 int i;
 for (i = 1; i < um_id->count; ++i) {
  if (um_id->wid[i] != ((void *)0)) {
   um_free(um_id->wid[i]);
  }
 }
}

variable_types: i int


decompiled_code:
__int64 __fastcall alloc_wid(__int64 a1)
{
void *v1; // rax
void *v2; // rdx
__int64 result; // rax

*(_DWORD *)a1 = 256;
v1 = malloc(0x800uLL);
*(_QWORD *)(a1 + 8) = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = v1;
result = 0LL;
memset(v2, 0, 0x800uLL);
*(_DWORD *)(a1 + 4) = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
void *v1; // rax
void *v2; // rdx
__int64 result; // rax

*(_DWORD *)a1 = 256;
v1 = malloc(0x800uLL);
*(_QWORD *)(a1 + 8) = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = v1;
result = 0LL;
memset(v2, 0, 0x800uLL);
*(_DWORD *)(a1 + 4) = 1;
return result;
}

original_code:
static int32_t alloc_wid(struct um_id *um_id)
{
 um_id->count = kUM_ID_INIT_COUNT + 1;
 um_id->wid = malloc(um_id->count * sizeof(um_id->wid[0]));
 if (um_id->wid == ((void *)0)) {
  return -1;
 }
 memset(um_id->wid, 0x00, um_id->count * sizeof(um_id->wid[0]));
 um_id->noccupied = 1;
 return 0;
}

canonical_original_code:
static int alloc_wid(struct um_id *um_id)
{
 um_id->count = kUM_ID_INIT_COUNT + 1;
 um_id->wid = malloc(um_id->count * sizeof(um_id->wid[0]));
 if (um_id->wid == ((void *)0)) {
  return -1;
 }
 memset(um_id->wid, 0x00, um_id->count * sizeof(um_id->wid[0]));
 um_id->noccupied = 1;
 return 0;
}


decompiled_code:
__int64 __fastcall add_wid(int *a1, __int64 a2)
{
unsigned int v2; // er12
_QWORD *v3; // rax
unsigned int v4; // edx
char *v5; // rax

if ( a1 )
{
if ( !a2 )
return 0;
v2 = *a1;
if ( a1[1] != *a1 )
goto LABEL_4;
*a1 = 2 * v2;
v5 = (char *)realloc(*((void **)a1 + 1), 16LL * v2);
*((_QWORD *)a1 + 1) = v5;
if ( v5 )
{
memset(&v5[8 * v2], 0, 8LL * v2);
LABEL_4:
if ( (unsigned int)*a1 <= 1 )
return 0;
v3 = (_QWORD *)(*((_QWORD *)a1 + 1) + 8LL);
v4 = 1;
while ( *v3 )
{
++v4;
++v3;
if ( v4 == *a1 )
return 0;
}
*v3 = a2;
++a1[1];
return v4;
}
return 0;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func16(int *a1, __int64 a2)
{
unsigned int v2; // er12
_QWORD *v3; // rax
unsigned int v4; // edx
char *v5; // rax

if ( a1 )
{
if ( !a2 )
return 0;
v2 = *a1;
if ( a1[1] != *a1 )
goto LABEL_4;
*a1 = 2 * v2;
v5 = (char *)realloc(*((void **)a1 + 1), 16LL * v2);
*((_QWORD *)a1 + 1) = v5;
if ( v5 )
{
memset(&v5[8 * v2], 0, 8LL * v2);
LABEL_4:
if ( (unsigned int)*a1 <= 1 )
return 0;
v3 = (_QWORD *)(*((_QWORD *)a1 + 1) + 8LL);
v4 = 1;
while ( *v3 )
{
++v4;
++v3;
if ( v4 == *a1 )
return 0;
}
*v3 = a2;
++a1[1];
return v4;
}
return 0;
}
return 0LL;
}

original_code:
static uint32_t add_wid(struct um_id *um_id, uint32_t *wid)
{
 uint32_t id = 0;
 uint32_t i = 0;
 if ((um_id == ((void *)0)) || (wid == ((void *)0))) {
  return 0;
 }
 if (um_id->noccupied == um_id->count) {
  uint32_t n = um_id->count;
  um_id->count *= 2;
  um_id->wid = realloc(um_id->wid, sizeof(um_id->wid[0]) * um_id->count);
  if (um_id->wid == ((void *)0)) {
   return 0;
  }
  memset(um_id->wid + n, 0x00, sizeof(um_id->wid[0]) * n);
 }
 for (i = 1; i < um_id->count; ++i) {
  if (um_id->wid[i] == ((void *)0)) {
   id = i;
   um_id->wid[i] = wid;
   ++um_id->noccupied;
   break;
  }
 }
 return id;
}

canonical_original_code:
static unsigned int add_wid(struct um_id *um_id, unsigned int *wid)
{
 unsigned int id = 0;
 unsigned int i = 0;
 if ((um_id == ((void *)0)) || (wid == ((void *)0))) {
  return 0;
 }
 if (um_id->noccupied == um_id->count) {
  unsigned int n = um_id->count;
  um_id->count *= 2;
  um_id->wid = realloc(um_id->wid, sizeof(um_id->wid[0]) * um_id->count);
  if (um_id->wid == ((void *)0)) {
   return 0;
  }
  memset(um_id->wid + n, 0x00, sizeof(um_id->wid[0]) * n);
 }
 for (i = 1; i < um_id->count; ++i) {
  if (um_id->wid[i] == ((void *)0)) {
   id = i;
   um_id->wid[i] = wid;
   ++um_id->noccupied;
   break;
  }
 }
 return id;
}

variable_types:
i
unsigned int

variable_types:
n
unsigned int


readbuffer

repo: gtmanfred/telnetecho

decompiled_code:
__int64 __fastcall readbuffer(int fd, _BYTE *a2, unsigned __int64 a3)
{
__int64 v4; // rbp
__int64 v5; // rbx
char v6; // al
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( a3 > 1 )
{
v4 = a3;
v5 = 1LL;
while ( 1 )
{
if ( read(fd, buf, 1uLL) == 1 )
{
v6 = buf[0];
*a2 = buf[0];
if ( v6 == 10 )
{
v4 = v5;
++a2;
goto LABEL_9;
}
++a2;
}
if ( ++v5 == v4 )
goto LABEL_9;
}
}
v4 = 1LL;
LABEL_9:
*a2 = 0;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, _BYTE *a2, unsigned __int64 a3)
{
__int64 v4; // rbp
__int64 v5; // rbx
char v6; // al
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( a3 > 1 )
{
v4 = a3;
v5 = 1LL;
while ( 1 )
{
if ( read(fd, buf, 1uLL) == 1 )
{
v6 = buf[0];
*a2 = buf[0];
if ( v6 == 10 )
{
v4 = v5;
++a2;
goto LABEL_9;
}
++a2;
}
if ( ++v5 == v4 )
goto LABEL_9;
}
}
v4 = 1LL;
LABEL_9:
*a2 = 0;
return v4;
}

original_code:
ssize_t readbuffer(int fd, void *buf, size_t count) {
 ssize_t n;
 ssize_t rc;
 char letter;
 char *buffer;
 buffer = buf;
 for ( n = 1; n < count; n++ ) {
  if ( (rc = read(fd, &letter, 1)) == 1 ) {
   *buffer++ = letter;
   if ( letter == '\n' )
    break;
  }
 }
 *buffer = 0;
 return n;
}

canonical_original_code:
long readbuffer(int fd, void *buf, unsigned long count) {
 long n;
 long rc;
 char letter;
 char *buffer;
 buffer = buf;
 for ( n = 1; n < count; n++ ) {
  if ( (rc = read(fd, &letter, 1)) == 1 ) {
   *buffer++ = letter;
   if ( letter == '\n' )
    break;
  }
 }
 *buffer = 0;
 return n;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: n long

variable_types: rc long

variable_types: letter char

variable_types:
buffer
char *


writebuffer

decompiled_code:
__int64 __fastcall writebuffer(int fd, void *buf, size_t n)
{
size_t i; // rbx
ssize_t v5; // rax

for ( i = n; i; i -= v5 )
{
v5 = write(fd, buf, i);
buf = (char *)buf + v5;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, void *buf, size_t n)
{
size_t i; // rbx
ssize_t v5; // rax

for ( i = n; i; i -= v5 )
{
v5 = write(fd, buf, i);
buf = (char *)buf + v5;
}
return 0LL;
}

original_code:
ssize_t writebuffer(int fdd, const void *buf, size_t remain) {
 ssize_t nwritten;
 const char *buffer;
 buffer = buf;
 while ( remain > 0 ) {
  nwritten = write(fdd, buffer, remain);
  remain -= nwritten;
  buffer += nwritten;
 }
 return 0;
}

canonical_original_code:
long writebuffer(int fdd, void *buf, unsigned long remain) {
 long nwritten;
 char *buffer;
 buffer = buf;
 while ( remain > 0 ) {
  nwritten = write(fdd, buffer, remain);
  remain -= nwritten;
  buffer += nwritten;
 }
 return 0;
}

variable_types: fdd int

variable_types:
remain
unsigned long

variable_types: nwritten long


createGame

repo: guiguilalane/Wumpus

decompiled_code:
char *__fastcall createGame(__int64 a1)
{
char *v1; // rax
char *v2; // rbx
_QWORD *v3; // rax
__int64 v4; // rdx

v1 = (char *)malloc(0x80uLL);
v2 = v1;
if ( v1 )
{
pthread_mutex_init((pthread_mutex_t *)(v1 + 8), 0LL);
pthread_mutex_init((pthread_mutex_t *)(v2 + 48), 0LL);
*((_QWORD *)v2 + 11) = stairInitialisation();
*((_QWORD *)v2 + 12) = a1;
*(_QWORD *)(a1 + 296) = v2;
*((_DWORD *)v2 + 26) = 1;
*((_DWORD *)v2 + 27) = 1;
*((_DWORD *)v2 + 28) = 1;
v3 = (_QWORD *)manager;
if ( *(_QWORD *)manager )
{
v4 = *(_QWORD *)(manager + 8);
*(_QWORD *)v2 = v4;
*(_QWORD *)(v4 + 120) = v2;
v3[1] = v2;
}
else
{
*(_QWORD *)v2 = 0LL;
*v3 = v2;
*(_QWORD *)(manager + 8) = v2;
}
*((_QWORD *)v2 + 15) = 0LL;
}
else
{
perror(s);
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1)
{
char *v1; // rax
char *v2; // rbx
_QWORD *v3; // rax
__int64 v4; // rdx

v1 = (char *)malloc(0x80uLL);
v2 = v1;
if ( v1 )
{
pthread_mutex_init((pthread_mutex_t *)(v1 + 8), 0LL);
pthread_mutex_init((pthread_mutex_t *)(v2 + 48), 0LL);
*((_QWORD *)v2 + 11) = stairInitialisation();
*((_QWORD *)v2 + 12) = a1;
*(_QWORD *)(a1 + 296) = v2;
*((_DWORD *)v2 + 26) = 1;
*((_DWORD *)v2 + 27) = 1;
*((_DWORD *)v2 + 28) = 1;
v3 = (_QWORD *)manager;
if ( *(_QWORD *)manager )
{
v4 = *(_QWORD *)(manager + 8);
*(_QWORD *)v2 = v4;
*(_QWORD *)(v4 + 120) = v2;
v3[1] = v2;
}
else
{
*(_QWORD *)v2 = 0LL;
*v3 = v2;
*(_QWORD *)(manager + 8) = v2;
}
*((_QWORD *)v2 + 15) = 0LL;
}
else
{
perror(s);
}
return v2;
}

original_code:
jeu *createGame(player* p)
{
    jeu* j = (jeu*) malloc(sizeof(jeu));
    if(j == ((void *)0))
    {
        perror("Erreur d'allocation mémoire lors de la création d'un nouveau jeu");
        return ((void *)0);
    }
    pthread_mutex_init(&(j->playerMutex), ((void *)0));
    pthread_mutex_init(&(j->acquitMutex), ((void *)0));
    j->etage = stairInitialisation();
    j->joueur = p;
    p->game = j;
    j->nbPlayer = 1;
    j->nbPlayerActive = 1;
    j->numberOfStairs = 1;
    if(((void *)0) == manager->firstCreatedGame)
    {
        j->previousGame = ((void *)0);
        manager->firstCreatedGame = j;
        manager->lastCreatedGame = j;
    }
    else
    {
        j->previousGame = manager->lastCreatedGame;
        manager->lastCreatedGame->nextGame = j;
        manager->lastCreatedGame = j;
    }
    j->nextGame = ((void *)0);
    return j;
}

canonical_original_code:
struct jeu *createGame(struct player *p)
{
    struct jeu *j = (jeu*) malloc(sizeof(jeu));
    if(j == ((void *)0))
    {
        perror("Erreur d'allocation mémoire lors de la création d'un nouveau jeu");
        return ((void *)0);
    }
    pthread_mutex_init(&(j->playerMutex), ((void *)0));
    pthread_mutex_init(&(j->acquitMutex), ((void *)0));
    j->etage = stairInitialisation();
    j->joueur = p;
    p->game = j;
    j->nbPlayer = 1;
    j->nbPlayerActive = 1;
    j->numberOfStairs = 1;
    if(((void *)0) == manager->firstCreatedGame)
    {
        j->previousGame = ((void *)0);
        manager->firstCreatedGame = j;
        manager->lastCreatedGame = j;
    }
    else
    {
        j->previousGame = manager->lastCreatedGame;
        manager->lastCreatedGame->nextGame = j;
        manager->lastCreatedGame = j;
    }
    j->nextGame = ((void *)0);
    return j;
}

variable_types:
p
struct player *

variable_types: struct jeu { struct jeu *previousGame; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } playerMutex; union pthread_mutex_t acquitMutex; struct stairs { char map[5][5]; _Bool wumpusAlive; _Bool tresureFounded; } *etage; struct player { struct player *previousPlayer; char pseudo[256]; int score; int posX; int posY; int tresurPosX; int tresurPosY; int direction; _Bool arrow; _Bool deadByWumpus; _Bool fallInHole; _Bool findTresure; _Bool shotTheWumpus; struct jeu *game; int sock; struct player *nextPlayer; } *joueur; int nbPlayer; int nbPlayerActive; int numberOfStairs; struct jeu *nextGame; } *

user_defined_types: struct jeu { struct jeu *previousGame; union pthread_mutex_t playerMutex; union pthread_mutex_t acquitMutex; struct stairs *etage; struct player *joueur; int nbPlayer; int nbPlayerActive; int numberOfStairs; struct jeu *nextGame; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


createNewStair

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

free(*(void **)(a1 + 88));
result = stairInitialisation();
*(_QWORD *)(a1 + 88) = result;
return result;
}

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

free(*(void **)(a1 + 88));
result = stairInitialisation();
*(_QWORD *)(a1 + 88) = result;
return result;
}

original_code:
void createNewStair(jeu *j)
{
    free(j->etage);
    j->etage = stairInitialisation();
}

canonical_original_code:
void createNewStair(struct jeu *j)
{
    free(j->etage);
    j->etage = stairInitialisation();
}

user_defined_types: struct stairs { char map[5][5]; _Bool wumpusAlive; _Bool tresureFounded; }

user_defined_types: struct player { struct player *previousPlayer; char pseudo[256]; int score; int posX; int posY; int tresurPosX; int tresurPosY; int direction; _Bool arrow; _Bool deadByWumpus; _Bool fallInHole; _Bool findTresure; _Bool shotTheWumpus; struct jeu *game; int sock; struct player *nextPlayer; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


removeGame

decompiled_code:
void __fastcall removeGame(void *ptr)
{
if ( *(void **)(manager + 8) == ptr )
*(_QWORD *)(manager + 8) = *(_QWORD *)ptr;
else
**((_QWORD **)ptr + 15) = *(_QWORD *)ptr;
if ( *(void **)manager == ptr )
*(_QWORD *)manager = *((_QWORD *)ptr + 15);
else
*(_QWORD *)(*(_QWORD *)ptr + 120LL) = *((_QWORD *)ptr + 15);
free(*((void **)ptr + 11));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
if ( *(void **)(manager + 8) == ptr )
*(_QWORD *)(manager + 8) = *(_QWORD *)ptr;
else
**((_QWORD **)ptr + 15) = *(_QWORD *)ptr;
if ( *(void **)manager == ptr )
*(_QWORD *)manager = *((_QWORD *)ptr + 15);
else
*(_QWORD *)(*(_QWORD *)ptr + 120LL) = *((_QWORD *)ptr + 15);
free(*((void **)ptr + 11));
free(ptr);
}

original_code:
void removeGame(jeu * j)
{
    if(manager->lastCreatedGame != j)
    {
        j->nextGame->previousGame = j->previousGame;
    }
    else
    {
        manager->lastCreatedGame = j->previousGame;
    }
    if(manager->firstCreatedGame != j)
    {
        j->previousGame->nextGame = j->nextGame;
    }
    else
    {
        manager->firstCreatedGame = j->nextGame;
    }
    free(j->etage);
    free(j);
}

canonical_original_code:
void removeGame(struct jeu *j)
{
    if(manager->lastCreatedGame != j)
    {
        j->nextGame->previousGame = j->previousGame;
    }
    else
    {
        manager->lastCreatedGame = j->previousGame;
    }
    if(manager->firstCreatedGame != j)
    {
        j->previousGame->nextGame = j->nextGame;
    }
    else
    {
        manager->firstCreatedGame = j->nextGame;
    }
    free(j->etage);
    free(j);
}


removePlayer

original_code:
jeu *removePlayer(player *p)
{
    jeu* j = p->game;
    if(j->joueur != p)
    {
        p->previousPlayer->nextPlayer = p->nextPlayer;
    }
    else
    {
        j->joueur = p->nextPlayer;
    }
    if(((void *)0) != j->joueur)
    {
        if(((void *)0) != p->nextPlayer)
        {
            p->nextPlayer->previousPlayer = p->previousPlayer;
        }
        else
        {
            p->previousPlayer->nextPlayer = ((void *)0);
        }
    }
    j->nbPlayer--;
    if (!p->deadByWumpus && !p->fallInHole)
    {
        j->nbPlayerActive--;
    }
    free(p);
    return j;
}

canonical_original_code:
struct jeu *removePlayer(struct player *p)
{
    struct jeu *j = p->game;
    if(j->joueur != p)
    {
        p->previousPlayer->nextPlayer = p->nextPlayer;
    }
    else
    {
        j->joueur = p->nextPlayer;
    }
    if(((void *)0) != j->joueur)
    {
        if(((void *)0) != p->nextPlayer)
        {
            p->nextPlayer->previousPlayer = p->previousPlayer;
        }
        else
        {
            p->previousPlayer->nextPlayer = ((void *)0);
        }
    }
    j->nbPlayer--;
    if (!p->deadByWumpus && !p->fallInHole)
    {
        j->nbPlayerActive--;
    }
    free(p);
    return j;
}

return_type: struct jeu { struct jeu *previousGame; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } playerMutex; union pthread_mutex_t acquitMutex; struct stairs { char map[5][5]; _Bool wumpusAlive; _Bool tresureFounded; } *etage; struct player { struct player *previousPlayer; char pseudo[256]; int score; int posX; int posY; int tresurPosX; int tresurPosY; int direction; _Bool arrow; _Bool deadByWumpus; _Bool fallInHole; _Bool findTresure; _Bool shotTheWumpus; struct jeu *game; int sock; struct player *nextPlayer; } *joueur; int nbPlayer; int nbPlayerActive; int numberOfStairs; struct jeu *nextGame; } *


findLastPlayerFromGame

decompiled_code:
__int64 __fastcall findLastPlayerFromGame(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rdx

v1 = *(_QWORD *)(a1 + 96);
do
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 312);
}
while ( v1 );
return v2;
}

original_code:
player *findLastPlayerFromGame(jeu *j)
{
    player *current = j->joueur;
    while(current->nextPlayer != ((void *)0))
    {
        current = current->nextPlayer;
    }
    return current;
}

canonical_original_code:
struct player *findLastPlayerFromGame(struct jeu *j)
{
    struct player *current = j->joueur;
    while(current->nextPlayer != ((void *)0))
    {
        current = current->nextPlayer;
    }
    return current;
}

variable_types:
j
struct jeu *

variable_types: struct player { struct player *previousPlayer; char pseudo[256]; int score; int posX; int posY; int tresurPosX; int tresurPosY; int direction; _Bool arrow; _Bool deadByWumpus; _Bool fallInHole; _Bool findTresure; _Bool shotTheWumpus; struct jeu { struct jeu *previousGame; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } playerMutex; union pthread_mutex_t acquitMutex; struct stairs { char map[5][5]; _Bool wumpusAlive; _Bool tresureFounded; } *etage; struct player *joueur; int nbPlayer; int nbPlayerActive; int numberOfStairs; struct jeu *nextGame; } *game; int sock; struct player *nextPlayer; } *


addPlayer

original_code:
jeu *addPlayer(player *p)
{
    jeu* j = manager->lastCreatedGame;
    if(((void *)0) != j)
    {
        {
            if(j->nbPlayer < 3 && j->numberOfStairs < 2)
            {
                player *lastPlayer = findLastPlayerFromGame(j);
                lastPlayer->nextPlayer = p;
                p->previousPlayer = lastPlayer;
                p->game = j;
                j->nbPlayer++;
                j->nbPlayerActive++;
            }
            else
            {
                j = createGame(p);
            }
        }
    }
    else
    {
        j = createGame(p);
    }
    return j;
}

canonical_original_code:
struct jeu *addPlayer(struct player *p)
{
    struct jeu *j = manager->lastCreatedGame;
    if(((void *)0) != j)
    {
        {
            if(j->nbPlayer < 3 && j->numberOfStairs < 2)
            {
                struct player *lastPlayer = findLastPlayerFromGame(j);
                lastPlayer->nextPlayer = p;
                p->previousPlayer = lastPlayer;
                p->game = j;
                j->nbPlayer++;
                j->nbPlayerActive++;
            }
            else
            {
                j = createGame(p);
            }
        }
    }
    else
    {
        j = createGame(p);
    }
    return j;
}


resetGamePlayer

decompiled_code:
void __fastcall resetGamePlayer(__int64 a1)
{
if ( *(_QWORD *)(a1 + 96) )
{
while ( *(_QWORD *)(reinitPlayer() + 312) )
;
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1)
{
if ( *(_QWORD *)(a1 + 96) )
{
while ( *(_QWORD *)(reinitPlayer() + 312) )
;
}
}

original_code:
void resetGamePlayer(jeu* j)
{
    player *p = j->joueur;
    while(p != ((void *)0))
    {
        p = reinitPlayer(p);
        p = p->nextPlayer;
    }
}

canonical_original_code:
void resetGamePlayer(struct jeu *j)
{
    struct player *p = j->joueur;
    while(p != ((void *)0))
    {
        p = reinitPlayer(p);
        p = p->nextPlayer;
    }
}


numberOfPlayer

decompiled_code:
__int64 numberOfPlayer()
{
__int64 v0; // rax
unsigned int v1; // edx

v0 = *(_QWORD *)manager;
if ( !*(_QWORD *)manager )
return 0;
v1 = 0;
do
{
v1 += *(_DWORD *)(v0 + 104);
v0 = *(_QWORD *)(v0 + 120);
}
while ( v0 );
return v1;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // rax
unsigned int v1; // edx

v0 = *(_QWORD *)manager;
if ( !*(_QWORD *)manager )
return 0;
v1 = 0;
do
{
v1 += *(_DWORD *)(v0 + 104);
v0 = *(_QWORD *)(v0 + 120);
}
while ( v0 );
return v1;
}

original_code:
int numberOfPlayer()
{
    int count = 0;
    jeu* current = manager->firstCreatedGame;
    while(current != ((void *)0))
    {
        count += current->nbPlayer;
        current = current->nextGame;
    }
    return count;
}

canonical_original_code:
int numberOfPlayer()
{
    int count = 0;
    struct jeu *current = manager->firstCreatedGame;
    while(current != ((void *)0))
    {
        count += current->nbPlayer;
        current = current->nextGame;
    }
    return count;
}

variable_types: count int


fatal

repo: guilffer/IP-Header

decompiled_code:
void __fastcall __noreturn fatal(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void fatal(char *s) {
  perror(s);
  exit(1);
}

variable_types:
s
char *


decompiled_code:
int __fastcall print_ip_header(__int64 a1)
{
unsigned __int16 v1; // ax
unsigned __int16 v2; // ax
unsigned __int16 v3; // ax
unsigned __int16 v4; // ax
char *v5; // rax
char *v6; // rax
struct in_addr v8; // [rsp+4h] [rbp-24h]
struct in_addr in_4; // [rsp+14h] [rbp-14h]

in_4.s_addr = *(_DWORD *)(a1 + 12);
v8.s_addr = *(_DWORD *)(a1 + 16);
putchar(10);
puts("IP Header");
printf(" Version : %d\n", *(_BYTE *)a1 >> 4);
printf(" Length : %d\n", 4 * (*(_BYTE *)a1 & 0xFu));
printf(" Type Of Service : %d\n", *(unsigned __int8 *)(a1 + 1));
v1 = _bswap_16(*(_WORD *)(a1 + 2));
printf(" Total Length : %d\n", v1);
v2 = _bswap_16(*(_WORD *)(a1 + 4));
printf(" Identification : %d\n", v2);
v3 = _bswap_16(*(_WORD *)(a1 + 6));
printf(" Fragment Offset : %d\n", v3);
printf(" TTL : %d\n", *(unsigned __int8 *)(a1 + 8));
printf(" Protocol : %d\n", *(unsigned __int8 *)(a1 + 9));
v4 = _bswap_16(*(_WORD *)(a1 + 10));
printf(" Checksum : %d\n", v4);
v5 = inet_ntoa(in_4);
printf(" Source : %s\n", v5);
v6 = inet_ntoa(v8);
return printf(" Destination : %s\n", v6);
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1)
{
unsigned __int16 v1; // ax
unsigned __int16 v2; // ax
unsigned __int16 v3; // ax
unsigned __int16 v4; // ax
char *v5; // rax
char *v6; // rax
struct in_addr v8; // [rsp+4h] [rbp-24h]
struct in_addr in_4; // [rsp+14h] [rbp-14h]

in_4.s_addr = *(_DWORD *)(a1 + 12);
v8.s_addr = *(_DWORD *)(a1 + 16);
putchar(10);
puts("IP Header");
printf(" Version : %d\n", *(_BYTE *)a1 >> 4);
printf(" Length : %d\n", 4 * (*(_BYTE *)a1 & 0xFu));
printf(" Type Of Service : %d\n", *(unsigned __int8 *)(a1 + 1));
v1 = func7(*(_WORD *)(a1 + 2));
printf(" Total Length : %d\n", v1);
v2 = func7(*(_WORD *)(a1 + 4));
printf(" Identification : %d\n", v2);
v3 = func7(*(_WORD *)(a1 + 6));
printf(" Fragment Offset : %d\n", v3);
printf(" TTL : %d\n", *(unsigned __int8 *)(a1 + 8));
printf(" Protocol : %d\n", *(unsigned __int8 *)(a1 + 9));
v4 = func7(*(_WORD *)(a1 + 10));
printf(" Checksum : %d\n", v4);
v5 = inet_ntoa(in_4);
printf(" Source : %s\n", v5);
v6 = inet_ntoa(v8);
return printf(" Destination : %s\n", v6);
}

original_code:
void print_ip_header(char *buffer) {
  struct iphdr *iph = (struct iphdr *)buffer;
  struct sockaddr_in source, dest;
  memset(&source, 0, sizeof(source));
  source.sin_addr.s_addr = iph->saddr;
  memset(&dest, 0, sizeof(dest));
  dest.sin_addr.s_addr = iph->daddr;
  printf("\n");
  printf("IP Header\n");
  printf("  Version         : %d\n",(unsigned int)iph->version);
  printf("  Length          : %d\n",((unsigned int)(iph->ihl))*4);
  printf("  Type Of Service : %d\n",(unsigned int)iph->tos);
  printf("  Total Length    : %d\n",ntohs(iph->tot_len));
  printf("  Identification  : %d\n",ntohs(iph->id));
  printf("  Fragment Offset : %d\n",ntohs(iph->frag_off));
  printf("  TTL             : %d\n",(unsigned int)iph->ttl);
  printf("  Protocol        : %d\n",(unsigned int)iph->protocol);
  printf("  Checksum        : %d\n",ntohs(iph->check));
  printf("  Source          : %s\n",inet_ntoa(source.sin_addr));
  printf("  Destination     : %s\n",inet_ntoa(dest.sin_addr));
}

canonical_original_code:
void print_ip_header(char *buffer) {
  struct iphdr *iph = (struct iphdr *)buffer;
  struct sockaddr_in source; struct sockaddr_in dest;
  memset(&source, 0, sizeof(source));
  source.sin_addr.s_addr = iph->saddr;
  memset(&dest, 0, sizeof(dest));
  dest.sin_addr.s_addr = iph->daddr;
  printf("\n");
  printf("IP Header\n");
  printf("  Version         : %d\n",(unsigned int)iph->version);
  printf("  Length          : %d\n",((unsigned int)(iph->ihl))*4);
  printf("  Type Of Service : %d\n",(unsigned int)iph->tos);
  printf("  Total Length    : %d\n",ntohs(iph->tot_len));
  printf("  Identification  : %d\n",ntohs(iph->id));
  printf("  Fragment Offset : %d\n",ntohs(iph->frag_off));
  printf("  TTL             : %d\n",(unsigned int)iph->ttl);
  printf("  Protocol        : %d\n",(unsigned int)iph->protocol);
  printf("  Checksum        : %d\n",ntohs(iph->check));
  printf("  Source          : %s\n",inet_ntoa(source.sin_addr));
  printf("  Destination     : %s\n",inet_ntoa(dest.sin_addr));
}

variable_types: struct iphdr { unsigned int ihl; unsigned int version; unsigned char tos; unsigned short tot_len; unsigned short id; unsigned short frag_off; unsigned char ttl; unsigned char protocol; unsigned short check; unsigned int saddr; unsigned int daddr; } *

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

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


original_code:
int verify_port(char * buffer) {
  struct iphdr *iph = (struct iphdr *)buffer;
  struct udphdr *udph = (struct udphdr*)(buffer + (iph->ihl*4));
  return (udph->dest == 29811);
}

canonical_original_code:
int verify_port(char *buffer) {
  struct iphdr *iph = (struct iphdr *)buffer;
  struct udphdr *udph = (struct udphdr*)(buffer + (iph->ihl*4));
  return (udph->dest == 29811);
}

variable_types: struct udphdr { union { struct { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_ulen; unsigned short uh_sum; }; struct { unsigned short source; unsigned short dest; unsigned short len; unsigned short check; }; }; } *

user_defined_types: struct iphdr { unsigned int ihl; unsigned int version; unsigned char tos; unsigned short tot_len; unsigned short id; unsigned short frag_off; unsigned char ttl; unsigned char protocol; unsigned short check; unsigned int saddr; unsigned int daddr; }


decompiled_code:
char *__fastcall get_source_addr(struct in_addr *a1)
{
return inet_ntoa(a1[3]);
}

canonical_decompiled_code:
char *__fastcall func12(struct in_addr *a1)
{
return inet_ntoa(a1[3]);
}

original_code:
char * get_source_addr(char *buffer) {
  struct iphdr *iph = (struct iphdr *)buffer;
  struct sockaddr_in source;
  memset(&source, 0, sizeof(source));
  source.sin_addr.s_addr = iph->saddr;
  return inet_ntoa(source.sin_addr);
}

canonical_original_code:
char *get_source_addr(char *buffer) {
  struct iphdr *iph = (struct iphdr *)buffer;
  struct sockaddr_in source;
  memset(&source, 0, sizeof(source));
  source.sin_addr.s_addr = iph->saddr;
  return inet_ntoa(source.sin_addr);
}


decompiled_code:
int __fastcall do_client(char *cp, char *src)
{
int v2; // eax
int v3; // ebx
char v5[1024]; // [rsp+0h] [rbp-428h] BYREF
struct sockaddr inp[2]; // [rsp+400h] [rbp-28h] BYREF

sleep(2u);
v2 = socket(2, 3, 17);
if ( v2 < 0 )
fatal("socket");
v3 = v2;
*(_QWORD *)&inp[0].sa_family = 2LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
*(_WORD *)inp[0].sa_data = _bswap_16(29811);
if ( !inet_aton(cp, (struct in_addr *)&inp[0].sa_data[2]) )
fatal("inet_aton");
strcpy(v5, src);
if ( (int)sendto(v3, v5, 0x400uLL, 0, inp, 0x10u) < 0 )
fatal("sendto");
puts("\nPacket Sent");
return close(v3);
}

canonical_decompiled_code:
int __fastcall func15(char *cp, char *src)
{
int v2; // eax
int v3; // ebx
char v5[1024]; // [rsp+0h] [rbp-428h] BYREF
struct sockaddr inp[2]; // [rsp+400h] [rbp-28h] BYREF

sleep(2u);
v2 = socket(2, 3, 17);
if ( v2 < 0 )
func9("socket");
v3 = v2;
*(_QWORD *)&inp[0].sa_family = 2LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
*(_WORD *)inp[0].sa_data = func7(29811);
if ( !inet_aton(cp, (struct in_addr *)&inp[0].sa_data[2]) )
func9("inet_aton");
strcpy(v5, src);
if ( (int)sendto(v3, v5, 0x400uLL, 0, inp, 0x10u) < 0 )
func9("sendto");
puts("\nPacket Sent");
return close(v3);
}

original_code:
void do_client(char *dest, char *message) {
  struct sockaddr_in si;
  int s, e, slen=sizeof(si);
  char buf[1024];
  sleep(2);
  s = socket(2, SOCK_RAW, IPPROTO_UDP);
  if (s < 0) fatal("socket");
  memset((char *) &si, 0, sizeof(si));
  si.sin_family = 2;
  si.sin_port = htons(29811);
  if (inet_aton(dest, &si.sin_addr)==0) fatal("inet_aton");
  sprintf(buf, "%s", message);
  e = sendto(s, buf, 1024, 0, (struct sockaddr *) &si, slen);
  if (e < 0) fatal("sendto");
  printf("\nPacket Sent\n");
  close(s);
}

canonical_original_code:
void do_client(char *dest, char *message) {
  struct sockaddr_in si;
  int s; int e; int slen = sizeof(si);
  char buf[1024];
  sleep(2);
  s = socket(2, SOCK_RAW, IPPROTO_UDP);
  if (s < 0) fatal("socket");
  memset((char *) &si, 0, sizeof(si));
  si.sin_family = 2;
  si.sin_port = htons(29811);
  if (inet_aton(dest, &si.sin_addr)==0) fatal("inet_aton");
  sprintf(buf, "%s", message);
  e = sendto(s, buf, 1024, 0, (struct sockaddr *) &si, slen);
  if (e < 0) fatal("sendto");
  printf("\nPacket Sent\n");
  close(s);
}

variable_types:
dest
char *

variable_types:
message
char *

variable_types: s int

variable_types: e int

variable_types: slen int

variable_types:
buf
char [1024]

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


repo: guilhem/sslh

decompiled_code:
__int64 __fastcall defer_write(unsigned int *a1, const void *a2, signed int a3)
{
void *v4; // rax

if ( verbose )
fprintf(stderr, "**** writing defered on fd %d\n", *a1);
v4 = malloc(a3);
*((_QWORD *)a1 + 2) = v4;
*((_QWORD *)a1 + 1) = v4;
a1[6] = a3;
memcpy(v4, a2, a3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1, const void *a2, signed int a3)
{
void *v4; // rax

if ( verbose )
fprintf(stderr, "**** writing defered on fd %d\n", *a1);
v4 = malloc(a3);
*((_QWORD *)a1 + 2) = v4;
*((_QWORD *)a1 + 1) = v4;
a1[6] = a3;
memcpy(v4, a2, a3);
return 0LL;
}

original_code:
int defer_write(struct queue *q, void* data, int data_size)
{
    if (verbose)
        fprintf(stderr, "**** writing defered on fd %d\n", q->fd);
    q->defered_data = malloc(data_size);
    q->begin_defered_data = q->defered_data;
    q->defered_data_size = data_size;
    memcpy(q->defered_data, data, data_size);
    return 0;
}

canonical_original_code:
int defer_write(struct queue *q, void *data, int data_size)
{
    if (verbose)
        fprintf(stderr, "**** writing defered on fd %d\n", q->fd);
    q->defered_data = malloc(data_size);
    q->begin_defered_data = q->defered_data;
    q->defered_data_size = data_size;
    memcpy(q->defered_data, data, data_size);
    return 0;
}

variable_types: struct queue { int fd; void *begin_defered_data; void *defered_data; int defered_data_size; } *

variable_types:
data
void *