decompiled_code:
unsigned __int64 __fastcall poly1305_process(unsigned int *a1, __int64 a2, __int64 a3)
{
unsigned int v3; // er12
unsigned int v4; // ebp
unsigned int v5; // er13
__int64 v6; // r14
unsigned int v7; // ebx
__int64 v8; // rax
unsigned int v9; // esi
unsigned __int64 v10; // r15
unsigned __int64 v11; // rbp
unsigned __int64 v12; // rbx
unsigned int v13; // er11
unsigned int v14; // er8
unsigned int v15; // edx
unsigned int v16; // er11
unsigned int v17; // er10
unsigned int v18; // edx
unsigned int v19; // er11
unsigned int v20; // edx
unsigned __int64 v21; // rax
__int64 v22; // r10
__int64 v23; // r10
unsigned __int64 v24; // rcx
__int64 v25; // r9
unsigned __int64 v26; // rax
__int64 v27; // r8
unsigned __int64 v28; // rdi
__int64 v29; // r8
__int64 v30; // rdi
int v31; // edx
unsigned int v32; // edx
unsigned __int64 v33; // rcx
unsigned __int64 v34; // r10
unsigned __int64 v35; // r9
unsigned __int64 result; // rax
unsigned __int64 v37; // rdx
unsigned int v38; // [rsp+0h] [rbp-78h]
unsigned int v39; // [rsp+4h] [rbp-74h]
unsigned int v40; // [rsp+8h] [rbp-70h]
unsigned int v41; // [rsp+Ch] [rbp-6Ch]
unsigned int v42; // [rsp+10h] [rbp-68h]
unsigned int v43; // [rsp+20h] [rbp-58h]
__int64 v44; // [rsp+28h] [rbp-50h]
unsigned int v45; // [rsp+30h] [rbp-48h]

v3 = *a1;
v4 = a1[10];
v38 = a1[1];
v40 = a1[2];
v41 = v38 + (v38 >> 2);
v45 = a1[3];
v39 = v40 + (v40 >> 2);
v5 = (v45 >> 2) + v45;
v6 = a3;
v42 = a1[9];
v7 = a1[11];
v8 = a3 + 16 * a2;
v9 = a1[8];
v44 = v8;
do
{
v43 = mbedtls_get_unaligned_uint32(v6);
v10 = v42
+ (unsigned __int64)(unsigned int)mbedtls_get_unaligned_uint32(v6 + 4)
+ ((v9 + (unsigned __int64)v43) >> 32);
v11 = HIDWORD(v10) + (unsigned int)mbedtls_get_unaligned_uint32(v6 + 8) + (unsigned __int64)v4;
v12 = HIDWORD(v11) + (unsigned int)mbedtls_get_unaligned_uint32(v6 + 12) + (unsigned __int64)v7;
mul64(v9 + v43, v3);
mul64(v10, v5);
mul64(v11, v39);
mul64(v12, v41);
mul64(v13, v38);
mul64(v10, v3);
mul64(v11, v5);
mul64(v12, v14);
mul64(v15, v41);
mul64(v16, v40);
mul64(v10, v17);
mul64(v11, v3);
mul64(v12, v5);
mul64(v18, v39);
mul64(v19, v45);
mul64(v10, v40);
mul64(v11, v38);
mul64(v12, v3);
v21 = mul64(v20, v5);
v23 = v21 + v22;
v26 = HIDWORD(v24) + v25;
v28 = HIDWORD(v26) + v27;
v29 = (unsigned int)v28;
v30 = v23 + HIDWORD(v28);
v32 = HIDWORD(v30) + v31;
v33 = (unsigned int)v24 + (v32 & 0xFFFFFFFC) + (unsigned __int64)(v32 >> 2);
v6 += 16LL;
v9 = v33;
v34 = (unsigned int)v26 + HIDWORD(v33);
v42 = v26 + HIDWORD(v33);
v35 = v29 + HIDWORD(v34);
v4 = v29 + HIDWORD(v34);
result = HIDWORD(v35) + (unsigned int)v30;
v7 = HIDWORD(v35) + v30;
v37 = HIDWORD(result) + (v32 & 3);
}
while ( v44 != v6 );
a1[8] = v33;
a1[9] = v34;
a1[10] = v35;
a1[11] = result;
a1[12] = v37;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(unsigned int *a1, __int64 a2, __int64 a3)
{
unsigned int v3; // er12
unsigned int v4; // ebp
unsigned int v5; // er13
__int64 v6; // r14
unsigned int v7; // ebx
__int64 v8; // rax
unsigned int v9; // esi
unsigned __int64 v10; // r15
unsigned __int64 v11; // rbp
unsigned __int64 v12; // rbx
unsigned int v13; // er11
unsigned int v14; // er8
unsigned int v15; // edx
unsigned int v16; // er11
unsigned int v17; // er10
unsigned int v18; // edx
unsigned int v19; // er11
unsigned int v20; // edx
unsigned __int64 v21; // rax
__int64 v22; // r10
__int64 v23; // r10
unsigned __int64 v24; // rcx
__int64 v25; // r9
unsigned __int64 v26; // rax
__int64 v27; // r8
unsigned __int64 v28; // rdi
__int64 v29; // r8
__int64 v30; // rdi
int v31; // edx
unsigned int v32; // edx
unsigned __int64 v33; // rcx
unsigned __int64 v34; // r10
unsigned __int64 v35; // r9
unsigned __int64 result; // rax
unsigned __int64 v37; // rdx
unsigned int v38; // [rsp+0h] [rbp-78h]
unsigned int v39; // [rsp+4h] [rbp-74h]
unsigned int v40; // [rsp+8h] [rbp-70h]
unsigned int v41; // [rsp+Ch] [rbp-6Ch]
unsigned int v42; // [rsp+10h] [rbp-68h]
unsigned int v43; // [rsp+20h] [rbp-58h]
__int64 v44; // [rsp+28h] [rbp-50h]
unsigned int v45; // [rsp+30h] [rbp-48h]

v3 = *a1;
v4 = a1[10];
v38 = a1[1];
v40 = a1[2];
v41 = v38 + (v38 >> 2);
v45 = a1[3];
v39 = v40 + (v40 >> 2);
v5 = (v45 >> 2) + v45;
v6 = a3;
v42 = a1[9];
v7 = a1[11];
v8 = a3 + 16 * a2;
v9 = a1[8];
v44 = v8;
do
{
v43 = mbedtls_get_unaligned_uint32(v6);
v10 = v42
+ (unsigned __int64)(unsigned int)mbedtls_get_unaligned_uint32(v6 + 4)
+ ((v9 + (unsigned __int64)v43) >> 32);
v11 = HIDWORD(v10) + (unsigned int)mbedtls_get_unaligned_uint32(v6 + 8) + (unsigned __int64)v4;
v12 = HIDWORD(v11) + (unsigned int)mbedtls_get_unaligned_uint32(v6 + 12) + (unsigned __int64)v7;
func0(v9 + v43, v3);
func0(v10, v5);
func0(v11, v39);
func0(v12, v41);
func0(v13, v38);
func0(v10, v3);
func0(v11, v5);
func0(v12, v14);
func0(v15, v41);
func0(v16, v40);
func0(v10, v17);
func0(v11, v3);
func0(v12, v5);
func0(v18, v39);
func0(v19, v45);
func0(v10, v40);
func0(v11, v38);
func0(v12, v3);
v21 = func0(v20, v5);
v23 = v21 + v22;
v26 = HIDWORD(v24) + v25;
v28 = HIDWORD(v26) + v27;
v29 = (unsigned int)v28;
v30 = v23 + HIDWORD(v28);
v32 = HIDWORD(v30) + v31;
v33 = (unsigned int)v24 + (v32 & 0xFFFFFFFC) + (unsigned __int64)(v32 >> 2);
v6 += 16LL;
v9 = v33;
v34 = (unsigned int)v26 + HIDWORD(v33);
v42 = v26 + HIDWORD(v33);
v35 = v29 + HIDWORD(v34);
v4 = v29 + HIDWORD(v34);
result = HIDWORD(v35) + (unsigned int)v30;
v7 = HIDWORD(v35) + v30;
v37 = HIDWORD(result) + (v32 & 3);
}
while ( v44 != v6 );
a1[8] = v33;
a1[9] = v34;
a1[10] = v35;
a1[11] = result;
a1[12] = v37;
return result;
}

original_code:
static void poly1305_process(mbedtls_poly1305_context *ctx,
                             size_t nblocks,
                             const unsigned char *input,
                             uint32_t needs_padding)
{
    uint64_t d0, d1, d2, d3;
    uint32_t acc0, acc1, acc2, acc3, acc4;
    uint32_t r0, r1, r2, r3;
    uint32_t rs1, rs2, rs3;
    size_t offset = 0U;
    size_t i;
    r0 = ctx->r[0];
    r1 = ctx->r[1];
    r2 = ctx->r[2];
    r3 = ctx->r[3];
    rs1 = r1 + (r1 >> 2U);
    rs2 = r2 + (r2 >> 2U);
    rs3 = r3 + (r3 >> 2U);
    acc0 = ctx->acc[0];
    acc1 = ctx->acc[1];
    acc2 = ctx->acc[2];
    acc3 = ctx->acc[3];
    acc4 = ctx->acc[4];
    for (i = 0U; i < nblocks; i++) {
        d0 = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((input) + (offset + 0))) : mbedtls_get_unaligned_uint32((input) + (offset + 0)) );
        d1 = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((input) + (offset + 4))) : mbedtls_get_unaligned_uint32((input) + (offset + 4)) );
        d2 = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((input) + (offset + 8))) : mbedtls_get_unaligned_uint32((input) + (offset + 8)) );
        d3 = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((input) + (offset + 12))) : mbedtls_get_unaligned_uint32((input) + (offset + 12)) );
        d0 += (uint64_t) acc0;
        d1 += (uint64_t) acc1 + (d0 >> 32U);
        d2 += (uint64_t) acc2 + (d1 >> 32U);
        d3 += (uint64_t) acc3 + (d2 >> 32U);
        acc0 = (uint32_t) d0;
        acc1 = (uint32_t) d1;
        acc2 = (uint32_t) d2;
        acc3 = (uint32_t) d3;
        acc4 += (uint32_t) (d3 >> 32U) + needs_padding;
        d0 = mul64(acc0, r0) +
             mul64(acc1, rs3) +
             mul64(acc2, rs2) +
             mul64(acc3, rs1);
        d1 = mul64(acc0, r1) +
             mul64(acc1, r0) +
             mul64(acc2, rs3) +
             mul64(acc3, rs2) +
             mul64(acc4, rs1);
        d2 = mul64(acc0, r2) +
             mul64(acc1, r1) +
             mul64(acc2, r0) +
             mul64(acc3, rs3) +
             mul64(acc4, rs2);
        d3 = mul64(acc0, r3) +
             mul64(acc1, r2) +
             mul64(acc2, r1) +
             mul64(acc3, r0) +
             mul64(acc4, rs3);
        acc4 *= r0;
        d1 += (d0 >> 32);
        d2 += (d1 >> 32);
        d3 += (d2 >> 32);
        acc0 = (uint32_t) d0;
        acc1 = (uint32_t) d1;
        acc2 = (uint32_t) d2;
        acc3 = (uint32_t) d3;
        acc4 = (uint32_t) (d3 >> 32) + acc4;
        d0 = (uint64_t) acc0 + (acc4 >> 2) + (acc4 & 0xFFFFFFFCU);
        acc4 &= 3U;
        acc0 = (uint32_t) d0;
        d0 = (uint64_t) acc1 + (d0 >> 32U);
        acc1 = (uint32_t) d0;
        d0 = (uint64_t) acc2 + (d0 >> 32U);
        acc2 = (uint32_t) d0;
        d0 = (uint64_t) acc3 + (d0 >> 32U);
        acc3 = (uint32_t) d0;
        d0 = (uint64_t) acc4 + (d0 >> 32U);
        acc4 = (uint32_t) d0;
        offset += (16U);
    }
    ctx->acc[0] = acc0;
    ctx->acc[1] = acc1;
    ctx->acc[2] = acc2;
    ctx->acc[3] = acc3;
    ctx->acc[4] = acc4;
}

canonical_original_code:
static void poly1305_process(struct mbedtls_poly1305_context *ctx, unsigned long nblocks, unsigned char *input, unsigned int needs_padding)
{
    unsigned long d0; unsigned long d1; unsigned long d2; unsigned long d3;
    unsigned int acc0; unsigned int acc1; unsigned int acc2; unsigned int acc3; unsigned int acc4;
    unsigned int r0; unsigned int r1; unsigned int r2; unsigned int r3;
    unsigned int rs1; unsigned int rs2; unsigned int rs3;
    unsigned long offset = 0U;
    unsigned long i;
    r0 = ctx->r[0];
    r1 = ctx->r[1];
    r2 = ctx->r[2];
    r3 = ctx->r[3];
    rs1 = r1 + (r1 >> 2U);
    rs2 = r2 + (r2 >> 2U);
    rs3 = r3 + (r3 >> 2U);
    acc0 = ctx->acc[0];
    acc1 = ctx->acc[1];
    acc2 = ctx->acc[2];
    acc3 = ctx->acc[3];
    acc4 = ctx->acc[4];
    for (i = 0U; i < nblocks; i++) {
        d0 = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((input) + (offset + 0))) : mbedtls_get_unaligned_uint32((input) + (offset + 0)) );
        d1 = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((input) + (offset + 4))) : mbedtls_get_unaligned_uint32((input) + (offset + 4)) );
        d2 = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((input) + (offset + 8))) : mbedtls_get_unaligned_uint32((input) + (offset + 8)) );
        d3 = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((input) + (offset + 12))) : mbedtls_get_unaligned_uint32((input) + (offset + 12)) );
        d0 += (unsigned long) acc0;
        d1 += (unsigned long) acc1 + (d0 >> 32U);
        d2 += (unsigned long) acc2 + (d1 >> 32U);
        d3 += (unsigned long) acc3 + (d2 >> 32U);
        acc0 = (unsigned int) d0;
        acc1 = (unsigned int) d1;
        acc2 = (unsigned int) d2;
        acc3 = (unsigned int) d3;
        acc4 += (unsigned int) (d3 >> 32U) + needs_padding;
        d0 = mul64(acc0, r0) +
             mul64(acc1, rs3) +
             mul64(acc2, rs2) +
             mul64(acc3, rs1);
        d1 = mul64(acc0, r1) +
             mul64(acc1, r0) +
             mul64(acc2, rs3) +
             mul64(acc3, rs2) +
             mul64(acc4, rs1);
        d2 = mul64(acc0, r2) +
             mul64(acc1, r1) +
             mul64(acc2, r0) +
             mul64(acc3, rs3) +
             mul64(acc4, rs2);
        d3 = mul64(acc0, r3) +
             mul64(acc1, r2) +
             mul64(acc2, r1) +
             mul64(acc3, r0) +
             mul64(acc4, rs3);
        acc4 *= r0;
        d1 += (d0 >> 32);
        d2 += (d1 >> 32);
        d3 += (d2 >> 32);
        acc0 = (unsigned int) d0;
        acc1 = (unsigned int) d1;
        acc2 = (unsigned int) d2;
        acc3 = (unsigned int) d3;
        acc4 = (unsigned int) (d3 >> 32) + acc4;
        d0 = (unsigned long) acc0 + (acc4 >> 2) + (acc4 & 0xFFFFFFFCU);
        acc4 &= 3U;
        acc0 = (unsigned int) d0;
        d0 = (unsigned long) acc1 + (d0 >> 32U);
        acc1 = (unsigned int) d0;
        d0 = (unsigned long) acc2 + (d0 >> 32U);
        acc2 = (unsigned int) d0;
        d0 = (unsigned long) acc3 + (d0 >> 32U);
        acc3 = (unsigned int) d0;
        d0 = (unsigned long) acc4 + (d0 >> 32U);
        acc4 = (unsigned int) d0;
        offset += (16U);
    }
    ctx->acc[0] = acc0;
    ctx->acc[1] = acc1;
    ctx->acc[2] = acc2;
    ctx->acc[3] = acc3;
    ctx->acc[4] = acc4;
}

