// File: /workspace/github_c_makefile_star2_O2/kevinp89[P]Operating-System-EXT2-file-system-with-commands/ext2_cp.c
// Function: main
int main(int argc, char** argv){
	//arguments check
	if(argc != 4){
		fprintf(stderr, "Usage: %s [disk] [os path] [virtual disk path]\n", argv[0]);
		exit(1);
	}
	//opening the disk
	int fd = open(argv[1], O_RDWR);
	if(!fd){
		fprintf(stderr, "Disk image '%s' not found\n", argv[1]);
		exit(1);	
	}
    //check virtual path is absolute or not.
    int check = check_path(argv[3]);
    if(check == 1){
        fprintf(stderr, "Please provide absolute path for virtual path");
        exit(1);
    }
	//open the file from this os
	int osfd = open(argv[2], O_RDONLY);
	if(osfd == -1){
		fprintf(stderr, "os path: '%s' invalid\n", argv[2]);
		exit(ENOENT);
	}
	// mmap the disk
	disk = mmap(NULL, 128*1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (disk == MAP_FAILED) {
		perror("mmap");
		exit(1);
	}
	// Grabbing super block and block descriptor
	sb = (struct ext2_super_block *)(disk + 1024);
	desc = (struct ext2_group_desc *)(disk + 2*1024);
	
	//get the file size
	int file_size = lseek(osfd, 0, SEEK_END);

	//calculate number of blocks needed to store file
	int blocks_needed = (file_size - 1) / EXT2_BLOCK_SIZE + 1;
	
	//check if there is space in disk img
	if(blocks_needed > sb->s_free_blocks_count){
		fprintf(stderr, "Not enough space in disk img\n");
		exit(1);
	}
	
	
	//get the bitmaps and inode table
	//int inode_bitmap = desc-> bg_inode_bitmap;
	unsigned char *block_bitmap = (unsigned char *) (disk+1024 * desc -> bg_block_bitmap);
	//int inode_table =  desc -> bg_inode_table;

	//copy osfilename into a variable
	char *dup = strdup(argv[2]);
	// Creating array and copying
	int c;
	int count=0;
	for (c = 0; dup[c] != 0; c++) {
		if (dup[c] == '/' && dup[c+1] != '\0') {
			count++;
		}
	}
	if(count!=0) {

		char *dirs[count];
		int cp;
		for (cp = 0; cp < count; cp++) {
			dirs[cp] = strsep(&dup, "/");
		}

		dup = dirs[cp - 1];
	}

	//we need the parent directory where file should belong
	int parent_index;
	//we need a temp variable to hold the virtual disk path
	char *virtual_path = strdup(argv[3]);
	//if it ends in / then append in the os file name
	if(argv[3][strlen(argv[3]) - 1] == '/'){
		//copy path into temp variable
		strcat(virtual_path, dup);
        //get the parent index
		parent_index = check_parent(disk, virtual_path);
		if(parent_index == -1){
			fprintf(stderr, "'%s' No such file or directory\n", argv[3]);
			exit(1);
		}
	} else {
        //doesnt end in / so check the parent (it could be a dir could be a new name
		parent_index = check_parent(disk, argv[3]);

		if(parent_index == -1){
			fprintf(stderr, "'%s' No such file or directory\n", argv[3]);
			exit(1);
		}
	}

	//get the parent node
	struct ext2_inode *parent_node = (struct ext2_inode *) ((disk+EXT2_BLOCK_SIZE * desc->bg_inode_table) +
            (sb->s_inode_size * parent_index));

	//take out the last / and get the file name (could be a dir)
	if (virtual_path[strlen(virtual_path) -1] == '/') virtual_path[strlen(virtual_path)-1] = '\0';
	
	/**get the new file name */
    //get the length of the name
	int i;
	int file_len1 = 0;
	for (i = strlen(virtual_path); virtual_path[i] != '/'; i--) {
		file_len1++;
		if (file_len1 > 255) {
			fprintf(stderr, "File name too large.\n");
			exit(1);
		}
	}
	
	//copy the new file name
	char file_name[file_len1];
	strcpy(file_name, virtual_path + i + 1);

	//check if last name is a directory
	int new_parent_index;
	new_parent_index = search_directories(disk, parent_node, file_name, 1);

	//if its a directory, thats the new parent, otherwise file_name is our new file name
	if(new_parent_index != -1){
		parent_node = (struct ext2_inode *) ((disk+EXT2_BLOCK_SIZE * desc->bg_inode_table) + (sb->s_inode_size *
				new_parent_index));
	}

	//if last name is a file and already exists, throw an err
	if(search_directories(disk, parent_node, file_name, 0) != -1){
		fprintf(stderr, "File name '%s' already exists", file_name);
		exit(1);	
	}


	//last name is either our new parent or a new file. all is set
	
	// Finding a free inode and allocating (set to 1)
	unsigned char *imap = (unsigned char *)(disk + 1024 * desc->bg_inode_bitmap);
	int inode;
	for (inode = 11; inode < sb->s_inodes_count; inode++) {
		if (check_node(inode, imap) == 0) {
			break;
		}
	}
	if (inode == sb->s_inodes_count) { // Maximum reached, no more inodes
		fprintf(stderr, "No more inodes available.");
		exit(1);
	}
	
	//new inode init
	struct ext2_inode *new_inode = (struct ext2_inode *)((disk + 1024 * desc->bg_inode_table)
														 + (sb->s_inode_size * (inode)));
	
	//set up the inode
	new_inode->i_mode = EXT2_S_IFREG; //file flag
	new_inode->i_size = file_size;
    new_inode->i_ctime = (unsigned int) time(0);
    new_inode->i_dtime = 0;
	new_inode->i_blocks = 1;
	new_inode->i_links_count = 1;
	sb->s_free_inodes_count--;
	desc->bg_free_inodes_count--;

	//map the src file
	unsigned char *source = mmap(NULL, file_size, PROT_READ| PROT_WRITE,
								 MAP_PRIVATE, osfd, 0);

	void *db; //block where data of the file belongs.
    void *indirect_block_ptr;
	unsigned int indir_data_blocks[15];
	//need to find free blocks for the file and read into them
	int block_indx;
    int copied = 0; //tracker for how much copied
    int size_remain = file_size; // tracker for how much remains
	for(block_indx=0; block_indx<blocks_needed; block_indx++){
		//if indirect blocks aren't needed
		if(block_indx<12){
			//find a free block
			int free_block = find_free_block(block_bitmap);
			if (free_block == -1) {
				fprintf(stderr, "No more blocks available.");
				exit(1);
			}
			//update superblock and new_inode
			adjust_free_blocks(-1, sb, desc);
			new_inode->i_blocks++;
			new_inode->i_block[block_indx] = (sb->s_first_data_block) + free_block-1;
			//update data block
			db = (void*)(disk + EXT2_BLOCK_SIZE * new_inode->i_block[block_indx]);
		}
		
		//if indirect blocks are needed
		//need to setup an indirect block
		else if(block_indx>=12){
			int free_block = find_free_block(block_bitmap);
			if(free_block == -1){
				fprintf(stderr, "No more blocks available.");
				exit(1);
			}
			
			//initialize an indirect block if i==12, since i_blocks[12] is the pointer to the indirect block
			if(i==12){
				//find space for this indirect block to set up a pointer to blocks
 				int indir_block_indx = find_free_block(block_bitmap);
				if(indir_block_indx == -1){
					fprintf(stderr, "No more blocks available.");
					exit(1);
				}
				new_inode->i_block[block_indx] = (sb->s_first_data_block) + indir_block_indx;
				indirect_block_ptr = (void*)(disk + EXT2_BLOCK_SIZE * new_inode->i_block[block_indx]);	
				indir_data_blocks[block_indx-12]  = (sb->s_first_data_block) + free_block - 1;
				memcpy(indirect_block_ptr, (void*)indirect_block_ptr, 16);
				
			}
			
			//update the new_node
			new_inode->i_block[block_indx] = (sb->s_first_data_block) + free_block;
			//update superblock
			adjust_free_blocks(-1, sb, desc);
			//update the data block
			db = (void*)(disk + EXT2_BLOCK_SIZE * indir_data_blocks[block_indx-12]);
		}
        if(size_remain < EXT2_BLOCK_SIZE) {
            memcpy(db, source + copied, size_remain);
            size_remain = 0;
            break;
        } else {
            memcpy(db, source + copied, EXT2_BLOCK_SIZE);
            size_remain -= EXT2_BLOCK_SIZE;
            copied += EXT2_BLOCK_SIZE;
        }

	}
	// set the new inode in the imap
	set_node(inode, imap);
	// set the block sectors occupied
	new_inode->i_blocks = (unsigned int)((new_inode->i_size / 512) + 1);
	/* update parent directory */
	
	int par_block_index;
	int k;
	for(k=0; parent_node->i_block[k] != 0; k++){
		par_block_index = parent_node -> i_block[k];	
	}

	struct ext2_dir_entry *curdir = (struct ext2_dir_entry *)(disk + (1024 * par_block_index));
	int total_rec_len;
	//make curdir the last dir
	for(total_rec_len=0; total_rec_len + curdir->rec_len < 1024;){
		total_rec_len += curdir->rec_len;
		curdir = (struct ext2_dir_entry *) (disk + (1024 * par_block_index) + total_rec_len);
	}
	
	//check theres enough space to place the new dir entry
	struct ext2_dir_entry *newdir;
	int remaining_rec_len = sizeof(curdir) + curdir->name_len; // Actual size of curdir
	remaining_rec_len += 4 - (remaining_rec_len % 4); // Add padding
	remaining_rec_len = 1024 - remaining_rec_len - total_rec_len; // Remaining space
	if (remaining_rec_len < (8 + strlen(file_name))) { // Not enough space
		// Allocate new block
		int newblock;
		for (newblock = 22; newblock < sb->s_blocks_count; newblock++) {
			if (check_node(newblock, block_bitmap) == 0) {
				break;
			}
		}
		if (newblock == sb->s_blocks_count) {
			fprintf(stderr, "No more blocks available.");
			exit(1);
		}
		set_node(newblock, block_bitmap);
		sb->s_free_blocks_count--;
		parent_node->i_block[i] = newblock; // i should still hold the first block space unused by parent
		parent_node->i_blocks += 2; // One more block being added
		newdir = (struct ext2_dir_entry *)(disk + (1024 * newblock));
		newdir->rec_len = 1024; // Takes up the whole of the new block.
	} else { // Enough space
		// Change curdir, add new directory
		curdir->rec_len = sizeof(curdir) + curdir->name_len;
		curdir->rec_len += 4 - (curdir->rec_len % 4);
		total_rec_len += curdir->rec_len;
		newdir = (struct ext2_dir_entry *)(disk + (1024 * par_block_index) + total_rec_len);
		newdir->rec_len = 1024 - total_rec_len; // Takes up the rest of the block.
	}
	newdir->inode = inode + 1;
	newdir->name_len = strlen(file_name);
	newdir->file_type = EXT2_FT_REG_FILE;
	strncpy(newdir->name, file_name, newdir->name_len);

	free(dup); //free dup variable
	free(virtual_path); // free the virtual path
	close(fd);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %r13
cmpl $0x4, %edi
jne 0x1d2a
movq 0x8(%r13), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x1110
testl %eax, %eax
je 0x1d44
movl %eax, %ebx
movq 0x18(%r13), %rax
cmpb $0x2f, (%rax)
jne 0x1d5e
movq 0x10(%r13), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1110
cmpl $-0x1, %eax
je 0x1d73
movl %eax, %r14d
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2a55(%rip)      # 0x40d8
cmpq $-0x1, %rax
je 0x1d93
leaq 0x400(%rax), %rcx
movq %rcx, 0x2a35(%rip)      # 0x40d0
addq $0x800, %rax            # imm = 0x800
movq %rax, 0x2a38(%rip)      # 0x40e0
xorl %r12d, %r12d
pushq $0x2
popq %rdx
movl %r14d, %edi
xorl %esi, %esi
callq 0x1080
movq %rax, -0x80(%rbp)
decl %eax
movl $0x400, %ecx            # imm = 0x400
cltd
idivl %ecx
movl %eax, %r15d
incl %r15d
movq 0x29fd(%rip), %rax      # 0x40d0
cmpl 0xc(%rax), %r15d
ja 0x1da1
movq 0x29f4(%rip), %rax      # 0x40d8
movq %rax, -0x40(%rbp)
movq 0x29f1(%rip), %rax      # 0x40e0
movl (%rax), %eax
shll $0xa, %eax
movq %rax, -0x30(%rbp)
movq 0x10(%r13), %rdi
callq 0x1160
movq %rax, -0x68(%rbp)
incq %rax
movzbl -0x1(%rax), %ecx
cmpl $0x2f, %ecx
je 0x1717
testl %ecx, %ecx
jne 0x1705
jmp 0x1720
cmpb $0x1, (%rax)
sbbl $-0x1, %r12d
jmp 0x1705
movl %r15d, -0x6c(%rbp)
movl %r14d, -0x38(%rbp)
movl %ebx, -0x44(%rbp)
testl %r12d, %r12d
je 0x178e
movq %r13, -0x60(%rbp)
movq %rsp, -0x58(%rbp)
movl %r12d, %eax
movq %rsp, %r14
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
xorl %ebx, %ebx
testl %r12d, %r12d
cmovlel %ebx, %r12d
leaq 0x8b8(%rip), %r15       # 0x2018
leaq -0x68(%rbp), %r13
cmpq %rbx, %r12
je 0x177d
movq %r13, %rdi
movq %r15, %rsi
callq 0x1100
movq %rax, (%r14,%rbx,8)
incq %rbx
jmp 0x1764
movq -0x8(%r14,%r12,8), %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rsp
movq -0x60(%rbp), %r13
movq 0x18(%r13), %r15
movq %r15, %rdi
callq 0x1160
movq %rax, %r14
movq %r15, %rdi
callq 0x1060
cmpb $0x2f, -0x1(%r15,%rax)
jne 0x17c5
movq -0x68(%rbp), %rsi
movq %r14, %rdi
callq 0x1130
movq 0x2918(%rip), %rdi      # 0x40d8
movq %r14, %rsi
jmp 0x17cf
movq 0x290c(%rip), %rdi      # 0x40d8
movq %r15, %rsi
callq 0x13d9
cmpl $-0x1, %eax
je 0x1db9
movq 0x28fc(%rip), %rcx      # 0x40e0
movl 0x8(%rcx), %r15d
shll $0xa, %r15d
addq 0x28e5(%rip), %r15      # 0x40d8
movq 0x28d6(%rip), %rcx      # 0x40d0
movzwl 0x58(%rcx), %ecx
movslq %eax, %rbx
imulq %rcx, %rbx
movq %r14, %rdi
callq 0x1060
cmpb $0x2f, -0x1(%r14,%rax)
jne 0x181b
movb $0x0, -0x1(%r14,%rax)
movq -0x30(%rbp), %rax
addq %rax, -0x40(%rbp)
addq %rbx, %r15
movq %r14, %rdi
callq 0x1060
movslq %eax, %rdx
movq %r14, %rax
movq %rdx, -0x90(%rbp)
addq %rdx, %rax
xorl %r13d, %r13d
cmpb $0x2f, (%rax,%r13)
je 0x185a
cmpl $0xffffff01, %r13d      # imm = 0xFFFFFF01
je 0x1cf1
decq %r13
jmp 0x1841
movq %rsp, -0xa8(%rbp)
pushq $0xf
popq %rcx
subq %r13, %rcx
andq $-0x10, %rcx
movq %rsp, %r12
subq %rcx, %r12
movq %r12, %rsp
leaq (%rax,%r13), %rsi
incq %rsi
movq %r12, %rdi
callq 0x1050
movq %r15, %rbx
movq 0x284b(%rip), %r15      # 0x40d8
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, -0x50(%rbp)
movq %r12, %rdx
callq 0x1308
cmpl $-0x1, %eax
je 0x18cc
movq 0x2832(%rip), %rcx      # 0x40e0
movl 0x8(%rcx), %ecx
shll $0xa, %ecx
addq %r15, %rcx
movq 0x2812(%rip), %rdx      # 0x40d0
movzwl 0x58(%rdx), %edx
movslq %eax, %rbx
imulq %rdx, %rbx
addq %rcx, %rbx
movq %r15, %rdi
movq %rbx, -0x88(%rbp)
movq %rbx, %rsi
movq -0x50(%rbp), %rdx
xorl %ecx, %ecx
callq 0x1308
cmpl $-0x1, %eax
jne 0x1dd0
pushq $0xc
popq %rax
subq -0x90(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq 0x27db(%rip), %rax      # 0x40e0
movl 0x4(%rax), %r10d
shll $0xa, %r10d
addq %r15, %r10
movq 0x27b9(%rip), %rcx      # 0x40d0
movl (%rcx), %edx
cmpl $0xc, %edx
pushq $0xb
popq %rsi
movl %esi, %r9d
cmovael %edx, %r9d
cmpl %edx, %esi
jae 0x1948
movl %esi, %edi
shrl $0x3, %edi
movzbl (%r10,%rdi), %edi
movl %esi, %r8d
andl $0x7, %r8d
btl %r8d, %edi
jae 0x1945
incl %esi
jmp 0x1926
movl %esi, %r9d
movq %r10, -0xa0(%rbp)
movq %r14, -0x78(%rbp)
cmpl %edx, %r9d
je 0x1df1
movl 0x8(%rax), %eax
shll $0xa, %eax
addq %rax, %r15
movzwl 0x58(%rcx), %eax
movl %r9d, -0x48(%rbp)
movl %r9d, %ecx
imulq %rax, %rcx
leaq (%r15,%rcx), %rax
movq %rax, -0x58(%rbp)
leaq (%rcx,%r15), %r14
addq $0x28, %r14
movw $0x8000, -0x28(%r14)    # imm = 0x8000
movq -0x80(%rbp), %r15
movl %r15d, -0x24(%r14)
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x10e0
movl %eax, -0x1c(%r14)
andl $0x0, -0x14(%r14)
movl $0x1, -0xc(%r14)
movq %r14, -0x98(%rbp)
movw $0x1, -0xe(%r14)
movq 0x270e(%rip), %rax      # 0x40d0
decl 0x10(%rax)
movq 0x2714(%rip), %rax      # 0x40e0
decw 0xe(%rax)
movslq %r15d, %rsi
pushq $0x3
popq %rdx
pushq $0x2
popq %rcx
xorl %edi, %edi
movl -0x38(%rbp), %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, -0x60(%rbp)
movl -0x6c(%rbp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
leaq -0x120(%rbp), %rax
movq %rax, -0x30(%rbp)
shlq $0xa, %rcx
movq %rcx, -0x38(%rbp)
xorl %r14d, %r14d
cmpq %rbx, -0x38(%rbp)
je 0x1b2a
movq -0x40(%rbp), %rdi
callq 0x15b6
movl %eax, %r12d
cmpq $0xb, %r14
ja 0x1a60
cmpl $-0x1, %r12d
je 0x1d06
movq 0x2695(%rip), %rax      # 0x40d0
movq 0x269e(%rip), %rcx      # 0x40e0
decl 0xc(%rax)
decw 0xc(%rcx)
movq -0x58(%rbp), %rcx
incl 0x1c(%rcx)
movl 0x14(%rax), %eax
leal (%r12,%rax), %edi
decl %edi
movl %edi, 0x28(%rcx,%r14,4)
jmp 0x1ad7
cmpl $-0x1, %r12d
je 0x1d06
cmpq %r13, -0xb0(%rbp)
jne 0x1aab
movq -0x40(%rbp), %rdi
callq 0x15b6
cmpl $-0x1, %eax
je 0x1d06
movq 0x2644(%rip), %rcx      # 0x40d0
addl 0x14(%rcx), %eax
movq -0x98(%rbp), %rdx
movl %eax, (%rdx,%r14,4)
movl 0x14(%rcx), %eax
leal (%r12,%rax), %edi
decl %edi
movq -0x30(%rbp), %rsi
movl %edi, (%rsi)
jmp 0x1ac2
movq 0x261e(%rip), %rcx      # 0x40d0
movl 0x14(%rcx), %eax
movq -0x30(%rbp), %rdx
movl (%rdx), %edi
movq -0x98(%rbp), %rdx
addl %r12d, %eax
movl %eax, (%rdx,%r14,4)
movq 0x2610(%rip), %rax      # 0x40e0
decl 0xc(%rcx)
decw 0xc(%rax)
shll $0xa, %edi
addq 0x25f7(%rip), %rdi      # 0x40d8
cmpl $0x3ff, %r15d           # imm = 0x3FF
jle 0x1b17
movq -0x60(%rbp), %rax
leaq (%rax,%rbx), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x10d0
addl $0xfffffc00, %r15d      # imm = 0xFFFFFC00
addq $0x400, %rbx            # imm = 0x400
incq %r14
addq $0x4, -0x30(%rbp)
jmp 0x1a0e
movq -0x60(%rbp), %rsi
addq %rbx, %rsi
movslq -0x80(%rbp), %rdx
subq %rbx, %rdx
callq 0x10d0
movl -0x48(%rbp), %edx
movl %edx, %ecx
andb $0x7, %cl
pushq $0x1
popq %rax
shll %cl, %eax
movl %edx, %ecx
shrl $0x3, %ecx
movq -0xa0(%rbp), %rdx
xorb %al, (%rdx,%rcx)
movq -0x58(%rbp), %rcx
movl 0x4(%rcx), %eax
shrl $0x9, %eax
incl %eax
movl %eax, 0x1c(%rcx)
movq -0x88(%rbp), %rax
leaq 0x28(%rax), %rcx
movq %rcx, -0x38(%rbp)
movl %edx, %eax
movl (%rcx), %edx
addq $0x4, %rcx
testl %edx, %edx
jne 0x1b64
shll $0xa, %eax
movslq %eax, %r12
addq 0x255b(%rip), %r12      # 0x40d8
xorl %r14d, %r14d
movq %r12, %r15
movzwl 0x4(%r15), %eax
addl %r14d, %eax
cmpl $0x3ff, %eax            # imm = 0x3FF
ja 0x1b9d
movl %eax, %r15d
addq %r12, %r15
movl %eax, %r14d
jmp 0x1b83
movzbl 0x6(%r15), %ebx
addq $0x8, %rbx
movl %ebx, %eax
andl $-0x4, %eax
addl %r14d, %eax
movl $0x3fc, %ecx            # imm = 0x3FC
subl %eax, %ecx
movslq %ecx, %rax
movq %rax, -0x30(%rbp)
movq -0x50(%rbp), %rdi
callq 0x1060
addq $0x8, %rax
cmpq -0x30(%rbp), %rax
jbe 0x1c13
movq 0x24fa(%rip), %rax      # 0x40d0
movl 0x4(%rax), %eax
cmpl $0x17, %eax
pushq $0x16
popq %rcx
movl %ecx, %r12d
cmovael %eax, %r12d
movl -0x44(%rbp), %ebx
movq -0x78(%rbp), %r14
movq -0x88(%rbp), %rdi
cmpl %eax, %ecx
jae 0x1c38
movl %ecx, %edx
shrl $0x3, %edx
movq -0x40(%rbp), %rsi
movzbl (%rsi,%rdx), %edx
movl %ecx, %esi
andl $0x7, %esi
btl %esi, %edx
jae 0x1c35
incl %ecx
jmp 0x1bf4
andl $-0x4, %ebx
addl $0x4, %ebx
movw %bx, 0x4(%r15)
addl %ebx, %r14d
addq %r14, %r12
movl $0x400, %eax            # imm = 0x400
subl %r14d, %eax
movl -0x44(%rbp), %ebx
movq -0x78(%rbp), %r14
jmp 0x1c89
movl %ecx, %r12d
cmpl %eax, %r12d
je 0x1d06
movl %r12d, %ecx
andb $0x7, %cl
pushq $0x1
popq %rdx
shll %cl, %edx
movl %r12d, %eax
shrl $0x3, %eax
movq -0x40(%rbp), %rcx
xorb %dl, (%rcx,%rax)
movq 0x2470(%rip), %rax      # 0x40d0
decl 0xc(%rax)
movq -0x90(%rbp), %rax
movq -0x38(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movl %r12d, (%rax,%r13,4)
addl $0x2, 0x1c(%rdi)
shll $0xa, %r12d
addq 0x2453(%rip), %r12      # 0x40d8
movw $0x400, %ax             # imm = 0x400
movw %ax, 0x4(%r12)
movl -0x48(%rbp), %eax
incl %eax
movl %eax, (%r12)
movq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x1060
movb %al, 0x6(%r12)
movb $0x1, 0x7(%r12)
addq $0x8, %r12
movzbl %al, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x1040
movq -0x68(%rbp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movl %ebx, %edi
callq 0x10a0
movq -0xa8(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2300(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x3eb(%rip), %rdi       # 0x20ed
pushq $0x15
jmp 0x1d19
movq 0x22eb(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x424(%rip), %rdi       # 0x213b
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x1150
pushq $0x1
popq %rdi
callq 0x1140
movq 0x22c7(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0x2db(%rip), %rsi       # 0x201a
jmp 0x1de5
movq 0x22ad(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x8(%r13), %rdx
leaq 0x2f1(%rip), %rsi       # 0x204a
jmp 0x1de5
movq 0x2293(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x2f6(%rip), %rdi       # 0x2065
pushq $0x2d
jmp 0x1d19
movq 0x227e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x10(%r13), %rdx
leaq 0x30b(%rip), %rsi       # 0x2093
xorl %eax, %eax
callq 0x10c0
pushq $0x2
jmp 0x1d24
leaq 0x310(%rip), %rdi       # 0x20aa
callq 0x1120
jmp 0x1d22
movq 0x2250(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x2fd(%rip), %rdi       # 0x20af
pushq $0x1d
jmp 0x1d19
movq 0x2238(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x18(%r13), %rdx
leaq 0x2ff(%rip), %rsi       # 0x20cd
jmp 0x1de5
movq 0x2221(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x322(%rip), %rsi       # 0x2103
movq -0x50(%rbp), %rdx
xorl %eax, %eax
callq 0x10c0
jmp 0x1d22
movq 0x2200(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x31f(%rip), %rdi       # 0x2121
jmp 0x1d17