printBPB

decompiled_code:
int __fastcall printBPB(unsigned __int8 *a1)
{
int v1; // eax
unsigned __int8 *v2; // rax
int v3; // edx

puts("==== BIOS PARAMETER BLOCK ====");
printf(" Boot Instruction: 0x%02x%02x%02x\n", *a1, a1[1], a1[2]);
printf(" OEM Name: '%8s'\n", (const char *)a1 + 3);
printf(" Bytes per Sector: %d\n", *(unsigned __int16 *)(a1 + 11));
printf("Sectors per Cluster: %d\n", a1[13]);
printf(" Reserved Sectors: %d\n", *((unsigned __int16 *)a1 + 7));
printf(" FATS: %d\n", a1[16]);
printf(" Root Entries: %d\n", *(unsigned __int16 *)(a1 + 17));
printf(" Media: ");
print_media(a1[21]);
putchar(10);
printf(" Fat Size (16b): %d\n", *((unsigned __int16 *)a1 + 11));
printf(" Sectors per Track: %d\n", *((unsigned __int16 *)a1 + 12));
printf(" Number of Heads: %d\n", *((unsigned __int16 *)a1 + 13));
printf(" Hidden Sectors: %d\n", *((unsigned int *)a1 + 7));
printf("Total Sectors (16b): %d\n", *(unsigned __int16 *)(a1 + 19));
printf("Total Sectors (32b): %d\n", *((unsigned int *)a1 + 8));
v1 = fat_type(a1);
if ( v1 == 1 )
{
puts(" FAT Type: FAT16");
goto LABEL_14;
}
if ( v1 != 2 )
{
if ( v1 )
{
puts("FAT TYPE UNRECOGNIZED!");
goto LABEL_5;
}
puts(" FAT Type: FAT12");
LABEL_14:
printf(" Drive Number: %d\n", a1[36]);
printf(" Boot Signature: 0x%02x\n", a1[38]);
printf(" Volume ID: 0x%04x\n", *(unsigned int *)(a1 + 39));
printf(" Volume Label: '%.*s'\n", 11, (const char *)a1 + 43);
printf(" FileSystemType: '%.*s'\n", 8, (const char *)a1 + 54);
return putchar(10);
}
puts(" FAT Type: FAT32");
LABEL_5:
printf(" FAT Size (32b): %d\n", *((unsigned int *)a1 + 9));
printf(" FAT Flags: 0x%04x\n", *((unsigned __int16 *)a1 + 20));
printf(" FS Version: 0x%04x\n", *((unsigned __int16 *)a1 + 21));
printf(" Root Cluster: %d\n", *((unsigned int *)a1 + 11));
printf(" FSINFO Sector: %d\n", *((unsigned __int16 *)a1 + 24));
printf("Bkup BootRec Sector: %d\n", *((unsigned __int16 *)a1 + 25));
v2 = a1 + 52;
v3 = 0;
do
{
if ( *v2 )
v3 = 1;
++v2;
}
while ( a1 + 64 != v2 );
if ( v3 )
puts(" Reserved Area: NONZERO!");
else
puts(" Reserved Area: Ok");
printf(" Drive Number: %d\n", a1[64]);
printf(" Boot Signature: 0x%02x\n", a1[66]);
printf(" Volume ID: 0x%04x\n", *(unsigned int *)(a1 + 67));
printf(" Volume Label: '%.*s'\n", 11, (const char *)a1 + 71);
printf(" FileSystemType: '%.*s'\n", 8, (const char *)a1 + 82);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func2(unsigned __int8 *a1)
{
int v1; // eax
unsigned __int8 *v2; // rax
int v3; // edx

puts("==== BIOS PARAMETER BLOCK ====");
printf(" Boot Instruction: 0x%02x%02x%02x\n", *a1, a1[1], a1[2]);
printf(" OEM Name: '%8s'\n", (const char *)a1 + 3);
printf(" Bytes per Sector: %d\n", *(unsigned __int16 *)(a1 + 11));
printf("Sectors per Cluster: %d\n", a1[13]);
printf(" Reserved Sectors: %d\n", *((unsigned __int16 *)a1 + 7));
printf(" FATS: %d\n", a1[16]);
printf(" Root Entries: %d\n", *(unsigned __int16 *)(a1 + 17));
printf(" Media: ");
func1(a1[21]);
putchar(10);
printf(" Fat Size (16b): %d\n", *((unsigned __int16 *)a1 + 11));
printf(" Sectors per Track: %d\n", *((unsigned __int16 *)a1 + 12));
printf(" Number of Heads: %d\n", *((unsigned __int16 *)a1 + 13));
printf(" Hidden Sectors: %d\n", *((unsigned int *)a1 + 7));
printf("Total Sectors (16b): %d\n", *(unsigned __int16 *)(a1 + 19));
printf("Total Sectors (32b): %d\n", *((unsigned int *)a1 + 8));
v1 = func0(a1);
if ( v1 == 1 )
{
puts(" FAT Type: FAT16");
goto LABEL_14;
}
if ( v1 != 2 )
{
if ( v1 )
{
puts("FAT TYPE UNRECOGNIZED!");
goto LABEL_5;
}
puts(" FAT Type: FAT12");
LABEL_14:
printf(" Drive Number: %d\n", a1[36]);
printf(" Boot Signature: 0x%02x\n", a1[38]);
printf(" Volume ID: 0x%04x\n", *(unsigned int *)(a1 + 39));
printf(" Volume Label: '%.*s'\n", 11, (const char *)a1 + 43);
printf(" FileSystemType: '%.*s'\n", 8, (const char *)a1 + 54);
return putchar(10);
}
puts(" FAT Type: FAT32");
LABEL_5:
printf(" FAT Size (32b): %d\n", *((unsigned int *)a1 + 9));
printf(" FAT Flags: 0x%04x\n", *((unsigned __int16 *)a1 + 20));
printf(" FS Version: 0x%04x\n", *((unsigned __int16 *)a1 + 21));
printf(" Root Cluster: %d\n", *((unsigned int *)a1 + 11));
printf(" FSINFO Sector: %d\n", *((unsigned __int16 *)a1 + 24));
printf("Bkup BootRec Sector: %d\n", *((unsigned __int16 *)a1 + 25));
v2 = a1 + 52;
v3 = 0;
do
{
if ( *v2 )
v3 = 1;
++v2;
}
while ( a1 + 64 != v2 );
if ( v3 )
puts(" Reserved Area: NONZERO!");
else
puts(" Reserved Area: Ok");
printf(" Drive Number: %d\n", a1[64]);
printf(" Boot Signature: 0x%02x\n", a1[66]);
printf(" Volume ID: 0x%04x\n", *(unsigned int *)(a1 + 67));
printf(" Volume Label: '%.*s'\n", 11, (const char *)a1 + 71);
printf(" FileSystemType: '%.*s'\n", 8, (const char *)a1 + 82);
return putchar(10);
}

original_code:
void printBPB(BPB_struct *s) {
 int i,j;
 printf("==== BIOS PARAMETER BLOCK ====\n");
 printf("   Boot Instruction: 0x%02x%02x%02x\n", s->BS_JumpBoot[0], s->BS_JumpBoot[1], s->BS_JumpBoot[2]);
 printf("           OEM Name: '%8s'\n", s->BS_OEMName);
 printf("   Bytes per Sector: %d\n", s->BytesPerSector);
 printf("Sectors per Cluster: %d\n", s->SectorsPerCluster);
 printf("   Reserved Sectors: %d\n", s->ReservedSectorCount);
 printf("               FATS: %d\n", s->NumFATs);
 printf("       Root Entries: %d\n", s->RootEntryCount);
 printf("              Media: "); print_media(s->Media); printf("\n");
 printf("     Fat Size (16b): %d\n", s->FATSize16);
 printf("  Sectors per Track: %d\n", s->SectorsPerTrack);
 printf("    Number of Heads: %d\n", s->NumberOfHeads);
 printf("     Hidden Sectors: %d\n", s->HiddenSectors);
 printf("Total Sectors (16b): %d\n", s->TotalSectors16);
 printf("Total Sectors (32b): %d\n", s->TotalSectors32);
 switch(fat_type(s)) {
  case 0: printf("           FAT Type: FAT12\n"); break;
  case 1: printf("           FAT Type: FAT16\n"); break;
  case 2: printf("           FAT Type: FAT32\n"); break;
  default: printf("FAT TYPE UNRECOGNIZED!\n"); break;
 }
 if((fat_type(s) == 0) | (fat_type(s) == 1)) {
  printf("       Drive Number: %d\n", s->FSTypeSpecificData.fat16.BS_DriveNumber);
  printf("     Boot Signature: 0x%02x\n", s->FSTypeSpecificData.fat16.BS_BootSig);
  printf("          Volume ID: 0x%04x\n", s->FSTypeSpecificData.fat16.BS_VolumeID);
  printf("       Volume Label: '%.*s'\n", 11, s->FSTypeSpecificData.fat16.BS_VolumeLabel);
  printf("     FileSystemType: '%.*s'\n", 8, s->FSTypeSpecificData.fat16.BS_FileSystemType);
 }
 else {
  printf("     FAT Size (32b): %d\n", s->FSTypeSpecificData.fat32.FATSize);
     printf("          FAT Flags: 0x%04x\n", s->FSTypeSpecificData.fat32.ExtFlags);
  printf("         FS Version: 0x%04x\n", s->FSTypeSpecificData.fat32.FSVersion);
  printf("       Root Cluster: %d\n", s->FSTypeSpecificData.fat32.RootCluster);
  printf("      FSINFO Sector: %d\n", s->FSTypeSpecificData.fat32.FSInfo);
  printf("Bkup BootRec Sector: %d\n", s->FSTypeSpecificData.fat32.BkBootSec);
  j=0;
  for(i=0; i<12; i++) {
   if(s->FSTypeSpecificData.fat32.Reserved[i] != 0x00) j=1;
  }
  if(j) printf("      Reserved Area: NONZERO!\n");
  else printf("      Reserved Area: Ok\n");
  printf("       Drive Number: %d\n", s->FSTypeSpecificData.fat32.BS_DriveNumber);
  printf("     Boot Signature: 0x%02x\n", s->FSTypeSpecificData.fat32.BS_BootSig);
  printf("          Volume ID: 0x%04x\n", s->FSTypeSpecificData.fat32.BS_VolumeID);
  printf("       Volume Label: '%.*s'\n", 11, s->FSTypeSpecificData.fat32.BS_VolumeLabel);
  printf("     FileSystemType: '%.*s'\n", 8, s->FSTypeSpecificData.fat32.BS_FileSystemType);
 }
 printf("\n");
}

canonical_original_code:
void printBPB(struct struct_BPB_struct *s) {
 int i; int j;
 printf("==== BIOS PARAMETER BLOCK ====\n");
 printf("   Boot Instruction: 0x%02x%02x%02x\n", s->BS_JumpBoot[0], s->BS_JumpBoot[1], s->BS_JumpBoot[2]);
 printf("           OEM Name: '%8s'\n", s->BS_OEMName);
 printf("   Bytes per Sector: %d\n", s->BytesPerSector);
 printf("Sectors per Cluster: %d\n", s->SectorsPerCluster);
 printf("   Reserved Sectors: %d\n", s->ReservedSectorCount);
 printf("               FATS: %d\n", s->NumFATs);
 printf("       Root Entries: %d\n", s->RootEntryCount);
 printf("              Media: "); print_media(s->Media); printf("\n");
 printf("     Fat Size (16b): %d\n", s->FATSize16);
 printf("  Sectors per Track: %d\n", s->SectorsPerTrack);
 printf("    Number of Heads: %d\n", s->NumberOfHeads);
 printf("     Hidden Sectors: %d\n", s->HiddenSectors);
 printf("Total Sectors (16b): %d\n", s->TotalSectors16);
 printf("Total Sectors (32b): %d\n", s->TotalSectors32);
 switch(fat_type(s)) {
  case 0: printf("           FAT Type: FAT12\n"); break;
  case 1: printf("           FAT Type: FAT16\n"); break;
  case 2: printf("           FAT Type: FAT32\n"); break;
  default: printf("FAT TYPE UNRECOGNIZED!\n"); break;
 }
 if((fat_type(s) == 0) | (fat_type(s) == 1)) {
  printf("       Drive Number: %d\n", s->FSTypeSpecificData.fat16.BS_DriveNumber);
  printf("     Boot Signature: 0x%02x\n", s->FSTypeSpecificData.fat16.BS_BootSig);
  printf("          Volume ID: 0x%04x\n", s->FSTypeSpecificData.fat16.BS_VolumeID);
  printf("       Volume Label: '%.*s'\n", 11, s->FSTypeSpecificData.fat16.BS_VolumeLabel);
  printf("     FileSystemType: '%.*s'\n", 8, s->FSTypeSpecificData.fat16.BS_FileSystemType);
 }
 else {
  printf("     FAT Size (32b): %d\n", s->FSTypeSpecificData.fat32.FATSize);
     printf("          FAT Flags: 0x%04x\n", s->FSTypeSpecificData.fat32.ExtFlags);
  printf("         FS Version: 0x%04x\n", s->FSTypeSpecificData.fat32.FSVersion);
  printf("       Root Cluster: %d\n", s->FSTypeSpecificData.fat32.RootCluster);
  printf("      FSINFO Sector: %d\n", s->FSTypeSpecificData.fat32.FSInfo);
  printf("Bkup BootRec Sector: %d\n", s->FSTypeSpecificData.fat32.BkBootSec);
  j=0;
  for(i=0; i<12; i++) {
   if(s->FSTypeSpecificData.fat32.Reserved[i] != 0x00) j=1;
  }
  if(j) printf("      Reserved Area: NONZERO!\n");
  else printf("      Reserved Area: Ok\n");
  printf("       Drive Number: %d\n", s->FSTypeSpecificData.fat32.BS_DriveNumber);
  printf("     Boot Signature: 0x%02x\n", s->FSTypeSpecificData.fat32.BS_BootSig);
  printf("          Volume ID: 0x%04x\n", s->FSTypeSpecificData.fat32.BS_VolumeID);
  printf("       Volume Label: '%.*s'\n", 11, s->FSTypeSpecificData.fat32.BS_VolumeLabel);
  printf("     FileSystemType: '%.*s'\n", 8, s->FSTypeSpecificData.fat32.BS_FileSystemType);
 }
 printf("\n");
}

variable_types: j int

user_defined_types: struct struct_BPBFAT1216_struct { unsigned char BS_DriveNumber; unsigned char BS_Reserved1; unsigned char BS_BootSig; unsigned int BS_VolumeID; unsigned char BS_VolumeLabel[11]; unsigned char BS_FileSystemType[8]; }

user_defined_types: struct struct_BPBFAT32_struct { unsigned int FATSize; unsigned short ExtFlags; unsigned short FSVersion; unsigned int RootCluster; unsigned short FSInfo; unsigned short BkBootSec; unsigned char Reserved[12]; unsigned char BS_DriveNumber; unsigned char BS_Reserved1; unsigned char BS_BootSig; unsigned int BS_VolumeID; unsigned char BS_VolumeLabel[11]; unsigned char BS_FileSystemType[8]; }


original_code:
void print_sector(unsigned char *sector) {
       int i;
 for(i=0; i<(512-8); i+=8) {
  printf(" %02x%02x%02x%02x %02x%02x%02x%02x        %c%c%c%c %c%c%c%c\n", sector[i], sector[i+1], sector[i+2], sector[i+3], sector[i+4], sector[i+5], sector[i+6], sector[i+7], sector[i], sector[i+1], sector[i+2], sector[i+3], sector[i+4], sector[i+5], sector[i+6], sector[i+7]);
 }
}

variable_types:
sector
unsigned char *

variable_types: i int

return_type: void


decompiled_code:
int __fastcall print_tf_info(unsigned __int8 *a1)
{
__int64 v1; // rsi

puts(" TFInfo Structure\n ----------------");
v1 = *a1;
if ( (_BYTE)v1 )
{
if ( (_BYTE)v1 == 1 )
puts(" Type: FAT32");
else
printf(" Type: UNRECOGNIZED! (0x%02x)\n", v1);
}
else
{
puts(" Type: FAT16");
}
printf("Sectors Per Cluster: %d\n", a1[1]);
printf(" Total Sectors: %d\n", *(unsigned int *)(a1 + 6));
printf(" Reserved Sectors: %d\n", *((unsigned __int16 *)a1 + 5));
return printf(" First Data Sector: %d\n", *(unsigned int *)(a1 + 2));
}

canonical_decompiled_code:
int __fastcall func4(unsigned __int8 *a1)
{
__int64 v1; // rsi

puts(" TFInfo Structure\n ----------------");
v1 = *a1;
if ( (_BYTE)v1 )
{
if ( (_BYTE)v1 == 1 )
puts(" Type: FAT32");
else
printf(" Type: UNRECOGNIZED! (0x%02x)\n", v1);
}
else
{
puts(" Type: FAT16");
}
printf("Sectors Per Cluster: %d\n", a1[1]);
printf(" Total Sectors: %d\n", *(unsigned int *)(a1 + 6));
printf(" Reserved Sectors: %d\n", *((unsigned __int16 *)a1 + 5));
return printf(" First Data Sector: %d\n", *(unsigned int *)(a1 + 2));
}

original_code:
void print_tf_info(TFInfo *t) {
 printf("    TFInfo Structure\n    ----------------\n");
 switch(t->type) {
  case 0: printf("               Type: FAT16\n"); break;
  case 1: printf("               Type: FAT32\n"); break;
  default: printf("               Type: UNRECOGNIZED! (0x%02x)\n", t->type); break;
 }
 printf("Sectors Per Cluster: %d\n", t->sectorsPerCluster);
 printf("      Total Sectors: %d\n", t->totalSectors);
 printf("   Reserved Sectors: %d\n", t->reservedSectors);
 printf("  First Data Sector: %d\n", t->firstDataSector);
}

canonical_original_code:
void print_tf_info(struct struct_tfinfo *t) {
 printf("    TFInfo Structure\n    ----------------\n");
 switch(t->type) {
  case 0: printf("               Type: FAT16\n"); break;
  case 1: printf("               Type: FAT32\n"); break;
  default: printf("               Type: UNRECOGNIZED! (0x%02x)\n", t->type); break;
 }
 printf("Sectors Per Cluster: %d\n", t->sectorsPerCluster);
 printf("      Total Sectors: %d\n", t->totalSectors);
 printf("   Reserved Sectors: %d\n", t->reservedSectors);
 printf("  First Data Sector: %d\n", t->firstDataSector);
}

variable_types: struct struct_tfinfo { unsigned char type; unsigned char sectorsPerCluster; unsigned int firstDataSector; unsigned int totalSectors; unsigned short reservedSectors; unsigned int currentSector; unsigned char sectorFlags; unsigned int rootDirectorySize; unsigned char buffer[512]; } *


decompiled_code:
int __fastcall print_TFFile(unsigned int *a1)
{
puts(" TFFile Structure\n ----------------");
printf(" currentCluster: %d\n", a1[3]);
printf(" currentClusterIdx: %d\n", a1[2]);
printf("parentStartCluster: %d\n", *a1);
printf(" startCluster: %d\n", a1[1]);
printf(" currentSector: %d\n", (unsigned int)*((__int16 *)a1 + 8));
printf(" currentByte: %d\n", (unsigned int)*((__int16 *)a1 + 9));
printf(" pos: %d\n", a1[5]);
printf(" flags: 0x%02x\n", *((unsigned __int8 *)a1 + 24));
return printf(" size: %d bytes\n", *(unsigned int *)((char *)a1 + 27));
}

canonical_decompiled_code:
int __fastcall func5(unsigned int *a1)
{
puts(" TFFile Structure\n ----------------");
printf(" currentCluster: %d\n", a1[3]);
printf(" currentClusterIdx: %d\n", a1[2]);
printf("parentStartCluster: %d\n", *a1);
printf(" startCluster: %d\n", a1[1]);
printf(" currentSector: %d\n", (unsigned int)*((__int16 *)a1 + 8));
printf(" currentByte: %d\n", (unsigned int)*((__int16 *)a1 + 9));
printf(" pos: %d\n", a1[5]);
printf(" flags: 0x%02x\n", *((unsigned __int8 *)a1 + 24));
return printf(" size: %d bytes\n", *(unsigned int *)((char *)a1 + 27));
}

original_code:
void print_TFFile(TFFile *fp) {
 printf("     TFFile Structure\n    ----------------\n");
 printf("    currentCluster: %d\n", fp->currentCluster);
 printf(" currentClusterIdx: %d\n", fp->currentClusterIdx);
 printf("parentStartCluster: %d\n", fp->parentStartCluster);
 printf("      startCluster: %d\n", fp->startCluster);
 printf("     currentSector: %d\n", fp->currentSector);
 printf("       currentByte: %d\n", fp->currentByte);
 printf("               pos: %d\n", fp->pos);
 printf("             flags: 0x%02x\n", fp->flags);
 printf("              size: %d bytes\n", fp->size);
}

canonical_original_code:
void print_TFFile(struct struct_TFFILE *fp) {
 printf("     TFFile Structure\n    ----------------\n");
 printf("    currentCluster: %d\n", fp->currentCluster);
 printf(" currentClusterIdx: %d\n", fp->currentClusterIdx);
 printf("parentStartCluster: %d\n", fp->parentStartCluster);
 printf("      startCluster: %d\n", fp->startCluster);
 printf("     currentSector: %d\n", fp->currentSector);
 printf("       currentByte: %d\n", fp->currentByte);
 printf("               pos: %d\n", fp->pos);
 printf("             flags: 0x%02x\n", fp->flags);
 printf("              size: %d bytes\n", fp->size);
}

variable_types: struct struct_TFFILE { unsigned int parentStartCluster; unsigned int startCluster; unsigned int currentClusterIdx; unsigned int currentCluster; short currentSector; short currentByte; unsigned int pos; unsigned char flags; unsigned char attributes; unsigned char mode; unsigned int size; unsigned char filename[256]; } *


decompiled_code:
int __fastcall print_FatFile83(__int64 a1)
{
puts(" Type: 8.3 Filename");
printf(" Filename: %.*s\n", 8, (const char *)a1);
printf(" Extension: %.*s\n", 3, (const char *)(a1 + 8));
printf(" Attributes: 0x%02x\n", *(unsigned __int8 *)(a1 + 11));
printf("First Cluster: %d\n", *(unsigned __int16 *)(a1 + 26) | (*(unsigned __int16 *)(a1 + 20) << 16));
return printf(
"Creation Time: %d/%d/%d\n",
(unsigned __int16)((*(_WORD *)(a1 + 16) >> 9) + 1980),
(*(unsigned __int16 *)(a1 + 16) >> 5) & 0xF,
2 * (*(_WORD *)(a1 + 16) & 0xFu));
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
puts(" Type: 8.3 Filename");
printf(" Filename: %.*s\n", 8, (const char *)a1);
printf(" Extension: %.*s\n", 3, (const char *)(a1 + 8));
printf(" Attributes: 0x%02x\n", *(unsigned __int8 *)(a1 + 11));
printf("First Cluster: %d\n", *(unsigned __int16 *)(a1 + 26) | (*(unsigned __int16 *)(a1 + 20) << 16));
return printf(
"Creation Time: %d/%d/%d\n",
(unsigned __int16)((*(_WORD *)(a1 + 16) >> 9) + 1980),
(*(unsigned __int16 *)(a1 + 16) >> 5) & 0xF,
2 * (*(_WORD *)(a1 + 16) & 0xFu));
}

original_code:
void print_FatFile83(FatFile83 *entry) {
 printf("         Type: 8.3 Filename\n");
 printf("     Filename: %.*s\n", 8, entry->filename);
 printf("    Extension: %.*s\n", 3, entry->extension);
 printf("   Attributes: 0x%02x\n", entry->attributes);
 printf("First Cluster: %d\n", ((entry->eaIndex & 0xffff) << 16) | ((entry->firstCluster & 0xffff)));
 printf("Creation Time: %d/%d/%d\n", ((entry->creationDate & 0xfe00) >> 9) + 1980, ((entry->creationDate & 0x1e0) >> 5), (entry->creationDate & 0xf)*2);
}

canonical_original_code:
void print_FatFile83(struct struct_FatFile83 *entry) {
 printf("         Type: 8.3 Filename\n");
 printf("     Filename: %.*s\n", 8, entry->filename);
 printf("    Extension: %.*s\n", 3, entry->extension);
 printf("   Attributes: 0x%02x\n", entry->attributes);
 printf("First Cluster: %d\n", ((entry->eaIndex & 0xffff) << 16) | ((entry->firstCluster & 0xffff)));
 printf("Creation Time: %d/%d/%d\n", ((entry->creationDate & 0xfe00) >> 9) + 1980, ((entry->creationDate & 0x1e0) >> 5), (entry->creationDate & 0xf)*2);
}

variable_types: struct struct_FatFile83 { unsigned char filename[8]; unsigned char extension[3]; unsigned char attributes; unsigned char reserved; unsigned char creationTimeMs; unsigned short creationTime; unsigned short creationDate; unsigned short lastAccessTime; unsigned short eaIndex; unsigned short modifiedTime; unsigned short modifiedDate; unsigned short firstCluster; unsigned int fileSize; } *


decompiled_code:
int print_FatFileLFN()
{
return puts(" Type: Long Filename (LFN)");
}

canonical_decompiled_code:
int func7()
{
return puts(" Type: Long Filename (LFN)");
}

original_code:
void print_FatFileLFN(FatFileLFN *entry) {
 printf("         Type: Long Filename (LFN)\n");
}

canonical_original_code:
void print_FatFileLFN(struct struct_FatFileLFN *entry) {
 printf("         Type: Long Filename (LFN)\n");
}

variable_types: struct struct_FatFileLFN { unsigned char sequence_number; unsigned short name1[5]; unsigned char attributes; unsigned char reserved; unsigned char checksum; unsigned short name2[6]; unsigned short firstCluster; unsigned short name3[2]; } *


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

puts(" FatFile Structure\n ---------------");
if ( *(_BYTE *)(a1 + 11) == 15 )
result = print_FatFileLFN();
else
result = print_FatFile83(a1);
return result;
}

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

