decompiled_code:
__off_t __fastcall mcfs_dev_read(__int64 a1, __off_t a2, void *a3, size_t a4)
{
int v5; // edi
__off_t v8; // r12
ssize_t v9; // r12
__off_t result; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax

v5 = *(_DWORD *)(a1 + 8);
if ( v5 == -1 )
{
v9 = -1LL;
puts("open device first");
return v9;
}
v8 = lseek(v5, a2, 0);
if ( v8 < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
printf("lseek file failed! err:%s\n", v12);
result = v8;
}
else
{
v9 = read(*(_DWORD *)(a1 + 8), a3, a4);
if ( v9 >= 0 )
return v9;
v13 = _errno_location();
v14 = strerror(*v13);
printf("read file failed! err:%s\n", v14);
result = v9;
}
return result;
}

original_code:
long long mcfs_dev_read(mcfs_dev_t * ptr, loff_t offset, char * block, long long bytes){
    loff_t ret;
    do { if((ptr->_fd == (-1))) { printf("open device first\n"); return -1; } } while (0);
    ret = lseek(ptr->_fd, offset, 0);
    do { if((ret < 0)) { printf("lseek file failed! err:%s\n", strerror((*__errno_location ()))); return ret; } } while (0);
    ret = read(ptr->_fd, block, bytes);
    do { if((ret < 0)) { printf("read file failed! err:%s\n", strerror((*__errno_location ()))); return ret; } } while (0);
    return ret;
}

canonical_original_code:
long long mcfs_dev_read(struct mcfs_dev *ptr, long offset, char *block, long long bytes){
    long ret;
    do { if((ptr->_fd == (-1))) { printf("open device first\n"); return -1; } } while (0);
    ret = lseek(ptr->_fd, offset, 0);
    do { if((ret < 0)) { printf("lseek file failed! err:%s\n", strerror((*__errno_location ()))); return ret; } } while (0);
    ret = read(ptr->_fd, block, bytes);
    do { if((ret < 0)) { printf("read file failed! err:%s\n", strerror((*__errno_location ()))); return ret; } } while (0);
    return ret;
}


repo: yunccll/fat

decompiled_code:
_BOOL8 __fastcall mcfs_dev_is_open(__int64 a1)
{
return *(_DWORD *)(a1 + 12) == 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1)
{
return *(_DWORD *)(a1 + 12) == 1;
}

original_code:
int mcfs_dev_is_open(mcfs_dev_t * ptr){
    return ptr->_is_connected == 1;
}

canonical_original_code:
int mcfs_dev_is_open(struct mcfs_dev *ptr){
    return ptr->_is_connected == 1;
}

variable_types: struct mcfs_dev { char *_fname; int _fd; int _is_connected; } *

return_type: int


decompiled_code:
__int64 __fastcall mcfs_dev_fname(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
const char * mcfs_dev_fname(mcfs_dev_t * ptr){
    return ptr->_fname;
}

canonical_original_code:
char *mcfs_dev_fname(struct mcfs_dev *ptr){
    return ptr->_fname;
}

return_type: char *

user_defined_types: struct mcfs_dev { char *_fname; int _fd; int _is_connected; }


decompiled_code:
int __fastcall mcfs_dev_print(__int64 a1)
{
return printf(
"fd:%d, is_connected:%d, name:%s\n",
*(unsigned int *)(a1 + 8),
*(unsigned int *)(a1 + 12),
*(const char **)a1);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
return printf(
"fd:%d, is_connected:%d, name:%s\n",
*(unsigned int *)(a1 + 8),
*(unsigned int *)(a1 + 12),
*(const char **)a1);
}

original_code:
void mcfs_dev_print(mcfs_dev_t * ptr){
    printf("fd:%d, is_connected:%d, name:%s\n", ptr->_fd, ptr->_is_connected, ptr->_fname);
}

canonical_original_code:
void mcfs_dev_print(struct mcfs_dev *ptr){
    printf("fd:%d, is_connected:%d, name:%s\n", ptr->_fd, ptr->_is_connected, ptr->_fname);
}

return_type: void


decompiled_code:
int __fastcall spin_lock(unsigned int *a1)
{
int result; // eax

if ( *a1 )
return printf("DEBUG: spin lock failed, count:[%d]\n", *a1);
result = *a1 + 1;
*a1 = result;
return result;
}

canonical_decompiled_code:
int __fastcall func1(unsigned int *a1)
{
int result; // eax

if ( *a1 )
return printf("DEBUG: spin lock failed, count:[%d]\n", *a1);
result = *a1 + 1;
*a1 = result;
return result;
}

original_code:
void spin_lock(spinlock_t * lock)
{
    if(lock->count == 0){
        ++(lock->count);
    }
    else{
        printf("DEBUG: spin lock failed, count:[%d]\n", lock->count);
    }
}

canonical_original_code:
void spin_lock(struct spinlock_t *lock)
{
    if(lock->count == 0){
        ++(lock->count);
    }
    else{
        printf("DEBUG: spin lock failed, count:[%d]\n", lock->count);
    }
}

variable_types:
lock
struct spinlock_t { int count; } *


decompiled_code:
int __fastcall spin_unlock(unsigned int *a1)
{
int result; // eax

if ( (int)*a1 <= 0 )
return printf("DEBUG: spinlock unlock failed, count:[%d]\n", *a1);
result = *a1 - 1;
*a1 = result;
return result;
}

canonical_decompiled_code:
int __fastcall func2(unsigned int *a1)
{
int result; // eax

if ( (int)*a1 <= 0 )
return printf("DEBUG: spinlock unlock failed, count:[%d]\n", *a1);
result = *a1 - 1;
*a1 = result;
return result;
}

original_code:
void spin_unlock(spinlock_t * lock)
{
    if(lock->count > 0){
        --(lock->count);
    }
    else{
        printf("DEBUG: spinlock unlock failed, count:[%d]\n", lock->count);
    }
}

canonical_original_code:
void spin_unlock(struct spinlock_t *lock)
{
    if(lock->count > 0){
        --(lock->count);
    }
    else{
        printf("DEBUG: spinlock unlock failed, count:[%d]\n", lock->count);
    }
}


decompiled_code:
__int64 __fastcall spin_trylock(_DWORD *a1)
{
__int64 result; // rax

result = 0LL;
if ( !*a1 )
{
++*a1;
result = 1LL;
}
return result;
}

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

result = 0LL;
if ( !*a1 )
{
++*a1;
result = 1LL;
}
return result;
}

original_code:
int spin_trylock(spinlock_t *lock)
{
    if(lock->count == 0){
        ++(lock->count);
        return 1;
    }
    else{
        return 0;
    }
}

canonical_original_code:
int spin_trylock(struct spinlock_t *lock)
{
    if(lock->count == 0){
        ++(lock->count);
        return 1;
    }
    else{
        return 0;
    }
}


decompiled_code:
_BOOL8 __fastcall spin_is_locked(_DWORD *a1)
{
return *a1 > 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(_DWORD *a1)
{
return *a1 > 0;
}

original_code:
int spin_is_locked(spinlock_t *lock)
{
    return lock->count > 0;
}

canonical_original_code:
int spin_is_locked(struct spinlock_t *lock)
{
    return lock->count > 0;
}


down

decompiled_code:
int __fastcall down(_DWORD *a1)
{
__int64 v1; // rsi
int result; // eax

v1 = (unsigned int)*a1;
if ( (int)v1 <= 0 )
return printf("DEBUG: down wait, count:[%d]\n", v1);
*a1 = v1 - 1;
return result;
}

canonical_decompiled_code:
int __fastcall func5(_DWORD *a1)
{
__int64 v1; // rsi
int result; // eax

v1 = (unsigned int)*a1;
if ( (int)v1 <= 0 )
return printf("DEBUG: down wait, count:[%d]\n", v1);
*a1 = v1 - 1;
return result;
}

original_code:
void down(struct semaphore *sem)
{
 if(sem->count > 0){
  sem->count --;
 }
 else{
  printf("DEBUG: down wait, count:[%d]\n", sem->count);
 }
}

variable_types:
sem
struct semaphore { int count; } *


up

decompiled_code:
int __fastcall up(_DWORD *a1)
{
__int64 v1; // rsi
int result; // eax

v1 = (unsigned int)*a1;
if ( (_DWORD)v1 )
return printf("DEBUG: up wait, count:[%d]\n", v1);
*a1 = 1;
return result;
}

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

v1 = (unsigned int)*a1;
if ( (_DWORD)v1 )
return printf("DEBUG: up wait, count:[%d]\n", v1);
*a1 = 1;
return result;
}

original_code:
void up(struct semaphore *sem)
{
 if(sem->count == 0){
  sem->count ++;
 }
 else{
  printf("DEBUG: up wait, count:[%d]\n", sem->count);
 }
}


decompiled_code:
__int64 __fastcall down_trylock(int *a1)
{
int v1; // eax

v1 = *a1 - 1;
if ( v1 >= 0 )
*a1 = v1;
return (unsigned int)v1 >> 31;
}

canonical_decompiled_code:
__int64 __fastcall func7(int *a1)
{
int v1; // eax

v1 = *a1 - 1;
if ( v1 >= 0 )
*a1 = v1;
return (unsigned int)v1 >> 31;
}

original_code:
int down_trylock(struct semaphore *sem)
{
 int count = sem->count - 1;
 if(count >= 0){
  sem->count = count;
 }
    return count < 0;
}

variable_types: count int


decompiled_code:
int __fastcall down_read(unsigned int *a1)
{
int result; // eax

if ( (int)*a1 <= 0 )
return printf("DEBUG: down_read wait, count:[%d]\n", *a1);
result = *a1 - 1;
*a1 = result;
return result;
}

canonical_decompiled_code:
int __fastcall func9(unsigned int *a1)
{
int result; // eax

if ( (int)*a1 <= 0 )
return printf("DEBUG: down_read wait, count:[%d]\n", *a1);
result = *a1 - 1;
*a1 = result;
return result;
}

original_code:
void down_read(struct rw_semaphore *sem)
{
    if(sem->count >0){
        sem->count --;
    }
    else{
        printf("DEBUG: down_read wait, count:[%d]\n", sem->count);
    }
}

variable_types: struct rw_semaphore { int count; int write_count; } *


decompiled_code:
__int64 __fastcall down_read_trylock(int *a1)
{
int v1; // eax

v1 = *a1 - 1;
if ( v1 >= 0 )
*a1 = v1;
return (unsigned int)v1 >> 31;
}

canonical_decompiled_code:
__int64 __fastcall func10(int *a1)
{
int v1; // eax

v1 = *a1 - 1;
if ( v1 >= 0 )
*a1 = v1;
return (unsigned int)v1 >> 31;
}

original_code:
int down_read_trylock(struct rw_semaphore *sem)
{
 int count = sem->count - 1;
 if(count >= 0){
  sem->count = count;
 }
    return count < 0;
}

user_defined_types: struct rw_semaphore { int count; int write_count; }


decompiled_code:
int __fastcall up_read(unsigned int *a1)
{
int result; // eax

if ( *a1 )
return printf("DEBUG: up wait, count:[%d]\n", *a1);
result = *a1 + 1;
*a1 = result;
return result;
}

canonical_decompiled_code:
int __fastcall func11(unsigned int *a1)
{
int result; // eax

if ( *a1 )
return printf("DEBUG: up wait, count:[%d]\n", *a1);
result = *a1 + 1;
*a1 = result;
return result;
}

original_code:
void up_read(struct rw_semaphore *sem)
{
 if(sem->count == 0){
  sem->count ++;
 }
 else{
  printf("DEBUG: up wait, count:[%d]\n", sem->count);
 }
}


decompiled_code:
int __fastcall down_write(__int64 a1)
{
int result; // eax

if ( *(int *)(a1 + 4) <= 0 )
return printf("DEBUG: down_write wait, write_count:[%d]\n", *(unsigned int *)(a1 + 4));
result = *(_DWORD *)(a1 + 4) - 1;
*(_DWORD *)(a1 + 4) = result;
return result;
}

canonical_decompiled_code:
int __fastcall func12(__int64 a1)
{
int result; // eax

if ( *(int *)(a1 + 4) <= 0 )
return printf("DEBUG: down_write wait, write_count:[%d]\n", *(unsigned int *)(a1 + 4));
result = *(_DWORD *)(a1 + 4) - 1;
*(_DWORD *)(a1 + 4) = result;
return result;
}

original_code:
void down_write(struct rw_semaphore *sem)
{
    if(sem->write_count >0){
        sem->write_count --;
    }
    else{
        printf("DEBUG: down_write wait, write_count:[%d]\n", sem->write_count);
    }
}


decompiled_code:
__int64 __fastcall down_write_trylock(__int64 a1)
{
int v1; // eax

v1 = *(_DWORD *)(a1 + 4) - 1;
if ( v1 >= 0 )
*(_DWORD *)(a1 + 4) = v1;
return (unsigned int)v1 >> 31;
}

original_code:
int down_write_trylock(struct rw_semaphore *sem)
{
 int count = sem->write_count - 1;
 if(count >= 0){
  sem->write_count = count;
 }
    return count < 0;
}


decompiled_code:
int __fastcall up_write(__int64 a1)
{
int result; // eax

if ( *(_DWORD *)(a1 + 4) )
return printf("DEBUG: up_write wait, write count:[%d]\n", *(unsigned int *)(a1 + 4));
result = *(_DWORD *)(a1 + 4) + 1;
*(_DWORD *)(a1 + 4) = result;
return result;
}

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

if ( *(_DWORD *)(a1 + 4) )
return printf("DEBUG: up_write wait, write count:[%d]\n", *(unsigned int *)(a1 + 4));
result = *(_DWORD *)(a1 + 4) + 1;
*(_DWORD *)(a1 + 4) = result;
return result;
}

original_code:
void up_write(struct rw_semaphore *sem)
{
 if(sem->write_count == 0){
  sem->write_count ++;
 }
 else{
  printf("DEBUG: up_write wait, write count:[%d]\n", sem->write_count);
 }
}


decompiled_code:
int __fastcall downgrade_write(unsigned int *a1)
{
up_write((__int64)a1);
return down_read(a1);
}

canonical_decompiled_code:
int __fastcall func15(unsigned int *a1)
{
func14((__int64)a1);
return func9(a1);
}

original_code:
void downgrade_write(struct rw_semaphore *sem)
{
    up_write(sem);
    down_read(sem);
}


decompiled_code:
int __fastcall mutex_lock(unsigned int *a1)
{
int result; // eax

if ( *a1 )
return printf("mutex lock wait....., count:[%d]\n", *a1);
result = *a1 + 1;
*a1 = result;
return result;
}

