// File: /workspace/github_cpp_makefile_star2_O1/nvanbenschoten[P]byzantine-agreement/src/udp_conn.cc
// Function: udp::Client::SendWithAck(char const*, unsigned long, unsigned int, std::function<udp::ServerAction (std::shared_ptr<udp::Client const>, char*, unsigned long)>) const
void Client::SendWithAck(const char *buf, size_t size, unsigned int attempts,
                         OnReceiveFn validAck) const {
  bool noLimit = attempts == 0;
  for (; noLimit || attempts > 0; --attempts) {
    // Send the message to the client.
    Send(buf, size);

    // Create a zeroed out buffer to read the ack message into.
    char ackbuf[BUFSIZE];
    bzero(ackbuf, BUFSIZE);

    // receive from the socket.
    struct sockaddr_in clientaddr;
    socklen_t clientlen = sizeof(clientaddr);
    int n = recvfrom(sockfd_, ackbuf, BUFSIZE, 0,
                     (struct sockaddr *)&clientaddr, &clientlen);

    // Check for error cases. This is either a timeout or some kind of
    // networking error. For timeouts, try sending the message again. For
    // anything else, throw an exception.
    if (n < 0) {
      if (IsErrnoTimeout()) {
        continue;
      } else {
        throw net::ReceiveException();
      }
    }

    // Make sure the ack was valid.
    auto action = validAck(shared_from_this(), ackbuf, n);
    if (action == ServerAction::Stop) {
      return;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
movq %r8, 0x10(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
movq %rcx, 0x18(%rsp)
leal -0x1(%rcx), %r12d
leaq 0x60(%rsp), %rbx
leaq 0x40(%rsp), %rbp
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x1055a
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x51f0
movl $0x10, 0xc(%rsp)
movl 0x10(%r13), %edi
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rsi
xorl %ecx, %ecx
leaq 0x50(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x5290
movq %rax, %r15
testl %r15d, %r15d
js 0x1069c
movq %rbp, %r14
movq %rbp, %rdi
movq %r13, %rsi
callq 0x10dc8
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %rbx, 0x38(%rsp)
movq %r15, 0x30(%rsp)
movq 0x10(%rsp), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x106ee
movq %r14, %rsi
leaq 0x38(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq *0x18(%rdi)
movl %eax, %r15d
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x10691
callq 0x100e8
xorl %eax, %eax
cmpl $0x1, %r15d
sete %al
jmp 0x106b9
callq 0x5040
movl (%rax), %ecx
movl $0x4, %eax
cmpl $0xb, %ecx
je 0x106b9
cmpl $0x6f, %ecx
leaq 0x60(%rsp), %rbx
je 0x106be
jmp 0x106f3
leaq 0x60(%rsp), %rbx
testb $0x3, %al
jne 0x106dc
addl $-0x1, %r12d
setb %al
cmpl $0x0, 0x18(%rsp)
je 0x105f6
testb %al, %al
jne 0x105f6
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5140
movl $0x180, %edi            # imm = 0x180
callq 0x51a0
movq %rax, %r14
movq %rax, %rdi
callq 0x10b00
leaq 0xf329(%rip), %rsi      # 0x1fa38
leaq 0x4ce(%rip), %rdx       # 0x10be4
movq %r14, %rdi
callq 0x55d0
movq %rax, %rbx
movq %r14, %rdi
callq 0x5270
jmp 0x1073f
jmp 0x1072d
movq %rax, %rbx
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x1073f
callq 0x100e8
movq %rbx, %rdi
callq 0x5600
nop