decompiled_code:
__int64 __fastcall audio_init(__int64 a1, __int64 a2)
{
sem_t *v2; // rax
__int64 i; // rax
unsigned int v4; // eax
unsigned int v5; // eax
__int64 result; // rax
const char *v7; // rax
const char *v8; // rax
int v9[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v10; // [rsp+8h] [rbp-30h]
__int64 v11; // [rsp+10h] [rbp-28h]
__int64 v12; // [rsp+18h] [rbp-20h]

*(_DWORD *)(a2 + 4) = 2048;
*(_DWORD *)a2 = 0;
*(_DWORD *)(a2 + 8) = 0;
*(_DWORD *)(a2 + 12) = 0;
*(_QWORD *)(a2 + 40) = malloc(0x800uLL);
*(_QWORD *)(a2 + 16) = sem_open("dogwatch-drawer", 64, 420LL, 0LL);
*(_QWORD *)(a2 + 24) = sem_open("dogwatch-writer", 64, 420LL, 0LL);
v2 = sem_open("dogwatch-detector", 64, 420LL, 0LL);
*(_QWORD *)(a2 + 32) = v2;
if ( !*(_QWORD *)(a2 + 16) )
{
fwrite("unable to create drawer semaphore\n", 1uLL, 0x22uLL, stderr);
sem_unlink("dogwatch-drawer");
exit(1);
}
if ( !*(_QWORD *)(a2 + 24) )
{
fwrite("unable to create writer semaphore\n", 1uLL, 0x22uLL, stderr);
sem_unlink("dogwatch-writer");
exit(1);
}
if ( !v2 )
{
fwrite("unable to create detector semaphore\n", 1uLL, 0x24uLL, stderr);
sem_unlink("dogwatch-detector");
exit(1);
}
if ( !*(_QWORD *)(a2 + 40) )
{
puts("Could not allocate audio buffer");
exit(1);
}
for ( i = 0LL; i != 2048; ++i )
*(_BYTE *)(*(_QWORD *)(a2 + 40) + i) = 0;
v4 = Pa_Initialize();
if ( v4 )
{
v7 = (const char *)Pa_GetErrorText(v4);
printf("PA Init Error: %s\n", v7);
exit(1);
}
v9[1] = 1;
v10 = 32LL;
v12 = 0LL;
v5 = Pa_GetDefaultInputDevice();
v9[0] = v5;
if ( v5 == -1 )
{
fwrite("Error: No default input device\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
v11 = *(_QWORD *)(Pa_GetDeviceInfo(v5) + 32);
result = Pa_OpenStream(a1, v9, 0LL, 512LL, 1LL, get_audio, 8000.0, a2);
if ( (_DWORD)result )
{
v8 = (const char *)Pa_GetErrorText((unsigned int)result);
printf("PA open default Error: %s\n", v8);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
sem_t *v2; // rax
__int64 i; // rax
unsigned int v4; // eax
unsigned int v5; // eax
__int64 result; // rax
const char *v7; // rax
const char *v8; // rax
int v9[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v10; // [rsp+8h] [rbp-30h]
__int64 v11; // [rsp+10h] [rbp-28h]
__int64 v12; // [rsp+18h] [rbp-20h]

*(_DWORD *)(a2 + 4) = 2048;
*(_DWORD *)a2 = 0;
*(_DWORD *)(a2 + 8) = 0;
*(_DWORD *)(a2 + 12) = 0;
*(_QWORD *)(a2 + 40) = malloc(0x800uLL);
*(_QWORD *)(a2 + 16) = sem_open("dogwatch-drawer", 64, 420LL, 0LL);
*(_QWORD *)(a2 + 24) = sem_open("dogwatch-writer", 64, 420LL, 0LL);
v2 = sem_open("dogwatch-detector", 64, 420LL, 0LL);
*(_QWORD *)(a2 + 32) = v2;
if ( !*(_QWORD *)(a2 + 16) )
{
fwrite("unable to create drawer semaphore\n", 1uLL, 0x22uLL, stderr);
sem_unlink("dogwatch-drawer");
exit(1);
}
if ( !*(_QWORD *)(a2 + 24) )
{
fwrite("unable to create writer semaphore\n", 1uLL, 0x22uLL, stderr);
sem_unlink("dogwatch-writer");
exit(1);
}
if ( !v2 )
{
fwrite("unable to create detector semaphore\n", 1uLL, 0x24uLL, stderr);
sem_unlink("dogwatch-detector");
exit(1);
}
if ( !*(_QWORD *)(a2 + 40) )
{
puts("Could not allocate audio buffer");
exit(1);
}
for ( i = 0LL; i != 2048; ++i )
*(_BYTE *)(*(_QWORD *)(a2 + 40) + i) = 0;
v4 = Pa_Initialize();
if ( v4 )
{
v7 = (const char *)Pa_GetErrorText(v4);
printf("PA Init Error: %s\n", v7);
exit(1);
}
v9[1] = 1;
v10 = 32LL;
v12 = 0LL;
v5 = Pa_GetDefaultInputDevice();
v9[0] = v5;
if ( v5 == -1 )
{
fwrite("Error: No default input device\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
v11 = *(_QWORD *)(Pa_GetDeviceInfo(v5) + 32);
result = Pa_OpenStream(a1, v9, 0LL, 512LL, 1LL, get_audio, 8000.0, a2);
if ( (_DWORD)result )
{
v8 = (const char *)Pa_GetErrorText((unsigned int)result);
printf("PA open default Error: %s\n", v8);
exit(1);
}
return result;
}

original_code:
void audio_init(PaStream **pstream, sound *data){
    PaStreamParameters inputConfig;
    PaError err=paNoError;
    int i,
        totalFrames,
        numSamples,
        numBytes;
    data->maxFrameIndex = totalFrames = 512 * 4;
    data->frameIndex = 0;
    data->pstart = 0;
    data->plen = 0;
    numSamples = totalFrames * 1;
    numBytes = numSamples * sizeof(SAMPLE);
    data->recorded = (SAMPLE *) malloc(numBytes);
    data->drawer = sem_open("dogwatch-drawer",0100,0644,0);
    data->writer = sem_open("dogwatch-writer",0100,0644,0);
    data->detector = sem_open("dogwatch-detector",0100,0644,0);
    if (data->drawer == ((sem_t *) 0)){
        fprintf(stderr, "unable to create drawer semaphore\n");
        sem_unlink("dogwatch-drawer");
        exit(1);
    }
    if (data->writer == ((sem_t *) 0)){
        fprintf(stderr, "unable to create writer semaphore\n");
        sem_unlink("dogwatch-writer");
        exit(1);
    }
    if (data->detector == ((sem_t *) 0)){
        fprintf(stderr, "unable to create detector semaphore\n");
        sem_unlink("dogwatch-detector");
        exit(1);
    }
    if (data->recorded == ((void *)0)){
        printf("Could not allocate audio buffer\n");
        exit(1);
    }
    for (i=0; i<numSamples; i++){
        data->recorded[i] = 0;
    }
    err = Pa_Initialize();
    if (err != paNoError){
        printf("PA Init Error: %s\n", Pa_GetErrorText(err));
        exit(1);
    }
    inputConfig.channelCount = 1;
    inputConfig.sampleFormat = ((PaSampleFormat) 0x00000020);
    inputConfig.hostApiSpecificStreamInfo=((void *)0);
    inputConfig.device = Pa_GetDefaultInputDevice();
    if (inputConfig.device == ((PaDeviceIndex)-1)){
        fprintf(stderr, "Error: No default input device\n");
        exit(1);
    }
    inputConfig.suggestedLatency = Pa_GetDeviceInfo(inputConfig.device)->defaultLowInputLatency;
    err = Pa_OpenStream(pstream,
                       &inputConfig,
                       ((void *)0),
                       8000,
                       512,
                       ((PaStreamFlags) 0x00000001),
                       get_audio,
                       data);
    if (err != paNoError){
        printf("PA open default Error: %s\n", Pa_GetErrorText(err));
        exit(1);
    }
}

canonical_original_code:
void audio_init(void **pstream, struct sound *data){
    struct PaStreamParameters inputConfig;
    int err = paNoError;
    int i; int totalFrames; int numSamples; int numBytes;
    data->maxFrameIndex = totalFrames = 512 * 4;
    data->frameIndex = 0;
    data->pstart = 0;
    data->plen = 0;
    numSamples = totalFrames * 1;
    numBytes = numSamples * sizeof(SAMPLE);
    data->recorded = (unsigned char *) malloc(numBytes);
    data->drawer = sem_open("dogwatch-drawer",0100,0644,0);
    data->writer = sem_open("dogwatch-writer",0100,0644,0);
    data->detector = sem_open("dogwatch-detector",0100,0644,0);
    if (data->drawer == ((union sem_t *) 0)){
        fprintf(stderr, "unable to create drawer semaphore\n");
        sem_unlink("dogwatch-drawer");
        exit(1);
    }
    if (data->writer == ((union sem_t *) 0)){
        fprintf(stderr, "unable to create writer semaphore\n");
        sem_unlink("dogwatch-writer");
        exit(1);
    }
    if (data->detector == ((union sem_t *) 0)){
        fprintf(stderr, "unable to create detector semaphore\n");
        sem_unlink("dogwatch-detector");
        exit(1);
    }
    if (data->recorded == ((void *)0)){
        printf("Could not allocate audio buffer\n");
        exit(1);
    }
    for (i=0; i<numSamples; i++){
        data->recorded[i] = 0;
    }
    err = Pa_Initialize();
    if (err != paNoError){
        printf("PA Init Error: %s\n", Pa_GetErrorText(err));
        exit(1);
    }
    inputConfig.channelCount = 1;
    inputConfig.sampleFormat = ((unsigned long) 0x00000020);
    inputConfig.hostApiSpecificStreamInfo=((void *)0);
    inputConfig.device = Pa_GetDefaultInputDevice();
    if (inputConfig.device == ((PaDeviceIndex)-1)){
        fprintf(stderr, "Error: No default input device\n");
        exit(1);
    }
    inputConfig.suggestedLatency = Pa_GetDeviceInfo(inputConfig.device)->defaultLowInputLatency;
    err = Pa_OpenStream(pstream,
                       &inputConfig,
                       ((void *)0),
                       8000,
                       512,
                       ((unsigned long) 0x00000001),
                       get_audio,
                       data);
    if (err != paNoError){
        printf("PA open default Error: %s\n", Pa_GetErrorText(err));
        exit(1);
    }
}

variable_types:
pstream
void **

variable_types: struct sound { int frameIndex; int maxFrameIndex; int pstart; int plen; union sem_t { char __size[32]; long __align; } *drawer; union sem_t *writer; union sem_t *detector; unsigned char *recorded; } *

variable_types: struct PaStreamParameters { int device; int channelCount; unsigned long sampleFormat; double suggestedLatency; void *hostApiSpecificStreamInfo; }

variable_types: err int

variable_types: totalFrames int

variable_types: numSamples int

variable_types: numBytes int


repo: pzl/dogwatch

decompiled_code:
int audio_start()
{
unsigned int v0; // eax
const char *v2; // rax

v0 = Pa_StartStream();
if ( v0 )
{
v2 = (const char *)Pa_GetErrorText(v0);
fprintf(stderr, "Error: could not start stream: %s\n", v2);
exit(1);
}
return puts("Recording started");
}

canonical_decompiled_code:
int func2()
{
unsigned int v0; // eax
const char *v2; // rax

v0 = Pa_StartStream();
if ( v0 )
{
v2 = (const char *)Pa_GetErrorText(v0);
fprintf(stderr, "Error: could not start stream: %s\n", v2);
exit(1);
}
return puts("Recording started");
}

original_code:
void audio_start(PaStream *stream){
    PaError err=paNoError;
    err = Pa_StartStream(stream);
    if (err != paNoError){
        fprintf(stderr, "Error: could not start stream: %s\n", Pa_GetErrorText(err));
        exit(1);
    }
    printf("Recording started\n");
}

canonical_original_code:
void audio_start(void *stream){
    int err = paNoError;
    err = Pa_StartStream(stream);
    if (err != paNoError){
        fprintf(stderr, "Error: could not start stream: %s\n", Pa_GetErrorText(err));
        exit(1);
    }
    printf("Recording started\n");
}

variable_types:
stream
void *

return_type: void


original_code:
static int sample_cpy(filebuf *from, filebuf *to, int expand){
 SAMPLE compress_val;
 int repeat,
  copied=0;
 while (from->index < from->len &&
     to->index < to->len){
  if (expand == 1 && from->buf[from->index] == 0 ){
   compress_val = from->buf[++(from->index)];
   repeat = from->buf[++(from->index)];
   while (repeat--){
    if (to->index < to->len){
     to->buf[(to->index)++] = compress_val;
     copied++;
    } else {
     from->buf[from->index] = ++repeat;
     from->index -= 2;
     break;
    }
   }
   if (repeat <= 0){
    from->index++;
   }
  } else {
   to->buf[(to->index)++] = from->buf[(from->index)++];
   copied++;
  }
 }
 return copied;
}

canonical_original_code:
static int sample_cpy(struct filebuf *from, struct filebuf *to, int expand){
 unsigned char compress_val;
 int repeat; int copied = 0;
 while (from->index < from->len &&
     to->index < to->len){
  if (expand == 1 && from->buf[from->index] == 0 ){
   compress_val = from->buf[++(from->index)];
   repeat = from->buf[++(from->index)];
   while (repeat--){
    if (to->index < to->len){
     to->buf[(to->index)++] = compress_val;
     copied++;
    } else {
     from->buf[from->index] = ++repeat;
     from->index -= 2;
     break;
    }
   }
   if (repeat <= 0){
    from->index++;
   }
  } else {
   to->buf[(to->index)++] = from->buf[(from->index)++];
   copied++;
  }
 }
 return copied;
}

variable_types: struct filebuf { unsigned char *buf; int index; int len; } *

variable_types:
to
struct filebuf *

variable_types: expand int

variable_types: repeat int

variable_types: copied int

return_type: int


decompiled_code:
void *__fastcall extend_buf(void **a1, int a2)
{
void *result; // rax

result = realloc(*a1, a2);
if ( !result )
return (void *)fwrite("Error extending buffer!\n", 1uLL, 0x18uLL, stderr);
*a1 = result;
return result;
}

canonical_decompiled_code:
void *__fastcall func2(void **a1, int a2)
{
void *result; // rax

result = realloc(*a1, a2);
if ( !result )
return (void *)fwrite("Error extending buffer!\n", 1uLL, 0x18uLL, stderr);
*a1 = result;
return result;
}

original_code:
static void extend_buf(SAMPLE **buf, int bytes){
 SAMPLE *t;
 t = realloc(*buf, bytes);
 if (t){
  *buf = t;
 } else {
  fprintf(stderr, "Error extending buffer!\n");
 }
}

canonical_original_code:
static void extend_buf(unsigned char **buf, int bytes){
 unsigned char *t;
 t = realloc(*buf, bytes);
 if (t){
  *buf = t;
 } else {
  fprintf(stderr, "Error extending buffer!\n");
 }
}

variable_types:
buf
unsigned char **

variable_types: bytes int

variable_types:
t
unsigned char *


repo: qbazd/utask

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

LODWORD(result) = 0;
do
{
tasks[13 * (int)result] = 0;
result = (unsigned int)(result + 1);
}
while ( (_DWORD)result != 10 );
return result;
}

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

LODWORD(result) = 0;
do
{
tasks[13 * (int)result] = 0;
result = (unsigned int)(result + 1);
}
while ( (_DWORD)result != 10 );
return result;
}

original_code:
void utask_init()
{
    int i;
    for (i=0;i<10;i++)
  tasks[i].state = 0;
}

variable_types: i int


decompiled_code:
void __fastcall utask_sleep(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 20) = a2;
*(_DWORD *)a1 = 3;
*(_QWORD *)(a1 + 8) = 0LL;
}

original_code:
void utask_sleep(utask_t* t,utask_timer_t ticks)
{
    t->sleep = ticks;
    t->state = 3;
  t->sem = 0;
}

canonical_original_code:
void utask_sleep(struct utask *t, unsigned int ticks)
{
    t->sleep = ticks;
    t->state = 3;
  t->sem = 0;
}

variable_types: struct utask { unsigned int state; unsigned int istate; unsigned int *sem; unsigned int sem_val; unsigned int sleep; void (*fun)(struct utask *t); unsigned int data[1]; unsigned int arg; unsigned int tid; char *name; } *

variable_types:
ticks
unsigned int


decompiled_code:
int *__fastcall utask_add(__int64 a1)
{
unsigned int v2; // ecx
int *v3; // r9
int v4; // esi
__int64 v5; // rdi

v2 = utask_free_tid;
v3 = &tasks[13 * utask_free_tid];
v4 = utask_free_slots;
if ( !utask_free_slots )
return 0LL;
tasks[13 * utask_free_tid] = 2;
v3[1] = 0;
v3[5] = 0;
v5 = 13LL * v2;
*(__int64 *)((char *)&qword_418 + 4 * v5) = a1;
dword_424[v5] = 0;
*(__int64 *)((char *)&qword_42C + 4 * v5) = (__int64)&unk_614;
LODWORD(v5) = utask_last_tid++;
dword_428[13 * v2] = v5;
utask_free_tid = v2 + 1;
utask_free_slots = v4 - 1;
++utask_last_used;
return v3;
}

canonical_decompiled_code:
int *__fastcall func2(__int64 a1)
{
unsigned int v2; // ecx
int *v3; // r9
int v4; // esi
__int64 v5; // rdi

v2 = utask_free_tid;
v3 = &tasks[13 * utask_free_tid];
v4 = utask_free_slots;
if ( !utask_free_slots )
return 0LL;
tasks[13 * utask_free_tid] = 2;
v3[1] = 0;
v3[5] = 0;
v5 = 13LL * v2;
*(__int64 *)((char *)&qword_418 + 4 * v5) = a1;
dword_424[v5] = 0;
*(__int64 *)((char *)&qword_42C + 4 * v5) = (__int64)&unk_614;
LODWORD(v5) = utask_last_tid++;
dword_428[13 * v2] = v5;
utask_free_tid = v2 + 1;
utask_free_slots = v4 - 1;
++utask_last_used;
return v3;
}

original_code:
utask_t* utask_add(utask_fun fun)
{
 utask_t* t = &tasks[utask_free_tid];
 if (!utask_free_slots)
  return 0;
    t->state = 2;
    t->istate = 0;
    t->sleep = 0;
 t->fun = fun;
  t->arg = 0;
  t->name = "";
  t->tid = utask_last_tid++;
  utask_free_tid++;
  utask_free_slots--;
  utask_last_used++;
 return t;
}

canonical_original_code:
struct utask *utask_add(void (*fun)(struct utask *t))
{
 struct utask *t = &tasks[utask_free_tid];
 if (!utask_free_slots)
  return 0;
    t->state = 2;
    t->istate = 0;
    t->sleep = 0;
 t->fun = fun;
  t->arg = 0;
  t->name = "";
  t->tid = utask_last_tid++;
  utask_free_tid++;
  utask_free_slots--;
  utask_last_used++;
 return t;
}

variable_types:
fun
void (*)(struct utask *t)

return_type: struct utask { unsigned int state; unsigned int istate; unsigned int *sem; unsigned int sem_val; unsigned int sleep; void (*fun)(struct utask *t); unsigned int data[1]; unsigned int arg; unsigned int tid; char *name; } *


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

result = utask_add(a1);
if ( result )
*(_QWORD *)(result + 11) = a2;
return result;
}

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

result = func2(a1);
if ( result )
*(_QWORD *)(result + 11) = a2;
return result;
}

original_code:
utask_t* utask_add_name(utask_fun fun, const char* name)
{
 utask_t* n = utask_add(fun);
 if (n)
  n->name = name;
 return n;
}

canonical_original_code:
struct utask *utask_add_name(void (*fun)(struct utask *t), char *name)
{
 struct utask *n = utask_add(fun);
 if (n)
  n->name = name;
 return n;
}

variable_types:
name
char *

user_defined_types: struct utask { unsigned int state; unsigned int istate; unsigned int *sem; unsigned int sem_val; unsigned int sleep; void (*fun)(struct utask *t); unsigned int data[1]; unsigned int arg; unsigned int tid; char *name; }


decompiled_code:
__int64 utask_check_sleep()
{
unsigned int v0; // edi
int v1; // edx
int *i; // rax

v0 = -1;
v1 = 0;
for ( i = tasks; *i != 2; i += 13 )
{
if ( (unsigned int)*i > 2 && i[5] < v0 )
v0 = i[5];
if ( ++v1 > (unsigned int)utask_last_used )
return v0;
}
return 0;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // edi
int v1; // edx
int *i; // rax

v0 = -1;
v1 = 0;
for ( i = tasks; *i != 2; i += 13 )
{
if ( (unsigned int)*i > 2 && i[5] < v0 )
v0 = i[5];
if ( ++v1 > (unsigned int)utask_last_used )
return v0;
}
return 0;
}

original_code:
utask_data_t utask_check_sleep()
{
 utask_t* t = tasks;
 utask_data_t i;
 utask_timer_t s = 0xFFFFFFFF;
 for (i=0;i<=utask_last_used;++i,++t)
 {
  if (t->state == 2)
   return 0;
  if (t->state >= 3)
   if (t->sleep < s)
    s = t->sleep;
 }
 return s;
}

canonical_original_code:
unsigned int utask_check_sleep()
{
 struct utask *t = tasks;
 unsigned int i;
 unsigned int s = 0xFFFFFFFF;
 for (i=0;i<=utask_last_used;++i,++t)
 {
  if (t->state == 2)
   return 0;
  if (t->state >= 3)
   if (t->sleep < s)
    s = t->sleep;
 }
 return s;
}

variable_types:
i
unsigned int

variable_types:
s
unsigned int

return_type: unsigned int


decompiled_code:
__int64 utask_sleep_process()
{
int v0; // esi
__int64 result; // rax

v0 = utask_last_used + 1;
LODWORD(result) = 0;
do
{
if ( (unsigned int)tasks[13 * (int)result] > 2 )
{
if ( tasks[13 * (int)result + 5] )
--tasks[13 * (int)result + 5];
else
tasks[13 * (int)result] = 2;
}
result = (unsigned int)(result + 1);
}
while ( (_DWORD)result != v0 );
return result;
}

canonical_decompiled_code:
__int64 func6()
{
int v0; // esi
__int64 result; // rax

v0 = utask_last_used + 1;
LODWORD(result) = 0;
do
{
if ( (unsigned int)tasks[13 * (int)result] > 2 )
{
if ( tasks[13 * (int)result + 5] )
--tasks[13 * (int)result + 5];
else
tasks[13 * (int)result] = 2;
}
result = (unsigned int)(result + 1);
}
while ( (_DWORD)result != v0 );
return result;
}

original_code:
void utask_sleep_process()
{
 int i;
 for (i=0;i<=utask_last_used;i++)
 {
  if (tasks[i].state < 3)
   continue;
  if (tasks[i].sleep > 0)
   tasks[i].sleep--;
  else
   tasks[i].state = 2;
 }
}


decompiled_code:
__int64 __fastcall utask_exit(_DWORD *a1)
{
char v1; // r8
int v2; // er9
char v3; // r10
unsigned int v4; // esi
__int64 result; // rax

*a1 = 0;
++utask_free_slots;
utask_free_tid = 10;
utask_last_used = 0;
v1 = 0;
v2 = 0;
v3 = 0;
v4 = 10;
LODWORD(result) = 0;
do
{
if ( tasks[13 * (int)result] )
{
v2 = result;
v1 = 1;
}
else if ( v4 > (unsigned int)result )
{
v4 = result;
v3 = 1;
}
result = (unsigned int)(result + 1);
}
while ( (_DWORD)result != 10 );
if ( v3 )
utask_free_tid = v4;
if ( v1 )
utask_last_used = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1)
{
char v1; // r8
int v2; // er9
char v3; // r10
unsigned int v4; // esi
__int64 result; // rax

*a1 = 0;
++utask_free_slots;
utask_free_tid = 10;
utask_last_used = 0;
v1 = 0;
v2 = 0;
v3 = 0;
v4 = 10;
LODWORD(result) = 0;
do
{
if ( tasks[13 * (int)result] )
{
v2 = result;
v1 = 1;
}
else if ( v4 > (unsigned int)result )
{
v4 = result;
v3 = 1;
}
result = (unsigned int)(result + 1);
}
while ( (_DWORD)result != 10 );
if ( v3 )
utask_free_tid = v4;
if ( v1 )
utask_last_used = v2;
return result;
}

original_code:
void utask_exit(utask_t* t)
{
 int i;
 t->state = 0;
 utask_free_slots++;
 utask_free_tid = 10;
 utask_last_used = 0;
 for (i=0;i<10;i++)
 {
  if (tasks[i].state == 0)
  {
   if (utask_free_tid > i)
    utask_free_tid = i;
  }
  else
   utask_last_used = i;
 }
}

canonical_original_code:
void utask_exit(struct utask *t)
{
 int i;
 t->state = 0;
 utask_free_slots++;
 utask_free_tid = 10;
 utask_last_used = 0;
 for (i=0;i<10;i++)
 {
  if (tasks[i].state == 0)
  {
   if (utask_free_tid > i)
    utask_free_tid = i;
  }
  else
   utask_last_used = i;
 }
}


original_code:
void utask_wait_eq(utask_t* t, utask_sem_t* sem, utask_sem_t sem_val, utask_timer_t timeout)
{
 t->sem = sem;
 t->sleep = timeout;
 t->state = 4;
 t->sem_val = sem_val;
}

canonical_original_code:
void utask_wait_eq(struct utask *t, unsigned int *sem, unsigned int sem_val, unsigned int timeout)
{
 t->sem = sem;
 t->sleep = timeout;
 t->state = 4;
 t->sem_val = sem_val;
}

variable_types:
sem
unsigned int *

variable_types:
timeout
unsigned int


original_code:
void utask_wait_neq(utask_t* t, utask_sem_t* sem, utask_sem_t sem_val, utask_timer_t timeout)
{
 t->sem = sem;
 t->sleep = timeout;
 t->state = 5;
 t->sem_val = sem_val;
}

canonical_original_code:
void utask_wait_neq(struct utask *t, unsigned int *sem, unsigned int sem_val, unsigned int timeout)
{
 t->sem = sem;
 t->sleep = timeout;
 t->state = 5;
 t->sem_val = sem_val;
}


decompiled_code:
__int64 utask_get_free_slots()
{
return (unsigned int)utask_free_slots;
}

canonical_decompiled_code:
__int64 func12()
{
return (unsigned int)utask_free_slots;
}

original_code:
utask_data_t utask_get_free_slots(void)
{
 return utask_free_slots;
}

canonical_original_code:
unsigned int utask_get_free_slots()
{
 return utask_free_slots;
}


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

if ( !utask_last_used )
return 0;
v1 = &qword_418;
v2 = 0;
do
{
v2 += *v1 == a1;
v1 = (__int64 *)((char *)v1 + 52);
}
while ( v1 != (__int64 *)((char *)&unk_44C + 52 * (unsigned int)(utask_last_used - 1)) );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
__int64 *v1; // rax
unsigned int v2; // edx

if ( !utask_last_used )
return 0;
v1 = &qword_418;
v2 = 0;
do
{
v2 += *v1 == a1;
v1 = (__int64 *)((char *)v1 + 52);
}
while ( v1 != (__int64 *)((char *)&unk_44C + 52 * (unsigned int)(utask_last_used - 1)) );
return v2;
}

original_code:
utask_data_t utask_get_task_cnt(utask_fun fun)
{
    int i;
    int cnt = 0;
 for (i=0;i<utask_last_used;i++)
  if (tasks[i].fun == fun)
   cnt++;
 return cnt;
}

canonical_original_code:
unsigned int utask_get_task_cnt(void (*fun)(struct utask *t))
{
    int i;
    int cnt = 0;
 for (i=0;i<utask_last_used;i++)
  if (tasks[i].fun == fun)
   cnt++;
 return cnt;
}

variable_types: void (*)(struct utask { unsigned int state; unsigned int istate; unsigned int *sem; unsigned int sem_val; unsigned int sleep; void (*fun)(struct utask *t); unsigned int data[1]; unsigned int arg; unsigned int tid; char *name; } *t)

variable_types: cnt int


decompiled_code:
int *__fastcall utask_get_by_tid(int a1)
{
int *v1; // rdx
int v2; // eax

if ( !utask_last_used )
return 0LL;
v1 = dword_428;
v2 = 0;
while ( *v1 != a1 )
{
++v2;
v1 += 13;
if ( v2 == utask_last_used )
return 0LL;
}
return &tasks[13 * v2];
}

canonical_decompiled_code:
int *__fastcall func14(int a1)
{
int *v1; // rdx
int v2; // eax

if ( !utask_last_used )
return 0LL;
v1 = dword_428;
v2 = 0;
while ( *v1 != a1 )
{
++v2;
v1 += 13;
if ( v2 == utask_last_used )
return 0LL;
}
return &tasks[13 * v2];
}

original_code:
utask_t* utask_get_by_tid(utask_data_t tid)
{
 int i;
 for (i=0;i<utask_last_used;i++)
  if (tasks[i].tid == tid)
   return tasks+i;
 return 0;
}

canonical_original_code:
struct utask *utask_get_by_tid(unsigned int tid)
{
 int i;
 for (i=0;i<utask_last_used;i++)
  if (tasks[i].tid == tid)
   return tasks+i;
 return 0;
}

variable_types:
tid
unsigned int


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

v1 = sysconf(30);
return (v1 + a1 - 1) & -v1;
}

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

