// File: /workspace/github_c_makefile_star2_O3/Carben-dev[P]ext2fs-simple-utilities/ext2_ln.c
// Function: main
int main(int argc, const char * argv[]) {
    // if -s flag set, this will be set to 1
    int create_symlink = 0;
    
    if(argc < 4) {
        fprintf(stderr, "Usage: <image file name> <-s flag> <src> <dst>\n");
        exit(1);
    }
    
    if (argc == 5 && strncmp("-s", argv[2], strlen("-s")) == 0) {
        create_symlink = 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();
    
    // find out max path len
    unsigned long src_path_len;
    unsigned long lnk_path_len;
    if (create_symlink) {
        src_path_len = strlen(argv[3]) + 1;
        lnk_path_len = strlen(argv[4]) + 1;
    } else {
        src_path_len = strlen(argv[2]) + 1;
        lnk_path_len = strlen(argv[3]) + 1;
    }
    
    //
    char src_path[src_path_len];
    char lnk_path[lnk_path_len];
    char src_parent_path[src_path_len];
    char src_file_name[EXT2_NAME_LEN];
    char lnk_parent_path[lnk_path_len];
    char lnk_file_name[EXT2_NAME_LEN];
    
    if (create_symlink) {
        strcpy(src_path, argv[3]);
        strcpy(lnk_path, argv[4]);
    } else {
        strcpy(src_path, argv[2]);
        strcpy(lnk_path, argv[3]);
    }
    
    clear_end_slash(src_path);
    clear_end_slash(lnk_path);
    
    cut_path(src_path, src_parent_path, src_file_name);
    cut_path(lnk_path, lnk_parent_path, lnk_file_name);
    
    int src_file_inode_num = get_inode_number_by_path(src_path);
    struct ext2_inode *src_file_inode = inode_table + (src_file_inode_num - 1);
    int lnk_parent_inode_num = get_inode_number_by_path(lnk_parent_path);
    struct ext2_inode *lnk_parent_inode = inode_table + (lnk_parent_inode_num - 1);
    
    // check if src path exist
    if (src_file_inode_num < 0) {
        return ENOENT;
    }
    
    // check if lnk parent exist
    if (lnk_parent_inode_num < 0) {
        return ENOENT;
    }
    
    
    // check if lnk already exist
    if (get_inode_number_by_name(lnk_parent_inode_num, lnk_parent_path) > 0) {
        return EEXIST;
    }
    
    if (create_symlink) { // create soft link
        // allocate space in inode_table for soft link
        int soft_link_inode_num = new_inode(EXT2_S_IFLNK, strlen(src_path)+1);
        struct ext2_inode *soft_link_inode = inode_table + (soft_link_inode_num - 1);
        
        // copy src_path to datablock
        copy_to_inode_datablock(soft_link_inode, (unsigned char *)src_path, strlen(src_path)+1);
        
        // add soft link inode to lnk_parent
        add_to_dir_entry(lnk_parent_inode, soft_link_inode_num, lnk_file_name, EXT2_FT_SYMLINK);
        return 0;
        
    } else { // create hard link
        // check if src is a dir
        if (src_file_inode->i_mode & EXT2_S_IFDIR) {
            return EISDIR;
        }
        
        unsigned char type;
        
        // check file type
        if ((src_file_inode->i_mode & EXT2_S_IFLNK) == EXT2_S_IFLNK) {
            type = EXT2_FT_SYMLINK;
        } else if (src_file_inode->i_mode & EXT2_S_IFREG) {
            type = EXT2_FT_REG_FILE;
        } else {
            exit(-1);
        }
        
        add_to_dir_entry(lnk_parent_inode, src_file_inode_num, lnk_file_name, type);
        return 0;
    }
    
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
cmpl $0x3, %edi
jle 0x14d5
movq %rsi, %rbx
movb $0x1, %r14b
cmpl $0x5, %edi
jne 0x12b5
movq 0x10(%rbx), %rcx
movzbl (%rcx), %eax
subl $0x2d, %eax
jne 0x12ad
movzbl 0x1(%rcx), %ecx
movl $0x73, %eax
subl %ecx, %eax
jmp 0x12af
negl %eax
testl %eax, %eax
setne %r14b
movq 0x8(%rbx), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10f0
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1080
movq %rax, 0x3dc8(%rip)      # 0x50b0
cmpq $-0x1, %rax
je 0x14da
callq 0x14ec
movb %r14b, -0x29(%rbp)
testb %r14b, %r14b
je 0x1316
movq 0x10(%rbx), %rdi
movq %rdi, -0x38(%rbp)
callq 0x1070
movq %rax, %r14
movq 0x18(%rbx), %r13
jmp 0x132a
movq 0x18(%rbx), %rdi
movq %rdi, -0x38(%rbp)
callq 0x1070
movq %rax, %r14
movq 0x20(%rbx), %r13
movq %r13, %rdi
callq 0x1070
movq %rsp, %rbx
addq $0x10, %r14
andq $-0x10, %r14
subq %r14, %rbx
movq %rbx, %rsp
movq %rsp, %r15
addq $0x10, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %r12
subq %r14, %r12
movq %r12, %rsp
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x1060
movq %r15, %rdi
movq %r13, %rsi
callq 0x1060
movq %rbx, %rdi
callq 0x232f
movq %r15, %rdi
callq 0x232f
leaq -0x240(%rbp), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2346
leaq -0x140(%rbp), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2346
movq %rbx, %rdi
callq 0x1791
movl %eax, %r15d
movslq %eax, %r13
shlq $0x7, %r13
addq 0x3cee(%rip), %r13      # 0x50b8
movq %r14, %rdi
callq 0x1791
movl %eax, %r12d
movl $0x2, %eax
testl %r15d, %r15d
js 0x14c6
testl %r12d, %r12d
js 0x14c6
movq 0x3cc5(%rip), %rax      # 0x50b8
movq %rax, -0x38(%rbp)
movl %r12d, %edi
movq %r14, %rsi
callq 0x155b
movl %eax, %ecx
movl $0x11, %eax
testl %ecx, %ecx
jg 0x14c6
movslq %r12d, %rax
shlq $0x7, %rax
movq -0x38(%rbp), %rcx
leaq (%rcx,%rax), %r14
addq $-0x80, %r14
cmpb $0x0, -0x29(%rbp)
je 0x1469
movzwl -0x80(%r13), %ecx
movl $0x15, %eax
btl $0xe, %ecx
jb 0x14c6
movl %ecx, %edx
notl %edx
movb $0x7, %al
testl $0xa000, %edx           # imm = 0xA000
je 0x1457
movb $0x1, %al
testw %cx, %cx
jns 0x14df
movzbl %al, %ecx
leaq -0x140(%rbp), %rdx
movq %r14, %rdi
movl %r15d, %esi
jmp 0x14bf
movq %rbx, %rdi
callq 0x1070
leal 0x1(%rax), %esi
movl $0xa000, %edi           # imm = 0xA000
callq 0x249a
movq 0x3c33(%rip), %rcx      # 0x50b8
movslq %eax, %r15
movq %r15, %rax
shlq $0x7, %rax
leaq (%rcx,%rax), %r12
addq $-0x80, %r12
movq %rbx, %rdi
callq 0x1070
leal 0x1(%rax), %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x23bf
leaq -0x140(%rbp), %rdx
movq %r14, %rdi
movl %r15d, %esi
movl $0x7, %ecx
callq 0x1a19
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1155
callq 0x1140
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110
nopl (%rax)