checkDir

decompiled_code:
__int64 __fastcall checkDir(__int64 a1, __int64 a2, int a3, unsigned int a4)
{
__int64 result; // rax
int v6; // er14
unsigned int v7; // ebx
int v8; // er15
__int64 v9; // rdi
unsigned int *v10; // r13
unsigned int v11; // eax
unsigned int v12; // eax
size_t v13; // rax
unsigned int v14; // esi
void *v15; // rsp
__int64 v16; // rdi
unsigned int v17; // eax
__int64 v18; // rdx
__int64 v19; // rcx
unsigned int v20; // eax
size_t v21; // rax
unsigned int v22; // esi
void *v23; // rsp
__int64 v24; // rdi
unsigned int v25; // eax
__int64 v26; // rdx
__int64 v27; // rcx
unsigned int v28; // eax
unsigned int v29; // eax
char v30; // [rsp+0h] [rbp-50h]
char v31; // [rsp+1h] [rbp-4Fh]
char v32; // [rsp+2h] [rbp-4Eh]
unsigned int *v33; // [rsp+8h] [rbp-48h]
__int64 v34; // [rsp+10h] [rbp-40h]
unsigned int v35; // [rsp+18h] [rbp-38h]
int v36; // [rsp+1Ch] [rbp-34h]

result = *(unsigned int *)(a1 + 4);
v34 = a1;
v35 = a4;
v36 = result;
if ( (int)result > 0 )
{
v6 = 0;
v7 = a3 + 1;
v8 = 0;
do
{
while ( 1 )
{
++v8;
v10 = (unsigned int *)(a2 + v6);
if ( v8 == 1 )
{
if ( *((_BYTE *)v10 + 6) != 1 )
goto LABEL_8;
v33 = v10 + 2;
v13 = strnlen((const char *)v10 + 8, 1uLL);
v14 = v13;
v15 = alloca(v13 + 9);
*(&v30 + v13) = 0;
if ( (_DWORD)v13 )
{
v16 = (__int64)v33;
v17 = 0;
do
{
v18 = v17++;
*(&v30 + v18) = *(_BYTE *)(v16 + v18);
}
while ( v17 < v14 );
}
if ( v30 == 46 && !v31 )
{
v19 = *v10;
if ( (_DWORD)v19 == v7 )
{
v20 = localNo(v7);
checkDirInode(v20, v7);
goto LABEL_5;
}
printf(
"Error: Partition %d Dir Inode %d: . has wrong dir->inode %d, change to itself\n",
partitionNumber,
v7,
v19);
}
else
{
LABEL_8:
printf("Error: Partition %d Dir Inode %d: first dir is not ., change to .\n", partitionNumber, v7);
*((_BYTE *)v10 + 6) = 1;
*((_BYTE *)v10 + 8) = 46;
}
goto LABEL_9;
}
if ( v8 == 2 )
break;
v9 = *v10;
if ( (_DWORD)v9 )
goto LABEL_14;
LABEL_5:
result = *((unsigned __int16 *)v10 + 2);
v6 += result;
if ( v36 <= v6 )
return result;
}
if ( *((_BYTE *)v10 + 6) == 2 )
{
v33 = v10 + 2;
v21 = strnlen((const char *)v10 + 8, 2uLL);
v22 = v21;
v23 = alloca(v21 + 9);
*(&v30 + v21) = 0;
if ( (_DWORD)v21 )
{
v24 = (__int64)v33;
v25 = 0;
do
{
v26 = v25++;
*(&v30 + v26) = *(_BYTE *)(v24 + v26);
}
while ( v25 < v22 );
}
if ( v30 == 46 && v31 == 46 && !v32 )
{
v27 = *v10;
v9 = v35;
if ( (_DWORD)v27 != v35 )
{
printf(
"Error: Partition %d Dir Inode %d: .. has wrong dir->inode %d, change to its parent\n",
partitionNumber,
v7,
v27);
v28 = v35;
*v10 = v35;
v29 = localNo(v28);
checkDirInode(v29, v7);
goto LABEL_10;
}
LABEL_14:
v12 = localNo(v9);
checkDirInode(v12, v7);
goto LABEL_5;
}
}
printf("Error: Partition %d Dir Inode %d: second dir is not .., change to ..\n", partitionNumber, v7);
*((_BYTE *)v10 + 6) = 2;
*((_WORD *)v10 + 4) = 11822;
LABEL_9:
*v10 = v7;
v11 = localNo(v7);
checkDirInode(v11, v7);
LABEL_10:
writeDisk(
(unsigned int)(v6 + (start << 9) + blockSize * *(_DWORD *)(v34 + 40)),
*((unsigned __int16 *)v10 + 2),
a2 + v6);
result = *((unsigned __int16 *)v10 + 2);
v6 += result;
}
while ( v36 > v6 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, int a3, unsigned int a4)
{
__int64 result; // rax
int v6; // er14
unsigned int v7; // ebx
int v8; // er15
__int64 v9; // rdi
unsigned int *v10; // r13
unsigned int v11; // eax
unsigned int v12; // eax
size_t v13; // rax
unsigned int v14; // esi
void *v15; // rsp
__int64 v16; // rdi
unsigned int v17; // eax
__int64 v18; // rdx
__int64 v19; // rcx
unsigned int v20; // eax
size_t v21; // rax
unsigned int v22; // esi
void *v23; // rsp
__int64 v24; // rdi
unsigned int v25; // eax
__int64 v26; // rdx
__int64 v27; // rcx
unsigned int v28; // eax
unsigned int v29; // eax
char v30; // [rsp+0h] [rbp-50h]
char v31; // [rsp+1h] [rbp-4Fh]
char v32; // [rsp+2h] [rbp-4Eh]
unsigned int *v33; // [rsp+8h] [rbp-48h]
__int64 v34; // [rsp+10h] [rbp-40h]
unsigned int v35; // [rsp+18h] [rbp-38h]
int v36; // [rsp+1Ch] [rbp-34h]

result = *(unsigned int *)(a1 + 4);
v34 = a1;
v35 = a4;
v36 = result;
if ( (int)result > 0 )
{
v6 = 0;
v7 = a3 + 1;
v8 = 0;
do
{
while ( 1 )
{
++v8;
v10 = (unsigned int *)(a2 + v6);
if ( v8 == 1 )
{
if ( *((_BYTE *)v10 + 6) != 1 )
goto LABEL_8;
v33 = v10 + 2;
v13 = strnlen((const char *)v10 + 8, 1uLL);
v14 = v13;
v15 = alloca(v13 + 9);
*(&v30 + v13) = 0;
if ( (_DWORD)v13 )
{
v16 = (__int64)v33;
v17 = 0;
do
{
v18 = v17++;
*(&v30 + v18) = *(_BYTE *)(v16 + v18);
}
while ( v17 < v14 );
}
if ( v30 == 46 && !v31 )
{
v19 = *v10;
if ( (_DWORD)v19 == v7 )
{
v20 = localNo(v7);
func0(v20, v7);
goto LABEL_5;
}
printf(
"Error: Partition %d Dir Inode %d: . has wrong dir->inode %d, change to itself\n",
partitionNumber,
v7,
v19);
}
else
{
LABEL_8:
printf("Error: Partition %d Dir Inode %d: first dir is not ., change to .\n", partitionNumber, v7);
*((_BYTE *)v10 + 6) = 1;
*((_BYTE *)v10 + 8) = 46;
}
goto LABEL_9;
}
if ( v8 == 2 )
break;
v9 = *v10;
if ( (_DWORD)v9 )
goto LABEL_14;
LABEL_5:
result = *((unsigned __int16 *)v10 + 2);
v6 += result;
if ( v36 <= v6 )
return result;
}
if ( *((_BYTE *)v10 + 6) == 2 )
{
v33 = v10 + 2;
v21 = strnlen((const char *)v10 + 8, 2uLL);
v22 = v21;
v23 = alloca(v21 + 9);
*(&v30 + v21) = 0;
if ( (_DWORD)v21 )
{
v24 = (__int64)v33;
v25 = 0;
do
{
v26 = v25++;
*(&v30 + v26) = *(_BYTE *)(v24 + v26);
}
while ( v25 < v22 );
}
if ( v30 == 46 && v31 == 46 && !v32 )
{
v27 = *v10;
v9 = v35;
if ( (_DWORD)v27 != v35 )
{
printf(
"Error: Partition %d Dir Inode %d: .. has wrong dir->inode %d, change to its parent\n",
partitionNumber,
v7,
v27);
v28 = v35;
*v10 = v35;
v29 = localNo(v28);
func0(v29, v7);
goto LABEL_10;
}
LABEL_14:
v12 = localNo(v9);
func0(v12, v7);
goto LABEL_5;
}
}
printf("Error: Partition %d Dir Inode %d: second dir is not .., change to ..\n", partitionNumber, v7);
*((_BYTE *)v10 + 6) = 2;
*((_WORD *)v10 + 4) = 11822;
LABEL_9:
*v10 = v7;
v11 = localNo(v7);
func0(v11, v7);
LABEL_10:
writeDisk(
(unsigned int)(v6 + (start << 9) + blockSize * *(_DWORD *)(v34 + 40)),
*((unsigned __int16 *)v10 + 2),
a2 + v6);
result = *((unsigned __int16 *)v10 + 2);
v6 += result;
}
while ( v36 > v6 );
}
return result;
}

original_code:
void checkDir(struct ext2_inode *inode, unsigned char *buf, int localId, int parent) {
 int changeDir = 0;
 int n = 0;
 int tempSize = 0;
 int totalSize = inode->i_size;
 struct ext2_dir_entry_2 *dir;
 while (tempSize < totalSize) {
  changeDir = 0;
  dir = (struct ext2_dir_entry_2 *)(buf + tempSize);
  ++n;
  if (n == 1) {
   if (dir->name_len == 1 && strcmp((__extension__ ({ const char *__old = (dir->name); size_t __len = strnlen (__old, (dir->name_len)); char *__new = (char *) __builtin_alloca (__len + 1); __new[__len] = '\0'; (char *) memcpy (__new, __old, __len); })), ".") == 0) {
    if (dir->inode != localId + 1) {
     printf("Error: Partition %d Dir Inode %d: . has wrong dir->inode %d, change to itself\n", partitionNumber, localId + 1, dir->inode);
     dir->inode = localId + 1;
     changeDir = 1;
    }
   } else {
    printf("Error: Partition %d Dir Inode %d: first dir is not ., change to .\n", partitionNumber, localId + 1);
    dir->name_len = 1;
    dir->name[0] = '.';
    dir->inode = localId + 1;
    changeDir = 1;
   }
   checkDirInode(localNo(dir->inode), localId + 1);
  } else if (n == 2) {
   if (dir->name_len == 2 && strcmp((__extension__ ({ const char *__old = (dir->name); size_t __len = strnlen (__old, (dir->name_len)); char *__new = (char *) __builtin_alloca (__len + 1); __new[__len] = '\0'; (char *) memcpy (__new, __old, __len); })), "..") == 0) {
    if (dir->inode != parent) {
     printf("Error: Partition %d Dir Inode %d: .. has wrong dir->inode %d, change to its parent\n", partitionNumber, localId + 1, dir->inode);
     dir->inode = parent;
     changeDir = 1;
    }
   } else {
    printf("Error: Partition %d Dir Inode %d: second dir is not .., change to ..\n", partitionNumber, localId + 1);
    dir->name_len = 2;
    dir->name[0] = '.';
    dir->name[1] = '.';
    dir->inode = localId + 1;
    changeDir = 1;
   }
   checkDirInode(localNo(dir->inode), localId + 1);
  } else {
   if (dir->inode != 0)
    checkDirInode(localNo(dir->inode), localId + 1);
  }
  if (changeDir)
   writeDisk(start * 512 + inode->i_block[0] * blockSize + tempSize, dir->rec_len, dir);
  tempSize += dir->rec_len;
 }
}

canonical_original_code:
void checkDir(struct ext2_inode *inode, unsigned char *buf, int localId, int parent) {
 int changeDir = 0;
 int n = 0;
 int tempSize = 0;
 int totalSize = inode->i_size;
 struct ext2_dir_entry_2 *dir;
 while (tempSize < totalSize) {
  changeDir = 0;
  dir = (struct ext2_dir_entry_2 *)(buf + tempSize);
  ++n;
  if (n == 1) {
   if (dir->name_len == 1 && strcmp((__extension__ ({ char *__old = (dir->name); unsigned long __len = strnlen (__old, (dir->name_len)); char *__new = (char *) __builtin_alloca (__len + 1); __new[__len] = '\0'; (char *) memcpy (__new, __old, __len); })), ".") == 0) {
    if (dir->inode != localId + 1) {
     printf("Error: Partition %d Dir Inode %d: . has wrong dir->inode %d, change to itself\n", partitionNumber, localId + 1, dir->inode);
     dir->inode = localId + 1;
     changeDir = 1;
    }
   } else {
    printf("Error: Partition %d Dir Inode %d: first dir is not ., change to .\n", partitionNumber, localId + 1);
    dir->name_len = 1;
    dir->name[0] = '.';
    dir->inode = localId + 1;
    changeDir = 1;
   }
   checkDirInode(localNo(dir->inode), localId + 1);
  } else if (n == 2) {
   if (dir->name_len == 2 && strcmp((__extension__ ({ char *__old = (dir->name); unsigned long __len = strnlen (__old, (dir->name_len)); char *__new = (char *) __builtin_alloca (__len + 1); __new[__len] = '\0'; (char *) memcpy (__new, __old, __len); })), "..") == 0) {
    if (dir->inode != parent) {
     printf("Error: Partition %d Dir Inode %d: .. has wrong dir->inode %d, change to its parent\n", partitionNumber, localId + 1, dir->inode);
     dir->inode = parent;
     changeDir = 1;
    }
   } else {
    printf("Error: Partition %d Dir Inode %d: second dir is not .., change to ..\n", partitionNumber, localId + 1);
    dir->name_len = 2;
    dir->name[0] = '.';
    dir->name[1] = '.';
    dir->inode = localId + 1;
    changeDir = 1;
   }
   checkDirInode(localNo(dir->inode), localId + 1);
  } else {
   if (dir->inode != 0)
    checkDirInode(localNo(dir->inode), localId + 1);
  }
  if (changeDir)
   writeDisk(start * 512 + inode->i_block[0] * blockSize + tempSize, dir->rec_len, dir);
  tempSize += dir->rec_len;
 }
}

variable_types: changeDir int

variable_types: n int

variable_types: tempSize int

variable_types: totalSize int

variable_types: struct ext2_dir_entry_2 { unsigned int inode; unsigned short rec_len; unsigned char name_len; unsigned char file_type; char name[255]; } *

user_defined_types: struct ext2_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_atime; unsigned int i_ctime; unsigned int i_mtime; unsigned int i_dtime; unsigned short i_gid; unsigned short i_links_count; unsigned int i_blocks; unsigned int i_flags; union { struct { unsigned int l_i_reserved1; } linux1; struct { unsigned int h_i_translator; } hurd1; struct { unsigned int m_i_reserved1; } masix1; } osd1; unsigned int i_block[-1]; unsigned int i_generation; unsigned int i_file_acl; unsigned int i_dir_acl; unsigned int i_faddr; union { struct { unsigned char l_i_frag; unsigned char l_i_fsize; unsigned short i_pad1; unsigned short l_i_uid_high; unsigned short l_i_gid_high; unsigned int l_i_reserved2; } linux2; struct { unsigned char h_i_frag; unsigned char h_i_fsize; unsigned short h_i_mode_high; unsigned short h_i_uid_high; unsigned short h_i_gid_high; unsigned int h_i_author; } hurd2; struct { unsigned char m_i_frag; unsigned char m_i_fsize; unsigned short m_pad1; unsigned int m_i_reserved2[2]; } masix2; } osd2; }


repo: Vvnesaa/fsck

decompiled_code:
__int64 pass1()
{
unsigned int v0; // eax
__int64 v1; // rbx
__int64 result; // rax

v0 = localNo(2LL);
checkDirInode(v0, 2u);
v1 = inodeLink;
result = (int)localNo(2LL);
--*(_DWORD *)(v1 + 4LL * (int)result);
return result;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // eax
__int64 v1; // rbx
__int64 result; // rax

v0 = localNo(2LL);
func0(v0, 2u);
v1 = inodeLink;
result = (int)localNo(2LL);
--*(_DWORD *)(v1 + 4LL * (int)result);
return result;
}

original_code:
void pass1() {
 checkDirInode(localNo(2), 2);
 inodeLink[localNo(2)]--;
}

return_type: void


findDirInode

original_code:
int findDirInode(int localId, const char *dirName) {
 int result = 0;
 struct ext2_dir_entry_2 *dir = 0;
 struct ext2_inode *root = inodeTable + localId;
 if (!isDirectory(root)) {
  printf("inode %d is not a directory\n", localId + 1);
  return result;
 }
 unsigned char *buf = malloc(root->i_size);
 getData(start, root, blockSize, buf);
 int tempSize = 0;
 int totalSize = root->i_size;
 while (tempSize < totalSize) {
  dir = (struct ext2_dir_entry_2 *)(buf + tempSize);
  if (strcmp((__extension__ ({ const char *__old = (dir->name); size_t __len = strnlen (__old, (dir->name_len)); char *__new = (char *) __builtin_alloca (__len + 1); __new[__len] = '\0'; (char *) memcpy (__new, __old, __len); })), dirName) == 0) {
   result = dir->inode;
   break;
  }
  tempSize += dir->rec_len;
 }
 free(buf);
 return result;
}

canonical_original_code:
int findDirInode(int localId, char *dirName) {
 int result = 0;
 struct ext2_dir_entry_2 *dir = 0;
 struct ext2_inode *root = inodeTable + localId;
 if (!isDirectory(root)) {
  printf("inode %d is not a directory\n", localId + 1);
  return result;
 }
 unsigned char *buf = malloc(root->i_size);
 getData(start, root, blockSize, buf);
 int tempSize = 0;
 int totalSize = root->i_size;
 while (tempSize < totalSize) {
  dir = (struct ext2_dir_entry_2 *)(buf + tempSize);
  if (strcmp((__extension__ ({ char *__old = (dir->name); unsigned long __len = strnlen (__old, (dir->name_len)); char *__new = (char *) __builtin_alloca (__len + 1); __new[__len] = '\0'; (char *) memcpy (__new, __old, __len); })), dirName) == 0) {
   result = dir->inode;
   break;
  }
  tempSize += dir->rec_len;
 }
 free(buf);
 return result;
}

variable_types: localId int

variable_types:
dirName
char *

variable_types: result int

variable_types: struct ext2_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_atime; unsigned int i_ctime; unsigned int i_mtime; unsigned int i_dtime; unsigned short i_gid; unsigned short i_links_count; unsigned int i_blocks; unsigned int i_flags; union { struct { unsigned int l_i_reserved1; } linux1; struct { unsigned int h_i_translator; } hurd1; struct { unsigned int m_i_reserved1; } masix1; } osd1; unsigned int i_block[-1]; unsigned int i_generation; unsigned int i_file_acl; unsigned int i_dir_acl; unsigned int i_faddr; union { struct { unsigned char l_i_frag; unsigned char l_i_fsize; unsigned short i_pad1; unsigned short l_i_uid_high; unsigned short l_i_gid_high; unsigned int l_i_reserved2; } linux2; struct { unsigned char h_i_frag; unsigned char h_i_fsize; unsigned short h_i_mode_high; unsigned short h_i_uid_high; unsigned short h_i_gid_high; unsigned int h_i_author; } hurd2; struct { unsigned char m_i_frag; unsigned char m_i_fsize; unsigned short m_pad1; unsigned int m_i_reserved2[2]; } masix2; } osd2; } *