v1 = sysconf(30);
return (v1 + a1 - 1) & -v1;
}

original_code:
static inline size_t page_align(size_t size)
{
    size_t page_size = sysconf(_SC_PAGESIZE);
    return (size + page_size - 1) & ~(page_size - 1);
}

canonical_original_code:
static inline unsigned long page_align(unsigned long size)
{
    unsigned long page_size = sysconf(_SC_PAGESIZE);
    return (size + page_size - 1) & ~(page_size - 1);
}

variable_types:
size
unsigned long

return_type: unsigned long


original_code:
static int
lvm_parse_pv(struct vg *vg, const char *name, int pvs, uint64_t start)
{
    int i, err;
    struct pv *pv;
    pv = ((void *)0);
    if (!vg->pvs) {
        vg->pvs = calloc(pvs, sizeof(struct pv));
        if (!vg->pvs)
            return -12;
    }
    for (i = 0; i < pvs; i++) {
        pv = vg->pvs + i;
        if (!pv->name[0])
            break;
        if (!strcmp(pv->name, name))
            return -17;
    }
    if (!pv)
        return -2;
    if (i == pvs)
        return -12;
    err = lvm_copy_name(pv->name, name, sizeof(pv->name) - 1);
    if (err)
        return err;
    pv->start = start;
    return 0;
}

