// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x388, %rsp            # imm = 0x388
movl %edi, %r14d
leal -0x5(%r14), %eax
cmpl $-0x3, %eax
jbe 0x13ee
movq %rsi, %r15
movq 0x8(%rsi), %rdi
pushq $0x2
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10b0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1060
movq %rax, 0x2e46(%rip)      # 0x4098
cmpq $-0x1, %rax
je 0x140c
callq 0x153b
movq %rax, 0x2e38(%rip)      # 0x40a0
movq 0x10(%r15), %rsi
cmpl $0x3, %r14d
je 0x1293
cmpb $0x2d, (%rsi)
jne 0x13d9
cmpb $0x61, 0x1(%rsi)
jne 0x13d9
cmpb $0x0, 0x2(%rsi)
jne 0x13d9
movq 0x18(%r15), %rsi
leaq -0x1b0(%rbp), %r15
movq %r15, %rdi
callq 0x1030
leaq -0x3b0(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x1030
movq %r12, %rdi
callq 0x1a66
testl %eax, %eax
je 0x13bb
movq 0x2dd5(%rip), %rcx      # 0x40a0
cltq
shlq $0x7, %rax
leaq (%rcx,%rax), %rsi
addq $-0x80, %rsi
pushq $0x20
popq %rcx
leaq -0xac(%rbp), %rax
movq %rax, %rdi
rep  movsl (%rsi), %es:(%rdi)
movl (%rax), %eax
btl $0xe, %eax
jb 0x131f
xorl %ebx, %ebx
cmpw $0x2000, %ax            # imm = 0x2000
jb 0x13a7
leaq -0x2b0(%rbp), %r14
leaq -0x1b0(%rbp), %rsi
movq %r14, %rdi
callq 0x1b2e
movq %r14, %rdi
callq 0x1040
jmp 0x13a7
movl -0x54(%rbp), %eax
movl %eax, -0x2c(%rbp)
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpq $0x10d, %rbx            # imm = 0x10D
je 0x13a5
cmpq $0xb, %rbx
ja 0x1342
movl -0x84(%rbp,%rbx,4), %edi
jmp 0x135e
cmpq $0xc, %rbx
jne 0x1359
movl -0x2c(%rbp), %edi
testl %edi, %edi
je 0x13a5
callq 0x178f
movq %rax, %r12
jmp 0x13a0
movl -0x34(%r12,%rbx,4), %edi
testl %edi, %edi
je 0x13a0
callq 0x178f
movq %rax, %r13
xorl %r15d, %r15d
cmpl $0x3ff, %r15d           # imm = 0x3FF
ja 0x13a0
cmpl $0x3, %r14d
jne 0x1383
cmpb $0x2e, 0x8(%r13)
je 0x1393
cmpb $0x0, 0x6(%r13)
je 0x1393
leaq 0x8(%r13), %rdi
callq 0x1040
movzwl 0x4(%r13), %eax
addl %eax, %r15d
addq %rax, %r13
jmp 0x136d
incq %rbx
jmp 0x132a
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c36(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc90(%rip), %rdi       # 0x205c
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
jmp 0x13a7
movq 0x2c18(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc64(%rip), %rdi       # 0x204e
pushq $0xd
jmp 0x1401
movq 0x2c03(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc05(%rip), %rdi       # 0x2004
pushq $0x44
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
jmp 0x1418
leaq 0xc36(%rip), %rdi       # 0x2049
callq 0x10c0
pushq $0x1
popq %rdi
callq 0x10e0