decompiled_code:
__int64 set_timeout_check()
{
return event_add(&cl_periodic_event, &cl_periodic_interval);
}

canonical_decompiled_code:
__int64 func9()
{
return event_add(&cl_periodic_event, &cl_periodic_interval);
}

original_code:
static void
set_timeout_check() {
    event_add(&cl_periodic_event, &cl_periodic_interval);
}


pusage

repo: dgomezferro/libpaxos

decompiled_code:
int pusage()
{
puts("benchmark_client options:");
puts("\t-c N : submit N values concurrently");
puts("\t-m N : min value size is N bytes");
puts("\t-M N : max value size is N bytes");
puts("\t-d N : duration is N seconds");
puts("\t-t N : submit timeout is N seconds");
puts("\t-p N : print submit count every N values");
puts("\t-s N : saves a latency sample every N values sent");
puts("\t-w N : after initialization is completed, wait N seconds before submitting");
return puts("\t-h : prints this message");
}

canonical_decompiled_code:
int func10()
{
puts("benchmark_client options:");
puts("\t-c N : submit N values concurrently");
puts("\t-m N : min value size is N bytes");
puts("\t-M N : max value size is N bytes");
puts("\t-d N : duration is N seconds");
puts("\t-t N : submit timeout is N seconds");
puts("\t-p N : print submit count every N values");
puts("\t-s N : saves a latency sample every N values sent");
puts("\t-w N : after initialization is completed, wait N seconds before submitting");
return puts("\t-h : prints this message");
}

original_code:
void pusage() {
    printf("benchmark_client options:\n");
    printf("\t-c N : submit N values concurrently\n");
    printf("\t-m N : min value size is N bytes\n");
    printf("\t-M N : max value size is N bytes\n");
    printf("\t-d N : duration is N seconds\n");
    printf("\t-t N : submit timeout is N seconds\n");
    printf("\t-p N : print submit count every N values\n");
    printf("\t-s N : saves a latency sample every N values sent\n");
    printf("\t-w N : after initialization is completed, wait N seconds before submitting\n");
    printf("\t-h   : prints this message\n");
}

return_type: void