variable_types: struct mbedtls_poly1305_context { unsigned int r[4]; unsigned int s[4]; unsigned int acc[5]; unsigned char queue[16]; unsigned long queue_len; } *

variable_types:
nblocks
unsigned long

variable_types:
input
unsigned char *

variable_types:
offset
unsigned long

variable_types:
i
unsigned long


repo: rcr/rirc

decompiled_code:
__int64 __fastcall poly1305_compute_mac(unsigned int *a1, __int64 a2)
{
__int64 v4; // r10
unsigned int v5; // er9
unsigned int v6; // ecx
unsigned __int64 v7; // r13
unsigned int v8; // esi
unsigned __int64 v9; // rbx
unsigned __int64 v10; // rdi
unsigned int v11; // ebp
unsigned int v12; // eax
int v13; // ebp
unsigned int v14; // ecx
unsigned __int64 v15; // r13
unsigned __int64 v16; // rbx
unsigned int v17; // ebp

v4 = a1[4];
v5 = a1[10];
v6 = a1[8];
v7 = a1[9] + (((unsigned __int64)v6 + 5) >> 32);
v8 = a1[4];
v9 = HIDWORD(v7) + v5;
v10 = a1[11] + HIDWORD(v9);
v11 = (a1[12] + HIDWORD(v10)) >> 2;
v12 = v11 - 1;
v13 = -v11;
v14 = v13 & (v6 + 5) | v12 & v6;
v15 = (v12 & a1[9] | v13 & (unsigned int)v7) + (unsigned __int64)a1[5] + ((v4 + (unsigned __int64)v14) >> 32);
v16 = HIDWORD(v15) + a1[6] + (unsigned __int64)(v12 & v5 | v13 & (unsigned int)v9);
v17 = HIDWORD(v16) + a1[7] + (a1[11] & v12 | v10 & v13);
mbedtls_put_unaligned_uint32(a2, v14 + v8);
mbedtls_put_unaligned_uint32(a2 + 4, (unsigned int)v15);
mbedtls_put_unaligned_uint32(a2 + 8, (unsigned int)v16);
return mbedtls_put_unaligned_uint32(a2 + 12, v17);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1, __int64 a2)
{
__int64 v4; // r10
unsigned int v5; // er9
unsigned int v6; // ecx
unsigned __int64 v7; // r13
unsigned int v8; // esi
unsigned __int64 v9; // rbx
unsigned __int64 v10; // rdi
unsigned int v11; // ebp
unsigned int v12; // eax
int v13; // ebp
unsigned int v14; // ecx
unsigned __int64 v15; // r13
unsigned __int64 v16; // rbx
unsigned int v17; // ebp

v4 = a1[4];
v5 = a1[10];
v6 = a1[8];
v7 = a1[9] + (((unsigned __int64)v6 + 5) >> 32);
v8 = a1[4];
v9 = HIDWORD(v7) + v5;
v10 = a1[11] + HIDWORD(v9);
v11 = (a1[12] + HIDWORD(v10)) >> 2;
v12 = v11 - 1;
v13 = -v11;
v14 = v13 & (v6 + 5) | v12 & v6;
v15 = (v12 & a1[9] | v13 & (unsigned int)v7) + (unsigned __int64)a1[5] + ((v4 + (unsigned __int64)v14) >> 32);
v16 = HIDWORD(v15) + a1[6] + (unsigned __int64)(v12 & v5 | v13 & (unsigned int)v9);
v17 = HIDWORD(v16) + a1[7] + (a1[11] & v12 | v10 & v13);
mbedtls_put_unaligned_uint32(a2, v14 + v8);
mbedtls_put_unaligned_uint32(a2 + 4, (unsigned int)v15);
mbedtls_put_unaligned_uint32(a2 + 8, (unsigned int)v16);
return mbedtls_put_unaligned_uint32(a2 + 12, v17);
}

original_code:
static void poly1305_compute_mac(const mbedtls_poly1305_context *ctx,
                                 unsigned char mac[16])
{
    uint64_t d;
    uint32_t g0, g1, g2, g3, g4;
    uint32_t acc0, acc1, acc2, acc3, acc4;
    uint32_t mask;
    uint32_t mask_inv;
    acc0 = ctx->acc[0];
    acc1 = ctx->acc[1];
    acc2 = ctx->acc[2];
    acc3 = ctx->acc[3];
    acc4 = ctx->acc[4];
    d = ((uint64_t) acc0 + 5U);
    g0 = (uint32_t) d;
    d = ((uint64_t) acc1 + (d >> 32));
    g1 = (uint32_t) d;
    d = ((uint64_t) acc2 + (d >> 32));
    g2 = (uint32_t) d;
    d = ((uint64_t) acc3 + (d >> 32));
    g3 = (uint32_t) d;
    g4 = acc4 + (uint32_t) (d >> 32U);
    mask = (uint32_t) 0U - (g4 >> 2U);
    mask_inv = ~mask;
    acc0 = (acc0 & mask_inv) | (g0 & mask);
    acc1 = (acc1 & mask_inv) | (g1 & mask);
    acc2 = (acc2 & mask_inv) | (g2 & mask);
    acc3 = (acc3 & mask_inv) | (g3 & mask);
    d = (uint64_t) acc0 + ctx->s[0];
    acc0 = (uint32_t) d;
    d = (uint64_t) acc1 + ctx->s[1] + (d >> 32U);
    acc1 = (uint32_t) d;
    d = (uint64_t) acc2 + ctx->s[2] + (d >> 32U);
    acc2 = (uint32_t) d;
    acc3 += ctx->s[3] + (uint32_t) (d >> 32U);
    { if (((1234) == (4321))) { mbedtls_put_unaligned_uint32((mac) + (0), __builtin_bswap32((uint32_t) (acc0))); } else { mbedtls_put_unaligned_uint32((mac) + (0), ((uint32_t) (acc0))); } };
    { if (((1234) == (4321))) { mbedtls_put_unaligned_uint32((mac) + (4), __builtin_bswap32((uint32_t) (acc1))); } else { mbedtls_put_unaligned_uint32((mac) + (4), ((uint32_t) (acc1))); } };
    { if (((1234) == (4321))) { mbedtls_put_unaligned_uint32((mac) + (8), __builtin_bswap32((uint32_t) (acc2))); } else { mbedtls_put_unaligned_uint32((mac) + (8), ((uint32_t) (acc2))); } };
    { if (((1234) == (4321))) { mbedtls_put_unaligned_uint32((mac) + (12), __builtin_bswap32((uint32_t) (acc3))); } else { mbedtls_put_unaligned_uint32((mac) + (12), ((uint32_t) (acc3))); } };
}

canonical_original_code:
static void poly1305_compute_mac(struct mbedtls_poly1305_context *ctx, unsigned char mac[16])
{
    unsigned long d;
    unsigned int g0; unsigned int g1; unsigned int g2; unsigned int g3; unsigned int g4;
    unsigned int acc0; unsigned int acc1; unsigned int acc2; unsigned int acc3; unsigned int acc4;
    unsigned int mask;
    unsigned int mask_inv;
    acc0 = ctx->acc[0];
    acc1 = ctx->acc[1];
    acc2 = ctx->acc[2];
    acc3 = ctx->acc[3];
    acc4 = ctx->acc[4];
    d = ((unsigned long) acc0 + 5U);
    g0 = (unsigned int) d;
    d = ((unsigned long) acc1 + (d >> 32));
    g1 = (unsigned int) d;
    d = ((unsigned long) acc2 + (d >> 32));
    g2 = (unsigned int) d;
    d = ((unsigned long) acc3 + (d >> 32));
    g3 = (unsigned int) d;
    g4 = acc4 + (unsigned int) (d >> 32U);
    mask = (unsigned int) 0U - (g4 >> 2U);
    mask_inv = ~mask;
    acc0 = (acc0 & mask_inv) | (g0 & mask);
    acc1 = (acc1 & mask_inv) | (g1 & mask);
    acc2 = (acc2 & mask_inv) | (g2 & mask);
    acc3 = (acc3 & mask_inv) | (g3 & mask);
    d = (unsigned long) acc0 + ctx->s[0];
    acc0 = (unsigned int) d;
    d = (unsigned long) acc1 + ctx->s[1] + (d >> 32U);
    acc1 = (unsigned int) d;
    d = (unsigned long) acc2 + ctx->s[2] + (d >> 32U);
    acc2 = (unsigned int) d;
    acc3 += ctx->s[3] + (unsigned int) (d >> 32U);
    { if (((1234) == (4321))) { mbedtls_put_unaligned_uint32((mac) + (0), __builtin_bswap32((unsigned int) (acc0))); } else { mbedtls_put_unaligned_uint32((mac) + (0), ((unsigned int) (acc0))); } };
    { if (((1234) == (4321))) { mbedtls_put_unaligned_uint32((mac) + (4), __builtin_bswap32((unsigned int) (acc1))); } else { mbedtls_put_unaligned_uint32((mac) + (4), ((unsigned int) (acc1))); } };
    { if (((1234) == (4321))) { mbedtls_put_unaligned_uint32((mac) + (8), __builtin_bswap32((unsigned int) (acc2))); } else { mbedtls_put_unaligned_uint32((mac) + (8), ((unsigned int) (acc2))); } };
    { if (((1234) == (4321))) { mbedtls_put_unaligned_uint32((mac) + (12), __builtin_bswap32((unsigned int) (acc3))); } else { mbedtls_put_unaligned_uint32((mac) + (12), ((unsigned int) (acc3))); } };
}

variable_types:
mac
unsigned char [16]

variable_types:
d
unsigned long

variable_types:
mask
unsigned int

return_type: void

user_defined_types: struct mbedtls_poly1305_context { unsigned int r[4]; unsigned int s[4]; unsigned int acc[5]; unsigned char queue[16]; unsigned long queue_len; }


decompiled_code:
__int64 __fastcall mbedtls_poly1305_init(__int64 a1)
{
return mbedtls_platform_zeroize(a1, 80LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return mbedtls_platform_zeroize(a1, 80LL);
}

original_code:
void mbedtls_poly1305_init(mbedtls_poly1305_context *ctx)
{
    mbedtls_platform_zeroize(ctx, sizeof(mbedtls_poly1305_context));
}

canonical_original_code:
void mbedtls_poly1305_init(struct mbedtls_poly1305_context *ctx)
{
    mbedtls_platform_zeroize(ctx, sizeof(mbedtls_poly1305_context));
}


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

if ( a1 )
result = mbedtls_platform_zeroize(a1, 80LL);
return result;
}

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

if ( a1 )
result = mbedtls_platform_zeroize(a1, 80LL);
return result;
}

original_code:
void mbedtls_poly1305_free(mbedtls_poly1305_context *ctx)
{
    if (ctx == ((void *)0)) {
        return;
    }
    mbedtls_platform_zeroize(ctx, sizeof(mbedtls_poly1305_context));
}

canonical_original_code:
void mbedtls_poly1305_free(struct mbedtls_poly1305_context *ctx)
{
    if (ctx == ((void *)0)) {
        return;
    }
    mbedtls_platform_zeroize(ctx, sizeof(mbedtls_poly1305_context));
}


