// File: /workspace/github_c_makefile_star2_O0/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
subq $0x170, %rsp            # imm = 0x170
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x4, -0x8(%rbp)
je 0x1948
movq 0x36d2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x16e3(%rip), %rsi      # 0x301a
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x1110
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x198f
movq 0x368c(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x16cc(%rip), %rsi      # 0x304a
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x17a0
movl %eax, -0x18(%rbp)
cmpl $0x1, -0x18(%rbp)
jne 0x19c7
movq 0x364c(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x16af(%rip), %rsi      # 0x3065
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1110
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x1a0b
movq 0x3610(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x1699(%rip), %rsi      # 0x3093
movb $0x0, %al
callq 0x10c0
movl $0x2, %edi
callq 0x1140
movl -0x14(%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, 0x369e(%rip)      # 0x50d0
movq $-0x1, %rax
cmpq %rax, 0x3690(%rip)      # 0x50d0
jne 0x1a58
leaq 0x1661(%rip), %rdi      # 0x30aa
callq 0x1120
movl $0x1, %edi
callq 0x1140
movq 0x3671(%rip), %rax      # 0x50d0
addq $0x400, %rax            # imm = 0x400
movq %rax, 0x365c(%rip)      # 0x50c8
movq 0x365d(%rip), %rax      # 0x50d0
addq $0x800, %rax            # imm = 0x800
movq %rax, 0x3658(%rip)      # 0x50d8
movl -0x1c(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1080
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
subl $0x1, %eax
movl $0x400, %ecx            # imm = 0x400
cltd
idivl %ecx
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq 0x3616(%rip), %rcx      # 0x50c8
cmpl 0xc(%rcx), %eax
jbe 0x1ad9
movq 0x353a(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x15e7(%rip), %rsi      # 0x30af
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
movq 0x35f0(%rip), %rax      # 0x50d0
movq 0x35f1(%rip), %rcx      # 0x50d8
movl (%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1150
movq %rax, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x1b62
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x1b55
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x1b55
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1b57
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1b14
cmpl $0x0, -0x40(%rbp)
je 0x1bf1
movl -0x40(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x48(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x148(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x1bd2
leaq -0x38(%rbp), %rdi
leaq 0x1468(%rip), %rsi      # 0x3018
callq 0x1100
movq %rax, %rdx
movq -0x148(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1b9d
movq -0x148(%rbp), %rax
movl -0x54(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movq %rax, %rsp
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1150
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x150(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1060
movq %rax, %rcx
movq -0x150(%rbp), %rax
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x1c87
movq -0x60(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1130
movq 0x3487(%rip), %rdi      # 0x50d0
movq -0x60(%rbp), %rsi
callq 0x1560
movl %eax, -0x58(%rbp)
cmpl $-0x1, -0x58(%rbp)
jne 0x1c85
movq 0x3396(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x1459(%rip), %rsi      # 0x30cd
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
jmp 0x1cd0
movq 0x3442(%rip), %rdi      # 0x50d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x1560
movl %eax, -0x58(%rbp)
cmpl $-0x1, -0x58(%rbp)
jne 0x1cce
movq 0x334d(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x1410(%rip), %rsi      # 0x30cd
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
jmp 0x1cd0
movq 0x33f9(%rip), %rax      # 0x50d0
movq 0x33fa(%rip), %rcx      # 0x50d8
movl 0x8(%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq 0x33d8(%rip), %rcx      # 0x50c8
movzwl 0x58(%rcx), %ecx
imull -0x58(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x60(%rbp), %rdi
callq 0x1060
movq %rax, %rcx
movq -0x158(%rbp), %rax
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x1d53
movq -0x60(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x60(%rbp), %rdi
callq 0x1060
movq %rax, %rcx
movq -0x160(%rbp), %rax
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, -0x70(%rbp)
movq -0x60(%rbp), %rdi
callq 0x1060
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
je 0x1db8
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
cmpl $0xff, -0x70(%rbp)
jle 0x1dab
movq 0x3268(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1353(%rip), %rsi      # 0x30ed
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
jmp 0x1dad
movl -0x6c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1d66
movl -0x70(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x78(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x168(%rbp)
movq %rdi, %rsp
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rsi
movslq -0x6c(%rbp), %rax
addq %rax, %rsi
addq $0x1, %rsi
callq 0x1050
movq -0x168(%rbp), %rdx
movq 0x32cd(%rip), %rdi      # 0x50d0
movq -0x68(%rbp), %rsi
movl $0x1, %ecx
callq 0x1410
movl %eax, -0x84(%rbp)
cmpl $-0x1, -0x84(%rbp)
je 0x1e55
movq 0x32a9(%rip), %rax      # 0x50d0
movq 0x32aa(%rip), %rcx      # 0x50d8
movl 0x8(%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq 0x3288(%rip), %rcx      # 0x50c8
movzwl 0x58(%rcx), %ecx
imull -0x84(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x168(%rbp), %rdx
movq 0x326d(%rip), %rdi      # 0x50d0
movq -0x68(%rbp), %rsi
xorl %ecx, %ecx
callq 0x1410
cmpl $-0x1, %eax
je 0x1e9c
movq -0x168(%rbp), %rdx
movq 0x3177(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1278(%rip), %rsi      # 0x3103
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
movq 0x322d(%rip), %rax      # 0x50d0
movq 0x322e(%rip), %rcx      # 0x50d8
movl 0x4(%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movl $0xb, -0x94(%rbp)
movl -0x94(%rbp), %eax
movq 0x31f5(%rip), %rcx      # 0x50c8
cmpl (%rcx), %eax
jae 0x1f03
movl -0x94(%rbp), %edi
movq -0x90(%rbp), %rsi
callq 0x1260
cmpl $0x0, %eax
jne 0x1ef0
jmp 0x1f03
jmp 0x1ef2
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x1ec6
movl -0x94(%rbp), %eax
movq 0x31b8(%rip), %rcx      # 0x50c8
cmpl (%rcx), %eax
jne 0x1f36
movq 0x30dd(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x11fc(%rip), %rsi      # 0x3121
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
movq 0x3193(%rip), %rax      # 0x50d0
movq 0x3194(%rip), %rcx      # 0x50d8
movl 0x8(%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq 0x3172(%rip), %rcx      # 0x50c8
movzwl 0x58(%rcx), %ecx
imull -0x94(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movw $0x8000, (%rax)         # imm = 0x8000
movl -0x20(%rbp), %ecx
movq -0xa0(%rbp), %rax
movl %ecx, 0x4(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0x10e0
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0xa0(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0xa0(%rbp), %rax
movl $0x1, 0x1c(%rax)
movq -0xa0(%rbp), %rax
movw $0x1, 0x1a(%rax)
movq 0x30fc(%rip), %rax      # 0x50c8
movl 0x10(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x10(%rax)
movq 0x30fc(%rip), %rax      # 0x50d8
movw 0xe(%rax), %cx
addw $-0x1, %cx
movw %cx, 0xe(%rax)
movslq -0x20(%rbp), %rsi
movl -0x1c(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x2, %ecx
movq %r9, %rdi
callq 0x1070
movq %rax, -0xa8(%rbp)
movl $0x0, -0x108(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x10c(%rbp)
movl $0x0, -0x104(%rbp)
movl -0x104(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x230c
cmpl $0xc, -0x104(%rbp)
jge 0x20fb
movq -0x30(%rbp), %rdi
callq 0x1880
movl %eax, -0x110(%rbp)
cmpl $-0x1, -0x110(%rbp)
jne 0x2081
movq 0x2f92(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x10cb(%rip), %rsi      # 0x313b
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
movq 0x3040(%rip), %rsi      # 0x50c8
movq 0x3049(%rip), %rdx      # 0x50d8
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1740
movq -0xa0(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1c(%rax)
movq 0x3018(%rip), %rax      # 0x50c8
movl 0x14(%rax), %edx
addl -0x110(%rbp), %edx
subl $0x1, %edx
movq -0xa0(%rbp), %rax
movslq -0x104(%rbp), %rcx
movl %edx, 0x28(%rax,%rcx,4)
movq 0x2ffb(%rip), %rax      # 0x50d0
movq -0xa0(%rbp), %rcx
movslq -0x104(%rbp), %rdx
movl 0x28(%rcx,%rdx,4), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
jmp 0x2276
cmpl $0xc, -0x104(%rbp)
jl 0x2274
movq -0x30(%rbp), %rdi
callq 0x1880
movl %eax, -0x114(%rbp)
cmpl $-0x1, -0x114(%rbp)
jne 0x2142
movq 0x2ed1(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x100a(%rip), %rsi      # 0x313b
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
cmpl $0xc, -0x6c(%rbp)
jne 0x2211
movq -0x30(%rbp), %rdi
callq 0x1880
movl %eax, -0x118(%rbp)
cmpl $-0x1, -0x118(%rbp)
jne 0x2186
movq 0x2e8d(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xfc6(%rip), %rsi       # 0x313b
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
movq 0x2f3b(%rip), %rax      # 0x50c8
movl 0x14(%rax), %edx
addl -0x118(%rbp), %edx
movq -0xa0(%rbp), %rax
movslq -0x104(%rbp), %rcx
movl %edx, 0x28(%rax,%rcx,4)
movq 0x2f21(%rip), %rax      # 0x50d0
movq -0xa0(%rbp), %rcx
movslq -0x104(%rbp), %rdx
movl 0x28(%rcx,%rdx,4), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq 0x2ef1(%rip), %rax      # 0x50c8
movl 0x14(%rax), %ecx
addl -0x114(%rbp), %ecx
subl $0x1, %ecx
movl -0x104(%rbp), %eax
subl $0xc, %eax
cltq
movl %ecx, -0x100(%rbp,%rax,4)
movq -0xb8(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq 0x2eb0(%rip), %rax      # 0x50c8
movl 0x14(%rax), %edx
addl -0x114(%rbp), %edx
movq -0xa0(%rbp), %rax
movslq -0x104(%rbp), %rcx
movl %edx, 0x28(%rax,%rcx,4)
movq 0x2e8e(%rip), %rsi      # 0x50c8
movq 0x2e97(%rip), %rdx      # 0x50d8
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1740
movq 0x2e7e(%rip), %rax      # 0x50d0
movl -0x104(%rbp), %ecx
subl $0xc, %ecx
movslq %ecx, %rcx
movl -0x100(%rbp,%rcx,4), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
jmp 0x2276
cmpl $0x400, -0x10c(%rbp)    # imm = 0x400
jge 0x22b2
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movslq -0x108(%rbp), %rax
addq %rax, %rsi
movslq -0x10c(%rbp), %rdx
callq 0x10d0
movl $0x0, -0x10c(%rbp)
jmp 0x230c
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movslq -0x108(%rbp), %rax
addq %rax, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x10d0
movl -0x10c(%rbp), %eax
subl $0x400, %eax            # imm = 0x400
movl %eax, -0x10c(%rbp)
movl -0x108(%rbp), %eax
addl $0x400, %eax            # imm = 0x400
movl %eax, -0x108(%rbp)
jmp 0x22f8
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0x202b
movl -0x94(%rbp), %edi
movq -0x90(%rbp), %rsi
callq 0x12b0
movq -0xa0(%rbp), %rax
movl 0x4(%rax), %ecx
shrl $0x9, %ecx
addl $0x1, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0x120(%rbp)
movq -0x68(%rbp), %rax
movslq -0x120(%rbp), %rcx
cmpl $0x0, 0x28(%rax,%rcx,4)
je 0x237a
movq -0x68(%rbp), %rax
movslq -0x120(%rbp), %rcx
movl 0x28(%rax,%rcx,4), %eax
movl %eax, -0x11c(%rbp)
movl -0x120(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x120(%rbp)
jmp 0x2342
movq 0x2d4f(%rip), %rax      # 0x50d0
movl -0x11c(%rbp), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x128(%rbp)
movl $0x0, -0x12c(%rbp)
movl -0x12c(%rbp), %eax
movq -0x128(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
addl %ecx, %eax
cmpl $0x400, %eax            # imm = 0x400
jge 0x23fb
movq -0x128(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0x12c(%rbp), %eax
movl %eax, -0x12c(%rbp)
movq 0x2cf7(%rip), %rax      # 0x50d0
movl -0x11c(%rbp), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0x12c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x128(%rbp)
jmp 0x23a1
movq -0x168(%rbp), %rdi
movq -0x128(%rbp), %rax
movzbl 0x6(%rax), %eax
addq $0x8, %rax
movl %eax, -0x13c(%rbp)
movl -0x13c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl $0x4, %eax
subl %edx, %eax
addl -0x13c(%rbp), %eax
movl %eax, -0x13c(%rbp)
movl $0x400, %eax            # imm = 0x400
subl -0x13c(%rbp), %eax
subl -0x12c(%rbp), %eax
movl %eax, -0x13c(%rbp)
movslq -0x13c(%rbp), %rax
movq %rax, -0x170(%rbp)
callq 0x1060
movq %rax, %rcx
movq -0x170(%rbp), %rax
addq $0x8, %rcx
cmpq %rcx, %rax
jae 0x255f
movl $0x16, -0x140(%rbp)
movl -0x140(%rbp), %eax
movq 0x2c38(%rip), %rcx      # 0x50c8
cmpl 0x4(%rcx), %eax
jae 0x24be
movl -0x140(%rbp), %edi
movq -0x30(%rbp), %rsi
callq 0x1260
cmpl $0x0, %eax
jne 0x24ab
jmp 0x24be
jmp 0x24ad
movl -0x140(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x140(%rbp)
jmp 0x2483
movl -0x140(%rbp), %eax
movq 0x2bfd(%rip), %rcx      # 0x50c8
cmpl 0x4(%rcx), %eax
jne 0x24f2
movq 0x2b21(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xc5a(%rip), %rsi       # 0x313b
movb $0x0, %al
callq 0x10c0
movl $0x1, %edi
callq 0x1140
movl -0x140(%rbp), %edi
movq -0x30(%rbp), %rsi
callq 0x12b0
movq 0x2bc0(%rip), %rax      # 0x50c8
movl 0xc(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0xc(%rax)
movl -0x140(%rbp), %edx
movq -0x68(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movl %edx, 0x28(%rax,%rcx,4)
movq -0x68(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x1c(%rax)
movq 0x2b99(%rip), %rax      # 0x50d0
movl -0x140(%rbp), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rax
movw $0x400, 0x4(%rax)       # imm = 0x400
jmp 0x2600
movq -0x128(%rbp), %rax
movzbl 0x6(%rax), %eax
addq $0x8, %rax
movw %ax, %cx
movq -0x128(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x128(%rbp), %rax
movzwl 0x4(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl $0x4, %edx
subl %eax, %edx
movq -0x128(%rbp), %rax
movzwl 0x4(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x4(%rax)
movq -0x128(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0x12c(%rbp), %eax
movl %eax, -0x12c(%rbp)
movq 0x2b09(%rip), %rax      # 0x50d0
movl -0x11c(%rbp), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0x12c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x138(%rbp)
movl $0x400, %eax            # imm = 0x400
subl -0x12c(%rbp), %eax
movw %ax, %cx
movq -0x138(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x168(%rbp), %rdi
movl -0x94(%rbp), %ecx
addl $0x1, %ecx
movq -0x138(%rbp), %rax
movl %ecx, (%rax)
callq 0x1060
movq -0x168(%rbp), %rsi
movb %al, %cl
movq -0x138(%rbp), %rax
movb %cl, 0x6(%rax)
movq -0x138(%rbp), %rax
movb $0x1, 0x7(%rax)
movq -0x138(%rbp), %rdi
addq $0x8, %rdi
movq -0x138(%rbp), %rax
movzbl 0x6(%rax), %eax
movl %eax, %edx
callq 0x1040
movq -0x38(%rbp), %rdi
callq 0x1030
movq -0x60(%rbp), %rdi
callq 0x1030
movl -0x14(%rbp), %edi
callq 0x10a0
movl $0x0, -0x4(%rbp)
movq -0x78(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq