// File: /workspace/github_c_makefile_star2_O0/Carben-dev[P]ext2fs-simple-utilities/ext2_rm.c
// Function: main
int main(int argc, const char * argv[]) {
    if(argc != 3) {
        fprintf(stderr, "Usage: <image file name> <absolute path of rm file>\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();
    
    //
    unsigned long path_len = strlen(argv[2]) + 1;
    char path[path_len];
    char path_parent[path_len];
    char path_name[EXT2_NAME_LEN];
    
    strcpy(path, argv[2]);
    clear_end_slash(path);
    cut_path(path, path_parent, path_name);
    
    // check if exist
    int parent_inode_num = get_inode_number_by_path(path_parent);
    int file_inode_num = get_inode_number_by_path(path);
    if (file_inode_num < 0) {
        return ENOENT;
    }
    
    struct ext2_inode *parent_inode = inode_table + parent_inode_num - 1;
    struct ext2_inode *file_inode = inode_table + file_inode_num - 1;
    if (file_inode->i_mode & EXT2_S_IFDIR) {
        return EISDIR;
    }
    
    remove_from_dir_entry(parent_inode, path_name);
    
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1271
movq 0x4da2(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x2da4(%rip), %rsi      # 0x4004
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1120
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x1100
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 0x1080
movq %rax, 0x4e01(%rip)      # 0x60b0
movq $-0x1, %rax
cmpq %rax, 0x4df3(%rip)      # 0x60b0
jne 0x12d5
leaq 0x2d73(%rip), %rdi      # 0x4039
callq 0x1110
movl $0x1, %edi
callq 0x1120
callq 0x1450
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1070
incq %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x168(%rbp)
movq %rdi, %rsp
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x170(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1060
movq -0x168(%rbp), %rdi
callq 0x2b60
movq -0x170(%rbp), %rsi
movq -0x168(%rbp), %rdi
leaq -0x140(%rbp), %rdx
callq 0x2bc0
movq -0x170(%rbp), %rdi
callq 0x18f0
movq -0x168(%rbp), %rdi
movl %eax, -0x144(%rbp)
callq 0x18f0
movl %eax, -0x148(%rbp)
cmpl $0x0, -0x148(%rbp)
jge 0x13b1
movl $0x2, -0x4(%rbp)
movl $0x1, -0x14c(%rbp)
jmp 0x143c
movq 0x4d00(%rip), %rax      # 0x60b8
movslq -0x144(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
addq $-0x80, %rax
movq %rax, -0x158(%rbp)
movq 0x4ce0(%rip), %rax      # 0x60b8
movslq -0x148(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
addq $-0x80, %rax
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rax
movzwl (%rax), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x1418
movl $0x15, -0x4(%rbp)
movl $0x1, -0x14c(%rbp)
jmp 0x143c
movq -0x158(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x20d0
movl $0x0, -0x4(%rbp)
movl $0x1, -0x14c(%rbp)
movq -0x28(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax,%rax)