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

if ( !(unsigned int)fbuf_extended(a1, a3 + *(_DWORD *)(a1 + 24)) )
return 0xFFFFFFFFLL;
memcpy((void *)(*(_QWORD *)(a1 + 8) + *(unsigned int *)(a1 + 24)), a2, (int)a3);
v4 = *(_QWORD *)(a1 + 8);
v5 = *(_DWORD *)(a1 + 24) + a3;
*(_DWORD *)(a1 + 24) = v5;
*(_BYTE *)(v4 + v5) = 0;
return a3;
}

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

if ( !(unsigned int)func1(a1, a3 + *(_DWORD *)(a1 + 24)) )
return 0xFFFFFFFFLL;
memcpy((void *)(*(_QWORD *)(a1 + 8) + *(unsigned int *)(a1 + 24)), a2, (int)a3);
v4 = *(_QWORD *)(a1 + 8);
v5 = *(_DWORD *)(a1 + 24) + a3;
*(_DWORD *)(a1 + 24) = v5;
*(_BYTE *)(v4 + v5) = 0;
return a3;
}

original_code:
int
fbuf_add_binary(fbuf_t *fbuf, const char *data, int len)
{
    if (!fbuf_extended(fbuf, fbuf->used+len))
 return -1;
    memcpy(fbuf->data+fbuf->used, data, len);
    fbuf->used += len;
    fbuf->data[fbuf->used] = 0;
    return len;
}

canonical_original_code:
int fbuf_add_binary(struct __fbuf *fbuf, char *data, int len)
{
    if (!fbuf_extended(fbuf, fbuf->used+len))
 return -1;
    memcpy(fbuf->data+fbuf->used, data, len);
    fbuf->used += len;
    fbuf->data[fbuf->used] = 0;
    return len;
}

variable_types:
data
char *


repo: xant/wurfld

decompiled_code:
__int64 __fastcall fbuf_add(__int64 a1, const char *a2)
{
unsigned int v3; // eax

if ( !a2 )
return 0LL;
if ( !*a2 )
return 0LL;
v3 = strlen(a2);
return fbuf_add_binary(a1, a2, v3);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const char *a2)
{
unsigned int v3; // eax

if ( !a2 )
return 0LL;
if ( !*a2 )
return 0LL;
v3 = strlen(a2);
return func6(a1, a2, v3);
}

original_code:
int
fbuf_add(fbuf_t *fbuf, const char *data)
{
    int datalen;
    if (!data || data[0] == '\0')
 return 0;
    datalen = strlen(data);
    return fbuf_add_binary(fbuf, data, datalen);
}

canonical_original_code:
int fbuf_add(struct __fbuf *fbuf, char *data)
{
    int datalen;
    if (!data || data[0] == '\0')
 return 0;
    datalen = strlen(data);
    return fbuf_add_binary(fbuf, data, datalen);
}

variable_types: struct __fbuf { unsigned int id; char *data; unsigned int len; unsigned int prefmaxlen; unsigned int used; } *

variable_types: datalen int

return_type: int


