// File: /workspace/github_cpp_makefile_star2_O1/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 0x12a96
leaq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x13046
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x12aca
movq 0x950c(%rip), %rcx      # 0x1bf98
cmpb $0x0, (%rcx)
je 0x12ac6
incl 0x8(%rax)
jmp 0x12aca
callq 0x5040
movl (%rax), %eax
cmpl $0x6f, %eax
je 0x12aab
cmpl $0xb, %eax
jne 0x12b39
cmpq $0x0, 0x10(%rbx)
je 0x12b39
movq %rbx, %rdi
callq *0x18(%rbx)
testl %eax, %eax
je 0x12a22
jmp 0x12b22
lock
incl 0x8(%rax)
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq %r12, 0x10(%rsp)
movq %rbp, 0x38(%rsp)
cmpq $0x0, 0x10(%r14)
je 0x12b5e
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 0x12b08
callq 0x78bc
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x12b17
callq 0x78bc
cmpl $0x1, %ebp
jne 0x12a22
jmp 0x12b27
cmpl $0x1, %eax
jne 0x12b63
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 0x12d28
leaq 0x90d3(%rip), %rsi      # 0x1bc28
leaq 0x2b0(%rip), %rdx       # 0x12e0c
jmp 0x12b8d
callq 0x5140
movl $0x10, %edi
callq 0x51a0
movq %rax, %r14
leaq 0x25ca(%rip), %rsi      # 0x15141
movq %rax, %rdi
callq 0x56a0
movq 0x9442(%rip), %rsi      # 0x1bfc8
movq 0x9423(%rip), %rdx      # 0x1bfb0
movq %r14, %rdi
callq 0x5610
jmp 0x12b97
movq %rax, %rbx
movq %r14, %rdi
callq 0x5280
jmp 0x12bc7
jmp 0x12ba6
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x12bb8
callq 0x78bc
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x12bc7
callq 0x78bc
movq %rbx, %rdi
callq 0x5650
nop