decompiled_code:
__int64 __fastcall mplite_print_stats(unsigned int *a1, void (__fastcall *a2)(char *))
{
__int64 result; // rax
char v3[280]; // [rsp-118h] [rbp-118h] BYREF

if ( a1 )
{
if ( a2 )
{
snprintf(v3, 0x100uLL, "Total number of calls to malloc: %u", a1[10]);
a2(v3);
snprintf(v3, 0x100uLL, "Total of all malloc calls - includes internal fragmentation: %u", a1[12]);
a2(v3);
snprintf(v3, 0x100uLL, "Total internal fragmentation: %u", a1[14]);
a2(v3);
snprintf(v3, 0x100uLL, "Current checkout, including internal fragmentation: %u", a1[16]);
a2(v3);
snprintf(v3, 0x100uLL, "Current number of distinct checkouts: %u", a1[17]);
a2(v3);
snprintf(v3, 0x100uLL, "Maximum instantaneous currentOut: %u", a1[18]);
a2(v3);
snprintf(v3, 0x100uLL, "Maximum instantaneous currentCount: %u", a1[19]);
a2(v3);
snprintf(v3, 0x100uLL, "Largest allocation (exclusive of internal frag): %u", a1[20]);
result = ((__int64 (__fastcall *)(char *))a2)(v3);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int *a1, void (__fastcall *a2)(char *))
{
__int64 result; // rax
char v3[280]; // [rsp-118h] [rbp-118h] BYREF

if ( a1 )
{
if ( a2 )
{
snprintf(v3, 0x100uLL, "Total number of calls to malloc: %u", a1[10]);
a2(v3);
snprintf(v3, 0x100uLL, "Total of all malloc calls - includes internal fragmentation: %u", a1[12]);
a2(v3);
snprintf(v3, 0x100uLL, "Total internal fragmentation: %u", a1[14]);
a2(v3);
snprintf(v3, 0x100uLL, "Current checkout, including internal fragmentation: %u", a1[16]);
a2(v3);
snprintf(v3, 0x100uLL, "Current number of distinct checkouts: %u", a1[17]);
a2(v3);
snprintf(v3, 0x100uLL, "Maximum instantaneous currentOut: %u", a1[18]);
a2(v3);
snprintf(v3, 0x100uLL, "Maximum instantaneous currentCount: %u", a1[19]);
a2(v3);
snprintf(v3, 0x100uLL, "Largest allocation (exclusive of internal frag): %u", a1[20]);
result = ((__int64 (__fastcall *)(char *))a2)(v3);
}
}
return result;
}

original_code:
void mplite_print_stats(const mplite_t * const handle,
                                   const mplite_putsfunc_t putsfunc)
{
    if ((handle != ((void *)0)) && (putsfunc != ((void *)0))) {
        char zStats[256];
        snprintf(zStats, sizeof (zStats), "Total number of calls to malloc: %u",
                (unsigned) handle->nAlloc);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Total of all malloc calls - includes "
                "internal fragmentation: %u", (unsigned) handle->totalAlloc);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Total internal fragmentation: %u",
                (unsigned) handle->totalExcess);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Current checkout, including internal "
                "fragmentation: %u", handle->currentOut);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Current number of distinct checkouts: %u",
                handle->currentCount);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Maximum instantaneous currentOut: %u",
                handle->maxOut);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Maximum instantaneous currentCount: %u",
                handle->maxCount);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Largest allocation (exclusive of "
                "internal frag): %u", handle->maxRequest);
        putsfunc(zStats);
    }
}

canonical_original_code:
void mplite_print_stats(struct mplite *handle, int (*putsfunc)(char *stats))
{
    if ((handle != ((void *)0)) && (putsfunc != ((void *)0))) {
        char zStats[256];
        snprintf(zStats, sizeof (zStats), "Total number of calls to malloc: %u",
                (unsigned int) handle->nAlloc);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Total of all malloc calls - includes "
                "internal fragmentation: %u", (unsigned int) handle->totalAlloc);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Total internal fragmentation: %u",
                (unsigned int) handle->totalExcess);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Current checkout, including internal "
                "fragmentation: %u", handle->currentOut);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Current number of distinct checkouts: %u",
                handle->currentCount);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Maximum instantaneous currentOut: %u",
                handle->maxOut);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Maximum instantaneous currentCount: %u",
                handle->maxCount);
        putsfunc(zStats);
        snprintf(zStats, sizeof (zStats), "Largest allocation (exclusive of "
                "internal frag): %u", handle->maxRequest);
        putsfunc(zStats);
    }
}

variable_types:
putsfunc
int (*)(char *stats)

variable_types:
zStats
char [256]


repo: jeftyneg/mempoolite

decompiled_code:
void *__fastcall multithreaded_main(void *a1)
{
unsigned int v1; // eax
const void *v2; // rdx

v1 = mplite_roundup(*((_QWORD *)a1 + 1), *((unsigned int *)a1 + 4));
printf("index: %u requested size: %d round-up size: %d\n", *(_QWORD *)a1, *((unsigned int *)a1 + 4), v1);
while ( 1 )
{
v2 = (const void *)mplite_malloc(*((_QWORD *)a1 + 1), *((unsigned int *)a1 + 4));
if ( !v2 )
break;
printf("index: %u address: %p size: %d\n", *(_QWORD *)a1, v2, *((unsigned int *)a1 + 4));
usleep(0x3E8u);
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
unsigned int v1; // eax
const void *v2; // rdx

v1 = mplite_roundup(*((_QWORD *)a1 + 1), *((unsigned int *)a1 + 4));
printf("index: %u requested size: %d round-up size: %d\n", *(_QWORD *)a1, *((unsigned int *)a1 + 4), v1);
while ( 1 )
{
v2 = (const void *)mplite_malloc(*((_QWORD *)a1 + 1), *((unsigned int *)a1 + 4));
if ( !v2 )
break;
printf("index: %u address: %p size: %d\n", *(_QWORD *)a1, v2, *((unsigned int *)a1 + 4));
usleep(0x3E8u);
}
return 0LL;
}

original_code:
void *
multithreaded_main(void *args)
{
    multithreaded_param_t *param;
    void *buffer;
    int round_size;
    param = (multithreaded_param_t *) args;
    round_size = mplite_roundup(param->pool, param->req_size);
    printf("index: %u requested size: %d round-up size: %d\n", param->index,
        param->req_size, round_size);
    while ((buffer = mplite_malloc(param->pool, param->req_size)) != ((void *)0)) {
        printf("index: %u address: %p size: %d\n", param->index, buffer,
            param->req_size);
        usleep(1000);
    }
    return ((void *)0);
}

canonical_original_code:
void *multithreaded_main(void *args)
{
    struct multithreaded_param *param;
    void *buffer;
    int round_size;
    param = (struct multithreaded_param *) args;
    round_size = mplite_roundup(param->pool, param->req_size);
    printf("index: %u requested size: %d round-up size: %d\n", param->index,
        param->req_size, round_size);
    while ((buffer = mplite_malloc(param->pool, param->req_size)) != ((void *)0)) {
        printf("index: %u address: %p size: %d\n", param->index, buffer,
            param->req_size);
        usleep(1000);
    }
    return ((void *)0);
}

variable_types:
args
void *

variable_types: struct multithreaded_param { unsigned long index; struct mplite { int szAtom; int nBlock; unsigned char *zPool; struct mplite_lock { void *arg; int (*acquire)(void *arg); int (*release)(void *arg); } lock; unsigned long nAlloc; unsigned long totalAlloc; unsigned long totalExcess; unsigned int currentOut; unsigned int currentCount; unsigned int maxOut; unsigned int maxCount; unsigned int maxRequest; int aiFreelist[-1]; unsigned char *aCtrl; } *pool; int req_size; } *

variable_types:
buffer
void *

return_type: void *

user_defined_types: struct multithreaded_param { unsigned long index; struct mplite *pool; int req_size; }

user_defined_types: struct mplite { int szAtom; int nBlock; unsigned char *zPool; struct mplite_lock lock; unsigned long nAlloc; unsigned long totalAlloc; unsigned long totalExcess; unsigned int currentOut; unsigned int currentCount; unsigned int maxOut; unsigned int maxCount; unsigned int maxRequest; int aiFreelist[-1]; unsigned char *aCtrl; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
size_t v4; // r14
void *v5; // r12
__int64 v6; // rdx
const void *v7; // rsi
unsigned __int64 v8; // r15
pthread_t *v9; // r14
pthread_t *v10; // r13
void *v11; // rax
unsigned __int64 v12; // rdx
unsigned __int64 *v13; // rbx
int v14; // eax
unsigned __int64 *v15; // rcx
pthread_t *v16; // rdi
unsigned __int64 i; // rbx
pthread_t v18; // rdi
unsigned __int64 ptra; // [rsp+0h] [rbp-198h]
void *ptr; // [rsp+0h] [rbp-198h]
int v22; // [rsp+14h] [rbp-184h] BYREF
size_t size; // [rsp+18h] [rbp-180h] BYREF
__int64 v24; // [rsp+20h] [rbp-178h] BYREF
unsigned __int64 v25; // [rsp+28h] [rbp-170h] BYREF
__int64 v26[4]; // [rsp+30h] [rbp-168h] BYREF
pthread_mutex_t mutex; // [rsp+50h] [rbp-148h] BYREF
char v28[280]; // [rsp+80h] [rbp-118h] BYREF

pthread_mutex_init(&mutex, 0LL);
do
{
v3 = 1LL;
puts("Memory pool testing using mplite API");
printf("Enter the total memory block size: ");
_isoc99_scanf("%u", &size);
printf("Enter the minimum memory allocation size: ");
_isoc99_scanf("%u", &v24);
printf("Enter the number of threads to run for multi-threaded test: ");
_isoc99_scanf("%u", &v25);
v4 = size;
v5 = malloc(size);
printf("buffer = %p size = %u minimum alloc = %u\n", v5, v4, v24);
mplite_init(v28, v5, (unsigned int)size, (unsigned int)v24, 0LL);
puts("Single-threaded test...");
while ( 1 )
{
v7 = (const void *)mplite_malloc(v28, (unsigned int)v24);
if ( !v7 )
break;
v6 = v3++;
printf("malloc = %p counter = %u\n", v7, v6);
}
v8 = 0LL;
mplite_print_stats(v28, &puts);
puts("Multi-threaded test...");
v26[1] = (__int64)&pthread_mutex_lock;
v26[0] = (__int64)&mutex;
v26[2] = (__int64)&pthread_mutex_unlock;
mplite_init(v28, v5, (unsigned int)size, (unsigned int)v24, v26);
ptra = v25;
v9 = (pthread_t *)malloc(8 * v25);
v10 = v9;
v11 = malloc(24 * ptra);
v12 = ptra;
ptr = v11;
v13 = (unsigned __int64 *)v11;
if ( v12 )
{
do
{
v14 = v24;
*v13 = v8;
v15 = v13;
v16 = v10;
v13[1] = (unsigned __int64)v28;
++v8;
*((_DWORD *)v13 + 4) = v14;
++v10;
v13 += 3;
pthread_create(v16, 0LL, multithreaded_main, v15);
}
while ( v25 > v8 );
if ( v25 )
{
for ( i = 0LL; i < v25; ++i )
{
v18 = v9[i];
pthread_join(v18, 0LL);
}
}
}
mplite_print_stats(v28, &puts);
free(ptr);
free(v9);
free(v5);
printf("Test again? <0:false, non-zero:true>: ");
_isoc99_scanf("%d", &v22);
}
while ( v22 );
pthread_mutex_destroy(&mutex);
return 0;
}

original_code:
int
main()
{
    size_t alloc_counter;
    size_t buffer_size;
    size_t min_alloc;
    size_t num_threads;
    char *large_buffer;
    char *alloc_ret;
    mplite_t mempool;
    pthread_t *threads;
    multithreaded_param_t *threads_param;
    mplite_lock_t pool_lock;
    pthread_mutex_t mutex;
    int test_again;
    int scanf_ret;
    pthread_mutex_init(&mutex, ((void *)0));
    do {
        printf("Memory pool testing using mplite API\n");
        printf("Enter the total memory block size: ");
        scanf_ret = scanf("%u", &buffer_size);
        printf("Enter the minimum memory allocation size: ");
        scanf_ret = scanf("%u", &min_alloc);
        printf("Enter the number of threads to run for multi-threaded test: ");
        scanf_ret = scanf("%u", &num_threads);
        large_buffer = malloc(buffer_size);
        printf("buffer = %p size = %u minimum alloc = %u\n", large_buffer,
            buffer_size, min_alloc);
        mplite_init(&mempool, large_buffer, (const int) buffer_size,
                        (const int) min_alloc, ((void *)0));
        printf("Single-threaded test...\n");
        alloc_counter = 1;
        while ((alloc_ret = mplite_malloc(&mempool, (const int) min_alloc)) != ((void *)0)) {
            printf("malloc = %p counter = %u\n", alloc_ret, alloc_counter);
            alloc_counter++;
        }
        mplite_print_stats(&mempool, puts);
        printf("Multi-threaded test...\n");
        pool_lock.arg = (void *) &mutex;
        pool_lock.acquire = (int (*)(void *))pthread_mutex_lock;
        pool_lock.release = (int (*)(void *))pthread_mutex_unlock;
        mplite_init(&mempool, large_buffer, (const int) buffer_size,
                        (const int) min_alloc, &pool_lock);
        threads = (pthread_t *) malloc(sizeof (*threads) * num_threads);
        threads_param = (multithreaded_param_t *) malloc(sizeof (*threads_param) * num_threads);
        for (alloc_counter = 0; alloc_counter < num_threads; alloc_counter++) {
            threads_param[alloc_counter].index = alloc_counter;
            threads_param[alloc_counter].req_size = (const int) min_alloc;
            threads_param[alloc_counter].pool = &mempool;
            pthread_create(&threads[alloc_counter], ((void *)0), multithreaded_main,
                        &threads_param[alloc_counter]);
        }
        for (alloc_counter = 0; alloc_counter < num_threads; alloc_counter++) {
            pthread_join(threads[alloc_counter], ((void *)0));
        }
        mplite_print_stats(&mempool, puts);
        free(threads_param);
        free(threads);
        free(large_buffer);
        printf("Test again? <0:false, non-zero:true>: ");
        scanf_ret = scanf("%d", &test_again);
    } while (test_again);
    pthread_mutex_destroy(&mutex);
    return 0;
}

canonical_original_code:
int main()
{
    unsigned long alloc_counter;
    unsigned long buffer_size;
    unsigned long min_alloc;
    unsigned long num_threads;
    char *large_buffer;
    char *alloc_ret;
    struct mplite mempool;
    unsigned long *threads;
    struct multithreaded_param *threads_param;
    struct mplite_lock pool_lock;
    union pthread_mutex_t mutex;
    int test_again;
    int scanf_ret;
    pthread_mutex_init(&mutex, ((void *)0));
    do {
        printf("Memory pool testing using mplite API\n");
        printf("Enter the total memory block size: ");
        scanf_ret = scanf("%u", &buffer_size);
        printf("Enter the minimum memory allocation size: ");
        scanf_ret = scanf("%u", &min_alloc);
        printf("Enter the number of threads to run for multi-threaded test: ");
        scanf_ret = scanf("%u", &num_threads);
        large_buffer = malloc(buffer_size);
        printf("buffer = %p size = %u minimum alloc = %u\n", large_buffer,
            buffer_size, min_alloc);
        mplite_init(&mempool, large_buffer, (int) buffer_size,
                        (int) min_alloc, ((void *)0));
        printf("Single-threaded test...\n");
        alloc_counter = 1;
        while ((alloc_ret = mplite_malloc(&mempool, (int) min_alloc)) != ((void *)0)) {
            printf("malloc = %p counter = %u\n", alloc_ret, alloc_counter);
            alloc_counter++;
        }
        mplite_print_stats(&mempool, puts);
        printf("Multi-threaded test...\n");
        pool_lock.arg = (void *) &mutex;
        pool_lock.acquire = (int (*)(void *))pthread_mutex_lock;
        pool_lock.release = (int (*)(void *))pthread_mutex_unlock;
        mplite_init(&mempool, large_buffer, (int) buffer_size,
                        (int) min_alloc, &pool_lock);
        threads = (unsigned long *) malloc(sizeof (*threads) * num_threads);
        threads_param = (struct multithreaded_param *) malloc(sizeof (*threads_param) * num_threads);
        for (alloc_counter = 0; alloc_counter < num_threads; alloc_counter++) {
            threads_param[alloc_counter].index = alloc_counter;
            threads_param[alloc_counter].req_size = (int) min_alloc;
            threads_param[alloc_counter].pool = &mempool;
            pthread_create(&threads[alloc_counter], ((void *)0), multithreaded_main,
                        &threads_param[alloc_counter]);
        }
        for (alloc_counter = 0; alloc_counter < num_threads; alloc_counter++) {
            pthread_join(threads[alloc_counter], ((void *)0));
        }
        mplite_print_stats(&mempool, puts);
        free(threads_param);
        free(threads);
        free(large_buffer);
        printf("Test again? <0:false, non-zero:true>: ");
        scanf_ret = scanf("%d", &test_again);
    } while (test_again);
    pthread_mutex_destroy(&mutex);
    return 0;
}

variable_types: struct mplite { int szAtom; int nBlock; unsigned char *zPool; struct mplite_lock { void *arg; int (*acquire)(void *arg); int (*release)(void *arg); } lock; unsigned long nAlloc; unsigned long totalAlloc; unsigned long totalExcess; unsigned int currentOut; unsigned int currentCount; unsigned int maxOut; unsigned int maxCount; unsigned int maxRequest; int aiFreelist[-1]; unsigned char *aCtrl; }

variable_types:
threads
unsigned long *

variable_types: union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; }

