// File: /workspace/github_c_makefile_star2_O0/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
subq $0x340, %rsp            # imm = 0x340
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1231
movq 0x3de2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1de4(%rip), %rsi      # 0x3004
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10e0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10b0
movl %eax, -0x32c(%rbp)
movl -0x32c(%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, 0x3e1b(%rip)      # 0x5090
movq $-0x1, %rax
cmpq %rax, 0x3e0d(%rip)      # 0x5090
jne 0x129b
leaq 0x1daf(%rip), %rdi      # 0x303b
callq 0x10c0
movl $0x1, %edi
callq 0x10e0
leaq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1030
leaq -0x210(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1030
leaq -0x210(%rbp), %rdi
leaq 0x1d6f(%rip), %rsi      # 0x3040
callq 0x1080
cmpl $0x0, %eax
jne 0x12ff
movq 0x3d16(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d56(%rip), %rsi      # 0x3042
movb $0x0, %al
callq 0x1090
movl $0x11, -0x4(%rbp)
jmp 0x14fe
leaq -0x210(%rbp), %rdi
callq 0x2150
leaq -0x210(%rbp), %rdi
callq 0x1fc0
movl %eax, -0x330(%rbp)
cmpl $0x0, -0x330(%rbp)
jne 0x134a
movq 0x3ccb(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d2f(%rip), %rsi      # 0x3066
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x14fe
callq 0x17b0
movq %rax, -0x320(%rbp)
movq -0x320(%rbp), %rax
movl -0x330(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x318(%rbp)
leaq -0x210(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x1030
leaq -0x310(%rbp), %rdi
leaq -0x210(%rbp), %rsi
callq 0x20f0
movq -0x318(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x1b10
movl %eax, -0x334(%rbp)
cmpl $0x0, -0x334(%rbp)
je 0x13e3
movq 0x3c32(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1c72(%rip), %rsi      # 0x3042
movb $0x0, %al
callq 0x1090
movl $0x11, -0x4(%rbp)
jmp 0x14fe
movl $0x4000, %edi           # imm = 0x4000
callq 0x1810
movl %eax, -0x334(%rbp)
movq -0x320(%rbp), %rax
movl -0x334(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x328(%rbp)
movq -0x318(%rbp), %rdi
movl -0x334(%rbp), %esi
leaq -0x310(%rbp), %rcx
movl $0x2, %edx
callq 0x1ee0
callq 0x1ac0
movl %eax, -0x338(%rbp)
movl -0x338(%rbp), %ecx
movq -0x328(%rbp), %rax
movl %ecx, 0x28(%rax)
movl -0x338(%rbp), %edi
callq 0x1aa0
movq %rax, -0x340(%rbp)
movq -0x340(%rbp), %rax
movb $0x2, 0x7(%rax)
movl -0x334(%rbp), %ecx
movq -0x340(%rbp), %rax
movl %ecx, (%rax)
movq -0x340(%rbp), %rdi
addq $0x8, %rdi
leaq 0x1c0e(%rip), %rsi      # 0x3099
callq 0x1030
movq -0x340(%rbp), %rax
movb $0x1, 0x6(%rax)
movq -0x340(%rbp), %rax
movw $0x400, 0x4(%rax)       # imm = 0x400
movq -0x328(%rbp), %rdi
movl -0x330(%rbp), %esi
movl $0x2, %edx
leaq 0x1bd7(%rip), %rcx      # 0x3098
callq 0x1ee0
callq 0x1530
movw 0x10(%rax), %cx
addw $0x1, %cx
movw %cx, 0x10(%rax)
movq -0x318(%rbp), %rax
movw 0x1a(%rax), %cx
addw $0x1, %cx
movw %cx, 0x1a(%rax)
movq -0x328(%rbp), %rax
movw $0x2, 0x1a(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
nopw (%rax,%rax)