// File: /workspace/github_c_makefile_star2_O0/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
subq $0x260, %rsp            # imm = 0x260
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x4, -0x8(%rbp)
je 0x1271
movq 0x3da2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1da4(%rip), %rsi      # 0x3004
movb $0x0, %al
callq 0x10b0
movl $0x1, %edi
callq 0x1120
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10e0
movl %eax, -0x244(%rbp)
movl -0x244(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x20000, %esi          # imm = 0x20000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x1070
movq %rax, 0x3dfb(%rip)      # 0x50b0
movq $-0x1, %rax
cmpq %rax, 0x3ded(%rip)      # 0x50b0
jne 0x12db
leaq 0x1d84(%rip), %rdi      # 0x3050
callq 0x1100
movl $0x1, %edi
callq 0x1120
callq 0x18f0
movq %rax, -0x230(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x1d5f(%rip), %rsi      # 0x3055
callq 0x10f0
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x1329
movq 0x3cec(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d41(%rip), %rsi      # 0x3057
movb $0x0, %al
callq 0x10b0
movl $0x2, -0x4(%rbp)
jmp 0x1637
leaq -0x120(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x1030
movsbl -0x120(%rbp), %eax
cmpl $0x2f, %eax
je 0x136d
movq 0x3ca8(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d18(%rip), %rsi      # 0x3072
movb $0x0, %al
callq 0x10b0
movl $0x2, -0x4(%rbp)
jmp 0x1637
movsbl -0x11f(%rbp), %eax
cmpl $0x0, %eax
jne 0x139d
movq 0x3c78(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1cff(%rip), %rsi      # 0x3089
movb $0x0, %al
callq 0x10b0
movl $0x11, -0x4(%rbp)
jmp 0x1637
leaq -0x120(%rbp), %rdi
callq 0x2290
jmp 0x13ab
leaq -0x120(%rbp), %rdi
callq 0x2100
movl %eax, -0x224(%rbp)
cmpl $0x0, -0x224(%rbp)
jne 0x13ea
movq 0x3c2b(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1c80(%rip), %rsi      # 0x3057
movb $0x0, %al
callq 0x10b0
movl $0x2, -0x4(%rbp)
jmp 0x1637
movq -0x230(%rbp), %rax
movl -0x224(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x238(%rbp)
leaq -0x220(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x2230
movq -0x238(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0x1c50
movl %eax, -0x228(%rbp)
cmpl $0x0, -0x228(%rbp)
je 0x1465
movq 0x3bb0(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1c5d(%rip), %rsi      # 0x30af
movb $0x0, %al
callq 0x10b0
movl $0x11, -0x4(%rbp)
jmp 0x1637
movl $0x8000, %edi           # imm = 0x8000
callq 0x1950
movl %eax, -0x228(%rbp)
movq -0x230(%rbp), %rax
movl -0x228(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x240(%rbp)
movq $0x0, -0x250(%rbp)
movl $0x0, -0x25c(%rbp)
movl $0x0, -0x260(%rbp)
movq -0x18(%rbp), %rdi
callq 0x10c0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x14cf
jmp 0x15af
callq 0x1c00
movl %eax, -0x254(%rbp)
cmpl $0xc, -0x260(%rbp)
jge 0x14fd
movl -0x254(%rbp), %edx
movq -0x240(%rbp), %rax
movslq -0x260(%rbp), %rcx
movl %edx, 0x28(%rax,%rcx,4)
jmp 0x1562
cmpl $0xc, -0x260(%rbp)
jne 0x1544
movl -0x254(%rbp), %edx
movq -0x240(%rbp), %rax
movslq -0x260(%rbp), %rcx
movl %edx, 0x28(%rax,%rcx,4)
movl -0x254(%rbp), %edi
callq 0x20b0
movq %rax, -0x250(%rbp)
movl -0x260(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x260(%rbp)
jmp 0x14b5
movl -0x254(%rbp), %edx
movq -0x250(%rbp), %rax
movl -0x260(%rbp), %ecx
subl $0xd, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1562
movl -0x254(%rbp), %edi
callq 0x1be0
movq %rax, %rdi
movq -0x18(%rbp), %rcx
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1040
movl %eax, -0x258(%rbp)
movl -0x258(%rbp), %eax
addl -0x25c(%rbp), %eax
movl %eax, -0x25c(%rbp)
movl -0x260(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x260(%rbp)
jmp 0x14b5
movq -0x18(%rbp), %rdi
callq 0x1050
movl -0x25c(%rbp), %ecx
movq -0x240(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpq $0x0, -0x250(%rbp)
jne 0x15f5
movl -0x25c(%rbp), %eax
addl $0x3ff, %eax            # imm = 0x3FF
movl $0x400, %ecx            # imm = 0x400
cltd
idivl %ecx
movl %eax, %ecx
shll %ecx
movq -0x240(%rbp), %rax
movl %ecx, 0x1c(%rax)
jmp 0x1619
movl -0x25c(%rbp), %eax
addl $0x3ff, %eax            # imm = 0x3FF
movl $0x400, %ecx            # imm = 0x400
cltd
idivl %ecx
movl %eax, %ecx
addl $0x1, %ecx
shll %ecx
movq -0x240(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x238(%rbp), %rdi
movl -0x228(%rbp), %esi
leaq -0x220(%rbp), %rcx
movl $0x1, %edx
callq 0x2020
movl -0x4(%rbp), %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)