// File: /workspace/github_c_makefile_star2_O2/JavieW[P]CSCC69_Ext2_File-System/ext2_mkdir.c
// Function: main
int main(int argc, char **argv) {
    char path[EXT2_NAME_LEN];
    char pathCopy[EXT2_NAME_LEN];
    char dirName[EXT2_NAME_LEN];
    struct ext2_inode *parent_inode, *inode_table, *target_inode;

    if(argc!=3) {
        fprintf(stderr, "Usage: ./ext2_mkdir <image file name> <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);
    }

    strcpy(path, argv[2]);
    strcpy(pathCopy, argv[2]);
    // if input is root, return EEXIST
    if (strcmp(pathCopy, "/") == 0) {
        fprintf(stderr, "Specified directory already exists\n");
        return EEXIST;
    }
    // get the parent directory of the specified directory
    getParentDirPath(pathCopy);
    int parent_inode_num = getInodeFromPath(pathCopy);
    // if any component on the path of parent directory does not exist,
    // return ENOENT
    if (parent_inode_num == 0) {
        fprintf(stderr, "One of the components on the path does not exist\n");
        return ENOENT;
    }

    // get the parent directory inode
    inode_table = getInodeTable();
    parent_inode = &inode_table[parent_inode_num-1];
    // get the directory name from input
    strcpy(pathCopy, path);
    getFileNameFromPath(dirName, pathCopy);
    int target_inode_num = searchFileInDir(parent_inode, dirName);
    // if specified dir already exists, return EEXIST
    if (target_inode_num != 0) {
        fprintf(stderr, "Specified directory already exists\n");
        return EEXIST;
    }

    // initialize an inode for the specified directory
    target_inode_num = initInode(EXT2_S_IFDIR);
    target_inode = &inode_table[target_inode_num-1];
    //create an directory entry for the specified directory
    initNewDirent(parent_inode, target_inode_num, EXT2_FT_DIR, dirName);

    // allocate a new block for the specified directory
    int newBlockNum = allocateNewBlock();
    target_inode->i_block[0] = newBlockNum;
    struct ext2_dir_entry_2 *firstDirent = (struct ext2_dir_entry_2 *)getBlock(newBlockNum);
    // initialize the data block information for the target directory
    firstDirent->file_type = EXT2_FT_DIR;
    firstDirent->inode = target_inode_num;
    // create an entry for .
    strcpy(firstDirent->name, ".");
    firstDirent->name_len = 1;
    firstDirent->rec_len = EXT2_BLOCK_SIZE;
    // create an entry for ..
    initNewDirent(target_inode, parent_inode_num, EXT2_FT_DIR, "..");

    // update the revelent information
    getGroupDesc()->bg_used_dirs_count++;
    parent_inode->i_links_count++;
    target_inode->i_links_count = 2;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
cmpl $0x3, %edi
jne 0x13ca
movq %rsi, %r14
movq 0x8(%rsi), %rdi
pushq $0x2
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1050
movq %rax, 0x2e55(%rip)      # 0x4090
cmpq $-0x1, %rax
je 0x13e8
movq 0x10(%r14), %r14
leaq -0x340(%rbp), %rdi
movq %r14, %rsi
callq 0x1030
leaq -0x140(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x1030
cmpw $0x2f, (%r15)
je 0x138b
leaq -0x140(%rbp), %r14
movq %r14, %rdi
callq 0x1b3f
movq %r14, %rdi
callq 0x1a42
testl %eax, %eax
je 0x13ac
movl %eax, %r14d
callq 0x1517
movq %rax, %rbx
movl %r14d, -0x2c(%rbp)
movslq %r14d, %r14
shlq $0x7, %r14
leaq (%rax,%r14), %r15
addq $-0x80, %r15
leaq -0x140(%rbp), %r12
leaq -0x340(%rbp), %rsi
movq %r12, %rdi
callq 0x1030
leaq -0x240(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x1b0a
movq %r15, %rdi
movq %r13, %rsi
callq 0x17d0
testl %eax, %eax
jne 0x138b
addq %rbx, %r14
movl $0x4000, %edi           # imm = 0x4000
callq 0x155d
movslq %eax, %r13
movq %r13, %r12
shlq $0x7, %r12
leaq (%rbx,%r12), %rax
addq $-0x80, %rax
movq %rax, -0x38(%rbp)
pushq $0x2
popq %rdx
leaq -0x240(%rbp), %rcx
movq %r15, %rdi
movl %r13d, %esi
movl %edx, %r15d
callq 0x19af
callq 0x1781
movl %eax, -0x58(%rbx,%r12)
movl %eax, %edi
callq 0x176b
movl %r13d, (%rax)
movw $0x2e, 0x8(%rax)
movl $0x2010400, 0x4(%rax)   # imm = 0x2010400
leaq 0xd46(%rip), %rcx       # 0x2096
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl %r15d, %edx
callq 0x19af
callq 0x1411
incw 0x10(%rax)
incw -0x66(%r14)
movw $0x2, -0x66(%rbx,%r12)
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c66(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xca4(%rip), %rdi       # 0x2040
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x10e0
pushq $0x11
popq %rbx
jmp 0x1377
movq 0x2c45(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xca7(%rip), %rdi       # 0x2064
pushq $0x31
popq %rsi
pushq $0x1
popq %rdx
callq 0x10e0
jmp 0x1377
movq 0x2c27(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc29(%rip), %rdi       # 0x2004
pushq $0x36
popq %rsi
pushq $0x1
popq %rdx
callq 0x10e0
jmp 0x13f4
leaq 0xc4c(%rip), %rdi       # 0x203b
callq 0x10b0
pushq $0x1
popq %rdi
callq 0x10d0