return_type: int

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
int __fastcall set_powersave(int a1)
{
if ( !a1 )
return system("cpufreq-set --min 1.4Ghz --max 1.92Ghz --governor ondemand");
system("cpufreq-set --min 200Mhz --max 200Mhz --governor ondemand");
system("echo 1 > /sys/devices/system/cpu/cpu1/online");
system("echo 1 > /sys/devices/system/cpu/cpu2/online");
return system("echo 1 > /sys/devices/system/cpu/cpu3/online");
}

canonical_decompiled_code:
int __fastcall func7(int a1)
{
if ( !a1 )
return system("cpufreq-set --min 1.4Ghz --max 1.92Ghz --governor ondemand");
system("cpufreq-set --min 200Mhz --max 200Mhz --governor ondemand");
system("echo 1 > /sys/devices/system/cpu/cpu1/online");
system("echo 1 > /sys/devices/system/cpu/cpu2/online");
return system("echo 1 > /sys/devices/system/cpu/cpu3/online");
}

original_code:
void set_powersave(int pws_flag){
 if (pws_flag){
  system("cpufreq-set --min 200Mhz --max 200Mhz --governor ondemand");
  system("echo 1 > /sys/devices/system/cpu/cpu1/online");
  system("echo 1 > /sys/devices/system/cpu/cpu2/online");
  system("echo 1 > /sys/devices/system/cpu/cpu3/online");
 } else{
  system("cpufreq-set --min 1.4Ghz --max 1.92Ghz --governor ondemand");
 }
}

return_type: void


unquote

repo: jelaas/eth-affinity