variable_types:
buf
unsigned char *

return_type: int

user_defined_types: struct ext2_dir_entry_2 { unsigned int inode; unsigned short rec_len; unsigned char name_len; unsigned char file_type; char name[255]; }


setFileType

decompiled_code:
__int64 __fastcall setFileType(int a1)
{
__int64 v1; // rbp
int v2; // er8
__int64 result; // rax
int v4; // er8

v1 = inodeTable + ((__int64)a1 << 7);
v2 = isDirectory(v1);
result = 2LL;
if ( !v2 )
{
v4 = isRegFile(v1);
result = 1LL;
if ( !v4 )
{
result = isSymbolicLink(v1);
if ( (_DWORD)result )
result = 7LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
__int64 v1; // rbp
int v2; // er8
__int64 result; // rax
int v4; // er8

v1 = inodeTable + ((__int64)a1 << 7);
v2 = isDirectory(v1);
result = 2LL;
if ( !v2 )
{
v4 = isRegFile(v1);
result = 1LL;
if ( !v4 )
{
result = isSymbolicLink(v1);
if ( (_DWORD)result )
result = 7LL;
}
}
return result;
}

original_code:
int setFileType(int localId) {
 struct ext2_inode *inode = inodeTable + localId;
 if (isDirectory(inode)) return EXT2_FT_DIR;
 if (isRegFile(inode)) return EXT2_FT_REG_FILE;
 if (isSymbolicLink(inode))return EXT2_FT_SYMLINK;
 return EXT2_FT_UNKNOWN;
}


addToLost

original_code:
void addToLost(struct ext2_inode *lost, int ID) {
 int totalSize = lost->i_size;
 unsigned char *buf = malloc(lost->i_size);
 getData(start, lost, blockSize, buf);
 int len = 2 + (int) floor(log10(ID));
 char *fileName = malloc(len);
 sprintf(fileName, "#%d", ID);
 int tempSize = 0;
 int n = 0;
 struct ext2_dir_entry_2 *temp;
 struct ext2_dir_entry_2 *other;
 int change = 0;
 while (tempSize < totalSize) {
  temp = (struct ext2_dir_entry_2 *)(buf + tempSize);
  ++n;
  if (n > 2) {
   if (temp->inode == 0 && temp->rec_len >= (((len) + 8 + (4 - 1)) & ~(4 - 1))) {
    temp->inode = ID;
    temp->file_type = setFileType(localNo(ID));
    printf("%d %d\n", ID, temp->file_type);
    temp->name_len = len;
    strcpy(temp->name, fileName);
    change = 1;
    break;
   } else if (temp->rec_len - (((temp->name_len) + 8 + (4 - 1)) & ~(4 - 1)) >= (((len) + 8 + (4 - 1)) & ~(4 - 1))) {
    other = (struct ext2_dir_entry_2 *)(buf + tempSize + (((temp->name_len) + 8 + (4 - 1)) & ~(4 - 1)));
    other->rec_len = temp->rec_len - (((temp->name_len) + 8 + (4 - 1)) & ~(4 - 1));
    other->inode = ID;
    other->file_type = setFileType(localNo(ID));
    printf("%d %d\n", ID, other->file_type);
    other->name_len = len;
    strcpy(other->name, fileName);
    temp->rec_len = (((temp->name_len) + 8 + (4 - 1)) & ~(4 - 1));
    change = 1;
    break;
   }
  }
  tempSize += temp->rec_len;
 }
 int i;
 tempSize = 0;
 for (i = 0; i < 12; ++i) {
  writeDisk(start * 512 + blockSize * lost->i_block[i], blockSize, buf + tempSize);
  tempSize += blockSize;
  if (tempSize >= totalSize) break;
 }
 free(buf);
 free(fileName);
}

variable_types: ID int

variable_types: len int

variable_types:
fileName
char *

variable_types: change int

variable_types: i int


updateParent

decompiled_code:
__int64 __fastcall updateParent(int *a1, int a2)
{
__int64 result; // rax
int v4; // er14
int v5; // er15
char *v6; // rbp
int v7; // er13
__int64 v8; // rdi
char *v9; // rbx
int v10; // edi
_DWORD *v11; // r14
_DWORD *v12; // rbx
int v13; // er12
__int64 v14; // rsi
int v15; // eax
__int64 v16; // [rsp+8h] [rbp-40h]

result = isDirectory(a1);
if ( (_DWORD)result )
{
v4 = 0;
v5 = 0;
v6 = (char *)malloc((unsigned int)a1[1]);
getData(start, a1, blockSize, v6);
v7 = a1[1];
if ( v7 > 0 )
{
do
{
while ( 1 )
{
++v4;
v9 = &v6[v5];
if ( v4 != 2 )
break;
puts("change inode's parent to lost+found");
*(_DWORD *)v9 = a2;
v5 += *((unsigned __int16 *)v9 + 2);
if ( v7 <= v5 )
goto LABEL_10;
}
if ( v4 > 2 )
{
v8 = *(unsigned int *)v9;
if ( (_DWORD)v8 )
{
v16 = inodeTable;
v15 = localNo(v8);
if ( (unsigned int)isDirectory(v16 + ((__int64)v15 << 7)) )
*(_DWORD *)v9 = 0;
}
}
v5 += *((unsigned __int16 *)v9 + 2);
}
while ( v7 > v5 );
}
LABEL_10:
v10 = blockSize;
v11 = a1 + 10;
v12 = a1 + 22;
v13 = 0;
do
{
v14 = (unsigned int)(v7 - v13);
if ( (int)v14 > v10 )
v14 = (unsigned int)v10;
result = writeDisk((unsigned int)((start << 9) + *v11 * v10), v14, &v6[v13]);
v10 = blockSize;
v13 += blockSize;
if ( v7 <= v13 )
break;
++v11;
}
while ( v12 != v11 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1, int a2)
{
__int64 result; // rax
int v4; // er14
int v5; // er15
char *v6; // rbp
int v7; // er13
__int64 v8; // rdi
char *v9; // rbx
int v10; // edi
_DWORD *v11; // r14
_DWORD *v12; // rbx
int v13; // er12
__int64 v14; // rsi
int v15; // eax
__int64 v16; // [rsp+8h] [rbp-40h]

result = isDirectory(a1);
if ( (_DWORD)result )
{
v4 = 0;
v5 = 0;
v6 = (char *)malloc((unsigned int)a1[1]);
getData(start, a1, blockSize, v6);
v7 = a1[1];
if ( v7 > 0 )
{
do
{
while ( 1 )
{
++v4;
v9 = &v6[v5];
if ( v4 != 2 )
break;
puts("change inode's parent to lost+found");
*(_DWORD *)v9 = a2;
v5 += *((unsigned __int16 *)v9 + 2);
if ( v7 <= v5 )
goto LABEL_10;
}
if ( v4 > 2 )
{
v8 = *(unsigned int *)v9;
if ( (_DWORD)v8 )
{
v16 = inodeTable;
v15 = localNo(v8);
if ( (unsigned int)isDirectory(v16 + ((__int64)v15 << 7)) )
*(_DWORD *)v9 = 0;
}
}
v5 += *((unsigned __int16 *)v9 + 2);
}
while ( v7 > v5 );
}
LABEL_10:
v10 = blockSize;
v11 = a1 + 10;
v12 = a1 + 22;
v13 = 0;
do
{
v14 = (unsigned int)(v7 - v13);
if ( (int)v14 > v10 )
v14 = (unsigned int)v10;
result = writeDisk((unsigned int)((start << 9) + *v11 * v10), v14, &v6[v13]);
v10 = blockSize;
v13 += blockSize;
if ( v7 <= v13 )
break;
++v11;
}
while ( v12 != v11 );
}
return result;
}

original_code:
void updateParent(struct ext2_inode *inode, int parent) {
 if (!isDirectory(inode)) return;
 unsigned char *buf = malloc(inode->i_size);
 getData(start, inode, blockSize, buf);
 int tempSize = 0;
 int n = 0;
 int totalSize = inode->i_size;
 struct ext2_dir_entry_2 *dir;
 while (tempSize < totalSize) {
  ++n;
  dir = (struct ext2_dir_entry_2 *)(buf + tempSize);
  if (n == 2) {
   printf("change inode's parent to lost+found\n");
   dir->inode = parent;
  } else if (n > 2 && dir->inode != 0 && isDirectory(inodeTable + localNo(dir->inode))) {
   dir->inode = 0;
  }
  tempSize += dir->rec_len;
 }
 tempSize = 0;
 int i;
 int currentTemp;
 for (i = 0; i < 12; ++i) {
  if (totalSize - tempSize < blockSize)
   currentTemp = totalSize - tempSize;
  else
   currentTemp = blockSize;
  writeDisk(start * 512 + inode->i_block[i] * blockSize, currentTemp, buf + tempSize);
  tempSize += blockSize;
  if (tempSize >= totalSize) break;
 }
}

variable_types: parent int

variable_types: currentTemp int


decompiled_code:
int pass2()
{
int v0; // eax
const char *v1; // rsi
unsigned int v2; // eax
__int64 v3; // rdi
unsigned int v4; // er13
unsigned int v5; // ebx
unsigned int v6; // eax
__int64 v7; // rdx
__int64 v8; // r12
__int64 v9; // rbp
__int64 v10; // r14
int v11; // eax
__int64 v12; // rcx
__int64 i; // rax

v0 = localNo(2LL);
v1 = "lost+found";
v2 = findDirInode(v0, "lost+found");
if ( !v2 )
return puts("/lost+found is not found");
v3 = 11LL;
v4 = v2;
if ( x[19] )
v3 = x[21];
v5 = localNo(v3);
v6 = x[10];
v7 = v6 * groupNum;
if ( (unsigned int)v7 > v5 )
{
v8 = 4LL * (int)v5;
do
{
++v5;
v9 = inodeTable + 32 * v8;
if ( *(_WORD *)(v9 + 26) && !*(_DWORD *)(inodeLink + v8) )
{
printf("partition %d inode %d should be put into lost+found\n", partitionNumber, v5);
v10 = inodeTable;
v11 = localNo(v4);
addToLost((unsigned int *)(v10 + ((__int64)v11 << 7)), v5);
v1 = (const char *)v4;
v3 = v9;
updateParent((int *)v9, v4);
v6 = x[10];
}
v8 += 4LL;
v7 = v6 * groupNum;
}
while ( (unsigned int)v7 > v5 );
}
v12 = inodeLink;
for ( i = 0LL; (unsigned int)v7 > (unsigned int)i; v7 = groupNum * x[10] )
*(_DWORD *)(v12 + 4 * i++) = 0;
return pass1(v3, v1, v7, v12);
}

canonical_decompiled_code:
int func4()
{
int v0; // eax
const char *v1; // rsi
unsigned int v2; // eax
__int64 v3; // rdi
unsigned int v4; // er13
unsigned int v5; // ebx
unsigned int v6; // eax
__int64 v7; // rdx
__int64 v8; // r12
__int64 v9; // rbp
__int64 v10; // r14
int v11; // eax
__int64 v12; // rcx
__int64 i; // rax

v0 = localNo(2LL);
v1 = "lost+found";
v2 = func0(v0, "lost+found");
if ( !v2 )
return puts("/lost+found is not found");
v3 = 11LL;
v4 = v2;
if ( x[19] )
v3 = x[21];
v5 = localNo(v3);
v6 = x[10];
v7 = v6 * groupNum;
if ( (unsigned int)v7 > v5 )
{
v8 = 4LL * (int)v5;
do
{
++v5;
v9 = inodeTable + 32 * v8;
if ( *(_WORD *)(v9 + 26) && !*(_DWORD *)(inodeLink + v8) )
{
printf("partition %d inode %d should be put into lost+found\n", partitionNumber, v5);
v10 = inodeTable;
v11 = localNo(v4);
func2((unsigned int *)(v10 + ((__int64)v11 << 7)), v5);
v1 = (const char *)v4;
v3 = v9;
func3((int *)v9, v4);
v6 = x[10];
}
v8 += 4LL;
v7 = v6 * groupNum;
}
while ( (unsigned int)v7 > v5 );
}
v12 = inodeLink;
for ( i = 0LL; (unsigned int)v7 > (unsigned int)i; v7 = groupNum * x[10] )
*(_DWORD *)(v12 + 4 * i++) = 0;
return pass1(v3, v1, v7, v12);
}

original_code:
void pass2() {
 int lostInodeId = findDirInode(localNo(2), "lost+found");
 if (lostInodeId == 0) {
  printf("/lost+found is not found\n");
  return;
 }
 int i;
 struct ext2_inode *inode;
 for (i = localNo((((&x)->s_rev_level == 0) ? 11 : (&x)->s_first_ino)); i < groupNum * ((&x)->s_inodes_per_group); ++i) {
  inode = inodeTable + i;
  if (inode->i_links_count > 0 && inodeLink[i] == 0) {
   printf("partition %d inode %d should be put into lost+found\n", partitionNumber, i + 1);
   addToLost(inodeTable + localNo(lostInodeId), i + 1);
   updateParent(inode, lostInodeId);
  }
 }
 for (i = 0; i < groupNum * ((&x)->s_inodes_per_group); ++i)
  inodeLink[i] = 0;
 pass1();
}

variable_types: lostInodeId int


checkDataBlock

decompiled_code:
void __fastcall checkDataBlock(int a1)
{
int *v1; // rax
int v2; // er12
__int64 v3; // rbx
int v4; // ebp
int v5; // er13
unsigned int *v6; // r15
_DWORD *v7; // rax
unsigned int v8; // er14
unsigned int v9; // er12
void *v10; // rbx
unsigned int v11; // eax
unsigned __int64 v12; // r12
unsigned int v13; // er14
void *v14; // r15
unsigned int v15; // eax
unsigned int v16; // er14
unsigned int *v17; // r15
unsigned int v18; // er12
void *v19; // rbx
unsigned int v20; // eax
char *v21; // r14
char *v22; // r13
unsigned int v23; // eax
unsigned int v24; // er15
void *v25; // r12
unsigned int v26; // eax
unsigned int v27; // er15
__int64 v28; // r12
unsigned int v29; // er15
void *v30; // r12
unsigned int v31; // eax
void *v32; // rsi
unsigned int *v33; // r12
unsigned int v34; // ebx
void *v35; // r14
unsigned int v36; // eax
unsigned int v37; // er14
unsigned int v38; // er15
void *v39; // r12
unsigned int v40; // eax
__int64 v41; // rsi
__int64 v42; // rbx
unsigned int v43; // er15
void *v44; // r12
unsigned int v45; // eax
int v46; // eax
unsigned int v47; // er13
int v48; // er15
unsigned int v49; // ebx
void *v50; // r13
unsigned int v51; // eax
__int64 v52; // rdx
unsigned int *v53; // rbx
unsigned int v54; // er12
void *v55; // r14
unsigned int v56; // eax
unsigned int v57; // er15
unsigned int v58; // er12
void *v59; // rbp
unsigned int v60; // eax
unsigned int v61; // er15
unsigned int v62; // er12
void *v63; // rbp
unsigned int v64; // eax
unsigned int *v65; // [rsp+8h] [rbp-80h]
unsigned int v66; // [rsp+8h] [rbp-80h]
__int64 v67; // [rsp+8h] [rbp-80h]
unsigned int v68; // [rsp+8h] [rbp-80h]
unsigned int *v69; // [rsp+8h] [rbp-80h]
unsigned int *v70; // [rsp+8h] [rbp-80h]
unsigned int *ptr; // [rsp+10h] [rbp-78h]
_DWORD *v72; // [rsp+18h] [rbp-70h]
_DWORD *v73; // [rsp+20h] [rbp-68h]
unsigned __int64 v74; // [rsp+28h] [rbp-60h]
_DWORD *v75; // [rsp+28h] [rbp-60h]
unsigned int v76; // [rsp+28h] [rbp-60h]
_DWORD *v77; // [rsp+28h] [rbp-60h]
_DWORD *v78; // [rsp+30h] [rbp-58h]
unsigned int v79; // [rsp+30h] [rbp-58h]
unsigned int v80; // [rsp+30h] [rbp-58h]
unsigned int v81; // [rsp+38h] [rbp-50h]
_DWORD *v82; // [rsp+38h] [rbp-50h]
_DWORD *v83; // [rsp+40h] [rbp-48h]
_DWORD *v84; // [rsp+40h] [rbp-48h]
_DWORD *v85; // [rsp+48h] [rbp-40h]
_DWORD *v86; // [rsp+48h] [rbp-40h]

v1 = (int *)((char *)inodeFlag + 4 * a1);
v2 = *v1;
if ( !*v1 )
{
*v1 = 1;
v3 = inodeTable + ((__int64)a1 << 7);
v4 = *(_DWORD *)(v3 + 4);
if ( (unsigned int)isDirectory(v3) )
{
v21 = (char *)malloc(*(unsigned int *)(v3 + 4));
getData(start, v3, blockSize, v21);
if ( v4 > 0 )
{
do
{
v22 = &v21[v2];
if ( *(_DWORD *)v22 )
{
v23 = ((__int64 (*)(void))localNo)();
checkDataBlock(v23);
}
v2 += *((unsigned __int16 *)v22 + 2);
}
while ( v4 > v2 );
}
free(v21);
}
v5 = isSymbolicLink(v3);
if ( !v5 )
{
v6 = (unsigned int *)(v3 + 40);
v74 = blockSize;
ptr = (unsigned int *)malloc(blockSize);
v72 = malloc(blockSize);
v7 = malloc(blockSize);
v78 = (_DWORD *)v3;
v8 = blockSize;
v73 = v7;
v65 = (unsigned int *)(v3 + 88);
while ( 1 )
{
v9 = groupNum;
v10 = newBitmap;
v11 = localNo(*v6);
setBlockBitmapSet(v11, v10, v9, v8, &x, 1LL);
v8 = blockSize;
v5 += blockSize;
if ( v4 <= v5 )
break;
if ( v65 == ++v6 )
{
v12 = v74 >> 2;
readDisk((unsigned int)(blockSize * v78[22] + (start << 9)), blockSize, ptr);
v13 = groupNum;
v14 = newBitmap;
v66 = blockSize;
v15 = localNo((unsigned int)v78[22]);
setBlockBitmapSet(v15, v14, v13, v66, &x, 1LL);
if ( (int)(v74 >> 2) <= 0 )
{
readDisk((unsigned int)(blockSize * v78[23] + (start << 9)), blockSize, v72);
v57 = blockSize;
v58 = groupNum;
v59 = newBitmap;
v60 = localNo((unsigned int)v78[23]);
setBlockBitmapSet(v60, v59, v58, v57, &x, 1LL);
readDisk((unsigned int)((start << 9) + blockSize * v78[24]), blockSize, v73);
v61 = blockSize;
v62 = groupNum;
v63 = newBitmap;
v64 = localNo((unsigned int)v78[24]);
setBlockBitmapSet(v64, v63, v62, v61, &x, 1LL);
}
else
{
v81 = v12 - 1;
v16 = blockSize;
v17 = ptr;
v67 = (__int64)&ptr[(unsigned int)(v12 - 1) + 1];
while ( 1 )
{
v18 = groupNum;
v19 = newBitmap;
v20 = localNo(*v17);
setBlockBitmapSet(v20, v19, v18, v16, &x, 1LL);
v16 = blockSize;
v5 += blockSize;
if ( v4 <= v5 )
break;
if ( ++v17 == (unsigned int *)v67 )
{
readDisk((unsigned int)(blockSize * v78[23] + (start << 9)), blockSize, v72);
v24 = groupNum;
v25 = newBitmap;
v68 = blockSize;
v26 = localNo((unsigned int)v78[23]);
setBlockBitmapSet(v26, v25, v24, v68, &x, 1LL);
v85 = v78;
v27 = blockSize;
v28 = 4LL * v81 + 4;
v75 = v72;
v83 = (_DWORD *)((char *)v72 + v28);
v69 = (unsigned int *)((char *)ptr + v28);
LABEL_19:
readDisk(v27 * *v75 + (start << 9), v27, ptr);
v29 = groupNum;
v30 = newBitmap;
v79 = blockSize;
v31 = localNo((unsigned int)*v75);
v32 = v30;
v33 = ptr;
setBlockBitmapSet(v31, v32, v29, v79, &x, 1LL);
v27 = blockSize;
while ( 1 )
{
v34 = groupNum;
v35 = newBitmap;
v36 = localNo(*v33);
setBlockBitmapSet(v36, v35, v34, v27, &x, 1LL);
v27 = blockSize;
v5 += blockSize;
if ( v4 <= v5 )
goto LABEL_12;
if ( v69 == ++v33 )
{
if ( v83 != ++v75 )
goto LABEL_19;
readDisk((unsigned int)((start << 9) + blockSize * v85[24]), blockSize, v73);
v37 = blockSize;
v38 = groupNum;
v39 = newBitmap;
v40 = localNo((unsigned int)v85[24]);
setBlockBitmapSet(v40, v39, v38, v37, &x, 1LL);
v41 = blockSize;
v42 = 4LL * v81 + 4;
v84 = v73;
v86 = (_DWORD *)((char *)v73 + v42);
v82 = (_DWORD *)((char *)v72 + v42);
v70 = (unsigned int *)((char *)ptr + v42);
LABEL_25:
readDisk((unsigned int)(v41 * *v84 + (start << 9)), v41, v72);
v43 = groupNum;
v44 = newBitmap;
v76 = blockSize;
v45 = localNo((unsigned int)*v84);
setBlockBitmapSet(v45, v44, v43, v76, &x, 1LL);
v77 = v72;
v46 = v5;
v47 = blockSize;
v48 = v46;
LABEL_26:
readDisk(v47 * *v77 + (start << 9), v47, ptr);
v49 = groupNum;
v50 = newBitmap;
v80 = blockSize;
v51 = localNo((unsigned int)*v77);
v52 = v49;
v53 = ptr;
setBlockBitmapSet(v51, v50, v52, v80, &x, 1LL);
v47 = blockSize;
while ( 1 )
{
v54 = groupNum;
v55 = newBitmap;
v56 = localNo(*v53);
setBlockBitmapSet(v56, v55, v54, v47, &x, 1LL);
v41 = blockSize;
v48 += blockSize;
v47 = blockSize;
if ( v4 <= v48 )
goto LABEL_12;
if ( v70 == ++v53 )
{
if ( v82 != ++v77 )
goto LABEL_26;
++v84;
v5 = v48;
if ( v86 != v84 )
goto LABEL_25;
goto LABEL_12;
}
}
}
}
}
}
}
break;
}
}
LABEL_12:
free(ptr);
free(v72);
free(v73);
}
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
int *v1; // rax
int v2; // er12
__int64 v3; // rbx
int v4; // ebp
int v5; // er13
unsigned int *v6; // r15
_DWORD *v7; // rax
unsigned int v8; // er14
unsigned int v9; // er12
void *v10; // rbx
unsigned int v11; // eax
unsigned __int64 v12; // r12
unsigned int v13; // er14
void *v14; // r15
unsigned int v15; // eax
unsigned int v16; // er14
unsigned int *v17; // r15
unsigned int v18; // er12
void *v19; // rbx
unsigned int v20; // eax
char *v21; // r14
char *v22; // r13
unsigned int v23; // eax
unsigned int v24; // er15
void *v25; // r12
unsigned int v26; // eax
unsigned int v27; // er15
__int64 v28; // r12
unsigned int v29; // er15
void *v30; // r12
unsigned int v31; // eax
void *v32; // rsi
unsigned int *v33; // r12
unsigned int v34; // ebx
void *v35; // r14
unsigned int v36; // eax
unsigned int v37; // er14
unsigned int v38; // er15
void *v39; // r12
unsigned int v40; // eax
__int64 v41; // rsi
__int64 v42; // rbx
unsigned int v43; // er15
void *v44; // r12
unsigned int v45; // eax
int v46; // eax
unsigned int v47; // er13
int v48; // er15
unsigned int v49; // ebx
void *v50; // r13
unsigned int v51; // eax
__int64 v52; // rdx
unsigned int *v53; // rbx
unsigned int v54; // er12
void *v55; // r14
unsigned int v56; // eax
unsigned int v57; // er15
unsigned int v58; // er12
void *v59; // rbp
unsigned int v60; // eax
unsigned int v61; // er15
unsigned int v62; // er12
void *v63; // rbp
unsigned int v64; // eax
unsigned int *v65; // [rsp+8h] [rbp-80h]
unsigned int v66; // [rsp+8h] [rbp-80h]
__int64 v67; // [rsp+8h] [rbp-80h]
unsigned int v68; // [rsp+8h] [rbp-80h]
unsigned int *v69; // [rsp+8h] [rbp-80h]
unsigned int *v70; // [rsp+8h] [rbp-80h]
unsigned int *ptr; // [rsp+10h] [rbp-78h]
_DWORD *v72; // [rsp+18h] [rbp-70h]
_DWORD *v73; // [rsp+20h] [rbp-68h]
unsigned __int64 v74; // [rsp+28h] [rbp-60h]
_DWORD *v75; // [rsp+28h] [rbp-60h]
unsigned int v76; // [rsp+28h] [rbp-60h]
_DWORD *v77; // [rsp+28h] [rbp-60h]
_DWORD *v78; // [rsp+30h] [rbp-58h]
unsigned int v79; // [rsp+30h] [rbp-58h]
unsigned int v80; // [rsp+30h] [rbp-58h]
unsigned int v81; // [rsp+38h] [rbp-50h]
_DWORD *v82; // [rsp+38h] [rbp-50h]
_DWORD *v83; // [rsp+40h] [rbp-48h]
_DWORD *v84; // [rsp+40h] [rbp-48h]
_DWORD *v85; // [rsp+48h] [rbp-40h]
_DWORD *v86; // [rsp+48h] [rbp-40h]

v1 = (int *)((char *)inodeFlag + 4 * a1);
v2 = *v1;
if ( !*v1 )
{
*v1 = 1;
v3 = inodeTable + ((__int64)a1 << 7);
v4 = *(_DWORD *)(v3 + 4);
if ( (unsigned int)isDirectory(v3) )
{
v21 = (char *)malloc(*(unsigned int *)(v3 + 4));
getData(start, v3, blockSize, v21);
if ( v4 > 0 )
{
do
{
v22 = &v21[v2];
if ( *(_DWORD *)v22 )
{
v23 = ((__int64 (*)(void))localNo)();
func0(v23);
}
v2 += *((unsigned __int16 *)v22 + 2);
}
while ( v4 > v2 );
}
free(v21);
}
v5 = isSymbolicLink(v3);
if ( !v5 )
{
v6 = (unsigned int *)(v3 + 40);
v74 = blockSize;
ptr = (unsigned int *)malloc(blockSize);
v72 = malloc(blockSize);
v7 = malloc(blockSize);
v78 = (_DWORD *)v3;
v8 = blockSize;
v73 = v7;
v65 = (unsigned int *)(v3 + 88);
while ( 1 )
{
v9 = groupNum;
v10 = newBitmap;
v11 = localNo(*v6);
setBlockBitmapSet(v11, v10, v9, v8, &x, 1LL);
v8 = blockSize;
v5 += blockSize;
if ( v4 <= v5 )
break;
if ( v65 == ++v6 )
{
v12 = v74 >> 2;
readDisk((unsigned int)(blockSize * v78[22] + (start << 9)), blockSize, ptr);
v13 = groupNum;
v14 = newBitmap;
v66 = blockSize;
v15 = localNo((unsigned int)v78[22]);
setBlockBitmapSet(v15, v14, v13, v66, &x, 1LL);
if ( (int)(v74 >> 2) <= 0 )
{
readDisk((unsigned int)(blockSize * v78[23] + (start << 9)), blockSize, v72);
v57 = blockSize;
v58 = groupNum;
v59 = newBitmap;
v60 = localNo((unsigned int)v78[23]);
setBlockBitmapSet(v60, v59, v58, v57, &x, 1LL);
readDisk((unsigned int)((start << 9) + blockSize * v78[24]), blockSize, v73);
v61 = blockSize;
v62 = groupNum;
v63 = newBitmap;
v64 = localNo((unsigned int)v78[24]);
setBlockBitmapSet(v64, v63, v62, v61, &x, 1LL);
}
else
{
v81 = v12 - 1;
v16 = blockSize;
v17 = ptr;
v67 = (__int64)&ptr[(unsigned int)(v12 - 1) + 1];
while ( 1 )
{
v18 = groupNum;
v19 = newBitmap;
v20 = localNo(*v17);
setBlockBitmapSet(v20, v19, v18, v16, &x, 1LL);
v16 = blockSize;
v5 += blockSize;
if ( v4 <= v5 )
break;
if ( ++v17 == (unsigned int *)v67 )
{
readDisk((unsigned int)(blockSize * v78[23] + (start << 9)), blockSize, v72);
v24 = groupNum;
v25 = newBitmap;
v68 = blockSize;
v26 = localNo((unsigned int)v78[23]);
setBlockBitmapSet(v26, v25, v24, v68, &x, 1LL);
v85 = v78;
v27 = blockSize;
v28 = 4LL * v81 + 4;
v75 = v72;
v83 = (_DWORD *)((char *)v72 + v28);
v69 = (unsigned int *)((char *)ptr + v28);
LABEL_19:
readDisk(v27 * *v75 + (start << 9), v27, ptr);
v29 = groupNum;
v30 = newBitmap;
v79 = blockSize;
v31 = localNo((unsigned int)*v75);
v32 = v30;
v33 = ptr;
setBlockBitmapSet(v31, v32, v29, v79, &x, 1LL);
v27 = blockSize;
while ( 1 )
{
v34 = groupNum;
v35 = newBitmap;
v36 = localNo(*v33);
setBlockBitmapSet(v36, v35, v34, v27, &x, 1LL);
v27 = blockSize;
v5 += blockSize;
if ( v4 <= v5 )
goto LABEL_12;
if ( v69 == ++v33 )
{
if ( v83 != ++v75 )
goto LABEL_19;
readDisk((unsigned int)((start << 9) + blockSize * v85[24]), blockSize, v73);
v37 = blockSize;
v38 = groupNum;
v39 = newBitmap;
v40 = localNo((unsigned int)v85[24]);
setBlockBitmapSet(v40, v39, v38, v37, &x, 1LL);
v41 = blockSize;
v42 = 4LL * v81 + 4;
v84 = v73;
v86 = (_DWORD *)((char *)v73 + v42);
v82 = (_DWORD *)((char *)v72 + v42);
v70 = (unsigned int *)((char *)ptr + v42);
LABEL_25:
readDisk((unsigned int)(v41 * *v84 + (start << 9)), v41, v72);
v43 = groupNum;
v44 = newBitmap;
v76 = blockSize;
v45 = localNo((unsigned int)*v84);
setBlockBitmapSet(v45, v44, v43, v76, &x, 1LL);
v77 = v72;
v46 = v5;
v47 = blockSize;
v48 = v46;
LABEL_26:
readDisk(v47 * *v77 + (start << 9), v47, ptr);
v49 = groupNum;
v50 = newBitmap;
v80 = blockSize;
v51 = localNo((unsigned int)*v77);
v52 = v49;
v53 = ptr;
setBlockBitmapSet(v51, v50, v52, v80, &x, 1LL);
v47 = blockSize;
while ( 1 )
{
v54 = groupNum;
v55 = newBitmap;
v56 = localNo(*v53);
setBlockBitmapSet(v56, v55, v54, v47, &x, 1LL);
v41 = blockSize;
v48 += blockSize;
v47 = blockSize;
if ( v4 <= v48 )
goto LABEL_12;
if ( v70 == ++v53 )
{
if ( v82 != ++v77 )
goto LABEL_26;
++v84;
v5 = v48;
if ( v86 != v84 )
goto LABEL_25;
goto LABEL_12;
}
}
}
}
}
}
}
break;
}
}
LABEL_12:
free(ptr);
free(v72);
free(v73);
}
}
}

original_code:
void checkDataBlock(int localId) {
 if (inodeFlag[localId]) return;
 inodeFlag[localId] = 1;
 struct ext2_inode *inode;
 inode = inodeTable + localId;
 int tempSize;
 int totalSize = inode->i_size;
 if (isDirectory(inode)) {
  unsigned char *buf = malloc(inode->i_size);
  getData(start, inode, blockSize, buf);
  tempSize = 0;
  struct ext2_dir_entry_2 *dir;
  while (tempSize < totalSize) {
   dir = (struct ext2_dir_entry_2 *)(buf + tempSize);
   if (dir->inode != 0)
    checkDataBlock(localNo(dir->inode));
   tempSize += dir->rec_len;
  }
  free(buf);
 }
 if (isSymbolicLink(inode)) return;
 tempSize = 0;
 int i, j, k;
 int len = blockSize / sizeof(int);
 int* indblock = malloc(blockSize);
 int* dindblock = malloc(blockSize);
 int* tindblock = malloc(blockSize);
 for (i = 0; i < 12; ++i) {
  setBlockBitmapSet(localNo(inode->i_block[i]), newBitmap, groupNum, blockSize, &x, 1);
  tempSize += blockSize;
  if (tempSize >= totalSize) goto FREE_MEMORY;
 }
 readDisk(start * 512 + blockSize * inode->i_block[12], blockSize, indblock);
 setBlockBitmapSet(localNo(inode->i_block[12]), newBitmap, groupNum, blockSize, &x, 1);
 for (i = 0; i < len; ++i) {
  setBlockBitmapSet(localNo(indblock[i]), newBitmap, groupNum, blockSize, &x, 1);
  tempSize += blockSize;
  if (tempSize >= totalSize) goto FREE_MEMORY;
 }
 readDisk(start * 512 + blockSize * inode->i_block[(12 + 1)], blockSize, dindblock);
 setBlockBitmapSet(localNo(inode->i_block[(12 + 1)]), newBitmap, groupNum, blockSize, &x, 1);
 for (i = 0; i < len; ++i) {
  readDisk(start * 512 + blockSize * dindblock[i], blockSize, indblock);
  setBlockBitmapSet(localNo(dindblock[i]), newBitmap, groupNum, blockSize, &x, 1);
  for (j = 0; j < len; ++j) {
   setBlockBitmapSet(localNo(indblock[j]), newBitmap, groupNum, blockSize, &x, 1);
   tempSize += blockSize;
   if (tempSize >= totalSize) goto FREE_MEMORY;
  }
 }
 readDisk(start * 512 + blockSize * inode->i_block[((12 + 1) + 1)], blockSize, tindblock);
 setBlockBitmapSet(localNo(inode->i_block[((12 + 1) + 1)]), newBitmap, groupNum, blockSize, &x, 1);
 for (i = 0; i < len; ++i) {
  readDisk(start * 512 + blockSize * tindblock[i], blockSize, dindblock);
  setBlockBitmapSet(localNo(tindblock[i]), newBitmap, groupNum, blockSize, &x, 1);
  for (j = 0; j < len; ++j) {
   readDisk(start * 512 + blockSize * dindblock[j], blockSize, indblock);
   setBlockBitmapSet(localNo(dindblock[j]), newBitmap, groupNum, blockSize, &x, 1);
   for (k = 0; k < len; ++k) {
    setBlockBitmapSet(localNo(indblock[k]), newBitmap, groupNum, blockSize, &x, 1);
    tempSize += blockSize;
    if (tempSize >= totalSize) goto FREE_MEMORY;
   }
  }
 }
 FREE_MEMORY:
  free(indblock);
  free(dindblock);
  free(tindblock);
}

canonical_original_code:
void checkDataBlock(int localId) {
 if (inodeFlag[localId]) return;
 inodeFlag[localId] = 1;
 struct ext2_inode *inode;
 inode = inodeTable + localId;
 int tempSize;
 int totalSize = inode->i_size;
 if (isDirectory(inode)) {
  unsigned char *buf = malloc(inode->i_size);
  getData(start, inode, blockSize, buf);
  tempSize = 0;
  struct ext2_dir_entry_2 *dir;
  while (tempSize < totalSize) {
   dir = (struct ext2_dir_entry_2 *)(buf + tempSize);
   if (dir->inode != 0)
    checkDataBlock(localNo(dir->inode));
   tempSize += dir->rec_len;
  }
  free(buf);
 }
 if (isSymbolicLink(inode)) return;
 tempSize = 0;
 int i; int j; int k;
 int len = blockSize / sizeof(int);
 int *indblock = malloc(blockSize);
 int *dindblock = malloc(blockSize);
 int *tindblock = malloc(blockSize);
 for (i = 0; i < 12; ++i) {
  setBlockBitmapSet(localNo(inode->i_block[i]), newBitmap, groupNum, blockSize, &x, 1);
  tempSize += blockSize;
  if (tempSize >= totalSize) goto FREE_MEMORY;
 }
 readDisk(start * 512 + blockSize * inode->i_block[12], blockSize, indblock);
 setBlockBitmapSet(localNo(inode->i_block[12]), newBitmap, groupNum, blockSize, &x, 1);
 for (i = 0; i < len; ++i) {
  setBlockBitmapSet(localNo(indblock[i]), newBitmap, groupNum, blockSize, &x, 1);
  tempSize += blockSize;
  if (tempSize >= totalSize) goto FREE_MEMORY;
 }
 readDisk(start * 512 + blockSize * inode->i_block[(12 + 1)], blockSize, dindblock);
 setBlockBitmapSet(localNo(inode->i_block[(12 + 1)]), newBitmap, groupNum, blockSize, &x, 1);
 for (i = 0; i < len; ++i) {
  readDisk(start * 512 + blockSize * dindblock[i], blockSize, indblock);
  setBlockBitmapSet(localNo(dindblock[i]), newBitmap, groupNum, blockSize, &x, 1);
  for (j = 0; j < len; ++j) {
   setBlockBitmapSet(localNo(indblock[j]), newBitmap, groupNum, blockSize, &x, 1);
   tempSize += blockSize;
   if (tempSize >= totalSize) goto FREE_MEMORY;
  }
 }
 readDisk(start * 512 + blockSize * inode->i_block[((12 + 1) + 1)], blockSize, tindblock);
 setBlockBitmapSet(localNo(inode->i_block[((12 + 1) + 1)]), newBitmap, groupNum, blockSize, &x, 1);
 for (i = 0; i < len; ++i) {
  readDisk(start * 512 + blockSize * tindblock[i], blockSize, dindblock);
  setBlockBitmapSet(localNo(tindblock[i]), newBitmap, groupNum, blockSize, &x, 1);
  for (j = 0; j < len; ++j) {
   readDisk(start * 512 + blockSize * dindblock[j], blockSize, indblock);
   setBlockBitmapSet(localNo(dindblock[j]), newBitmap, groupNum, blockSize, &x, 1);
   for (k = 0; k < len; ++k) {
    setBlockBitmapSet(localNo(indblock[k]), newBitmap, groupNum, blockSize, &x, 1);
    tempSize += blockSize;
    if (tempSize >= totalSize) goto FREE_MEMORY;
   }
  }
 }
 FREE_MEMORY:
  free(indblock);
  free(dindblock);
  free(tindblock);
}

variable_types: j int

variable_types: k int

variable_types:
indblock
int *

variable_types:
dindblock
int *

variable_types:
tindblock
int *


checkBlockBitmap

decompiled_code:
void checkBlockBitmap()
{
int v0; // ebp
int v1; // eax
__int64 v2; // rdx
unsigned int v3; // er13
__int64 v4; // r14
unsigned int v5; // ebx
unsigned int v6; // er15
__int64 v7; // rbx
__int64 v8; // r12
unsigned int v9; // eax
void *v10; // r12
unsigned int v11; // eax
__int64 v12; // r12
unsigned int v13; // eax
void *v14; // r12
unsigned int v15; // eax
int v16; // ebx
unsigned int v17; // er12
int v18; // ebx
__int64 v19; // rdi
unsigned int v20; // er15
unsigned int v21; // ebx
void *v22; // rax
__int64 v23; // rbx
int v24; // eax
unsigned int v25; // [rsp+0h] [rbp-48h]
unsigned int v26; // [rsp+0h] [rbp-48h]
unsigned int v27; // [rsp+0h] [rbp-48h]
unsigned int v28; // [rsp+0h] [rbp-48h]
unsigned int v29; // [rsp+4h] [rbp-44h]
unsigned int v30; // [rsp+4h] [rbp-44h]
unsigned int v31; // [rsp+4h] [rbp-44h]
unsigned int v32; // [rsp+4h] [rbp-44h]
int v33; // [rsp+8h] [rbp-40h]
int v34; // [rsp+Ch] [rbp-3Ch]

v33 = x[8];
v0 = roundUp((unsigned int)(32 * groupNum), blockSize);
v1 = roundUp((unsigned int)(x[10] << 7), blockSize);
v2 = groupNum;
v34 = v1;
if ( groupNum > 0 )
{
v3 = 0;
v4 = 0LL;
do
{
if ( !(unsigned int)isBlockBitmapSet(v3, blockBitmap, v2, blockSize, x) )
{
printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, v3 + 1, 0LL, 1LL);
change = 1;
}
v5 = v3 + 2;
setBlockBitmapSet(v3, newBitmap, groupNum, blockSize, x, 1LL);
if ( v0 > 0 )
{
do
{
v6 = v5 - 1;
if ( !(unsigned int)isBlockBitmapSet(v5 - 1, blockBitmap, groupNum, blockSize, x) )
{
printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, v5, 0LL, 1LL);
change = 1;
}
++v5;
setBlockBitmapSet(v6, newBitmap, groupNum, blockSize, x, 1LL);
}
while ( v0 >= (int)(~v3 + v5) );
}
v7 = 32 * v4;
v8 = blockBitmap;
v29 = blockSize;
v25 = groupNum;
v9 = localNo(*(unsigned int *)(groupDescs + 32 * v4));
if ( !(unsigned int)isBlockBitmapSet(v9, v8, v25, v29, x) )
{
printf(
"partition %d block %d : bitmap wrong: %d Vs %d\n",
partitionNumber,
*(unsigned int *)(groupDescs + 32 * v4),
0LL,
1LL);
change = 1;
}
v10 = newBitmap;
v30 = blockSize;
v26 = groupNum;
v11 = localNo(*(unsigned int *)(groupDescs + 32 * v4));
setBlockBitmapSet(v11, v10, v26, v30, x, 1LL);
v12 = blockBitmap;
v31 = blockSize;
v27 = groupNum;
v13 = localNo(*(unsigned int *)(groupDescs + v7 + 4));
if ( !(unsigned int)isBlockBitmapSet(v13, v12, v27, v31, x) )
{
printf(
"partition %d block %d : bitmap wrong: %d Vs %d\n",
partitionNumber,
*(unsigned int *)(groupDescs + v7 + 4),
0LL,
1LL);
change = 1;
}
v14 = newBitmap;
v32 = blockSize;
v28 = groupNum;
v15 = localNo(*(unsigned int *)(groupDescs + v7 + 4));
setBlockBitmapSet(v15, v14, v28, v32, x, 1LL);
v16 = *(_DWORD *)(groupDescs + v7 + 8);
v17 = v16 - 1;
v18 = v34 + v16;
if ( v34 >= 0 )
{
do
{
v19 = v17;
v20 = v17++;
if ( !(unsigned int)isBlockBitmapSet(v19, blockBitmap, groupNum, blockSize, x) )
{
printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, v17, 0LL, 1LL);
change = 1;
}
setBlockBitmapSet(v20, newBitmap, groupNum, blockSize, x, 1LL);
}
while ( v18 > (int)v17 );
}
v2 = groupNum;
++v4;
v3 += v33;
}
while ( groupNum > (int)v4 );
}
v21 = x[10];
v22 = malloc(4 * v21 * v2);
v23 = groupNum * v21;
inodeFlag = v22;
if ( (_DWORD)v23 )
memset(v22, 0, 4 * v23);
v24 = localNo(2LL);
checkDataBlock(v24);
free(inodeFlag);
}

canonical_decompiled_code:
void func1()
{
int v0; // ebp
int v1; // eax
__int64 v2; // rdx
unsigned int v3; // er13
__int64 v4; // r14
unsigned int v5; // ebx
unsigned int v6; // er15
__int64 v7; // rbx
__int64 v8; // r12
unsigned int v9; // eax
void *v10; // r12
unsigned int v11; // eax
__int64 v12; // r12
unsigned int v13; // eax
void *v14; // r12
unsigned int v15; // eax
int v16; // ebx
unsigned int v17; // er12
int v18; // ebx
__int64 v19; // rdi
unsigned int v20; // er15
unsigned int v21; // ebx
void *v22; // rax
__int64 v23; // rbx
int v24; // eax
unsigned int v25; // [rsp+0h] [rbp-48h]
unsigned int v26; // [rsp+0h] [rbp-48h]
unsigned int v27; // [rsp+0h] [rbp-48h]
unsigned int v28; // [rsp+0h] [rbp-48h]
unsigned int v29; // [rsp+4h] [rbp-44h]
unsigned int v30; // [rsp+4h] [rbp-44h]
unsigned int v31; // [rsp+4h] [rbp-44h]
unsigned int v32; // [rsp+4h] [rbp-44h]
int v33; // [rsp+8h] [rbp-40h]
int v34; // [rsp+Ch] [rbp-3Ch]

v33 = x[8];
v0 = roundUp((unsigned int)(32 * groupNum), blockSize);
v1 = roundUp((unsigned int)(x[10] << 7), blockSize);
v2 = groupNum;
v34 = v1;
if ( groupNum > 0 )
{
v3 = 0;
v4 = 0LL;
do
{
if ( !(unsigned int)isBlockBitmapSet(v3, blockBitmap, v2, blockSize, x) )
{
printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, v3 + 1, 0LL, 1LL);
change = 1;
}
v5 = v3 + 2;
setBlockBitmapSet(v3, newBitmap, groupNum, blockSize, x, 1LL);
if ( v0 > 0 )
{
do
{
v6 = v5 - 1;
if ( !(unsigned int)isBlockBitmapSet(v5 - 1, blockBitmap, groupNum, blockSize, x) )
{
printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, v5, 0LL, 1LL);
change = 1;
}
++v5;
setBlockBitmapSet(v6, newBitmap, groupNum, blockSize, x, 1LL);
}
while ( v0 >= (int)(~v3 + v5) );
}
v7 = 32 * v4;
v8 = blockBitmap;
v29 = blockSize;
v25 = groupNum;
v9 = localNo(*(unsigned int *)(groupDescs + 32 * v4));
if ( !(unsigned int)isBlockBitmapSet(v9, v8, v25, v29, x) )
{
printf(
"partition %d block %d : bitmap wrong: %d Vs %d\n",
partitionNumber,
*(unsigned int *)(groupDescs + 32 * v4),
0LL,
1LL);
change = 1;
}
v10 = newBitmap;
v30 = blockSize;
v26 = groupNum;
v11 = localNo(*(unsigned int *)(groupDescs + 32 * v4));
setBlockBitmapSet(v11, v10, v26, v30, x, 1LL);
v12 = blockBitmap;
v31 = blockSize;
v27 = groupNum;
v13 = localNo(*(unsigned int *)(groupDescs + v7 + 4));
if ( !(unsigned int)isBlockBitmapSet(v13, v12, v27, v31, x) )
{
printf(
"partition %d block %d : bitmap wrong: %d Vs %d\n",
partitionNumber,
*(unsigned int *)(groupDescs + v7 + 4),
0LL,
1LL);
change = 1;
}
v14 = newBitmap;
v32 = blockSize;
v28 = groupNum;
v15 = localNo(*(unsigned int *)(groupDescs + v7 + 4));
setBlockBitmapSet(v15, v14, v28, v32, x, 1LL);
v16 = *(_DWORD *)(groupDescs + v7 + 8);
v17 = v16 - 1;
v18 = v34 + v16;
if ( v34 >= 0 )
{
do
{
v19 = v17;
v20 = v17++;
if ( !(unsigned int)isBlockBitmapSet(v19, blockBitmap, groupNum, blockSize, x) )
{
printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, v17, 0LL, 1LL);
change = 1;
}
setBlockBitmapSet(v20, newBitmap, groupNum, blockSize, x, 1LL);
}
while ( v18 > (int)v17 );
}
v2 = groupNum;
++v4;
v3 += v33;
}
while ( groupNum > (int)v4 );
}
v21 = x[10];
v22 = malloc(4 * v21 * v2);
v23 = groupNum * v21;
inodeFlag = v22;
if ( (_DWORD)v23 )
memset(v22, 0, 4 * v23);
v24 = localNo(2LL);
func0(v24);
free(inodeFlag);
}