decompiled_code:
__int64 __fastcall fbuf_add_nl(__int64 a1, const char *a2)
{
int v2; // eax
int v3; // ebx
int v4; // eax
__int64 result; // rax

v2 = fbuf_add(a1, a2);
if ( v2 == -1 || (v3 = v2, v4 = fbuf_add(a1, "\n"), v4 == -1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)(v3 + v4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, const char *a2)
{
int v2; // eax
int v3; // ebx
int v4; // eax
__int64 result; // rax

v2 = func7(a1, a2);
if ( v2 == -1 || (v3 = v2, v4 = func7(a1, "\n"), v4 == -1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)(v3 + v4);
return result;
}

original_code:
int
fbuf_add_nl(fbuf_t *fbuf, const char *data)
{
    int n1 = 0, n2 = 0;
    n1 = fbuf_add(fbuf, data);
    if (n1 == -1)
 return -1;
    n2 = fbuf_add(fbuf, "\n");
    if (n2 == -1)
 return -1;
    return n1 + n2;
}

canonical_original_code:
int fbuf_add_nl(struct __fbuf *fbuf, char *data)
{
    int n1 = 0; int n2 = 0;
    n1 = fbuf_add(fbuf, data);
    if (n1 == -1)
 return -1;
    n2 = fbuf_add(fbuf, "\n");
    if (n2 == -1)
 return -1;
    return n1 + n2;
}

user_defined_types: struct __fbuf { unsigned int id; char *data; unsigned int len; unsigned int prefmaxlen; unsigned int used; }


decompiled_code:
__int64 __fastcall fbuf_concat(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
int v3; // eax
unsigned int v5; // er12

v2 = 0;
v3 = *(_DWORD *)(a2 + 24);
if ( !v3 )
return v2;
v5 = *(_DWORD *)(a2 + 24);
if ( !(unsigned int)fbuf_extended(a1, v3 + *(_DWORD *)(a1 + 24)) )
return (unsigned int)-1;
memcpy((void *)(*(_QWORD *)(a1 + 8) + *(unsigned int *)(a1 + 24)), *(const void **)(a2 + 8), (int)(v5 + 1));
*(_DWORD *)(a1 + 24) += v5;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
int v3; // eax
unsigned int v5; // er12

v2 = 0;
v3 = *(_DWORD *)(a2 + 24);
if ( !v3 )
return v2;
v5 = *(_DWORD *)(a2 + 24);
if ( !(unsigned int)func1(a1, v3 + *(_DWORD *)(a1 + 24)) )
return (unsigned int)-1;
memcpy((void *)(*(_QWORD *)(a1 + 8) + *(unsigned int *)(a1 + 24)), *(const void **)(a2 + 8), (int)(v5 + 1));
*(_DWORD *)(a1 + 24) += v5;
return v5;
}

original_code:
int
fbuf_concat(fbuf_t *fbufdst, fbuf_t *fbufsrc)
{
    int datalen;
    if (fbufsrc->used == 0)
 return 0;
    datalen = fbufsrc->used;
    if (!fbuf_extended(fbufdst, fbufdst->used+datalen))
 return -1;
    memcpy(fbufdst->data+fbufdst->used, fbufsrc->data, datalen+1);
    fbufdst->used += datalen;
    return datalen;
}

canonical_original_code:
int fbuf_concat(struct __fbuf *fbufdst, struct __fbuf *fbufsrc)
{
    int datalen;
    if (fbufsrc->used == 0)
 return 0;
    datalen = fbufsrc->used;
    if (!fbuf_extended(fbufdst, fbufdst->used+datalen))
 return -1;
    memcpy(fbufdst->data+fbufdst->used, fbufsrc->data, datalen+1);
    fbufdst->used += datalen;
    return datalen;
}

variable_types:
fbufsrc
struct __fbuf *


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

if ( !(unsigned int)fbuf_extended(a2, *(_DWORD *)(a1 + 24)) )
return 0xFFFFFFFFLL;
fbuf_clear(a2);
strcpy(*(char **)(a2 + 8), *(const char **)(a1 + 8));
result = *(unsigned int *)(a1 + 24);
*(_DWORD *)(a2 + 24) = result;
return result;
}

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

if ( !(unsigned int)func1(a2, *(_DWORD *)(a1 + 24)) )
return 0xFFFFFFFFLL;
fbuf_clear(a2);
strcpy(*(char **)(a2 + 8), *(const char **)(a1 + 8));
result = *(unsigned int *)(a1 + 24);
*(_DWORD *)(a2 + 24) = result;
return result;
}

original_code:
int
fbuf_copy(fbuf_t *fbufsrc, fbuf_t *fbufdst)
{
    if (!fbuf_extended(fbufdst, fbufsrc->used))
 return -1;
    fbuf_clear(fbufdst);
    strcpy(fbufdst->data, fbufsrc->data);
    fbufdst->used = fbufsrc->used;
    return fbufdst->used;
}

canonical_original_code:
int fbuf_copy(struct __fbuf *fbufsrc, struct __fbuf *fbufdst)
{
    if (!fbuf_extended(fbufdst, fbufsrc->used))
 return -1;
    fbuf_clear(fbufdst);
    strcpy(fbufdst->data, fbufsrc->data);
    fbufdst->used = fbufsrc->used;
    return fbufdst->used;
}

variable_types:
fbufdst
struct __fbuf *


decompiled_code:
__int64 __fastcall fbuf_set(__int64 a1, const char *a2)
{
int v2; // ebx
unsigned int v3; // er13

v2 = strlen(a2);
v3 = v2;
if ( !(unsigned int)fbuf_extended(a1, v2) )
return (unsigned int)-1;
memcpy(*(void **)(a1 + 8), a2, v2 + 1);
*(_DWORD *)(a1 + 24) = v2;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, const char *a2)
{
int v2; // ebx
unsigned int v3; // er13

v2 = strlen(a2);
v3 = v2;
if ( !(unsigned int)func1(a1, v2) )
return (unsigned int)-1;
memcpy(*(void **)(a1 + 8), a2, v2 + 1);
*(_DWORD *)(a1 + 24) = v2;
return v3;
}

original_code:
int
fbuf_set(fbuf_t *fbuf, const char *data)
{
    int datalen = strlen(data);
    if (!fbuf_extended(fbuf, datalen))
 return -1;
    memcpy(fbuf->data, data, datalen+1);
    fbuf->used = datalen;
    return datalen;
}

canonical_original_code:
int fbuf_set(struct __fbuf *fbuf, char *data)
{
    int datalen = strlen(data);
    if (!fbuf_extended(fbuf, datalen))
 return -1;
    memcpy(fbuf->data, data, datalen+1);
    fbuf->used = datalen;
    return datalen;
}


decompiled_code:
unsigned int fbuf_printf(__int64 a1, const char *a2, ...)
{
__int64 v4; // rdi
size_t v5; // rsi
unsigned int result; // eax
int v7; // esi
__int64 v8; // rdx
__int64 v9; // rsi
__int64 v10; // rdi
size_t v11; // rsi
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( !(unsigned int)fbuf_extended(a1, *(_DWORD *)(a1 + 24) + 127) )
return -1;
v4 = *(unsigned int *)(a1 + 24);
v5 = (unsigned int)(*(_DWORD *)(a1 + 16) - v4);
va_start(arg, a2);
result = vsnprintf((char *)(*(_QWORD *)(a1 + 8) + v4), v5, a2, arg);
v7 = *(_DWORD *)(a1 + 24);
if ( *(_DWORD *)(a1 + 16) - v7 > result )
{
LABEL_3:
v8 = *(_QWORD *)(a1 + 8);
v9 = result + v7;
*(_DWORD *)(a1 + 24) = v9;
*(_BYTE *)(v8 + v9) = 0;
return result;
}
if ( (unsigned int)fbuf_extended(a1, result + v7) )
{
v10 = *(unsigned int *)(a1 + 24);
v11 = (unsigned int)(*(_DWORD *)(a1 + 16) - v10);
va_end(arg);
va_start(arg, a2);
result = vsnprintf((char *)(*(_QWORD *)(a1 + 8) + v10), v11, a2, arg);
v7 = *(_DWORD *)(a1 + 24);
goto LABEL_3;
}
*(_BYTE *)(*(_QWORD *)(a1 + 8) + *(unsigned int *)(a1 + 24)) = 0;
return -1;
}

canonical_decompiled_code:
unsigned int func12(__int64 a1, const char *a2, ...)
{
__int64 v4; // rdi
size_t v5; // rsi
unsigned int result; // eax
int v7; // esi
__int64 v8; // rdx
__int64 v9; // rsi
__int64 v10; // rdi
size_t v11; // rsi
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( !(unsigned int)func1(a1, *(_DWORD *)(a1 + 24) + 127) )
return -1;
v4 = *(unsigned int *)(a1 + 24);
v5 = (unsigned int)(*(_DWORD *)(a1 + 16) - v4);
va_start(arg, a2);
result = vsnprintf((char *)(*(_QWORD *)(a1 + 8) + v4), v5, a2, arg);
v7 = *(_DWORD *)(a1 + 24);
if ( *(_DWORD *)(a1 + 16) - v7 > result )
{
LABEL_3:
v8 = *(_QWORD *)(a1 + 8);
v9 = result + v7;
*(_DWORD *)(a1 + 24) = v9;
*(_BYTE *)(v8 + v9) = 0;
return result;
}
if ( (unsigned int)func1(a1, result + v7) )
{
v10 = *(unsigned int *)(a1 + 24);
v11 = (unsigned int)(*(_DWORD *)(a1 + 16) - v10);
va_end(arg);
va_start(arg, a2);
result = vsnprintf((char *)(*(_QWORD *)(a1 + 8) + v10), v11, a2, arg);
v7 = *(_DWORD *)(a1 + 24);
goto LABEL_3;
}
*(_BYTE *)(*(_QWORD *)(a1 + 8) + *(unsigned int *)(a1 + 24)) = 0;
return -1;
}

original_code:
int
fbuf_printf(fbuf_t *fbuf, const char *fmt, ...)
{
    va_list args;
    unsigned int n;
    if (!fbuf_extended(fbuf, fbuf->used+127))
        return -1;
    __builtin_va_start(args,fmt);
    n = vsnprintf(fbuf->data+fbuf->used, fbuf->len-fbuf->used, fmt, args);
    __builtin_va_end(args);
    if (n >= fbuf->len-fbuf->used) {
        if (!fbuf_extended(fbuf, fbuf->used+n)) {
            fbuf->data[fbuf->used] = '\0';
            return -1;
        }
        __builtin_va_start(args,fmt);
        n = vsnprintf(fbuf->data+fbuf->used, fbuf->len-fbuf->used, fmt, args);
        __builtin_va_end(args);
    }
    fbuf->used += n;
    fbuf->data[fbuf->used] = '\0';
    __builtin_va_end(args);
    return n;
}

canonical_original_code:
int fbuf_printf(struct __fbuf *fbuf, char *fmt, ...)
{
    __builtin_va_list args;
    unsigned int n;
    if (!fbuf_extended(fbuf, fbuf->used+127))
        return -1;
    __builtin_va_start(args,fmt);
    n = vsnprintf(fbuf->data+fbuf->used, fbuf->len-fbuf->used, fmt, args);
    __builtin_va_end(args);
    if (n >= fbuf->len-fbuf->used) {
        if (!fbuf_extended(fbuf, fbuf->used+n)) {
            fbuf->data[fbuf->used] = '\0';
            return -1;
        }
        __builtin_va_start(args,fmt);
        n = vsnprintf(fbuf->data+fbuf->used, fbuf->len-fbuf->used, fmt, args);
        __builtin_va_end(args);
    }
    fbuf->used += n;
    fbuf->data[fbuf->used] = '\0';
    __builtin_va_end(args);
    return n;
}

variable_types:
fmt
char *

variable_types:
n
unsigned int


decompiled_code:
__int64 __fastcall fbuf_read(__int64 a1, int a2, unsigned int a3)
{
unsigned int v3; // ebx
int v4; // eax
unsigned int v5; // er8
__int64 v6; // rax

v3 = a3;
if ( !a3 )
v3 = 1024;
if ( !(unsigned int)fbuf_extended(a1, v3 + *(_DWORD *)(a1 + 24)) )
return (unsigned int)-1;
v4 = read(a2, (void *)(*(_QWORD *)(a1 + 8) + *(unsigned int *)(a1 + 24)), v3);
v5 = v4;
if ( v4 <= 0 )
{
v6 = *(unsigned int *)(a1 + 24);
}
else
{
v6 = (unsigned int)(*(_DWORD *)(a1 + 24) + v4);
*(_DWORD *)(a1 + 24) = v6;
}
*(_BYTE *)(*(_QWORD *)(a1 + 8) + v6) = 0;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, int a2, unsigned int a3)
{
unsigned int v3; // ebx
int v4; // eax
unsigned int v5; // er8
__int64 v6; // rax

v3 = a3;
if ( !a3 )
v3 = 1024;
if ( !(unsigned int)func1(a1, v3 + *(_DWORD *)(a1 + 24)) )
return (unsigned int)-1;
v4 = read(a2, (void *)(*(_QWORD *)(a1 + 8) + *(unsigned int *)(a1 + 24)), v3);
v5 = v4;
if ( v4 <= 0 )
{
v6 = *(unsigned int *)(a1 + 24);
}
else
{
v6 = (unsigned int)(*(_DWORD *)(a1 + 24) + v4);
*(_DWORD *)(a1 + 24) = v6;
}
*(_BYTE *)(*(_QWORD *)(a1 + 8) + v6) = 0;
return v5;
}

original_code:
int
fbuf_read(fbuf_t *fbuf, int fd, unsigned int explen)
{
    int n;
    if (explen == 0)
 explen = 1024;
    if (!fbuf_extended(fbuf, fbuf->used+explen))
 return -1;
    n = read(fd, fbuf->data+fbuf->used, explen);
    if (n > 0)
 fbuf->used += n;
    fbuf->data[fbuf->used] = '\0';
    return n;
}

canonical_original_code:
int fbuf_read(struct __fbuf *fbuf, int fd, unsigned int explen)
{
    int n;
    if (explen == 0)
 explen = 1024;
    if (!fbuf_extended(fbuf, fbuf->used+explen))
 return -1;
    n = read(fd, fbuf->data+fbuf->used, explen);
    if (n > 0)
 fbuf->used += n;
    fbuf->data[fbuf->used] = '\0';
    return n;
}

variable_types: fd int

variable_types:
explen
unsigned int

variable_types: n int


decompiled_code:
__int64 __fastcall fbuf_remove(__int64 a1, unsigned int a2)
{
__int64 result; // rax
_BYTE *v3; // rax
int v4; // eax

result = *(unsigned int *)(a1 + 24);
if ( (unsigned int)result > a2 )
{
if ( a2 )
{
v4 = result - a2;
*(_DWORD *)(a1 + 24) = v4;
memmove(*(void **)(a1 + 8), (const void *)(*(_QWORD *)(a1 + 8) + a2), (unsigned int)(v4 + 1));
result = *(unsigned int *)(a1 + 24);
}
}
else
{
v3 = *(_BYTE **)(a1 + 8);
*(_DWORD *)(a1 + 24) = 0;
*v3 = 0;
result = *(unsigned int *)(a1 + 24);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, unsigned int a2)
{
__int64 result; // rax
_BYTE *v3; // rax
int v4; // eax

result = *(unsigned int *)(a1 + 24);
if ( (unsigned int)result > a2 )
{
if ( a2 )
{
v4 = result - a2;
*(_DWORD *)(a1 + 24) = v4;
memmove(*(void **)(a1 + 8), (const void *)(*(_QWORD *)(a1 + 8) + a2), (unsigned int)(v4 + 1));
result = *(unsigned int *)(a1 + 24);
}
}
else
{
v3 = *(_BYTE **)(a1 + 8);
*(_DWORD *)(a1 + 24) = 0;
*v3 = 0;
result = *(unsigned int *)(a1 + 24);
}
return result;
}

original_code:
int
fbuf_remove(fbuf_t *fbuf, unsigned int len)
{
    if (len >= fbuf->used) {
        fbuf->used = 0;
 fbuf->data[fbuf->used] = '\0';
    } else if (len) {
 fbuf->used -= len;
 memmove(fbuf->data, fbuf->data+len, fbuf->used+1);
    }
    return fbuf->used;
}

canonical_original_code:
int fbuf_remove(struct __fbuf *fbuf, unsigned int len)
{
    if (len >= fbuf->used) {
        fbuf->used = 0;
 fbuf->data[fbuf->used] = '\0';
    } else if (len) {
 fbuf->used -= len;
 memmove(fbuf->data, fbuf->data+len, fbuf->used+1);
    }
    return fbuf->used;
}

variable_types:
len
unsigned int


decompiled_code:
__int64 __fastcall fbuf_write(__int64 a1, int a2, unsigned int a3)
{
unsigned int v3; // eax
unsigned int v4; // er12
signed int v6; // eax

v3 = *(_DWORD *)(a1 + 24);
if ( a3 && v3 > a3 )
v3 = a3;
v4 = 0;
if ( !v3 )
return v4;
v6 = write(a2, *(const void **)(a1 + 8), v3);
v4 = v6;
if ( v6 <= 0 )
return v4;
fbuf_remove(a1, v6);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, int a2, unsigned int a3)
{
unsigned int v3; // eax
unsigned int v4; // er12
signed int v6; // eax

v3 = *(_DWORD *)(a1 + 24);
if ( a3 && v3 > a3 )
v3 = a3;
v4 = 0;
if ( !v3 )
return v4;
v6 = write(a2, *(const void **)(a1 + 8), v3);
v4 = v6;
if ( v6 <= 0 )
return v4;
func15(a1, v6);
return v4;
}

original_code:
int
fbuf_write(fbuf_t *fbuf, int fd, unsigned int nbytes)
{
    int n = 0;
    if (nbytes == 0)
 nbytes = fbuf->used;
    if (nbytes > fbuf->used)
 nbytes = fbuf->used;
    if (nbytes > 0) {
 n = write(fd, fbuf->data, nbytes);
 if (n > 0)
     fbuf_remove(fbuf, n);
    }
    return n;
}

canonical_original_code:
int fbuf_write(struct __fbuf *fbuf, int fd, unsigned int nbytes)
{
    int n = 0;
    if (nbytes == 0)
 nbytes = fbuf->used;
    if (nbytes > fbuf->used)
 nbytes = fbuf->used;
    if (nbytes > 0) {
 n = write(fd, fbuf->data, nbytes);
 if (n > 0)
     fbuf_remove(fbuf, n);
    }
    return n;
}

variable_types:
nbytes
unsigned int


decompiled_code:
__int64 __fastcall fbuf_trim(__int64 a1)
{
int v1; // ebx
unsigned int v2; // esi
const unsigned __int16 *v3; // rcx
char *v4; // rax
unsigned int v5; // er12

v1 = *(_DWORD *)(a1 + 24);
if ( v1 )
{
v2 = 0;
v3 = *_ctype_b_loc();
v4 = *(char **)(a1 + 8);
do
{
if ( (v3[*v4] & 0x2000) == 0 )
break;
++v2;
++v4;
}
while ( v2 != v1 );
v5 = v2;
}
else
{
v2 = 0;
v5 = 0;
}
fbuf_remove(a1, v2);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1)
{
int v1; // ebx
unsigned int v2; // esi
const unsigned __int16 *v3; // rcx
char *v4; // rax
unsigned int v5; // er12

v1 = *(_DWORD *)(a1 + 24);
if ( v1 )
{
v2 = 0;
v3 = *_ctype_b_loc();
v4 = *(char **)(a1 + 8);
do
{
if ( (v3[*v4] & 0x2000) == 0 )
break;
++v2;
++v4;
}
while ( v2 != v1 );
v5 = v2;
}
else
{
v2 = 0;
v5 = 0;
}
func15(a1, v2);
return v5;
}

original_code:
int
fbuf_trim(fbuf_t *fbuf)
{
    unsigned int i = 0;
    while (i < fbuf->used && ((*__ctype_b_loc ())[(int) ((fbuf->data[i]))] & (unsigned short int) _ISspace))
 i++;
    fbuf_remove(fbuf, i);
    return i;
}

canonical_original_code:
int fbuf_trim(struct __fbuf *fbuf)
{
    unsigned int i = 0;
    while (i < fbuf->used && ((*__ctype_b_loc ())[(int) ((fbuf->data[i]))] & (unsigned short) _ISspace))
 i++;
    fbuf_remove(fbuf, i);
    return i;
}

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall fbuf_data(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

original_code:
char *
fbuf_data(fbuf_t *fbuf)
{
    return fbuf->data;
}

canonical_original_code:
char *fbuf_data(struct __fbuf *fbuf)
{
    return fbuf->data;
}

return_type: char *


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

result = *(_QWORD *)(a1 + 8);
if ( result )
result += *(unsigned int *)(a1 + 24);
return result;
}

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

result = *(_QWORD *)(a1 + 8);
if ( result )
result += *(unsigned int *)(a1 + 24);
return result;
}

original_code:
char *
fbuf_end(fbuf_t *fbuf)
{
    if (fbuf->data)
 return fbuf->data + fbuf->used;
    else
 return ((void *)0);
}

canonical_original_code:
char *fbuf_end(struct __fbuf *fbuf)
{
    if (fbuf->data)
 return fbuf->data + fbuf->used;
    else
 return ((void *)0);
}


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

result = *(unsigned int *)(a1 + 24);
if ( (unsigned int)result > a2 )
{
v3 = *(_QWORD *)(a1 + 8);
*(_DWORD *)(a1 + 24) = a2;
*(_BYTE *)(v3 + a2) = 0;
result = *(unsigned int *)(a1 + 24);
}
return result;
}

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

result = *(unsigned int *)(a1 + 24);
if ( (unsigned int)result > a2 )
{
v3 = *(_QWORD *)(a1 + 8);
*(_DWORD *)(a1 + 24) = a2;
*(_BYTE *)(v3 + a2) = 0;
result = *(unsigned int *)(a1 + 24);
}
return result;
}

original_code:
int
fbuf_set_used(fbuf_t *fbuf, unsigned int newused)
{
    if (newused < fbuf->used) {
 fbuf->used = newused;
 fbuf->data[fbuf->used] = '\0';
    }
    return fbuf->used;
}

canonical_original_code:
int fbuf_set_used(struct __fbuf *fbuf, unsigned int newused)
{
    if (newused < fbuf->used) {
 fbuf->used = newused;
 fbuf->data[fbuf->used] = '\0';
    }
    return fbuf->used;
}

variable_types:
newused
unsigned int