decompiled_code:
__int64 __fastcall unquote(_BYTE *a1)
{
__int64 result; // rax
_BYTE *v2; // rdi

if ( *a1 == 92 )
{
do
{
result = (unsigned __int8)a1[1];
v2 = a1 + 1;
*(v2 - 1) = result;
if ( !(_BYTE)result )
break;
result = (unsigned __int8)v2[1];
a1 = v2 + 1;
*(a1 - 1) = result;
}
while ( (_BYTE)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
__int64 result; // rax
_BYTE *v2; // rdi

if ( *a1 == 92 )
{
do
{
result = (unsigned __int8)a1[1];
v2 = a1 + 1;
*(v2 - 1) = result;
if ( !(_BYTE)result )
break;
result = (unsigned __int8)v2[1];
a1 = v2 + 1;
*(a1 - 1) = result;
}
while ( (_BYTE)result );
}
return result;
}

original_code:
static void unquote(char *s)
{
  char *p;
  if(*s != '\\')
    return;
  for(p=s;*p;p++)
    *p = *(p+1);
}

variable_types:
s
char *

variable_types:
p
char *


decompiled_code:
__int64 __fastcall find_short_arg(__int64 a1, int a2)
{
__int64 v3; // rdi
__int64 v4; // rbx

v3 = *(_QWORD *)(a1 + 8);
if ( v3 )
{
v4 = 1LL;
do
{
if ( *(_BYTE *)v3 == 45 )
{
if ( *(_BYTE *)(v3 + 1) == 45 )
{
if ( !*(_BYTE *)(v3 + 2) )
return 0xFFFFFFFFLL;
}
else if ( strchr((const char *)v3, a2) )
{
return (unsigned int)v4;
}
}
++v4;
v3 = *(_QWORD *)(a1 + 8 * v4);
}
while ( v3 );
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
__int64 v3; // rdi
__int64 v4; // rbx

v3 = *(_QWORD *)(a1 + 8);
if ( v3 )
{
v4 = 1LL;
do
{
if ( *(_BYTE *)v3 == 45 )
{
if ( *(_BYTE *)(v3 + 1) == 45 )
{
if ( !*(_BYTE *)(v3 + 2) )
return 0xFFFFFFFFLL;
}
else if ( strchr((const char *)v3, a2) )
{
return (unsigned int)v4;
}
}
++v4;
v3 = *(_QWORD *)(a1 + 8 * v4);
}
while ( v3 );
}
return 0xFFFFFFFFLL;
}

original_code:
static int find_short_arg(char **argv, int opt_short)
{
  int i;
  for(i=1;argv[i];i++)
    {
      char *a = argv[i];
      if(*a == '-')
 {
   if(*(a+1) == '-')
     {
       if(*(a+2) == 0)
  return -1;
       continue;
     }
   if(strchr(a, opt_short))
     return i;
 }
    }
  return -1;
}

variable_types:
argv
char **

variable_types: i int

variable_types:
a
char *


decompiled_code:
__int64 __fastcall find_long_arg(__int64 a1, const char *a2)
{
int v2; // eax
__int64 v3; // rbp
__int64 v4; // rbx
size_t v5; // r12
unsigned int v6; // er13
char v7; // al
size_t v9; // [rsp+8h] [rbp-40h]

v2 = strlen(a2);
v3 = *(_QWORD *)(a1 + 8);
if ( !v3 )
return (unsigned int)-1;
v4 = 1LL;
v5 = v2;
v9 = v2 + 2;
while ( 1 )
{
v6 = v4;
if ( *(_BYTE *)v3 == 45 && *(_BYTE *)(v3 + 1) == 45 )
{
if ( !*(_BYTE *)(v3 + 2) )
return (unsigned int)-1;
if ( strlen((const char *)v3) >= v9 )
{
v7 = *(_BYTE *)(v3 + v5 + 2);
if ( (!v7 || v7 == 61) && !strncmp((const char *)(v3 + 2), a2, v5) )
break;
}
}
++v4;
v3 = *(_QWORD *)(a1 + 8 * v4);
if ( !v3 )
return (unsigned int)-1;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2)
{
int v2; // eax
__int64 v3; // rbp
__int64 v4; // rbx
size_t v5; // r12
unsigned int v6; // er13
char v7; // al
size_t v9; // [rsp+8h] [rbp-40h]

v2 = strlen(a2);
v3 = *(_QWORD *)(a1 + 8);
if ( !v3 )
return (unsigned int)-1;
v4 = 1LL;
v5 = v2;
v9 = v2 + 2;
while ( 1 )
{
v6 = v4;
if ( *(_BYTE *)v3 == 45 && *(_BYTE *)(v3 + 1) == 45 )
{
if ( !*(_BYTE *)(v3 + 2) )
return (unsigned int)-1;
if ( strlen((const char *)v3) >= v9 )
{
v7 = *(_BYTE *)(v3 + v5 + 2);
if ( (!v7 || v7 == 61) && !strncmp((const char *)(v3 + 2), a2, v5) )
break;
}
}
++v4;
v3 = *(_QWORD *)(a1 + 8 * v4);
if ( !v3 )
return (unsigned int)-1;
}
return v6;
}

original_code:
static int find_long_arg(char **argv, char *opt_long)
{
  int i;
  int len = strlen(opt_long);
  for(i=1;argv[i];i++)
    {
      char *a = argv[i];
      if(*a == '-' && (*(a+1) == '-'))
 {
   if((*(a+2) == 0))
     return -1;
   if(strlen(a) < len+2)
     continue;
   if(a[len+2] != 0 && a[len+2] != '=')
     continue;
   if(strncmp(a+2, opt_long, len) == 0)
     return i;
 }
    }
  return -1;
}

variable_types: len int


original_code:
int jelopt_final(char **argv, int *err)
{
  int i, last=-1;
  for(i=1;argv[i];)
    {
      char *a = argv[i];
      if(*a == '-')
 {
   if(*(a+1) == '-')
     {
       if(*(a+2) == 0)
  break;
       if(err) *err |= 8;
     }
   else
     {
       if(*(a+1) == 0)
  break;
       if(err) *err |= 8;
     }
 }
      if(strcmp(argv[i], "\001\002\090"))
 i++;
      else
 rem_opt(argv, i, i);
    }
  for(i = 0;argv[i]; i++)
    last = i;
  for(i=0;argv[i];i++)
    {
      char *a = argv[i];
      if(*a == '-' && *(a+1) == '-' && *(a+2) == 0)
 {
   rem_opt(argv, i, i);
   return last;
 }
    }
  return last+1;
}

canonical_original_code:
int jelopt_final(char **argv, int *err)
{
  int i; int last = -1;
  for(i=1;argv[i];)
    {
      char *a = argv[i];
      if(*a == '-')
 {
   if(*(a+1) == '-')
     {
       if(*(a+2) == 0)
  break;
       if(err) *err |= 8;
     }
   else
     {
       if(*(a+1) == 0)
  break;
       if(err) *err |= 8;
     }
 }
      if(strcmp(argv[i], "\001\002\090"))
 i++;
      else
 rem_opt(argv, i, i);
    }
  for(i = 0;argv[i]; i++)
    last = i;
  for(i=0;argv[i];i++)
    {
      char *a = argv[i];
      if(*a == '-' && *(a+1) == '-' && *(a+2) == 0)
 {
   rem_opt(argv, i, i);
   return last;
 }
    }
  return last+1;
}

variable_types:
err
int *

variable_types: last int


original_code:
int jelopt_multi(char **argv, int opt_short, char *opt_long,
   int count, char **values,
   int *err)
{
  int j,i = -1;
  int type = 0;
  if(opt_short)
    if( (i = find_short_arg(argv, opt_short)) >= 0)
      type = 1;
  if(!type && opt_long)
    if( (i = find_long_arg(argv, opt_long)) >= 0)
      type = 2;
  if(!type)
    return 0;
  if(count < 0)
    {
      if(err) *err |= 1;
      return 0;
    }
  if(count && values == ((void *)0))
    {
      if(err) *err |= 1;
      return 0;
    }
  if(type == 1)
    {
      if(count)
 {
   char *p = strchr(argv[i], opt_short);
   if(*(p+1))
     {
       *values = strdup(p+1);
       unquote(*values++);
       count--;
       *p = 0;
     }
 }
      for(j=i+1;count--;j++)
 {
   if(argv[j])
     {
       if(strcmp(argv[j], "\001\002\090"))
  *values = strdup(argv[j]);
       else
  {
    if(err) *err |= 4;
    goto err_short;
  }
     }
   else
     {
       if(err) *err |= 2;
     err_short:
       rem_short_opt(argv, opt_short, i, i);
       return 0;
     }
   unquote(*values++);
   argv[j] = "\001\002\090";
 }
      rem_short_opt(argv, opt_short, i, i);
      if(values) *values++ = 0;
      return 1;
    }
  if(type == 2)
    {
      if(count == 0)
 {
   argv[i] = "\001\002\090";
   return 1;
 }
      if(strchr(argv[i], '='))
 {
   *values = strdup(strchr(argv[i], '=')+1);
   unquote(*values++);
   count--;
 }
      for(j=i+1;count--;j++)
 {
   if(argv[j])
     {
       if(strcmp(argv[j], "\001\002\090"))
  *values = strdup(argv[j]);
       else
  {
    if(err) *err |= 4;
    goto err_long;
  }
     }
   else
     {
       if(err) *err |= 2;
     err_long:
       rem_opt(argv, i, i);
       return 0;
     }
   unquote(*values++);
   argv[j] = "\001\002\090";
 }
      argv[i] = "\001\002\090";
      *values++ = 0;
      return 1;
    }
  return 0;
}

canonical_original_code:
int jelopt_multi(char **argv, int opt_short, char *opt_long, int count, char **values, int *err)
{
  int j; int i = -1;
  int type = 0;
  if(opt_short)
    if( (i = find_short_arg(argv, opt_short)) >= 0)
      type = 1;
  if(!type && opt_long)
    if( (i = find_long_arg(argv, opt_long)) >= 0)
      type = 2;
  if(!type)
    return 0;
  if(count < 0)
    {
      if(err) *err |= 1;
      return 0;
    }
  if(count && values == ((void *)0))
    {
      if(err) *err |= 1;
      return 0;
    }
  if(type == 1)
    {
      if(count)
 {
   char *p = strchr(argv[i], opt_short);
   if(*(p+1))
     {
       *values = strdup(p+1);
       unquote(*values++);
       count--;
       *p = 0;
     }
 }
      for(j=i+1;count--;j++)
 {
   if(argv[j])
     {
       if(strcmp(argv[j], "\001\002\090"))
  *values = strdup(argv[j]);
       else
  {
    if(err) *err |= 4;
    goto err_short;
  }
     }
   else
     {
       if(err) *err |= 2;
     err_short:
       rem_short_opt(argv, opt_short, i, i);
       return 0;
     }
   unquote(*values++);
   argv[j] = "\001\002\090";
 }
      rem_short_opt(argv, opt_short, i, i);
      if(values) *values++ = 0;
      return 1;
    }
  if(type == 2)
    {
      if(count == 0)
 {
   argv[i] = "\001\002\090";
   return 1;
 }
      if(strchr(argv[i], '='))
 {
   *values = strdup(strchr(argv[i], '=')+1);
   unquote(*values++);
   count--;
 }
      for(j=i+1;count--;j++)
 {
   if(argv[j])
     {
       if(strcmp(argv[j], "\001\002\090"))
  *values = strdup(argv[j]);
       else
  {
    if(err) *err |= 4;
    goto err_long;
  }
     }
   else
     {
       if(err) *err |= 2;
     err_long:
       rem_opt(argv, i, i);
       return 0;
     }
   unquote(*values++);
   argv[j] = "\001\002\090";
 }
      argv[i] = "\001\002\090";
      *values++ = 0;
      return 1;
    }
  return 0;
}

variable_types: count int

variable_types:
values
char **

variable_types: j int

variable_types: type int


jelopt

decompiled_code:
__int64 __fastcall jelopt(_QWORD *a1, int a2, const char *a3, _QWORD *a4, _DWORD *a5)
{
__int64 result; // rax
__int64 v7[3]; // [rsp+0h] [rbp-18h] BYREF

result = jelopt_multi(a1, a2, a3, a4 != 0LL, v7, a5);
if ( (_DWORD)result )
{
result = 1LL;
if ( a4 )
*a4 = v7[0];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1, int a2, const char *a3, _QWORD *a4, _DWORD *a5)
{
__int64 result; // rax
__int64 v7[3]; // [rsp+0h] [rbp-18h] BYREF

result = func6(a1, a2, a3, a4 != 0LL, v7, a5);
if ( (_DWORD)result )
{
result = 1LL;
if ( a4 )
*a4 = v7[0];
}
return result;
}

original_code:
int jelopt(char **argv, int opt_short, char *opt_long, char **value, int *err)
{
  char *cvalue[2];
  if(jelopt_multi(argv, opt_short, opt_long, value?1:0, cvalue, err))
    {
      if(value)
 *value = cvalue[0];
      return 1;
    }
  return 0;
}

variable_types:
value
char **

variable_types:
cvalue
char *[2]


decompiled_code:
__int64 __fastcall jelopt_int(_QWORD *a1, int a2, const char *a3, int *a4, _DWORD *a5)
{
__int64 result; // rax
char *v7; // rbp
char *nptr; // [rsp+8h] [rbp-20h] BYREF

result = jelopt(a1, a2, a3, &nptr, a5);
if ( (_DWORD)result )
{
v7 = nptr;
*a4 = atoi(nptr);
free(v7);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1, int a2, const char *a3, int *a4, _DWORD *a5)
{
__int64 result; // rax
char *v7; // rbp
char *nptr; // [rsp+8h] [rbp-20h] BYREF

result = func7(a1, a2, a3, &nptr, a5);
if ( (_DWORD)result )
{
v7 = nptr;
*a4 = atoi(nptr);
free(v7);
result = 1LL;
}
return result;
}

original_code:
int jelopt_int(char **argv, int opt_short, char *opt_long,
        int *value, int *err)
{
  char *cvalue;
  if(jelopt(argv, opt_short, opt_long, &cvalue, err))
    {
      *value = atoi(cvalue);
      free(cvalue);
      return 1;
    }
  return 0;
}

variable_types:
value
int *

variable_types:
cvalue
char *


repo: jemk/cedrus

decompiled_code:
size_t __fastcall frame_show(_QWORD *a1)
{
unsigned int v2; // er14
unsigned int v3; // er13
unsigned int v4; // er12
unsigned int v5; // ebp
unsigned int v6; // eax
unsigned int v7; // er12
__int64 v8; // rdi
unsigned int v9; // ebp
unsigned int v10; // eax

if ( disp_initialized_1 )
goto LABEL_4;
if ( (unsigned int)disp_open() )
{
v2 = *((unsigned __int16 *)a1 + 9);
v3 = *((unsigned __int16 *)a1 + 8);
v4 = *((_DWORD *)a1 + 5);
v5 = ve_virt2phys(a1[1]);
v6 = ve_virt2phys(*a1);
disp_set_para(v6, v5, v4, v3, v2, 0LL, 0LL, 800LL, 600LL);
disp_initialized_1 = 1;
LABEL_4:
v7 = frame_id_0;
v8 = a1[1];
++frame_id_0;
v9 = ve_virt2phys(v8);
v10 = ve_virt2phys(*a1);
return disp_new_frame(v10, v9, v7, 24000LL);
}
return fwrite("Can't open /dev/disp\n", 1uLL, 0x15uLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func2(_QWORD *a1)
{
unsigned int v2; // er14
unsigned int v3; // er13
unsigned int v4; // er12
unsigned int v5; // ebp
unsigned int v6; // eax
unsigned int v7; // er12
__int64 v8; // rdi
unsigned int v9; // ebp
unsigned int v10; // eax

if ( disp_initialized_1 )
goto LABEL_4;
if ( (unsigned int)disp_open() )
{
v2 = *((unsigned __int16 *)a1 + 9);
v3 = *((unsigned __int16 *)a1 + 8);
v4 = *((_DWORD *)a1 + 5);
v5 = ve_virt2phys(a1[1]);
v6 = ve_virt2phys(*a1);
disp_set_para(v6, v5, v4, v3, v2, 0LL, 0LL, 800LL, 600LL);
disp_initialized_1 = 1;
LABEL_4:
v7 = frame_id_0;
v8 = a1[1];
++frame_id_0;
v9 = ve_virt2phys(v8);
v10 = ve_virt2phys(*a1);
return disp_new_frame(v10, v9, v7, 24000LL);
}
return fwrite("Can't open /dev/disp\n", 1uLL, 0x15uLL, stderr);
}

original_code:
void frame_show(struct frame_t *frame)
{
 static int disp_initialized = 0;
 static int frame_id = 0;
 if (!disp_initialized)
 {
  if (!disp_open())
  {
   fprintf(stderr, "Can't open /dev/disp\n");
   return;
  }
  disp_set_para(ve_virt2phys(frame->luma_buffer), ve_virt2phys(frame->chroma_buffer),
   frame->color, frame->width, frame->height,
   0, 0, 800, 600);
  disp_initialized = 1;
 }
 disp_new_frame(ve_virt2phys(frame->luma_buffer), ve_virt2phys(frame->chroma_buffer),
  frame_id++, 24000);
}

variable_types: struct frame_t { void *luma_buffer; void *chroma_buffer; unsigned short width; int color; int ref_counter; } *


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

result = a1;
if ( a1 )
++*(_DWORD *)(a1 + 24);
return result;
}

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

result = a1;
if ( a1 )
++*(_DWORD *)(a1 + 24);
return result;
}

original_code:
struct frame_t *frame_ref(struct frame_t *frame)
{
 if (!frame)
  return ((void *)0);
 frame->ref_counter++;
 return frame;
}

variable_types:
frame
struct frame_t *

return_type: struct frame_t { void *luma_buffer; void *chroma_buffer; unsigned short width; int color; int ref_counter; } *


decompiled_code:
void __fastcall frame_unref(void *ptr)
{
int v1; // eax

if ( ptr )
{
v1 = *((_DWORD *)ptr + 6) - 1;
*((_DWORD *)ptr + 6) = v1;
if ( v1 <= 0 )
{
ve_free(*(_QWORD *)ptr);
ve_free(*((_QWORD *)ptr + 1));
free(ptr);
}
}
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
int v1; // eax

if ( ptr )
{
v1 = *((_DWORD *)ptr + 6) - 1;
*((_DWORD *)ptr + 6) = v1;
if ( v1 <= 0 )
{
ve_free(*(_QWORD *)ptr);
ve_free(*((_QWORD *)ptr + 1));
free(ptr);
}
}
}

original_code:
void frame_unref(struct frame_t *frame)
{
 if (!frame)
  return;
 frame->ref_counter--;
 if (frame->ref_counter <= 0)
 {
  ve_free(frame->luma_buffer);
  ve_free(frame->chroma_buffer);
  free(frame);
 }
}

user_defined_types: struct frame_t { void *luma_buffer; void *chroma_buffer; unsigned short width; int color; int ref_counter; }


original_code:
void decode_mpeg(struct frame_buffers_t *frame_buffers, const struct mpeg_t * const mpeg)
{
 int input_size = (mpeg->len + 65535) & ~65535;
 uint8_t *input_buffer = ve_malloc(input_size);
 memcpy(input_buffer, mpeg->data, mpeg->len);
 ve_flush_cache(input_buffer, mpeg->len);
 void *ve_regs = ve_get(0x0, 0);
 set_quantization_tables(ve_regs, mpeg_default_intra_quant, mpeg_default_non_intra_quant);
 uint16_t width = (mpeg->width + 15) / 16;
 uint16_t height = (mpeg->height + 15) / 16;
 writel((width << 8) | height, ve_regs + 0x108);
 writel(((width * 16) << 16) | (height * 16), ve_regs + 0x10c);
 uint32_t pic_header = 0x00000000;
 pic_header |= ((mpeg->picture_coding_type & 0xf) << 28);
 pic_header |= ((mpeg->f_code[0][0] & 0xf) << 24);
 pic_header |= ((mpeg->f_code[0][1] & 0xf) << 20);
 pic_header |= ((mpeg->f_code[1][0] & 0xf) << 16);
 pic_header |= ((mpeg->f_code[1][1] & 0xf) << 12);
 pic_header |= ((mpeg->intra_dc_precision & 0x3) << 10);
 pic_header |= ((mpeg->picture_structure & 0x3) << 8);
 pic_header |= ((mpeg->top_field_first & 0x1) << 7);
 pic_header |= ((mpeg->frame_pred_frame_dct & 0x1) << 6);
 pic_header |= ((mpeg->concealment_motion_vectors & 0x1) << 5);
 pic_header |= ((mpeg->q_scale_type & 0x1) << 4);
 pic_header |= ((mpeg->intra_vlc_format & 0x1) << 3);
 pic_header |= ((mpeg->alternate_scan & 0x1) << 2);
 pic_header |= ((mpeg->full_pel_forward_vector & 0x1) << 1);
 pic_header |= ((mpeg->full_pel_backward_vector & 0x1) << 0);
 writel(pic_header, ve_regs + 0x100);
 writel(0x00000000, ve_regs + 0x100 + 0x10);
 writel(0x800001b8, ve_regs + 0x114);
 writel(0x00000000, ve_regs + 0x100 + 0xc4);
 writel(0x00000000, ve_regs + 0x100 + 0xc8);
 if (mpeg->picture_coding_type == PCT_I || mpeg->picture_coding_type == PCT_P)
 {
  frame_unref(frame_buffers->forward);
  frame_buffers->forward = frame_ref(frame_buffers->backward);
  frame_unref(frame_buffers->backward);
  frame_buffers->backward = frame_ref(frame_buffers->output);
 }
 writel(ve_virt2phys(frame_buffers->forward->luma_buffer), ve_regs + 0x150);
 writel(ve_virt2phys(frame_buffers->forward->chroma_buffer), ve_regs + 0x154);
 writel(ve_virt2phys(frame_buffers->backward->luma_buffer), ve_regs + 0x158);
 writel(ve_virt2phys(frame_buffers->backward->chroma_buffer), ve_regs + 0x15c);
 writel(ve_virt2phys(frame_buffers->output->luma_buffer), ve_regs + 0x148);
 writel(ve_virt2phys(frame_buffers->output->chroma_buffer), ve_regs + 0x14c);
 writel(ve_virt2phys(frame_buffers->output->luma_buffer), ve_regs + 0x1cc);
 writel(ve_virt2phys(frame_buffers->output->chroma_buffer), ve_regs + 0x1d0);
 writel((mpeg->pos - 4) * 8, ve_regs + 0x12c);
 writel((mpeg->len - (mpeg->pos - 4) + 16) * 8, ve_regs + 0x130);
 writel(ve_virt2phys(input_buffer) + input_size - 1, ve_regs + 0x134);
 writel(ve_virt2phys(input_buffer) | 0x50000000, ve_regs + 0x128);
 writel((mpeg->type ? 0x02000000 : 0x01000000) | 0x8000000f, ve_regs + 0x118);
 ve_wait(1);
 writel(0x0000c00f, ve_regs + 0x11c);
 ve_put();
 ve_free(input_buffer);
}

canonical_original_code:
void decode_mpeg(struct frame_buffers_t *frame_buffers, struct mpeg_t *mpeg)
{
 int input_size = (mpeg->len + 65535) & ~65535;
 unsigned char *input_buffer = ve_malloc(input_size);
 memcpy(input_buffer, mpeg->data, mpeg->len);
 ve_flush_cache(input_buffer, mpeg->len);
 void *ve_regs = ve_get(0x0, 0);
 set_quantization_tables(ve_regs, mpeg_default_intra_quant, mpeg_default_non_intra_quant);
 unsigned short width = (mpeg->width + 15) / 16;
 unsigned short height = (mpeg->height + 15) / 16;
 writel((width << 8) | height, ve_regs + 0x108);
 writel(((width * 16) << 16) | (height * 16), ve_regs + 0x10c);
 unsigned int pic_header = 0x00000000;
 pic_header |= ((mpeg->picture_coding_type & 0xf) << 28);
 pic_header |= ((mpeg->f_code[0][0] & 0xf) << 24);
 pic_header |= ((mpeg->f_code[0][1] & 0xf) << 20);
 pic_header |= ((mpeg->f_code[1][0] & 0xf) << 16);
 pic_header |= ((mpeg->f_code[1][1] & 0xf) << 12);
 pic_header |= ((mpeg->intra_dc_precision & 0x3) << 10);
 pic_header |= ((mpeg->picture_structure & 0x3) << 8);
 pic_header |= ((mpeg->top_field_first & 0x1) << 7);
 pic_header |= ((mpeg->frame_pred_frame_dct & 0x1) << 6);
 pic_header |= ((mpeg->concealment_motion_vectors & 0x1) << 5);
 pic_header |= ((mpeg->q_scale_type & 0x1) << 4);
 pic_header |= ((mpeg->intra_vlc_format & 0x1) << 3);
 pic_header |= ((mpeg->alternate_scan & 0x1) << 2);
 pic_header |= ((mpeg->full_pel_forward_vector & 0x1) << 1);
 pic_header |= ((mpeg->full_pel_backward_vector & 0x1) << 0);
 writel(pic_header, ve_regs + 0x100);
 writel(0x00000000, ve_regs + 0x100 + 0x10);
 writel(0x800001b8, ve_regs + 0x114);
 writel(0x00000000, ve_regs + 0x100 + 0xc4);
 writel(0x00000000, ve_regs + 0x100 + 0xc8);
 if (mpeg->picture_coding_type == PCT_I || mpeg->picture_coding_type == PCT_P)
 {
  frame_unref(frame_buffers->forward);
  frame_buffers->forward = frame_ref(frame_buffers->backward);
  frame_unref(frame_buffers->backward);
  frame_buffers->backward = frame_ref(frame_buffers->output);
 }
 writel(ve_virt2phys(frame_buffers->forward->luma_buffer), ve_regs + 0x150);
 writel(ve_virt2phys(frame_buffers->forward->chroma_buffer), ve_regs + 0x154);
 writel(ve_virt2phys(frame_buffers->backward->luma_buffer), ve_regs + 0x158);
 writel(ve_virt2phys(frame_buffers->backward->chroma_buffer), ve_regs + 0x15c);
 writel(ve_virt2phys(frame_buffers->output->luma_buffer), ve_regs + 0x148);
 writel(ve_virt2phys(frame_buffers->output->chroma_buffer), ve_regs + 0x14c);
 writel(ve_virt2phys(frame_buffers->output->luma_buffer), ve_regs + 0x1cc);
 writel(ve_virt2phys(frame_buffers->output->chroma_buffer), ve_regs + 0x1d0);
 writel((mpeg->pos - 4) * 8, ve_regs + 0x12c);
 writel((mpeg->len - (mpeg->pos - 4) + 16) * 8, ve_regs + 0x130);
 writel(ve_virt2phys(input_buffer) + input_size - 1, ve_regs + 0x134);
 writel(ve_virt2phys(input_buffer) | 0x50000000, ve_regs + 0x128);
 writel((mpeg->type ? 0x02000000 : 0x01000000) | 0x8000000f, ve_regs + 0x118);
 ve_wait(1);
 writel(0x0000c00f, ve_regs + 0x11c);
 ve_put();
 ve_free(input_buffer);
}

variable_types: struct frame_buffers_t { struct frame_t { void *luma_buffer; void *chroma_buffer; unsigned short width; int color; int ref_counter; } *output; struct frame_t *backward; struct frame_t *forward; } *

variable_types: struct mpeg_t { unsigned char *data; int len; int pos; unsigned int gop; enum { MPEG1=0, MPEG2=1 } type; unsigned short width; unsigned short height; unsigned short temporal_reference; enum { PCT_I=1, PCT_P=2, PCT_B=3, PCT_D=4 } picture_coding_type; unsigned char full_pel_forward_vector; unsigned char full_pel_backward_vector; unsigned char f_code[2][2]; unsigned char intra_dc_precision; unsigned char picture_structure; unsigned char top_field_first; unsigned char frame_pred_frame_dct; unsigned char concealment_motion_vectors; unsigned char q_scale_type; unsigned char intra_vlc_format; unsigned char alternate_scan; } *

variable_types:
width
unsigned short

variable_types:
height
unsigned short

user_defined_types: struct frame_buffers_t { struct frame_t *output; struct frame_t *backward; struct frame_t *forward; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
int v4; // eax
int v5; // ebp
unsigned int v6; // er12
unsigned int v7; // er14
int v8; // er12
int v9; // er13
unsigned int v10; // ebx
_WORD *v11; // rax
char v12; // al
_QWORD *v13; // rdi
char v14; // r15
int v15; // er15
unsigned int v16; // er13
char v17; // r12
_QWORD *v18; // rdi
__int64 v19; // rbp
const char *v21; // rax
size_t v22; // rdx
const char *v23; // rdi
FILE *v24; // rcx
__int64 v25; // [rsp+8h] [rbp-140h] BYREF
_QWORD *v26; // [rsp+10h] [rbp-138h] BYREF
void *v27; // [rsp+18h] [rbp-130h]
void *v28; // [rsp+20h] [rbp-128h]
__int128 v29; // [rsp+30h] [rbp-118h] BYREF
__int128 v30; // [rsp+40h] [rbp-108h]
__int128 v31; // [rsp+50h] [rbp-F8h]
__int64 v32; // [rsp+60h] [rbp-E8h]
void *ptr[2]; // [rsp+70h] [rbp-D8h]
__int128 v34; // [rsp+80h] [rbp-C8h]
__int128 v35; // [rsp+90h] [rbp-B8h]
__int128 v36; // [rsp+A0h] [rbp-A8h]
char v37[24]; // [rsp+B0h] [rbp-98h] BYREF
__int64 v38; // [rsp+C8h] [rbp-80h]
unsigned int v39; // [rsp+D0h] [rbp-78h]
int v40; // [rsp+D4h] [rbp-74h]

v25 = 0LL;
if ( argc <= 1 )
{
fprintf(stderr, "Usage: %s filename\n", *argv);
LABEL_31:
exit(1);
}
v3 = argv[1];
av_register_all(argc, argv, envp);
if ( (int)avformat_open_input(&v25, v3, 0LL, 0LL) < 0 )
{
fprintf(stderr, "Could not open source file %s\n", v3);
goto LABEL_31;
}
if ( (int)avformat_find_stream_info(v25, 0LL) < 0 )
{
v22 = 34LL;
v23 = "Could not find stream information\n";
v24 = stderr;
LABEL_34:
fwrite(v23, 1uLL, v22, v24);
avformat_close_input(&v25);
exit(1);
}
v4 = av_find_best_stream(v25, 0LL, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0LL, 0LL);
v5 = v4;
if ( v4 < 0 )
{
v22 = 42LL;
v23 = "Could not find video stream in input file\n";
v24 = stderr;
goto LABEL_34;
}
v6 = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(v25 + 48) + 8LL * v4) + 8LL) + 24LL);
if ( v6 - 1 > 1 )
{
v21 = (const char *)avcodec_get_name(v6);
fprintf(stderr, "Can't handle codec %s\n", v21);
avformat_close_input(&v25);
exit(1);
}
av_init_packet(v37);
v38 = 0LL;
v39 = 0;
v32 = 0LL;
v29 = 0LL;
v30 = 0LL;
v31 = 0LL;
if ( v6 != 1 )
DWORD1(v30) = 1;
if ( !(unsigned int)ve_open() )
err(1, "Can't open VE");
v7 = 0;
v8 = 0;
v26 = 0LL;
v9 = 0;
v10 = 0;
v27 = 0LL;
v28 = 0LL;
*(_OWORD *)ptr = 0LL;
v34 = 0LL;
v35 = 0LL;
v36 = 0LL;
puts("Playing now... press Enter for next frame!");
while ( (int)av_read_frame(v25, v37) >= 0 )
{
*(_QWORD *)&v29 = v38;
*((_QWORD *)&v29 + 1) = v39;
if ( v40 == v5 && (unsigned int)parse_mpeg(&v29) )
{
v11 = frame_new(WORD4(v30), WORD5(v30), 0);
v26 = v11;
if ( !v27 )
v27 = (void *)frame_ref((__int64)v11);
if ( !v28 )
v28 = (void *)frame_ref((__int64)v26);
decode_mpeg(&v26, (__int64)&v29);
if ( (unsigned int)v30 > v7 )
{
v9 += v8;
v7 = v30;
v8 = 0;
}
v12 = BYTE12(v30);
++v8;
if ( ptr[(BYTE12(v30) + (_BYTE)v9) & 7] )
{
puts("Buffer overrun!");
frame_unref(ptr[((_BYTE)v9 + BYTE12(v30)) & 7]);
v12 = BYTE12(v30);
}
ptr[((_BYTE)v9 + v12) & 7] = v26;
v13 = ptr[v10 & 7];
if ( v13 )
{
frame_show(v13);
v14 = v10++ - 2;
v15 = v14 & 7;
frame_unref(ptr[v15]);
ptr[v15] = 0LL;
getchar();
}
}
av_free_packet(v37);
}
v16 = v8 + v9;
if ( v16 > v10 )
{
while ( 1 )
{
v17 = v10 - 2;
v18 = ptr[v10 & 7];
v19 = ((_BYTE)v10 - 2) & 7;
if ( !v18 )
break;
frame_show(v18);
++v10;
frame_unref(ptr[v19]);
ptr[v19] = 0LL;
getchar();
if ( v16 == v10 )
goto LABEL_29;
}
}
else
{
LABEL_29:
v17 = v10 - 2;
}
disp_close();
frame_unref(ptr[v17 & 7]);
frame_unref(ptr[((_BYTE)v10 - 1) & 7]);
frame_unref(v28);
frame_unref(v27);
ve_close();
avformat_close_input(&v25);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
int v4; // eax
int v5; // ebp
unsigned int v6; // er12
unsigned int v7; // er14
int v8; // er12
int v9; // er13
unsigned int v10; // ebx
_WORD *v11; // rax
char v12; // al
_QWORD *v13; // rdi
char v14; // r15
int v15; // er15
unsigned int v16; // er13
char v17; // r12
_QWORD *v18; // rdi
__int64 v19; // rbp
const char *v21; // rax
size_t v22; // rdx
const char *v23; // rdi
FILE *v24; // rcx
__int64 v25; // [rsp+8h] [rbp-140h] BYREF
_QWORD *v26; // [rsp+10h] [rbp-138h] BYREF
void *v27; // [rsp+18h] [rbp-130h]
void *v28; // [rsp+20h] [rbp-128h]
__int128 v29; // [rsp+30h] [rbp-118h] BYREF
__int128 v30; // [rsp+40h] [rbp-108h]
__int128 v31; // [rsp+50h] [rbp-F8h]
__int64 v32; // [rsp+60h] [rbp-E8h]
void *ptr[2]; // [rsp+70h] [rbp-D8h]
__int128 v34; // [rsp+80h] [rbp-C8h]
__int128 v35; // [rsp+90h] [rbp-B8h]
__int128 v36; // [rsp+A0h] [rbp-A8h]
char v37[24]; // [rsp+B0h] [rbp-98h] BYREF
__int64 v38; // [rsp+C8h] [rbp-80h]
unsigned int v39; // [rsp+D0h] [rbp-78h]
int v40; // [rsp+D4h] [rbp-74h]

v25 = 0LL;
if ( argc <= 1 )
{
fprintf(stderr, "Usage: %s filename\n", *argv);
LABEL_31:
exit(1);
}
v3 = argv[1];
av_register_all(argc, argv, envp);
if ( (int)avformat_open_input(&v25, v3, 0LL, 0LL) < 0 )
{
fprintf(stderr, "Could not open source file %s\n", v3);
goto LABEL_31;
}
if ( (int)avformat_find_stream_info(v25, 0LL) < 0 )
{
v22 = 34LL;
v23 = "Could not find stream information\n";
v24 = stderr;
LABEL_34:
fwrite(v23, 1uLL, v22, v24);
avformat_close_input(&v25);
exit(1);
}
v4 = av_find_best_stream(v25, 0LL, 0xFFFFFFFFLL, 0xFFFFFFFFLL, 0LL, 0LL);
v5 = v4;
if ( v4 < 0 )
{
v22 = 42LL;
v23 = "Could not find video stream in input file\n";
v24 = stderr;
goto LABEL_34;
}
v6 = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(v25 + 48) + 8LL * v4) + 8LL) + 24LL);
if ( v6 - 1 > 1 )
{
v21 = (const char *)avcodec_get_name(v6);
fprintf(stderr, "Can't handle codec %s\n", v21);
avformat_close_input(&v25);
exit(1);
}
av_init_packet(v37);
v38 = 0LL;
v39 = 0;
v32 = 0LL;
v29 = 0LL;
v30 = 0LL;
v31 = 0LL;
if ( v6 != 1 )
DWORD1(v30) = 1;
if ( !(unsigned int)ve_open() )
err(1, "Can't open VE");
v7 = 0;
v8 = 0;
v26 = 0LL;
v9 = 0;
v10 = 0;
v27 = 0LL;
v28 = 0LL;
*(_OWORD *)ptr = 0LL;
v34 = 0LL;
v35 = 0LL;
v36 = 0LL;
puts("Playing now... press Enter for next frame!");
while ( (int)av_read_frame(v25, v37) >= 0 )
{
*(_QWORD *)&v29 = v38;
*((_QWORD *)&v29 + 1) = v39;
if ( v40 == v5 && (unsigned int)parse_mpeg(&v29) )
{
v11 = frame_new(WORD4(v30), WORD5(v30), 0);
v26 = v11;
if ( !v27 )
v27 = (void *)func3((__int64)v11);
if ( !v28 )
v28 = (void *)func3((__int64)v26);
func5(&v26, (__int64)&v29);
if ( (unsigned int)v30 > v7 )
{
v9 += v8;
v7 = v30;
v8 = 0;
}
v12 = BYTE12(v30);
++v8;
if ( ptr[(BYTE12(v30) + (_BYTE)v9) & 7] )
{
puts("Buffer overrun!");
func4(ptr[((_BYTE)v9 + BYTE12(v30)) & 7]);
v12 = BYTE12(v30);
}
ptr[((_BYTE)v9 + v12) & 7] = v26;
v13 = ptr[v10 & 7];
if ( v13 )
{
func2(v13);
v14 = v10++ - 2;
v15 = v14 & 7;
func4(ptr[v15]);
ptr[v15] = 0LL;
getchar();
}
}
av_free_packet(v37);
}
v16 = v8 + v9;
if ( v16 > v10 )
{
while ( 1 )
{
v17 = v10 - 2;
v18 = ptr[v10 & 7];
v19 = ((_BYTE)v10 - 2) & 7;
if ( !v18 )
break;
func2(v18);
++v10;
func4(ptr[v19]);
ptr[v19] = 0LL;
getchar();
if ( v16 == v10 )
goto LABEL_29;
}
}
else
{
LABEL_29:
v17 = v10 - 2;
}
disp_close();
func4(ptr[v17 & 7]);
func4(ptr[((_BYTE)v10 - 1) & 7]);
func4(v28);
func4(v27);
ve_close();
avformat_close_input(&v25);
return 0;
}