puts(" FatFile Structure\n ---------------");
if ( *(_BYTE *)(a1 + 11) == 15 )
result = func7();
else
result = func6(a1);
return result;
}

original_code:
void print_FatFileEntry(FatFileEntry *entry) {
 printf("    FatFile Structure\n    ---------------\n");
 if(entry->msdos.attributes == 0x0f) {
  print_FatFileLFN(&(entry->lfn));
 }
 else {
  print_FatFile83(&(entry->msdos));
 }
}

canonical_original_code:
void print_FatFileEntry(union struct_FatFileEntry *entry) {
 printf("    FatFile Structure\n    ---------------\n");
 if(entry->msdos.attributes == 0x0f) {
  print_FatFileLFN(&(entry->lfn));
 }
 else {
  print_FatFile83(&(entry->msdos));
 }
}

variable_types: union struct_FatFileEntry { struct struct_FatFile83 { unsigned char filename[8]; unsigned char extension[3]; unsigned char attributes; unsigned char reserved; unsigned char creationTimeMs; unsigned short creationTime; unsigned short creationDate; unsigned short lastAccessTime; unsigned short eaIndex; unsigned short modifiedTime; unsigned short modifiedDate; unsigned short firstCluster; unsigned int fileSize; } msdos; struct struct_FatFileLFN { unsigned char sequence_number; unsigned short name1[5]; unsigned char attributes; unsigned char reserved; unsigned char checksum; unsigned short name2[6]; unsigned short firstCluster; unsigned short name3[2]; } lfn; } *

user_defined_types: union struct_FatFileEntry { struct struct_FatFile83 msdos; struct struct_FatFileLFN lfn; }


decompiled_code:
__int64 __fastcall test_basic_read(__int64 a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // rbp
__int64 v4; // rbx
int v5; // eax
int v6; // edx
unsigned int v7; // er12
char v9[152]; // [rsp+0h] [rbp-98h] BYREF

v2 = tf_fopen(a1, &unk_16F);
if ( !v2 )
return (unsigned int)-1;
v3 = v2;
v4 = 0LL;
do
{
v5 = tf_fread(&v9[v4], 1LL, v3);
v6 = ++v4;
}
while ( !v5 );
v9[v6] = 0;
v7 = strcmp(v9, a2);
tf_fclose(v3);
if ( v7 )
v7 = -4;
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // rbp
__int64 v4; // rbx
int v5; // eax
int v6; // edx
unsigned int v7; // er12
char v9[152]; // [rsp+0h] [rbp-98h] BYREF

v2 = tf_fopen(a1, &unk_16F);
if ( !v2 )
return (unsigned int)-1;
v3 = v2;
v4 = 0LL;
do
{
v5 = tf_fread(&v9[v4], 1LL, v3);
v6 = ++v4;
}
while ( !v5 );
v9[v6] = 0;
v7 = strcmp(v9, a2);
tf_fclose(v3);
if ( v7 )
v7 = -4;
return v7;
}

original_code:
int test_basic_read(char *input_file, char *expected) {
    TFFile *fp;
    char data[128];
    int size = strlen(expected);
    fp = tf_fopen(input_file, "r");
    int i=0;
    if(fp) {
        while(!tf_fread(&(data[i]), 1, fp)) {i+=1;}
        data[i+1] = '\x00';
        if(strcmp(data, expected)) {
            tf_fclose(fp);
            return -4;
        }
        else {
            tf_fclose(fp);
            return 0;
        }
    }
    else {
        return -1;
    }
}

canonical_original_code:
int test_basic_read(char *input_file, char *expected) {
    struct struct_TFFILE *fp;
    char data[128];
    int size = strlen(expected);
    fp = tf_fopen(input_file, "r");
    int i = 0;
    if(fp) {
        while(!tf_fread(&(data[i]), 1, fp)) {i+=1;}
        data[i+1] = '\x00';
        if(strcmp(data, expected)) {
            tf_fclose(fp);
            return -4;
        }
        else {
            tf_fclose(fp);
            return 0;
        }
    }
    else {
        return -1;
    }
}