decompiled_code:
__int64 __fastcall fbuf_used(__int64 a1)
{
return *(unsigned int *)(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func21(__int64 a1)
{
return *(unsigned int *)(a1 + 24);
}

original_code:
unsigned int
fbuf_used(fbuf_t *fbuf)
{
    return fbuf->used;
}

canonical_original_code:
unsigned int fbuf_used(struct __fbuf *fbuf)
{
    return fbuf->used;
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall fbuf_len(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func22(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

original_code:
unsigned int
fbuf_len(fbuf_t *fbuf)
{
    return fbuf->len;
}

canonical_original_code:
unsigned int fbuf_len(struct __fbuf *fbuf)
{
    return fbuf->len;
}


decompiled_code:
__int64 __fastcall fbuf_set_pref_maxlen(__int64 a1, unsigned int a2)
{
*(_DWORD *)(a1 + 20) = a2;
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func23(__int64 a1, unsigned int a2)
{
*(_DWORD *)(a1 + 20) = a2;
return a2;
}

original_code:
unsigned int
fbuf_set_pref_maxlen(fbuf_t *fbuf, unsigned int newprefmaxlen)
{
    return (fbuf->prefmaxlen = newprefmaxlen);
}

canonical_original_code:
unsigned int fbuf_set_pref_maxlen(struct __fbuf *fbuf, unsigned int newprefmaxlen)
{
    return (fbuf->prefmaxlen = newprefmaxlen);
}

variable_types:
newprefmaxlen
unsigned int


decompiled_code:
__int64 __fastcall fbuf_pref_maxlen(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

canonical_decompiled_code:
__int64 __fastcall func24(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

original_code:
unsigned int
fbuf_pref_maxlen(fbuf_t *fbuf)
{
    return fbuf->prefmaxlen;
}

canonical_original_code:
unsigned int fbuf_pref_maxlen(struct __fbuf *fbuf)
{
    return fbuf->prefmaxlen;
}


decompiled_code:
__int64 __fastcall fbuf_set_maxlen(unsigned int a1)
{
globalMaxLen = a1;
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func25(unsigned int a1)
{
globalMaxLen = a1;
return a1;
}

original_code:
unsigned int
fbuf_set_maxlen(unsigned int maxlen)
{
    return (globalMaxLen = maxlen);
}

variable_types:
maxlen
unsigned int


decompiled_code:
__int64 fbuf_maxlen()
{
return (unsigned int)globalMaxLen;
}

canonical_decompiled_code:
__int64 func26()
{
return (unsigned int)globalMaxLen;
}

original_code:
unsigned int
fbuf_maxlen(void)
{
    return globalMaxLen;
}

canonical_original_code:
unsigned int fbuf_maxlen()
{
    return globalMaxLen;
}


original_code:
unsigned long
byte_escape(char ch, char esc, char *buffer, unsigned long len, char **dest, unsigned long *newlen)
{
    char *newbuf;
    unsigned long buflen;
    unsigned long i;
    unsigned long cnt;
    int escape;
    char *p;
    unsigned long off;
    if(len == 0)
        return 0;
    newbuf = (char *)malloc(len);
    if(!newbuf)
        return 0;
    buflen = len;
    p = buffer;
    off = 0;
    cnt = 0;
    for(i=0;i<len;i++)
    {
        escape = 0;
        if(*p == ch)
            cnt ++;
        if(*p == ch || *p == esc)
            escape = 1;
        if(escape)
        {
            buflen++;
            newbuf = (char *)realloc(newbuf, buflen+1);
            memcpy(newbuf+off, &esc, 1);
            off++;
        }
        memcpy(newbuf+off, p, 1);
        p++;
        off++;
    }
    *dest = newbuf;
    *newlen = buflen;
    return cnt;
}

variable_types: ch char

variable_types: esc char

variable_types:
buffer
char *

variable_types:
len
unsigned long

variable_types:
dest
char **

variable_types:
newlen
unsigned long *

variable_types:
newbuf
char *

variable_types:
buflen
unsigned long

variable_types:
i
unsigned long

variable_types:
cnt
unsigned long

variable_types: escape int

variable_types:
p
char *

variable_types:
off
unsigned long

return_type: unsigned long


decompiled_code:
char *__fastcall hex_escape(char *a1, int a2)
{
char *result; // rax
char *v4; // rbx
char *v5; // r13
__int64 v6; // rdx
char *v7; // rdi

result = (char *)realloc(str_0, 2 * a2 + 4);
str_0 = result;
strcpy(result, "0x");
if ( a2 > 0 )
{
v4 = result + 2;
v5 = &result[2 * (a2 + 1)];
do
{
v6 = (unsigned int)*a1;
v7 = v4;
v4 += 2;
++a1;
sprintf(v7, "%02x", v6);
}
while ( v4 != v5 );
result = (char *)str_0;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func1(char *a1, int a2)
{
char *result; // rax
char *v4; // rbx
char *v5; // r13
__int64 v6; // rdx
char *v7; // rdi

result = (char *)realloc(str_0, 2 * a2 + 4);
str_0 = result;
strcpy(result, "0x");
if ( a2 > 0 )
{
v4 = result + 2;
v5 = &result[2 * (a2 + 1)];
do
{
v6 = (unsigned int)*a1;
v7 = v4;
v4 += 2;
++a1;
sprintf(v7, "%02x", v6);
}
while ( v4 != v5 );
result = (char *)str_0;
}
return result;
}

original_code:
char *hex_escape(const char *buf, int len) {
    int i;
    static char *str = ((void *)0);
    str = realloc(str, (len*2)+4);
    strcpy(str, "0x");
    char *p = str+2;
    for (i = 0; i < len; i++) {
        sprintf(p, "%02x", buf[i]);
        p+=2;
    }
    return str;
}

canonical_original_code:
char *hex_escape(char *buf, int len) {
    int i;
    static char *str = ((void *)0);
    str = realloc(str, (len*2)+4);
    strcpy(str, "0x");
    char *p = str+2;
    for (i = 0; i < len; i++) {
        sprintf(p, "%02x", buf[i]);
        p+=2;
    }
    return str;
}

variable_types:
buf
char *

variable_types: len int

variable_types: i int

variable_types:
str
char *


decompiled_code:
int __fastcall log_init(const char *a1, int a2)
{
_loglevel = a2;
openlog(a1, 34, 128);
return setlogmask(255);
}

canonical_decompiled_code:
int __fastcall func2(const char *a1, int a2)
{
_loglevel = a2;
openlog(a1, 34, 128);
return setlogmask(255);
}

original_code:
void log_init(char *ident, int loglevel)
{
    __loglevel = loglevel;
    openlog(ident, 0x02|0x20, (16<<3));
    setlogmask(((1 << ((7)+1)) - 1));
}

variable_types:
ident
char *

variable_types: loglevel int

return_type: void


decompiled_code:
__int64 log_level()
{
return (unsigned int)_loglevel;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned int)_loglevel;
}

original_code:
unsigned int log_level()
{
    return __loglevel;
}


decompiled_code:
void log_message(int pri, int a2, const char *a3, ...)
{
const char *v4; // r13
size_t v5; // r14
size_t v6; // rax
char *v7; // rax
char *v8; // r14
gcc_va_list ap; // [rsp+8h] [rbp-F0h] BYREF

switch ( pri )
{
case 3:
v4 = "[ERROR]: ";
if ( a3 )
goto LABEL_6;
return;
case 4:
v4 = "[WARNING]: ";
goto LABEL_3;
case 5:
v4 = "[NOTICE]: ";
goto LABEL_3;
case 6:
v4 = "[INFO]: ";
goto LABEL_3;
case 7:
v4 = "[DBG3]: ";
if ( a2 != 3 )
{
if ( a2 > 3 )
{
v4 = "[DBGX]: ";
if ( a2 == 4 )
v4 = "[DBG4]: ";
}
else
{
v4 = "[DBG]: ";
if ( a2 != 1 )
{
v4 = "[DBGX]: ";
if ( a2 == 2 )
v4 = "[DBG2]: ";
}
}
}
goto LABEL_3;
default:
v4 = "[UNKNOWN]: ";
LABEL_3:
if ( a3 )
{
LABEL_6:
v5 = strlen(a3);
v6 = strlen(v4);
v7 = (char *)calloc(1uLL, v5 + v6 + 1);
v8 = v7;
if ( v7 )
{
sprintf(v7, "%s%s", v4, a3);
va_start(ap, a3);
vsyslog(pri, v8, ap);
free(v8);
}
}
return;
}
}

canonical_decompiled_code:
void func4(int pri, int a2, const char *a3, ...)
{
const char *v4; // r13
size_t v5; // r14
size_t v6; // rax
char *v7; // rax
char *v8; // r14
gcc_va_list ap; // [rsp+8h] [rbp-F0h] BYREF

switch ( pri )
{
case 3:
v4 = "[ERROR]: ";
if ( a3 )
goto LABEL_6;
return;
case 4:
v4 = "[WARNING]: ";
goto LABEL_3;
case 5:
v4 = "[NOTICE]: ";
goto LABEL_3;
case 6:
v4 = "[INFO]: ";
goto LABEL_3;
case 7:
v4 = "[DBG3]: ";
if ( a2 != 3 )
{
if ( a2 > 3 )
{
v4 = "[DBGX]: ";
if ( a2 == 4 )
v4 = "[DBG4]: ";
}
else
{
v4 = "[DBG]: ";
if ( a2 != 1 )
{
v4 = "[DBGX]: ";
if ( a2 == 2 )
v4 = "[DBG2]: ";
}
}
}
goto LABEL_3;
default:
v4 = "[UNKNOWN]: ";
LABEL_3:
if ( a3 )
{
LABEL_6:
v5 = strlen(a3);
v6 = strlen(v4);
v7 = (char *)calloc(1uLL, v5 + v6 + 1);
v8 = v7;
if ( v7 )
{
sprintf(v7, "%s%s", v4, a3);
va_start(ap, a3);
vsyslog(pri, v8, ap);
free(v8);
}
}
return;
}
}

original_code:
void log_message(int prio, int dbglevel, const char *fmt, ...)
{
    char *newfmt = ((void *)0);
    const char *prefix = ((void *)0);
    switch (prio) {
        case 3:
            prefix = "[ERROR]: ";
            break;
        case 4:
            prefix = "[WARNING]: ";
            break;
        case 5:
            prefix = "[NOTICE]: ";
            break;
        case 6:
            prefix = "[INFO]: ";
            break;
        case 7:
            switch (dbglevel) {
                case 1:
                    prefix = "[DBG]: ";
                    break;
                case 2:
                    prefix = "[DBG2]: ";
                    break;
                case 3:
                    prefix = "[DBG3]: ";
                    break;
                case 4:
                    prefix = "[DBG4]: ";
                    break;
                default:
                    prefix = "[DBGX]: ";
                    break;
            }
            break;
        default:
            prefix = "[UNKNOWN]: ";
            break;
    }
    if (prefix && fmt) {
        newfmt = (char *)calloc(1, strlen(fmt)+strlen(prefix)+1);
        if (newfmt) {
            sprintf(newfmt, "%s%s", prefix, fmt);
            va_list arg;
            __builtin_va_start(arg,fmt);
            vsyslog(prio, newfmt, arg);
            __builtin_va_end(arg);
            free(newfmt);
        }
    }
}

canonical_original_code:
void log_message(int prio, int dbglevel, char *fmt, ...)
{
    char *newfmt = ((void *)0);
    char *prefix = ((void *)0);
    switch (prio) {
        case 3:
            prefix = "[ERROR]: ";
            break;
        case 4:
            prefix = "[WARNING]: ";
            break;
        case 5:
            prefix = "[NOTICE]: ";
            break;
        case 6:
            prefix = "[INFO]: ";
            break;
        case 7:
            switch (dbglevel) {
                case 1:
                    prefix = "[DBG]: ";
                    break;
                case 2:
                    prefix = "[DBG2]: ";
                    break;
                case 3:
                    prefix = "[DBG3]: ";
                    break;
                case 4:
                    prefix = "[DBG4]: ";
                    break;
                default:
                    prefix = "[DBGX]: ";
                    break;
            }
            break;
        default:
            prefix = "[UNKNOWN]: ";
            break;
    }
    if (prefix && fmt) {
        newfmt = (char *)calloc(1, strlen(fmt)+strlen(prefix)+1);
        if (newfmt) {
            sprintf(newfmt, "%s%s", prefix, fmt);
            __builtin_va_list arg;
            __builtin_va_start(arg,fmt);
            vsyslog(prio, newfmt, arg);
            __builtin_va_end(arg);
            free(newfmt);
        }
    }
}

variable_types: prio int

variable_types: dbglevel int

variable_types:
fmt
char *

variable_types:
newfmt
char *

variable_types:
prefix
char *


repo: xcvista/libcore

decompiled_code:
bool __fastcall vector_binary_compare_internal(const void *a1, const void *a2)
{
return memcmp(a1, a2, vector_current_size) == 0;
}

canonical_decompiled_code:
bool __fastcall func0(const void *a1, const void *a2)
{
return memcmp(a1, a2, vector_current_size) == 0;
}

original_code:
_Bool vector_binary_compare_internal(const void *r1, const void *r2)
{
    return !memcmp(r1, r2, vector_current_size);
}

canonical_original_code:
_Bool vector_binary_compare_internal(void *r1, void *r2)
{
    return !memcmp(r1, r2, vector_current_size);
}


decompiled_code:
__int64 __fastcall vector_max_pointer(_QWORD *a1)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x12u, "vector_max_pointer");
return *a1 + a1[3] * a1[2];
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x12u, "vector_max_pointer");
return *a1 + a1[3] * a1[2];
}

original_code:
void *vector_max_pointer(vector_t vec)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 18, __extension__ __PRETTY_FUNCTION__); }));
    void *base = vec -> data;
    size_t size = vec -> element_size * vec -> count;
    return base + size;
}

canonical_original_code:
void *vector_max_pointer(struct __vector_s *vec)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 18, __extension__ __PRETTY_FUNCTION__); }));
    void *base = vec -> data;
    unsigned long size = vec -> element_size * vec -> count;
    return base + size;
}

