decompiled_code:
__int64 __fastcall trace_get_current_exec_basename(char *dest, size_t n)
{
int v2; // eax
unsigned int v3; // edx
char *v4; // r12
char *v5; // rax
__int64 v7[2]; // [rsp+0h] [rbp-1018h] BYREF
char v8[4080]; // [rsp+10h] [rbp-1008h] BYREF

v7[0] = 0LL;
v7[1] = 0LL;
memset(v8, 0, sizeof(v8));
v2 = readlink("/proc/self/exe", (char *)v7, 0xFFFuLL);
v3 = v2;
if ( v2 >= 0 )
{
*((_BYTE *)v7 + v2) = 0;
v4 = &dest[n - 1];
*v4 = 0;
v5 = basename((const char *)v7);
strncpy(dest, v5, n);
if ( *v4 )
{
*_errno_location() = 36;
v3 = -1;
}
else
{
v3 = 0;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *dest, size_t n)
{
int v2; // eax
unsigned int v3; // edx
char *v4; // r12
char *v5; // rax
__int64 v7[2]; // [rsp+0h] [rbp-1018h] BYREF
char v8[4080]; // [rsp+10h] [rbp-1008h] BYREF

v7[0] = 0LL;
v7[1] = 0LL;
memset(v8, 0, sizeof(v8));
v2 = readlink("/proc/self/exe", (char *)v7, 0xFFFuLL);
v3 = v2;
if ( v2 >= 0 )
{
*((_BYTE *)v7 + v2) = 0;
v4 = &dest[n - 1];
*v4 = 0;
v5 = basename((const char *)v7);
strncpy(dest, v5, n);
if ( *v4 )
{
*_errno_location() = 36;
v3 = -1;
}
else
{
v3 = 0;
}
}
return v3;
}

original_code:
int trace_get_current_exec_basename(char *exec_name, size_t exec_name_size)
{
    char exec_path[4096] = "";
    int rc = readlink("/proc/self/exe", exec_path, sizeof(exec_path) - 1);
    if (rc < 0) {
        return rc;
    }
    exec_path[rc] = '\0';
    exec_name[exec_name_size - 1] = '\0';
    strncpy(exec_name, basename(exec_path), exec_name_size);
    if ('\0' != exec_name[exec_name_size - 1]) {
        (*__errno_location ()) = 36;
        return -1;
    }
    return 0;
}

canonical_original_code:
int trace_get_current_exec_basename(char *exec_name, unsigned long exec_name_size)
{
    char exec_path[4096] = "";
    int rc = readlink("/proc/self/exe", exec_path, sizeof(exec_path) - 1);
    if (rc < 0) {
        return rc;
    }
    exec_path[rc] = '\0';
    exec_name[exec_name_size - 1] = '\0';
    strncpy(exec_name, basename(exec_path), exec_name_size);
    if ('\0' != exec_name[exec_name_size - 1]) {
        (*__errno_location ()) = 36;
        return -1;
    }
    return 0;
}


repo: Infinidat/infinitrace

decompiled_code:
__int64 __fastcall trace_get_fd_path(unsigned int a1, char *a2, size_t a3)
{
ssize_t v4; // rax
__int64 result; // rax
char v6[72]; // [rsp+0h] [rbp-48h] BYREF

sprintf(v6, "/proc/self/fd/%d", a1);
v4 = readlink(v6, a2, a3);
if ( v4 < 0 )
return 0xFFFFFFFFLL;
if ( v4 >= a3 )
{
a2[a3 - 1] = 0;
*_errno_location() = 36;
result = 0xFFFFFFFFLL;
}
else
{
a2[v4] = 0;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, char *a2, size_t a3)
{
ssize_t v4; // rax
__int64 result; // rax
char v6[72]; // [rsp+0h] [rbp-48h] BYREF

sprintf(v6, "/proc/self/fd/%d", a1);
v4 = readlink(v6, a2, a3);
if ( v4 < 0 )
return 0xFFFFFFFFLL;
if ( v4 >= a3 )
{
a2[a3 - 1] = 0;
*_errno_location() = 36;
result = 0xFFFFFFFFLL;
}
else
{
a2[v4] = 0;
result = 0LL;
}
return result;
}

original_code:
int trace_get_fd_path(int fd, char *file_path, size_t file_path_buf_size)
{
    char fd_proc_path[40];
    sprintf(fd_proc_path, "/proc/self/fd/%d", fd);
    const ssize_t path_len = readlink(fd_proc_path, file_path, file_path_buf_size);
    if (path_len < 0) {
        return -1;
    }
    if ((size_t) path_len >= file_path_buf_size) {
        file_path[file_path_buf_size - 1] = '\0';
        (*__errno_location ()) = 36;
        return -1;
    }
    file_path[path_len] = '\0';
    return 0;
}

canonical_original_code:
int trace_get_fd_path(int fd, char *file_path, unsigned long file_path_buf_size)
{
    char fd_proc_path[40];
    sprintf(fd_proc_path, "/proc/self/fd/%d", fd);
    long path_len = readlink(fd_proc_path, file_path, file_path_buf_size);
    if (path_len < 0) {
        return -1;
    }
    if ((unsigned long) path_len >= file_path_buf_size) {
        file_path[file_path_buf_size - 1] = '\0';
        (*__errno_location ()) = 36;
        return -1;
    }
    file_path[path_len] = '\0';
    return 0;
}

variable_types: fd int

return_type: int


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

v1 = _errno_location();
v2 = *v1;
if ( !kill(pid, 0) )
return 1LL;
result = (unsigned int)*v1;
switch ( (_DWORD)result )
{
case 1:
*v1 = v2;
break;
case 3:
*v1 = 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; // rbx
int v2; // er12
__int64 result; // rax

v1 = _errno_location();
v2 = *v1;
if ( !kill(pid, 0) )
return 1LL;
result = (unsigned int)*v1;
switch ( (_DWORD)result )
{
case 1:
*v1 = v2;
break;
case 3:
*v1 = 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);
}

variable_types: pid int


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:
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

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; // edx
__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; // edx
__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[1] = a2 % 0x3B9ACA00;
*a1 = 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[1] = a2 % 0x3B9ACA00;
*a1 = 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; // ebx
__int64 v4; // r8
char v5; // r12
unsigned int v6; // ebx
char *v7; // rax
unsigned int v8; // er13
char *v9; // rax
char *v10; // rax
__pid_t v12; // eax
int *v13; // rbx
char *v14; // rax
__int64 v15; // rcx
__int64 v16; // r8
int v17; // eax
size_t v18; // rax
void *v19; // rsp
size_t v20; // rax
void *v21; // rsp
int *v22; // r14
unsigned int v23; // ebx
char *v24; // r15
unsigned int v25; // eax
char *v26; // r12
char *v27; // rax
int *v28; // rax
__int64 v29; // rcx
__int64 v30; // r8
char *v31; // rax
unsigned int v32; // er12
char *v33; // rax
int stat_loc[2]; // [rsp+0h] [rbp-50h] BYREF
void *v35; // [rsp+8h] [rbp-48h]
__int64 v36; // [rsp+10h] [rbp-40h]

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

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

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

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 func2(__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)
{
unsigned __int64 v2; // rbx
unsigned __int64 v3; // rax

v2 = (unsigned __int64)*a1 << 6;
v3 = internal_buf_bytes_to_recs(a2) << 6;
if ( v3 - 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((char *)a1 + v2 + a2 - v3 + 64, 105, v3 - a2);
}

canonical_decompiled_code:
void *__fastcall func3(unsigned int *a1, __int64 a2)
{
unsigned __int64 v2; // rbx
unsigned __int64 v3; // rax

v2 = (unsigned __int64)*a1 << 6;
v3 = func0(a2) << 6;
if ( v3 - 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((char *)a1 + v2 + a2 - v3 + 64, 105, v3 - 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 func4(__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
unsigned int v6; // ecx
__int64 result; // rax

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

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

v3 = func4(a1);
v4 = *(_DWORD *)(a1 + 12);
v5 = (unsigned int)(v3 - v4);
v6 = v4 + a2;
result = (unsigned int)(v4 + a2 - v3);
if ( v5 > a2 )
result = v6;
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; // er12
__int64 result; // rax
char *v8; // rax
int *v9; // rbx
char *v10; // rax
_BYTE v11[24]; // [rsp+0h] [rbp-58h] BYREF
__int64 v12[8]; // [rsp+18h] [rbp-40h] BYREF

if ( !a2 )
{
*_errno_location() = 14;
return -1LL;
}
if ( a3 <= 0 )
return 0LL;
v4 = *a2;
v5 = (size_t)a2[1];
v12[0] = 0LL;
v6 = snappy_init_env(v11);
if ( v6 < 0 )
{
snappy_free_env(v11);
if ( v4 == *a2 )
goto LABEL_20;
goto LABEL_8;
}
if ( a3 <= 1
|| (v5 = total_iovec_len(a2, (unsigned int)a3), v8 = (char *)malloc(v5), (v4 = v8) != 0LL)
&& copy_iov_to_buffer(v8, a2, (unsigned int)a3) == v5 )
{
if ( v5 )
{
v5 -= trace_r_count_chr_occurrences(v4 + 1, v5 - 1, 165LL);
v6 = snappy_compress(v11, v4, v5, a1, v12);
}
}
else
{
v6 = -*_errno_location();
}
snappy_free_env(v11);
if ( *a2 != v4 )
LABEL_8:
free(v4);
if ( v6 )
{
LABEL_20:
v9 = _errno_location();
*v9 = -v6;
v10 = strerror(-v6);
ERR("Buffer compression failed with err", (unsigned int)*v9, v10);
return -1LL;
}
result = v12[0];
if ( v5 )
{
if ( v12[0] <= 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 func6(__int64 a1, char **a2, int a3)
{
char *v4; // r15
size_t v5; // r14
int v6; // er12
__int64 result; // rax
char *v8; // rax
int *v9; // rbx
char *v10; // rax
_BYTE v11[24]; // [rsp+0h] [rbp-58h] BYREF
__int64 v12[8]; // [rsp+18h] [rbp-40h] BYREF

if ( !a2 )
{
*_errno_location() = 14;
return -1LL;
}
if ( a3 <= 0 )
return 0LL;
v4 = *a2;
v5 = (size_t)a2[1];
v12[0] = 0LL;
v6 = snappy_init_env(v11);
if ( v6 < 0 )
{
snappy_free_env(v11);
if ( v4 == *a2 )
goto LABEL_20;
goto LABEL_8;
}
if ( a3 <= 1
|| (v5 = total_iovec_len(a2, (unsigned int)a3), v8 = (char *)malloc(v5), (v4 = v8) != 0LL)
&& copy_iov_to_buffer(v8, a2, (unsigned int)a3) == v5 )
{
if ( v5 )
{
v5 -= trace_r_count_chr_occurrences(v4 + 1, v5 - 1, 165LL);
v6 = snappy_compress(v11, v4, v5, a1, v12);
}
}
else
{
v6 = -*_errno_location();
}
snappy_free_env(v11);
if ( *a2 != v4 )
LABEL_8:
free(v4);
if ( v6 )
{
LABEL_20:
v9 = _errno_location();
*v9 = -v6;
v10 = strerror(-v6);
ERR("Buffer compression failed with err", (unsigned int)*v9, v10);
return -1LL;
}
result = v12[0];
if ( v5 )
{
if ( v12[0] <= 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 func7(_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 = func2((__int64)a1);
memcpy(&a1[16 * (unsigned __int64)v5 + 16], a1 + 16, a3 << 6);
a1[5] -= a3;
result = func2((__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)
{
size_t v1; // rbx

if ( !addr )
return 0LL;
v1 = (unsigned int)((*((_DWORD *)addr + 4) + 1) << 6);
if ( trace_round_size_up_to_page_size(v1) != 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, v1) != 0);
}

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

if ( !addr )
return 0LL;
v1 = (unsigned int)((*((_DWORD *)addr + 4) + 1) << 6);
if ( trace_round_size_up_to_page_size(v1) != 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, v1) != 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 v3; // edx

result = *a1;
v3 = a1[3];
if ( (unsigned int)result >= v3 )
{
if ( (_DWORD)result == -1 )
*_errno_location() = 12;
result = *a1;
if ( a1[4] >= a2 + result )
{
*a1 = result + a2;
goto LABEL_11;
}
if ( a1[3] >= a2 )
{
a1[5] = a1[4] - result;
*a1 = a2;
result = 0LL;
goto LABEL_11;
}
goto LABEL_8;
}
if ( v3 < a2 + (unsigned int)result )
{
LABEL_8:
*_errno_location() = 12;
result = 0xFFFFFFFFLL;
goto LABEL_11;
}
*a1 = result + a2;
LABEL_11:
if ( *a1 == a1[3] )
{
if ( a2 )
*a1 = -1;
}
return result;
}

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

result = *a1;
v3 = a1[3];
if ( (unsigned int)result >= v3 )
{
if ( (_DWORD)result == -1 )
*_errno_location() = 12;
result = *a1;
if ( a1[4] >= a2 + result )
{
*a1 = result + a2;
goto LABEL_11;
}
if ( a1[3] >= a2 )
{
a1[5] = a1[4] - result;
*a1 = a2;
result = 0LL;
goto LABEL_11;
}
goto LABEL_8;
}
if ( v3 < a2 + (unsigned int)result )
{
LABEL_8:
*_errno_location() = 12;
result = 0xFFFFFFFFLL;
goto LABEL_11;
}
*a1 = result + a2;
LABEL_11:
if ( *a1 == a1[3] )
{
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:
unsigned int *__fastcall internal_buf_write_recs(unsigned int *a1, const void *a2, unsigned __int64 a3)
{
unsigned int v4; // eax
unsigned int *v5; // rbp

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

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

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

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; // er12
__int64 v9; // rax
__int64 v10; // rbp
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 )
{
compactify_buffer(a1, v8, 0);
v10 = -1LL;
}
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; // er12
__int64 v9; // rax
__int64 v10; // rbp
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 = func6((__int64)&a1[16 * (unsigned __int64)v7 + 16], a2, a3);
v10 = v9;
if ( v9 < 0 )
{
func7(a1, v8, 0);
v10 = -1LL;
}
else
{
v11 = func0(v9);
func7(a1, v8, v11);
func3(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)func4(a1);
result = v1 - v2;
if ( v1 == -1 )
result = func4(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, _QWORD *a2)
{
unsigned int v2; // ebp
unsigned int v3; // er13
unsigned int v4; // eax
_BOOL4 v5; // er14
int v6; // ebp

v2 = internal_buf_num_recs_pending((__int64)a1);
v3 = a1[3];
v4 = effective_capacity_c((__int64)a1);
if ( v3 >= v4 )
_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");
v5 = v4 >= v3 + v2;
if ( v4 >= v3 + v2 )
{
*a2 = 0LL;
a2[1] = 0LL;
if ( !v2 )
return v5;
}
else
{
v2 = v4 - v3;
*a2 = 0LL;
a2[1] = 0LL;
}
*a2 = &a1[16 * (unsigned __int64)(unsigned int)a1[3] + 16];
a2[1] = v2 << 6;
if ( *a1 == -1 )
*a1 = a1[2];
v6 = internal_buf_recs_ahead_of_read_c((__int64)a1, v2);
a1[2] = v6;
if ( index_has_wrapped_around(a1 + 2) )
{
if ( v6 )
_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");
a1[5] = 0;
}
return v5;
}

canonical_decompiled_code:
_BOOL8 __fastcall func15(_DWORD *a1, _QWORD *a2)
{
unsigned int v2; // ebp
unsigned int v3; // er13
unsigned int v4; // eax
_BOOL4 v5; // er14
int v6; // ebp

v2 = func14((__int64)a1);
v3 = a1[3];
v4 = func4((__int64)a1);
if ( v3 >= v4 )
_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");
v5 = v4 >= v3 + v2;
if ( v4 >= v3 + v2 )
{
*a2 = 0LL;
a2[1] = 0LL;
if ( !v2 )
return v5;
}
else
{
v2 = v4 - v3;
*a2 = 0LL;
a2[1] = 0LL;
}
*a2 = &a1[16 * (unsigned __int64)(unsigned int)a1[3] + 16];
a2[1] = v2 << 6;
if ( *a1 == -1 )
*a1 = a1[2];
v6 = func5((__int64)a1, v2);
a1[2] = v6;
if ( func1(a1 + 2) )
{
if ( v6 )
_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");
a1[5] = 0;
}
return v5;
}

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, int *a3)
{
__int64 result; // rax
unsigned int v5; // eax

*a3 = 0;
result = internal_buf_contiguous_pending_read_as_iov(a1, a2);
if ( a2[1] )
++*a3;
if ( !(_DWORD)result )
{
v5 = 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] = (v5 - (a2[1] >> 6)) << 6;
result = internal_buf_recs_ahead_of_read_c((__int64)a1, v5);
a1[2] = result;
++*a3;
}
if ( *a3 > 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, int *a3)
{
__int64 result; // rax
unsigned int v5; // eax

*a3 = 0;
result = func15(a1, a2);
if ( a2[1] )
++*a3;
if ( !(_DWORD)result )
{
v5 = 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] = (v5 - (a2[1] >> 6)) << 6;
result = func5((__int64)a1, v5);
a1[2] = result;
++*a3;
}
if ( *a3 > 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:
__int64 __fastcall relocate_metadata_for_fmt_version(__int64 a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5, unsigned int a6)
{
__int64 v6; // r14
_QWORD *v8; // r12
int v9; // er13
__int64 result; // rax
_QWORD *v11; // r12
__int64 v12; // [rsp+0h] [rbp-58h]
__int64 v15; // [rsp+10h] [rbp-48h]

v6 = a3;
v12 = get_log_descriptor_size(a6);
v15 = v12 * a4;
if ( a4 )
{
v8 = (_QWORD *)(v6 + 8);
v9 = 0;
do
{
relocate_ptr(a1, a2, v8);
if ( a6 > 0xA1 )
{
relocate_ptr(a1, a2, v8 + 1);
relocate_ptr(a1, a2, v8 + 2);
}
relocate_descriptor_parameters(a1, a2, (__int64)(v8 - 1));
++v9;
v8 = (_QWORD *)((char *)v8 + v12);
}
while ( a4 != v9 );
}
result = a5;
if ( a5 )
{
v11 = (_QWORD *)(a3 + v15 + 8);
do
{
relocate_ptr(a1, a2, v11);
relocate_ptr(a1, a2, v11 + 1);
result = relocate_type_definition_params(a1, a2, (__int64)(v11 - 1));
v11 += 3;
}
while ( v11 != (_QWORD *)(v15 + a3 + 24LL * (a5 - 1) + 32) );
}
return result;
}

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
char v4[28]; // [rsp+0h] [rbp-38h] BYREF
int longind[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

short_opts_from_long_opts(v4, &long_opts_2);
while ( 2 )
{
v2 = getopt_long(argc, argv, v4, &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
char v4[28]; // [rsp+0h] [rbp-38h] BYREF
int longind[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

func11(v4, &long_opts_2);
while ( 2 )
{
v2 = getopt_long(argc, argv, v4, &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