canonical_decompiled_code:
int __fastcall func17(unsigned int *a1)
{
int result; // eax

if ( *a1 )
return printf("mutex lock wait....., count:[%d]\n", *a1);
result = *a1 + 1;
*a1 = result;
return result;
}

original_code:
void mutex_lock(struct mutex * lock)
{
    if(lock->count == 0){
        lock->count ++;
    }
    else{
        printf("mutex lock wait....., count:[%d]\n", lock->count);
    }
}

canonical_original_code:
void mutex_lock(struct mutex *lock)
{
    if(lock->count == 0){
        lock->count ++;
    }
    else{
        printf("mutex lock wait....., count:[%d]\n", lock->count);
    }
}

variable_types:
lock
struct mutex { int count; } *


decompiled_code:
int __fastcall mutex_unlock(unsigned int *a1)
{
int result; // eax

if ( (int)*a1 <= 0 )
return printf("mutex unlock failed, count:[%d]\n", *a1);
result = *a1 - 1;
*a1 = result;
return result;
}

canonical_decompiled_code:
int __fastcall func18(unsigned int *a1)
{
int result; // eax

if ( (int)*a1 <= 0 )
return printf("mutex unlock failed, count:[%d]\n", *a1);
result = *a1 - 1;
*a1 = result;
return result;
}

original_code:
void mutex_unlock(struct mutex * lock)
{
    if(lock->count > 0){
        lock->count --;
    }
    else{
        printf("mutex unlock failed, count:[%d]\n", lock->count);
    }
}

canonical_original_code:
void mutex_unlock(struct mutex *lock)
{
    if(lock->count > 0){
        lock->count --;
    }
    else{
        printf("mutex unlock failed, count:[%d]\n", lock->count);
    }
}


