// File: /workspace/github_c_makefile_star2_O2/sheheryarbutt[P]Ext2-File-System/ext2_checker.c
// Function: main
int main(int argc, char **argv) {

    if(argc != 2) {
        fprintf(stderr, "Usage: ext2_checker <image file name>\n");
        exit(1);
    }
    int fd = open(argv[1], O_RDWR);

    disk = mmap(NULL, 128 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
	perror("mmap");
	exit(1);
    }
    int fixes=0;
    sb = (struct ext2_super_block *)(disk + EXT2_BLOCK_SIZE);
    gd = (struct ext2_group_desc *)(disk + (2*EXT2_BLOCK_SIZE));
	inode_table = (struct ext2_inode *)(disk + (5*EXT2_BLOCK_SIZE));

	int actual_free_inodes=0;
	int actual_free_blocks=0;
	unsigned int * bb=(unsigned int *)(disk + (EXT2_BLOCK_SIZE*3));

	for (int i=0; i<4; i++){ //4= 128/32 because 128 is the total block and 32 is 4 bytes=unsigned int
		int j=0;	
		while(j<32){
			if (bb[i]&(1<<j)){
				actual_free_blocks++;
			}	
			j++;
		}	
	}
	int j=0;
	unsigned int * ib = (unsigned int *)(disk+(EXT2_BLOCK_SIZE*4));
	while(j<32){
			if (ib[0]&(1<<j)){
				actual_free_inodes++;
			}
			j++;
	}
	int diff;
	if (sb->s_free_blocks_count!=actual_free_blocks){
		diff=abs(sb->s_free_blocks_count-actual_free_blocks);
		sb->s_free_blocks_count=actual_free_blocks;
		fixes+=diff;
		printf("Fixed: superblock's free blocks counter was off by %d compared to the bitmap\n",diff);
	}
	if (sb->s_free_inodes_count!=actual_free_inodes){
		diff=abs(sb->s_free_inodes_count-actual_free_inodes);
		sb->s_free_inodes_count=actual_free_inodes;
		fixes+=diff;
		printf("Fixed: superblock's free inodes counter was off by %d compared to the bitmap\n",diff);
	}
	if (gd->bg_free_blocks_count!=actual_free_blocks){
		diff=abs(gd->bg_free_blocks_count-actual_free_blocks);
		gd->bg_free_blocks_count=actual_free_blocks;
		fixes+=diff;
		printf("Fixed: block group's free blocks counter was off by %d compared to the bitmap\n",diff);
	}
	if (gd->bg_free_inodes_count!=actual_free_inodes){
		diff=abs(gd->bg_free_inodes_count-actual_free_inodes);
		gd->bg_free_inodes_count=actual_free_inodes;
		fixes+=diff;
		printf("Fixed: block group's free inodes counter was off by %d compared to the bitmap\n",diff);
	}
	if(fixes!=0){
		printf("%d file system inconsistencies repaired!\n",fixes);
	}else{
		printf("No file system inconsistencies detected!\n");
	}			   	   
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
cmpl $0x2, %edi
jne 0x13c3
movq 0x8(%rsi), %rdi
xorl %r14d, %r14d
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x10f0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1080
movq %rax, 0x2e41(%rip)      # 0x40b0
cmpq $-0x1, %rax
je 0x13e1
leaq 0x400(%rax), %rcx
movq %rcx, 0x2e31(%rip)      # 0x40b8
leaq 0x800(%rax), %rdx
movq %rdx, 0x2e2b(%rip)      # 0x40c0
movq %rax, %rdx
addq $0x1400, %rdx           # imm = 0x1400
movq %rdx, 0x2e22(%rip)      # 0x40c8
xorl %edx, %edx
cmpq $0x4, %rdx
je 0x12cc
movl 0xc00(%rax,%rdx,4), %esi
xorl %edi, %edi
cmpl $0x20, %edi
je 0x12c7
btl %edi, %esi
adcl $0x0, %r14d
incl %edi
jmp 0x12b7
incq %rdx
jmp 0x12a8
movl 0x1000(%rax), %edx
xorl %r15d, %r15d
xorl %esi, %esi
cmpl $0x20, %esi
je 0x12e7
btl %esi, %edx
adcl $0x0, %r15d
incl %esi
jmp 0x12d7
movl 0x40c(%rax), %edx
xorl %ebx, %ebx
subl %r14d, %edx
je 0x1319
movl %edx, %ebx
negl %ebx
cmovsl %edx, %ebx
movl %r14d, 0x40c(%rax)
leaq 0xd27(%rip), %rdi       # 0x2030
movl %ebx, %esi
xorl %eax, %eax
callq 0x1090
movq 0x2d9f(%rip), %rcx      # 0x40b8
movl 0x10(%rcx), %eax
subl %r15d, %eax
je 0x133c
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
movl %r15d, 0x10(%rcx)
addl %esi, %ebx
leaq 0xd49(%rip), %rdi       # 0x207e
xorl %eax, %eax
callq 0x1090
movq 0x2d7d(%rip), %rax      # 0x40c0
movzwl 0xc(%rax), %ecx
subl %r14d, %ecx
je 0x136f
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
movw %r14w, 0xc(%rax)
addl %esi, %ebx
leaq 0xd6b(%rip), %rdi       # 0x20cc
xorl %eax, %eax
callq 0x1090
movq 0x2d51(%rip), %rax      # 0x40c0
movzwl 0xe(%rax), %ecx
subl %r15d, %ecx
je 0x1394
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
movw %r15w, 0xe(%rax)
addl %esi, %ebx
leaq 0xd8e(%rip), %rdi       # 0x211b
xorl %eax, %eax
callq 0x1090
testl %ebx, %ebx
je 0x13aa
leaq 0xdcb(%rip), %rdi       # 0x216a
movl %ebx, %esi
xorl %eax, %eax
callq 0x1090
jmp 0x13b6
leaq 0xde3(%rip), %rdi       # 0x2194
callq 0x1060
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c2e(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc30(%rip), %rdi       # 0x2004
pushq $0x26
popq %rsi
pushq $0x1
popq %rdx
callq 0x1120
jmp 0x13ed
leaq 0xc43(%rip), %rdi       # 0x202b
callq 0x1100
pushq $0x1
popq %rdi
callq 0x1110
nopl (%rax)