canonical_original_code:
static int lvm_parse_pv(struct vg *vg, char *name, int pvs, unsigned long start)
{
    int i; int err;
    struct pv *pv;
    pv = ((void *)0);
    if (!vg->pvs) {
        vg->pvs = calloc(pvs, sizeof(struct pv));
        if (!vg->pvs)
            return -12;
    }
    for (i = 0; i < pvs; i++) {
        pv = vg->pvs + i;
        if (!pv->name[0])
            break;
        if (!strcmp(pv->name, name))
            return -17;
    }
    if (!pv)
        return -2;
    if (i == pvs)
        return -12;
    err = lvm_copy_name(pv->name, name, sizeof(pv->name) - 1);
    if (err)
        return err;
    pv->start = start;
    return 0;
}

variable_types: struct vg { char name[256]; unsigned long extent_size; int pv_cnt; struct pv { char name[256]; unsigned long start; } *pvs; int lv_cnt; struct lv { char name[256]; unsigned long size; unsigned int segments; struct lv_segment { unsigned char type; char device[256]; unsigned long pe_start; unsigned long pe_size; } first_segment; } *lvs; } *

variable_types: pvs int

variable_types:
start
unsigned long

variable_types: err int

user_defined_types: struct vg { char name[256]; unsigned long extent_size; int pv_cnt; struct pv *pvs; int lv_cnt; struct lv *lvs; }

user_defined_types: struct lv { char name[256]; unsigned long size; unsigned int segments; struct lv_segment first_segment; }


decompiled_code:
int __fastcall lvm_parse_lv_devices(__int64 a1, __int64 a2, const char *a3)
{
size_t i; // rbx
int v5; // eax
__int64 v6; // rbx
__int64 v7; // r15
int result; // eax
__int64 v9; // rcx
__int64 v10; // [rsp+8h] [rbp-40h]

for ( i = 0LL; strlen(a3) > i; ++i )
{
if ( strchr(",()", a3[i]) )
a3[i] = 32;
}
if ( (unsigned int)__isoc99_sscanf() == 2 )
{
v5 = *(_DWORD *)(a1 + 264);
if ( v5 <= 0 )
goto LABEL_12;
v6 = *(_QWORD *)(a1 + 272);
v7 = v6 + 264LL * (unsigned int)(v5 - 1) + 264;
while ( 1 )
{
result = strcmp((const char *)v6, (const char *)(a2 + 1));
if ( !result )
break;
v6 += 264LL;
if ( v6 == v7 )
goto LABEL_12;
}
v9 = *(_QWORD *)(v6 + 256);
if ( v9 == -1 )
{
LABEL_12:
syslog(6, "%s: invalid pe_start value\n", "lvm_parse_lv_devices");
result = -22;
}
else
{
*(_QWORD *)(a2 + 264) = v9 + v10 * *(_QWORD *)(a1 + 256);
}
}
else
{
syslog(6, "%s: sscanf failed on '%s'\n", "lvm_parse_lv_devices", a3);
result = -22;
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1, __int64 a2, const char *a3)
{
size_t i; // rbx
int v5; // eax
__int64 v6; // rbx
__int64 v7; // r15
int result; // eax
__int64 v9; // rcx
__int64 v10; // [rsp+8h] [rbp-40h]

for ( i = 0LL; strlen(a3) > i; ++i )
{
if ( strchr(",()", a3[i]) )
a3[i] = 32;
}
if ( (unsigned int)__isoc99_sscanf() == 2 )
{
v5 = *(_DWORD *)(a1 + 264);
if ( v5 <= 0 )
goto LABEL_12;
v6 = *(_QWORD *)(a1 + 272);
v7 = v6 + 264LL * (unsigned int)(v5 - 1) + 264;
while ( 1 )
{
result = strcmp((const char *)v6, (const char *)(a2 + 1));
if ( !result )
break;
v6 += 264LL;
if ( v6 == v7 )
goto LABEL_12;
}
v9 = *(_QWORD *)(v6 + 256);
if ( v9 == -1 )
{
LABEL_12:
syslog(6, "%s: invalid pe_start value\n", "lvm_parse_lv_devices");
result = -22;
}
else
{
*(_QWORD *)(a2 + 264) = v9 + v10 * *(_QWORD *)(a1 + 256);
}
}
else
{
syslog(6, "%s: sscanf failed on '%s'\n", "lvm_parse_lv_devices", a3);
result = -22;
}
return result;
}

original_code:
static int
lvm_parse_lv_devices(struct vg *vg, struct lv_segment *seg, char *devices)
{
    int i;
    uint64_t start, pe_start;
    for (i = 0; i < strlen(devices); i++)
        if (strchr(",()", devices[i]))
            devices[i] = ' ';
    if (sscanf(devices, "%255s" " %" "l" "u", seg->device, &start) != 2) {
        do { syslog(6, "%s: " "sscanf failed on '%s'\n", __func__, devices); } while (0);
        return -22;
    }
    pe_start = -1;
    for (i = 0; i < vg->pv_cnt; i++)
        if (!strcmp(vg->pvs[i].name, seg->device)) {
            pe_start = vg->pvs[i].start;
            break;
        }
    if (pe_start == -1) {
        do { syslog(6, "%s: " "invalid pe_start value\n", __func__); } while (0);
        return -22;
    }
    seg->pe_start = (start * vg->extent_size) + pe_start;
    return 0;
}

canonical_original_code:
static int lvm_parse_lv_devices(struct vg *vg, struct lv_segment *seg, char *devices)
{
    int i;
    unsigned long start; unsigned long pe_start;
    for (i = 0; i < strlen(devices); i++)
        if (strchr(",()", devices[i]))
            devices[i] = ' ';
    if (sscanf(devices, "%255s" " %" "l" "u", seg->device, &start) != 2) {
        do { syslog(6, "%s: " "sscanf failed on '%s'\n", __func__, devices); } while (0);
        return -22;
    }
    pe_start = -1;
    for (i = 0; i < vg->pv_cnt; i++)
        if (!strcmp(vg->pvs[i].name, seg->device)) {
            pe_start = vg->pvs[i].start;
            break;
        }
    if (pe_start == -1) {
        do { syslog(6, "%s: " "invalid pe_start value\n", __func__); } while (0);
        return -22;
    }
    seg->pe_start = (start * vg->extent_size) + pe_start;
    return 0;
}

variable_types:
seg
struct lv_segment *

variable_types:
devices
char *

variable_types: i int

user_defined_types: struct pv { char name[256]; unsigned long start; }

user_defined_types: struct lv_segment { unsigned char type; char device[256]; unsigned long pe_start; unsigned long pe_size; }


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

free(*(void **)(a1 + 288));
free(*(void **)(a1 + 272));
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 288) = 0LL;
result = 0LL;
memset(
(void *)((a1 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 296) >> 3));
return result;
}

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

free(*(void **)(a1 + 288));
free(*(void **)(a1 + 272));
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 288) = 0LL;
result = 0LL;
memset(
(void *)((a1 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 296) >> 3));
return result;
}

original_code:
void lvm_free_vg(struct vg *vg)
{
    free(vg->lvs);
    free(vg->pvs);
    memset(vg, 0, sizeof(*vg));
}


decompiled_code:
__int64 __fastcall lvm_scan_vg(char *s2, __int64 a2)
{
unsigned int v2; // ebp

*(_QWORD *)a2 = 0LL;
*(_QWORD *)(a2 + 288) = 0LL;
memset(
(void *)((a2 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a2 - (((_DWORD)a2 + 8) & 0xFFFFFFF8) + 296) >> 3));
v2 = lvm_open_vg(s2, (char *)a2);
if ( !v2 )
{
v2 = lvm_scan_lvs(a2);
if ( v2 )
lvm_free_vg(a2);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func16(char *s2, __int64 a2)
{
unsigned int v2; // ebp

*(_QWORD *)a2 = 0LL;
*(_QWORD *)(a2 + 288) = 0LL;
memset(
(void *)((a2 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a2 - (((_DWORD)a2 + 8) & 0xFFFFFFF8) + 296) >> 3));
v2 = func15(s2, (char *)a2);
if ( !v2 )
{
v2 = func12(a2);
if ( v2 )
func14(a2);
}
return v2;
}

original_code:
int lvm_scan_vg(const char *vg_name, struct vg *vg)
{
    int err;
    memset(vg, 0, sizeof(*vg));
    err = lvm_open_vg(vg_name, vg);
    if (err)
        return err;
    err = lvm_scan_lvs(vg);
    if (err) {
        lvm_free_vg(vg);
        return err;
    }
    return 0;
}

canonical_original_code:
int lvm_scan_vg(char *vg_name, struct vg *vg)
{
    int err;
    memset(vg, 0, sizeof(*vg));
    err = lvm_open_vg(vg_name, vg);
    if (err)
        return err;
    err = lvm_scan_lvs(vg);
    if (err) {
        lvm_free_vg(vg);
        return err;
    }
    return 0;
}


original_code:
static int
format_partition(const char *image, int type, struct partition_table *pt)
{
    uint64_t lend;
    uint32_t start, end;
    int ret, sec_size, fd;
    unsigned int cylinders;
    struct hd_geometry geo;
    struct primary_partition *pp;
    struct partition_geometry pgeo;
    unsigned long long bytes, llcyls;
    ret = 1;
    fd = -1;
    memset(pt, 0, sizeof(*pt));
    pp = pt->partitions;
    srandom(time(((void *)0)));
    fd = open(image, 02);
    if (fd == -1)
        goto out;
    if (ioctl(fd, 0x0301, &geo))
        goto out;
    if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((0x12)) << (0 +8)) | (((114)) << 0) | ((((sizeof(size_t)))) << ((0 +8)+8))), &bytes))
        goto out;
    if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | (((0x12)) << (0 +8)) | (((104)) << 0) | ((0) << ((0 +8)+8))), &sec_size))
        goto out;
    llcyls = (bytes >> 9) / ((sec_size >> 9) * geo.heads * geo.sectors);
    cylinders = llcyls;
    if (cylinders != llcyls)
        cylinders = ~0;
    pgeo.heads = geo.heads;
    pgeo.sectors = geo.sectors;
    pgeo.cylinders = cylinders;
    start = pgeo.sectors;
    lend = geo.heads * geo.sectors * llcyls - 1;
    end = lend;
    if (end != lend)
        end = ~0;
    pp->status = 0x80;
    pp->type = type;
    pp->lba = start;
    pp->blocks = end - start + 1;
    pp->chs_first = lba_to_chs(&pgeo, start);
    pp->chs_last = lba_to_chs(&pgeo, lend);
    pt->mbr_signature = 0xAA55;
    pt->disk_signature = random();
    partition_table_out(pt);
    if (write(fd, pt, sizeof(*pt)) != sizeof(*pt)) {
        (*__errno_location ()) = (*__errno_location ()) ? : 5;
        goto out;
    }
    ret = 0;
  out:
    close(fd);
    return ret;
}

canonical_original_code:
static int format_partition(char *image, int type, struct partition_table *pt)
{
    unsigned long lend;
    unsigned int start; unsigned int end;
    int ret; int sec_size; int fd;
    unsigned int cylinders;
    struct hd_geometry geo;
    struct primary_partition *pp;
    struct partition_geometry pgeo;
    unsigned long long bytes; unsigned long long llcyls;
    ret = 1;
    fd = -1;
    memset(pt, 0, sizeof(*pt));
    pp = pt->partitions;
    srandom(time(((void *)0)));
    fd = open(image, 02);
    if (fd == -1)
        goto out;
    if (ioctl(fd, 0x0301, &geo))
        goto out;
    if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((0x12)) << (0 +8)) | (((114)) << 0) | ((((sizeof(unsigned long)))) << ((0 +8)+8))), &bytes))
        goto out;
    if (ioctl(fd, (((0U) << (((0 +8)+8)+14)) | (((0x12)) << (0 +8)) | (((104)) << 0) | ((0) << ((0 +8)+8))), &sec_size))
        goto out;
    llcyls = (bytes >> 9) / ((sec_size >> 9) * geo.heads * geo.sectors);
    cylinders = llcyls;
    if (cylinders != llcyls)
        cylinders = ~0;
    pgeo.heads = geo.heads;
    pgeo.sectors = geo.sectors;
    pgeo.cylinders = cylinders;
    start = pgeo.sectors;
    lend = geo.heads * geo.sectors * llcyls - 1;
    end = lend;
    if (end != lend)
        end = ~0;
    pp->status = 0x80;
    pp->type = type;
    pp->lba = start;
    pp->blocks = end - start + 1;
    pp->chs_first = lba_to_chs(&pgeo, start);
    pp->chs_last = lba_to_chs(&pgeo, lend);
    pt->mbr_signature = 0xAA55;
    pt->disk_signature = random();
    partition_table_out(pt);
    if (write(fd, pt, sizeof(*pt)) != sizeof(*pt)) {
        (*__errno_location ()) = (*__errno_location ()) ? : 5;
        goto out;
    }
    ret = 0;
  out:
    close(fd);
    return ret;
}

