// File: m1c0l[P]TCP/server.cpp
// Function: getAcks(int, sockaddr_in*, unsigned int)
int getAcks(int sockfd, sockaddr_in *si_other, socklen_t len) {
	// find the oldest ACK we are waiting for
	uint16_t oldestSeq = BAD_SEQ_NUM;
	timeval oldestTimeval = now();
	for (auto i : packetsInWindow) {
		uint16_t currSeq = i.first;
		// this packet is older
		if (timeval_cmp(timestamps[currSeq], oldestTimeval)) {
			oldestSeq = currSeq;
			oldestTimeval = timestamps[currSeq];
		}
	}
	// cerr << "oldest seq = " << oldestSeq << endl;

	// if this packet already timed out, just return
	timeval remaining = timeRemaining(oldestTimeval);
	if (remaining.tv_sec == 0 && remaining.tv_usec == 0) {
		timedOutSeq = oldestSeq;
		return RECV_TIMED_OUT_ALREADY;
	}

	// try to recv until this packet times out
	setSocketTimeout(sockfd, timeRemaining(oldestTimeval));

	TcpMessage ack;
	int r = ack.recvfrom(sockfd, si_other, len);
	if (r == RECV_SUCCESS) {
		printRecv(ack.ackNum);
		ack.dump();
		lastAckRecvd = ack.ackNum;
		clientRecvWindow = ack.recvWindow;
		return RECV_SUCCESS;
	}
	else { // RECV_TIMEOUT
		timedOutSeq = oldestSeq;
		return RECV_TIMEOUT;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movw $0xd903, -0x16(%rbp)    # imm = 0xD903
callq 0x31e0
movq %rax, -0x28(%rbp)
movq %rdx, -0x20(%rbp)
leaq 0xae11(%rip), %rax      # 0xe270
movq %rax, -0x30(%rbp)
leaq 0xae06(%rip), %rdi      # 0xe270
callq 0x5470
movq %rax, -0x38(%rbp)
leaq 0xadf6(%rip), %rdi      # 0xe270
callq 0x54a0
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x54d0
testb $0x1, %al
jne 0x3499
jmp 0x3590
leaq -0x38(%rbp), %rdi
callq 0x5500
movq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0x5530
movw -0x70(%rbp), %ax
movw %ax, -0x72(%rbp)
leaq 0xadeb(%rip), %rdi      # 0xe2a8
leaq -0x72(%rbp), %rsi
callq 0x5570
movq %rax, -0x128(%rbp)
jmp 0x34cf
movq -0x128(%rbp), %rax
movups (%rax), %xmm0
movaps %xmm0, -0x90(%rbp)
movups -0x28(%rbp), %xmm0
movaps %xmm0, -0xb0(%rbp)
movq -0x90(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq -0xa8(%rbp), %rcx
callq 0x32b0
movb %al, -0x129(%rbp)
jmp 0x3514
movb -0x129(%rbp), %al
testb $0x1, %al
jne 0x3520
jmp 0x3579
movw -0x72(%rbp), %ax
movw %ax, -0x16(%rbp)
leaq 0xad79(%rip), %rdi      # 0xe2a8
leaq -0x72(%rbp), %rsi
callq 0x5570
movq %rax, -0x138(%rbp)
jmp 0x3541
movq -0x138(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x28(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x3579
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x55a0
jmp 0x3725
leaq -0x70(%rbp), %rdi
callq 0x55a0
leaq -0x38(%rbp), %rdi
callq 0x55c0
jmp 0x3483
movq -0x28(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xd0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x3210
movq %rax, -0xc0(%rbp)
movq %rdx, -0xb8(%rbp)
cmpq $0x0, -0xc0(%rbp)
jne 0x35f2
cmpq $0x0, -0xb8(%rbp)
jne 0x35f2
movw -0x16(%rbp), %ax
movw %ax, 0xacfa(%rip)       # 0xe2e0
movl $0x3, -0x4(%rbp)
jmp 0x3719
movl -0x8(%rbp), %eax
movl %eax, -0x14c(%rbp)
movups -0x28(%rbp), %xmm0
movaps %xmm0, -0xf0(%rbp)
movq -0xf0(%rbp), %rdi
movq -0xe8(%rbp), %rsi
callq 0x3210
movl -0x14c(%rbp), %edi
movq %rax, -0xe0(%rbp)
movq %rdx, -0xd8(%rbp)
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
callq 0x3180
leaq -0x118(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x2860
movq -0x148(%rbp), %rdi
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
movl -0x14(%rbp), %ecx
callq 0x2bb0
movl %eax, -0x13c(%rbp)
jmp 0x3671
movl -0x13c(%rbp), %eax
movl %eax, -0x11c(%rbp)
cmpl $0x1, -0x11c(%rbp)
jne 0x36f1
movzwl -0x116(%rbp), %edi
callq 0x3300
jmp 0x3694
leaq -0x118(%rbp), %rdi
callq 0x2c60
jmp 0x36a2
movw -0x116(%rbp), %ax
movw %ax, 0xabb2(%rip)       # 0xe262
movw -0x114(%rbp), %ax
movw %ax, 0xaba6(%rip)       # 0xe264
movl $0x1, -0x4(%rbp)
movl $0x1, -0x120(%rbp)
jmp 0x370d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x55f0
jmp 0x3725
movw -0x16(%rbp), %ax
movw %ax, 0xabe4(%rip)       # 0xe2e0
movl $0x2, -0x4(%rbp)
movl $0x1, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x55f0
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x98(%rbp), %rdi
callq 0x23f0
nopw %cs:(%rax,%rax)