variable_types: struct __vector_s { void *data; unsigned long alloc_size; unsigned long element_size; unsigned long count; } *

variable_types:
base
void *

variable_types:
size
unsigned long

return_type: void *


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

if ( !a1 )
_assert_fail("vec", "vector.c", 0x23u, "vector_indexof_ptr");
if ( a2 >= vector_max_pointer(a1) || *a1 >= a2 )
result = 0xFFFFFFFFLL;
else
result = (a2 - *a1) / a1[2];
return result;
}

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

if ( !a1 )
_assert_fail("vec", "vector.c", 0x23u, "vector_indexof_ptr");
if ( a2 >= func1(a1) || *a1 >= a2 )
result = 0xFFFFFFFFLL;
else
result = (a2 - *a1) / a1[2];
return result;
}

original_code:
int vector_indexof_ptr(vector_t vec, const void *ptr)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 35, __extension__ __PRETTY_FUNCTION__); }));
    void *max = vector_max_pointer(vec);
    if (ptr >= max || ptr <= vec -> data)
        return (-1);
    else
        return (int)((ptr - vec -> data) / vec -> element_size);
}

canonical_original_code:
int vector_indexof_ptr(struct __vector_s *vec, void *ptr)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 35, __extension__ __PRETTY_FUNCTION__); }));
    void *max = vector_max_pointer(vec);
    if (ptr >= max || ptr <= vec -> data)
        return (-1);
    else
        return (int)((ptr - vec -> data) / vec -> element_size);
}

variable_types:
ptr
void *

variable_types:
max
void *

user_defined_types: struct __vector_s { void *data; unsigned long alloc_size; unsigned long element_size; unsigned long count; }


decompiled_code:
__int64 __fastcall vector_count(__int64 a1)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x71u, "vector_count");
return *(_QWORD *)(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x71u, "vector_count");
return *(_QWORD *)(a1 + 24);
}

original_code:
size_t vector_count(vector_t vec)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 113, __extension__ __PRETTY_FUNCTION__); }));
    return vec -> count;
}

canonical_original_code:
unsigned long vector_count(struct __vector_s *vec)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 113, __extension__ __PRETTY_FUNCTION__); }));
    return vec -> count;
}


decompiled_code:
void *__fastcall vector_alloc_object(__int64 a1)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x7Au, "vector_alloc_object");
return calloc(*(_QWORD *)(a1 + 16), 1uLL);
}

canonical_decompiled_code:
void *__fastcall func5(__int64 a1)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x7Au, "vector_alloc_object");
return calloc(*(_QWORD *)(a1 + 16), 1uLL);
}

original_code:
void *vector_alloc_object(vector_t vec)
{
    void *block = ((void *)0);
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 122, __extension__ __PRETTY_FUNCTION__); }));
    block = malloc(vec -> element_size);
    if (block)
        memset(block, 0, vec -> element_size);
    return block;
}

canonical_original_code:
void *vector_alloc_object(struct __vector_s *vec)
{
    void *block = ((void *)0);
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 122, __extension__ __PRETTY_FUNCTION__); }));
    block = malloc(vec -> element_size);
    if (block)
        memset(block, 0, vec -> element_size);
    return block;
}

variable_types:
block
void *


decompiled_code:
void *__fastcall vector_objectat(_QWORD *a1, void *a2, unsigned __int64 a3)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x87u, "vector_objectat");
if ( !a2 )
_assert_fail("buf", "vector.c", 0x88u, "vector_objectat");
if ( a1[3] <= a3 )
_assert_fail("index < vec -> count", "vector.c", 0x89u, "vector_objectat");
return memcpy(a2, (const void *)(a1[2] * a3 + *a1), a1[2]);
}

canonical_decompiled_code:
void *__fastcall func6(_QWORD *a1, void *a2, unsigned __int64 a3)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x87u, "vector_objectat");
if ( !a2 )
_assert_fail("buf", "vector.c", 0x88u, "vector_objectat");
if ( a1[3] <= a3 )
_assert_fail("index < vec -> count", "vector.c", 0x89u, "vector_objectat");
return memcpy(a2, (const void *)(a1[2] * a3 + *a1), a1[2]);
}

original_code:
void *vector_objectat(vector_t vec, void *buf, size_t index)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 135, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((buf) ? 1 : 0), __extension__ ({ if (buf) ; else __assert_fail ("buf", "vector.c", 136, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((index < vec -> count) ? 1 : 0), __extension__ ({ if (index < vec -> count) ; else __assert_fail ("index < vec -> count", "vector.c", 137, __extension__ __PRETTY_FUNCTION__); }));
    void *ptr = vec -> data + index * vec -> element_size;
    return memcpy(buf, ptr, vec -> element_size);
}

canonical_original_code:
void *vector_objectat(struct __vector_s *vec, void *buf, unsigned long index)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 135, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((buf) ? 1 : 0), __extension__ ({ if (buf) ; else __assert_fail ("buf", "vector.c", 136, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((index < vec -> count) ? 1 : 0), __extension__ ({ if (index < vec -> count) ; else __assert_fail ("index < vec -> count", "vector.c", 137, __extension__ __PRETTY_FUNCTION__); }));
    void *ptr = vec -> data + index * vec -> element_size;
    return memcpy(buf, ptr, vec -> element_size);
}

variable_types:
buf
void *

variable_types:
index
unsigned long


decompiled_code:
void *__fastcall vector_set(_QWORD *a1, const void *a2, __int64 a3)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0xC1u, "vector_set");
if ( !a2 )
_assert_fail("object", "vector.c", 0xC2u, "vector_set");
return memcpy((void *)(*a1 + a1[2] * a3), a2, a1[2]);
}

canonical_decompiled_code:
void *__fastcall func7(_QWORD *a1, const void *a2, __int64 a3)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0xC1u, "vector_set");
if ( !a2 )
_assert_fail("object", "vector.c", 0xC2u, "vector_set");
return memcpy((void *)(*a1 + a1[2] * a3), a2, a1[2]);
}

original_code:
void vector_set(vector_t vec, const void *object, size_t index)
{
    void *ptr = ((void *)0);
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 193, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 194, __extension__ __PRETTY_FUNCTION__); }));
    ptr = vec -> data + vec -> element_size * index;
    memcpy(ptr, object, vec -> element_size);
}

canonical_original_code:
void vector_set(struct __vector_s *vec, void *object, unsigned long index)
{
    void *ptr = ((void *)0);
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 193, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 194, __extension__ __PRETTY_FUNCTION__); }));
    ptr = vec -> data + vec -> element_size * index;
    memcpy(ptr, object, vec -> element_size);
}

variable_types:
object
void *


decompiled_code:
void *__fastcall vector_removeall(_QWORD *a1, __int64 a2, __int64 (__fastcall *a3)(void *, __int64))
{
void *result; // rax
void *v5; // rbp
unsigned __int64 v6; // rbx

if ( !a1 )
_assert_fail("vec", "vector.c", 0xDDu, "vector_removeall");
if ( !a2 )
_assert_fail("object", "vector.c", 0xDEu, "vector_removeall");
if ( !a3 )
_assert_fail("eq", "vector.c", 0xDFu, "vector_removeall");
result = vector_alloc_object((__int64)a1);
v5 = result;
if ( result )
{
vector_current_size = a1[2];
result = (void *)a1[3];
v6 = (unsigned __int64)result - 1;
if ( (__int64)result - 1 > 0 )
{
do
{
while ( 1 )
{
result = vector_objectat(a1, v5, v6);
if ( result )
{
result = (void *)a3(v5, a2);
if ( (_BYTE)result )
break;
}
if ( !--v6 )
return result;
}
result = vector_removeat(a1, v6--);
}
while ( v6 );
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func9(_QWORD *a1, __int64 a2, __int64 (__fastcall *a3)(void *, __int64))
{
void *result; // rax
void *v5; // rbp
unsigned __int64 v6; // rbx

if ( !a1 )
_assert_fail("vec", "vector.c", 0xDDu, "vector_removeall");
if ( !a2 )
_assert_fail("object", "vector.c", 0xDEu, "vector_removeall");
if ( !a3 )
_assert_fail("eq", "vector.c", 0xDFu, "vector_removeall");
result = func5((__int64)a1);
v5 = result;
if ( result )
{
vector_current_size = a1[2];
result = (void *)a1[3];
v6 = (unsigned __int64)result - 1;
if ( (__int64)result - 1 > 0 )
{
do
{
while ( 1 )
{
result = func6(a1, v5, v6);
if ( result )
{
result = (void *)a3(v5, a2);
if ( (_BYTE)result )
break;
}
if ( !--v6 )
return result;
}
result = func8(a1, v6--);
}
while ( v6 );
}
}
return result;
}

original_code:
void vector_removeall(vector_t vec, const void *object, equality_t eq)
{
    ssize_t idx = 0;
    void *buf = ((void *)0);
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 221, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 222, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((eq) ? 1 : 0), __extension__ ({ if (eq) ; else __assert_fail ("eq", "vector.c", 223, __extension__ __PRETTY_FUNCTION__); }));
    buf = vector_alloc_object(vec);
    if (!buf)
        return;
    vector_current_size = vec -> element_size;
    for (idx = vec -> count - 1; idx > 0; idx--)
    {
        if (vector_objectat(vec, buf, idx))
        {
            if (eq(buf, object))
                vector_removeat(vec, idx);
        }
    }
}

canonical_original_code:
void vector_removeall(struct __vector_s *vec, void *object, _Bool (*eq)(void *, void *))
{
    long idx = 0;
    void *buf = ((void *)0);
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 221, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 222, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((eq) ? 1 : 0), __extension__ ({ if (eq) ; else __assert_fail ("eq", "vector.c", 223, __extension__ __PRETTY_FUNCTION__); }));
    buf = vector_alloc_object(vec);
    if (!buf)
        return;
    vector_current_size = vec -> element_size;
    for (idx = vec -> count - 1; idx > 0; idx--)
    {
        if (vector_objectat(vec, buf, idx))
        {
            if (eq(buf, object))
                vector_removeat(vec, idx);
        }
    }
}

variable_types:
eq
_Bool (*)(void *, void *)

variable_types: idx long


decompiled_code:
void __fastcall vector_sort(__int64 a1, int (*a2)(const void *, const void *))
{
qsort(*(void **)a1, *(_QWORD *)(a1 + 24), *(_QWORD *)(a1 + 16), a2);
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1, int (*a2)(const void *, const void *))
{
qsort(*(void **)a1, *(_QWORD *)(a1 + 24), *(_QWORD *)(a1 + 16), a2);
}

original_code:
void vector_sort(vector_t vec, comparator_t comp)
{
    qsort(vec -> data,
          vec -> count,
          vec -> element_size,
          comp);
}

canonical_original_code:
void vector_sort(struct __vector_s *vec, enum __comparison_result (*comp)(void *, void *))
{
    qsort(vec -> data,
          vec -> count,
          vec -> element_size,
          comp);
}

variable_types: enum __comparison_result { compare_ascending=-1, compare_same=0, compare_descending=1 } (*)(void *, void *)


decompiled_code:
__int64 __fastcall vector_expand(__int64 a1, unsigned __int64 a2)
{
size_t v2; // rax
size_t v3; // rsi
void *v4; // rax
void *v5; // rax

if ( !a1 )
_assert_fail("vec", "vector.c", 0x111u, "vector_expand");
if ( *(_QWORD *)(a1 + 24) > a2 )
_assert_fail("alloc_size >= vec -> count", "vector.c", 0x112u, "vector_expand");
v2 = *(_QWORD *)(a1 + 16);
v3 = v2 * a2;
if ( v2 >= v3 )
v3 = *(_QWORD *)(a1 + 16);
v4 = calloc(v3, 1uLL);
if ( !v4 )
return 0LL;
v5 = memcpy(v4, *(const void **)a1, *(_QWORD *)(a1 + 8));
*(_QWORD *)(a1 + 8) = v3;
*(_QWORD *)a1 = v5;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, unsigned __int64 a2)
{
size_t v2; // rax
size_t v3; // rsi
void *v4; // rax
void *v5; // rax

if ( !a1 )
_assert_fail("vec", "vector.c", 0x111u, "vector_expand");
if ( *(_QWORD *)(a1 + 24) > a2 )
_assert_fail("alloc_size >= vec -> count", "vector.c", 0x112u, "vector_expand");
v2 = *(_QWORD *)(a1 + 16);
v3 = v2 * a2;
if ( v2 >= v3 )
v3 = *(_QWORD *)(a1 + 16);
v4 = calloc(v3, 1uLL);
if ( !v4 )
return 0LL;
v5 = memcpy(v4, *(const void **)a1, *(_QWORD *)(a1 + 8));
*(_QWORD *)(a1 + 8) = v3;
*(_QWORD *)a1 = v5;
return 1LL;
}

original_code:
_Bool vector_expand(vector_t vec, size_t alloc_size)
{
    void *buf = ((void *)0);
    size_t size;
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 273, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((alloc_size >= vec -> count) ? 1 : 0), __extension__ ({ if (alloc_size >= vec -> count) ; else __assert_fail ("alloc_size >= vec -> count", "vector.c", 274, __extension__ __PRETTY_FUNCTION__); }));
    size = alloc_size * vec -> element_size;
    if (size < vec -> element_size)
        size = vec -> element_size;
    buf = malloc(size);
    if (!buf)
        return 0;
    memset(buf, 0, size);
    memcpy(buf, vec -> data, vec -> alloc_size);
    vec -> data = buf;
    vec -> alloc_size = size;
    return 1;
}