variable_types:
expected
char *

variable_types:
data
char [128]

variable_types: size int

return_type: int

user_defined_types: struct struct_TFFILE { unsigned int parentStartCluster; unsigned int startCluster; unsigned int currentClusterIdx; unsigned int currentCluster; short currentSector; short currentByte; unsigned int pos; unsigned char flags; unsigned char attributes; unsigned char mode; unsigned int size; unsigned char filename[256]; }


decompiled_code:
__int64 __fastcall test_basic_write(__int64 a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // rbp
size_t v4; // rax
__int64 result; // rax

v2 = tf_fopen(a1, &unk_171);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
printf("\r\n[TEST] writing data to file: %s", a2);
v4 = strlen(a2);
if ( (int)tf_fwrite(a2, 1LL, v4, v3) <= 0 )
{
tf_fclose(v3);
result = 4294967293LL;
}
else
{
tf_fclose(v3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // rbp
size_t v4; // rax
__int64 result; // rax

v2 = tf_fopen(a1, &unk_171);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
printf("\r\n[TEST] writing data to file: %s", a2);
v4 = strlen(a2);
if ( (int)tf_fwrite(a2, 1LL, v4, v3) <= 0 )
{
tf_fclose(v3);
result = 4294967293LL;
}
else
{
tf_fclose(v3);
result = 0LL;
}
return result;
}

original_code:
int test_basic_write(char *input_file, char *write_string) {
    TFFile *fp;
    int rc;
    fp = tf_fopen(input_file, "w");
    if(fp) {
        printf("\r\n[TEST] writing data to file: %s", write_string);
        rc = tf_fwrite(write_string, 1, strlen(write_string), fp);
        if(rc<1) {
            tf_fclose(fp);
            return -3;
        }
        else {
            tf_fclose(fp);
            return 0;
        }
    }
    else {
        return -1;
    }
}

canonical_original_code:
int test_basic_write(char *input_file, char *write_string) {
    struct struct_TFFILE *fp;
    int rc;
    fp = tf_fopen(input_file, "w");
    if(fp) {
        printf("\r\n[TEST] writing data to file: %s", write_string);
        rc = tf_fwrite(write_string, 1, strlen(write_string), fp);
        if(rc<1) {
            tf_fclose(fp);
            return -3;
        }
        else {
            tf_fclose(fp);
            return 0;
        }
    }
    else {
        return -1;
    }
}

variable_types: rc int


decompiled_code:
__int64 __fastcall test_basic_append(__int64 a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // rbp
size_t v4; // rax
unsigned int v5; // er12

v2 = tf_fopen(a1, "a");
if ( !v2 )
return (unsigned int)-1;
v3 = v2;
v4 = strlen(a2);
v5 = tf_fwrite(a2, 1LL, v4, v3);
tf_fclose(v3);
if ( v5 )
v5 = -3;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // rbp
size_t v4; // rax
unsigned int v5; // er12

v2 = tf_fopen(a1, "a");
if ( !v2 )
return (unsigned int)-1;
v3 = v2;
v4 = strlen(a2);
v5 = tf_fwrite(a2, 1LL, v4, v3);
tf_fclose(v3);
if ( v5 )
v5 = -3;
return v5;
}

original_code:
int test_basic_append(char *input_file, char *write_string) {
    TFFile *fp;
    int rc;
    fp = tf_fopen(input_file, "a");
    if(fp) {
        rc = tf_fwrite(write_string, 1, strlen(write_string), fp);
        if(rc) {
            tf_fclose(fp);
            return -3;
        }
        else {
            tf_fclose(fp);
            return 0;
        }
    }
    else {
        return -1;
    }
}

canonical_original_code:
int test_basic_append(char *input_file, char *write_string) {
    struct struct_TFFILE *fp;
    int rc;
    fp = tf_fopen(input_file, "a");
    if(fp) {
        rc = tf_fwrite(write_string, 1, strlen(write_string), fp);
        if(rc) {
            tf_fclose(fp);
            return -3;
        }
        else {
            tf_fclose(fp);
            return 0;
        }
    }
    else {
        return -1;
    }
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax

printf("\r\nFAT32 Filesystem Test");
printf("\r\n-----------------------");
tf_init("\r\n-----------------------", argv);
printf("\r\n[TEST] Basic LFN write test ");
v3 = test_basic_write((__int64)"/test_longfilename0.txt", "Hello, World!");
if ( v3 )
printf("failed with error code 0x%x", v3);
else
printf(" PASSED.");
printf("\r\n[TEST] Basic LFN read test ");
v4 = test_basic_read((__int64)"/test_longfilename0.txt", "Hello, World!");
if ( v4 )
printf("failed with error code 0x%x", v4);
else
puts(" PASSED.");
v5 = test_basic_write((__int64)"/test0.txt", "Hello, World!");
if ( v5 )
printf("\r\n[TEST] Basic 8.3 write test failed with error code 0x%x", v5);
else
printf("\r\n[TEST] Basic 8.3 write test PASSED.");
v6 = test_basic_read((__int64)"/test0.txt", "Hello, World!");
if ( v6 )
printf("\r\n[TEST] Basic 8.3 read test failed with error code 0x%x", v6);
else
printf("\r\n[TEST] Basic 8.3 read test PASSED.");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax

printf("\r\nFAT32 Filesystem Test");
printf("\r\n-----------------------");
tf_init("\r\n-----------------------", argv);
printf("\r\n[TEST] Basic LFN write test ");
v3 = func1((__int64)"/test_longfilename0.txt", "Hello, World!");
if ( v3 )
printf("failed with error code 0x%x", v3);
else
printf(" PASSED.");
printf("\r\n[TEST] Basic LFN read test ");
v4 = func0((__int64)"/test_longfilename0.txt", "Hello, World!");
if ( v4 )
printf("failed with error code 0x%x", v4);
else
puts(" PASSED.");
v5 = func1((__int64)"/test0.txt", "Hello, World!");
if ( v5 )
printf("\r\n[TEST] Basic 8.3 write test failed with error code 0x%x", v5);
else
printf("\r\n[TEST] Basic 8.3 write test PASSED.");
v6 = func0((__int64)"/test0.txt", "Hello, World!");
if ( v6 )
printf("\r\n[TEST] Basic 8.3 read test failed with error code 0x%x", v6);
else
printf("\r\n[TEST] Basic 8.3 read test PASSED.");
return 0;
}

original_code:
int main(int argc, char **argv) {
    TFFile *fp;
    char data;
    int rc;
    printf("\r\nFAT32 Filesystem Test");
    printf("\r\n-----------------------");
    tf_init();
    printf("\r\n[TEST] Basic LFN write test ") ;
    if(rc = test_basic_write("/test_longfilename0.txt", "Hello, World!")) {
        printf("failed with error code 0x%x", rc) ;
    }else { printf(" PASSED."); }
    printf("\r\n[TEST] Basic LFN read test ") ;
    if(rc = test_basic_read("/test_longfilename0.txt", "Hello, World!")) {
        printf("failed with error code 0x%x", rc) ;
    }else { printf(" PASSED.\n"); }
    if(rc = test_basic_write("/test0.txt", "Hello, World!")) {
        printf("\r\n[TEST] Basic 8.3 write test failed with error code 0x%x", rc);
    }else { printf("\r\n[TEST] Basic 8.3 write test PASSED."); }
    if(rc = test_basic_read("/test0.txt", "Hello, World!")) {
        printf("\r\n[TEST] Basic 8.3 read test failed with error code 0x%x", rc) ;
    }else { printf("\r\n[TEST] Basic 8.3 read test PASSED."); }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    struct struct_TFFILE *fp;
    char data;
    int rc;
    printf("\r\nFAT32 Filesystem Test");
    printf("\r\n-----------------------");
    tf_init();
    printf("\r\n[TEST] Basic LFN write test ") ;
    if(rc = test_basic_write("/test_longfilename0.txt", "Hello, World!")) {
        printf("failed with error code 0x%x", rc) ;
    }else { printf(" PASSED."); }
    printf("\r\n[TEST] Basic LFN read test ") ;
    if(rc = test_basic_read("/test_longfilename0.txt", "Hello, World!")) {
        printf("failed with error code 0x%x", rc) ;
    }else { printf(" PASSED.\n"); }
    if(rc = test_basic_write("/test0.txt", "Hello, World!")) {
        printf("\r\n[TEST] Basic 8.3 write test failed with error code 0x%x", rc);
    }else { printf("\r\n[TEST] Basic 8.3 write test PASSED."); }
    if(rc = test_basic_read("/test0.txt", "Hello, World!")) {
        printf("\r\n[TEST] Basic 8.3 read test failed with error code 0x%x", rc) ;
    }else { printf("\r\n[TEST] Basic 8.3 read test PASSED."); }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: data char


matrixIdentity

decompiled_code:
__int64 __fastcall matrixIdentity(__int64 a1)
{
int i; // edx
__int64 result; // rax
int v3; // xmm0_4

for ( i = 0; i != 4; ++i )
{
for ( result = 0LL; result != 4; ++result )
{
v3 = LC1;
if ( i != (_DWORD)result )
v3 = 0;
*(_DWORD *)(a1 + 4 * result) = v3;
}
a1 += 16LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
int i; // edx
__int64 result; // rax
int v3; // xmm0_4

for ( i = 0; i != 4; ++i )
{
for ( result = 0LL; result != 4; ++result )
{
v3 = LC1;
if ( i != (_DWORD)result )
v3 = 0;
*(_DWORD *)(a1 + 4 * result) = v3;
}
a1 += 16LL;
}
return result;
}

variable_types:
M
float [-1]

variable_types: c int

variable_types: r int


matrixPush

decompiled_code:
__int64 __fastcall matrixPush(const __m128i *a1)
{
int v1; // edx
__int64 result; // rax
__int128 v3; // xmm1

v1 = matrixStackSize + 1;
result = (__int64)matrixStackSize << 6;
*(__int128 *)((char *)&matrixStack + result) = (__int128)_mm_loadu_si128(a1);
v3 = (__int128)_mm_loadu_si128(a1 + 1);
matrixStackSize = v1;
*(__int128 *)((char *)&xmmword_5B0 + result) = v3;
*(__int128 *)((char *)&xmmword_5C0 + result) = (__int128)_mm_loadu_si128(a1 + 2);
*(__int128 *)((char *)&xmmword_5D0 + result) = (__int128)_mm_loadu_si128(a1 + 3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(const __m128i *a1)
{
int v1; // edx
__int64 result; // rax
__int128 v3; // xmm1

v1 = matrixStackSize + 1;
result = (__int64)matrixStackSize << 6;
*(__int128 *)((char *)&matrixStack + result) = (__int128)_mm_loadu_si128(a1);
v3 = (__int128)_mm_loadu_si128(a1 + 1);
matrixStackSize = v1;
*(__int128 *)((char *)&xmmword_5B0 + result) = v3;
*(__int128 *)((char *)&xmmword_5C0 + result) = (__int128)_mm_loadu_si128(a1 + 2);
*(__int128 *)((char *)&xmmword_5D0 + result) = (__int128)_mm_loadu_si128(a1 + 3);
return result;
}

original_code:
void matrixPush(GLfloat M[4*4]) {
  memcpy(matrixStack[matrixStackSize++], M, 4*4*sizeof(GLfloat));
}

canonical_original_code:
void matrixPush(float M[-1]) {
  memcpy(matrixStack[matrixStackSize++], M, 4*4*sizeof(GLfloat));
}


matrixPop

decompiled_code:
__int64 __fastcall matrixPop(__m128i *a1)
{
__int64 result; // rax

result = (__int64)--matrixStackSize << 6;
*a1 = _mm_load_si128((const __m128i *)((char *)&matrixStack + result));
a1[1] = _mm_load_si128((const __m128i *)((char *)&xmmword_5B0 + result));
a1[2] = _mm_load_si128((const __m128i *)((char *)&xmmword_5C0 + result));
a1[3] = _mm_load_si128((const __m128i *)((char *)&xmmword_5D0 + result));
return result;
}

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

result = (__int64)--matrixStackSize << 6;
*a1 = _mm_load_si128((const __m128i *)((char *)&matrixStack + result));
a1[1] = _mm_load_si128((const __m128i *)((char *)&xmmword_5B0 + result));
a1[2] = _mm_load_si128((const __m128i *)((char *)&xmmword_5C0 + result));
a1[3] = _mm_load_si128((const __m128i *)((char *)&xmmword_5D0 + result));
return result;
}

original_code:
void matrixPop(GLfloat M[4*4]) {
  memcpy(M, matrixStack[--matrixStackSize], 4*4*sizeof(GLfloat));
}

canonical_original_code:
void matrixPop(float M[-1]) {
  memcpy(M, matrixStack[--matrixStackSize], 4*4*sizeof(GLfloat));
}


decompiled_code:
__int64 __fastcall get_filename(char *src, char *s)
{
char v2; // al
char *v5; // r12
char *v6; // rdi
char *v7; // rax
char *v8; // rax

v2 = src[11];
if ( v2 == 15 || (v2 & 0x1E) != 0 )
return 0LL;
strncpy(s, src, 8uLL);
if ( (unsigned __int8)((*s & 0xDF) - 65) > 0x19u )
return 0LL;
s[8] = 0;
v5 = strchr(s, 32);
if ( !v5 )
v5 = s + 8;
if ( src[8] != 32 )
{
*v5 = 46;
v6 = v5 + 1;
v5 += 4;
v7 = strncpy(v6, src + 8, 3uLL);
*v5 = 0;
v8 = strchr(v7, 32);
if ( v8 )
v5 = v8;
}
*v5 = 0;
return 1LL;
}

original_code:
int get_filename(uint8_t* dir_entry, char buff[8+1+3+1])
{
    char* curpos;
    char* chkterm;
    if(( ((uint32_t)( ((uint8_t*)(dir_entry)) [0xB] ))) == 0x0f) {
        return 0;
    }
    if((( ((uint32_t)( ((uint8_t*)(dir_entry)) [0xB] ))) & 0x1E) != 0) {
        return 0;
    }
    curpos = buff;
    strncpy( curpos, ((char*)( &((uint8_t*)(dir_entry))[0x0] )), 8);
    if( (('A' <= curpos[0]) && (curpos[0] <= 'Z')) ||
        (('a' <= curpos[0]) && (curpos[0] <= 'z')) ){
        curpos[8] = '\0';
        chkterm = strchr(curpos, ' ');
        if(chkterm != ((void *)0)){
            curpos = chkterm;
        }
        else {
            curpos = &(curpos[8]);
        }
        if((((char*)( &((uint8_t*)(dir_entry))[0x8] )))[0] != ' ') {
            *curpos = '.';
            curpos++;
            strncpy(curpos, ((char*)( &((uint8_t*)(dir_entry))[0x8] )), 3);
            curpos[3] = '\0';
            chkterm = strchr(curpos, ' ');
            if(chkterm != ((void *)0)){
                curpos = chkterm;
            }
            else {
                curpos = &(curpos[3]);
            }
        }
        *curpos = '\0';
    }
    else {
        return 0;
    }
    return 1;
}

canonical_original_code:
int get_filename(unsigned char *dir_entry, char buff[-1])
{
    char *curpos;
    char *chkterm;
    if(( ((unsigned int)( ((unsigned char *)(dir_entry)) [0xB] ))) == 0x0f) {
        return 0;
    }
    if((( ((unsigned int)( ((unsigned char *)(dir_entry)) [0xB] ))) & 0x1E) != 0) {
        return 0;
    }
    curpos = buff;
    strncpy( curpos, ((char *)( &((unsigned char *)(dir_entry))[0x0] )), 8);
    if( (('A' <= curpos[0]) && (curpos[0] <= 'Z')) ||
        (('a' <= curpos[0]) && (curpos[0] <= 'z')) ){
        curpos[8] = '\0';
        chkterm = strchr(curpos, ' ');
        if(chkterm != ((void *)0)){
            curpos = chkterm;
        }
        else {
            curpos = &(curpos[8]);
        }
        if((((char *)( &((unsigned char *)(dir_entry))[0x8] )))[0] != ' ') {
            *curpos = '.';
            curpos++;
            strncpy(curpos, ((char *)( &((unsigned char *)(dir_entry))[0x8] )), 3);
            curpos[3] = '\0';
            chkterm = strchr(curpos, ' ');
            if(chkterm != ((void *)0)){
                curpos = chkterm;
            }
            else {
                curpos = &(curpos[3]);
            }
        }
        *curpos = '\0';
    }
    else {
        return 0;
    }
    return 1;
}

variable_types:
buff
char [-1]

variable_types:
curpos
char *

variable_types:
chkterm
char *


decompiled_code:
__int64 __fastcall getsize_or_loadfile(const char *a1, char *a2, unsigned __int64 a3, int a4)
{
int v4; // er14
int v5; // er12
unsigned int v6; // er13
int v7; // ebp
unsigned int v8; // ebx
char *v9; // rbx
__int64 result; // rax
char *v11; // r15
unsigned int v12; // ebx
unsigned int v13; // ecx
unsigned int v14; // er8
int v15; // eax
unsigned int v16; // eax
unsigned int v17; // eax
unsigned int v18; // ebp
__int16 v19; // bp
unsigned int v20; // ebx
int v21; // eax
int v22; // er12
unsigned int v23; // eax
char *v24; // rdi
char *v25; // rcx
unsigned int s1; // [rsp+8h] [rbp-80h]
unsigned int v27; // [rsp+10h] [rbp-78h]
unsigned int v28; // [rsp+10h] [rbp-78h]
int v29; // [rsp+18h] [rbp-70h]
unsigned int v30; // [rsp+18h] [rbp-70h]
unsigned int v32; // [rsp+1Ch] [rbp-6Ch]
int v34; // [rsp+28h] [rbp-60h]
unsigned int v35; // [rsp+2Ch] [rbp-5Ch]
int v36; // [rsp+38h] [rbp-50h]
char s2[69]; // [rsp+43h] [rbp-45h] BYREF

if ( read_func )
{
if ( (int)read_func((unsigned int)lba_fs_top, 1LL, &readbuff_1) < 0 )
{
printf("ERROR: FAT: can not read BPB. LBA = 0x%08x\n", (unsigned int)lba_fs_top);
result = -1LL;
}
else if ( (unsigned __int8)byte_E3E + ((unsigned __int8)byte_E3F << 8) == 43605 )
{
v34 = lba_fs_top + (unsigned __int8)byte_C4E + ((unsigned __int8)byte_C4F << 8);
v4 = (unsigned __int8)byte_C51 + ((unsigned __int8)byte_C52 << 8);
v5 = v34 + (unsigned __int8)byte_C50 * ((unsigned __int8)byte_C56 + ((unsigned __int8)byte_C57 << 8));
v35 = (unsigned __int8)byte_C4D;
if ( byte_C4D )
{
v29 = ((unsigned __int8)byte_C54 << 8) + (unsigned __int8)byte_C53;
if ( !v29 )
v29 = (unsigned __int8)byte_C60
+ ((unsigned __int8)byte_C63 << 24)
+ ((unsigned __int8)byte_C62 << 16)
+ ((unsigned __int8)byte_C61 << 8);
v6 = 0;
v7 = 0;
if ( v4 )
{
while ( 1 )
{
v8 = v5 + (v7 >> 4);
if ( v8 > v6 )
{
if ( (int)read_func(v8, 1LL, &readbuff_1) < 0 )
{
printf("ERROR: FAT: can not read DIR ENTRY. LBA = 0x%08x\n", v8);
return -1LL;
}
v6 = v5 + (v7 >> 4);
}
v9 = (char *)&readbuff_1 + ((32 * (_WORD)v7) & 0x1E0);
if ( (unsigned int)get_filename(v9, s2) )
{
if ( !strcasecmp(a1, s2) )
break;
}
if ( v4 == ++v7 )
goto LABEL_14;
}
v11 = a2;
v27 = (unsigned __int8)v9[28]
+ ((unsigned __int8)v9[31] << 24)
+ ((unsigned __int8)v9[30] << 16)
+ ((unsigned __int8)v9[29] << 8);
v12 = (unsigned __int8)v9[26] + ((unsigned __int8)v9[27] << 8);
if ( !v12 )
goto LABEL_14;
if ( a4 )
{
if ( a2 )
{
v13 = a3;
s1 = 0;
if ( v27 <= a3 )
v13 = v27;
v28 = v13;
v36 = (v4 >> 4) + v5;
v32 = (_DWORD)a2 + v13;
v30 = (v29 - v36) / v35 + 2;
while ( 1 )
{
result = (unsigned int)((_DWORD)v11 - (_DWORD)a2);
if ( (unsigned int)result >= v28 )
break;
v18 = v36 + v35 * (v12 - 2);
if ( v35 << 9 > v32 - (unsigned int)v11 )
{
v22 = 0;
while ( (int)read_func(v22 + v18, 1LL, &readbuff_1) >= 0 )
{
v23 = v32 - (_DWORD)v11;
if ( v32 - (unsigned int)v11 > 0x200 )
v23 = 512;
if ( v23 < 8 )
{
if ( (v23 & 4) != 0 )
{
*(_DWORD *)v11 = readbuff_1;
*(_DWORD *)&v11[v23 - 4] = *(_DWORD *)((char *)&qword_C38 + v23 + 4);
}
else if ( v23 )
{
*v11 = readbuff_1;
if ( (v23 & 2) != 0 )
*(_WORD *)&v11[v23 - 2] = *(_WORD *)((char *)&qword_C38 + v23 + 6);
}
++v22;
v11 += v23;
if ( (int)v35 <= v22 )
goto LABEL_32;
}
else
{
++v22;
v24 = (char *)((unsigned __int64)(v11 + 8) & 0xFFFFFFFFFFFFFFF8LL);
*(_QWORD *)v11 = readbuff_1;
*(_QWORD *)&v11[v23 - 8] = *(__int64 *)((char *)&qword_C38 + v23);
v25 = v11;
v11 += v23;
qmemcpy(v24, (char *)&readbuff_1 - (v25 - v24), 8LL * ((v23 + (_DWORD)v25 - (_DWORD)v24) >> 3));
if ( (int)v35 <= v22 )
goto LABEL_32;
}
}
printf("ERROR: FAT: can not read DATA. LBA = 0x%08x\n", v22 + v18);
return -1LL;
}
if ( (int)read_func(v18, v35, v11) < 0 )
{
printf("ERROR: FAT: can not read DATA. LBA = 0x%08x\n", v18);
return -1LL;
}
v11 += 512 * v35;
LABEL_32:
if ( v30 <= 0xFF6 )
{
v14 = v34 + ((3 * v12) >> 10);
if ( s1 != v14 )
{
v15 = read_func(v14, 2LL, fat_cache_0);
v14 = v34 + ((3 * v12) >> 10);
if ( v15 < 0 )
{
LABEL_55:
printf("ERROR: FAT: can not read FAT. LBA = 0x%08x\n", v14);
return -1LL;
}
}
v16 = (unsigned __int8)fat_cache_0[((3 * v12) >> 1) & 0x1FF]
+ ((unsigned __int8)fat_cache_0[(((3 * v12) >> 1) & 0x1FF) + 1] << 8);
if ( (v12 & 1) != 0 )
{
v17 = v16 >> 4;
s1 = v14;
}
else
{
s1 = v14;
v17 = v16 & 0xFFF;
}
v12 = v17;
}
else
{
v19 = 2 * v12;
v20 = v12 >> 8;
v14 = v20 + v34;
if ( s1 != v20 + v34 )
{
v21 = read_func(v14, 1LL, fat_cache_0);
v14 = v20 + v34;
if ( v21 < 0 )
goto LABEL_55;
}
s1 = v14;
v12 = (unsigned __int8)fat_cache_0[v19 & 0x1FE]
+ ((unsigned __int8)fat_cache_0[(v19 & 0x1FE) + 1] << 8);
}
if ( v12 > v30 )
return (unsigned int)((_DWORD)v11 - (_DWORD)a2);
}
}
else
{
puts("ERROR: FAT: invalid destination address NULL");
result = -1LL;
}
}
else
{
result = v27;
}
}
else
{
LABEL_14:
printf("ERROR: FAT: file is not found \"%s\"\n", a1);
result = -1LL;
}
}
else
{
puts("ERROR: FAT : invalid FAT format");
printf("ERROR: Top LBA : 0x%08x\n", (unsigned int)lba_fs_top);
printf(
"ERROR: byte / sector : 0x%08x\n",
(unsigned __int8)byte_C4B + ((unsigned __int8)byte_C4C << 8));
printf("ERROR: sector / cluster : 0x%08x\n", (unsigned __int8)byte_C4D);
printf(
"ERROR: no of boot sector : 0x%08x\n",
(unsigned __int8)byte_C4E + ((unsigned __int8)byte_C4F << 8));
printf("ERROR: no of FAT : 0x%08x\n", (unsigned __int8)byte_C50);
printf(
"ERROR: no of directory entry : 0x%08x\n",
(unsigned __int8)byte_C51 + ((unsigned __int8)byte_C52 << 8));
printf(
"ERROR: no of sector : 0x%08x\n",
(unsigned __int8)byte_C53 + ((unsigned __int8)byte_C54 << 8));
printf("ERROR: media descriptor : 0x%08x\n", (unsigned __int8)byte_C55);
printf(
"ERROR: sector / FAT : 0x%08x\n",
(unsigned __int8)byte_C56 + ((unsigned __int8)byte_C57 << 8));
printf(
"ERROR: no of sector over 32MB : 0x%08x\n",
((unsigned __int8)byte_C61 << 8)
+ (unsigned __int8)byte_C60
+ ((unsigned __int8)byte_C62 << 16)
+ ((unsigned __int8)byte_C63 << 24));
printf("ERROR: enhanced record sign : 0x%08x\n", (unsigned __int8)byte_C66);
result = -1LL;
}
}
else
{
puts("ERROR: FAT: invalid BPB");
result = -1LL;
}
}
else
{
puts("ERROR: FAT: not registered read function");
result = -1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, char *a2, unsigned __int64 a3, int a4)
{
int v4; // er14
int v5; // er12
unsigned int v6; // er13
int v7; // ebp
unsigned int v8; // ebx
char *v9; // rbx
__int64 result; // rax
char *v11; // r15
unsigned int v12; // ebx
unsigned int v13; // ecx
unsigned int v14; // er8
int v15; // eax
unsigned int v16; // eax
unsigned int v17; // eax
unsigned int v18; // ebp
__int16 v19; // bp
unsigned int v20; // ebx
int v21; // eax
int v22; // er12
unsigned int v23; // eax
char *v24; // rdi
char *v25; // rcx
unsigned int s1; // [rsp+8h] [rbp-80h]
unsigned int v27; // [rsp+10h] [rbp-78h]
unsigned int v28; // [rsp+10h] [rbp-78h]
int v29; // [rsp+18h] [rbp-70h]
unsigned int v30; // [rsp+18h] [rbp-70h]
unsigned int v32; // [rsp+1Ch] [rbp-6Ch]
int v34; // [rsp+28h] [rbp-60h]
unsigned int v35; // [rsp+2Ch] [rbp-5Ch]
int v36; // [rsp+38h] [rbp-50h]
char s2[69]; // [rsp+43h] [rbp-45h] BYREF

if ( read_func )
{
if ( (int)read_func((unsigned int)lba_fs_top, 1LL, &readbuff_1) < 0 )
{
printf("ERROR: FAT: can not read BPB. LBA = 0x%08x\n", (unsigned int)lba_fs_top);
result = -1LL;
}
else if ( (unsigned __int8)byte_E3E + ((unsigned __int8)byte_E3F << 8) == 43605 )
{
v34 = lba_fs_top + (unsigned __int8)byte_C4E + ((unsigned __int8)byte_C4F << 8);
v4 = (unsigned __int8)byte_C51 + ((unsigned __int8)byte_C52 << 8);
v5 = v34 + (unsigned __int8)byte_C50 * ((unsigned __int8)byte_C56 + ((unsigned __int8)byte_C57 << 8));
v35 = (unsigned __int8)byte_C4D;
if ( byte_C4D )
{
v29 = ((unsigned __int8)byte_C54 << 8) + (unsigned __int8)byte_C53;
if ( !v29 )
v29 = (unsigned __int8)byte_C60
+ ((unsigned __int8)byte_C63 << 24)
+ ((unsigned __int8)byte_C62 << 16)
+ ((unsigned __int8)byte_C61 << 8);
v6 = 0;
v7 = 0;
if ( v4 )
{
while ( 1 )
{
v8 = v5 + (v7 >> 4);
if ( v8 > v6 )
{
if ( (int)read_func(v8, 1LL, &readbuff_1) < 0 )
{
printf("ERROR: FAT: can not read DIR ENTRY. LBA = 0x%08x\n", v8);
return -1LL;
}
v6 = v5 + (v7 >> 4);
}
v9 = (char *)&readbuff_1 + ((32 * (_WORD)v7) & 0x1E0);
if ( (unsigned int)func0(v9, s2) )
{
if ( !strcasecmp(a1, s2) )
break;
}
if ( v4 == ++v7 )
goto LABEL_14;
}
v11 = a2;
v27 = (unsigned __int8)v9[28]
+ ((unsigned __int8)v9[31] << 24)
+ ((unsigned __int8)v9[30] << 16)
+ ((unsigned __int8)v9[29] << 8);
v12 = (unsigned __int8)v9[26] + ((unsigned __int8)v9[27] << 8);
if ( !v12 )
goto LABEL_14;
if ( a4 )
{
if ( a2 )
{
v13 = a3;
s1 = 0;
if ( v27 <= a3 )
v13 = v27;
v28 = v13;
v36 = (v4 >> 4) + v5;
v32 = (_DWORD)a2 + v13;
v30 = (v29 - v36) / v35 + 2;
while ( 1 )
{
result = (unsigned int)((_DWORD)v11 - (_DWORD)a2);
if ( (unsigned int)result >= v28 )
break;
v18 = v36 + v35 * (v12 - 2);
if ( v35 << 9 > v32 - (unsigned int)v11 )
{
v22 = 0;
while ( (int)read_func(v22 + v18, 1LL, &readbuff_1) >= 0 )
{
v23 = v32 - (_DWORD)v11;
if ( v32 - (unsigned int)v11 > 0x200 )
v23 = 512;
if ( v23 < 8 )
{
if ( (v23 & 4) != 0 )
{
*(_DWORD *)v11 = readbuff_1;
*(_DWORD *)&v11[v23 - 4] = *(_DWORD *)((char *)&qword_C38 + v23 + 4);
}
else if ( v23 )
{
*v11 = readbuff_1;
if ( (v23 & 2) != 0 )
*(_WORD *)&v11[v23 - 2] = *(_WORD *)((char *)&qword_C38 + v23 + 6);
}
++v22;
v11 += v23;
if ( (int)v35 <= v22 )
goto LABEL_32;
}
else
{
++v22;
v24 = (char *)((unsigned __int64)(v11 + 8) & 0xFFFFFFFFFFFFFFF8LL);
*(_QWORD *)v11 = readbuff_1;
*(_QWORD *)&v11[v23 - 8] = *(__int64 *)((char *)&qword_C38 + v23);
v25 = v11;
v11 += v23;
qmemcpy(v24, (char *)&readbuff_1 - (v25 - v24), 8LL * ((v23 + (_DWORD)v25 - (_DWORD)v24) >> 3));
if ( (int)v35 <= v22 )
goto LABEL_32;
}
}
printf("ERROR: FAT: can not read DATA. LBA = 0x%08x\n", v22 + v18);
return -1LL;
}
if ( (int)read_func(v18, v35, v11) < 0 )
{
printf("ERROR: FAT: can not read DATA. LBA = 0x%08x\n", v18);
return -1LL;
}
v11 += 512 * v35;
LABEL_32:
if ( v30 <= 0xFF6 )
{
v14 = v34 + ((3 * v12) >> 10);
if ( s1 != v14 )
{
v15 = read_func(v14, 2LL, fat_cache_0);
v14 = v34 + ((3 * v12) >> 10);
if ( v15 < 0 )
{
LABEL_55:
printf("ERROR: FAT: can not read FAT. LBA = 0x%08x\n", v14);
return -1LL;
}
}
v16 = (unsigned __int8)fat_cache_0[((3 * v12) >> 1) & 0x1FF]
+ ((unsigned __int8)fat_cache_0[(((3 * v12) >> 1) & 0x1FF) + 1] << 8);
if ( (v12 & 1) != 0 )
{
v17 = v16 >> 4;
s1 = v14;
}
else
{
s1 = v14;
v17 = v16 & 0xFFF;
}
v12 = v17;
}
else
{
v19 = 2 * v12;
v20 = v12 >> 8;
v14 = v20 + v34;
if ( s1 != v20 + v34 )
{
v21 = read_func(v14, 1LL, fat_cache_0);
v14 = v20 + v34;
if ( v21 < 0 )
goto LABEL_55;
}
s1 = v14;
v12 = (unsigned __int8)fat_cache_0[v19 & 0x1FE]
+ ((unsigned __int8)fat_cache_0[(v19 & 0x1FE) + 1] << 8);
}
if ( v12 > v30 )
return (unsigned int)((_DWORD)v11 - (_DWORD)a2);
}
}
else
{
puts("ERROR: FAT: invalid destination address NULL");
result = -1LL;
}
}
else
{
result = v27;
}
}
else
{
LABEL_14:
printf("ERROR: FAT: file is not found \"%s\"\n", a1);
result = -1LL;
}
}
else
{
puts("ERROR: FAT : invalid FAT format");
printf("ERROR: Top LBA : 0x%08x\n", (unsigned int)lba_fs_top);
printf(
"ERROR: byte / sector : 0x%08x\n",
(unsigned __int8)byte_C4B + ((unsigned __int8)byte_C4C << 8));
printf("ERROR: sector / cluster : 0x%08x\n", (unsigned __int8)byte_C4D);
printf(
"ERROR: no of boot sector : 0x%08x\n",
(unsigned __int8)byte_C4E + ((unsigned __int8)byte_C4F << 8));
printf("ERROR: no of FAT : 0x%08x\n", (unsigned __int8)byte_C50);
printf(
"ERROR: no of directory entry : 0x%08x\n",
(unsigned __int8)byte_C51 + ((unsigned __int8)byte_C52 << 8));
printf(
"ERROR: no of sector : 0x%08x\n",
(unsigned __int8)byte_C53 + ((unsigned __int8)byte_C54 << 8));
printf("ERROR: media descriptor : 0x%08x\n", (unsigned __int8)byte_C55);
printf(
"ERROR: sector / FAT : 0x%08x\n",
(unsigned __int8)byte_C56 + ((unsigned __int8)byte_C57 << 8));
printf(
"ERROR: no of sector over 32MB : 0x%08x\n",
((unsigned __int8)byte_C61 << 8)
+ (unsigned __int8)byte_C60
+ ((unsigned __int8)byte_C62 << 16)
+ ((unsigned __int8)byte_C63 << 24));
printf("ERROR: enhanced record sign : 0x%08x\n", (unsigned __int8)byte_C66);
result = -1LL;
}
}
else
{
puts("ERROR: FAT: invalid BPB");
result = -1LL;
}
}
else
{
puts("ERROR: FAT: not registered read function");
result = -1LL;
}
return result;
}

original_code:
ssize_t getsize_or_loadfile(const char* srcfilename, void* dest, size_t limit, int load_to_dest)
{
    int i;
    uint8_t* destaddr;
    static uint8_t readbuff[512];
    uint32_t lba_cur = 0;
    uint32_t lba_fat_top;
    uint32_t lba_direntry;
    uint32_t lba_data;
    int sector_per_cluster;
    int noof_dir_entry;
    uint32_t noof_cluster;
    uint8_t* dir_entry_cur;
    char filename_cur[8+1+3+1];
    uint32_t cluster_cur;
    uint32_t sizeof_file;
    uint32_t sizeof_read;
    static uint8_t fat_cache[512*2];
    uint32_t lba_fat_last;
    if(read_func == ((void *)0)) {
        printf("ERROR: " "FAT: not registered read function\n");;;
        return -1;
    }
    if((*read_func)(lba_fs_top, 1, readbuff) < 0) {
        printf("ERROR: " "FAT: can not read BPB. LBA = 0x%08x\n", lba_fs_top);;;
        return -1;
    }
    if((( ((uint32_t)( ((uint8_t*)(readbuff)) [0x1FF] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x1FE] )) <<0)) != 0xAA55){
        printf("ERROR: " "FAT: invalid BPB\n");;;
        return -1;
    }
    lba_fat_top = lba_fs_top + (( ((uint32_t)( ((uint8_t*)(readbuff)) [0x0F] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x0E] )) <<0));
    lba_direntry = lba_fat_top + ((( ((uint32_t)( ((uint8_t*)(readbuff)) [0x17] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x16] )) <<0)) * ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x10] ))));
    noof_dir_entry = (( ((uint32_t)( ((uint8_t*)(readbuff)) [0x12] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x11] )) <<0));
    sector_per_cluster = ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x0D] )));
    lba_data = lba_direntry + (noof_dir_entry / (512 / 32));
    if(sector_per_cluster <= 0) {
        printf("ERROR: " "FAT : invalid FAT format\n");;;
        printf("ERROR: " "  Top LBA                : 0x%08x\n", lba_fs_top);;;
        printf("ERROR: " "  byte / sector          : 0x%08x\n", (( ((uint32_t)( ((uint8_t*)(readbuff)) [0x0C] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x0B] )) <<0)));;;
        printf("ERROR: " "  sector / cluster       : 0x%08x\n", ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x0D] ))));;;
        printf("ERROR: " "  no of boot sector      : 0x%08x\n", (( ((uint32_t)( ((uint8_t*)(readbuff)) [0x0F] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x0E] )) <<0)));;;
        printf("ERROR: " "  no of FAT              : 0x%08x\n", ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x10] ))));;;
        printf("ERROR: " "  no of directory entry  : 0x%08x\n", (( ((uint32_t)( ((uint8_t*)(readbuff)) [0x12] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x11] )) <<0)));;;
        printf("ERROR: " "  no of sector           : 0x%08x\n", (( ((uint32_t)( ((uint8_t*)(readbuff)) [0x14] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x13] )) <<0)));;;
        printf("ERROR: " "  media descriptor       : 0x%08x\n", ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x15] ))));;;
        printf("ERROR: " "  sector / FAT           : 0x%08x\n", (( ((uint32_t)( ((uint8_t*)(readbuff)) [0x17] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x16] )) <<0)));;;
        printf("ERROR: " "  no of sector over 32MB : 0x%08x\n", (( ((uint32_t)( ((uint8_t*)(readbuff)) [0x23] )) <<24) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x22] )) <<16) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x21] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x20] )) <<0)));;;
        printf("ERROR: " "  enhanced record sign   : 0x%08x\n", ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x26] ))));;;
        return -1;
    }
    noof_cluster = (((((( ((uint32_t)( ((uint8_t*)(readbuff)) [0x14] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x13] )) <<0)) != 0) ? (( ((uint32_t)( ((uint8_t*)(readbuff)) [0x14] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x13] )) <<0)) : (( ((uint32_t)( ((uint8_t*)(readbuff)) [0x23] )) <<24) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x22] )) <<16) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x21] )) <<8) + ( ((uint32_t)( ((uint8_t*)(readbuff)) [0x20] )) <<0))) - lba_data) / sector_per_cluster) + 2;
    lba_cur = 0;
    cluster_cur = 0;
    for(i = 0; i < noof_dir_entry; i++) {
        if(lba_cur < lba_direntry + ((32*i)/512)) {
            lba_cur = lba_direntry + ((32*i)/512);
            if((*read_func)(lba_cur, 1, readbuff) < 0) {
                printf("ERROR: " "FAT: can not read DIR ENTRY. LBA = 0x%08x\n", lba_cur);;;
                return -1;
            }
        }
        dir_entry_cur = &(readbuff[32 * (i%(512/32))]);
        if(0 != get_filename(dir_entry_cur, filename_cur)) {
            if(0 == strcasecmp(srcfilename, filename_cur)) {
                cluster_cur = (( ((uint32_t)( ((uint8_t*)(dir_entry_cur)) [0x1B] )) <<8) + ( ((uint32_t)( ((uint8_t*)(dir_entry_cur)) [0x1A] )) <<0));
                sizeof_file = (( ((uint32_t)( ((uint8_t*)(dir_entry_cur)) [0x1F] )) <<24) + ( ((uint32_t)( ((uint8_t*)(dir_entry_cur)) [0x1E] )) <<16) + ( ((uint32_t)( ((uint8_t*)(dir_entry_cur)) [0x1D] )) <<8) + ( ((uint32_t)( ((uint8_t*)(dir_entry_cur)) [0x1C] )) <<0));
                break;
            }
        }
    }
    if(cluster_cur == 0) {
        printf("ERROR: " "FAT: file is not found \"%s\"\n", srcfilename);;;
        return -1;
    }
    if(load_to_dest == 0) {
        return sizeof_file;
    }
    if(dest == ((void *)0)) {
        printf("ERROR: " "FAT: invalid destination address NULL\n");;;
        return -1;
    }
    if(limit < sizeof_file) {
        sizeof_file = limit;
    }
    lba_fat_last = 0;
    destaddr = (uint8_t*)dest;
    while(((uint32_t)destaddr - (uint32_t)dest) < sizeof_file) {
        lba_cur = lba_data + ((cluster_cur-2) * sector_per_cluster);
        if((sector_per_cluster * 512) <= (sizeof_file - ((uint32_t)destaddr - (uint32_t)dest))){
            if((*read_func)(lba_cur, sector_per_cluster, destaddr) < 0) {
                printf("ERROR: " "FAT: can not read DATA. LBA = 0x%08x\n", lba_cur);;;
                return -1;
            }
            destaddr += (sector_per_cluster * 512);
        }
        else {
            for(i = 0; i < sector_per_cluster; i++) {
                if((*read_func)(lba_cur, 1, readbuff) < 0) {
                    printf("ERROR: " "FAT: can not read DATA. LBA = 0x%08x\n", lba_cur);;;
                    return -1;
                }
                if( 512 <= (sizeof_file - ((uint32_t)destaddr - (uint32_t)dest)) ) {
                    sizeof_read = 512;
                }
                else {
                    sizeof_read = (sizeof_file - ((uint32_t)destaddr - (uint32_t)dest));
                }
                memcpy(destaddr, readbuff, sizeof_read);
                destaddr += sizeof_read;
                lba_cur++;
            }
        }
        if(noof_cluster <= 0xFF6) {
            lba_cur = lba_fat_top + (((cluster_cur * 3) / 2) / 512);
            if(lba_fat_last != lba_cur) {
                if((*read_func)(lba_cur, 2, fat_cache) < 0) {
                    printf("ERROR: " "FAT: can not read FAT. LBA = 0x%08x\n", lba_cur);;;
                    return -1;
                }
                lba_fat_last = lba_cur;
            }
            if((cluster_cur & 0x001) == 0) {
                cluster_cur = ( (uint32_t)(fat_cache[(((cluster_cur * 3) / 2) % 512)+1] << 8) +
                                (uint32_t)(fat_cache[(((cluster_cur * 3) / 2) % 512)]) ) & 0xFFF;
            }
            else {
                cluster_cur = (( (uint32_t)(fat_cache[(((cluster_cur * 3) / 2) % 512)+1] << 8) +
                                 (uint32_t)(fat_cache[(((cluster_cur * 3) / 2) % 512)]) ) & 0xFFF0) >> 4;
            }
        }
        else {
            lba_cur = lba_fat_top + ((cluster_cur * 2) / 512);
            if(lba_fat_last != lba_cur) {
                if((*read_func)(lba_cur, 1, fat_cache) < 0) {
                    printf("ERROR: " "FAT: can not read FAT. LBA = 0x%08x\n", lba_cur);;;
                    return -1;
                }
                lba_fat_last = lba_cur;
            }
            cluster_cur = ( (uint32_t)(fat_cache[((cluster_cur * 2) % 512)+1] << 8) +
                            (uint32_t)(fat_cache[((cluster_cur * 2) % 512)]) );
        }
        if(noof_cluster < cluster_cur){
            break;
        }
    }
    return (ssize_t)((uint32_t)destaddr - (uint32_t)dest);
}

