decompiled_code:
__int64 __fastcall trace_process_exists(__pid_t pid)
{
int *v1; // rax
int v2; // er12
int *v3; // rbx
__int64 result; // rax

v1 = _errno_location();
v2 = *v1;
v3 = v1;
if ( !kill(pid, 0) )
return 1LL;
result = (unsigned int)*v3;
switch ( (_DWORD)result )
{
case 1:
*v3 = v2;
break;
case 3:
*v3 = v2;
result = 0LL;
break;
case 0x16:
_assert_fail(
"(22 != (*__errno_location ())) || TRACE_LOG_ASSERTION_FAILURE(22 != (*__errno_location ()))",
"trace_proc_util.c",
0xCBu,
"trace_process_exists");
default:
result = 0LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__pid_t pid)
{
int *v1; // rax
int v2; // er12
int *v3; // rbx
__int64 result; // rax

v1 = _errno_location();
v2 = *v1;
v3 = v1;
if ( !kill(pid, 0) )
return 1LL;
result = (unsigned int)*v3;
switch ( (_DWORD)result )
{
case 1:
*v3 = v2;
break;
case 3:
*v3 = v2;
result = 0LL;
break;
case 0x16:
_assert_fail(
"(22 != (*__errno_location ())) || TRACE_LOG_ASSERTION_FAILURE(22 != (*__errno_location ()))",
"trace_proc_util.c",
0xCBu,
"trace_process_exists");
default:
result = 0LL;
break;
}
return result;
}

original_code:
bool_t trace_process_exists(pid_t pid) {
    const int saved_errno = (*__errno_location ());
    if (0 == kill(pid, 0)) {
        return (1);
    }
    switch ((*__errno_location ())) {
    case 1:
        (*__errno_location ()) = saved_errno;
        return (1);
    case 3:
        (*__errno_location ()) = saved_errno;
        break;
    default:
        ((void) sizeof (((22 != (*__errno_location ())) || (0)) ? 1 : 0), __extension__ ({ if ((22 != (*__errno_location ())) || (0)) ; else __assert_fail ("(22 != (*__errno_location ())) || TRACE_LOG_ASSERTION_FAILURE(22 != (*__errno_location ()))", "trace_proc_util.c", 203, __extension__ __PRETTY_FUNCTION__); }));
        break;
    }
    return (0);
}

canonical_original_code:
int trace_process_exists(int pid) {
    int saved_errno = (*__errno_location ());
    if (0 == kill(pid, 0)) {
        return (1);
    }
    switch ((*__errno_location ())) {
    case 1:
        (*__errno_location ()) = saved_errno;
        return (1);
    case 3:
        (*__errno_location ()) = saved_errno;
        break;
    default:
        ((void) sizeof (((22 != (*__errno_location ())) || (0)) ? 1 : 0), __extension__ ({ if ((22 != (*__errno_location ())) || (0)) ; else __assert_fail ("(22 != (*__errno_location ())) || TRACE_LOG_ASSERTION_FAILURE(22 != (*__errno_location ()))", "trace_proc_util.c", 203, __extension__ __PRETTY_FUNCTION__); }));
        break;
    }
    return (0);
}


repo: Infinidat/infinitrace

decompiled_code:
int __fastcall trace_get_process_time(unsigned int a1, _QWORD *a2)
{
int result; // eax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

if ( a2 )
{
result = stat_pid(a1, &v3);
if ( result )
{
set_esrch_if_necessary();
result = -1;
}
else
{
*a2 = v3.st_ctim.tv_nsec + 1000000000 * v3.st_ctim.tv_sec;
}
}
else
{
*_errno_location() = 14;
result = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(unsigned int a1, _QWORD *a2)
{
int result; // eax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

if ( a2 )
{
result = func2(a1, &v3);
if ( result )
{
func0();
result = -1;
}
else
{
*a2 = v3.st_ctim.tv_nsec + 1000000000 * v3.st_ctim.tv_sec;
}
}
else
{
*_errno_location() = 14;
result = -1;
}
return result;
}

original_code:
int trace_get_process_time(pid_t pid, trace_ts_t *curtime)
{
    if (((void *)0) == curtime) {
        (*__errno_location ()) = 14;
        return -1;
    }
    struct stat stat_buf;
    int rc = stat_pid(pid, &stat_buf);
    if (0 != rc) {
        set_esrch_if_necessary();
        return -1;
    }
    *curtime = stat_buf.st_ctim.tv_sec * (((1000LL) * 1000) * 1000) + stat_buf.st_ctim.tv_nsec;
    return 0;
}

canonical_original_code:
int trace_get_process_time(int pid, unsigned long long *curtime)
{
    if (((void *)0) == curtime) {
        (*__errno_location ()) = 14;
        return -1;
    }
    struct stat stat_buf;
    int rc = stat_pid(pid, &stat_buf);
    if (0 != rc) {
        set_esrch_if_necessary();
        return -1;
    }
    *curtime = stat_buf.st_ctim.tv_sec * (((1000LL) * 1000) * 1000) + stat_buf.st_ctim.tv_nsec;
    return 0;
}

variable_types: pid int

variable_types:
curtime
unsigned long long *

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

variable_types: rc int

return_type: int

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


decompiled_code:
__syscall_slong_t __fastcall trace_get_nsec_from_clk(clockid_t a1)
{
int v1; // er8
__syscall_slong_t result; // rax
struct timespec v3; // [rsp+0h] [rbp-18h] BYREF

v1 = clock_gettime(a1, &v3);
result = -1LL;
if ( !v1 )
result = v3.tv_nsec + 1000000000 * v3.tv_sec;
return result;
}

canonical_decompiled_code:
__syscall_slong_t __fastcall func0(clockid_t a1)
{
int v1; // er8
__syscall_slong_t result; // rax
struct timespec v3; // [rsp+0h] [rbp-18h] BYREF

v1 = clock_gettime(a1, &v3);
result = -1LL;
if ( !v1 )
result = v3.tv_nsec + 1000000000 * v3.tv_sec;
return result;
}

original_code:
static trace_ts_t trace_get_nsec_from_clk(clockid_t clk_id)
{
 struct timespec now;
 int rc = clock_gettime(clk_id, &now);
 if (0 != rc) {
  return ((trace_ts_t) -1);
 }
 return now.tv_nsec + (((1000LL) * 1000) * 1000) * now.tv_sec;
}

canonical_original_code:
static unsigned long long trace_get_nsec_from_clk(int clk_id)
{
 struct timespec now;
 int rc = clock_gettime(clk_id, &now);
 if (0 != rc) {
  return ((trace_ts_t) -1);
 }
 return now.tv_nsec + (((1000LL) * 1000) * 1000) * now.tv_sec;
}

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

return_type: unsigned long long


decompiled_code:
__syscall_slong_t trace_get_nsec()
{
return trace_get_nsec_from_clk(0);
}

canonical_decompiled_code:
__syscall_slong_t func1()
{
return func0(0);
}

original_code:
trace_ts_t trace_get_nsec(void)
{
     return trace_get_nsec_from_clk(0);
}

canonical_original_code:
unsigned long long trace_get_nsec()
{
     return trace_get_nsec_from_clk(0);
}


decompiled_code:
__syscall_slong_t trace_get_nsec_monotonic()
{
return trace_get_nsec_from_clk(1);
}

canonical_decompiled_code:
__syscall_slong_t func2()
{
return func0(1);
}

original_code:
trace_ts_t trace_get_nsec_monotonic(void)
{
 return trace_get_nsec_from_clk(1);
}

canonical_original_code:
unsigned long long trace_get_nsec_monotonic()
{
 return trace_get_nsec_from_clk(1);
}


decompiled_code:
__suseconds_t trace_get_walltime_ns()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( gettimeofday(&v1, 0LL) )
_assert_fail("0 == gettimeofday(&tv, NULL)", "trace_clock.c", 0x29u, "trace_get_walltime_ns");
return 1000 * (v1.tv_usec + 1000000 * v1.tv_sec);
}

canonical_decompiled_code:
__suseconds_t func3()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( gettimeofday(&v1, 0LL) )
_assert_fail("0 == gettimeofday(&tv, NULL)", "trace_clock.c", 0x29u, "trace_get_walltime_ns");
return 1000 * (v1.tv_usec + 1000000 * v1.tv_sec);
}

original_code:
trace_ts_t trace_get_walltime_ns(void)
{
    struct timeval tv;
    ((void) sizeof ((0 == gettimeofday(&tv, ((void *)0))) ? 1 : 0), __extension__ ({ if (0 == gettimeofday(&tv, ((void *)0))) ; else __assert_fail ("0 == gettimeofday(&tv, NULL)", "trace_clock.c", 41, __extension__ __PRETTY_FUNCTION__); }));
    return (1000LL) * ((((trace_ts_t)tv.tv_sec) * 1000000ULL) + tv.tv_usec);
}

canonical_original_code:
unsigned long long trace_get_walltime_ns()
{
    struct timeval tv;
    ((void) sizeof ((0 == gettimeofday(&tv, ((void *)0))) ? 1 : 0), __extension__ ({ if (0 == gettimeofday(&tv, ((void *)0))) ; else __assert_fail ("0 == gettimeofday(&tv, NULL)", "trace_clock.c", 41, __extension__ __PRETTY_FUNCTION__); }));
    return (1000LL) * ((((unsigned long long)tv.tv_sec) * 1000000ULL) + tv.tv_usec);
}

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


decompiled_code:
unsigned __int64 trace_get_walltime_ms()
{
return trace_get_walltime_ns() / 0xF4240uLL;
}

canonical_decompiled_code:
unsigned __int64 func4()
{
return func3() / 0xF4240uLL;
}

original_code:
unsigned long long trace_get_walltime_ms(void)
{
 return trace_get_walltime_ns() / ((1000LL) * 1000);
}

canonical_original_code:
unsigned long long trace_get_walltime_ms()
{
 return trace_get_walltime_ns() / ((1000LL) * 1000);
}


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

if ( a1 )
{
result = 1000000000 * (a2 / 0x3B9ACA00);
*a1 = a2 / 0x3B9ACA00;
a1[1] = a2 % 0x3B9ACA00;
}
return result;
}

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

if ( a1 )
{
result = 1000000000 * (a2 / 0x3B9ACA00);
*a1 = a2 / 0x3B9ACA00;
a1[1] = a2 % 0x3B9ACA00;
}
return result;
}

original_code:
void trace_init_timespec(struct timespec *ts, trace_ts_t t)
{
    if (((void *)0) != ts) {
        ts->tv_nsec = t % (((1000LL) * 1000) * 1000);
        ts->tv_sec = t / (((1000LL) * 1000) * 1000);
    }
 }

canonical_original_code:
void trace_init_timespec(struct timespec *ts, unsigned long long t)
{
    if (((void *)0) != ts) {
        ts->tv_nsec = t % (((1000LL) * 1000) * 1000);
        ts->tv_sec = t / (((1000LL) * 1000) * 1000);
    }
 }

variable_types: struct timespec { long tv_sec; long tv_nsec; } *

variable_types:
t
unsigned long long

return_type: void


