// File: /workspace/github_c_makefile_star2_O1/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 0x1673
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10e0
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x3df7(%rip)      # 0x50b0
cmpq $-0x1, %rax
je 0x1678
callq 0x1680
movq %rax, %r14
movl (%rax), %esi
leaq 0x1d57(%rip), %rdi      # 0x302b
xorl %eax, %eax
callq 0x1080
movl 0x4(%r14), %esi
leaq 0x1e0f(%rip), %rdi      # 0x30f5
xorl %eax, %eax
callq 0x1080
callq 0x1695
movq %rax, %rbx
leaq 0x1e5d(%rip), %rdi      # 0x3159
callq 0x1050
movl (%rbx), %esi
leaq 0x1d2d(%rip), %rdi      # 0x3037
xorl %eax, %eax
callq 0x1080
movl 0x4(%rbx), %esi
leaq 0x1d2f(%rip), %rdi      # 0x304a
xorl %eax, %eax
callq 0x1080
movl 0x8(%rbx), %esi
leaq 0x1d31(%rip), %rdi      # 0x305d
xorl %eax, %eax
callq 0x1080
movzwl 0xc(%rbx), %esi
leaq 0x1d31(%rip), %rdi      # 0x306f
xorl %eax, %eax
callq 0x1080
movzwl 0xe(%rbx), %esi
leaq 0x1d31(%rip), %rdi      # 0x3081
xorl %eax, %eax
callq 0x1080
movzwl 0x10(%rbx), %esi
leaq 0x1d31(%rip), %rdi      # 0x3093
xorl %eax, %eax
callq 0x1080
xorl %edi, %edi
callq 0x16aa
movq %rax, %rbx
movl $0x1, %edi
callq 0x16aa
movq %rax, %r13
leaq 0x1d1c(%rip), %rdi      # 0x30a3
xorl %eax, %eax
callq 0x1080
movq %r14, -0x30(%rbp)
cmpl $0x8, 0x4(%r14)
jb 0x13e4
leaq 0x1d11(%rip), %r15      # 0x30b1
xorl %r14d, %r14d
movl $0x20, %edi
callq 0x1030
xorl %r12d, %r12d
movzbl (%rbx,%r14), %eax
xorl %esi, %esi
btl %r12d, %eax
setb %sil
movq %r15, %rdi
xorl %eax, %eax
callq 0x1080
incl %r12d
cmpl $0x8, %r12d
jne 0x13b0
incq %r14
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
shrl $0x3, %eax
cmpq %rax, %r14
jb 0x13a3
leaq 0x1cc9(%rip), %rdi      # 0x30b4
xorl %eax, %eax
callq 0x1080
movq -0x30(%rbp), %rax
cmpl $0x8, (%rax)
jb 0x1446
leaq 0x1caf(%rip), %rbx      # 0x30b1
xorl %r14d, %r14d
movl $0x20, %edi
callq 0x1030
xorl %r15d, %r15d
movzbl (%r13,%r14), %eax
xorl %esi, %esi
btl %r15d, %eax
setb %sil
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1080
incl %r15d
cmpl $0x8, %r15d
jne 0x1412
incq %r14
movq -0x30(%rbp), %rax
movl (%rax), %eax
shrl $0x3, %eax
cmpq %rax, %r14
jb 0x1405
movq %r13, -0x48(%rbp)
leaq 0x1cd1(%rip), %rdi      # 0x3122
callq 0x1050
callq 0x17a9
movq %rax, -0x40(%rbp)
leaq 0x1d00(%rip), %rdi      # 0x3166
callq 0x1050
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x152e
movq -0x40(%rbp), %rax
leaq 0x28(%rax), %r14
xorl %r13d, %r13d
leaq 0x1c66(%rip), %r15      # 0x30f0
xorl %r12d, %r12d
movq -0x48(%rbp), %rdi
movl %r12d, %esi
callq 0x16dc
cmpl $0x1, %eax
sete %al
cmpq $0xb, %r12
setae %cl
cmpq $0x1, %r12
je 0x14b0
andb %al, %cl
je 0x1518
cmpl $0x0, (%r14)
je 0x1518
movl -0x28(%r14), %eax
movb $0x64, %bl
btl $0xe, %eax
jb 0x14df
testw %ax, %ax
js 0x14dd
movzwl %ax, %eax
cmpl $0x2000, %eax           # imm = 0x2000
movzbl %r13b, %ebx
movl $0x6c, %eax
cmovael %eax, %ebx
jmp 0x14df
movb $0x66, %bl
movzbl %bl, %edx
movzwl -0xe(%r14), %r8d
movl -0x24(%r14), %ecx
movl -0xc(%r14), %r9d
leal 0x1(%r12), %r13d
leaq 0x1bc8(%rip), %rdi      # 0x30c3
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
movq -0x30(%rbp), %rax
movl (%rax), %eax
subq $-0x80, %r14
cmpq %rax, %r12
jb 0x148d
movl $0xa, %edi
callq 0x1030
leaq 0x1c2f(%rip), %rdi      # 0x316e
callq 0x1050
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x1662
xorl %r15d, %r15d
leaq 0x1bc9(%rip), %r13      # 0x3124
xorl %ebx, %ebx
movq -0x48(%rbp), %rdi
movl %ebx, %esi
callq 0x16dc
cmpl $0x1, %eax
sete %al
cmpq $0xb, %rbx
setae %cl
cmpq $0x1, %rbx
movq %rbx, -0x50(%rbp)
je 0x1587
andb %al, %cl
je 0x164c
movq %rbx, %r12
shlq $0x7, %r12
movq -0x40(%rbp), %rax
testb $0x40, 0x1(%rax,%r12)
je 0x164c
addq %rax, %r12
leal 0x1(%rbx), %eax
movl %eax, -0x34(%rbp)
xorl %r14d, %r14d
movq %r12, -0x58(%rbp)
movl 0x28(%r12,%r14,4), %esi
testl %esi, %esi
je 0x163b
xorl %ebx, %ebx
leaq 0x1b3d(%rip), %rdi      # 0x3101
movl -0x34(%rbp), %edx
xorl %eax, %eax
callq 0x1080
movl 0x28(%r12,%r14,4), %edi
callq 0x1a56
movq %rax, %r12
movzbl 0x7(%r12), %ecx
cmpq $0x7, %rcx
ja 0x1601
movl $0x87, %eax
btl %ecx, %eax
jae 0x1601
shll $0x3, %ecx
movabsq $0x6c75757575646675, %r15 # imm = 0x6C75757575646675
shrq %cl, %r15
movzbl 0x6(%r12), %ecx
testl %ecx, %ecx
je 0x1628
movl (%r12), %esi
movzwl 0x4(%r12), %edx
movzbl %r15b, %r8d
leaq 0x8(%r12), %r9
movq %r13, %rdi
xorl %eax, %eax
callq 0x1080
movzwl 0x4(%r12), %eax
addl %eax, %ebx
addq %rax, %r12
cmpl $0x400, %ebx            # imm = 0x400
jb 0x15db
incq %r14
cmpq $0xc, %r14
movq -0x58(%rbp), %r12
jne 0x15ae
movq -0x50(%rbp), %rbx
incq %rbx
movq -0x30(%rbp), %rax
movl (%rax), %eax
cmpq %rax, %rbx
jb 0x155d
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1140
callq 0x1168
nopl (%rax)