// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rsi, 0x18(%rsp)
movq %rdi, %r15
leaq 0x50(%rsp), %rbp
leaq 0x40(%rsp), %rbx
movq %rdi, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movl $0x400, %edx            # imm = 0x400
movq %rbp, %rdi
xorl %esi, %esi
callq 0x51f0
movl $0x10, 0x4(%rsp)
movl (%r15), %edi
movl $0x400, %edx            # imm = 0x400
movq %rbp, %rsi
xorl %ecx, %ecx
movq %rbx, %r8
leaq 0x4(%rsp), %r9
callq 0x52a0
movq %rax, %r12
testl %r12d, %r12d
js 0x10858
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %r15
movq %rbx, %rsi
callq 0x10b88
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x72ee
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %rbp, %r13
movq %rbp, %rdx
movq %r12, %rcx
callq 0x10a82
movl %eax, %r12d
leaq 0x28(%rsp), %rdi
callq 0x708e
leaq 0x38(%rsp), %rdi
callq 0x708e
cmpl $0x1, %r12d
movq %r15, %rbx
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r15
movq %r13, %rbp
jne 0x107ac
jmp 0x1087d
callq 0x10a15
testb %al, %al
je 0x1088f
cmpq $0x0, 0x10(%r14)
je 0x1088f
movq %r14, %rdi
callq 0x10b78
testl %eax, %eax
je 0x107ac
cmpl $0x1, %eax
jne 0x108b4
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 0xb385(%rip), %rsi      # 0x1bc30
leaq 0x2a6(%rip), %rdx       # 0x10b58
jmp 0x108dc
pushq $0x10
popq %rdi
callq 0x51b0
movq %rax, %r14
leaq 0x289b(%rip), %rsi      # 0x13161
movq %rax, %rdi
callq 0x5680
movq 0xb6f3(%rip), %rsi      # 0x1bfc8
movq 0xb6d4(%rip), %rdx      # 0x1bfb0
movq %r14, %rdi
callq 0x55f0
jmp 0x108e6
movq %rax, %rbx
movq %r14, %rdi
callq 0x5270
jmp 0x1090a
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x708e
leaq 0x38(%rsp), %rdi
callq 0x708e
movq %rbx, %rdi
callq 0x5620