// File: /workspace/github_cpp_makefile_star2_O2/nvanbenschoten[P]total-order-multicast/src/udp_conn.cc
// Function: udp::Client::SendWithAck(char const*, unsigned long, unsigned int, std::function<udp::ServerAction (std::shared_ptr<udp::Client>, char*, unsigned long)>)
void Client::SendWithAck(const char *buf, size_t size, unsigned int attempts,
                         OnReceiveFn validAck) {
  std::lock_guard<std::recursive_mutex> guard(sockfd_mutex_);
  unsigned int backoff_mult = 1;
  bool noLimit = attempts == kUnlimitedAttempts;
  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()) {
        // Wait for a backoff period.
        WaitBackoff(backoff_mult);
        if (backoff_mult < kMaxBackoffMultiplier) {
          backoff_mult <<= 1;
        }
        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 $0x458, %rsp            # imm = 0x458
movq %r8, 0x18(%rsp)
movl %ecx, %ebp
movq %rdx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
addq $0x10, %rdi
movq %rdi, 0x10(%rsp)
callq 0x10c9a
pushq $0x1
popq %rbx
leaq 0x30(%rsp), %r14
movl %ebp, 0x8(%rsp)
cmpl $0x0, 0x8(%rsp)
sete %al
addl $-0x1, %ebp
setb %cl
orb %al, %cl
testb $0x1, %cl
je 0x105ed
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x10444
movl $0x400, %edx            # imm = 0x400
leaq 0x50(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x51f0
movl $0x10, 0xc(%rsp)
movl 0x38(%r13), %edi
movl $0x400, %edx            # imm = 0x400
movq %r15, %rsi
xorl %ecx, %ecx
leaq 0x40(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x52a0
movq %rax, %r15
testl %r15d, %r15d
js 0x105d0
movq %r14, %r12
movq %r14, %rdi
movq %r13, %rsi
callq 0x10cae
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rdi
movq %r12, %rsi
leaq 0x50(%rsp), %rdx
movq %r15, %rcx
callq 0x10a82
movl %eax, %r15d
leaq 0x38(%rsp), %rdi
callq 0x708e
cmpl $0x1, %r15d
jne 0x10521
jmp 0x105ed
callq 0x10a15
testb %al, %al
je 0x10609
movl %ebx, %esi
callq 0x1066c
cmpl $0x40, %ebx
setb %cl
shll %cl, %ebx
jmp 0x10521
movq 0x10(%rsp), %rdi
callq 0x5290
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x180, %edi            # imm = 0x180
callq 0x51b0
movq %rax, %r14
movq %rax, %rdi
callq 0x10a2e
leaq 0xb60b(%rip), %rsi      # 0x1bc30
leaq 0x52c(%rip), %rdx       # 0x10b58
movq %r14, %rdi
callq 0x55f0
jmp 0x10656
movq %rax, %rbx
movq %r14, %rdi
callq 0x5270
jmp 0x10659
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x708e
jmp 0x10659
jmp 0x10656
jmp 0x10656
movq %rax, %rbx
movq 0x10(%rsp), %rdi
callq 0x5290
movq %rbx, %rdi
callq 0x5620
nop