variable_types:
image
char *

variable_types: type int

variable_types: struct partition_table { unsigned char code[440]; unsigned int disk_signature; unsigned char pad[2]; struct primary_partition { unsigned char status; struct partition_chs { unsigned char chs[3]; } chs_first; unsigned char type; struct partition_chs chs_last; unsigned int lba; unsigned int blocks; } partitions[4]; unsigned short mbr_signature; } *

variable_types:
lend
unsigned long

variable_types:
start
unsigned int

variable_types:
end
unsigned int

variable_types: ret int

variable_types: fd int

variable_types:
cylinders
unsigned int

variable_types: struct hd_geometry { unsigned char heads; unsigned char sectors; unsigned short cylinders; unsigned long start; }

variable_types: struct primary_partition { unsigned char status; struct partition_chs { unsigned char chs[3]; } chs_first; unsigned char type; struct partition_chs chs_last; unsigned int lba; unsigned int blocks; } *

variable_types: struct partition_geometry { unsigned char heads; unsigned char sectors; unsigned int cylinders; }

variable_types:
bytes
unsigned long long

variable_types:
llcyls
unsigned long long

user_defined_types: struct partition_table { unsigned char code[440]; unsigned int disk_signature; unsigned char pad[2]; struct primary_partition partitions[4]; unsigned short mbr_signature; }

user_defined_types: struct primary_partition { unsigned char status; struct partition_chs chs_first; unsigned char type; struct partition_chs chs_last; unsigned int lba; unsigned int blocks; }