decompiled_code:
__int64 __fastcall run_executable(char *file, void *src)
{
int v2; // eax
unsigned int v3; // er12
unsigned int v4; // er13
unsigned int v5; // er14
__pid_t v7; // eax
size_t v8; // rax
void *v9; // rsp
size_t v10; // rax
void *v11; // rsp
void *v12; // rax
unsigned int v13; // er14
char *v14; // rax
char *v15; // rax
unsigned int v16; // ebx
char *v17; // rax
int *v18; // rax
unsigned int v19; // ebx
int *v20; // r12
char *v21; // r15
unsigned int v22; // eax
char *v23; // r13
char *v24; // rax
__int64 v25; // r8
int *v26; // r12
char *v27; // rax
int *v28; // rax
__int64 v29; // rcx
__int64 v30; // r8
char *v31; // rax
int *v32; // rbx
char *v33; // rax
__int64 v34; // rcx
__int64 v35; // r8
int v36; // eax
int stat_loc[2]; // [rsp+0h] [rbp-50h] BYREF
void *v38; // [rsp+8h] [rbp-48h]
__int64 v39; // [rsp+10h] [rbp-40h]

if ( access(file, 1) < 0 )
{
v16 = *_errno_location();
v17 = strerror(v16);
WARN(
"Preliminary check before attempting to run the executable",
file,
"with the argument",
src,
"has failed with",
v16,
v17);
}
trace_get_nsec_monotonic();
v2 = trace_fork();
v3 = v2;
if ( v2 == -1 )
{
v13 = *_errno_location();
v14 = strerror(v13);
ERR("Failed to run", file, "at 1st fork due to", v13, v14);
v15 = strerror(v13);
syslog(11, "Failed to run %s at 1st fork due to error %d (%s)", file, v13, v15);
return v3;
}
if ( !v2 )
{
v7 = fork();
if ( v7 != -1 )
{
if ( !v7 )
{
v8 = strlen(file);
v9 = alloca(v8 + 9);
*(_QWORD *)stat_loc = memcpy(stat_loc, file, v8 + 1);
v10 = strlen((const char *)src);
v11 = alloca(v10 + 9);
v12 = memcpy(stat_loc, src, v10 + 1);
v39 = 0LL;
v38 = v12;
if ( execvp(file, (char *const *)stat_loc) != -1 )
_assert_fail(
"(-1 == execvp(executable, argv)) || TRACE_LOG_ASSERTION_FAILURE(-1 == execvp(executable, argv))",
"trace_dumper/events.c",
0x4Au,
"run_executable");
v18 = _errno_location();
v19 = *v18;
v20 = v18;
v21 = strerror(*v18);
v22 = getpid();
syslog(11, "Failed to exec %s in pid %d due to error %d (%s)", file, v22, v19, v21);
if ( (int)trace_init(0LL) < 0 )
{
v23 = strerror(v19);
v24 = strerror(*v20);
syslog(
11,
"Failed to initialize traces due to errno=%d (%s) after failing execvp() with errno=%d (%s)",
(unsigned int)*v20,
v24,
v19,
v23);
}
else
{
strerror(v19);
ERR("Failed to exec", file, "with", src, "due to");
}
exit(71);
}
DEBUG("Intermediate child completed successfully");
exit(0);
}
v32 = _errno_location();
v33 = strerror(*v32);
ERR("Attempt to fork second child failed with err=", (unsigned int)*v32, v33, v34, v35);
v36 = trace_capped_errno();
exit(v36);
}
if ( v2 <= 0 )
_assert_fail("(pid > 0) || TRACE_LOG_ASSERTION_FAILURE(pid > 0)", "trace_dumper/events.c", 0x61u, "run_executable");
DEBUG("Forked intermediate child with");
stat_loc[0] = -1;
v4 = waitpid(v3, stat_loc, 0);
if ( v4 != v3 )
{
v28 = _errno_location();
ERR("waitpid failed for 1st child", v3, (unsigned int)*v28, v29, v30);
return (unsigned int)-1;
}
v5 = stat_loc[0];
if ( stat_loc[0] )
{
v26 = _errno_location();
if ( (v5 & 0x7F) != 0 )
{
if ( (char)((v5 & 0x7F) + 1) <= 1 )
{
ERR("1st child", v4, "exited inexplicably returning", v5, v25);
*v26 = 71;
}
else
{
v27 = strsignal(v5 & 0x7F);
ERR("1st child", v4, "returned exited with signal", v5 & 0x7F, v27);
*v26 = 4;
}
}
else
{
v31 = strerror(BYTE1(v5));
ERR("1st child", v4, "returned error status", BYTE1(v5), v31);
*v26 = BYTE1(v5);
}
return (unsigned int)-1;
}
trace_get_nsec_monotonic();
v3 = 0;
DEBUG("waitpid for 1st child");
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *file, void *src)
{
int v2; // eax
unsigned int v3; // er12
unsigned int v4; // er13
unsigned int v5; // er14
__pid_t v7; // eax
size_t v8; // rax
void *v9; // rsp
size_t v10; // rax
void *v11; // rsp
void *v12; // rax
unsigned int v13; // er14
char *v14; // rax
char *v15; // rax
unsigned int v16; // ebx
char *v17; // rax
int *v18; // rax
unsigned int v19; // ebx
int *v20; // r12
char *v21; // r15
unsigned int v22; // eax
char *v23; // r13
char *v24; // rax
__int64 v25; // r8
int *v26; // r12
char *v27; // rax
int *v28; // rax
__int64 v29; // rcx
__int64 v30; // r8
char *v31; // rax
int *v32; // rbx
char *v33; // rax
__int64 v34; // rcx
__int64 v35; // r8
int v36; // eax
int stat_loc[2]; // [rsp+0h] [rbp-50h] BYREF
void *v38; // [rsp+8h] [rbp-48h]
__int64 v39; // [rsp+10h] [rbp-40h]

if ( access(file, 1) < 0 )
{
v16 = *_errno_location();
v17 = strerror(v16);
WARN(
"Preliminary check before attempting to run the executable",
file,
"with the argument",
src,
"has failed with",
v16,
v17);
}
trace_get_nsec_monotonic();
v2 = trace_fork();
v3 = v2;
if ( v2 == -1 )
{
v13 = *_errno_location();
v14 = strerror(v13);
ERR("Failed to run", file, "at 1st fork due to", v13, v14);
v15 = strerror(v13);
syslog(11, "Failed to run %s at 1st fork due to error %d (%s)", file, v13, v15);
return v3;
}
if ( !v2 )
{
v7 = fork();
if ( v7 != -1 )
{
if ( !v7 )
{
v8 = strlen(file);
v9 = alloca(v8 + 9);
*(_QWORD *)stat_loc = memcpy(stat_loc, file, v8 + 1);
v10 = strlen((const char *)src);
v11 = alloca(v10 + 9);
v12 = memcpy(stat_loc, src, v10 + 1);
v39 = 0LL;
v38 = v12;
if ( execvp(file, (char *const *)stat_loc) != -1 )
_assert_fail(
"(-1 == execvp(executable, argv)) || TRACE_LOG_ASSERTION_FAILURE(-1 == execvp(executable, argv))",
"trace_dumper/events.c",
0x4Au,
"run_executable");
v18 = _errno_location();
v19 = *v18;
v20 = v18;
v21 = strerror(*v18);
v22 = getpid();
syslog(11, "Failed to exec %s in pid %d due to error %d (%s)", file, v22, v19, v21);
if ( (int)trace_init(0LL) < 0 )
{
v23 = strerror(v19);
v24 = strerror(*v20);
syslog(
11,
"Failed to initialize traces due to errno=%d (%s) after failing execvp() with errno=%d (%s)",
(unsigned int)*v20,
v24,
v19,
v23);
}
else
{
strerror(v19);
ERR("Failed to exec", file, "with", src, "due to");
}
exit(71);
}
DEBUG("Intermediate child completed successfully");
exit(0);
}
v32 = _errno_location();
v33 = strerror(*v32);
ERR("Attempt to fork second child failed with err=", (unsigned int)*v32, v33, v34, v35);
v36 = func0();
exit(v36);
}
if ( v2 <= 0 )
_assert_fail("(pid > 0) || TRACE_LOG_ASSERTION_FAILURE(pid > 0)", "trace_dumper/events.c", 0x61u, "run_executable");
DEBUG("Forked intermediate child with");
stat_loc[0] = -1;
v4 = waitpid(v3, stat_loc, 0);
if ( v4 != v3 )
{
v28 = _errno_location();
ERR("waitpid failed for 1st child", v3, (unsigned int)*v28, v29, v30);
return (unsigned int)-1;
}
v5 = stat_loc[0];
if ( stat_loc[0] )
{
v26 = _errno_location();
if ( (v5 & 0x7F) != 0 )
{
if ( (char)((v5 & 0x7F) + 1) <= 1 )
{
ERR("1st child", v4, "exited inexplicably returning", v5, v25);
*v26 = 71;
}
else
{
v27 = strsignal(v5 & 0x7F);
ERR("1st child", v4, "returned exited with signal", v5 & 0x7F, v27);
*v26 = 4;
}
}
else
{
v31 = strerror(BYTE1(v5));
ERR("1st child", v4, "returned error status", BYTE1(v5), v31);
*v26 = BYTE1(v5);
}
return (unsigned int)-1;
}
trace_get_nsec_monotonic();
v3 = 0;
DEBUG("waitpid for 1st child");
return v3;
}

original_code:
static int run_executable(const char *executable, const char *arg)
{
    if (access(executable, 1) < 0) {
        const int err = (*__errno_location ());
        WARN("Preliminary check before attempting to run the executable", executable, "with the argument", arg, "has failed with", err, strerror(err));
    }
    const trace_ts_t start = trace_get_nsec_monotonic();
    const pid_t pid = trace_fork();
    switch (pid) {
    case -1: {
        const int err = (*__errno_location ());
        ERR("Failed to run", executable, "at 1st fork due to", err, strerror(err));
        syslog((1<<3)|3, "Failed to run %s at 1st fork due to error %d (%s)", executable, err, strerror(err));
        return -1;
    }
    case 0:
        switch (fork()) {
        case -1:
            ERR("Attempt to fork second child failed with err=", (*__errno_location ()), strerror((*__errno_location ())));
            _exit(trace_capped_errno());
            break;
        case 0: {
            char *const argv[] = { (__extension__ ({ const char *__old = (executable); size_t __len = strlen (__old) + 1; char *__new = (char *) __builtin_alloca (__len); (char *) memcpy (__new, __old, __len); })), (__extension__ ({ const char *__old = (arg); size_t __len = strlen (__old) + 1; char *__new = (char *) __builtin_alloca (__len); (char *) memcpy (__new, __old, __len); })), ((void *)0) };
            ((void) sizeof (((-1 == execvp(executable, argv)) || (0)) ? 1 : 0), __extension__ ({ if ((-1 == execvp(executable, argv)) || (0)) ; else __assert_fail ("(-1 == execvp(executable, argv)) || TRACE_LOG_ASSERTION_FAILURE(-1 == execvp(executable, argv))", "trace_dumper/events.c", 74, __extension__ __PRETTY_FUNCTION__); }));
            const int err = (*__errno_location ());
            syslog((1<<3)|3, "Failed to exec %s in pid %d due to error %d (%s)", executable, (int) getpid(), err, strerror(err));
            if (trace_init(((void *)0)) >= 0) {
                ERR("Failed to exec", executable, "with", arg, "due to", err, strerror(err));
            }
            else {
                syslog((1<<3)|3, "Failed to initialize traces due to errno=%d (%s) after failing execvp() with errno=%d (%s)",
                        (*__errno_location ()), strerror((*__errno_location ())), err, strerror(err));
            }
            _exit(71);
            break;
        }
        default:
            DEBUG("Intermediate child completed successfully");
            break;
        }
        _exit(0);
        break;
    default: {
        ((void) sizeof (((pid > 0) || (0)) ? 1 : 0), __extension__ ({ if ((pid > 0) || (0)) ; else __assert_fail ("(pid > 0) || TRACE_LOG_ASSERTION_FAILURE(pid > 0)", "trace_dumper/events.c", 97, __extension__ __PRETTY_FUNCTION__); }));
        DEBUG("Forked intermediate child with", pid);
        int status = -1;
        if (waitpid(pid, &status, 0) != pid) {
            ERR("waitpid failed for 1st child", pid, (*__errno_location ()));
            return -1;
        }
        if (0 != status) {
            if ((((status) & 0x7f) == 0)) {
                const int err = (((status) & 0xff00) >> 8);
                ERR("1st child", pid, "returned error status", err, strerror(err));
                (*__errno_location ()) = err;
            }
            else if ((((signed char) (((status) & 0x7f) + 1) >> 1) > 0)) {
                const int sig = ((status) & 0x7f);
                ERR("1st child", pid, "returned exited with signal", sig, strsignal(sig));
                (*__errno_location ()) = 4;
            }
            else {
                ERR("1st child", pid, "exited inexplicably returning", status);
                (*__errno_location ()) = 71;
            }
            return -1;
        }
        const trace_ts_t total_duration = trace_get_nsec_monotonic() - start;
        DEBUG("waitpid for 1st child", pid, "returned successfully.", total_duration);
        break;
    }
    }
    return 0;
}

canonical_original_code:
static int run_executable(char *executable, char *arg)
{
    if (access(executable, 1) < 0) {
        int err = (*__errno_location ());
        WARN("Preliminary check before attempting to run the executable", executable, "with the argument", arg, "has failed with", err, strerror(err));
    }
    unsigned long long start = trace_get_nsec_monotonic();
    int pid = trace_fork();
    switch (pid) {
    case -1: {
        int err = (*__errno_location ());
        ERR("Failed to run", executable, "at 1st fork due to", err, strerror(err));
        syslog((1<<3)|3, "Failed to run %s at 1st fork due to error %d (%s)", executable, err, strerror(err));
        return -1;
    }
    case 0:
        switch (fork()) {
        case -1:
            ERR("Attempt to fork second child failed with err=", (*__errno_location ()), strerror((*__errno_location ())));
            _exit(trace_capped_errno());
            break;
        case 0: {
            char *argv[] = { (__extension__ ({ const char *__old = (executable); size_t __len = strlen (__old) + 1; char *__new = (char *) __builtin_alloca (__len); (char *) memcpy (__new, __old, __len); })), (__extension__ ({ const char *__old = (arg); size_t __len = strlen (__old) + 1; char *__new = (char *) __builtin_alloca (__len); (char *) memcpy (__new, __old, __len); })), ((void *)0) };
            ((void) sizeof (((-1 == execvp(executable, argv)) || (0)) ? 1 : 0), __extension__ ({ if ((-1 == execvp(executable, argv)) || (0)) ; else __assert_fail ("(-1 == execvp(executable, argv)) || TRACE_LOG_ASSERTION_FAILURE(-1 == execvp(executable, argv))", "trace_dumper/events.c", 74, __extension__ __PRETTY_FUNCTION__); }));
            int err = (*__errno_location ());
            syslog((1<<3)|3, "Failed to exec %s in pid %d due to error %d (%s)", executable, (int) getpid(), err, strerror(err));
            if (trace_init(((void *)0)) >= 0) {
                ERR("Failed to exec", executable, "with", arg, "due to", err, strerror(err));
            }
            else {
                syslog((1<<3)|3, "Failed to initialize traces due to errno=%d (%s) after failing execvp() with errno=%d (%s)",
                        (*__errno_location ()), strerror((*__errno_location ())), err, strerror(err));
            }
            _exit(71);
            break;
        }
        default:
            DEBUG("Intermediate child completed successfully");
            break;
        }
        _exit(0);
        break;
    default: {
        ((void) sizeof (((pid > 0) || (0)) ? 1 : 0), __extension__ ({ if ((pid > 0) || (0)) ; else __assert_fail ("(pid > 0) || TRACE_LOG_ASSERTION_FAILURE(pid > 0)", "trace_dumper/events.c", 97, __extension__ __PRETTY_FUNCTION__); }));
        DEBUG("Forked intermediate child with", pid);
        int status = -1;
        if (waitpid(pid, &status, 0) != pid) {
            ERR("waitpid failed for 1st child", pid, (*__errno_location ()));
            return -1;
        }
        if (0 != status) {
            if ((((status) & 0x7f) == 0)) {
                int err = (((status) & 0xff00) >> 8);
                ERR("1st child", pid, "returned error status", err, strerror(err));
                (*__errno_location ()) = err;
            }
            else if ((((char) (((status) & 0x7f) + 1) >> 1) > 0)) {
                int sig = ((status) & 0x7f);
                ERR("1st child", pid, "returned exited with signal", sig, strsignal(sig));
                (*__errno_location ()) = 4;
            }
            else {
                ERR("1st child", pid, "exited inexplicably returning", status);
                (*__errno_location ()) = 71;
            }
            return -1;
        }
        unsigned long long total_duration = trace_get_nsec_monotonic() - start;
        DEBUG("waitpid for 1st child", pid, "returned successfully.", total_duration);
        break;
    }
    }
    return 0;
}

variable_types:
executable
char *

variable_types:
arg
char *

variable_types: err int

variable_types:
start
unsigned long long

variable_types:
argv
char *[]

variable_types: status int

variable_types: sig int


decompiled_code:
unsigned __int64 __fastcall internal_buf_bytes_to_recs(__int64 a1)
{
return (unsigned __int64)(a1 + 63) >> 6;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1)
{
return (unsigned __int64)(a1 + 63) >> 6;
}

original_code:
static inline size_t internal_buf_bytes_to_recs(size_t n_bytes)
{
    return (n_bytes + 64U - 1) / 64U;
}