canonical_original_code:
long getsize_or_loadfile(char *srcfilename, void *dest, unsigned long limit, int load_to_dest)
{
    int i;
    unsigned char *destaddr;
    static unsigned char readbuff[512];
    unsigned int lba_cur = 0;
    unsigned int lba_fat_top;
    unsigned int lba_direntry;
    unsigned int lba_data;
    int sector_per_cluster;
    int noof_dir_entry;
    unsigned int noof_cluster;
    unsigned char *dir_entry_cur;
    char filename_cur[-1];
    unsigned int cluster_cur;
    unsigned int sizeof_file;
    unsigned int sizeof_read;
    static unsigned char fat_cache[-1];
    unsigned int lba_fat_last;
    if(read_func == ((void *)0)) {
        printf("ERROR: " "FAT: not registered read function\n");;;
        return -1;
    }
    if((*read_func)(lba_fs_top, 1, readbuff) < 0) {
        printf("ERROR: " "FAT: can not read BPB. LBA = 0x%08x\n", lba_fs_top);;;
        return -1;
    }
    if((( ((unsigned int)( ((unsigned char *)(readbuff)) [0x1FF] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x1FE] )) <<0)) != 0xAA55){
        printf("ERROR: " "FAT: invalid BPB\n");;;
        return -1;
    }
    lba_fat_top = lba_fs_top + (( ((unsigned int)( ((unsigned char *)(readbuff)) [0x0F] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x0E] )) <<0));
    lba_direntry = lba_fat_top + ((( ((unsigned int)( ((unsigned char *)(readbuff)) [0x17] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x16] )) <<0)) * ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x10] ))));
    noof_dir_entry = (( ((unsigned int)( ((unsigned char *)(readbuff)) [0x12] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x11] )) <<0));
    sector_per_cluster = ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x0D] )));
    lba_data = lba_direntry + (noof_dir_entry / (512 / 32));
    if(sector_per_cluster <= 0) {
        printf("ERROR: " "FAT : invalid FAT format\n");;;
        printf("ERROR: " "  Top LBA                : 0x%08x\n", lba_fs_top);;;
        printf("ERROR: " "  byte / sector          : 0x%08x\n", (( ((unsigned int)( ((unsigned char *)(readbuff)) [0x0C] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x0B] )) <<0)));;;
        printf("ERROR: " "  sector / cluster       : 0x%08x\n", ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x0D] ))));;;
        printf("ERROR: " "  no of boot sector      : 0x%08x\n", (( ((unsigned int)( ((unsigned char *)(readbuff)) [0x0F] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x0E] )) <<0)));;;
        printf("ERROR: " "  no of FAT              : 0x%08x\n", ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x10] ))));;;
        printf("ERROR: " "  no of directory entry  : 0x%08x\n", (( ((unsigned int)( ((unsigned char *)(readbuff)) [0x12] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x11] )) <<0)));;;
        printf("ERROR: " "  no of sector           : 0x%08x\n", (( ((unsigned int)( ((unsigned char *)(readbuff)) [0x14] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x13] )) <<0)));;;
        printf("ERROR: " "  media descriptor       : 0x%08x\n", ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x15] ))));;;
        printf("ERROR: " "  sector / FAT           : 0x%08x\n", (( ((unsigned int)( ((unsigned char *)(readbuff)) [0x17] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x16] )) <<0)));;;
        printf("ERROR: " "  no of sector over 32MB : 0x%08x\n", (( ((unsigned int)( ((unsigned char *)(readbuff)) [0x23] )) <<24) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x22] )) <<16) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x21] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x20] )) <<0)));;;
        printf("ERROR: " "  enhanced record sign   : 0x%08x\n", ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x26] ))));;;
        return -1;
    }
    noof_cluster = (((((( ((unsigned int)( ((unsigned char *)(readbuff)) [0x14] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x13] )) <<0)) != 0) ? (( ((unsigned int)( ((unsigned char *)(readbuff)) [0x14] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x13] )) <<0)) : (( ((unsigned int)( ((unsigned char *)(readbuff)) [0x23] )) <<24) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x22] )) <<16) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x21] )) <<8) + ( ((unsigned int)( ((unsigned char *)(readbuff)) [0x20] )) <<0))) - lba_data) / sector_per_cluster) + 2;
    lba_cur = 0;
    cluster_cur = 0;
    for(i = 0; i < noof_dir_entry; i++) {
        if(lba_cur < lba_direntry + ((32*i)/512)) {
            lba_cur = lba_direntry + ((32*i)/512);
            if((*read_func)(lba_cur, 1, readbuff) < 0) {
                printf("ERROR: " "FAT: can not read DIR ENTRY. LBA = 0x%08x\n", lba_cur);;;
                return -1;
            }
        }
        dir_entry_cur = &(readbuff[32 * (i%(512/32))]);
        if(0 != get_filename(dir_entry_cur, filename_cur)) {
            if(0 == strcasecmp(srcfilename, filename_cur)) {
                cluster_cur = (( ((unsigned int)( ((unsigned char *)(dir_entry_cur)) [0x1B] )) <<8) + ( ((unsigned int)( ((unsigned char *)(dir_entry_cur)) [0x1A] )) <<0));
                sizeof_file = (( ((unsigned int)( ((unsigned char *)(dir_entry_cur)) [0x1F] )) <<24) + ( ((unsigned int)( ((unsigned char *)(dir_entry_cur)) [0x1E] )) <<16) + ( ((unsigned int)( ((unsigned char *)(dir_entry_cur)) [0x1D] )) <<8) + ( ((unsigned int)( ((unsigned char *)(dir_entry_cur)) [0x1C] )) <<0));
                break;
            }
        }
    }
    if(cluster_cur == 0) {
        printf("ERROR: " "FAT: file is not found \"%s\"\n", srcfilename);;;
        return -1;
    }
    if(load_to_dest == 0) {
        return sizeof_file;
    }
    if(dest == ((void *)0)) {
        printf("ERROR: " "FAT: invalid destination address NULL\n");;;
        return -1;
    }
    if(limit < sizeof_file) {
        sizeof_file = limit;
    }
    lba_fat_last = 0;
    destaddr = (unsigned char *)dest;
    while(((unsigned int)destaddr - (unsigned int)dest) < sizeof_file) {
        lba_cur = lba_data + ((cluster_cur-2) * sector_per_cluster);
        if((sector_per_cluster * 512) <= (sizeof_file - ((unsigned int)destaddr - (unsigned int)dest))){
            if((*read_func)(lba_cur, sector_per_cluster, destaddr) < 0) {
                printf("ERROR: " "FAT: can not read DATA. LBA = 0x%08x\n", lba_cur);;;
                return -1;
            }
            destaddr += (sector_per_cluster * 512);
        }
        else {
            for(i = 0; i < sector_per_cluster; i++) {
                if((*read_func)(lba_cur, 1, readbuff) < 0) {
                    printf("ERROR: " "FAT: can not read DATA. LBA = 0x%08x\n", lba_cur);;;
                    return -1;
                }
                if( 512 <= (sizeof_file - ((unsigned int)destaddr - (unsigned int)dest)) ) {
                    sizeof_read = 512;
                }
                else {
                    sizeof_read = (sizeof_file - ((unsigned int)destaddr - (unsigned int)dest));
                }
                memcpy(destaddr, readbuff, sizeof_read);
                destaddr += sizeof_read;
                lba_cur++;
            }
        }
        if(noof_cluster <= 0xFF6) {
            lba_cur = lba_fat_top + (((cluster_cur * 3) / 2) / 512);
            if(lba_fat_last != lba_cur) {
                if((*read_func)(lba_cur, 2, fat_cache) < 0) {
                    printf("ERROR: " "FAT: can not read FAT. LBA = 0x%08x\n", lba_cur);;;
                    return -1;
                }
                lba_fat_last = lba_cur;
            }
            if((cluster_cur & 0x001) == 0) {
                cluster_cur = ( (unsigned int)(fat_cache[(((cluster_cur * 3) / 2) % 512)+1] << 8) +
                                (unsigned int)(fat_cache[(((cluster_cur * 3) / 2) % 512)]) ) & 0xFFF;
            }
            else {
                cluster_cur = (( (unsigned int)(fat_cache[(((cluster_cur * 3) / 2) % 512)+1] << 8) +
                                 (unsigned int)(fat_cache[(((cluster_cur * 3) / 2) % 512)]) ) & 0xFFF0) >> 4;
            }
        }
        else {
            lba_cur = lba_fat_top + ((cluster_cur * 2) / 512);
            if(lba_fat_last != lba_cur) {
                if((*read_func)(lba_cur, 1, fat_cache) < 0) {
                    printf("ERROR: " "FAT: can not read FAT. LBA = 0x%08x\n", lba_cur);;;
                    return -1;
                }
                lba_fat_last = lba_cur;
            }
            cluster_cur = ( (unsigned int)(fat_cache[((cluster_cur * 2) % 512)+1] << 8) +
                            (unsigned int)(fat_cache[((cluster_cur * 2) % 512)]) );
        }
        if(noof_cluster < cluster_cur){
            break;
        }
    }
    return (long)((unsigned int)destaddr - (unsigned int)dest);
}

