// File: /workspace/github_c_makefile_star2_O0/JavieW[P]CSCC69_Ext2_File-System/ext2_ls.c
// Function: main
int main(int argc, char **argv) {
    char path[EXT2_NAME_LEN];
    char pathCopy[EXT2_NAME_LEN];
    char fileName[EXT2_NAME_LEN];
    int flagged = 0;
    struct ext2_inode inode;

    if(argc!=3 && argc!=4) {
        fprintf(stderr, "Usage: ext2_ls <image file name> <optional flag -a> <absolute path>\n");
        exit(1);
    }
    int fd = open(argv[1], O_RDWR);

    // read disk and get inode table
    disk = mmap(NULL, 128 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
        perror("mmap");
        exit(1);
    }
    inodeTable = getInodeTable();

    // read other arguments
    if(argc == 3) {
        strcpy(path, argv[2]);
    } else {
        if (strcmp(argv[2], "-a")!=0) {
            fprintf(stderr, "Invalid Flag\n");
            exit(1);
        }
        flagged = 1;
        strcpy(path, argv[3]);
    }

    // get the inode from path
    strcpy(pathCopy, path);
    int inodeNum = getInodeFromPath(pathCopy);
    if (inodeNum == 0) {
        fprintf(stderr, "No such file or directory\n");
        return ENOENT;
    }
    inode = inodeTable[inodeNum-1];
    
    // print all file nemes in directory data block
    if (inode.i_mode & EXT2_S_IFDIR) {
        struct ext2_dir_entry_2 *dir_entry = NULL;
        unsigned int *singleIndirect = NULL;
        int total_rec_len = 0;

        // print file names in direct blocks
        for (int i=0; i<13+EXT2_BLOCK_SIZE/4; i++) {

            if (i<12) {
                if (inode.i_block[i] == 0) continue;
                dir_entry = (struct ext2_dir_entry_2 *)getBlock(inode.i_block[i]);
            } else if (i==12) {
                if (inode.i_block[i] == 0) break;
                singleIndirect = (unsigned int *)getBlock(inode.i_block[12]);
                continue;
            } else {
                if (singleIndirect[i-13] == 0) continue;
                dir_entry = (struct ext2_dir_entry_2 *)getBlock(singleIndirect[i-13]);
            }
                
            // for each dir entry in the block
            total_rec_len = 0;
            while (total_rec_len < EXT2_BLOCK_SIZE) {
                if ((dir_entry->name[0]!='.' || flagged) && (dir_entry->name_len!=0)) {
                    printf("%s\n", dir_entry->name);
                }
                total_rec_len = total_rec_len + dir_entry->rec_len;
                dir_entry = (void *) dir_entry + dir_entry->rec_len;
            }
        }

    // print file name
    } else if (inode.i_mode&EXT2_S_IFREG || inode.i_mode&EXT2_S_IFLNK) {
        getFileNameFromPath(fileName, path);
        printf("%s\n", fileName);
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3c0, %rsp            # imm = 0x3C0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x314(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1261
cmpl $0x4, -0x8(%rbp)
je 0x1261
movq 0x3db2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1db4(%rip), %rsi      # 0x3004
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x1100
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10d0
movl %eax, -0x398(%rbp)
movl -0x398(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x20000, %esi          # imm = 0x20000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x1050
movq %rax, 0x3dfb(%rip)      # 0x50a0
movq $-0x1, %rax
cmpq %rax, 0x3ded(%rip)      # 0x50a0
jne 0x12cb
leaq 0x1d8d(%rip), %rdi      # 0x3049
callq 0x10e0
movl $0x1, %edi
callq 0x1100
callq 0x1880
movq %rax, 0x3dd1(%rip)      # 0x50a8
cmpl $0x3, -0x8(%rbp)
jne 0x12f3
leaq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1030
jmp 0x134c
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x1d4c(%rip), %rsi      # 0x304e
callq 0x1090
cmpl $0x0, %eax
je 0x132e
movq 0x3ce5(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d34(%rip), %rsi      # 0x3051
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x1100
movl $0x1, -0x314(%rbp)
leaq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x1030
leaq -0x210(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x1030
leaq -0x210(%rbp), %rdi
callq 0x2090
movl %eax, -0x39c(%rbp)
cmpl $0x0, -0x39c(%rbp)
jne 0x139e
movq 0x3c77(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1cd4(%rip), %rsi      # 0x305f
movb $0x0, %al
callq 0x10a0
movl $0x2, -0x4(%rbp)
jmp 0x15ca
movq 0x3d03(%rip), %rsi      # 0x50a8
movl -0x39c(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x7, %rax
addq %rax, %rsi
leaq -0x394(%rbp), %rdi
movl $0x80, %edx
callq 0x10b0
movzwl -0x394(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x1577
movq $0x0, -0x3a8(%rbp)
movq $0x0, -0x3b0(%rbp)
movl $0x0, -0x3b4(%rbp)
movl $0x0, -0x3b8(%rbp)
cmpl $0x10d, -0x3b8(%rbp)    # imm = 0x10D
jge 0x1575
cmpl $0xc, -0x3b8(%rbp)
jge 0x1452
movslq -0x3b8(%rbp), %rax
cmpl $0x0, -0x36c(%rbp,%rax,4)
jne 0x1436
jmp 0x1561
movslq -0x3b8(%rbp), %rax
movl -0x36c(%rbp,%rax,4), %edi
callq 0x1b70
movq %rax, -0x3a8(%rbp)
jmp 0x14ca
cmpl $0xc, -0x3b8(%rbp)
jne 0x1488
movslq -0x3b8(%rbp), %rax
cmpl $0x0, -0x36c(%rbp,%rax,4)
jne 0x1471
jmp 0x1575
movl -0x33c(%rbp), %edi
callq 0x1b70
movq %rax, -0x3b0(%rbp)
jmp 0x1561
movq -0x3b0(%rbp), %rax
movl -0x3b8(%rbp), %ecx
subl $0xd, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x14a6
jmp 0x1561
movq -0x3b0(%rbp), %rax
movl -0x3b8(%rbp), %ecx
subl $0xd, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edi
callq 0x1b70
movq %rax, -0x3a8(%rbp)
jmp 0x14ca
movl $0x0, -0x3b4(%rbp)
cmpl $0x400, -0x3b4(%rbp)    # imm = 0x400
jge 0x155f
movq -0x3a8(%rbp), %rax
movsbl 0x8(%rax), %eax
cmpl $0x2e, %eax
jne 0x14f9
cmpl $0x0, -0x314(%rbp)
je 0x1522
movq -0x3a8(%rbp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x0, %eax
je 0x1522
movq -0x3a8(%rbp), %rsi
addq $0x8, %rsi
leaq 0x1b5f(%rip), %rdi      # 0x307a
movb $0x0, %al
callq 0x1060
movl -0x3b4(%rbp), %eax
movq -0x3a8(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x3b4(%rbp)
movq -0x3a8(%rbp), %rax
movq -0x3a8(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x3a8(%rbp)
jmp 0x14d4
jmp 0x1561
movl -0x3b8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3b8(%rbp)
jmp 0x1407
jmp 0x15c3
movzwl -0x394(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
jne 0x1599
movzwl -0x394(%rbp), %eax
andl $0xa000, %eax           # imm = 0xA000
cmpl $0x0, %eax
je 0x15c1
leaq -0x310(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x21c0
leaq -0x310(%rbp), %rsi
leaq 0x1ac0(%rip), %rdi      # 0x307a
movb $0x0, %al
callq 0x1060
jmp 0x15c3
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x3c0, %rsp            # imm = 0x3C0
popq %rbp
retq
nopw %cs:(%rax,%rax)