decompiled_code:
__int64 __fastcall mutex_trylock(_DWORD *a1)
{
__int64 result; // rax

result = 0LL;
if ( !*a1 )
{
--*a1;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(_DWORD *a1)
{
__int64 result; // rax

result = 0LL;
if ( !*a1 )
{
--*a1;
result = 1LL;
}
return result;
}

original_code:
int mutex_trylock(struct mutex * lock)
{
    if(lock->count == 0){
        lock->count --;
        return 1;
    }
    return 0;
}

canonical_original_code:
int mutex_trylock(struct mutex *lock)
{
    if(lock->count == 0){
        lock->count --;
        return 1;
    }
    return 0;
}


decompiled_code:
void __fastcall fat_block_view_free(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func8(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void fat_block_view_free(fat_block_view_t * ptr){
    if(ptr){
        free(ptr);
    }
}

canonical_original_code:
void fat_block_view_free(struct fat_block_view_s *ptr){
    if(ptr){
        free(ptr);
    }
}

variable_types: struct fat_block_view_s { int _index; char *_offset; int _size; } *


decompiled_code:
int __fastcall fat_block_view_print(const void **a1)
{
printf("block index: %d ", *(unsigned int *)a1);
printf("block offset: %p ", a1[1]);
printf("block_size: %d ", *((unsigned int *)a1 + 4));
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func10(const void **a1)
{
printf("block index: %d ", *(unsigned int *)a1);
printf("block offset: %p ", a1[1]);
printf("block_size: %d ", *((unsigned int *)a1 + 4));
return putchar(10);
}

original_code:
void fat_block_view_print(fat_block_view_t * ptr){
    printf("block index: %d ", ptr->_index);
    printf("block offset: %p ", ptr->_offset);
    printf("block_size: %d ", ptr->_size);
    printf("\n");
}

canonical_original_code:
void fat_block_view_print(struct fat_block_view_s *ptr){
    printf("block index: %d ", ptr->_index);
    printf("block offset: %p ", ptr->_offset);
    printf("block_size: %d ", ptr->_size);
    printf("\n");
}

user_defined_types: struct fat_block_view_s { int _index; char *_offset; int _size; }


original_code:
int fat_boot_parser(fat_boot_t * ptr, char * block_offset, int bytes){
    if(bytes >= 512){
        struct boot_sector_s * boot = (struct boot_sector_s*)block_offset;
        ptr->_total_sector = boot->bpb.total_sector16;
        ptr->_bytes_per_sector = boot->bpb.bytes_per_sector;
        ptr->_reserved_sector_count = boot->bpb.reserved_sector_count;
        ptr->_number_of_fats = boot->bpb.number_of_fats;
        ptr->_sector_per_fat = boot->bpb.sector_per_fat;
        ptr->_sector_per_cluster = boot->bpb.sector_per_cluster;
        ptr->_root_entry_count = boot->bpb.root_entry_count;
        ptr->_bytes_per_entry = 32;
        ptr->_media = boot->bpb.media;
        ptr->_first_sector_of_data = (1 + 9 + 9 + 14);
        memcpy(ptr-> _file_system_type, &(boot->bpb.special.fat_boot_sector.file_system_type), 8);
        return 0;
    }
    printf("Error: boot block size < 512bytes \n");
    return -1;
}

canonical_original_code:
int fat_boot_parser(struct fat_boot_s *ptr, char *block_offset, int bytes){
    if(bytes >= 512){
        struct boot_sector_s *boot = (struct boot_sector_s*)block_offset;
        ptr->_total_sector = boot->bpb.total_sector16;
        ptr->_bytes_per_sector = boot->bpb.bytes_per_sector;
        ptr->_reserved_sector_count = boot->bpb.reserved_sector_count;
        ptr->_number_of_fats = boot->bpb.number_of_fats;
        ptr->_sector_per_fat = boot->bpb.sector_per_fat;
        ptr->_sector_per_cluster = boot->bpb.sector_per_cluster;
        ptr->_root_entry_count = boot->bpb.root_entry_count;
        ptr->_bytes_per_entry = 32;
        ptr->_media = boot->bpb.media;
        ptr->_first_sector_of_data = (1 + 9 + 9 + 14);
        memcpy(ptr-> _file_system_type, &(boot->bpb.special.fat_boot_sector.file_system_type), 8);
        return 0;
    }
    printf("Error: boot block size < 512bytes \n");
    return -1;
}

variable_types: struct fat_boot_s { long long _total_sector; int _bytes_per_sector; int _reserved_sector_count; int _number_of_fats; int _sector_per_fat; int _sector_per_cluster; int _root_entry_count; int _bytes_per_entry; int _media; int _first_sector_of_data; char _file_system_type[9]; } *

variable_types: bytes int

variable_types: struct boot_sector_s { char jump_to_boot[3]; char oem_name[8]; struct bios_parameter_block_s { unsigned short bytes_per_sector; unsigned char sector_per_cluster; unsigned short reserved_sector_count; unsigned char number_of_fats; unsigned short root_entry_count; unsigned short total_sector16; unsigned char media; unsigned short sector_per_fat; unsigned short sector_per_track; unsigned short number_of_headers; unsigned int hidden_sectors; unsigned int total_sector32; union fatSpecial { struct fat_s { unsigned char number_of_driver; unsigned char reserved1; unsigned char boot_signature; unsigned int volumeId; char volume_label[11]; char file_system_type[8]; } fat_boot_sector; struct fat32_s { unsigned int sector_per_fat32; unsigned short extend_flag; unsigned short file_system_version; unsigned int cluster_number_of_root_directory; unsigned short fs_info; unsigned short backup_boot_sector; char reserved[12]; unsigned char driver_number; unsigned char reserved1; } fat32_boot_sector; } special; } bpb; } *

user_defined_types: struct boot_sector_s { char jump_to_boot[3]; char oem_name[8]; struct bios_parameter_block_s bpb; }

user_defined_types: struct bios_parameter_block_s { unsigned short bytes_per_sector; unsigned char sector_per_cluster; unsigned short reserved_sector_count; unsigned char number_of_fats; unsigned short root_entry_count; unsigned short total_sector16; unsigned char media; unsigned short sector_per_fat; unsigned short sector_per_track; unsigned short number_of_headers; unsigned int hidden_sectors; unsigned int total_sector32; union fatSpecial special; }

user_defined_types: union fatSpecial { struct fat_s fat_boot_sector; struct fat32_s fat32_boot_sector; }


decompiled_code:
void *fat_boot_create()
{
void *v0; // r12

v0 = calloc(0x38uLL, 1uLL);
if ( !v0 )
puts("no enough memory");
return v0;
}

canonical_decompiled_code:
void *func1()
{
void *v0; // r12

v0 = calloc(0x38uLL, 1uLL);
if ( !v0 )
puts("no enough memory");
return v0;
}

original_code:
fat_boot_t * fat_boot_create(){
    fat_boot_t * ptr = (fat_boot_t *)calloc(sizeof(fat_boot_t), 1);
    do { if((ptr == ((void *)0))) { printf("no enough memory\n"); return ((void *)0); } } while (0);
    return ptr;
}

canonical_original_code:
struct fat_boot_s *fat_boot_create(){
    struct fat_boot_s *ptr = (fat_boot_t *)calloc(sizeof(fat_boot_t), 1);
    do { if((ptr == ((void *)0))) { printf("no enough memory\n"); return ((void *)0); } } while (0);
    return ptr;
}

return_type: struct fat_boot_s { long long _total_sector; int _bytes_per_sector; int _reserved_sector_count; int _number_of_fats; int _sector_per_fat; int _sector_per_cluster; int _root_entry_count; int _bytes_per_entry; int _media; int _first_sector_of_data; char _file_system_type[9]; } *


decompiled_code:
void __fastcall fat_boot_destroy(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func2(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void fat_boot_destroy(fat_boot_t * ptr){
    if(ptr){
        free(ptr);
    }
}

canonical_original_code:
void fat_boot_destroy(struct fat_boot_s *ptr){
    if(ptr){
        free(ptr);
    }
}

user_defined_types: struct fat_boot_s { long long _total_sector; int _bytes_per_sector; int _reserved_sector_count; int _number_of_fats; int _sector_per_fat; int _sector_per_cluster; int _root_entry_count; int _bytes_per_entry; int _media; int _first_sector_of_data; char _file_system_type[9]; }


decompiled_code:
int __fastcall fat_boot_print(__int64 a1)
{
printf("total_sector:%lld ", *(_QWORD *)a1);
printf(",bytes_per_sector:%d ", *(unsigned int *)(a1 + 8));
printf(",reserved_sector_count:%d ", *(unsigned int *)(a1 + 12));
printf(",number_of_fats:%d ", *(unsigned int *)(a1 + 16));
printf(",sector_per_fat:%d ", *(unsigned int *)(a1 + 20));
putchar(10);
printf(",sector_per_cluster:%d ", *(unsigned int *)(a1 + 24));
printf(",root_entry_count:%d ", *(unsigned int *)(a1 + 28));
printf(",bytes_per_sector:%d ", *(unsigned int *)(a1 + 32));
putchar(10);
printf(",media:%d ", *(unsigned int *)(a1 + 36));
printf(",firstSectorOfData: %d ", *(unsigned int *)(a1 + 40));
printf(",file_system_type:%s", (const char *)(a1 + 44));
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
printf("total_sector:%lld ", *(_QWORD *)a1);
printf(",bytes_per_sector:%d ", *(unsigned int *)(a1 + 8));
printf(",reserved_sector_count:%d ", *(unsigned int *)(a1 + 12));
printf(",number_of_fats:%d ", *(unsigned int *)(a1 + 16));
printf(",sector_per_fat:%d ", *(unsigned int *)(a1 + 20));
putchar(10);
printf(",sector_per_cluster:%d ", *(unsigned int *)(a1 + 24));
printf(",root_entry_count:%d ", *(unsigned int *)(a1 + 28));
printf(",bytes_per_sector:%d ", *(unsigned int *)(a1 + 32));
putchar(10);
printf(",media:%d ", *(unsigned int *)(a1 + 36));
printf(",firstSectorOfData: %d ", *(unsigned int *)(a1 + 40));
printf(",file_system_type:%s", (const char *)(a1 + 44));
return putchar(10);
}

original_code:
void fat_boot_print(fat_boot_t * ptr){
    printf("total_sector:%lld ", ptr->_total_sector);
    printf(",bytes_per_sector:%d ", ptr->_bytes_per_sector);
    printf(",reserved_sector_count:%d ", ptr->_reserved_sector_count);
    printf(",number_of_fats:%d ", ptr->_number_of_fats);
    printf(",sector_per_fat:%d ", ptr->_sector_per_fat);
    printf("\n");
    printf(",sector_per_cluster:%d ", ptr->_sector_per_cluster);
    printf(",root_entry_count:%d ", ptr->_root_entry_count);
    printf(",bytes_per_sector:%d ", ptr->_bytes_per_entry);
    printf("\n");
    printf(",media:%d ", ptr->_media);
    printf(",firstSectorOfData: %d ", ptr->_first_sector_of_data);
    printf(",file_system_type:%s", ptr->_file_system_type);
    printf("\n");
}

canonical_original_code:
void fat_boot_print(struct fat_boot_s *ptr){
    printf("total_sector:%lld ", ptr->_total_sector);
    printf(",bytes_per_sector:%d ", ptr->_bytes_per_sector);
    printf(",reserved_sector_count:%d ", ptr->_reserved_sector_count);
    printf(",number_of_fats:%d ", ptr->_number_of_fats);
    printf(",sector_per_fat:%d ", ptr->_sector_per_fat);
    printf("\n");
    printf(",sector_per_cluster:%d ", ptr->_sector_per_cluster);
    printf(",root_entry_count:%d ", ptr->_root_entry_count);
    printf(",bytes_per_sector:%d ", ptr->_bytes_per_entry);
    printf("\n");
    printf(",media:%d ", ptr->_media);
    printf(",firstSectorOfData: %d ", ptr->_first_sector_of_data);
    printf(",file_system_type:%s", ptr->_file_system_type);
    printf("\n");
}


decompiled_code:
void *fat_fat_create()
{
void *v0; // r12

v0 = calloc(4uLL, 1uLL);
if ( !v0 )
puts("no enough memory");
return v0;
}

canonical_decompiled_code:
void *func0()
{
void *v0; // r12

v0 = calloc(4uLL, 1uLL);
if ( !v0 )
puts("no enough memory");
return v0;
}

original_code:
fat_fat_t * fat_fat_create(){
    fat_fat_t * ptr = (fat_fat_t*)calloc(sizeof(fat_fat_t), 1);
    do { if((ptr == ((void *)0))) { printf("no enough memory\n"); return ((void *)0); } } while (0);
    return ptr;
}

canonical_original_code:
struct fat_fat_s *fat_fat_create(){
    struct fat_fat_s *ptr = (fat_fat_t*)calloc(sizeof(fat_fat_t), 1);
    do { if((ptr == ((void *)0))) { printf("no enough memory\n"); return ((void *)0); } } while (0);
    return ptr;
}

variable_types:
ptr
struct fat_fat_s { int loop; } *

return_type: struct fat_fat_s { int loop; } *


decompiled_code:
void __fastcall fat_fat_destroy(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void fat_fat_destroy(fat_fat_t * ptr){
    if(ptr){
        free(ptr);
    }
}

canonical_original_code:
void fat_fat_destroy(struct fat_fat_s *ptr){
    if(ptr){
        free(ptr);
    }
}


decompiled_code:
void fat_fat_print()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void fat_fat_print(fat_fat_t * ptr){
}

canonical_original_code:
void fat_fat_print(struct fat_fat_s *ptr){
}


decompiled_code:
__int64 fat_fat_parser()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
return 0LL;
}

original_code:
int fat_fat_parser(fat_fat_t * ptr, const char * block_offset, int bytes){
    return 0;
}

canonical_original_code:
int fat_fat_parser(struct fat_fat_s *ptr, char *block_offset, int bytes){
    return 0;
}


decompiled_code:
int __fastcall fat_sb_print(unsigned __int16 *a1)
{
printf("DEBUG: sector_size in disk - [%d]\n", *a1);
printf("DEBUG: fat bits (12, 16 32) - [%d]\n", *((unsigned __int8 *)a1 + 2));
printf("DEBUG: sector number of fsinfo - [%d]\n", *((unsigned int *)a1 + 1));
printf("DEBUG: fat sector start - [%d]\n", a1[4]);
printf("DEBUG: fat length - [%d]\n", *((unsigned int *)a1 + 3));
printf("DEBUG: number of fats - [%d]\n", *((unsigned __int8 *)a1 + 16));
printf("DEBUG: root dir sector start - [%d]\n", *((unsigned int *)a1 + 5));
printf("DEBUG: number of entry in root_directory - [%d]\n", a1[12]);
printf("DEBUG: first data sector - [%d]\n", *((unsigned int *)a1 + 7));
printf("DEBUG: max sectors in disk - [%d]\n", *((unsigned int *)a1 + 8));
printf("DEBUG: number of sectors per cluster - [%d]\n", a1[18]);
printf("DEBUG: first cluster of root cluster - [%d]\n", *((unsigned int *)a1 + 10));
printf("DEBUG: maximum cluster number - [%d]\n", *((unsigned int *)a1 + 11));
printf("DEBUG: cluster_size - [%d]\n", *((unsigned int *)a1 + 12));
return printf("DEBUG: log2(cluster_size) - [%d]\n", a1[26]);
}

canonical_decompiled_code:
int __fastcall func2(unsigned __int16 *a1)
{
printf("DEBUG: sector_size in disk - [%d]\n", *a1);
printf("DEBUG: fat bits (12, 16 32) - [%d]\n", *((unsigned __int8 *)a1 + 2));
printf("DEBUG: sector number of fsinfo - [%d]\n", *((unsigned int *)a1 + 1));
printf("DEBUG: fat sector start - [%d]\n", a1[4]);
printf("DEBUG: fat length - [%d]\n", *((unsigned int *)a1 + 3));
printf("DEBUG: number of fats - [%d]\n", *((unsigned __int8 *)a1 + 16));
printf("DEBUG: root dir sector start - [%d]\n", *((unsigned int *)a1 + 5));
printf("DEBUG: number of entry in root_directory - [%d]\n", a1[12]);
printf("DEBUG: first data sector - [%d]\n", *((unsigned int *)a1 + 7));
printf("DEBUG: max sectors in disk - [%d]\n", *((unsigned int *)a1 + 8));
printf("DEBUG: number of sectors per cluster - [%d]\n", a1[18]);
printf("DEBUG: first cluster of root cluster - [%d]\n", *((unsigned int *)a1 + 10));
printf("DEBUG: maximum cluster number - [%d]\n", *((unsigned int *)a1 + 11));
printf("DEBUG: cluster_size - [%d]\n", *((unsigned int *)a1 + 12));
return printf("DEBUG: log2(cluster_size) - [%d]\n", a1[26]);
}

original_code:
static void fat_sb_print(struct fat_sb * fsb)
{
 printf("DEBUG: sector_size in disk - [%d]\n", fsb->sector_size);
 printf("DEBUG: fat bits (12, 16 32) - [%d]\n",fsb->fat_bits);
 printf("DEBUG: sector number of fsinfo - [%d]\n", fsb->fsinfo_sector);
 printf("DEBUG: fat sector start - [%d]\n", fsb->fat_sector_start);
 printf("DEBUG: fat length - [%d]\n", fsb->fat_length);
 printf("DEBUG: number of fats - [%d]\n", fsb->fats);
 printf("DEBUG: root dir sector start - [%d]\n", fsb->root_dir_sector_start);
 printf("DEBUG: number of entry in root_directory - [%d]\n", fsb->root_dir_entries);
 printf("DEBUG: first data sector - [%d]\n", fsb->data_sector_start);
 printf("DEBUG: max sectors in disk - [%d]\n", fsb->max_sectors);
 printf("DEBUG: number of sectors per cluster - [%d]\n", fsb->sec_per_clus);
 printf("DEBUG: first cluster of root cluster - [%d]\n", fsb->root_cluster);
 printf("DEBUG: maximum cluster number - [%d]\n", fsb->max_cluster);
 printf("DEBUG: cluster_size - [%d]\n", fsb->cluster_size);
 printf("DEBUG: log2(cluster_size) - [%d]\n", fsb->cluster_bits);
}

canonical_original_code:
static void fat_sb_print(struct fat_sb *fsb)
{
 printf("DEBUG: sector_size in disk - [%d]\n", fsb->sector_size);
 printf("DEBUG: fat bits (12, 16 32) - [%d]\n",fsb->fat_bits);
 printf("DEBUG: sector number of fsinfo - [%d]\n", fsb->fsinfo_sector);
 printf("DEBUG: fat sector start - [%d]\n", fsb->fat_sector_start);
 printf("DEBUG: fat length - [%d]\n", fsb->fat_length);
 printf("DEBUG: number of fats - [%d]\n", fsb->fats);
 printf("DEBUG: root dir sector start - [%d]\n", fsb->root_dir_sector_start);
 printf("DEBUG: number of entry in root_directory - [%d]\n", fsb->root_dir_entries);
 printf("DEBUG: first data sector - [%d]\n", fsb->data_sector_start);
 printf("DEBUG: max sectors in disk - [%d]\n", fsb->max_sectors);
 printf("DEBUG: number of sectors per cluster - [%d]\n", fsb->sec_per_clus);
 printf("DEBUG: first cluster of root cluster - [%d]\n", fsb->root_cluster);
 printf("DEBUG: maximum cluster number - [%d]\n", fsb->max_cluster);
 printf("DEBUG: cluster_size - [%d]\n", fsb->cluster_size);
 printf("DEBUG: log2(cluster_size) - [%d]\n", fsb->cluster_bits);
}

variable_types: struct fat_sb { unsigned short sector_size; unsigned char fat_bits; unsigned int fsinfo_sector; unsigned short fat_sector_start; unsigned int fat_length; unsigned char fats; unsigned int root_dir_sector_start; unsigned short root_dir_entries; unsigned int data_sector_start; unsigned int max_sectors; unsigned short sec_per_clus; unsigned int root_cluster; unsigned int max_cluster; unsigned int cluster_size; unsigned short cluster_bits; int dir_per_block; int dir_per_block_bits; } *


decompiled_code:
__int64 __fastcall get_unaligned_le16(unsigned __int16 *a1)
{
return _le16_to_cpup_isra_0(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int16 *a1)
{
return func3(*a1);
}

original_code:
static inline u16 get_unaligned_le16(const void *p)
{
 return __le16_to_cpup((__le16 *)p);
}

canonical_original_code:
static inline unsigned short get_unaligned_le16(void *p)
{
 return __le16_to_cpup((unsigned short *)p);
}

variable_types:
p
void *

return_type: unsigned short


decompiled_code:
__int64 __fastcall calc_max_sectors(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = (unsigned __int16)get_unaligned_le16((unsigned __int16 *)(a1 + 19));
if ( !(_WORD)result )
result = *(unsigned int *)(v2 + 32);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = (unsigned __int16)func4((unsigned __int16 *)(a1 + 19));
if ( !(_WORD)result )
result = *(unsigned int *)(v2 + 32);
return result;
}

original_code:
static u32 calc_max_sectors(const struct fat_boot_sector * fbs)
{
 u32 max_sectors = get_unaligned_le16(&fbs->sectors);
 if(max_sectors == 0)
  max_sectors = ((__u32)(__le32)(fbs->total_sect));
 return max_sectors;
}

canonical_original_code:
static unsigned int calc_max_sectors(struct fat_boot_sector *fbs)
{
 unsigned int max_sectors = get_unaligned_le16(&fbs->sectors);
 if(max_sectors == 0)
  max_sectors = ((__u32)(__le32)(fbs->total_sect));
 return max_sectors;
}

variable_types: struct fat_boot_sector { unsigned char ignored[3]; unsigned char system_id[8]; unsigned char sector_size[2]; unsigned char sec_per_clus; unsigned short reserved; unsigned char fats; unsigned char dir_entries[2]; unsigned char sectors[2]; unsigned char media; unsigned short fat_length; unsigned short secs_track; unsigned short heads; unsigned int hidden; unsigned int total_sect; union { struct { unsigned char drive_number; unsigned char state; unsigned char signature; unsigned char vol_id[4]; unsigned char vol_label[11]; unsigned char fs_type[8]; } fat16; struct { unsigned int length; unsigned short flags; unsigned char version[2]; unsigned int root_cluster; unsigned short info_sector; unsigned short backup_boot; unsigned short reserved2[6]; unsigned char drive_number; unsigned char state; unsigned char signature; unsigned char vol_id[4]; unsigned char vol_label[11]; unsigned char fs_type[8]; } fat32; }; } *

return_type: unsigned int


original_code:
static int fat_sb_read(struct fat_sb *fsb, const struct fat_boot_sector * fbs)
{
 fsb->sector_size = get_unaligned_le16(fbs->sector_size);
 fsb->fat_bits = 12;
 fsb->fsinfo_sector = 0;
 fsb->fat_sector_start = ((__u16)(__le16)(fbs->reserved));
 fsb->fat_length = ((__u16)(__le16)(fbs->fat_length));
 fsb->fats = fbs->fats;
 fsb->root_dir_sector_start = fsb->fat_sector_start + fbs->fats * fsb->fat_length;
 fsb->root_dir_entries = get_unaligned_le16(&fbs->dir_entries);
 fsb->data_sector_start = fsb->root_dir_sector_start + fsb->root_dir_entries * sizeof(struct msdos_dir_entry)/fsb->sector_size;
 fsb->max_sectors = calc_max_sectors(fbs);
 fsb->sec_per_clus = fbs->sec_per_clus;
 fsb->root_cluster = 0;
 fsb->max_cluster = calc_max_cluster(fsb);
 fsb->cluster_size = (fsb->sec_per_clus) * (fsb->sector_size);
 fsb->cluster_bits = ffs(fsb->cluster_size) - 1;
    return 0;
}

canonical_original_code:
static int fat_sb_read(struct fat_sb *fsb, struct fat_boot_sector *fbs)
{
 fsb->sector_size = get_unaligned_le16(fbs->sector_size);
 fsb->fat_bits = 12;
 fsb->fsinfo_sector = 0;
 fsb->fat_sector_start = ((__u16)(__le16)(fbs->reserved));
 fsb->fat_length = ((__u16)(__le16)(fbs->fat_length));
 fsb->fats = fbs->fats;
 fsb->root_dir_sector_start = fsb->fat_sector_start + fbs->fats * fsb->fat_length;
 fsb->root_dir_entries = get_unaligned_le16(&fbs->dir_entries);
 fsb->data_sector_start = fsb->root_dir_sector_start + fsb->root_dir_entries * sizeof(struct msdos_dir_entry)/fsb->sector_size;
 fsb->max_sectors = calc_max_sectors(fbs);
 fsb->sec_per_clus = fbs->sec_per_clus;
 fsb->root_cluster = 0;
 fsb->max_cluster = calc_max_cluster(fsb);
 fsb->cluster_size = (fsb->sec_per_clus) * (fsb->sector_size);
 fsb->cluster_bits = ffs(fsb->cluster_size) - 1;
    return 0;
}

user_defined_types: struct msdos_dir_entry { unsigned char name[11]; unsigned char attr; unsigned char lcase; unsigned char ctime_cs; unsigned short ctime; unsigned short cdate; unsigned short adate; unsigned short starthi; unsigned short time; unsigned int size; }

user_defined_types: struct fat_sb { unsigned short sector_size; unsigned char fat_bits; unsigned int fsinfo_sector; unsigned short fat_sector_start; unsigned int fat_length; unsigned char fats; unsigned int root_dir_sector_start; unsigned short root_dir_entries; unsigned int data_sector_start; unsigned int max_sectors; unsigned short sec_per_clus; unsigned int root_cluster; unsigned int max_cluster; unsigned int cluster_size; unsigned short cluster_bits; int dir_per_block; int dir_per_block_bits; }

user_defined_types: struct fat_boot_sector { unsigned char ignored[3]; unsigned char system_id[8]; unsigned char sector_size[2]; unsigned char sec_per_clus; unsigned short reserved; unsigned char fats; unsigned char dir_entries[2]; unsigned char sectors[2]; unsigned char media; unsigned short fat_length; unsigned short secs_track; unsigned short heads; unsigned int hidden; unsigned int total_sect; union { struct { unsigned char drive_number; unsigned char state; unsigned char signature; unsigned char vol_id[4]; unsigned char vol_label[11]; unsigned char fs_type[8]; } fat16; struct { unsigned int length; unsigned short flags; unsigned char version[2]; unsigned int root_cluster; unsigned short info_sector; unsigned short backup_boot; unsigned short reserved2[6]; unsigned char drive_number; unsigned char state; unsigned char signature; unsigned char vol_id[4]; unsigned char vol_label[11]; unsigned char fs_type[8]; } fat32; }; }


decompiled_code:
// attributes: thunk
__int64 fat_sb_free()
{
return kfree();
}

canonical_decompiled_code:
__int64 func7()
{
return kfree();
}

original_code:
void fat_sb_free(struct fat_sb * fsb)
{
    kfree(fsb);
}

canonical_original_code:
void fat_sb_free(struct fat_sb *fsb)
{
    kfree(fsb);
}


decompiled_code:
unsigned __int16 *__fastcall fat_sb_create(unsigned __int8 *a1)
{
unsigned __int16 *v1; // rax
unsigned __int16 *v2; // r12
unsigned __int16 *result; // rax

if ( !a1 )
return 0LL;
printf("MRB info %x,%x,%x\n", *a1, a1[1], a1[2]);
v1 = (unsigned __int16 *)kzalloc(64LL, 3264LL);
v2 = v1;
if ( v1 && !(unsigned int)fat_sb_read(v1, (__int64)a1) )
{
fat_sb_print(v2);
result = v2;
}
else
{
fat_sb_free(v2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
unsigned __int16 *__fastcall func8(unsigned __int8 *a1)
{
unsigned __int16 *v1; // rax
unsigned __int16 *v2; // r12
unsigned __int16 *result; // rax

if ( !a1 )
return 0LL;
printf("MRB info %x,%x,%x\n", *a1, a1[1], a1[2]);
v1 = (unsigned __int16 *)kzalloc(64LL, 3264LL);
v2 = v1;
if ( v1 && !(unsigned int)func6(v1, (__int64)a1) )
{
func2(v2);
result = v2;
}
else
{
func7(v2);
result = 0LL;
}
return result;
}

original_code:
struct fat_sb * fat_sb_create(u8 * data, unsigned long size)
{
    struct fat_sb * fsb;
    if(!data){
        return ((void *)0);
    }
    printf("MRB info %x,%x,%x\n", data[0], data[1], data[2]);
    fsb = (struct fat_sb*)kzalloc(sizeof(struct fat_sb), (((gfp_t)(0x400u|0x800u)) | ((gfp_t)0x40u) | ((gfp_t)0x80u)));
    if(fsb && !fat_sb_read(fsb, (const struct fat_boot_sector*)data)){
        fat_sb_print(fsb);
        return fsb;
    }
    fat_sb_free(fsb);
    return ((void *)0);
}

canonical_original_code:
struct fat_sb *fat_sb_create(unsigned char *data, unsigned long size)
{
    struct fat_sb *fsb;
    if(!data){
        return ((void *)0);
    }
    printf("MRB info %x,%x,%x\n", data[0], data[1], data[2]);
    fsb = (struct fat_sb *)kzalloc(sizeof(struct fat_sb), (((gfp_t)(0x400u|0x800u)) | ((unsigned int)0x40u) | ((unsigned int)0x80u)));
    if(fsb && !fat_sb_read(fsb, (struct fat_boot_sector *)data)){
        fat_sb_print(fsb);
        return fsb;
    }
    fat_sb_free(fsb);
    return ((void *)0);
}

variable_types:
data
unsigned char *

variable_types:
size
unsigned long

return_type: struct fat_sb { unsigned short sector_size; unsigned char fat_bits; unsigned int fsinfo_sector; unsigned short fat_sector_start; unsigned int fat_length; unsigned char fats; unsigned int root_dir_sector_start; unsigned short root_dir_entries; unsigned int data_sector_start; unsigned int max_sectors; unsigned short sec_per_clus; unsigned int root_cluster; unsigned int max_cluster; unsigned int cluster_size; unsigned short cluster_bits; int dir_per_block; int dir_per_block_bits; } *


kzalloc

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

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

original_code:
void * kzalloc(size_t size, gfp_t flags){
 return calloc(size, 1);
}

canonical_original_code:
void *kzalloc(unsigned long size, unsigned int flags){
 return calloc(size, 1);
}

variable_types:
flags
unsigned int

return_type: void *


kfree

decompiled_code:
// attributes: thunk
void kfree(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func1(void *ptr)
{
free(ptr);
}

original_code:
void kfree(void *p){
 free(p);
}

canonical_original_code:
void kfree(void *p){
 free(p);
}


decompiled_code:
bool __fastcall is_power_of_2(__int64 a1)
{
return (a1 & (a1 - 1)) == 0;
}

canonical_decompiled_code:
bool __fastcall func0(__int64 a1)
{
return (a1 & (a1 - 1)) == 0;
}

original_code:
static inline bool is_power_of_2(unsigned long n)
{
    return (n != 0 && ((n & (n - 1)) == 0));
}

canonical_original_code:
static inline _Bool is_power_of_2(unsigned long n)
{
    return (n != 0 && ((n & (n - 1)) == 0));
}

variable_types:
n
unsigned long


decompiled_code:
__int64 __fastcall blksize_bits(unsigned int a1)
{
__int64 result; // rax

LODWORD(result) = 8;
do
{
a1 >>= 1;
result = (unsigned int)(result + 1);
}
while ( a1 > 0x100 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
__int64 result; // rax

LODWORD(result) = 8;
do
{
a1 >>= 1;
result = (unsigned int)(result + 1);
}
while ( a1 > 0x100 );
return result;
}

original_code:
static unsigned int blksize_bits(unsigned int size)
{
    unsigned int bits = 8;
    do {
        bits++;
        size >>= 1;
    } while (size > 256);
    return bits;
}

variable_types:
size
unsigned int

variable_types:
bits
unsigned int


decompiled_code:
// attributes: thunk
__int64 test_fat_device()
{
return test_device();
}

canonical_decompiled_code:
__int64 func1()
{
return func0();
}

original_code:
void test_fat_device(void){
    test_device();
}

canonical_original_code:
void test_fat_device(){
    test_device();
}


decompiled_code:
bool __fastcall atomic_dec_and_test(_DWORD *a1)
{
return (*a1)-- == 1;
}

canonical_decompiled_code:
bool __fastcall func1(_DWORD *a1)
{
return (*a1)-- == 1;
}

original_code:
static inline bool atomic_dec_and_test(atomic_t *v)
{
    return (--v->counter) == 0 ? 1 : 0;
}

canonical_original_code:
static inline _Bool atomic_dec_and_test(struct atomic_t *v)
{
    return (--v->counter) == 0 ? 1 : 0;
}

variable_types:
v
struct atomic_t { int counter; } *


decompiled_code:
__int64 __fastcall atomic_inc_not_zero(_DWORD *a1)
{
unsigned int v1; // er8

v1 = 0;
if ( *a1 )
{
v1 = 1;
++*a1;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1)
{
unsigned int v1; // er8

v1 = 0;
if ( *a1 )
{
v1 = 1;
++*a1;
}
return v1;
}

original_code:
static inline bool atomic_inc_not_zero(atomic_t *v)
{
    if(v->counter != 0){
        ++v->counter;
        return 1;
    }
    return 0;
}

canonical_original_code:
static inline _Bool atomic_inc_not_zero(struct atomic_t *v)
{
    if(v->counter != 0){
        ++v->counter;
        return 1;
    }
    return 0;
}


decompiled_code:
bool __fastcall IS_ERR(__int64 a1)
{
return (unsigned __int64)(a1 + 4095) <= 0xFFF;
}

canonical_decompiled_code:
bool __fastcall func3(__int64 a1)
{
return (unsigned __int64)(a1 + 4095) <= 0xFFF;
}

original_code:
static inline bool IS_ERR(const void * ptr)
{
    return (((long)(void *)(ptr)) <= 0 && ((long)(void *)(ptr)) >= -4095);
}

canonical_original_code:
static inline _Bool IS_ERR(void *ptr)
{
    return (((long)(void *)(ptr)) <= 0 && ((long)(void *)(ptr)) >= -4095);
}

variable_types:
ptr
void *


decompiled_code:
__int64 __fastcall PTR_ERR(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return a1;
}

original_code:
static inline long PTR_ERR(const void * ptr)
{
    return (long)ptr;
}

canonical_original_code:
static inline long PTR_ERR(void *ptr)
{
    return (long)ptr;
}

return_type: long


decompiled_code:
__int64 __fastcall ERR_PTR(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return a1;
}

original_code:
static inline void * ERR_PTR(long error)
{
    return (void *) error;
}

canonical_original_code:
static inline void *ERR_PTR(long error)
{
    return (void *) error;
}

variable_types: error long


decompiled_code:
__int64 fat_make_fs()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0xFFFFFFFFLL;
}

original_code:
int fat_make_fs(fat_dev_t * pdev){
    return -1;
}

canonical_original_code:
int fat_make_fs(struct fat_dev_s *pdev){
    return -1;
}

variable_types:
pdev
struct fat_dev_s { int fd; } *


decompiled_code:
__int64 __fastcall list_add(_QWORD *a1, __int64 *a2)
{
return _list_add(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1, __int64 *a2)
{
return func7(a1, a2);
}

original_code:
static inline void list_add(struct list_head *new, struct list_head *head)
{
 __list_add(new, head, head->next);
}

variable_types: struct list_head { struct list_head *next; } *

variable_types:
head
struct list_head *


canonical_decompiled_code:
bool __fastcall func0(_DWORD *a1)
{
return (*a1)-- == 1;
}


repo: yunfengHe/UnixC

decompiled_code:
ssize_t __fastcall sig_parentsideint(char a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = a1;
return write(sigpipe1, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func7(char a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = a1;
return write(sigpipe1, buf, 1uLL);
}

original_code:
static void sig_parentsideint(int signo){
  char rett = signo;
  write(sigpipe1, &rett,1);
}

variable_types: signo int

variable_types: rett char


decompiled_code:
ssize_t __fastcall sig_usr(char a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = a1;
return write(sigpipe, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func8(char a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = a1;
return write(sigpipe, buf, 1uLL);
}

original_code:
static void sig_usr(int signo){
 char ret = signo;
 write(sigpipe,&ret,1);
}

variable_types: ret char


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int result; // eax
__pid_t v5; // ebp
int v6; // er12
char buf; // [rsp+Fh] [rbp-169h] BYREF
int pipedes[2]; // [rsp+10h] [rbp-168h] BYREF
int v9[2]; // [rsp+18h] [rbp-160h] BYREF
struct sigaction v10; // [rsp+20h] [rbp-158h] BYREF
struct sigaction v11; // [rsp+C0h] [rbp-B8h] BYREF

if ( pipe(pipedes) )
__assert_fail("pipe(pipefd) == 0", "IPCpipe.c", 0x1Fu, "main");
sigpipe = pipedes[1];
if ( pipe(v9) )
__assert_fail("pipe(pipefd1) == 0", "IPCpipe.c", 0x22u, "main");
sigpipe1 = v9[1];
sigemptyset(&v10.sa_mask);
v10.sa_flags = 0;
v10.sa_handler = (__sighandler_t)sig_usr;
if ( sigaction(10, &v10, 0LL) )
__assert_fail("(sigaction(SIGUSR1, &sig,NULL))== 0", "IPCpipe.c", 0x2Du, "main");
if ( sigaction(12, &v10, 0LL) )
__assert_fail("(sigaction(SIGUSR2, &sig,NULL)) == 0", "IPCpipe.c", 0x2Eu, "main");
sigemptyset(&v11.sa_mask);
v11.sa_flags = 0;
v11.sa_handler = (__sighandler_t)sig_parentsideint;
if ( sigaction(2, &v11, 0LL) )
__assert_fail("(sigaction(SIGINT, &sig1,NULL))== 0", "IPCpipe.c", 0x35u, "main");
v3 = fork();
if ( v3 > 0 )
{
close(pipedes[0]);
close(pipedes[1]);
while ( (unsigned int)read(v9[0], &buf, 1uLL) != 1 )
;
v6 = 10;
do
{
kill(v3, 10);
while ( (unsigned int)read(v9[0], &buf, 1uLL) != 1 )
;
kill(v3, 12);
while ( (unsigned int)read(v9[0], &buf, 1uLL) != 1 )
;
--v6;
}
while ( v6 );
result = kill(v3, 9);
}
else
{
result = 0;
if ( !v3 )
{
close(v9[1]);
close(v9[0]);
v5 = getppid();
while ( 1 )
{
kill(v5, 2);
while ( (unsigned int)read(pipedes[0], &buf, 1uLL) != 1 )
;
if ( buf == 10 )
{
puts("received SIGUSR1");
}
else if ( buf == 12 )
{
puts("received SIGUSR2");
}
}
}
}
return result;
}

original_code:
int main(void){
 int pipefd[2];
 int pipefd1[2];
 ((void) sizeof ((pipe(pipefd) == 0) ? 1 : 0), __extension__ ({ if (pipe(pipefd) == 0) ; else __assert_fail ("pipe(pipefd) == 0", "IPCpipe.c", 31, __extension__ __PRETTY_FUNCTION__); }));
 sigpipe = pipefd[1];
 ((void) sizeof ((pipe(pipefd1) == 0) ? 1 : 0), __extension__ ({ if (pipe(pipefd1) == 0) ; else __assert_fail ("pipe(pipefd1) == 0", "IPCpipe.c", 34, __extension__ __PRETTY_FUNCTION__); }));
 sigpipe1 = pipefd1[1];
 pid_t tpid;
 struct sigaction sig;
 sigemptyset(&sig.sa_mask);
 sig.sa_flags = 0;
 sig.__sigaction_handler.sa_handler = sig_usr;
 ((void) sizeof (((sigaction(10, &sig,((void *)0)))== 0) ? 1 : 0), __extension__ ({ if ((sigaction(10, &sig,((void *)0)))== 0) ; else __assert_fail ("(sigaction(SIGUSR1, &sig,NULL))== 0", "IPCpipe.c", 45, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof (((sigaction(12, &sig,((void *)0))) == 0) ? 1 : 0), __extension__ ({ if ((sigaction(12, &sig,((void *)0))) == 0) ; else __assert_fail ("(sigaction(SIGUSR2, &sig,NULL)) == 0", "IPCpipe.c", 46, __extension__ __PRETTY_FUNCTION__); }));
 struct sigaction sig1;
 sigemptyset(&sig1.sa_mask);
 sig1.sa_flags = 0;
 sig1.__sigaction_handler.sa_handler = sig_parentsideint;
 ((void) sizeof (((sigaction(2, &sig1,((void *)0)))== 0) ? 1 : 0), __extension__ ({ if ((sigaction(2, &sig1,((void *)0)))== 0) ; else __assert_fail ("(sigaction(SIGINT, &sig1,NULL))== 0", "IPCpipe.c", 53, __extension__ __PRETTY_FUNCTION__); }));
 tpid = fork();
 if (tpid>0){
  close(pipefd[0]);
  close(pipefd[1]);
  int i=0;
  for(;;){
   char mysignal;
   int res = read(pipefd1[0],&mysignal,1);
   if (res == 1)
    break;
   else
    continue;
  }
  for (i=0; i<10;i++){
   kill(tpid, 10);
   for(;;){
    char mysignal;
    int res = read(pipefd1[0],&mysignal,1);
    if (res == 1)
     break;
    else
     continue;
   }
   kill(tpid,12);
   for(;;){
    char mysignal;
    int res = read(pipefd1[0],&mysignal,1);
    if (res == 1)
     break;
    else
     continue;
   }
  }
  kill(tpid,9);
  return;
 }
 if(tpid == 0){
  close(pipefd1[1]);
  close(pipefd1[0]);
  pid_t ppid = getppid();
  for(;;){
   kill(ppid,2);
   for(;;){
    char mysignal;
    int res = read(pipefd[0],&mysignal,1);
    if (res == 1){
     if (mysignal == 10)
            printf("received SIGUSR1\n");
            else if (mysignal == 12)
      printf("received SIGUSR2\n");
     break;
     }
    else
     continue;
   }
  }
 }
 return 0;
}

canonical_original_code:
int main(){
 int pipefd[2];
 int pipefd1[2];
 ((void) sizeof ((pipe(pipefd) == 0) ? 1 : 0), __extension__ ({ if (pipe(pipefd) == 0) ; else __assert_fail ("pipe(pipefd) == 0", "IPCpipe.c", 31, __extension__ __PRETTY_FUNCTION__); }));
 sigpipe = pipefd[1];
 ((void) sizeof ((pipe(pipefd1) == 0) ? 1 : 0), __extension__ ({ if (pipe(pipefd1) == 0) ; else __assert_fail ("pipe(pipefd1) == 0", "IPCpipe.c", 34, __extension__ __PRETTY_FUNCTION__); }));
 sigpipe1 = pipefd1[1];
 int tpid;
 struct sigaction sig;
 sigemptyset(&sig.sa_mask);
 sig.sa_flags = 0;
 sig.__sigaction_handler.sa_handler = sig_usr;
 ((void) sizeof (((sigaction(10, &sig,((void *)0)))== 0) ? 1 : 0), __extension__ ({ if ((sigaction(10, &sig,((void *)0)))== 0) ; else __assert_fail ("(sigaction(SIGUSR1, &sig,NULL))== 0", "IPCpipe.c", 45, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof (((sigaction(12, &sig,((void *)0))) == 0) ? 1 : 0), __extension__ ({ if ((sigaction(12, &sig,((void *)0))) == 0) ; else __assert_fail ("(sigaction(SIGUSR2, &sig,NULL)) == 0", "IPCpipe.c", 46, __extension__ __PRETTY_FUNCTION__); }));
 struct sigaction sig1;
 sigemptyset(&sig1.sa_mask);
 sig1.sa_flags = 0;
 sig1.__sigaction_handler.sa_handler = sig_parentsideint;
 ((void) sizeof (((sigaction(2, &sig1,((void *)0)))== 0) ? 1 : 0), __extension__ ({ if ((sigaction(2, &sig1,((void *)0)))== 0) ; else __assert_fail ("(sigaction(SIGINT, &sig1,NULL))== 0", "IPCpipe.c", 53, __extension__ __PRETTY_FUNCTION__); }));
 tpid = fork();
 if (tpid>0){
  close(pipefd[0]);
  close(pipefd[1]);
  int i = 0;
  for(;;){
   char mysignal;
   int res = read(pipefd1[0],&mysignal,1);
   if (res == 1)
    break;
   else
    continue;
  }
  for (i=0; i<10;i++){
   kill(tpid, 10);
   for(;;){
    char mysignal;
    int res = read(pipefd1[0],&mysignal,1);
    if (res == 1)
     break;
    else
     continue;
   }
   kill(tpid,12);
   for(;;){
    char mysignal;
    int res = read(pipefd1[0],&mysignal,1);
    if (res == 1)
     break;
    else
     continue;
   }
  }
  kill(tpid,9);
  return;
 }
 if(tpid == 0){
  close(pipefd1[1]);
  close(pipefd1[0]);
  int ppid = getppid();
  for(;;){
   kill(ppid,2);
   for(;;){
    char mysignal;
    int res = read(pipefd[0],&mysignal,1);
    if (res == 1){
     if (mysignal == 10)
            printf("received SIGUSR1\n");
            else if (mysignal == 12)
      printf("received SIGUSR2\n");
     break;
     }
    else
     continue;
   }
  }
 }
 return 0;
}

variable_types:
pipefd
int [2]

variable_types: tpid int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: i int

variable_types: mysignal char

variable_types: res int

variable_types: ppid int

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
void __fastcall sig_int(int a1)
{
signal(2, sig_int);
signal(10, sig_int);
if ( a1 == 2 )
{
puts("caught SIGINT");
}
else if ( a1 == 10 )
{
puts("caught SIGUSR1");
}
}

canonical_decompiled_code:
void __fastcall func7(int a1)
{
signal(2, sig_int);
signal(10, sig_int);
if ( a1 == 2 )
{
puts("caught SIGINT");
}
else if ( a1 == 10 )
{
puts("caught SIGUSR1");
}
}

original_code:
static void sig_int(int signo){
 signal(2,sig_int);
 signal(10,sig_int);
 if(signo ==2)
  printf("caught SIGINT\n");
 if(signo==10)
  printf("caught SIGUSR1\n");
 return;
}


CloseWalkDir

repo: yunus-idrisov/image-viewer

decompiled_code:
__int64 CloseWalkDir()
{
unsigned int v0; // er12
__int64 result; // rax
int *v2; // rax
char *v3; // rax
void *thread_return; // [rsp+8h] [rbp-10h] BYREF

v0 = mode;
if ( mode == 1 )
{
pthread_join(thread_id, &thread_return);
DeleteStrList(&imageNames);
if ( !closedir(dir) )
{
FreeImage_Unload(pPrevFIBITMAP);
pPrevFIBITMAP = 0LL;
FreeImage_Unload(pNextFIBITMAP);
result = v0;
pNextFIBITMAP = 0LL;
return result;
}
}
else
{
DeleteStrList(&imageNames);
if ( !closedir(dir) )
return 1LL;
}
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "closedir(...) error: %s.\n", v3);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // er12
__int64 result; // rax
int *v2; // rax
char *v3; // rax
void *thread_return; // [rsp+8h] [rbp-10h] BYREF

v0 = mode;
if ( mode == 1 )
{
pthread_join(thread_id, &thread_return);
DeleteStrList(&imageNames);
if ( !closedir(dir) )
{
FreeImage_Unload(pPrevFIBITMAP);
pPrevFIBITMAP = 0LL;
FreeImage_Unload(pNextFIBITMAP);
result = v0;
pNextFIBITMAP = 0LL;
return result;
}
}
else
{
DeleteStrList(&imageNames);
if ( !closedir(dir) )
return 1LL;
}
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "closedir(...) error: %s.\n", v3);
return 0xFFFFFFFFLL;
}

original_code:
int CloseWalkDir(){
 if( mode == 1 ){
  void* status;
  pthread_join( thread_id, &status );
  DeleteStrList(&imageNames);
  if( closedir(dir) != 0 ){
   fprintf(stderr, "closedir(...) error: %s.\n", strerror((*__errno_location ())));
   return -1;
  }
  FreeImage_Unload( pPrevFIBITMAP );
  pPrevFIBITMAP = 0;
  FreeImage_Unload( pNextFIBITMAP );
  pNextFIBITMAP = 0;
 }
 else{
  DeleteStrList(&imageNames);
  if( closedir(dir) != 0 ){
   fprintf(stderr, "closedir(...) error: %s.\n", strerror((*__errno_location ())));
   return -1;
  }
 }
 return 1;
}

canonical_original_code:
int CloseWalkDir(){
 if( mode == 1 ){
  void *status;
  pthread_join( thread_id, &status );
  DeleteStrList(&imageNames);
  if( closedir(dir) != 0 ){
   fprintf(stderr, "closedir(...) error: %s.\n", strerror((*__errno_location ())));
   return -1;
  }
  FreeImage_Unload( pPrevFIBITMAP );
  pPrevFIBITMAP = 0;
  FreeImage_Unload( pNextFIBITMAP );
  pNextFIBITMAP = 0;
 }
 else{
  DeleteStrList(&imageNames);
  if( closedir(dir) != 0 ){
   fprintf(stderr, "closedir(...) error: %s.\n", strerror((*__errno_location ())));
   return -1;
  }
 }
 return 1;
}

variable_types:
status
void *


GetNextNodeCycle

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 8);
if ( !result )
result = *(_QWORD *)imageNames;
return result;
}

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 8);
if ( !result )
result = *(_QWORD *)imageNames;
return result;
}

original_code:
ListNode* GetNextNodeCycle(ListNode* curImageNode){
 if( curImageNode != 0 ){
  curImageNode = curImageNode->next;
  if( curImageNode == 0 )
   curImageNode = imageNames->head;
  return curImageNode;
 }else
  return 0;
}

canonical_original_code:
struct ListNode *GetNextNodeCycle(struct ListNode *curImageNode){
 if( curImageNode != 0 ){
  curImageNode = curImageNode->next;
  if( curImageNode == 0 )
   curImageNode = imageNames->head;
  return curImageNode;
 }else
  return 0;
}

variable_types:
curImageNode
struct ListNode *

return_type: struct ListNode { char *str; struct ListNode *next; struct ListNode *prev; } *


GetPrevNodeCycle

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 16);
if ( !result )
result = *(_QWORD *)(imageNames + 8);
return result;
}

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 16);
if ( !result )
result = *(_QWORD *)(imageNames + 8);
return result;
}

original_code:
ListNode* GetPrevNodeCycle(ListNode* curImageNode){
 if( curImageNode != 0 ){
  curImageNode = curImageNode->prev;
  if( curImageNode == 0 )
   curImageNode = imageNames->tail;
  return curImageNode;
 }else
  return 0;
}

canonical_original_code:
struct ListNode *GetPrevNodeCycle(struct ListNode *curImageNode){
 if( curImageNode != 0 ){
  curImageNode = curImageNode->prev;
  if( curImageNode == 0 )
   curImageNode = imageNames->tail;
  return curImageNode;
 }else
  return 0;
}

user_defined_types: struct ListNode { char *str; struct ListNode *next; struct ListNode *prev; }


OpenWalkDir

decompiled_code:
__int64 __fastcall OpenWalkDir(char *src)
{
DIR *v1; // rbp
int *v2; // rax
int *v3; // rbx
size_t v4; // rax
struct dirent *v5; // rax
unsigned int v6; // er12
__int64 v7; // rdi
char *v9; // rbp
char *v10; // rax
char *v11; // rdx
const char *v12; // rsi
char *v13; // rcx
char *v14; // rax
const char **v15; // rax
__int64 v16; // rax
__int64 v17; // rdi
const char *v18; // rsi

InitStrList(&imageNames);
dir = opendir(src);
v1 = dir;
v2 = _errno_location();
v3 = v2;
if ( !v1 )
{
v14 = strerror(*v2);
v11 = src;
v12 = "Error in open path \"%s\": %s\n";
v13 = v14;
goto LABEL_16;
}
strcpy(imagePath, src);
v4 = strlen(imagePath);
if ( *((_BYTE *)&thread_id + v4 + 7) != 47 )
*(_WORD *)((char *)&thread_id + ++v4 + 7) = 47;
imagePathLen = v4;
*v3 = 0;
while ( 1 )
{
v5 = readdir(v1);
if ( !v5 )
break;
if ( v5->d_type == 8 )
{
v9 = v5->d_name;
if ( (unsigned int)IsSupTexture(v5->d_name) )
AddStringToStrList(imageNames, v9);
}
v1 = dir;
}
if ( *v3 )
{
v10 = strerror(*v3);
v11 = src;
v12 = "Read path \"%s\" error: %s\n";
v13 = v10;
LABEL_16:
fprintf(stderr, v12, v11, v13);
DeleteStrList(&imageNames);
return 0xFFFFFFFFLL;
}
v6 = 1;
SortStrList(imageNames);
v7 = *(_QWORD *)imageNames;
curImagePointer = v7;
if ( v7 )
{
v6 = mode;
if ( mode == 1 )
{
v15 = (const char **)GetPrevNodeCycle(v7);
strcat(imagePath, *v15);
v16 = GetFIBITMAP(imagePath);
v17 = curImagePointer;
pPrevFIBITMAP = v16;
imagePath[imagePathLen] = 0;
v18 = *(const char **)GetNextNodeCycle(v17);
strcat(imagePath, v18);
pNextFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
}
else
{
v6 = 1;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *src)
{
DIR *v1; // rbp
int *v2; // rax
int *v3; // rbx
size_t v4; // rax
struct dirent *v5; // rax
unsigned int v6; // er12
__int64 v7; // rdi
char *v9; // rbp
char *v10; // rax
char *v11; // rdx
const char *v12; // rsi
char *v13; // rcx
char *v14; // rax
const char **v15; // rax
__int64 v16; // rax
__int64 v17; // rdi
const char *v18; // rsi

InitStrList(&imageNames);
dir = opendir(src);
v1 = dir;
v2 = _errno_location();
v3 = v2;
if ( !v1 )
{
v14 = strerror(*v2);
v11 = src;
v12 = "Error in open path \"%s\": %s\n";
v13 = v14;
goto LABEL_16;
}
strcpy(imagePath, src);
v4 = strlen(imagePath);
if ( *((_BYTE *)&thread_id + v4 + 7) != 47 )
*(_WORD *)((char *)&thread_id + ++v4 + 7) = 47;
imagePathLen = v4;
*v3 = 0;
while ( 1 )
{
v5 = readdir(v1);
if ( !v5 )
break;
if ( v5->d_type == 8 )
{
v9 = v5->d_name;
if ( (unsigned int)IsSupTexture(v5->d_name) )
AddStringToStrList(imageNames, v9);
}
v1 = dir;
}
if ( *v3 )
{
v10 = strerror(*v3);
v11 = src;
v12 = "Read path \"%s\" error: %s\n";
v13 = v10;
LABEL_16:
fprintf(stderr, v12, v11, v13);
DeleteStrList(&imageNames);
return 0xFFFFFFFFLL;
}
v6 = 1;
SortStrList(imageNames);
v7 = *(_QWORD *)imageNames;
curImagePointer = v7;
if ( v7 )
{
v6 = mode;
if ( mode == 1 )
{
v15 = (const char **)func3(v7);
strcat(imagePath, *v15);
v16 = GetFIBITMAP(imagePath);
v17 = curImagePointer;
pPrevFIBITMAP = v16;
imagePath[imagePathLen] = 0;
v18 = *(const char **)func1(v17);
strcat(imagePath, v18);
pNextFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
}
else
{
v6 = 1;
}
}
return v6;
}

original_code:
int OpenWalkDir(const char* path){
 InitStrList(&imageNames);
 dir = opendir(path);
 if( dir == ((void *)0) ){
  fprintf(stderr, "Error in open path \"%s\": %s\n", path, strerror((*__errno_location ())));
  DeleteStrList(&imageNames);
  return -1;
 }
 strcpy(imagePath, path);
 if( imagePath[strlen(imagePath)-1] != '/' )
  strcat(imagePath, "/");
 imagePathLen = strlen(imagePath);
 struct dirent* de;
 (*__errno_location ()) = 0;
 while( (de = readdir(dir)) != ((void *)0) ){
  if( (de->d_type == DT_REG) && IsSupTexture(de->d_name) )
   AddStringToStrList(imageNames, de->d_name);
 }
 if( (*__errno_location ()) != 0 ){
  fprintf(stderr, "Read path \"%s\" error: %s\n", path, strerror((*__errno_location ())));
  DeleteStrList(&imageNames);
  return -1;
 }
 SortStrList(imageNames);
 curImagePointer = imageNames->head;
 if( curImagePointer != 0 && mode == 1 ){
  ListNode* prevImagePointer = GetPrevNodeCycle( curImagePointer );
  strcat(imagePath, prevImagePointer->str);
  pPrevFIBITMAP = GetFIBITMAP( imagePath );
  imagePath[imagePathLen] = '\0';
  ListNode* nextImagePointer = GetNextNodeCycle( curImagePointer );
  strcat(imagePath, nextImagePointer->str);
  pNextFIBITMAP = GetFIBITMAP( imagePath );
  imagePath[imagePathLen] = '\0';
 }
 return 1;
}

canonical_original_code:
int OpenWalkDir(char *path){
 InitStrList(&imageNames);
 dir = opendir(path);
 if( dir == ((void *)0) ){
  fprintf(stderr, "Error in open path \"%s\": %s\n", path, strerror((*__errno_location ())));
  DeleteStrList(&imageNames);
  return -1;
 }
 strcpy(imagePath, path);
 if( imagePath[strlen(imagePath)-1] != '/' )
  strcat(imagePath, "/");
 imagePathLen = strlen(imagePath);
 struct dirent *de;
 (*__errno_location ()) = 0;
 while( (de = readdir(dir)) != ((void *)0) ){
  if( (de->d_type == DT_REG) && IsSupTexture(de->d_name) )
   AddStringToStrList(imageNames, de->d_name);
 }
 if( (*__errno_location ()) != 0 ){
  fprintf(stderr, "Read path \"%s\" error: %s\n", path, strerror((*__errno_location ())));
  DeleteStrList(&imageNames);
  return -1;
 }
 SortStrList(imageNames);
 curImagePointer = imageNames->head;
 if( curImagePointer != 0 && mode == 1 ){
  struct ListNode *prevImagePointer = GetPrevNodeCycle( curImagePointer );
  strcat(imagePath, prevImagePointer->str);
  pPrevFIBITMAP = GetFIBITMAP( imagePath );
  imagePath[imagePathLen] = '\0';
  struct ListNode *nextImagePointer = GetNextNodeCycle( curImagePointer );
  strcat(imagePath, nextImagePointer->str);
  pNextFIBITMAP = GetFIBITMAP( imagePath );
  imagePath[imagePathLen] = '\0';
 }
 return 1;
}

variable_types:
path
char *

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } *

variable_types: struct ListNode { char *str; struct ListNode *next; struct ListNode *prev; } *


decompiled_code:
void *__fastcall thread_func(void *a1)
{
__int64 v1; // rax
__int64 v2; // rdi
const char **v3; // rax
__int64 v5; // rax
__int64 v6; // rdi
const char **v7; // rax

if ( imageToLoad )
{
if ( imageToLoad == 2 )
{
if ( curImagePointer )
{
FreeImage_Unload(pPrevFIBITMAP);
pPrevFIBITMAP = 0LL;
strcat(imagePath, *(const char **)curImagePointer);
v1 = GetFIBITMAP(imagePath);
v2 = curImagePointer;
pPrevFIBITMAP = v1;
imagePath[imagePathLen] = 0;
curImagePointer = GetNextNodeCycle(v2);
v3 = (const char **)GetNextNodeCycle(curImagePointer);
strcat(imagePath, *v3);
pNextFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
}
}
return 0LL;
}
if ( !curImagePointer )
return 0LL;
FreeImage_Unload(pNextFIBITMAP);
pNextFIBITMAP = 0LL;
strcat(imagePath, *(const char **)curImagePointer);
v5 = GetFIBITMAP(imagePath);
v6 = curImagePointer;
pNextFIBITMAP = v5;
imagePath[imagePathLen] = 0;
curImagePointer = GetPrevNodeCycle(v6);
v7 = (const char **)GetPrevNodeCycle(curImagePointer);
strcat(imagePath, *v7);
pPrevFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func5(void *a1)
{
__int64 v1; // rax
__int64 v2; // rdi
const char **v3; // rax
__int64 v5; // rax
__int64 v6; // rdi
const char **v7; // rax

if ( imageToLoad )
{
if ( imageToLoad == 2 )
{
if ( curImagePointer )
{
FreeImage_Unload(pPrevFIBITMAP);
pPrevFIBITMAP = 0LL;
strcat(imagePath, *(const char **)curImagePointer);
v1 = GetFIBITMAP(imagePath);
v2 = curImagePointer;
pPrevFIBITMAP = v1;
imagePath[imagePathLen] = 0;
curImagePointer = func1(v2);
v3 = (const char **)func1(curImagePointer);
strcat(imagePath, *v3);
pNextFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
}
}
return 0LL;
}
if ( !curImagePointer )
return 0LL;
FreeImage_Unload(pNextFIBITMAP);
pNextFIBITMAP = 0LL;
strcat(imagePath, *(const char **)curImagePointer);
v5 = GetFIBITMAP(imagePath);
v6 = curImagePointer;
pNextFIBITMAP = v5;
imagePath[imagePathLen] = 0;
curImagePointer = func3(v6);
v7 = (const char **)func3(curImagePointer);
strcat(imagePath, *v7);
pPrevFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
return 0LL;
}

original_code:
static void* thread_func(void* arg){
 switch( imageToLoad ){
  case 0:
   if( curImagePointer != 0 ){
    FreeImage_Unload( pNextFIBITMAP );
    pNextFIBITMAP = 0;
    strcat(imagePath, curImagePointer->str);
    pNextFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
    curImagePointer = GetPrevNodeCycle( curImagePointer );
    ListNode* prevImagePointer = GetPrevNodeCycle( curImagePointer );
    strcat(imagePath, prevImagePointer->str);
    pPrevFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
   }
   break;
  case 2:
   if( curImagePointer != 0 ){
    FreeImage_Unload( pPrevFIBITMAP );
    pPrevFIBITMAP = 0;
    strcat(imagePath, curImagePointer->str);
    pPrevFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
    curImagePointer = GetNextNodeCycle( curImagePointer );
    ListNode* nextImagePointer = GetNextNodeCycle( curImagePointer );
    strcat(imagePath, nextImagePointer->str);
    pNextFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
   }
   break;
 }
 return 0;
}

canonical_original_code:
static void *thread_func(void *arg){
 switch( imageToLoad ){
  case 0:
   if( curImagePointer != 0 ){
    FreeImage_Unload( pNextFIBITMAP );
    pNextFIBITMAP = 0;
    strcat(imagePath, curImagePointer->str);
    pNextFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
    curImagePointer = GetPrevNodeCycle( curImagePointer );
    struct ListNode *prevImagePointer = GetPrevNodeCycle( curImagePointer );
    strcat(imagePath, prevImagePointer->str);
    pPrevFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
   }
   break;
  case 2:
   if( curImagePointer != 0 ){
    FreeImage_Unload( pPrevFIBITMAP );
    pPrevFIBITMAP = 0;
    strcat(imagePath, curImagePointer->str);
    pPrevFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
    curImagePointer = GetNextNodeCycle( curImagePointer );
    struct ListNode *nextImagePointer = GetNextNodeCycle( curImagePointer );
    strcat(imagePath, nextImagePointer->str);
    pNextFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
   }
   break;
 }
 return 0;
}

variable_types:
arg
void *


AddStringToStrList

original_code:
void AddStringToStrList(StrList* list, const char* str){
 if( strlen(str) == 0 )
  return;
 ListNode* tmp = malloc(sizeof(ListNode));
 int len = strlen(str);
 tmp->str = malloc(sizeof(char)*(len + 1));
 strcpy(tmp->str, str);
 tmp->next = 0;
 if( (list->head == 0) && (list->tail == 0) ){
  tmp->prev = 0;
  list->head = tmp;
  list->tail = tmp;
 }
 else {
  tmp->prev = list->tail;
  list->tail->next = tmp;
  list->tail = tmp;
 }
}

canonical_original_code:
void AddStringToStrList(struct StrList *list, char *str){
 if( strlen(str) == 0 )
  return;
 struct ListNode *tmp = malloc(sizeof(ListNode));
 int len = strlen(str);
 tmp->str = malloc(sizeof(char)*(len + 1));
 strcpy(tmp->str, str);
 tmp->next = 0;
 if( (list->head == 0) && (list->tail == 0) ){
  tmp->prev = 0;
  list->head = tmp;
  list->tail = tmp;
 }
 else {
  tmp->prev = list->tail;
  list->tail->next = tmp;
  list->tail = tmp;
 }
}

variable_types: struct StrList { struct ListNode { char *str; struct ListNode *next; struct ListNode *prev; } *head; struct ListNode *tail; } *

variable_types:
str
char *

variable_types: len int

user_defined_types: struct StrList { struct ListNode *head; struct ListNode *tail; }


DeleteStrList

original_code:
void DeleteStrList(StrList** list){
 if( (*list) == 0 )
  return;
 ListNode* cur = (*list)->head;
 while( cur != 0 ){
  ListNode* next = cur->next;
  free(cur->str);
  free(cur);
  cur = next;
 }
 free(*list);
 *list = 0;
}

canonical_original_code:
void DeleteStrList(struct StrList **list){
 if( (*list) == 0 )
  return;
 struct ListNode *cur = (*list)->head;
 while( cur != 0 ){
  struct ListNode *next = cur->next;
  free(cur->str);
  free(cur);
  cur = next;
 }
 free(*list);
 *list = 0;
}

variable_types: struct StrList { struct ListNode { char *str; struct ListNode *next; struct ListNode *prev; } *head; struct ListNode *tail; } **


SortStrList

decompiled_code:
void __fastcall SortStrList(__int64 a1)
{
const char **v1; // rax
int v2; // edx

if ( a1 )
{
if ( *(_QWORD *)a1 )
{
v1 = *(const char ***)a1;
v2 = 0;
do
{
v1 = (const char **)v1[1];
++v2;
}
while ( v1 );
}
else
{
v2 = 0;
}
MergeSort_isra_0(*(const char ***)a1, *(_QWORD *)(a1 + 8), v2);
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
const char **v1; // rax
int v2; // edx

if ( a1 )
{
if ( *(_QWORD *)a1 )
{
v1 = *(const char ***)a1;
v2 = 0;
do
{
v1 = (const char **)v1[1];
++v2;
}
while ( v1 );
}
else
{
v2 = 0;
}
func1(*(const char ***)a1, *(_QWORD *)(a1 + 8), v2);
}
}

original_code:
void SortStrList(StrList* list){
 if( list == 0 )
  return;
 int elemCount = 0;
 ListNode* p = list->head;
 while( p != 0 ){
  elemCount++;
  p = p->next;
 }
 MergeSort( list, list->head, list->tail, elemCount );
}

canonical_original_code:
void SortStrList(struct StrList *list){
 if( list == 0 )
  return;
 int elemCount = 0;
 struct ListNode *p = list->head;
 while( p != 0 ){
  elemCount++;
  p = p->next;
 }
 MergeSort( list, list->head, list->tail, elemCount );
}

variable_types: elemCount int


decompiled_code:
__int64 __fastcall setup_socket(__int16 a1, int a2)
{
int v2; // edx
int v3; // eax
unsigned int v4; // er12
int v5; // eax
int *v7; // rbx
char *v8; // rax
int v9; // edi
int *v10; // rbx
char *v11; // rax
int *v12; // rax
char *v13; // rax
const char *v14; // r8
__int64 v15; // rdx
char *v16; // r9
int *v17; // rax
char *v18; // rax
int *v19; // rbx
char *v20; // rax
int optval; // [rsp+0h] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+4h] [rbp-28h] BYREF

addr = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(a1);
v3 = socket(2, a2, v2);
if ( v3 < 0 )
{
v4 = -1;
v10 = _errno_location();
v11 = strerror(*v10);
ast_log(4LL, "mtp3io.c", 65LL, "setup_socket", "Cannot create UDP socket, errno=%d: %s\n", (unsigned int)*v10, v11);
return v4;
}
v4 = v3;
optval = 1;
setsockopt(v3, 1, 2, &optval, 4u);
v5 = fcntl(v4, 3);
if ( v5 < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
v14 = "Could not obtain flags for socket fd: %s.\n";
v15 = 73LL;
v16 = v13;
LABEL_12:
v4 = -1;
ast_log(3LL, "mtp3io.c", v15, "setup_socket", v14, v16);
return v4;
}
BYTE1(v5) |= 8u;
if ( fcntl(v4, 4, (unsigned int)v5) < 0 )
{
v17 = _errno_location();
v18 = strerror(*v17);
v14 = "Could not set socket fd non-blocking: %s.\n";
v15 = 79LL;
v16 = v18;
goto LABEL_12;
}
if ( bind(v4, &addr, 0x10u) < 0 )
{
v19 = _errno_location();
v20 = strerror(*v19);
ast_log(
4LL,
"mtp3io.c",
84LL,
"setup_socket",
"Cannot bind receiver socket, errno=%d: %s\n",
(unsigned int)*v19,
v20);
goto LABEL_9;
}
if ( a2 != 2 && listen(v4, 8) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
ast_log(4LL, "mtp3io.c", 90LL, "setup_socket", "Cannot listen on socket, errno=%d: %s\n", (unsigned int)*v7, v8);
LABEL_9:
v9 = v4;
v4 = -1;
close(v9);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1, int a2)
{
int v2; // edx
int v3; // eax
unsigned int v4; // er12
int v5; // eax
int *v7; // rbx
char *v8; // rax
int v9; // edi
int *v10; // rbx
char *v11; // rax
int *v12; // rax
char *v13; // rax
const char *v14; // r8
__int64 v15; // rdx
char *v16; // r9
int *v17; // rax
char *v18; // rax
int *v19; // rbx
char *v20; // rax
int optval; // [rsp+0h] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+4h] [rbp-28h] BYREF

addr = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(a1);
v3 = socket(2, a2, v2);
if ( v3 < 0 )
{
v4 = -1;
v10 = _errno_location();
v11 = strerror(*v10);
ast_log(4LL, "mtp3io.c", 65LL, "setup_socket", "Cannot create UDP socket, errno=%d: %s\n", (unsigned int)*v10, v11);
return v4;
}
v4 = v3;
optval = 1;
setsockopt(v3, 1, 2, &optval, 4u);
v5 = fcntl(v4, 3);
if ( v5 < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
v14 = "Could not obtain flags for socket fd: %s.\n";
v15 = 73LL;
v16 = v13;
LABEL_12:
v4 = -1;
ast_log(3LL, "mtp3io.c", v15, "setup_socket", v14, v16);
return v4;
}
BYTE1(v5) |= 8u;
if ( fcntl(v4, 4, (unsigned int)v5) < 0 )
{
v17 = _errno_location();
v18 = strerror(*v17);
v14 = "Could not set socket fd non-blocking: %s.\n";
v15 = 79LL;
v16 = v18;
goto LABEL_12;
}
if ( bind(v4, &addr, 0x10u) < 0 )
{
v19 = _errno_location();
v20 = strerror(*v19);
ast_log(
4LL,
"mtp3io.c",
84LL,
"setup_socket",
"Cannot bind receiver socket, errno=%d: %s\n",
(unsigned int)*v19,
v20);
goto LABEL_9;
}
if ( a2 != 2 && listen(v4, 8) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
ast_log(4LL, "mtp3io.c", 90LL, "setup_socket", "Cannot listen on socket, errno=%d: %s\n", (unsigned int)*v7, v8);
LABEL_9:
v9 = v4;
v4 = -1;
close(v9);
}
return v4;
}

original_code:
static int setup_socket(int localport, int sockettype, int ipproto)
{
  struct sockaddr_in sock;
  in_addr_t addr = ((in_addr_t) 0x00000000);
  int parm, res, flags;
  int s;
  memset(&sock, 0, sizeof(struct sockaddr_in));
  sock.sin_family = 2;
  sock.sin_port = htons(localport);
  memcpy(&sock.sin_addr, &addr, sizeof(addr));
  s = socket(2, sockettype, ipproto);
  if (s < 0) {
    ast_log(4, "mtp3io.c", 65, __PRETTY_FUNCTION__, "Cannot create UDP socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    return -1;
  }
  parm = 1;
  setsockopt(s, 1, 2, &parm, sizeof(int));
  res = fcntl(s, 3);
  if(res < 0) {
    ast_log(3, "mtp3io.c", 73, __PRETTY_FUNCTION__, "Could not obtain flags for socket fd: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  flags = res | 04000;
  res = fcntl(s, 4, flags);
  if(res < 0) {
    ast_log(3, "mtp3io.c", 79, __PRETTY_FUNCTION__, "Could not set socket fd non-blocking: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  if (bind(s, &sock, sizeof(sock)) < 0) {
    ast_log(4, "mtp3io.c", 84, __PRETTY_FUNCTION__, "Cannot bind receiver socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    close(s);
    return -1;
  }
  if (sockettype != SOCK_DGRAM)
    if (listen(s, 8) < 0) {
      ast_log(4, "mtp3io.c", 90, __PRETTY_FUNCTION__, "Cannot listen on socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
      close(s);
      return -1;
    }
  return s;
}

canonical_original_code:
static int setup_socket(int localport, int sockettype, int ipproto)
{
  struct sockaddr_in sock;
  unsigned int addr = ((in_addr_t) 0x00000000);
  int parm; int res; int flags;
  int s;
  memset(&sock, 0, sizeof(struct sockaddr_in));
  sock.sin_family = 2;
  sock.sin_port = htons(localport);
  memcpy(&sock.sin_addr, &addr, sizeof(addr));
  s = socket(2, sockettype, ipproto);
  if (s < 0) {
    ast_log(4, "mtp3io.c", 65, __PRETTY_FUNCTION__, "Cannot create UDP socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    return -1;
  }
  parm = 1;
  setsockopt(s, 1, 2, &parm, sizeof(int));
  res = fcntl(s, 3);
  if(res < 0) {
    ast_log(3, "mtp3io.c", 73, __PRETTY_FUNCTION__, "Could not obtain flags for socket fd: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  flags = res | 04000;
  res = fcntl(s, 4, flags);
  if(res < 0) {
    ast_log(3, "mtp3io.c", 79, __PRETTY_FUNCTION__, "Could not set socket fd non-blocking: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  if (bind(s, &sock, sizeof(sock)) < 0) {
    ast_log(4, "mtp3io.c", 84, __PRETTY_FUNCTION__, "Cannot bind receiver socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    close(s);
    return -1;
  }
  if (sockettype != SOCK_DGRAM)
    if (listen(s, 8) < 0) {
      ast_log(4, "mtp3io.c", 90, __PRETTY_FUNCTION__, "Cannot listen on socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
      close(s);
      return -1;
    }
  return s;
}

variable_types: localport int

variable_types: sockettype int

variable_types: ipproto int

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

variable_types:
addr
unsigned int

variable_types: parm int

variable_types: flags int

variable_types: s int

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


decompiled_code:
__int64 __fastcall mtp3_setup_socket(__int16 a1, __int16 a2)
{
return setup_socket(a2 + a1, mtp3_sockettype);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1, __int16 a2)
{
return func1(a2 + a1, mtp3_sockettype);
}

original_code:
int mtp3_setup_socket(int port, int schannel)
{
  return setup_socket(port + schannel, mtp3_sockettype, mtp3_ipproto);
}

variable_types: port int

variable_types: schannel int


decompiled_code:
__int64 __fastcall mtp3_connect_socket(const char *a1, const char *a2)
{
int v2; // ebp
int v3; // er12
int v4; // eax
struct addrinfo *i; // r14
unsigned int v6; // eax
__int64 v7; // r15
__int64 v8; // rbx
char *v9; // rax
int *v10; // rax
char *v11; // rax
const char *v13; // rax
struct addrinfo *pai; // [rsp+18h] [rbp-70h] BYREF
__int128 v15; // [rsp+20h] [rbp-68h]
__int128 v16; // [rsp+30h] [rbp-58h]
__int128 v17; // [rsp+40h] [rbp-48h]

LODWORD(v15) = 0;
v2 = mtp3_sockettype;
v3 = mtp3_ipproto;
DWORD1(v15) = 2;
*((_QWORD *)&v15 + 1) = __PAIR64__(mtp3_ipproto, mtp3_sockettype);
v16 = 0LL;
v17 = 0LL;
v4 = getaddrinfo(a1, a2, 0LL, &pai);
if ( v4 )
{
LODWORD(v7) = -1;
v13 = gai_strerror(v4);
ast_log(
4LL,
"mtp3io.c",
117LL,
"mtp3_connect_socket",
"Invalid hostname/IP address '%s' or port '%s': %s.\n",
a1,
a2,
v13);
}
else
{
for ( i = pai; i; i = i->ai_next )
{
v6 = socket(i->ai_family, v2, v3);
v7 = v6;
if ( v6 != -1 )
{
v8 = (unsigned __int16)_bswap_16(HIWORD(i->ai_flags));
v9 = inet_ntoa((struct in_addr)i->ai_family);
ast_log(0LL, "mtp3io.c", 126LL, "mtp3_connect_socket", "connecting to mtp3d %s:%d, fd %d\n", v9, v8, v7);
if ( connect(v7, i->ai_addr, i->ai_addrlen) != -1 )
goto LABEL_8;
close(v7);
}
}
v10 = _errno_location();
LODWORD(v7) = -1;
v11 = strerror(*v10);
ast_log(
4LL,
"mtp3io.c",
132LL,
"mtp3_connect_socket",
"Could not connect to hostname/IP address '%s', port '%s': %s.\n",
a1,
a2,
v11);
LABEL_8:
freeaddrinfo(pai);
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, const char *a2)
{
int v2; // ebp
int v3; // er12
int v4; // eax
struct addrinfo *i; // r14
unsigned int v6; // eax
__int64 v7; // r15
__int64 v8; // rbx
char *v9; // rax
int *v10; // rax
char *v11; // rax
const char *v13; // rax
struct addrinfo *pai; // [rsp+18h] [rbp-70h] BYREF
__int128 v15; // [rsp+20h] [rbp-68h]
__int128 v16; // [rsp+30h] [rbp-58h]
__int128 v17; // [rsp+40h] [rbp-48h]

LODWORD(v15) = 0;
v2 = mtp3_sockettype;
v3 = mtp3_ipproto;
DWORD1(v15) = 2;
*((_QWORD *)&v15 + 1) = __PAIR64__(mtp3_ipproto, mtp3_sockettype);
v16 = 0LL;
v17 = 0LL;
v4 = getaddrinfo(a1, a2, 0LL, &pai);
if ( v4 )
{
LODWORD(v7) = -1;
v13 = gai_strerror(v4);
ast_log(
4LL,
"mtp3io.c",
117LL,
"mtp3_connect_socket",
"Invalid hostname/IP address '%s' or port '%s': %s.\n",
a1,
a2,
v13);
}
else
{
for ( i = pai; i; i = i->ai_next )
{
v6 = socket(i->ai_family, v2, v3);
v7 = v6;
if ( v6 != -1 )
{
v8 = (unsigned __int16)func0(HIWORD(i->ai_flags));
v9 = inet_ntoa((struct in_addr)i->ai_family);
ast_log(0LL, "mtp3io.c", 126LL, "mtp3_connect_socket", "connecting to mtp3d %s:%d, fd %d\n", v9, v8, v7);
if ( connect(v7, i->ai_addr, i->ai_addrlen) != -1 )
goto LABEL_8;
close(v7);
}
}
v10 = _errno_location();
LODWORD(v7) = -1;
v11 = strerror(*v10);
ast_log(
4LL,
"mtp3io.c",
132LL,
"mtp3_connect_socket",
"Could not connect to hostname/IP address '%s', port '%s': %s.\n",
a1,
a2,
v11);
LABEL_8:
freeaddrinfo(pai);
}
return (unsigned int)v7;
}

original_code:
int mtp3_connect_socket(const char* host, const char* port)
{
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s, res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = 2;
  hints.ai_socktype = mtp3_sockettype;
  hints.ai_protocol = mtp3_ipproto;
  res = getaddrinfo(host, port, ((void *)0), &result);
  if (res != 0) {
    ast_log(4, "mtp3io.c", 117, __PRETTY_FUNCTION__, "Invalid hostname/IP address '%s' or port '%s': %s.\n", host, port, gai_strerror(res)
     );
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    res = socket(rp->ai_family, hints.ai_socktype, hints.ai_protocol);
    if (res == -1)
      continue;
    ast_log(0, "mtp3io.c", 126, __PRETTY_FUNCTION__, "connecting to mtp3d %s:%d, fd %d\n", inet_ntoa(((struct sockaddr_in*) rp)->sin_addr), ntohs(((struct sockaddr_in*) rp)->sin_port), res);
    if ((s = connect(res, rp->ai_addr, rp->ai_addrlen)) != -1)
      break;
    close(res);
  }
  if (rp == ((void *)0)) {
    ast_log(4, "mtp3io.c", 132, __PRETTY_FUNCTION__, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", host, port, strerror((*__errno_location ())));
    res = -1;
  }
  freeaddrinfo(result);
  return res;
}

canonical_original_code:
int mtp3_connect_socket(char *host, char *port)
{
  struct addrinfo hints;
  struct addrinfo *result; struct addrinfo *rp;
  int s; int res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = 2;
  hints.ai_socktype = mtp3_sockettype;
  hints.ai_protocol = mtp3_ipproto;
  res = getaddrinfo(host, port, ((void *)0), &result);
  if (res != 0) {
    ast_log(4, "mtp3io.c", 117, __PRETTY_FUNCTION__, "Invalid hostname/IP address '%s' or port '%s': %s.\n", host, port, gai_strerror(res)
     );
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    res = socket(rp->ai_family, hints.ai_socktype, hints.ai_protocol);
    if (res == -1)
      continue;
    ast_log(0, "mtp3io.c", 126, __PRETTY_FUNCTION__, "connecting to mtp3d %s:%d, fd %d\n", inet_ntoa(((struct sockaddr_in *) rp)->sin_addr), ntohs(((struct sockaddr_in *) rp)->sin_port), res);
    if ((s = connect(res, rp->ai_addr, rp->ai_addrlen)) != -1)
      break;
    close(res);
  }
  if (rp == ((void *)0)) {
    ast_log(4, "mtp3io.c", 132, __PRETTY_FUNCTION__, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", host, port, strerror((*__errno_location ())));
    res = -1;
  }
  freeaddrinfo(result);
  return res;
}

variable_types:
host
char *

variable_types:
port
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: res int

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
__int64 __fastcall mtp3_send(int fd, void *buf, size_t n)
{
unsigned int v5; // ebx
int v6; // eax
int v7; // er13
int v8; // edi
char *v9; // rax

v5 = n;
do
{
v6 = send(fd, buf, v5, 0);
v7 = v6;
if ( v6 >= 0 )
{
if ( v6 )
{
v5 -= v6;
buf = (char *)buf + v6;
}
}
else
{
v8 = *_errno_location();
if ( v8 != 4 )
goto LABEL_7;
}
}
while ( v5 );
if ( v7 < 0 )
{
v8 = *_errno_location();
LABEL_7:
v9 = strerror(v8);
ast_log(4LL, "mtp3io.c", 155LL, "mtp3_send", "Cannot send mtp3 packet: %s\n", v9);
return (unsigned int)v7;
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, void *buf, size_t n)
{
unsigned int v5; // ebx
int v6; // eax
int v7; // er13
int v8; // edi
char *v9; // rax

v5 = n;
do
{
v6 = send(fd, buf, v5, 0);
v7 = v6;
if ( v6 >= 0 )
{
if ( v6 )
{
v5 -= v6;
buf = (char *)buf + v6;
}
}
else
{
v8 = *_errno_location();
if ( v8 != 4 )
goto LABEL_7;
}
}
while ( v5 );
if ( v7 < 0 )
{
v8 = *_errno_location();
LABEL_7:
v9 = strerror(v8);
ast_log(4LL, "mtp3io.c", 155LL, "mtp3_send", "Cannot send mtp3 packet: %s\n", v9);
return (unsigned int)v7;
}
return (unsigned int)v7;
}

original_code:
int mtp3_send(int s, const unsigned char* buff, unsigned int len)
{
  int res;
  do {
    res = send(s, buff, len, 0);
    if (res < 0) {
      if ((*__errno_location ()) != 4)
 break;
    }
    else if (res > 0) {
      buff += res;
      len -= res;
    }
  } while (len > 0);
  if (res < 0) {
    ast_log(4, "mtp3io.c", 155, __PRETTY_FUNCTION__, "Cannot send mtp3 packet: %s\n", strerror((*__errno_location ())));
  }
  return res;
}

canonical_original_code:
int mtp3_send(int s, unsigned char *buff, unsigned int len)
{
  int res;
  do {
    res = send(s, buff, len, 0);
    if (res < 0) {
      if ((*__errno_location ()) != 4)
 break;
    }
    else if (res > 0) {
      buff += res;
      len -= res;
    }
  } while (len > 0);
  if (res < 0) {
    ast_log(4, "mtp3io.c", 155, __PRETTY_FUNCTION__, "Cannot send mtp3 packet: %s\n", strerror((*__errno_location ())));
  }
  return res;
}

variable_types:
buff
unsigned char *

variable_types:
len
unsigned int


decompiled_code:
__int64 __fastcall mtp3_reply(int fd, void *buf, size_t n, struct sockaddr *addr, socklen_t addr_len)
{
unsigned int v8; // ebx
struct in_addr *v9; // rcx
__int64 v10; // r15
char *v11; // rax
__int64 result; // rax
int v13; // er15
int *v14; // rax
char *v15; // rax

v8 = n;
v10 = (unsigned __int16)_bswap_16(*(_WORD *)addr->sa_data);
v11 = inet_ntoa(v9[1]);
ast_log(0LL, "mtp3io.c", 162LL, "mtp3_reply", "Send packet to %s:%d\n", v11, v10);
do
{
result = sendto(fd, buf, v8, 0, addr, addr_len);
v13 = result;
if ( (int)result >= 0 )
{
if ( (_DWORD)result )
{
v8 -= result;
buf = (char *)buf + (int)result;
}
continue;
}
result = (__int64)_errno_location();
if ( *(_DWORD *)result != 4 )
break;
}
while ( v8 );
if ( v13 == -1 )
{
v14 = _errno_location();
v15 = strerror(*v14);
result = ast_log(4LL, "mtp3io.c", 176LL, "mtp3_reply", "Cannot send reply mtp3 packet: %s\n", v15);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, void *buf, size_t n, struct sockaddr *addr, socklen_t addr_len)
{
unsigned int v8; // ebx
struct in_addr *v9; // rcx
__int64 v10; // r15
char *v11; // rax
__int64 result; // rax
int v13; // er15
int *v14; // rax
char *v15; // rax

v8 = n;
v10 = (unsigned __int16)func0(*(_WORD *)addr->sa_data);
v11 = inet_ntoa(v9[1]);
ast_log(0LL, "mtp3io.c", 162LL, "mtp3_reply", "Send packet to %s:%d\n", v11, v10);
do
{
result = sendto(fd, buf, v8, 0, addr, addr_len);
v13 = result;
if ( (int)result >= 0 )
{
if ( (_DWORD)result )
{
v8 -= result;
buf = (char *)buf + (int)result;
}
continue;
}
result = (__int64)_errno_location();
if ( *(_DWORD *)result != 4 )
break;
}
while ( v8 );
if ( v13 == -1 )
{
v14 = _errno_location();
v15 = strerror(*v14);
result = ast_log(4LL, "mtp3io.c", 176LL, "mtp3_reply", "Cannot send reply mtp3 packet: %s\n", v15);
}
return result;
}

original_code:
void mtp3_reply(int s, const unsigned char* buff, unsigned int len, const struct sockaddr* to, socklen_t tolen)
{
  ast_log(0, "mtp3io.c", 162, __PRETTY_FUNCTION__, "Send packet to %s:%d\n", inet_ntoa(((struct sockaddr_in*) to)->sin_addr), ntohs(((struct sockaddr_in*) to)->sin_port));
  int res;
  do {
    res = sendto(s, buff, len, 0, to, tolen);
    if (res < 0) {
      if ((*__errno_location ()) != 4)
 break;
    }
    else if (res > 0) {
      buff += res;
      len -= res;
    }
  } while (len > 0);
  if (res == -1)
    ast_log(4, "mtp3io.c", 176, __PRETTY_FUNCTION__, "Cannot send reply mtp3 packet: %s\n", strerror((*__errno_location ())));
}

canonical_original_code:
void mtp3_reply(int s, unsigned char *buff, unsigned int len, struct sockaddr *to, unsigned int tolen)
{
  ast_log(0, "mtp3io.c", 162, __PRETTY_FUNCTION__, "Send packet to %s:%d\n", inet_ntoa(((struct sockaddr_in *) to)->sin_addr), ntohs(((struct sockaddr_in *) to)->sin_port));
  int res;
  do {
    res = sendto(s, buff, len, 0, to, tolen);
    if (res < 0) {
      if ((*__errno_location ()) != 4)
 break;
    }
    else if (res > 0) {
      buff += res;
      len -= res;
    }
  } while (len > 0);
  if (res == -1)
    ast_log(4, "mtp3io.c", 176, __PRETTY_FUNCTION__, "Cannot send reply mtp3 packet: %s\n", strerror((*__errno_location ())));
}

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

variable_types:
tolen
unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char **v4; // rbx
const char **v5; // rbp
size_t v6; // rax
int v7; // eax
int v8; // er12
unsigned int v9; // ebx
int v10; // edi
const char *v12; // rsi
char *v13; // rdx
int *v14; // rax
struct pollfd fds; // [rsp+Ch] [rbp-28BCh] BYREF
char v16[8]; // [rsp+16h] [rbp-28B2h] BYREF
__int16 v17; // [rsp+1Eh] [rbp-28AAh]
char dest[16]; // [rsp+20h] [rbp-28A8h] BYREF
__int128 v19; // [rsp+30h] [rbp-2898h]
__int128 v20; // [rsp+40h] [rbp-2888h]
__int128 v21; // [rsp+50h] [rbp-2878h]
__int128 v22; // [rsp+60h] [rbp-2868h]
__int128 v23; // [rsp+70h] [rbp-2858h]
__int128 v24; // [rsp+80h] [rbp-2848h]
__int128 v25; // [rsp+90h] [rbp-2838h]
int buf[29]; // [rsp+A0h] [rbp-2828h] BYREF
int v27; // [rsp+114h] [rbp-27B4h]
char s[10160]; // [rsp+118h] [rbp-27B0h] BYREF

*(_QWORD *)v16 = 0x3939393131LL;
v17 = 0;
*(__m128i *)dest = _mm_load_si128((const __m128i *)&xmmword_402100);
v19 = 0LL;
v20 = 0LL;
v21 = 0LL;
v22 = 0LL;
v23 = 0LL;
v24 = 0LL;
v25 = 0LL;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "h:p:");
if ( v3 == -1 )
break;
if ( v3 == 104 )
{
strcpy(dest, optarg);
}
else
{
if ( v3 != 112 )
usage();
strcpy(v16, optarg);
}
}
s[0] = 0;
buf[0] = 6;
if ( argc > optind )
{
v4 = &argv[optind];
v5 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
++v4;
v6 = strlen(s);
*(_WORD *)stpcpy(&s[v6], *(v4 - 1)) = 10;
}
while ( v4 != v5 );
}
v27 = strlen(s) + 1;
v7 = connect_socket(dest, v16);
v8 = v7;
if ( v7 != -1 )
{
if ( (unsigned int)write(v7, buf, v27 + 120LL) == -1 )
{
v14 = __errno_location();
v12 = "Could not write socket: %s.\n";
v13 = strerror(*v14);
}
else
{
fds.fd = v8;
fds.events = 57;
poll(&fds, 1uLL, 10000);
shutdown(v8, 1);
while ( 1 )
{
v9 = read(v8, buf, 0x2800uLL);
printf("read %d\n", v9);
if ( v9 == -1 )
break;
if ( !(unsigned int)write(1, buf, (int)v9) )
return 0;
}
v10 = *__errno_location();
if ( v10 == 107 )
return 0;
v12 = "Could not read socket: %s.\n";
v13 = strerror(v10);
}
v8 = -1;
fprintf(stderr, v12, v13);
}
return v8;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char **v4; // rbx
const char **v5; // rbp
size_t v6; // rax
int v7; // eax
int v8; // er12
unsigned int v9; // ebx
int v10; // edi
const char *v12; // rsi
char *v13; // rdx
int *v14; // rax
struct pollfd fds; // [rsp+Ch] [rbp-28BCh] BYREF
char v16[8]; // [rsp+16h] [rbp-28B2h] BYREF
__int16 v17; // [rsp+1Eh] [rbp-28AAh]
char dest[16]; // [rsp+20h] [rbp-28A8h] BYREF
__int128 v19; // [rsp+30h] [rbp-2898h]
__int128 v20; // [rsp+40h] [rbp-2888h]
__int128 v21; // [rsp+50h] [rbp-2878h]
__int128 v22; // [rsp+60h] [rbp-2868h]
__int128 v23; // [rsp+70h] [rbp-2858h]
__int128 v24; // [rsp+80h] [rbp-2848h]
__int128 v25; // [rsp+90h] [rbp-2838h]
int buf[29]; // [rsp+A0h] [rbp-2828h] BYREF
int v27; // [rsp+114h] [rbp-27B4h]
char s[10160]; // [rsp+118h] [rbp-27B0h] BYREF

*(_QWORD *)v16 = 0x3939393131LL;
v17 = 0;
*(__m128i *)dest = _mm_load_si128((const __m128i *)&xmmword_402100);
v19 = 0LL;
v20 = 0LL;
v21 = 0LL;
v22 = 0LL;
v23 = 0LL;
v24 = 0LL;
v25 = 0LL;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "h:p:");
if ( v3 == -1 )
break;
if ( v3 == 104 )
{
strcpy(dest, optarg);
}
else
{
if ( v3 != 112 )
func7();
strcpy(v16, optarg);
}
}
s[0] = 0;
buf[0] = 6;
if ( argc > optind )
{
v4 = &argv[optind];
v5 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
++v4;
v6 = strlen(s);
*(_WORD *)stpcpy(&s[v6], *(v4 - 1)) = 10;
}
while ( v4 != v5 );
}
v27 = strlen(s) + 1;
v7 = func8(dest, v16);
v8 = v7;
if ( v7 != -1 )
{
if ( (unsigned int)write(v7, buf, v27 + 120LL) == -1 )
{
v14 = __errno_location();
v12 = "Could not write socket: %s.\n";
v13 = strerror(*v14);
}
else
{
fds.fd = v8;
fds.events = 57;
poll(&fds, 1uLL, 10000);
shutdown(v8, 1);
while ( 1 )
{
v9 = read(v8, buf, 0x2800uLL);
printf("read %d\n", v9);
if ( v9 == -1 )
break;
if ( !(unsigned int)write(1, buf, (int)v9) )
return 0;
}
v10 = *__errno_location();
if ( v10 == 107 )
return 0;
v12 = "Could not read socket: %s.\n";
v13 = strerror(v10);
}
v8 = -1;
fprintf(stderr, v12, v13);
}
return v8;
}

original_code:
int main(int argc, char* argv[])
{
  char dumpfn[4096] = {0,};
  int c;
  strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
  while ((c = getopt(argc, argv, "fc:m:dp")) != -1) {
    switch (c) {
    case 'c':
      strcpy(ast_config_AST_CONFIG_DIR, optarg);
      break;
    case 'm':
      strcpy(dumpfn, optarg);
      break;
    case 'd':
      option_debug++;
      break;
    case 'f':
      do_fork = 1;
      break;
    case 'p':
      do_pid = 1;
      break;
    default:
      usage();
    }
  }
  is_mtp3d = 1;
  if (do_fork) {
    reopen_logfiles();
  }
  printf("Using %s for config directory\n", ast_config_AST_CONFIG_DIR);
  if(load_config(0)) {
    return -1;
  }
  setsigactions();
  if (*dumpfn)
    setup_dump(dumpfn);
  if (do_pid || do_fork)
    if (setup_daemon())
      return -1;
  if(mtp_init()) {
    ast_log(4, "mtp3d.c", 773, __PRETTY_FUNCTION__, "Unable to initialize MTP.\n");
    return -1;
  }
  if (do_pid)
  make_pid_file();
  if(start_mtp_thread()) {
    ast_log(4, "mtp3d.c", 779, __PRETTY_FUNCTION__, "Unable to start MTP thread.\n");
    return -1;
  }
  monitor_running = 1;
  if(pthread_create(&monitor_thread, ((void *)0), monitor_main, ((void *)0)) < 0) {
    ast_log(4, "mtp3d.c", 785, __PRETTY_FUNCTION__, "Unable to start monitor thread.\n");
    monitor_running = 0;
    return -1;
  }
  mtp_mainloop();
  stop_mtp_thread();
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  char dumpfn[4096] = {0,};
  int c;
  strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
  while ((c = getopt(argc, argv, "fc:m:dp")) != -1) {
    switch (c) {
    case 'c':
      strcpy(ast_config_AST_CONFIG_DIR, optarg);
      break;
    case 'm':
      strcpy(dumpfn, optarg);
      break;
    case 'd':
      option_debug++;
      break;
    case 'f':
      do_fork = 1;
      break;
    case 'p':
      do_pid = 1;
      break;
    default:
      usage();
    }
  }
  is_mtp3d = 1;
  if (do_fork) {
    reopen_logfiles();
  }
  printf("Using %s for config directory\n", ast_config_AST_CONFIG_DIR);
  if(load_config(0)) {
    return -1;
  }
  setsigactions();
  if (*dumpfn)
    setup_dump(dumpfn);
  if (do_pid || do_fork)
    if (setup_daemon())
      return -1;
  if(mtp_init()) {
    ast_log(4, "mtp3d.c", 773, __PRETTY_FUNCTION__, "Unable to initialize MTP.\n");
    return -1;
  }
  if (do_pid)
  make_pid_file();
  if(start_mtp_thread()) {
    ast_log(4, "mtp3d.c", 779, __PRETTY_FUNCTION__, "Unable to start MTP thread.\n");
    return -1;
  }
  monitor_running = 1;
  if(pthread_create(&monitor_thread, ((void *)0), monitor_main, ((void *)0)) < 0) {
    ast_log(4, "mtp3d.c", 785, __PRETTY_FUNCTION__, "Unable to start monitor thread.\n");
    monitor_running = 0;
    return -1;
  }
  mtp_mainloop();
  stop_mtp_thread();
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
dumpfn
char [4096]

variable_types: c int