decompiled_code:
int __fastcall parse_args(int argc, char **argv)
{
int result; // eax

while ( 1 )
{
result = getopt(argc, argv, "c:m:M:d:t:p:s:w:h");
if ( result == -1 )
break;
switch ( result )
{
case 'M':
max_val_size = atoi(optarg);
break;
case 'c':
concurrent_values = atoi(optarg);
break;
case 'd':
duration = atoi(optarg);
break;
case 'm':
min_val_size = atoi(optarg);
break;
case 'p':
print_step = atoi(optarg);
break;
case 's':
sample_frequency = atoi(optarg);
samples = (__int64)malloc(0xFA0uLL);
break;
case 't':
values_timeout = atoi(optarg);
break;
case 'w':
wait_after_init = atoi(optarg);
break;
default:
pusage();
exit(0);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(int argc, char **argv)
{
int result; // eax

while ( 1 )
{
result = getopt(argc, argv, "c:m:M:d:t:p:s:w:h");
if ( result == -1 )
break;
switch ( result )
{
case 'M':
max_val_size = atoi(optarg);
break;
case 'c':
concurrent_values = atoi(optarg);
break;
case 'd':
duration = atoi(optarg);
break;
case 'm':
min_val_size = atoi(optarg);
break;
case 'p':
print_step = atoi(optarg);
break;
case 's':
sample_frequency = atoi(optarg);
samples = (__int64)malloc(0xFA0uLL);
break;
case 't':
values_timeout = atoi(optarg);
break;
case 'w':
wait_after_init = atoi(optarg);
break;
default:
func10();
exit(0);
}
}
return result;
}

original_code:
void parse_args(int argc, char * const argv[]) {
    int c;
    while((c = getopt(argc, argv, "c:m:M:d:t:p:s:w:h")) != -1) {
        switch(c) {
            case 'c': {
                concurrent_values = atoi(optarg);
            }
            break;
            case 'm': {
                min_val_size = atoi(optarg);
            }
            break;
            case 'M': {
                max_val_size = atoi(optarg);
            }
            break;
            case 'd': {
                duration = atoi(optarg);
            }
            break;
            case 't': {
                values_timeout.tv_sec = atoi(optarg);
            }
            break;
            case 'p': {
                print_step = atoi(optarg);
            }
            break;
            case 's': {
                sample_frequency = atoi(optarg);
                samples = malloc(sizeof(double)*max_samples);
            }
            break;
            case 'w': {
                wait_after_init = atoi(optarg);
            }
            break;
            case 'h':
            default: {
                pusage();
                exit(0);
            }
        }
    }
}

canonical_original_code:
void parse_args(int argc, char *argv[]) {
    int c;
    while((c = getopt(argc, argv, "c:m:M:d:t:p:s:w:h")) != -1) {
        switch(c) {
            case 'c': {
                concurrent_values = atoi(optarg);
            }
            break;
            case 'm': {
                min_val_size = atoi(optarg);
            }
            break;
            case 'M': {
                max_val_size = atoi(optarg);
            }
            break;
            case 'd': {
                duration = atoi(optarg);
            }
            break;
            case 't': {
                values_timeout.tv_sec = atoi(optarg);
            }
            break;
            case 'p': {
                print_step = atoi(optarg);
            }
            break;
            case 's': {
                sample_frequency = atoi(optarg);
                samples = malloc(sizeof(double)*max_samples);
            }
            break;
            case 'w': {
                wait_after_init = atoi(optarg);
            }
            break;
            case 'h':
            default: {
                pusage();
                exit(0);
            }
        }
    }
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: c int


decompiled_code:
__int64 __fastcall random_value_gen(__int64 a1)
{
__int64 v1; // rsi
unsigned __int64 v2; // rax
unsigned __int64 v3; // rcx
unsigned __int64 v4; // rdx
bool v5; // cc
__int64 v6; // rax

v1 = random();
v2 = (int)(min_val_size + v1 % (max_val_size - min_val_size + 1));
v3 = v2 >> 3;
if ( v2 <= 7 )
{
v6 = 0LL;
}
else
{
v4 = 0LL;
do
*(_QWORD *)(a1 + 8 * v4++) = v1;
while ( v3 > v4 );
v5 = v2 <= 7;
v6 = 1LL;
if ( !v5 )
v6 = v3;
}
return 8 * v6;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
__int64 v1; // rsi
unsigned __int64 v2; // rax
unsigned __int64 v3; // rcx
unsigned __int64 v4; // rdx
bool v5; // cc
__int64 v6; // rax

v1 = random();
v2 = (int)(min_val_size + v1 % (max_val_size - min_val_size + 1));
v3 = v2 >> 3;
if ( v2 <= 7 )
{
v6 = 0LL;
}
else
{
v4 = 0LL;
do
*(_QWORD *)(a1 + 8 * v4++) = v1;
while ( v3 > v4 );
v5 = v2 <= 7;
v6 = 1LL;
if ( !v5 )
v6 = v3;
}
return 8 * v6;
}

original_code:
size_t random_value_gen(char * buf) {
    long r = random();
    int val_size = (r % ((max_val_size - min_val_size)+1)) + min_val_size;
    size_t passes = val_size / sizeof(long);
    long * ary = (long*) buf;
    size_t i;
    for(i = 0; i < passes; i++) {
        ary[i] = r;
    }
    return i*sizeof(long);
}

canonical_original_code:
unsigned long random_value_gen(char *buf) {
    long r = random();
    int val_size = (r % ((max_val_size - min_val_size)+1)) + min_val_size;
    unsigned long passes = val_size / sizeof(long);
    long *ary = (long*) buf;
    unsigned long i;
    for(i = 0; i < passes; i++) {
        ary[i] = r;
    }
    return i*sizeof(long);
}

variable_types:
buf
char *

variable_types: r long

variable_types:
passes
unsigned long

variable_types:
ary
long *

variable_types:
i
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall submit_new_value(struct timeval *tv)
{
__int64 v1; // rsi

v1 = (unsigned int)(submitted_count + 1);
submitted_count = v1;
if ( !((int)v1 % print_step) )
{
printf("Submitted %u values\n", v1);
printf(
"%d %% %d = %d\n",
(unsigned int)submitted_count,
(unsigned int)print_step,
(unsigned int)(submitted_count % print_step));
fflush(stdout);
}
tv[2].tv_sec = random_value_gen((__int64)&tv[2].tv_usec);
gettimeofday(tv, 0LL);
sum_timevals(&tv[1].tv_sec, (__int64)tv, &values_timeout);
return pax_submit_nonblock(psh, &tv[2].tv_usec, tv[2].tv_sec);
}

canonical_decompiled_code:
__int64 __fastcall func13(struct timeval *tv)
{
__int64 v1; // rsi

v1 = (unsigned int)(submitted_count + 1);
submitted_count = v1;
if ( !((int)v1 % print_step) )
{
printf("Submitted %u values\n", v1);
printf(
"%d %% %d = %d\n",
(unsigned int)submitted_count,
(unsigned int)print_step,
(unsigned int)(submitted_count % print_step));
fflush(stdout);
}
tv[2].tv_sec = func12((__int64)&tv[2].tv_usec);
gettimeofday(tv, 0LL);
func0(&tv[1].tv_sec, (__int64)tv, &values_timeout);
return pax_submit_nonblock(psh, &tv[2].tv_usec, tv[2].tv_sec);
}

original_code:
static void
submit_new_value(client_value_record * cvr) {
    submitted_count += 1;
    if((submitted_count % print_step) == 0) {
        printf("Submitted %u values\n", submitted_count);
        printf("%d %% %d = %d\n", submitted_count, print_step, (submitted_count % print_step));
 fflush(stdout);
    }
    cvr->value_size = random_value_gen(cvr->value);
    gettimeofday(&cvr->creation_time, ((void *)0));
    sum_timevals(&cvr->expire_time, &cvr->creation_time, &values_timeout);
    pax_submit_nonblock(psh, cvr->value, cvr->value_size);
}

canonical_original_code:
static void submit_new_value(struct client_value_record_t *cvr) {
    submitted_count += 1;
    if((submitted_count % print_step) == 0) {
        printf("Submitted %u values\n", submitted_count);
        printf("%d %% %d = %d\n", submitted_count, print_step, (submitted_count % print_step));
 fflush(stdout);
    }
    cvr->value_size = random_value_gen(cvr->value);
    gettimeofday(&cvr->creation_time, ((void *)0));
    sum_timevals(&cvr->expire_time, &cvr->creation_time, &values_timeout);
    pax_submit_nonblock(psh, cvr->value, cvr->value_size);
}

variable_types: struct client_value_record_t { struct timeval { long tv_sec; long tv_usec; } creation_time; struct timeval expire_time; unsigned long value_size; char value[-1]; } *

user_defined_types: struct client_value_record_t { struct timeval creation_time; struct timeval expire_time; unsigned long value_size; char value[-1]; }


decompiled_code:
__int64 cl_init()
{
unsigned int v0; // ebx
__int64 result; // rax

psh = pax_submit_handle_init();
if ( psh )
{
values_table = (struct timeval *)malloc(7504LL * (unsigned int)concurrent_values);
if ( values_table )
{
if ( (int)wait_after_init > 0 )
sleep(wait_after_init);
if ( concurrent_values )
{
v0 = 0;
do
submit_new_value(&values_table[469 * v0++]);
while ( concurrent_values > v0 );
}
event_set(&cl_periodic_event, 0xFFFFFFFFLL, 0LL, cl_periodic_timeout_check, 0LL);
set_timeout_check();
result = 0LL;
}
else
{
puts("Client init failed [malloc]");
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Client init failed [submit handle]");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func14()
{
unsigned int v0; // ebx
__int64 result; // rax

psh = pax_submit_handle_init();
if ( psh )
{
values_table = (struct timeval *)malloc(7504LL * (unsigned int)concurrent_values);
if ( values_table )
{
if ( (int)wait_after_init > 0 )
sleep(wait_after_init);
if ( concurrent_values )
{
v0 = 0;
do
func13(&values_table[469 * v0++]);
while ( concurrent_values > v0 );
}
event_set(&cl_periodic_event, 0xFFFFFFFFLL, 0LL, cl_periodic_timeout_check, 0LL);
func9();
result = 0LL;
}
else
{
puts("Client init failed [malloc]");
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Client init failed [submit handle]");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int cl_init() {
    psh = pax_submit_handle_init();
    if (psh == ((void *)0)) {
        printf("Client init failed [submit handle]\n");
        return -1;
    }
    values_table = malloc(sizeof(client_value_record) * concurrent_values);
    if(values_table == ((void *)0)) {
        printf("Client init failed [malloc]\n");
        return -1;
    }
    if(wait_after_init > 0) {
        sleep(wait_after_init);
    }
    unsigned int i;
    for(i = 0; i < concurrent_values; i++) {
        submit_new_value(&values_table[i]);
    }
    event_set((&cl_periodic_event), -1, 0, (cl_periodic_timeout_check), (((void *)0)));
    set_timeout_check();
    return 0;
}

variable_types:
i
unsigned int

return_type: int


decompiled_code:
int __fastcall cl_deliver(void *s1, size_t n, int a3)
{
__int64 v3; // rax
struct timeval *v4; // rbx
struct timeval *v5; // r12
struct timeval v7; // [rsp+0h] [rbp-38h] BYREF

if ( ++delivered_count != a3 )
_assert_fail("(int)iid == delivered_count", "benchmark_client.c", 0x163u, "cl_deliver");
gettimeofday(&v7, 0LL);
LODWORD(v3) = concurrent_values;
if ( concurrent_values )
{
v4 = values_table;
v3 = 7504LL * (unsigned int)(concurrent_values - 1);
v5 = (struct timeval *)((char *)values_table + v3 + 7504);
while ( 1 )
{
if ( v4[2].tv_sec == n )
{
LODWORD(v3) = memcmp(s1, &v4[2].tv_usec, n);
if ( !(_DWORD)v3 )
break;
}
v4 += 469;
if ( v4 == v5 )
return v3;
}
save_latency_info(v4, &v7.tv_sec);
LODWORD(v3) = submit_new_value(v4);
}
return v3;
}

canonical_decompiled_code:
int __fastcall func15(void *s1, size_t n, int a3)
{
__int64 v3; // rax
struct timeval *v4; // rbx
struct timeval *v5; // r12
struct timeval v7; // [rsp+0h] [rbp-38h] BYREF

if ( ++delivered_count != a3 )
_assert_fail("(int)iid == delivered_count", "benchmark_client.c", 0x163u, "cl_deliver");
gettimeofday(&v7, 0LL);
LODWORD(v3) = concurrent_values;
if ( concurrent_values )
{
v4 = values_table;
v3 = 7504LL * (unsigned int)(concurrent_values - 1);
v5 = (struct timeval *)((char *)values_table + v3 + 7504);
while ( 1 )
{
if ( v4[2].tv_sec == n )
{
LODWORD(v3) = memcmp(s1, &v4[2].tv_usec, n);
if ( !(_DWORD)v3 )
break;
}
v4 += 469;
if ( v4 == v5 )
return v3;
}
func5(v4, &v7.tv_sec);
LODWORD(v3) = func13(v4);
}
return v3;
}

original_code:
void cl_deliver(char* value, size_t val_size, iid_t iid, ballot_t ballot, int proposer) {
    delivered_count += 1;
    ((void) sizeof (((int)iid == delivered_count) ? 1 : 0), __extension__ ({ if ((int)iid == delivered_count) ; else __assert_fail ("(int)iid == delivered_count", "benchmark_client.c", 355, __extension__ __PRETTY_FUNCTION__); }));
    struct timeval time_now;
    gettimeofday(&time_now, ((void *)0));
    unsigned int i;
    client_value_record * iter;
    for(i = 0; i < concurrent_values; i++) {
        iter = &values_table[i];
        if(val_size == iter->value_size &&
                memcmp(value, iter->value, val_size) == 0) {
            save_latency_info(iter, &time_now);
            submit_new_value(iter);
            break;
        }
    }
    iid = iid;
    ballot = ballot;
    proposer = proposer;
}

canonical_original_code:
void cl_deliver(char *value, unsigned long val_size, unsigned int iid, unsigned int ballot, int proposer) {
    delivered_count += 1;
    ((void) sizeof (((int)iid == delivered_count) ? 1 : 0), __extension__ ({ if ((int)iid == delivered_count) ; else __assert_fail ("(int)iid == delivered_count", "benchmark_client.c", 355, __extension__ __PRETTY_FUNCTION__); }));
    struct timeval time_now;
    gettimeofday(&time_now, ((void *)0));
    unsigned int i;
    struct client_value_record_t *iter;
    for(i = 0; i < concurrent_values; i++) {
        iter = &values_table[i];
        if(val_size == iter->value_size &&
                memcmp(value, iter->value, val_size) == 0) {
            save_latency_info(iter, &time_now);
            submit_new_value(iter);
            break;
        }
    }
    iid = iid;
    ballot = ballot;
    proposer = proposer;
}

variable_types:
value
char *

variable_types:
iid
unsigned int

variable_types:
ballot
unsigned int

variable_types: proposer int

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


decompiled_code:
_BOOL8 __fastcall is_expired(_QWORD *a1, _QWORD *a2)
{
_BOOL8 result; // rax

result = 1LL;
if ( *a2 <= *a1 )
{
result = 0LL;
if ( *a2 == *a1 )
result = a2[1] >= a1[1];
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func16(_QWORD *a1, _QWORD *a2)
{
_BOOL8 result; // rax

result = 1LL;
if ( *a2 <= *a1 )
{
result = 0LL;
if ( *a2 == *a1 )
result = a2[1] >= a1[1];
}
return result;
}

original_code:
int is_expired(struct timeval * deadline, struct timeval * time_now) {
    if(time_now->tv_sec > deadline->tv_sec) {
        return 1;
    }
    if(time_now->tv_sec == deadline->tv_sec &&
            time_now->tv_usec >= deadline->tv_usec) {
        return 1;
    }
    return 0;
}

canonical_original_code:
int is_expired(struct timeval *deadline, struct timeval *time_now) {
    if(time_now->tv_sec > deadline->tv_sec) {
        return 1;
    }
    if(time_now->tv_sec == deadline->tv_sec &&
            time_now->tv_usec >= deadline->tv_usec) {
        return 1;
    }
    return 0;
}

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


decompiled_code:
__int64 cl_periodic_timeout_check()
{
unsigned int v0; // ebp
struct timeval *v1; // rbx
struct timeval v3; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v3, 0LL);
if ( concurrent_values )
{
v0 = 0;
do
{
v1 = &values_table[469 * v0];
if ( is_expired(&v1[1].tv_sec, &v3) )
submit_old_value((__int64)v1);
++v0;
}
while ( concurrent_values > v0 );
}
return set_timeout_check();
}

canonical_decompiled_code:
__int64 func17()
{
unsigned int v0; // ebp
struct timeval *v1; // rbx
struct timeval v3; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v3, 0LL);
if ( concurrent_values )
{
v0 = 0;
do
{
v1 = &values_table[469 * v0];
if ( func16(&v1[1].tv_sec, &v3) )
func7((__int64)v1);
++v0;
}
while ( concurrent_values > v0 );
}
return func9();
}

original_code:
static void
cl_periodic_timeout_check(int fd, short event, void *arg) {
    struct timeval time_now;
    gettimeofday(&time_now, ((void *)0));
    unsigned int i;
    client_value_record * iter;
    for(i = 0; i < concurrent_values; i++) {
        iter = &values_table[i];
        if (is_expired(&iter->expire_time, &time_now)) {
            submit_old_value(iter);
        }
    }
    set_timeout_check();
    fd = fd;
    event = event;
    arg = arg;
}

canonical_original_code:
static void cl_periodic_timeout_check(int fd, short event, void *arg) {
    struct timeval time_now;
    gettimeofday(&time_now, ((void *)0));
    unsigned int i;
    struct client_value_record_t *iter;
    for(i = 0; i < concurrent_values; i++) {
        iter = &values_table[i];
        if (is_expired(&iter->expire_time, &time_now)) {
            submit_old_value(iter);
        }
    }
    set_timeout_check();
    fd = fd;
    event = event;
    arg = arg;
}

variable_types: fd int

variable_types: event short

variable_types:
arg
void *

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


decompiled_code:
void __fastcall handle_cltr_c(unsigned int a1)
{
printf("Caught signal %d\n", a1);
force_exit = 1;
}

canonical_decompiled_code:
void __fastcall func6(unsigned int a1)
{
printf("Caught signal %d\n", a1);
force_exit = 1;
}

original_code:
void handle_cltr_c (int sig) {
 printf("Caught signal %d\n", sig);
    force_exit = 1;
}

canonical_original_code:
void handle_cltr_c(int sig) {
 printf("Caught signal %d\n", sig);
    force_exit = 1;
}

variable_types: sig int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
double v4; // xmm3_8
__int64 v6; // rbx

signal(2, (__sighandler_t)handle_cltr_c);
values_timeout = 5LL;
*(&values_timeout + 1) = 0LL;
parse_args(argc, (char **)argv);
print_benchmark_parameters();
end_time = duration + time(0LL);
cl_periodic_interval = 3LL;
qword_B98 = 0LL;
min_latency = 999999LL;
qword_B78 = 999999LL;
max_latency = 0LL;
qword_B68 = 0LL;
aggregated_latency = 0LL;
qword_B58 = 0LL;
if ( (unsigned int)learner_init(cl_deliver, cl_init) )
{
puts("Failed to start the learner!");
v3 = -1;
}
else
{
v3 = 0;
while ( !force_exit )
{
if ( time(0LL) >= end_time )
break;
sleep(1u);
}
printf("Total delivered:%u\n", (unsigned int)delivered_count);
printf("\tRate:%f\n", (float)((float)delivered_count / (float)duration));
printf("Total submitted:%u\n", (unsigned int)submitted_count);
printf("\tRate:%f\n", (float)((float)submitted_count / (float)duration));
printf("Timed-out values:%u\n", (unsigned int)retried_count);
if ( aggregated_latency_count < 0 )
v4 = (double)(int)(aggregated_latency_count & 1 | ((unsigned __int64)aggregated_latency_count >> 1))
+ (double)(int)(aggregated_latency_count & 1 | ((unsigned __int64)aggregated_latency_count >> 1));
else
v4 = (double)(int)aggregated_latency_count;
printf(
"Latency - Avg:%.2f, Min:%.2f, Max:%.2f\n",
((double)(int)aggregated_latency * 1000.0 + (double)(int)qword_B58 / 1000.0) / v4,
(double)(int)min_latency * 1000.0 + (double)(int)qword_B78 / 1000.0,
(double)(int)max_latency * 1000.0 + (double)(int)qword_B68 / 1000.0);
if ( sample_frequency )
{
puts("Latency samples: ");
if ( samples_count > 0 )
{
v6 = 0LL;
do
{
printf("%.2f, ", *(double *)(samples + 8 * v6));
if ( 20 * ((int)v6 / 20) == (_DWORD)v6 )
putchar(10);
++v6;
}
while ( samples_count > (int)v6 );
}
putchar(10);
}
fflush(stdout);
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
double v4; // xmm3_8
__int64 v6; // rbx

signal(2, (__sighandler_t)handle_cltr_c);
values_timeout = 5LL;
*(&values_timeout + 1) = 0LL;
func11(argc, (char **)argv);
func8();
end_time = duration + time(0LL);
cl_periodic_interval = 3LL;
qword_B98 = 0LL;
min_latency = 999999LL;
qword_B78 = 999999LL;
max_latency = 0LL;
qword_B68 = 0LL;
aggregated_latency = 0LL;
qword_B58 = 0LL;
if ( (unsigned int)learner_init(cl_deliver, cl_init) )
{
puts("Failed to start the learner!");
v3 = -1;
}
else
{
v3 = 0;
while ( !force_exit )
{
if ( time(0LL) >= end_time )
break;
sleep(1u);
}
printf("Total delivered:%u\n", (unsigned int)delivered_count);
printf("\tRate:%f\n", (float)((float)delivered_count / (float)duration));
printf("Total submitted:%u\n", (unsigned int)submitted_count);
printf("\tRate:%f\n", (float)((float)submitted_count / (float)duration));
printf("Timed-out values:%u\n", (unsigned int)retried_count);
if ( aggregated_latency_count < 0 )
v4 = (double)(int)(aggregated_latency_count & 1 | ((unsigned __int64)aggregated_latency_count >> 1))
+ (double)(int)(aggregated_latency_count & 1 | ((unsigned __int64)aggregated_latency_count >> 1));
else
v4 = (double)(int)aggregated_latency_count;
printf(
"Latency - Avg:%.2f, Min:%.2f, Max:%.2f\n",
((double)(int)aggregated_latency * 1000.0 + (double)(int)qword_B58 / 1000.0) / v4,
(double)(int)min_latency * 1000.0 + (double)(int)qword_B78 / 1000.0,
(double)(int)max_latency * 1000.0 + (double)(int)qword_B68 / 1000.0);
if ( sample_frequency )
{
puts("Latency samples: ");
if ( samples_count > 0 )
{
v6 = 0LL;
do
{
printf("%.2f, ", *(double *)(samples + 8 * v6));
if ( 20 * ((int)v6 / 20) == (_DWORD)v6 )
putchar(10);
++v6;
}
while ( samples_count > (int)v6 );
}
putchar(10);
}
fflush(stdout);
}
return v3;
}

original_code:
int main (int argc, char const *argv[]) {
    signal(2, handle_cltr_c);
    values_timeout.tv_sec = 5;
    values_timeout.tv_usec = 0;
    parse_args(argc, (char **)argv);
    print_benchmark_parameters();
    start_time = time(((void *)0));
    end_time = start_time + duration;
    cl_periodic_interval.tv_sec = 3;
    cl_periodic_interval.tv_usec = 0;
    min_latency.tv_sec = 999999;
    min_latency.tv_usec = 999999;
    max_latency.tv_sec = 0;
    max_latency.tv_usec = 0;
    aggregated_latency.tv_sec = 0;
    aggregated_latency.tv_usec = 0;
    if(learner_init(cl_deliver, cl_init) != 0) {
        printf("Failed to start the learner!\n");
        return -1;
    }
    while(!force_exit && time(((void *)0)) < end_time) {
        sleep(1);
    }
    printf("Total delivered:%u\n", delivered_count);
    printf("\tRate:%f\n", ((float)delivered_count/duration));
    printf("Total submitted:%u\n", submitted_count);
    printf("\tRate:%f\n", ((float)submitted_count/duration));
    printf("Timed-out values:%u\n", retried_count);
    double min_lat_ms = ((double)min_latency.tv_sec * 1000) +
        ((double)min_latency.tv_usec / 1000);
    double max_lat_ms = ((double)max_latency.tv_sec * 1000) +
        ((double)max_latency.tv_usec / 1000);
    double avg_lat_ms = (((double)aggregated_latency.tv_sec * 1000) +
        ((double)aggregated_latency.tv_usec / 1000))/aggregated_latency_count;
    printf("Latency - Avg:%.2f, Min:%.2f, Max:%.2f\n",
        avg_lat_ms, min_lat_ms, max_lat_ms);
    if(sample_frequency != 0) {
        printf("Latency samples: \n");
        int i;
        for(i = 0; i < samples_count; i++) {
            printf("%.2f, ", samples[i]);
            if(i % 20 == 0) {
                printf("\n");
            }
        }
        printf("\n");
    }
    fflush(stdout);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    signal(2, handle_cltr_c);
    values_timeout.tv_sec = 5;
    values_timeout.tv_usec = 0;
    parse_args(argc, (char **)argv);
    print_benchmark_parameters();
    start_time = time(((void *)0));
    end_time = start_time + duration;
    cl_periodic_interval.tv_sec = 3;
    cl_periodic_interval.tv_usec = 0;
    min_latency.tv_sec = 999999;
    min_latency.tv_usec = 999999;
    max_latency.tv_sec = 0;
    max_latency.tv_usec = 0;
    aggregated_latency.tv_sec = 0;
    aggregated_latency.tv_usec = 0;
    if(learner_init(cl_deliver, cl_init) != 0) {
        printf("Failed to start the learner!\n");
        return -1;
    }
    while(!force_exit && time(((void *)0)) < end_time) {
        sleep(1);
    }
    printf("Total delivered:%u\n", delivered_count);
    printf("\tRate:%f\n", ((float)delivered_count/duration));
    printf("Total submitted:%u\n", submitted_count);
    printf("\tRate:%f\n", ((float)submitted_count/duration));
    printf("Timed-out values:%u\n", retried_count);
    double min_lat_ms = ((double)min_latency.tv_sec * 1000) +
        ((double)min_latency.tv_usec / 1000);
    double max_lat_ms = ((double)max_latency.tv_sec * 1000) +
        ((double)max_latency.tv_usec / 1000);
    double avg_lat_ms = (((double)aggregated_latency.tv_sec * 1000) +
        ((double)aggregated_latency.tv_usec / 1000))/aggregated_latency_count;
    printf("Latency - Avg:%.2f, Min:%.2f, Max:%.2f\n",
        avg_lat_ms, min_lat_ms, max_lat_ms);
    if(sample_frequency != 0) {
        printf("Latency samples: \n");
        int i;
        for(i = 0; i < samples_count; i++) {
            printf("%.2f, ", samples[i]);
            if(i % 20 == 0) {
                printf("\n");
            }
        }
        printf("\n");
    }
    fflush(stdout);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int


decompiled_code:
__int64 __fastcall lea_check_quorum(__int64 a1)
{
unsigned __int64 v1; // rcx
__int64 v2; // rsi
__int64 v3; // rax
__int64 result; // rax
__int64 v5; // rdx

v1 = 0LL;
v2 = -1LL;
v3 = 0LL;
while ( 1 )
{
v5 = *(_QWORD *)(a1 + 8 * v3 + 8);
if ( !v5 || *(_DWORD *)(v5 + 4) != *(_DWORD *)(a1 + 4) )
goto LABEL_5;
if ( *(_WORD *)(v5 + 12) )
break;
++v1;
v2 = v3;
LABEL_5:
if ( ++v3 == 3 )
goto LABEL_3;
}
v1 += 4LL;
v2 = v3;
LABEL_3:
result = 0LL;
if ( v1 > 1 )
{
*(_QWORD *)(a1 + 32) = *(_QWORD *)(a1 + 8 * v2 + 8);
result = 1LL;
if ( *(_DWORD *)a1 > (unsigned int)highest_iid_closed )
highest_iid_closed = *(_DWORD *)a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned __int64 v1; // rcx
__int64 v2; // rsi
__int64 v3; // rax
__int64 result; // rax
__int64 v5; // rdx

v1 = 0LL;
v2 = -1LL;
v3 = 0LL;
while ( 1 )
{
v5 = *(_QWORD *)(a1 + 8 * v3 + 8);
if ( !v5 || *(_DWORD *)(v5 + 4) != *(_DWORD *)(a1 + 4) )
goto LABEL_5;
if ( *(_WORD *)(v5 + 12) )
break;
++v1;
v2 = v3;
LABEL_5:
if ( ++v3 == 3 )
goto LABEL_3;
}
v1 += 4LL;
v2 = v3;
LABEL_3:
result = 0LL;
if ( v1 > 1 )
{
*(_QWORD *)(a1 + 32) = *(_QWORD *)(a1 + 8 * v2 + 8);
result = 1LL;
if ( *(_DWORD *)a1 > (unsigned int)highest_iid_closed )
highest_iid_closed = *(_DWORD *)a1;
}
return result;
}

original_code:
static int lea_check_quorum(l_inst_info * ii) {
    size_t i, a_valid_index = -1, count = 0;
    accept_ack * curr_ack;
    for(i = 0; i < 3; i++) {
        curr_ack = ii->acks[i];
        if(curr_ack == ((void *)0)) {
            continue;
        }
        if(curr_ack->ballot == ii->last_update_ballot){
            a_valid_index = i;
            count++;
            if(curr_ack->is_final) {
                count += 3;
                break;
            }
        }
    }
    if(count >= (((int)(3/2))+1)) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Reached quorum, iid:%lu is closed!\n", ii->iid) ;};
        ii->final_value = ii->acks[a_valid_index];
        if(ii->iid > highest_iid_closed) {
            highest_iid_closed = ii->iid;
        }
        return 1;
    }
    return 0;
}

canonical_original_code:
static int lea_check_quorum(struct learner_instance_info *ii) {
    unsigned long i; unsigned long a_valid_index = -1; unsigned long count = 0;
    struct accept_ack_t *curr_ack;
    for(i = 0; i < 3; i++) {
        curr_ack = ii->acks[i];
        if(curr_ack == ((void *)0)) {
            continue;
        }
        if(curr_ack->ballot == ii->last_update_ballot){
            a_valid_index = i;
            count++;
            if(curr_ack->is_final) {
                count += 3;
                break;
            }
        }
    }
    if(count >= (((int)(3/2))+1)) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Reached quorum, iid:%lu is closed!\n", ii->iid) ;};
        ii->final_value = ii->acks[a_valid_index];
        if(ii->iid > highest_iid_closed) {
            highest_iid_closed = ii->iid;
        }
        return 1;
    }
    return 0;
}

variable_types: struct learner_instance_info { unsigned int iid; unsigned int last_update_ballot; struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; } *acks[3]; struct accept_ack_t *final_value; } *

variable_types:
count
unsigned long

user_defined_types: struct learner_instance_info { unsigned int iid; unsigned int last_update_ballot; struct accept_ack_t *acks[3]; struct accept_ack_t *final_value; }


original_code:
static void lea_clear_instance_info(l_inst_info * ii) {
    ii->iid = (0);
    ii->last_update_ballot = 0;
    ii->final_value = ((void *)0);
    size_t i;
    for(i = 0; i < 3; i++) {
        if(ii->acks[i] != ((void *)0)) {
            free(ii->acks[i]);
            ii->acks[i] = ((void *)0);
        }
    }
}

canonical_original_code:
static void lea_clear_instance_info(struct learner_instance_info *ii) {
    ii->iid = (0);
    ii->last_update_ballot = 0;
    ii->final_value = ((void *)0);
    unsigned long i;
    for(i = 0; i < 3; i++) {
        if(ii->acks[i] != ((void *)0)) {
            free(ii->acks[i]);
            ii->acks[i] = ((void *)0);
        }
    }
}

variable_types:
i
unsigned long

user_defined_types: struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; }


decompiled_code:
__int64 init_lea_structs()
{
int *v0; // rbx

memset(&learner_state, 0, (size_t)&unk_50000);
v0 = &learner_state;
do
{
lea_clear_instance_info((__int64)v0);
v0 += 10;
}
while ( v0 != &highest_iid_closed );
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
int *v0; // rbx

memset(&learner_state, 0, (size_t)&unk_50000);
v0 = &learner_state;
do
{
func1((__int64)v0);
v0 += 10;
}
while ( v0 != &highest_iid_closed );
return 0LL;
}

original_code:
static int init_lea_structs() {
    if ((8192 & (8192 -1)) != 0) {
        printf("Error: LEARNER_ARRAY_SIZE is not a power of 2\n");
        return (-1);
    }
    memset(learner_state, 0, (sizeof(l_inst_info) * 8192));
    size_t i;
    for(i = 0; i < 8192; i++) {
        lea_clear_instance_info(&learner_state[i]);
    }
    return 0;
}

canonical_original_code:
static int init_lea_structs() {
    if ((8192 & (8192 -1)) != 0) {
        printf("Error: LEARNER_ARRAY_SIZE is not a power of 2\n");
        return (-1);
    }
    memset(learner_state, 0, (sizeof(l_inst_info) * 8192));
    unsigned long i;
    for(i = 0; i < 8192; i++) {
        lea_clear_instance_info(&learner_state[i]);
    }
    return 0;
}


decompiled_code:
__int64 init_lea_network()
{
__int64 result; // rax

to_acceptors = udp_sendbuf_new(3LL, "10.78.36.211", 6002LL, "10.78.36.212", 6002LL, "10.78.36.213", 6002LL);
if ( to_acceptors )
{
for_learner = udp_receiver_new(6001LL);
if ( for_learner || (for_learner = udp_receiver_new(6007LL)) != 0 )
{
event_set(&learner_msg_event, *(unsigned int *)for_learner, 18LL, lea_handle_newmsg, 0LL);
event_add(&learner_msg_event, 0LL);
result = 0LL;
}
else
{
puts("Error creating learner network receiver");
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Error creating learner network sender");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax

to_acceptors = udp_sendbuf_new(3LL, "10.78.36.211", 6002LL, "10.78.36.212", 6002LL, "10.78.36.213", 6002LL);
if ( to_acceptors )
{
for_learner = udp_receiver_new(6001LL);
if ( for_learner || (for_learner = udp_receiver_new(6007LL)) != 0 )
{
event_set(&learner_msg_event, *(unsigned int *)for_learner, 18LL, lea_handle_newmsg, 0LL);
event_add(&learner_msg_event, 0LL);
result = 0LL;
}
else
{
puts("Error creating learner network receiver");
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Error creating learner network sender");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int init_lea_network() {
    to_acceptors = udp_sendbuf_new(3, "10.78.36.211", 6002, "10.78.36.212", 6002, "10.78.36.213", 6002);
    if(to_acceptors == ((void *)0)) {
        printf("Error creating learner network sender\n");
        return (-1);
    }
    for_learner = udp_receiver_new(6001);
    if (for_learner == ((void *)0)) {
        for_learner = udp_receiver_new(6007);
        if (for_learner == ((void *)0)) {
            printf("Error creating learner network receiver\n");
            return (-1);
        }
    }
    event_set(&learner_msg_event, for_learner->sock, 0x02|0x10, lea_handle_newmsg, ((void *)0));
    event_add(&learner_msg_event, ((void *)0));
    return 0;
}


decompiled_code:
__int64 init_lea_timers()
{
__int64 result; // rax

event_set(&hole_check_event, 0xFFFFFFFFLL, 0LL, lea_hole_check, 0LL);
hole_check_interval = 0LL;
qword_A18 = 500000LL;
result = event_add(&hole_check_event, &hole_check_interval);
if ( (_DWORD)result )
{
puts("Error while adding first periodic hole_check event");
result = 0xFFFFFFFFLL;
}
return result;
}

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

event_set(&hole_check_event, 0xFFFFFFFFLL, 0LL, lea_hole_check, 0LL);
hole_check_interval = 0LL;
qword_A18 = 500000LL;
result = event_add(&hole_check_event, &hole_check_interval);
if ( (_DWORD)result )
{
puts("Error while adding first periodic hole_check event");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int
init_lea_timers() {
    event_set((&hole_check_event), -1, 0, (lea_hole_check), (((void *)0)));
 ((&hole_check_interval)->tv_sec = (&hole_check_interval)->tv_usec = 0);
 hole_check_interval.tv_sec = 500000 / 1000000;
    hole_check_interval.tv_usec = 500000 % 1000000;
 if(event_add(&hole_check_event, &hole_check_interval) != 0) {
    printf("Error while adding first periodic hole_check event\n");
       return -1;
 }
    return 0;
}


decompiled_code:
__int64 init_lea_signal_ready()
{
__int64 result; // rax

event_set(&init_complete_event, 0xFFFFFFFFLL, 0LL, init_lea_success, 0LL);
result = event_add(&init_complete_event, &asap_interval);
if ( (_DWORD)result )
{
puts("Error while adding lea successful init event");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 result; // rax

event_set(&init_complete_event, 0xFFFFFFFFLL, 0LL, init_lea_success, 0LL);
result = event_add(&init_complete_event, &asap_interval);
if ( (_DWORD)result )
{
puts("Error while adding lea successful init event");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int
init_lea_signal_ready() {
    event_set((&init_complete_event), -1, 0, (init_lea_success), (((void *)0)));
 if(event_add(&init_complete_event, &asap_interval) != 0) {
    printf("Error while adding lea successful init event\n");
       return -1;
   }
   return 0;
}


decompiled_code:
__int64 lea_deliver_next_closed()
{
__int64 result; // rax
__int64 v1; // rdi
int *v2; // rbp
__int16 v3; // bx

result = current_iid & 0x1FFF;
v1 = qword_B60[5 * result];
if ( v1 )
{
v2 = &learner_state[10 * result];
do
{
if ( *v2 != current_iid )
_assert_fail("ii->iid == current_iid", "learner.c", 0xD9u, "lea_deliver_next_closed");
delfun(
v1 + 24,
*(_QWORD *)(v1 + 16),
(unsigned int)current_iid,
*(unsigned int *)(v1 + 4),
*(_DWORD *)(v1 + 4) % 0xAu);
v3 = ++current_iid;
lea_clear_instance_info((__int64)v2);
result = v3 & 0x1FFF;
v2 = &learner_state[10 * result];
v1 = qword_B60[5 * result];
}
while ( v1 );
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 result; // rax
__int64 v1; // rdi
int *v2; // rbp
__int16 v3; // bx

result = current_iid & 0x1FFF;
v1 = qword_B60[5 * result];
if ( v1 )
{
v2 = &learner_state[10 * result];
do
{
if ( *v2 != current_iid )
_assert_fail("ii->iid == current_iid", "learner.c", 0xD9u, "lea_deliver_next_closed");
delfun(
v1 + 24,
*(_QWORD *)(v1 + 16),
(unsigned int)current_iid,
*(unsigned int *)(v1 + 4),
*(_DWORD *)(v1 + 4) % 0xAu);
v3 = ++current_iid;
func1((__int64)v2);
result = v3 & 0x1FFF;
v2 = &learner_state[10 * result];
v1 = qword_B60[5 * result];
}
while ( v1 );
}
return result;
}

original_code:
static void lea_deliver_next_closed() {
    l_inst_info * ii = &learner_state[((current_iid) & (8192 -1))];
    accept_ack * aa;
    while((ii->final_value != ((void *)0))) {
        ((void) sizeof ((ii->iid == current_iid) ? 1 : 0), __extension__ ({ if (ii->iid == current_iid) ; else __assert_fail ("ii->iid == current_iid", "learner.c", 217, __extension__ __PRETTY_FUNCTION__); }));
        aa = ii->final_value;
        short int proposer_id = aa->ballot % 10;
        delfun(aa->value, aa->value_size, current_iid, aa->ballot, proposer_id);
        current_iid++;
        lea_clear_instance_info(ii);
        ii = &learner_state[((current_iid) & (8192 -1))];
    }
}

canonical_original_code:
static void lea_deliver_next_closed() {
    struct learner_instance_info *ii = &learner_state[((current_iid) & (8192 -1))];
    struct accept_ack_t *aa;
    while((ii->final_value != ((void *)0))) {
        ((void) sizeof ((ii->iid == current_iid) ? 1 : 0), __extension__ ({ if (ii->iid == current_iid) ; else __assert_fail ("ii->iid == current_iid", "learner.c", 217, __extension__ __PRETTY_FUNCTION__); }));
        aa = ii->final_value;
        short proposer_id = aa->ballot % 10;
        delfun(aa->value, aa->value_size, current_iid, aa->ballot, proposer_id);
        current_iid++;
        lea_clear_instance_info(ii);
        ii = &learner_state[((current_iid) & (8192 -1))];
    }
}

variable_types: struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; } *


decompiled_code:
__int64 __fastcall lea_store_accept_ack(__int64 a1, __int16 a2, __int64 a3)
{
void *v4; // r12
__int64 result; // rax

v4 = (void *)paxos_normal_malloc(*(_QWORD *)(a3 + 16) + 24LL);
memcpy(v4, (const void *)a3, *(_QWORD *)(a3 + 16) + 24LL);
*(_QWORD *)(a1 + 8LL * a2 + 8) = v4;
result = *(unsigned int *)(a3 + 4);
*(_DWORD *)(a1 + 4) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int16 a2, __int64 a3)
{
void *v4; // r12
__int64 result; // rax

v4 = (void *)paxos_normal_malloc(*(_QWORD *)(a3 + 16) + 24LL);
memcpy(v4, (const void *)a3, *(_QWORD *)(a3 + 16) + 24LL);
*(_QWORD *)(a1 + 8LL * a2 + 8) = v4;
result = *(unsigned int *)(a3 + 4);
*(_DWORD *)(a1 + 4) = result;
return result;
}

original_code:
static void lea_store_accept_ack(l_inst_info * ii, short int acceptor_id, accept_ack * aa) {
    accept_ack * new_ack = paxos_normal_malloc((aa->value_size + sizeof(accept_ack)));;
    memcpy(new_ack, aa, (aa->value_size + sizeof(accept_ack)));
    ii->acks[acceptor_id] = new_ack;
    ii->last_update_ballot = aa->ballot;
}

canonical_original_code:
static void lea_store_accept_ack(struct learner_instance_info *ii, short acceptor_id, struct accept_ack_t *aa) {
    struct accept_ack_t *new_ack = paxos_normal_malloc((aa->value_size + sizeof(accept_ack)));;
    memcpy(new_ack, aa, (aa->value_size + sizeof(accept_ack)));
    ii->acks[acceptor_id] = new_ack;
    ii->last_update_ballot = aa->ballot;
}

variable_types:
aa
struct accept_ack_t *


decompiled_code:
__int64 __fastcall lea_update_state(__int64 a1, __int16 a2, _DWORD *a3)
{
__int64 result; // rax
_DWORD *v6; // rdi

if ( !*(_DWORD *)a1 )
{
*(_DWORD *)a1 = *a3;
*(_DWORD *)(a1 + 4) = a3[1];
}
if ( *(_DWORD *)a1 != *a3 )
_assert_fail("ii->iid == aa->iid", "learner.c", 0x7Bu, "lea_update_state");
result = 0LL;
if ( !*(_QWORD *)(a1 + 32) )
{
v6 = *(_DWORD **)(a1 + 8LL * a2 + 8);
if ( v6 )
{
result = 0LL;
if ( v6[1] < a3[1] )
{
free(v6);
lea_store_accept_ack(a1, a2, (__int64)a3);
*(_DWORD *)(a1 + 4) = a3[1];
result = 1LL;
}
}
else
{
lea_store_accept_ack(a1, a2, (__int64)a3);
*(_DWORD *)(a1 + 4) = a3[1];
result = 1LL;
}
}
return result;
}

original_code:
static int lea_update_state(l_inst_info * ii, short int acceptor_id, accept_ack * aa) {
    if(ii->iid == (0)) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Received first message for instance:%lu\n", aa->iid) ;};
        ii->iid = aa->iid;
        ii->last_update_ballot = aa->ballot;
    }
    ((void) sizeof ((ii->iid == aa->iid) ? 1 : 0), __extension__ ({ if (ii->iid == aa->iid) ; else __assert_fail ("ii->iid == aa->iid", "learner.c", 123, __extension__ __PRETTY_FUNCTION__); }));
    if((ii->final_value != ((void *)0))) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Dropping accept_ack for iid:%lu, already closed\n", aa->iid) ;};
        return 0;
    }
    if(ii->acks[acceptor_id] == ((void *)0)) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Got first ack for iid:%lu, acceptor:%d\n", ii->iid, acceptor_id) ;};
        lea_store_accept_ack(ii, acceptor_id, aa);
        ii->last_update_ballot = aa->ballot;
        return 1;
    }
    accept_ack * prev_ack = ii->acks[acceptor_id];
    if(prev_ack->ballot >= aa->ballot) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Dropping accept_ack for iid:%lu, stored ballot is newer or equal\n", aa->iid) ;};
        return 0;
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Overwriting previous accept_ack for iid:%lu\n", aa->iid) ;};
    free(prev_ack);
    lea_store_accept_ack(ii, acceptor_id, aa);
    ii->last_update_ballot = aa->ballot;
    return 1;
}

canonical_original_code:
static int lea_update_state(struct learner_instance_info *ii, short acceptor_id, struct accept_ack_t *aa) {
    if(ii->iid == (0)) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Received first message for instance:%lu\n", aa->iid) ;};
        ii->iid = aa->iid;
        ii->last_update_ballot = aa->ballot;
    }
    ((void) sizeof ((ii->iid == aa->iid) ? 1 : 0), __extension__ ({ if (ii->iid == aa->iid) ; else __assert_fail ("ii->iid == aa->iid", "learner.c", 123, __extension__ __PRETTY_FUNCTION__); }));
    if((ii->final_value != ((void *)0))) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Dropping accept_ack for iid:%lu, already closed\n", aa->iid) ;};
        return 0;
    }
    if(ii->acks[acceptor_id] == ((void *)0)) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Got first ack for iid:%lu, acceptor:%d\n", ii->iid, acceptor_id) ;};
        lea_store_accept_ack(ii, acceptor_id, aa);
        ii->last_update_ballot = aa->ballot;
        return 1;
    }
    struct accept_ack_t *prev_ack = ii->acks[acceptor_id];
    if(prev_ack->ballot >= aa->ballot) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Dropping accept_ack for iid:%lu, stored ballot is newer or equal\n", aa->iid) ;};
        return 0;
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Overwriting previous accept_ack for iid:%lu\n", aa->iid) ;};
    free(prev_ack);
    lea_store_accept_ack(ii, acceptor_id, aa);
    ii->last_update_ballot = aa->ballot;
    return 1;
}


decompiled_code:
__int64 __fastcall handle_accept_ack(__int16 a1, int *a2)
{
__int64 result; // rax
int *v3; // rbp

result = (unsigned int)*a2;
if ( (unsigned int)result > highest_iid_seen )
highest_iid_seen = *a2;
if ( (unsigned int)result >= current_iid && (unsigned int)result < current_iid + 0x2000 )
{
v3 = &learner_state[10 * (result & 0x1FFF)];
result = lea_update_state((__int64)v3, a1, a2);
if ( (_DWORD)result )
{
result = lea_check_quorum((__int64)v3);
if ( (_DWORD)result )
{
if ( *a2 == current_iid )
result = lea_deliver_next_closed();
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int16 a1, int *a2)
{
__int64 result; // rax
int *v3; // rbp

result = (unsigned int)*a2;
if ( (unsigned int)result > highest_iid_seen )
highest_iid_seen = *a2;
if ( (unsigned int)result >= current_iid && (unsigned int)result < current_iid + 0x2000 )
{
v3 = &learner_state[10 * (result & 0x1FFF)];
result = func8((__int64)v3, a1, a2);
if ( (_DWORD)result )
{
result = func0((__int64)v3);
if ( (_DWORD)result )
{
if ( *a2 == current_iid )
result = func6();
}
}
}
return result;
}

original_code:
static void handle_accept_ack(short int acceptor_id, accept_ack * aa) {
    if(aa->iid > highest_iid_seen) {
        highest_iid_seen = aa->iid;
    }
    if(aa->iid < current_iid) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Dropping accept_ack for already delivered iid:%lu\n", aa->iid) ;};
        return;
    }
    if(aa->iid >= current_iid + 8192) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Dropping accept_ack for iid:%lu, too far in future\n", aa->iid) ;};
        return;
    }
    l_inst_info * ii = &learner_state[((aa->iid) & (8192 -1))];
    int relevant = lea_update_state(ii, acceptor_id, aa);
    if(!relevant) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Learner discarding learn for iid:%lu\n", aa->iid) ;};
        return;
    }
    int closed = lea_check_quorum(ii);
    if(!closed) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Not yet a quorum for iid:%lu\n", aa->iid) ;};
        return;
    }
    if (aa->iid == current_iid) {
        lea_deliver_next_closed(aa->iid);
    }
}

canonical_original_code:
static void handle_accept_ack(short acceptor_id, struct accept_ack_t *aa) {
    if(aa->iid > highest_iid_seen) {
        highest_iid_seen = aa->iid;
    }
    if(aa->iid < current_iid) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Dropping accept_ack for already delivered iid:%lu\n", aa->iid) ;};
        return;
    }
    if(aa->iid >= current_iid + 8192) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Dropping accept_ack for iid:%lu, too far in future\n", aa->iid) ;};
        return;
    }
    struct learner_instance_info *ii = &learner_state[((aa->iid) & (8192 -1))];
    int relevant = lea_update_state(ii, acceptor_id, aa);
    if(!relevant) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Learner discarding learn for iid:%lu\n", aa->iid) ;};
        return;
    }
    int closed = lea_check_quorum(ii);
    if(!closed) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Not yet a quorum for iid:%lu\n", aa->iid) ;};
        return;
    }
    if (aa->iid == current_iid) {
        lea_deliver_next_closed(aa->iid);
    }
}

variable_types: relevant int

variable_types: closed int


decompiled_code:
__int64 __fastcall handle_accept_ack_batch(__int16 *a1)
{
int v1; // er12
__int64 v2; // rbp
__int64 result; // rax

if ( a1[1] > 0 )
{
v1 = 0;
v2 = 0LL;
do
{
handle_accept_ack(*a1, (int *)((char *)a1 + v2 + 4));
result = *(_QWORD *)((char *)a1 + v2 + 20);
v2 += result + 24;
++v1;
}
while ( a1[1] > (__int16)v1 );
}
return result;
}

original_code:
static void handle_accept_ack_batch(accept_ack_batch* aab) {
    size_t data_offset;
    accept_ack * aa;
    data_offset = 0;
    short int i;
    for(i = 0; i < aab->count; i++) {
        aa = (accept_ack*) &aab->data[data_offset];
        handle_accept_ack(aab->acceptor_id, aa);
        data_offset += (aa->value_size + sizeof(accept_ack));
    }
}

canonical_original_code:
static void handle_accept_ack_batch(struct accept_ack_batch_t *aab) {
    unsigned long data_offset;
    struct accept_ack_t *aa;
    data_offset = 0;
    short i;
    for(i = 0; i < aab->count; i++) {
        aa = (struct accept_ack_t *) &aab->data[data_offset];
        handle_accept_ack(aab->acceptor_id, aa);
        data_offset += (aa->value_size + sizeof(accept_ack));
    }
}

variable_types: struct accept_ack_batch_t { short acceptor_id; short count; char data[]; } *

variable_types: i short


decompiled_code:
int __fastcall lea_handle_newmsg(int a1)
{
__int64 v1; // rsi
int result; // eax

if ( *(_DWORD *)for_learner != a1 )
_assert_fail("sock == for_learner->sock", "learner.c", 0x167u, "lea_handle_newmsg");
if ( (int)udp_read_next_message() < 0 )
return puts("Dropping invalid learner message");
v1 = *(unsigned int *)(for_learner + 28);
if ( (_DWORD)v1 == 8 )
result = handle_accept_ack_batch((__int16 *)(for_learner + 32));
else
result = printf("Unknow msg type %d received by learner\n", v1);
return result;
}

canonical_decompiled_code:
int __fastcall func11(int a1)
{
__int64 v1; // rsi
int result; // eax

if ( *(_DWORD *)for_learner != a1 )
_assert_fail("sock == for_learner->sock", "learner.c", 0x167u, "lea_handle_newmsg");
if ( (int)udp_read_next_message() < 0 )
return puts("Dropping invalid learner message");
v1 = *(unsigned int *)(for_learner + 28);
if ( (_DWORD)v1 == 8 )
result = func10((__int16 *)(for_learner + 32));
else
result = printf("Unknow msg type %d received by learner\n", v1);
return result;
}

original_code:
static void lea_handle_newmsg(int sock, short event, void *arg) {
    (sock = sock);;
    (event = event);;
    (arg = arg);;
    ((void) sizeof ((sock == for_learner->sock) ? 1 : 0), __extension__ ({ if (sock == for_learner->sock) ; else __assert_fail ("sock == for_learner->sock", "learner.c", 359, __extension__ __PRETTY_FUNCTION__); }));
    int valid = udp_read_next_message(for_learner);
    if (valid < 0) {
        printf("Dropping invalid learner message\n");
        return;
    }
    paxos_msg * msg = (paxos_msg*) &for_learner->recv_buffer;
    switch(msg->type) {
        case accept_acks: {
            handle_accept_ack_batch((accept_ack_batch*) msg->data);
        }
        break;
        default: {
            printf("Unknow msg type %d received by learner\n", msg->type);
        }
    }
}

canonical_original_code:
static void lea_handle_newmsg(int sock, short event, void *arg) {
    (sock = sock);;
    (event = event);;
    (arg = arg);;
    ((void) sizeof ((sock == for_learner->sock) ? 1 : 0), __extension__ ({ if (sock == for_learner->sock) ; else __assert_fail ("sock == for_learner->sock", "learner.c", 359, __extension__ __PRETTY_FUNCTION__); }));
    int valid = udp_read_next_message(for_learner);
    if (valid < 0) {
        printf("Dropping invalid learner message\n");
        return;
    }
    struct paxos_msg_t *msg = (paxos_msg*) &for_learner->recv_buffer;
    switch(msg->type) {
        case accept_acks: {
            handle_accept_ack_batch((struct accept_ack_batch_t *) msg->data);
        }
        break;
        default: {
            printf("Unknow msg type %d received by learner\n", msg->type);
        }
    }
}

variable_types: sock int

variable_types: valid int

variable_types: struct paxos_msg_t { unsigned long data_size; enum pax_msg_code_e { prepare_reqs=1, prepare_acks=2, accept_reqs=4, accept_acks=8, repeat_reqs=16, submit=32, leader_announce=64, alive_ping=65 } type; char data[]; } *

user_defined_types: struct paxos_msg_t { unsigned long data_size; enum pax_msg_code_e type; char data[]; }

user_defined_types: struct accept_ack_batch_t { short acceptor_id; short count; char data[]; }


decompiled_code:
__int64 __fastcall lea_send_repeat_request(unsigned int a1, unsigned int a2)
{
unsigned int v2; // ebx

v2 = a1;
sendbuf_clear(to_acceptors, 16LL, 0xFFFFFFFFLL);
if ( a1 < a2 )
{
do
{
if ( !qword_B60[5 * (v2 & 0x1FFF)] )
sendbuf_add_repeat_req(to_acceptors, v2);
++v2;
}
while ( a2 != v2 );
}
return sendbuf_flush(to_acceptors);
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1, unsigned int a2)
{
unsigned int v2; // ebx

v2 = a1;
sendbuf_clear(to_acceptors, 16LL, 0xFFFFFFFFLL);
if ( a1 < a2 )
{
do
{
if ( !qword_B60[5 * (v2 & 0x1FFF)] )
sendbuf_add_repeat_req(to_acceptors, v2);
++v2;
}
while ( a2 != v2 );
}
return sendbuf_flush(to_acceptors);
}

original_code:
static void
lea_send_repeat_request(iid_t from, iid_t to) {
    iid_t i;
    l_inst_info * ii;
    sendbuf_clear(to_acceptors, repeat_reqs, -1);
    for(i = from; i < to; i++) {
        ii = &learner_state[((i) & (8192 -1))];
        if(!(ii->final_value != ((void *)0))) {
            sendbuf_add_repeat_req(to_acceptors, i);
        }
    }
    sendbuf_flush(to_acceptors);
}

canonical_original_code:
static void lea_send_repeat_request(unsigned int from, unsigned int to) {
    unsigned int i;
    struct learner_instance_info *ii;
    sendbuf_clear(to_acceptors, repeat_reqs, -1);
    for(i = from; i < to; i++) {
        ii = &learner_state[((i) & (8192 -1))];
        if(!(ii->final_value != ((void *)0))) {
            sendbuf_add_repeat_req(to_acceptors, i);
        }
    }
    sendbuf_flush(to_acceptors);
}

variable_types:
from
unsigned int

variable_types:
to
unsigned int

variable_types:
i
unsigned int


decompiled_code:
int lea_hole_check()
{
int result; // eax

if ( current_iid + 0x2000 < (unsigned int)highest_iid_seen )
{
printf("[%s] ", "lea_hole_check");
printf(
"This learner is lagging behind!!!, highest seen:%lu, highest delivered:%lu\n",
(unsigned int)highest_iid_seen,
(unsigned int)(current_iid - 1));
lea_send_repeat_request(current_iid, highest_iid_seen);
}
else if ( current_iid < (unsigned int)highest_iid_closed )
{
lea_send_repeat_request(current_iid, highest_iid_closed);
}
result = event_add(&hole_check_event, &hole_check_interval);
if ( result )
result = puts("Error while adding next hole_check event");
return result;
}

canonical_decompiled_code:
int func13()
{
int result; // eax

if ( current_iid + 0x2000 < (unsigned int)highest_iid_seen )
{
printf("[%s] ", "lea_hole_check");
printf(
"This learner is lagging behind!!!, highest seen:%lu, highest delivered:%lu\n",
(unsigned int)highest_iid_seen,
(unsigned int)(current_iid - 1));
func12(current_iid, highest_iid_seen);
}
else if ( current_iid < (unsigned int)highest_iid_closed )
{
func12(current_iid, highest_iid_closed);
}
result = event_add(&hole_check_event, &hole_check_interval);
if ( result )
result = puts("Error while adding next hole_check event");
return result;
}

original_code:
static void
lea_hole_check(int fd, short event, void *arg) {
    (fd = fd);;
    (event = event);;
    (arg = arg);;
    if (highest_iid_seen > current_iid + 8192) {
        if(0 >= 0) { printf("[%s] ", __func__) ; printf ("This learner is lagging behind!!!, highest seen:%lu, highest delivered:%lu\n", highest_iid_seen, current_iid-1) ;};
        lea_send_repeat_request(current_iid, highest_iid_seen);
    } else if(highest_iid_closed > current_iid) {
        if(0 >= 1) { printf("[%s] ", __func__) ; printf ("Out of sync, highest closed:%lu, highest delivered:%lu\n", highest_iid_closed, current_iid-1) ;};
        lea_send_repeat_request(current_iid, highest_iid_closed);
    }
    if(event_add(&hole_check_event, &hole_check_interval) != 0) {
    printf("Error while adding next hole_check event\n");
 }
}

variable_types: fd int

variable_types: event short

variable_types:
arg
void *


decompiled_code:
int init_lea_success()
{
pthread_mutex_lock(&ready_lock);
learner_ready = 0;
pthread_cond_signal(&ready_cond);
return pthread_mutex_unlock(&ready_lock);
}

canonical_decompiled_code:
int func14()
{
pthread_mutex_lock(&ready_lock);
learner_ready = 0;
pthread_cond_signal(&ready_cond);
return pthread_mutex_unlock(&ready_lock);
}

original_code:
static void
init_lea_success(int fd, short event, void *arg) {
    (fd = fd);;
    (event = event);;
    (arg = arg);;
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Learner thread setting status to ready\n") ;};
    pthread_mutex_lock(&ready_lock);
    learner_ready = (0);
    pthread_cond_signal(&ready_cond);
    pthread_mutex_unlock(&ready_lock);
}


decompiled_code:
int __fastcall init_lea_failure(const char *a1)
{
printf("Learner init error: %s\n", a1);
pthread_mutex_lock(&ready_lock);
learner_ready = -1;
pthread_cond_signal(&ready_cond);
return pthread_mutex_unlock(&ready_lock);
}

canonical_decompiled_code:
int __fastcall func15(const char *a1)
{
printf("Learner init error: %s\n", a1);
pthread_mutex_lock(&ready_lock);
learner_ready = -1;
pthread_cond_signal(&ready_cond);
return pthread_mutex_unlock(&ready_lock);
}

original_code:
static void
init_lea_failure(char * msg) {
    printf("Learner init error: %s\n", msg);
    pthread_mutex_lock(&ready_lock);
    learner_ready = (-1);
    pthread_cond_signal(&ready_cond);
    pthread_mutex_unlock(&ready_lock);
}

canonical_original_code:
static void init_lea_failure(char *msg) {
    printf("Learner init error: %s\n", msg);
    pthread_mutex_lock(&ready_lock);
    learner_ready = (-1);
    pthread_cond_signal(&ready_cond);
    pthread_mutex_unlock(&ready_lock);
}

variable_types:
msg
char *


decompiled_code:
void *__fastcall init_learner_thread(void *a1)
{
delfun = (__int64 (__fastcall *)(_QWORD, _QWORD, _QWORD, _QWORD, _QWORD))a1;
if ( a1 )
{
if ( event_init() )
{
if ( (unsigned int)init_lea_structs() )
{
init_lea_failure("Error in learner structures initialization\n");
}
else if ( (unsigned int)init_lea_network() )
{
init_lea_failure("Error in learner network init\n");
}
else
{
if ( (unsigned int)init_lea_timers() )
goto LABEL_15;
if ( custom_init && (unsigned int)custom_init() )
{
init_lea_failure("Error in custom_init_function\n");
return 0LL;
}
if ( (unsigned int)init_lea_signal_ready() )
{
LABEL_15:
init_lea_failure("Error in learner timers initialization\n");
}
else
{
event_dispatch();
puts("libeven loop terminated");
}
}
}
else
{
init_lea_failure("Error in libevent init\n");
}
}
else
{
init_lea_failure("Error in libevent init\n");
puts("Error NULL callback!");
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func16(void *a1)
{
delfun = (__int64 (__fastcall *)(_QWORD, _QWORD, _QWORD, _QWORD, _QWORD))a1;
if ( a1 )
{
if ( event_init() )
{
if ( (unsigned int)func2() )
{
func15("Error in learner structures initialization\n");
}
else if ( (unsigned int)func3() )
{
func15("Error in learner network init\n");
}
else
{
if ( (unsigned int)func4() )
goto LABEL_15;
if ( custom_init && (unsigned int)custom_init() )
{
func15("Error in custom_init_function\n");
return 0LL;
}
if ( (unsigned int)func5() )
{
LABEL_15:
func15("Error in learner timers initialization\n");
}
else
{
event_dispatch();
puts("libeven loop terminated");
}
}
}
else
{
func15("Error in libevent init\n");
}
}
else
{
func15("Error in libevent init\n");
puts("Error NULL callback!");
}
return 0LL;
}

original_code:
static void*
init_learner_thread(void* arg) {
    delfun = (deliver_function) arg;
    if(delfun == ((void *)0)) {
        init_lea_failure("Error in libevent init\n");
        printf("Error NULL callback!\n");
        return ((void *)0);
    }
    if((eb = event_init()) == ((void *)0)) {
        init_lea_failure("Error in libevent init\n");
        return ((void *)0);
    }
    if(init_lea_structs() != 0) {
        init_lea_failure("Error in learner structures initialization\n");
        return ((void *)0);
    }
    if(init_lea_network() != 0) {
        init_lea_failure("Error in learner network init\n");
        return ((void *)0);
    }
    if(init_lea_timers() != 0) {
        init_lea_failure("Error in learner timers initialization\n");
        return ((void *)0);
    }
    if(custom_init != ((void *)0) && custom_init() != 0) {
        init_lea_failure("Error in custom_init_function\n");
        return ((void *)0);
    } else {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Custom init completed\n") ;};
    }
    if(init_lea_signal_ready() != 0) {
        init_lea_failure("Error in learner timers initialization\n");
        return ((void *)0);
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Learner thread ready, starting libevent loop\n") ;};
    event_dispatch();
    printf("libeven loop terminated\n");
    return ((void *)0);
}

canonical_original_code:
static void *init_learner_thread(void *arg) {
    delfun = (void (*)(char *, unsigned long, unsigned int, unsigned int, int)) arg;
    if(delfun == ((void *)0)) {
        init_lea_failure("Error in libevent init\n");
        printf("Error NULL callback!\n");
        return ((void *)0);
    }
    if((eb = event_init()) == ((void *)0)) {
        init_lea_failure("Error in libevent init\n");
        return ((void *)0);
    }
    if(init_lea_structs() != 0) {
        init_lea_failure("Error in learner structures initialization\n");
        return ((void *)0);
    }
    if(init_lea_network() != 0) {
        init_lea_failure("Error in learner network init\n");
        return ((void *)0);
    }
    if(init_lea_timers() != 0) {
        init_lea_failure("Error in learner timers initialization\n");
        return ((void *)0);
    }
    if(custom_init != ((void *)0) && custom_init() != 0) {
        init_lea_failure("Error in custom_init_function\n");
        return ((void *)0);
    } else {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Custom init completed\n") ;};
    }
    if(init_lea_signal_ready() != 0) {
        init_lea_failure("Error in learner timers initialization\n");
        return ((void *)0);
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Learner thread ready, starting libevent loop\n") ;};
    event_dispatch();
    printf("libeven loop terminated\n");
    return ((void *)0);
}

return_type: void *


decompiled_code:
__int64 init_lea_wait_ready()
{
unsigned int v0; // ebx

pthread_mutex_lock(&ready_lock);
do
{
pthread_cond_wait(&ready_cond, &ready_lock);
v0 = learner_ready;
}
while ( learner_ready == 1 );
if ( (unsigned int)(learner_ready + 1) > 1 )
{
printf("Unknow learner status: %d\n", (unsigned int)learner_ready);
v0 = -1;
}
pthread_mutex_unlock(&ready_lock);
return v0;
}

canonical_decompiled_code:
__int64 func17()
{
unsigned int v0; // ebx

pthread_mutex_lock(&ready_lock);
do
{
pthread_cond_wait(&ready_cond, &ready_lock);
v0 = learner_ready;
}
while ( learner_ready == 1 );
if ( (unsigned int)(learner_ready + 1) > 1 )
{
printf("Unknow learner status: %d\n", (unsigned int)learner_ready);
v0 = -1;
}
pthread_mutex_unlock(&ready_lock);
return v0;
}

original_code:
static int
init_lea_wait_ready() {
    int status;
    pthread_mutex_lock(&ready_lock);
    while(1) {
        pthread_cond_wait(&ready_cond, &ready_lock);
        status = learner_ready;
        if(status == (1)) {
            continue;
        } else {
            break;
        }
    }
    if (status != (0) && status != (-1)) {
        printf("Unknow learner status: %d\n", status);
        status = (-1);
    }
    pthread_mutex_unlock(&ready_lock);
    return status;
}

variable_types: status int


decompiled_code:
_BOOL8 __fastcall learner_is_closed(int a1)
{
_BOOL4 v1; // edx

v1 = 0;
if ( learner_state[10 * (a1 & 0x1FFF)] == a1 )
v1 = qword_B60[5 * (a1 & 0x1FFF)] != 0;
return v1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func18(int a1)
{
_BOOL4 v1; // edx

v1 = 0;
if ( learner_state[10 * (a1 & 0x1FFF)] == a1 )
v1 = qword_B60[5 * (a1 & 0x1FFF)] != 0;
return v1;
}

original_code:
int learner_is_closed(iid_t iid) {
    l_inst_info * ii = &learner_state[((iid) & (8192 -1))];
    return ((iid == ii->iid) && (ii->final_value != ((void *)0)));
}

canonical_original_code:
int learner_is_closed(unsigned int iid) {
    struct learner_instance_info *ii = &learner_state[((iid) & (8192 -1))];
    return ((iid == ii->iid) && (ii->final_value != ((void *)0)));
}

variable_types:
iid
unsigned int

variable_types: struct learner_instance_info { unsigned int iid; unsigned int last_update_ballot; struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; } *acks[3]; struct accept_ack_t *final_value; } *


decompiled_code:
__int64 __fastcall learner_init(void *arg, __int64 (*a2)(void))
{
unsigned int v2; // ebx

custom_init = a2;
if ( pthread_create(&learner_thread, 0LL, init_learner_thread, arg) )
{
perror("pthread create learner thread");
v2 = -1;
}
else
{
v2 = 0;
if ( (unsigned int)init_lea_wait_ready() == -1 )
{
puts("Learner initialization failed!");
v2 = -1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func19(void *arg, __int64 (*a2)(void))
{
unsigned int v2; // ebx

custom_init = a2;
if ( pthread_create(&learner_thread, 0LL, init_learner_thread, arg) )
{
perror("pthread create learner thread");
v2 = -1;
}
else
{
v2 = 0;
if ( (unsigned int)func17() == -1 )
{
puts("Learner initialization failed!");
v2 = -1;
}
}
return v2;
}

original_code:
int learner_init(deliver_function f, custom_init_function cif) {
    custom_init = cif;
    if (pthread_create(&learner_thread, ((void *)0), init_learner_thread, (void*) f) != 0) {
        perror("pthread create learner thread");
        return -1;
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Learner thread started, waiting for ready signal\n") ;};
    if (init_lea_wait_ready() == (-1)) {
        printf("Learner initialization failed!\n");
        return -1;
    }
    if(0 >= 1) { printf("[%s] ", __func__) ; printf ("Learner is ready\n") ;};
    return 0;
}

canonical_original_code:
int learner_init(void (*f)(char *, unsigned long, unsigned int, unsigned int, int), int (*cif)()) {
    custom_init = cif;
    if (pthread_create(&learner_thread, ((void *)0), init_learner_thread, (void *) f) != 0) {
        perror("pthread create learner thread");
        return -1;
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Learner thread started, waiting for ready signal\n") ;};
    if (init_lea_wait_ready() == (-1)) {
        printf("Learner initialization failed!\n");
        return -1;
    }
    if(0 >= 1) { printf("[%s] ", __func__) ; printf ("Learner is ready\n") ;};
    return 0;
}

variable_types: void (*)(char *, unsigned long, unsigned int, unsigned int, int)

variable_types:
cif
int (*)()


decompiled_code:
__int64 learner_suspend()
{
__int64 result; // rax

event_del(&learner_msg_event);
result = udp_receiver_destroy(for_learner);
for_learner = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func20()
{
__int64 result; // rax

event_del(&learner_msg_event);
result = udp_receiver_destroy(for_learner);
for_learner = 0LL;
return result;
}

original_code:
void learner_suspend() {
    event_del(&learner_msg_event);
    udp_receiver_destroy(for_learner);
    for_learner = ((void *)0);
    if(0 >= 1) { printf("[%s] ", __func__) ; printf ("Learner events suspended!\n") ;};
}


decompiled_code:
size_t *__fastcall vh_wrap_value(void *src, size_t n)
{
size_t *v2; // rbx

v2 = (size_t *)paxos_normal_malloc(n + 16);
*v2 = n;
v2[1] = 0LL;
memcpy(v2 + 2, src, n);
return v2;
}

canonical_decompiled_code:
size_t *__fastcall func0(void *src, size_t n)
{
size_t *v2; // rbx

v2 = (size_t *)paxos_normal_malloc(n + 16);
*v2 = n;
v2[1] = 0LL;
memcpy(v2 + 2, src, n);
return v2;
}

original_code:
vh_value_wrapper *
vh_wrap_value(char * value, size_t size) {
    vh_value_wrapper * vw = paxos_normal_malloc(sizeof(vh_value_wrapper) + size);;
    vw->value_size = size;
    vw->next = ((void *)0);
    memcpy(vw->value, value, size);
    return vw;
}

canonical_original_code:
struct vh_value_wrapper_t *vh_wrap_value(char *value, unsigned long size) {
    struct vh_value_wrapper_t *vw = paxos_normal_malloc(sizeof(vh_value_wrapper) + size);;
    vw->value_size = size;
    vw->next = ((void *)0);
    memcpy(vw->value, value, size);
    return vw;
}

variable_types:
value
char *

variable_types:
size
unsigned long

variable_types: struct vh_value_wrapper_t { unsigned long value_size; struct vh_value_wrapper_t *next; char value[]; } *


decompiled_code:
int __fastcall vh_value_compare(size_t *a1, _QWORD *a2)
{
int result; // eax

if ( *a1 == *a2 )
result = memcmp(a1 + 2, a2 + 2, *a1);
else
result = -1;
return result;
}

canonical_decompiled_code:
int __fastcall func1(size_t *a1, _QWORD *a2)
{
int result; // eax

if ( *a1 == *a2 )
result = memcmp(a1 + 2, a2 + 2, *a1);
else
result = -1;
return result;
}

original_code:
int vh_value_compare(vh_value_wrapper * vw1, vh_value_wrapper * vw2) {
    if(vw1->value_size != vw2->value_size) {
        return -1;
    }
    return memcmp(vw1->value, vw2->value, vw1->value_size);
}

canonical_original_code:
int vh_value_compare(struct vh_value_wrapper_t *vw1, struct vh_value_wrapper_t *vw2) {
    if(vw1->value_size != vw2->value_size) {
        return -1;
    }
    return memcmp(vw1->value, vw2->value, vw1->value_size);
}

user_defined_types: struct vh_value_wrapper_t { unsigned long value_size; struct vh_value_wrapper_t *next; char value[]; }


decompiled_code:
__int64 vh_init()
{
unsigned int *v0; // rax
__int64 result; // rax

vh_list_size = 0;
vh_list_head = 0LL;
vh_list_tail = 0LL;
dropped_count = 0LL;
v0 = (unsigned int *)udp_receiver_new(6004LL);
for_leader = (__int64)v0;
if ( v0 )
{
event_set(&leader_msg_event, *v0, 18LL, vh_handle_newmsg, 0LL);
event_add(&leader_msg_event, 0LL);
result = 0LL;
}
else
{
puts("Error creating proposer network receiver");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int *v0; // rax
__int64 result; // rax

vh_list_size = 0;
vh_list_head = 0LL;
vh_list_tail = 0LL;
dropped_count = 0LL;
v0 = (unsigned int *)udp_receiver_new(6004LL);
for_leader = (__int64)v0;
if ( v0 )
{
event_set(&leader_msg_event, *v0, 18LL, vh_handle_newmsg, 0LL);
event_add(&leader_msg_event, 0LL);
result = 0LL;
}
else
{
puts("Error creating proposer network receiver");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
vh_init() {
    vh_list_size = 0;
    vh_list_head = ((void *)0);
    vh_list_tail = ((void *)0);
    dropped_count = 0;
    for_leader = udp_receiver_new(6004);
    if (for_leader == ((void *)0)) {
        printf("Error creating proposer network receiver\n");
        return -1;
    }
    event_set(&leader_msg_event, for_leader->sock, 0x02|0x10, vh_handle_newmsg, ((void *)0));
    event_add(&leader_msg_event, ((void *)0));
    return 0;
}


decompiled_code:
__int64 vh_pending_list_size()
{
unsigned int v0; // ebx

pthread_mutex_lock(&pending_list_lock);
v0 = vh_list_size;
pthread_mutex_unlock(&pending_list_lock);
return v0;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // ebx

pthread_mutex_lock(&pending_list_lock);
v0 = vh_list_size;
pthread_mutex_unlock(&pending_list_lock);
return v0;
}

original_code:
int vh_pending_list_size() {
    pthread_mutex_lock(&pending_list_lock);
    int ls = vh_list_size;
    pthread_mutex_unlock(&pending_list_lock);
    return ls;
}

variable_types: ls int


decompiled_code:
__int64 vh_get_dropped_count()
{
int v0; // ebx

pthread_mutex_lock(&pending_list_lock);
v0 = dropped_count;
pthread_mutex_unlock(&pending_list_lock);
return v0;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // ebx

pthread_mutex_lock(&pending_list_lock);
v0 = dropped_count;
pthread_mutex_unlock(&pending_list_lock);
return v0;
}

original_code:
long unsigned int vh_get_dropped_count() {
    pthread_mutex_lock(&pending_list_lock);
    int dc = dropped_count;
    pthread_mutex_unlock(&pending_list_lock);
    return dc;
}

canonical_original_code:
unsigned long vh_get_dropped_count() {
    pthread_mutex_lock(&pending_list_lock);
    int dc = dropped_count;
    pthread_mutex_unlock(&pending_list_lock);
    return dc;
}

variable_types: dc int


decompiled_code:
int __fastcall vh_enqueue_value(void *src, size_t n)
{
size_t *v2; // rax
int result; // eax

pthread_mutex_lock(&pending_list_lock);
if ( vh_list_size > 50 )
{
result = pthread_mutex_unlock(&pending_list_lock);
++dropped_count;
}
else
{
v2 = vh_wrap_value(src, n);
if ( __PAIR128__(vh_list_head, vh_list_tail) == 0 )
{
vh_list_head = (__int64)v2;
vh_list_tail = (__int64)v2;
vh_list_size = 1;
}
else
{
*(_QWORD *)(vh_list_tail + 8) = v2;
vh_list_tail = (__int64)v2;
++vh_list_size;
}
result = pthread_mutex_unlock(&pending_list_lock);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(void *src, size_t n)
{
size_t *v2; // rax
int result; // eax

pthread_mutex_lock(&pending_list_lock);
if ( vh_list_size > 50 )
{
result = pthread_mutex_unlock(&pending_list_lock);
++dropped_count;
}
else
{
v2 = func0(src, n);
if ( __PAIR128__(vh_list_head, vh_list_tail) == 0 )
{
vh_list_head = (__int64)v2;
vh_list_tail = (__int64)v2;
vh_list_size = 1;
}
else
{
*(_QWORD *)(vh_list_tail + 8) = v2;
vh_list_tail = (__int64)v2;
++vh_list_size;
}
result = pthread_mutex_unlock(&pending_list_lock);
}
return result;
}

original_code:
void vh_enqueue_value(char * value, size_t value_size) {
    pthread_mutex_lock(&pending_list_lock);
    if(vh_list_size > 50) {
        pthread_mutex_unlock(&pending_list_lock);
        if(0 >= 1) { printf("[%s] ", __func__) ; printf ("Value dropped, list is already too long\n") ;};
        dropped_count += 1;
        return;
    }
    vh_value_wrapper * new_vw = vh_wrap_value(value, value_size);
 if (vh_list_head == ((void *)0) && vh_list_tail == ((void *)0)) {
  vh_list_head = new_vw;
  vh_list_tail = new_vw;
        vh_list_size = 1;
 } else {
  vh_list_tail->next = new_vw;
  vh_list_tail = new_vw;
        vh_list_size += 1;
 }
    pthread_mutex_unlock(&pending_list_lock);
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Value of size %lu enqueued\n", value_size) ;};
}

canonical_original_code:
void vh_enqueue_value(char *value, unsigned long value_size) {
    pthread_mutex_lock(&pending_list_lock);
    if(vh_list_size > 50) {
        pthread_mutex_unlock(&pending_list_lock);
        if(0 >= 1) { printf("[%s] ", __func__) ; printf ("Value dropped, list is already too long\n") ;};
        dropped_count += 1;
        return;
    }
    struct vh_value_wrapper_t *new_vw = vh_wrap_value(value, value_size);
 if (vh_list_head == ((void *)0) && vh_list_tail == ((void *)0)) {
  vh_list_head = new_vw;
  vh_list_tail = new_vw;
        vh_list_size = 1;
 } else {
  vh_list_tail->next = new_vw;
  vh_list_tail = new_vw;
        vh_list_size += 1;
 }
    pthread_mutex_unlock(&pending_list_lock);
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Value of size %lu enqueued\n", value_size) ;};
}


decompiled_code:
int __fastcall vh_handle_newmsg(int a1)
{
__int64 v1; // rsi
int result; // eax

if ( *(_DWORD *)for_leader != a1 )
_assert_fail("sock == for_leader->sock", "proposer_values_handler.c", 0x30u, "vh_handle_newmsg");
if ( (int)udp_read_next_message() < 0 )
return puts("Dropping invalid client-to-leader message");
v1 = *(unsigned int *)(for_leader + 28);
if ( (_DWORD)v1 == 32 )
result = vh_enqueue_value((void *)(for_leader + 32), *(_QWORD *)(for_leader + 20));
else
result = printf("Unknow msg type %d received by proposer\n", v1);
return result;
}

canonical_decompiled_code:
int __fastcall func6(int a1)
{
__int64 v1; // rsi
int result; // eax

if ( *(_DWORD *)for_leader != a1 )
_assert_fail("sock == for_leader->sock", "proposer_values_handler.c", 0x30u, "vh_handle_newmsg");
if ( (int)udp_read_next_message() < 0 )
return puts("Dropping invalid client-to-leader message");
v1 = *(unsigned int *)(for_leader + 28);
if ( (_DWORD)v1 == 32 )
result = func5((void *)(for_leader + 32), *(_QWORD *)(for_leader + 20));
else
result = printf("Unknow msg type %d received by proposer\n", v1);
return result;
}

original_code:
static void
vh_handle_newmsg(int sock, short event, void *arg) {
    (sock = sock);;
    (event = event);;
    (arg = arg);;
    ((void) sizeof ((sock == for_leader->sock) ? 1 : 0), __extension__ ({ if (sock == for_leader->sock) ; else __assert_fail ("sock == for_leader->sock", "proposer_values_handler.c", 48, __extension__ __PRETTY_FUNCTION__); }));
    int valid = udp_read_next_message(for_leader);
    if (valid < 0) {
        printf("Dropping invalid client-to-leader message\n");
        return;
    }
    paxos_msg * msg = (paxos_msg*) &for_leader->recv_buffer;
    switch(msg->type) {
        case submit: {
            vh_enqueue_value(msg->data, msg->data_size);
        }
        break;
        default: {
            printf("Unknow msg type %d received by proposer\n", msg->type);
        }
    }
}

canonical_original_code:
static void vh_handle_newmsg(int sock, short event, void *arg) {
    (sock = sock);;
    (event = event);;
    (arg = arg);;
    ((void) sizeof ((sock == for_leader->sock) ? 1 : 0), __extension__ ({ if (sock == for_leader->sock) ; else __assert_fail ("sock == for_leader->sock", "proposer_values_handler.c", 48, __extension__ __PRETTY_FUNCTION__); }));
    int valid = udp_read_next_message(for_leader);
    if (valid < 0) {
        printf("Dropping invalid client-to-leader message\n");
        return;
    }
    struct paxos_msg_t *msg = (paxos_msg*) &for_leader->recv_buffer;
    switch(msg->type) {
        case submit: {
            vh_enqueue_value(msg->data, msg->data_size);
        }
        break;
        default: {
            printf("Unknow msg type %d received by proposer\n", msg->type);
        }
    }
}

variable_types: sock int

variable_types: valid int

variable_types: struct paxos_msg_t { unsigned long data_size; enum pax_msg_code_e { prepare_reqs=1, prepare_acks=2, accept_reqs=4, accept_acks=8, repeat_reqs=16, submit=32, leader_announce=64, alive_ping=65 } type; char data[]; } *


decompiled_code:
__int64 vh_get_next_pending()
{
__int64 v0; // rbx
__int64 v2; // rbp

pthread_mutex_lock(&pending_list_lock);
v0 = vh_list_head;
if ( vh_list_head )
{
vh_list_head = *(_QWORD *)(vh_list_head + 8);
if ( v0 == vh_list_tail )
vh_list_tail = 0LL;
}
else
{
v2 = vh_list_tail;
if ( !vh_list_tail )
{
pthread_mutex_unlock(&pending_list_lock);
return v2;
}
vh_list_head = *(_QWORD *)((char *)&loc_7 + 1);
}
--vh_list_size;
pthread_mutex_unlock(&pending_list_lock);
return v0;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // rbx
__int64 v2; // rbp

pthread_mutex_lock(&pending_list_lock);
v0 = vh_list_head;
if ( vh_list_head )
{
vh_list_head = *(_QWORD *)(vh_list_head + 8);
if ( v0 == vh_list_tail )
vh_list_tail = 0LL;
}
else
{
v2 = vh_list_tail;
if ( !vh_list_tail )
{
pthread_mutex_unlock(&pending_list_lock);
return v2;
}
vh_list_head = *(_QWORD *)((char *)&loc_7 + 1);
}
--vh_list_size;
pthread_mutex_unlock(&pending_list_lock);
return v0;
}

original_code:
vh_value_wrapper *
vh_get_next_pending() {
    pthread_mutex_lock(&pending_list_lock);
 if (vh_list_head == ((void *)0) && vh_list_tail == ((void *)0)) {
        pthread_mutex_unlock(&pending_list_lock);
        return ((void *)0);
    }
    vh_value_wrapper * first_vw = vh_list_head;
    vh_list_head = first_vw->next;
    if(vh_list_tail == first_vw) {
        vh_list_tail = ((void *)0);
    }
    vh_list_size -= 1;
    pthread_mutex_unlock(&pending_list_lock);
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Popping value of size %lu\n", first_vw->value_size) ;};
    return first_vw;
}

canonical_original_code:
struct vh_value_wrapper_t *vh_get_next_pending() {
    pthread_mutex_lock(&pending_list_lock);
 if (vh_list_head == ((void *)0) && vh_list_tail == ((void *)0)) {
        pthread_mutex_unlock(&pending_list_lock);
        return ((void *)0);
    }
    struct vh_value_wrapper_t *first_vw = vh_list_head;
    vh_list_head = first_vw->next;
    if(vh_list_tail == first_vw) {
        vh_list_tail = ((void *)0);
    }
    vh_list_size -= 1;
    pthread_mutex_unlock(&pending_list_lock);
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Popping value of size %lu\n", first_vw->value_size) ;};
    return first_vw;
}

return_type: struct vh_value_wrapper_t { unsigned long value_size; struct vh_value_wrapper_t *next; char value[]; } *


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

event_del(&leader_msg_event);
udp_receiver_destroy(for_leader);
while ( 1 )
{
result = (void *)vh_get_next_pending();
if ( !result )
break;
free(result);
}
return result;
}

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

event_del(&leader_msg_event);
udp_receiver_destroy(for_leader);
while ( 1 )
{
result = (void *)func7();
if ( !result )
break;
free(result);
}
return result;
}

original_code:
void
vh_shutdown() {
    event_del(&leader_msg_event);
    udp_receiver_destroy(for_leader);
    vh_value_wrapper * vw;
    while ((vw = vh_get_next_pending()) != ((void *)0)) {
        vh_notify_client(-1, vw);
        free(vw);
    }
}

canonical_original_code:
void vh_shutdown() {
    event_del(&leader_msg_event);
    udp_receiver_destroy(for_leader);
    struct vh_value_wrapper_t *vw;
    while ((vw = vh_get_next_pending()) != ((void *)0)) {
        vh_notify_client(-1, vw);
        free(vw);
    }
}


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

pthread_mutex_lock(&pending_list_lock);
v1 = vh_list_head;
*(_QWORD *)(a1 + 8) = vh_list_head;
if ( v1 || vh_list_tail )
{
vh_list_head = a1;
++vh_list_size;
}
else
{
vh_list_head = a1;
vh_list_tail = a1;
vh_list_size = 1;
}
return pthread_mutex_unlock(&pending_list_lock);
}

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

pthread_mutex_lock(&pending_list_lock);
v1 = vh_list_head;
*(_QWORD *)(a1 + 8) = vh_list_head;
if ( v1 || vh_list_tail )
{
vh_list_head = a1;
++vh_list_size;
}
else
{
vh_list_head = a1;
vh_list_tail = a1;
vh_list_size = 1;
}
return pthread_mutex_unlock(&pending_list_lock);
}

original_code:
void
vh_push_back_value(vh_value_wrapper * vw) {
    pthread_mutex_lock(&pending_list_lock);
    vw->next = vh_list_head;
 if (vh_list_head == ((void *)0) && vh_list_tail == ((void *)0)) {
  vh_list_head = vw;
  vh_list_tail = vw;
        vh_list_size = 1;
 } else {
  vh_list_head = vw;
        vh_list_size += 1;
 }
    pthread_mutex_unlock(&pending_list_lock);
}

canonical_original_code:
void vh_push_back_value(struct vh_value_wrapper_t *vw) {
    pthread_mutex_lock(&pending_list_lock);
    vw->next = vh_list_head;
 if (vh_list_head == ((void *)0) && vh_list_tail == ((void *)0)) {
  vh_list_head = vw;
  vh_list_tail = vw;
        vh_list_size = 1;
 } else {
  vh_list_head = vw;
        vh_list_size += 1;
 }
    pthread_mutex_unlock(&pending_list_lock);
}


decompiled_code:
void vh_notify_client()
{
;
}

canonical_decompiled_code:
void func10()
{
;
}

original_code:
void vh_notify_client(unsigned int result, vh_value_wrapper * vw) {
    if(result != 0) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Notify client -> Submit failed\n") ;};
    } else {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Notify client -> Submit successful\n") ;};
    }
}

canonical_original_code:
void vh_notify_client(unsigned int result, struct vh_value_wrapper_t *vw) {
    if(result != 0) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Notify client -> Submit failed\n") ;};
    } else {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Notify client -> Submit successful\n") ;};
    }
}

variable_types:
result
unsigned int


decompiled_code:
int __fastcall pax_submit_sharedmem(void *a1, size_t a2)
{
return vh_enqueue_value(a1, a2);
}

canonical_decompiled_code:
int __fastcall func11(void *a1, size_t a2)
{
return func5(a1, a2);
}

original_code:
void pax_submit_sharedmem(char* value, size_t val_size) {
    vh_enqueue_value(value, val_size);
}

canonical_original_code:
void pax_submit_sharedmem(char *value, unsigned long val_size) {
    vh_enqueue_value(value, val_size);
}


original_code:
size_t prepare_ack_batch_size_calc(prepare_ack_batch * pab) {
    size_t total_size = 0;
    size_t offset = 0;
    short int i;
    prepare_ack * pa;
    for(i = 0; i < pab->count; i++) {
        pa = (prepare_ack*) &pab->data[offset];
        offset += (pa->value_size + sizeof(prepare_ack));
    }
    total_size = sizeof(prepare_ack_batch) + offset;
    return total_size;
}

canonical_original_code:
unsigned long prepare_ack_batch_size_calc(struct prepare_ack_batch_t *pab) {
    unsigned long total_size = 0;
    unsigned long offset = 0;
    short i;
    struct prepare_ack_t *pa;
    for(i = 0; i < pab->count; i++) {
        pa = (struct prepare_ack_t *) &pab->data[offset];
        offset += (pa->value_size + sizeof(prepare_ack));
    }
    total_size = sizeof(prepare_ack_batch) + offset;
    return total_size;
}

variable_types: struct prepare_ack_batch_t { short acceptor_id; short count; char data[]; } *

variable_types:
offset
unsigned long

variable_types: i short

variable_types: struct prepare_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; unsigned long value_size; char value[]; } *


decompiled_code:
__int64 __fastcall accept_req_batch_size_calc(__int16 *a1)
{
int v1; // edx
__int64 v2; // rax

if ( *a1 <= 0 )
{
v2 = 0LL;
}
else
{
v1 = 0;
v2 = 0LL;
do
{
v2 += *(_QWORD *)((char *)a1 + v2 + 12) + 16LL;
++v1;
}
while ( (_WORD)v1 != *a1 );
}
return v2 + 4;
}

original_code:
size_t accept_req_batch_size_calc(accept_req_batch * arb) {
    size_t total_size = 0;
    size_t offset = 0;
    short int i;
    accept_req * ar;
    for(i = 0; i < arb->count; i++) {
        ar = (accept_req*) &arb->data[offset];
        offset += (ar->value_size + sizeof(accept_req));
    }
    total_size = sizeof(accept_req_batch) + offset;
    return total_size;
}

canonical_original_code:
unsigned long accept_req_batch_size_calc(struct accept_req_batch_t *arb) {
    unsigned long total_size = 0;
    unsigned long offset = 0;
    short i;
    struct accept_req_t *ar;
    for(i = 0; i < arb->count; i++) {
        ar = (struct accept_req_t *) &arb->data[offset];
        offset += (ar->value_size + sizeof(accept_req));
    }
    total_size = sizeof(accept_req_batch) + offset;
    return total_size;
}

variable_types: struct accept_req_batch_t { short count; short proposer_id; char data[]; } *

variable_types: struct accept_req_t { unsigned int iid; unsigned int ballot; unsigned long value_size; char value[]; } *


original_code:
size_t accept_ack_batch_size_calc(accept_ack_batch * aab) {
    size_t total_size = 0;
    size_t offset = 0;
    short int i;
    accept_ack * aa;
    for(i = 0; i < aab->count; i++) {
        aa = (accept_ack*) &aab->data[offset];
        offset += (aa->value_size + sizeof(accept_ack));
    }
    total_size = sizeof(accept_ack_batch) + offset;
    return total_size;
}

canonical_original_code:
unsigned long accept_ack_batch_size_calc(struct accept_ack_batch_t *aab) {
    unsigned long total_size = 0;
    unsigned long offset = 0;
    short i;
    struct accept_ack_t *aa;
    for(i = 0; i < aab->count; i++) {
        aa = (struct accept_ack_t *) &aab->data[offset];
        offset += (aa->value_size + sizeof(accept_ack));
    }
    total_size = sizeof(accept_ack_batch) + offset;
    return total_size;
}

variable_types: struct accept_ack_batch_t { short acceptor_id; short count; char data[]; } *

variable_types: struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; } *


decompiled_code:
__int64 __fastcall validate_paxos_msg(__int64 a1, __int64 a2)
{
__int64 v3; // rsi
__int64 v4; // rsi
__int64 result; // rax
unsigned __int16 v6; // ax
unsigned __int16 v7; // ax

v3 = *(_QWORD *)a1 + 16LL;
if ( v3 != a2 )
{
printf("Invalid message, declared size:%lu received size:%u\n", v3, a2);
return 0xFFFFFFFFLL;
}
v4 = *(unsigned int *)(a1 + 8);
if ( (unsigned int)v4 > 0x20 )
{
if ( (unsigned int)(v4 - 64) <= 1 )
return 0LL;
LABEL_7:
printf("Unknow paxos message type:%d\n", v4);
return 0xFFFFFFFFLL;
}
if ( !(_DWORD)v4 )
goto LABEL_7;
switch ( (int)v4 )
{
case 1:
v6 = *(_WORD *)(a1 + 14);
if ( v6 > 9u )
goto LABEL_12;
result = 0LL;
break;
case 2:
v7 = *(_WORD *)(a1 + 12);
if ( v7 > 2u )
goto LABEL_15;
result = 0LL;
break;
case 4:
v6 = *(_WORD *)(a1 + 14);
if ( v6 > 9u )
{
LABEL_12:
printf("Invalida proposer id:%d\n", (unsigned int)(__int16)v6);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
break;
case 8:
v7 = *(_WORD *)(a1 + 12);
if ( v7 > 2u )
{
LABEL_15:
printf("Invalida acceptor id:%d\n", (unsigned int)(__int16)v7);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
break;
case 16:
case 32:
result = 0LL;
break;
default:
goto LABEL_7;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
__int64 v3; // rsi
__int64 v4; // rsi
__int64 result; // rax
unsigned __int16 v6; // ax
unsigned __int16 v7; // ax

v3 = *(_QWORD *)a1 + 16LL;
if ( v3 != a2 )
{
printf("Invalid message, declared size:%lu received size:%u\n", v3, a2);
return 0xFFFFFFFFLL;
}
v4 = *(unsigned int *)(a1 + 8);
if ( (unsigned int)v4 > 0x20 )
{
if ( (unsigned int)(v4 - 64) <= 1 )
return 0LL;
LABEL_7:
printf("Unknow paxos message type:%d\n", v4);
return 0xFFFFFFFFLL;
}
if ( !(_DWORD)v4 )
goto LABEL_7;
switch ( (int)v4 )
{
case 1:
v6 = *(_WORD *)(a1 + 14);
if ( v6 > 9u )
goto LABEL_12;
result = 0LL;
break;
case 2:
v7 = *(_WORD *)(a1 + 12);
if ( v7 > 2u )
goto LABEL_15;
result = 0LL;
break;
case 4:
v6 = *(_WORD *)(a1 + 14);
if ( v6 > 9u )
{
LABEL_12:
printf("Invalida proposer id:%d\n", (unsigned int)(__int16)v6);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
break;
case 8:
v7 = *(_WORD *)(a1 + 12);
if ( v7 > 2u )
{
LABEL_15:
printf("Invalida acceptor id:%d\n", (unsigned int)(__int16)v7);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
break;
case 16:
case 32:
result = 0LL;
break;
default:
goto LABEL_7;
}
return result;
}

original_code:
static int validate_paxos_msg(paxos_msg * m, size_t msg_size) {
    size_t expected_size = sizeof(paxos_msg);
    if (m->data_size + sizeof(paxos_msg) != msg_size) {
        printf("Invalid message, declared size:%lu received size:%u\n", (m->data_size + sizeof(paxos_msg)), (unsigned int)msg_size);
        return -1;
    }
    switch(m->type) {
        case prepare_reqs: {
            prepare_req_batch * prb = (prepare_req_batch *)m->data;
            if(prb->proposer_id < 0 || prb->proposer_id >= 10) {
                printf("Invalida proposer id:%d\n", prb->proposer_id);
                return -1;
            }
            expected_size += (sizeof(prepare_req_batch) + (prb->count*sizeof(prepare_req)));
        }
        break;
        case prepare_acks: {
            prepare_ack_batch * pab = (prepare_ack_batch *)m->data;
            if(pab->acceptor_id < 0 || pab->acceptor_id >= 3) {
                printf("Invalida acceptor id:%d\n", pab->acceptor_id);
                return -1;
            }
            expected_size += (prepare_ack_batch_size_calc(pab));
        }
        break;
        case accept_reqs: {
            accept_req_batch * arb = (accept_req_batch *)m->data;
            if(arb->proposer_id < 0 || arb->proposer_id >= 10) {
                printf("Invalida proposer id:%d\n", arb->proposer_id);
                return -1;
            }
            expected_size += (accept_req_batch_size_calc(arb));
        }
        break;
        case accept_acks: {
            accept_ack_batch * aab = (accept_ack_batch *)m->data;
            if(aab->acceptor_id < 0 || aab->acceptor_id >= 3) {
                printf("Invalida acceptor id:%d\n", aab->acceptor_id);
                return -1;
            }
            expected_size += (accept_ack_batch_size_calc(aab));
        }
        break;
        case repeat_reqs: {
            repeat_req_batch * rrb = (repeat_req_batch *)m->data;
            expected_size += (sizeof(repeat_req_batch) + (sizeof(iid_t) * rrb->count));
        }
        break;
        case submit: {
            expected_size += m->data_size;
        }
        break;
        case alive_ping: {
            expected_size += sizeof(alive_ping_msg);
        }
        break;
        case leader_announce: {
            expected_size += sizeof(leader_announce_msg);
        }
        break;
        default: {
            printf("Unknow paxos message type:%d\n", m->type);
            return -1;
        }
        if(msg_size != expected_size) {
            printf("Invalid size for msg_type:%d declared:%u received:%u\n", m->type, (unsigned int)expected_size, (unsigned int)msg_size);
        }
    }
    return 0;
}

canonical_original_code:
static int validate_paxos_msg(struct paxos_msg_t *m, unsigned long msg_size) {
    unsigned long expected_size = sizeof(paxos_msg);
    if (m->data_size + sizeof(paxos_msg) != msg_size) {
        printf("Invalid message, declared size:%lu received size:%u\n", (m->data_size + sizeof(paxos_msg)), (unsigned int)msg_size);
        return -1;
    }
    switch(m->type) {
        case prepare_reqs: {
            struct prepare_req_batch_t *prb = (prepare_req_batch *)m->data;
            if(prb->proposer_id < 0 || prb->proposer_id >= 10) {
                printf("Invalida proposer id:%d\n", prb->proposer_id);
                return -1;
            }
            expected_size += (sizeof(prepare_req_batch) + (prb->count*sizeof(prepare_req)));
        }
        break;
        case prepare_acks: {
            struct prepare_ack_batch_t *pab = (prepare_ack_batch *)m->data;
            if(pab->acceptor_id < 0 || pab->acceptor_id >= 3) {
                printf("Invalida acceptor id:%d\n", pab->acceptor_id);
                return -1;
            }
            expected_size += (prepare_ack_batch_size_calc(pab));
        }
        break;
        case accept_reqs: {
            struct accept_req_batch_t *arb = (accept_req_batch *)m->data;
            if(arb->proposer_id < 0 || arb->proposer_id >= 10) {
                printf("Invalida proposer id:%d\n", arb->proposer_id);
                return -1;
            }
            expected_size += (accept_req_batch_size_calc(arb));
        }
        break;
        case accept_acks: {
            struct accept_ack_batch_t *aab = (accept_ack_batch *)m->data;
            if(aab->acceptor_id < 0 || aab->acceptor_id >= 3) {
                printf("Invalida acceptor id:%d\n", aab->acceptor_id);
                return -1;
            }
            expected_size += (accept_ack_batch_size_calc(aab));
        }
        break;
        case repeat_reqs: {
            struct repeat_req_batch_t *rrb = (repeat_req_batch *)m->data;
            expected_size += (sizeof(repeat_req_batch) + (sizeof(iid_t) * rrb->count));
        }
        break;
        case submit: {
            expected_size += m->data_size;
        }
        break;
        case alive_ping: {
            expected_size += sizeof(alive_ping_msg);
        }
        break;
        case leader_announce: {
            expected_size += sizeof(leader_announce_msg);
        }
        break;
        default: {
            printf("Unknow paxos message type:%d\n", m->type);
            return -1;
        }
        if(msg_size != expected_size) {
            printf("Invalid size for msg_type:%d declared:%u received:%u\n", m->type, (unsigned int)expected_size, (unsigned int)msg_size);
        }
    }
    return 0;
}

variable_types: struct prepare_req_batch_t { short count; short proposer_id; struct prepare_req_t { unsigned int iid; unsigned int ballot; } prepares[]; } *

variable_types: struct repeat_req_batch_t { short count; unsigned int requests[]; } *

user_defined_types: struct prepare_req_batch_t { short count; short proposer_id; struct prepare_req_t prepares[]; }

user_defined_types: struct prepare_ack_batch_t { short acceptor_id; short count; char data[]; }

user_defined_types: struct accept_req_batch_t { short count; short proposer_id; char data[]; }

user_defined_types: enum pax_msg_code_e { prepare_reqs=1, prepare_acks=2, accept_reqs=4, accept_acks=8, repeat_reqs=16, submit=32, leader_announce=64, alive_ping=65 }