// File: /workspace/github_cpp_makefile_star2_O2/m1c0l[P]TCP/server.cpp
// Function: main
int main(int argc, char **argv) {
	if (argc != 3) {
		cerr << "usage: " << argv[0] << " PORT-NUMBER FILENAME" << '\n';
		return 1;
	}

	string port = argv[1];
	string filename = argv[2];

	srand (time(NULL)); //Used to generate random ISN

	//Create UDP socket
	int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd == -1) {
		perror("socket");
		return 1;
	}

	//Timeout flags and stuff could be set here
	int yes = 1;
	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
		perror("setsockopt");
		return 1;
	}


	sockaddr_in addr, other;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(stoi(port));
	addr.sin_addr.s_addr = htonl(INADDR_ANY);//inet_addr("127.0.0.1");

	memset(addr.sin_zero, '\0', sizeof(addr.sin_zero));

	if (bind(sockfd, (sockaddr*) &addr, sizeof(addr)) == -1) {
		perror("bind");
		return 2;
	}

	socklen_t other_length = sizeof(other);
	TcpMessage received;
	TcpMessage toSend;
	bool hasReceivedSyn = false;
	bool sendFile = false;
	uint16_t seqToSend = rand() % MAX_SEQ_NUM; //The first sync
	uint16_t ackToSend;
	uint16_t ssThresh = MAX_SEQ_NUM;
	ifstream wantedFile;
	string flagsToSend = "";
	for (int packetsSent = 0; true; packetsSent++) {
	    // cerr << "Waiting for something" << endl;
		int r = received.recvfrom(sockfd, &other, other_length);
		if (r == RECV_TIMEOUT) {
			if (hasReceivedSyn) { // client ACK not received; resend SYN-ACK
				printSend("SYN", toSend.seqNum, DATA_SIZE, DATA_SIZE, true);
				toSend.sendto(sockfd, &other, other_length);
				// cerr << "Sending SYN-ACK (retransmit):" << endl;
				toSend.dump();
			}
			continue;
		}

		// cerr << "Packet arrived from" << inet_ntoa(addr.sin_addr)<< ": " << ntohs(other.sin_port) << endl;
		// cerr << "Received:" << endl;
		received.dump();

		clientRecvWindow = received.recvWindow;

		if (!packetsSent) {
			// Set receive timeout of 0.5s
			// Only want to run this code once
			timeval recvTimeval;
			recvTimeval.tv_sec = 0;
			recvTimeval.tv_usec = TIMEOUT * 1000;
			setSocketTimeout(sockfd, recvTimeval);
		}


		//Send SYN-ACK if client is trying to set up connection

		switch (received.flags) {
		case SYN_FLAG:
			printRecv(received.ackNum);
		    if (hasReceivedSyn) {
				// cerr << "Got another SYN, sending SYN-ACK\n";
			}
		    flagsToSend ="SA";
			printSend("SYN", seqToSend, DATA_SIZE, DATA_SIZE, hasReceivedSyn);
		    hasReceivedSyn = true;

		    break;

		case SYN_FLAG | ACK_FLAG:
		    // cerr << "Both SYN and ACK were set by client\n";
		    //exit(1);
		    break;

		case ACK_FLAG:
			printRecv(received.ackNum);
		    if (!hasReceivedSyn) {
				// cerr << "ACK before handshake\n";
				break;
			}
		    flagsToSend = "A";
		    //Time to start sending the file back
		    sendFile = true;

		    break;

		default:
		    // cerr << "Incorrect flags set\n";
		    break;
		}

		// if we're done handshake and are ready to send the file now
		if (sendFile) {
			break;
		}

		ackToSend = incSeqNum(received.seqNum, 1);
		toSend = TcpMessage(seqToSend, ackToSend, clientRecvWindow, flagsToSend);
		toSend.sendto(sockfd, &other, other_length);
		// cerr << "Handshake: sending packet\n";
		toSend.dump();
	}

	char filebuf[DATA_SIZE];
	wantedFile.open(filename);
	if (!wantedFile) {
		perror("fstream open");
		exit(1);
	}

	// if the client sent data, increment ack by that number
	size_t recvLength = received.data.length();
	ackToSend = incSeqNum(received.seqNum, recvLength);
	// increment our sequence number by 1
	seqToSend = incSeqNum(seqToSend, 1);

	double cwnd = DATA_SIZE;
	uint16_t cwndBot = seqToSend;
	uint16_t cwndTop = seqToSend + cwnd;
	bool useSlowStart = true;
	int filepkts = 0;

	uint16_t lastAckExpected = BAD_SEQ_NUM;
	lastAckRecvd = seqToSend;



	/* data */

	while(true) {

		// update receiver/congestion windows
		rwndBot = lastAckRecvd;
		rwndTop = incSeqNum(rwndBot, clientRecvWindow);
		cwndBot = lastAckRecvd;
		cwndTop = incSeqNum(cwndBot, cwnd);

		// remove old packets that are outside receiver window
		auto it = packetsInWindow.begin(); // iterator
		while (it != packetsInWindow.end()) {
			auto curr = it;
			it++;
			uint16_t currSeq = curr->second.seqNum;
			if (!inWindow(currSeq, rwndBot, rwndTop)) {
				packetsInWindow.erase(curr);
			}
		}

		// create new packets
		uint16_t seqToSendEnd = incSeqNum(seqToSend, DATA_SIZE);
		// stop if next packet will be outside rwnd or cwnd
		while (inWindow(seqToSendEnd, cwndBot, cwndTop) &&
			   lastAckExpected == BAD_SEQ_NUM) {

			memset(filebuf, 0, DATA_SIZE);
			wantedFile.read(filebuf, DATA_SIZE);
			streamsize dataSize = wantedFile.gcount();
			toSend = TcpMessage(seqToSend, ackToSend, clientRecvWindow, "A");
			toSend.data = string(filebuf, dataSize);

			// if already in map, we're retransmitting
			bool isRetransmit = packetsInWindow.count(toSend.seqNum);
			// store packet in case retransmission needed
			packetsInWindow[toSend.seqNum] = toSend;
			timestamps[toSend.seqNum] = now();

			seqToSend = incSeqNum(seqToSend, dataSize);
			seqToSendEnd = incSeqNum(seqToSend, DATA_SIZE);

			// send packet
			if (dataSize > 0) {
				printSend("data", toSend.seqNum, cwnd, ssThresh, isRetransmit);
				// cerr << "sending packet " << filepkts << " of file: " << endl;
				toSend.dump();
				toSend.sendto(sockfd, &other, other_length);
				filepkts++;
			}
			// no more data; this packet's seq should be the last ACK
			else {
				lastAckExpected = seqToSend;
				break;
			}

		}

	    int r = getAcks(sockfd, &other, other_length);

		// a old packet timed out; resend it
		if (r == RECV_TIMED_OUT_ALREADY) {
			// no packets in window
			if (timedOutSeq == BAD_SEQ_NUM) {
				// cerr << "!!!!!!!!!!!! no packets in window" << endl;
			}

			// cerr << "retransmit (timed out already)" << endl;
			packetsInWindow[timedOutSeq].dump();
			packetsInWindow[timedOutSeq].sendto(sockfd, &other, other_length);
			timestamps[timedOutSeq] = now();
		}

		// no ACK received
		else if (r == RECV_TIMEOUT) {
			// retransmit last ACKed packet
			if (packetsInWindow.count(lastAckRecvd) == 0) {
				// cerr << "!!!!! packet not in window: " << lastAckRecvd << endl;
				// cerr << "!!!!! should never reach this" << endl;
				for (auto i : packetsInWindow) {
					// cerr << i.first << " ";
				}
				// cerr << endl;
				if (packetsInWindow.size() == 0) {
					// cerr << "packetsInWindow is empty!" << endl;
				}
				continue;
			}
			// cerr << "retransmitting (timeout)" << endl;
			printSend("data", packetsInWindow[lastAckRecvd].seqNum, cwnd, ssThresh, true);
			packetsInWindow[lastAckRecvd].dump();
			packetsInWindow[lastAckRecvd].sendto(sockfd, &other, other_length);
			timestamps[lastAckRecvd] = now();

			// update congestion control
			ssThresh = cwnd / 2;
			if (ssThresh < DATA_SIZE) {
				ssThresh = DATA_SIZE;
			}
			cwnd = DATA_SIZE;
			useSlowStart = true;
		}
		// got an ACK
		else {
			// check if we are done
			// this can only be true after the last packet is sent; otherwise,
			// lastAckExpected has a impossible value
			if (lastAckRecvd == lastAckExpected) {
				break;
			}

			// fast retransmit
			if (lastAckRecvd == previousAck) {
				acksInARow++; 
				if (acksInARow == 4) {
					// cerr << "retransmitting (fast retransmit)" << lastAckRecvd << endl;
					if (packetsInWindow.count(lastAckRecvd) == 0) {
						// cerr << "!!!!! packet not in window: " << lastAckRecvd << endl;
						// cerr << "!!!!! should never reach this" << endl;
						for (auto i : packetsInWindow) {
							// cerr << i.first << " ";
						}
						// cerr << endl;
						if (packetsInWindow.size() == 0) {
							// cerr << "packetsInWindow is empty!" << endl;
						}
						continue;
					}
					printSend("data", packetsInWindow[lastAckRecvd].seqNum, cwnd, ssThresh, true);
					packetsInWindow[lastAckRecvd].dump();
					packetsInWindow[lastAckRecvd].sendto(sockfd, &other, other_length);
					timestamps[lastAckRecvd] = now();
					acksInARow = 0;
					//fast recovery
					//should actually be same logic as timeout retransmit, just without setting useSlowStart
					ssThresh = (cwnd/2 < DATA_SIZE) ? DATA_SIZE : cwnd/2;
					cwnd = ssThresh > clientRecvWindow ? clientRecvWindow : ssThresh;
					useSlowStart = false;
					previousAck = lastAckRecvd;
					continue;
					
				}
			}
			previousAck = lastAckRecvd;
			
			// check if all packets in window were cumulatively ACKed
			if (lastAckRecvd == seqToSend) {
				// cerr << "!!!!!!!!! all packets ACKed" << endl;
			}

			// update congestion control
			if (useSlowStart) {
				cwnd = cwnd + DATA_SIZE;
			}
			else {
				cwnd = cwnd + DATA_SIZE * DATA_SIZE /cwnd;
			}
			if (cwnd > clientRecvWindow) {
				cwnd = clientRecvWindow;
			}
			if (cwnd >= ssThresh) {
				useSlowStart = false;
			}
		}
	}


	// cerr << "out of loop\n";


	/* FIN */

	// reset to 0.5s timeout
	timeval recvTimeval;
	recvTimeval.tv_sec = 0;
	recvTimeval.tv_usec = TIMEOUT * 1000;
	setSocketTimeout(sockfd, recvTimeval);

	bool hasReceivedFin = false;
	bool hasReceivedFinAck = false;
	bool hasSentFin = false;
	bool hasSentAckFin = false;
	int timedWaitTimer = 2 * MAX_SEG_LIFETIME; // milliseconds to wait before closing

	while (timedWaitTimer > 0) {

		/* send FIN if no FIN-ACK received yet */
		if (!hasReceivedFinAck) {
			flagsToSend = "F";
			// Sequence number shouldn't change since we already increased by total data size
			ackToSend = 0; // ACK is invalid this packet
			toSend = TcpMessage(seqToSend, ackToSend, clientRecvWindow, flagsToSend);
			toSend.sendto(sockfd, &other, other_length);
			printSend("FIN", seqToSend, DATA_SIZE, ssThresh, hasSentFin);
			// cerr << "Sending FIN\n";
			toSend.dump();
			hasSentFin = true;
		}

		/* timed wait timer counts down after FIN received */
		if (hasReceivedFin) {
			timedWaitTimer -= TIMEOUT; // wait TIMEOUT msec each loop
		}

		int r = received.recvfrom(sockfd, &other, other_length);
		if (r == RECV_TIMEOUT) {
			// cerr << "Timeout while waiting for FIN-ACK/FIN\n";
			continue;
		}
		clientRecvWindow = received.recvWindow;

		switch(received.flags) {
			// FIN-ACK
			case FIN_FLAG | ACK_FLAG:
				printRecv(received.ackNum);
				// cerr << "Received FIN-ACK\n";
				hasReceivedFinAck = true;
				break;

			// FIN
			case FIN_FLAG:
				printRecv(received.ackNum);
				// cerr << "Received FIN\n";

				flagsToSend = "A";// this is "FIN-ACK" but without FIN flag
				seqToSend = incSeqNum(seqToSend, 1);// increase sequence number by 1
				ackToSend = incSeqNum(received.seqNum, 1); // increase ack by 1
				toSend = TcpMessage(seqToSend, ackToSend, clientRecvWindow, flagsToSend);
				toSend.sendto(sockfd, &other, other_length);
				printSend("data", seqToSend, DATA_SIZE, ssThresh, hasSentAckFin);
				// cerr << "Sending ACK of FIN\n";
				toSend.dump();
				// cerr << "Server received FIN; starting timed wait..." << endl;
				hasReceivedFinAck = true; // assume that client got a FIN
				hasReceivedFin = true;
				hasSentAckFin = true;
				break;

			default:
				// cerr << "Received packet wasn't FIN-ACK or FIN!\n";
				//exit(1);
				break;
		}
		received.dump();
	}

	// cerr << "Shouldn't receive anything else from client now\n";

	close(sockfd);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8a8, %rsp            # imm = 0x8A8