original_code:
void checkBlockBitmap() {
 int i, j, temp;
 int blockPerGroup = ((&x)->s_blocks_per_group);
 int bitmap;
 int groupBlock = roundUp(sizeof(struct ext2_group_desc) * groupNum, blockSize);
 int inodeTableBlock = roundUp(sizeof(struct ext2_inode) * ((&x)->s_inodes_per_group), blockSize);
 int used = 0;
 for (i = 0; i < groupNum; ++i) {
  ++used;
  bitmap = isBlockBitmapSet(i * blockPerGroup + 0, blockBitmap, groupNum, blockSize, &x);
  if (!bitmap) {
   printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, i * blockPerGroup + 1, 0, 1);
   change = 1;
  }
  setBlockBitmapSet(i * blockPerGroup + 0, newBitmap, groupNum, blockSize, &x, 1);
  for (j = 1; j <= groupBlock; ++j) {
   ++used;
   bitmap = isBlockBitmapSet(i * blockPerGroup + j, blockBitmap, groupNum, blockSize, &x);
   if (!bitmap) {
    printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, i * blockPerGroup + j + 1, 0, 1);
    change = 1;
   }
   setBlockBitmapSet(i * blockPerGroup + j, newBitmap, groupNum, blockSize, &x, 1);
  }
  ++used;
  bitmap = isBlockBitmapSet(localNo(groupDescs[i].bg_block_bitmap), blockBitmap, groupNum, blockSize, &x);
  if (!bitmap) {
   printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, groupDescs[i].bg_block_bitmap, 0, 1);
   change = 1;
  }
  setBlockBitmapSet(localNo(groupDescs[i].bg_block_bitmap), newBitmap, groupNum, blockSize, &x, 1);
  ++used;
  bitmap = isBlockBitmapSet(localNo(groupDescs[i].bg_inode_bitmap), blockBitmap, groupNum, blockSize, &x);
  if (!bitmap) {
   printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, groupDescs[i].bg_inode_bitmap, 0, 1);
   change = 1;
  }
  setBlockBitmapSet(localNo(groupDescs[i].bg_inode_bitmap), newBitmap, groupNum, blockSize, &x, 1);
  temp = groupDescs[i].bg_inode_table;
  for (j = temp - 1; j < temp + inodeTableBlock; ++j) {
   ++used;
   bitmap = isBlockBitmapSet(j, blockBitmap, groupNum, blockSize, &x);
   if (!bitmap) {
    printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, j + 1, 0, 1);
    change = 1;
   }
   setBlockBitmapSet(j, newBitmap, groupNum, blockSize, &x, 1);
  }
 }
 inodeFlag = malloc(sizeof(int) * groupNum * ((&x)->s_inodes_per_group));
 for (i = 0; i < groupNum * ((&x)->s_inodes_per_group); ++i)
  inodeFlag[i] = 0;
 checkDataBlock(localNo(2));
 free(inodeFlag);
}