decompiled_code:
__int64 __fastcall mbedtls_poly1305_starts(__int64 a1, __int64 a2)
{
int v2; // eax

*(_DWORD *)a1 = mbedtls_get_unaligned_uint32(a2) & 0xFFFFFFF;
*(_DWORD *)(a1 + 4) = mbedtls_get_unaligned_uint32(a2 + 4) & 0xFFFFFFC;
*(_DWORD *)(a1 + 8) = mbedtls_get_unaligned_uint32(a2 + 8) & 0xFFFFFFC;
*(_DWORD *)(a1 + 12) = mbedtls_get_unaligned_uint32(a2 + 12) & 0xFFFFFFC;
*(_DWORD *)(a1 + 16) = mbedtls_get_unaligned_uint32(a2 + 16);
*(_DWORD *)(a1 + 20) = mbedtls_get_unaligned_uint32(a2 + 20);
*(_DWORD *)(a1 + 24) = mbedtls_get_unaligned_uint32(a2 + 24);
v2 = mbedtls_get_unaligned_uint32(a2 + 28);
*(_DWORD *)(a1 + 48) = 0;
*(_DWORD *)(a1 + 28) = v2;
*(_QWORD *)(a1 + 32) = 0LL;
*(_QWORD *)(a1 + 40) = 0LL;
mbedtls_platform_zeroize(a1 + 52, 16LL);
*(_QWORD *)(a1 + 72) = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
int v2; // eax

*(_DWORD *)a1 = mbedtls_get_unaligned_uint32(a2) & 0xFFFFFFF;
*(_DWORD *)(a1 + 4) = mbedtls_get_unaligned_uint32(a2 + 4) & 0xFFFFFFC;
*(_DWORD *)(a1 + 8) = mbedtls_get_unaligned_uint32(a2 + 8) & 0xFFFFFFC;
*(_DWORD *)(a1 + 12) = mbedtls_get_unaligned_uint32(a2 + 12) & 0xFFFFFFC;
*(_DWORD *)(a1 + 16) = mbedtls_get_unaligned_uint32(a2 + 16);
*(_DWORD *)(a1 + 20) = mbedtls_get_unaligned_uint32(a2 + 20);
*(_DWORD *)(a1 + 24) = mbedtls_get_unaligned_uint32(a2 + 24);
v2 = mbedtls_get_unaligned_uint32(a2 + 28);
*(_DWORD *)(a1 + 48) = 0;
*(_DWORD *)(a1 + 28) = v2;
*(_QWORD *)(a1 + 32) = 0LL;
*(_QWORD *)(a1 + 40) = 0LL;
mbedtls_platform_zeroize(a1 + 52, 16LL);
*(_QWORD *)(a1 + 72) = 0LL;
return 0LL;
}

original_code:
int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx,
                            const unsigned char key[32])
{
    ctx->r[0] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (0))) : mbedtls_get_unaligned_uint32((key) + (0)) ) & 0x0FFFFFFFU;
    ctx->r[1] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (4))) : mbedtls_get_unaligned_uint32((key) + (4)) ) & 0x0FFFFFFCU;
    ctx->r[2] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (8))) : mbedtls_get_unaligned_uint32((key) + (8)) ) & 0x0FFFFFFCU;
    ctx->r[3] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (12))) : mbedtls_get_unaligned_uint32((key) + (12)) ) & 0x0FFFFFFCU;
    ctx->s[0] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (16))) : mbedtls_get_unaligned_uint32((key) + (16)) );
    ctx->s[1] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (20))) : mbedtls_get_unaligned_uint32((key) + (20)) );
    ctx->s[2] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (24))) : mbedtls_get_unaligned_uint32((key) + (24)) );
    ctx->s[3] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (28))) : mbedtls_get_unaligned_uint32((key) + (28)) );
    ctx->acc[0] = 0U;
    ctx->acc[1] = 0U;
    ctx->acc[2] = 0U;
    ctx->acc[3] = 0U;
    ctx->acc[4] = 0U;
    mbedtls_platform_zeroize(ctx->queue, sizeof(ctx->queue));
    ctx->queue_len = 0U;
    return 0;
}

canonical_original_code:
int mbedtls_poly1305_starts(struct mbedtls_poly1305_context *ctx, unsigned char key[32])
{
    ctx->r[0] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (0))) : mbedtls_get_unaligned_uint32((key) + (0)) ) & 0x0FFFFFFFU;
    ctx->r[1] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (4))) : mbedtls_get_unaligned_uint32((key) + (4)) ) & 0x0FFFFFFCU;
    ctx->r[2] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (8))) : mbedtls_get_unaligned_uint32((key) + (8)) ) & 0x0FFFFFFCU;
    ctx->r[3] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (12))) : mbedtls_get_unaligned_uint32((key) + (12)) ) & 0x0FFFFFFCU;
    ctx->s[0] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (16))) : mbedtls_get_unaligned_uint32((key) + (16)) );
    ctx->s[1] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (20))) : mbedtls_get_unaligned_uint32((key) + (20)) );
    ctx->s[2] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (24))) : mbedtls_get_unaligned_uint32((key) + (24)) );
    ctx->s[3] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (28))) : mbedtls_get_unaligned_uint32((key) + (28)) );
    ctx->acc[0] = 0U;
    ctx->acc[1] = 0U;
    ctx->acc[2] = 0U;
    ctx->acc[3] = 0U;
    ctx->acc[4] = 0U;
    mbedtls_platform_zeroize(ctx->queue, sizeof(ctx->queue));
    ctx->queue_len = 0U;
    return 0;
}

variable_types:
key
unsigned char [32]

return_type: int


original_code:
int mbedtls_poly1305_update(mbedtls_poly1305_context *ctx,
                            const unsigned char *input,
                            size_t ilen)
{
    size_t offset = 0U;
    size_t remaining = ilen;
    size_t queue_free_len;
    size_t nblocks;
    if ((remaining > 0U) && (ctx->queue_len > 0U)) {
        queue_free_len = ((16U) - ctx->queue_len);
        if (ilen < queue_free_len) {
            memcpy(&ctx->queue[ctx->queue_len],
                   input,
                   ilen);
            ctx->queue_len += ilen;
            remaining = 0U;
        } else {
            memcpy(&ctx->queue[ctx->queue_len],
                   input,
                   queue_free_len);
            ctx->queue_len = 0U;
            poly1305_process(ctx, 1U, ctx->queue, 1U);
            offset += queue_free_len;
            remaining -= queue_free_len;
        }
    }
    if (remaining >= (16U)) {
        nblocks = remaining / (16U);
        poly1305_process(ctx, nblocks, &input[offset], 1U);
        offset += nblocks * (16U);
        remaining %= (16U);
    }
    if (remaining > 0U) {
        ctx->queue_len = remaining;
        memcpy(ctx->queue, &input[offset], remaining);
    }
    return 0;
}

canonical_original_code:
int mbedtls_poly1305_update(struct mbedtls_poly1305_context *ctx, unsigned char *input, unsigned long ilen)
{
    unsigned long offset = 0U;
    unsigned long remaining = ilen;
    unsigned long queue_free_len;
    unsigned long nblocks;
    if ((remaining > 0U) && (ctx->queue_len > 0U)) {
        queue_free_len = ((16U) - ctx->queue_len);
        if (ilen < queue_free_len) {
            memcpy(&ctx->queue[ctx->queue_len],
                   input,
                   ilen);
            ctx->queue_len += ilen;
            remaining = 0U;
        } else {
            memcpy(&ctx->queue[ctx->queue_len],
                   input,
                   queue_free_len);
            ctx->queue_len = 0U;
            poly1305_process(ctx, 1U, ctx->queue, 1U);
            offset += queue_free_len;
            remaining -= queue_free_len;
        }
    }
    if (remaining >= (16U)) {
        nblocks = remaining / (16U);
        poly1305_process(ctx, nblocks, &input[offset], 1U);
        offset += nblocks * (16U);
        remaining %= (16U);
    }
    if (remaining > 0U) {
        ctx->queue_len = remaining;
        memcpy(ctx->queue, &input[offset], remaining);
    }
    return 0;
}

variable_types:
ilen
unsigned long

variable_types:
remaining
unsigned long


original_code:
int mbedtls_poly1305_finish(mbedtls_poly1305_context *ctx,
                            unsigned char mac[16])
{
    if (ctx->queue_len > 0U) {
        ctx->queue[ctx->queue_len] = 1U;
        ctx->queue_len++;
        memset(&ctx->queue[ctx->queue_len],
               0,
               (16U) - ctx->queue_len);
        poly1305_process(ctx, 1U,
                         ctx->queue, 0U);
    }
    poly1305_compute_mac(ctx, mac);
    return 0;
}

canonical_original_code:
int mbedtls_poly1305_finish(struct mbedtls_poly1305_context *ctx, unsigned char mac[16])
{
    if (ctx->queue_len > 0U) {
        ctx->queue[ctx->queue_len] = 1U;
        ctx->queue_len++;
        memset(&ctx->queue[ctx->queue_len],
               0,
               (16U) - ctx->queue_len);
        poly1305_process(ctx, 1U,
                         ctx->queue, 0U);
    }
    poly1305_compute_mac(ctx, mac);
    return 0;
}


decompiled_code:
__int64 __fastcall mbedtls_poly1305_mac(__int64 a1, _DWORD *a2, size_t a3, __int64 a4)
{
unsigned int v6; // er12
_BYTE v8[120]; // [rsp+0h] [rbp-78h] BYREF

mbedtls_poly1305_init((__int64)v8);
v6 = mbedtls_poly1305_starts((__int64)v8, a1);
if ( !v6 )
{
v6 = mbedtls_poly1305_update((__int64)v8, a2, a3);
if ( !v6 )
v6 = mbedtls_poly1305_finish((__int64)v8, a4);
}
mbedtls_poly1305_free((__int64)v8);
return v6;
}

original_code:
int mbedtls_poly1305_mac(const unsigned char key[32],
                         const unsigned char *input,
                         size_t ilen,
                         unsigned char mac[16])
{
    mbedtls_poly1305_context ctx;
    int ret = -0x006E;
    mbedtls_poly1305_init(&ctx);
    ret = mbedtls_poly1305_starts(&ctx, key);
    if (ret != 0) {
        goto cleanup;
    }
    ret = mbedtls_poly1305_update(&ctx, input, ilen);
    if (ret != 0) {
        goto cleanup;
    }
    ret = mbedtls_poly1305_finish(&ctx, mac);
cleanup:
    mbedtls_poly1305_free(&ctx);
    return ret;
}

canonical_original_code:
int mbedtls_poly1305_mac(unsigned char key[32], unsigned char *input, unsigned long ilen, unsigned char mac[16])
{
    struct mbedtls_poly1305_context ctx;
    int ret = -0x006E;
    mbedtls_poly1305_init(&ctx);
    ret = mbedtls_poly1305_starts(&ctx, key);
    if (ret != 0) {
        goto cleanup;
    }
    ret = mbedtls_poly1305_update(&ctx, input, ilen);
    if (ret != 0) {
        goto cleanup;
    }
    ret = mbedtls_poly1305_finish(&ctx, mac);
cleanup:
    mbedtls_poly1305_free(&ctx);
    return ret;
}

variable_types: struct mbedtls_poly1305_context { unsigned int r[4]; unsigned int s[4]; unsigned int acc[5]; unsigned char queue[16]; unsigned long queue_len; }

variable_types: ret int


decompiled_code:
__int64 __fastcall mbedtls_get_unaligned_uint16(unsigned __int16 *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 *a1)
{
return *a1;
}

original_code:
inline uint16_t mbedtls_get_unaligned_uint16(const void *p)
{
    uint16_t r;
    memcpy(&r, p, sizeof(r));
    return r;
}

canonical_original_code:
inline unsigned short mbedtls_get_unaligned_uint16(void *p)
{
    unsigned short r;
    memcpy(&r, p, sizeof(r));
    return r;
}

variable_types:
p
void *

variable_types:
r
unsigned short

return_type: unsigned short


decompiled_code:
__int64 __fastcall mbedtls_get_unaligned_uint32(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1)
{
return *a1;
}

original_code:
inline uint32_t mbedtls_get_unaligned_uint32(const void *p)
{
    uint32_t r;
    memcpy(&r, p, sizeof(r));
    return r;
}

canonical_original_code:
inline unsigned int mbedtls_get_unaligned_uint32(void *p)
{
    unsigned int r;
    memcpy(&r, p, sizeof(r));
    return r;
}

variable_types:
r
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall mbedtls_get_unaligned_uint64(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
inline uint64_t mbedtls_get_unaligned_uint64(const void *p)
{
    uint64_t r;
    memcpy(&r, p, sizeof(r));
    return r;
}

canonical_original_code:
inline unsigned long mbedtls_get_unaligned_uint64(void *p)
{
    unsigned long r;
    memcpy(&r, p, sizeof(r));
    return r;
}

variable_types:
r
unsigned long

return_type: unsigned long


decompiled_code:
void __fastcall mbedtls_xor(__int64 a1, __int64 a2, __int64 a3, unsigned __int64 a4)
{
unsigned __int64 v7; // rdx
__int64 v8; // rbp
__int64 v9; // rdx
__int64 v10; // r9
int v11; // esi
int v12; // eax
__int64 v13; // rdx
__int64 v14; // r8
unsigned __int64 v15; // rax

if ( a4 <= 3 )
{
v15 = 0LL;
}
else
{
v7 = 4LL;
v8 = a2 - 4;
do
{
v11 = mbedtls_get_unaligned_uint32((unsigned int *)(v8 + v7));
v12 = mbedtls_get_unaligned_uint32((unsigned int *)(v10 + v9));
mbedtls_put_unaligned_uint32((_DWORD *)(v14 + v13), v12 ^ v11);
}
while ( a4 >= v7 );
}
if ( a4 > v15 )
{
do
{
*(_BYTE *)(a1 + v15) = *(_BYTE *)(a3 + v15) ^ *(_BYTE *)(a2 + v15);
++v15;
}
while ( a4 != v15 );
}
}

original_code:
inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n)
{
    size_t i = 0;
    for (; (i + 4) <= n; i += 4) {
        uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
        mbedtls_put_unaligned_uint32(r + i, x);
    }
    for (; i < n; i++) {
        r[i] = a[i] ^ b[i];
    }
}

canonical_original_code:
inline void mbedtls_xor(unsigned char *r, unsigned char *a, unsigned char *b, unsigned long n)
{
    unsigned long i = 0;
    for (; (i + 4) <= n; i += 4) {
        unsigned int x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
        mbedtls_put_unaligned_uint32(r + i, x);
    }
    for (; i < n; i++) {
        r[i] = a[i] ^ b[i];
    }
}

variable_types:
r
unsigned char *

variable_types:
a
unsigned char *

variable_types:
b
unsigned char *

variable_types:
n
unsigned long

variable_types:
x
unsigned int


decompiled_code:
// attributes: thunk
struct tm *mbedtls_platform_gmtime_r(const time_t *timer, struct tm *tp)
{
return gmtime_r(timer, tp);
}

canonical_decompiled_code:
struct tm *func8(const time_t *timer, struct tm *tp)
{
return gmtime_r(timer, tp);
}

original_code:
struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
                                     struct tm *tm_buf)
{
    return gmtime_r(tt, tm_buf);
}

canonical_original_code:
struct tm *mbedtls_platform_gmtime_r(long *tt, struct tm *tm_buf)
{
    return gmtime_r(tt, tm_buf);
}

variable_types:
tt
long *

return_type: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


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

if ( a2 )
result = explicit_bzero();
return result;
}

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

if ( a2 )
result = explicit_bzero();
return result;
}