canonical_original_code:
_Bool vector_expand(struct __vector_s *vec, unsigned long alloc_size)
{
    void *buf = ((void *)0);
    unsigned long size;
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 273, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((alloc_size >= vec -> count) ? 1 : 0), __extension__ ({ if (alloc_size >= vec -> count) ; else __assert_fail ("alloc_size >= vec -> count", "vector.c", 274, __extension__ __PRETTY_FUNCTION__); }));
    size = alloc_size * vec -> element_size;
    if (size < vec -> element_size)
        size = vec -> element_size;
    buf = malloc(size);
    if (!buf)
        return 0;
    memset(buf, 0, size);
    memcpy(buf, vec -> data, vec -> alloc_size);
    vec -> data = buf;
    vec -> alloc_size = size;
    return 1;
}


decompiled_code:
__int64 __fastcall vector_append(_QWORD *a1, const void *a2)
{
size_t v3; // r12
size_t v4; // rsi
__int64 result; // rax
void *v6; // rax

if ( !a1 )
_assert_fail("vec", "vector.c", 0xAEu, "vector_append");
if ( !a2 )
_assert_fail("object", "vector.c", 0xAFu, "vector_append");
v3 = a1[2];
v4 = a1[3];
if ( v4 <= a1[1] / v3 )
{
result = vector_expand((__int64)a1, v4 + 1);
if ( !(_BYTE)result )
return result;
v3 = a1[2];
}
v6 = (void *)vector_max_pointer(a1);
memcpy(v6, a2, v3);
++a1[3];
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1, const void *a2)
{
size_t v3; // r12
size_t v4; // rsi
__int64 result; // rax
void *v6; // rax

if ( !a1 )
_assert_fail("vec", "vector.c", 0xAEu, "vector_append");
if ( !a2 )
_assert_fail("object", "vector.c", 0xAFu, "vector_append");
v3 = a1[2];
v4 = a1[3];
if ( v4 <= a1[1] / v3 )
{
result = func11((__int64)a1, v4 + 1);
if ( !(_BYTE)result )
return result;
v3 = a1[2];
}
v6 = (void *)func1(a1);
memcpy(v6, a2, v3);
++a1[3];
return 1LL;
}

original_code:
_Bool vector_append(vector_t vec, const void * object)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 174, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 175, __extension__ __PRETTY_FUNCTION__); }));
    if (vec -> count <= vec -> alloc_size / vec -> element_size)
        if (!vector_expand(vec, vec -> count + 1))
            return 0;
    memcpy(vector_max_pointer(vec), object, vec -> element_size);
    vec -> count++;
    return 1;
}

canonical_original_code:
_Bool vector_append(struct __vector_s *vec, void *object)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 174, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 175, __extension__ __PRETTY_FUNCTION__); }));
    if (vec -> count <= vec -> alloc_size / vec -> element_size)
        if (!vector_expand(vec, vec -> count + 1))
            return 0;
    memcpy(vector_max_pointer(vec), object, vec -> element_size);
    vec -> count++;
    return 1;
}


decompiled_code:
__int64 __fastcall vector_insert(_QWORD *a1, const void *a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rsi
unsigned __int64 v6; // rcx
__int64 result; // rax
char *v8; // rbp
char *v9; // r13
__int64 v10; // rax

if ( !a1 )
_assert_fail("vec", "vector.c", 0x96u, "vector_insert");
if ( !a2 )
_assert_fail("object", "vector.c", 0x97u, "vector_insert");
v4 = a1[3];
if ( v4 < a3 )
_assert_fail("index <= vec -> count", "vector.c", 0x98u, "vector_insert");
if ( v4 == a3 )
return vector_append(a1, a2);
v6 = a1[2];
if ( v4 <= a1[1] / v6 )
{
result = vector_expand((__int64)a1, v4 + 1);
if ( !(_BYTE)result )
return result;
v6 = a1[2];
}
v8 = (char *)(*a1 + v6 * a3);
v9 = &v8[v6];
v10 = vector_max_pointer(a1);
memmove(v9, v8, v10 - (_QWORD)v8);
memcpy(v8, a2, a1[2]);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func13(_QWORD *a1, const void *a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rsi
unsigned __int64 v6; // rcx
__int64 result; // rax
char *v8; // rbp
char *v9; // r13
__int64 v10; // rax

if ( !a1 )
_assert_fail("vec", "vector.c", 0x96u, "vector_insert");
if ( !a2 )
_assert_fail("object", "vector.c", 0x97u, "vector_insert");
v4 = a1[3];
if ( v4 < a3 )
_assert_fail("index <= vec -> count", "vector.c", 0x98u, "vector_insert");
if ( v4 == a3 )
return func12(a1, a2);
v6 = a1[2];
if ( v4 <= a1[1] / v6 )
{
result = func11((__int64)a1, v4 + 1);
if ( !(_BYTE)result )
return result;
v6 = a1[2];
}
v8 = (char *)(*a1 + v6 * a3);
v9 = &v8[v6];
v10 = func1(a1);
memmove(v9, v8, v10 - (_QWORD)v8);
memcpy(v8, a2, a1[2]);
return 1LL;
}

original_code:
_Bool vector_insert(vector_t vec, const void *object, size_t index)
{
    void *ptr = ((void *)0);
    void *ptrnext = ((void *)0);
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 150, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 151, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((index <= vec -> count) ? 1 : 0), __extension__ ({ if (index <= vec -> count) ; else __assert_fail ("index <= vec -> count", "vector.c", 152, __extension__ __PRETTY_FUNCTION__); }));
    if (index == vec -> count)
        return vector_append(vec, object);
    if (vec -> count <= vec -> alloc_size / vec -> element_size)
        if (!vector_expand(vec, vec -> count + 1))
            return 0;
    ptr = vec -> data + vec -> element_size * index;
    ptrnext = ptr + vec -> element_size;
    memmove(ptrnext, ptr, vector_max_pointer(vec) - ptr);
    memcpy(ptr, object, vec -> element_size);
    return 1;
}

canonical_original_code:
_Bool vector_insert(struct __vector_s *vec, void *object, unsigned long index)
{
    void *ptr = ((void *)0);
    void *ptrnext = ((void *)0);
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 150, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 151, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((index <= vec -> count) ? 1 : 0), __extension__ ({ if (index <= vec -> count) ; else __assert_fail ("index <= vec -> count", "vector.c", 152, __extension__ __PRETTY_FUNCTION__); }));
    if (index == vec -> count)
        return vector_append(vec, object);
    if (vec -> count <= vec -> alloc_size / vec -> element_size)
        if (!vector_expand(vec, vec -> count + 1))
            return 0;
    ptr = vec -> data + vec -> element_size * index;
    ptrnext = ptr + vec -> element_size;
    memmove(ptrnext, ptr, vector_max_pointer(vec) - ptr);
    memcpy(ptr, object, vec -> element_size);
    return 1;
}

variable_types:
ptrnext
void *


decompiled_code:
__int64 __fastcall vector_compact(__int64 a1)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x128u, "vector_compact");
return vector_expand(a1, *(_QWORD *)(a1 + 24));
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x128u, "vector_compact");
return func11(a1, *(_QWORD *)(a1 + 24));
}

original_code:
_Bool vector_compact(vector_t vec)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 296, __extension__ __PRETTY_FUNCTION__); }));
    return vector_expand(vec, vec -> count);
}

canonical_original_code:
_Bool vector_compact(struct __vector_s *vec)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 296, __extension__ __PRETTY_FUNCTION__); }));
    return vector_expand(vec, vec -> count);
}


decompiled_code:
void __fastcall vector_fini(void *ptr)
{
void *v2; // rdi

if ( !ptr )
_assert_fail("vec", "vector.c", 0x130u, "vector_fini");
v2 = *(void **)ptr;
if ( v2 )
free(v2);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func15(void *ptr)
{
void *v2; // rdi

if ( !ptr )
_assert_fail("vec", "vector.c", 0x130u, "vector_fini");
v2 = *(void **)ptr;
if ( v2 )
free(v2);
free(ptr);
}

original_code:
void vector_fini(vector_t vec)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 304, __extension__ __PRETTY_FUNCTION__); }));
    if (vec -> data)
        free(vec -> data);
    free(vec);
}

canonical_original_code:
void vector_fini(struct __vector_s *vec)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 304, __extension__ __PRETTY_FUNCTION__); }));
    if (vec -> data)
        free(vec -> data);
    free(vec);
}


decompiled_code:
__int64 __fastcall vector_begin_iterator(__int64 a1)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x148u, "vector_begin_iterator");
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1)
{
if ( !a1 )
_assert_fail("vec", "vector.c", 0x148u, "vector_begin_iterator");
return *(_QWORD *)a1;
}

original_code:
void *vector_begin_iterator(vector_t vec)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 328, __extension__ __PRETTY_FUNCTION__); }));
    return vec->data;
}

canonical_original_code:
void *vector_begin_iterator(struct __vector_s *vec)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 328, __extension__ __PRETTY_FUNCTION__); }));
    return vec->data;
}


decompiled_code:
bool __fastcall vector_check_iterator(_QWORD *a1, __int64 a2)
{
bool result; // al

if ( !a1 )
_assert_fail("vec", "vector.c", 0x14Fu, "vector_check_iterator");
if ( a2 )
result = (unsigned __int64)(a2 - *a1) / a1[2] < a1[3];
else
result = 0;
return result;
}

canonical_decompiled_code:
bool __fastcall func17(_QWORD *a1, __int64 a2)
{
bool result; // al

if ( !a1 )
_assert_fail("vec", "vector.c", 0x14Fu, "vector_check_iterator");
if ( a2 )
result = (unsigned __int64)(a2 - *a1) / a1[2] < a1[3];
else
result = 0;
return result;
}

original_code:
_Bool vector_check_iterator(vector_t vec, const void *it)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 335, __extension__ __PRETTY_FUNCTION__); }));
    if (!it)
        return 0;
    return ((it - vec->data) / vec->element_size) < vec->count ? 1 : 0;
}

canonical_original_code:
_Bool vector_check_iterator(struct __vector_s *vec, void *it)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 335, __extension__ __PRETTY_FUNCTION__); }));
    if (!it)
        return 0;
    return ((it - vec->data) / vec->element_size) < vec->count ? 1 : 0;
}

variable_types:
it
void *


decompiled_code:
__int64 __fastcall vector_next_iterator(_QWORD *a1, __int64 a2)
{
unsigned __int64 v2; // rcx
unsigned __int64 v3; // rax
__int64 v4; // rsi
bool v5; // cf
__int64 result; // rax

if ( !a1 )
_assert_fail("vec", "vector.c", 0x158u, "vector_next_iterator");
v2 = a1[2];
v3 = (a2 - *a1) / v2;
v4 = v2 + a2;
v5 = v3 < a1[3] - 1LL;
result = 0LL;
if ( v5 )
result = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func18(_QWORD *a1, __int64 a2)
{
unsigned __int64 v2; // rcx
unsigned __int64 v3; // rax
__int64 v4; // rsi
bool v5; // cf
__int64 result; // rax

if ( !a1 )
_assert_fail("vec", "vector.c", 0x158u, "vector_next_iterator");
v2 = a1[2];
v3 = (a2 - *a1) / v2;
v4 = v2 + a2;
v5 = v3 < a1[3] - 1LL;
result = 0LL;
if ( v5 )
result = v4;
return result;
}

original_code:
void *vector_next_iterator(vector_t vec, const void *it)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 344, __extension__ __PRETTY_FUNCTION__); }));
    if (((it - vec->data) / vec->element_size) < (vec->count - 1))
        return (void *)it + vec->element_size;
    else
        return ((void *)0);
}

