// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movq %rsi, %r13
cmpl $0x4, %edi
jne 0x1f91
movq 0x8(%r13), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x1110
testl %eax, %eax
je 0x1f99
movl %eax, %ebx
movq 0x18(%r13), %rax
cmpb $0x2f, (%rax)
jne 0x1fa5
leaq 0x10(%r13), %r15
movq 0x10(%r13), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1110
cmpl $-0x1, %eax
je 0x1faa
movl %eax, %r12d
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x27f4(%rip)      # 0x40d8
cmpq $-0x1, %rax
je 0x1fb2
leaq 0x400(%rax), %rcx
movq %rcx, 0x27d4(%rip)      # 0x40d0
addq $0x800, %rax            # imm = 0x800
movq %rax, 0x27d7(%rip)      # 0x40e0
xorl %r14d, %r14d
movl %r12d, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1080
leal -0x1(%rax), %edx
movq %rax, -0x48(%rbp)
addl $0x3fe, %eax            # imm = 0x3FE
testl %edx, %edx
cmovnsl %edx, %eax
sarl $0xa, %eax
movq %rax, -0x38(%rbp)
incl %eax
movq 0x2794(%rip), %rcx      # 0x40d0
cmpl 0xc(%rcx), %eax
ja 0x1fb7
addq $0x18, %r13
movq 0x2788(%rip), %rax      # 0x40d8
movq %rax, -0x40(%rbp)
movq 0x2785(%rip), %rax      # 0x40e0
movl (%rax), %eax
shll $0xa, %eax
movq %rax, -0x58(%rbp)
movq (%r15), %rdi
callq 0x1160
movq %rax, -0x68(%rbp)
incq %rax
movzbl -0x1(%rax), %ecx
cmpl $0x2f, %ecx
je 0x1982
testl %ecx, %ecx
jne 0x1970
jmp 0x198b
cmpb $0x1, (%rax)
sbbl $-0x1, %r14d
jmp 0x1970
movl %r12d, -0x60(%rbp)
movl %ebx, -0x74(%rbp)
testl %r14d, %r14d
je 0x19f8
movq %r13, -0x30(%rbp)
movq %rsp, -0x70(%rbp)
movl %r14d, %ebx
movq %rsp, %r13
leaq 0xf(,%rbx,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
testl %r14d, %r14d
jle 0x19e4
xorl %r14d, %r14d
leaq 0x652(%rip), %r15       # 0x2018
leaq -0x68(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x1100
movq %rax, (%r13,%r14,8)
incq %r14
cmpq %r14, %rbx
jne 0x19ca
jmp 0x19e7
xorl %r14d, %r14d
movq -0x8(%r13,%r14,8), %rax
movq %rax, -0x68(%rbp)
movq -0x70(%rbp), %rsp
movq -0x30(%rbp), %r13
movq (%r13), %r12
movq %r12, %rdi
callq 0x1160
movq %rax, %r15
movq %r12, %rdi
callq 0x1060
cmpb $0x2f, -0x1(%r12,%rax)
jne 0x1a3f
movq -0x68(%rbp), %rsi
movq %r15, %rdi
callq 0x1130
movq 0x26ae(%rip), %rdi      # 0x40d8
movq %r15, %rsi
callq 0x164a
cmpl $-0x1, %eax
jne 0x1a57
movq %r13, %rdi
callq 0x131a
movq 0x2692(%rip), %rdi      # 0x40d8
movq %r12, %rsi
callq 0x164a
cmpl $-0x1, %eax
je 0x1fca
movl %eax, %r13d
movq 0x267f(%rip), %rax      # 0x40e0
movl 0x8(%rax), %r14d
shll $0xa, %r14d
addq 0x2668(%rip), %r14      # 0x40d8
movq 0x2659(%rip), %rax      # 0x40d0
movzwl 0x58(%rax), %ebx
movq %r15, %rdi
callq 0x1060
cmpb $0x2f, -0x1(%r15,%rax)
jne 0x1a91
movb $0x0, -0x1(%r15,%rax)
movq %r15, %rdi
callq 0x1060
movq %rax, %r12
movslq %r12d, %rdx
cmpb $0x2f, (%r15,%rdx)
jne 0x1aaa
xorl %eax, %eax
jmp 0x1acf
movslq %r12d, %rcx
xorl %eax, %eax
cmpl $0xff, %eax
je 0x1f7d
leaq -0x1(%rcx), %rdx
incq %rax
cmpb $0x2f, -0x1(%r15,%rcx)
movq %rdx, %rcx
movq %rdx, %r12
jne 0x1aaf
movq %rsp, -0x88(%rbp)
movslq %r13d, %rcx
imulq %rbx, %rcx
addq %rcx, %r14
movq %rsp, %r13
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movq %rdx, -0x98(%rbp)
leaq (%r15,%rdx), %rsi
incq %rsi
movq %r13, %rdi
callq 0x1050
movq 0x25ca(%rip), %rbx      # 0x40d8
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, -0x50(%rbp)
movq %r13, %rdx
movl $0x1, %ecx
callq 0x1562
cmpl $-0x1, %eax
je 0x1b4f
movq 0x25af(%rip), %rcx      # 0x40e0
movl 0x8(%rcx), %ecx
shll $0xa, %ecx
addq %rbx, %rcx
movq 0x258f(%rip), %rdx      # 0x40d0
movzwl 0x58(%rdx), %edx
movslq %eax, %r14
imulq %rdx, %r14
addq %rcx, %r14
movq %rbx, %rdi
movq %r14, -0x90(%rbp)
movq %r14, %rsi
movq -0x50(%rbp), %rdx
xorl %ecx, %ecx
callq 0x1562
cmpl $-0x1, %eax
jne 0x1fbc
movq 0x2569(%rip), %rax      # 0x40e0
movl 0x4(%rax), %r8d
shll $0xa, %r8d
addq %rbx, %r8
movq 0x2547(%rip), %rcx      # 0x40d0
movl (%rcx), %edx
movl $0xb, %r14d
cmpl $0xc, %edx
jb 0x1bb7
movl %r14d, %esi
shrl $0x3, %esi
movzbl (%r8,%rsi), %esi
movl %r14d, %edi
andl $0x7, %edi
btl %edi, %esi
jae 0x1bb7
incl %r14d
cmpl %r14d, %edx
jne 0x1b96
movl %edx, %r14d
movq %r8, -0xa0(%rbp)
movq %r15, -0x80(%rbp)
cmpl %edx, %r14d
je 0x1fc5
movq -0x58(%rbp), %rdx
addq %rdx, -0x40(%rbp)
movl 0x8(%rax), %eax
shll $0xa, %eax
addq %rax, %rbx
movzwl 0x58(%rcx), %eax
movl %r14d, %r13d
imulq %rax, %r13
leaq (%rbx,%r13), %rax
movq %rax, -0x30(%rbp)
movw $0x8000, (%r13,%rbx)    # imm = 0x8000
movq -0x48(%rbp), %rax
movl %eax, 0x4(%r13,%rbx)
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x10e0
movl %eax, 0xc(%r13,%rbx)
movl $0x0, 0x14(%r13,%rbx)
movl $0x1, 0x1c(%r13,%rbx)
movw $0x1, 0x1a(%r13,%rbx)
movq 0x24a0(%rip), %rax      # 0x40d0
decl 0x10(%rax)
movq 0x24a6(%rip), %rax      # 0x40e0
decw 0xe(%rax)
movq -0x48(%rbp), %rbx
movslq %ebx, %rsi
xorl %edi, %edi
movl $0x3, %edx
movl $0x2, %ecx
movl -0x60(%rbp), %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, -0x60(%rbp)
cmpl $0xfffffc02, %ebx       # imm = 0xFFFFFC02
jl 0x1da3
movl %r12d, %eax
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rax
testl %eax, %eax
cmovlel %r15d, %eax
movq -0x30(%rbp), %rcx
addq $0x28, %rcx
movq %rcx, -0x58(%rbp)
incl %eax
movq %rax, -0x38(%rbp)
xorl %ebx, %ebx
movq -0x48(%rbp), %rax
movl %eax, %r13d
movq -0x40(%rbp), %rdi
callq 0x180e
movl %eax, %r12d
cmpq $0xb, %r15
ja 0x1ce1
cmpl $-0x1, %r12d
je 0x1f82
movq 0x2414(%rip), %rax      # 0x40d0
movq 0x241d(%rip), %rcx      # 0x40e0
decl 0xc(%rax)
decw 0xc(%rcx)
movq -0x30(%rbp), %rcx
incl 0x1c(%rcx)
movl 0x14(%rax), %eax
leal (%r12,%rax), %edi
decl %edi
movl %edi, 0x28(%rcx,%r15,4)
jmp 0x1d4e
cmpl $-0x1, %r12d
je 0x1f87
cmpl $0xc, -0x70(%rbp)
jne 0x1d25
movq -0x40(%rbp), %rdi
callq 0x180e
cmpl $-0x1, %eax
je 0x1f8c
movq 0x23c6(%rip), %rcx      # 0x40d0
addl 0x14(%rcx), %eax
movq -0x58(%rbp), %rdx
movl %eax, (%rdx,%r15,4)
movl 0x14(%rcx), %eax
addl %r12d, %eax
decl %eax
movl %eax, -0x110(%rbp,%r15,4)
movq 0x23a4(%rip), %rax      # 0x40d0
addl 0x14(%rax), %r12d
movq -0x58(%rbp), %rcx
movl %r12d, (%rcx,%r15,4)
movq 0x23a1(%rip), %rcx      # 0x40e0
decl 0xc(%rax)
decw 0xc(%rcx)
movl -0x110(%rbp,%r15,4), %edi
shll $0xa, %edi
addq 0x2380(%rip), %rdi      # 0x40d8
cmpl $0x3ff, %r13d           # imm = 0x3FF
jle 0x1d90
movq -0x60(%rbp), %rax
leaq (%rax,%rbx), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x10d0
addl $0xfffffc00, %r13d      # imm = 0xFFFFFC00
addq $0x400, %rbx            # imm = 0x400
incq %r15
cmpq %r15, -0x38(%rbp)
jne 0x1c99
jmp 0x1da3
movq -0x60(%rbp), %rsi
addq %rbx, %rsi
movslq -0x48(%rbp), %rdx
subq %rbx, %rdx
callq 0x10d0
movl %r14d, %ecx
andb $0x7, %cl
movl $0x1, %eax
shll %cl, %eax
movl %r14d, %ecx
shrl $0x3, %ecx
movq -0xa0(%rbp), %rdx
xorb %al, (%rdx,%rcx)
movq -0x30(%rbp), %rcx
movl 0x4(%rcx), %eax
shrl $0x9, %eax
incl %eax
movl %eax, 0x1c(%rcx)
movq -0x90(%rbp), %rax
addq $0x28, %rax
movl %edx, %ecx
movl (%rax), %edx
addq $0x4, %rax
testl %edx, %edx
jne 0x1dda
movq 0x22eb(%rip), %rax      # 0x40d8
movslq %ecx, %rcx
shlq $0xa, %rcx
leaq (%rax,%rcx), %r12
movzwl 0x4(%rax,%rcx), %eax
xorl %ebx, %ebx
movq %r12, %r15
cmpl $0x3ff, %eax            # imm = 0x3FF
ja 0x1e20
movl %eax, %ebx
movl %eax, %r15d
movzwl 0x4(%r12,%r15), %eax
addl %ebx, %eax
cmpl $0x400, %eax            # imm = 0x400
jb 0x1e09
addq %r12, %r15
movzbl 0x6(%r15), %r13d
addq $0x8, %r13
movl %r13d, %eax
andl $-0x4, %eax
addl %ebx, %eax
movl $0x3fc, %ecx            # imm = 0x3FC
subl %eax, %ecx
movslq %ecx, %rax
movq %rax, -0x38(%rbp)
movq -0x50(%rbp), %rdi
callq 0x1060
addq $0x8, %rax
cmpq -0x38(%rbp), %rax
jbe 0x1ef5
movq 0x2273(%rip), %rax      # 0x40d0
movl 0x4(%rax), %eax
movl $0x16, %r12d
cmpl $0x17, %eax
movq -0x80(%rbp), %rbx
movq -0x88(%rbp), %r15
jb 0x1ea0
movl $0x16, %r12d
movl %r12d, %ecx
shrl $0x3, %ecx
movq -0x40(%rbp), %rdx
movzbl (%rdx,%rcx), %ecx
movl %r12d, %edx
andl $0x7, %edx
btl %edx, %ecx
jae 0x1ea0
incl %r12d
cmpl %r12d, %eax
jne 0x1e7c
movl %eax, %r12d
cmpl %eax, %r12d
je 0x1fd2
movl %r12d, %ecx
andb $0x7, %cl
movl $0x1, %eax
shll %cl, %eax
movl %r12d, %ecx
shrl $0x3, %ecx
movq -0x40(%rbp), %rdx
xorb %al, (%rdx,%rcx)
movq 0x2206(%rip), %rax      # 0x40d0
decl 0xc(%rax)
movq -0x90(%rbp), %rax
movq -0x98(%rbp), %rcx
movl %r12d, 0x28(%rax,%rcx,4)
addl $0x2, 0x1c(%rax)
shll $0xa, %r12d
addq 0x21e9(%rip), %r12      # 0x40d8
movw $0x400, %ax             # imm = 0x400
jmp 0x1f1a
andl $-0x4, %r13d
addl $0x4, %r13d
movw %r13w, 0x4(%r15)
addl %r13d, %ebx
addq %rbx, %r12
movl $0x400, %eax            # imm = 0x400
subl %ebx, %eax
movq -0x80(%rbp), %rbx
movq -0x88(%rbp), %r15
movw %ax, 0x4(%r12)
incl %r14d
movl %r14d, (%r12)
movq -0x50(%rbp), %r14
movq %r14, %rdi
callq 0x1060
movb %al, 0x6(%r12)
movb $0x1, 0x7(%r12)
addq $0x8, %r12
movzbl %al, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x1040
movq -0x68(%rbp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movl -0x74(%rbp), %edi
callq 0x10a0
movq %r15, %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1218
callq 0x12ab
callq 0x1286
callq 0x1261
movq %r13, %rdi
callq 0x11a5
addq $0x8, %r13
movq %r13, %rdi
callq 0x13a2
callq 0x11cc
movq %r15, %rdi
callq 0x137b
callq 0x1366
callq 0x1341
movq -0x50(%rbp), %rdi
callq 0x123d
callq 0x12f5
movq %r13, %rdi
callq 0x11f1
callq 0x12d0