variable_types:
srcfilename
char *

variable_types:
dest
void *

variable_types:
limit
unsigned long

variable_types: i int

variable_types:
destaddr
unsigned char *

variable_types:
readbuff
unsigned char [512]

return_type: long


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

result = 0LL;
if ( !read_func )
{
read_func = a1;
result = 1LL;
lba_fs_top = a2;
}
return result;
}

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

result = 0LL;
if ( !read_func )
{
read_func = a1;
result = 1LL;
lba_fs_top = a2;
}
return result;
}

original_code:
int fat_init(read_callback func, uint32_t lba_top)
{
    int ret = 0;
    if(read_func == ((void *)0)){
        read_func = func;
        lba_fs_top = lba_top;
        ret = 1;
    }
    return ret;
}

canonical_original_code:
int fat_init(int (*func)(unsigned int start, int blocks, unsigned char *buf), unsigned int lba_top)
{
    int ret = 0;
    if(read_func == ((void *)0)){
        read_func = func;
        lba_fs_top = lba_top;
        ret = 1;
    }
    return ret;
}

variable_types: int (*)(unsigned int start, int blocks, unsigned char *buf)

variable_types: ret int


decompiled_code:
void fat_term()
{
read_func = 0LL;
lba_fs_top = 0;
}

canonical_decompiled_code:
void func3()
{
read_func = 0LL;
lba_fs_top = 0;
}

