// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x2190, %rsp           # imm = 0x2190
cmpl $0x4, %edi
jne 0x141a
movq %rsi, %r14
movq 0x8(%rsi), %rdi
movl $0x2, %ebp
movl $0x2, %esi
xorl %eax, %eax
callq 0x1100
movl %eax, %ebx
movq 0x10(%r14), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x1100
movl %eax, %r15d
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1080
movq %rax, 0x3dc4(%rip)      # 0x50b8
cmpq $-0x1, %rax
je 0x141f
movq 0x10(%r14), %rdi
movq %rsp, %rsi
callq 0x10c0
cmpl $-0x1, %eax
je 0x1408
movq 0x30(%rsp), %rbx
movslq %ebx, %rsi
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %r15d, %r8d
xorl %r9d, %r9d
callq 0x1080
cmpq $-0x1, %rax
je 0x1424
movq %rax, %r15
callq 0x142c
movq 0x18(%r14), %rsi
leaq 0x1190(%rsp), %r14
movq %r14, %rdi
callq 0x1060
movq %r14, %rdi
callq 0x226f
leaq 0x190(%rsp), %r12
leaq 0x90(%rsp), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2286
movq %r12, %rdi
callq 0x16d1
testl %eax, %eax
js 0x1403
movl %eax, %r14d
leaq 0x90(%rsp), %rsi
movl %eax, %edi
callq 0x149b
movl $0x11, %ebp
testl %eax, %eax
jg 0x1408
movq 0x3d17(%rip), %rax      # 0x50c0
movl %r14d, %ecx
shlq $0x7, %rcx
leaq (%rax,%rcx), %r14
addq $-0x80, %r14
movl $0x8000, %edi           # imm = 0x8000
movl %ebx, %esi
callq 0x23da
movq 0x3cf5(%rip), %rcx      # 0x50c0
movslq %eax, %r12
movq %r12, %rax
shlq $0x7, %rax
leaq (%rcx,%rax), %rdi
addq $-0x80, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x22ff
leaq 0x90(%rsp), %rdx
movq %r14, %rdi
movl %r12d, %esi
movl $0x1, %ecx
callq 0x1959
xorl %ebp, %ebp
jmp 0x1408
movl $0x2, %ebp
movl %ebp, %eax
addq $0x2190, %rsp           # imm = 0x2190
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x1150
callq 0x118a
callq 0x1175
nopl (%rax)