// File: /workspace/github_cpp_makefile_star2_O1/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 0x2b29
movq %rax, %r15
movq %rdx, %r12
movq 0x5487(%rip), %r13      # 0x81d0
movw $0xd903, %bp            # imm = 0xD903
testq %r13, %r13
je 0x2dfb
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 0x2900
movzwl 0x10(%rsp), %eax
movw %ax, 0x6(%rsp)
leaq 0x545a(%rip), %rdi      # 0x81f8
movq %r14, %rsi
callq 0x4664
movq (%rax), %rdi
movq 0x8(%rax), %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x2bba
testb %al, %al
je 0x2dd7
movzwl 0x6(%rsp), %ebp
leaq 0x5430(%rip), %rdi      # 0x81f8
movq %r14, %rsi
callq 0x4664
movq (%rax), %r15
movq 0x8(%rax), %r12
movq 0x20(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2dee
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
movq (%r13), %r13
testq %r13, %r13
jne 0x2d60
movq %r15, %rdi
movq %r12, %rsi
callq 0x2b48
orq %rax, %rdx
jne 0x2e1c
movw %bp, 0x541e(%rip)       # 0x8230
movl $0x3, %ebx
jmp 0x2eb7
movq %r15, %rdi
movq %r12, %rsi
callq 0x2b48
movl 0x8(%rsp), %r14d
movl %r14d, %edi
movq %rax, %rsi
callq 0x2af5
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x273c
movq %rbx, %rdi
movl %r14d, %esi
movq 0x40(%rsp), %rdx
movl 0xc(%rsp), %ecx
callq 0x2898
cmpl $0x1, %eax
jne 0x2e90
movzwl 0x12(%rsp), %edi
callq 0x2bd0
leaq 0x10(%rsp), %rdi
callq 0x28fe
movzwl 0x12(%rsp), %eax
movzwl 0x14(%rsp), %ecx
movw %ax, 0x5330(%rip)       # 0x81b2
movw %cx, 0x532b(%rip)       # 0x81b4
movl $0x1, %ebx
jmp 0x2e9c
movw %bp, 0x5399(%rip)       # 0x8230
movl $0x2, %ebx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2eb7
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 0x2efa
movq 0x28(%rsp), %rsi
jmp 0x2ef2
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2efa
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %r14, %rdi
callq 0x22d0