original_code:
void mbedtls_platform_zeroize(void *buf, size_t len)
{
    do { } while (0);
    if (len > 0) {
        explicit_bzero(buf, len);
    }
}

canonical_original_code:
void mbedtls_platform_zeroize(void *buf, unsigned long len)
{
    do { } while (0);
    if (len > 0) {
        explicit_bzero(buf, len);
    }
}

variable_types:
buf
void *

variable_types:
len
unsigned long


decompiled_code:
__int64 __fastcall mbedtls_base64_encode(_BYTE *a1, unsigned __int64 a2, unsigned __int64 *a3, unsigned __int8 *a4, unsigned __int64 a5)
{
unsigned __int64 v6; // rax
unsigned __int64 v7; // rax
_BYTE *v8; // rbp
unsigned __int64 i; // r14
int v10; // er13
int v11; // ebx
unsigned __int8 v12; // r12
unsigned __int64 v13; // rax
unsigned __int64 v14; // rax
_BYTE *v15; // r12
__int64 result; // rax
int v17; // edi
char v18; // bl
__int64 v19; // rdi
unsigned __int8 v20; // bl
char v21; // al
int v22; // ebp
unsigned __int64 v23; // [rsp+0h] [rbp-58h]
_BYTE *v25; // [rsp+10h] [rbp-48h]

v25 = a1;
if ( a5 )
{
v23 = (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)a5) >> 64) & 0xFFFFFFFFFFFFFFFELL) + a5 / 3;
v6 = a5 / 3 + (a5 != v23);
if ( v6 > 0x3FFFFFFFFFFFFFFFLL )
{
*a3 = -1LL;
result = 4294967254LL;
}
else
{
v7 = 4 * v6 + 1;
if ( a1 && v7 <= a2 )
{
if ( v23 )
{
v8 = a1;
for ( i = 0LL; i < v23; i += 3LL )
{
v10 = *a4;
v11 = a4[1];
v8 += 4;
a4 += 3;
v12 = *(a4 - 1);
*(v8 - 4) = mbedtls_ct_base64_enc_char(v10 >> 2);
*(v8 - 3) = mbedtls_ct_base64_enc_char(((16 * (_BYTE)v10) & 0x30u) + (v11 >> 4));
*(v8 - 2) = mbedtls_ct_base64_enc_char((unsigned __int8)((v12 >> 6) + ((4 * v11) & 0x3C)));
*(v8 - 1) = mbedtls_ct_base64_enc_char(v12 & 0x3F);
v13 = i;
}
v14 = v13 + 4;
v15 = v8;
if ( a5 <= i )
{
LABEL_9:
*a3 = v8 - v25;
result = 0LL;
*v8 = 0;
return result;
}
}
else
{
v15 = a1;
v14 = 1LL;
}
v17 = *a4;
v18 = 16 * v17;
v19 = v17 >> 2;
v20 = v18 & 0x30;
if ( a5 > v14 )
{
v22 = a4[1];
*v15 = mbedtls_ct_base64_enc_char(v19);
v15[1] = mbedtls_ct_base64_enc_char((unsigned __int8)(v20 + (v22 >> 4)));
v15[2] = mbedtls_ct_base64_enc_char((4 * (_BYTE)v22) & 0x3C);
}
else
{
*v15 = mbedtls_ct_base64_enc_char(v19);
v21 = mbedtls_ct_base64_enc_char(v20);
v15[2] = 61;
v15[1] = v21;
}
v15[3] = 61;
v8 = v15 + 4;
goto LABEL_9;
}
*a3 = v7;
result = 4294967254LL;
}
}
else
{
*a3 = 0LL;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, unsigned __int64 a2, unsigned __int64 *a3, unsigned __int8 *a4, unsigned __int64 a5)
{
unsigned __int64 v6; // rax
unsigned __int64 v7; // rax
_BYTE *v8; // rbp
unsigned __int64 i; // r14
int v10; // er13
int v11; // ebx
unsigned __int8 v12; // r12
unsigned __int64 v13; // rax
unsigned __int64 v14; // rax
_BYTE *v15; // r12
__int64 result; // rax
int v17; // edi
char v18; // bl
__int64 v19; // rdi
unsigned __int8 v20; // bl
char v21; // al
int v22; // ebp
unsigned __int64 v23; // [rsp+0h] [rbp-58h]
_BYTE *v25; // [rsp+10h] [rbp-48h]

v25 = a1;
if ( a5 )
{
v23 = (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)a5) >> 64) & 0xFFFFFFFFFFFFFFFELL) + a5 / 3;
v6 = a5 / 3 + (a5 != v23);
if ( v6 > 0x3FFFFFFFFFFFFFFFLL )
{
*a3 = -1LL;
result = 4294967254LL;
}
else
{
v7 = 4 * v6 + 1;
if ( a1 && v7 <= a2 )
{
if ( v23 )
{
v8 = a1;
for ( i = 0LL; i < v23; i += 3LL )
{
v10 = *a4;
v11 = a4[1];
v8 += 4;
a4 += 3;
v12 = *(a4 - 1);
*(v8 - 4) = mbedtls_ct_base64_enc_char(v10 >> 2);
*(v8 - 3) = mbedtls_ct_base64_enc_char(((16 * (_BYTE)v10) & 0x30u) + (v11 >> 4));
*(v8 - 2) = mbedtls_ct_base64_enc_char((unsigned __int8)((v12 >> 6) + ((4 * v11) & 0x3C)));
*(v8 - 1) = mbedtls_ct_base64_enc_char(v12 & 0x3F);
v13 = i;
}
v14 = v13 + 4;
v15 = v8;
if ( a5 <= i )
{
LABEL_9:
*a3 = v8 - v25;
result = 0LL;
*v8 = 0;
return result;
}
}
else
{
v15 = a1;
v14 = 1LL;
}
v17 = *a4;
v18 = 16 * v17;
v19 = v17 >> 2;
v20 = v18 & 0x30;
if ( a5 > v14 )
{
v22 = a4[1];
*v15 = mbedtls_ct_base64_enc_char(v19);
v15[1] = mbedtls_ct_base64_enc_char((unsigned __int8)(v20 + (v22 >> 4)));
v15[2] = mbedtls_ct_base64_enc_char((4 * (_BYTE)v22) & 0x3C);
}
else
{
*v15 = mbedtls_ct_base64_enc_char(v19);
v21 = mbedtls_ct_base64_enc_char(v20);
v15[2] = 61;
v15[1] = v21;
}
v15[3] = 61;
v8 = v15 + 4;
goto LABEL_9;
}
*a3 = v7;
result = 4294967254LL;
}
}
else
{
*a3 = 0LL;
result = 0LL;
}
return result;
}

original_code:
int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
                          const unsigned char *src, size_t slen)
{
    size_t i, n;
    int C1, C2, C3;
    unsigned char *p;
    if (slen == 0) {
        *olen = 0;
        return 0;
    }
    n = slen / 3 + (slen % 3 != 0);
    if (n > (((size_t) -1) - 1) / 4) {
        *olen = ((size_t) -1);
        return -0x002A;
    }
    n *= 4;
    if ((dlen < n + 1) || (((void *)0) == dst)) {
        *olen = n + 1;
        return -0x002A;
    }
    n = (slen / 3) * 3;
    for (i = 0, p = dst; i < n; i += 3) {
        C1 = *src++;
        C2 = *src++;
        C3 = *src++;
        *p++ = mbedtls_ct_base64_enc_char((C1 >> 2) & 0x3F);
        *p++ = mbedtls_ct_base64_enc_char((((C1 & 3) << 4) + (C2 >> 4))
                                          & 0x3F);
        *p++ = mbedtls_ct_base64_enc_char((((C2 & 15) << 2) + (C3 >> 6))
                                          & 0x3F);
        *p++ = mbedtls_ct_base64_enc_char(C3 & 0x3F);
    }
    if (i < slen) {
        C1 = *src++;
        C2 = ((i + 1) < slen) ? *src++ : 0;
        *p++ = mbedtls_ct_base64_enc_char((C1 >> 2) & 0x3F);
        *p++ = mbedtls_ct_base64_enc_char((((C1 & 3) << 4) + (C2 >> 4))
                                          & 0x3F);
        if ((i + 1) < slen) {
            *p++ = mbedtls_ct_base64_enc_char(((C2 & 15) << 2) & 0x3F);
        } else {
            *p++ = '=';
        }
        *p++ = '=';
    }
    *olen = p - dst;
    *p = 0;
    return 0;
}

canonical_original_code:
int mbedtls_base64_encode(unsigned char *dst, unsigned long dlen, unsigned long *olen, unsigned char *src, unsigned long slen)
{
    unsigned long i; unsigned long n;
    int C1; int C2; int C3;
    unsigned char *p;
    if (slen == 0) {
        *olen = 0;
        return 0;
    }
    n = slen / 3 + (slen % 3 != 0);
    if (n > (((unsigned long) -1) - 1) / 4) {
        *olen = ((unsigned long) -1);
        return -0x002A;
    }
    n *= 4;
    if ((dlen < n + 1) || (((void *)0) == dst)) {
        *olen = n + 1;
        return -0x002A;
    }
    n = (slen / 3) * 3;
    for (i = 0, p = dst; i < n; i += 3) {
        C1 = *src++;
        C2 = *src++;
        C3 = *src++;
        *p++ = mbedtls_ct_base64_enc_char((C1 >> 2) & 0x3F);
        *p++ = mbedtls_ct_base64_enc_char((((C1 & 3) << 4) + (C2 >> 4))
                                          & 0x3F);
        *p++ = mbedtls_ct_base64_enc_char((((C2 & 15) << 2) + (C3 >> 6))
                                          & 0x3F);
        *p++ = mbedtls_ct_base64_enc_char(C3 & 0x3F);
    }
    if (i < slen) {
        C1 = *src++;
        C2 = ((i + 1) < slen) ? *src++ : 0;
        *p++ = mbedtls_ct_base64_enc_char((C1 >> 2) & 0x3F);
        *p++ = mbedtls_ct_base64_enc_char((((C1 & 3) << 4) + (C2 >> 4))
                                          & 0x3F);
        if ((i + 1) < slen) {
            *p++ = mbedtls_ct_base64_enc_char(((C2 & 15) << 2) & 0x3F);
        } else {
            *p++ = '=';
        }
        *p++ = '=';
    }
    *olen = p - dst;
    *p = 0;
    return 0;
}

variable_types:
dst
unsigned char *

variable_types:
dlen
unsigned long

variable_types:
olen
unsigned long *

variable_types:
src
unsigned char *

variable_types:
slen
unsigned long

variable_types:
i
unsigned long

variable_types:
p
unsigned char *


original_code:
int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,
                          const unsigned char *src, size_t slen)
{
    size_t i;
    size_t n;
    uint32_t x;
    unsigned accumulated_digits = 0;
    unsigned equals = 0;
    int spaces_present = 0;
    unsigned char *p;
    for (i = n = 0; i < slen; i++) {
        spaces_present = 0;
        while (i < slen && src[i] == ' ') {
            ++i;
            spaces_present = 1;
        }
        if (i == slen) {
            break;
        }
        if ((slen - i) >= 2 &&
            src[i] == '\r' && src[i + 1] == '\n') {
            continue;
        }
        if (src[i] == '\n') {
            continue;
        }
        if (spaces_present) {
            return -0x002C;
        }
        if (src[i] > 127) {
            return -0x002C;
        }
        if (src[i] == '=') {
            if (++equals > 2) {
                return -0x002C;
            }
        } else {
            if (equals != 0) {
                return -0x002C;
            }
            if (mbedtls_ct_base64_dec_value(src[i]) < 0) {
                return -0x002C;
            }
        }
        n++;
    }
    if (n == 0) {
        *olen = 0;
        return 0;
    }
    n = (6 * (n >> 3)) + ((6 * (n & 0x7) + 7) >> 3);
    n -= equals;
    if (dst == ((void *)0) || dlen < n) {
        *olen = n;
        return -0x002A;
    }
    equals = 0;
    for (x = 0, p = dst; i > 0; i--, src++) {
        if (*src == '\r' || *src == '\n' || *src == ' ') {
            continue;
        }
        x = x << 6;
        if (*src == '=') {
            ++equals;
        } else {
            x |= mbedtls_ct_base64_dec_value(*src);
        }
        if (++accumulated_digits == 4) {
            accumulated_digits = 0;
            *p++ = ((uint8_t) (((x) >> 16) & 0xff));
            if (equals <= 1) {
                *p++ = ((uint8_t) (((x) >> 8) & 0xff));
            }
            if (equals <= 0) {
                *p++ = ((uint8_t) ((x) & 0xff));
            }
        }
    }
    *olen = p - dst;
    return 0;
}

canonical_original_code:
int mbedtls_base64_decode(unsigned char *dst, unsigned long dlen, unsigned long *olen, unsigned char *src, unsigned long slen)
{
    unsigned long i;
    unsigned long n;
    unsigned int x;
    unsigned int accumulated_digits = 0;
    unsigned int equals = 0;
    int spaces_present = 0;
    unsigned char *p;
    for (i = n = 0; i < slen; i++) {
        spaces_present = 0;
        while (i < slen && src[i] == ' ') {
            ++i;
            spaces_present = 1;
        }
        if (i == slen) {
            break;
        }
        if ((slen - i) >= 2 &&
            src[i] == '\r' && src[i + 1] == '\n') {
            continue;
        }
        if (src[i] == '\n') {
            continue;
        }
        if (spaces_present) {
            return -0x002C;
        }
        if (src[i] > 127) {
            return -0x002C;
        }
        if (src[i] == '=') {
            if (++equals > 2) {
                return -0x002C;
            }
        } else {
            if (equals != 0) {
                return -0x002C;
            }
            if (mbedtls_ct_base64_dec_value(src[i]) < 0) {
                return -0x002C;
            }
        }
        n++;
    }
    if (n == 0) {
        *olen = 0;
        return 0;
    }
    n = (6 * (n >> 3)) + ((6 * (n & 0x7) + 7) >> 3);
    n -= equals;
    if (dst == ((void *)0) || dlen < n) {
        *olen = n;
        return -0x002A;
    }
    equals = 0;
    for (x = 0, p = dst; i > 0; i--, src++) {
        if (*src == '\r' || *src == '\n' || *src == ' ') {
            continue;
        }
        x = x << 6;
        if (*src == '=') {
            ++equals;
        } else {
            x |= mbedtls_ct_base64_dec_value(*src);
        }
        if (++accumulated_digits == 4) {
            accumulated_digits = 0;
            *p++ = ((unsigned char) (((x) >> 16) & 0xff));
            if (equals <= 1) {
                *p++ = ((unsigned char) (((x) >> 8) & 0xff));
            }
            if (equals <= 0) {
                *p++ = ((unsigned char) ((x) & 0xff));
            }
        }
    }
    *olen = p - dst;
    return 0;
}

