// File: /workspace/github_cpp_makefile_star2_O1/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 0x12860
leal -0x1(%r12), %r15d
movl $0x1, %ebx
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x12632
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 0x127d3
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x12ff0
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq 0x50(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %r13, 0x30(%rsp)
cmpq $0x0, 0x10(%r14)
je 0x12830
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 0x127c8
callq 0x78bc
xorl %eax, %eax
cmpl $0x1, %r13d
sete %al
jmp 0x127f8
callq 0x5040
movl (%rax), %eax
cmpl $0x6f, %eax
je 0x127e4
cmpl $0xb, %eax
jne 0x12835
movl %ebx, %esi
callq 0x128a6
cmpl $0x40, %ebx
setb %cl
shll %cl, %ebx
movl $0x4, %eax
testb $0x3, %al
jne 0x12814
addl $-0x1, %r15d
setb %al
testl %r12d, %r12d
je 0x12720
testb %al, %al
jne 0x12720
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 0x12d28
leaq 0x93d7(%rip), %rsi      # 0x1bc28
leaq 0x5b4(%rip), %rdx       # 0x12e0c
movq %r14, %rdi
callq 0x5610
movl %eax, %edi
callq 0x5230
jmp 0x12890
movq %rax, %rbx
movq %r14, %rdi
callq 0x5280
jmp 0x12893
jmp 0x12878
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x12893
callq 0x78bc
jmp 0x12893
jmp 0x12890
jmp 0x12890
movq %rax, %rbx
movq 0x8(%rsp), %rdi
callq 0x52a0
movq %rbx, %rdi
callq 0x5650
nop