original_code:
void fat_term(void)
{
    read_func = ((void *)0);
    lba_fs_top = 0;
}

canonical_original_code:
void fat_term()
{
    read_func = ((void *)0);
    lba_fs_top = 0;
}


decompiled_code:
__int64 __fastcall fat_getsize(const char *a1)
{
return getsize_or_loadfile(a1, 0LL, 0LL, 0);
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
return func1(a1, 0LL, 0LL, 0);
}

original_code:
ssize_t fat_getsize(const char* srcfilename)
{
    return getsize_or_loadfile(srcfilename, ((void *)0), 0, 0);
}

canonical_original_code:
long fat_getsize(char *srcfilename)
{
    return getsize_or_loadfile(srcfilename, ((void *)0), 0, 0);
}


decompiled_code:
__int64 __fastcall fat_loadfile(const char *a1, char *a2, unsigned __int64 a3)
{
return getsize_or_loadfile(a1, a2, a3, 1);
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1, char *a2, unsigned __int64 a3)
{
return func1(a1, a2, a3, 1);
}

original_code:
ssize_t fat_loadfile(const char* srcfilename, void* dest, size_t limit)
{
    return getsize_or_loadfile(srcfilename, dest, limit, 1);
}

canonical_original_code:
long fat_loadfile(char *srcfilename, void *dest, unsigned long limit)
{
    return getsize_or_loadfile(srcfilename, dest, limit, 1);
}


decompiled_code:
__int64 __fastcall lba_read_emulation(int a1, unsigned int a2, void *a3)
{
int v3; // er14
__off_t v6; // rsi
unsigned int v7; // er12
ssize_t v8; // rax

v3 = lba_emulation_fd;
if ( lba_emulation_fd < 0 )
{
a2 = -1;
fprintf(stderr, "%s(): ERROR: not initialized yet\n", "lba_read_emulation");
}
else
{
v6 = (unsigned int)(a1 << 9);
if ( lseek(lba_emulation_fd, v6, 0) == -1 )
{
a2 = -1;
fprintf(stderr, "%s(): ERROR: lseek failed for offset %d\n", "lba_read_emulation", (unsigned int)v6);
}
else
{
v7 = a2 << 9;
v8 = read(v3, a3, (int)(a2 << 9));
if ( a2 << 9 != v8 )
{
a2 = -1;
fprintf(stderr, "%s(): ERROR: read() failed for %d, returned %d\n", "lba_read_emulation", v7, v8);
}
}
}
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, unsigned int a2, void *a3)
{
int v3; // er14
__off_t v6; // rsi
unsigned int v7; // er12
ssize_t v8; // rax

v3 = lba_emulation_fd;
if ( lba_emulation_fd < 0 )
{
a2 = -1;
fprintf(stderr, "%s(): ERROR: not initialized yet\n", "lba_read_emulation");
}
else
{
v6 = (unsigned int)(a1 << 9);
if ( lseek(lba_emulation_fd, v6, 0) == -1 )
{
a2 = -1;
fprintf(stderr, "%s(): ERROR: lseek failed for offset %d\n", "lba_read_emulation", (unsigned int)v6);
}
else
{
v7 = a2 << 9;
v8 = read(v3, a3, (int)(a2 << 9));
if ( a2 << 9 != v8 )
{
a2 = -1;
fprintf(stderr, "%s(): ERROR: read() failed for %d, returned %d\n", "lba_read_emulation", v7, v8);
}
}
}
return a2;
}

original_code:
int lba_read_emulation(uint32_t lba_start, int lba_blocks, uint8_t* buf)
{
    off_t lseek_ret;
    ssize_t read_ret;
    if ( lba_emulation_fd < 0 ) {
        fprintf(stderr, "%s(): ERROR: " "not initialized yet" "\n", __func__);
        return -1;
    }
    lseek_ret = lseek(lba_emulation_fd, lba_start*512, 0);
    if ( lseek_ret == (off_t)-1) {
        fprintf(stderr, "%s(): ERROR: " "lseek failed for offset %d" "\n", __func__, lba_start*512);
        return -1;
    }
    read_ret = read(lba_emulation_fd, buf, lba_blocks*512);
    if ( read_ret != lba_blocks*512 ) {
        fprintf(stderr, "%s(): ERROR: " "read() failed for %d, returned %d" "\n", __func__, lba_blocks*512, read_ret);
        return -1;
    }
    return lba_blocks;
}

canonical_original_code:
int lba_read_emulation(unsigned int lba_start, int lba_blocks, unsigned char *buf)
{
    long lseek_ret;
    long read_ret;
    if ( lba_emulation_fd < 0 ) {
        fprintf(stderr, "%s(): ERROR: " "not initialized yet" "\n", __func__);
        return -1;
    }
    lseek_ret = lseek(lba_emulation_fd, lba_start*512, 0);
    if ( lseek_ret == (off_t)-1) {
        fprintf(stderr, "%s(): ERROR: " "lseek failed for offset %d" "\n", __func__, lba_start*512);
        return -1;
    }
    read_ret = read(lba_emulation_fd, buf, lba_blocks*512);
    if ( read_ret != lba_blocks*512 ) {
        fprintf(stderr, "%s(): ERROR: " "read() failed for %d, returned %d" "\n", __func__, lba_blocks*512, read_ret);
        return -1;
    }
    return lba_blocks;
}