canonical_original_code:
void checkBlockBitmap() {
 int i; int j; int temp;
 int blockPerGroup = ((&x)->s_blocks_per_group);
 int bitmap;
 int groupBlock = roundUp(sizeof(struct ext2_group_desc) * groupNum, blockSize);
 int inodeTableBlock = roundUp(sizeof(struct ext2_inode) * ((&x)->s_inodes_per_group), blockSize);
 int used = 0;
 for (i = 0; i < groupNum; ++i) {
  ++used;
  bitmap = isBlockBitmapSet(i * blockPerGroup + 0, blockBitmap, groupNum, blockSize, &x);
  if (!bitmap) {
   printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, i * blockPerGroup + 1, 0, 1);
   change = 1;
  }
  setBlockBitmapSet(i * blockPerGroup + 0, newBitmap, groupNum, blockSize, &x, 1);
  for (j = 1; j <= groupBlock; ++j) {
   ++used;
   bitmap = isBlockBitmapSet(i * blockPerGroup + j, blockBitmap, groupNum, blockSize, &x);
   if (!bitmap) {
    printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, i * blockPerGroup + j + 1, 0, 1);
    change = 1;
   }
   setBlockBitmapSet(i * blockPerGroup + j, newBitmap, groupNum, blockSize, &x, 1);
  }
  ++used;
  bitmap = isBlockBitmapSet(localNo(groupDescs[i].bg_block_bitmap), blockBitmap, groupNum, blockSize, &x);
  if (!bitmap) {
   printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, groupDescs[i].bg_block_bitmap, 0, 1);
   change = 1;
  }
  setBlockBitmapSet(localNo(groupDescs[i].bg_block_bitmap), newBitmap, groupNum, blockSize, &x, 1);
  ++used;
  bitmap = isBlockBitmapSet(localNo(groupDescs[i].bg_inode_bitmap), blockBitmap, groupNum, blockSize, &x);
  if (!bitmap) {
   printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, groupDescs[i].bg_inode_bitmap, 0, 1);
   change = 1;
  }
  setBlockBitmapSet(localNo(groupDescs[i].bg_inode_bitmap), newBitmap, groupNum, blockSize, &x, 1);
  temp = groupDescs[i].bg_inode_table;
  for (j = temp - 1; j < temp + inodeTableBlock; ++j) {
   ++used;
   bitmap = isBlockBitmapSet(j, blockBitmap, groupNum, blockSize, &x);
   if (!bitmap) {
    printf("partition %d block %d : bitmap wrong: %d Vs %d\n", partitionNumber, j + 1, 0, 1);
    change = 1;
   }
   setBlockBitmapSet(j, newBitmap, groupNum, blockSize, &x, 1);
  }
 }
 inodeFlag = malloc(sizeof(int) * groupNum * ((&x)->s_inodes_per_group));
 for (i = 0; i < groupNum * ((&x)->s_inodes_per_group); ++i)
  inodeFlag[i] = 0;
 checkDataBlock(localNo(2));
 free(inodeFlag);
}