original_code:
int main(const int argc, const char **argv)
{
 int in;
 struct stat s;
 uint8_t *data;
 if (argc < 2)
 {
  fprintf(stderr, "Usage: %s infile.jpg\n", argv[0]);
  exit(1);
 }
 const char *filename = argv[1];
 if ((in = open(filename, 00)) == -1)
  err(1, "%s", filename);
 if (fstat(in, &s) < 0)
  err(1, "stat %s", filename);
 if (s.st_size == 0)
  errx(1, "%s empty", filename);
 if ((data = mmap(((void *)0), s.st_size, 0x1, 0x01, in, 0)) == ((void *) -1))
  err(1, "mmap %s", filename);
 struct jpeg_t jpeg ;
 memset(&jpeg, 0, sizeof(jpeg));
 if (!parse_jpeg(&jpeg, data, s.st_size))
  warnx("Can't parse JPEG");
 decode_jpeg(&jpeg);
 munmap(data, s.st_size);
 close(in);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int in;
 struct stat s;
 unsigned char *data;
 if (argc < 2)
 {
  fprintf(stderr, "Usage: %s infile.jpg\n", argv[0]);
  exit(1);
 }
 char *filename = argv[1];
 if ((in = open(filename, 00)) == -1)
  err(1, "%s", filename);
 if (fstat(in, &s) < 0)
  err(1, "stat %s", filename);
 if (s.st_size == 0)
  errx(1, "%s empty", filename);
 if ((data = mmap(((void *)0), s.st_size, 0x1, 0x01, in, 0)) == ((void *) -1))
  err(1, "mmap %s", filename);
 struct jpeg_t jpeg;
 memset(&jpeg, 0, sizeof(jpeg));
 if (!parse_jpeg(&jpeg, data, s.st_size))
  warnx("Can't parse JPEG");
 decode_jpeg(&jpeg);
 munmap(data, s.st_size);
 close(in);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: in int

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:
data
unsigned char *

variable_types:
filename
char *

variable_types: struct jpeg_t { unsigned char bits; unsigned short width; unsigned short height; struct comp_t { unsigned char samp_h; unsigned char samp_v; unsigned char qt; unsigned char ht_ac; unsigned char ht_dc; } comp[3]; struct quant_t { unsigned char coeff[64]; } *quant[4]; struct huffman_t { unsigned char num[16]; unsigned char codes[256]; } *huffman[8]; unsigned short restart_interval; unsigned char *data; unsigned int data_len; }

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]; }

user_defined_types: struct jpeg_t { unsigned char bits; unsigned short width; unsigned short height; struct comp_t comp[3]; struct quant_t *quant[4]; struct huffman_t *huffman[8]; unsigned short restart_interval; unsigned char *data; unsigned int data_len; }