variable_types:
buf
unsigned char *


decompiled_code:
__int64 __fastcall lba_read_emulation_start(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
int *v4; // rax
char *v5; // rax

v1 = open(a1, 0);
v2 = v1;
if ( v1 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
fprintf(stderr, "%s(): ERROR: File open failed for %s (%s)\n", "lba_read_emulation_start", a1, v5);
}
else
{
lba_emulation_fd = v1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
int *v4; // rax
char *v5; // rax

v1 = open(a1, 0);
v2 = v1;
if ( v1 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
fprintf(stderr, "%s(): ERROR: File open failed for %s (%s)\n", "lba_read_emulation_start", a1, v5);
}
else
{
lba_emulation_fd = v1;
}
return v2;
}

original_code:
int lba_read_emulation_start(char* filename)
{
    int fd = open(filename, 00);
    if ( fd >= 0 ) {
        lba_emulation_fd = fd;
    }
    else {
        fprintf(stderr, "%s(): ERROR: " "File open failed for %s (%s)" "\n", __func__, filename, strerror((*__errno_location ())));
    }
    return fd;
}

canonical_original_code:
int lba_read_emulation_start(char *filename)
{
    int fd = open(filename, 00);
    if ( fd >= 0 ) {
        lba_emulation_fd = fd;
    }
    else {
        fprintf(stderr, "%s(): ERROR: " "File open failed for %s (%s)" "\n", __func__, filename, strerror((*__errno_location ())));
    }
    return fd;
}

variable_types:
filename
char *

variable_types: fd int


decompiled_code:
int lba_read_emulation_end()
{
int result; // eax

result = close(lba_emulation_fd);
lba_emulation_fd = -1;
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax

result = close(lba_emulation_fd);
lba_emulation_fd = -1;
return result;
}

original_code:
void lba_read_emulation_end(void)
{
    close(lba_emulation_fd);
    lba_emulation_fd = -1;
    return;
}

canonical_original_code:
void lba_read_emulation_end()
{
    close(lba_emulation_fd);
    lba_emulation_fd = -1;
    return;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rcx
__int64 v4; // rax
int v5; // er12
const char *v6; // rbp
int v7; // eax
__int64 v8; // r12
void *v9; // r13
int v10; // eax
char v12; // dl
int v13; // eax

if ( argc <= 0 )
goto LABEL_22;
if ( argc == 1 )
goto LABEL_23;
v3 = argv[1];
if ( *v3 != 45 )
{
v4 = 1LL;
v5 = 1;
goto LABEL_5;
}
v12 = v3[1];
v4 = 2LL;
if ( v12 == 114 )
{
v5 = 0;
}
else
{
v5 = 1;
if ( v12 != 115 )
{
if ( v12 != 104 )
{
fprintf(stderr, "%s(): ERROR: Unknown option: %s\n", "main");
goto LABEL_17;
}
LABEL_22:
puts(
"Usage: tinyfat [OPTION] FAT 8_3_FILENAME\n"
" Reads a file named '8_3_FILENAME' from FAT12/16 partiton 'FAT'.\n"
" Only traditional 8.3 file on the topmost directory is supported.\n"
" If a option is '-r'(read), the content of the file is written to\n"
" standard output.\n"
" If no option given, -s is the default\n"
"\n"
"OPTIONS\n"
" -h Show this help.\n"
" -r Read the file.\n"
" -s Get the file size in bytes.\n");
exit(1);
}
}
LABEL_5:
if ( (int)v4 + 1 >= argc )
{
LABEL_23:
fprintf(stderr, "%s(): ERROR: Not enough argument. Use '-h' option for help.\n", "main");
exit(1);
}
v6 = argv[v4 + 1];
if ( (int)lba_read_emulation_start(argv[v4]) < 0 )
goto LABEL_17;
if ( (int)fat_init(lba_read_emulation, 0LL) < 0 )
fprintf(stderr, "%s(): ERROR: fat_init() returned error\n", "main");
if ( v5 != 1 )
{
v7 = fat_getsize(v6);
if ( v7 >= 0 )
{
v8 = v7;
v9 = malloc(v7);
v10 = fat_loadfile(v6, v9, v8);
write(1, v9, v10);
free(v9);
goto LABEL_12;
}
fprintf(stderr, "%s(): ERROR: Failed to get filesize for %s\n", "main");
LABEL_17:
exit(1);
}
v13 = fat_getsize(v6);
if ( v13 < 0 )
fprintf(stderr, "%s(): ERROR: \n", "main");
else
printf("%s: %dbytes\n", v6, (unsigned int)v13);
LABEL_12:
fat_term();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rcx
__int64 v4; // rax
int v5; // er12
const char *v6; // rbp
int v7; // eax
__int64 v8; // r12
void *v9; // r13
int v10; // eax
char v12; // dl
int v13; // eax

if ( argc <= 0 )
goto LABEL_22;
if ( argc == 1 )
goto LABEL_23;
v3 = argv[1];
if ( *v3 != 45 )
{
v4 = 1LL;
v5 = 1;
goto LABEL_5;
}
v12 = v3[1];
v4 = 2LL;
if ( v12 == 114 )
{
v5 = 0;
}
else
{
v5 = 1;
if ( v12 != 115 )
{
if ( v12 != 104 )
{
fprintf(stderr, "%s(): ERROR: Unknown option: %s\n", "main");
goto LABEL_17;
}
LABEL_22:
puts(
"Usage: tinyfat [OPTION] FAT 8_3_FILENAME\n"
" Reads a file named '8_3_FILENAME' from FAT12/16 partiton 'FAT'.\n"
" Only traditional 8.3 file on the topmost directory is supported.\n"
" If a option is '-r'(read), the content of the file is written to\n"
" standard output.\n"
" If no option given, -s is the default\n"
"\n"
"OPTIONS\n"
" -h Show this help.\n"
" -r Read the file.\n"
" -s Get the file size in bytes.\n");
exit(1);
}
}
LABEL_5:
if ( (int)v4 + 1 >= argc )
{
LABEL_23:
fprintf(stderr, "%s(): ERROR: Not enough argument. Use '-h' option for help.\n", "main");
exit(1);
}
v6 = argv[v4 + 1];
if ( (int)func1(argv[v4]) < 0 )
goto LABEL_17;
if ( (int)fat_init(lba_read_emulation, 0LL) < 0 )
fprintf(stderr, "%s(): ERROR: fat_init() returned error\n", "main");
if ( v5 != 1 )
{
v7 = fat_getsize(v6);
if ( v7 >= 0 )
{
v8 = v7;
v9 = malloc(v7);
v10 = fat_loadfile(v6, v9, v8);
write(1, v9, v10);
free(v9);
goto LABEL_12;
}
fprintf(stderr, "%s(): ERROR: Failed to get filesize for %s\n", "main");
LABEL_17:
exit(1);
}
v13 = fat_getsize(v6);
if ( v13 < 0 )
fprintf(stderr, "%s(): ERROR: \n", "main");
else
printf("%s: %dbytes\n", v6, (unsigned int)v13);
LABEL_12:
fat_term();
return 0;
}

original_code:
int main(int argc, char** argv)
{
    char* fat_partition;
    char* filename_8_3;
    int whattodo;
    int argstart = 1;
    if ( argc < 1 ) {
        puts(HELP);
        exit(1);
    }
    if ( argc >= 2 ) {
        if ( argv[1][0] == '-' ) {
            switch (argv[1][1]) {
                case 'r': whattodo = DO_READFILE; break;
                case 's': whattodo = DO_GETSIZE; break;
                case 'h': puts(HELP);
                    exit(1);
                    break;
                default:
                    fprintf(stderr, "%s(): ERROR: " "Unknown option: %s" "\n", __func__, argv[1]);
                    exit(1);
            }
            argstart++;
        } else {
            whattodo = DO_GETSIZE;
        }
    }
    if (argc >= argstart + 2) {
        fat_partition = argv[argstart++];
        filename_8_3 = argv[argstart++];
    }
    else {
        fprintf(stderr, "%s(): ERROR: " "Not enough argument. Use '-h' option for help." "\n", __func__);
        exit(1);
    }
    int rc;
    rc = lba_read_emulation_start(fat_partition);
    if ( rc < 0 ) {
        exit(1);
    }
    rc = fat_init(&lba_read_emulation, 0);
    if ( rc < 0 ) {
        fprintf(stderr, "%s(): ERROR: " "fat_init() returned error" "\n", __func__);
    }
    switch (whattodo) {
        case DO_GETSIZE:
            {
                int size = fat_getsize(filename_8_3);
                if ( size < 0 ) {
                    fprintf(stderr, "%s(): ERROR: " "" "\n", __func__);
                } else {
                    printf("%s: %dbytes\n", filename_8_3, size);
                }
            }
            break;
        case DO_READFILE:
            {
                int size = fat_getsize(filename_8_3);
                if ( size < 0 ) {
                    fprintf(stderr, "%s(): ERROR: " "Failed to get filesize for %s" "\n", __func__, filename_8_3);
                    exit(1);
                }
                char* buf = malloc(size);
                int loadsize = fat_loadfile(filename_8_3, buf, size);
                write(1, buf, loadsize);
                free(buf);
            }
            break;
        default:
            break;
    }
    fat_term();
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    char *fat_partition;
    char *filename_8_3;
    int whattodo;
    int argstart = 1;
    if ( argc < 1 ) {
        puts(HELP);
        exit(1);
    }
    if ( argc >= 2 ) {
        if ( argv[1][0] == '-' ) {
            switch (argv[1][1]) {
                case 'r': whattodo = DO_READFILE; break;
                case 's': whattodo = DO_GETSIZE; break;
                case 'h': puts(HELP);
                    exit(1);
                    break;
                default:
                    fprintf(stderr, "%s(): ERROR: " "Unknown option: %s" "\n", __func__, argv[1]);
                    exit(1);
            }
            argstart++;
        } else {
            whattodo = DO_GETSIZE;
        }
    }
    if (argc >= argstart + 2) {
        fat_partition = argv[argstart++];
        filename_8_3 = argv[argstart++];
    }
    else {
        fprintf(stderr, "%s(): ERROR: " "Not enough argument. Use '-h' option for help." "\n", __func__);
        exit(1);
    }
    int rc;
    rc = lba_read_emulation_start(fat_partition);
    if ( rc < 0 ) {
        exit(1);
    }
    rc = fat_init(&lba_read_emulation, 0);
    if ( rc < 0 ) {
        fprintf(stderr, "%s(): ERROR: " "fat_init() returned error" "\n", __func__);
    }
    switch (whattodo) {
        case DO_GETSIZE:
            {
                int size = fat_getsize(filename_8_3);
                if ( size < 0 ) {
                    fprintf(stderr, "%s(): ERROR: " "" "\n", __func__);
                } else {
                    printf("%s: %dbytes\n", filename_8_3, size);
                }
            }
            break;
        case DO_READFILE:
            {
                int size = fat_getsize(filename_8_3);
                if ( size < 0 ) {
                    fprintf(stderr, "%s(): ERROR: " "Failed to get filesize for %s" "\n", __func__, filename_8_3);
                    exit(1);
                }
                char *buf = malloc(size);
                int loadsize = fat_loadfile(filename_8_3, buf, size);
                write(1, buf, loadsize);
                free(buf);
            }
            break;
        default:
            break;
    }
    fat_term();
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: whattodo int

variable_types: argstart int

variable_types: rc int

variable_types: size int

variable_types:
buf
char *

variable_types: loadsize int


ToUpper

repo: ryuever/C-language

decompiled_code:
void __fastcall ToUpper(char *a1)
{
__int64 v1; // rbx
char *v2; // rbp
const __int32_t **v3; // rdx

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = a1;
v3 = __ctype_toupper_loc();
do
{
*v2++ = (*v3)[v1];
v1 = *v2;
}
while ( (_BYTE)v1 );
}
}

canonical_decompiled_code:
void __fastcall func7(char *a1)
{
__int64 v1; // rbx
char *v2; // rbp
const __int32_t **v3; // rdx

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = a1;
v3 = __ctype_toupper_loc();
do
{
*v2++ = (*v3)[v1];
v1 = *v2;
}
while ( (_BYTE)v1 );
}
}

original_code:
void ToUpper(char * str)
{
  while (*str)
    {
      *str = toupper(*str);
      str++;
    }
}

canonical_original_code:
void ToUpper(char *str)
{
  while (*str)
    {
      *str = toupper(*str);
      str++;
    }
}

variable_types:
str
char *


Transpose

decompiled_code:
void __fastcall Transpose(char *a1)
{
__int64 v1; // rbx
_BYTE *v2; // rbp
const unsigned __int16 **v3; // r12
unsigned __int16 v4; // ax

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = a1;
v3 = __ctype_b_loc();
do
{
while ( 1 )
{
v4 = (*v3)[v1];
if ( (v4 & 0x200) == 0 )
break;
*v2 = (*__ctype_toupper_loc())[v1];
LABEL_4:
v1 = (char)*++v2;
if ( !(_BYTE)v1 )
return;
}
if ( (v4 & 0x100) == 0 )
goto LABEL_4;
*v2++ = (*__ctype_tolower_loc())[v1];
v1 = (char)*v2;
}
while ( (_BYTE)v1 );
}
}

canonical_decompiled_code:
void __fastcall func8(char *a1)
{
__int64 v1; // rbx
_BYTE *v2; // rbp
const unsigned __int16 **v3; // r12
unsigned __int16 v4; // ax

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = a1;
v3 = __ctype_b_loc();
do
{
while ( 1 )
{
v4 = (*v3)[v1];
if ( (v4 & 0x200) == 0 )
break;
*v2 = (*__ctype_toupper_loc())[v1];
LABEL_4:
v1 = (char)*++v2;
if ( !(_BYTE)v1 )
return;
}
if ( (v4 & 0x100) == 0 )
goto LABEL_4;
*v2++ = (*__ctype_tolower_loc())[v1];
v1 = (char)*v2;
}
while ( (_BYTE)v1 );
}
}

original_code:
void Transpose(char * str)
{
  while (*str)
    {
      if (((*__ctype_b_loc ())[(int) ((*str))] & (unsigned short int) _ISlower))
        *str = toupper(*str);
      else if (((*__ctype_b_loc ())[(int) ((*str))] & (unsigned short int) _ISupper))
        *str = tolower(*str);
      str++;
    }
}

canonical_original_code:
void Transpose(char *str)
{
  while (*str)
    {
      if (((*__ctype_b_loc ())[(int) ((*str))] & (unsigned short) _ISlower))
        *str = toupper(*str);
      else if (((*__ctype_b_loc ())[(int) ((*str))] & (unsigned short) _ISupper))
        *str = tolower(*str);
      str++;
    }
}


