// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, 0xc(%rsp)
movq %rsi, 0x40(%rsp)
movl %edi, 0x8(%rsp)
callq 0x2b3b
movq %rax, %r15
movq %rdx, %r12
movq 0x546b(%rip), %r13      # 0x81d0
movw $0xd903, %bp            # imm = 0xD903
testq %r13, %r13
je 0x2e17
leaq 0x30(%rsp), %rbx
leaq 0x6(%rsp), %r14
movzwl 0x8(%r13), %eax
movw %ax, 0x10(%rsp)
movq 0x10(%r13), %rax
movq %rax, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq 0x18(%r13), %rsi
movq 0x20(%r13), %rdx
addq %rsi, %rdx
leaq 0x20(%rsp), %rdi
callq 0x28fc
movzwl 0x10(%rsp), %eax
movw %ax, 0x6(%rsp)
leaq 0x543e(%rip), %rdi      # 0x81f8
movq %r14, %rsi
callq 0x4616
movq (%rax), %rdi
movq 0x8(%rax), %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x2bd6
testb %al, %al
je 0x2df3
movzwl 0x6(%rsp), %ebp
leaq 0x5414(%rip), %rdi      # 0x81f8
movq %r14, %rsi
callq 0x4616
movq (%rax), %r15
movq 0x8(%rax), %r12
movq 0x20(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2e0a
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
movq (%r13), %r13
testq %r13, %r13
jne 0x2d7c
movq %r15, %rdi
movq %r12, %rsi
callq 0x2b5a
orq %rax, %rdx
jne 0x2e38
movw %bp, 0x5402(%rip)       # 0x8230
movl $0x3, %ebx
jmp 0x2ed3
movq %r15, %rdi
movq %r12, %rsi
callq 0x2b5a
movl 0x8(%rsp), %r14d
movl %r14d, %edi
movq %rax, %rsi
callq 0x2b07
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x2738
movq %rbx, %rdi
movl %r14d, %esi
movq 0x40(%rsp), %rdx
movl 0xc(%rsp), %ecx
callq 0x2894
cmpl $0x1, %eax
jne 0x2eac
movzwl 0x12(%rsp), %edi
callq 0x2bec
leaq 0x10(%rsp), %rdi
callq 0x28fa
movzwl 0x12(%rsp), %eax
movzwl 0x14(%rsp), %ecx
movw %ax, 0x5314(%rip)       # 0x81b2
movw %cx, 0x530f(%rip)       # 0x81b4
movl $0x1, %ebx
jmp 0x2eb8
movw %bp, 0x537d(%rip)       # 0x8230
movl $0x2, %ebx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2ed3
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21d0
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2f16
movq 0x28(%rsp), %rsi
jmp 0x2f0e
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2f16
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %r14, %rdi
callq 0x22d0