decompiled_code:
__int64 __fastcall create_htlock(_QWORD *a1)
{
_DWORD *v1; // rax
_DWORD *v2; // rbx
_DWORD *v3; // rax
__int64 result; // rax

if ( !a1 )
_assert_fail("htl != NULL", "src/htlock.c", 0x31u, "create_htlock");
v1 = memalign(0x40uLL, 0x40uLL);
*a1 = v1;
v2 = v1;
if ( !v1 )
_assert_fail("htl->global != NULL", "src/htlock.c", 0x38u, "create_htlock");
v3 = malloc(0x40uLL);
v3[1] = 128;
a1[1] = v3;
*v3 = 0;
result = 0LL;
v2[1] = 0;
*v2 = 0;
_mm_mfence();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1)
{
_DWORD *v1; // rax
_DWORD *v2; // rbx
_DWORD *v3; // rax
__int64 result; // rax

if ( !a1 )
_assert_fail("htl != NULL", "src/htlock.c", 0x31u, "create_htlock");
v1 = memalign(0x40uLL, 0x40uLL);
*a1 = v1;
v2 = v1;
if ( !v1 )
_assert_fail("htl->global != NULL", "src/htlock.c", 0x38u, "create_htlock");
v3 = malloc(0x40uLL);
v3[1] = 128;
a1[1] = v3;
*v3 = 0;
result = 0LL;
v2[1] = 0;
*v2 = 0;
_mm_mfence();
return result;
}

original_code:
int create_htlock(htlock_t* htl)
{
    ((void) sizeof ((htl != ((void *)0)) ? 1 : 0), __extension__ ({ if (htl != ((void *)0)) ; else __assert_fail ("htl != NULL", "src/htlock.c", 49, __extension__ __PRETTY_FUNCTION__); }));
    htl->global = memalign(64, sizeof(htlock_global_t));
    if (htl == ((void *)0))
    {
        fprintf(stderr,"Error @ memalign : create htlock\n");
     }
    ((void) sizeof ((htl->global != ((void *)0)) ? 1 : 0), __extension__ ({ if (htl->global != ((void *)0)) ; else __assert_fail ("htl->global != NULL", "src/htlock.c", 56, __extension__ __PRETTY_FUNCTION__); }));
    uint32_t s;
    for (s = 0; s < 1; s++)
    {
        htl->local[s] = (htlock_local_t*) malloc(sizeof(htlock_local_t));
        htl->local[s]->cur = 128;
        htl->local[s]->nxt = 0;
        ((void) sizeof ((htl->local != ((void *)0)) ? 1 : 0), __extension__ ({ if (htl->local != ((void *)0)) ; else __assert_fail ("htl->local != NULL", "src/htlock.c", 69, __extension__ __PRETTY_FUNCTION__); }));
    }
    htl->global->cur = 0;
    htl->global->nxt = 0;
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int create_htlock(struct htlock *htl)
{
    ((void) sizeof ((htl != ((void *)0)) ? 1 : 0), __extension__ ({ if (htl != ((void *)0)) ; else __assert_fail ("htl != NULL", "src/htlock.c", 49, __extension__ __PRETTY_FUNCTION__); }));
    htl->global = memalign(64, sizeof(htlock_global_t));
    if (htl == ((void *)0))
    {
        fprintf(stderr,"Error @ memalign : create htlock\n");
     }
    ((void) sizeof ((htl->global != ((void *)0)) ? 1 : 0), __extension__ ({ if (htl->global != ((void *)0)) ; else __assert_fail ("htl->global != NULL", "src/htlock.c", 56, __extension__ __PRETTY_FUNCTION__); }));
    unsigned int s;
    for (s = 0; s < 1; s++)
    {
        htl->local[s] = (struct htlock_local *) malloc(sizeof(htlock_local_t));
        htl->local[s]->cur = 128;
        htl->local[s]->nxt = 0;
        ((void) sizeof ((htl->local != ((void *)0)) ? 1 : 0), __extension__ ({ if (htl->local != ((void *)0)) ; else __assert_fail ("htl->local != NULL", "src/htlock.c", 69, __extension__ __PRETTY_FUNCTION__); }));
    }
    htl->global->cur = 0;
    htl->global->nxt = 0;
    __sync_synchronize();
    return 0;
}

variable_types: struct htlock { struct htlock_global { unsigned int nxt; unsigned int cur; unsigned char padding[-1]; } *global; struct htlock_local { int nxt; int cur; unsigned char padding[-1]; } *local[1]; } *

variable_types:
s
unsigned int

user_defined_types: struct htlock { struct htlock_global *global; struct htlock_local *local[1]; }


repo: tudordavid/libslock

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

set_cpu(a1);
__writefsdword((unsigned int)&htlock_id_mine, a1);
result = get_cluster(a1);
__writefsdword((unsigned int)&htlock_node_mine, result);
_mm_mfence();
return result;
}

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

func4(a1);
__writefsdword((unsigned int)&htlock_id_mine, a1);
result = func0(a1);
__writefsdword((unsigned int)&htlock_node_mine, result);
_mm_mfence();
return result;
}

original_code:
void
init_thread_htlocks(uint32_t phys_core)
{
    set_cpu(phys_core);
    htlock_id_mine = phys_core;
    htlock_node_mine = get_cluster(phys_core);
    __sync_synchronize();
}

canonical_original_code:
void init_thread_htlocks(unsigned int phys_core)
{
    set_cpu(phys_core);
    htlock_id_mine = phys_core;
    htlock_node_mine = get_cluster(phys_core);
    __sync_synchronize();
}

return_type: void


decompiled_code:
_BOOL8 __fastcall is_free_hticket(__int64 a1)
{
return *(_DWORD *)(*(_QWORD *)a1 + 4LL) == **(_DWORD **)a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(__int64 a1)
{
return *(_DWORD *)(*(_QWORD *)a1 + 4LL) == **(_DWORD **)a1;
}

original_code:
uint32_t
is_free_hticket(htlock_t* htl)
{
    htlock_global_t* glb = htl->global;
    if (glb->cur == glb->nxt)
    {
        return 1;
    }
    return 0;
}

canonical_original_code:
unsigned int is_free_hticket(struct htlock *htl)
{
    struct htlock_global *glb = htl->global;
    if (glb->cur == glb->nxt)
    {
        return 1;
    }
    return 0;
}

variable_types: struct htlock_global { unsigned int nxt; unsigned int cur; unsigned char padding[-1]; } *

return_type: unsigned int

user_defined_types: struct htlock_local { int nxt; int cur; unsigned char padding[-1]; }


decompiled_code:
// attributes: thunk
void free_htlocks(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func11(void *ptr)
{
free(ptr);
}

original_code:
void
free_htlocks(htlock_t* locks)
{
    free(locks);
}

canonical_original_code:
void free_htlocks(struct htlock *locks)
{
    free(locks);
}

user_defined_types: struct htlock_global { unsigned int nxt; unsigned int cur; unsigned char padding[-1]; }


decompiled_code:
__int64 __fastcall htlock_lock(__int64 *a1)
{
__int64 v1; // rax
__int64 v3; // r8
signed int v4; // esi

LODWORD(v1) = htlock_node_mine;
v3 = a1[v1 + 1];
while ( 1 )
{
v4 = _InterlockedDecrement((volatile signed __int32 *)v3);
if ( v4 >= -1 )
break;
_mm_pause();
wait_cycles(-120 * v4);
_mm_pause();
}
if ( v4 != -1 )
return htlock_wait_ticket(v3, v4);
while ( *(_DWORD *)(v3 + 4) != 128 )
;
*(_DWORD *)v3 = 128;
return htlock_wait_global(*a1, _InterlockedExchangeAdd((volatile signed __int32 *)*a1, 1u));
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 *a1)
{
__int64 v1; // rax
__int64 v3; // r8
signed int v4; // esi

LODWORD(v1) = htlock_node_mine;
v3 = a1[v1 + 1];
while ( 1 )
{
v4 = _InterlockedDecrement((volatile signed __int32 *)v3);
if ( v4 >= -1 )
break;
_mm_pause();
func5(-120 * v4);
_mm_pause();
}
if ( v4 != -1 )
return func6(v3, v4);
while ( *(_DWORD *)(v3 + 4) != 128 )
;
*(_DWORD *)v3 = 128;
return func7(*a1, _InterlockedExchangeAdd((volatile signed __int32 *)*a1, 1u));
}

original_code:
void
htlock_lock(htlock_t* l)
{
    htlock_local_t* localp = l->local[htlock_node_mine];
    int32_t local_ticket;
again_local:
    local_ticket = __sync_sub_and_fetch(&localp->nxt,1);
    if (local_ticket < -1)
    {
        _mm_pause();
        wait_cycles(-local_ticket * 120);
        _mm_pause();
        goto again_local;
    }
    if (local_ticket >= 0)
    {
        htlock_wait_ticket((htlock_local_t*) localp, local_ticket);
    }
    else
    {
        do
        {
        } while (localp->cur != 128);
        localp->nxt = 128;
        htlock_global_t* globalp = l->global;
        uint32_t global_ticket = __sync_fetch_and_add(&globalp->nxt,1);
        htlock_wait_global((htlock_local_t*) globalp, global_ticket);
    }
}

canonical_original_code:
void htlock_lock(struct htlock *l)
{
    struct htlock_local *localp = l->local[htlock_node_mine];
    int local_ticket;
again_local:
    local_ticket = __sync_sub_and_fetch(&localp->nxt,1);
    if (local_ticket < -1)
    {
        _mm_pause();
        wait_cycles(-local_ticket * 120);
        _mm_pause();
        goto again_local;
    }
    if (local_ticket >= 0)
    {
        htlock_wait_ticket((struct htlock_local *) localp, local_ticket);
    }
    else
    {
        do
        {
        } while (localp->cur != 128);
        localp->nxt = 128;
        struct htlock_global *globalp = l->global;
        unsigned int global_ticket = __sync_fetch_and_add(&globalp->nxt,1);
        htlock_wait_global((struct htlock_local *) globalp, global_ticket);
    }
}

variable_types: struct htlock_local { int nxt; int cur; unsigned char padding[-1]; } *


decompiled_code:
__int64 __fastcall htlock_release(__int64 *a1)
{
__int64 v1; // rax
__int64 v2; // rcx
signed __int32 v3; // edx
__int64 result; // rax
__int64 v5; // rdx

LODWORD(v1) = htlock_node_mine;
v2 = a1[v1 + 1];
v3 = *(_DWORD *)(v2 + 4);
result = (unsigned int)_InterlockedCompareExchange((volatile signed __int32 *)v2, 0, v3);
if ( !v3 || (_DWORD)result == v3 )
{
v5 = *a1;
*(_DWORD *)(v2 + 4) = 128;
result = (unsigned int)(*(_DWORD *)(v5 + 4) + 1);
*(_DWORD *)(v5 + 4) = result;
}
else
{
*(_DWORD *)(v2 + 4) = v3 - 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 *a1)
{
__int64 v1; // rax
__int64 v2; // rcx
signed __int32 v3; // edx
__int64 result; // rax
__int64 v5; // rdx

LODWORD(v1) = htlock_node_mine;
v2 = a1[v1 + 1];
v3 = *(_DWORD *)(v2 + 4);
result = (unsigned int)_InterlockedCompareExchange((volatile signed __int32 *)v2, 0, v3);
if ( !v3 || (_DWORD)result == v3 )
{
v5 = *a1;
*(_DWORD *)(v2 + 4) = 128;
result = (unsigned int)(*(_DWORD *)(v5 + 4) + 1);
*(_DWORD *)(v5 + 4) = result;
}
else
{
*(_DWORD *)(v2 + 4) = v3 - 1;
}
return result;
}

original_code:
void
htlock_release(htlock_t* l)
{
    htlock_local_t* localp = l->local[htlock_node_mine];
    int32_t local_cur = localp->cur;
    int32_t local_nxt = __sync_val_compare_and_swap(&localp->nxt,local_cur,0);
    if (local_cur == 0 || local_cur == local_nxt)
    {
        localp->cur = 128;
        l->global->cur++;
    }
    else
    {
        localp->cur = local_cur - 1;
    }
}

canonical_original_code:
void htlock_release(struct htlock *l)
{
    struct htlock_local *localp = l->local[htlock_node_mine];
    int local_cur = localp->cur;
    int local_nxt = __sync_val_compare_and_swap(&localp->nxt,local_cur,0);
    if (local_cur == 0 || local_cur == local_nxt)
    {
        localp->cur = 128;
        l->global->cur++;
    }
    else
    {
        localp->cur = local_cur - 1;
    }
}


decompiled_code:
_BOOL8 __fastcall htlock_trylock(volatile signed __int64 **a1)
{
signed __int64 v2; // [rsp+0h] [rbp-80h]
signed __int64 v3; // [rsp+40h] [rbp-40h]

LODWORD(v2) = *(_DWORD *)*a1;
HIDWORD(v2) = v2;
LODWORD(v3) = v2 + 1;
HIDWORD(v3) = v2;
return _InterlockedCompareExchange64(*a1, v3, v2) == v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(volatile signed __int64 **a1)
{
signed __int64 v2; // [rsp+0h] [rbp-80h]
signed __int64 v3; // [rsp+40h] [rbp-40h]

LODWORD(v2) = *(_DWORD *)*a1;
HIDWORD(v2) = v2;
LODWORD(v3) = v2 + 1;
HIDWORD(v3) = v2;
return _InterlockedCompareExchange64(*a1, v3, v2) == v2;
}

original_code:
uint32_t
htlock_trylock(htlock_t* l)
{
    htlock_global_t* globalp = l->global;
    ;
    uint32_t global_nxt = globalp->nxt;
    htlock_global_t tmp =
    {
        .nxt = global_nxt,
        .cur = global_nxt
    };
    htlock_global_t tmp_new =
    {
        .nxt = global_nxt + 1,
        .cur = global_nxt
    };
    uint64_t tmp64 = *(uint64_t*) &tmp;
    uint64_t tmp_new64 = *(uint64_t*) &tmp_new;
    if (__sync_val_compare_and_swap((uint64_t*) globalp,tmp64,tmp_new64) == tmp64)
    {
        return 1;
    }
    return 0;
}

canonical_original_code:
unsigned int htlock_trylock(struct htlock *l)
{
    struct htlock_global *globalp = l->global;
    ;
    unsigned int global_nxt = globalp->nxt;
    struct htlock_global tmp = {
        .nxt = global_nxt,
        .cur = global_nxt
    };
    struct htlock_global tmp_new = {
        .nxt = global_nxt + 1,
        .cur = global_nxt
    };
    unsigned long tmp64 = *(uint64_t*) &tmp;
    unsigned long tmp_new64 = *(uint64_t*) &tmp_new;
    if (__sync_val_compare_and_swap((unsigned long *) globalp,tmp64,tmp_new64) == tmp64)
    {
        return 1;
    }
    return 0;
}

variable_types: struct htlock_global { unsigned int nxt; unsigned int cur; unsigned char padding[-1]; }


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

result = (unsigned int)(*(_DWORD *)(*(_QWORD *)a1 + 4LL) + 1);
*(_DWORD *)(*(_QWORD *)a1 + 4LL) = result;
return result;
}

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

result = (unsigned int)(*(_DWORD *)(*(_QWORD *)a1 + 4LL) + 1);
*(_DWORD *)(*(_QWORD *)a1 + 4LL) = result;
return result;
}

original_code:
inline void
htlock_release_try(htlock_t* l)
{
    ;
    l->global->cur++;
}

canonical_original_code:
inline void htlock_release_try(struct htlock *l)
{
    ;
    l->global->cur++;
}


decompiled_code:
__int64 __fastcall get_cluster(int a1)
{
return (unsigned int)(a1 / 8);
}

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

original_code:
static inline int get_cluster(int thread_id) {
        return thread_id/8;
    }

return_type: int


getticks

decompiled_code:
unsigned __int64 getticks()
{
return __rdtsc();
}

canonical_decompiled_code:
unsigned __int64 func1()
{
return __rdtsc();
}

original_code:
static inline ticks getticks(void)
    {
        unsigned hi, lo;
        __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
        return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
    }

canonical_original_code:
static inline unsigned long getticks()
    {
        unsigned int hi; unsigned int lo;
        __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
        return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
    }

variable_types:
hi
unsigned int

variable_types:
lo
unsigned int

return_type: unsigned long


cdelay

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

v1 = getticks() + a1;
do
result = getticks();
while ( result < v1 );
return result;
}

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

v1 = func1() + a1;
do
result = func1();
while ( result < v1 );
return result;
}