variable_types:
equals
unsigned int


decompiled_code:
void __fastcall mbedtls_debug_set_threshold(int a1)
{
debug_threshold = a1;
}

canonical_decompiled_code:
void __fastcall func2(int a1)
{
debug_threshold = a1;
}

original_code:
void mbedtls_debug_set_threshold(int threshold)
{
    debug_threshold = threshold;
}

variable_types: threshold int


main

canonical_name: main

repo: rdhananjaya/thread-safe-fifo-queue

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
char *v4; // rbx
void *v5; // r13
pthread_t *v6; // rdi
char *v7; // rcx
char *v8; // r12
char *v9; // rbx
pthread_t *v10; // rbp
pthread_t v11; // rdi
pthread_t v12; // rdi
_BYTE v14[40]; // [rsp+0h] [rbp-E8h] BYREF
char v15; // [rsp+28h] [rbp-C0h] BYREF
pthread_t newthread[6]; // [rsp+30h] [rbp-B8h] BYREF
char arg[136]; // [rsp+60h] [rbp-88h] BYREF

v3 = 0LL;
v4 = arg;
v5 = (void *)queue_factory(argc, argv, envp);
do
{
*((_DWORD *)v4 + 1) = v3;
v6 = &newthread[v3];
v7 = v4;
++v3;
*(_DWORD *)v4 = 1000;
v4 += 16;
*((_QWORD *)v4 - 1) = v5;
v8 = (char *)newthread;
pthread_create(v6, 0LL, func, v7);
}
while ( v3 != 6 );
v9 = v14;
v10 = (pthread_t *)v14;
do
pthread_create(v10++, 0LL, func_d, v5);
while ( &v15 != (char *)v10 );
do
{
v11 = *(_QWORD *)v8;
v8 += 8;
pthread_join(v11, 0LL);
}
while ( arg != v8 );
do
{
v12 = *(_QWORD *)v9;
v9 += 8;
pthread_join(v12, 0LL);
}
while ( v9 != &v15 );
puts("Going to run queue_destroy(...) ");
queue_destroy(v5);
puts("que freeed... :) ");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
char *v4; // rbx
void *v5; // r13
pthread_t *v6; // rdi
char *v7; // rcx
char *v8; // r12
char *v9; // rbx
pthread_t *v10; // rbp
pthread_t v11; // rdi
pthread_t v12; // rdi
_BYTE v14[40]; // [rsp+0h] [rbp-E8h] BYREF
char v15; // [rsp+28h] [rbp-C0h] BYREF
pthread_t newthread[6]; // [rsp+30h] [rbp-B8h] BYREF
char arg[136]; // [rsp+60h] [rbp-88h] BYREF

v3 = 0LL;
v4 = arg;
v5 = (void *)func7(argc, argv, envp);
do
{
*((_DWORD *)v4 + 1) = v3;
v6 = &newthread[v3];
v7 = v4;
++v3;
*(_DWORD *)v4 = 1000;
v4 += 16;
*((_QWORD *)v4 - 1) = v5;
v8 = (char *)newthread;
pthread_create(v6, 0LL, func, v7);
}
while ( v3 != 6 );
v9 = v14;
v10 = (pthread_t *)v14;
do
pthread_create(v10++, 0LL, func_d, v5);
while ( &v15 != (char *)v10 );
do
{
v11 = *(_QWORD *)v8;
v8 += 8;
pthread_join(v11, 0LL);
}
while ( arg != v8 );
do
{
v12 = *(_QWORD *)v9;
v9 += 8;
pthread_join(v12, 0LL);
}
while ( v9 != &v15 );
puts("Going to run queue_destroy(...) ");
func8(v5);
puts("que freeed... :) ");
return 0;
}

original_code:
int main(void)
{
    queue * q = queue_factory();
    pthread_t threads[6];
    pthread_t thread_d[5];
    int i;
    struct _stru arg[6];
    for(i = 0; i < 6; i++) {
        arg[i].number = 1000;
        arg[i].thread_no = i;
        arg[i].q = q;
        pthread_create(threads+i, ((void *)0), func, (void *)&arg[i]);
    }
    for(i = 0; i < 5; i++) {
        pthread_create(thread_d+i, ((void *)0), func_d, (void *)q);
    }
    for(i = 0; i < 6; i++) {
        pthread_join(*(threads+i), ((void *)0));
    }
    for(i = 0; i < 5; i++) {
        pthread_join(*(thread_d+i), ((void *)0));
    }
    printf("Going to run queue_destroy(...) \n");
    queue_destroy(q);
    printf("que freeed... :) \n");
    return 0;
}

canonical_original_code:
int main()
{
    struct _end_q *q = queue_factory();
    unsigned long threads[6];
    unsigned long thread_d[5];
    int i;
    struct _stru arg[6];
    for(i = 0; i < 6; i++) {
        arg[i].number = 1000;
        arg[i].thread_no = i;
        arg[i].q = q;
        pthread_create(threads+i, ((void *)0), func, (void *)&arg[i]);
    }
    for(i = 0; i < 5; i++) {
        pthread_create(thread_d+i, ((void *)0), func_d, (void *)q);
    }
    for(i = 0; i < 6; i++) {
        pthread_join(*(threads+i), ((void *)0));
    }
    for(i = 0; i < 5; i++) {
        pthread_join(*(thread_d+i), ((void *)0));
    }
    printf("Going to run queue_destroy(...) \n");
    queue_destroy(q);
    printf("que freeed... :) \n");
    return 0;
}

variable_types:
threads
unsigned long [6]

variable_types: i int

variable_types: struct _stru { int number; int thread_no; struct _end_q { struct _node { void *data; struct _node *next; } *first; struct _node *last; } *q; } [6]

user_defined_types: struct _end_q { struct _node *first; struct _node *last; }

user_defined_types: struct _stru { int number; int thread_no; struct _end_q *q; }


decompiled_code:
_QWORD *queue_factory()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

v0 = malloc(0x10uLL);
v1 = v0;
if ( v0 )
{
*v0 = 0LL;
v0[1] = 0LL;
}
else
{
fwrite("Malloc failed creating the que\n", 1uLL, 0x1FuLL, stderr);
}
return v1;
}

canonical_decompiled_code:
_QWORD *func7()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

v0 = malloc(0x10uLL);
v1 = v0;
if ( v0 )
{
*v0 = 0LL;
v0[1] = 0LL;
}
else
{
fwrite("Malloc failed creating the que\n", 1uLL, 0x1FuLL, stderr);
}
return v1;
}

original_code:
queue * queue_factory()
{
   queue * new_queue = malloc(sizeof(queue));
   if(new_queue == ((void *)0)) {
       fprintf(stderr, "Malloc failed creating the que\n");
       return ((void *)0);
   }
   new_queue->first = ((void *)0);
   new_queue->last = ((void *)0);
   ;
   return new_queue;
}

canonical_original_code:
struct _end_q *queue_factory()
{
   struct _end_q *new_queue = malloc(sizeof(queue));
   if(new_queue == ((void *)0)) {
       fprintf(stderr, "Malloc failed creating the que\n");
       return ((void *)0);
   }
   new_queue->first = ((void *)0);
   new_queue->last = ((void *)0);
   ;
   return new_queue;
}

variable_types: struct _end_q { struct _node { void *data; struct _node *next; } *first; struct _node *last; } *