original_code:
static int process_dht(struct jpeg_t *jpeg, const uint8_t *data, const int len)
{
 int pos = 0;
 while (pos < len)
 {
  uint8_t id = ((data[pos] & 0x03) << 1) | ((data[pos] & 0x10) >> 4);
  jpeg->huffman[id] = (struct huffman_t *)&(data[pos + 1]);
  int i;
  pos += 17;
  for (i = 0; i < 16; i++)
   pos += jpeg->huffman[id]->num[i];
 }
 return 1;
}

canonical_original_code:
static int process_dht(struct jpeg_t *jpeg, unsigned char *data, int len)
{
 int pos = 0;
 while (pos < len)
 {
  unsigned char id = ((data[pos] & 0x03) << 1) | ((data[pos] & 0x10) >> 4);
  jpeg->huffman[id] = (struct huffman_t *)&(data[pos + 1]);
  int i;
  pos += 17;
  for (i = 0; i < 16; i++)
   pos += jpeg->huffman[id]->num[i];
 }
 return 1;
}

variable_types: struct jpeg_t { unsigned char bits; unsigned short width; unsigned short height; struct comp_t { unsigned char samp_h; unsigned char samp_v; unsigned char qt; unsigned char ht_ac; unsigned char ht_dc; } comp[3]; struct quant_t { unsigned char coeff[64]; } *quant[4]; struct huffman_t { unsigned char num[16]; unsigned char codes[256]; } *huffman[8]; unsigned short restart_interval; unsigned char *data; unsigned int data_len; } *

variable_types: len int

variable_types: pos int

variable_types:
id
unsigned char

variable_types: i int


