// File: /workspace/github_c_makefile_star2_O2/JavieW[P]CSCC69_Ext2_File-System/readimage.c
// Function: main
int main(int argc, char **argv) {

    if(argc != 2) {
        fprintf(stderr, "Usage: readimg <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);
    }

    struct ext2_super_block *sb = getSuperblock();
    printf("Inodes: %d\n", sb->s_inodes_count);
    printf("Blocks: %d\n", sb->s_blocks_count);

    struct ext2_group_desc *gd = getGroupDesc();
    printf("Block group:\n");
    printf("\tblock bitmap: %d\n", gd->bg_block_bitmap);
    printf("\tinode bitmap: %d\n", gd->bg_inode_bitmap);
    printf("\tinode table: %d\n", gd->bg_inode_table);
    printf("\tfree blocks: %d\n", gd->bg_free_blocks_count);
    printf("\tfree inodes: %d\n", gd->bg_free_inodes_count);
    printf("\tused_dirs: %d\n", gd->bg_used_dirs_count);

    // task1, print bitmap
    int i,j;
    char unsigned *block_bitmap = getBitmap(BLOCK_BITMAP);
    char unsigned *inode_bitmap = getBitmap(INODE_BITMAP);
    printf("Block bitmap:");
    for (i=0; i<sb->s_blocks_count/8; i++) {
        printf(" ");
        for (j=0; j<8; j++) {
            printf("%d", ((block_bitmap[i]>>j)&1));
        }
    }
    printf("\nInode bitmap:");
    for (i=0; i<sb->s_inodes_count/8; i++) {
        printf(" ");
        for (j=0; j<8; j++) {
            printf("%d", ((inode_bitmap[i]>>j)&1));
        }
    }
    printf("\n\n");

    // task2, print inode
    struct ext2_inode *inodeTable = getInodeTable();
    char mode='\0';
    printf("Inodes:\n");
    for(i = 0; i < sb->s_inodes_count; i++) 
    {
        // print root inode and used unreserved inodes
        if ((getBit(inode_bitmap, i)==1 && i>=EXT2_GOOD_OLD_FIRST_INO) || (i==EXT2_ROOT_INO-1))
        {
            // skip the inodes that don't reserve blocks
            if (inodeTable[i].i_block[0] == 0)
                continue;

            // print out the following info about the inode 
            if (inodeTable[i].i_mode & EXT2_S_IFDIR)
                mode = 'd';
            else if (inodeTable[i].i_mode & EXT2_S_IFREG)
                mode = 'f';
            else if (inodeTable[i].i_mode & EXT2_S_IFLNK)
                mode = 'l';
            printf("[%d] type: %c size: %d links: %d blocks: %d\n", i+1, mode, inodeTable[i].i_size, inodeTable[i].i_links_count, inodeTable[i].i_blocks);
            printf("[%d] Blocks: %d\n", i+1, inodeTable[i].i_block[0]);
        }
    }
    printf("\n");

    // task3, print directory enties
    char type='\0';
    struct ext2_dir_entry_2 *dir_entry;
    int total_rec_len;
    printf("Directory Blocks:\n");
    for(i = 0; i < sb->s_inodes_count; i++) 
    {
        // print root inode and used unreserved inodes
        if ((getBit(inode_bitmap, i)==1 && i>=EXT2_GOOD_OLD_FIRST_INO) || (i==EXT2_ROOT_INO-1))
        {
            // skip the inodes that not belong to a directory
            if (!(inodeTable[i].i_mode & EXT2_S_IFDIR))
                continue;
            
            for (int j=0; j<12; j++) {
                if (inodeTable[i].i_block[j] == 0) continue;
                printf("\tDIR BLOCK NUM: %d (for inode %d)\n", inodeTable[i].i_block[j], i+1);
                dir_entry = (struct ext2_dir_entry_2 *)getBlock(inodeTable[i].i_block[j]);
                
                // while not hit the end of the block
                total_rec_len = 0;
                while (total_rec_len < EXT2_BLOCK_SIZE) { 
                    if (dir_entry->file_type == EXT2_FT_UNKNOWN)
                        type = 'u';
                    else if (dir_entry->file_type == EXT2_FT_REG_FILE)
                        type = 'f';
                    else if (dir_entry->file_type == EXT2_FT_DIR)
                        type = 'd';
                    else if (dir_entry->file_type == EXT2_FT_SYMLINK)
                        type = 'l';
                    if (dir_entry->name_len != 0)
                        printf("Inode: %d rec_len: %d name_len: %d type= %c name=%s\n", dir_entry->inode, dir_entry->rec_len, dir_entry->name_len, type, dir_entry->name);
                    total_rec_len = total_rec_len + dir_entry->rec_len;
                    dir_entry = (void *) dir_entry + dir_entry->rec_len;
                }
            }   
        }
    }



    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpl $0x2, %edi
jne 0x160f
movq 0x8(%rsi), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x10d0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2e45(%rip)      # 0x40a8
cmpq $-0x1, %rax
je 0x162d
callq 0x1644
movq %rax, %rbx
movl (%rax), %esi
leaq 0xdad(%rip), %rdi       # 0x202b
xorl %eax, %eax
callq 0x1080
movq %rbx, -0x30(%rbp)
movl 0x4(%rbx), %esi
leaq 0xe62(%rip), %rdi       # 0x20f5
xorl %eax, %eax
callq 0x1080
callq 0x1659
movq %rax, %rbx
leaq 0xeb0(%rip), %rdi       # 0x2159
callq 0x1050
movl (%rbx), %esi
leaq 0xd80(%rip), %rdi       # 0x2037
xorl %eax, %eax
callq 0x1080
movl 0x4(%rbx), %esi
leaq 0xd82(%rip), %rdi       # 0x204a
xorl %eax, %eax
callq 0x1080
movl 0x8(%rbx), %esi
leaq 0xd84(%rip), %rdi       # 0x205d
xorl %eax, %eax
callq 0x1080
movzwl 0xc(%rbx), %esi
leaq 0xd84(%rip), %rdi       # 0x206f
xorl %eax, %eax
callq 0x1080
movzwl 0xe(%rbx), %esi
leaq 0xd84(%rip), %rdi       # 0x2081
xorl %eax, %eax
callq 0x1080
movzwl 0x10(%rbx), %esi
leaq 0xd84(%rip), %rdi       # 0x2093
xorl %eax, %eax
callq 0x1080
xorl %edi, %edi
callq 0x166e
movq %rax, %rbx
pushq $0x1
popq %rdi
callq 0x166e
movq %rax, %r13
leaq 0xd71(%rip), %rdi       # 0x20a3
xorl %eax, %eax
callq 0x1080
pushq $0x20
popq %r15
leaq 0xd6d(%rip), %r12       # 0x20b1
xorl %r14d, %r14d
movq %r13, -0x38(%rbp)
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
shrl $0x3, %eax
cmpq %rax, %r14
jae 0x1392
movl %r15d, %edi
callq 0x1030
xorl %r13d, %r13d
cmpl $0x8, %r13d
je 0x1389
movzbl (%rbx,%r14), %eax
xorl %esi, %esi
btl %r13d, %eax
setb %sil
movq %r12, %rdi
xorl %eax, %eax
callq 0x1080
incl %r13d
jmp 0x1365
incq %r14
movq -0x38(%rbp), %r13
jmp 0x134b
leaq 0xd1b(%rip), %rdi       # 0x20b4
xorl %eax, %eax
callq 0x1080
pushq $0x20
popq %rbx
leaq 0xd07(%rip), %r15       # 0x20b1
xorl %r14d, %r14d
movq -0x30(%rbp), %rax
movl (%rax), %eax
shrl $0x3, %eax
cmpq %rax, %r14
jae 0x13ef
movl %ebx, %edi
callq 0x1030
xorl %r12d, %r12d
cmpl $0x8, %r12d
je 0x13ea
movzbl (%r13,%r14), %eax
xorl %esi, %esi
btl %r12d, %eax
setb %sil
movq %r15, %rdi
xorl %eax, %eax
callq 0x1080
incl %r12d
jmp 0x13c5
incq %r14
jmp 0x13ad
leaq 0xd2c(%rip), %rdi       # 0x2122
callq 0x1050
callq 0x175f
movq %rax, %rbx
leaq 0xd5c(%rip), %rdi       # 0x2166
callq 0x1050
movq %rbx, -0x48(%rbp)
leaq 0x28(%rbx), %r14
xorl %r13d, %r13d
leaq 0xccf(%rip), %r15       # 0x20f0
xorl %r12d, %r12d
movq -0x30(%rbp), %rax
movl (%rax), %eax
cmpq %rax, %r12
jae 0x14c4
movq -0x38(%rbp), %rdi
movl %r12d, %esi
callq 0x16a0
cmpl $0x1, %eax
sete %al
cmpq $0xb, %r12
setae %cl
cmpq $0x1, %r12
je 0x1456
andb %al, %cl
je 0x14b8
cmpl $0x0, (%r14)
je 0x14b8
movl -0x28(%r14), %eax
movb $0x64, %bl
btl $0xe, %eax
jb 0x147f
testw %ax, %ax
js 0x147d
cmpw $0x2000, %ax            # imm = 0x2000
movzbl %r13b, %ebx
pushq $0x6c
popq %rax
cmovael %eax, %ebx
jmp 0x147f
movb $0x66, %bl
movzbl %bl, %edx
movzwl -0xe(%r14), %r8d
movl -0x24(%r14), %ecx
movl -0xc(%r14), %r9d
leal 0x1(%r12), %r13d
leaq 0xc28(%rip), %rdi       # 0x20c3
movl %r13d, %esi
xorl %eax, %eax
callq 0x1080
movl (%r14), %edx
movq %r15, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x1080
movl %ebx, %r13d
incq %r12
subq $-0x80, %r14
jmp 0x1424
pushq $0xa
popq %rdi
callq 0x1030
leaq 0xc9b(%rip), %rdi       # 0x216e
callq 0x1050
leaq 0xc45(%rip), %r13       # 0x2124
xorl %r15d, %r15d
xorl %ebx, %ebx
movq -0x48(%rbp), %r14
movq -0x30(%rbp), %rax
movl (%rax), %eax
cmpq %rax, %r15
jae 0x15fe
movq -0x38(%rbp), %rdi
movl %r15d, %esi
callq 0x16a0
cmpl $0x1, %eax
sete %al
cmpq $0xb, %r15
setae %cl
cmpq $0x1, %r15
je 0x151e
andb %al, %cl
je 0x15f6
movq %r15, %r12
shlq $0x7, %r12
testb $0x40, 0x1(%r14,%r12)
je 0x15f6
addq %r14, %r12
leal 0x1(%r15), %eax
movl %eax, -0x3c(%rbp)
xorl %r14d, %r14d
movq %r15, -0x58(%rbp)
movq %r12, -0x50(%rbp)
cmpq $0xc, %r14
je 0x15f2
movl 0x28(%r12,%r14,4), %esi
testl %esi, %esi
je 0x15ea
xorl %r15d, %r15d
leaq 0xb9a(%rip), %rdi       # 0x2101
movl -0x3c(%rbp), %edx
xorl %eax, %eax
callq 0x1080
movl 0x28(%r12,%r14,4), %edi
callq 0x19b3
movq %rax, %r12
cmpl $0x3ff, %r15d           # imm = 0x3FF
ja 0x15e2
movzbl 0x7(%r12), %ecx
cmpq $0x7, %rcx
ja 0x15ad
movl $0x87, %eax
btl %ecx, %eax
jae 0x15ad
shll $0x3, %ecx
movabsq $0x6c75757575646675, %rbx # imm = 0x6C75757575646675
shrq %cl, %rbx
movzbl 0x6(%r12), %ecx
testl %ecx, %ecx
je 0x15d4
movl (%r12), %esi
movzwl 0x4(%r12), %edx
movzbl %bl, %r8d
leaq 0x8(%r12), %r9
movq %r13, %rdi
xorl %eax, %eax
callq 0x1080
movzwl 0x4(%r12), %eax
addl %eax, %r15d
addq %rax, %r12
jmp 0x157e
movq -0x58(%rbp), %r15
movq -0x50(%rbp), %r12
incq %r14
jmp 0x1546
movq -0x48(%rbp), %r14
incq %r15
jmp 0x14e8
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x29e2(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x9e4(%rip), %rdi       # 0x2004
pushq $0x21
popq %rsi
pushq $0x1
popq %rdx
callq 0x1110
jmp 0x1639
leaq 0x9f2(%rip), %rdi       # 0x2026
callq 0x10e0
pushq $0x1
popq %rdi
callq 0x1100
nopl (%rax)