// File: /workspace/github_cpp_makefile_star2_O3/nvanbenschoten[P]total-order-multicast/src/udp_conn.cc
// Function: udp::Server::Listen(std::function<udp::ServerAction (std::shared_ptr<udp::Client>, char*, unsigned long)>, std::function<udp::ServerAction ()>) const
void Server::Listen(OnReceiveFn rcv, OnTimeout timeout) const {
  // While the server is running, wait for datagrams and
  // call the provided closure with their data.
  while (1) {
    // Create a zeroed out buffer to read the message into.
    char buf[BUFSIZE];
    bzero(buf, BUFSIZE);

    // Receive from the socket.
    struct sockaddr_in clientaddr;
    socklen_t clientlen = sizeof(clientaddr);
    int n = recvfrom(sockfd_, buf, BUFSIZE, 0, (struct sockaddr *)&clientaddr,
                     &clientlen);
    if (n < 0) {
      if (IsErrnoTimeout() && timeout != nullptr) {
        auto action = timeout();
        switch (action) {
          case ServerAction::Continue:
            continue;
          case ServerAction::Stop:
            return;
          default:
            throw std::invalid_argument("unexpected ServerAction value");
        }
      } else {
        throw net::ReceiveException();
      }
    }

    // Call closure with new client.
    auto client = std::make_shared<udp::Client>(clientaddr);

    // Call the receive callback with the data received.
    auto action = rcv(client, buf, 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 %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
leaq 0x50(%rsp), %r12
leaq 0x40(%rsp), %r13
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x51f0
movl $0x10, 0xc(%rsp)
movl (%r15), %edi
movl $0x400, %edx            # imm = 0x400
movq %r12, %rsi
xorl %ecx, %ecx
movq %r13, %r8
leaq 0xc(%rsp), %r9
callq 0x52b0
movq %rax, %rbp
testl %ebp, %ebp
js 0x128b4
leaq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x12e64
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x128e8
movq 0x96ee(%rip), %rcx      # 0x1bf98
cmpb $0x0, (%rcx)
je 0x128e4
incl 0x8(%rax)
jmp 0x128e8
callq 0x5040
movl (%rax), %eax
cmpl $0x6f, %eax
je 0x128c9
cmpl $0xb, %eax
jne 0x12957
cmpq $0x0, 0x10(%rbx)
je 0x12957
movq %rbx, %rdi
callq *0x18(%rbx)
testl %eax, %eax
je 0x12840
jmp 0x12940
lock
incl 0x8(%rax)
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq %r12, 0x10(%rsp)
movq %rbp, 0x38(%rsp)
cmpq $0x0, 0x10(%r14)
je 0x1297c
movq %r14, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq *0x18(%r14)
movl %eax, %ebp
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x12926
callq 0x78ac
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x12935
callq 0x78ac
cmpl $0x1, %ebp
jne 0x12840
jmp 0x12945
cmpl $0x1, %eax
jne 0x12981
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x180, %edi            # imm = 0x180
callq 0x51a0
movq %rax, %r14
movq %rax, %rdi
callq 0x12b46
leaq 0x92b5(%rip), %rsi      # 0x1bc28
leaq 0x2b0(%rip), %rdx       # 0x12c2a
jmp 0x129ab
callq 0x5140
movl $0x10, %edi
callq 0x51a0
movq %rax, %r14
leaq 0x27ac(%rip), %rsi      # 0x15141
movq %rax, %rdi
callq 0x56a0
movq 0x9624(%rip), %rsi      # 0x1bfc8
movq 0x9605(%rip), %rdx      # 0x1bfb0
movq %r14, %rdi
callq 0x5610
jmp 0x129b5
movq %rax, %rbx
movq %r14, %rdi
callq 0x5280
jmp 0x129e5
jmp 0x129c4
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x129d6
callq 0x78ac
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x129e5
callq 0x78ac
movq %rbx, %rdi
callq 0x5650
nop