decompiled_code:
__int64 __fastcall count_partitions(const char *a1, _DWORD *a2)
{
int v2; // eax
int v3; // ebx
unsigned int v4; // er12
int *v5; // rax
int v6; // edx
char *v8; // rax
_BYTE v9[64]; // [rsp+0h] [rbp-218h] BYREF
char v10; // [rsp+40h] [rbp-1D8h] BYREF

v2 = open(a1, 0);
v3 = v2;
v4 = 1;
if ( v2 != -1 )
{
if ( read(v2, v9, 0x200uLL) == 512 )
{
partition_table_in(v9);
v4 = partition_table_validate(v9);
if ( v4 )
{
*a2 = 0;
v4 = 0;
}
else
{
*a2 = 0;
v8 = v9;
do
{
if ( v8[450] )
++*a2;
v8 += 16;
}
while ( v8 != &v10 );
}
}
else
{
v5 = _errno_location();
v6 = *v5;
if ( !*v5 )
v6 = 5;
*v5 = v6;
v4 = 1;
}
}
close(v3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, _DWORD *a2)
{
int v2; // eax
int v3; // ebx
unsigned int v4; // er12
int *v5; // rax
int v6; // edx
char *v8; // rax
_BYTE v9[64]; // [rsp+0h] [rbp-218h] BYREF
char v10; // [rsp+40h] [rbp-1D8h] BYREF

v2 = open(a1, 0);
v3 = v2;
v4 = 1;
if ( v2 != -1 )
{
if ( read(v2, v9, 0x200uLL) == 512 )
{
partition_table_in(v9);
v4 = partition_table_validate(v9);
if ( v4 )
{
*a2 = 0;
v4 = 0;
}
else
{
*a2 = 0;
v8 = v9;
do
{
if ( v8[450] )
++*a2;
v8 += 16;
}
while ( v8 != &v10 );
}
}
else
{
v5 = _errno_location();
v6 = *v5;
if ( !*v5 )
v6 = 5;
*v5 = v6;
v4 = 1;
}
}
close(v3);
return v4;
}

original_code:
static int count_partitions(const char *image, int *count)
{
    int i, fd, ret;
    struct partition_table pt;
    ret = 1;
    fd = -1;
    fd = open(image, 00);
    if (fd == -1)
        goto out;
    if (read(fd, &pt, sizeof(pt)) != sizeof(pt)) {
        (*__errno_location ()) = (*__errno_location ()) ? : 5;
        goto out;
    }
    partition_table_in(&pt);
    if (partition_table_validate(&pt)) {
        *count = 0;
        goto done;
    }
    *count = 0;
    for (i = 0; i < 4; i++)
        if (pt.partitions[i].type)
            (*count)++;
  done:
    ret = 0;
  out:
    close(fd);
    return ret;
}

canonical_original_code:
static int count_partitions(char *image, int *count)
{
    int i; int fd; int ret;
    struct partition_table pt;
    ret = 1;
    fd = -1;
    fd = open(image, 00);
    if (fd == -1)
        goto out;
    if (read(fd, &pt, sizeof(pt)) != sizeof(pt)) {
        (*__errno_location ()) = (*__errno_location ()) ? : 5;
        goto out;
    }
    partition_table_in(&pt);
    if (partition_table_validate(&pt)) {
        *count = 0;
        goto done;
    }
    *count = 0;
    for (i = 0; i < 4; i++)
        if (pt.partitions[i].type)
            (*count)++;
  done:
    ret = 0;
  out:
    close(fd);
    return ret;
}

variable_types:
count
int *

variable_types: struct partition_table { unsigned char code[440]; unsigned int disk_signature; unsigned char pad[2]; struct primary_partition { unsigned char status; struct partition_chs { unsigned char chs[3]; } chs_first; unsigned char type; struct partition_chs chs_last; unsigned int lba; unsigned int blocks; } partitions[4]; unsigned short mbr_signature; }


decompiled_code:
__int64 __fastcall dump_signature(const char *a1, int a2)
{
int v2; // eax
int v3; // ebx
unsigned int v4; // ebp
int *v5; // rax
int v6; // edx
_BYTE v8[536]; // [rsp+0h] [rbp-218h] BYREF

v2 = open(a1, 0);
v3 = v2;
v4 = 1;
if ( v2 != -1 )
{
if ( read(v2, v8, 0x200uLL) == 512 )
{
partition_table_in(v8);
v4 = partition_table_validate(v8);
if ( v4 )
{
*_errno_location() = 22;
puts("table invalid");
v4 = 1;
}
else
{
_dump_signature((__int64)v8, a2);
}
}
else
{
v5 = _errno_location();
v6 = *v5;
if ( !*v5 )
v6 = 5;
*v5 = v6;
v4 = 1;
}
}
close(v3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1, int a2)
{
int v2; // eax
int v3; // ebx
unsigned int v4; // ebp
int *v5; // rax
int v6; // edx
_BYTE v8[536]; // [rsp+0h] [rbp-218h] BYREF

v2 = open(a1, 0);
v3 = v2;
v4 = 1;
if ( v2 != -1 )
{
if ( read(v2, v8, 0x200uLL) == 512 )
{
partition_table_in(v8);
v4 = partition_table_validate(v8);
if ( v4 )
{
*_errno_location() = 22;
puts("table invalid");
v4 = 1;
}
else
{
func2((__int64)v8, a2);
}
}
else
{
v5 = _errno_location();
v6 = *v5;
if ( !*v5 )
v6 = 5;
*v5 = v6;
v4 = 1;
}
}
close(v3);
return v4;
}

original_code:
static int dump_signature(const char *image, int part)
{
    int fd, ret;
    struct partition_table pt;
    ret = 1;
    fd = -1;
    fd = open(image, 00);
    if (fd == -1)
        goto out;
    if (read(fd, &pt, sizeof(pt)) != sizeof(pt)) {
        (*__errno_location ()) = (*__errno_location ()) ? : 5;
        goto out;
    }
    partition_table_in(&pt);
    if (partition_table_validate(&pt)) {
        (*__errno_location ()) = 22;
        printf("table invalid\n");
        goto out;
    }
    __dump_signature(&pt, part);
    ret = 0;
  out:
    close(fd);
    return ret;
}

canonical_original_code:
static int dump_signature(char *image, int part)
{
    int fd; int ret;
    struct partition_table pt;
    ret = 1;
    fd = -1;
    fd = open(image, 00);
    if (fd == -1)
        goto out;
    if (read(fd, &pt, sizeof(pt)) != sizeof(pt)) {
        (*__errno_location ()) = (*__errno_location ()) ? : 5;
        goto out;
    }
    partition_table_in(&pt);
    if (partition_table_validate(&pt)) {
        (*__errno_location ()) = 22;
        printf("table invalid\n");
        goto out;
    }
    __dump_signature(&pt, part);
    ret = 0;
  out:
    close(fd);
    return ret;
}

variable_types: part int

user_defined_types: struct partition_chs { unsigned char chs[3]; }


decompiled_code:
int __fastcall partition_table_dump(__int64 a1)
{
__int64 v1; // rbp
unsigned int i; // ebx
int result; // eax
unsigned __int8 v4; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned __int8 v5; // [rsp+Dh] [rbp-1Bh] BYREF
__int16 v6[13]; // [rsp+Eh] [rbp-1Ah] BYREF

printf("disk signature 0x%08x\n", *(unsigned int *)(a1 + 440));
printf("mbr signature 0x%04x\n", *(unsigned __int16 *)(a1 + 510));
putchar(10);
v1 = a1 + 447;
for ( i = 0; i != 4; ++i )
{
printf(" %d status 0x%02x\n", i, *(unsigned __int8 *)(v1 - 1));
chs_unpack((_BYTE *)v1, &v4, &v5, v6);
printf(" %d s cylinder 0x%04x\n", i, (unsigned __int16)v6[0]);
printf(" %d s sector 0x%01x\n", i, v5);
printf(" %d s head 0x%01x\n", i, v4);
printf(" %d type 0x%01x\n", i, *(unsigned __int8 *)(v1 + 3));
chs_unpack((_BYTE *)(v1 + 4), &v4, &v5, v6);
printf(" %d e cylinder 0x%04x\n", i, (unsigned __int16)v6[0]);
printf(" %d e sector 0x%01x\n", i, v5);
printf(" %d e head 0x%01x\n", i, v4);
printf(" %d lba 0x%08x\n", i, *(unsigned int *)(v1 + 7));
printf(" %d blocks 0x%08x\n", i, *(unsigned int *)(v1 + 11));
result = putchar(10);
v1 += 16LL;
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
__int64 v1; // rbp
unsigned int i; // ebx
int result; // eax
unsigned __int8 v4; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned __int8 v5; // [rsp+Dh] [rbp-1Bh] BYREF
__int16 v6[13]; // [rsp+Eh] [rbp-1Ah] BYREF

printf("disk signature 0x%08x\n", *(unsigned int *)(a1 + 440));
printf("mbr signature 0x%04x\n", *(unsigned __int16 *)(a1 + 510));
putchar(10);
v1 = a1 + 447;
for ( i = 0; i != 4; ++i )
{
printf(" %d status 0x%02x\n", i, *(unsigned __int8 *)(v1 - 1));
func0((_BYTE *)v1, &v4, &v5, v6);
printf(" %d s cylinder 0x%04x\n", i, (unsigned __int16)v6[0]);
printf(" %d s sector 0x%01x\n", i, v5);
printf(" %d s head 0x%01x\n", i, v4);
printf(" %d type 0x%01x\n", i, *(unsigned __int8 *)(v1 + 3));
func0((_BYTE *)(v1 + 4), &v4, &v5, v6);
printf(" %d e cylinder 0x%04x\n", i, (unsigned __int16)v6[0]);
printf(" %d e sector 0x%01x\n", i, v5);
printf(" %d e head 0x%01x\n", i, v4);
printf(" %d lba 0x%08x\n", i, *(unsigned int *)(v1 + 7));
printf(" %d blocks 0x%08x\n", i, *(unsigned int *)(v1 + 11));
result = putchar(10);
v1 += 16LL;
}
return result;
}

original_code:
void partition_table_dump(struct partition_table *pt)
{
    int i;
    printf("disk signature   0x%08x\n", pt->disk_signature);
    printf("mbr signature    0x%04x\n", pt->mbr_signature);
    printf("\n");
    for (i = 0; i < 4; i++) {
        struct primary_partition *p = pt->partitions + i;
        uint8_t head, sector;
        uint16_t cylinder;
        printf("  %d status       0x%02x\n", i, p->status);
        chs_unpack(&p->chs_first, &head, &sector, &cylinder);
        printf("  %d s cylinder   0x%04x\n", i, cylinder);
        printf("  %d s sector     0x%01x\n", i, sector);
        printf("  %d s head       0x%01x\n", i, head);
        printf("  %d type         0x%01x\n", i, p->type);
        chs_unpack(&p->chs_last, &head, &sector, &cylinder);
        printf("  %d e cylinder   0x%04x\n", i, cylinder);
        printf("  %d e sector     0x%01x\n", i, sector);
        printf("  %d e head       0x%01x\n", i, head);
        printf("  %d lba          0x%08x\n", i, p->lba);
        printf("  %d blocks       0x%08x\n", i, p->blocks);
        printf("\n");
    }
}

canonical_original_code:
void partition_table_dump(struct partition_table *pt)
{
    int i;
    printf("disk signature   0x%08x\n", pt->disk_signature);
    printf("mbr signature    0x%04x\n", pt->mbr_signature);
    printf("\n");
    for (i = 0; i < 4; i++) {
        struct primary_partition *p = pt->partitions + i;
        unsigned char head; unsigned char sector;
        unsigned short cylinder;
        printf("  %d status       0x%02x\n", i, p->status);
        chs_unpack(&p->chs_first, &head, &sector, &cylinder);
        printf("  %d s cylinder   0x%04x\n", i, cylinder);
        printf("  %d s sector     0x%01x\n", i, sector);
        printf("  %d s head       0x%01x\n", i, head);
        printf("  %d type         0x%01x\n", i, p->type);
        chs_unpack(&p->chs_last, &head, &sector, &cylinder);
        printf("  %d e cylinder   0x%04x\n", i, cylinder);
        printf("  %d e sector     0x%01x\n", i, sector);
        printf("  %d e head       0x%01x\n", i, head);
        printf("  %d lba          0x%08x\n", i, p->lba);
        printf("  %d blocks       0x%08x\n", i, p->blocks);
        printf("\n");
    }
}

variable_types: i int

variable_types:
head
unsigned char

variable_types:
sector
unsigned char

variable_types:
cylinder
unsigned short


decompiled_code:
__int64 __fastcall dump_partitions(const char *a1)
{
int v1; // eax
int v2; // ebx
unsigned int v3; // ebp
int *v4; // rax
int v5; // edx
_BYTE v7[536]; // [rsp+0h] [rbp-218h] BYREF

v1 = open(a1, 0);
v2 = v1;
v3 = 1;
if ( v1 != -1 )
{
if ( read(v1, v7, 0x200uLL) == 512 )
{
partition_table_in(v7);
v3 = partition_table_validate(v7);
if ( v3 )
{
*_errno_location() = 22;
puts("table invalid");
v3 = 1;
}
else
{
partition_table_dump((__int64)v7);
}
}
else
{
v4 = _errno_location();
v5 = *v4;
if ( !*v4 )
v5 = 5;
*v4 = v5;
v3 = 1;
}
}
close(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1)
{
int v1; // eax
int v2; // ebx
unsigned int v3; // ebp
int *v4; // rax
int v5; // edx
_BYTE v7[536]; // [rsp+0h] [rbp-218h] BYREF

v1 = open(a1, 0);
v2 = v1;
v3 = 1;
if ( v1 != -1 )
{
if ( read(v1, v7, 0x200uLL) == 512 )
{
partition_table_in(v7);
v3 = partition_table_validate(v7);
if ( v3 )
{
*_errno_location() = 22;
puts("table invalid");
v3 = 1;
}
else
{
func6((__int64)v7);
}
}
else
{
v4 = _errno_location();
v5 = *v4;
if ( !*v4 )
v5 = 5;
*v4 = v5;
v3 = 1;
}
}
close(v2);
return v3;
}

original_code:
static int dump_partitions(const char *image)
{
    int fd, ret;
    struct partition_table pt;
    ret = 1;
    fd = -1;
    fd = open(image, 00);
    if (fd == -1)
        goto out;
    if (read(fd, &pt, sizeof(pt)) != sizeof(pt)) {
        (*__errno_location ()) = (*__errno_location ()) ? : 5;
        goto out;
    }
    partition_table_in(&pt);
    if (partition_table_validate(&pt)) {
        (*__errno_location ()) = 22;
        printf("table invalid\n");
        goto out;
    }
    partition_table_dump(&pt);
    ret = 0;
  out:
    close(fd);
    return ret;
}

canonical_original_code:
static int dump_partitions(char *image)
{
    int fd; int ret;
    struct partition_table pt;
    ret = 1;
    fd = -1;
    fd = open(image, 00);
    if (fd == -1)
        goto out;
    if (read(fd, &pt, sizeof(pt)) != sizeof(pt)) {
        (*__errno_location ()) = (*__errno_location ()) ? : 5;
        goto out;
    }
    partition_table_in(&pt);
    if (partition_table_validate(&pt)) {
        (*__errno_location ()) = 22;
        printf("table invalid\n");
        goto out;
    }
    partition_table_dump(&pt);
    ret = 0;
  out:
    close(fd);
    return ret;
}

variable_types:
image
char *


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
return printf("usage: %s <-i image> [-d dump] [-c count] [-f format] [-t type] [-s sig <part>]\n", a1);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
return printf("usage: %s <-i image> [-d dump] [-c count] [-f format] [-t type] [-s sig <part>]\n", a1);
}

original_code:
static void usage(const char *app)
{
    printf("usage: %s <-i image> "
           "[-d dump] [-c count] [-f format] "
           "[-t type] [-s sig <part>]\n", app);
}

canonical_original_code:
static void usage(char *app)
{
    printf("usage: %s <-i image> "
           "[-d dump] [-c count] [-f format] "
           "[-t type] [-s sig <part>]\n", app);
}

variable_types:
app
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // er15
char *v5; // r14
int v6; // eax
char *v7; // r15
int v8; // eax
int v10; // [rsp+8h] [rbp-250h]
int v11; // [rsp+Ch] [rbp-24Ch]
unsigned int v12; // [rsp+1Ch] [rbp-23Ch] BYREF
char v13[568]; // [rsp+20h] [rbp-238h] BYREF

v12 = 0;
v10 = -1;
v11 = 0;
v3 = 0;
v4 = 0;
v5 = 0LL;
while ( 2 )
{
v6 = getopt(argc, (char *const *)argv, "i:fdt:cs:h");
if ( v6 == -1 )
{
if ( v5 && v10 | v12 | v3 | v11 )
{
if ( v11 )
{
if ( !v4 )
{
*_errno_location() = 22;
perror("type required");
return v11;
}
if ( (unsigned int)format_partition(v5, v4, v13) )
{
perror("formatting partition");
return v11;
}
_dump_signature((__int64)v13, 1);
}
if ( v12 )
{
if ( (unsigned int)count_partitions(v5, &v12) )
{
perror("counting partitions");
return 1;
}
printf("%d\n", v12);
}
if ( v10 == -1 || !(unsigned int)dump_signature(v5, v10) )
{
if ( v3 )
{
if ( (unsigned int)dump_partitions(v5) )
perror("dumping partitions");
else
v3 = 0;
}
}
else
{
perror("dumping signature");
v3 = 1;
}
}
else
{
*_errno_location() = 22;
usage(*argv);
v3 = 1;
}
}
else
{
switch ( v6 )
{
case 'c':
v12 = 1;
continue;
case 'd':
v3 = 1;
continue;
case 'f':
v11 = 1;
continue;
case 'h':
usage(*argv);
v3 = 0;
break;
case 'i':
v5 = optarg;
continue;
case 's':
v10 = atoi(optarg);
continue;
case 't':
v7 = optarg;
v8 = strncasecmp(optarg, "0x", 2uLL);
v4 = strtol(v7, 0LL, v8 == 0 ? 16 : 10);
continue;
default:
continue;
}
}
break;
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // er15
char *v5; // r14
int v6; // eax
char *v7; // r15
int v8; // eax
int v10; // [rsp+8h] [rbp-250h]
int v11; // [rsp+Ch] [rbp-24Ch]
unsigned int v12; // [rsp+1Ch] [rbp-23Ch] BYREF
char v13[568]; // [rsp+20h] [rbp-238h] BYREF

v12 = 0;
v10 = -1;
v11 = 0;
v3 = 0;
v4 = 0;
v5 = 0LL;
while ( 2 )
{
v6 = getopt(argc, (char *const *)argv, "i:fdt:cs:h");
if ( v6 == -1 )
{
if ( v5 && v10 | v12 | v3 | v11 )
{
if ( v11 )
{
if ( !v4 )
{
*_errno_location() = 22;
perror("type required");
return v11;
}
if ( (unsigned int)func3(v5, v4, v13) )
{
perror("formatting partition");
return v11;
}
func2((__int64)v13, 1);
}
if ( v12 )
{
if ( (unsigned int)func4(v5, &v12) )
{
perror("counting partitions");
return 1;
}
printf("%d\n", v12);
}
if ( v10 == -1 || !(unsigned int)func5(v5, v10) )
{
if ( v3 )
{
if ( (unsigned int)func7(v5) )
perror("dumping partitions");
else
v3 = 0;
}
}
else
{
perror("dumping signature");
v3 = 1;
}
}
else
{
*_errno_location() = 22;
func1(*argv);
v3 = 1;
}
}
else
{
switch ( v6 )
{
case 'c':
v12 = 1;
continue;
case 'd':
v3 = 1;
continue;
case 'f':
v11 = 1;
continue;
case 'h':
func1(*argv);
v3 = 0;
break;
case 'i':
v5 = optarg;
continue;
case 's':
v10 = atoi(optarg);
continue;
case 't':
v7 = optarg;
v8 = strncasecmp(optarg, "0x", 2uLL);
v4 = strtol(v7, 0LL, v8 == 0 ? 16 : 10);
continue;
default:
continue;
}
}
break;
}
return v3;
}

original_code:
int main(int argc, char *argv[])
{
    char *image;
    struct partition_table pt;
    int ret, c, type, count, dump, format, signature;
    ret = 1;
    format = 0;
    count = 0;
    dump = 0;
    type = 0;
    signature = -1;
    image = ((void *)0);
    while ((c = getopt(argc, argv, "i:fdt:cs:h")) != -1) {
        switch (c) {
        case 'i':
            image = optarg;
            break;
        case 'c':
            count = 1;
            break;
        case 's':
            signature = atoi(optarg);
            break;
        case 'f':
            format = 1;
            break;
        case 't':{
                int base = (!strncasecmp(optarg, "0x", 2) ? 16 : 10);
                type = strtol(optarg, ((void *)0), base);
                break;
            }
        case 'd':
            dump = 1;
            break;
        case 'h':
            usage(argv[0]);
            ret = 0;
            goto out;
        }
    }
    if (!image || (!format && !count && !signature && !dump)) {
        (*__errno_location ()) = 22;
        usage(argv[0]);
        goto out;
    }
    if (format) {
        if (!type) {
            (*__errno_location ()) = 22;
            perror("type required");
            goto out;
        }
        if (format_partition(image, type, &pt)) {
            perror("formatting partition");
            goto out;
        }
        __dump_signature(&pt, 1);
    }
    if (count) {
        if (count_partitions(image, &count)) {
            perror("counting partitions");
            goto out;
        }
        printf("%d\n", count);
    }
    if (signature != -1) {
        if (dump_signature(image, signature)) {
            perror("dumping signature");
            goto out;
        }
    }
    if (dump) {
        if (dump_partitions(image)) {
            perror("dumping partitions");
            goto out;
        }
    }
    ret = 0;
  out:
    return ret;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    char *image;
    struct partition_table pt;
    int ret; int c; int type; int count; int dump; int format; int signature;
    ret = 1;
    format = 0;
    count = 0;
    dump = 0;
    type = 0;
    signature = -1;
    image = ((void *)0);
    while ((c = getopt(argc, argv, "i:fdt:cs:h")) != -1) {
        switch (c) {
        case 'i':
            image = optarg;
            break;
        case 'c':
            count = 1;
            break;
        case 's':
            signature = atoi(optarg);
            break;
        case 'f':
            format = 1;
            break;
        case 't':{
                int base = (!strncasecmp(optarg, "0x", 2) ? 16 : 10);
                type = strtol(optarg, ((void *)0), base);
                break;
            }
        case 'd':
            dump = 1;
            break;
        case 'h':
            usage(argv[0]);
            ret = 0;
            goto out;
        }
    }
    if (!image || (!format && !count && !signature && !dump)) {
        (*__errno_location ()) = 22;
        usage(argv[0]);
        goto out;
    }
    if (format) {
        if (!type) {
            (*__errno_location ()) = 22;
            perror("type required");
            goto out;
        }
        if (format_partition(image, type, &pt)) {
            perror("formatting partition");
            goto out;
        }
        __dump_signature(&pt, 1);
    }
    if (count) {
        if (count_partitions(image, &count)) {
            perror("counting partitions");
            goto out;
        }
        printf("%d\n", count);
    }
    if (signature != -1) {
        if (dump_signature(image, signature)) {
            perror("dumping signature");
            goto out;
        }
    }
    if (dump) {
        if (dump_partitions(image)) {
            perror("dumping partitions");
            goto out;
        }
    }
    ret = 0;
  out:
    return ret;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int

variable_types: c int

variable_types: type int

variable_types: count int

variable_types: dump int

variable_types: format int

variable_types: signature int

variable_types: base int


decompiled_code:
__int64 __fastcall tapdisk_storage_type(const char *a1)
{
int v1; // eax
struct stat v3; // [rsp+0h] [rbp-1098h] BYREF
char resolved[4104]; // [rsp+90h] [rbp-1008h] BYREF

if ( !realpath(a1, resolved) )
return (unsigned int)-*_errno_location();
if ( stat(resolved, &v3) )
return (unsigned int)-*_errno_location();
v1 = v3.st_mode & 0xF000;
if ( v1 == 24576 )
return _tapdisk_blk_storage_type(resolved);
if ( v1 == 0x8000 )
return _tapdisk_fs_storage_type(resolved);
return 4294967274LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
int v1; // eax
struct stat v3; // [rsp+0h] [rbp-1098h] BYREF
char resolved[4104]; // [rsp+90h] [rbp-1008h] BYREF

if ( !realpath(a1, resolved) )
return (unsigned int)-*_errno_location();
if ( stat(resolved, &v3) )
return (unsigned int)-*_errno_location();
v1 = v3.st_mode & 0xF000;
if ( v1 == 24576 )
return func0(resolved);
if ( v1 == 0x8000 )
return func1(resolved);
return 4294967274LL;
}

original_code:
int tapdisk_storage_type(const char *path)
{
    char rpath[4096], *p;
    struct stat st;
    int err, rv;
    p = realpath(path, rpath);
    if (!p)
        return -(*__errno_location ());
    err = stat(rpath, &st);
    if (err)
        return -(*__errno_location ());
    switch (st.st_mode & 0170000) {
    case 0060000:
        rv = __tapdisk_blk_storage_type(rpath);
        break;
    case 0100000:
        rv = __tapdisk_fs_storage_type(rpath);
        break;
    default:
        rv = -22;
        break;
    }
    return rv;
}

canonical_original_code:
int tapdisk_storage_type(char *path)
{
    char rpath[4096]; char *p;
    struct stat st;
    int err; int rv;
    p = realpath(path, rpath);
    if (!p)
        return -(*__errno_location ());
    err = stat(rpath, &st);
    if (err)
        return -(*__errno_location ());
    switch (st.st_mode & 0170000) {
    case 0060000:
        rv = __tapdisk_blk_storage_type(rpath);
        break;
    case 0100000:
        rv = __tapdisk_fs_storage_type(rpath);
        break;
    default:
        rv = -22;
        break;
    }
    return rv;
}

variable_types:
path
char *

variable_types:
rpath
char [4096]

variable_types:
p
char *

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

variable_types: rv int

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


decompiled_code:
const char *__fastcall tapdisk_storage_name(int a1)
{
const char *result; // rax

if ( a1 == 2 )
return "ext";
if ( a1 > 2 )
{
result = "<unknown-type>";
if ( a1 == 3 )
result = "lvm";
}
else
{
result = "n/a";
if ( a1 != -1 )
{
result = "<unknown-type>";
if ( a1 == 1 )
result = "nfs";
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func3(int a1)
{
const char *result; // rax

if ( a1 == 2 )
return "ext";
if ( a1 > 2 )
{
result = "<unknown-type>";
if ( a1 == 3 )
result = "lvm";
}
else
{
result = "n/a";
if ( a1 != -1 )
{
result = "<unknown-type>";
if ( a1 == 1 )
result = "nfs";
}
}
return result;
}

original_code:
const char *tapdisk_storage_name(int type)
{
    switch (type) {
    case 1:
        return "nfs";
    case 2:
        return "ext";
    case 3:
        return "lvm";
    case -1:
        return "n/a";
    default:
        return "<unknown-type>";
    }
}

return_type: char *


decompiled_code:
__int64 __fastcall count_nodes(char *a1)
{
char v1; // al
unsigned int v2; // edx

if ( !a1 )
return 0;
v1 = *a1;
if ( !*a1 )
return 0;
v2 = 0;
do
{
v2 += v1 == 47;
v1 = *++a1;
}
while ( *a1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char v1; // al
unsigned int v2; // edx

if ( !a1 )
return 0;
v1 = *a1;
if ( !*a1 )
return 0;
v2 = 0;
do
{
v2 += v1 == 47;
v1 = *++a1;
}
while ( *a1 );
return v2;
}

original_code:
static int count_nodes(char *path)
{
    int i;
    char *tmp;
    if (!path)
        return 0;
    for (i = 0, tmp = path; *tmp != '\0'; tmp++)
        if (*tmp == '/')
            i++;
    return i;
}

variable_types: i int

variable_types:
tmp
char *


original_code:
static char *next_node(char **path, int *err)
{
    int ret;
    char *tmp, *start;
    if (!path || !*path) {
        *err = -22;
        return ((void *)0);
    }
    *err = 0;
    start = *path;
    for (tmp = *path; *tmp != '\0'; tmp++)
        if (*tmp == '/') {
            int size;
            char *node;
            size = tmp - start + 1;
            node = malloc(size);
            if (!node) {
                *err = -12;
                return ((void *)0);
            }
            ret = snprintf(node, size, "%s", start);
            if (ret < 0) {
                free(node);
                *err = -22;
                return ((void *)0);
            }
            *path = tmp;
            return node;
        }
    return ((void *)0);
}

canonical_original_code:
static char *next_node(char **path, int *err)
{
    int ret;
    char *tmp; char *start;
    if (!path || !*path) {
        *err = -22;
        return ((void *)0);
    }
    *err = 0;
    start = *path;
    for (tmp = *path; *tmp != '\0'; tmp++)
        if (*tmp == '/') {
            int size;
            char *node;
            size = tmp - start + 1;
            node = malloc(size);
            if (!node) {
                *err = -12;
                return ((void *)0);
            }
            ret = snprintf(node, size, "%s", start);
            if (ret < 0) {
                free(node);
                *err = -22;
                return ((void *)0);
            }
            *path = tmp;
            return node;
        }
    return ((void *)0);
}

variable_types:
path
char **

variable_types:
err
int *

variable_types:
start
char *

variable_types: size int

variable_types:
node
char *


decompiled_code:
__int64 __fastcall count_common_nodes(const char *a1, const char *a2)
{
unsigned int i; // er12
char *v3; // rax
char *v4; // rbx
char *v5; // rax
char *v6; // rbp
const char *v8; // [rsp+0h] [rbp-48h] BYREF
const char *v9; // [rsp+8h] [rbp-40h] BYREF
int v10[11]; // [rsp+1Ch] [rbp-2Ch] BYREF

v9 = a1;
v8 = a2;
if ( !a1 || !a2 )
return (unsigned int)-22;
v10[0] = 0;
for ( i = 0; ; ++i )
{
v3 = next_node(&v9, v10);
v4 = v3;
if ( v10[0] || !v3 )
break;
v5 = next_node(&v8, v10);
v6 = v5;
if ( v10[0] || !v5 || strncmp(v4, v5, 0x3E8uLL) )
goto LABEL_12;
++v9;
++v8;
free(v4);
free(v6);
}
v6 = 0LL;
LABEL_12:
free(v4);
free(v6);
if ( v10[0] )
i = v10[0];
return i;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, const char *a2)
{
unsigned int i; // er12
char *v3; // rax
char *v4; // rbx
char *v5; // rax
char *v6; // rbp
const char *v8; // [rsp+0h] [rbp-48h] BYREF
const char *v9; // [rsp+8h] [rbp-40h] BYREF
int v10[11]; // [rsp+1Ch] [rbp-2Ch] BYREF

v9 = a1;
v8 = a2;
if ( !a1 || !a2 )
return (unsigned int)-22;
v10[0] = 0;
for ( i = 0; ; ++i )
{
v3 = func2(&v9, v10);
v4 = v3;
if ( v10[0] || !v3 )
break;
v5 = func2(&v8, v10);
v6 = v5;
if ( v10[0] || !v5 || strncmp(v4, v5, 0x3E8uLL) )
goto LABEL_12;
++v9;
++v8;
free(v4);
free(v6);
}
v6 = 0LL;
LABEL_12:
free(v4);
free(v6);
if ( v10[0] )
i = v10[0];
return i;
}

original_code:
static int count_common_nodes(char *to, char *from)
{
    int err, common;
    char *to_node, *from_node;
    if (!to || !from)
        return -22;
    err = 0;
    common = 0;
    to_node = ((void *)0);
    from_node = ((void *)0);
    do {
        to_node = next_node(&to, &err);
        if (err || !to_node)
            break;
        from_node = next_node(&from, &err);
        if (err || !from_node)
            break;
        if (strncmp(to_node, from_node, 1000))
            break;
        ++to;
        ++from;
        ++common;
        do { free(to_node); to_node = ((void *)0); } while (0);
        do { free(from_node); from_node = ((void *)0); } while (0);
    } while (1);
    do { free(to_node); to_node = ((void *)0); } while (0);
    do { free(from_node); from_node = ((void *)0); } while (0);
    if (err)
        return err;
    return common;
}

canonical_original_code:
static int count_common_nodes(char *to, char *from)
{
    int err; int common;
    char *to_node; char *from_node;
    if (!to || !from)
        return -22;
    err = 0;
    common = 0;
    to_node = ((void *)0);
    from_node = ((void *)0);
    do {
        to_node = next_node(&to, &err);
        if (err || !to_node)
            break;
        from_node = next_node(&from, &err);
        if (err || !from_node)
            break;
        if (strncmp(to_node, from_node, 1000))
            break;
        ++to;
        ++from;
        ++common;
        do { free(to_node); to_node = ((void *)0); } while (0);
        do { free(from_node); from_node = ((void *)0); } while (0);
    } while (1);
    do { free(to_node); to_node = ((void *)0); } while (0);
    do { free(from_node); from_node = ((void *)0); } while (0);
    if (err)
        return err;
    return common;
}

variable_types:
to
char *

variable_types:
from
char *

variable_types: common int


decompiled_code:
char *__fastcall up_nodes(int a1)
{
int v2; // edi
_DWORD *v3; // rcx
_DWORD *v4; // rax
_DWORD *v5; // rdx

if ( !a1 )
return strdup("./");
v2 = 3 * a1;
v3 = 0LL;
if ( v2 <= 999 )
{
v4 = malloc(v2 + 1);
v3 = v4;
if ( v4 )
{
if ( a1 > 0 )
{
v5 = (_DWORD *)((char *)v4 + 3 * (unsigned int)a1);
do
{
*v4 = 3092014;
v4 = (_DWORD *)((char *)v4 + 3);
}
while ( v5 != v4 );
}
}
}
return (char *)v3;
}

canonical_decompiled_code:
char *__fastcall func4(int a1)
{
int v2; // edi
_DWORD *v3; // rcx
_DWORD *v4; // rax
_DWORD *v5; // rdx

if ( !a1 )
return strdup("./");
v2 = 3 * a1;
v3 = 0LL;
if ( v2 <= 999 )
{
v4 = malloc(v2 + 1);
v3 = v4;
if ( v4 )
{
if ( a1 > 0 )
{
v5 = (_DWORD *)((char *)v4 + 3 * (unsigned int)a1);
do
{
*v4 = 3092014;
v4 = (_DWORD *)((char *)v4 + 3);
}
while ( v5 != v4 );
}
}
}
return (char *)v3;
}

original_code:
static char *up_nodes(int count)
{
    char *path, *tmp;
    int i, ret, len, size;
    if (!count)
        return strdup("./");
    len = strlen("../");
    size = len * count;
    if (size >= 1000)
        return ((void *)0);
    path = malloc(size + 1);
    if (!path)
        return ((void *)0);
    tmp = path;
    for (i = 0; i < count; i++) {
        ret = sprintf(tmp, "../");
        if (ret < 0 || ret != len) {
            free(path);
            return ((void *)0);
        }
        tmp += ret;
    }
    return path;
}

canonical_original_code:
static char *up_nodes(int count)
{
    char *path; char *tmp;
    int i; int ret; int len; int size;
    if (!count)
        return strdup("./");
    len = strlen("../");
    size = len * count;
    if (size >= 1000)
        return ((void *)0);
    path = malloc(size + 1);
    if (!path)
        return ((void *)0);
    tmp = path;
    for (i = 0; i < count; i++) {
        ret = sprintf(tmp, "../");
        if (ret < 0 || ret != len) {
            free(path);
            return ((void *)0);
        }
        tmp += ret;
    }
    return path;
}

variable_types: count int

variable_types: ret int

variable_types: len int


decompiled_code:
char *__fastcall relative_path_to(char *name, char *a2, int *a3)
{
char *v4; // r13
char *v5; // rbx
int v6; // er12
int v7; // eax
int v8; // er14
char *v9; // r12
char *v10; // rax
const char *v11; // rbx
char *result; // rax
char *ptr; // [rsp+8h] [rbp-2030h] BYREF
char v14[4096]; // [rsp+10h] [rbp-2028h] BYREF
char resolved[4136]; // [rsp+1010h] [rbp-1028h] BYREF

*a3 = 0;
ptr = 0LL;
if ( strnlen(a2, 0x3E8uLL) == 1000 || strnlen(name, 0x3E8uLL) == 1000 )
{
syslog(3, "tap-err:%s: invalid input; max path length is %d\n", "relative_path_to", 1000LL);
*a3 = -36;
result = 0LL;
}
else
{
v4 = realpath(a2, resolved);
if ( v4 )
{
v5 = realpath(name, v14);
if ( v5 )
{
if ( strnlen(v4, 0x3E8uLL) == 1000 || strnlen(v5, 0x3E8uLL) == 1000 )
{
syslog(3, "tap-err:%s: invalid input; max path length is %d\n", "relative_path_to", 1000LL);
*a3 = -36;
v9 = 0LL;
}
else
{
v6 = count_nodes(v5);
v7 = count_common_nodes(v4 + 1, v5 + 1);
v8 = v7;
if ( v7 < 0 )
{
syslog(
3,
"tap-err:%s: failed to count common nodes of %s and %s: %d\n",
"relative_path_to",
v4,
v5,
(unsigned int)v7);
*a3 = v8;
v9 = 0LL;
}
else
{
v9 = up_nodes(v6 - v7 - 1);
if ( v9 )
{
v10 = node_offset(v4, v8 + 1);
v11 = v10;
if ( v10 )
{
if ( asprintf(&ptr, "%s%s", v9, v10) == -1 )
{
syslog(
3,
"tap-err:%s: failed to construct final path %s%s: %d\n",
"relative_path_to",
v9,
v11,
4294967284LL);
ptr = 0LL;
*a3 = -12;
}
}
else
{
syslog(
3,
"tap-err:%s: failed to find common target path to %s: %d\n",
"relative_path_to",
v4,
4294967274LL);
*a3 = -22;
}
}
else
{
syslog(
3,
"tap-err:%s: failed to allocate relative path for %s: %d\n",
"relative_path_to",
v5,
4294967284LL);
*a3 = -12;
}
}
}
}
else
{
syslog(3, "tap-err:%s: failed to get absolute path of %s\n", "relative_path_to", name);
*a3 = -*_errno_location();
v9 = 0LL;
}
}
else
{
syslog(3, "tap-err:%s: failed to get absolute path of %s\n", "relative_path_to", a2);
*a3 = -*_errno_location();
v9 = 0LL;
}
free(v9);
result = ptr;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func5(char *name, char *a2, int *a3)
{
char *v4; // r13
char *v5; // rbx
int v6; // er12
int v7; // eax
int v8; // er14
char *v9; // r12
char *v10; // rax
const char *v11; // rbx
char *result; // rax
char *ptr; // [rsp+8h] [rbp-2030h] BYREF
char v14[4096]; // [rsp+10h] [rbp-2028h] BYREF
char resolved[4136]; // [rsp+1010h] [rbp-1028h] BYREF

*a3 = 0;
ptr = 0LL;
if ( strnlen(a2, 0x3E8uLL) == 1000 || strnlen(name, 0x3E8uLL) == 1000 )
{
syslog(3, "tap-err:%s: invalid input; max path length is %d\n", "relative_path_to", 1000LL);
*a3 = -36;
result = 0LL;
}
else
{
v4 = realpath(a2, resolved);
if ( v4 )
{
v5 = realpath(name, v14);
if ( v5 )
{
if ( strnlen(v4, 0x3E8uLL) == 1000 || strnlen(v5, 0x3E8uLL) == 1000 )
{
syslog(3, "tap-err:%s: invalid input; max path length is %d\n", "relative_path_to", 1000LL);
*a3 = -36;
v9 = 0LL;
}
else
{
v6 = func0(v5);
v7 = func3(v4 + 1, v5 + 1);
v8 = v7;
if ( v7 < 0 )
{
syslog(
3,
"tap-err:%s: failed to count common nodes of %s and %s: %d\n",
"relative_path_to",
v4,
v5,
(unsigned int)v7);
*a3 = v8;
v9 = 0LL;
}
else
{
v9 = func4(v6 - v7 - 1);
if ( v9 )
{
v10 = func1(v4, v8 + 1);
v11 = v10;
if ( v10 )
{
if ( asprintf(&ptr, "%s%s", v9, v10) == -1 )
{
syslog(
3,
"tap-err:%s: failed to construct final path %s%s: %d\n",
"relative_path_to",
v9,
v11,
4294967284LL);
ptr = 0LL;
*a3 = -12;
}
}
else
{
syslog(
3,
"tap-err:%s: failed to find common target path to %s: %d\n",
"relative_path_to",
v4,
4294967274LL);
*a3 = -22;
}
}
else
{
syslog(
3,
"tap-err:%s: failed to allocate relative path for %s: %d\n",
"relative_path_to",
v5,
4294967284LL);
*a3 = -12;
}
}
}
}
else
{
syslog(3, "tap-err:%s: failed to get absolute path of %s\n", "relative_path_to", name);
*a3 = -*_errno_location();
v9 = 0LL;
}
}
else
{
syslog(3, "tap-err:%s: failed to get absolute path of %s\n", "relative_path_to", a2);
*a3 = -*_errno_location();
v9 = 0LL;
}
free(v9);
result = ptr;
}
return result;
}

original_code:
char *relative_path_to(char *from, char *to, int *err)
{
    int from_nodes, common;
    char *to_absolute, __to_absolute[4096];
    char *from_absolute, __from_absolute[4096];
    char *up, *common_target_path, *relative_path;
    *err = 0;
    up = ((void *)0);
    to_absolute = ((void *)0);
    from_absolute = ((void *)0);
    relative_path = ((void *)0);
    if (strnlen(to, 1000) == 1000 ||
        strnlen(from, 1000) == 1000) {
        syslog(3, "tap-err:%s: " "invalid input; max path length is %d\n", __func__, 1000);
        *err = -36;
        return ((void *)0);
    }
    to_absolute = realpath(to, __to_absolute);
    if (!to_absolute) {
        syslog(3, "tap-err:%s: " "failed to get absolute path of %s\n", __func__, to);
        *err = -(*__errno_location ());
        goto out;
    }
    from_absolute = realpath(from, __from_absolute);
    if (!from_absolute) {
        syslog(3, "tap-err:%s: " "failed to get absolute path of %s\n", __func__, from);
        *err = -(*__errno_location ());
        goto out;
    }
    if (strnlen(to_absolute, 1000) == 1000 ||
        strnlen(from_absolute, 1000) == 1000) {
        syslog(3, "tap-err:%s: " "invalid input; max path length is %d\n", __func__, 1000);
        *err = -36;
        goto out;
    }
    from_nodes = count_nodes(from_absolute);
    common = count_common_nodes(to_absolute + 1, from_absolute + 1);
    if (common < 0) {
        syslog(3, "tap-err:%s: " "failed to count common nodes of %s and %s: %d\n", __func__, to_absolute, from_absolute, common);
        *err = common;
        goto out;
    }
    up = up_nodes(from_nodes - common - 1);
    if (!up) {
        syslog(3, "tap-err:%s: " "failed to allocate relative path for %s: %d\n", __func__, from_absolute, -12);
        *err = -12;
        goto out;
    }
    common_target_path = node_offset(to_absolute, common + 1);
    if (!common_target_path) {
        syslog(3, "tap-err:%s: " "failed to find common target path to %s: %d\n", __func__, to_absolute, -22);
        *err = -22;
        goto out;
    }
    if (asprintf(&relative_path, "%s%s", up, common_target_path) == -1) {
        syslog(3, "tap-err:%s: " "failed to construct final path %s%s: %d\n", __func__, up, common_target_path, -12);
        relative_path = ((void *)0);
        *err = -12;
        goto out;
    }
  out:
    do { free(up); up = ((void *)0); } while (0);
    return relative_path;
}

canonical_original_code:
char *relative_path_to(char *from, char *to, int *err)
{
    int from_nodes; int common;
    char *to_absolute; char __to_absolute[4096];
    char *from_absolute; char __from_absolute[4096];
    char *up; char *common_target_path; char *relative_path;
    *err = 0;
    up = ((void *)0);
    to_absolute = ((void *)0);
    from_absolute = ((void *)0);
    relative_path = ((void *)0);
    if (strnlen(to, 1000) == 1000 ||
        strnlen(from, 1000) == 1000) {
        syslog(3, "tap-err:%s: " "invalid input; max path length is %d\n", __func__, 1000);
        *err = -36;
        return ((void *)0);
    }
    to_absolute = realpath(to, __to_absolute);
    if (!to_absolute) {
        syslog(3, "tap-err:%s: " "failed to get absolute path of %s\n", __func__, to);
        *err = -(*__errno_location ());
        goto out;
    }
    from_absolute = realpath(from, __from_absolute);
    if (!from_absolute) {
        syslog(3, "tap-err:%s: " "failed to get absolute path of %s\n", __func__, from);
        *err = -(*__errno_location ());
        goto out;
    }
    if (strnlen(to_absolute, 1000) == 1000 ||
        strnlen(from_absolute, 1000) == 1000) {
        syslog(3, "tap-err:%s: " "invalid input; max path length is %d\n", __func__, 1000);
        *err = -36;
        goto out;
    }
    from_nodes = count_nodes(from_absolute);
    common = count_common_nodes(to_absolute + 1, from_absolute + 1);
    if (common < 0) {
        syslog(3, "tap-err:%s: " "failed to count common nodes of %s and %s: %d\n", __func__, to_absolute, from_absolute, common);
        *err = common;
        goto out;
    }
    up = up_nodes(from_nodes - common - 1);
    if (!up) {
        syslog(3, "tap-err:%s: " "failed to allocate relative path for %s: %d\n", __func__, from_absolute, -12);
        *err = -12;
        goto out;
    }
    common_target_path = node_offset(to_absolute, common + 1);
    if (!common_target_path) {
        syslog(3, "tap-err:%s: " "failed to find common target path to %s: %d\n", __func__, to_absolute, -22);
        *err = -22;
        goto out;
    }
    if (asprintf(&relative_path, "%s%s", up, common_target_path) == -1) {
        syslog(3, "tap-err:%s: " "failed to construct final path %s%s: %d\n", __func__, up, common_target_path, -12);
        relative_path = ((void *)0);
        *err = -12;
        goto out;
    }
  out:
    do { free(up); up = ((void *)0); } while (0);
    return relative_path;
}

variable_types:
up
char *


repo: qileilu/htcnetup

decompiled_code:
int __fastcall process_info(int fd)
{
int v2; // eax
int v3; // eax
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

if ( (int)send(fd, &unk_402004, 4uLL, 0) < 0 )
{
perror("send");
exit(1);
}
puts("send 0x00020000 sucess!");
v2 = recv(fd, buf, 4uLL, 0);
if ( v2 < 0 )
{
perror("recv");
exit(1);
}
buf[v2] = 0;
if ( memcmp(buf, &unk_402009, 3uLL) )
{
perror("0x80020000 handshaking failure");
exit(1);
}
puts("recv 0x80020000 sucess!");
if ( (int)send(fd, &unk_40200E, 4uLL, 0) < 0 )
{
perror("send");
exit(1);
}
puts("send 0x00030000 sucess!");
v3 = recv(fd, buf, 4uLL, 0);
if ( v3 < 0 )
{
perror("recv");
exit(1);
}
buf[v3] = 0;
if ( memcmp(buf, &unk_402013, 3uLL) )
{
perror("0x80030000 handshaking failure");
exit(1);
}
puts("recv 0x80030000 sucess!");
if ( (int)send(fd, &unk_402018, 4uLL, 0) < 0 )
{
perror("send");
exit(1);
}
puts("send 0x00050000 sucess!");
return puts("the activater process success!");
}

canonical_decompiled_code:
int __fastcall func8(int fd)
{
int v2; // eax
int v3; // eax
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

if ( (int)send(fd, &unk_402004, 4uLL, 0) < 0 )
{
perror("send");
exit(1);
}
puts("send 0x00020000 sucess!");
v2 = recv(fd, buf, 4uLL, 0);
if ( v2 < 0 )
{
perror("recv");
exit(1);
}
buf[v2] = 0;
if ( memcmp(buf, &unk_402009, 3uLL) )
{
perror("0x80020000 handshaking failure");
exit(1);
}
puts("recv 0x80020000 sucess!");
if ( (int)send(fd, &unk_40200E, 4uLL, 0) < 0 )
{
perror("send");
exit(1);
}
puts("send 0x00030000 sucess!");
v3 = recv(fd, buf, 4uLL, 0);
if ( v3 < 0 )
{
perror("recv");
exit(1);
}
buf[v3] = 0;
if ( memcmp(buf, &unk_402013, 3uLL) )
{
perror("0x80030000 handshaking failure");
exit(1);
}
puts("recv 0x80030000 sucess!");
if ( (int)send(fd, &unk_402018, 4uLL, 0) < 0 )
{
perror("send");
exit(1);
}
puts("send 0x00050000 sucess!");
return puts("the activater process success!");
}

original_code:
void process_info(int s)
{
        int send_num;
        int recv_num;
        char recv_buf[4];
        send_num = send(s,"\x00\x02\x00\x00",4,0);
        if (send_num < 0){
         perror("send");
                exit(1);
        } else {
  printf("send 0x00020000 sucess!\n");
        }
        recv_num = recv(s,recv_buf,sizeof(recv_buf),0);
        if(recv_num < 0){
                perror("recv");
                exit(1);
        } else {
                recv_buf[recv_num]='\0';
  if (strcmp(recv_buf, "\x80\x02\x00\x00") != 0){
   perror("0x80020000 handshaking failure");
                        exit(1);
  } else {
   printf("recv 0x80020000 sucess!\n");
   send_num = send(s,"\x00\x03\x00\x00",4,0);
                  if (send_num < 0){
                           perror("send");
                           exit(1);
                   } else {
                           printf("send 0x00030000 sucess!\n");
                   }
  }
 }
        recv_num = recv(s,recv_buf,sizeof(recv_buf),0);
 if(recv_num < 0){
                perror("recv");
                exit(1);
        } else {
                recv_buf[recv_num]='\0';
  if (strcmp(recv_buf, "\x80\x03\x00\x00") != 0){
   perror("0x80030000 handshaking failure");
                        exit(1);
  } else {
   printf("recv 0x80030000 sucess!\n");
   send_num = send(s,"\x00\x05\x00\x00",4,0);
                   if (send_num < 0){
                           perror("send");
                           exit(1);
                   } else {
    printf("send 0x00050000 sucess!\n");
                   }
   }
 }
 printf("the activater process success!\n");
}

variable_types: s int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int *v5; // rax
struct sockaddr v6; // [rsp+0h] [rbp-28h] BYREF

if ( argc != 2 )
{
puts("usage: htcnetup <IPaddress>");
exit(1);
}
v3 = socket(2, 1, 0);
if ( v3 < 0 )
{
perror("sock");
exit(1);
}
puts("sock sucessful");
*(_QWORD *)&v6.sa_family = 2LL;
*(_QWORD *)&v6.sa_data[6] = 0LL;
*(_WORD *)v6.sa_data = _bswap_16(6000);
*(_DWORD *)&v6.sa_data[2] = inet_addr(argv[1]);
if ( connect(v3, &v6, 0x10u) < 0 )
{
perror("connect");
v5 = __errno_location();
printf("connect (%d)\n", (unsigned int)*v5);
exit(1);
}
puts("connect sucessful");
process_info(v3);
close(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int *v5; // rax
struct sockaddr v6; // [rsp+0h] [rbp-28h] BYREF

if ( argc != 2 )
{
puts("usage: htcnetup <IPaddress>");
exit(1);
}
v3 = socket(2, 1, 0);
if ( v3 < 0 )
{
perror("sock");
exit(1);
}
puts("sock sucessful");
*(_QWORD *)&v6.sa_family = 2LL;
*(_QWORD *)&v6.sa_data[6] = 0LL;
*(_WORD *)v6.sa_data = func7(6000);
*(_DWORD *)&v6.sa_data[2] = inet_addr(argv[1]);
if ( connect(v3, &v6, 0x10u) < 0 )
{
perror("connect");
v5 = __errno_location();
printf("connect (%d)\n", (unsigned int)*v5);
exit(1);
}
puts("connect sucessful");
func8(v3);
close(v3);
return 0;
}

original_code:
int main(int argc,char *argv[])
{
        int sock_fd;
        struct sockaddr_in addr_serv;
 if(argc != 2)
 {
 printf("usage: htcnetup <IPaddress>\n");
 exit(1);
 }
        sock_fd = socket(2,SOCK_STREAM,0);
        if(sock_fd < 0){
               perror("sock");
                exit(1);
        } else {
                printf("sock sucessful\n");
        }
        memset(&addr_serv,0,sizeof(addr_serv));
        addr_serv.sin_family = 2;
        addr_serv.sin_port = htons(6000);
        addr_serv.sin_addr.s_addr = inet_addr(argv[1]);
       if( connect(sock_fd,(struct sockaddr *)&addr_serv,sizeof(struct sockaddr)) < 0){
                perror("connect");
                printf("connect (%d)\n",(*__errno_location ()));
                exit(1);
       } else {
                printf("connect sucessful\n");
       }
        process_info(sock_fd);
        close(sock_fd);
}

canonical_original_code:
int main(int argc, char *argv[])
{
        int sock_fd;
        struct sockaddr_in addr_serv;
 if(argc != 2)
 {
 printf("usage: htcnetup <IPaddress>\n");
 exit(1);
 }
        sock_fd = socket(2,SOCK_STREAM,0);
        if(sock_fd < 0){
               perror("sock");
                exit(1);
        } else {
                printf("sock sucessful\n");
        }
        memset(&addr_serv,0,sizeof(addr_serv));
        addr_serv.sin_family = 2;
        addr_serv.sin_port = htons(6000);
        addr_serv.sin_addr.s_addr = inet_addr(argv[1]);
       if( connect(sock_fd,(struct sockaddr *)&addr_serv,sizeof(struct sockaddr)) < 0){
                perror("connect");
                printf("connect (%d)\n",(*__errno_location ()));
                exit(1);
       } else {
                printf("connect sucessful\n");
       }
        process_info(sock_fd);
        close(sock_fd);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
void __fastcall get_cur_time(char *s)
{
char *v1; // rbp
struct tm *v2; // r12
time_t timer; // [rsp+8h] [rbp-20h] BYREF

v1 = (char *)malloc(2uLL);
*(_WORD *)v1 = 0;
*(_QWORD *)s = 0LL;
*((_QWORD *)s + 1) = 0LL;
*((_DWORD *)s + 4) = 0;
time(&timer);
v2 = localtime(&timer);
strcpy(&s[strlen(s)], " (");
itoa(v2->tm_hour, v1);
strcat(s, v1);
*(_WORD *)&s[strlen(s)] = 58;
itoa(v2->tm_min, v1);
strcat(s, v1);
*(_WORD *)&s[strlen(s)] = 58;
itoa(v2->tm_sec, v1);
strcat(s, v1);
*(_WORD *)&s[strlen(s)] = 41;
free(v1);
}

canonical_decompiled_code:
void __fastcall func9(char *s)
{
char *v1; // rbp
struct tm *v2; // r12
time_t timer; // [rsp+8h] [rbp-20h] BYREF

v1 = (char *)malloc(2uLL);
*(_WORD *)v1 = 0;
*(_QWORD *)s = 0LL;
*((_QWORD *)s + 1) = 0LL;
*((_DWORD *)s + 4) = 0;
time(&timer);
v2 = localtime(&timer);
strcpy(&s[strlen(s)], " (");
func8(v2->tm_hour, v1);
strcat(s, v1);
*(_WORD *)&s[strlen(s)] = 58;
func8(v2->tm_min, v1);
strcat(s, v1);
*(_WORD *)&s[strlen(s)] = 58;
func8(v2->tm_sec, v1);
strcat(s, v1);
*(_WORD *)&s[strlen(s)] = 41;
free(v1);
}

original_code:
void get_cur_time(char * time_str)
{
   time_t timep;
   struct tm *p_curtime;
   char *time_tmp;
   time_tmp=(char *)malloc(2);
   memset(time_tmp,0,2);
   memset(time_str,0,20);
   time(&timep);
   p_curtime = localtime(&timep);
   strcat(time_str," (");
   itoa(p_curtime->tm_hour,time_tmp);
   strcat(time_str,time_tmp);
   strcat(time_str,":");
   itoa(p_curtime->tm_min,time_tmp);
   strcat(time_str,time_tmp);
   strcat(time_str,":");
   itoa(p_curtime->tm_sec,time_tmp);
   strcat(time_str,time_tmp);
   strcat(time_str,")");
   free(time_tmp);
}

canonical_original_code:
void get_cur_time(char *time_str)
{
   long timep;
   struct tm *p_curtime;
   char *time_tmp;
   time_tmp=(char *)malloc(2);
   memset(time_tmp,0,2);
   memset(time_str,0,20);
   time(&timep);
   p_curtime = localtime(&timep);
   strcat(time_str," (");
   itoa(p_curtime->tm_hour,time_tmp);
   strcat(time_str,time_tmp);
   strcat(time_str,":");
   itoa(p_curtime->tm_min,time_tmp);
   strcat(time_str,time_tmp);
   strcat(time_str,":");
   itoa(p_curtime->tm_sec,time_tmp);
   strcat(time_str,time_tmp);
   strcat(time_str,")");
   free(time_tmp);
}

variable_types: timep long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


decompiled_code:
int shm_create()
{
int result; // eax
int *v1; // rax
char *v2; // rax

result = shmget(0, 0x400uLL, 384);
if ( result == -1 )
{
v1 = __errno_location();
v2 = strerror(*v1);
fprintf(stderr, "Create Share Memory Error:%s\n\a", v2);
exit(1);
}
return result;
}

canonical_decompiled_code:
int func10()
{
int result; // eax
int *v1; // rax
char *v2; // rax

result = shmget(0, 0x400uLL, 384);
if ( result == -1 )
{
v1 = __errno_location();
v2 = strerror(*v1);
fprintf(stderr, "Create Share Memory Error:%s\n\a", v2);
exit(1);
}
return result;
}

original_code:
key_t shm_create()
{
   key_t shmid;
   if((shmid = shmget(((__key_t) 0),1024,0400|0200)) == -1)
   {
     fprintf(stderr,"Create Share Memory Error:%s\n\a",strerror((*__errno_location ())));
     exit(1);
   }
   return shmid;
}

canonical_original_code:
int shm_create()
{
   int shmid;
   if((shmid = shmget(((int) 0),1024,0400|0200)) == -1)
   {
     fprintf(stderr,"Create Share Memory Error:%s\n\a",strerror((*__errno_location ())));
     exit(1);
   }
   return shmid;
}

variable_types: shmid int


bindPort

decompiled_code:
__int64 __fastcall bindPort(__int16 a1)
{
unsigned int v1; // ebp
struct sockaddr v3; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 0);
v3.sa_family = 2;
*(_WORD *)v3.sa_data = _bswap_16(a1);
*(_DWORD *)&v3.sa_data[2] = 0;
if ( bind(v1, &v3, 0x10u) == -1 )
{
perror("bind");
exit(1);
}
puts("bind success!");
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int16 a1)
{
unsigned int v1; // ebp
struct sockaddr v3; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 0);
v3.sa_family = 2;
*(_WORD *)v3.sa_data = func7(a1);
*(_DWORD *)&v3.sa_data[2] = 0;
if ( bind(v1, &v3, 0x10u) == -1 )
{
perror("bind");
exit(1);
}
puts("bind success!");
return v1;
}

original_code:
int bindPort(unsigned short int port)
{
   int sockfd;
   struct sockaddr_in my_addr;
   sockfd = socket(2,SOCK_STREAM,0);
   my_addr.sin_family = 2;
   my_addr.sin_port = htons(port);
   my_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
   bzero(&(my_addr.sin_zero),0);
   if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)) == -1)
   {
     perror("bind");
     exit(1);
   }
   printf("bind success!\n");
   return sockfd;
}

canonical_original_code:
int bindPort(unsigned short port)
{
   int sockfd;
   struct sockaddr_in my_addr;
   sockfd = socket(2,SOCK_STREAM,0);
   my_addr.sin_family = 2;
   my_addr.sin_port = htons(port);
   my_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
   bzero(&(my_addr.sin_zero),0);
   if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1)
   {
     perror("bind");
     exit(1);
   }
   printf("bind success!\n");
   return sockfd;
}

variable_types:
port
unsigned short

variable_types: sockfd int


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
int v4; // er13
FILE *v5; // r14
int v6; // ebx
unsigned int v7; // eax
void *v8; // rbp
__pid_t v9; // er13
char *v10; // r15
size_t v11; // rax
char *v12; // r8
int shmid; // [rsp+4h] [rbp-64h]
char *s1; // [rsp+8h] [rbp-60h]
struct sockaddr addr; // [rsp+10h] [rbp-58h] BYREF
socklen_t addr_len[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

shmid = shm_create();
s1 = (char *)malloc(0xFFuLL);
v3 = (char *)malloc(0x14uLL);
v4 = bindPort(4500);
v5 = fopen("log.txt", "w");
if ( v5 )
{
while ( listen(v4, 10) != -1 )
{
puts("listening......");
v6 = accept(v4, &addr, addr_len);
if ( v6 < 0 )
{
perror("accept error");
exit(1);
}
v7 = (unsigned int)inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
printf("accept from:%d\n", v7);
send(v6, "|----------Welcome to the chat room! ----------|", 0x30uLL, 0);
v8 = malloc(0xFFuLL);
if ( !fork() )
{
v9 = fork();
while ( 1 )
{
while ( 1 )
{
while ( v9 > 0 )
{
memset(v8, 0, 0xF8uLL);
*((_DWORD *)v8 + 62) = 0;
*((_WORD *)v8 + 126) = 0;
*((_BYTE *)v8 + 254) = 0;
if ( (int)recv(v6, v8, 0xFFuLL, 0) <= 0 )
{
perror("recv1");
close(v6);
raise(9);
exit(1);
}
v12 = (char *)shmat(shmid, 0LL, 0);
*(_QWORD *)v12 = 0LL;
*((_QWORD *)v12 + 127) = 0LL;
memset(
(void *)((unsigned __int64)(v12 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v12 - (((_DWORD)v12 + 8) & 0xFFFFFFF8) + 1024) >> 3));
strncpy(v12, (const char *)v8, 0x400uLL);
get_cur_time(v3);
strcat((char *)v8, v3);
fprintf(v5, "%s\n", (const char *)v8);
printf(" %s\n", (const char *)v8);
}
if ( !v9 )
break;
perror("fork error");
}
sleep(1u);
v10 = (char *)shmat(shmid, 0LL, 0);
if ( strcmp(s1, v10) )
{
strcpy(s1, v10);
get_cur_time(v3);
strcat(v10, v3);
v11 = strlen(v10);
if ( send(v6, v10, v11, 0) == -1 )
perror("send error");
*(_QWORD *)v10 = 0LL;
*((_QWORD *)v10 + 127) = 0LL;
memset(
(void *)((unsigned __int64)(v10 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v10 - (((_DWORD)v10 + 8) & 0xFFFFFFF8) + 1024) >> 3));
strcpy(v10, s1);
}
}
}
}
perror("listen error");
exit(1);
}
perror("create log fail");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
int v4; // er13
FILE *v5; // r14
int v6; // ebx
unsigned int v7; // eax
void *v8; // rbp
__pid_t v9; // er13
char *v10; // r15
size_t v11; // rax
char *v12; // r8
int shmid; // [rsp+4h] [rbp-64h]
char *s1; // [rsp+8h] [rbp-60h]
struct sockaddr addr; // [rsp+10h] [rbp-58h] BYREF
socklen_t addr_len[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

shmid = func10();
s1 = (char *)malloc(0xFFuLL);
v3 = (char *)malloc(0x14uLL);
v4 = func11(4500);
v5 = fopen("log.txt", "w");
if ( v5 )
{
while ( listen(v4, 10) != -1 )
{
puts("listening......");
v6 = accept(v4, &addr, addr_len);
if ( v6 < 0 )
{
perror("accept error");
exit(1);
}
v7 = (unsigned int)inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
printf("accept from:%d\n", v7);
send(v6, "|----------Welcome to the chat room! ----------|", 0x30uLL, 0);
v8 = malloc(0xFFuLL);
if ( !fork() )
{
v9 = fork();
while ( 1 )
{
while ( 1 )
{
while ( v9 > 0 )
{
memset(v8, 0, 0xF8uLL);
*((_DWORD *)v8 + 62) = 0;
*((_WORD *)v8 + 126) = 0;
*((_BYTE *)v8 + 254) = 0;
if ( (int)recv(v6, v8, 0xFFuLL, 0) <= 0 )
{
perror("recv1");
close(v6);
raise(9);
exit(1);
}
v12 = (char *)shmat(shmid, 0LL, 0);
*(_QWORD *)v12 = 0LL;
*((_QWORD *)v12 + 127) = 0LL;
memset(
(void *)((unsigned __int64)(v12 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v12 - (((_DWORD)v12 + 8) & 0xFFFFFFF8) + 1024) >> 3));
strncpy(v12, (const char *)v8, 0x400uLL);
func9(v3);
strcat((char *)v8, v3);
fprintf(v5, "%s\n", (const char *)v8);
printf(" %s\n", (const char *)v8);
}
if ( !v9 )
break;
perror("fork error");
}
sleep(1u);
v10 = (char *)shmat(shmid, 0LL, 0);
if ( strcmp(s1, v10) )
{
strcpy(s1, v10);
func9(v3);
strcat(v10, v3);
v11 = strlen(v10);
if ( send(v6, v10, v11, 0) == -1 )
perror("send error");
*(_QWORD *)v10 = 0LL;
*((_QWORD *)v10 + 127) = 0LL;
memset(
(void *)((unsigned __int64)(v10 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v10 - (((_DWORD)v10 + 8) & 0xFFFFFFF8) + 1024) >> 3));
strcpy(v10, s1);
}
}
}
}
perror("listen error");
exit(1);
}
perror("create log fail");
exit(1);
}

original_code:
int main(int argc, char *argv[])
{
   struct sockaddr_in clientaddr;
   pid_t pid;
   int clientfd,sendbytes,recvbytes;
   struct hostent *host;
   char *buf,*buf_r;
   if(argc < 4)
   {
    printf("usage:\n");
    printf("%s hostname port yourname\n",argv[0]);
    exit(1);
   }
   host = gethostbyname(argv[1]);
   if((clientfd = socket(2,SOCK_STREAM,0)) == -1)
   {
     perror("socket error\n");
     exit(1);
   }
   clientaddr.sin_family = 2;
   clientaddr.sin_port = htons((uint16_t)atoi(argv[2]));
   clientaddr.sin_addr = *((struct in_addr *)host->h_addr_list[0]);
   bzero(&(clientaddr.sin_zero),0);
   if(connect(clientfd,(struct sockaddr *)&clientaddr,sizeof(struct sockaddr)) == -1)
   {
     perror("connect error\n");
     exit(1);
   }
   buf=(char *)malloc(120);
   memset(buf,0,120);
   buf_r=(char *)malloc(100);
   if( recv(clientfd,buf,100,0) == -1)
   {
     perror("recv error");
     exit(1);
   }
   printf("\n%s\n",buf);
   pid = fork();
   while(1)
   {
     if(pid > 0){
       strcpy(buf,argv[3]);
       strcat(buf,":");
       memset(buf_r,0,100);
       fgets(buf_r,100,stdin);
       strncat(buf,buf_r,strlen(buf_r)-1);
       if((sendbytes = send(clientfd,buf,strlen(buf),0)) == -1)
       {
         perror("send\n");
         exit(1);
       }
     }
     else if(pid == 0)
     {
       memset(buf,0,100);
       if(recv(clientfd,buf,100,0) <= 0)
       {
         perror("recv error");
         close(clientfd);
         raise(19);
         exit(1);
       }
       printf("%s\n",buf);
     }
     else
       perror("fork error");
   }
   close(clientfd);
   return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
   struct sockaddr_in clientaddr;
   int pid;
   int clientfd; int sendbytes; int recvbytes;
   struct hostent *host;
   char *buf; char *buf_r;
   if(argc < 4)
   {
    printf("usage:\n");
    printf("%s hostname port yourname\n",argv[0]);
    exit(1);
   }
   host = gethostbyname(argv[1]);
   if((clientfd = socket(2,SOCK_STREAM,0)) == -1)
   {
     perror("socket error\n");
     exit(1);
   }
   clientaddr.sin_family = 2;
   clientaddr.sin_port = htons((unsigned short)atoi(argv[2]));
   clientaddr.sin_addr = *((struct in_addr *)host->h_addr_list[0]);
   bzero(&(clientaddr.sin_zero),0);
   if(connect(clientfd,(struct sockaddr *)&clientaddr,sizeof(struct sockaddr)) == -1)
   {
     perror("connect error\n");
     exit(1);
   }
   buf=(char *)malloc(120);
   memset(buf,0,120);
   buf_r=(char *)malloc(100);
   if( recv(clientfd,buf,100,0) == -1)
   {
     perror("recv error");
     exit(1);
   }
   printf("\n%s\n",buf);
   pid = fork();
   while(1)
   {
     if(pid > 0){
       strcpy(buf,argv[3]);
       strcat(buf,":");
       memset(buf_r,0,100);
       fgets(buf_r,100,stdin);
       strncat(buf,buf_r,strlen(buf_r)-1);
       if((sendbytes = send(clientfd,buf,strlen(buf),0)) == -1)
       {
         perror("send\n");
         exit(1);
       }
     }
     else if(pid == 0)
     {
       memset(buf,0,100);
       if(recv(clientfd,buf,100,0) <= 0)
       {
         perror("recv error");
         close(clientfd);
         raise(19);
         exit(1);
       }
       printf("%s\n",buf);
     }
     else
       perror("fork error");
   }
   close(clientfd);
   return 0;
}

variable_types: pid int

variable_types: clientfd int

variable_types: sendbytes int

variable_types: recvbytes int

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types:
buf
char *


alloc

decompiled_code:
void *__fastcall alloc(size_t a1)
{
return calloc(a1, 1uLL);
}

canonical_decompiled_code:
void *__fastcall func1(size_t a1)
{
return calloc(a1, 1uLL);
}

original_code:
static void *alloc(size_t size)
{
 return calloc(size, 1);
}

canonical_original_code:
static void *alloc(unsigned long size)
{
 return calloc(size, 1);
}

variable_types:
size
unsigned long

return_type: void *