variable_types: temp int

variable_types: blockPerGroup int

variable_types: bitmap int

variable_types: groupBlock int

variable_types: inodeTableBlock int

variable_types: used int


decompiled_code:
__int64 pass4()
{
unsigned int v0; // eax
__int64 v1; // rdx
unsigned int v2; // ebx
__int64 v3; // rbp
__int64 v4; // r8
int v5; // edi
unsigned int v6; // er12
unsigned int v7; // eax
__int64 v8; // r9
__int64 v9; // rbx
int v10; // edi
int v11; // edx
_BYTE *v12; // rdx
__int64 v13; // rax
unsigned int v14; // ebx
unsigned int v15; // er13
__int64 v16; // rdi
unsigned int v17; // er12
unsigned int v18; // eax
unsigned int v19; // ebp
__int64 v20; // rbx
__int64 result; // rax
int v22; // edi
int v23; // edx

change = 0;
v0 = localNo(2LL);
v1 = groupNum;
v2 = v0;
if ( groupNum * x[10] > v0 )
{
v3 = 4LL * (int)v0;
while ( 1 )
{
v6 = v2++;
if ( v6 - 2 > 7 )
{
v7 = isInodeBitmapSet(v6, inodeBitmap, v1, blockSize, x);
if ( v7 )
{
v1 = groupNum;
if ( !*(_DWORD *)(inodeLink + v3) )
{
printf(
"partition %d inode %d : bitmap is inconsistent with inode table: %d Vs %d\n",
partitionNumber,
v2,
v7,
0LL);
v8 = 0LL;
LABEL_9:
setInodeBitmapSet(v6, inodeBitmap, groupNum, blockSize, x, v8);
v1 = groupNum;
change = 1;
goto LABEL_4;
}
}
else
{
v1 = groupNum;
v4 = *(unsigned int *)(inodeLink + v3);
if ( (_DWORD)v4 )
{
printf(
"partition %d inode %d : bitmap is inconsistent with inode table: %d Vs %d\n",
partitionNumber,
v2,
0LL,
v4);
v8 = 1LL;
goto LABEL_9;
}
}
}
LABEL_4:
v5 = v1;
v3 += 4LL;
if ( (int)v1 * x[10] <= v2 )
goto LABEL_11;
}
}
v5 = groupNum;
LABEL_11:
if ( change && (int)v1 > 0 )
{
v9 = 0LL;
do
{
v10 = *(_DWORD *)(groupDescs + 32 * v9 + 4);
v11 = v9++ * blockSize;
writeDisk((unsigned int)((start << 9) + blockSize * v10), blockSize, inodeBitmap + v11);
v5 = groupNum;
}
while ( groupNum > (int)v9 );
}
newBitmap = malloc(blockSize * v5 + 1);
v12 = newBitmap;
if ( groupNum * blockSize >= 0 )
{
v13 = 0LL;
while ( 1 )
{
v12[v13++] = 0;
if ( groupNum * blockSize < (int)v13 )
break;
v12 = newBitmap;
}
}
v14 = 0;
checkBlockBitmap();
if ( x[1] != 1 )
{
do
{
v15 = v14;
v16 = v14;
v17 = isBlockBitmapSet(v14++, blockBitmap, groupNum, blockSize, x);
v18 = isBlockBitmapSet(v16, newBitmap, groupNum, blockSize, x);
v19 = v18;
if ( v17 != v18 )
{
printf("partition %d block bitmap: block %d: %d Vs %d\n", partitionNumber, v14, v17, v18);
setBlockBitmapSet(v15, blockBitmap, groupNum, blockSize, x, v19);
}
}
while ( x[1] - 1 > v14 );
}
v20 = 0LL;
free(newBitmap);
result = groupNum;
if ( groupNum > 0 )
{
do
{
v22 = *(_DWORD *)(groupDescs + 32 * v20);
v23 = v20++ * blockSize;
result = writeDisk((unsigned int)((start << 9) + blockSize * v22), blockSize, blockBitmap + v23);
}
while ( groupNum > (int)v20 );
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // eax
__int64 v1; // rdx
unsigned int v2; // ebx
__int64 v3; // rbp
__int64 v4; // r8
int v5; // edi
unsigned int v6; // er12
unsigned int v7; // eax
__int64 v8; // r9
__int64 v9; // rbx
int v10; // edi
int v11; // edx
_BYTE *v12; // rdx
__int64 v13; // rax
unsigned int v14; // ebx
unsigned int v15; // er13
__int64 v16; // rdi
unsigned int v17; // er12
unsigned int v18; // eax
unsigned int v19; // ebp
__int64 v20; // rbx
__int64 result; // rax
int v22; // edi
int v23; // edx

change = 0;
v0 = localNo(2LL);
v1 = groupNum;
v2 = v0;
if ( groupNum * x[10] > v0 )
{
v3 = 4LL * (int)v0;
while ( 1 )
{
v6 = v2++;
if ( v6 - 2 > 7 )
{
v7 = isInodeBitmapSet(v6, inodeBitmap, v1, blockSize, x);
if ( v7 )
{
v1 = groupNum;
if ( !*(_DWORD *)(inodeLink + v3) )
{
printf(
"partition %d inode %d : bitmap is inconsistent with inode table: %d Vs %d\n",
partitionNumber,
v2,
v7,
0LL);
v8 = 0LL;
LABEL_9:
setInodeBitmapSet(v6, inodeBitmap, groupNum, blockSize, x, v8);
v1 = groupNum;
change = 1;
goto LABEL_4;
}
}
else
{
v1 = groupNum;
v4 = *(unsigned int *)(inodeLink + v3);
if ( (_DWORD)v4 )
{
printf(
"partition %d inode %d : bitmap is inconsistent with inode table: %d Vs %d\n",
partitionNumber,
v2,
0LL,
v4);
v8 = 1LL;
goto LABEL_9;
}
}
}
LABEL_4:
v5 = v1;
v3 += 4LL;
if ( (int)v1 * x[10] <= v2 )
goto LABEL_11;
}
}
v5 = groupNum;
LABEL_11:
if ( change && (int)v1 > 0 )
{
v9 = 0LL;
do
{
v10 = *(_DWORD *)(groupDescs + 32 * v9 + 4);
v11 = v9++ * blockSize;
writeDisk((unsigned int)((start << 9) + blockSize * v10), blockSize, inodeBitmap + v11);
v5 = groupNum;
}
while ( groupNum > (int)v9 );
}
newBitmap = malloc(blockSize * v5 + 1);
v12 = newBitmap;
if ( groupNum * blockSize >= 0 )
{
v13 = 0LL;
while ( 1 )
{
v12[v13++] = 0;
if ( groupNum * blockSize < (int)v13 )
break;
v12 = newBitmap;
}
}
v14 = 0;
func1();
if ( x[1] != 1 )
{
do
{
v15 = v14;
v16 = v14;
v17 = isBlockBitmapSet(v14++, blockBitmap, groupNum, blockSize, x);
v18 = isBlockBitmapSet(v16, newBitmap, groupNum, blockSize, x);
v19 = v18;
if ( v17 != v18 )
{
printf("partition %d block bitmap: block %d: %d Vs %d\n", partitionNumber, v14, v17, v18);
setBlockBitmapSet(v15, blockBitmap, groupNum, blockSize, x, v19);
}
}
while ( x[1] - 1 > v14 );
}
v20 = 0LL;
free(newBitmap);
result = groupNum;
if ( groupNum > 0 )
{
do
{
v22 = *(_DWORD *)(groupDescs + 32 * v20);
v23 = v20++ * blockSize;
result = writeDisk((unsigned int)((start << 9) + blockSize * v22), blockSize, blockBitmap + v23);
}
while ( groupNum > (int)v20 );
}
return result;
}

original_code:
void pass4() {
 int i;
 int bitmap;
 change = 0;
 for (i = localNo(2); i < groupNum * ((&x)->s_inodes_per_group); ++i) {
  if (i >= 2 && i <= 9) continue;
  bitmap = isInodeBitmapSet(i, inodeBitmap, groupNum, blockSize, &x);
  if (bitmap && !inodeLink[i]) {
   printf("partition %d inode %d : bitmap is inconsistent with inode table: %d Vs %d\n", partitionNumber, i + 1, bitmap, inodeLink[i]);
   setInodeBitmapSet(i, inodeBitmap, groupNum, blockSize, &x, 0);
   change = 1;
  } else if (!bitmap && inodeLink[i]) {
   printf("partition %d inode %d : bitmap is inconsistent with inode table: %d Vs %d\n", partitionNumber, i + 1, bitmap, inodeLink[i]);
   setInodeBitmapSet(i, inodeBitmap, groupNum, blockSize, &x, 1);
   change = 1;
  }
 }
 if (change) {
  for (i = 0; i < groupNum; ++i)
   writeDisk(start * 512 + groupDescs[i].bg_inode_bitmap * blockSize, blockSize, inodeBitmap + i * blockSize);
 }
 newBitmap = malloc(blockSize * groupNum + 1);
 for (i = 0; i < blockSize * groupNum + 1; ++i)
  newBitmap[i] = 0;
 checkBlockBitmap();
 int oldbit;
 int newbit;
 for (i = 0; i < x.s_blocks_count - 1; ++i) {
  oldbit = isBlockBitmapSet(i, blockBitmap, groupNum, blockSize, &x);
  newbit = isBlockBitmapSet(i, newBitmap, groupNum, blockSize, &x);
  if (oldbit != newbit) {
   printf("partition %d block bitmap: block %d: %d Vs %d\n", partitionNumber, i + 1, oldbit, newbit);
   setBlockBitmapSet(i, blockBitmap, groupNum, blockSize, &x, newbit);
  }
 }
 free(newBitmap);
 for (i = 0; i < groupNum; ++i)
  writeDisk(start * 512 + groupDescs[i].bg_block_bitmap * blockSize, blockSize, blockBitmap + i * blockSize);
}

variable_types: oldbit int

variable_types: newbit int


decompiled_code:
__int64 pass3()
{
signed int v0; // eax
int v1; // edx
unsigned int v2; // ebx
__int64 v3; // rbp
__int64 result; // rax
unsigned int v5; // er14
__int64 v6; // r13
__int64 v7; // r8
__int64 v8; // rcx
int v9; // [rsp+8h] [rbp-30h] BYREF
int v10[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v0 = localNo(2LL);
v1 = x[10];
v2 = v0;
v3 = 4LL * v0;
for ( result = (unsigned int)(v1 * groupNum); (unsigned int)result > v2; result = (unsigned int)(v1 * groupNum) )
{
v5 = v2++;
v6 = 32 * v3 + inodeTable;
v7 = *(unsigned int *)(inodeLink + v3);
v8 = *(unsigned __int16 *)(v6 + 26);
if ( (_DWORD)v8 != (_DWORD)v7 )
{
printf("partition %d inode %d inconsistent link count: %d Vs %d\n", partitionNumber, v2, v8, v7);
*(_WORD *)(v6 + 26) = *(_DWORD *)(inodeLink + v3);
getInodeGroupBlock(v5, &v9, v10, x);
writeDisk(
(unsigned int)((v10[0] << 7) + (start << 9) + blockSize * *(_DWORD *)(groupDescs + 32LL * v9 + 8)),
128LL,
32 * v3 + inodeTable);
v1 = x[10];
}
v3 += 4LL;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
signed int v0; // eax
int v1; // edx
unsigned int v2; // ebx
__int64 v3; // rbp
__int64 result; // rax
unsigned int v5; // er14
__int64 v6; // r13
__int64 v7; // r8
__int64 v8; // rcx
int v9; // [rsp+8h] [rbp-30h] BYREF
int v10[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v0 = localNo(2LL);
v1 = x[10];
v2 = v0;
v3 = 4LL * v0;
for ( result = (unsigned int)(v1 * groupNum); (unsigned int)result > v2; result = (unsigned int)(v1 * groupNum) )
{
v5 = v2++;
v6 = 32 * v3 + inodeTable;
v7 = *(unsigned int *)(inodeLink + v3);
v8 = *(unsigned __int16 *)(v6 + 26);
if ( (_DWORD)v8 != (_DWORD)v7 )
{
printf("partition %d inode %d inconsistent link count: %d Vs %d\n", partitionNumber, v2, v8, v7);
*(_WORD *)(v6 + 26) = *(_DWORD *)(inodeLink + v3);
getInodeGroupBlock(v5, &v9, v10, x);
writeDisk(
(unsigned int)((v10[0] << 7) + (start << 9) + blockSize * *(_DWORD *)(groupDescs + 32LL * v9 + 8)),
128LL,
32 * v3 + inodeTable);
v1 = x[10];
}
v3 += 4LL;
}
return result;
}

original_code:
void pass3() {
 int i;
 struct ext2_inode *inode;
 for (i = localNo(2); i< groupNum * ((&x)->s_inodes_per_group); ++i) {
  inode = inodeTable + i;
  if (inode->i_links_count != inodeLink[i]) {
   printf("partition %d inode %d inconsistent link count: %d Vs %d\n", partitionNumber, i + 1, inode->i_links_count, inodeLink[i]);
   inode->i_links_count = inodeLink[i];
   int groupNo;
   int groupInodeNo;
   getInodeGroupBlock(i, &groupNo, &groupInodeNo, &x);
   writeDisk(start * 512 + groupDescs[groupNo].bg_inode_table * blockSize + sizeof(struct ext2_inode) * groupInodeNo, sizeof(struct ext2_inode), inodeTable + i);
  }
 }
}

variable_types: i int

variable_types: struct ext2_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_atime; unsigned int i_ctime; unsigned int i_mtime; unsigned int i_dtime; unsigned short i_gid; unsigned short i_links_count; unsigned int i_blocks; unsigned int i_flags; union { struct { unsigned int l_i_reserved1; } linux1; struct { unsigned int h_i_translator; } hurd1; struct { unsigned int m_i_reserved1; } masix1; } osd1; unsigned int i_block[-1]; unsigned int i_generation; unsigned int i_file_acl; unsigned int i_dir_acl; unsigned int i_faddr; union { struct { unsigned char l_i_frag; unsigned char l_i_fsize; unsigned short i_pad1; unsigned short l_i_uid_high; unsigned short l_i_gid_high; unsigned int l_i_reserved2; } linux2; struct { unsigned char h_i_frag; unsigned char h_i_fsize; unsigned short h_i_mode_high; unsigned short h_i_uid_high; unsigned short h_i_gid_high; unsigned int h_i_author; } hurd2; struct { unsigned char m_i_frag; unsigned char m_i_fsize; unsigned short m_pad1; unsigned int m_i_reserved2[2]; } masix2; } osd2; } *

variable_types: groupNo int

variable_types: groupInodeNo int


init

decompiled_code:
void *__fastcall init(int a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // ecx
unsigned int v4; // er12
unsigned int v5; // ebx
unsigned int v6; // er12
unsigned int v7; // ebx
unsigned int v8; // er12
unsigned int v9; // er12
__int64 v10; // rbx
void *result; // rax

start = a2;
partitionNumber = a1;
getSuperBlock(a2, x);
v2 = getGroupNumber(x);
v3 = x[6];
v4 = v2;
groupNum = v2;
blockSize = 1024 << v3;
groupDescs = malloc(32LL * v2);
getGroupDescs((unsigned int)start, groupDescs, v4);
v5 = blockSize;
v6 = groupNum;
blockBitmap = malloc(groupNum * blockSize + 1);
getBlockBitmap((unsigned int)start, groupDescs, v6, blockBitmap, v5);
v7 = blockSize;
v8 = groupNum;
inodeBitmap = malloc(groupNum * blockSize + 1);
getInodeBitmap((unsigned int)start, groupDescs, v8, inodeBitmap, v7);
v9 = groupNum;
inodeTable = malloc((groupNum * (unsigned __int64)x[10]) << 7);
getInodeTable((unsigned int)start, x, groupDescs, v9, inodeTable, (unsigned int)blockSize);
v10 = x[10] * groupNum;
result = malloc(4 * x[10] * (__int64)groupNum);
inodeLink = result;
if ( (_DWORD)v10 )
result = memset(result, 0, 4 * v10);
return result;
}

canonical_decompiled_code:
void *__fastcall func0(int a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // ecx
unsigned int v4; // er12
unsigned int v5; // ebx
unsigned int v6; // er12
unsigned int v7; // ebx
unsigned int v8; // er12
unsigned int v9; // er12
__int64 v10; // rbx
void *result; // rax

start = a2;
partitionNumber = a1;
getSuperBlock(a2, x);
v2 = getGroupNumber(x);
v3 = x[6];
v4 = v2;
groupNum = v2;
blockSize = 1024 << v3;
groupDescs = malloc(32LL * v2);
getGroupDescs((unsigned int)start, groupDescs, v4);
v5 = blockSize;
v6 = groupNum;
blockBitmap = malloc(groupNum * blockSize + 1);
getBlockBitmap((unsigned int)start, groupDescs, v6, blockBitmap, v5);
v7 = blockSize;
v8 = groupNum;
inodeBitmap = malloc(groupNum * blockSize + 1);
getInodeBitmap((unsigned int)start, groupDescs, v8, inodeBitmap, v7);
v9 = groupNum;
inodeTable = malloc((groupNum * (unsigned __int64)x[10]) << 7);
getInodeTable((unsigned int)start, x, groupDescs, v9, inodeTable, (unsigned int)blockSize);
v10 = x[10] * groupNum;
result = malloc(4 * x[10] * (__int64)groupNum);
inodeLink = result;
if ( (_DWORD)v10 )
result = memset(result, 0, 4 * v10);
return result;
}

original_code:
void init(int pN, int parStart) {
 partitionNumber = pN;
 start = parStart;
 getSuperBlock(start, &x);
 groupNum = getGroupNumber(&x);
 blockSize = (1024 << (&x)->s_log_block_size);
 groupDescs = malloc(groupNum * sizeof(struct ext2_group_desc));
 getGroupDescs(start, groupDescs, groupNum);
 blockBitmap = malloc(blockSize * groupNum + 1);
 getBlockBitmap(start, groupDescs, groupNum, blockBitmap, blockSize);
 inodeBitmap = malloc(blockSize * groupNum + 1);
 getInodeBitmap(start, groupDescs, groupNum, inodeBitmap, blockSize);
 inodeTable = malloc(sizeof(struct ext2_inode) * groupNum * ((&x)->s_inodes_per_group));
 getInodeTable(start, &x, groupDescs, groupNum, inodeTable, blockSize);
 inodeLink = malloc(sizeof(int) * groupNum * ((&x)->s_inodes_per_group));
 int i;
 for (i = 0; i < groupNum * ((&x)->s_inodes_per_group); ++i)
  inodeLink[i] = 0;
}

variable_types: pN int

variable_types: parStart int

user_defined_types: struct ext2_group_desc { unsigned int bg_block_bitmap; unsigned int bg_inode_bitmap; unsigned int bg_inode_table; unsigned short bg_free_blocks_count; unsigned short bg_free_inodes_count; unsigned short bg_used_dirs_count; unsigned short bg_pad; unsigned int bg_reserved[3]; }


cleanup

decompiled_code:
void cleanup()
{
free(groupDescs);
free(blockBitmap);
free(inodeBitmap);
free(inodeTable);
free(inodeLink);
}

canonical_decompiled_code:
void func1()
{
free(groupDescs);
free(blockBitmap);
free(inodeBitmap);
free(inodeTable);
free(inodeLink);
}


printPartitionInfo

decompiled_code:
void __fastcall printPartitionInfo(int a1)
{
__int64 v1; // rbx
int *v2; // r13
int *v3; // rbp
int v4; // eax
__int64 v5; // rsi
int v6; // ebp
int v7; // ebx
int v8; // eax
int *v9; // r13
int v10; // eax
bool v11; // zf
_BYTE v12[4]; // [rsp+0h] [rbp-98h] BYREF
char v13[148]; // [rsp+4h] [rbp-94h]

device = open(diskFileName, 2);
if ( device == -1 )
{
perror("Open disk image error");
}
else
{
v1 = 0LL;
readDisk(446LL, 64LL, v12);
v2 = par;
v3 = par;
do
{
v3 += 3;
*(_BYTE *)v3 = v13[v1];
v4 = getIntFromBuf(v12, (unsigned int)(v1 + 8));
v5 = (unsigned int)(v1 + 12);
v1 += 16LL;
v3[1] = v4;
v3[2] = getIntFromBuf(v12, v5);
}
while ( v1 != 64 );
pNum = 4;
v6 = -1;
do
{
if ( *((_BYTE *)v2 + 12) == 5 )
v6 = v2[4];
v2 += 3;
}
while ( v2 != &par[12] );
if ( v6 != -1 )
{
v7 = v6;
do
{
readDisk((unsigned int)((v7 << 9) + 446), 64LL, v12);
LOBYTE(par[3 * ++pNum]) = v13[0];
v8 = getIntFromBuf(v12, 8LL);
v9 = &par[3 * pNum];
v9[1] = v8 + v7;
v10 = getIntFromBuf(v12, 12LL);
v11 = v13[16] == 5;
v9[2] = v10;
if ( !v11 )
break;
v7 = getIntFromBuf(v12, 24LL) + v6;
getIntFromBuf(v12, 28LL);
}
while ( v7 != -1 );
}
if ( !a1 )
{
if ( theParNum > 0 && theParNum <= pNum )
printf(
"0x%02X %d %d\n",
LOBYTE(par[3 * theParNum]),
(unsigned int)par[3 * theParNum + 1],
(unsigned int)par[3 * theParNum + 2]);
else
puts("-1");
}
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
__int64 v1; // rbx
int *v2; // r13
int *v3; // rbp
int v4; // eax
__int64 v5; // rsi
int v6; // ebp
int v7; // ebx
int v8; // eax
int *v9; // r13
int v10; // eax
bool v11; // zf
_BYTE v12[4]; // [rsp+0h] [rbp-98h] BYREF
char v13[148]; // [rsp+4h] [rbp-94h]

device = open(diskFileName, 2);
if ( device == -1 )
{
perror("Open disk image error");
}
else
{
v1 = 0LL;
readDisk(446LL, 64LL, v12);
v2 = par;
v3 = par;
do
{
v3 += 3;
*(_BYTE *)v3 = v13[v1];
v4 = getIntFromBuf(v12, (unsigned int)(v1 + 8));
v5 = (unsigned int)(v1 + 12);
v1 += 16LL;
v3[1] = v4;
v3[2] = getIntFromBuf(v12, v5);
}
while ( v1 != 64 );
pNum = 4;
v6 = -1;
do
{
if ( *((_BYTE *)v2 + 12) == 5 )
v6 = v2[4];
v2 += 3;
}
while ( v2 != &par[12] );
if ( v6 != -1 )
{
v7 = v6;
do
{
readDisk((unsigned int)((v7 << 9) + 446), 64LL, v12);
LOBYTE(par[3 * ++pNum]) = v13[0];
v8 = getIntFromBuf(v12, 8LL);
v9 = &par[3 * pNum];
v9[1] = v8 + v7;
v10 = getIntFromBuf(v12, 12LL);
v11 = v13[16] == 5;
v9[2] = v10;
if ( !v11 )
break;
v7 = getIntFromBuf(v12, 24LL) + v6;
getIntFromBuf(v12, 28LL);
}
while ( v7 != -1 );
}
if ( !a1 )
{
if ( theParNum > 0 && theParNum <= pNum )
printf(
"0x%02X %d %d\n",
LOBYTE(par[3 * theParNum]),
(unsigned int)par[3 * theParNum + 1],
(unsigned int)par[3 * theParNum + 2]);
else
puts("-1");
}
}
}

original_code:
void printPartitionInfo(int fsckFlag) {
 if ((device = open(diskFileName, 02)) == -1) {
  perror("Open disk image error");
  return;
 }
 int i;
 unsigned char buf[100];
 readDisk(446, 64, buf);
 for (i = 0; i < 4; ++i) {
  par[i + 1].type = buf[i*16 + 4];
  par[i + 1].start = getIntFromBuf(buf, i*16 + 8);
  par[i + 1].length = getIntFromBuf(buf, i*16 + 12);
 }
 pNum = 4;
 int extendBase = -1;
 int extendStart = -1;
 int extendLength = 0;
 for (i = 1; i <= pNum; ++i)
  if (par[i].type == 5) {
   extendBase = par[i].start;
   extendStart = par[i].start;
   extendLength = par[i].length;
  }
 while (extendStart != -1) {
  readDisk(446 + extendStart * 512, 64, buf);
  ++pNum;
  par[pNum].type = buf[4];
  par[pNum].start = extendStart + getIntFromBuf(buf, 8);
  par[pNum].length = getIntFromBuf(buf, 12);
  if (buf[16 + 4] != 5) {
   extendStart = -1;
  } else {
   extendStart = extendBase + getIntFromBuf(buf, 16 + 8);
   extendLength = getIntFromBuf(buf, 16 + 12);
  }
 }
 if (fsckFlag) return;
 if (theParNum > 0 && theParNum <= pNum)
  printf("0x%02X %d %d\n", par[theParNum].type, par[theParNum].start, par[theParNum].length);
 else
  printf("-1\n");
 return;
}

variable_types: fsckFlag int

variable_types:
buf
unsigned char [100]

variable_types: extendBase int

variable_types: extendStart int

variable_types: extendLength int


decompiled_code:
__int64 __fastcall app_singleton(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
unsigned int v3; // eax
int v4; // ebx
int v6; // edi
int v7[8]; // [rsp+0h] [rbp-78h] BYREF
char s[16]; // [rsp+20h] [rbp-58h] BYREF
__int128 v9; // [rsp+30h] [rbp-48h]
__int128 v10; // [rsp+40h] [rbp-38h]
__int128 v11; // [rsp+50h] [rbp-28h]

*(_OWORD *)s = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v1 = open(a1, 66, 384LL);
if ( v1 == -1 )
return -1;
v2 = v1;
v7[0] = 1;
if ( fcntl(v1, 6, v7) < 0 )
{
v6 = v2;
v2 = -1;
close(v6);
return v2;
}
v3 = getpid();
v4 = snprintf(s, 0x40uLL, "%u", v3);
if ( v4 != (unsigned int)write(v2, s, v4) )
return -1;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
unsigned int v3; // eax
int v4; // ebx
int v6; // edi
int v7[8]; // [rsp+0h] [rbp-78h] BYREF
char s[16]; // [rsp+20h] [rbp-58h] BYREF
__int128 v9; // [rsp+30h] [rbp-48h]
__int128 v10; // [rsp+40h] [rbp-38h]
__int128 v11; // [rsp+50h] [rbp-28h]

*(_OWORD *)s = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v1 = open(a1, 66, 384LL);
if ( v1 == -1 )
return -1;
v2 = v1;
v7[0] = 1;
if ( fcntl(v1, 6, v7) < 0 )
{
v6 = v2;
v2 = -1;
close(v6);
return v2;
}
v3 = getpid();
v4 = snprintf(s, 0x40uLL, "%u", v3);
if ( v4 != (unsigned int)write(v2, s, v4) )
return -1;
return v2;
}

original_code:
int app_singleton(char *pid_file)
{
    int fd = -1;
    int m = 0;
    int n = 0;
    char buf[64] = {0};
    struct flock lock;
    fd = open(pid_file, 02|0100, 0400|0200);
    if(-1 == fd){
        return -1;
    }
    lock.l_type = 1;
    lock.l_whence = 0;
    if (fcntl(fd, 6, &lock) < 0){
        close(fd);
        return -1;
    }
    m = snprintf(buf, 64, "%u", (unsigned int)getpid());
    n = write(fd, buf, m);
    if(n != m){
        return -1;
    }
    return fd;
}

variable_types: fd int

variable_types: m int

variable_types: n int

variable_types:
buf
char [64]

variable_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }


repo: Wescoeur/Reversi

original_code:
int ia_eval_grid(Reversi *reversi, Player player)
{
  int i, n = 0;
  for(i = 0; i < 8 * 8; i++)
    if(reversi->array[i] == player)
      n += grid[i];
    else if(reversi->array[i] == (player == 'X' ? 'O' : 'X'))
      n -= grid[i];
  return n;
}

canonical_original_code:
int ia_eval_grid(struct Reversi *reversi, char player)
{
  int i; int n = 0;
  for(i = 0; i < 8 * 8; i++)
    if(reversi->array[i] == player)
      n += grid[i];
    else if(reversi->array[i] == (player == 'X' ? 'O' : 'X'))
      n -= grid[i];
  return n;
}

variable_types: struct Reversi { char *array; unsigned int n_moves; } *

variable_types: player char


decompiled_code:
__int64 __fastcall ia_alphabeta_bis(__int64 *a1, int a2, unsigned int a3, unsigned int a4, char a5, int a6)
{
unsigned int v6; // er14
__int64 result; // rax
int v11; // er9
int i; // ebx
__m128i **v13; // rbp
int j; // ebx
__m128i **v15; // rbp
int v17; // [rsp+Ch] [rbp-5Ch]
unsigned int v18; // [rsp+18h] [rbp-50h]
unsigned int v19; // [rsp+1Ch] [rbp-4Ch]
int v20; // [rsp+28h] [rbp-40h] BYREF
int v21; // [rsp+2Ch] [rbp-3Ch]

v6 = a5;
if ( !a2 || (unsigned int)reversi_game_over() )
return ia_eval_grid(a1, v6);
v11 = a6;
v17 = 0;
v18 = 9 * (a5 != 88) + 79;
v19 = a2 - 1;
if ( v11 )
{
do
{
for ( i = 0; i != 8; ++i )
{
v20 = i;
v21 = v17;
v13 = _grid_copy((const __m128i **)a1);
if ( (unsigned int)reversi_set_ia_move(v13, v6, &v20) )
{
if ( (int)ia_alphabeta_bis(v13, v19, a3, a4, v18, 0LL) < (int)a3 )
{
if ( (int)a3 >= (int)a4 )
goto LABEL_11;
}
else
{
a3 = ia_alphabeta_bis(v13, v19, a3, a4, v18, 0LL);
if ( (int)a3 >= (int)a4 )
{
LABEL_11:
reversi_free(v13);
return a3;
}
}
}
reversi_free(v13);
}
++v17;
}
while ( v17 != 8 );
result = a3;
}
else
{
while ( 2 )
{
for ( j = 0; j != 8; ++j )
{
v20 = j;
v21 = v17;
v15 = _grid_copy((const __m128i **)a1);
if ( (unsigned int)reversi_set_ia_move(v15, v6, &v20) )
{
if ( (int)ia_alphabeta_bis(v15, v19, a3, a4, v18, 1LL) > (int)a4 )
{
if ( (int)a4 <= (int)a3 )
goto LABEL_19;
}
else
{
a4 = ia_alphabeta_bis(v15, v19, a3, a4, v18, 1LL);
if ( (int)a4 <= (int)a3 )
{
LABEL_19:
reversi_free(v15);
return a4;
}
}
}
reversi_free(v15);
}
if ( ++v17 != 8 )
continue;
break;
}
result = a4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1, int a2, unsigned int a3, unsigned int a4, char a5, int a6)
{
unsigned int v6; // er14
__int64 result; // rax
int v11; // er9
int i; // ebx
__m128i **v13; // rbp
int j; // ebx
__m128i **v15; // rbp
int v17; // [rsp+Ch] [rbp-5Ch]
unsigned int v18; // [rsp+18h] [rbp-50h]
unsigned int v19; // [rsp+1Ch] [rbp-4Ch]
int v20; // [rsp+28h] [rbp-40h] BYREF
int v21; // [rsp+2Ch] [rbp-3Ch]

v6 = a5;
if ( !a2 || (unsigned int)reversi_game_over() )
return func0(a1, v6);
v11 = a6;
v17 = 0;
v18 = 9 * (a5 != 88) + 79;
v19 = a2 - 1;
if ( v11 )
{
do
{
for ( i = 0; i != 8; ++i )
{
v20 = i;
v21 = v17;
v13 = _grid_copy((const __m128i **)a1);
if ( (unsigned int)reversi_set_ia_move(v13, v6, &v20) )
{
if ( (int)func1(v13, v19, a3, a4, v18, 0LL) < (int)a3 )
{
if ( (int)a3 >= (int)a4 )
goto LABEL_11;
}
else
{
a3 = func1(v13, v19, a3, a4, v18, 0LL);
if ( (int)a3 >= (int)a4 )
{
LABEL_11:
reversi_free(v13);
return a3;
}
}
}
reversi_free(v13);
}
++v17;
}
while ( v17 != 8 );
result = a3;
}
else
{
while ( 2 )
{
for ( j = 0; j != 8; ++j )
{
v20 = j;
v21 = v17;
v15 = _grid_copy((const __m128i **)a1);
if ( (unsigned int)reversi_set_ia_move(v15, v6, &v20) )
{
if ( (int)func1(v15, v19, a3, a4, v18, 1LL) > (int)a4 )
{
if ( (int)a4 <= (int)a3 )
goto LABEL_19;
}
else
{
a4 = func1(v15, v19, a3, a4, v18, 1LL);
if ( (int)a4 <= (int)a3 )
{
LABEL_19:
reversi_free(v15);
return a4;
}
}
}
reversi_free(v15);
}
if ( ++v17 != 8 )
continue;
break;
}
result = a4;
}
return result;
}

original_code:
int ia_alphabeta_bis (Reversi *reversi, int depth, int alpha, int beta, Player player, int maximizingPlayer)
{
  int i,j;
  Reversi *reversiCpy = ((void *)0);
  Pos playedPos;
  if (depth == 0 || reversi_game_over(reversi))
  {
    return ia_eval_grid(reversi, player);
  }
  if (maximizingPlayer)
  {
    for(j = 0; j < 8; j++)
    {
      for(i = 0; i < 8; i++)
      {
        playedPos.x = i;
        playedPos.y = j;
        reversiCpy = __grid_copy(reversi);
        if (reversi_set_ia_move(reversiCpy, player, &playedPos))
        {
          alpha = (((alpha) > (ia_alphabeta_bis(reversiCpy, depth - 1, alpha, beta, (player == 'X' ? 'O' : 'X'), 0))) ? (alpha) : (ia_alphabeta_bis(reversiCpy, depth - 1, alpha, beta, (player == 'X' ? 'O' : 'X'), 0)));
          if (beta <= alpha)
          {
            reversi_free(reversiCpy);
            return alpha;
          }
        }
        reversi_free(reversiCpy);
      }
    }
    return alpha;
  }
  for(j = 0; j < 8; j++)
  {
    for(i = 0; i < 8; i++)
    {
      playedPos.x = i;
      playedPos.y = j;
      reversiCpy = __grid_copy(reversi);
      if (reversi_set_ia_move(reversiCpy, player, &playedPos))
      {
        beta = (((beta) < (ia_alphabeta_bis(reversiCpy, depth - 1, alpha, beta, (player == 'X' ? 'O' : 'X'), 1))) ? (beta) : (ia_alphabeta_bis(reversiCpy, depth - 1, alpha, beta, (player == 'X' ? 'O' : 'X'), 1)));
        if (beta <= alpha)
        {
          reversi_free(reversiCpy);
          return beta;
        }
      }
      reversi_free(reversiCpy);
    }
  }
  return beta;
}

canonical_original_code:
int ia_alphabeta_bis(struct Reversi *reversi, int depth, int alpha, int beta, char player, int maximizingPlayer)
{
  int i; int j;
  struct Reversi *reversiCpy = ((void *)0);
  struct Pos playedPos;
  if (depth == 0 || reversi_game_over(reversi))
  {
    return ia_eval_grid(reversi, player);
  }
  if (maximizingPlayer)
  {
    for(j = 0; j < 8; j++)
    {
      for(i = 0; i < 8; i++)
      {
        playedPos.x = i;
        playedPos.y = j;
        reversiCpy = __grid_copy(reversi);
        if (reversi_set_ia_move(reversiCpy, player, &playedPos))
        {
          alpha = (((alpha) > (ia_alphabeta_bis(reversiCpy, depth - 1, alpha, beta, (player == 'X' ? 'O' : 'X'), 0))) ? (alpha) : (ia_alphabeta_bis(reversiCpy, depth - 1, alpha, beta, (player == 'X' ? 'O' : 'X'), 0)));
          if (beta <= alpha)
          {
            reversi_free(reversiCpy);
            return alpha;
          }
        }
        reversi_free(reversiCpy);
      }
    }
    return alpha;
  }
  for(j = 0; j < 8; j++)
  {
    for(i = 0; i < 8; i++)
    {
      playedPos.x = i;
      playedPos.y = j;
      reversiCpy = __grid_copy(reversi);
      if (reversi_set_ia_move(reversiCpy, player, &playedPos))
      {
        beta = (((beta) < (ia_alphabeta_bis(reversiCpy, depth - 1, alpha, beta, (player == 'X' ? 'O' : 'X'), 1))) ? (beta) : (ia_alphabeta_bis(reversiCpy, depth - 1, alpha, beta, (player == 'X' ? 'O' : 'X'), 1)));
        if (beta <= alpha)
        {
          reversi_free(reversiCpy);
          return beta;
        }
      }
      reversi_free(reversiCpy);
    }
  }
  return beta;
}

variable_types: depth int

variable_types: alpha int

variable_types: beta int

variable_types: maximizingPlayer int

variable_types: i int

variable_types: j int

variable_types:
playedPos
struct Pos { int x; int y; }

user_defined_types: struct Reversi { char *array; unsigned int n_moves; }


decompiled_code:
__int64 __fastcall ia_alphabeta(const __m128i **a1, char a2, int a3)
{
signed int v3; // er12
int v4; // ebp
int i; // ebx
__int64 *v6; // r15
signed int v7; // eax
__int128 v9; // [rsp+10h] [rbp-58h]
int v10[16]; // [rsp+28h] [rbp-40h] BYREF

v3 = 0x80000000;
DWORD2(v9) = -1;
v4 = 0;
DWORD1(v9) = -1;
LODWORD(v9) = a3 - 1;
do
{
for ( i = 0; i != 8; ++i )
{
v10[0] = i;
v10[1] = v4;
v6 = (__int64 *)_grid_copy(a1);
if ( (unsigned int)reversi_set_ia_move(v6, (unsigned int)a2, v10) )
{
v7 = ia_alphabeta_bis(v6, v9, v3, 0x7FFFFFFFu, 9 * (a2 != 88) + 79, 0);
if ( v7 >= v3 )
{
DWORD2(v9) = v4;
v3 = v7;
DWORD1(v9) = i;
}
}
reversi_free(v6);
}
++v4;
}
while ( v4 != 8 );
return v9 >> 32;
}

original_code:
Pos ia_alphabeta (Reversi *reversi, Player player, int depth)
{
  int i, j, temp;
  Reversi *reversiCpy = ((void *)0);
  Pos playedPos, bestPos;
  int alpha = (-0x7fffffff - 1), beta = 0x7fffffff;
  bestPos.x = -1;
  bestPos.y = -1;
  for(j = 0; j < 8; j++)
  {
    for(i = 0; i < 8; i++)
    {
      playedPos.x = i;
      playedPos.y = j;
      reversiCpy = __grid_copy(reversi);
      if (reversi_set_ia_move(reversiCpy, player, &playedPos))
      {
        temp = ia_alphabeta_bis(reversiCpy, depth - 1, alpha, beta, (player == 'X' ? 'O' : 'X'), 0);
        if (temp >= alpha)
        {
          alpha = temp;
          bestPos.x = i;
          bestPos.y = j;
        }
      }
      reversi_free(reversiCpy);
    }
  }
  return bestPos;
}

canonical_original_code:
struct Pos ia_alphabeta(struct Reversi *reversi, char player, int depth)
{
  int i; int j; int temp;
  struct Reversi *reversiCpy = ((void *)0);
  struct Pos playedPos; struct Pos bestPos;
  int alpha = (-0x7fffffff - 1); int beta = 0x7fffffff;
  bestPos.x = -1;
  bestPos.y = -1;
  for(j = 0; j < 8; j++)
  {
    for(i = 0; i < 8; i++)
    {
      playedPos.x = i;
      playedPos.y = j;
      reversiCpy = __grid_copy(reversi);
      if (reversi_set_ia_move(reversiCpy, player, &playedPos))
      {
        temp = ia_alphabeta_bis(reversiCpy, depth - 1, alpha, beta, (player == 'X' ? 'O' : 'X'), 0);
        if (temp >= alpha)
        {
          alpha = temp;
          bestPos.x = i;
          bestPos.y = j;
        }
      }
      reversi_free(reversiCpy);
    }
  }
  return bestPos;
}

variable_types: temp int

variable_types:
bestPos
struct Pos { int x; int y; }

return_type: struct Pos { int x; int y; }