// File: SaadBazaz[P]RIP-Protocol-Simulation/ProxyRouter.cpp
// Function: main
int main()
{
	cout << "Which Unique Port do you want to use?\n";
	cin >> port;

	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	assert(sockfd != -1);

	printf("sockfd=%d\n", sockfd);

	struct sockaddr_in saddr, caddr;
	memset(&saddr, 0, sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(port);
	saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	int res = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
	assert(res != -1);

	//Create listening queue
	listen(sockfd, 5);

	//Define fdset collection
	fd_set fdset;

	//Define fds array
	int fds[MAXFD];
	int i = 0;
	for (; i < MAXFD; ++i)
	{
		fds[i] = -1;
	}


	//Add a file descriptor to the fds array
	fds_add(fds, sockfd);





{
	char option;
	
	do {

		cout << "Would you like to connect to any other routers? (y/n)\n";
		cin >> option;
		if (option == 'n' or option == 'N'){
			break;
		} 

		int router_port;
		cout<<"Enter the partner router's Unique Port: ";
		cin >> router_port;

		int sockfd_router = socket(AF_INET, SOCK_STREAM, 0);
		assert(sockfd_router != -1);

		//Set Address Information
		struct sockaddr_in saddr;
		memset(&saddr, 0, sizeof(saddr));
		saddr.sin_family = AF_INET;
		saddr.sin_port = htons(router_port);
		saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

		//Link to router
		int res = connect(sockfd_router, (struct sockaddr *)&saddr, sizeof(saddr));
		assert(res != -1);

		TableRow myself(port, -1, "ROUTER");

		cout << "Sending this router's unique port to partner router..." << endl;
		string init_msg = constructNewMessage(UPDATE, port, router_port, (void *)&myself);
		send(sockfd_router, init_msg.c_str(), strlen(init_msg.c_str()), 0);

		cout << endl;


		TableRow table_row (router_port, sockfd_router, "ROUTER");
		table.push_back(table_row);

		fds_add(fds, sockfd_router);		

		printTable();

	}
	while(option != 'n' and option != 'N');


}


	while (1)
	{
		FD_ZERO(&fdset); //Clear the fdset array to 0

		int maxfd = -1;

		int i = 0;

		//For loop finds the maximum subscript for the ready event in the fds array
		for (; i < MAXFD; i++)
		{
			if (fds[i] == -1)
			{
				continue;
			}

			FD_SET(fds[i], &fdset);

			if (fds[i] > maxfd)
			{
				maxfd = fds[i];
			}
		}

		struct timeval tv = {5, 0}; //Set timeout of 5 seconds

		int n = select(maxfd + 1, &fdset, NULL, NULL, &tv); //Selectect system call, where we only focus on read events
		if (n == -1)										//fail
		{
			perror("select error");
		}
		else if (n == 0) //Timeout, meaning no file descriptor returned
		{
			printf(".\n");
			sendTableToAllRouters();
		}
		else //Ready event generation
		{
			//Because we only know the number of ready events by the return value of select, we don't know which events are ready.
			//Therefore, each file descriptor needs to be traversed for judgment
			for (i = 0; i < MAXFD; ++i)
			{
				if (fds[i] == -1) //If fds[i]==-1, the event is not ready
				{
					continue;
				}
				if (FD_ISSET(fds[i], &fdset)) //Determine if the event corresponding to the file descriptor is ready
				{

					//There are two kinds of cases for judging file descriptors
					try
					{
						if (fds[i] == sockfd) //A file descriptor is a socket, meaning accept if a new client requests a connection
						{
							//accept
							struct sockaddr_in caddr;
							int len = sizeof(caddr);

							int c = accept(sockfd, (struct sockaddr *)&caddr, (socklen_t *)&len); //Accept new client connections
							if (c < 0)
							{
								continue;
							}

							printf("accept c=%d\n", c);
							fds_add(fds, c); //Add the connection socket to the array where the file descriptor is stored
							TableRow newClient(-1, c, "UNKNOWN");
							table.push_back(newClient);
							printTable();

							sendTableToAllRouters();

						}
						else //Receive data recv when an existing client sends data
						{
							char buff[500] = {0};
							int res = recv(fds[i], buff, 499, 0);
							if (res <= 0)
							{
								close(fds[i]);
								auto index_vec = findAllRowsByFD(fds[i]);

								fds[i] = -1;
								printf("one client over\n");

								for (int i=0; i<index_vec.size(); i++)
								{
									table.erase(table.begin() + index_vec[i]);
									if (i+1!=index_vec.size())
										index_vec[i+1]--;
								}

								printTable();
								sendTableToAllRouters();
							}
							else
							{
								printf("recv(%d)=%s\n", fds[i], buff); //Output Client Sent Information
								handlePacket(fds[i], buff);
							}
						}
					}
					catch (...)
					{
						cout<<"Crashed!"<<endl;
						// send(fds[i], "BAD", 3, 0);
						sleep(5);
					}
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5e0, %rsp            # imm = 0x5E0
movl $0x0, -0x4(%rbp)
movq 0xb01f(%rip), %rdi      # 0x10fc8
leaq 0x619f(%rip), %rsi      # 0xc14f
callq 0x3310
movq 0xb024(%rip), %rdi      # 0x10fe0
leaq 0xb33d(%rip), %rsi      # 0x11300
callq 0x31d0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30b0
movl %eax, -0x8(%rbp)
cmpl $-0x1, -0x8(%rbp)
je 0x5fe4
jmp 0x6003
leaq 0x618b(%rip), %rdi      # 0xc176
leaq 0x6191(%rip), %rsi      # 0xc183
movl $0x1ae, %edx            # imm = 0x1AE
leaq 0x6195(%rip), %rcx      # 0xc193
callq 0x3200
movl -0x8(%rbp), %esi
leaq 0x6191(%rip), %rdi      # 0xc19e
movb $0x0, %al
callq 0x3060
leaq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3190
movw $0x2, -0x18(%rbp)
movl 0xb2d0(%rip), %eax      # 0x11300
movzwl %ax, %edi
callq 0x3070
movw %ax, -0x16(%rbp)
leaq 0x6166(%rip), %rdi      # 0xc1a9
callq 0x3330
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %edi
leaq -0x18(%rbp), %rsi
movl $0x10, %edx
callq 0x3090
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
je 0x6067
jmp 0x6086
leaq 0x6145(%rip), %rdi      # 0xc1b3
leaq 0x610e(%rip), %rsi      # 0xc183
movl $0x1b9, %edx            # imm = 0x1B9
leaq 0x6112(%rip), %rcx      # 0xc193
callq 0x3200
movl -0x8(%rbp), %edi
movl $0x5, %esi
callq 0x33b0
movl $0x0, -0xe4(%rbp)
cmpl $0xa, -0xe4(%rbp)
jge 0x60c9
movslq -0xe4(%rbp), %rax
movl $0xffffffff, -0xe0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0xe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe4(%rbp)
jmp 0x609d
leaq -0xe0(%rbp), %rdi
movl -0x8(%rbp), %esi
callq 0x5f40
movq 0xaee9(%rip), %rdi      # 0x10fc8
leaq 0x60d7(%rip), %rsi      # 0xc1bd
callq 0x3310
movq 0xaeee(%rip), %rdi      # 0x10fe0
leaq -0xe5(%rbp), %rsi
callq 0x3420
movsbl -0xe5(%rbp), %eax
cmpl $0x6e, %eax
je 0x6116
movsbl -0xe5(%rbp), %eax
cmpl $0x4e, %eax
jne 0x611b
jmp 0x647f
movq 0xaea6(%rip), %rdi      # 0x10fc8
leaq 0x60cb(%rip), %rsi      # 0xc1f4
callq 0x3310
movq 0xaeab(%rip), %rdi      # 0x10fe0
leaq -0xec(%rbp), %rsi
callq 0x31d0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30b0
movl %eax, -0xf0(%rbp)
cmpl $-0x1, -0xf0(%rbp)
je 0x6163
jmp 0x6182
leaq 0x60b3(%rip), %rdi      # 0xc21d
leaq 0x6012(%rip), %rsi      # 0xc183
movl $0x1e1, %edx            # imm = 0x1E1
leaq 0x6016(%rip), %rcx      # 0xc193
callq 0x3200
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3190
movw $0x2, -0x100(%rbp)
movl -0xec(%rbp), %eax
movzwl %ax, %edi
callq 0x3070
movw %ax, -0xfe(%rbp)
leaq 0x5fef(%rip), %rdi      # 0xc1a9
callq 0x3330
movl %eax, -0xfc(%rbp)
movl -0xf0(%rbp), %edi
leaq -0x100(%rbp), %rsi
movl $0x10, %edx
callq 0x32a0
movl %eax, -0x104(%rbp)
cmpl $-0x1, -0x104(%rbp)
je 0x61ed
jmp 0x620c
leaq 0x5fbf(%rip), %rdi      # 0xc1b3
leaq 0x5f88(%rip), %rsi      # 0xc183
movl $0x1ec, %edx            # imm = 0x1EC
leaq 0x5f8c(%rip), %rcx      # 0xc193
callq 0x3200
movl 0xb0ee(%rip), %eax      # 0x11300
movl %eax, -0x51c(%rbp)
leaq -0x169(%rbp), %rdi
movq %rdi, -0x518(%rbp)
callq 0x3500
movq -0x518(%rbp), %rdx
leaq 0x5e52(%rip), %rsi      # 0xc08b
leaq -0x168(%rbp), %rdi
callq 0x7980
jmp 0x6247
movl -0x51c(%rbp), %esi
movq %rsp, %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl $0x0, (%rax)
leaq -0x148(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq -0x168(%rbp), %rcx
xorl %r9d, %r9d
movl %r9d, %r8d
callq 0x8150
jmp 0x6284
leaq -0x168(%rbp), %rdi
callq 0x31e0
leaq -0x169(%rbp), %rdi
callq 0x3380
movq 0xad25(%rip), %rdi      # 0x10fc8
leaq 0x5f87(%rip), %rsi      # 0xc231
callq 0x3310
movq %rax, -0x528(%rbp)
jmp 0x62b8
movq -0x528(%rbp), %rdi
movq 0xacf2(%rip), %rsi      # 0x10fb8
callq 0x3350
jmp 0x62cd
movl 0xb02d(%rip), %edx      # 0x11300
movl -0xec(%rbp), %ecx
leaq -0x1a0(%rbp), %rdi
movl $0x2, %esi
leaq -0x148(%rbp), %r8
callq 0x3c70
jmp 0x62f3
movl -0xf0(%rbp), %eax
movl %eax, -0x534(%rbp)
leaq -0x1a0(%rbp), %rdi
movq %rdi, -0x540(%rbp)
callq 0x3130
movq -0x540(%rbp), %rdi
movq %rax, -0x530(%rbp)
callq 0x3130
movq %rax, %rdi
callq 0x3150
movl -0x534(%rbp), %edi
movq -0x530(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x3170
jmp 0x6346
movq 0xac7b(%rip), %rdi      # 0x10fc8
movq 0xac64(%rip), %rsi      # 0x10fb8
callq 0x3350
jmp 0x635b
movl -0xec(%rbp), %eax
movl %eax, -0x550(%rbp)
movl -0xf0(%rbp), %eax
movl %eax, -0x54c(%rbp)
leaq -0x201(%rbp), %rdi
movq %rdi, -0x548(%rbp)
callq 0x3500
movq -0x548(%rbp), %rdx
leaq 0x5cf7(%rip), %rsi      # 0xc08b
leaq -0x200(%rbp), %rdi
callq 0x7980
jmp 0x63a2
movl -0x54c(%rbp), %edx
movl -0x550(%rbp), %esi
movq %rsp, %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl $0x0, (%rax)
leaq -0x1e0(%rbp), %rdi
leaq -0x200(%rbp), %rcx
xorl %r9d, %r9d
movl %r9d, %r8d
callq 0x8150
jmp 0x63e0
leaq -0x200(%rbp), %rdi
callq 0x31e0
leaq -0x201(%rbp), %rdi
callq 0x3380
leaq 0xaee9(%rip), %rdi      # 0x112e8
leaq -0x1e0(%rbp), %rsi
callq 0x7770
jmp 0x640d
movl -0xf0(%rbp), %esi
leaq -0xe0(%rbp), %rdi
callq 0x5f40
callq 0x4320
jmp 0x6426
leaq -0x1e0(%rbp), %rdi
callq 0x77e0
leaq -0x1a0(%rbp), %rdi
callq 0x31e0
leaq -0x148(%rbp), %rdi
callq 0x77e0
movsbl -0xe5(%rbp), %ecx
xorl %eax, %eax
cmpl $0x6e, %ecx
movb %al, -0x551(%rbp)
je 0x6471
movsbl -0xe5(%rbp), %eax
cmpl $0x4e, %eax
setne %al
movb %al, -0x551(%rbp)
movb -0x551(%rbp), %al
testb $0x1, %al
jne 0x60d8
jmp 0x6481
jmp 0x6483
leaq -0xb0(%rbp), %rax
movq %rax, -0x210(%rbp)
movl $0x0, -0x208(%rbp)
movl -0x208(%rbp), %eax
cmpq $0x10, %rax
jae 0x65b7
movq -0x210(%rbp), %rax
movl -0x208(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x208(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x208(%rbp)
jmp 0x649b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
jmp 0x6503
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x31e0
leaq -0x169(%rbp), %rdi
callq 0x3380
jmp 0x6cd6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
jmp 0x65a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
jmp 0x659a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
jmp 0x656e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x31e0
leaq -0x201(%rbp), %rdi
callq 0x3380
jmp 0x659a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x77e0
leaq -0x1a0(%rbp), %rdi
callq 0x31e0
leaq -0x148(%rbp), %rdi
callq 0x77e0
jmp 0x6cd6
jmp 0x65b9
movl $0xffffffff, -0x214(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x218(%rbp)
cmpl $0xa, -0x218(%rbp)
jge 0x667a
movslq -0x218(%rbp), %rax
cmpl $-0x1, -0xe0(%rbp,%rax,4)
jne 0x65ed
jmp 0x6666
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xb0(%rbp,%rax,8), %rcx
movq %rcx, -0xb0(%rbp,%rax,8)
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %eax
cmpl -0x214(%rbp), %eax
jle 0x6664
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %eax
movl %eax, -0x214(%rbp)
jmp 0x6666
movl -0x218(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x218(%rbp)
jmp 0x65cd
movq 0x5c7f(%rip), %rax      # 0xc300
movq %rax, -0x228(%rbp)
movq 0x5c79(%rip), %rax      # 0xc308
movq %rax, -0x220(%rbp)
movl -0x214(%rbp), %edi
addl $0x1, %edi
leaq -0xb0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x228(%rbp), %r8
movq %rcx, %rdx
callq 0x32f0
movl %eax, -0x22c(%rbp)
cmpl $-0x1, -0x22c(%rbp)
jne 0x66d9
leaq 0x5b99(%rip), %rdi      # 0xc268
callq 0x32b0
jmp 0x6cd1
cmpl $0x0, -0x22c(%rbp)
jne 0x66ff
leaq 0x5a28(%rip), %rdi      # 0xc111
movb $0x0, %al
callq 0x3060
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4870
jmp 0x6ccf
movl $0x0, -0x218(%rbp)
cmpl $0xa, -0x218(%rbp)
jge 0x6ccd
movslq -0x218(%rbp), %rax
cmpl $-0x1, -0xe0(%rbp,%rax,4)
jne 0x672c
jmp 0x6cb9
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xb0(%rbp,%rax,8), %rax
movq %rax, -0x560(%rbp)
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x560(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x6cb7
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %eax
cmpl -0x8(%rbp), %eax
jne 0x6951
movl $0x10, -0x240(%rbp)
movl -0x8(%rbp), %edi
leaq -0x23c(%rbp), %rsi
leaq -0x240(%rbp), %rdx
callq 0x30e0
movl %eax, -0x564(%rbp)
jmp 0x67c9
movl -0x564(%rbp), %eax
movl %eax, -0x244(%rbp)
cmpl $0x0, -0x244(%rbp)
jge 0x67fa
jmp 0x6cb9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
jmp 0x6c48
movl -0x244(%rbp), %esi
leaq 0x5a6e(%rip), %rdi      # 0xc275
xorl %eax, %eax
callq 0x3060
jmp 0x6810
movl -0x244(%rbp), %esi
leaq -0xe0(%rbp), %rdi
callq 0x5f40
movl -0x244(%rbp), %eax
movl %eax, -0x574(%rbp)
leaq -0x2a9(%rbp), %rdi
movq %rdi, -0x570(%rbp)
callq 0x3500
movq -0x570(%rbp), %rdx
leaq 0x5a33(%rip), %rsi      # 0xc282
leaq -0x2a8(%rbp), %rdi
callq 0x7980
jmp 0x685d
movl -0x574(%rbp), %edx
movq %rsp, %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl $0x0, (%rax)
leaq -0x288(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq -0x2a8(%rbp), %rcx
xorl %r9d, %r9d
movl %r9d, %r8d
callq 0x8150
jmp 0x689a
leaq -0x2a8(%rbp), %rdi
callq 0x31e0
leaq -0x2a9(%rbp), %rdi
callq 0x3380
leaq 0xaa2f(%rip), %rdi      # 0x112e8
leaq -0x288(%rbp), %rsi
callq 0x7770
jmp 0x68c7
callq 0x4320
jmp 0x68ce
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4870
jmp 0x68da
leaq -0x288(%rbp), %rdi
callq 0x77e0
jmp 0x6c9a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
jmp 0x691d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x31e0
leaq -0x2a9(%rbp), %rdi
callq 0x3380
jmp 0x6c48
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x77e0
jmp 0x6c48
leaq -0x4a0(%rbp), %rdi
movq %rdi, -0x590(%rbp)
xorl %esi, %esi
movl %esi, -0x584(%rbp)
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x3190
movq -0x590(%rbp), %rsi
movl -0x584(%rbp), %ecx
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %edi
movl $0x1f3, %edx            # imm = 0x1F3
callq 0x30f0
movq %rax, -0x580(%rbp)
jmp 0x699f
movq -0x580(%rbp), %rax
movl %eax, -0x4a4(%rbp)
cmpl $0x0, -0x4a4(%rbp)
jg 0x6b6e
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %edi
callq 0x3550
jmp 0x69ce
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %esi
leaq -0x4c0(%rbp), %rdi
callq 0x47a0
jmp 0x69ea
movslq -0x218(%rbp), %rax
movl $0xffffffff, -0xe0(%rbp,%rax,4) # imm = 0xFFFFFFFF
leaq 0x5887(%rip), %rdi      # 0xc28a
xorl %eax, %eax
callq 0x3060
jmp 0x6a0c
movl $0x0, -0x4c4(%rbp)
movslq -0x4c4(%rbp), %rax
movq %rax, -0x598(%rbp)
leaq -0x4c0(%rbp), %rdi
callq 0x7ba0
movq %rax, %rcx
movq -0x598(%rbp), %rax
cmpq %rcx, %rax
jae 0x6b4a
leaq 0xa89e(%rip), %rdi      # 0x112e8
movq %rdi, -0x5a8(%rbp)
callq 0x7d70
movq %rax, -0x4e0(%rbp)
movslq -0x4c4(%rbp), %rsi
leaq -0x4c0(%rbp), %rdi
callq 0x7bc0
movslq (%rax), %rsi
leaq -0x4e0(%rbp), %rdi
callq 0x7da0
movq %rax, -0x4d8(%rbp)
leaq -0x4d0(%rbp), %rdi
leaq -0x4d8(%rbp), %rsi
callq 0x7de0
movq -0x5a8(%rbp), %rdi
movq -0x4d0(%rbp), %rsi
callq 0x7d00
movq %rax, -0x5a0(%rbp)
jmp 0x6ab5
movq -0x5a0(%rbp), %rax
movq %rax, -0x4e8(%rbp)
movl -0x4c4(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x5b0(%rbp)
leaq -0x4c0(%rbp), %rdi
callq 0x7ba0
movq %rax, %rcx
movq -0x5b0(%rbp), %rax
cmpq %rcx, %rax
je 0x6b34
movl -0x4c4(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
leaq -0x4c0(%rbp), %rdi
callq 0x7bc0
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x6b34
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
leaq -0x4c0(%rbp), %rdi
callq 0x7b40
jmp 0x6c48
jmp 0x6b36
movl -0x4c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c4(%rbp)
jmp 0x6a16
callq 0x4320
jmp 0x6b51
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4870
jmp 0x6b5d
leaq -0x4c0(%rbp), %rdi
callq 0x7b40
jmp 0x6c98
movslq -0x218(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %esi
leaq 0x5718(%rip), %rdi      # 0xc29b
xorl %eax, %eax
leaq -0x4a0(%rbp), %rdx
callq 0x3060
jmp 0x6b93
movslq -0x218(%rbp), %rax
leaq -0xe0(%rbp,%rax,4), %rax
movq %rax, -0x5c0(%rbp)
leaq -0x509(%rbp), %rdi
movq %rdi, -0x5b8(%rbp)
callq 0x3500
movq -0x5b8(%rbp), %rdx
leaq -0x508(%rbp), %rdi
leaq -0x4a0(%rbp), %rsi
callq 0x7980
jmp 0x6bd8
movq -0x5c0(%rbp), %rdi
leaq -0x508(%rbp), %rsi
callq 0x4c10
jmp 0x6bed
leaq -0x508(%rbp), %rdi
callq 0x31e0
leaq -0x509(%rbp), %rdi
callq 0x3380
jmp 0x6c98
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
jmp 0x6c3c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
leaq -0x508(%rbp), %rdi
callq 0x31e0
leaq -0x509(%rbp), %rdi
callq 0x3380
movq -0x178(%rbp), %rdi
callq 0x3120
movq 0xa36d(%rip), %rdi      # 0x10fc8
leaq 0x5646(%rip), %rsi      # 0xc2a8
callq 0x3310
movq %rax, -0x5c8(%rbp)
jmp 0x6c70
movq -0x5c8(%rbp), %rdi
movq 0xa33a(%rip), %rsi      # 0x10fb8
callq 0x3350
jmp 0x6c85
movl $0x5, %edi
callq 0x3260
jmp 0x6c91
callq 0x34b0
jmp 0x6cb7
jmp 0x6c9a
jmp 0x6c96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x178(%rbp)
movl %eax, -0x17c(%rbp)
callq 0x34b0
jmp 0x6cb5
jmp 0x6cd6
jmp 0x6cb9
movl -0x218(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x218(%rbp)
jmp 0x6709
jmp 0x6ccf
jmp 0x6cd1
jmp 0x6481
movq -0x178(%rbp), %rdi
callq 0x34f0
movq %rax, %rdi
callq 0x81e0
nopw (%rax,%rax)