// File: /workspace/github_c_makefile_star2_O0/Carben-dev[P]ext2fs-simple-utilities/ext2_mkdir.c
// Function: main
int main(int argc, const char * argv[]) {
    if(argc != 3) {
        fprintf(stderr, "Usage: <image file name> <absolute path of new dir>\n");
        exit(1);
    }
    int fd = open(argv[1], O_RDWR);
    
    // map disk img into memory
    disk = mmap(NULL, 128 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
        perror("mmap");
        exit(1);
    }
    
    // Init utils
    ext2_utils_init();
    
    char path[PATH_MAX];
    char new_dir_name[EXT2_NAME_LEN];
    char parent_path[PATH_MAX];
    
    // Copy argv[2] to user stack
    memcpy(path, argv[2], strlen(argv[2]));
    
    // Case: handle user input path end with '/'
    // For example.
    //      "/d1/" -> "/d1"
    clear_end_slash(path);
    
    
    
    // Prepare parent path and new dir name
    // For example.
    //      "/d1/d2" -> parent path: "/d1" , new dir name: "d2"
    //      "/d1"    -> parent path: "/"   , new dir name: "d1"
    cut_path(path, parent_path, new_dir_name);
    
    
    
    // Find out in which inode this dir should be created
    int in_which_inode = get_inode_number_by_path(parent_path);
    if (in_which_inode == -1) {
        return ENOENT;
    }
    
    // Check if dir already exists
    if (get_inode_number_by_name(in_which_inode, new_dir_name) > 0) {
        return EEXIST;
    }
    
    // create dir
    int rs;
    if ((rs = inode_mkdir(in_which_inode, new_dir_name)) != 0) {
        return rs;
    }
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2140, %rsp           # imm = 0x2140
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1261
movq 0x4db2(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x2db4(%rip), %rsi      # 0x4004
movb $0x0, %al
callq 0x10b0
movl $0x1, %edi
callq 0x1110
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10f0
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x20000, %esi          # imm = 0x20000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x1070
movq %rax, 0x4e09(%rip)      # 0x60a8
movq $-0x1, %rax
cmpq %rax, 0x4dfb(%rip)      # 0x60a8
jne 0x12c5
leaq 0x2d83(%rip), %rdi      # 0x4039
callq 0x1100
movl $0x1, %edi
callq 0x1110
callq 0x13b0
leaq -0x1020(%rbp), %rax
movq %rax, -0x2138(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x2130(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1060
movq -0x2138(%rbp), %rdi
movq -0x2130(%rbp), %rsi
movq %rax, %rdx
callq 0x10c0
leaq -0x1020(%rbp), %rdi
callq 0x2ac0
leaq -0x1020(%rbp), %rdi
leaq -0x2120(%rbp), %rsi
leaq -0x1120(%rbp), %rdx
callq 0x2b20
leaq -0x2120(%rbp), %rdi
callq 0x1850
movl %eax, -0x2124(%rbp)
cmpl $-0x1, -0x2124(%rbp)
jne 0x1354
movl $0x2, -0x4(%rbp)
jmp 0x139a
movl -0x2124(%rbp), %edi
leaq -0x1120(%rbp), %rsi
callq 0x1470
cmpl $0x0, %eax
jle 0x1374
movl $0x11, -0x4(%rbp)
jmp 0x139a
movl -0x2124(%rbp), %edi
leaq -0x1120(%rbp), %rsi
callq 0x18b0
movl %eax, -0x2128(%rbp)
cmpl $0x0, %eax
je 0x139a
movl -0x2128(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2140, %rsp           # imm = 0x2140
popq %rbp
retq
nopw %cs:(%rax,%rax)