canonical_original_code:
void *vector_next_iterator(struct __vector_s *vec, void *it)
{
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 344, __extension__ __PRETTY_FUNCTION__); }));
    if (((it - vec->data) / vec->element_size) < (vec->count - 1))
        return (void *)it + vec->element_size;
    else
        return ((void *)0);
}


decompiled_code:
__int64 __fastcall vector_indexof(unsigned __int64 *a1, __int64 a2, unsigned __int8 (__fastcall *a3)(__int64, __int64))
{
__int64 i; // rbp

if ( !a1 )
_assert_fail("vec", "vector.c", 0x5Bu, "vector_indexof");
if ( !a2 )
_assert_fail("object", "vector.c", 0x5Cu, "vector_indexof");
if ( !a3 )
_assert_fail("eq", "vector.c", 0x5Du, "vector_indexof");
if ( vector_alloc_object((__int64)a1) )
{
vector_current_size = a1[2];
for ( i = vector_begin_iterator((__int64)a1); vector_check_iterator(a1, i); i = vector_next_iterator(a1, i) )
{
if ( a3(i, a2) )
return (int)vector_indexof_ptr(a1, i);
}
}
return -1LL;
}

canonical_decompiled_code:
__int64 __fastcall func19(unsigned __int64 *a1, __int64 a2, unsigned __int8 (__fastcall *a3)(__int64, __int64))
{
__int64 i; // rbp

if ( !a1 )
_assert_fail("vec", "vector.c", 0x5Bu, "vector_indexof");
if ( !a2 )
_assert_fail("object", "vector.c", 0x5Cu, "vector_indexof");
if ( !a3 )
_assert_fail("eq", "vector.c", 0x5Du, "vector_indexof");
if ( func5((__int64)a1) )
{
vector_current_size = a1[2];
for ( i = func16((__int64)a1); func17(a1, i); i = func18(a1, i) )
{
if ( a3(i, a2) )
return (int)func3(a1, i);
}
}
return -1LL;
}

original_code:
ssize_t vector_indexof(vector_t vec, const void *object, equality_t eq)
{
    void *buf = ((void *)0);
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 91, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 92, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((eq) ? 1 : 0), __extension__ ({ if (eq) ; else __assert_fail ("eq", "vector.c", 93, __extension__ __PRETTY_FUNCTION__); }));
    buf = vector_alloc_object(vec);
    if (!buf)
        return (-1);
    vector_current_size = vec -> element_size;
    for (void * obj = vector_begin_iterator(vec); vector_check_iterator(vec, obj); obj = vector_next_iterator(vec, obj))
    {
        if (eq(obj, object))
            return vector_indexof_ptr(vec, obj);
    }
    return (-1);
}

canonical_original_code:
long vector_indexof(struct __vector_s *vec, void *object, _Bool (*eq)(void *, void *))
{
    void *buf = ((void *)0);
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 91, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 92, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((eq) ? 1 : 0), __extension__ ({ if (eq) ; else __assert_fail ("eq", "vector.c", 93, __extension__ __PRETTY_FUNCTION__); }));
    buf = vector_alloc_object(vec);
    if (!buf)
        return (-1);
    vector_current_size = vec -> element_size;
    for (void *obj = vector_begin_iterator(vec); vector_check_iterator(vec, obj); obj = vector_next_iterator(vec, obj))
    {
        if (eq(obj, object))
            return vector_indexof_ptr(vec, obj);
    }
    return (-1);
}

variable_types:
obj
void *

return_type: long


decompiled_code:
signed __int64 __fastcall vector_removefirst(unsigned __int64 *a1, __int64 a2, unsigned __int8 (__fastcall *a3)(__int64, __int64))
{
signed __int64 result; // rax

if ( !a1 )
_assert_fail("vec", "vector.c", 0xF8u, "vector_removefirst");
if ( !a2 )
_assert_fail("object", "vector.c", 0xF9u, "vector_removefirst");
if ( !a3 )
_assert_fail("eq", "vector.c", 0xFAu, "vector_removefirst");
vector_current_size = a1[2];
result = vector_indexof(a1, a2, a3);
if ( result >= 0 )
result = (signed __int64)vector_removeat(a1, result);
return result;
}

canonical_decompiled_code:
signed __int64 __fastcall func20(unsigned __int64 *a1, __int64 a2, unsigned __int8 (__fastcall *a3)(__int64, __int64))
{
signed __int64 result; // rax

if ( !a1 )
_assert_fail("vec", "vector.c", 0xF8u, "vector_removefirst");
if ( !a2 )
_assert_fail("object", "vector.c", 0xF9u, "vector_removefirst");
if ( !a3 )
_assert_fail("eq", "vector.c", 0xFAu, "vector_removefirst");
vector_current_size = a1[2];
result = func19(a1, a2, a3);
if ( result >= 0 )
result = (signed __int64)func8(a1, result);
return result;
}

original_code:
void vector_removefirst(vector_t vec, const void *object, equality_t eq)
{
    ssize_t idx = 0;
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 248, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 249, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((eq) ? 1 : 0), __extension__ ({ if (eq) ; else __assert_fail ("eq", "vector.c", 250, __extension__ __PRETTY_FUNCTION__); }));
    vector_current_size = vec -> element_size;
    idx = vector_indexof(vec, object, eq);
    if (idx >= 0)
        vector_removeat(vec, idx);
}