original_code:
static inline void cdelay(ticks cycles){
        ticks __ts_end = getticks() + (ticks) cycles;
        while (getticks() < __ts_end);
    }

canonical_original_code:
static inline void cdelay(unsigned long cycles){
        unsigned long __ts_end = getticks() + (ticks) cycles;
        while (getticks() < __ts_end);
    }

variable_types:
cycles
unsigned long


original_code:
static inline unsigned long xorshf96(unsigned long* x, unsigned long* y, unsigned long* z) {
        unsigned long t;
        (*x) ^= (*x) << 16;
        (*x) ^= (*x) >> 5;
        (*x) ^= (*x) << 1;
        t = *x;
        (*x) = *y;
        (*y) = *z;
        (*z) = t ^ (*x) ^ (*y);
        return *z;
    }

canonical_original_code:
static inline unsigned long xorshf96(unsigned long *x, unsigned long *y, unsigned long *z) {
        unsigned long t;
        (*x) ^= (*x) << 16;
        (*x) ^= (*x) >> 5;
        (*x) ^= (*x) << 1;
        t = *x;
        (*x) = *y;
        (*y) = *z;
        (*z) = t ^ (*x) ^ (*y);
        return *z;
    }

variable_types:
x
unsigned long *

variable_types:
y
unsigned long *

variable_types:
z
unsigned long *

variable_types:
t
unsigned long


decompiled_code:
int __fastcall set_cpu(int a1)
{
unsigned int v1; // eax
pthread_t v2; // rax
int result; // eax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a1 <= 0x3FF )
v4.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
v1 = get_cluster(a1);
numa_set_preferred(v1);
v2 = pthread_self();
result = pthread_setaffinity_np(v2, 0x80uLL, &v4);
if ( result )
result = fwrite("Error setting thread affinity\n", 1uLL, 0x1EuLL, stderr);
return result;
}

canonical_decompiled_code:
int __fastcall func4(int a1)
{
unsigned int v1; // eax
pthread_t v2; // rax
int result; // eax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a1 <= 0x3FF )
v4.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
v1 = func0(a1);
numa_set_preferred(v1);
v2 = pthread_self();
result = pthread_setaffinity_np(v2, 0x80uLL, &v4);
if ( result )
result = fwrite("Error setting thread affinity\n", 1uLL, 0x1EuLL, stderr);
return result;
}