decompiled_code:
__int64 __fastcall func_01(unsigned int a1)
{
printf("function func_01 : argument is %d \n", a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1)
{
printf("function func_01 : argument is %d \n", a1);
return 0LL;
}

original_code:
int func_01 (int a){
  printf("function func_01 : argument is %d \n",a);
  return 0;
}

canonical_original_code:
int func_01(int a){
  printf("function func_01 : argument is %d \n",a);
  return 0;
}

variable_types: a int


show

decompiled_code:
int __fastcall show(void (__fastcall *a1)(const char *), const char *a2)
{
a1(a2);
return puts(a2);
}

canonical_decompiled_code:
int __fastcall func10(void (__fastcall *a1)(const char *), const char *a2)
{
a1(a2);
return puts(a2);
}

original_code:
void show(void (* fp)(char *), char * str)
{
  (*fp)(str);
  printf("%s\n",str);
}

canonical_original_code:
void show(void (*fp)(char *), char *str)
{
  (*fp)(str);
  printf("%s\n",str);
}

variable_types:
fp
void (*)(char *)


decompiled_code:
int __fastcall func_02(void (__fastcall *a1)(_QWORD), unsigned int a2)
{
puts("\nBeginning of func_02.");
a1(a2);
return puts("Ending of func_02.");
}

canonical_decompiled_code:
int __fastcall func11(void (__fastcall *a1)(_QWORD), unsigned int a2)
{
puts("\nBeginning of func_02.");
a1(a2);
return puts("Ending of func_02.");
}

original_code:
void func_02 (int (* ptr)(int a),int b){
  printf("\nBeginning of func_02.\n");
  (*ptr)(b);
  printf("Ending of func_02.\n");
}

canonical_original_code:
void func_02(int (*ptr)(int a), int b){
  printf("\nBeginning of func_02.\n");
  (*ptr)(b);
  printf("Ending of func_02.\n");
}

variable_types:
ptr
int (*)(int a)

variable_types: b int


decompiled_code:
int __fastcall func_03(void (__fastcall **a1)(_QWORD), unsigned int a2)
{
void (__fastcall **v2)(_QWORD); // rbx

v2 = a1;
puts("\nBeginning of func_03.");
do
(*v2++)(a2);
while ( a1 + 3 != v2 );
return puts("Ending of func_02.");
}

canonical_decompiled_code:
int __fastcall func12(void (__fastcall **a1)(_QWORD), unsigned int a2)
{
void (__fastcall **v2)(_QWORD); // rbx

v2 = a1;
puts("\nBeginning of func_03.");
do
(*v2++)(a2);
while ( a1 + 3 != v2 );
return puts("Ending of func_02.");
}

original_code:
void func_03 ( int(* ptr[3])(int a),int b){
  printf("\nBeginning of func_03.\n");
  int i;
  for (i = 0; i<3 ; i++, ptr++){
    (*ptr)(b);
  }
  printf("Ending of func_02.\n");
}

canonical_original_code:
void func_03(int (*ptr[3])(int a), int b){
  printf("\nBeginning of func_03.\n");
  int i;
  for (i = 0; i<3 ; i++, ptr++){
    (*ptr)(b);
  }
  printf("Ending of func_02.\n");
}

variable_types:
ptr
int (*[3])(int a)

variable_types: i int


decompiled_code:
__int64 __fastcall func_04(unsigned int a1, __int64 a2)
{
printf("\nBeginning of func_04, and int argument is %d.\n", a1);
puts("Ending of func_04.");
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned int a1, __int64 a2)
{
printf("\nBeginning of func_04, and int argument is %d.\n", a1);
puts("Ending of func_04.");
return a2;
}

original_code:
def_int_int func_04(int a, def_int_int b){
  printf("\nBeginning of func_04, and int argument is %d.\n",a);
  printf("Ending of func_04.\n");
  return b;
}

canonical_original_code:
int (*func_04(int a, int (*b)(int)))(int){
  printf("\nBeginning of func_04, and int argument is %d.\n",a);
  printf("Ending of func_04.\n");
  return b;
}

variable_types:
b
int (*)(int)

return_type: int (*)(int)


decompiled_code:
__int64 __fastcall func_05(unsigned int a1, void (__fastcall *a2)(__int64))
{
printf("\nBeginning of func_04, and int argument is %d.\n", a1);
a2(4LL);
puts("Ending of func_05.");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned int a1, void (__fastcall *a2)(__int64))
{
printf("\nBeginning of func_04, and int argument is %d.\n", a1);
a2(4LL);
puts("Ending of func_05.");
return 0LL;
}

original_code:
int * func_05(int a, def_int_int b){
  printf("\nBeginning of func_04, and int argument is %d.\n",a);
  int m = 4;
  int * i = &m;
  b(m);
  printf("Ending of func_05.\n");
  return i;
}

canonical_original_code:
int *func_05(int a, int (*b)(int)){
  printf("\nBeginning of func_04, and int argument is %d.\n",a);
  int m = 4;
  int *i = &m;
  b(m);
  printf("Ending of func_05.\n");
  return i;
}

variable_types: m int

variable_types:
i
int *

return_type: int *


decompiled_code:
__int64 __fastcall func_06(unsigned int *a1, unsigned int a2)
{
printf("%d,%d", *a1, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned int *a1, unsigned int a2)
{
printf("%d,%d", *a1, a2);
return 0LL;
}

original_code:
int func_06(int * a, int b){
  printf("%d,%d",*a,b);
  return 0;
}

canonical_original_code:
int func_06(int *a, int b){
  printf("%d,%d",*a,b);
  return 0;
}

variable_types:
a
int *


decompiled_code:
__int64 __fastcall const_func_01(unsigned int a1)
{
printf("\nBeginning of const_func1, and int argument is %d.\n", a1);
puts("Ending of const_func1.");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned int a1)
{
printf("\nBeginning of const_func1, and int argument is %d.\n", a1);
puts("Ending of const_func1.");
return 0LL;
}

original_code:
int const_func_01(int a){
  printf("\nBeginning of const_func1, and int argument is %d.\n",a);
  printf("Ending of const_func1.\n");
  return 0;
}


decompiled_code:
__int64 __fastcall const_func_02(unsigned int a1)
{
printf("\nBeginning of const_func2, and int argument is %d.\n", a1);
puts("Ending of const_func2.");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned int a1)
{
printf("\nBeginning of const_func2, and int argument is %d.\n", a1);
puts("Ending of const_func2.");
return 0LL;
}

original_code:
int const_func_02(int a){
  printf("\nBeginning of const_func2, and int argument is %d.\n",a);
  printf("Ending of const_func2.\n");
  return 0;
}


main

variable_types: temp int

variable_types:
str
char []

variable_types:
pfun
void (*)(char *)

variable_types: choice int

variable_types:
m
int (*)(int)

variable_types: c int

variable_types:
test
char **


fatal

decompiled_code:
void __noreturn fatal()
{
exit(1);
}

canonical_decompiled_code:
void __noreturn func7()
{
exit(1);
}

original_code:
void fatal (){
  exit(1);
}

canonical_original_code:
void fatal(){
  exit(1);
}


decompiled_code:
int __fastcall function_1(const char *a1)
{
return printf("From function_1:\n x=%d, y=%f\n", (unsigned int)x, *(double *)&y);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
return printf("From function_1:\n x=%d, y=%f\n", (unsigned int)x, *(double *)&y);
}

original_code:
void function_1()
{
  printf("From function_1:\n  x=%d, y=%f\n", x, y);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8

function_1((const char *)argc, argv, envp);
v3 = *(double *)&y;
printf("Within the main block:\n x=%d, y=%f\n", 4321LL, *(double *)&y);
function_1("Within the main block:\n x=%d, y=%f\n", 4321LL, v3);
printf("Within the nested block:\n x=%d, y=%f\n", 4321LL, 7.654321);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8

func7((const char *)argc, argv, envp);
v3 = *(double *)&y;
printf("Within the main block:\n x=%d, y=%f\n", 4321LL, *(double *)&y);
func7("Within the main block:\n x=%d, y=%f\n", 4321LL, v3);
printf("Within the nested block:\n x=%d, y=%f\n", 4321LL, 7.654321);
return 0;
}

original_code:
int main()
{
  int x = 4321;
  function_1();
  printf("Within the main block:\n  x=%d, y=%f\n", x, y);
  {
    double y = 7.654321;
    function_1();
    printf("Within the nested block:\n  x=%d, y=%f\n", x, y);
  }
  return 0;
}

variable_types: x int

variable_types: y double


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
timediff(argc, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func15(argc, argv, envp);
return 0;
}

original_code:
int
main()
{
    timediff();
    return 0;
}

canonical_original_code:
int main()
{
    timediff();
    return 0;
}


canonical_decompiled_code:
char *func7()
{
return ttyname(0);
}

original_code:
static char *
get_tty_name(void)
{
  return ttyname(0);
}

canonical_original_code:
static char *get_tty_name()
{
  return ttyname(0);
}

return_type: char *


decompiled_code:
char *get_filename()
{
char *v0; // r13
char *v1; // r12
char *v2; // rax
__uid_t v3; // eax

v0 = (char *)malloc(0xFFuLL);
v1 = get_tty_name();
while ( 1 )
{
v2 = strchr(v1, 47);
if ( !v2 )
break;
*v2 = 95;
}
v3 = getuid();
sprintf(v0, "/tmp/baps1_%u_%s", v3, v1);
return v0;
}

canonical_decompiled_code:
char *func8()
{
char *v0; // r13
char *v1; // r12
char *v2; // rax
__uid_t v3; // eax

v0 = (char *)malloc(0xFFuLL);
v1 = func7();
while ( 1 )
{
v2 = strchr(v1, 47);
if ( !v2 )
break;
*v2 = 95;
}
v3 = getuid();
sprintf(v0, "/tmp/baps1_%u_%s", v3, v1);
return v0;
}

original_code:
static char *
get_filename(void)
{
  char *filename = malloc(sizeof(char) * 255);
  char *tty = get_tty_name();
  char *pch;
  while ((pch = strchr(tty, '/')))
    *pch = '_';
  sprintf(filename, "/tmp/baps1_%u_%s", getuid(), tty);
  return filename;
}

canonical_original_code:
static char *get_filename()
{
  char *filename = malloc(sizeof(char) * 255);
  char *tty = get_tty_name();
  char *pch;
  while ((pch = strchr(tty, '/')))
    *pch = '_';
  sprintf(filename, "/tmp/baps1_%u_%s", getuid(), tty);
  return filename;
}

variable_types:
filename
char *

variable_types:
tty
char *

variable_types:
pch
char *


decompiled_code:
__int64 __fastcall get_previous_time(const char *a1)
{
__int64 result; // rax

if ( access(a1, 4) )
result = 0LL;
else
result = load_previous_time(a1);
return result;
}

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

if ( access(a1, 4) )
result = 0LL;
else
result = func9(a1);
return result;
}

original_code:
static time_t
get_previous_time(char *filename)
{
  time_t prev;
  if (access(filename, 4) == 0) {
    prev = load_previous_time(filename);
  }
  else {
    prev = 0;
  }
  return prev;
}

canonical_original_code:
static long get_previous_time(char *filename)
{
  long prev;
  if (access(filename, 4) == 0) {
    prev = load_previous_time(filename);
  }
  else {
    prev = 0;
  }
  return prev;
}

variable_types: prev long


decompiled_code:
int get_tty_number()
{
char *v0; // rax
char *v1; // rax

v0 = get_tty_name();
v1 = strrchr(v0, 47);
return atoi(v1 + 1);
}

canonical_decompiled_code:
int func11()
{
char *v0; // rax
char *v1; // rax

v0 = func7();
v1 = strrchr(v0, 47);
return atoi(v1 + 1);
}

original_code:
static int
get_tty_number(void)
{
  char *tty = get_tty_name();
  char *num = strrchr(tty, '/') + 1;
  return atoi(num);
}

canonical_original_code:
static int get_tty_number()
{
  char *tty = get_tty_name();
  char *num = strrchr(tty, '/') + 1;
  return atoi(num);
}

variable_types:
num
char *


decompiled_code:
__int64 get_tty_type()
{
char *v0; // rax

v0 = get_tty_name();
strtok(v0, "/");
return (unsigned __int8)*strtok(0LL, "/");
}

canonical_decompiled_code:
__int64 func12()
{
char *v0; // rax

v0 = func7();
strtok(v0, "/");
return (unsigned __int8)*strtok(0LL, "/");
}

original_code:
static char
get_tty_type(void)
{
  char *tty = get_tty_name();
  strtok(tty, "/");
  return strtok(((void *)0), "/")[0];
}

canonical_original_code:
static char get_tty_type()
{
  char *tty = get_tty_name();
  strtok(tty, "/");
  return strtok(((void *)0), "/")[0];
}

return_type: char


original_code:
static void
print_time_since(time_t lasttime)
{
  double h, m, s;
  time_t now;
  time(&now);
  s = difftime(now, lasttime);
  if (s >= 60.0) {
    m = floor(s / 60.0);
    s -= m * 60;
    if (m >= 60.0) {
      h = floor(m / 60.0);
      m -= h * 60;
      printf("%.0lfh", h);
    }
    printf("%.0lfm", m);
  }
  printf("%.0lfs", s);
}

canonical_original_code:
static void print_time_since(long lasttime)
{
  double h; double m; double s;
  long now;
  time(&now);
  s = difftime(now, lasttime);
  if (s >= 60.0) {
    m = floor(s / 60.0);
    s -= m * 60;
    if (m >= 60.0) {
      h = floor(m / 60.0);
      m -= h * 60;
      printf("%.0lfh", h);
    }
    printf("%.0lfm", m);
  }
  printf("%.0lfs", s);
}

variable_types: lasttime long

variable_types: h double

variable_types: m double

variable_types: s double

variable_types: now long


timediff

decompiled_code:
void timediff()
{
char *v0; // rbp
__int64 v1; // r12
unsigned int v2; // er13
char v3; // al

v0 = get_filename();
v1 = get_previous_time(v0);
v2 = get_tty_number();
v3 = get_tty_type();
printf("%c%d:", (unsigned int)v3, v2);
if ( v1 <= 0 )
puts("new");
else
print_time_since(v1);
save_current_time_isra_0(v0);
free(v0);
}

canonical_decompiled_code:
void func15()
{
char *v0; // rbp
__int64 v1; // r12
unsigned int v2; // er13
char v3; // al

v0 = func8();
v1 = func10(v0);
v2 = func11();
v3 = func12();
printf("%c%d:", (unsigned int)v3, v2);
if ( v1 <= 0 )
puts("new");
else
func13(v1);
func14(v0);
free(v0);
}

original_code:
void
timediff(void)
{
  char *filename = get_filename();
  time_t lasttime = get_previous_time(filename);
  printf("%c%d:", get_tty_type(), get_tty_number());
  if (lasttime > 0) {
    print_time_since(lasttime);
  }
  else {
    puts("new");
  }
  save_current_time(filename);
  free(filename);
}

canonical_original_code:
void timediff()
{
  char *filename = get_filename();
  long lasttime = get_previous_time(filename);
  printf("%c%d:", get_tty_type(), get_tty_number());
  if (lasttime > 0) {
    print_time_since(lasttime);
  }
  else {
    puts("new");
  }
  save_current_time(filename);
  free(filename);
}