// File: /workspace/github_c_makefile_star2_O0/Carben-dev[P]ext2fs-simple-utilities/ext2_cp.c
// Function: main
int main(int argc, const char * argv[]) {
    if(argc != 4) {
        fprintf(stderr, "Usage: <image file name> <src> <dst>\n");
        exit(1);
    }
    int fd = open(argv[1], O_RDWR);
    int src_fd = open(argv[2], 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);
    }
    
    // map src file into memory
    struct stat src_stat;
    if (lstat(argv[2], &src_stat) == -1) {
        return ENOENT;
    }
    int src_file_size = (int)src_stat.st_size;
    unsigned char *src_file = mmap(NULL, src_file_size, PROT_READ | PROT_WRITE, MAP_SHARED, src_fd, 0);
    if(src_file == MAP_FAILED) {
        perror("mmap");
        exit(1);
    }
    // Init utils
    ext2_utils_init();
    
    
    char dst_path[PATH_MAX];
    char dst_file_name[EXT2_NAME_LEN];
    char dst_file_parent[PATH_MAX];
    strcpy(dst_path, argv[3]);
    clear_end_slash(dst_path);
    cut_path(dst_path, dst_file_parent, dst_file_name);
    
    
    // find out dst file parent dir inode
    int dst_file_parent_inode_num = get_inode_number_by_path(dst_file_parent);
    // check if dst file parent exist
    if (dst_file_parent_inode_num < 0) {
        return ENOENT;
    }
    // check if there is a file in dst dir has same name as src file
    if (get_inode_number_by_name(dst_file_parent_inode_num, dst_file_name) > 0) {
        return EEXIST;
    }
    struct ext2_inode *dst_file_parent_inode = inode_table + (dst_file_parent_inode_num - 1);
    
    // create inode for dst_file
    int dst_file_inode_num = new_inode(EXT2_S_IFREG, src_file_size);
    struct ext2_inode *dst_file_inode = inode_table + dst_file_inode_num - 1;
    
    
    /* -- copy datablock -- */
    copy_to_inode_datablock(dst_file_inode, src_file, src_file_size);
    
    // add new file entry to dst parent dir entry
    add_to_dir_entry(dst_file_parent_inode, dst_file_inode_num, dst_file_name, EXT2_FT_REG_FILE);

    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x21e0, %rsp           # imm = 0x21E0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x4, -0x8(%rbp)
je 0x1281
movq 0x4d92(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x2d94(%rip), %rsi      # 0x4004
movb $0x0, %al
callq 0x10d0
movl $0x1, %edi
callq 0x1130
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x1110
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x1110
movl %eax, -0x18(%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, 0x4de2(%rip)      # 0x60b8
movq $-0x1, %rax
cmpq %rax, 0x4dd4(%rip)      # 0x60b8
jne 0x12fc
leaq 0x2d3d(%rip), %rdi      # 0x402a
callq 0x1120
movl $0x1, %edi
callq 0x1130
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x10c0
cmpl $-0x1, %eax
jne 0x1321
movl $0x2, -0x4(%rbp)
jmp 0x1491
movq -0x78(%rbp), %rax
movl %eax, -0xac(%rbp)
movslq -0xac(%rbp), %rsi
movl -0x18(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x1080
movq %rax, -0xb8(%rbp)
movq $-0x1, %rax
cmpq %rax, -0xb8(%rbp)
jne 0x137a
leaq 0x2cbf(%rip), %rdi      # 0x402a
callq 0x1120
movl $0x1, %edi
callq 0x1130
callq 0x14a0
leaq -0x10c0(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x1060
leaq -0x10c0(%rbp), %rdi
callq 0x2bb0
leaq -0x10c0(%rbp), %rdi
leaq -0x21c0(%rbp), %rsi
leaq -0x11c0(%rbp), %rdx
callq 0x2c10
leaq -0x21c0(%rbp), %rdi
callq 0x1940
movl %eax, -0x21c4(%rbp)
cmpl $0x0, -0x21c4(%rbp)
jge 0x13e0
movl $0x2, -0x4(%rbp)
jmp 0x1491
movl -0x21c4(%rbp), %edi
leaq -0x11c0(%rbp), %rsi
callq 0x1560
cmpl $0x0, %eax
jle 0x1403
movl $0x11, -0x4(%rbp)
jmp 0x1491
movq 0x4cb6(%rip), %rax      # 0x60c0
movl -0x21c4(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x21d0(%rbp)
movl -0xac(%rbp), %esi
movl $0x8000, %edi           # imm = 0x8000
callq 0x2e00
movl %eax, -0x21d4(%rbp)
movq 0x4c7f(%rip), %rax      # 0x60c0
movslq -0x21d4(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
addq $-0x80, %rax
movq %rax, -0x21e0(%rbp)
movq -0x21e0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movl -0xac(%rbp), %edx
callq 0x2cd0
movq -0x21d0(%rbp), %rdi
movl -0x21d4(%rbp), %esi
leaq -0x11c0(%rbp), %rdx
movl $0x1, %ecx
callq 0x1e30
movl -0x4(%rbp), %eax
addq $0x21e0, %rsp           # imm = 0x21E0
popq %rbp
retq
nopl (%rax)