// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movl %ecx, %r12d
movq %rdx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbp
addq $0x10, %rdi
movq %rdi, 0x8(%rsp)
callq 0x5510
testl %eax, %eax
jne 0x12680
leal -0x1(%r12), %r15d
movl $0x1, %ebx
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x1245e
movl $0x400, %edx            # imm = 0x400
leaq 0x50(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x51f0
movl $0x10, 0x4(%rsp)
movl 0x38(%rbp), %edi
movl $0x400, %edx            # imm = 0x400
movq %r13, %rsi
xorl %ecx, %ecx
leaq 0x40(%rsp), %r8
leaq 0x4(%rsp), %r9
callq 0x52b0
movq %rax, %r13
testl %r13d, %r13d
js 0x125fc
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x12e0e
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq 0x50(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %r13, 0x30(%rsp)
cmpq $0x0, 0x10(%r14)
je 0x12650
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x38(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq *0x18(%r14)
movl %eax, %r13d
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x125f4
callq 0x78ac
cmpl $0x1, %r13d
jne 0x1261c
jmp 0x12634
callq 0x5040
movl (%rax), %eax
cmpl $0x6f, %eax
je 0x1260d
cmpl $0xb, %eax
jne 0x12655
movl %ebx, %esi
callq 0x126c6
cmpl $0x40, %ebx
setb %cl
shll %cl, %ebx
addl $-0x1, %r15d
setb %al
testl %r12d, %r12d
je 0x1254c
testb %al, %al
jne 0x1254c
movq 0x8(%rsp), %rdi
callq 0x52a0
addq $0x458, %rsp            # imm = 0x458
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 0x12b46
leaq 0x95b7(%rip), %rsi      # 0x1bc28
leaq 0x5b2(%rip), %rdx       # 0x12c2a
movq %r14, %rdi
callq 0x5610
movl %eax, %edi
callq 0x5230
jmp 0x126b0
movq %rax, %rbx
movq %r14, %rdi
callq 0x5280
jmp 0x126b3
jmp 0x12698
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x126b3
callq 0x78ac
jmp 0x126b3
jmp 0x126b0
jmp 0x126b0
movq %rax, %rbx
movq 0x8(%rsp), %rdi
callq 0x52a0
movq %rbx, %rdi
callq 0x5650
nop