original_code:
static inline void set_cpu(int cpu) {
        cpu_set_t mask;
        do __builtin_memset (&mask, '\0', sizeof (cpu_set_t)); while (0);
        (__extension__ ({ size_t __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&mask)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
        numa_set_preferred(get_cluster(cpu));
        pthread_t thread = pthread_self();
        if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
            fprintf(stderr, "Error setting thread affinity\n");
        }
    }

canonical_original_code:
static inline void set_cpu(int cpu) {
        struct cpu_set_t mask;
        do __builtin_memset (&mask, '\0', sizeof (cpu_set_t)); while (0);
        (__extension__ ({ unsigned long __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&mask)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
        numa_set_preferred(get_cluster(cpu));
        unsigned long thread = pthread_self();
        if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
            fprintf(stderr, "Error setting thread affinity\n");
        }
    }

variable_types: cpu int

variable_types: struct cpu_set_t { unsigned long __bits[-1]; }

variable_types:
thread
unsigned long


decompiled_code:
__int64 seed_rand()
{
unsigned __int64 v0; // rax
unsigned __int64 *v1; // r8
unsigned __int64 v2; // rax
__int64 v3; // r8
unsigned __int64 v4; // rcx
__int64 v5; // r8
__int64 result; // rax

memalign(0x40uLL, 0x40uLL);
v0 = getticks();
*v1 = v0 % 0x75BCD15;
v2 = getticks();
*(_QWORD *)(v3 + 8) = v2 % 0x159A55E5;
v4 = getticks();
result = v5;
*(_QWORD *)(v5 + 16) = v4 % 0x1F123BB5;
return result;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned __int64 v0; // rax
unsigned __int64 *v1; // r8
unsigned __int64 v2; // rax
__int64 v3; // r8
unsigned __int64 v4; // rcx
__int64 v5; // r8
__int64 result; // rax

memalign(0x40uLL, 0x40uLL);
v0 = func1();
*v1 = v0 % 0x75BCD15;
v2 = func1();
*(_QWORD *)(v3 + 8) = v2 % 0x159A55E5;
v4 = func1();
result = v5;
*(_QWORD *)(v5 + 16) = v4 % 0x1F123BB5;
return result;
}

original_code:
static inline unsigned long* seed_rand() {
        unsigned long* seeds;
        int num_seeds = 64/sizeof(unsigned long);
        if (num_seeds<3) num_seeds=3;
        seeds = (unsigned long*) memalign(64, num_seeds * sizeof(unsigned long));
        seeds[0] = getticks() % 123456789;
        seeds[1] = getticks() % 362436069;
        seeds[2] = getticks() % 521288629;
        return seeds;
    }

canonical_original_code:
static inline unsigned long *seed_rand() {
        unsigned long *seeds;
        int num_seeds = 64/sizeof(unsigned long);
        if (num_seeds<3) num_seeds=3;
        seeds = (unsigned long *) memalign(64, num_seeds * sizeof(unsigned long));
        seeds[0] = getticks() % 123456789;
        seeds[1] = getticks() % 362436069;
        seeds[2] = getticks() % 521288629;
        return seeds;
    }

variable_types:
seeds
unsigned long *

return_type: unsigned long *


decompiled_code:
_BOOL8 __fastcall rw_trylock(volatile signed __int16 *a1)
{
return _InterlockedCompareExchange16(a1, 256, 0) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(volatile signed __int16 *a1)
{
return _InterlockedCompareExchange16(a1, 256, 0) != 0;
}

original_code:
int rw_trylock(rw_ttas* lock, uint32_t* limit) {
    if (__sync_val_compare_and_swap(&lock->lock_data,0,0x100)==0) return 0;
    return 1;
}

canonical_original_code:
int rw_trylock(struct rw_ttas *lock, unsigned int *limit) {
    if (__sync_val_compare_and_swap(&lock->lock_data,0,0x100)==0) return 0;
    return 1;
}

variable_types: struct rw_ttas { union { struct rw_ttas_data { unsigned char read_lock; unsigned char write_lock; } rw; unsigned short lock_data; unsigned char padding[64]; }; } *

variable_types:
limit
unsigned int *

user_defined_types: struct rw_ttas { union { struct rw_ttas_data rw; unsigned short lock_data; unsigned char padding[64]; }; }


decompiled_code:
signed __int16 __fastcall read_acquire(volatile signed __int16 *a1)
{
unsigned __int64 v2; // rax
_DWORD *v3; // r8
int v4; // er9
unsigned __int64 v5; // rcx
int v6; // edx
signed __int16 v7; // dx
signed __int16 result; // ax

while ( 1 )
{
v7 = (unsigned __int8)*a1;
result = _InterlockedCompareExchange16(a1, v7 + 1, v7);
if ( result == v7 )
break;
v2 = xorshf96((__int64 *)rw_seeds, (__int64 *)(rw_seeds + 8), (__int64 *)(rw_seeds + 16));
v5 = (unsigned int)*v3;
v6 = 2 * v5;
if ( (unsigned int)(2 * v5) > 0x3E8 )
v6 = v4;
*v3 = v6;
cdelay(v2 % v5);
}
return result;
}

canonical_decompiled_code:
signed __int16 __fastcall func7(volatile signed __int16 *a1)
{
unsigned __int64 v2; // rax
_DWORD *v3; // r8
int v4; // er9
unsigned __int64 v5; // rcx
int v6; // edx
signed __int16 v7; // dx
signed __int16 result; // ax

while ( 1 )
{
v7 = (unsigned __int8)*a1;
result = _InterlockedCompareExchange16(a1, v7 + 1, v7);
if ( result == v7 )
break;
v2 = func3((__int64 *)rw_seeds, (__int64 *)(rw_seeds + 8), (__int64 *)(rw_seeds + 16));
v5 = (unsigned int)*v3;
v6 = 2 * v5;
if ( (unsigned int)(2 * v5) > 0x3E8 )
v6 = v4;
*v3 = v6;
func2(v2 % v5);
}
return result;
}

original_code:
void read_acquire(rw_ttas* lock, uint32_t* limit) {
    uint32_t delay;
    while (1)
    {
        rw_data_t aux;
        while ((aux=lock->lock_data)>(255))
        {
        }
        if (__sync_val_compare_and_swap(&lock->lock_data,aux,aux+1)==aux) {
            return;
        }
        else
        {
            delay = xorshf96(&(rw_seeds[0]),&(rw_seeds[1]),&(rw_seeds[2]))%(*limit);
            *limit = 1000 > 2*(*limit) ? 2*(*limit) : 1000;
            cdelay(delay);
        }
    }
}

canonical_original_code:
void read_acquire(struct rw_ttas *lock, unsigned int *limit) {
    unsigned int delay;
    while (1)
    {
        unsigned char aux;
        while ((aux=lock->lock_data)>(255))
        {
        }
        if (__sync_val_compare_and_swap(&lock->lock_data,aux,aux+1)==aux) {
            return;
        }
        else
        {
            delay = xorshf96(&(rw_seeds[0]),&(rw_seeds[1]),&(rw_seeds[2]))%(*limit);
            *limit = 1000 > 2*(*limit) ? 2*(*limit) : 1000;
            cdelay(delay);
        }
    }
}

variable_types:
delay
unsigned int

variable_types:
aux
unsigned char

user_defined_types: struct rw_ttas_data { unsigned char read_lock; unsigned char write_lock; }


decompiled_code:
void __fastcall read_release(volatile signed __int16 *a1)
{
_InterlockedSub16(a1, 1u);
}

canonical_decompiled_code:
void __fastcall func8(volatile signed __int16 *a1)
{
_InterlockedSub16(a1, 1u);
}

original_code:
void read_release(rw_ttas* lock) {
    __sync_sub_and_fetch(&(lock->lock_data),1);
}

canonical_original_code:
void read_release(struct rw_ttas *lock) {
    __sync_sub_and_fetch(&(lock->lock_data),1);
}


decompiled_code:
signed __int16 __fastcall write_acquire(volatile signed __int16 *a1)
{
signed __int16 v2; // r10
signed __int16 result; // ax
unsigned __int64 v4; // rax
int *v5; // r9
unsigned __int64 v6; // rcx
int v7; // edx

v2 = 256;
while ( 1 )
{
while ( *a1 )
;
result = _InterlockedCompareExchange16(a1, v2, 0);
if ( !result )
break;
v4 = xorshf96((__int64 *)rw_seeds, (__int64 *)(rw_seeds + 8), (__int64 *)(rw_seeds + 16));
v6 = (unsigned int)*v5;
v7 = 2 * v6;
if ( (unsigned int)(2 * v6) > 0x3E8 )
v7 = 1000;
*v5 = v7;
cdelay(v4 % v6);
}
return result;
}

canonical_decompiled_code:
signed __int16 __fastcall func9(volatile signed __int16 *a1)
{
signed __int16 v2; // r10
signed __int16 result; // ax
unsigned __int64 v4; // rax
int *v5; // r9
unsigned __int64 v6; // rcx
int v7; // edx

v2 = 256;
while ( 1 )
{
while ( *a1 )
;
result = _InterlockedCompareExchange16(a1, v2, 0);
if ( !result )
break;
v4 = func3((__int64 *)rw_seeds, (__int64 *)(rw_seeds + 8), (__int64 *)(rw_seeds + 16));
v6 = (unsigned int)*v5;
v7 = 2 * v6;
if ( (unsigned int)(2 * v6) > 0x3E8 )
v7 = 1000;
*v5 = v7;
func2(v4 % v6);
}
return result;
}

original_code:
void write_acquire(rw_ttas* lock, uint32_t* limit) {
    uint32_t delay;
    while (1)
    {
        while (lock->lock_data!=0)
        {
        }
        if (__sync_val_compare_and_swap(&lock->lock_data,0,0x100)==0) {
            return;
        }
        else {
            delay = xorshf96(&(rw_seeds[0]),&(rw_seeds[1]),&(rw_seeds[2]))%(*limit);
            *limit = 1000 > 2*(*limit) ? 2*(*limit) : 1000;
            cdelay(delay);
        }
    }
}

canonical_original_code:
void write_acquire(struct rw_ttas *lock, unsigned int *limit) {
    unsigned int delay;
    while (1)
    {
        while (lock->lock_data!=0)
        {
        }
        if (__sync_val_compare_and_swap(&lock->lock_data,0,0x100)==0) {
            return;
        }
        else {
            delay = xorshf96(&(rw_seeds[0]),&(rw_seeds[1]),&(rw_seeds[2]))%(*limit);
            *limit = 1000 > 2*(*limit) ? 2*(*limit) : 1000;
            cdelay(delay);
        }
    }
}


decompiled_code:
_BOOL8 __fastcall is_free_rw(_WORD *a1)
{
return *a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(_WORD *a1)
{
return *a1 == 0;
}

original_code:
int is_free_rw(rw_ttas* lock){
    if (lock->lock_data==0) return 1;
    return 0;
}

canonical_original_code:
int is_free_rw(struct rw_ttas *lock){
    if (lock->lock_data==0) return 1;
    return 0;
}


decompiled_code:
// attributes: thunk
void end_rw_ttas_array_local(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func13(void *ptr)
{
free(ptr);
}

original_code:
void end_rw_ttas_array_local(uint32_t* limits) {
    free(limits);
}

canonical_original_code:
void end_rw_ttas_array_local(unsigned int *limits) {
    free(limits);
}

variable_types:
limits
unsigned int *


decompiled_code:
// attributes: thunk
void __fastcall end_rw_ttas_array_global(void *ptr)
{
end_rw_ttas_array_local(ptr);
}

canonical_decompiled_code:
void __fastcall func14(void *ptr)
{
func13(ptr);
}

original_code:
void end_rw_ttas_array_global(rw_ttas* the_locks) {
    free(the_locks);
}

canonical_original_code:
void end_rw_ttas_array_global(struct rw_ttas *the_locks) {
    free(the_locks);
}


decompiled_code:
__int64 __fastcall init_rw_ttas_global(_WORD *a1)
{
__int64 result; // rax

*a1 = 0;
result = 0LL;
_mm_mfence();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(_WORD *a1)
{
__int64 result; // rax

*a1 = 0;
result = 0LL;
_mm_mfence();
return result;
}

original_code:
int init_rw_ttas_global(rw_ttas* the_lock) {
    the_lock->lock_data=0;
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int init_rw_ttas_global(struct rw_ttas *the_lock) {
    the_lock->lock_data=0;
    __sync_synchronize();
    return 0;
}


decompiled_code:
__int64 __fastcall init_rw_ttas_local(int a1, _DWORD *a2)
{
__int64 result; // rax

set_cpu(a1);
*a2 = 1;
__writefsqword((unsigned int)&rw_seeds, seed_rand());
result = 0LL;
_mm_mfence();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1, _DWORD *a2)
{
__int64 result; // rax

func4(a1);
*a2 = 1;
__writefsqword((unsigned int)&rw_seeds, seed_rand());
result = 0LL;
_mm_mfence();
return result;
}

original_code:
int init_rw_ttas_local(uint32_t thread_num, uint32_t * limit){
    set_cpu(thread_num);
    *limit = 1;
    rw_seeds = seed_rand();
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int init_rw_ttas_local(unsigned int thread_num, unsigned int *limit){
    set_cpu(thread_num);
    *limit = 1;
    rw_seeds = seed_rand();
    __sync_synchronize();
    return 0;
}


decompiled_code:
void end_rw_ttas_local()
{
;
}

canonical_decompiled_code:
void func17()
{
;
}

original_code:
void end_rw_ttas_local() {
}

canonical_original_code:
void end_rw_ttas_local() {
}


decompiled_code:
void end_rw_ttas_global()
{
;
}

canonical_decompiled_code:
void func18()
{
;
}

original_code:
void end_rw_ttas_global() {
}

canonical_original_code:
void end_rw_ttas_global() {
}


decompiled_code:
char __fastcall tas_uint8(char *a1)
{
char result; // al

result = *a1;
*a1 = -1;
return result;
}

canonical_decompiled_code:
char __fastcall func0(char *a1)
{
char result; // al

result = *a1;
*a1 = -1;
return result;
}

original_code:
static inline uint8_t tas_uint8(volatile uint8_t *addr) {
    uint8_t oldval;
    __asm__ __volatile__("xchgb %0,%1"
            : "=q"(oldval), "=m"(*addr)
            : "0"((unsigned char) 0xff), "m"(*addr) : "memory");
    return (uint8_t) oldval;
}

canonical_original_code:
static inline unsigned char tas_uint8(unsigned char *addr) {
    unsigned char oldval;
    __asm__ __volatile__("xchgb %0,%1"
            : "=q"(oldval), "=m"(*addr)
            : "0"((unsigned char) 0xff), "m"(*addr) : "memory");
    return (unsigned char) oldval;
}

variable_types:
addr
unsigned char *

variable_types:
oldval
unsigned char

return_type: unsigned char


decompiled_code:
__int64 __fastcall get_cluster(int a1)
{
return (unsigned int)(a1 / 8);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return (unsigned int)(a1 / 8);
}

original_code:
static inline int get_cluster(int thread_id) {
        return thread_id/8;
    }


getticks

decompiled_code:
unsigned __int64 getticks()
{
return __rdtsc();
}

canonical_decompiled_code:
unsigned __int64 func2()
{
return __rdtsc();
}

original_code:
static inline ticks getticks(void)
    {
        unsigned hi, lo;
        __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
        return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
    }

canonical_original_code:
static inline unsigned long getticks()
    {
        unsigned int hi; unsigned int lo;
        __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
        return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
    }

variable_types:
hi
unsigned int

variable_types:
lo
unsigned int


cdelay

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

v1 = getticks() + a1;
do
result = getticks();
while ( result < v1 );
return result;
}

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

v1 = func2() + a1;
do
result = func2();
while ( result < v1 );
return result;
}

original_code:
static inline void cdelay(ticks cycles){
        ticks __ts_end = getticks() + (ticks) cycles;
        while (getticks() < __ts_end);
    }

canonical_original_code:
static inline void cdelay(unsigned long cycles){
        unsigned long __ts_end = getticks() + (ticks) cycles;
        while (getticks() < __ts_end);
    }

variable_types:
cycles
unsigned long


original_code:
static inline unsigned long xorshf96(unsigned long* x, unsigned long* y, unsigned long* z) {
        unsigned long t;
        (*x) ^= (*x) << 16;
        (*x) ^= (*x) >> 5;
        (*x) ^= (*x) << 1;
        t = *x;
        (*x) = *y;
        (*y) = *z;
        (*z) = t ^ (*x) ^ (*y);
        return *z;
    }

canonical_original_code:
static inline unsigned long xorshf96(unsigned long *x, unsigned long *y, unsigned long *z) {
        unsigned long t;
        (*x) ^= (*x) << 16;
        (*x) ^= (*x) >> 5;
        (*x) ^= (*x) << 1;
        t = *x;
        (*x) = *y;
        (*y) = *z;
        (*z) = t ^ (*x) ^ (*y);
        return *z;
    }

variable_types:
x
unsigned long *

variable_types:
y
unsigned long *


decompiled_code:
int __fastcall set_cpu(int a1)
{
unsigned int v1; // eax
pthread_t v2; // rax
int result; // eax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a1 <= 0x3FF )
v4.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
v1 = get_cluster(a1);
numa_set_preferred(v1);
v2 = pthread_self();
result = pthread_setaffinity_np(v2, 0x80uLL, &v4);
if ( result )
result = fwrite("Error setting thread affinity\n", 1uLL, 0x1EuLL, stderr);
return result;
}

canonical_decompiled_code:
int __fastcall func5(int a1)
{
unsigned int v1; // eax
pthread_t v2; // rax
int result; // eax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a1 <= 0x3FF )
v4.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
v1 = func1(a1);
numa_set_preferred(v1);
v2 = pthread_self();
result = pthread_setaffinity_np(v2, 0x80uLL, &v4);
if ( result )
result = fwrite("Error setting thread affinity\n", 1uLL, 0x1EuLL, stderr);
return result;
}

original_code:
static inline void set_cpu(int cpu) {
        cpu_set_t mask;
        do __builtin_memset (&mask, '\0', sizeof (cpu_set_t)); while (0);
        (__extension__ ({ size_t __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&mask)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
        numa_set_preferred(get_cluster(cpu));
        pthread_t thread = pthread_self();
        if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
            fprintf(stderr, "Error setting thread affinity\n");
        }
    }

canonical_original_code:
static inline void set_cpu(int cpu) {
        struct cpu_set_t mask;
        do __builtin_memset (&mask, '\0', sizeof (cpu_set_t)); while (0);
        (__extension__ ({ unsigned long __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&mask)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
        numa_set_preferred(get_cluster(cpu));
        unsigned long thread = pthread_self();
        if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
            fprintf(stderr, "Error setting thread affinity\n");
        }
    }

user_defined_types: struct cpu_set_t { unsigned long __bits[-1]; }


decompiled_code:
__int64 seed_rand()
{
unsigned __int64 v0; // rax
unsigned __int64 *v1; // r8
unsigned __int64 v2; // rax
__int64 v3; // r8
unsigned __int64 v4; // rcx
__int64 v5; // r8
__int64 result; // rax

memalign(0x40uLL, 0x40uLL);
v0 = getticks();
*v1 = v0 % 0x75BCD15;
v2 = getticks();
*(_QWORD *)(v3 + 8) = v2 % 0x159A55E5;
v4 = getticks();
result = v5;
*(_QWORD *)(v5 + 16) = v4 % 0x1F123BB5;
return result;
}

canonical_decompiled_code:
__int64 func6()
{
unsigned __int64 v0; // rax
unsigned __int64 *v1; // r8
unsigned __int64 v2; // rax
__int64 v3; // r8
unsigned __int64 v4; // rcx
__int64 v5; // r8
__int64 result; // rax

memalign(0x40uLL, 0x40uLL);
v0 = func2();
*v1 = v0 % 0x75BCD15;
v2 = func2();
*(_QWORD *)(v3 + 8) = v2 % 0x159A55E5;
v4 = func2();
result = v5;
*(_QWORD *)(v5 + 16) = v4 % 0x1F123BB5;
return result;
}

original_code:
static inline unsigned long* seed_rand() {
        unsigned long* seeds;
        int num_seeds = 64/sizeof(unsigned long);
        if (num_seeds<3) num_seeds=3;
        seeds = (unsigned long*) memalign(64, num_seeds * sizeof(unsigned long));
        seeds[0] = getticks() % 123456789;
        seeds[1] = getticks() % 362436069;
        seeds[2] = getticks() % 521288629;
        return seeds;
    }

canonical_original_code:
static inline unsigned long *seed_rand() {
        unsigned long *seeds;
        int num_seeds = 64/sizeof(unsigned long);
        if (num_seeds<3) num_seeds=3;
        seeds = (unsigned long *) memalign(64, num_seeds * sizeof(unsigned long));
        seeds[0] = getticks() % 123456789;
        seeds[1] = getticks() % 362436069;
        seeds[2] = getticks() % 521288629;
        return seeds;
    }


decompiled_code:
_BOOL8 __fastcall ttas_trylock(char *a1)
{
return tas_uint8(a1) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(char *a1)
{
return func0(a1) != 0;
}

original_code:
int ttas_trylock(ttas_lock_t * the_lock, uint32_t * limits) {
    if (tas_uint8(&(the_lock->lock))==0) return 0;
    return 1;
}

canonical_original_code:
int ttas_trylock(struct ttas_lock_t *the_lock, unsigned int *limits) {
    if (tas_uint8(&(the_lock->lock))==0) return 0;
    return 1;
}

variable_types: struct ttas_lock_t { union { unsigned char lock; unsigned char padding[64]; }; } *


decompiled_code:
char __fastcall ttas_lock(char *a1)
{
char result; // al
unsigned __int64 v3; // rax
_DWORD *v4; // r9
int v5; // er10
unsigned __int64 v6; // rcx
int v7; // edx

while ( 1 )
{
while ( *a1 == 1 )
;
result = tas_uint8(a1);
if ( !result )
break;
v3 = xorshf96((__int64 *)ttas_seeds, (__int64 *)(ttas_seeds + 8), (__int64 *)(ttas_seeds + 16));
v6 = (unsigned int)*v4;
v7 = 2 * v6;
if ( (unsigned int)(2 * v6) > 0x3E8 )
v7 = v5;
*v4 = v7;
cdelay(v3 % v6);
}
return result;
}

canonical_decompiled_code:
char __fastcall func8(char *a1)
{
char result; // al
unsigned __int64 v3; // rax
_DWORD *v4; // r9
int v5; // er10
unsigned __int64 v6; // rcx
int v7; // edx

while ( 1 )
{
while ( *a1 == 1 )
;
result = func0(a1);
if ( !result )
break;
v3 = func4((__int64 *)ttas_seeds, (__int64 *)(ttas_seeds + 8), (__int64 *)(ttas_seeds + 16));
v6 = (unsigned int)*v4;
v7 = 2 * v6;
if ( (unsigned int)(2 * v6) > 0x3E8 )
v7 = v5;
*v4 = v7;
func3(v3 % v6);
}
return result;
}

original_code:
void ttas_lock(ttas_lock_t * the_lock, uint32_t* limit) {
    uint32_t delay;
    volatile ttas_lock_data_t* l = &(the_lock->lock);
    while (1){
        while ((*l)==1) {}
        if (tas_uint8(l)==0) {
            return;
        } else {
            delay = xorshf96(&(ttas_seeds[0]),&(ttas_seeds[1]),&(ttas_seeds[2]))%(*limit);
            *limit = 1000 > 2*(*limit) ? 2*(*limit) : 1000;
            cdelay(delay);
        }
    }
}

canonical_original_code:
void ttas_lock(struct ttas_lock_t *the_lock, unsigned int *limit) {
    unsigned int delay;
    unsigned char *l = &(the_lock->lock);
    while (1){
        while ((*l)==1) {}
        if (tas_uint8(l)==0) {
            return;
        } else {
            delay = xorshf96(&(ttas_seeds[0]),&(ttas_seeds[1]),&(ttas_seeds[2]))%(*limit);
            *limit = 1000 > 2*(*limit) ? 2*(*limit) : 1000;
            cdelay(delay);
        }
    }
}

variable_types:
l
unsigned char *

user_defined_types: struct ttas_lock_t { union { unsigned char lock; unsigned char padding[64]; }; }


decompiled_code:
_BOOL8 __fastcall is_free_ttas(_BYTE *a1)
{
return *a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(_BYTE *a1)
{
return *a1 == 0;
}

original_code:
int is_free_ttas(ttas_lock_t * the_lock){
    if (the_lock->lock==0) return 1;
    return 0;
}

canonical_original_code:
int is_free_ttas(struct ttas_lock_t *the_lock){
    if (the_lock->lock==0) return 1;
    return 0;
}


decompiled_code:
// attributes: thunk
void end_ttas_array_local(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func11(void *ptr)
{
free(ptr);
}

original_code:
void end_ttas_array_local(uint32_t* limits) {
    free(limits);
}

canonical_original_code:
void end_ttas_array_local(unsigned int *limits) {
    free(limits);
}


decompiled_code:
// attributes: thunk
void __fastcall end_ttas_array_global(void *ptr)
{
end_ttas_array_local(ptr);
}

canonical_decompiled_code:
void __fastcall func12(void *ptr)
{
func11(ptr);
}

original_code:
void end_ttas_array_global(ttas_lock_t* the_locks) {
    free(the_locks);
}

canonical_original_code:
void end_ttas_array_global(struct ttas_lock_t *the_locks) {
    free(the_locks);
}


decompiled_code:
__int64 __fastcall init_ttas_global(_BYTE *a1)
{
__int64 result; // rax

*a1 = 0;
result = 0LL;
_mm_mfence();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(_BYTE *a1)
{
__int64 result; // rax

*a1 = 0;
result = 0LL;
_mm_mfence();
return result;
}

original_code:
int init_ttas_global(ttas_lock_t* the_lock) {
    the_lock->lock=0;
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int init_ttas_global(struct ttas_lock_t *the_lock) {
    the_lock->lock=0;
    __sync_synchronize();
    return 0;
}


decompiled_code:
__int64 __fastcall init_ttas_local(int a1, _DWORD *a2)
{
__int64 result; // rax

set_cpu(a1);
*a2 = 1;
__writefsqword((unsigned int)&ttas_seeds, seed_rand());
result = 0LL;
_mm_mfence();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1, _DWORD *a2)
{
__int64 result; // rax

func5(a1);
*a2 = 1;
__writefsqword((unsigned int)&ttas_seeds, seed_rand());
result = 0LL;
_mm_mfence();
return result;
}

original_code:
int init_ttas_local(uint32_t thread_num , uint32_t* limit){
    set_cpu(thread_num);
    *limit=1;
    ttas_seeds = seed_rand();
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int init_ttas_local(unsigned int thread_num, unsigned int *limit){
    set_cpu(thread_num);
    *limit=1;
    ttas_seeds = seed_rand();
    __sync_synchronize();
    return 0;
}


decompiled_code:
void end_ttas_local()
{
;
}

canonical_decompiled_code:
void func15()
{
;
}

original_code:
void end_ttas_local() {
}

canonical_original_code:
void end_ttas_local() {
}


decompiled_code:
void end_ttas_global()
{
;
}

canonical_decompiled_code:
void func16()
{
;
}

original_code:
void end_ttas_global() {
}

canonical_original_code:
void end_ttas_global() {
}


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

canonical_original_code:
static inline int get_cluster(int thread_id) {
        return thread_id/8;
    }


canonical_decompiled_code:
int __fastcall func1(int a1)
{
unsigned int v1; // eax
pthread_t v2; // rax
int result; // eax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a1 <= 0x3FF )
v4.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
v1 = func0(a1);
numa_set_preferred(v1);
v2 = pthread_self();
result = pthread_setaffinity_np(v2, 0x80uLL, &v4);
if ( result )
result = fwrite("Error setting thread affinity\n", 1uLL, 0x1EuLL, stderr);
return result;
}

variable_types: cpu int

variable_types: struct cpu_set_t { unsigned long __bits[-1]; }


decompiled_code:
__int64 __fastcall wait_for_grant_or_cluster_master(int *a1, char a2)
{
int v3; // [rsp+0h] [rbp-40h]

v3 = *a1;
if ( (unsigned __int8)BYTE2(*a1) == a2 )
goto LABEL_5;
while ( BYTE1(v3) != 1 && BYTE2(v3) == a2 )
{
_mm_pause();
v3 = *a1;
if ( (unsigned __int8)BYTE2(*a1) == a2 )
{
LABEL_5:
if ( !(_WORD)v3 )
return 1LL;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int *a1, char a2)
{
int v3; // [rsp+0h] [rbp-40h]

v3 = *a1;
if ( (unsigned __int8)BYTE2(*a1) == a2 )
goto LABEL_5;
while ( BYTE1(v3) != 1 && BYTE2(v3) == a2 )
{
_mm_pause();
v3 = *a1;
if ( (unsigned __int8)BYTE2(*a1) == a2 )
{
LABEL_5:
if ( !(_WORD)v3 )
return 1LL;
}
}
return 0LL;
}

original_code:
uint16_t wait_for_grant_or_cluster_master(volatile qnode *q, uint8_t my_cluster) {
    qnode aux;
    while(1)
    {
        aux.data=q->data;
        if ((aux.fields.cluster_id==my_cluster) &&
                (aux.fields.tail_when_spliced==0) &&
                (aux.fields.successor_must_wait==0))
            return 1;
        if (aux.fields.tail_when_spliced==1)
            return 0;
        if (aux.fields.cluster_id!=my_cluster)
            return 0;
        _mm_pause();
    }
}

canonical_original_code:
unsigned short wait_for_grant_or_cluster_master(struct qnode *q, unsigned char my_cluster) {
    struct qnode aux;
    while(1)
    {
        aux.data=q->data;
        if ((aux.fields.cluster_id==my_cluster) &&
                (aux.fields.tail_when_spliced==0) &&
                (aux.fields.successor_must_wait==0))
            return 1;
        if (aux.fields.tail_when_spliced==1)
            return 0;
        if (aux.fields.cluster_id!=my_cluster)
            return 0;
        _mm_pause();
    }
}

variable_types: struct qnode { union { unsigned int data; struct node_fields { unsigned char successor_must_wait; unsigned char tail_when_spliced; unsigned char cluster_id; } fields; unsigned char padding[64]; }; } *

return_type: unsigned short

user_defined_types: struct qnode { union { unsigned int data; struct node_fields fields; unsigned char padding[64]; }; }


decompiled_code:
_BOOL8 __fastcall is_free_hclh(int **a1, _BYTE **a2, __int64 a3)
{
_BOOL8 result; // rax
int v4; // [rsp+0h] [rbp-40h]

if ( *a1 && (v4 = **a1, BYTE2(v4) == *(_BYTE *)(a3 + 2)) && !(_WORD)v4 )
result = 1LL;
else
result = **a2 == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(int **a1, _BYTE **a2, __int64 a3)
{
_BOOL8 result; // rax
int v4; // [rsp+0h] [rbp-40h]

if ( *a1 && (v4 = **a1, BYTE2(v4) == *(_BYTE *)(a3 + 2)) && !(_WORD)v4 )
result = 1LL;
else
result = **a2 == 0;
return result;
}

original_code:
int is_free_hclh(local_queue *lq, global_queue *gq, qnode *my_qnode) {
    if ((*lq)!=((void *)0)) {
        qnode aux;
        aux.data=(*lq)->data;
        if ((aux.fields.cluster_id==my_qnode->fields.cluster_id) &&
                (aux.fields.tail_when_spliced==0) &&
                (aux.fields.successor_must_wait==0))
            return 1;
    }
    if ((*gq)->fields.successor_must_wait==0) return 1;
    return 0;
}

canonical_original_code:
int is_free_hclh(struct qnode **lq, struct qnode **gq, struct qnode *my_qnode) {
    if ((*lq)!=((void *)0)) {
        struct qnode aux;
        aux.data=(*lq)->data;
        if ((aux.fields.cluster_id==my_qnode->fields.cluster_id) &&
                (aux.fields.tail_when_spliced==0) &&
                (aux.fields.successor_must_wait==0))
            return 1;
    }
    if ((*gq)->fields.successor_must_wait==0) return 1;
    return 0;
}

variable_types: struct qnode { union { unsigned int data; struct node_fields { unsigned char successor_must_wait; unsigned char tail_when_spliced; unsigned char cluster_id; } fields; unsigned char padding[64]; }; } **

variable_types:
gq
struct qnode **


decompiled_code:
volatile signed __int32 *__fastcall hclh_release(_BYTE *a1, volatile signed __int32 *a2)
{
signed __int32 v2; // edx
signed __int32 v4; // [rsp+0h] [rbp-40h]

*a1 = 0;
v4 = 1;
BYTE2(v4) = hclh_node_mine;
v2 = *a2;
while ( _InterlockedCompareExchange(a2, v4, v2) != v2 )
{
v2 = *a2;
_mm_pause();
}
return a2;
}

canonical_decompiled_code:
volatile signed __int32 *__fastcall func4(_BYTE *a1, volatile signed __int32 *a2)
{
signed __int32 v2; // edx
signed __int32 v4; // [rsp+0h] [rbp-40h]

*a1 = 0;
v4 = 1;
BYTE2(v4) = hclh_node_mine;
v2 = *a2;
while ( _InterlockedCompareExchange(a2, v4, v2) != v2 )
{
v2 = *a2;
_mm_pause();
}
return a2;
}

original_code:
qnode* hclh_release(qnode *my_qnode, qnode * my_pred) {
    my_qnode->fields.successor_must_wait = 0;
    qnode* pr = my_pred;
    qnode new_node;
    new_node.data=0;
    new_node.fields.cluster_id=hclh_node_mine;
    new_node.fields.successor_must_wait = 1;
    new_node.fields.tail_when_spliced=0;
    uint32_t old_data = pr->data;
    while (__sync_val_compare_and_swap(&pr->data,old_data,new_node.data)!=old_data)
    {
        old_data=pr->data;
        _mm_pause();
    }
    my_qnode=pr;
    return my_qnode;
}

canonical_original_code:
struct qnode *hclh_release(struct qnode *my_qnode, struct qnode *my_pred) {
    my_qnode->fields.successor_must_wait = 0;
    struct qnode *pr = my_pred;
    struct qnode new_node;
    new_node.data=0;
    new_node.fields.cluster_id=hclh_node_mine;
    new_node.fields.successor_must_wait = 1;
    new_node.fields.tail_when_spliced=0;
    unsigned int old_data = pr->data;
    while (__sync_val_compare_and_swap(&pr->data,old_data,new_node.data)!=old_data)
    {
        old_data=pr->data;
        _mm_pause();
    }
    my_qnode=pr;
    return my_qnode;
}

return_type: struct qnode { union { unsigned int data; struct node_fields { unsigned char successor_must_wait; unsigned char tail_when_spliced; unsigned char cluster_id; } fields; unsigned char padding[64]; }; } *


decompiled_code:
void __fastcall end_hclh_array_local(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

v2 = ptr;
if ( a2 )
{
v3 = (void **)ptr;
v4 = (void **)((char *)ptr + 24 * (unsigned int)(a2 - 1) + 24);
do
{
v5 = *v3;
v3 += 3;
free(v5);
}
while ( v3 != v4 );
}
free(v2);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

v2 = ptr;
if ( a2 )
{
v3 = (void **)ptr;
v4 = (void **)((char *)ptr + 24 * (unsigned int)(a2 - 1) + 24);
do
{
v5 = *v3;
v3 += 3;
free(v5);
}
while ( v3 != v4 );
}
free(v2);
}

original_code:
void end_hclh_array_local(hclh_local_params* local_params, uint32_t size) {
    uint32_t i;
    for (i = 0; i < size; i++) {
        free(local_params[i].my_qnode);
    }
    free(local_params);
}

canonical_original_code:
void end_hclh_array_local(struct hclh_local_params *local_params, unsigned int size) {
    unsigned int i;
    for (i = 0; i < size; i++) {
        free(local_params[i].my_qnode);
    }
    free(local_params);
}

variable_types: struct hclh_local_params { struct qnode { union { unsigned int data; struct node_fields { unsigned char successor_must_wait; unsigned char tail_when_spliced; unsigned char cluster_id; } fields; unsigned char padding[64]; }; } *my_qnode; struct qnode *my_pred; struct qnode **my_queue; } *

variable_types:
size
unsigned int

variable_types:
i
unsigned int

user_defined_types: struct hclh_local_params { struct qnode *my_qnode; struct qnode *my_pred; struct qnode **my_queue; }


decompiled_code:
void __fastcall end_hclh_array_global(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

v2 = ptr;
if ( a2 )
{
v3 = (void **)ptr;
v4 = (void **)((char *)ptr + 72 * (unsigned int)(a2 - 1) + 72);
do
{
v5 = *v3;
v3 += 9;
free(v5);
free(*(v3 - 8));
}
while ( v3 != v4 );
}
free(v2);
}

canonical_decompiled_code:
void __fastcall func7(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

v2 = ptr;
if ( a2 )
{
v3 = (void **)ptr;
v4 = (void **)((char *)ptr + 72 * (unsigned int)(a2 - 1) + 72);
do
{
v5 = *v3;
v3 += 9;
free(v5);
free(*(v3 - 8));
}
while ( v3 != v4 );
}
free(v2);
}

original_code:
void end_hclh_array_global(hclh_global_params* global_params, uint32_t size) {
    uint32_t i;
    for (i = 0; i < size; i++) {
        free(global_params[i].shared_queue);
        free(global_params[i].local_queues);
    }
    free(global_params);
}

canonical_original_code:
void end_hclh_array_global(struct hclh_global_params *global_params, unsigned int size) {
    unsigned int i;
    for (i = 0; i < size; i++) {
        free(global_params[i].shared_queue);
        free(global_params[i].local_queues);
    }
    free(global_params);
}

variable_types: struct hclh_global_params { struct qnode { union { unsigned int data; struct node_fields { unsigned char successor_must_wait; unsigned char tail_when_spliced; unsigned char cluster_id; } fields; unsigned char padding[64]; }; } **shared_queue; struct qnode ***local_queues; unsigned int *init_done; unsigned char padding[-1]; } *

user_defined_types: struct hclh_global_params { struct qnode **shared_queue; struct qnode ***local_queues; unsigned int *init_done; unsigned char padding[-1]; }


decompiled_code:
__int64 __fastcall init_hclh_global(_QWORD *a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rbp
_BYTE *v3; // rax
__int64 result; // rax

a1[1] = malloc(8uLL);
a1[2] = malloc(4uLL);
v1 = malloc(8uLL);
*a1 = v1;
v2 = v1;
v3 = malloc(0x40uLL);
*(_DWORD *)v3 = 0;
*v2 = v3;
v3[2] = 2;
result = 0LL;
_mm_mfence();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rbp
_BYTE *v3; // rax
__int64 result; // rax

a1[1] = malloc(8uLL);
a1[2] = malloc(4uLL);
v1 = malloc(8uLL);
*a1 = v1;
v2 = v1;
v3 = malloc(0x40uLL);
*(_DWORD *)v3 = 0;
*v2 = v3;
v3[2] = 2;
result = 0LL;
_mm_mfence();
return result;
}

original_code:
int init_hclh_global(hclh_global_params* the_params) {
    the_params->local_queues = (local_queue**)malloc(1*sizeof(local_queue*));
    the_params->init_done=(uint32_t*)malloc(1 * sizeof(uint32_t));
    the_params->shared_queue = (global_queue*)malloc(sizeof(global_queue));
    qnode * a_node = (qnode *) malloc(sizeof(qnode));
    a_node->data=0;
    a_node->fields.cluster_id = 1 +1;
    *(the_params->shared_queue) = a_node;
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int init_hclh_global(struct hclh_global_params *the_params) {
    the_params->local_queues = (struct qnode ***)malloc(1*sizeof(struct qnode **));
    the_params->init_done=(unsigned int *)malloc(1 * sizeof(unsigned int));
    the_params->shared_queue = (struct qnode **)malloc(sizeof(global_queue));
    struct qnode *a_node = (qnode *) malloc(sizeof(qnode));
    a_node->data=0;
    a_node->fields.cluster_id = 1 +1;
    *(the_params->shared_queue) = a_node;
    __sync_synchronize();
    return 0;
}

user_defined_types: struct node_fields { unsigned char successor_must_wait; unsigned char tail_when_spliced; unsigned char cluster_id; }


original_code:
int init_hclh_local(uint32_t phys_core, hclh_global_params* the_params, hclh_local_params* local_params) {
    set_cpu(phys_core);
    hclh_node_mine = phys_core/8;
    local_params->my_qnode = (qnode*) malloc(sizeof(qnode));
    local_params->my_qnode->data = 0;
    local_params->my_qnode->fields.cluster_id = phys_core/8;
    local_params->my_qnode->fields.successor_must_wait=1;
    local_params->my_pred = ((void *)0);
    if (phys_core%8==0) {
        the_params->local_queues[phys_core/8] = (local_queue*)malloc(sizeof(local_queue));
        *(the_params->local_queues[phys_core/8]) = ((void *)0);
        the_params->init_done[phys_core/8]=123;
    }
    while(the_params->init_done[phys_core/8]!=123) {}
    local_params->my_queue = the_params->local_queues[phys_core/8];
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int init_hclh_local(unsigned int phys_core, struct hclh_global_params *the_params, struct hclh_local_params *local_params) {
    set_cpu(phys_core);
    hclh_node_mine = phys_core/8;
    local_params->my_qnode = (struct qnode *) malloc(sizeof(qnode));
    local_params->my_qnode->data = 0;
    local_params->my_qnode->fields.cluster_id = phys_core/8;
    local_params->my_qnode->fields.successor_must_wait=1;
    local_params->my_pred = ((void *)0);
    if (phys_core%8==0) {
        the_params->local_queues[phys_core/8] = (struct qnode **)malloc(sizeof(local_queue));
        *(the_params->local_queues[phys_core/8]) = ((void *)0);
        the_params->init_done[phys_core/8]=123;
    }
    while(the_params->init_done[phys_core/8]!=123) {}
    local_params->my_queue = the_params->local_queues[phys_core/8];
    __sync_synchronize();
    return 0;
}

variable_types: struct hclh_local_params { struct qnode *my_qnode; struct qnode *my_pred; struct qnode **my_queue; } *


decompiled_code:
void __fastcall end_hclh_local(int a1, int a2, int a3, int a4, int a5, int a6, void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void __fastcall func10(int a1, int a2, int a3, int a4, int a5, int a6, void *ptr)
{
free(ptr);
}

original_code:
void end_hclh_local(hclh_local_params local_params) {
    free(local_params.my_qnode);
}

canonical_original_code:
void end_hclh_local(struct hclh_local_params local_params) {
    free(local_params.my_qnode);
}

variable_types: struct hclh_local_params { struct qnode { union { unsigned int data; struct node_fields { unsigned char successor_must_wait; unsigned char tail_when_spliced; unsigned char cluster_id; } fields; unsigned char padding[64]; }; } *my_qnode; struct qnode *my_pred; struct qnode **my_queue; }


decompiled_code:
void __fastcall end_hclh_global(int a1, int a2, int a3, int a4, int a5, int a6, void *ptr, void *a8)
{
free(ptr);
free(*(void **)a8);
free(a8);
}

canonical_decompiled_code:
void __fastcall func11(int a1, int a2, int a3, int a4, int a5, int a6, void *ptr, void *a8)
{
free(ptr);
free(*(void **)a8);
free(a8);
}

original_code:
void end_hclh_global(hclh_global_params global_params) {
    free(global_params.shared_queue);
    int i;
    for (i=0;i<sizeof(global_params.local_queues)/sizeof(global_params.local_queues[0]);i++) {
       free(global_params.local_queues[i]);
    }
    free(global_params.local_queues);
}

canonical_original_code:
void end_hclh_global(struct hclh_global_params global_params) {
    free(global_params.shared_queue);
    int i;
    for (i=0;i<sizeof(global_params.local_queues)/sizeof(global_params.local_queues[0]);i++) {
       free(global_params.local_queues[i]);
    }
    free(global_params.local_queues);
}

variable_types: struct hclh_global_params { struct qnode { union { unsigned int data; struct node_fields { unsigned char successor_must_wait; unsigned char tail_when_spliced; unsigned char cluster_id; } fields; unsigned char padding[64]; }; } **shared_queue; struct qnode ***local_queues; unsigned int *init_done; unsigned char padding[-1]; }

variable_types: i int


decompiled_code:
__int64 __fastcall swap_pointer(volatile __int64 *a1, __int64 a2)
{
return _InterlockedExchange64(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(volatile __int64 *a1, __int64 a2)
{
return _InterlockedExchange64(a1, a2);
}

original_code:
static inline void* swap_pointer(volatile void* ptr, void *x) {
    __asm__ __volatile__("xchgq %0,%1"
            :"=r" ((unsigned long long) x)
            :"m" (*(volatile long long *)ptr), "0" ((unsigned long long) x)
            :"memory");
    return x;
}

canonical_original_code:
static inline void *swap_pointer(void *ptr, void *x) {
    __asm__ __volatile__("xchgq %0,%1"
            :"=r" ((unsigned long long) x)
            :"m" (*(long long *)ptr), "0" ((unsigned long long) x)
            :"memory");
    return x;
}

variable_types:
ptr
void *

variable_types:
x
void *

return_type: void *


decompiled_code:
int __fastcall set_cpu(int a1)
{
unsigned int v1; // eax
pthread_t v2; // rax
int result; // eax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a1 <= 0x3FF )
v4.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
v1 = get_cluster(a1);
numa_set_preferred(v1);
v2 = pthread_self();
result = pthread_setaffinity_np(v2, 0x80uLL, &v4);
if ( result )
result = fwrite("Error setting thread affinity\n", 1uLL, 0x1EuLL, stderr);
return result;
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
unsigned int v1; // eax
pthread_t v2; // rax
int result; // eax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a1 <= 0x3FF )
v4.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
v1 = func0(a1);
numa_set_preferred(v1);
v2 = pthread_self();
result = pthread_setaffinity_np(v2, 0x80uLL, &v4);
if ( result )
result = fwrite("Error setting thread affinity\n", 1uLL, 0x1EuLL, stderr);
return result;
}

original_code:
static inline void set_cpu(int cpu) {
        cpu_set_t mask;
        do __builtin_memset (&mask, '\0', sizeof (cpu_set_t)); while (0);
        (__extension__ ({ size_t __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&mask)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
        numa_set_preferred(get_cluster(cpu));
        pthread_t thread = pthread_self();
        if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
            fprintf(stderr, "Error setting thread affinity\n");
        }
    }

canonical_original_code:
static inline void set_cpu(int cpu) {
        struct cpu_set_t mask;
        do __builtin_memset (&mask, '\0', sizeof (cpu_set_t)); while (0);
        (__extension__ ({ unsigned long __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&mask)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
        numa_set_preferred(get_cluster(cpu));
        unsigned long thread = pthread_self();
        if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
            fprintf(stderr, "Error setting thread affinity\n");
        }
    }

variable_types:
thread
unsigned long


decompiled_code:
__int64 clh_trylock()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func3()
{
return 1LL;
}

original_code:
int clh_trylock(clh_lock * L, clh_qnode_ptr I) {
    return 1;
}

canonical_original_code:
int clh_trylock(struct clh_qnode **L, struct clh_qnode *I) {
    return 1;
}

variable_types: struct clh_qnode { unsigned char locked; unsigned char padding[-1]; } **

variable_types:
I
struct clh_qnode *


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

result = a2;
*a1 = 0;
return result;
}

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

result = a2;
*a1 = 0;
return result;
}

original_code:
clh_qnode* clh_release(clh_qnode *my_qnode, clh_qnode * my_pred) {
    asm volatile("" ::: "memory");
    my_qnode->locked=0;
    return my_pred;
}

canonical_original_code:
struct clh_qnode *clh_release(struct clh_qnode *my_qnode, struct clh_qnode *my_pred) {
    asm volatile("" ::: "memory");
    my_qnode->locked=0;
    return my_pred;
}

return_type: struct clh_qnode { unsigned char locked; unsigned char padding[-1]; } *


decompiled_code:
// attributes: thunk
void end_clh_array_local(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func7(void *ptr)
{
free(ptr);
}

original_code:
void end_clh_array_local(clh_local_params* the_params, uint32_t size){
    free(the_params);
}

canonical_original_code:
void end_clh_array_local(struct clh_local_params *the_params, unsigned int size){
    free(the_params);
}

variable_types: struct clh_local_params { struct clh_qnode { unsigned char locked; unsigned char padding[-1]; } *my_qnode; struct clh_qnode *my_pred; } *

user_defined_types: struct clh_local_params { struct clh_qnode *my_qnode; struct clh_qnode *my_pred; }


decompiled_code:
void __fastcall end_clh_array_global(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

v2 = ptr;
if ( a2 )
{
v3 = (void **)ptr;
v4 = (void **)((char *)ptr + 64 * (unsigned __int64)(unsigned int)(a2 - 1) + 64);
do
{
v5 = *v3;
v3 += 8;
free(v5);
}
while ( v3 != v4 );
}
free(v2);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

v2 = ptr;
if ( a2 )
{
v3 = (void **)ptr;
v4 = (void **)((char *)ptr + 64 * (unsigned __int64)(unsigned int)(a2 - 1) + 64);
do
{
v5 = *v3;
v3 += 8;
free(v5);
}
while ( v3 != v4 );
}
free(v2);
}

original_code:
void end_clh_array_global(clh_global_params* the_locks, uint32_t size) {
    uint32_t i;
    for (i = 0; i < size; i++) {
        free(the_locks[i].the_lock);
    }
    free(the_locks);
}

canonical_original_code:
void end_clh_array_global(struct clh_global_params *the_locks, unsigned int size) {
    unsigned int i;
    for (i = 0; i < size; i++) {
        free(the_locks[i].the_lock);
    }
    free(the_locks);
}

variable_types: struct clh_global_params { struct clh_qnode { unsigned char locked; unsigned char padding[-1]; } **the_lock; unsigned char padding[-1]; } *

user_defined_types: struct clh_global_params { struct clh_qnode **the_lock; unsigned char padding[-1]; }


decompiled_code:
__int64 __fastcall init_clh_global(_QWORD *a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rbx
_BYTE *v3; // rax
__int64 result; // rax

v1 = malloc(8uLL);
*a1 = v1;
v2 = v1;
v3 = malloc(0x40uLL);
*v3 = 0;
*v2 = v3;
result = 0LL;
_mm_mfence();
return result;
}

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

v1 = malloc(8uLL);
*a1 = v1;
v2 = v1;
v3 = malloc(0x40uLL);
*v3 = 0;
*v2 = v3;
result = 0LL;
_mm_mfence();
return result;
}

original_code:
int init_clh_global(clh_global_params* the_params) {
    the_params->the_lock=(clh_lock*)malloc(sizeof(clh_lock));
    clh_qnode * a_node = (clh_qnode *) malloc(sizeof(clh_qnode));
    a_node->locked=0;
    *(the_params->the_lock) = a_node;
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int init_clh_global(struct clh_global_params *the_params) {
    the_params->the_lock=(struct clh_qnode **)malloc(sizeof(clh_lock));
    struct clh_qnode *a_node = (clh_qnode *) malloc(sizeof(clh_qnode));
    a_node->locked=0;
    *(the_params->the_lock) = a_node;
    __sync_synchronize();
    return 0;
}

variable_types: struct clh_qnode { unsigned char locked; unsigned char padding[-1]; } *


decompiled_code:
__int64 __fastcall init_clh_local(int a1, _QWORD *a2)
{
_BYTE *v2; // rax
__int64 result; // rax

set_cpu(a1);
v2 = malloc(0x40uLL);
a2[1] = 0LL;
*a2 = v2;
*v2 = 0;
result = 0LL;
_mm_mfence();
return result;
}

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

func2(a1);
v2 = malloc(0x40uLL);
a2[1] = 0LL;
*a2 = v2;
*v2 = 0;
result = 0LL;
_mm_mfence();
return result;
}

original_code:
int init_clh_local(uint32_t thread_num, clh_local_params* local_params) {
    set_cpu(thread_num);
    local_params->my_qnode = (clh_qnode*) malloc(sizeof(clh_qnode));
    local_params->my_qnode->locked=0;
    local_params->my_pred = ((void *)0);
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int init_clh_local(unsigned int thread_num, struct clh_local_params *local_params) {
    set_cpu(thread_num);
    local_params->my_qnode = (struct clh_qnode *) malloc(sizeof(clh_qnode));
    local_params->my_qnode->locked=0;
    local_params->my_pred = ((void *)0);
    __sync_synchronize();
    return 0;
}

user_defined_types: struct clh_qnode { unsigned char locked; unsigned char padding[-1]; }


decompiled_code:
void end_clh_local()
{
;
}

canonical_decompiled_code:
void func11()
{
;
}

original_code:
void end_clh_local(clh_local_params the_params){
}

canonical_original_code:
void end_clh_local(struct clh_local_params the_params){
}

variable_types: struct clh_local_params { struct clh_qnode { unsigned char locked; unsigned char padding[-1]; } *my_qnode; struct clh_qnode *my_pred; }


decompiled_code:
void __fastcall end_clh_global(int a1, int a2, int a3, int a4, int a5, int a6, void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void __fastcall func12(int a1, int a2, int a3, int a4, int a5, int a6, void *ptr)
{
free(ptr);
}

original_code:
void end_clh_global(clh_global_params the_lock) {
    free(the_lock.the_lock);
}

canonical_original_code:
void end_clh_global(struct clh_global_params the_lock) {
    free(the_lock.the_lock);
}

variable_types: struct clh_global_params { struct clh_qnode { unsigned char locked; unsigned char padding[-1]; } **the_lock; unsigned char padding[-1]; }


decompiled_code:
char __fastcall tas_uint8(char *a1)
{
char result; // al

result = *a1;
*a1 = -1;
return result;
}

canonical_decompiled_code:
char __fastcall func0(char *a1)
{
char result; // al

result = *a1;
*a1 = -1;
return result;
}

original_code:
static inline uint8_t tas_uint8(volatile uint8_t *addr) {
    uint8_t oldval;
    __asm__ __volatile__("xchgb %0,%1"
            : "=q"(oldval), "=m"(*addr)
            : "0"((unsigned char) 0xff), "m"(*addr) : "memory");
    return (uint8_t) oldval;
}

canonical_original_code:
static inline unsigned char tas_uint8(unsigned char *addr) {
    unsigned char oldval;
    __asm__ __volatile__("xchgb %0,%1"
            : "=q"(oldval), "=m"(*addr)
            : "0"((unsigned char) 0xff), "m"(*addr) : "memory");
    return (unsigned char) oldval;
}

variable_types:
addr
unsigned char *

variable_types:
oldval
unsigned char


decompiled_code:
__int64 __fastcall get_cluster(int a1)
{
return (unsigned int)(a1 / 8);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return (unsigned int)(a1 / 8);
}

original_code:
static inline int get_cluster(int thread_id) {
        return thread_id/8;
    }


getticks

decompiled_code:
unsigned __int64 getticks()
{
return __rdtsc();
}

canonical_decompiled_code:
unsigned __int64 func2()
{
return __rdtsc();
}

original_code:
static inline ticks getticks(void)
    {
        unsigned hi, lo;
        __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
        return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
    }

canonical_original_code:
static inline unsigned long getticks()
    {
        unsigned int hi; unsigned int lo;
        __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
        return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
    }

variable_types:
hi
unsigned int

variable_types:
lo
unsigned int


canonical_decompiled_code:
int __fastcall func3(int a1)
{
unsigned int v1; // eax
pthread_t v2; // rax
int result; // eax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a1 <= 0x3FF )
v4.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
v1 = func1(a1);
numa_set_preferred(v1);
v2 = pthread_self();
result = pthread_setaffinity_np(v2, 0x80uLL, &v4);
if ( result )
result = fwrite("Error setting thread affinity\n", 1uLL, 0x1EuLL, stderr);
return result;
}

variable_types: cpu int

variable_types: struct cpu_set_t { unsigned long __bits[-1]; }


decompiled_code:
__int64 seed_rand()
{
unsigned __int64 v0; // rax
unsigned __int64 *v1; // r8
unsigned __int64 v2; // rax
__int64 v3; // r8
unsigned __int64 v4; // rcx
__int64 v5; // r8
__int64 result; // rax

memalign(0x40uLL, 0x40uLL);
v0 = getticks();
*v1 = v0 % 0x75BCD15;
v2 = getticks();
*(_QWORD *)(v3 + 8) = v2 % 0x159A55E5;
v4 = getticks();
result = v5;
*(_QWORD *)(v5 + 16) = v4 % 0x1F123BB5;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned __int64 v0; // rax
unsigned __int64 *v1; // r8
unsigned __int64 v2; // rax
__int64 v3; // r8
unsigned __int64 v4; // rcx
__int64 v5; // r8
__int64 result; // rax

memalign(0x40uLL, 0x40uLL);
v0 = func2();
*v1 = v0 % 0x75BCD15;
v2 = func2();
*(_QWORD *)(v3 + 8) = v2 % 0x159A55E5;
v4 = func2();
result = v5;
*(_QWORD *)(v5 + 16) = v4 % 0x1F123BB5;
return result;
}

original_code:
static inline unsigned long* seed_rand() {
        unsigned long* seeds;
        int num_seeds = 64/sizeof(unsigned long);
        if (num_seeds<3) num_seeds=3;
        seeds = (unsigned long*) memalign(64, num_seeds * sizeof(unsigned long));
        seeds[0] = getticks() % 123456789;
        seeds[1] = getticks() % 362436069;
        seeds[2] = getticks() % 521288629;
        return seeds;
    }

canonical_original_code:
static inline unsigned long *seed_rand() {
        unsigned long *seeds;
        int num_seeds = 64/sizeof(unsigned long);
        if (num_seeds<3) num_seeds=3;
        seeds = (unsigned long *) memalign(64, num_seeds * sizeof(unsigned long));
        seeds[0] = getticks() % 123456789;
        seeds[1] = getticks() % 362436069;
        seeds[2] = getticks() % 521288629;
        return seeds;
    }

variable_types:
seeds
unsigned long *


decompiled_code:
_BOOL8 __fastcall spinlock_trylock(char *a1)
{
return tas_uint8(a1) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(char *a1)
{
return func0(a1) != 0;
}

original_code:
int spinlock_trylock(spinlock_lock_t* the_lock, uint32_t* limits) {
    if (tas_uint8(&(the_lock->lock))==0) return 0;
    return 1;
}

canonical_original_code:
int spinlock_trylock(struct spinlock_lock_t *the_lock, unsigned int *limits) {
    if (tas_uint8(&(the_lock->lock))==0) return 0;
    return 1;
}

variable_types: struct spinlock_lock_t { union { unsigned char lock; unsigned char padding[64]; }; } *

variable_types:
limits
unsigned int *


decompiled_code:
char __fastcall spinlock_lock(char *a1)
{
char result; // al

while ( 1 )
{
result = tas_uint8(a1);
if ( !result )
break;
_mm_pause();
}
return result;
}

canonical_decompiled_code:
char __fastcall func6(char *a1)
{
char result; // al

while ( 1 )
{
result = func0(a1);
if ( !result )
break;
_mm_pause();
}
return result;
}

original_code:
void
spinlock_lock(spinlock_lock_t* the_lock, uint32_t* limits)
{
    volatile spinlock_lock_data_t* l = &(the_lock->lock);
    while (tas_uint8(l))
    {
        _mm_pause();
    }
}

canonical_original_code:
void spinlock_lock(struct spinlock_lock_t *the_lock, unsigned int *limits)
{
    unsigned char *l = &(the_lock->lock);
    while (tas_uint8(l))
    {
        _mm_pause();
    }
}

variable_types:
l
unsigned char *

user_defined_types: struct spinlock_lock_t { union { unsigned char lock; unsigned char padding[64]; }; }


decompiled_code:
_BOOL8 __fastcall is_free_spinlock(_BYTE *a1)
{
return *a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(_BYTE *a1)
{
return *a1 == 0;
}

original_code:
int is_free_spinlock(spinlock_lock_t * the_lock){
    if (the_lock->lock==0) return 1;
    return 0;
}

canonical_original_code:
int is_free_spinlock(struct spinlock_lock_t *the_lock){
    if (the_lock->lock==0) return 1;
    return 0;
}


decompiled_code:
// attributes: thunk
void end_spinlock_array_local(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func9(void *ptr)
{
free(ptr);
}

original_code:
void end_spinlock_array_local(uint32_t* limits)
{
    free(limits);
}

canonical_original_code:
void end_spinlock_array_local(unsigned int *limits)
{
    free(limits);
}


decompiled_code:
// attributes: thunk
void __fastcall end_spinlock_array_global(void *ptr)
{
end_spinlock_array_local(ptr);
}

canonical_decompiled_code:
void __fastcall func10(void *ptr)
{
func9(ptr);
}

original_code:
void end_spinlock_array_global(spinlock_lock_t* the_locks)
{
    free(the_locks);
}

canonical_original_code:
void end_spinlock_array_global(struct spinlock_lock_t *the_locks)
{
    free(the_locks);
}


decompiled_code:
__int64 __fastcall init_spinlock_global(_BYTE *a1)
{
__int64 result; // rax

*a1 = 0;
result = 0LL;
_mm_mfence();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(_BYTE *a1)
{
__int64 result; // rax

*a1 = 0;
result = 0LL;
_mm_mfence();
return result;
}

original_code:
int init_spinlock_global(spinlock_lock_t* the_lock)
{
    the_lock->lock = 0;
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int init_spinlock_global(struct spinlock_lock_t *the_lock)
{
    the_lock->lock = 0;
    __sync_synchronize();
    return 0;
}


decompiled_code:
__int64 __fastcall init_spinlock_local(int a1, _DWORD *a2)
{
__int64 result; // rax

set_cpu(a1);
*a2 = 1;
__writefsqword((unsigned int)&spinlock_seeds, seed_rand());
result = 0LL;
_mm_mfence();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, _DWORD *a2)
{
__int64 result; // rax

func3(a1);
*a2 = 1;
__writefsqword((unsigned int)&spinlock_seeds, seed_rand());
result = 0LL;
_mm_mfence();
return result;
}

original_code:
int init_spinlock_local(uint32_t thread_num, uint32_t* limit)
{
    set_cpu(thread_num);
    *limit = 1;
    spinlock_seeds = seed_rand();
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int init_spinlock_local(unsigned int thread_num, unsigned int *limit)
{
    set_cpu(thread_num);
    *limit = 1;
    spinlock_seeds = seed_rand();
    __sync_synchronize();
    return 0;
}

variable_types:
limit
unsigned int *


decompiled_code:
void end_spinlock_local()
{
;
}

canonical_decompiled_code:
void func13()
{
;
}

original_code:
void end_spinlock_local()
{
}

canonical_original_code:
void end_spinlock_local()
{
}


decompiled_code:
void end_spinlock_global()
{
;
}

canonical_decompiled_code:
void func14()
{
;
}

original_code:
void end_spinlock_global()
{
}

canonical_original_code:
void end_spinlock_global()
{
}


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

canonical_original_code:
static inline int get_cluster(int thread_id) {
        return thread_id/8;
    }


decompiled_code:
void __fastcall nop_rep(int a1)
{
int i; // eax

if ( a1 )
{
for ( i = 0; i != a1; ++i )
;
}
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
int i; // eax

if ( a1 )
{
for ( i = 0; i != a1; ++i )
;
}
}

original_code:
static inline void
        nop_rep(uint32_t num_reps)
        {
            uint32_t i;
            for (i = 0; i < num_reps; i++)
            {
                asm volatile ("NOP");
            }
        }

canonical_original_code:
static inline void nop_rep(unsigned int num_reps)
        {
            unsigned int i;
            for (i = 0; i < num_reps; i++)
            {
                asm volatile ("NOP");
            }
        }

variable_types:
i
unsigned int


decompiled_code:
int __fastcall set_cpu(int a1)
{
unsigned int v1; // eax
pthread_t v2; // rax
int result; // eax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a1 <= 0x3FF )
v4.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
v1 = get_cluster(a1);
numa_set_preferred(v1);
v2 = pthread_self();
result = pthread_setaffinity_np(v2, 0x80uLL, &v4);
if ( result )
result = fwrite("Error setting thread affinity\n", 1uLL, 0x1EuLL, stderr);
return result;
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
unsigned int v1; // eax
pthread_t v2; // rax
int result; // eax
cpu_set_t v4; // [rsp+0h] [rbp-88h] BYREF

memset(&v4, 0, sizeof(v4));
if ( (unsigned __int64)a1 <= 0x3FF )
v4.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
v1 = func0(a1);
numa_set_preferred(v1);
v2 = pthread_self();
result = pthread_setaffinity_np(v2, 0x80uLL, &v4);
if ( result )
result = fwrite("Error setting thread affinity\n", 1uLL, 0x1EuLL, stderr);
return result;
}

original_code:
static inline void set_cpu(int cpu) {
        cpu_set_t mask;
        do __builtin_memset (&mask, '\0', sizeof (cpu_set_t)); while (0);
        (__extension__ ({ size_t __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&mask)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
        numa_set_preferred(get_cluster(cpu));
        pthread_t thread = pthread_self();
        if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
            fprintf(stderr, "Error setting thread affinity\n");
        }
    }

canonical_original_code:
static inline void set_cpu(int cpu) {
        struct cpu_set_t mask;
        do __builtin_memset (&mask, '\0', sizeof (cpu_set_t)); while (0);
        (__extension__ ({ unsigned long __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&mask)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
        numa_set_preferred(get_cluster(cpu));
        unsigned long thread = pthread_self();
        if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
            fprintf(stderr, "Error setting thread affinity\n");
        }
    }

variable_types:
thread
unsigned long


decompiled_code:
_BOOL8 __fastcall ticket_trylock(__int64 a1)
{
__int64 v1; // rdx

v1 = *(unsigned int *)(a1 + 4);
return _InterlockedCompareExchange64(
(volatile signed __int64 *)a1,
0x100000001LL * (unsigned int)(v1 + 1),
(unsigned int)(v1 + 1) + (v1 << 32)) != (unsigned int)(v1 + 1) + (v1 << 32);
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(__int64 a1)
{
__int64 v1; // rdx

v1 = *(unsigned int *)(a1 + 4);
return _InterlockedCompareExchange64(
(volatile signed __int64 *)a1,
0x100000001LL * (unsigned int)(v1 + 1),
(unsigned int)(v1 + 1) + (v1 << 32)) != (unsigned int)(v1 + 1) + (v1 << 32);
}

original_code:
int
ticket_trylock(ticketlock_t* lock)
{
  uint32_t me = lock->tail;
  uint32_t me_new = me + 1;
  uint64_t cmp = ((uint64_t) me << 32) + me_new;
  uint64_t cmp_new = ((uint64_t) me_new << 32) + me_new;
  uint64_t* la = (uint64_t*) lock;
  if (__sync_val_compare_and_swap(la,cmp,cmp_new) == cmp)
    {
      return 0;
    }
  return 1;
}

canonical_original_code:
int ticket_trylock(struct ticketlock_t *lock)
{
  unsigned int me = lock->tail;
  unsigned int me_new = me + 1;
  unsigned long cmp = ((uint64_t) me << 32) + me_new;
  unsigned long cmp_new = ((uint64_t) me_new << 32) + me_new;
  unsigned long *la = (uint64_t*) lock;
  if (__sync_val_compare_and_swap(la,cmp,cmp_new) == cmp)
    {
      return 0;
    }
  return 1;
}

variable_types: struct ticketlock_t { unsigned int head; unsigned int tail; unsigned char padding1[-1]; } *

variable_types:
me
unsigned int

variable_types:
cmp
unsigned long

variable_types:
la
unsigned long *


decompiled_code:
__int64 __fastcall create_ticketlock(_DWORD *a1)
{
__int64 result; // rax

*a1 = 1;
result = 0LL;
a1[1] = 0;
_mm_mfence();
return result;
}

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

*a1 = 1;
result = 0LL;
a1[1] = 0;
_mm_mfence();
return result;
}

original_code:
int create_ticketlock(ticketlock_t* the_lock)
{
    the_lock->head=1;
    the_lock->tail=0;
    __sync_synchronize();
    return 0;
}

canonical_original_code:
int create_ticketlock(struct ticketlock_t *the_lock)
{
    the_lock->head=1;
    the_lock->tail=0;
    __sync_synchronize();
    return 0;
}

user_defined_types: struct ticketlock_t { unsigned int head; unsigned int tail; unsigned char padding1[-1]; }


decompiled_code:
_BOOL8 __fastcall is_free_ticket(_DWORD *a1)
{
return *a1 - a1[1] == 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(_DWORD *a1)
{
return *a1 - a1[1] == 1;
}

original_code:
int is_free_ticket(ticketlock_t* t)
{
  if ((t->head - t->tail) == 1)
    {
      return 1;
    }
  return 0;
}

canonical_original_code:
int is_free_ticket(struct ticketlock_t *t)
{
  if ((t->head - t->tail) == 1)
    {
      return 1;
    }
  return 0;
}


decompiled_code:
// attributes: thunk
int __fastcall init_thread_ticketlocks(int a1)
{
return set_cpu(a1);
}

canonical_decompiled_code:
int __fastcall func9(int a1)
{
return func3(a1);
}

original_code:
void init_thread_ticketlocks(uint32_t thread_num)
{
  set_cpu(thread_num);
}

canonical_original_code:
void init_thread_ticketlocks(unsigned int thread_num)
{
  set_cpu(thread_num);
}


decompiled_code:
// attributes: thunk
void free_ticketlocks(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func10(void *ptr)
{
free(ptr);
}

original_code:
void
free_ticketlocks(ticketlock_t* the_locks)
{
  free(the_locks);
}

canonical_original_code:
void free_ticketlocks(struct ticketlock_t *the_locks)
{
  free(the_locks);
}


decompiled_code:
int ticket_print_contention_stats()
{
double v0; // xmm0_8
double v1; // xmm1_8

if ( ticket_queued_total < 0 )
{
v0 = (double)(int)(ticket_queued_total & 1 | ((unsigned __int64)ticket_queued_total >> 1))
+ (double)(int)(ticket_queued_total & 1 | ((unsigned __int64)ticket_queued_total >> 1));
if ( ticket_acquires >= 0 )
goto LABEL_3;
LABEL_6:
v1 = (double)(int)(ticket_acquires & 1 | ((unsigned __int64)ticket_acquires >> 1))
+ (double)(int)(ticket_acquires & 1 | ((unsigned __int64)ticket_acquires >> 1));
return printf(
"#Acquires: %10llu / #Total queuing: %10llu / Avg. queuing: %.3f\n",
ticket_acquires,
ticket_queued_total,
v0 / v1);
}
v0 = (double)(int)ticket_queued_total;
if ( ticket_acquires < 0 )
goto LABEL_6;
LABEL_3:
v1 = (double)(int)ticket_acquires;
return printf(
"#Acquires: %10llu / #Total queuing: %10llu / Avg. queuing: %.3f\n",
ticket_acquires,
ticket_queued_total,
v0 / v1);
}

canonical_decompiled_code:
int func11()
{
double v0; // xmm0_8
double v1; // xmm1_8

if ( ticket_queued_total < 0 )
{
v0 = (double)(int)(ticket_queued_total & 1 | ((unsigned __int64)ticket_queued_total >> 1))
+ (double)(int)(ticket_queued_total & 1 | ((unsigned __int64)ticket_queued_total >> 1));
if ( ticket_acquires >= 0 )
goto LABEL_3;
LABEL_6:
v1 = (double)(int)(ticket_acquires & 1 | ((unsigned __int64)ticket_acquires >> 1))
+ (double)(int)(ticket_acquires & 1 | ((unsigned __int64)ticket_acquires >> 1));
return printf(
"#Acquires: %10llu / #Total queuing: %10llu / Avg. queuing: %.3f\n",
ticket_acquires,
ticket_queued_total,
v0 / v1);
}
v0 = (double)(int)ticket_queued_total;
if ( ticket_acquires < 0 )
goto LABEL_6;
LABEL_3:
v1 = (double)(int)ticket_acquires;
return printf(
"#Acquires: %10llu / #Total queuing: %10llu / Avg. queuing: %.3f\n",
ticket_acquires,
ticket_queued_total,
v0 / v1);
}

original_code:
void
ticket_print_contention_stats()
{
  double avg_q = ticket_queued_total / (double) ticket_acquires;
  printf("#Acquires: %10llu / #Total queuing: %10llu / Avg. queuing: %.3f\n",
  (long long unsigned) ticket_acquires, (long long unsigned) ticket_queued_total, avg_q);
}

canonical_original_code:
void ticket_print_contention_stats()
{
  double avg_q = ticket_queued_total / (double) ticket_acquires;
  printf("#Acquires: %10llu / #Total queuing: %10llu / Avg. queuing: %.3f\n",
  (unsigned long long) ticket_acquires, (unsigned long long) ticket_queued_total, avg_q);
}


decompiled_code:
double ticket_avg_queue()
{
double v0; // xmm0_8
unsigned __int64 v1; // rax

if ( ticket_queued_total < 0 )
{
v1 = ticket_acquires;
v0 = (double)(int)(ticket_queued_total & 1 | ((unsigned __int64)ticket_queued_total >> 1))
+ (double)(int)(ticket_queued_total & 1 | ((unsigned __int64)ticket_queued_total >> 1));
if ( ticket_acquires >= 0 )
return v0 / (double)(int)v1;
}
else
{
v0 = (double)(int)ticket_queued_total;
v1 = ticket_acquires;
if ( ticket_acquires >= 0 )
return v0 / (double)(int)v1;
}
return v0 / ((double)(int)(v1 & 1 | (v1 >> 1)) + (double)(int)(v1 & 1 | (v1 >> 1)));
}

canonical_decompiled_code:
double func12()
{
double v0; // xmm0_8
unsigned __int64 v1; // rax

if ( ticket_queued_total < 0 )
{
v1 = ticket_acquires;
v0 = (double)(int)(ticket_queued_total & 1 | ((unsigned __int64)ticket_queued_total >> 1))
+ (double)(int)(ticket_queued_total & 1 | ((unsigned __int64)ticket_queued_total >> 1));
if ( ticket_acquires >= 0 )
return v0 / (double)(int)v1;
}
else
{
v0 = (double)(int)ticket_queued_total;
v1 = ticket_acquires;
if ( ticket_acquires >= 0 )
return v0 / (double)(int)v1;
}
return v0 / ((double)(int)(v1 & 1 | (v1 >> 1)) + (double)(int)(v1 & 1 | (v1 >> 1)));
}

original_code:
double
ticket_avg_queue()
{
  double avg_q = ticket_queued_total / (double) ticket_acquires;
  return avg_q;
}

return_type: double


decompiled_code:
__int64 __fastcall local_lock_write(volatile signed __int32 *a1)
{
signed __int32 v1; // edx
__int64 result; // rax

do
{
while ( *((_WORD *)a1 + 1) )
;
v1 = *(unsigned __int16 *)a1;
result = (unsigned int)_InterlockedCompareExchange(a1, v1 + 256, v1);
}
while ( (_DWORD)result != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(volatile signed __int32 *a1)
{
signed __int32 v1; // edx
__int64 result; // rax

do
{
while ( *((_WORD *)a1 + 1) )
;
v1 = *(unsigned __int16 *)a1;
result = (unsigned int)_InterlockedCompareExchange(a1, v1 + 256, v1);
}
while ( (_DWORD)result != v1 );
return result;
}

original_code:
void local_lock_write(global_lock* gl) {
    while(1) {
        while (gl->lock_short.global_lock != 0) {}
        unsigned int aux = (unsigned int) gl->lock_short.local_lock;
        if (__sync_val_compare_and_swap(&gl->lock_data, aux,aux+0x100) == (aux)) {
            return;
        }
    }
}

canonical_original_code:
void local_lock_write(struct global_lock *gl) {
    while(1) {
        while (gl->lock_short.global_lock != 0) {}
        unsigned int aux = (unsigned int) gl->lock_short.local_lock;
        if (__sync_val_compare_and_swap(&gl->lock_data, aux,aux+0x100) == (aux)) {
            return;
        }
    }
}

variable_types: struct global_lock { union { unsigned int lock_data; struct glock_2 { unsigned short local_lock; unsigned short global_lock; } lock_short; struct glock { unsigned char local_read; unsigned char local_write; unsigned char global_read; unsigned char global_write; } lock; unsigned char padding[64]; }; } *

variable_types:
aux
unsigned int

user_defined_types: struct global_lock { union { unsigned int lock_data; struct glock_2 lock_short; struct glock lock; unsigned char padding[64]; }; }


decompiled_code:
void __fastcall local_unlock_write(__int64 a1)
{
_InterlockedSub8((volatile signed __int8 *)(a1 + 1), 1u);
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
_InterlockedSub8((volatile signed __int8 *)(a1 + 1), 1u);
}

original_code:
void local_unlock_write(global_lock* gl){
    __sync_sub_and_fetch(&(gl->lock.local_write),1);
}

canonical_original_code:
void local_unlock_write(struct global_lock *gl){
    __sync_sub_and_fetch(&(gl->lock.local_write),1);
}

user_defined_types: struct glock_2 { unsigned short local_lock; unsigned short global_lock; }

user_defined_types: struct glock { unsigned char local_read; unsigned char local_write; unsigned char global_read; unsigned char global_write; }


decompiled_code:
__int64 __fastcall local_lock_read(__int64 a1)
{
int v1; // edx
__int64 result; // rax

do
{
while ( *(_BYTE *)(a1 + 3) )
;
v1 = *(_DWORD *)a1 & 0xFFFFFF;
result = (unsigned int)_InterlockedCompareExchange((volatile signed __int32 *)a1, v1 + 1, v1);
}
while ( (_DWORD)result != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int v1; // edx
__int64 result; // rax

do
{
while ( *(_BYTE *)(a1 + 3) )
;
v1 = *(_DWORD *)a1 & 0xFFFFFF;
result = (unsigned int)_InterlockedCompareExchange((volatile signed __int32 *)a1, v1 + 1, v1);
}
while ( (_DWORD)result != v1 );
return result;
}

original_code:
void local_lock_read(global_lock* gl) {
    while(1) {
        while (gl->lock.global_write != 0) {}
        unsigned int aux = (unsigned int) gl->lock_data & 0x00ffffff;
        if (__sync_val_compare_and_swap(&gl->lock_data, aux,aux+1) == (aux)) {
            return;
        }
    }
}

canonical_original_code:
void local_lock_read(struct global_lock *gl) {
    while(1) {
        while (gl->lock.global_write != 0) {}
        unsigned int aux = (unsigned int) gl->lock_data & 0x00ffffff;
        if (__sync_val_compare_and_swap(&gl->lock_data, aux,aux+1) == (aux)) {
            return;
        }
    }
}


decompiled_code:
void __fastcall local_unlock_read(volatile signed __int8 *a1)
{
_InterlockedSub8(a1, 1u);
}

canonical_decompiled_code:
void __fastcall func3(volatile signed __int8 *a1)
{
_InterlockedSub8(a1, 1u);
}

original_code:
void local_unlock_read(global_lock* gl){
    __sync_sub_and_fetch(&(gl->lock.local_read),1);
}

canonical_original_code:
void local_unlock_read(struct global_lock *gl){
    __sync_sub_and_fetch(&(gl->lock.local_read),1);
}


decompiled_code:
__int64 __fastcall global_acquire_write(volatile signed __int32 *a1)
{
__int64 result; // rax

do
{
while ( *a1 )
;
result = (unsigned int)_InterlockedCompareExchange(a1, 0, 0);
}
while ( (_DWORD)result );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(volatile signed __int32 *a1)
{
__int64 result; // rax

do
{
while ( *a1 )
;
result = (unsigned int)_InterlockedCompareExchange(a1, 0, 0);
}
while ( (_DWORD)result );
return result;
}

original_code:
void global_acquire_write(global_lock* gl) {
    while(1) {
        while (gl->lock_data != 0) {}
        unsigned short aux = (unsigned short) 0x1000000;
        if (__sync_val_compare_and_swap(&gl->lock_data, 0, aux) == 0) {
            return;
        }
    }
}

canonical_original_code:
void global_acquire_write(struct global_lock *gl) {
    while(1) {
        while (gl->lock_data != 0) {}
        unsigned short aux = (unsigned short) 0x1000000;
        if (__sync_val_compare_and_swap(&gl->lock_data, 0, aux) == 0) {
            return;
        }
    }
}

variable_types:
aux
unsigned short


decompiled_code:
__int64 __fastcall global_acquire_read(__int64 a1)
{
int v1; // ecx
__int64 result; // rax

while ( 1 )
{
while ( *(_BYTE *)(a1 + 3) )
;
if ( !*(_BYTE *)(a1 + 1) )
{
v1 = *(_DWORD *)a1 & 0xFF00FF;
result = (unsigned int)_InterlockedCompareExchange((volatile signed __int32 *)a1, v1 + 0x10000, v1);
if ( (_DWORD)result == v1 )
break;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int v1; // ecx
__int64 result; // rax

while ( 1 )
{
while ( *(_BYTE *)(a1 + 3) )
;
if ( !*(_BYTE *)(a1 + 1) )
{
v1 = *(_DWORD *)a1 & 0xFF00FF;
result = (unsigned int)_InterlockedCompareExchange((volatile signed __int32 *)a1, v1 + 0x10000, v1);
if ( (_DWORD)result == v1 )
break;
}
}
return result;
}

original_code:
void global_acquire_read(global_lock* gl) {
    while(1) {
        while ((gl->lock.global_write != 0) || (gl->lock.local_write != 0)) {}
        unsigned int aux = (unsigned int) gl->lock_data & 0x00ff00ff;
        if (__sync_val_compare_and_swap(&gl->lock_data, aux,aux+0x10000) == (aux)) {
            return;
        }
    }
}

canonical_original_code:
void global_acquire_read(struct global_lock *gl) {
    while(1) {
        while ((gl->lock.global_write != 0) || (gl->lock.local_write != 0)) {}
        unsigned int aux = (unsigned int) gl->lock_data & 0x00ff00ff;
        if (__sync_val_compare_and_swap(&gl->lock_data, aux,aux+0x10000) == (aux)) {
            return;
        }
    }
}


decompiled_code:
void __fastcall global_unlock_read(__int64 a1)
{
_InterlockedSub8((volatile signed __int8 *)(a1 + 2), 1u);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
_InterlockedSub8((volatile signed __int8 *)(a1 + 2), 1u);
}

original_code:
void global_unlock_read(global_lock* gl){
    __sync_sub_and_fetch(&(gl->lock.global_read),1);
}

canonical_original_code:
void global_unlock_read(struct global_lock *gl){
    __sync_sub_and_fetch(&(gl->lock.global_read),1);
}


propname

decompiled_code:
char *__fastcall propname(int a1)
{
unsigned int v1; // edi
char *result; // rax

v1 = a1 - 2048;
result = (char *)&unk_7C0;
if ( v1 <= 8 )
result = namestr[v1];
return result;
}

canonical_decompiled_code:
char *__fastcall func0(int a1)
{
unsigned int v1; // edi
char *result; // rax

v1 = a1 - 2048;
result = (char *)&unk_7C0;
if ( v1 <= 8 )
result = namestr[v1];
return result;
}

original_code:
static char *propname(int prop) {
    if (prop < vLOCAL || prop >= pEND) return "";
    return namestr[prop-vLOCAL];
}

variable_types: prop int

return_type: char *


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

v0 = scope_cnt + 1;
scope_cnt = v0;
if ( v0 > 99 )
{
parse_error("too much scopes");
result = 0LL;
}
else
{
result = scope_buf + 16LL * v0;
*(_QWORD *)(result + 8) = (unsigned int)(symcnt + 1);
}
return result;
}

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

v0 = scope_cnt + 1;
scope_cnt = v0;
if ( v0 > 99 )
{
parse_error("too much scopes");
result = 0LL;
}
else
{
result = scope_buf + 16LL * v0;
*(_QWORD *)(result + 8) = (unsigned int)(symcnt + 1);
}
return result;
}

original_code:
static scope *new_scope() {
    scope *sp = 0;
    if (++scope_cnt < 100) {
      sp = &scope_buf[scope_cnt];
      sp->begin = symcnt+1;
      sp->end = 0;
    } else {
 parse_error("too much scopes");
    }
    return sp;
}

canonical_original_code:
static struct scope *new_scope() {
    struct scope *sp = 0;
    if (++scope_cnt < 100) {
      sp = &scope_buf[scope_cnt];
      sp->begin = symcnt+1;
      sp->end = 0;
    } else {
 parse_error("too much scopes");
    }
    return sp;
}

variable_types: struct scope { struct scope *next; int begin; int end; } *


gen

decompiled_code:
char *__fastcall gen(int a1)
{
int v1; // ecx
__int64 v2; // rdx
int *v3; // rax
__int64 v4; // rcx
char s[18]; // [rsp+0h] [rbp-12h] BYREF

if ( a1 == 2052 )
{
v1 = con_seq;
v2 = 67LL;
v3 = &con_seq;
goto LABEL_6;
}
if ( a1 > 2052 )
{
if ( a1 == 2055 )
{
v1 = type_seq;
v2 = 84LL;
v3 = &type_seq;
goto LABEL_6;
}
}
else
{
if ( a1 == 2048 )
{
v1 = var_seq;
v2 = 86LL;
v3 = &var_seq;
goto LABEL_6;
}
if ( a1 == 2050 )
{
v1 = func_seq;
v2 = 70LL;
v3 = &func_seq;
LABEL_6:
v4 = (unsigned int)(v1 + 1);
*v3 = v4;
sprintf(s, "$%c%04d", v2, v4);
return strdup(s);
}
}
return "$$ERROR";
}

canonical_decompiled_code:
char *__fastcall func2(int a1)
{
int v1; // ecx
__int64 v2; // rdx
int *v3; // rax
__int64 v4; // rcx
char s[18]; // [rsp+0h] [rbp-12h] BYREF

if ( a1 == 2052 )
{
v1 = con_seq;
v2 = 67LL;
v3 = &con_seq;
goto LABEL_6;
}
if ( a1 > 2052 )
{
if ( a1 == 2055 )
{
v1 = type_seq;
v2 = 84LL;
v3 = &type_seq;
goto LABEL_6;
}
}
else
{
if ( a1 == 2048 )
{
v1 = var_seq;
v2 = 86LL;
v3 = &var_seq;
goto LABEL_6;
}
if ( a1 == 2050 )
{
v1 = func_seq;
v2 = 70LL;
v3 = &func_seq;
LABEL_6:
v4 = (unsigned int)(v1 + 1);
*v3 = v4;
sprintf(s, "$%c%04d", v2, v4);
return strdup(s);
}
}
return "$$ERROR";
}

original_code:
char *gen(int kind) {
    char buf[10];
    int *p=0;
    int c;
    switch (kind) {
       case vLOCAL: p = &var_seq; c = 'V'; break;
       case cLOCAL: p = &con_seq; c = 'C'; break;
       case fLOCAL: p = &func_seq; c = 'F'; break;
       case tGLOBAL: p = &type_seq; c = 'T'; break;
       default: break;
    }
    if (p) {
        sprintf(buf, "$%c%04d", c, ++(*p));
        return strdup(buf);
    } else
        return "$$ERROR";
}

canonical_original_code:
char *gen(int kind) {
    char buf[10];
    int *p = 0;
    int c;
    switch (kind) {
       case vLOCAL: p = &var_seq; c = 'V'; break;
       case cLOCAL: p = &con_seq; c = 'C'; break;
       case fLOCAL: p = &func_seq; c = 'F'; break;
       case tGLOBAL: p = &type_seq; c = 'T'; break;
       default: break;
    }
    if (p) {
        sprintf(buf, "$%c%04d", c, ++(*p));
        return strdup(buf);
    } else
        return "$$ERROR";
}

variable_types: kind int

variable_types:
buf
char [10]

variable_types:
p
int *

variable_types: c int


decompiled_code:
__int64 init_SYM()
{
void *v0; // rax
void *v1; // rax
__int64 result; // rax

v0 = calloc(0x5DD8uLL, 1uLL);
symcnt = 0;
symtab = (__int64)v0;
v1 = calloc(0x650uLL, 1uLL);
scope_cnt = 0;
scope_buf = (__int64)v1;
var_seq = 0;
con_seq = 0;
type_seq = 0;
result = new_scope();
cur = result;
return result;
}

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

v0 = calloc(0x5DD8uLL, 1uLL);
symcnt = 0;
symtab = (__int64)v0;
v1 = calloc(0x650uLL, 1uLL);
scope_cnt = 0;
scope_buf = (__int64)v1;
var_seq = 0;
con_seq = 0;
type_seq = 0;
result = func1();
cur = result;
return result;
}

original_code:
void init_SYM() {
    int sz;
    sz = (1000 +1) * sizeof(symentry);
    symtab = (symentry *)malloc(sz);
    bzero(symtab, sz);
    symcnt = 0;
    sz = (100 +1) * sizeof(scope);
    scope_buf = (scope *)malloc(sz);
    bzero(scope_buf, sz);
    scope_cnt = 0;
    var_seq = 0;
    con_seq = 0;
    type_seq = 0;
    cur_root = cur = new_scope();
}

canonical_original_code:
void init_SYM() {
    int sz;
    sz = (1000 +1) * sizeof(symentry);
    symtab = (struct symentry *)malloc(sz);
    bzero(symtab, sz);
    symcnt = 0;
    sz = (100 +1) * sizeof(scope);
    scope_buf = (struct scope *)malloc(sz);
    bzero(scope_buf, sz);
    scope_cnt = 0;
    var_seq = 0;
    con_seq = 0;
    type_seq = 0;
    cur_root = cur = new_scope();
}

variable_types: sz int

user_defined_types: struct symentry { char *name; int type; int prop; int val; int loc; }

user_defined_types: struct scope { struct scope *next; int begin; int end; }


decompiled_code:
void delete_scope()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

original_code:
void delete_scope(scope *sp) {
}

canonical_original_code:
void delete_scope(struct scope *sp) {
}