// File: /workspace/github_c_makefile_star2_O2/JavieW[P]CSCC69_Ext2_File-System/ext2_cp.c
// Function: main
int main(int argc, char **argv) {
    FILE *src_fd;
    char parentDirPath[EXT2_NAME_LEN];
    char fileName[EXT2_NAME_LEN];
    int parentInodeNum, childInodeNum;
    struct ext2_inode *inodeTable;
    struct ext2_inode *parentInode, *childInode;

    if(argc!=4) {
        fprintf(stderr, "Usage: ext2_cp <image file name> <native path> <absolute path on the disk>\n");
        exit(1);
    }

    // read disk and get inode table
    int fd = open(argv[1], O_RDWR);
    disk = mmap(NULL, 128 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
        perror("mmap");
        exit(1);
    }
    inodeTable = getInodeTable();

    // open native file for read
    if((src_fd = fopen(argv[2], "r")) == NULL) {
		fprintf(stderr, "No such file or directory\n");
        return ENOENT;
	}

    // get the parent directory inode
    strcpy(parentDirPath, argv[3]);
    if (parentDirPath[0]!='/') {
        fprintf(stderr, "Must be absolute path\n");
        return ENOENT;
    } else if (parentDirPath[1]=='\0'){
        fprintf(stderr, "Destination cannot be root directory\n");
        return EEXIST;
    } else {
        getParentDirPath(parentDirPath);
    }
    parentInodeNum = getInodeFromPath(parentDirPath);
    if (parentInodeNum == 0) {
        fprintf(stderr, "No such file or directory\n");
        return ENOENT;
    }
    parentInode = &inodeTable[parentInodeNum-1];

    // check file exist
    getFileNameFromPath(fileName, argv[3]);
    childInodeNum = searchFileInDir(parentInode, fileName);
    if (childInodeNum != 0) {
        fprintf(stderr, "File or directory already exist\n");
        return EEXIST;
    }

    // create file and cp
    childInodeNum = initInode(EXT2_S_IFREG);
    childInode = &inodeTable[childInodeNum-1];
    unsigned int *singleIndirect = NULL;
    int nextBlockNum, byteRead;
    int fileSize = 0;
    int i = 0;
    while (!feof(src_fd)) {
        nextBlockNum = allocateNewBlock();
        if (i<12) {
            childInode->i_block[i] = nextBlockNum;
        } else if (i==12) {
            childInode->i_block[i] = nextBlockNum;
            singleIndirect = initSingleIndirect(nextBlockNum);
            i++;
            continue;
        } else {
            singleIndirect[i-13] = nextBlockNum;
        }

        byteRead = fread(getBlock(nextBlockNum), 1, 1024, src_fd);
        fileSize += byteRead;
        i++;
    }
    fclose(src_fd);
    // update inode fields
    childInode->i_size = fileSize;
    if (singleIndirect == NULL)
        childInode->i_blocks = ((fileSize+1023)/1024)*2;
    else
        childInode->i_blocks = ((fileSize+1023)/1024+1)*2;
    // add dir_entry fot this file into parent dir
    initNewDirent(parentInode, childInodeNum, EXT2_FT_REG_FILE, fileName);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
cmpl $0x4, %edi
jne 0x149a
movq %rsi, %r15
movq 0x8(%rsi), %rdi
pushq $0x2
popq %r14
movl %r14d, %esi
xorl %eax, %eax
callq 0x10d0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2e33(%rip)      # 0x40b0
cmpq $-0x1, %rax
je 0x14b8
callq 0x15e7
movq %rax, %r12
movq 0x10(%r15), %rdi
leaq 0xdbb(%rip), %rsi       # 0x2055
callq 0x10e0
testq %rax, %rax
je 0x1430
movq %rax, %rbx
movq 0x18(%r15), %rsi
leaq -0x150(%rbp), %r13
movq %r13, %rdi
callq 0x1030
cmpb $0x2f, (%r13)
jne 0x1445
cmpb $0x0, -0x14f(%rbp)
je 0x1463
movq %r13, %rdi
callq 0x1c0f
movq %r13, %rdi
callq 0x1b12
testl %eax, %eax
je 0x1430
cltq
shlq $0x7, %rax
leaq (%r12,%rax), %r13
addq $-0x80, %r13
movq 0x18(%r15), %rsi
leaq -0x250(%rbp), %r14
movq %r14, %rdi
callq 0x1bda
movq %r13, %rdi
movq %r14, %rsi
callq 0x18a0
testl %eax, %eax
jne 0x1478
movq %r13, -0x50(%rbp)
movl $0x8000, %edi           # imm = 0x8000
callq 0x162d
movl %eax, -0x34(%rbp)
cltq
shlq $0x7, %rax
leaq (%rax,%r12), %rcx
movq %rcx, -0x40(%rbp)
addq %r12, %rax
addq $-0x58, %rax
movq $0x0, -0x30(%rbp)
pushq $0x1
popq %r14
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %rax, -0x48(%rbp)
leaq (%rax,%r15,4), %r12
movq %rbx, %rdi
callq 0x10b0
testl %eax, %eax
jne 0x13ce
callq 0x1851
movq %r12, %rcx
cmpq $0xc, %r15
jb 0x138a
je 0x13b2
movq -0x30(%rbp), %rcx
leaq (%rcx,%r15,4), %rcx
addq $-0x34, %rcx
movl %eax, (%rcx)
movl %eax, %edi
callq 0x183b
movl $0x400, %edx            # imm = 0x400
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rcx
callq 0x1040
addl %eax, %r13d
incq %r15
addq $0x4, %r12
jmp 0x1362
movq -0x40(%rbp), %rcx
movl %eax, -0x28(%rcx)
movl %eax, %edi
callq 0x1ae8
movq %rax, -0x30(%rbp)
pushq $0xd
popq %r15
movq -0x48(%rbp), %rax
jmp 0x135e
movq %rbx, %rdi
callq 0x1050
movq -0x40(%rbp), %rsi
movl %r13d, -0x7c(%rsi)
addl $0x3ff, %r13d           # imm = 0x3FF
movl $0x400, %ecx            # imm = 0x400
movl %r13d, %eax
cltd
idivl %ecx
leal (%rax,%rax), %ecx
cmpq $0x0, -0x30(%rbp)
leal 0x2(%rax,%rax), %eax
cmovel %ecx, %eax
movl %eax, -0x64(%rsi)
pushq $0x1
popq %rdx
leaq -0x250(%rbp), %rcx
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x1a7f
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2bc1(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc16(%rip), %rdi       # 0x2057
pushq $0x1a
jmp 0x1458
movq 0x2bac(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc1c(%rip), %rdi       # 0x2072
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x1120
jmp 0x141b
movq 0x2b8e(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc15(%rip), %rdi       # 0x2089
pushq $0x25
jmp 0x148b
movq 0x2b79(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc26(%rip), %rdi       # 0x20af
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x1120
pushq $0x11
popq %r14
jmp 0x141b
movq 0x2b57(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb59(%rip), %rdi       # 0x2004
pushq $0x4b
popq %rsi
pushq $0x1
popq %rdx
callq 0x1120
jmp 0x14c4
leaq 0xb91(%rip), %rdi       # 0x2050
callq 0x10f0
pushq $0x1
popq %rdi
callq 0x1110