decompiled_code:
__int64 __fastcall process_dqt(__int64 a1, __int64 a2, int a3)
{
__int64 v4; // rsi
int v5; // edx

if ( a3 <= 0 )
return 1LL;
v4 = a2 + 1;
v5 = 0;
while ( !(*(_BYTE *)(v4 - 1) >> 4) )
{
v5 += 65;
*(_QWORD *)(a1 + 8LL * (*(_BYTE *)(v4 - 1) & 3) + 24) = v4;
v4 += 65LL;
if ( a3 <= v5 )
return 1LL;
}
fwrite("Only 8bit Quantization Tables supported\n", 1uLL, 0x28uLL, stderr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, int a3)
{
__int64 v4; // rsi
int v5; // edx

if ( a3 <= 0 )
return 1LL;
v4 = a2 + 1;
v5 = 0;
while ( !(*(_BYTE *)(v4 - 1) >> 4) )
{
v5 += 65;
*(_QWORD *)(a1 + 8LL * (*(_BYTE *)(v4 - 1) & 3) + 24) = v4;
v4 += 65LL;
if ( a3 <= v5 )
return 1LL;
}
fwrite("Only 8bit Quantization Tables supported\n", 1uLL, 0x28uLL, stderr);
return 0LL;
}

original_code:
static int process_dqt(struct jpeg_t *jpeg, const uint8_t *data, const int len)
{
 int pos;
 for (pos = 0; pos < len; pos += 65)
 {
  if ((data[pos] >> 4) != 0)
  {
   fprintf(stderr, "Only 8bit Quantization Tables supported\n");
   return 0;
  }
  jpeg->quant[data[pos] & 0x03] = (struct quant_t *)&(data[pos + 1]);
 }
 return 1;
}

canonical_original_code:
static int process_dqt(struct jpeg_t *jpeg, unsigned char *data, int len)
{
 int pos;
 for (pos = 0; pos < len; pos += 65)
 {
  if ((data[pos] >> 4) != 0)
  {
   fprintf(stderr, "Only 8bit Quantization Tables supported\n");
   return 0;
  }
  jpeg->quant[data[pos] & 0x03] = (struct quant_t *)&(data[pos + 1]);
 }
 return 1;
}

user_defined_types: struct quant_t { unsigned char coeff[64]; }

user_defined_types: struct comp_t { unsigned char samp_h; unsigned char samp_v; unsigned char qt; unsigned char ht_ac; unsigned char ht_dc; }

user_defined_types: struct huffman_t { unsigned char num[16]; unsigned char codes[256]; }


decompiled_code:
__int64 __fastcall parse_jpeg(__int64 a1, _BYTE *a2, int a3)
{
__int64 result; // rax
int v7; // ebx
_BYTE *v8; // rax
int v9; // edi
__int64 v10; // r8
__int64 v11; // rcx
char v12; // dl
unsigned __int16 v13; // ax
_BYTE *v14; // rdi
unsigned __int8 *v15; // rdx
int v16; // esi
unsigned __int8 v17; // cl
_BYTE *v18; // rax
unsigned __int8 v19; // al
_BYTE *v20; // rdi
int v21; // esi
unsigned __int8 *v22; // rdx
unsigned __int8 v23; // cl
__int64 v24; // rax
unsigned __int8 v25; // al

if ( a3 <= 1 )
return 0LL;
if ( *a2 != 0xFF || a2[1] != 0xD8 )
return 0LL;
v7 = 2;
LABEL_7:
while ( 2 )
{
if ( a3 > v7 )
{
v8 = &a2[v7];
do
{
if ( *v8 != 0xFF )
break;
++v7;
++v8;
}
while ( a3 != v7 );
}
if ( v7 + 2 >= a3 )
return 0LL;
v9 = v7 + 1;
v10 = v7;
v11 = v7 + 1;
v12 = a2[v7];
v13 = _byteswap_ushort(*(_WORD *)&a2[v11]);
v7 += v13 + 1;
if ( v7 >= a3 )
return 0LL;
switch ( v12 )
{
case -64:
v14 = &a2[v9 + 7];
*(_BYTE *)a1 = a2[v11 + 2];
*(_WORD *)(a1 + 2) = _byteswap_ushort(*(_WORD *)&a2[v11 + 5]);
*(_WORD *)(a1 + 4) = _byteswap_ushort(*(_WORD *)&a2[v11 + 3]);
if ( !*v14 )
continue;
v15 = &a2[v10 + 10];
v16 = 0;
while ( 1 )
{
v19 = *(v15 - 1) - 1;
if ( v19 > 2u )
goto LABEL_24;
v17 = *v15;
++v16;
v15 += 3;
v18 = (_BYTE *)(a1 + 5LL * v19);
v18[6] = v17 >> 4;
v18[7] = *(v15 - 3) & 0xF;
v18[8] = *(v15 - 2);
if ( (unsigned __int8)*v14 <= v16 )
goto LABEL_7;
}
case -63:
case -62:
case -61:
case -59:
case -58:
case -57:
case -55:
case -54:
case -53:
case -51:
case -50:
case -49:
fwrite("only Baseline DCT supported (yet?)\n", 1uLL, 0x23uLL, stderr);
return 0LL;
case -60:
if ( !(unsigned int)process_dht(a1, (__int64)&a2[v11 + 2], (unsigned int)v13 - 2) )
return 0LL;
continue;
case -52:
fwrite("Arithmetic Coding unsupported\n", 1uLL, 0x1EuLL, stderr);
return 0LL;
case -40:
case -39:
fwrite("corrupted file\n", 1uLL, 0xFuLL, stderr);
return 0LL;
case -38:
v20 = &a2[v9 + 2];
v21 = 0;
v22 = &a2[v10 + 5];
if ( !*v20 )
goto LABEL_33;
break;
case -37:
if ( !(unsigned int)process_dqt(a1, (__int64)&a2[v11 + 2], (unsigned int)v13 - 2) )
return 0LL;
continue;
case -35:
*(_WORD *)(a1 + 120) = _byteswap_ushort(*(_WORD *)&a2[v9 + 2]);
continue;
default:
continue;
}
break;
}
do
{
v25 = *(v22 - 1) - 1;
if ( v25 > 2u )
{
LABEL_24:
fwrite("only YCbCr supported\n", 1uLL, 0x15uLL, stderr);
return 0LL;
}
v23 = *v22;
++v21;
v22 += 2;
v24 = a1 + 5LL * v25;
*(_BYTE *)(v24 + 10) = v23 >> 4;
*(_BYTE *)(v24 + 9) = *(v22 - 2) & 0xF;
}
while ( (unsigned __int8)*v20 > v21 );
LABEL_33:
*(_DWORD *)(a1 + 136) = a3 - v7;
result = 1LL;
*(_QWORD *)(a1 + 128) = &a2[v7];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, _BYTE *a2, int a3)
{
__int64 result; // rax
int v7; // ebx
_BYTE *v8; // rax
int v9; // edi
__int64 v10; // r8
__int64 v11; // rcx
char v12; // dl
unsigned __int16 v13; // ax
_BYTE *v14; // rdi
unsigned __int8 *v15; // rdx
int v16; // esi
unsigned __int8 v17; // cl
_BYTE *v18; // rax
unsigned __int8 v19; // al
_BYTE *v20; // rdi
int v21; // esi
unsigned __int8 *v22; // rdx
unsigned __int8 v23; // cl
__int64 v24; // rax
unsigned __int8 v25; // al

if ( a3 <= 1 )
return 0LL;
if ( *a2 != 0xFF || a2[1] != 0xD8 )
return 0LL;
v7 = 2;
LABEL_7:
while ( 2 )
{
if ( a3 > v7 )
{
v8 = &a2[v7];
do
{
if ( *v8 != 0xFF )
break;
++v7;
++v8;
}
while ( a3 != v7 );
}
if ( v7 + 2 >= a3 )
return 0LL;
v9 = v7 + 1;
v10 = v7;
v11 = v7 + 1;
v12 = a2[v7];
v13 = _byteswap_ushort(*(_WORD *)&a2[v11]);
v7 += v13 + 1;
if ( v7 >= a3 )
return 0LL;
switch ( v12 )
{
case -64:
v14 = &a2[v9 + 7];
*(_BYTE *)a1 = a2[v11 + 2];
*(_WORD *)(a1 + 2) = _byteswap_ushort(*(_WORD *)&a2[v11 + 5]);
*(_WORD *)(a1 + 4) = _byteswap_ushort(*(_WORD *)&a2[v11 + 3]);
if ( !*v14 )
continue;
v15 = &a2[v10 + 10];
v16 = 0;
while ( 1 )
{
v19 = *(v15 - 1) - 1;
if ( v19 > 2u )
goto LABEL_24;
v17 = *v15;
++v16;
v15 += 3;
v18 = (_BYTE *)(a1 + 5LL * v19);
v18[6] = v17 >> 4;
v18[7] = *(v15 - 3) & 0xF;
v18[8] = *(v15 - 2);
if ( (unsigned __int8)*v14 <= v16 )
goto LABEL_7;
}
case -63:
case -62:
case -61:
case -59:
case -58:
case -57:
case -55:
case -54:
case -53:
case -51:
case -50:
case -49:
fwrite("only Baseline DCT supported (yet?)\n", 1uLL, 0x23uLL, stderr);
return 0LL;
case -60:
if ( !(unsigned int)func0(a1, (__int64)&a2[v11 + 2], (unsigned int)v13 - 2) )
return 0LL;
continue;
case -52:
fwrite("Arithmetic Coding unsupported\n", 1uLL, 0x1EuLL, stderr);
return 0LL;
case -40:
case -39:
fwrite("corrupted file\n", 1uLL, 0xFuLL, stderr);
return 0LL;
case -38:
v20 = &a2[v9 + 2];
v21 = 0;
v22 = &a2[v10 + 5];
if ( !*v20 )
goto LABEL_33;
break;
case -37:
if ( !(unsigned int)func1(a1, (__int64)&a2[v11 + 2], (unsigned int)v13 - 2) )
return 0LL;
continue;
case -35:
*(_WORD *)(a1 + 120) = _byteswap_ushort(*(_WORD *)&a2[v9 + 2]);
continue;
default:
continue;
}
break;
}
do
{
v25 = *(v22 - 1) - 1;
if ( v25 > 2u )
{
LABEL_24:
fwrite("only YCbCr supported\n", 1uLL, 0x15uLL, stderr);
return 0LL;
}
v23 = *v22;
++v21;
v22 += 2;
v24 = a1 + 5LL * v25;
*(_BYTE *)(v24 + 10) = v23 >> 4;
*(_BYTE *)(v24 + 9) = *(v22 - 2) & 0xF;
}
while ( (unsigned __int8)*v20 > v21 );
LABEL_33:
*(_DWORD *)(a1 + 136) = a3 - v7;
result = 1LL;
*(_QWORD *)(a1 + 128) = &a2[v7];
return result;
}

original_code:
int parse_jpeg(struct jpeg_t *jpeg, const uint8_t *data, const int len)
{
 if (len < 2 || data[0] != 0xff || data[1] != 0xd8)
  return 0;
 int pos = 2;
 int sos = 0;
 while (!sos)
 {
  int i;
  while (pos < len && data[pos] == 0xff)
   pos++;
  if (!(pos + 2 < len))
   return 0;
  uint8_t marker = data[pos++];
  uint16_t seg_len = ((uint16_t)data[pos]) << 8 | (uint16_t)data[pos + 1];
  if (!(pos + seg_len < len))
   return 0;
  switch (marker)
  {
  case 0xdb:
   if (!process_dqt(jpeg, &data[pos + 2], seg_len - 2))
    return 0;
   break;
  case 0xc4:
   if (!process_dht(jpeg, &data[pos + 2], seg_len - 2))
    return 0;
   break;
  case 0xc0:
   jpeg->bits = data[pos + 2];
   jpeg->width = ((uint16_t)data[pos + 5]) << 8 | (uint16_t)data[pos + 6];
   jpeg->height = ((uint16_t)data[pos + 3]) << 8 | (uint16_t)data[pos + 4];
   for (i = 0; i < data[pos + 7]; i++)
   {
    uint8_t id = data[pos + 8 + 3 * i] - 1;
    if (id > 2)
    {
     fprintf(stderr, "only YCbCr supported\n");
     return 0;
    }
    jpeg->comp[id].samp_h = data[pos + 9 + 3 * i] >> 4;
    jpeg->comp[id].samp_v = data[pos + 9 + 3 * i] & 0x0f;
    jpeg->comp[id].qt = data[pos + 10 + 3 * i];
   }
   break;
  case 0xdd:
   jpeg->restart_interval = ((uint16_t)data[pos + 2]) << 8 | (uint16_t)data[pos + 3];
   break;
  case 0xda:
   for (i = 0; i < data[pos + 2]; i++)
   {
    uint8_t id = data[pos + 3 + 2 * i] - 1;
    if (id > 2)
    {
     fprintf(stderr, "only YCbCr supported\n");
     return 0;
    }
    jpeg->comp[id].ht_dc = data[pos + 4 + 2 * i] >> 4;
    jpeg->comp[id].ht_ac = data[pos + 4 + 2 * i] & 0x0f;
   }
   sos = 1;
   break;
  case 0xcc:
   fprintf(stderr, "Arithmetic Coding unsupported\n");
   return 0;
  case 0xc1:
  case 0xc2:
  case 0xc3:
  case 0xc5:
  case 0xc6:
  case 0xc7:
  case 0xc9:
  case 0xca:
  case 0xcb:
  case 0xcd:
  case 0xce:
  case 0xcf:
   fprintf(stderr, "only Baseline DCT supported (yet?)\n");
   return 0;
  case 0xd8:
  case 0xd9:
   fprintf(stderr, "corrupted file\n");
   return 0;
  default:
   break;
  }
  pos += seg_len;
 }
 jpeg->data = (uint8_t *)&(data[pos]);
 jpeg->data_len = len - pos;
 return 1;
}

canonical_original_code:
int parse_jpeg(struct jpeg_t *jpeg, unsigned char *data, int len)
{
 if (len < 2 || data[0] != 0xff || data[1] != 0xd8)
  return 0;
 int pos = 2;
 int sos = 0;
 while (!sos)
 {
  int i;
  while (pos < len && data[pos] == 0xff)
   pos++;
  if (!(pos + 2 < len))
   return 0;
  unsigned char marker = data[pos++];
  unsigned short seg_len = ((uint16_t)data[pos]) << 8 | (uint16_t)data[pos + 1];
  if (!(pos + seg_len < len))
   return 0;
  switch (marker)
  {
  case 0xdb:
   if (!process_dqt(jpeg, &data[pos + 2], seg_len - 2))
    return 0;
   break;
  case 0xc4:
   if (!process_dht(jpeg, &data[pos + 2], seg_len - 2))
    return 0;
   break;
  case 0xc0:
   jpeg->bits = data[pos + 2];
   jpeg->width = ((unsigned short)data[pos + 5]) << 8 | (unsigned short)data[pos + 6];
   jpeg->height = ((unsigned short)data[pos + 3]) << 8 | (unsigned short)data[pos + 4];
   for (i = 0; i < data[pos + 7]; i++)
   {
    unsigned char id = data[pos + 8 + 3 * i] - 1;
    if (id > 2)
    {
     fprintf(stderr, "only YCbCr supported\n");
     return 0;
    }
    jpeg->comp[id].samp_h = data[pos + 9 + 3 * i] >> 4;
    jpeg->comp[id].samp_v = data[pos + 9 + 3 * i] & 0x0f;
    jpeg->comp[id].qt = data[pos + 10 + 3 * i];
   }
   break;
  case 0xdd:
   jpeg->restart_interval = ((unsigned short)data[pos + 2]) << 8 | (unsigned short)data[pos + 3];
   break;
  case 0xda:
   for (i = 0; i < data[pos + 2]; i++)
   {
    unsigned char id = data[pos + 3 + 2 * i] - 1;
    if (id > 2)
    {
     fprintf(stderr, "only YCbCr supported\n");
     return 0;
    }
    jpeg->comp[id].ht_dc = data[pos + 4 + 2 * i] >> 4;
    jpeg->comp[id].ht_ac = data[pos + 4 + 2 * i] & 0x0f;
   }
   sos = 1;
   break;
  case 0xcc:
   fprintf(stderr, "Arithmetic Coding unsupported\n");
   return 0;
  case 0xc1:
  case 0xc2:
  case 0xc3:
  case 0xc5:
  case 0xc6:
  case 0xc7:
  case 0xc9:
  case 0xca:
  case 0xcb:
  case 0xcd:
  case 0xce:
  case 0xcf:
   fprintf(stderr, "only Baseline DCT supported (yet?)\n");
   return 0;
  case 0xd8:
  case 0xd9:
   fprintf(stderr, "corrupted file\n");
   return 0;
  default:
   break;
  }
  pos += seg_len;
 }
 jpeg->data = (unsigned char *)&(data[pos]);
 jpeg->data_len = len - pos;
 return 1;
}

variable_types: sos int

variable_types:
marker
unsigned char


original_code:
void dump_jpeg(const struct jpeg_t *jpeg)
{
 int i, j, k;
 printf("Width: %u  Height: %u  Bits: %u\nRestart interval: %u\nComponents:\n", jpeg->width, jpeg->height, jpeg->bits, jpeg->restart_interval);
 for (i = 0; i < 3; i++)
 {
  if (jpeg->comp[i].samp_h && jpeg->comp[i].samp_v)
   printf("\tType: %s  Sampling: %u:%u  QT: %u  HT: %u/%u\n", comp_types[i], jpeg->comp[i].samp_h, jpeg->comp[i].samp_v, jpeg->comp[i].qt, jpeg->comp[i].ht_dc, jpeg->comp[i].ht_dc);
 }
 printf("Quantization Tables:\n");
 for (i = 0; i < 4; i++)
 {
  if (jpeg->quant[i])
  {
   printf("\tID: %u\n", i);
   for (j = 0; j < 64 / 8; j++)
   {
    printf("\t\t");
    for (k = 0; k < 8; k++)
    {
     printf("0x%02x ", jpeg->quant[i]->coeff[j * 8 + k]);
    }
    printf("\n");
   }
  }
 }
 printf("Huffman Tables:\n");
 for (i = 0; i < 8; i++)
 {
  if (jpeg->huffman[i])
  {
   printf("\tID: %u (%cC)\n", (i & 0x06) >> 1, i & 0x01 ? 'A' : 'D');
   int sum = 0;
   for (j = 0; j < 16; j++)
   {
    if (jpeg->huffman[i]->num[j])
    {
     printf("\t\t%u bits:", j + 1);
     for (k = 0; k < jpeg->huffman[i]->num[j]; k++)
     {
      printf(" 0x%02x", jpeg->huffman[i]->codes[sum + k]);
     }
     printf("\n");
     sum += jpeg->huffman[i]->num[j];
    }
   }
  }
 }
 printf("Data length: %u\n", jpeg->data_len);
}

canonical_original_code:
void dump_jpeg(struct jpeg_t *jpeg)
{
 int i; int j; int k;
 printf("Width: %u  Height: %u  Bits: %u\nRestart interval: %u\nComponents:\n", jpeg->width, jpeg->height, jpeg->bits, jpeg->restart_interval);
 for (i = 0; i < 3; i++)
 {
  if (jpeg->comp[i].samp_h && jpeg->comp[i].samp_v)
   printf("\tType: %s  Sampling: %u:%u  QT: %u  HT: %u/%u\n", comp_types[i], jpeg->comp[i].samp_h, jpeg->comp[i].samp_v, jpeg->comp[i].qt, jpeg->comp[i].ht_dc, jpeg->comp[i].ht_dc);
 }
 printf("Quantization Tables:\n");
 for (i = 0; i < 4; i++)
 {
  if (jpeg->quant[i])
  {
   printf("\tID: %u\n", i);
   for (j = 0; j < 64 / 8; j++)
   {
    printf("\t\t");
    for (k = 0; k < 8; k++)
    {
     printf("0x%02x ", jpeg->quant[i]->coeff[j * 8 + k]);
    }
    printf("\n");
   }
  }
 }
 printf("Huffman Tables:\n");
 for (i = 0; i < 8; i++)
 {
  if (jpeg->huffman[i])
  {
   printf("\tID: %u (%cC)\n", (i & 0x06) >> 1, i & 0x01 ? 'A' : 'D');
   int sum = 0;
   for (j = 0; j < 16; j++)
   {
    if (jpeg->huffman[i]->num[j])
    {
     printf("\t\t%u bits:", j + 1);
     for (k = 0; k < jpeg->huffman[i]->num[j]; k++)
     {
      printf(" 0x%02x", jpeg->huffman[i]->codes[sum + k]);
     }
     printf("\n");
     sum += jpeg->huffman[i]->num[j];
    }
   }
  }
 }
 printf("Data length: %u\n", jpeg->data_len);
}

variable_types: j int

variable_types: k int

variable_types: sum int


original_code:
static int parse_sequence_header(struct mpeg_t * const mpeg)
{
 const uint8_t * const data = mpeg->data + mpeg->pos;
 if ((mpeg->pos += 8) > mpeg->len)
  return 0;
 mpeg->width = (data[0] << 4) | (data[1] >> 4);
 mpeg->height = ((data[1] & 0x0f) << 8) | data[2];
 return 1;
}

canonical_original_code:
static int parse_sequence_header(struct mpeg_t *mpeg)
{
 unsigned char *data = mpeg->data + mpeg->pos;
 if ((mpeg->pos += 8) > mpeg->len)
  return 0;
 mpeg->width = (data[0] << 4) | (data[1] >> 4);
 mpeg->height = ((data[1] & 0x0f) << 8) | data[2];
 return 1;
}

variable_types: struct mpeg_t { unsigned char *data; int len; int pos; unsigned int gop; enum { MPEG1=0, MPEG2=1 } type; unsigned short width; unsigned short height; unsigned short temporal_reference; enum { PCT_I=1, PCT_P=2, PCT_B=3, PCT_D=4 } picture_coding_type; unsigned char full_pel_forward_vector; unsigned char full_pel_backward_vector; unsigned char f_code[2][2]; unsigned char intra_dc_precision; unsigned char picture_structure; unsigned char top_field_first; unsigned char frame_pred_frame_dct; unsigned char concealment_motion_vectors; unsigned char q_scale_type; unsigned char intra_vlc_format; unsigned char alternate_scan; } *


original_code:
static int parse_gop(struct mpeg_t * const mpeg)
{
 if ((mpeg->pos += 4) > mpeg->len)
  return 0;
 mpeg->gop++;
 return 1;
}

canonical_original_code:
static int parse_gop(struct mpeg_t *mpeg)
{
 if ((mpeg->pos += 4) > mpeg->len)
  return 0;
 mpeg->gop++;
 return 1;
}

user_defined_types: struct mpeg_t { unsigned char *data; int len; int pos; unsigned int gop; enum { MPEG1=0, MPEG2=1 } type; unsigned short width; unsigned short height; unsigned short temporal_reference; enum { PCT_I=1, PCT_P=2, PCT_B=3, PCT_D=4 } picture_coding_type; unsigned char full_pel_forward_vector; unsigned char full_pel_backward_vector; unsigned char f_code[2][2]; unsigned char intra_dc_precision; unsigned char picture_structure; unsigned char top_field_first; unsigned char frame_pred_frame_dct; unsigned char concealment_motion_vectors; unsigned char q_scale_type; unsigned char intra_vlc_format; unsigned char alternate_scan; }


original_code:
static int parse_picture(struct mpeg_t * const mpeg)
{
 const uint8_t * const data = mpeg->data + mpeg->pos;
 if ((mpeg->pos += 4) > mpeg->len)
  return 0;
 mpeg->temporal_reference = (data[0] << 2) | (data[1] >> 6);
 mpeg->picture_coding_type = ((data[1] & 0x38) >> 3);
 if (mpeg->picture_coding_type == 2 || mpeg->picture_coding_type == 3)
 {
  if (mpeg->pos++ > mpeg->len)
   return 0;
  mpeg->full_pel_forward_vector = ((data[3] & 0x04) >> 2);
  mpeg->f_code[0][0] = mpeg->f_code[0][1] = ((data[3] & 0x03) << 1) | ((data[4] & 0x80) >> 7);
  if (mpeg->picture_coding_type == 3)
  {
   mpeg->full_pel_backward_vector = ((data[4] & 0x40) >> 6);
   mpeg->f_code[1][0] = mpeg->f_code[1][1] = ((data[4] & 0x38) >> 3);
  }
 }
 else
 {
  mpeg->full_pel_forward_vector = mpeg->full_pel_backward_vector = 0;
  mpeg->f_code[0][0] = mpeg->f_code[0][1] = 0;
  mpeg->f_code[1][0] = mpeg->f_code[1][1] = 0;
 }
 mpeg->picture_structure = 0x3;
 mpeg->top_field_first = 0x1;
 mpeg->frame_pred_frame_dct = 0x1;
 return 1;
}

canonical_original_code:
static int parse_picture(struct mpeg_t *mpeg)
{
 unsigned char *data = mpeg->data + mpeg->pos;
 if ((mpeg->pos += 4) > mpeg->len)
  return 0;
 mpeg->temporal_reference = (data[0] << 2) | (data[1] >> 6);
 mpeg->picture_coding_type = ((data[1] & 0x38) >> 3);
 if (mpeg->picture_coding_type == 2 || mpeg->picture_coding_type == 3)
 {
  if (mpeg->pos++ > mpeg->len)
   return 0;
  mpeg->full_pel_forward_vector = ((data[3] & 0x04) >> 2);
  mpeg->f_code[0][0] = mpeg->f_code[0][1] = ((data[3] & 0x03) << 1) | ((data[4] & 0x80) >> 7);
  if (mpeg->picture_coding_type == 3)
  {
   mpeg->full_pel_backward_vector = ((data[4] & 0x40) >> 6);
   mpeg->f_code[1][0] = mpeg->f_code[1][1] = ((data[4] & 0x38) >> 3);
  }
 }
 else
 {
  mpeg->full_pel_forward_vector = mpeg->full_pel_backward_vector = 0;
  mpeg->f_code[0][0] = mpeg->f_code[0][1] = 0;
  mpeg->f_code[1][0] = mpeg->f_code[1][1] = 0;
 }
 mpeg->picture_structure = 0x3;
 mpeg->top_field_first = 0x1;
 mpeg->frame_pred_frame_dct = 0x1;
 return 1;
}


original_code:
static int parse_extension(struct mpeg_t * const mpeg)
{
 const uint8_t * const data = mpeg->data + mpeg->pos;
 if (mpeg->pos > mpeg->len)
  return 0;
 switch (data[0] >> 4)
 {
 case 0x1:
  if ((mpeg->pos += 6)> mpeg->len)
   return 0;
  mpeg->width |= ((data[1] & 0x01) << 13) | ((data[2] & 0x80) << 5);
  mpeg->height |= ((data[2] & 0x60) << 7);
  return 1;
 case 0x2:
  return 1;
 case 0x8:
  if ((mpeg->pos += 5)> mpeg->len)
   return 0;
  mpeg->f_code[0][0] = (data[0] & 0x0f);
  mpeg->f_code[0][1] = ((data[1] & 0xf0) >> 4);
  mpeg->f_code[1][0] = (data[1] & 0x0f);
  mpeg->f_code[1][1] = ((data[2] & 0xf0) >> 4);
  mpeg->intra_dc_precision = ((data[2] & 0x0c) >> 2);
  mpeg->picture_structure = (data[2] & 0x03);
  mpeg->top_field_first = ((data[3] & 0x80) >> 7);
  mpeg->frame_pred_frame_dct = ((data[3] & 0x40) >> 6);
  mpeg->concealment_motion_vectors = ((data[3] & 0x20) >> 5);
  mpeg->q_scale_type = ((data[3] & 0x10) >> 4);
  mpeg->intra_vlc_format = ((data[3] & 0x08) >> 3);
  mpeg->alternate_scan = ((data[3] & 0x04) >> 2);
  return 1;
 default:
  fprintf(stderr, "unhandled extension: %01x\n", data[0] >> 4);
  break;
 }
 return 0;
}

canonical_original_code:
static int parse_extension(struct mpeg_t *mpeg)
{
 unsigned char *data = mpeg->data + mpeg->pos;
 if (mpeg->pos > mpeg->len)
  return 0;
 switch (data[0] >> 4)
 {
 case 0x1:
  if ((mpeg->pos += 6)> mpeg->len)
   return 0;
  mpeg->width |= ((data[1] & 0x01) << 13) | ((data[2] & 0x80) << 5);
  mpeg->height |= ((data[2] & 0x60) << 7);
  return 1;
 case 0x2:
  return 1;
 case 0x8:
  if ((mpeg->pos += 5)> mpeg->len)
   return 0;
  mpeg->f_code[0][0] = (data[0] & 0x0f);
  mpeg->f_code[0][1] = ((data[1] & 0xf0) >> 4);
  mpeg->f_code[1][0] = (data[1] & 0x0f);
  mpeg->f_code[1][1] = ((data[2] & 0xf0) >> 4);
  mpeg->intra_dc_precision = ((data[2] & 0x0c) >> 2);
  mpeg->picture_structure = (data[2] & 0x03);
  mpeg->top_field_first = ((data[3] & 0x80) >> 7);
  mpeg->frame_pred_frame_dct = ((data[3] & 0x40) >> 6);
  mpeg->concealment_motion_vectors = ((data[3] & 0x20) >> 5);
  mpeg->q_scale_type = ((data[3] & 0x10) >> 4);
  mpeg->intra_vlc_format = ((data[3] & 0x08) >> 3);
  mpeg->alternate_scan = ((data[3] & 0x04) >> 2);
  return 1;
 default:
  fprintf(stderr, "unhandled extension: %01x\n", data[0] >> 4);
  break;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall parse_mpeg(__int64 *a1)
{
int v2; // edx
int v3; // edi
__int64 v4; // r8
__int64 v5; // rax
int v6; // esi
char v7; // cl
unsigned __int8 v8; // al

v2 = *((_DWORD *)a1 + 3);
v3 = *((_DWORD *)a1 + 2);
if ( v3 <= v2 )
return 0LL;
while ( 1 )
{
v4 = *a1;
v5 = v2;
v6 = 0;
if ( v3 > v2 )
{
while ( 1 )
{
while ( 1 )
{
v7 = *(_BYTE *)(v4 + v5);
if ( v7 )
break;
v2 = ++v5;
++v6;
*((_DWORD *)a1 + 3) = v5;
if ( v3 <= (int)v5 )
goto LABEL_9;
}
if ( v6 > 1 && v7 == 1 )
break;
v2 = ++v5;
v6 = 0;
*((_DWORD *)a1 + 3) = v5;
if ( v3 <= (int)v5 )
goto LABEL_9;
}
v2 = v5 + 1;
}
LABEL_9:
*((_DWORD *)a1 + 3) = v2 + 1;
v8 = *(_BYTE *)(v4 + v2);
if ( v8 != 0xB5 )
break;
if ( !(unsigned int)parse_extension((__int64)a1) )
return 0LL;
LABEL_17:
v2 = *((_DWORD *)a1 + 3);
v3 = *((_DWORD *)a1 + 2);
if ( v2 >= v3 )
return 0LL;
}
if ( v8 > 0xB5u )
{
if ( v8 == 0xB8 )
{
if ( !(unsigned int)parse_gop(a1) )
return 0LL;
goto LABEL_17;
}
LABEL_26:
fprintf(stderr, "unhandled startcode: %02x\n", v8);
goto LABEL_17;
}
if ( !v8 )
{
if ( !(unsigned int)parse_picture(a1) )
return 0LL;
goto LABEL_17;
}
if ( v8 == 0xB3 )
{
if ( !(unsigned int)parse_sequence_header(a1) )
return 0LL;
goto LABEL_17;
}
if ( (unsigned __int8)(v8 - 1) > 0xAEu )
goto LABEL_26;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 *a1)
{
int v2; // edx
int v3; // edi
__int64 v4; // r8
__int64 v5; // rax
int v6; // esi
char v7; // cl
unsigned __int8 v8; // al

v2 = *((_DWORD *)a1 + 3);
v3 = *((_DWORD *)a1 + 2);
if ( v3 <= v2 )
return 0LL;
while ( 1 )
{
v4 = *a1;
v5 = v2;
v6 = 0;
if ( v3 > v2 )
{
while ( 1 )
{
while ( 1 )
{
v7 = *(_BYTE *)(v4 + v5);
if ( v7 )
break;
v2 = ++v5;
++v6;
*((_DWORD *)a1 + 3) = v5;
if ( v3 <= (int)v5 )
goto LABEL_9;
}
if ( v6 > 1 && v7 == 1 )
break;
v2 = ++v5;
v6 = 0;
*((_DWORD *)a1 + 3) = v5;
if ( v3 <= (int)v5 )
goto LABEL_9;
}
v2 = v5 + 1;
}
LABEL_9:
*((_DWORD *)a1 + 3) = v2 + 1;
v8 = *(_BYTE *)(v4 + v2);
if ( v8 != 0xB5 )
break;
if ( !(unsigned int)func3((__int64)a1) )
return 0LL;
LABEL_17:
v2 = *((_DWORD *)a1 + 3);
v3 = *((_DWORD *)a1 + 2);
if ( v2 >= v3 )
return 0LL;
}
if ( v8 > 0xB5u )
{
if ( v8 == 0xB8 )
{
if ( !(unsigned int)func1(a1) )
return 0LL;
goto LABEL_17;
}
LABEL_26:
fprintf(stderr, "unhandled startcode: %02x\n", v8);
goto LABEL_17;
}
if ( !v8 )
{
if ( !(unsigned int)func2(a1) )
return 0LL;
goto LABEL_17;
}
if ( v8 == 0xB3 )
{
if ( !(unsigned int)func0(a1) )
return 0LL;
goto LABEL_17;
}
if ( (unsigned __int8)(v8 - 1) > 0xAEu )
goto LABEL_26;
return 1LL;
}

original_code:
int parse_mpeg(struct mpeg_t * const mpeg)
{
 while (mpeg->pos < mpeg->len)
 {
  int zeros = 0;
  for ( ; mpeg->pos < mpeg->len; mpeg->pos++)
  {
   if (mpeg->data[mpeg->pos] == 0x00)
    zeros++;
   else if (mpeg->data[mpeg->pos] == 0x01 && zeros >= 2)
   {
    mpeg->pos++;
    break;
   }
   else
    zeros = 0;
  }
  uint8_t marker = mpeg->data[mpeg->pos++];
  switch (marker)
  {
  case 0x00:
   if (!parse_picture(mpeg))
    return 0;
   break;
  case 0xb3:
   if (!parse_sequence_header(mpeg))
    return 0;
   break;
  case 0xb5:
   if (!parse_extension(mpeg))
    return 0;
   break;
  case 0xb8:
   if (!parse_gop(mpeg))
    return 0;
   break;
  default:
   if (marker >= 0x01 && marker <= 0xaf)
    return 1;
   fprintf(stderr, "unhandled startcode: %02x\n", marker);
   break;
  }
 }
 return 0;
}

canonical_original_code:
int parse_mpeg(struct mpeg_t *mpeg)
{
 while (mpeg->pos < mpeg->len)
 {
  int zeros = 0;
  for ( ; mpeg->pos < mpeg->len; mpeg->pos++)
  {
   if (mpeg->data[mpeg->pos] == 0x00)
    zeros++;
   else if (mpeg->data[mpeg->pos] == 0x01 && zeros >= 2)
   {
    mpeg->pos++;
    break;
   }
   else
    zeros = 0;
  }
  unsigned char marker = mpeg->data[mpeg->pos++];
  switch (marker)
  {
  case 0x00:
   if (!parse_picture(mpeg))
    return 0;
   break;
  case 0xb3:
   if (!parse_sequence_header(mpeg))
    return 0;
   break;
  case 0xb5:
   if (!parse_extension(mpeg))
    return 0;
   break;
  case 0xb8:
   if (!parse_gop(mpeg))
    return 0;
   break;
  default:
   if (marker >= 0x01 && marker <= 0xaf)
    return 1;
   fprintf(stderr, "unhandled startcode: %02x\n", marker);
   break;
  }
 }
 return 0;
}

variable_types: zeros int


decompiled_code:
__int64 __fastcall read_frame(int fd, __int64 a2, int a3)
{
int i; // ebx
int v5; // eax

if ( a3 <= 0 )
return 1LL;
for ( i = 0; i < a3; i += v5 )
{
v5 = read(fd, (void *)(a2 + i), a3 - i);
if ( !v5 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, __int64 a2, int a3)
{
int i; // ebx
int v5; // eax

if ( a3 <= 0 )
return 1LL;
for ( i = 0; i < a3; i += v5 )
{
v5 = read(fd, (void *)(a2 + i), a3 - i);
if ( !v5 )
return 0LL;
}
return 1LL;
}

original_code:
static int read_frame(int fd, void *buffer, int size)
{
 int total = 0, len;
 while (total < size)
 {
  len = read(fd, buffer + total, size - total);
  if (len == 0)
   return 0;
  total += len;
 }
 return 1;
}

canonical_original_code:
static int read_frame(int fd, void *buffer, int size)
{
 int total = 0; int len;
 while (total < size)
 {
  len = read(fd, buffer + total, size - total);
  if (len == 0)
   return 0;
  total += len;
 }
 return 1;
}

variable_types: fd int

variable_types:
buffer
void *

variable_types: size int

variable_types: total int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // er12
int v5; // er13
const char *v6; // r14
int v7; // ebp
int v8; // er14
__int64 v9; // rax
__int64 v10; // r12
const void *v11; // r15
int v12; // ebx
__int64 v13; // r13
unsigned int v14; // eax
int v15[2]; // [rsp+0h] [rbp-68h] BYREF
unsigned int v16; // [rsp+8h] [rbp-60h]
unsigned int v17; // [rsp+Ch] [rbp-5Ch]
__int64 v18; // [rsp+10h] [rbp-58h]
__int64 v19; // [rsp+18h] [rbp-50h]
__int64 v20; // [rsp+20h] [rbp-48h]

if ( argc == 5 )
{
v4 = atoi(argv[2]);
v5 = atoi(argv[3]);
if ( (unsigned int)ve_open() )
{
v6 = argv[1];
v7 = strcmp(v6, "-");
if ( v7 && (v7 = open(v6, 0), v7 == -1) )
{
puts("could not open input file");
result = 1;
}
else
{
v8 = open(argv[4], 578, 420LL);
if ( v8 == -1 )
{
puts("could not open output file");
result = 1;
}
else
{
v15[0] = v4;
v15[1] = v5;
v16 = (v4 + 15) & 0xFFFFFFF0;
v17 = (v5 + 15) & 0xFFFFFFF0;
v18 = 0x4D00000000LL;
v19 = 0x100000029LL;
v20 = 0x1900000018LL;
v9 = h264enc_new(v15);
v10 = v9;
if ( v9 )
{
v11 = (const void *)h264enc_get_bytestream_buffer(v9);
v12 = v16 * (v17 + (v17 >> 1));
v13 = h264enc_get_input_buffer(v10);
while ( (unsigned int)read_frame(v7, v13, v12) )
{
if ( (unsigned int)h264enc_encode_picture(v10) )
{
v14 = h264enc_get_bytestream_length(v10);
write(v8, v11, v14);
}
else
{
puts("encoding error");
}
}
h264enc_free(v10);
}
else
{
puts("could not create encoder");
}
ve_close();
close(v8);
close(v7);
result = 0;
}
}
}
else
{
result = 1;
}
}
else
{
printf("Usage: %s <infile> <width> <height> <outfile>\n", *argv);
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // er12
int v5; // er13
const char *v6; // r14
int v7; // ebp
int v8; // er14
__int64 v9; // rax
__int64 v10; // r12
const void *v11; // r15
int v12; // ebx
__int64 v13; // r13
unsigned int v14; // eax
int v15[2]; // [rsp+0h] [rbp-68h] BYREF
unsigned int v16; // [rsp+8h] [rbp-60h]
unsigned int v17; // [rsp+Ch] [rbp-5Ch]
__int64 v18; // [rsp+10h] [rbp-58h]
__int64 v19; // [rsp+18h] [rbp-50h]
__int64 v20; // [rsp+20h] [rbp-48h]

if ( argc == 5 )
{
v4 = atoi(argv[2]);
v5 = atoi(argv[3]);
if ( (unsigned int)ve_open() )
{
v6 = argv[1];
v7 = strcmp(v6, "-");
if ( v7 && (v7 = open(v6, 0), v7 == -1) )
{
puts("could not open input file");
result = 1;
}
else
{
v8 = open(argv[4], 578, 420LL);
if ( v8 == -1 )
{
puts("could not open output file");
result = 1;
}
else
{
v15[0] = v4;
v15[1] = v5;
v16 = (v4 + 15) & 0xFFFFFFF0;
v17 = (v5 + 15) & 0xFFFFFFF0;
v18 = 0x4D00000000LL;
v19 = 0x100000029LL;
v20 = 0x1900000018LL;
v9 = h264enc_new(v15);
v10 = v9;
if ( v9 )
{
v11 = (const void *)h264enc_get_bytestream_buffer(v9);
v12 = v16 * (v17 + (v17 >> 1));
v13 = h264enc_get_input_buffer(v10);
while ( (unsigned int)func0(v7, v13, v12) )
{
if ( (unsigned int)h264enc_encode_picture(v10) )
{
v14 = h264enc_get_bytestream_length(v10);
write(v8, v11, v14);
}
else
{
puts("encoding error");
}
}
h264enc_free(v10);
}
else
{
puts("could not create encoder");
}
ve_close();
close(v8);
close(v7);
result = 0;
}
}
}
else
{
result = 1;
}
}
else
{
printf("Usage: %s <infile> <width> <height> <outfile>\n", *argv);
result = 1;
}
return result;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: in int

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:
data
unsigned char *

variable_types:
filename
char *

variable_types: struct jpeg_t { unsigned char bits; unsigned short width; unsigned short height; struct comp_t { unsigned char samp_h; unsigned char samp_v; unsigned char qt; unsigned char ht_ac; unsigned char ht_dc; } comp[3]; struct quant_t { unsigned char coeff[64]; } *quant[4]; struct huffman_t { unsigned char num[16]; unsigned char codes[256]; } *huffman[8]; unsigned short restart_interval; unsigned char *data; unsigned int data_len; }


repo: jemofthewest/ad

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
__int64 v5[5]; // [rsp+0h] [rbp-28h] BYREF

v5[0] = 0x3FDD3E4EF0281BA8LL;
v5[1] = 0x3FD937B4A2339C0FLL;
v5[2] = 0xBFE66299524BFD2FLL;
v5[3] = 0x3FED8BFC2224EDF6LL;
rosenbrock(v5, 4LL, envp);
printf("f is %f\n", v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
__int64 v5[5]; // [rsp+0h] [rbp-28h] BYREF

v5[0] = 0x3FDD3E4EF0281BA8LL;
v5[1] = 0x3FD937B4A2339C0FLL;
v5[2] = 0xBFE66299524BFD2FLL;
v5[3] = 0x3FED8BFC2224EDF6LL;
func7(v5, 4LL, envp);
printf("f is %f\n", v3);
return 0;
}

original_code:
int main(int argc, char **argv)
{
  double x[] = { 0.456928, 0.394025, -0.699536, 0.923338 };
  int n = sizeof(x) / sizeof(x[0]);
  double f;
  f = rosenbrock(x,n);
  printf("f is %f\n", f);
  return 0;
}

variable_types:
x
double []

variable_types: n int

variable_types: f double


repo: jeremie-koenig/hurd

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

if ( problems )
{
*(_QWORD *)(fail_0((const char **)problems) + 8) = free_problems;
result = problems;
free_problems = (char **)problems;
}
return result;
}

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

if ( problems )
{
*(_QWORD *)(func0((const char **)problems) + 8) = free_problems;
result = problems;
free_problems = (char **)problems;
}
return result;
}

original_code:
static void
flush_problems ()
{
  struct problem *fail (struct problem *prob)
    {
      struct problem *last = prob->prev ? fail (prob->prev) : prob;
      if (preen && device_name)
 printf ("%s: %s\n", device_name, prob->desc);
      else
 puts (prob->desc);
      free (prob->desc);
      return last;
    }
  if (problems)
    {
      fail (problems)->prev = free_problems;
      free_problems = problems;
    }
}

canonical_original_code:
static void flush_problems()
{
  struct problem *fail (struct problem *prob)
    {
      struct problem *last = prob->prev ? fail (prob->prev) : prob;
      if (preen && device_name)
 printf ("%s: %s\n", device_name, prob->desc);
      else
 puts (prob->desc);
      free (prob->desc);
      return last;
    }
  if (problems)
    {
      fail (problems)->prev = free_problems;
      free_problems = problems;
    }
}

variable_types: struct problem { char *desc; struct problem *prev; } *


retch

decompiled_code:
__int64 __fastcall retch(const char *a1)
{
flush_problems();
error(99, 0, "(internal error) %s!", a1);
return resolve_problem();
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
func1();
error(99, 0, "(internal error) %s!", a1);
return func3();
}

original_code:
static void
retch (char *reason)
{
  flush_problems ();
  error (99, 0, "(internal error) %s!", reason);
}

canonical_original_code:
static void retch(char *reason)
{
  flush_problems ();
  error (99, 0, "(internal error) %s!", reason);
}

variable_types:
reason
char *


decompiled_code:
void __fastcall resolve_problem(const char *a1, void *a2)
{
__int64 v2; // rbx
const char *v3; // rsi

v2 = problems;
if ( problems )
{
v3 = *(const char **)problems;
problems = *(_QWORD *)(problems + 8);
*(_QWORD *)(v2 + 8) = free_problems;
if ( preen && device_name )
{
printf("%s: %s", device_name, v3);
if ( !a1 )
{
LABEL_5:
putchar(10);
free(*(void **)v2);
return;
}
}
else
{
printf("%s", v3);
if ( !a1 )
goto LABEL_5;
}
printf(" (%s)\n", a1);
free(*(void **)v2);
}
else
{
retch("no more problems");
push_problem("no more problems", a2);
}
}

canonical_decompiled_code:
void __fastcall func3(const char *a1, void *a2)
{
__int64 v2; // rbx
const char *v3; // rsi

v2 = problems;
if ( problems )
{
v3 = *(const char **)problems;
problems = *(_QWORD *)(problems + 8);
*(_QWORD *)(v2 + 8) = free_problems;
if ( preen && device_name )
{
printf("%s: %s", device_name, v3);
if ( !a1 )
{
LABEL_5:
putchar(10);
free(*(void **)v2);
return;
}
}
else
{
printf("%s", v3);
if ( !a1 )
goto LABEL_5;
}
printf(" (%s)\n", a1);
free(*(void **)v2);
}
else
{
func2("no more problems");
func4("no more problems", a2);
}
}

original_code:
static void
resolve_problem (char *fix)
{
  struct problem *prob = problems;
  if (! prob)
    retch ("no more problems");
  problems = prob->prev;
  prob->prev = free_problems;
  if (preen && device_name)
    printf ("%s: %s", device_name, prob->desc);
  else
    printf ("%s", prob->desc);
  if (fix)
    printf (" (%s)\n", fix);
  else
    putchar ('\n');
  free (prob->desc);
}

canonical_original_code:
static void resolve_problem(char *fix)
{
  struct problem *prob = problems;
  if (! prob)
    retch ("no more problems");
  problems = prob->prev;
  prob->prev = free_problems;
  if (preen && device_name)
    printf ("%s: %s", device_name, prob->desc);
  else
    printf ("%s", prob->desc);
  if (fix)
    printf (" (%s)\n", fix);
  else
    putchar ('\n');
  free (prob->desc);
}

variable_types:
fix
char *

user_defined_types: struct problem { char *desc; struct problem *prev; }


decompiled_code:
char *__fastcall push_problem(char *f, __gnuc_va_list arg)
{
__gnuc_va_list v2; // rdx
char **v3; // rbx
char *result; // rax
char **v5; // rax

v2 = arg;
v3 = free_problems;
if ( free_problems )
{
problems = (__int64)free_problems[1];
}
else
{
v5 = (char **)malloc(0x10uLL);
v2 = arg;
v3 = v5;
if ( !v5 )
{
retch("malloc failed");
goto LABEL_7;
}
}
if ( vasprintf(v3, f, v2) >= 0 )
{
result = (char *)problems;
problems = (__int64)v3;
v3[1] = result;
return result;
}
LABEL_7:
retch("vasprintf failed");
return (char *)allocblk();
}

canonical_decompiled_code:
char *__fastcall func4(char *f, __gnuc_va_list arg)
{
__gnuc_va_list v2; // rdx
char **v3; // rbx
char *result; // rax
char **v5; // rax

v2 = arg;
v3 = free_problems;
if ( free_problems )
{
problems = (__int64)free_problems[1];
}
else
{
v5 = (char **)malloc(0x10uLL);
v2 = arg;
v3 = v5;
if ( !v5 )
{
func2("malloc failed");
goto LABEL_7;
}
}
if ( vasprintf(v3, f, v2) >= 0 )
{
result = (char *)problems;
problems = (__int64)v3;
v3[1] = result;
return result;
}
LABEL_7:
func2("vasprintf failed");
return (char *)func5();
}

original_code:
static void
push_problem (char *fmt, va_list args)
{
  struct problem *prob = free_problems;
  if (! prob)
    prob = malloc (sizeof (struct problem));
  else
    problems = prob->prev;
  if (! prob)
    retch ("malloc failed");
  if (vasprintf (&prob->desc, fmt, args) < 0)
    retch ("vasprintf failed");
  prob->prev = problems;
  problems = prob;
}

canonical_original_code:
static void push_problem(char *fmt, __builtin_va_list args)
{
  struct problem *prob = free_problems;
  if (! prob)
    prob = malloc (sizeof (struct problem));
  else
    problems = prob->prev;
  if (! prob)
    retch ("malloc failed");
  if (vasprintf (&prob->desc, fmt, args) < 0)
    retch ("vasprintf failed");
  prob->prev = problems;
  problems = prob;
}

variable_types:
fmt
char *


allocblk

decompiled_code:
__int64 __fastcall allocblk(int a1)
{
unsigned int v1; // er9
__int64 v2; // rax
__int64 v4; // r11
__int64 v5; // rdi
int v6; // er12
int v7; // ebp
int v8; // esi
int v9; // edx
int v10; // ecx
int v11; // er10
unsigned int v12; // er8
int v13; // eax
unsigned int v14; // esi
char v15; // cl

v1 = 0;
if ( a1 <= 0 )
return 0LL;
v2 = a1;
v4 = *(_QWORD *)(sblock + 96LL);
if ( a1 > v4 )
return 0LL;
if ( maxfsblock - v4 > 0 )
{
v5 = blockmap;
v6 = 0;
while ( 1 )
{
v7 = 0;
LABEL_6:
v1 = v6 + v7;
v8 = 0;
while ( 1 )
{
v9 = v6 + v7;
v10 = v8 + v1 + 7;
if ( (int)(v8 + v1) >= 0 )
v10 = v8 + v1;
v11 = *(char *)(blockmap + (v10 >> 3));
if ( _bittest(&v11, (int)(v8 + v1) % 8) )
break;
if ( a1 == ++v8 )
goto LABEL_11;
}
if ( a1 <= v8 )
break;
v7 += v8 + 1;
if ( v7 <= v4 - v2 )
goto LABEL_6;
v6 += v4;
if ( v6 >= maxfsblock - v4 )
return 0;
}
LABEL_11:
v12 = v1 + a1;
while ( 1 )
{
v13 = v9 + 7;
if ( v9 >= 0 )
v13 = v9;
v14 = (unsigned int)(v9 >> 31) >> 29;
v15 = v9++ + v14;
*(_BYTE *)(v5 + (v13 >> 3)) |= 1 << ((v15 & 7) - v14);
if ( v12 == v9 )
break;
v5 = blockmap;
}
}
return v1;
}

original_code:
daddr_t
allocblk (int nfrags)
{
  daddr_t i;
  int j, k;
  if (nfrags <= 0 || nfrags > sblock->fs_frag)
    return 0;
  for (i = 0; i < maxfsblock - sblock->fs_frag; i += sblock->fs_frag)
    {
      for (j = 0; j <= sblock->fs_frag - nfrags; j++)
 {
   for (k = 0; k < nfrags; k++)
     if (((blockmap)[(i + j + k)/8] & (1<<((i + j + k)%8))))
       break;
   if (k < nfrags)
     {
       j += k;
       continue;
     }
   for (k = 0; k < nfrags; k++)
     ((blockmap)[(i + j + k)/8] |= 1<<((i + j + k)%8));
   return (i + j);
 }
    }
  return 0;
}

canonical_original_code:
int allocblk(int nfrags)
{
  int i;
  int j; int k;
  if (nfrags <= 0 || nfrags > sblock->fs_frag)
    return 0;
  for (i = 0; i < maxfsblock - sblock->fs_frag; i += sblock->fs_frag)
    {
      for (j = 0; j <= sblock->fs_frag - nfrags; j++)
 {
   for (k = 0; k < nfrags; k++)
     if (((blockmap)[(i + j + k)/8] & (1<<((i + j + k)%8))))
       break;
   if (k < nfrags)
     {
       j += k;
       continue;
     }
   for (k = 0; k < nfrags; k++)
     ((blockmap)[(i + j + k)/8] |= 1<<((i + j + k)%8));
   return (i + j);
 }
    }
  return 0;
}

variable_types: nfrags int

variable_types: i int

variable_types: j int

variable_types: k int


decompiled_code:
_BOOL8 __fastcall check_range(int a1, int a2)
{
int v2; // esi
_BOOL8 result; // rax
__int64 v4; // r8
__int64 v5; // rax
__int64 v6; // rdx

v2 = a1 + a2;
result = 1LL;
if ( (unsigned int)v2 <= maxfsblock )
{
v4 = *(_QWORD *)(sblock + 360LL);
v5 = a1 / v4;
v6 = (int)v5 * (int)v4 + *(_QWORD *)(sblock + 32LL) * ((int)v5 & ~*(_QWORD *)(sblock + 40LL));
if ( a1 >= v6 + *(int *)(sblock + 28LL) )
result = v2 > (int)v4 * ((int)v5 + 1);
else
result = v2 > *(int *)(sblock + 16LL) + v6;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(int a1, int a2)
{
int v2; // esi
_BOOL8 result; // rax
__int64 v4; // r8
__int64 v5; // rax
__int64 v6; // rdx

v2 = a1 + a2;
result = 1LL;
if ( (unsigned int)v2 <= maxfsblock )
{
v4 = *(_QWORD *)(sblock + 360LL);
v5 = a1 / v4;
v6 = (int)v5 * (int)v4 + *(_QWORD *)(sblock + 32LL) * ((int)v5 & ~*(_QWORD *)(sblock + 40LL));
if ( a1 >= v6 + *(int *)(sblock + 28LL) )
result = v2 > (int)v4 * ((int)v5 + 1);
else
result = v2 > *(int *)(sblock + 16LL) + v6;
}
return result;
}

original_code:
int
check_range (daddr_t blk, int cnt)
{
  int c;
  if ((unsigned)(blk + cnt) > maxfsblock)
    return 1;
  c = ((blk) / (sblock)->fs_fpg);
  if (blk < ((((daddr_t)((sblock)->fs_fpg * (c))) + (sblock)->fs_cgoffset * ((c) & ~((sblock)->fs_cgmask))) + (sblock)->fs_dblkno))
    {
      if (blk + cnt > ((((daddr_t)((sblock)->fs_fpg * (c))) + (sblock)->fs_cgoffset * ((c) & ~((sblock)->fs_cgmask))) + (sblock)->fs_sblkno))
 return 1;
    }
  else
    {
      if (blk + cnt > ((daddr_t)((sblock)->fs_fpg * (c + 1))))
 return 1;
    }
  return 0;
}

canonical_original_code:
int check_range(int blk, int cnt)
{
  int c;
  if ((unsigned int)(blk + cnt) > maxfsblock)
    return 1;
  c = ((blk) / (sblock)->fs_fpg);
  if (blk < ((((daddr_t)((sblock)->fs_fpg * (c))) + (sblock)->fs_cgoffset * ((c) & ~((sblock)->fs_cgmask))) + (sblock)->fs_dblkno))
    {
      if (blk + cnt > ((((daddr_t)((sblock)->fs_fpg * (c))) + (sblock)->fs_cgoffset * ((c) & ~((sblock)->fs_cgmask))) + (sblock)->fs_sblkno))
 return 1;
    }
  else
    {
      if (blk + cnt > ((daddr_t)((sblock)->fs_fpg * (c + 1))))
 return 1;
    }
  return 0;
}

variable_types: blk int

variable_types: cnt int

variable_types: c int


errexit

decompiled_code:
void __noreturn errexit(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

flush_problems();
if ( preen )
{
if ( device_name )
printf("%s: ", device_name);
}
va_start(arg, format);
vprintf(format, arg);
putchar(10);
exit(8);
}

canonical_decompiled_code:
void __noreturn func7(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

func1();
if ( preen )
{
if ( device_name )
printf("%s: ", device_name);
}
va_start(arg, format);
vprintf(format, arg);
putchar(10);
exit(8);
}

original_code:
void
errexit (char *fmt, ...)
{
  va_list args;
  flush_problems ();
  if (preen && device_name)
    printf ("%s: ", device_name);
  __builtin_va_start(args,fmt);
  vprintf (fmt, args);
  __builtin_va_end(args);
  putchar ('\n');
  exit (8);
}

canonical_original_code:
void errexit(char *fmt, ...)
{
  __builtin_va_list args;
  flush_problems ();
  if (preen && device_name)
    printf ("%s: ", device_name);
  __builtin_va_start(args,fmt);
  vprintf (fmt, args);
  __builtin_va_end(args);
  putchar ('\n');
  exit (8);
}


readblock

decompiled_code:
ssize_t __fastcall readblock(unsigned int a1, void *buf, size_t nbytes)
{
ssize_t result; // rax

if ( lseek64(readfd, (int)(a1 << 9), 0) == -1 )
errexit("CANNOT SEEK TO BLOCK %ld", a1);
result = read(readfd, buf, nbytes);
if ( result != nbytes )
errexit("CANNOT READ BLOCK %ld", a1);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func8(unsigned int a1, void *buf, size_t nbytes)
{
ssize_t result; // rax

if ( lseek64(readfd, (int)(a1 << 9), 0) == -1 )
func7("CANNOT SEEK TO BLOCK %ld", a1);
result = read(readfd, buf, nbytes);
if ( result != nbytes )
func7("CANNOT READ BLOCK %ld", a1);
return result;
}

original_code:
void
readblock (daddr_t addr, void *buf, size_t size)
{
  if (lseek (readfd, addr * 512, 0) == -1)
    errexit ("CANNOT SEEK TO BLOCK %ld", addr);
  if (read (readfd, buf, size) != size)
    errexit ("CANNOT READ BLOCK %ld", addr);
}

canonical_original_code:
void readblock(int addr, void *buf, unsigned long size)
{
  if (lseek (readfd, addr * 512, 0) == -1)
    errexit ("CANNOT SEEK TO BLOCK %ld", addr);
  if (read (readfd, buf, size) != size)
    errexit ("CANNOT READ BLOCK %ld", addr);
}

variable_types: addr int

variable_types:
buf
void *

variable_types:
size
unsigned long