// File: /workspace/github_c_makefile_star2_O1/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 0x146d
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10b0
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1050
movq %rax, 0x2d99(%rip)      # 0x4098
cmpq $-0x1, %rax
je 0x1472
movq 0x10(%rbx), %rbx
leaq -0x340(%rbp), %rdi
movq %rbx, %rsi
callq 0x1030
leaq -0x140(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1030
cmpw $0x2f, (%r14)
je 0x144e
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
callq 0x1ce9
movq %rbx, %rdi
callq 0x1bce
testl %eax, %eax
je 0x1455
movl %eax, %r14d
callq 0x15a1
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 0x1ca2
movq %r15, %rdi
movq %r13, %rsi
callq 0x18de
testl %eax, %eax
jne 0x1461
addq %rbx, %r14
movl $0x4000, %edi           # imm = 0x4000
callq 0x15e7
movslq %eax, %r13
movq %r13, %r12
shlq $0x7, %r12
leaq (%rbx,%r12), %rax
addq $-0x80, %rax
movq %rax, -0x38(%rbp)
leaq -0x240(%rbp), %rcx
movq %r15, %rdi
movl %r13d, %esi
movl $0x2, %edx
callq 0x1b42
callq 0x1864
movl %eax, -0x58(%rbx,%r12)
movl %eax, %edi
callq 0x184e
movl %r13d, (%rax)
movw $0x2e, 0x8(%rax)
movl $0x2010400, 0x4(%rax)   # imm = 0x2010400
leaq 0xc83(%rip), %rcx       # 0x2096
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl $0x2, %edx
callq 0x1b42
callq 0x148d
incw 0x10(%rax)
incw -0x66(%r14)
movw $0x2, -0x66(%rbx,%r12)
xorl %eax, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x117c
jmp 0x1466
callq 0x115a
movl $0x2, %eax
jmp 0x143c
callq 0x1138
movl $0x11, %eax
jmp 0x143c
callq 0x1110
callq 0x119e
nop