decompiled_code:
int __fastcall queue_destroy(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi
int result; // eax

if ( ptr )
{
pthread_mutex_lock(&mutex);
v2 = *(_QWORD *)ptr;
while ( v2 )
{
free(*(void **)v2);
v3 = (void *)v2;
v2 = *(_QWORD *)(v2 + 8);
free(v3);
}
free(ptr);
result = pthread_mutex_unlock(&mutex);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi
int result; // eax

if ( ptr )
{
pthread_mutex_lock(&mutex);
v2 = *(_QWORD *)ptr;
while ( v2 )
{
free(*(void **)v2);
v3 = (void *)v2;
v2 = *(_QWORD *)(v2 + 8);
free(v3);
}
free(ptr);
result = pthread_mutex_unlock(&mutex);
}
return result;
}

original_code:
void queue_destroy(queue * que)
{
    ;
    if(que == ((void *)0)) {
        return;
    }
    ;
    pthread_mutex_lock(&mutex);
    if(que->first == ((void *)0)) {
        ;
        free(que);
        pthread_mutex_unlock(&mutex);
        return;
    }
    ;
    node * _node = que->first;
    while(_node != ((void *)0)) {
        ;
        free(_node->data);
        node *tmp = _node->next;
        free(_node);
        _node = tmp;
    }
    free(que);
    pthread_mutex_unlock(&mutex);
}

canonical_original_code:
void queue_destroy(struct _end_q *que)
{
    ;
    if(que == ((void *)0)) {
        return;
    }
    ;
    pthread_mutex_lock(&mutex);
    if(que->first == ((void *)0)) {
        ;
        free(que);
        pthread_mutex_unlock(&mutex);
        return;
    }
    ;
    struct _node *_node = que->first;
    while(_node != ((void *)0)) {
        ;
        free(_node->data);
        struct _node *tmp = _node->next;
        free(_node);
        _node = tmp;
    }
    free(que);
    pthread_mutex_unlock(&mutex);
}

variable_types: struct _node { void *data; struct _node *next; } *


enque

decompiled_code:
__int64 __fastcall enque(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
__int64 result; // rax

v2 = malloc(0x10uLL);
if ( v2 )
{
*v2 = a2;
v3 = v2;
v2[1] = 0LL;
pthread_mutex_lock(&mutex);
if ( *a1 )
*(_QWORD *)(a1[1] + 8LL) = v3;
else
*a1 = v3;
a1[1] = v3;
pthread_mutex_unlock(&mutex);
result = 0LL;
}
else
{
fwrite("Malloc failed creating a node\n", 1uLL, 0x1EuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
__int64 result; // rax

v2 = malloc(0x10uLL);
if ( v2 )
{
*v2 = a2;
v3 = v2;
v2[1] = 0LL;
pthread_mutex_lock(&mutex);
if ( *a1 )
*(_QWORD *)(a1[1] + 8LL) = v3;
else
*a1 = v3;
a1[1] = v3;
pthread_mutex_unlock(&mutex);
result = 0LL;
}
else
{
fwrite("Malloc failed creating a node\n", 1uLL, 0x1EuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int enque(queue * que, void * data)
{
    node * new_node = malloc(sizeof(node));
    if(new_node == ((void *)0)) {
        fprintf(stderr, "Malloc failed creating a node\n");
        return -1;
    }
    new_node->data = data;
    new_node->next = ((void *)0);
    pthread_mutex_lock(&mutex);
    if (que->first == ((void *)0)) {
        que->first = new_node;
        que->last = new_node;
    } else {
        que->last->next = new_node;
        que->last = new_node;
    }
    pthread_mutex_unlock(&mutex);
    return 0;
}

canonical_original_code:
int enque(struct _end_q *que, void *data)
{
    struct _node *new_node = malloc(sizeof(node));
    if(new_node == ((void *)0)) {
        fprintf(stderr, "Malloc failed creating a node\n");
        return -1;
    }
    new_node->data = data;
    new_node->next = ((void *)0);
    pthread_mutex_lock(&mutex);
    if (que->first == ((void *)0)) {
        que->first = new_node;
        que->last = new_node;
    } else {
        que->last->next = new_node;
        que->last = new_node;
    }
    pthread_mutex_unlock(&mutex);
    return 0;
}

variable_types:
data
void *

user_defined_types: struct _node { void *data; struct _node *next; }


deque

decompiled_code:
__int64 __fastcall deque(__int64 a1)
{
__int64 *v2; // rdi
__int64 v3; // r12

if ( !a1 )
return 0LL;
pthread_mutex_lock(&mutex);
v2 = *(__int64 **)a1;
if ( v2 )
{
if ( v2 == *(__int64 **)(a1 + 8) )
{
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}
else
{
*(_QWORD *)a1 = v2[1];
}
v3 = *v2;
free(v2);
pthread_mutex_unlock(&mutex);
}
else
{
v3 = 0LL;
pthread_mutex_unlock(&mutex);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 *v2; // rdi
__int64 v3; // r12

if ( !a1 )
return 0LL;
pthread_mutex_lock(&mutex);
v2 = *(__int64 **)a1;
if ( v2 )
{
if ( v2 == *(__int64 **)(a1 + 8) )
{
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}
else
{
*(_QWORD *)a1 = v2[1];
}
v3 = *v2;
free(v2);
pthread_mutex_unlock(&mutex);
}
else
{
v3 = 0LL;
pthread_mutex_unlock(&mutex);
}
return v3;
}

original_code:
void * deque(queue * que)
{
    ;
    if (que == ((void *)0)) {
        ;
        return ((void *)0);
    }
    pthread_mutex_lock(&mutex);
    if (que->first == ((void *)0)) {
        pthread_mutex_unlock(&mutex);
        ;
        return ((void *)0);
    }
    void * data;
    node * _node = que->first;
    if (que->first == que->last) {
        que->first = ((void *)0);
        que->last = ((void *)0);
    } else {
        que->first = _node->next;
    }
    data = _node->data;
    ;
    free(_node);
    pthread_mutex_unlock(&mutex);
    ;
    return data;
}

canonical_original_code:
void *deque(struct _end_q *que)
{
    ;
    if (que == ((void *)0)) {
        ;
        return ((void *)0);
    }
    pthread_mutex_lock(&mutex);
    if (que->first == ((void *)0)) {
        pthread_mutex_unlock(&mutex);
        ;
        return ((void *)0);
    }
    void *data;
    struct _node *_node = que->first;
    if (que->first == que->last) {
        que->first = ((void *)0);
        que->last = ((void *)0);
    } else {
        que->first = _node->next;
    }
    data = _node->data;
    ;
    free(_node);
    pthread_mutex_unlock(&mutex);
    ;
    return data;
}

return_type: void *


func

decompiled_code:
void *__fastcall func(void *a1)
{
int v1; // ebx
_QWORD *v2; // r12
unsigned int v3; // eax
int v4; // er13
char *v5; // rbp
unsigned int v6; // eax

v1 = *(_DWORD *)a1;
v2 = (_QWORD *)*((_QWORD *)a1 + 1);
v3 = time(0LL);
srand(v3);
if ( v1 > 0 )
{
v4 = 0;
do
{
++v4;
v5 = (char *)malloc(0x10uLL);
v6 = rand();
snprintf(v5, 0xFuLL, "rand: %d", v6);
enque(v2, (__int64)v5);
}
while ( v1 != v4 );
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func11(void *a1)
{
int v1; // ebx
_QWORD *v2; // r12
unsigned int v3; // eax
int v4; // er13
char *v5; // rbp
unsigned int v6; // eax

v1 = *(_DWORD *)a1;
v2 = (_QWORD *)*((_QWORD *)a1 + 1);
v3 = time(0LL);
srand(v3);
if ( v1 > 0 )
{
v4 = 0;
do
{
++v4;
v5 = (char *)malloc(0x10uLL);
v6 = rand();
snprintf(v5, 0xFuLL, "rand: %d", v6);
func9(v2, (__int64)v5);
}
while ( v1 != v4 );
}
return 0LL;
}

original_code:
void * func(void * arg)
{
    struct _stru * args = (struct _stru *) arg;
    int number = args->number;
    queue * q = args->q;
    int i;
    srand(time(((void *)0)));
    for(i = 0; i < number; i++) {
        char * message = malloc(16);
        snprintf(message, 15, "rand: %d", rand());
        enque(q, (void *)message);
    }
    return ((void *)0);
}

canonical_original_code:
void *func(void *arg)
{
    struct _stru *args = (struct _stru *) arg;
    int number = args->number;
    struct _end_q *q = args->q;
    int i;
    srand(time(((void *)0)));
    for(i = 0; i < number; i++) {
        char *message = malloc(16);
        snprintf(message, 15, "rand: %d", rand());
        enque(q, (void *)message);
    }
    return ((void *)0);
}

variable_types:
arg
void *

variable_types: struct _stru { int number; int thread_no; struct _end_q { struct _node { void *data; struct _node *next; } *first; struct _node *last; } *q; } *

variable_types: number int

variable_types:
message
char *


decompiled_code:
void *__fastcall func_d(void *a1)
{
const char *v1; // rax
char *v2; // rbp

puts("Func_D running....");
while ( 1 )
{
v1 = (const char *)deque((__int64)a1);
v2 = (char *)v1;
if ( !v1 )
break;
printf("DeQued : %s, @%p\n", v1, v1);
free(v2);
}
puts("Func_D exiting....");
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func12(void *a1)
{
const char *v1; // rax
char *v2; // rbp

puts("Func_D running....");
while ( 1 )
{
v1 = (const char *)func10((__int64)a1);
v2 = (char *)v1;
if ( !v1 )
break;
printf("DeQued : %s, @%p\n", v1, v1);
free(v2);
}
puts("Func_D exiting....");
return 0LL;
}

original_code:
void * func_d(void * args)
{
    printf("Func_D running....\n");
    queue * q = (queue *) args;
    void * data;
    while((data = deque(q)) != ((void *)0)) {
        char * string = (char *)data;
        printf("DeQued : %s, @%p\n", string, data);
        free(data);
    }
    printf("Func_D exiting....\n");
    return ((void *)0);
}

canonical_original_code:
void *func_d(void *args)
{
    printf("Func_D running....\n");
    struct _end_q *q = (queue *) args;
    void *data;
    while((data = deque(q)) != ((void *)0)) {
        char *string = (char *)data;
        printf("DeQued : %s, @%p\n", string, data);
        free(data);
    }
    printf("Func_D exiting....\n");
    return ((void *)0);
}

variable_types:
args
void *

variable_types:
string
char *


repo: reagent/buffer

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

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

original_code:
int
buffer_strlen(Buffer *buf)
{
    return buf->bytes_used;
}

canonical_original_code:
int buffer_strlen(struct Buffer *buf)
{
    return buf->bytes_used;
}

variable_types: struct Buffer { char *contents; int bytes_used; int total_size; } *


decompiled_code:
void __fastcall buffer_free(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void
buffer_free(Buffer *buf)
{
    free(buf->contents);
    free(buf);
}

canonical_original_code:
void buffer_free(struct Buffer *buf)
{
    free(buf->contents);
    free(buf);
}

user_defined_types: struct Buffer { char *contents; int bytes_used; int total_size; }


decompiled_code:
_BOOL8 __fastcall buffer_has_space(__int64 a1, int a2)
{
return *(_DWORD *)(a1 + 12) - *(_DWORD *)(a1 + 8) >= a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, int a2)
{
return *(_DWORD *)(a1 + 12) - *(_DWORD *)(a1 + 8) >= a2;
}

original_code:
int
buffer_has_space(Buffer *buf, int desired_length)
{
    int bytes_remaining = buf->total_size - buf->bytes_used;
    ;
    return desired_length <= bytes_remaining;
}

canonical_original_code:
int buffer_has_space(struct Buffer *buf, int desired_length)
{
    int bytes_remaining = buf->total_size - buf->bytes_used;
    ;
    return desired_length <= bytes_remaining;
}


decompiled_code:
__int64 __fastcall buffer_grow(__int64 a1, int a2)
{
int v4; // esi
void *v5; // rdi
void *v6; // rax

v4 = *(_DWORD *)(a1 + 12);
v5 = *(void **)a1;
if ( v4 < a2 )
v4 = a2;
v6 = realloc(v5, 2 * v4);
if ( !v6 )
return 0xFFFFFFFFLL;
*(_DWORD *)(a1 + 12) = 2 * v4;
*(_QWORD *)a1 = v6;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
int v4; // esi
void *v5; // rdi
void *v6; // rax

v4 = *(_DWORD *)(a1 + 12);
v5 = *(void **)a1;
if ( v4 < a2 )
v4 = a2;
v6 = realloc(v5, 2 * v4);
if ( !v6 )
return 0xFFFFFFFFLL;
*(_DWORD *)(a1 + 12) = 2 * v4;
*(_QWORD *)a1 = v6;
return 0LL;
}

original_code:
int
buffer_grow(Buffer *buf, int minimum_size)
{
    int factor = buf->total_size;
    if (factor < minimum_size) {
        factor = minimum_size;
    }
    int new_size = factor * 2;
    ;
    char *tmp = realloc(buf->contents, new_size * sizeof(char));
    if (tmp == ((void *)0)) { goto error; };
    buf->contents = tmp;
    buf->total_size = new_size;
    return 0;
error:
    return -1;
}

canonical_original_code:
int buffer_grow(struct Buffer *buf, int minimum_size)
{
    int factor = buf->total_size;
    if (factor < minimum_size) {
        factor = minimum_size;
    }
    int new_size = factor * 2;
    ;
    char *tmp = realloc(buf->contents, new_size * sizeof(char));
    if (tmp == ((void *)0)) { goto error; };
    buf->contents = tmp;
    buf->total_size = new_size;
    return 0;
error:
    return -1;
}

variable_types: factor int

variable_types:
tmp
char *


decompiled_code:
__int64 __fastcall buffer_append(__int64 *a1, _BYTE *a2, int a3)
{
int v3; // er13

v3 = a3 + 1;
if ( !buffer_has_space((__int64)a1, a3 + 1) && (unsigned int)buffer_grow((__int64)a1, v3) )
return 0xFFFFFFFFLL;
buffer_cat(a1, a2, a3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 *a1, _BYTE *a2, int a3)
{
int v3; // er13

v3 = a3 + 1;
if ( !func2((__int64)a1, a3 + 1) && (unsigned int)func3((__int64)a1, v3) )
return 0xFFFFFFFFLL;
func4(a1, a2, a3);
return 0LL;
}

original_code:
int
buffer_append(Buffer *buf, char *append, int length)
{
    int status = 0;
    int desired_length = length + 1;
    if (!buffer_has_space(buf, desired_length)) {
        status = buffer_grow(buf, desired_length);
        if (!(status == 0)) { goto error; }
    }
    buffer_cat(buf, append, length);
    return 0;
error:
    return -1;
}

canonical_original_code:
int buffer_append(struct Buffer *buf, char *append, int length)
{
    int status = 0;
    int desired_length = length + 1;
    if (!buffer_has_space(buf, desired_length)) {
        status = buffer_grow(buf, desired_length);
        if (!(status == 0)) { goto error; }
    }
    buffer_cat(buf, append, length);
    return 0;
error:
    return -1;
}

variable_types:
append
char *

variable_types: length int

variable_types: status int


decompiled_code:
__int64 buffer_appendf(__int64 *a1, const char *a2, ...)
{
int v2; // edx
__int64 result; // rax
void *ptr; // [rsp+0h] [rbp-D8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
ptr = 0LL;
v2 = vasprintf((char **)&ptr, a2, arg);
if ( v2 < 0 || (unsigned int)buffer_append(a1, ptr, v2) )
{
if ( ptr )
free(ptr);
result = 0xFFFFFFFFLL;
}
else
{
free(ptr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func6(__int64 *a1, const char *a2, ...)
{
int v2; // edx
__int64 result; // rax
void *ptr; // [rsp+0h] [rbp-D8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
ptr = 0LL;
v2 = vasprintf((char **)&ptr, a2, arg);
if ( v2 < 0 || (unsigned int)func5(a1, ptr, v2) )
{
if ( ptr )
free(ptr);
result = 0xFFFFFFFFLL;
}
else
{
free(ptr);
result = 0LL;
}
return result;
}

original_code:
int
buffer_appendf(Buffer *buf, const char *format, ...)
{
    char *tmp = ((void *)0);
    int bytes_written, status;
    va_list argp;
    __builtin_va_start(argp,format);
    bytes_written = vasprintf(&tmp, format, argp);
    if (bytes_written < 0) { goto error; };
    __builtin_va_end(argp);
    status = buffer_append(buf, tmp, bytes_written);
    if (!(status == 0)) { goto error; };
    free(tmp);
    return 0;
error:
    if (tmp != ((void *)0)) { free(tmp); }
    return -1;
}

canonical_original_code:
int buffer_appendf(struct Buffer *buf, char *format, ...)
{
    char *tmp = ((void *)0);
    int bytes_written; int status;
    __builtin_va_list argp;
    __builtin_va_start(argp,format);
    bytes_written = vasprintf(&tmp, format, argp);
    if (bytes_written < 0) { goto error; };
    __builtin_va_end(argp);
    status = buffer_append(buf, tmp, bytes_written);
    if (!(status == 0)) { goto error; };
    free(tmp);
    return 0;
error:
    if (tmp != ((void *)0)) { free(tmp); }
    return -1;
}

variable_types:
format
char *


decompiled_code:
__int64 buffer_nappendf(__int64 *a1, int a2, const char *a3, ...)
{
char *v4; // rax
char *v5; // rbp
gcc_va_list arg; // [rsp+18h] [rbp-F0h] BYREF

v4 = (char *)calloc(1uLL, a2 + 1);
v5 = v4;
if ( !v4 )
return 0xFFFFFFFFLL;
va_start(arg, a3);
if ( vsnprintf(v4, a2 + 1, a3, arg) < 0 || (unsigned int)buffer_append(a1, v5, a2) )
{
free(v5);
return 0xFFFFFFFFLL;
}
free(v5);
return 0LL;
}

canonical_decompiled_code:
__int64 func7(__int64 *a1, int a2, const char *a3, ...)
{
char *v4; // rax
char *v5; // rbp
gcc_va_list arg; // [rsp+18h] [rbp-F0h] BYREF

v4 = (char *)calloc(1uLL, a2 + 1);
v5 = v4;
if ( !v4 )
return 0xFFFFFFFFLL;
va_start(arg, a3);
if ( vsnprintf(v4, a2 + 1, a3, arg) < 0 || (unsigned int)func5(a1, v5, a2) )
{
free(v5);
return 0xFFFFFFFFLL;
}
free(v5);
return 0LL;
}

original_code:
int
buffer_nappendf(Buffer *buf, size_t length, const char *format, ...)
{
    int status = 0,
        printf_length = length + 1;
    char *tmp = calloc(1, printf_length * sizeof(char));
    if (tmp == ((void *)0)) { goto error; };
    va_list argp;
    __builtin_va_start(argp,format);
    status = vsnprintf(tmp, printf_length, format, argp);
    if (status < 0) { goto error; };
    __builtin_va_end(argp);
    status = buffer_append(buf, tmp, length);
    if (!(status == 0)) { goto error; };
    free(tmp);
    return 0;
error:
    if (tmp != ((void *)0)) { free(tmp); }
    return -1;
}

canonical_original_code:
int buffer_nappendf(struct Buffer *buf, unsigned long length, char *format, ...)
{
    int status = 0; int printf_length = length + 1;
    char *tmp = calloc(1, printf_length * sizeof(char));
    if (tmp == ((void *)0)) { goto error; };
    __builtin_va_list argp;
    __builtin_va_start(argp,format);
    status = vsnprintf(tmp, printf_length, format, argp);
    if (status < 0) { goto error; };
    __builtin_va_end(argp);
    status = buffer_append(buf, tmp, length);
    if (!(status == 0)) { goto error; };
    free(tmp);
    return 0;
error:
    if (tmp != ((void *)0)) { free(tmp); }
    return -1;
}

variable_types:
length
unsigned long


decompiled_code:
char *__fastcall buffer_to_s(__int64 a1)
{
char *v1; // r12
int v2; // eax

v1 = (char *)calloc(1uLL, *(_DWORD *)(a1 + 8) + 1);
v2 = buffer_strlen(a1);
return strncpy(v1, *(const char **)a1, v2);
}

canonical_decompiled_code:
char *__fastcall func8(__int64 a1)
{
char *v1; // r12
int v2; // eax

v1 = (char *)calloc(1uLL, *(_DWORD *)(a1 + 8) + 1);
v2 = func0(a1);
return strncpy(v1, *(const char **)a1, v2);
}

original_code:
char *
buffer_to_s(Buffer *buf)
{
    char *result = calloc(1, buf->bytes_used + 1);
    strncpy(result, buf->contents, buffer_strlen(buf));
    return result;
}

canonical_original_code:
char *buffer_to_s(struct Buffer *buf)
{
    char *result = calloc(1, buf->bytes_used + 1);
    strncpy(result, buf->contents, buffer_strlen(buf));
    return result;
}

variable_types:
result
char *

return_type: char *


repo: reagent/encode

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

result = 97LL;
if ( (_BYTE)a1 != 90 )
{
result = (unsigned int)(a1 + 1);
if ( (_BYTE)a1 == 122 )
result = 65LL;
}
return result;
}

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

result = 97LL;
if ( (_BYTE)a1 != 90 )
{
result = (unsigned int)(a1 + 1);
if ( (_BYTE)a1 == 122 )
result = 65LL;
}
return result;
}

original_code:
char
char_next(char source)
{
    char next = '\0';
    if(source == 'Z') {
        next = 'a';
    } else if(source == 'z') {
        next = 'A';
    } else {
        next = (char) source + 1;
    }
    return next;
}

variable_types: source char

variable_types: next char

return_type: char


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

result = 90LL;
if ( (_BYTE)a1 != 97 )
{
result = (unsigned int)(a1 - 1);
if ( (_BYTE)a1 == 65 )
result = 122LL;
}
return result;
}

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

result = 90LL;
if ( (_BYTE)a1 != 97 )
{
result = (unsigned int)(a1 - 1);
if ( (_BYTE)a1 == 65 )
result = 122LL;
}
return result;
}

original_code:
char
char_previous(char source)
{
    char previous = '\0';
    if(source == 'a') {
        previous = 'Z';
    } else if(source == 'A') {
        previous = 'z';
    } else {
        previous = (char) source - 1;
    }
    return previous;
}

variable_types: previous char


decompiled_code:
__int64 __fastcall char_between(char a1, char a2, char a3)
{
return (a1 >= a2) & (unsigned __int8)(a1 <= a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1, char a2, char a3)
{
return (a1 >= a2) & (unsigned __int8)(a1 <= a3);
}

original_code:
int
char_between(char character, char lower_limit, char upper_limit)
{
    return character >= lower_limit && character <= upper_limit;
}

variable_types: character char


decompiled_code:
_BOOL8 __fastcall char_can_shift(char a1)
{
int v1; // er8
_BOOL8 result; // rax

v1 = char_between(a1, 65, 90);
result = 1LL;
if ( !v1 )
result = (unsigned int)char_between(a1, 97, 122) != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(char a1)
{
int v1; // er8
_BOOL8 result; // rax

v1 = func2(a1, 65, 90);
result = 1LL;
if ( !v1 )
result = (unsigned int)func2(a1, 97, 122) != 0;
return result;
}

original_code:
int
char_can_shift(char source)
{
    return char_between(source, 'A', 'Z') || char_between(source, 'a', 'z');
}


decompiled_code:
__int64 __fastcall char_shift(char a1, int a2)
{
int v2; // ebp
_BOOL4 v3; // eax
unsigned int v4; // er9
__int64 (__fastcall *v5)(int); // r12
int v6; // ebx

v2 = a2;
v3 = char_can_shift(a1);
if ( a2 < 0 )
{
if ( !v3 )
return v4;
v2 = -a2;
v5 = char_previous;
goto LABEL_5;
}
if ( v3 && a2 )
{
v5 = char_next;
LABEL_5:
v6 = 0;
do
{
++v6;
v4 = v5((char)v4);
}
while ( v6 < v2 );
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1, int a2)
{
int v2; // ebp
_BOOL4 v3; // eax
unsigned int v4; // er9
__int64 (__fastcall *v5)(int); // r12
int v6; // ebx

v2 = a2;
v3 = func3(a1);
if ( a2 < 0 )
{
if ( !v3 )
return v4;
v2 = -a2;
v5 = char_previous;
goto LABEL_5;
}
if ( v3 && a2 )
{
v5 = char_next;
LABEL_5:
v6 = 0;
do
{
++v6;
v4 = v5((char)v4);
}
while ( v6 < v2 );
}
return v4;
}

original_code:
char
char_shift(char source, int offset)
{
    int position = 0;
    char shifted = source;
    shift_cb shift = (offset < 0) ? char_previous : char_next;
    if(char_can_shift(source)) {
        for(position = 0; position < abs(offset); position++) {
            shifted = shift(shifted);
        }
    }
    return shifted;
}

canonical_original_code:
char char_shift(char source, int offset)
{
    int position = 0;
    char shifted = source;
    char (*shift)(char source) = (offset < 0) ? char_previous : char_next;
    if(char_can_shift(source)) {
        for(position = 0; position < abs(offset); position++) {
            shifted = shift(shifted);
        }
    }
    return shifted;
}

variable_types: offset int

variable_types: position int

variable_types: shifted char

variable_types:
shift
char (*)(char source)


repo: reagent/ggvm

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

if ( a2 == 2 )
return *(_QWORD *)(*(_QWORD *)a1 + 40LL) != 0LL;
if ( a2 > 2 )
{
if ( a2 == 3 )
result = *(_QWORD *)(*(_QWORD *)a1 + 48LL) != 0LL;
else
result = 0LL;
}
else if ( a2 )
{
result = *(_QWORD *)(*(_QWORD *)a1 + 32LL) != 0LL;
}
else
{
result = *(_QWORD *)(*(_QWORD *)a1 + 24LL) != 0LL;
}
return result;
}

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

if ( a2 == 2 )
return *(_QWORD *)(*(_QWORD *)a1 + 40LL) != 0LL;
if ( a2 > 2 )
{
if ( a2 == 3 )
result = *(_QWORD *)(*(_QWORD *)a1 + 48LL) != 0LL;
else
result = 0LL;
}
else if ( a2 )
{
result = *(_QWORD *)(*(_QWORD *)a1 + 32LL) != 0LL;
}
else
{
result = *(_QWORD *)(*(_QWORD *)a1 + 24LL) != 0LL;
}
return result;
}

original_code:
int map_has_location_at(Map *map, Direction direction)
{
  int has_location = 0;
  switch(direction) {
    case NORTH:
      has_location = map->location->north != ((void *)0);
      break;
    case SOUTH:
      has_location = map->location->south != ((void *)0);
      break;
    case EAST:
      has_location = map->location->east != ((void *)0);
      break;
    case WEST:
      has_location = map->location->west != ((void *)0);
      break;
  }
  return has_location;
}

canonical_original_code:
int map_has_location_at(struct Map *map, enum Direction direction)
{
  int has_location = 0;
  switch(direction) {
    case NORTH:
      has_location = map->location->north != ((void *)0);
      break;
    case SOUTH:
      has_location = map->location->south != ((void *)0);
      break;
    case EAST:
      has_location = map->location->east != ((void *)0);
      break;
    case WEST:
      has_location = map->location->west != ((void *)0);
      break;
  }
  return has_location;
}

variable_types: struct Map { struct Room { char *name; struct Monster { char *name; int health; } *monster; struct Weapon { char *name; int damage; } *weapon; struct Room *north; struct Room *south; struct Room *east; struct Room *west; } *location; struct Player { int health; struct Weapon *weapon; } *player; } *

variable_types: enum Direction { NORTH=0, SOUTH=1, EAST=2, WEST=3 }

user_defined_types: struct Map { struct Room *location; struct Player *player; }

user_defined_types: struct Room { char *name; struct Monster *monster; struct Weapon *weapon; struct Room *north; struct Room *south; struct Room *east; struct Room *west; }


decompiled_code:
__int64 __fastcall map_equip(_QWORD *a1)
{
return room_equip(*a1, a1[1]);
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
return room_equip(*a1, a1[1]);
}

original_code:
void map_equip(Map *map)
{
  room_equip(map->location, map->player);
}

canonical_original_code:
void map_equip(struct Map *map)
{
  room_equip(map->location, map->player);
}

user_defined_types: struct Player { int health; struct Weapon *weapon; }

user_defined_types: struct Monster { char *name; int health; }

user_defined_types: struct Weapon { char *name; int damage; }


decompiled_code:
__int64 __fastcall map_attack(_QWORD *a1)
{
return room_attack(*a1, a1[1]);
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
return room_attack(*a1, a1[1]);
}

original_code:
void map_attack(Map *map)
{
  room_attack(map->location, map->player);
}

canonical_original_code:
void map_attack(struct Map *map)
{
  room_attack(map->location, map->player);
}


decompiled_code:
int __fastcall map_print_directions(__int64 a1)
{
_QWORD *v1; // rax

puts("You can go:");
v1 = *(_QWORD **)a1;
if ( *(_QWORD *)(*(_QWORD *)a1 + 24LL) )
{
puts("* NORTH");
v1 = *(_QWORD **)a1;
}
if ( v1[4] )
{
puts("* SOUTH");
v1 = *(_QWORD **)a1;
}
if ( v1[5] )
{
puts("* EAST");
v1 = *(_QWORD **)a1;
}
if ( v1[6] )
LODWORD(v1) = puts("* WEST");
return (int)v1;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
_QWORD *v1; // rax

puts("You can go:");
v1 = *(_QWORD **)a1;
if ( *(_QWORD *)(*(_QWORD *)a1 + 24LL) )
{
puts("* NORTH");
v1 = *(_QWORD **)a1;
}
if ( v1[4] )
{
puts("* SOUTH");
v1 = *(_QWORD **)a1;
}
if ( v1[5] )
{
puts("* EAST");
v1 = *(_QWORD **)a1;
}
if ( v1[6] )
LODWORD(v1) = puts("* WEST");
return (int)v1;
}

original_code:
void map_print_directions(Map *map)
{
  puts("You can go:");
  if(map->location->north) {
    puts("* NORTH");
  }
  if(map->location->south) {
    puts("* SOUTH");
  }
  if(map->location->east) {
    puts("* EAST");
  }
  if(map->location->west) {
    puts("* WEST");
  }
}

canonical_original_code:
void map_print_directions(struct Map *map)
{
  puts("You can go:");
  if(map->location->north) {
    puts("* NORTH");
  }
  if(map->location->south) {
    puts("* SOUTH");
  }
  if(map->location->east) {
    puts("* EAST");
  }
  if(map->location->west) {
    puts("* WEST");
  }
}


decompiled_code:
int __fastcall map_move(_QWORD *a1, unsigned int a2)
{
_QWORD *v3; // rdi
int result; // eax
__int64 v5; // rdi
__int64 v6; // rdi

if ( map_has_location_at((__int64)a1, a2) )
{
v3 = (_QWORD *)*a1;
if ( a2 == 2 )
{
v6 = v3[5];
*a1 = v6;
result = room_print_description(v6);
}
else
{
if ( a2 > 2 )
{
if ( a2 == 3 )
{
v5 = v3[6];
*a1 = v5;
return room_print_description(v5);
}
}
else
{
if ( a2 )
v3 = (_QWORD *)v3[4];
else
v3 = (_QWORD *)v3[3];
*a1 = v3;
}
result = room_print_description(v3);
}
}
else
{
puts("You cannot go there.");
result = map_print_directions((__int64)a1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(_QWORD *a1, unsigned int a2)
{
_QWORD *v3; // rdi
int result; // eax
__int64 v5; // rdi
__int64 v6; // rdi

if ( func0((__int64)a1, a2) )
{
v3 = (_QWORD *)*a1;
if ( a2 == 2 )
{
v6 = v3[5];
*a1 = v6;
result = room_print_description(v6);
}
else
{
if ( a2 > 2 )
{
if ( a2 == 3 )
{
v5 = v3[6];
*a1 = v5;
return room_print_description(v5);
}
}
else
{
if ( a2 )
v3 = (_QWORD *)v3[4];
else
v3 = (_QWORD *)v3[3];
*a1 = v3;
}
result = room_print_description(v3);
}
}
else
{
puts("You cannot go there.");
result = func3((__int64)a1);
}
return result;
}

original_code:
void map_move(Map *map, Direction direction)
{
  if(map_has_location_at(map, direction)) {
    switch(direction) {
      case NORTH:
        map->location = map->location->north;
        break;
      case SOUTH:
        map->location = map->location->south;
        break;
      case EAST:
        map->location = map->location->east;
        break;
      case WEST:
        map->location = map->location->west;
        break;
    }
    room_print_description(map->location);
  } else {
    puts("You cannot go there.");
    map_print_directions(map);
  }
}

canonical_original_code:
void map_move(struct Map *map, enum Direction direction)
{
  if(map_has_location_at(map, direction)) {
    switch(direction) {
      case NORTH:
        map->location = map->location->north;
        break;
      case SOUTH:
        map->location = map->location->south;
        break;
      case EAST:
        map->location = map->location->east;
        break;
      case WEST:
        map->location = map->location->west;
        break;
    }
    room_print_description(map->location);
  } else {
    puts("You cannot go there.");
    map_print_directions(map);
  }
}

user_defined_types: enum Direction { NORTH=0, SOUTH=1, EAST=2, WEST=3 }


decompiled_code:
__int64 __fastcall map_destroy(void *a1)
{
free(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(void *a1)
{
free(a1);
return 0LL;
}

original_code:
int map_destroy(Map *map)
{
  free(map);
  return 0;
}

canonical_original_code:
int map_destroy(struct Map *map)
{
  free(map);
  return 0;
}


decompiled_code:
int __fastcall room_equip(__int64 a1, __int64 a2)
{
int result; // eax

if ( !*(_QWORD *)(a1 + 16) )
return puts("You search the room and find no weapons.");
result = player_equip(a2);
*(_QWORD *)(a1 + 16) = 0LL;
return result;
}

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

if ( !*(_QWORD *)(a1 + 16) )
return puts("You search the room and find no weapons.");
result = player_equip(a2);
*(_QWORD *)(a1 + 16) = 0LL;
return result;
}

original_code:
void room_equip(Room *room, Player *player)
{
  if (room->weapon != ((void *)0)) {
    player_equip(player, room->weapon);
    room->weapon = ((void *)0);
  } else {
    printf("You search the room and find no weapons.\n");
  }
}

canonical_original_code:
void room_equip(struct Room *room, struct Player *player)
{
  if (room->weapon != ((void *)0)) {
    player_equip(player, room->weapon);
    room->weapon = ((void *)0);
  } else {
    printf("You search the room and find no weapons.\n");
  }
}

variable_types: struct Room { char *name; struct Monster { char *name; int health; } *monster; struct Weapon { char *name; int damage; } *weapon; struct Room *north; struct Room *south; struct Room *east; struct Room *west; } *

variable_types: struct Player { int health; struct Weapon *weapon; } *


decompiled_code:
int __fastcall room_attack(__int64 a1, __int64 a2)
{
int result; // eax

if ( !*(_QWORD *)(a1 + 8) )
return puts("You flail in the air at nothing.");
result = player_attack(a2);
if ( !result )
result = monster_attack(*(_QWORD *)(a1 + 8), a2);
return result;
}

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

if ( !*(_QWORD *)(a1 + 8) )
return puts("You flail in the air at nothing.");
result = player_attack(a2);
if ( !result )
result = monster_attack(*(_QWORD *)(a1 + 8), a2);
return result;
}

original_code:
void room_attack(Room *room, Player *player)
{
  int killed = 0;
  if (room->monster != ((void *)0)) {
    killed = player_attack(player, room->monster);
    if (!killed) {
      monster_attack(room->monster, player);
    }
  } else {
    printf("You flail in the air at nothing.\n");
  }
}

canonical_original_code:
void room_attack(struct Room *room, struct Player *player)
{
  int killed = 0;
  if (room->monster != ((void *)0)) {
    killed = player_attack(player, room->monster);
    if (!killed) {
      monster_attack(room->monster, player);
    }
  } else {
    printf("You flail in the air at nothing.\n");
  }
}

variable_types: killed int


decompiled_code:
int __fastcall room_print_description(__int64 a1)
{
const char **v1; // rax
const char **v2; // rax

printf("You have entered the %s\n", *(const char **)a1);
v1 = *(const char ***)(a1 + 8);
if ( v1 )
printf("You encounter the %s!\n", *v1);
v2 = *(const char ***)(a1 + 16);
if ( v2 )
LODWORD(v2) = printf("You see the %s in the corner of the room.\n", *v2);
return (int)v2;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
const char **v1; // rax
const char **v2; // rax

printf("You have entered the %s\n", *(const char **)a1);
v1 = *(const char ***)(a1 + 8);
if ( v1 )
printf("You encounter the %s!\n", *v1);
v2 = *(const char ***)(a1 + 16);
if ( v2 )
LODWORD(v2) = printf("You see the %s in the corner of the room.\n", *v2);
return (int)v2;
}

original_code:
void room_print_description(Room *room)
{
  printf("You have entered the %s\n", room->name);
  if(room->monster != ((void *)0)) {
    printf("You encounter the %s!\n", room->monster->name);
  }
  if(room->weapon != ((void *)0)) {
    printf("You see the %s in the corner of the room.\n", room->weapon->name);
  }
}

canonical_original_code:
void room_print_description(struct Room *room)
{
  printf("You have entered the %s\n", room->name);
  if(room->monster != ((void *)0)) {
    printf("You encounter the %s!\n", room->monster->name);
  }
  if(room->weapon != ((void *)0)) {
    printf("You see the %s in the corner of the room.\n", room->weapon->name);
  }
}


decompiled_code:
__int64 __fastcall room_destroy(void *ptr)
{
free(*(void **)ptr);
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(void *ptr)
{
free(*(void **)ptr);
free(ptr);
return 0LL;
}

original_code:
int room_destroy(Room *room)
{
  free(room->name);
  free(room);
  return 0;
}

canonical_original_code:
int room_destroy(struct Room *room)
{
  free(room->name);
  free(room);
  return 0;
}


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

v2 = rand();
v3 = rand();
if ( v2 % 2 != 1 )
return puts("The monster lunges, but misses you by mere inches.");
if ( *a2 <= 0 )
return puts("Game over.");
v4 = v3 % 5 + 1;
printf("The monster lands a powerful attack. It does %d damage.\n", v4);
result = *a2 - v4;
*a2 = result;
if ( result <= 0 )
result = printf("You have been killed by the %s!\n", *a1);
return result;
}

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

v2 = rand();
v3 = rand();
if ( v2 % 2 != 1 )
return puts("The monster lunges, but misses you by mere inches.");
if ( *a2 <= 0 )
return puts("Game over.");
v4 = v3 % 5 + 1;
printf("The monster lands a powerful attack. It does %d damage.\n", v4);
result = *a2 - v4;
*a2 = result;
if ( result <= 0 )
result = printf("You have been killed by the %s!\n", *a1);
return result;
}

original_code:
void monster_attack(Monster *monster, Player *player)
{
  int hit = rand() % 2;
  int damage = (rand() % 5) + 1;
  if (hit == 1) {
    if (player->health <= 0) {
      printf("Game over.\n");
    } else {
      printf("The monster lands a powerful attack. It does %d damage.\n", damage);
      player->health -= damage;
      if (player->health <= 0) {
        printf("You have been killed by the %s!\n", monster->name);
      }
    }
  } else {
    printf("The monster lunges, but misses you by mere inches.\n");
  }
}

canonical_original_code:
void monster_attack(struct Monster *monster, struct Player *player)
{
  int hit = rand() % 2;
  int damage = (rand() % 5) + 1;
  if (hit == 1) {
    if (player->health <= 0) {
      printf("Game over.\n");
    } else {
      printf("The monster lands a powerful attack. It does %d damage.\n", damage);
      player->health -= damage;
      if (player->health <= 0) {
        printf("You have been killed by the %s!\n", monster->name);
      }
    }
  } else {
    printf("The monster lunges, but misses you by mere inches.\n");
  }
}

variable_types: struct Monster { char *name; int health; } *

variable_types: struct Player { int health; struct Weapon { char *name; int damage; } *weapon; } *

variable_types: hit int

variable_types: damage int


decompiled_code:
__int64 __fastcall monster_destroy(void *ptr)
{
free(*(void **)ptr);
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *ptr)
{
free(*(void **)ptr);
free(ptr);
return 0LL;
}

original_code:
int monster_destroy(Monster *monster)
{
  free(monster->name);
  free(monster);
  return 0;
}

canonical_original_code:
int monster_destroy(struct Monster *monster)
{
  free(monster->name);
  free(monster);
  return 0;
}


decompiled_code:
_QWORD *player_create()
{
_QWORD *result; // rax

result = malloc(0x10uLL);
*(_DWORD *)result = 10;
result[1] = 0LL;
return result;
}

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

result = malloc(0x10uLL);
*(_DWORD *)result = 10;
result[1] = 0LL;
return result;
}

original_code:
Player *player_create(char *name)
{
  Player *player = malloc(sizeof(Player));
  player->health = 10;
  player->weapon = ((void *)0);
  return player;
}

canonical_original_code:
struct Player *player_create(char *name)
{
  struct Player *player = malloc(sizeof(Player));
  player->health = 10;
  player->weapon = ((void *)0);
  return player;
}

variable_types:
name
char *

return_type: struct Player { int health; struct Weapon { char *name; int damage; } *weapon; } *


decompiled_code:
int __fastcall player_equip(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 8) = a2;
return printf(
"You have picked up the %s, it can do a maximum of %d damage.\n",
*(const char **)a2,
*(unsigned int *)(a2 + 8));
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 8) = a2;
return printf(
"You have picked up the %s, it can do a maximum of %d damage.\n",
*(const char **)a2,
*(unsigned int *)(a2 + 8));
}

original_code:
void player_equip(Player *player, Weapon *weapon)
{
  player->weapon = weapon;
  printf("You have picked up the %s, it can do a maximum of %d damage.\n",
         player->weapon->name,
         player->weapon->damage);
}

canonical_original_code:
void player_equip(struct Player *player, struct Weapon *weapon)
{
  player->weapon = weapon;
  printf("You have picked up the %s, it can do a maximum of %d damage.\n",
         player->weapon->name,
         player->weapon->damage);
}

variable_types:
weapon
struct Weapon *


decompiled_code:
__int64 __fastcall player_attack(__int64 a1, const char **a2)
{
__int64 v2; // rax
int v3; // ebx
unsigned int v4; // er12
int v5; // eax
__int64 v6; // rbx
int v7; // eax
__int64 result; // rax

v2 = *(_QWORD *)(a1 + 8);
v3 = 1;
if ( v2 )
v3 = *(_DWORD *)(v2 + 8);
v4 = rand() % 2;
v5 = rand();
if ( v4 == 1 )
{
if ( *((int *)a2 + 2) <= 0 )
{
printf("You strike at the lifeless corpse of the %s.\n", *a2);
result = 1LL;
}
else
{
v6 = (unsigned int)(v5 % v3 + 1);
printf("You land a powerful blow. It does %d damage.\n", v6);
v7 = *((_DWORD *)a2 + 2) - v6;
*((_DWORD *)a2 + 2) = v7;
if ( v7 <= 0 )
printf("You have defeated the %s!\n", *a2);
else
v4 = 0;
result = v4;
}
}
else
{
puts("You swing and miss.");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char **a2)
{
__int64 v2; // rax
int v3; // ebx
unsigned int v4; // er12
int v5; // eax
__int64 v6; // rbx
int v7; // eax
__int64 result; // rax

v2 = *(_QWORD *)(a1 + 8);
v3 = 1;
if ( v2 )
v3 = *(_DWORD *)(v2 + 8);
v4 = rand() % 2;
v5 = rand();
if ( v4 == 1 )
{
if ( *((int *)a2 + 2) <= 0 )
{
printf("You strike at the lifeless corpse of the %s.\n", *a2);
result = 1LL;
}
else
{
v6 = (unsigned int)(v5 % v3 + 1);
printf("You land a powerful blow. It does %d damage.\n", v6);
v7 = *((_DWORD *)a2 + 2) - v6;
*((_DWORD *)a2 + 2) = v7;
if ( v7 <= 0 )
printf("You have defeated the %s!\n", *a2);
else
v4 = 0;
result = v4;
}
}
else
{
puts("You swing and miss.");
result = 0LL;
}
return result;
}

original_code:
int player_attack(Player *player, Monster *monster)
{
  int killed = 0;
  int maximum_damage = 1;
  if (player->weapon) { maximum_damage = player->weapon->damage; }
  int hit = rand() % 2;
  int damage = (rand() % maximum_damage) + 1;
  if (hit == 1) {
    if (monster->health <= 0) {
      printf("You strike at the lifeless corpse of the %s.\n", monster->name);
      killed = 1;
    } else {
      printf("You land a powerful blow. It does %d damage.\n", damage);
      monster->health -= damage;
      if (monster->health <= 0) {
        printf("You have defeated the %s!\n", monster->name);
        killed = 1;
      }
    }
  } else {
    printf("You swing and miss.\n");
  }
  return killed;
}

canonical_original_code:
int player_attack(struct Player *player, struct Monster *monster)
{
  int killed = 0;
  int maximum_damage = 1;
  if (player->weapon) { maximum_damage = player->weapon->damage; }
  int hit = rand() % 2;
  int damage = (rand() % maximum_damage) + 1;
  if (hit == 1) {
    if (monster->health <= 0) {
      printf("You strike at the lifeless corpse of the %s.\n", monster->name);
      killed = 1;
    } else {
      printf("You land a powerful blow. It does %d damage.\n", damage);
      monster->health -= damage;
      if (monster->health <= 0) {
        printf("You have defeated the %s!\n", monster->name);
        killed = 1;
      }
    }
  } else {
    printf("You swing and miss.\n");
  }
  return killed;
}


decompiled_code:
__int64 __fastcall player_destroy(void *a1)
{
free(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(void *a1)
{
free(a1);
return 0LL;
}

original_code:
int player_destroy(Player *player)
{
  free(player);
  return 0;
}

canonical_original_code:
int player_destroy(struct Player *player)
{
  free(player);
  return 0;
}


decompiled_code:
__int64 __fastcall weapon_destroy(void *ptr)
{
free(*(void **)ptr);
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *ptr)
{
free(*(void **)ptr);
free(ptr);
return 0LL;
}

original_code:
int weapon_destroy(Weapon *weapon)
{
  free(weapon->name);
  free(weapon);
  return 0;
}

canonical_original_code:
int weapon_destroy(struct Weapon *weapon)
{
  free(weapon->name);
  free(weapon);
  return 0;
}

variable_types: struct Weapon { char *name; int damage; } *


repo: realthunder/usbip

decompiled_code:
int __fastcall usb_os_set_debug(unsigned int a1)
{
int result; // eax
const char *v2; // rcx

result = usb_debug | a1;
if ( usb_debug | a1 )
{
v2 = (const char *)&unk_5E4;
if ( !a1 )
v2 = (const char *)&unk_5E7;
result = fprintf(stderr, "usb_set_debug: Setting debugging level to %d (%s)\n", a1, v2);
}
usb_debug = a1;
return result;
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1)
{
int result; // eax
const char *v2; // rcx

result = usb_debug | a1;
if ( usb_debug | a1 )
{
v2 = (const char *)&unk_5E4;
if ( !a1 )
v2 = (const char *)&unk_5E7;
result = fprintf(stderr, "usb_set_debug: Setting debugging level to %d (%s)\n", a1, v2);
}
usb_debug = a1;
return result;
}

original_code:
void usb_os_set_debug(int level)
{
  if (usb_debug || level)
    fprintf(stderr, "usb_set_debug: Setting debugging level to %d (%s)\n",
 level, level ? "on" : "off");
  usb_debug = level;
}

variable_types: level int


decompiled_code:
__int64 usb_init()
{
char *v0; // rax
unsigned int v1; // eax

if ( getenv("USB_DEBUG") )
{
v0 = getenv("USB_DEBUG");
v1 = atoi(v0);
usb_set_debug(v1);
}
return usb_os_init();
}

canonical_decompiled_code:
__int64 func1()
{
char *v0; // rax
unsigned int v1; // eax

if ( getenv("USB_DEBUG") )
{
v0 = getenv("USB_DEBUG");
v1 = atoi(v0);
usb_set_debug(v1);
}
return usb_os_init();
}

original_code:
void usb_init(void)
{
  if (getenv("USB_DEBUG"))
    usb_set_debug(atoi(getenv("USB_DEBUG")));
  usb_os_init();
}

canonical_original_code:
void usb_init()
{
  if (getenv("USB_DEBUG"))
    usb_set_debug(atoi(getenv("USB_DEBUG")));
  usb_os_init();
}