// File: /workspace/github_c_makefile_star2_O1/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 0x14ca
movq %rsi, %r14
movb $0x1, %bl
cmpl $0x5, %edi
jne 0x12ad
movq 0x10(%r14), %rsi
leaq 0x1d96(%rip), %rdi      # 0x3034
movl $0x2, %edx
callq 0x1050
testl %eax, %eax
setne %bl
movq 0x8(%r14), %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, 0x3dd0(%rip)      # 0x50b0
cmpq $-0x1, %rax
je 0x14cf
callq 0x14e0
movb %bl, -0x29(%rbp)
movl %ebx, %eax
xorb $0x1, %al
movzbl %al, %ebx
movq 0x10(%r14,%rbx,8), %rdi
callq 0x1070
movq %rax, %r12
movq 0x18(%r14,%rbx,8), %rdi
movq %rbx, %r15
movq %rbx, -0x38(%rbp)
callq 0x1070
addq $0x10, %r12
andq $-0x10, %r12
movq %r14, %rcx
movq %rsp, %r14
subq %r12, %r14
movq %r14, %rsp
addq $0x10, %rax
andq $-0x10, %rax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %rsp, %rbx
subq %r12, %rbx
movq %rbx, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq 0x10(%rcx,%r15,8), %rsi
movq %rcx, %r15
movq %r14, %rdi
callq 0x1060
movq -0x38(%rbp), %rax
movq 0x18(%r15,%rax,8), %rsi
movq %r13, %rdi
callq 0x1060
movq %r14, %rdi
callq 0x2265
movq %r13, %rdi
callq 0x2265
leaq -0x240(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x227c
leaq -0x140(%rbp), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x227c
movq %r14, %rdi
callq 0x17c2
movl %eax, %r15d
movslq %eax, %r13
shlq $0x7, %r13
addq 0x3cfc(%rip), %r13      # 0x50b8
movq %r12, %rdi
callq 0x17c2
testl %r15d, %r15d
js 0x1456
movl %eax, %ebx
testl %eax, %eax
movl $0x2, %ecx
js 0x14b9
movq 0x3cd5(%rip), %rax      # 0x50b8
movq %rax, -0x38(%rbp)
movl %ebx, %edi
movq %r12, %rsi
callq 0x154f
movl $0x11, %ecx
testl %eax, %eax
jg 0x14b9
movslq %ebx, %rax
shlq $0x7, %rax
movq -0x38(%rbp), %rcx
leaq (%rcx,%rax), %r12
addq $-0x80, %r12
cmpb $0x0, -0x29(%rbp)
je 0x145d
movzwl -0x80(%r13), %eax
movl $0x15, %ecx
btl $0xe, %eax
jb 0x14b9
movl %eax, %edx
notl %edx
movb $0x7, %cl
testl $0xa000, %edx           # imm = 0xA000
je 0x1444
movb $0x1, %cl
testw %ax, %ax
jns 0x14d4
movzbl %cl, %ecx
leaq -0x140(%rbp), %rdx
movq %r12, %rdi
movl %r15d, %esi
jmp 0x14b2
movl $0x2, %ecx
jmp 0x14b9
movq %r14, %rdi
callq 0x1070
leal 0x1(%rax), %esi
movl $0xa000, %edi           # imm = 0xA000
callq 0x23c0
movq 0x3c3f(%rip), %rcx      # 0x50b8
movslq %eax, %rbx
movq %rbx, %rax
shlq $0x7, %rax
leaq (%rcx,%rax), %r15
addq $-0x80, %r15
movq %r14, %rdi
callq 0x1070
leal 0x1(%rax), %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x22f6
leaq -0x140(%rbp), %rdx
movq %r12, %rdi
movl %ebx, %esi
movl $0x7, %ecx
callq 0x1a4a
xorl %ecx, %ecx
movl %ecx, %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
nop