movq %rsi, %rbx
cmpl $0x3, %edi
jne 0x3002
movq 0x8(%rbx), %rsi
leaq 0x108(%rsp), %rdi
leaq 0x4a0(%rsp), %rdx
callq 0x2810
movq 0x10(%rbx), %rsi
leaq 0xe8(%rsp), %rdi
leaq 0x4a0(%rsp), %rdx
callq 0x2810
xorl %edi, %edi
callq 0x21b0
movl %eax, %edi
callq 0x21c0
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x2070
cmpl $-0x1, %eax
je 0x3a18
leaq 0xe4(%rsp), %rcx
movl $0x1, (%rcx)
movl %eax, %edi
pushq $0x1
popq %rbp
pushq $0x2
popq %rbx
pushq $0x4
popq %r8
movl %eax, (%rsp)
movl %ebp, %esi
movl %ebx, %edx
callq 0x2300
cmpl $-0x1, %eax
je 0x3a29
movw $0x2, 0x128(%rsp)
leaq 0x108(%rsp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x3c05
rolw $0x8, %ax
leaq 0x128(%rsp), %rsi
movw %ax, 0x2(%rsi)
andl $0x0, 0x4(%rsi)
andq $0x0, 0x8(%rsi)
pushq $0x10
popq %rdx
movl (%rsp), %edi
callq 0x2060
cmpl $-0x1, %eax
je 0x3a37
leaq 0x40(%rsp), %rdi
callq 0x2642
leaq 0x68(%rsp), %rdi
callq 0x2642
callq 0x2090
movl $0x7800, %ecx           # imm = 0x7800
cltd
idivl %ecx
movl %edx, %ebx
leaq 0x298(%rsp), %rdi
callq 0x2200
leaq 0x2290(%rip), %rsi      # 0x50a6
leaq 0x90(%rsp), %rdi
leaq 0x4a0(%rsp), %rdx
callq 0x2810
xorl %r15d, %r15d
leaq 0x4a0(%rsp), %r13
leaq 0x238(%rsp), %rbp
movzwl %bx, %eax
movl %eax, 0x4(%rsp)
xorl %ebx, %ebx
leaq 0x40(%rsp), %rdi
movl (%rsp), %esi
leaq 0x2c(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x27a0
cmpl $0x2, %eax
jne 0x2ecb
testb $0x1, %bl
je 0x2ffa
leaq 0x278(%rsp), %r14
movq %r14, %rdi
leaq 0x224d(%rip), %rsi      # 0x50c9
movq %r13, %rdx
callq 0x2810
movzwl 0x68(%rsp), %esi
movq %r14, %rdi
movl $0x400, %edx            # imm = 0x400
movl $0x400, %ecx            # imm = 0x400
pushq $0x1
popq %r8
callq 0x2ab8
movq %r14, %rdi
callq 0x20f0
leaq 0x68(%rsp), %rdi
movl (%rsp), %esi
leaq 0x2c(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x2744
leaq 0x68(%rsp), %rdi
callq 0x280e
jmp 0x2ffa
leaq 0x40(%rsp), %rdi
callq 0x280e
movzwl 0x44(%rsp), %eax
movw %ax, 0x52eb(%rip)       # 0x81cc
testl %r15d, %r15d
jne 0x2ef5
movl $0x7a120, %edx          # imm = 0x7A120
movl (%rsp), %edi
xorl %esi, %esi
callq 0x29af
movzwl 0x46(%rsp), %eax
cmpl $0x4, %eax
je 0x2f66
cmpl $0x2, %eax
jne 0x2f79
movzwl 0x42(%rsp), %edi
callq 0x2a8c
leaq 0x90(%rsp), %rdi
leaq 0x21b0(%rip), %rsi      # 0x50cd
callq 0x2320
leaq 0x258(%rsp), %r14
movq %r14, %rdi
leaq 0x2195(%rip), %rsi      # 0x50c9
movq %r13, %rdx
callq 0x2810
movzbl %bl, %r8d
andl $0x1, %r8d
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl $0x400, %edx            # imm = 0x400
movl $0x400, %ecx            # imm = 0x400
callq 0x2ab8
movq %r14, %rdi
callq 0x20f0
movb $0x1, %bl
jmp 0x2f79
movzwl 0x42(%rsp), %edi
callq 0x2a8c
testb $0x1, %bl
jne 0x3042
movzwl 0x40(%rsp), %edi
pushq $0x1
popq %rsi
callq 0x28e4
movl %eax, %r14d
movzwl 0x523b(%rip), %r12d     # 0x81cc
movq %rbp, %rdi
leaq 0x90(%rsp), %rsi
callq 0x2270
movzwl %r14w, %edx
movzwl %r12w, %ecx
movq %r13, %rdi
movl 0x4(%rsp), %esi
movq %rbp, %r8
callq 0x252c
leaq 0x68(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x3c22
leaq 0x4a8(%rsp), %rdi
callq 0x20f0
movq %rbp, %rdi
callq 0x20f0
movq %r14, %rdi
movl (%rsp), %esi
leaq 0x2c(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x2744
leaq 0x68(%rsp), %rdi
callq 0x280e
decl %r15d
jmp 0x2e47
movq 0x4fe7(%rip), %rdi      # 0x7ff0
leaq 0x208f(%rip), %rsi      # 0x509f
callq 0x21d0
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x21d0
leaq 0x2080(%rip), %rsi      # 0x50a7
movq %rax, %rdi
callq 0x21d0
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x2210
pushq $0x1
popq %rbp
jmp 0x3a04
leaq 0x2085(%rip), %rsi      # 0x50ce
leaq 0x90(%rsp), %rdi
callq 0x2320
leaq 0x298(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x2180
movq 0x298(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x2b8(%rsp,%rax)
jne 0x3a47
movzwl 0x50(%rsp), %esi
movzwl 0x40(%rsp), %edi
callq 0x28e4
movl %eax, %ebx
pushq $0x1
popq %rsi
movl 0x4(%rsp), %edi
callq 0x28e4
movw %ax, 0x511e(%rip)       # 0x81ca
movw $0x7800, %cx            # imm = 0x7800
movl %ecx, 0x24(%rsp)
movsd 0x1f94(%rip), %xmm0     # 0x5050
movb $0x1, %cl
movl %ecx, 0x14(%rsp)
movw $0xd903, %bp            # imm = 0xD903
movzwl %bx, %ecx
movl %ecx, 0x3c(%rsp)
movsd %xmm0, 0x18(%rsp)
leaq 0x68(%rsp), %rbx
movl %eax, %r14d
movq %rbx, %r13
movw %ax, 0x50e9(%rip)       # 0x81ce
movzwl 0x50e0(%rip), %esi      # 0x81cc
movzwl %ax, %edi
callq 0x28e4
movw %ax, 0x50d5(%rip)       # 0x81d0
cvttsd2si 0x18(%rsp), %eax
movzwl 0x50c2(%rip), %edi      # 0x81ca
movl %eax, 0x8(%rsp)
movzwl %ax, %esi
movl %edi, 0x4(%rsp)
callq 0x28e4
movl %eax, %r12d
movq 0x50c6(%rip), %rbx      # 0x81e8
testq %rbx, %rbx
je 0x315a
movq %rbx, %r15
movq (%rbx), %rbx
movzwl 0x10(%r15), %edi
movzwl 0x5095(%rip), %esi      # 0x81ce
movzwl 0x5090(%rip), %edx      # 0x81d0
callq 0x28f3
testb %al, %al
jne 0x3122
leaq 0x5088(%rip), %rdi      # 0x81d8
movq %r15, %rsi
callq 0x4226
jmp 0x3122
movzwl %r14w, %edi
movl $0x400, %esi            # imm = 0x400
callq 0x28e4
movl %eax, %r15d
movzwl 0x24(%rsp), %eax
movl %eax, 0xc(%rsp)
movzwl %r12w, %eax
movl %eax, 0x10(%rsp)
movzwl %r15w, %edi
movl 0x4(%rsp), %esi
movl 0x10(%rsp), %edx
callq 0x28f3
cmpw $0xd903, %bp            # imm = 0xD903
sete %cl
andb %cl, %al
cmpb $0x1, %al
jne 0x335a
movl $0x400, %edx            # imm = 0x400
leaq 0x4a0(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2110
movl $0x400, %edx            # imm = 0x400
leaq 0x298(%rsp), %rdi
movq %rbx, %rsi
callq 0x2240
movq 0x2a0(%rsp), %rbx
movzwl 0x4ff1(%rip), %r15d     # 0x81cc
leaq 0x218(%rsp), %r12
movq %r12, %rdi
leaq 0x1ee1(%rip), %rsi      # 0x50ce
leaq 0x2b(%rsp), %rdx
callq 0x2810
movzwl %r14w, %ebp
movzwl %r15w, %ecx
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
movl %ebp, %esi
movl 0x3c(%rsp), %edx
movq %r12, %r8
callq 0x252c
movq %r13, %r15
movq %r13, %rdi
movq %r14, %rsi
callq 0x3c22
leaq 0xb8(%rsp), %rdi
callq 0x20f0
movq %r12, %rdi
callq 0x20f0
leaq 0xc0(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq (%rsp,%rbx), %rdx
addq $0x4a0, %rdx            # imm = 0x4A0
movq %r14, %rdi
leaq 0x4a0(%rsp), %rsi
callq 0x284c
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x2230
movq %r14, %rdi
callq 0x20f0
leaq 0x4f56(%rip), %rdi      # 0x81d8
movq %r15, %rsi
callq 0x432a
movq %rax, %r12
leaq 0x4f44(%rip), %rdi      # 0x81d8
movq %r13, %rsi
callq 0x43be
movq %rax, %rdi
movq %r13, %rsi
callq 0x3c3e
callq 0x29ec
movq %rax, %r15
movq %rdx, %r14
leaq 0x4f57(%rip), %rdi      # 0x8210
movq %r13, %rsi
callq 0x3eb0
movq %r15, (%rax)
movq %r14, 0x8(%rax)
movzwl %bx, %esi
movl %ebp, %edi
callq 0x28e4
movl %eax, %r14d
movzwl %ax, %edi
movl $0x400, %esi            # imm = 0x400
callq 0x28e4
testq %rbx, %rbx
jle 0x3357
movl %eax, %r15d
leaq 0x1f8(%rsp), %rbp
movq %rbp, %rdi
leaq 0x1d9e(%rip), %rsi      # 0x509a
leaq 0xb0(%rsp), %rdx
callq 0x2810
movq %r13, %rbx
xorl %r8d, %r8d
testq %r12, %r12
setne %r8b
movzwl 0x68(%rsp), %esi
movq %rbp, %rdi
movl 0x8(%rsp), %edx
movl 0xc(%rsp), %ecx
callq 0x2ab8
movq %rbp, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x280e
movw $0xd903, %bp            # imm = 0xD903
movq %r13, %rdi
movl (%rsp), %esi
leaq 0x2c(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x2744
jmp 0x317c
movl %r14d, %ebp
movq %r13, %rbx
movl (%rsp), %edi
leaq 0x2c(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x2b7f
leaq 0xb0(%rsp), %r15
cmpl $0x2, %eax
je 0x33ee
cmpl $0x3, %eax
jne 0x34fd
leaq 0x4e4e(%rip), %rdi      # 0x81d8
leaq 0x4eb7(%rip), %rsi      # 0x8248
callq 0x43be
movq %rax, %rdi
callq 0x280e
leaq 0x4e33(%rip), %rdi      # 0x81d8
leaq 0x4e9c(%rip), %rsi      # 0x8248
callq 0x43be
movq %rax, %rdi
movl (%rsp), %esi
leaq 0x2c(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x2744
callq 0x29ec
movq %rax, %r12
movq %rdx, %r15
leaq 0x4e3a(%rip), %rdi      # 0x8210
leaq 0x4e6b(%rip), %rsi      # 0x8248
callq 0x3eb0
movq %r12, (%rax)
movq %r15, 0x8(%rax)
jmp 0x370b
leaq 0x4de3(%rip), %rdi      # 0x81d8
leaq 0x4dce(%rip), %rsi      # 0x81ca
callq 0x432a
testq %rax, %rax
je 0x36b4
leaq 0x178(%rsp), %rdi
leaq 0x1c81(%rip), %rsi      # 0x509a
movq %r15, %rdx
callq 0x2810
leaq 0x4db0(%rip), %rdi      # 0x81d8
leaq 0x4d9b(%rip), %rsi      # 0x81ca
callq 0x43be
leaq 0x178(%rsp), %r15
movzwl (%rax), %esi
movq %r15, %rdi
movl 0x8(%rsp), %edx
movl 0xc(%rsp), %ecx
pushq $0x1
popq %r8
callq 0x2ab8
movq %r15, %rdi
callq 0x20f0
leaq 0x4d76(%rip), %rdi      # 0x81d8
leaq 0x4d61(%rip), %rsi      # 0x81ca
callq 0x43be
movq %rax, %rdi
callq 0x280e
leaq 0x4d5b(%rip), %rdi      # 0x81d8
leaq 0x4d46(%rip), %rsi      # 0x81ca
callq 0x43be
movq %rax, %rdi
movl (%rsp), %esi
leaq 0x2c(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x2744
callq 0x29ec
movq %rax, %r12
movq %rdx, %r15
leaq 0x4d62(%rip), %rdi      # 0x8210
leaq 0x4d15(%rip), %rsi      # 0x81ca
callq 0x3eb0
movq %r12, (%rax)
movq %r15, 0x8(%rax)
movsd 0x18(%rsp), %xmm0
mulsd 0x1b89(%rip), %xmm0     # 0x5058
cvttsd2si %xmm0, %ecx
cmpw $0x401, %cx             # imm = 0x401
movl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
movl %ecx, 0x24(%rsp)
movb $0x1, %al
movl %eax, 0x14(%rsp)
movsd 0x1b5e(%rip), %xmm0     # 0x5050
movsd %xmm0, 0x18(%rsp)
jmp 0x370b
movzwl 0x4cc6(%rip), %eax      # 0x81ca
cmpw %bp, %ax
je 0x3717
cmpw 0x4ca6(%rip), %ax       # 0x81ba
jne 0x365b
movzwl 0x4d29(%rip), %ecx      # 0x824a
incl %ecx
movw %cx, 0x4d20(%rip)       # 0x824a
cmpw $0x4, %cx
jne 0x365b
leaq 0x4c9d(%rip), %rdi      # 0x81d8
leaq 0x4c88(%rip), %rsi      # 0x81ca
callq 0x432a
testq %rax, %rax
je 0x36de
leaq 0x158(%rsp), %rdi
leaq 0x1b3b(%rip), %rsi      # 0x509a
movq %r15, %rdx
callq 0x2810
leaq 0x4c6a(%rip), %rdi      # 0x81d8
leaq 0x4c55(%rip), %rsi      # 0x81ca
callq 0x43be
leaq 0x158(%rsp), %r15
movzwl (%rax), %esi
movq %r15, %rdi
movl 0x8(%rsp), %edx
movl 0xc(%rsp), %ecx
pushq $0x1
popq %r8
callq 0x2ab8
movq %r15, %rdi
callq 0x20f0
leaq 0x4c30(%rip), %rdi      # 0x81d8
leaq 0x4c1b(%rip), %rsi      # 0x81ca
callq 0x43be
movq %rax, %rdi
callq 0x280e
leaq 0x4c15(%rip), %rdi      # 0x81d8
leaq 0x4c00(%rip), %rsi      # 0x81ca
callq 0x43be
movq %rax, %rdi
movl (%rsp), %esi
leaq 0x2c(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x2744
callq 0x29ec
movq %rax, %r12
movq %rdx, %r15
leaq 0x4c1c(%rip), %rdi      # 0x8210
leaq 0x4bcf(%rip), %rsi      # 0x81ca
callq 0x3eb0
movq %r12, (%rax)
movq %r15, 0x8(%rax)
andw $0x0, 0x4c3b(%rip)      # 0x824a
movsd 0x18(%rsp), %xmm1
mulsd 0x1a3b(%rip), %xmm1     # 0x5058
movsd 0x1a2b(%rip), %xmm0     # 0x5050
maxsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movzwl 0x4b98(%rip), %eax      # 0x81cc
cmpw %cx, %ax
movl %ecx, 0x24(%rsp)
cmovbl %eax, %ecx
movzwl %cx, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x18(%rsp)
movzwl 0x4b78(%rip), %eax      # 0x81ca
movw %ax, 0x4b61(%rip)       # 0x81ba
jmp 0x36aa
movw %ax, 0x4b58(%rip)       # 0x81ba
movsd 0x19e6(%rip), %xmm0     # 0x5050
testb $0x1, 0x14(%rsp)
jne 0x367f
movsd 0x19e7(%rip), %xmm0     # 0x5060
divsd 0x18(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movzwl 0x4b3c(%rip), %eax      # 0x81cc
cvtsi2sd %eax, %xmm2
minsd %xmm1, %xmm2
cvtsi2sdl 0xc(%rsp), %xmm0
movsd %xmm2, 0x18(%rsp)
ucomisd %xmm0, %xmm2
jb 0x370b
movl $0x0, 0x14(%rsp)
jmp 0x370b
leaq 0x4b2d(%rip), %rbx      # 0x81e8
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x3708
leaq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x3bf2
leaq 0xc0(%rsp), %rdi
callq 0x20f0
jmp 0x36bb
leaq 0x4b03(%rip), %rbx      # 0x81e8
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x3708
leaq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x3bf2
leaq 0xc0(%rsp), %rdi
callq 0x20f0
jmp 0x36e5
movq %r13, %rbx
movzwl 0x4ab8(%rip), %eax      # 0x81ca
jmp 0x30db
movl $0x7a120, %edx          # imm = 0x7A120
movl (%rsp), %edi
xorl %esi, %esi
callq 0x29af
movl $0x1d4c0, %ebp          # imm = 0x1D4C0
movl $0x0, 0x8(%rsp)
leaq 0x138(%rsp), %r13
movl $0x0, 0x10(%rsp)
xorl %r12d, %r12d
movzwl %r14w, %eax
movl %eax, 0x4(%rsp)
testl %ebp, %ebp
jle 0x39b2
testb $0x1, 0x10(%rsp)
jne 0x382c
leaq 0x90(%rsp), %rdi
leaq 0x196d(%rip), %rsi      # 0x50dd
callq 0x2320
movzwl 0x4a50(%rip), %ebx      # 0x81cc
movq %r13, %rdi
leaq 0x90(%rsp), %rsi
callq 0x2270
movzwl %bx, %ecx
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movl 0x4(%rsp), %esi
xorl %edx, %edx
movq %r13, %r8
callq 0x252c
leaq 0x68(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3c22
leaq 0xb8(%rsp), %rdi
callq 0x20f0
movq %r13, %rdi
callq 0x20f0
movq %r15, %rdi
movl (%rsp), %esi
leaq 0x2c(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x2744
leaq 0x1d8(%rsp), %r15
movq %r15, %rdi
leaq 0x18ed(%rip), %rsi      # 0x50df
movq %rbx, %rdx
callq 0x2810
movzbl %r12b, %r8d
andl $0x1, %r8d
movq %r15, %rdi
movl 0x4(%rsp), %esi
movl $0x400, %edx            # imm = 0x400
movl 0xc(%rsp), %ecx
callq 0x2ab8
movq %r15, %rdi
callq 0x20f0
movb $0x1, %r12b
leaq 0x68(%rsp), %rdi
callq 0x280e
leaq 0x40(%rsp), %rdi
movl (%rsp), %esi
leaq 0x2c(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x27a0
leal -0x1f4(%rbp), %ecx
testb $0x1, 0x8(%rsp)
cmovnel %ecx, %ebp
cmpl $0x2, %eax
je 0x374e
movzwl 0x44(%rsp), %eax
movw %ax, 0x4968(%rip)       # 0x81cc
movzwl 0x46(%rsp), %eax
cmpl $0x1, %eax
je 0x388c
cmpl $0x5, %eax
jne 0x39a3
movb $0x1, %al
movl %eax, 0x10(%rsp)
movzwl 0x42(%rsp), %edi
callq 0x2a8c
jmp 0x39a3
movzwl 0x42(%rsp), %edi
callq 0x2a8c
leaq 0x90(%rsp), %rdi
leaq 0x1829(%rip), %rsi      # 0x50ce
callq 0x2320
movl 0x4(%rsp), %edi
pushq $0x1
popq %rsi
callq 0x28e4
movl %eax, %r14d
movzwl 0x40(%rsp), %edi
pushq $0x1
popq %rsi
callq 0x28e4
movl %eax, %r13d
movzwl 0x48fb(%rip), %r15d     # 0x81cc
leaq 0x1b8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x90(%rsp), %rsi
callq 0x2270
movzwl %r14w, %esi
movzwl %r13w, %edx
movzwl %r15w, %ecx
leaq 0xb0(%rsp), %rdi
movq %rdi, %r15
movl %esi, 0x4(%rsp)
movq %rbx, %r8
callq 0x252c
leaq 0x68(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x3c22
leaq 0xb8(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x20f0
movq %r13, %rdi
movl (%rsp), %esi
leaq 0x2c(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x2744
leaq 0x198(%rsp), %rbx
leaq 0x138(%rsp), %r13
movq %rbx, %rdi
leaq 0x173c(%rip), %rsi      # 0x509a
movq %r15, %rdx
callq 0x2810
movzbl 0x8(%rsp), %r8d
andl $0x1, %r8d
movq %rbx, %rdi
movl 0x4(%rsp), %esi
movl $0x400, %edx            # imm = 0x400
movl 0xc(%rsp), %ecx
callq 0x2ab8
movq %rbx, %rdi
callq 0x20f0
leaq 0x68(%rsp), %rdi
callq 0x280e
movb $0x1, %al
movl %eax, 0x8(%rsp)
movb $0x1, %al
movl %eax, 0x10(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x280e
jmp 0x3746
movl (%rsp), %edi
callq 0x2330
leaq 0x90(%rsp), %rdi
callq 0x20f0
leaq 0x298(%rsp), %rdi
callq 0x2080
leaq 0x70(%rsp), %rdi
callq 0x20f0
leaq 0x48(%rsp), %rdi
callq 0x20f0
xorl %ebp, %ebp
leaq 0xe8(%rsp), %rdi
callq 0x20f0
leaq 0x108(%rsp), %rdi
callq 0x20f0
movl %ebp, %eax
addq $0x8a8, %rsp            # imm = 0x8A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x169e(%rip), %rdi      # 0x50bd
callq 0x2170
pushq $0x1
popq %rbp
jmp 0x39ea
leaq 0x160e(%rip), %rdi      # 0x503e
callq 0x2170
jmp 0x39ea
leaq 0x1686(%rip), %rdi      # 0x50c4
callq 0x2170
movl %ebx, %ebp
jmp 0x39ea
leaq 0x1682(%rip), %rdi      # 0x50d0
callq 0x2170
pushq $0x1
popq %rdi
callq 0x2220
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
jmp 0x3b7b
jmp 0x3b88
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
jmp 0x3b7b
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
movq %rax, %rbx
jmp 0x3b98
movq %rax, %rbx
jmp 0x3ba5
movq %rax, %rbx
jmp 0x3baf
jmp 0x3ab3
movq %rax, %rbx
jmp 0x3bb9
movq %rax, %rbx
jmp 0x3bc6
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
jmp 0x3b7b
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
jmp 0x3b7b
jmp 0x3b88
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
jmp 0x3b7b
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
jmp 0x3b7b
jmp 0x3b88
jmp 0x3b88
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
jmp 0x3b7b
jmp 0x3b88
movq %rax, %rbx
leaq 0x278(%rsp), %rdi
jmp 0x3b7b
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
jmp 0x3b7b
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
jmp 0x3b7b
jmp 0x3b88
jmp 0x3b88
movq %rax, %rbx
leaq 0x218(%rsp), %rdi
callq 0x20f0
jmp 0x3b8b
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x20f0
leaq 0x298(%rsp), %rdi
callq 0x2080
leaq 0x70(%rsp), %rdi
callq 0x20f0
leaq 0x48(%rsp), %rdi
callq 0x20f0
leaq 0xe8(%rsp), %rdi
callq 0x20f0
leaq 0x108(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x22f0