canonical_original_code:
void vector_removefirst(struct __vector_s *vec, void *object, _Bool (*eq)(void *, void *))
{
    long idx = 0;
    ((void) sizeof ((vec) ? 1 : 0), __extension__ ({ if (vec) ; else __assert_fail ("vec", "vector.c", 248, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((object) ? 1 : 0), __extension__ ({ if (object) ; else __assert_fail ("object", "vector.c", 249, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((eq) ? 1 : 0), __extension__ ({ if (eq) ; else __assert_fail ("eq", "vector.c", 250, __extension__ __PRETTY_FUNCTION__); }));
    vector_current_size = vec -> element_size;
    idx = vector_indexof(vec, object, eq);
    if (idx >= 0)
        vector_removeat(vec, idx);
}


die

decompiled_code:
void __fastcall __noreturn die(const char *a1)
{
fprintf(stderr, "# fatal error: %s\n", a1);
exit(2);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
fprintf(stderr, "# fatal error: %s\n", a1);
exit(2);
}

original_code:
static void
die(const char * msg)
{
  fprintf(stderr, "# fatal error: %s\n", msg);
  exit(2);
}

canonical_original_code:
static void die(char *msg)
{
  fprintf(stderr, "# fatal error: %s\n", msg);
  exit(2);
}

variable_types:
msg
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r13
char *v4; // r12
const char *v5; // r14
int v6; // eax
const char **v7; // rbx
void *v8; // r14
void *v9; // rbp
__int64 v10; // rsi
void *v11; // r12
void *v12; // r12
const char *v13; // r13
size_t v14; // rbx
char *v15; // rax
char *v16; // r12
char *v17; // rax
const char *v18; // rbx
FILE *v19; // r13
size_t v20; // rax
unsigned int v22; // [rsp+8h] [rbp-40h] BYREF
unsigned int v23; // [rsp+Ch] [rbp-3Ch] BYREF
unsigned int v24; // [rsp+10h] [rbp-38h] BYREF
unsigned int v25; // [rsp+14h] [rbp-34h] BYREF
unsigned int n[12]; // [rsp+18h] [rbp-30h] BYREF

v3 = 0LL;
v4 = 0LL;
v5 = *argv;
v22 = 0;
v23 = 0;
v24 = 0;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "v:m:p:h");
if ( v6 == -1 )
break;
if ( v6 == 112 )
{
v3 = optarg;
}
else if ( v6 > 112 )
{
if ( v6 != 118 )
goto LABEL_7;
if ( (int)_isoc99_sscanf(optarg, "%u.%u", &v22, &v23) <= 1 || v22 > 0xFFFF || v23 > 0xFFFF )
{
fwrite("# bad version number, setting to 0.0\n", 1uLL, 0x25uLL, stderr);
v23 = 0;
v22 = 0;
}
}
else if ( v6 == 109 )
{
v4 = optarg;
}
else
{
if ( v6 <= 109 && (v6 == 63 || v6 == 104) )
{
usage(v5);
exit(0);
}
LABEL_7:
fprintf(stderr, "# unknown option \"%c\"\n", (unsigned int)v6);
}
}
v7 = &argv[optind];
if ( argc - optind != 1 )
{
usage(v5);
exit(1);
}
v8 = readFile(*v7, &v25);
v9 = (void *)woffEncode(v8, v25, (unsigned __int16)v22, (unsigned __int16)v23, n, &v24);
free(v8);
v10 = v24;
if ( (_BYTE)v24 )
goto LABEL_41;
if ( v4 )
{
v11 = readFile(v4, &n[1]);
v9 = (void *)woffSetMetadata(v9, n, v11, n[1], &v24);
free(v11);
v10 = v24;
if ( (_BYTE)v24 )
goto LABEL_41;
}
if ( v3 )
{
v12 = readFile(v3, &n[1]);
v9 = (void *)woffSetPrivateData(v9, n, v12, n[1], &v24);
free(v12);
v10 = v24;
if ( (_BYTE)v24 )
LABEL_41:
reportErr(v10);
}
if ( (v10 & 0xFFFFFF00) != 0 )
woffPrintStatus(stderr, v10, "### ");
v13 = *v7;
v14 = strlen(*v7);
v15 = (char *)malloc(v14 + 8);
v16 = v15;
if ( !v15 )
die("malloc failure");
memcpy(v15, v13, v14 + 1);
v17 = strrchr(v16, 46);
v18 = v17;
if ( v17 && (!strcmp(v17, ".ttf") || !strcmp(v18, ".otf")) )
*v18 = 0;
strcat(v16, ".woff");
if ( !v9 )
die("unable to create WOFF data");
v19 = fopen(v16, "wb");
free(v16);
if ( !v19 )
die("unable to open output file");
v20 = fwrite(v9, 1uLL, n[0], v19);
if ( v20 != n[0] )
die("file write failure");
fclose(v19);
free(v9);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r13
char *v4; // r12
const char *v5; // r14
int v6; // eax
const char **v7; // rbx
void *v8; // r14
void *v9; // rbp
__int64 v10; // rsi
void *v11; // r12
void *v12; // r12
const char *v13; // r13
size_t v14; // rbx
char *v15; // rax
char *v16; // r12
char *v17; // rax
const char *v18; // rbx
FILE *v19; // r13
size_t v20; // rax
unsigned int v22; // [rsp+8h] [rbp-40h] BYREF
unsigned int v23; // [rsp+Ch] [rbp-3Ch] BYREF
unsigned int v24; // [rsp+10h] [rbp-38h] BYREF
unsigned int v25; // [rsp+14h] [rbp-34h] BYREF
unsigned int n[12]; // [rsp+18h] [rbp-30h] BYREF

v3 = 0LL;
v4 = 0LL;
v5 = *argv;
v22 = 0;
v23 = 0;
v24 = 0;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "v:m:p:h");
if ( v6 == -1 )
break;
if ( v6 == 112 )
{
v3 = optarg;
}
else if ( v6 > 112 )
{
if ( v6 != 118 )
goto LABEL_7;
if ( (int)_isoc99_sscanf(optarg, "%u.%u", &v22, &v23) <= 1 || v22 > 0xFFFF || v23 > 0xFFFF )
{
fwrite("# bad version number, setting to 0.0\n", 1uLL, 0x25uLL, stderr);
v23 = 0;
v22 = 0;
}
}
else if ( v6 == 109 )
{
v4 = optarg;
}
else
{
if ( v6 <= 109 && (v6 == 63 || v6 == 104) )
{
func0(v5);
exit(0);
}
LABEL_7:
fprintf(stderr, "# unknown option \"%c\"\n", (unsigned int)v6);
}
}
v7 = &argv[optind];
if ( argc - optind != 1 )
{
func0(v5);
exit(1);
}
v8 = func3(*v7, &v25);
v9 = (void *)woffEncode(v8, v25, (unsigned __int16)v22, (unsigned __int16)v23, n, &v24);
free(v8);
v10 = v24;
if ( (_BYTE)v24 )
goto LABEL_41;
if ( v4 )
{
v11 = func3(v4, &n[1]);
v9 = (void *)woffSetMetadata(v9, n, v11, n[1], &v24);
free(v11);
v10 = v24;
if ( (_BYTE)v24 )
goto LABEL_41;
}
if ( v3 )
{
v12 = func3(v3, &n[1]);
v9 = (void *)woffSetPrivateData(v9, n, v12, n[1], &v24);
free(v12);
v10 = v24;
if ( (_BYTE)v24 )
LABEL_41:
func2(v10);
}
if ( (v10 & 0xFFFFFF00) != 0 )
woffPrintStatus(stderr, v10, "### ");
v13 = *v7;
v14 = strlen(*v7);
v15 = (char *)malloc(v14 + 8);
v16 = v15;
if ( !v15 )
func1("malloc failure");
memcpy(v15, v13, v14 + 1);
v17 = strrchr(v16, 46);
v18 = v17;
if ( v17 && (!strcmp(v17, ".ttf") || !strcmp(v18, ".otf")) )
*v18 = 0;
strcat(v16, ".woff");
if ( !v9 )
func1("unable to create WOFF data");
v19 = fopen(v16, "wb");
free(v16);
if ( !v19 )
func1("unable to open output file");
v20 = fwrite(v9, 1uLL, n[0], v19);
if ( v20 != n[0] )
func1("file write failure");
fclose(v19);
free(v9);
return 0;
}

original_code:
int
main(int argc, char *argv[])
{
  const char * progName = argv[0];
  uint32_t status = eWOFF_ok;
  int opt;
  int option = 0;
  while ((opt = getopt(argc, argv, "vmph")) != -1) {
    switch (opt) {
    case 'v':
      if (option)
        fprintf(stderr, "# ignoring option '%c', already got '%c'\n", opt, option);
      else
        option = 'v';
      break;
    case 'm':
      if (option)
        fprintf(stderr, "# ignoring option '%c', already got '%c'\n", opt, option);
      else
        option = 'm';
      break;
    case 'p':
      if (option)
        fprintf(stderr, "# ignoring option '%c', already got '%c'\n", opt, option);
      else
        option = 'p';
      break;
    case 'h':
    case '?':
      usage(progName);
      exit(0);
    default:
      fprintf(stderr, "# unknown option '%c'\n", opt);
      break;
    }
  }
  argc -= optind;
  argv += optind;
  if (argc != 1) {
    usage(progName);
    exit(1);
  }
  uint32_t woffLen;
  const uint8_t * woffData = readFile(argv[0], &woffLen);
  uint32_t len;
  const uint8_t * data;
  switch (option) {
  case 'v':
    {
      uint16_t maj, min;
      woffGetFontVersion(woffData, woffLen, &maj, &min, &status);
      if ((!(((uint32_t)(status) & 0xff) == eWOFF_ok))) {
        reportErr(status);
      }
      printf("%u.%u\n", maj, min);
    }
    break;
  case 'm':
    {
      data = woffGetMetadata(woffData, woffLen, &len, &status);
      if ((!(((uint32_t)(status) & 0xff) == eWOFF_ok))) {
        reportErr(status);
      }
      if (data) {
        if (fwrite(data, 1, len, stdout) != len)
          die("error writing metadata to output");
        free((void *) data);
      } else {
        printf("<!-- No WOFF metadata available. -->\n");
      }
    }
    break;
  case 'p':
    {
      data = woffGetPrivateData(woffData, woffLen, &len, &status);
      if ((!(((uint32_t)(status) & 0xff) == eWOFF_ok))) {
        reportErr(status);
      }
      if (data) {
        if (fwrite(data, 1, len, stdout) != len)
          die("error writing private data to output");
        free((void *) data);
      }
    }
    break;
  default:
    {
      data = woffDecode(woffData, woffLen, &len, &status);
      if ((!(((uint32_t)(status) & 0xff) == eWOFF_ok))) {
        reportErr(status);
      }
      if (data) {
        if (fwrite(data, 1, len, stdout) != len)
          die("error writing sfnt data to output");
        free((void *) data);
      } else {
        die("unable to decode WOFF data");
      }
    }
    break;
  }
  if (((uint32_t)(status) & ~0xff)) {
    woffPrintStatus(stderr, status, "### ");
  }
  free((void *) woffData);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  char *progName = argv[0];
  unsigned int status = eWOFF_ok;
  int opt;
  int option = 0;
  while ((opt = getopt(argc, argv, "vmph")) != -1) {
    switch (opt) {
    case 'v':
      if (option)
        fprintf(stderr, "# ignoring option '%c', already got '%c'\n", opt, option);
      else
        option = 'v';
      break;
    case 'm':
      if (option)
        fprintf(stderr, "# ignoring option '%c', already got '%c'\n", opt, option);
      else
        option = 'm';
      break;
    case 'p':
      if (option)
        fprintf(stderr, "# ignoring option '%c', already got '%c'\n", opt, option);
      else
        option = 'p';
      break;
    case 'h':
    case '?':
      usage(progName);
      exit(0);
    default:
      fprintf(stderr, "# unknown option '%c'\n", opt);
      break;
    }
  }
  argc -= optind;
  argv += optind;
  if (argc != 1) {
    usage(progName);
    exit(1);
  }
  unsigned int woffLen;
  unsigned char *woffData = readFile(argv[0], &woffLen);
  unsigned int len;
  unsigned char *data;
  switch (option) {
  case 'v':
    {
      unsigned short maj; unsigned short min;
      woffGetFontVersion(woffData, woffLen, &maj, &min, &status);
      if ((!(((unsigned int)(status) & 0xff) == eWOFF_ok))) {
        reportErr(status);
      }
      printf("%u.%u\n", maj, min);
    }
    break;
  case 'm':
    {
      data = woffGetMetadata(woffData, woffLen, &len, &status);
      if ((!(((unsigned int)(status) & 0xff) == eWOFF_ok))) {
        reportErr(status);
      }
      if (data) {
        if (fwrite(data, 1, len, stdout) != len)
          die("error writing metadata to output");
        free((void *) data);
      } else {
        printf("<!-- No WOFF metadata available. -->\n");
      }
    }
    break;
  case 'p':
    {
      data = woffGetPrivateData(woffData, woffLen, &len, &status);
      if ((!(((unsigned int)(status) & 0xff) == eWOFF_ok))) {
        reportErr(status);
      }
      if (data) {
        if (fwrite(data, 1, len, stdout) != len)
          die("error writing private data to output");
        free((void *) data);
      }
    }
    break;
  default:
    {
      data = woffDecode(woffData, woffLen, &len, &status);
      if ((!(((unsigned int)(status) & 0xff) == eWOFF_ok))) {
        reportErr(status);
      }
      if (data) {
        if (fwrite(data, 1, len, stdout) != len)
          die("error writing sfnt data to output");
        free((void *) data);
      } else {
        die("unable to decode WOFF data");
      }
    }
    break;
  }
  if (((unsigned int)(status) & ~0xff)) {
    woffPrintStatus(stderr, status, "### ");
  }
  free((void *) woffData);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
progName
char *

variable_types:
status
unsigned int

variable_types: opt int

variable_types: option int

variable_types:
woffLen
unsigned int

variable_types:
woffData
unsigned char *

variable_types:
len
unsigned int

variable_types:
data
unsigned char *

variable_types:
maj
unsigned short

variable_types:
min
unsigned short


usage

repo: xenserver/xentpm

decompiled_code:
int usage()
{
puts("usage: xentpm");
puts(" --is_tpm_owned");
puts(" --take_ownership");
puts(" --get_ekey");
puts(" --get_ekcert");
puts(" --gen_aik <xencert_base64>");
puts(" --get_aik_pem ");
puts(" --get_aik_tcpa ");
puts(" --tpm_challenge <challenge>");
return puts(" --tpm_quote <nonce> ");
}

canonical_decompiled_code:
int func0()
{
puts("usage: xentpm");
puts(" --is_tpm_owned");
puts(" --take_ownership");
puts(" --get_ekey");
puts(" --get_ekcert");
puts(" --gen_aik <xencert_base64>");
puts(" --get_aik_pem ");
puts(" --get_aik_tcpa ");
puts(" --tpm_challenge <challenge>");
return puts(" --tpm_quote <nonce> ");
}

original_code:
void usage()
{
    printf("usage: xentpm\n");
    printf("           --is_tpm_owned\n");
    printf("           --take_ownership\n");
    printf("           --get_ekey\n");
    printf("           --get_ekcert\n");
    printf("           --gen_aik <xencert_base64>\n");
    printf("           --get_aik_pem \n");
    printf("           --get_aik_tcpa \n");
    printf("           --tpm_challenge <challenge>\n");
    printf("           --tpm_quote <nonce> \n");
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
int v4; // eax
__int64 v6; // rdx
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = 3;
longind[0] = 0;
openlog("xentpm", 1, 8);
if ( argc <= 1 )
{
usage();
closelog();
LABEL_27:
v6 = v3;
v3 = 1;
fprintf(stderr, "%d", v6);
return v3;
}
while ( 2 )
{
v4 = getopt_long(argc, (char *const *)argv, "otek:a:p:b:c:q:", &long_options_0, longind);
if ( v4 == -1 )
goto LABEL_24;
switch ( v4 )
{
case 'a':
if ( argc != 3 )
goto LABEL_23;
v3 = generate_aik(optarg);
continue;
case 'b':
if ( argc != 2 )
goto LABEL_23;
v3 = get_aik_tcpa();
continue;
case 'c':
if ( argc != 3 )
goto LABEL_23;
v3 = tpm_challenge(optarg);
continue;
case 'e':
if ( argc != 2 )
goto LABEL_23;
v3 = get_endorsment_key();
continue;
case 'k':
if ( argc != 2 )
goto LABEL_23;
v3 = get_endorsment_keycert();
continue;
case 'o':
if ( argc != 2 )
goto LABEL_23;
v3 = tpm_owned();
continue;
case 'p':
if ( argc != 2 )
goto LABEL_23;
v3 = get_aik_pem();
continue;
case 'q':
if ( argc != 3 )
goto LABEL_23;
v3 = tpm_quote(optarg);
continue;
case 't':
if ( argc == 2 )
{
v3 = take_ownership();
continue;
}
LABEL_23:
usage();
LABEL_24:
closelog();
if ( v3 )
goto LABEL_27;
return v3;
default:
usage();
continue;
}
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
int v4; // eax
__int64 v6; // rdx
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = 3;
longind[0] = 0;
openlog("xentpm", 1, 8);
if ( argc <= 1 )
{
func0();
closelog();
LABEL_27:
v6 = v3;
v3 = 1;
fprintf(stderr, "%d", v6);
return v3;
}
while ( 2 )
{
v4 = getopt_long(argc, (char *const *)argv, "otek:a:p:b:c:q:", &long_options_0, longind);
if ( v4 == -1 )
goto LABEL_24;
switch ( v4 )
{
case 'a':
if ( argc != 3 )
goto LABEL_23;
v3 = generate_aik(optarg);
continue;
case 'b':
if ( argc != 2 )
goto LABEL_23;
v3 = get_aik_tcpa();
continue;
case 'c':
if ( argc != 3 )
goto LABEL_23;
v3 = tpm_challenge(optarg);
continue;
case 'e':
if ( argc != 2 )
goto LABEL_23;
v3 = get_endorsment_key();
continue;
case 'k':
if ( argc != 2 )
goto LABEL_23;
v3 = get_endorsment_keycert();
continue;
case 'o':
if ( argc != 2 )
goto LABEL_23;
v3 = tpm_owned();
continue;
case 'p':
if ( argc != 2 )
goto LABEL_23;
v3 = get_aik_pem();
continue;
case 'q':
if ( argc != 3 )
goto LABEL_23;
v3 = tpm_quote(optarg);
continue;
case 't':
if ( argc == 2 )
{
v3 = take_ownership();
continue;
}
LABEL_23:
func0();
LABEL_24:
closelog();
if ( v3 )
goto LABEL_27;
return v3;
default:
func0();
continue;
}
}
}

original_code:
int main(int argc, char *argv[])
{
    int opt = 0;
    int status = (UINT32)(0x00000000L + 0x003L);
    int long_index = 0;
    openlog("xentpm", 0x01, (1<<3));
    static struct option long_options[] = {
        {"is_tpm_owned", 0, 0, 'o' },
        {"take_ownership", 0, 0, 't' },
        {"get_ekey", 0, 0, 'e' },
        {"get_ekcert", 0, 0, 'k' },
        {"gen_aik", 1, 0, 'a' },
        {"get_aik_pem", 0, 0, 'p' },
        {"get_aik_tcpa", 0, 0, 'b' },
        {"tpm_challenge", 1, 0, 'c' },
        {"tpm_quote", 1, 0, 'q' },
        {0, 0, 0, 0 }
    };
    if( argc < 2 ){
        usage();
        goto clean;
    }
    while ((opt = getopt_long(argc, argv,"otek:a:p:b:c:q:",
                    long_options, &long_index)) != -1) {
        switch (opt) {
            case 'o' :
                if (argc != 2) {
                    usage();
                    goto clean;
                }
                status = tpm_owned();
                break;
            case 't' :
                if (argc != 2) {
                    usage();
                    goto clean;
                }
                status = take_ownership();
                break;
            case 'e' :
                if (argc != 2) {
                    usage();
                    goto clean;
                }
                status = get_endorsment_key();
                break;
            case 'k' :
                if (argc != 2) {
                    usage();
                    goto clean;
                }
                status = get_endorsment_keycert();
                break;
            case 'a' :
                if (argc != 3) {
                    usage();
                    goto clean;
                }
                status = generate_aik(optarg);
                break;
            case 'p' :
                if (argc != 2) {
                    usage();
                    goto clean;
                }
                status = get_aik_pem();
                break;
            case 'b' :
                if (argc != 2) {
                    usage();
                    goto clean;
                }
                status = get_aik_tcpa();
                break;
            case 'c' :
                if (argc != 3) {
                    usage();
                    goto clean;
                }
                status = tpm_challenge(optarg);
                break;
            case 'q' :
                if (argc != 3) {
                    usage();
                    goto clean;
                }
                status = tpm_quote(optarg);
                break;
            default:
                usage();
                break;
        }
    }
clean:
    closelog();
    if (!status) {
        return 0;
    } else {
        fprintf(stderr, "%d", status);
        return 1;
    }
}

variable_types: status int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []


decompiled_code:
__int64 take_ownership()
{
__int64 result; // rax
unsigned int v1; // er12
unsigned int v2; // eax
unsigned int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // ebx
unsigned int v6; // eax
unsigned int v7; // ebx
unsigned int v8; // eax
unsigned int v9; // eax
unsigned int v10; // ebx
const char *v11; // rax
const char *v12; // rax
const char *v13; // rax
const char *v14; // rax
const char *v15; // rax
unsigned int v16; // [rsp+Ch] [rbp-4Ch] BYREF
unsigned int v17; // [rsp+10h] [rbp-48h] BYREF
unsigned int v18; // [rsp+14h] [rbp-44h] BYREF
unsigned int v19; // [rsp+18h] [rbp-40h] BYREF
unsigned int v20; // [rsp+1Ch] [rbp-3Ch] BYREF
char v21[56]; // [rsp+20h] [rbp-38h] BYREF

if ( (unsigned int)tpm_owned() )
return 0LL;
syslog(6, "Taking ownership of the TPM.\n");
v1 = read_tpm_key(v21, 20LL);
if ( v1 )
{
syslog(3, "TPM Key Not Found \n");
result = v1;
}
else
{
v2 = tpm_init_context(&v16, &v17, &v18);
v3 = v2;
if ( v2 )
{
v1 = v2;
v12 = (const char *)Trspi_Error_String(v2);
syslog(3, "Error 0x%x tpm_init_context %s \n", v3, v12);
}
else
{
v4 = Tspi_Context_CreateObject(v16, 2LL, 67108865LL, &v19);
v5 = v4;
if ( v4 )
{
v1 = v4;
v11 = (const char *)Trspi_Error_String(v4);
syslog(3, "Error 0x%x on Tspi_Context_CreateObject %s \n", v5, v11);
}
else
{
v6 = Tspi_GetPolicyObject(v19, 1LL, &v20);
v7 = v6;
if ( v6 )
{
v1 = v6;
v14 = (const char *)Trspi_Error_String(v6);
syslog(3, "Error 0x%x on Tspi_GetPolicyObject %s \n", v7, v14);
}
else
{
v8 = Tspi_Policy_SetSecret(v20, 4096LL, 20LL, v21);
if ( v8 )
{
v1 = v8;
v15 = (const char *)Trspi_Error_String(v8);
syslog(3, "Error Setting SRK Password %s \n", v15);
}
else
{
v9 = Tspi_TPM_TakeOwnership(v17, v19, 0LL);
v10 = v9;
if ( v9 )
{
v1 = v9;
v13 = (const char *)Trspi_Error_String(v9);
syslog(3, "Error 0x%x on Tspi_TPM_TakeOwnership (%s)\n", v10, v13);
}
else
{
syslog(6, "XenServer now owns the TPM.\n");
unregister_aik_uuid(v16);
}
}
}
}
}
tpm_free_context(v16, v18);
result = v1;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
unsigned int v1; // er12
unsigned int v2; // eax
unsigned int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // ebx
unsigned int v6; // eax
unsigned int v7; // ebx
unsigned int v8; // eax
unsigned int v9; // eax
unsigned int v10; // ebx
const char *v11; // rax
const char *v12; // rax
const char *v13; // rax
const char *v14; // rax
const char *v15; // rax
unsigned int v16; // [rsp+Ch] [rbp-4Ch] BYREF
unsigned int v17; // [rsp+10h] [rbp-48h] BYREF
unsigned int v18; // [rsp+14h] [rbp-44h] BYREF
unsigned int v19; // [rsp+18h] [rbp-40h] BYREF
unsigned int v20; // [rsp+1Ch] [rbp-3Ch] BYREF
char v21[56]; // [rsp+20h] [rbp-38h] BYREF

if ( (unsigned int)func0() )
return 0LL;
syslog(6, "Taking ownership of the TPM.\n");
v1 = read_tpm_key(v21, 20LL);
if ( v1 )
{
syslog(3, "TPM Key Not Found \n");
result = v1;
}
else
{
v2 = tpm_init_context(&v16, &v17, &v18);
v3 = v2;
if ( v2 )
{
v1 = v2;
v12 = (const char *)Trspi_Error_String(v2);
syslog(3, "Error 0x%x tpm_init_context %s \n", v3, v12);
}
else
{
v4 = Tspi_Context_CreateObject(v16, 2LL, 67108865LL, &v19);
v5 = v4;
if ( v4 )
{
v1 = v4;
v11 = (const char *)Trspi_Error_String(v4);
syslog(3, "Error 0x%x on Tspi_Context_CreateObject %s \n", v5, v11);
}
else
{
v6 = Tspi_GetPolicyObject(v19, 1LL, &v20);
v7 = v6;
if ( v6 )
{
v1 = v6;
v14 = (const char *)Trspi_Error_String(v6);
syslog(3, "Error 0x%x on Tspi_GetPolicyObject %s \n", v7, v14);
}
else
{
v8 = Tspi_Policy_SetSecret(v20, 4096LL, 20LL, v21);
if ( v8 )
{
v1 = v8;
v15 = (const char *)Trspi_Error_String(v8);
syslog(3, "Error Setting SRK Password %s \n", v15);
}
else
{
v9 = Tspi_TPM_TakeOwnership(v17, v19, 0LL);
v10 = v9;
if ( v9 )
{
v1 = v9;
v13 = (const char *)Trspi_Error_String(v9);
syslog(3, "Error 0x%x on Tspi_TPM_TakeOwnership (%s)\n", v10, v13);
}
else
{
syslog(6, "XenServer now owns the TPM.\n");
unregister_aik_uuid(v16);
}
}
}
}
}
tpm_free_context(v16, v18);
result = v1;
}
return result;
}

original_code:
int take_ownership()
{
    TSS_HCONTEXT context;
    TSS_HTPM tpm_handle;
    TSS_RESULT result;
    TSS_HPOLICY tpm_policy;
    TSS_HKEY srk_handle;
    TSS_HPOLICY srk_policy;
    TSS_FLAG srk_attributes;
    BYTE tpm_key[20];
    if (tpm_owned()) {
        return (UINT32)(0x00000000L);
    }
    syslog(6, "Taking ownership of the TPM.\n");
    if ((result = read_tpm_key(tpm_key, 20)) != 0) {
        syslog(3, "TPM Key Not Found \n");
        goto out;
    }
    result = tpm_init_context(& context, &tpm_handle, &tpm_policy);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error 0x%x tpm_init_context %s \n",
                result,Trspi_Error_String(result));
        goto free_context;
    }
    srk_attributes = (0x04000000) | (0x00000001);
    result = Tspi_Context_CreateObject(context, (0x02),
  srk_attributes, &srk_handle);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error 0x%x on Tspi_Context_CreateObject %s \n",
                result,Trspi_Error_String(result));
        goto free_context;
    }
    result = Tspi_GetPolicyObject(srk_handle, (0x00000001), &srk_policy);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error 0x%x on Tspi_GetPolicyObject %s \n",
                result, Trspi_Error_String(result));
        goto free_context;
    }
    result = Tspi_Policy_SetSecret(srk_policy, (0x00001000),
            (UINT32)(sizeof(tpm_key)), (BYTE*)tpm_key);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error Setting SRK Password %s \n",
  Trspi_Error_String(result));
        goto free_context;
    }
    result = Tspi_TPM_TakeOwnership(tpm_handle, srk_handle, 0);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error 0x%x on Tspi_TPM_TakeOwnership (%s)\n",
  result, Trspi_Error_String(result));
        goto free_context;
    }
    syslog(6, "XenServer now owns the TPM.\n");
    unregister_aik_uuid(context);
