// File: /workspace/github_c_makefile_star2_O3/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, %ebx
leal -0x5(%rbx), %eax
cmpl $-0x3, %eax
jbe 0x1474
movq %rsi, %r14
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10c0
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1060
movq %rax, 0x2dac(%rip)      # 0x40a0
cmpq $-0x1, %rax
je 0x1479
callq 0x15a9
movq %rax, 0x2d9e(%rip)      # 0x40a8
movq 0x10(%r14), %rsi
cmpl $0x3, %ebx
je 0x1334
cmpb $0x2d, (%rsi)
jne 0x146f
cmpb $0x61, 0x1(%rsi)
jne 0x146f
cmpb $0x0, 0x2(%rsi)
jne 0x146f
movq 0x18(%r14), %rsi
leaq -0x1b0(%rbp), %r14
movq %r14, %rdi
callq 0x1030
leaq -0x3b0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x1030
movq %r15, %rdi
callq 0x1bc5
testl %eax, %eax
je 0x1463
movq 0x2d3c(%rip), %rcx      # 0x40a8
cltq
shlq $0x7, %rax
leaq (%rcx,%rax), %rsi
addq $-0x80, %rsi
leaq -0xac(%rbp), %rax
movl $0x20, %ecx
movq %rax, %rdi
rep  movsl (%rsi), %es:(%rdi)
movl (%rax), %eax
btl $0xe, %eax
jb 0x13c6
movzwl %ax, %eax
xorl %ebx, %ebx
cmpl $0x2000, %eax           # imm = 0x2000
jb 0x144f
leaq -0x2b0(%rbp), %r14
leaq -0x1b0(%rbp), %rsi
movq %r14, %rdi
callq 0x1c99
movq %r14, %rdi
callq 0x1040
jmp 0x144f
movl -0x54(%rbp), %eax
movl %eax, -0x2c(%rbp)
xorl %r13d, %r13d
xorl %r15d, %r15d
cmpq $0xb, %r13
ja 0x13e2
movl -0x84(%rbp,%r13,4), %edi
jmp 0x13fe
cmpq $0xc, %r13
jne 0x13f9
movl -0x2c(%rbp), %edi
testl %edi, %edi
je 0x144d
callq 0x1856
movq %rax, %r15
jmp 0x1441
movl -0x34(%r15,%r13,4), %edi
testl %edi, %edi
je 0x1441
callq 0x1856
movq %rax, %r12
xorl %r14d, %r14d
cmpl $0x3, %ebx
jne 0x141a
cmpb $0x2e, 0x8(%r12)
je 0x142c
cmpb $0x0, 0x6(%r12)
je 0x142c
leaq 0x8(%r12), %rdi
callq 0x1040
movzwl 0x4(%r12), %eax
addl %eax, %r14d
addq %rax, %r12
cmpl $0x400, %r14d           # imm = 0x400
jb 0x140d
incq %r13
cmpq $0x10d, %r13            # imm = 0x10D
jne 0x13d2
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1148
movl $0x2, %ebx
jmp 0x144f
callq 0x1120
callq 0x1182
callq 0x116a
nop