canonical_original_code:
static inline unsigned long internal_buf_bytes_to_recs(unsigned long n_bytes)
{
    return (n_bytes + 64U - 1) / 64U;
}

return_type: unsigned long


decompiled_code:
__int64 __fastcall effective_capacity_t(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // edx

v1 = *(_DWORD *)(a1 + 16);
v2 = *(_DWORD *)(a1 + 20);
if ( v1 <= v2 )
_assert_fail(
"(buf->n_recs > buf->n_slack_recs.t) || TRACE_LOG_ASSERTION_FAILURE(buf->n_recs > buf->n_slack_recs.t)",
"trace_dumper/internal_buffer.c",
0xBEu,
"effective_capacity_t");
return v1 - v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // edx

v1 = *(_DWORD *)(a1 + 16);
v2 = *(_DWORD *)(a1 + 20);
if ( v1 <= v2 )
_assert_fail(
"(buf->n_recs > buf->n_slack_recs.t) || TRACE_LOG_ASSERTION_FAILURE(buf->n_recs > buf->n_slack_recs.t)",
"trace_dumper/internal_buffer.c",
0xBEu,
"effective_capacity_t");
return v1 - v2;
}

original_code:
static unsigned effective_capacity_t(const struct trace_internal_buf *buf)
{
    ((void) sizeof (((buf->n_recs > buf->n_slack_recs.t) || (0)) ? 1 : 0), __extension__ ({ if ((buf->n_recs > buf->n_slack_recs.t) || (0)) ; else __assert_fail ("(buf->n_recs > buf->n_slack_recs.t) || TRACE_LOG_ASSERTION_FAILURE(buf->n_recs > buf->n_slack_recs.t)", "trace_dumper/internal_buffer.c", 190, __extension__ __PRETTY_FUNCTION__); }));
    return buf->n_recs - buf->n_slack_recs.t;
}

canonical_original_code:
static unsigned int effective_capacity_t(struct trace_internal_buf *buf)
{
    ((void) sizeof (((buf->n_recs > buf->n_slack_recs.t) || (0)) ? 1 : 0), __extension__ ({ if ((buf->n_recs > buf->n_slack_recs.t) || (0)) ; else __assert_fail ("(buf->n_recs > buf->n_slack_recs.t) || TRACE_LOG_ASSERTION_FAILURE(buf->n_recs > buf->n_slack_recs.t)", "trace_dumper/internal_buffer.c", 190, __extension__ __PRETTY_FUNCTION__); }));
    return buf->n_recs - buf->n_slack_recs.t;
}

variable_types: struct trace_internal_buf { struct trace_internal_buf_index { unsigned int t; unsigned int c; } write_idx; struct trace_internal_buf_index read_idx; unsigned int n_recs; struct trace_internal_buf_index n_slack_recs; struct trace_record { unsigned long long ts; unsigned short pid; unsigned short tid; short nesting; short bit_fields[]; unsigned int termination; unsigned int module_id; unsigned int reserved; unsigned int severity; unsigned int rec_type; unsigned int generation; union trace_record_u { unsigned char payload[44]; struct trace_record_typed { unsigned int log_id; unsigned char payload[]; } typed; struct trace_record_file_header { char machine_id[24]; unsigned short format_version; unsigned short flags; unsigned char reserved[4]; unsigned int magic; } file_header; struct trace_record_metadata { unsigned int metadata_size_bytes; unsigned short dead_pids[4]; unsigned char reserved[-1]; unsigned int metadata_magic; } metadata; struct trace_record_dump_header { unsigned int prev_dump_offset; unsigned int total_dump_size; unsigned int first_chunk_offset; unsigned long records_previously_discarded; } dump_header; struct trace_record_buffer_dump { unsigned int last_metadata_offset; unsigned int prev_chunk_offset; unsigned int dump_header_offset; unsigned long long ts; unsigned int records; unsigned int severity_type; unsigned int lost_records; unsigned short flags; unsigned char slack_bytes; } buffer_chunk; struct trace_record_data_loss { unsigned int lost_records; unsigned long long ts_start; unsigned long long ts_end; } data_loss; } u; } records[]; } *

return_type: unsigned int

user_defined_types: struct trace_internal_buf { struct trace_internal_buf_index write_idx; struct trace_internal_buf_index read_idx; unsigned int n_recs; struct trace_internal_buf_index n_slack_recs; struct trace_record records[]; }

user_defined_types: struct trace_record { unsigned long long ts; unsigned short pid; unsigned short tid; short nesting; short bit_fields[]; unsigned int termination; unsigned int module_id; unsigned int reserved; unsigned int severity; unsigned int rec_type; unsigned int generation; union trace_record_u u; }

user_defined_types: union trace_record_u { unsigned char payload[44]; struct trace_record_typed typed; struct trace_record_file_header file_header; struct trace_record_metadata metadata; struct trace_record_dump_header dump_header; struct trace_record_buffer_dump buffer_chunk; struct trace_record_data_loss data_loss; }


decompiled_code:
void *__fastcall fill_compression_remainder_with_pattern(unsigned int *a1, __int64 a2)
{
__int64 v4; // rsi
unsigned __int64 v5; // rax
__int64 v6; // rdx
__int64 v7; // rsi

v4 = *a1;
v5 = internal_buf_bytes_to_recs(a2) << 6;
v7 = v4 << 6;
if ( v5 - a2 > 0x3F )
_assert_fail(
"(fill_len < 64U) || TRACE_LOG_ASSERTION_FAILURE(fill_len < 64U)",
"trace_dumper/internal_buffer.c",
0xDAu,
"fill_compression_remainder_with_pattern");
return memset((void *)(v6 + a2 + v7 - v5), 105, v5 - a2);
}

canonical_decompiled_code:
void *__fastcall func2(unsigned int *a1, __int64 a2)
{
__int64 v4; // rsi
unsigned __int64 v5; // rax
__int64 v6; // rdx
__int64 v7; // rsi

v4 = *a1;
v5 = func0(a2) << 6;
v7 = v4 << 6;
if ( v5 - a2 > 0x3F )
_assert_fail(
"(fill_len < 64U) || TRACE_LOG_ASSERTION_FAILURE(fill_len < 64U)",
"trace_dumper/internal_buffer.c",
0xDAu,
"fill_compression_remainder_with_pattern");
return memset((void *)(v6 + a2 + v7 - v5), 105, v5 - a2);
}

original_code:
static void fill_compression_remainder_with_pattern(struct trace_internal_buf *buf, size_t compressed_len)
{
    char *const end_p = (char *)(buf->records + buf->write_idx.t);
    const size_t fill_len = 64U * internal_buf_bytes_to_recs(compressed_len) - compressed_len;
    ((void) sizeof (((fill_len < 64U) || (0)) ? 1 : 0), __extension__ ({ if ((fill_len < 64U) || (0)) ; else __assert_fail ("(fill_len < 64U) || TRACE_LOG_ASSERTION_FAILURE(fill_len < 64U)", "trace_dumper/internal_buffer.c", 218, __extension__ __PRETTY_FUNCTION__); }));
    memset(end_p - fill_len, 0x69, fill_len);
}

canonical_original_code:
static void fill_compression_remainder_with_pattern(struct trace_internal_buf *buf, unsigned long compressed_len)
{
    char *end_p = (char *)(buf->records + buf->write_idx.t);
    unsigned long fill_len = 64U * internal_buf_bytes_to_recs(compressed_len) - compressed_len;
    ((void) sizeof (((fill_len < 64U) || (0)) ? 1 : 0), __extension__ ({ if ((fill_len < 64U) || (0)) ; else __assert_fail ("(fill_len < 64U) || TRACE_LOG_ASSERTION_FAILURE(fill_len < 64U)", "trace_dumper/internal_buffer.c", 218, __extension__ __PRETTY_FUNCTION__); }));
    memset(end_p - fill_len, 0x69, fill_len);
}

user_defined_types: struct trace_internal_buf_index { unsigned int t; unsigned int c; }

user_defined_types: struct trace_record_typed { unsigned int log_id; unsigned char payload[]; }

user_defined_types: struct trace_record_file_header { char machine_id[24]; unsigned short format_version; unsigned short flags; unsigned char reserved[4]; unsigned int magic; }

user_defined_types: struct trace_record_metadata { unsigned int metadata_size_bytes; unsigned short dead_pids[4]; unsigned char reserved[-1]; unsigned int metadata_magic; }

user_defined_types: struct trace_record_dump_header { unsigned int prev_dump_offset; unsigned int total_dump_size; unsigned int first_chunk_offset; unsigned long records_previously_discarded; }

user_defined_types: struct trace_record_buffer_dump { unsigned int last_metadata_offset; unsigned int prev_chunk_offset; unsigned int dump_header_offset; unsigned long long ts; unsigned int records; unsigned int severity_type; unsigned int lost_records; unsigned short flags; unsigned char slack_bytes; }

user_defined_types: struct trace_record_data_loss { unsigned int lost_records; unsigned long long ts_start; unsigned long long ts_end; }


decompiled_code:
__int64 __fastcall effective_capacity_c(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // edx

v1 = *(_DWORD *)(a1 + 16);
v2 = *(_DWORD *)(a1 + 24);
if ( v1 <= v2 )
_assert_fail(
"(buf->n_recs > buf->n_slack_recs.c) || TRACE_LOG_ASSERTION_FAILURE(buf->n_recs > buf->n_slack_recs.c)",
"trace_dumper/internal_buffer.c",
0xC4u,
"effective_capacity_c");
return v1 - v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // edx

v1 = *(_DWORD *)(a1 + 16);
v2 = *(_DWORD *)(a1 + 24);
if ( v1 <= v2 )
_assert_fail(
"(buf->n_recs > buf->n_slack_recs.c) || TRACE_LOG_ASSERTION_FAILURE(buf->n_recs > buf->n_slack_recs.c)",
"trace_dumper/internal_buffer.c",
0xC4u,
"effective_capacity_c");
return v1 - v2;
}

original_code:
static unsigned effective_capacity_c(const struct trace_internal_buf *buf)
{
    ((void) sizeof (((buf->n_recs > buf->n_slack_recs.c) || (0)) ? 1 : 0), __extension__ ({ if ((buf->n_recs > buf->n_slack_recs.c) || (0)) ; else __assert_fail ("(buf->n_recs > buf->n_slack_recs.c) || TRACE_LOG_ASSERTION_FAILURE(buf->n_recs > buf->n_slack_recs.c)", "trace_dumper/internal_buffer.c", 196, __extension__ __PRETTY_FUNCTION__); }));
    return buf->n_recs - buf->n_slack_recs.c;
}

canonical_original_code:
static unsigned int effective_capacity_c(struct trace_internal_buf *buf)
{
    ((void) sizeof (((buf->n_recs > buf->n_slack_recs.c) || (0)) ? 1 : 0), __extension__ ({ if ((buf->n_recs > buf->n_slack_recs.c) || (0)) ; else __assert_fail ("(buf->n_recs > buf->n_slack_recs.c) || TRACE_LOG_ASSERTION_FAILURE(buf->n_recs > buf->n_slack_recs.c)", "trace_dumper/internal_buffer.c", 196, __extension__ __PRETTY_FUNCTION__); }));
    return buf->n_recs - buf->n_slack_recs.c;
}