free_context:
    tpm_free_context(context, tpm_policy);
out:
    return result;
}

canonical_original_code:
int take_ownership()
{
    unsigned int context;
    unsigned int tpm_handle;
    unsigned int result;
    unsigned int tpm_policy;
    unsigned int srk_handle;
    unsigned int srk_policy;
    unsigned int srk_attributes;
    unsigned char tpm_key[20];
    if (tpm_owned()) {
        return (UINT32)(0x00000000L);
    }
    syslog(6, "Taking ownership of the TPM.\n");
    if ((result = read_tpm_key(tpm_key, 20)) != 0) {
        syslog(3, "TPM Key Not Found \n");
        goto out;
    }
    result = tpm_init_context(& context, &tpm_handle, &tpm_policy);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error 0x%x tpm_init_context %s \n",
                result,Trspi_Error_String(result));
        goto free_context;
    }
    srk_attributes = (0x04000000) | (0x00000001);
    result = Tspi_Context_CreateObject(context, (0x02),
  srk_attributes, &srk_handle);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error 0x%x on Tspi_Context_CreateObject %s \n",
                result,Trspi_Error_String(result));
        goto free_context;
    }
    result = Tspi_GetPolicyObject(srk_handle, (0x00000001), &srk_policy);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error 0x%x on Tspi_GetPolicyObject %s \n",
                result, Trspi_Error_String(result));
        goto free_context;
    }
    result = Tspi_Policy_SetSecret(srk_policy, (0x00001000),
            (UINT32)(sizeof(tpm_key)), (unsigned char *)tpm_key);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error Setting SRK Password %s \n",
  Trspi_Error_String(result));
        goto free_context;
    }
    result = Tspi_TPM_TakeOwnership(tpm_handle, srk_handle, 0);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error 0x%x on Tspi_TPM_TakeOwnership (%s)\n",
  result, Trspi_Error_String(result));
        goto free_context;
    }
    syslog(6, "XenServer now owns the TPM.\n");
    unregister_aik_uuid(context);
free_context:
    tpm_free_context(context, tpm_policy);
out:
    return result;
}

variable_types:
context
unsigned int

variable_types:
result
unsigned int


decompiled_code:
__int64 __fastcall get_nonce_sha1(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rax
void *v5; // rbp
__int64 result; // rax
unsigned int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v4 = base64_decode(a1, v7);
if ( v4 )
{
v5 = (void *)v4;
sha1(a3, v4, v7[0], a2);
free(v5);
result = 0LL;
}
else
{
syslog(3, "Unable to b64 decode nonce \n");
result = 3LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rax
void *v5; // rbp
__int64 result; // rax
unsigned int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v4 = base64_decode(a1, v7);
if ( v4 )
{
v5 = (void *)v4;
sha1(a3, v4, v7[0], a2);
free(v5);
result = 0LL;
}
else
{
syslog(3, "Unable to b64 decode nonce \n");
result = 3LL;
}
return result;
}

original_code:
static int
get_nonce_sha1(char* b64_nonce, BYTE * nonce_hash, TSS_HCONTEXT tpm_context)
{
    int nonce_len ;
    BYTE* nonce_buf = ((void *)0);
    nonce_buf = base64_decode(b64_nonce, &nonce_len);
    if (!nonce_buf) {
        syslog(3, "Unable to b64 decode nonce \n");
        return (UINT32)(0x00000000L + 0x003L);
    }
    sha1(tpm_context, nonce_buf, nonce_len, nonce_hash);
    free(nonce_buf);
    return (UINT32)(0x00000000L);
}

canonical_original_code:
static int get_nonce_sha1(char *b64_nonce, unsigned char *nonce_hash, unsigned int tpm_context)
{
    int nonce_len;
    unsigned char *nonce_buf = ((void *)0);
    nonce_buf = base64_decode(b64_nonce, &nonce_len);
    if (!nonce_buf) {
        syslog(3, "Unable to b64 decode nonce \n");
        return (UINT32)(0x00000000L + 0x003L);
    }
    sha1(tpm_context, nonce_buf, nonce_len, nonce_hash);
    free(nonce_buf);
    return (UINT32)(0x00000000L);
}