decompiled_code:
__int64 __fastcall internal_buf_recs_ahead_of_read_c(__int64 a1, unsigned __int64 a2)
{
int v3; // edx
int v4; // eax
unsigned __int64 v5; // rsi
__int64 result; // rax

v3 = effective_capacity_c(a1);
v4 = *(_DWORD *)(a1 + 12);
v5 = (unsigned int)(v3 - v4);
result = (unsigned int)(a2 + v4);
if ( v5 <= a2 )
result = (unsigned int)(result - v3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int64 a2)
{
int v3; // edx
int v4; // eax
unsigned __int64 v5; // rsi
__int64 result; // rax

v3 = func3(a1);
v4 = *(_DWORD *)(a1 + 12);
v5 = (unsigned int)(v3 - v4);
result = (unsigned int)(a2 + v4);
if ( v5 <= a2 )
result = (unsigned int)(result - v3);
return result;
}

original_code:
static unsigned internal_buf_recs_ahead_of_read_c(struct trace_internal_buf *buf, size_t n_recs)
{
    const unsigned read_till_slack = effective_capacity_c(buf) - buf->read_idx.c;
    if (n_recs < read_till_slack) {
        return buf->read_idx.c + n_recs;
    }
    return n_recs - read_till_slack;
}

canonical_original_code:
static unsigned int internal_buf_recs_ahead_of_read_c(struct trace_internal_buf *buf, unsigned long n_recs)
{
    unsigned int read_till_slack = effective_capacity_c(buf) - buf->read_idx.c;
    if (n_recs < read_till_slack) {
        return buf->read_idx.c + n_recs;
    }
    return n_recs - read_till_slack;
}


decompiled_code:
__int64 __fastcall compress_iov_to_buffer(__int64 a1, char **a2, int a3)
{
char *v4; // r15
size_t v5; // r14
int v6; // ebp
char *v7; // rax
__int64 result; // rax
int *v9; // rbx
char *v10; // rax
__int64 v11; // [rsp+18h] [rbp-60h] BYREF
char v12[88]; // [rsp+20h] [rbp-58h] BYREF

if ( !a2 )
{
*_errno_location() = 14;
return -1LL;
}
if ( a3 <= 0 )
return 0LL;
v4 = *a2;
v5 = (size_t)a2[1];
v11 = 0LL;
v6 = snappy_init_env(v12);
if ( v6 < 0 )
{
snappy_free_env(v12);
if ( v4 == *a2 )
goto LABEL_18;
goto LABEL_16;
}
if ( a3 == 1
|| (v5 = total_iovec_len(a2, (unsigned int)a3), v7 = (char *)malloc(v5), (v4 = v7) != 0LL)
&& copy_iov_to_buffer(v7, a2, (unsigned int)a3) == v5 )
{
if ( v5 )
{
v5 -= trace_r_count_chr_occurrences(v4 + 1, v5 - 1, 165LL);
v6 = snappy_compress(v12, v4, v5, a1, &v11);
}
}
else
{
v6 = -*_errno_location();
}
snappy_free_env(v12);
if ( *a2 != v4 )
LABEL_16:
free(v4);
if ( v6 )
{
LABEL_18:
v9 = _errno_location();
*v9 = -v6;
v10 = strerror(-v6);
ERR("Buffer compression failed with err", (unsigned int)*v9, v10);
return -1LL;
}
result = v11;
if ( v5 )
{
if ( v11 <= 0 )
_assert_fail(
"(((ssize_t) compressed_length > 0) || (0 == input_len)) || TRACE_LOG_ASSERTION_FAILURE(((ssize_t) compressed_len"
"gth > 0) || (0 == input_len))",
"trace_dumper/internal_buffer.c",
0xB8u,
"compress_iov_to_buffer");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, char **a2, int a3)
{
char *v4; // r15
size_t v5; // r14
int v6; // ebp
char *v7; // rax
__int64 result; // rax
int *v9; // rbx
char *v10; // rax
__int64 v11; // [rsp+18h] [rbp-60h] BYREF
char v12[88]; // [rsp+20h] [rbp-58h] BYREF

if ( !a2 )
{
*_errno_location() = 14;
return -1LL;
}
if ( a3 <= 0 )
return 0LL;
v4 = *a2;
v5 = (size_t)a2[1];
v11 = 0LL;
v6 = snappy_init_env(v12);
if ( v6 < 0 )
{
snappy_free_env(v12);
if ( v4 == *a2 )
goto LABEL_18;
goto LABEL_16;
}
if ( a3 == 1
|| (v5 = total_iovec_len(a2, (unsigned int)a3), v7 = (char *)malloc(v5), (v4 = v7) != 0LL)
&& copy_iov_to_buffer(v7, a2, (unsigned int)a3) == v5 )
{
if ( v5 )
{
v5 -= trace_r_count_chr_occurrences(v4 + 1, v5 - 1, 165LL);
v6 = snappy_compress(v12, v4, v5, a1, &v11);
}
}
else
{
v6 = -*_errno_location();
}
snappy_free_env(v12);
if ( *a2 != v4 )
LABEL_16:
free(v4);
if ( v6 )
{
LABEL_18:
v9 = _errno_location();
*v9 = -v6;
v10 = strerror(-v6);
ERR("Buffer compression failed with err", (unsigned int)*v9, v10);
return -1LL;
}
result = v11;
if ( v5 )
{
if ( v11 <= 0 )
_assert_fail(
"(((ssize_t) compressed_length > 0) || (0 == input_len)) || TRACE_LOG_ASSERTION_FAILURE(((ssize_t) compressed_len"
"gth > 0) || (0 == input_len))",
"trace_dumper/internal_buffer.c",
0xB8u,
"compress_iov_to_buffer");
}
return result;
}

original_code:
static ssize_t compress_iov_to_buffer(struct trace_record *target, const struct iovec *iov, int iovcnt)
{
    if (((void *)0) == iov) {
        (*__errno_location ()) = 14;
        return -1;
    }
    if (iovcnt < 1) {
        return 0;
    }
    char *src_buf = iov[0].iov_base;
    size_t input_len = iov[0].iov_len;
    size_t compressed_length = 0;
    struct snappy_env env;
    int rc = snappy_init_env(&env);
    if (rc < 0) {
        goto finish;
    }
    if (iovcnt > 1) {
        input_len = total_iovec_len(iov, iovcnt);
        src_buf = malloc(input_len);
        if ((((void *)0) == src_buf) || (copy_iov_to_buffer(src_buf, iov, iovcnt) != (ssize_t) input_len)) {
            rc = -(*__errno_location ());
            goto finish;
        }
    }
    if (input_len > 0) {
        input_len -= trace_r_count_chr_occurrences(src_buf + 1, input_len - 1, TRACE_UNUSED_SPACE_FILL_VALUE);
        rc = snappy_compress(&env, src_buf, input_len, (char *)target, &compressed_length);
    }
finish:
    snappy_free_env(&env);
    if (src_buf != iov[0].iov_base) {
        free(src_buf);
    }
    if (0 != rc) {
        (*__errno_location ()) = -rc;
        ERR("Buffer compression failed with err", (*__errno_location ()), strerror((*__errno_location ())));
        return (ssize_t) -1;
    }
    ((void) sizeof (((((ssize_t) compressed_length > 0) || (0 == input_len)) || (0)) ? 1 : 0), __extension__ ({ if ((((ssize_t) compressed_length > 0) || (0 == input_len)) || (0)) ; else __assert_fail ("(((ssize_t) compressed_length > 0) || (0 == input_len)) || TRACE_LOG_ASSERTION_FAILURE(((ssize_t) compressed_length > 0) || (0 == input_len))", "trace_dumper/internal_buffer.c", 184, __extension__ __PRETTY_FUNCTION__); }));
    return (ssize_t) compressed_length;
}

canonical_original_code:
static long compress_iov_to_buffer(struct trace_record *target, struct iovec *iov, int iovcnt)
{
    if (((void *)0) == iov) {
        (*__errno_location ()) = 14;
        return -1;
    }
    if (iovcnt < 1) {
        return 0;
    }
    char *src_buf = iov[0].iov_base;
    unsigned long input_len = iov[0].iov_len;
    unsigned long compressed_length = 0;
    struct snappy_env env;
    int rc = snappy_init_env(&env);
    if (rc < 0) {
        goto finish;
    }
    if (iovcnt > 1) {
        input_len = total_iovec_len(iov, iovcnt);
        src_buf = malloc(input_len);
        if ((((void *)0) == src_buf) || (copy_iov_to_buffer(src_buf, iov, iovcnt) != (long) input_len)) {
            rc = -(*__errno_location ());
            goto finish;
        }
    }
    if (input_len > 0) {
        input_len -= trace_r_count_chr_occurrences(src_buf + 1, input_len - 1, TRACE_UNUSED_SPACE_FILL_VALUE);
        rc = snappy_compress(&env, src_buf, input_len, (char *)target, &compressed_length);
    }
finish:
    snappy_free_env(&env);
    if (src_buf != iov[0].iov_base) {
        free(src_buf);
    }
    if (0 != rc) {
        (*__errno_location ()) = -rc;
        ERR("Buffer compression failed with err", (*__errno_location ()), strerror((*__errno_location ())));
        return (long) -1;
    }
    ((void) sizeof (((((long) compressed_length > 0) || (0 == input_len)) || (0)) ? 1 : 0), __extension__ ({ if ((((long) compressed_length > 0) || (0 == input_len)) || (0)) ; else __assert_fail ("(((ssize_t) compressed_length > 0) || (0 == input_len)) || TRACE_LOG_ASSERTION_FAILURE(((ssize_t) compressed_length > 0) || (0 == input_len))", "trace_dumper/internal_buffer.c", 184, __extension__ __PRETTY_FUNCTION__); }));
    return (long) compressed_length;
}

variable_types: struct trace_record { unsigned long long ts; unsigned short pid; unsigned short tid; short nesting; short bit_fields[]; unsigned int termination; unsigned int module_id; unsigned int reserved; unsigned int severity; unsigned int rec_type; unsigned int generation; union trace_record_u { unsigned char payload[44]; struct trace_record_typed { unsigned int log_id; unsigned char payload[]; } typed; struct trace_record_file_header { char machine_id[24]; unsigned short format_version; unsigned short flags; unsigned char reserved[4]; unsigned int magic; } file_header; struct trace_record_metadata { unsigned int metadata_size_bytes; unsigned short dead_pids[4]; unsigned char reserved[-1]; unsigned int metadata_magic; } metadata; struct trace_record_dump_header { unsigned int prev_dump_offset; unsigned int total_dump_size; unsigned int first_chunk_offset; unsigned long records_previously_discarded; } dump_header; struct trace_record_buffer_dump { unsigned int last_metadata_offset; unsigned int prev_chunk_offset; unsigned int dump_header_offset; unsigned long long ts; unsigned int records; unsigned int severity_type; unsigned int lost_records; unsigned short flags; unsigned char slack_bytes; } buffer_chunk; struct trace_record_data_loss { unsigned int lost_records; unsigned long long ts_start; unsigned long long ts_end; } data_loss; } u; } *

variable_types: struct iovec { void *iov_base; unsigned long iov_len; } *

variable_types: iovcnt int

variable_types: struct snappy_env { unsigned short *hash_table; void *scratch; void *scratch_output; }

return_type: long


decompiled_code:
__int64 __fastcall compactify_buffer(_DWORD *a1, int a2, unsigned int a3)
{
__int64 result; // rax
unsigned int v5; // eax

result = a2 + a3;
if ( *a1 < (unsigned int)result )
_assert_fail(
"(buf->write_idx.t >= start_idx + actually_used) || TRACE_LOG_ASSERTION_FAILURE(buf->write_idx.t >= start_idx + actually_used)",
"trace_dumper/internal_buffer.c",
0xCAu,
"compactify_buffer");
if ( a2 || a1[5] < a3 )
{
*a1 = result;
}
else
{
DEBUG("Copying", a3, "records into slack space");
v5 = effective_capacity_t((__int64)a1);
memcpy(&a1[16 * (unsigned __int64)v5 + 16], a1 + 16, a3 << 6);
a1[5] -= a3;
result = effective_capacity_t((__int64)a1);
*a1 = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_DWORD *a1, int a2, unsigned int a3)
{
__int64 result; // rax
unsigned int v5; // eax

result = a2 + a3;
if ( *a1 < (unsigned int)result )
_assert_fail(
"(buf->write_idx.t >= start_idx + actually_used) || TRACE_LOG_ASSERTION_FAILURE(buf->write_idx.t >= start_idx + actually_used)",
"trace_dumper/internal_buffer.c",
0xCAu,
"compactify_buffer");
if ( a2 || a1[5] < a3 )
{
*a1 = result;
}
else
{
DEBUG("Copying", a3, "records into slack space");
v5 = func1((__int64)a1);
memcpy(&a1[16 * (unsigned __int64)v5 + 16], a1 + 16, a3 << 6);
a1[5] -= a3;
result = func1((__int64)a1);
*a1 = result;
}
return result;
}

original_code:
static void compactify_buffer(struct trace_internal_buf *buf, unsigned start_idx, unsigned actually_used)
{
    ((void) sizeof (((buf->write_idx.t >= start_idx + actually_used) || (0)) ? 1 : 0), __extension__ ({ if ((buf->write_idx.t >= start_idx + actually_used) || (0)) ; else __assert_fail ("(buf->write_idx.t >= start_idx + actually_used) || TRACE_LOG_ASSERTION_FAILURE(buf->write_idx.t >= start_idx + actually_used)", "trace_dumper/internal_buffer.c", 202, __extension__ __PRETTY_FUNCTION__); }));
    if ((0 == start_idx) && (buf->n_slack_recs.t >= actually_used)) {
        DEBUG("Copying", actually_used, "records into slack space", buf->write_idx.t, buf->n_slack_recs.t);
        memcpy(buf->records + effective_capacity_t(buf), buf->records, 64U * actually_used);
        buf->n_slack_recs.t -= actually_used;
        buf->write_idx.t = effective_capacity_t(buf);
    }
    else {
        buf->write_idx.t = start_idx + actually_used;
    }
}

canonical_original_code:
static void compactify_buffer(struct trace_internal_buf *buf, unsigned int start_idx, unsigned int actually_used)
{
    ((void) sizeof (((buf->write_idx.t >= start_idx + actually_used) || (0)) ? 1 : 0), __extension__ ({ if ((buf->write_idx.t >= start_idx + actually_used) || (0)) ; else __assert_fail ("(buf->write_idx.t >= start_idx + actually_used) || TRACE_LOG_ASSERTION_FAILURE(buf->write_idx.t >= start_idx + actually_used)", "trace_dumper/internal_buffer.c", 202, __extension__ __PRETTY_FUNCTION__); }));
    if ((0 == start_idx) && (buf->n_slack_recs.t >= actually_used)) {
        DEBUG("Copying", actually_used, "records into slack space", buf->write_idx.t, buf->n_slack_recs.t);
        memcpy(buf->records + effective_capacity_t(buf), buf->records, 64U * actually_used);
        buf->n_slack_recs.t -= actually_used;
        buf->write_idx.t = effective_capacity_t(buf);
    }
    else {
        buf->write_idx.t = start_idx + actually_used;
    }
}


decompiled_code:
__int64 __fastcall internal_buf_free(void *addr)
{
__int64 v1; // rbx
size_t v2; // rsi

if ( !addr )
return 0LL;
v1 = (unsigned int)((*((_DWORD *)addr + 4) + 1) << 6);
v2 = trace_round_size_up_to_page_size(v1);
if ( v2 != v1 )
_assert_fail(
"(trace_round_size_up_to_page_size(len) == len) || TRACE_LOG_ASSERTION_FAILURE(trace_round_size_up_to_page_size(len) == len)",
"trace_dumper/internal_buffer.c",
0x3Cu,
"internal_buf_free");
return (unsigned int)-(munmap(addr, v2) != 0);
}

canonical_decompiled_code:
__int64 __fastcall func9(void *addr)
{
__int64 v1; // rbx
size_t v2; // rsi

if ( !addr )
return 0LL;
v1 = (unsigned int)((*((_DWORD *)addr + 4) + 1) << 6);
v2 = trace_round_size_up_to_page_size(v1);
if ( v2 != v1 )
_assert_fail(
"(trace_round_size_up_to_page_size(len) == len) || TRACE_LOG_ASSERTION_FAILURE(trace_round_size_up_to_page_size(len) == len)",
"trace_dumper/internal_buffer.c",
0x3Cu,
"internal_buf_free");
return (unsigned int)-(munmap(addr, v2) != 0);
}

original_code:
int internal_buf_free(struct trace_internal_buf *buf)
{
    if (((void *)0) != buf)
    {
        const size_t len = 64U * buf->n_recs + BUF_HEADER_SIZE;
        ((void) sizeof (((trace_round_size_up_to_page_size(len) == len) || (0)) ? 1 : 0), __extension__ ({ if ((trace_round_size_up_to_page_size(len) == len) || (0)) ; else __assert_fail ("(trace_round_size_up_to_page_size(len) == len) || TRACE_LOG_ASSERTION_FAILURE(trace_round_size_up_to_page_size(len) == len)", "trace_dumper/internal_buffer.c", 60, __extension__ __PRETTY_FUNCTION__); }));
        if (munmap(buf, len) != 0) {
            return -1;
        }
    }
    return 0;
}

canonical_original_code:
int internal_buf_free(struct trace_internal_buf *buf)
{
    if (((void *)0) != buf)
    {
        unsigned long len = 64U * buf->n_recs + BUF_HEADER_SIZE;
        ((void) sizeof (((trace_round_size_up_to_page_size(len) == len) || (0)) ? 1 : 0), __extension__ ({ if ((trace_round_size_up_to_page_size(len) == len) || (0)) ; else __assert_fail ("(trace_round_size_up_to_page_size(len) == len) || TRACE_LOG_ASSERTION_FAILURE(trace_round_size_up_to_page_size(len) == len)", "trace_dumper/internal_buffer.c", 60, __extension__ __PRETTY_FUNCTION__); }));
        if (munmap(buf, len) != 0) {
            return -1;
        }
    }
    return 0;
}

variable_types:
len
unsigned long


decompiled_code:
__int64 __fastcall internal_buf_free_and_invalidate(void **a1)
{
__int64 result; // rax

if ( a1 )
{
result = internal_buf_free(*a1);
if ( (_DWORD)result )
result = 0xFFFFFFFFLL;
else
*a1 = 0LL;
}
else
{
*_errno_location() = 14;
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(void **a1)
{
__int64 result; // rax

if ( a1 )
{
result = func9(*a1);
if ( (_DWORD)result )
result = 0xFFFFFFFFLL;
else
*a1 = 0LL;
}
else
{
*_errno_location() = 14;
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int internal_buf_free_and_invalidate(struct trace_internal_buf **p_buf)
{
    if (((void *)0) == p_buf) {
        (*__errno_location ()) = 14;
        return -1;
    }
    if (internal_buf_free(*p_buf) != 0) {
        return -1;
    }
    *p_buf = ((void *)0);
    return 0;
}

variable_types: struct trace_internal_buf { struct trace_internal_buf_index { unsigned int t; unsigned int c; } write_idx; struct trace_internal_buf_index read_idx; unsigned int n_recs; struct trace_internal_buf_index n_slack_recs; struct trace_record { unsigned long long ts; unsigned short pid; unsigned short tid; short nesting; short bit_fields[]; unsigned int termination; unsigned int module_id; unsigned int reserved; unsigned int severity; unsigned int rec_type; unsigned int generation; union trace_record_u { unsigned char payload[44]; struct trace_record_typed { unsigned int log_id; unsigned char payload[]; } typed; struct trace_record_file_header { char machine_id[24]; unsigned short format_version; unsigned short flags; unsigned char reserved[4]; unsigned int magic; } file_header; struct trace_record_metadata { unsigned int metadata_size_bytes; unsigned short dead_pids[4]; unsigned char reserved[-1]; unsigned int metadata_magic; } metadata; struct trace_record_dump_header { unsigned int prev_dump_offset; unsigned int total_dump_size; unsigned int first_chunk_offset; unsigned long records_previously_discarded; } dump_header; struct trace_record_buffer_dump { unsigned int last_metadata_offset; unsigned int prev_chunk_offset; unsigned int dump_header_offset; unsigned long long ts; unsigned int records; unsigned int severity_type; unsigned int lost_records; unsigned short flags; unsigned char slack_bytes; } buffer_chunk; struct trace_record_data_loss { unsigned int lost_records; unsigned long long ts_start; unsigned long long ts_end; } data_loss; } u; } records[]; } **


decompiled_code:
__int64 __fastcall internal_buf_alloc_space(unsigned int *a1, unsigned __int64 a2)
{
__int64 result; // rax
unsigned int v4; // edx
unsigned __int64 v5; // rcx
unsigned __int64 v6; // rsi
int v7; // ecx
unsigned int v8; // esi
unsigned __int64 v9; // rcx

result = *a1;
v4 = a1[3];
if ( (unsigned int)result < v4 )
{
if ( v4 >= a2 + (unsigned int)result )
goto LABEL_4;
LABEL_9:
*_errno_location() = 12;
result = 0xFFFFFFFFLL;
v7 = *a1;
v4 = a1[3];
goto LABEL_5;
}
if ( (_DWORD)result == -1 )
{
*_errno_location() = 12;
result = *a1;
v4 = a1[3];
v9 = a2 + result;
v6 = a1[4];
if ( v6 >= v9 )
goto LABEL_4;
}
else
{
v5 = a2 + (unsigned int)result;
v6 = a1[4];
if ( v6 >= v5 )
{
LABEL_4:
v7 = result + a2;
*a1 = result + a2;
goto LABEL_5;
}
}
if ( v4 < a2 )
goto LABEL_9;
v8 = v6 - result;
*a1 = a2;
v7 = a2;
result = 0LL;
a1[5] = v8;
LABEL_5:
if ( v7 == v4 )
{
if ( a2 )
*a1 = -1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int *a1, unsigned __int64 a2)
{
__int64 result; // rax
unsigned int v4; // edx
unsigned __int64 v5; // rcx
unsigned __int64 v6; // rsi
int v7; // ecx
unsigned int v8; // esi
unsigned __int64 v9; // rcx

result = *a1;
v4 = a1[3];
if ( (unsigned int)result < v4 )
{
if ( v4 >= a2 + (unsigned int)result )
goto LABEL_4;
LABEL_9:
*_errno_location() = 12;
result = 0xFFFFFFFFLL;
v7 = *a1;
v4 = a1[3];
goto LABEL_5;
}
if ( (_DWORD)result == -1 )
{
*_errno_location() = 12;
result = *a1;
v4 = a1[3];
v9 = a2 + result;
v6 = a1[4];
if ( v6 >= v9 )
goto LABEL_4;
}
else
{
v5 = a2 + (unsigned int)result;
v6 = a1[4];
if ( v6 >= v5 )
{
LABEL_4:
v7 = result + a2;
*a1 = result + a2;
goto LABEL_5;
}
}
if ( v4 < a2 )
goto LABEL_9;
v8 = v6 - result;
*a1 = a2;
v7 = a2;
result = 0LL;
a1[5] = v8;
LABEL_5:
if ( v7 == v4 )
{
if ( a2 )
*a1 = -1;
}
return result;
}

original_code:
unsigned internal_buf_alloc_space(struct trace_internal_buf *buf, size_t n_recs)
{
    unsigned ret = ((unsigned) -1);
    if (buf->write_idx.t >= buf->read_idx.c) {
        if (((unsigned) -1) == buf->write_idx.t) {
            (*__errno_location ()) = 12;
        }
        if (buf->n_recs >= buf->write_idx.t + n_recs) {
            ret = buf->write_idx.t;
            buf->write_idx.t += n_recs;
        }
        else if (buf->read_idx.c >= n_recs) {
            buf->n_slack_recs.t = buf->n_recs - buf->write_idx.t;
            buf->write_idx.t = n_recs;
            ret = 0;
        }
        else {
            (*__errno_location ()) = 12;
        }
    }
    else {
        if (buf->read_idx.c >= buf->write_idx.t + n_recs) {
            ret = buf->write_idx.t;
            buf->write_idx.t += n_recs;
        }
        else {
            (*__errno_location ()) = 12;
        }
    }
    if ((buf->write_idx.t == buf->read_idx.c) && (n_recs > 0)) {
        buf->write_idx.t = ((unsigned) -1);
    }
    return ret;
}

canonical_original_code:
unsigned int internal_buf_alloc_space(struct trace_internal_buf *buf, unsigned long n_recs)
{
    unsigned int ret = ((unsigned) -1);
    if (buf->write_idx.t >= buf->read_idx.c) {
        if (((unsigned int) -1) == buf->write_idx.t) {
            (*__errno_location ()) = 12;
        }
        if (buf->n_recs >= buf->write_idx.t + n_recs) {
            ret = buf->write_idx.t;
            buf->write_idx.t += n_recs;
        }
        else if (buf->read_idx.c >= n_recs) {
            buf->n_slack_recs.t = buf->n_recs - buf->write_idx.t;
            buf->write_idx.t = n_recs;
            ret = 0;
        }
        else {
            (*__errno_location ()) = 12;
        }
    }
    else {
        if (buf->read_idx.c >= buf->write_idx.t + n_recs) {
            ret = buf->write_idx.t;
            buf->write_idx.t += n_recs;
        }
        else {
            (*__errno_location ()) = 12;
        }
    }
    if ((buf->write_idx.t == buf->read_idx.c) && (n_recs > 0)) {
        buf->write_idx.t = ((unsigned int) -1);
    }
    return ret;
}

variable_types:
ret
unsigned int


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

v4 = internal_buf_alloc_space(a1, a3);
if ( v4 == -1 )
result = 0LL;
else
result = memcpy(&a1[16 * (unsigned __int64)v4 + 16], a2, a3 << 6);
return result;
}

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

v4 = func11(a1, a3);
if ( v4 == -1 )
result = 0LL;
else
result = memcpy(&a1[16 * (unsigned __int64)v4 + 16], a2, a3 << 6);
return result;
}

original_code:
struct trace_record *internal_buf_write_recs(struct trace_internal_buf *buf, const struct trace_record *recs, size_t n_recs)
{
    unsigned start_idx = internal_buf_alloc_space(buf, n_recs);
    if (((unsigned) -1) == start_idx) {
        return ((void *)0);
    }
    struct trace_record *const p = buf->records + start_idx;
    memcpy(p, recs, 64U * n_recs);
    return p;
}

canonical_original_code:
struct trace_record *internal_buf_write_recs(struct trace_internal_buf *buf, struct trace_record *recs, unsigned long n_recs)
{
    unsigned int start_idx = internal_buf_alloc_space(buf, n_recs);
    if (((unsigned int) -1) == start_idx) {
        return ((void *)0);
    }
    struct trace_record *p = buf->records + start_idx;
    memcpy(p, recs, 64U * n_recs);
    return p;
}

variable_types: struct trace_internal_buf { struct trace_internal_buf_index { unsigned int t; unsigned int c; } write_idx; struct trace_internal_buf_index read_idx; unsigned int n_recs; struct trace_internal_buf_index n_slack_recs; struct trace_record records[]; } *

variable_types:
recs
struct trace_record *

return_type: struct trace_record { unsigned long long ts; unsigned short pid; unsigned short tid; short nesting; short bit_fields[]; unsigned int termination; unsigned int module_id; unsigned int reserved; unsigned int severity; unsigned int rec_type; unsigned int generation; union trace_record_u { unsigned char payload[44]; struct trace_record_typed { unsigned int log_id; unsigned char payload[]; } typed; struct trace_record_file_header { char machine_id[24]; unsigned short format_version; unsigned short flags; unsigned char reserved[4]; unsigned int magic; } file_header; struct trace_record_metadata { unsigned int metadata_size_bytes; unsigned short dead_pids[4]; unsigned char reserved[-1]; unsigned int metadata_magic; } metadata; struct trace_record_dump_header { unsigned int prev_dump_offset; unsigned int total_dump_size; unsigned int first_chunk_offset; unsigned long records_previously_discarded; } dump_header; struct trace_record_buffer_dump { unsigned int last_metadata_offset; unsigned int prev_chunk_offset; unsigned int dump_header_offset; unsigned long long ts; unsigned int records; unsigned int severity_type; unsigned int lost_records; unsigned short flags; unsigned char slack_bytes; } buffer_chunk; struct trace_record_data_loss { unsigned int lost_records; unsigned long long ts_start; unsigned long long ts_end; } data_loss; } u; } *


decompiled_code:
__int64 __fastcall internal_buf_write_compressed(unsigned int *a1, char **a2, unsigned int a3)
{
__int64 v4; // rax
__int64 v5; // rax
unsigned __int64 v6; // rax
unsigned int v7; // eax
int v8; // er13
__int64 v9; // rax
__int64 v10; // r12
unsigned int v11; // eax

v4 = total_iovec_len(a2, a3);
v5 = snappy_max_compressed_length(v4);
v6 = internal_buf_bytes_to_recs(v5);
v7 = internal_buf_alloc_space(a1, v6);
if ( v7 == -1 )
return -1LL;
v8 = v7;
v9 = compress_iov_to_buffer((__int64)&a1[16 * (unsigned __int64)v7 + 16], a2, a3);
v10 = v9;
if ( v9 < 0 )
{
v10 = -1LL;
compactify_buffer(a1, v8, 0);
}
else
{
v11 = internal_buf_bytes_to_recs(v9);
compactify_buffer(a1, v8, v11);
fill_compression_remainder_with_pattern(a1, v10);
}
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned int *a1, char **a2, unsigned int a3)
{
__int64 v4; // rax
__int64 v5; // rax
unsigned __int64 v6; // rax
unsigned int v7; // eax
int v8; // er13
__int64 v9; // rax
__int64 v10; // r12
unsigned int v11; // eax

v4 = total_iovec_len(a2, a3);
v5 = snappy_max_compressed_length(v4);
v6 = func0(v5);
v7 = func11(a1, v6);
if ( v7 == -1 )
return -1LL;
v8 = v7;
v9 = func5((__int64)&a1[16 * (unsigned __int64)v7 + 16], a2, a3);
v10 = v9;
if ( v9 < 0 )
{
v10 = -1LL;
func6(a1, v8, 0);
}
else
{
v11 = func0(v9);
func6(a1, v8, v11);
func2(a1, v10);
}
return v10;
}

original_code:
ssize_t internal_buf_write_compressed(struct trace_internal_buf *buf, const struct iovec *iov, int iovcnt)
{
    const size_t max_size = snappy_max_compressed_length(total_iovec_len(iov, iovcnt));
    const size_t max_recs = internal_buf_bytes_to_recs(max_size);
    const unsigned start_idx = internal_buf_alloc_space(buf, max_recs);
    if (((unsigned) -1) == start_idx) {
        return -1;
    }
    const ssize_t compressed_len = compress_iov_to_buffer(buf->records + start_idx, iov, iovcnt);
    if (compressed_len < 0) {
        compactify_buffer(buf, start_idx, 0);
        return -1;
    }
    compactify_buffer(buf, start_idx, internal_buf_bytes_to_recs(compressed_len));
    fill_compression_remainder_with_pattern(buf, compressed_len);
    return compressed_len;
}

canonical_original_code:
long internal_buf_write_compressed(struct trace_internal_buf *buf, struct iovec *iov, int iovcnt)
{
    unsigned long max_size = snappy_max_compressed_length(total_iovec_len(iov, iovcnt));
    unsigned long max_recs = internal_buf_bytes_to_recs(max_size);
    unsigned int start_idx = internal_buf_alloc_space(buf, max_recs);
    if (((unsigned int) -1) == start_idx) {
        return -1;
    }
    long compressed_len = compress_iov_to_buffer(buf->records + start_idx, iov, iovcnt);
    if (compressed_len < 0) {
        compactify_buffer(buf, start_idx, 0);
        return -1;
    }
    compactify_buffer(buf, start_idx, internal_buf_bytes_to_recs(compressed_len));
    fill_compression_remainder_with_pattern(buf, compressed_len);
    return compressed_len;
}

user_defined_types: struct iovec { void *iov_base; unsigned long iov_len; }


decompiled_code:
__int64 __fastcall internal_buf_num_recs_pending(__int64 a1)
{
unsigned int v1; // ebx
unsigned int v2; // ebp
__int64 result; // rax

if ( !a1 )
return 0LL;
v1 = *(_DWORD *)(a1 + 4);
v2 = *(_DWORD *)(a1 + 12);
if ( v1 < v2 )
return v1 - v2 + (unsigned int)effective_capacity_c(a1);
result = v1 - v2;
if ( v1 == -1 )
result = effective_capacity_c(a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
unsigned int v1; // ebx
unsigned int v2; // ebp
__int64 result; // rax

if ( !a1 )
return 0LL;
v1 = *(_DWORD *)(a1 + 4);
v2 = *(_DWORD *)(a1 + 12);
if ( v1 < v2 )
return v1 - v2 + (unsigned int)func3(a1);
result = v1 - v2;
if ( v1 == -1 )
result = func3(a1);
return result;
}

original_code:
unsigned internal_buf_num_recs_pending(const struct trace_internal_buf *buf)
{
    if (!buf) {
        return 0;
    }
    if (buf->write_idx.c >= buf->read_idx.c) {
        if (((unsigned) -1) == buf->write_idx.c) {
            return effective_capacity_c(buf);
        }
        return buf->write_idx.c - buf->read_idx.c;
    }
    return effective_capacity_c(buf) + buf->write_idx.c - buf->read_idx.c;
}

canonical_original_code:
unsigned int internal_buf_num_recs_pending(struct trace_internal_buf *buf)
{
    if (!buf) {
        return 0;
    }
    if (buf->write_idx.c >= buf->read_idx.c) {
        if (((unsigned int) -1) == buf->write_idx.c) {
            return effective_capacity_c(buf);
        }
        return buf->write_idx.c - buf->read_idx.c;
    }
    return effective_capacity_c(buf) + buf->write_idx.c - buf->read_idx.c;
}


decompiled_code:
_BOOL8 __fastcall internal_buf_contiguous_pending_read_as_iov(_DWORD *a1, __int64 a2)
{
unsigned int v2; // eax
unsigned int v3; // er12
unsigned int v4; // ebp
unsigned int v5; // eax
_BOOL4 v6; // er14
__int64 v7; // r12
bool v8; // zf
unsigned int v9; // eax
int v10; // edx
_BOOL8 result; // rax

v2 = internal_buf_num_recs_pending((__int64)a1);
v3 = a1[3];
v4 = v2;
v5 = effective_capacity_c((__int64)a1);
if ( v3 >= v5 )
_assert_fail(
"(buf->read_idx.c < effective_capacity_c(buf)) || TRACE_LOG_ASSERTION_FAILURE(buf->read_idx.c < effective_capacity_c(buf))",
"trace_dumper/internal_buffer.c",
0x118u,
"internal_buf_contiguous_pending_read_as_iov");
v6 = v5 >= v3 + v4;
if ( v5 >= v3 + v4 )
{
*(_OWORD *)a2 = 0LL;
if ( !v4 )
return v6;
}
else
{
v4 = v5 - v3;
*(_OWORD *)a2 = 0LL;
}
v7 = (unsigned int)a1[3];
*(_QWORD *)a2 = &a1[16 * v7 + 16];
v8 = *a1 == -1;
*(_QWORD *)(a2 + 8) = v4 << 6;
if ( v8 )
*a1 = a1[2];
v9 = internal_buf_recs_ahead_of_read_c((__int64)a1, v4);
a1[2] = v9;
if ( !index_has_wrapped_around_isra_0(v9, v7) )
return v6;
if ( v10 )
_assert_fail(
"(0 == buf->read_idx.t) || TRACE_LOG_ASSERTION_FAILURE(0 == buf->read_idx.t)",
"trace_dumper/internal_buffer.c",
0x12Au,
"internal_buf_contiguous_pending_read_as_iov");
result = v6;
a1[5] = 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func15(_DWORD *a1, __int64 a2)
{
unsigned int v2; // eax
unsigned int v3; // er12
unsigned int v4; // ebp
unsigned int v5; // eax
_BOOL4 v6; // er14
__int64 v7; // r12
bool v8; // zf
unsigned int v9; // eax
int v10; // edx
_BOOL8 result; // rax

v2 = func14((__int64)a1);
v3 = a1[3];
v4 = v2;
v5 = func3((__int64)a1);
if ( v3 >= v5 )
_assert_fail(
"(buf->read_idx.c < effective_capacity_c(buf)) || TRACE_LOG_ASSERTION_FAILURE(buf->read_idx.c < effective_capacity_c(buf))",
"trace_dumper/internal_buffer.c",
0x118u,
"internal_buf_contiguous_pending_read_as_iov");
v6 = v5 >= v3 + v4;
if ( v5 >= v3 + v4 )
{
*(_OWORD *)a2 = 0LL;
if ( !v4 )
return v6;
}
else
{
v4 = v5 - v3;
*(_OWORD *)a2 = 0LL;
}
v7 = (unsigned int)a1[3];
*(_QWORD *)a2 = &a1[16 * v7 + 16];
v8 = *a1 == -1;
*(_QWORD *)(a2 + 8) = v4 << 6;
if ( v8 )
*a1 = a1[2];
v9 = func4((__int64)a1, v4);
a1[2] = v9;
if ( !func7(v9, v7) )
return v6;
if ( v10 )
_assert_fail(
"(0 == buf->read_idx.t) || TRACE_LOG_ASSERTION_FAILURE(0 == buf->read_idx.t)",
"trace_dumper/internal_buffer.c",
0x12Au,
"internal_buf_contiguous_pending_read_as_iov");
result = v6;
a1[5] = 0;
return result;
}

original_code:
bool_t internal_buf_contiguous_pending_read_as_iov(struct trace_internal_buf *buf, struct iovec *iov)
{
    const unsigned num_recs_pending = internal_buf_num_recs_pending(buf);
    ((void) sizeof (((buf->read_idx.c < effective_capacity_c(buf)) || (0)) ? 1 : 0), __extension__ ({ if ((buf->read_idx.c < effective_capacity_c(buf)) || (0)) ; else __assert_fail ("(buf->read_idx.c < effective_capacity_c(buf)) || TRACE_LOG_ASSERTION_FAILURE(buf->read_idx.c < effective_capacity_c(buf))", "trace_dumper/internal_buffer.c", 280, __extension__ __PRETTY_FUNCTION__); }));
    const bool_t all_pending_flushed = (buf->read_idx.c + num_recs_pending <= effective_capacity_c(buf));
    const unsigned len = all_pending_flushed ? num_recs_pending : effective_capacity_c(buf) - buf->read_idx.c;
    memset(iov, 0, sizeof(*iov));
    if (len > 0) {
        iov->iov_base = buf->records + buf->read_idx.c;
        iov->iov_len = 64U * len;
        if (((unsigned) -1) == buf->write_idx.t) {
            buf->write_idx.t = buf->read_idx.t;
        }
        buf->read_idx.t = internal_buf_recs_ahead_of_read_c(buf, len);
        if (index_has_wrapped_around(&buf->read_idx)) {
            ((void) sizeof (((0 == buf->read_idx.t) || (0)) ? 1 : 0), __extension__ ({ if ((0 == buf->read_idx.t) || (0)) ; else __assert_fail ("(0 == buf->read_idx.t) || TRACE_LOG_ASSERTION_FAILURE(0 == buf->read_idx.t)", "trace_dumper/internal_buffer.c", 298, __extension__ __PRETTY_FUNCTION__); }));
            buf->n_slack_recs.t = 0;
        }
    }
    else {
        ((void) sizeof (((all_pending_flushed) || (0)) ? 1 : 0), __extension__ ({ if ((all_pending_flushed) || (0)) ; else __assert_fail ("(all_pending_flushed) || TRACE_LOG_ASSERTION_FAILURE(all_pending_flushed)", "trace_dumper/internal_buffer.c", 303, __extension__ __PRETTY_FUNCTION__); }));
    }
    return all_pending_flushed;
}

canonical_original_code:
int internal_buf_contiguous_pending_read_as_iov(struct trace_internal_buf *buf, struct iovec *iov)
{
    unsigned int num_recs_pending = internal_buf_num_recs_pending(buf);
    ((void) sizeof (((buf->read_idx.c < effective_capacity_c(buf)) || (0)) ? 1 : 0), __extension__ ({ if ((buf->read_idx.c < effective_capacity_c(buf)) || (0)) ; else __assert_fail ("(buf->read_idx.c < effective_capacity_c(buf)) || TRACE_LOG_ASSERTION_FAILURE(buf->read_idx.c < effective_capacity_c(buf))", "trace_dumper/internal_buffer.c", 280, __extension__ __PRETTY_FUNCTION__); }));
    int all_pending_flushed = (buf->read_idx.c + num_recs_pending <= effective_capacity_c(buf));
    unsigned int len = all_pending_flushed ? num_recs_pending : effective_capacity_c(buf) - buf->read_idx.c;
    memset(iov, 0, sizeof(*iov));
    if (len > 0) {
        iov->iov_base = buf->records + buf->read_idx.c;
        iov->iov_len = 64U * len;
        if (((unsigned int) -1) == buf->write_idx.t) {
            buf->write_idx.t = buf->read_idx.t;
        }
        buf->read_idx.t = internal_buf_recs_ahead_of_read_c(buf, len);
        if (index_has_wrapped_around(&buf->read_idx)) {
            ((void) sizeof (((0 == buf->read_idx.t) || (0)) ? 1 : 0), __extension__ ({ if ((0 == buf->read_idx.t) || (0)) ; else __assert_fail ("(0 == buf->read_idx.t) || TRACE_LOG_ASSERTION_FAILURE(0 == buf->read_idx.t)", "trace_dumper/internal_buffer.c", 298, __extension__ __PRETTY_FUNCTION__); }));
            buf->n_slack_recs.t = 0;
        }
    }
    else {
        ((void) sizeof (((all_pending_flushed) || (0)) ? 1 : 0), __extension__ ({ if ((all_pending_flushed) || (0)) ; else __assert_fail ("(all_pending_flushed) || TRACE_LOG_ASSERTION_FAILURE(all_pending_flushed)", "trace_dumper/internal_buffer.c", 303, __extension__ __PRETTY_FUNCTION__); }));
    }
    return all_pending_flushed;
}

variable_types:
len
unsigned int


decompiled_code:
__int64 __fastcall internal_buf_create_iov_for_pending_reads(_DWORD *a1, _QWORD *a2, _DWORD *a3)
{
_BOOL4 v4; // eax
__int64 result; // rax
unsigned int v6; // eax

*a3 = 0;
v4 = internal_buf_contiguous_pending_read_as_iov(a1, (__int64)a2);
if ( a2[1] )
++*a3;
if ( v4 )
{
result = (unsigned int)*a3;
}
else
{
v6 = internal_buf_num_recs_pending((__int64)a1);
if ( *a3 != 1 )
_assert_fail(
"(1 == *iovcnt) || TRACE_LOG_ASSERTION_FAILURE(1 == *iovcnt)",
"trace_dumper/internal_buffer.c",
0x140u,
"internal_buf_create_iov_for_pending_reads");
a2[2] = a1 + 16;
a2[3] = (v6 - (a2[1] >> 6)) << 6;
a1[2] = internal_buf_recs_ahead_of_read_c((__int64)a1, v6);
result = (unsigned int)(*a3 + 1);
*a3 = result;
}
if ( (int)result > 2 )
_assert_fail(
"(*iovcnt <= 2) || TRACE_LOG_ASSERTION_FAILURE(*iovcnt <= 2)",
"trace_dumper/internal_buffer.c",
0x147u,
"internal_buf_create_iov_for_pending_reads");
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(_DWORD *a1, _QWORD *a2, _DWORD *a3)
{
_BOOL4 v4; // eax
__int64 result; // rax
unsigned int v6; // eax

*a3 = 0;
v4 = func15(a1, (__int64)a2);
if ( a2[1] )
++*a3;
if ( v4 )
{
result = (unsigned int)*a3;
}
else
{
v6 = func14((__int64)a1);
if ( *a3 != 1 )
_assert_fail(
"(1 == *iovcnt) || TRACE_LOG_ASSERTION_FAILURE(1 == *iovcnt)",
"trace_dumper/internal_buffer.c",
0x140u,
"internal_buf_create_iov_for_pending_reads");
a2[2] = a1 + 16;
a2[3] = (v6 - (a2[1] >> 6)) << 6;
a1[2] = func4((__int64)a1, v6);
result = (unsigned int)(*a3 + 1);
*a3 = result;
}
if ( (int)result > 2 )
_assert_fail(
"(*iovcnt <= 2) || TRACE_LOG_ASSERTION_FAILURE(*iovcnt <= 2)",
"trace_dumper/internal_buffer.c",
0x147u,
"internal_buf_create_iov_for_pending_reads");
return result;
}

original_code:
void internal_buf_create_iov_for_pending_reads(struct trace_internal_buf *buf, struct iovec iov[2], int *iovcnt)
{
    *iovcnt = 0;
    const bool_t complete_flush = internal_buf_contiguous_pending_read_as_iov(buf, iov);
    if (iov[0].iov_len > 0) {
        ++ *iovcnt;
    }
    if (!complete_flush) {
        const unsigned num_recs_pending = internal_buf_num_recs_pending(buf);
        ((void) sizeof (((1 == *iovcnt) || (0)) ? 1 : 0), __extension__ ({ if ((1 == *iovcnt) || (0)) ; else __assert_fail ("(1 == *iovcnt) || TRACE_LOG_ASSERTION_FAILURE(1 == *iovcnt)", "trace_dumper/internal_buffer.c", 320, __extension__ __PRETTY_FUNCTION__); }));
        iov[1].iov_base = buf->records;
        iov[1].iov_len = 64U * (num_recs_pending - iov[0].iov_len / 64U);
        buf->read_idx.t = internal_buf_recs_ahead_of_read_c(buf, num_recs_pending);
        ++ *iovcnt;
    }
    ((void) sizeof (((*iovcnt <= 2) || (0)) ? 1 : 0), __extension__ ({ if ((*iovcnt <= 2) || (0)) ; else __assert_fail ("(*iovcnt <= 2) || TRACE_LOG_ASSERTION_FAILURE(*iovcnt <= 2)", "trace_dumper/internal_buffer.c", 327, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void internal_buf_create_iov_for_pending_reads(struct trace_internal_buf *buf, struct iovec iov[2], int *iovcnt)
{
    *iovcnt = 0;
    int complete_flush = internal_buf_contiguous_pending_read_as_iov(buf, iov);
    if (iov[0].iov_len > 0) {
        ++ *iovcnt;
    }
    if (!complete_flush) {
        unsigned int num_recs_pending = internal_buf_num_recs_pending(buf);
        ((void) sizeof (((1 == *iovcnt) || (0)) ? 1 : 0), __extension__ ({ if ((1 == *iovcnt) || (0)) ; else __assert_fail ("(1 == *iovcnt) || TRACE_LOG_ASSERTION_FAILURE(1 == *iovcnt)", "trace_dumper/internal_buffer.c", 320, __extension__ __PRETTY_FUNCTION__); }));
        iov[1].iov_base = buf->records;
        iov[1].iov_len = 64U * (num_recs_pending - iov[0].iov_len / 64U);
        buf->read_idx.t = internal_buf_recs_ahead_of_read_c(buf, num_recs_pending);
        ++ *iovcnt;
    }
    ((void) sizeof (((*iovcnt <= 2) || (0)) ? 1 : 0), __extension__ ({ if ((*iovcnt <= 2) || (0)) ; else __assert_fail ("(*iovcnt <= 2) || TRACE_LOG_ASSERTION_FAILURE(*iovcnt <= 2)", "trace_dumper/internal_buffer.c", 327, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: struct iovec { void *iov_base; unsigned long iov_len; } [2]

variable_types:
iovcnt
int *


decompiled_code:
unsigned __int64 __fastcall get_log_descriptor_size(unsigned int a1)
{
return (-(__int64)(a1 < 0xA2) & 0xFFFFFFFFFFFFFFF0LL) + 32;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(unsigned int a1)
{
return (-(__int64)(a1 < 0xA2) & 0xFFFFFFFFFFFFFFF0LL) + 32;
}

original_code:
size_t get_log_descriptor_size(unsigned fmt_version)
{
 return (fmt_version < (0xA2)) ? 16 : sizeof(struct trace_log_descriptor);
}

canonical_original_code:
unsigned long get_log_descriptor_size(unsigned int fmt_version)
{
 return (fmt_version < (0xA2)) ? 16 : sizeof(struct trace_log_descriptor);
}

user_defined_types: struct trace_log_descriptor { enum trace_log_descriptor_kind kind; unsigned int line; unsigned int severity; struct trace_param_descriptor *params; char *file; char *function; }

user_defined_types: enum trace_log_descriptor_kind { TRACE_LOG_DESCRIPTOR_KIND_FUNC_ENTRY=0, TRACE_LOG_DESCRIPTOR_KIND_FUNC_LEAVE=1, TRACE_LOG_DESCRIPTOR_KIND_EXPLICIT=2 }

user_defined_types: struct trace_param_descriptor { unsigned long flags; char *param_name; union { char *str; char *const_str; char *type_name; }; }


decompiled_code:
void __fastcall relocate_metadata_for_fmt_version(__int64 a1, __int64 a2, __int64 a3, unsigned int a4, int a5, unsigned int a6)
{
__int64 v10; // rax
_QWORD *v11; // r8
__int64 v12; // r10
int v13; // er9
__int64 v14; // r8
__int64 v15; // r8
__int64 v16; // rcx
_QWORD *v17; // rcx
__int64 v18; // rcx
__int64 v19; // r10

v10 = get_log_descriptor_size(a6);
v11 = (_QWORD *)(a3 + 8);
v12 = v10 * a4;
if ( a4 )
{
do
{
relocate_ptr_isra_0(a1, a2, v11);
if ( a6 > 0xA1 )
{
relocate_ptr_isra_0(a1, a2, (_QWORD *)(v14 + 8));
relocate_ptr_isra_0(a1, a2, (_QWORD *)(v15 + 16));
}
relocate_descriptor_parameters_isra_0(a1, a2, *(_QWORD **)v14);
}
while ( a4 != v13 );
}
if ( a5 )
{
v16 = a3 + v12 + 16;
do
{
relocate_ptr_isra_0(a1, a2, (_QWORD *)(v16 - 8));
relocate_ptr_isra_0(a1, a2, v17);
relocate_type_definition_params_isra_0(a1, a2, *(_QWORD **)(v18 - 24));
}
while ( v16 != v19 );
}
}

original_code:
void relocate_metadata_for_fmt_version(
  const void *original_base_address,
  const void *new_base_address,
  char *data,
  unsigned int descriptor_count,
  unsigned int type_count,
  unsigned int fmt_version)
{
    unsigned int i;
    ((void) sizeof ((sizeof(unsigned long long) >= sizeof(void *)) ? 1 : 0), __extension__ ({ if (sizeof(unsigned long long) >= sizeof(void *)) ; else __assert_fail ("sizeof(unsigned long long) >= sizeof(void *)", "trace_metadata_util.c", 89, __extension__ __PRETTY_FUNCTION__); }));
    size_t log_descriptor_size = get_log_descriptor_size(fmt_version);
    struct trace_type_definition *type_definitions = (struct trace_type_definition *) (data + log_descriptor_size * descriptor_count);
    for (i = 0; i < descriptor_count; i++) {
     struct trace_log_descriptor *log_descr = (struct trace_log_descriptor *) (data + log_descriptor_size * i);
        relocate_ptr((unsigned long long)original_base_address, (unsigned long long)new_base_address, (unsigned long long *) &log_descr->params);
        if (fmt_version >= (0xA2)) {
         relocate_ptr((unsigned long long)original_base_address, (unsigned long long)new_base_address, (unsigned long long *) &log_descr->file);
         relocate_ptr((unsigned long long)original_base_address, (unsigned long long)new_base_address, (unsigned long long *) &log_descr->function);
        }
        relocate_descriptor_parameters(original_base_address, new_base_address, log_descr);
    }
    for (i = 0; i < type_count; i++) {
        relocate_ptr((unsigned long long)original_base_address, (unsigned long long)new_base_address, (unsigned long long *) &type_definitions[i].type_name);
        relocate_ptr((unsigned long long)original_base_address, (unsigned long long)new_base_address, (unsigned long long *) &type_definitions[i].params);
        relocate_type_definition_params(original_base_address, new_base_address, &type_definitions[i]);
    }
}

canonical_original_code:
void relocate_metadata_for_fmt_version(void *original_base_address, void *new_base_address, char *data, unsigned int descriptor_count, unsigned int type_count, unsigned int fmt_version)
{
    unsigned int i;
    ((void) sizeof ((sizeof(unsigned long long) >= sizeof(void *)) ? 1 : 0), __extension__ ({ if (sizeof(unsigned long long) >= sizeof(void *)) ; else __assert_fail ("sizeof(unsigned long long) >= sizeof(void *)", "trace_metadata_util.c", 89, __extension__ __PRETTY_FUNCTION__); }));
    unsigned long log_descriptor_size = get_log_descriptor_size(fmt_version);
    struct trace_type_definition *type_definitions = (struct trace_type_definition *) (data + log_descriptor_size * descriptor_count);
    for (i = 0; i < descriptor_count; i++) {
     struct trace_log_descriptor *log_descr = (struct trace_log_descriptor *) (data + log_descriptor_size * i);
        relocate_ptr((unsigned long long)original_base_address, (unsigned long long)new_base_address, (unsigned long long *) &log_descr->params);
        if (fmt_version >= (0xA2)) {
         relocate_ptr((unsigned long long)original_base_address, (unsigned long long)new_base_address, (unsigned long long *) &log_descr->file);
         relocate_ptr((unsigned long long)original_base_address, (unsigned long long)new_base_address, (unsigned long long *) &log_descr->function);
        }
        relocate_descriptor_parameters(original_base_address, new_base_address, log_descr);
    }
    for (i = 0; i < type_count; i++) {
        relocate_ptr((unsigned long long)original_base_address, (unsigned long long)new_base_address, (unsigned long long *) &type_definitions[i].type_name);
        relocate_ptr((unsigned long long)original_base_address, (unsigned long long)new_base_address, (unsigned long long *) &type_definitions[i].params);
        relocate_type_definition_params(original_base_address, new_base_address, &type_definitions[i]);
    }
}

variable_types:
data
char *

variable_types:
i
unsigned int

variable_types: struct trace_type_definition { enum trace_type_id { TRACE_TYPE_ID_ENUM=1, TRACE_TYPE_ID_RECORD=2, TRACE_TYPE_ID_TYPEDEF=3 } type_id; char *type_name; union { void *params; struct trace_enum_value { char *name; unsigned int value; } *enum_values; }; } *

variable_types: struct trace_log_descriptor { enum trace_log_descriptor_kind { TRACE_LOG_DESCRIPTOR_KIND_FUNC_ENTRY=0, TRACE_LOG_DESCRIPTOR_KIND_FUNC_LEAVE=1, TRACE_LOG_DESCRIPTOR_KIND_EXPLICIT=2 } kind; unsigned int line; unsigned int severity; struct trace_param_descriptor { unsigned long flags; char *param_name; union { char *str; char *const_str; char *type_name; }; } *params; char *file; char *function; } *

user_defined_types: struct trace_type_definition { enum trace_type_id type_id; char *type_name; union { void *params; struct trace_enum_value *enum_values; }; }


decompiled_code:
__int64 __fastcall relocate_metadata(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
return relocate_metadata_for_fmt_version(a1, a2, a3, a4, a5, 163LL);
}

original_code:
void relocate_metadata(const void *original_base_address, const void *new_base_address, char *data, unsigned int descriptor_count, unsigned int type_count)
{
 relocate_metadata_for_fmt_version(original_base_address,new_base_address, data, descriptor_count, type_count, (0xA3));
}

canonical_original_code:
void relocate_metadata(void *original_base_address, void *new_base_address, char *data, unsigned int descriptor_count, unsigned int type_count)
{
 relocate_metadata_for_fmt_version(original_base_address,new_base_address, data, descriptor_count, type_count, (0xA3));
}


halt

decompiled_code:
void __fastcall __noreturn halt(const char *a1, const char *a2, unsigned int a3)
{
unsigned int v4; // eax
unsigned int v5; // eax

v4 = getpid();
fprintf(stderr, "HALT!!!!! In (%d) %s(%s:%d)", v4, a1, a2, a3);
while ( 1 )
{
v5 = getpid();
syslog(10, "TRACE CRITICAL ERROR! Halted a thread in pid %d %s(%s:%d)", v5, a1, a2, a3);
sleep(0x12Cu);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func12(const char *a1, const char *a2, unsigned int a3)
{
unsigned int v4; // eax
unsigned int v5; // eax

v4 = getpid();
fprintf(stderr, "HALT!!!!! In (%d) %s(%s:%d)", v4, a1, a2, a3);
while ( 1 )
{
v5 = getpid();
syslog(10, "TRACE CRITICAL ERROR! Halted a thread in pid %d %s(%s:%d)", v5, a1, a2, a3);
sleep(0x12Cu);
}
}

original_code:
void halt(const char *filename, const char *function, int lineno)
{
    fprintf(stderr, "HALT!!!!! In (%d) %s(%s:%d)", getpid(), filename, function, lineno);
    while (1) {
     syslog((1<<3)|2, "TRACE CRITICAL ERROR! Halted a thread in pid %d %s(%s:%d)", getpid(), filename, function, lineno);
        sleep(300);
    }
}

canonical_original_code:
void halt(char *filename, char *function, int lineno)
{
    fprintf(stderr, "HALT!!!!! In (%d) %s(%s:%d)", getpid(), filename, function, lineno);
    while (1) {
     syslog((1<<3)|2, "TRACE CRITICAL ERROR! Halted a thread in pid %d %s(%s:%d)", getpid(), filename, function, lineno);
        sleep(300);
    }
}

variable_types:
filename
char *

variable_types:
function
char *

variable_types: lineno int


decompiled_code:
__int64 __fastcall print_end_diagnostics(int a1)
{
__int64 result; // rax
int *v2; // rax
char *v3; // rax

if ( a1 < 0 )
{
v2 = __errno_location();
v3 = strerror(*v2);
fprintf(stderr, "Error reading file: %s\n", v3);
result = 74LL;
}
else if ( a1 <= 0 || a1 == 64 )
{
result = 0LL;
}
else
{
fprintf(stderr, "The file ends with an incomplete record with %d bytes\n", (unsigned int)a1);
result = 65LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
__int64 result; // rax
int *v2; // rax
char *v3; // rax

if ( a1 < 0 )
{
v2 = __errno_location();
v3 = strerror(*v2);
fprintf(stderr, "Error reading file: %s\n", v3);
result = 74LL;
}
else if ( a1 <= 0 || a1 == 64 )
{
result = 0LL;
}
else
{
fprintf(stderr, "The file ends with an incomplete record with %d bytes\n", (unsigned int)a1);
result = 65LL;
}
return result;
}

original_code:
static int print_end_diagnostics(int last_read_bytes_num)
{
 if (last_read_bytes_num < 0) {
  fprintf(stderr, "Error reading file: %s\n", strerror((*__errno_location ())));
  return 74;
 }
 if ((last_read_bytes_num > 0) && (last_read_bytes_num != 64U)) {
  fprintf(stderr, "The file ends with an incomplete record with %d bytes\n", last_read_bytes_num);
  return 65;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall parse_options(int argc, char **argv)
{
unsigned int v2; // eax
__int64 result; // rax
int longind; // [rsp+Ch] [rbp-4Ch] BYREF
char shortopts[72]; // [rsp+10h] [rbp-48h] BYREF

short_opts_from_long_opts(shortopts, &long_opts_2);
while ( 2 )
{
v2 = getopt_long(argc, argv, shortopts, &long_opts_2, &longind);
if ( v2 == -1 )
{
filenames = &argv[optind];
result = 0LL;
if ( !*filenames )
{
fprintf(stderr, "%s: No filenames specified. Usage:\n", *argv);
fprintf(
stderr,
"%s [options] [files]\n"
"Available options:\n"
"-h print this text\n"
"-d show duplicate records (A timestamp recurring in the same thread)\n"
"-o show out-of-order records (when a record has an earlier timestamp than one appearing earlier in the file"
"\n"
"-b n begin at record n\n"
"-e n end at record n\n",
*argv);
result = 64LL;
}
}
else
{
switch ( v2 )
{
case 'b':
record_range[0] = strtoul(optarg, 0LL, 10);
continue;
case 'd':
show_duplicates = 1;
continue;
case 'e':
record_range[1] = strtoul(optarg, 0LL, 10);
continue;
case 'h':
printf(
"%s [options] [files]\n"
"Available options:\n"
"-h print this text\n"
"-d show duplicate records (A timestamp recurring in the same thread)\n"
"-o show out-of-order records (when a record has an earlier timestamp than one appearing earlier in the fi"
"le\n"
"-b n begin at record n\n"
"-e n end at record n\n",
*argv);
result = 1LL;
break;
case 'o':
show_out_of_order = 1;
continue;
default:
fprintf(stderr, "Unrecognized option %c\n", v2);
result = 64LL;
break;
}
}
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int argc, char **argv)
{
unsigned int v2; // eax
__int64 result; // rax
int longind; // [rsp+Ch] [rbp-4Ch] BYREF
char shortopts[72]; // [rsp+10h] [rbp-48h] BYREF

func11(shortopts, &long_opts_2);
while ( 2 )
{
v2 = getopt_long(argc, argv, shortopts, &long_opts_2, &longind);
if ( v2 == -1 )
{
filenames = &argv[optind];
result = 0LL;
if ( !*filenames )
{
fprintf(stderr, "%s: No filenames specified. Usage:\n", *argv);
fprintf(
stderr,
"%s [options] [files]\n"
"Available options:\n"
"-h print this text\n"
"-d show duplicate records (A timestamp recurring in the same thread)\n"
"-o show out-of-order records (when a record has an earlier timestamp than one appearing earlier in the file"
"\n"
"-b n begin at record n\n"
"-e n end at record n\n",
*argv);
result = 64LL;
}
}
else
{
switch ( v2 )
{
case 'b':
record_range[0] = strtoul(optarg, 0LL, 10);
continue;
case 'd':
show_duplicates = 1;
continue;
case 'e':
record_range[1] = strtoul(optarg, 0LL, 10);
continue;
case 'h':
printf(
"%s [options] [files]\n"
"Available options:\n"
"-h print this text\n"
"-d show duplicate records (A timestamp recurring in the same thread)\n"
"-o show out-of-order records (when a record has an earlier timestamp than one appearing earlier in the fi"
"le\n"
"-b n begin at record n\n"
"-e n end at record n\n",
*argv);
result = 1LL;
break;
case 'o':
show_out_of_order = 1;
continue;
default:
fprintf(stderr, "Unrecognized option %c\n", v2);
result = 64LL;
break;
}
}
break;
}
return result;
}

original_code:
static int parse_options(int argc, const char *argv[])
{
 static const struct option long_opts[] = {
     { "help", 0, 0, 'h'},
  { "duplicate", 0, 0, 'd'},
  { "out-of-order", 0, 0, 'o'},
  { "begin", 1, 0, 'b'},
  { "end", 1, 0, 'e'},
  { 0, 0, 0, 0}
 };
    int o;
    int longindex;
 char short_opts[(3*(sizeof(long_opts) / sizeof(long_opts[0])) + 1)];
 short_opts_from_long_opts(short_opts, long_opts);
 static const char help_text[] =
   "%s [options] [files]\n"
   "Available options:\n"
   "-h    print this text\n"
   "-d    show duplicate records (A timestamp recurring in the same thread)\n"
   "-o    show out-of-order records (when a record has an earlier timestamp than one appearing earlier in the file\n"
   "-b n  begin at record n\n"
   "-e n  end at record n\n";
 while ((o = getopt_long(argc, (char *const *)argv, short_opts, long_opts, &longindex)) != (-1)) {
  switch (o) {
  case 'h':
   printf(help_text, argv[0]);
   return 1;
  case 'd':
   show_duplicates = (1);
   break;
  case 'o':
   show_out_of_order = (1);
   break;
  case 'b':
   record_range[0] = strtoul(optarg, ((void *)0), 10);
   break;
  case 'e':
   record_range[1] = strtoul(optarg, ((void *)0), 10);
   break;
  default:
   fprintf(stderr, "Unrecognized option %c\n", o);
   return 64;
  }
 }
 filenames = argv + optind;
 if (((void *)0) == *filenames) {
  fprintf(stderr, "%s: No filenames specified. Usage:\n", argv[0]);
  fprintf(stderr, help_text, argv[0]);
  return 64;
 }
 return 0;
}

canonical_original_code:
static int parse_options(int argc, char *argv[])
{
 static struct option long_opts[] = {
     { "help", 0, 0, 'h'},
  { "duplicate", 0, 0, 'd'},
  { "out-of-order", 0, 0, 'o'},
  { "begin", 1, 0, 'b'},
  { "end", 1, 0, 'e'},
  { 0, 0, 0, 0}
 };
    int o;
    int longindex;
 char short_opts[-1];
 short_opts_from_long_opts(short_opts, long_opts);
 static char help_text[] = "%s [options] [files]\n"
   "Available options:\n"
   "-h    print this text\n"
   "-d    show duplicate records (A timestamp recurring in the same thread)\n"
   "-o    show out-of-order records (when a record has an earlier timestamp than one appearing earlier in the file\n"
   "-b n  begin at record n\n"
   "-e n  end at record n\n";
 while ((o = getopt_long(argc, (char **)argv, short_opts, long_opts, &longindex)) != (-1)) {
  switch (o) {
  case 'h':
   printf(help_text, argv[0]);
   return 1;
  case 'd':
   show_duplicates = (1);
   break;
  case 'o':
   show_out_of_order = (1);
   break;
  case 'b':
   record_range[0] = strtoul(optarg, ((void *)0), 10);
   break;
  case 'e':
   record_range[1] = strtoul(optarg, ((void *)0), 10);
   break;
  default:
   fprintf(stderr, "Unrecognized option %c\n", o);
   return 64;
  }
 }
 filenames = argv + optind;
 if (((void *)0) == *filenames) {
  fprintf(stderr, "%s: No filenames specified. Usage:\n", argv[0]);
  fprintf(stderr, help_text, argv[0]);
  return 64;
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

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

variable_types: o int

variable_types: longindex int


decompiled_code:
int __fastcall validate_timestamp(__int64 *a1)
{
char v1; // cl
char *v2; // rax
__int64 v3; // r9
int v5; // [rsp-10h] [rbp-18h]

v1 = *((_BYTE *)a1 + 15);
LODWORD(v2) = v1 & 0xF0;
if ( (v1 & 0xF0) != 16 )
__assert_fail("TRACE_REC_TYPE_TYPED == rec->rec_type", "tools/dump_file_diags.c", 0x6Au, "validate_timestamp");
if ( (*((_BYTE *)a1 + 14) & 2) != 0 )
{
v3 = *a1;
v2 = (char *)&thread_access_info + 16 * *((unsigned __int16 *)a1 + 5);
if ( (unsigned __int64)*a1 > *(_QWORD *)v2 )
{
*(_QWORD *)v2 = v3;
*((_DWORD *)v2 + 2) = record_num;
}
else if ( *a1 == *(_QWORD *)v2 )
{
if ( show_duplicates )
LODWORD(v2) = printf(
"n=%lu: \t\tDuplicate record with sev=%u for thread %u last accessed n=%u, with ts=%llu \n",
*(_QWORD *)&record_num,
v1 & 0xF,
*((unsigned __int16 *)a1 + 5),
*((unsigned int *)v2 + 2),
v3);
}
else if ( show_out_of_order )
{
printf(
"n=%lu: \t\tOut of order record with sev=%u for thread %u last accessed n=%u, with ts=%llu < %llu\n",
*(_QWORD *)&record_num,
v1 & 0xF,
*((unsigned __int16 *)a1 + 5),
*((unsigned int *)v2 + 2),
v3,
*(_QWORD *)v2);
LODWORD(v2) = v5;
}
}
return (int)v2;
}

canonical_decompiled_code:
int __fastcall func9(__int64 *a1)
{
char v1; // cl
char *v2; // rax
__int64 v3; // r9
int v5; // [rsp-10h] [rbp-18h]

v1 = *((_BYTE *)a1 + 15);
LODWORD(v2) = v1 & 0xF0;
if ( (v1 & 0xF0) != 16 )
__assert_fail("TRACE_REC_TYPE_TYPED == rec->rec_type", "tools/dump_file_diags.c", 0x6Au, "validate_timestamp");
if ( (*((_BYTE *)a1 + 14) & 2) != 0 )
{
v3 = *a1;
v2 = (char *)&thread_access_info + 16 * *((unsigned __int16 *)a1 + 5);
if ( (unsigned __int64)*a1 > *(_QWORD *)v2 )
{
*(_QWORD *)v2 = v3;
*((_DWORD *)v2 + 2) = record_num;
}
else if ( *a1 == *(_QWORD *)v2 )
{
if ( show_duplicates )
LODWORD(v2) = printf(
"n=%lu: \t\tDuplicate record with sev=%u for thread %u last accessed n=%u, with ts=%llu \n",
*(_QWORD *)&record_num,
v1 & 0xF,
*((unsigned __int16 *)a1 + 5),
*((unsigned int *)v2 + 2),
v3);
}
else if ( show_out_of_order )
{
printf(
"n=%lu: \t\tOut of order record with sev=%u for thread %u last accessed n=%u, with ts=%llu < %llu\n",
*(_QWORD *)&record_num,
v1 & 0xF,
*((unsigned __int16 *)a1 + 5),
*((unsigned int *)v2 + 2),
v3,
*(_QWORD *)v2);
LODWORD(v2) = v5;
}
}
return (int)v2;
}

original_code:
static void validate_timestamp(const struct trace_record *rec)
{
 ((void) sizeof ((TRACE_REC_TYPE_TYPED == rec->rec_type) ? 1 : 0), __extension__ ({ if (TRACE_REC_TYPE_TYPED == rec->rec_type) ; else __assert_fail ("TRACE_REC_TYPE_TYPED == rec->rec_type", "tools/dump_file_diags.c", 106, __extension__ __PRETTY_FUNCTION__); }));
 trace_pid_t tid = rec->tid;
 if (0 == (rec->termination & TRACE_TERMINATION_FIRST)) {
  return;
 }
 if (rec->ts <= thread_access_info[tid].last_ts) {
  if (rec->ts == thread_access_info[tid].last_ts) {
   if (show_duplicates)
    printf("n=%lu: \t\tDuplicate record with sev=%u for thread %u last accessed n=%u, with ts=%llu \n", record_num, rec->severity, tid, thread_access_info[tid].last_n, rec->ts);
  }
  else if (show_out_of_order) {
   printf("n=%lu: \t\tOut of order record with sev=%u for thread %u last accessed n=%u, with ts=%llu < %llu\n", record_num, rec->severity, tid, thread_access_info[tid].last_n, rec->ts, thread_access_info[tid].last_ts);
  }
 }
 else {
  thread_access_info[tid].last_ts = rec->ts;
  thread_access_info[tid].last_n = record_num;
 }
}

canonical_original_code:
static void validate_timestamp(struct trace_record *rec)
{
 ((void) sizeof ((TRACE_REC_TYPE_TYPED == rec->rec_type) ? 1 : 0), __extension__ ({ if (TRACE_REC_TYPE_TYPED == rec->rec_type) ; else __assert_fail ("TRACE_REC_TYPE_TYPED == rec->rec_type", "tools/dump_file_diags.c", 106, __extension__ __PRETTY_FUNCTION__); }));
 unsigned short tid = rec->tid;
 if (0 == (rec->termination & TRACE_TERMINATION_FIRST)) {
  return;
 }
 if (rec->ts <= thread_access_info[tid].last_ts) {
  if (rec->ts == thread_access_info[tid].last_ts) {
   if (show_duplicates)
    printf("n=%lu: \t\tDuplicate record with sev=%u for thread %u last accessed n=%u, with ts=%llu \n", record_num, rec->severity, tid, thread_access_info[tid].last_n, rec->ts);
  }
  else if (show_out_of_order) {
   printf("n=%lu: \t\tOut of order record with sev=%u for thread %u last accessed n=%u, with ts=%llu < %llu\n", record_num, rec->severity, tid, thread_access_info[tid].last_n, rec->ts, thread_access_info[tid].last_ts);
  }
 }
 else {
  thread_access_info[tid].last_ts = rec->ts;
  thread_access_info[tid].last_n = record_num;
 }
}

variable_types: struct trace_record { unsigned long long ts; unsigned short pid; unsigned short tid; short nesting; short bit_fields[]; unsigned int termination; unsigned int module_id; unsigned int reserved; unsigned int severity; unsigned int rec_type; unsigned int generation; union trace_record_u { unsigned char payload[44]; struct trace_record_typed { unsigned int log_id; unsigned char payload[]; } typed; struct trace_record_file_header { char machine_id[24]; unsigned short format_version; unsigned short flags; unsigned char reserved[4]; unsigned int magic; } file_header; struct trace_record_metadata { unsigned int metadata_size_bytes; unsigned short dead_pids[4]; unsigned char reserved[-1]; unsigned int metadata_magic; } metadata; struct trace_record_dump_header { unsigned int prev_dump_offset; unsigned int total_dump_size; unsigned int first_chunk_offset; unsigned long records_previously_discarded; } dump_header; struct trace_record_buffer_dump { unsigned int last_metadata_offset; unsigned int prev_chunk_offset; unsigned int dump_header_offset; unsigned long long ts; unsigned int records; unsigned int severity_type; unsigned int lost_records; unsigned short flags; unsigned char slack_bytes; } buffer_chunk; struct trace_record_data_loss { unsigned int lost_records; unsigned long long ts_start; unsigned long long ts_end; } data_loss; } u; } *

variable_types:
tid
unsigned short