// File: /workspace/github_cpp_makefile_star2_O3/cybozu[P]yrmcds/cybozu/tcp.cpp
// Function: cybozu::setup_server_socket(char const*, unsigned short, bool)
int
setup_server_socket(const char* bind_addr, std::uint16_t port, bool freebind) {
    struct addrinfo hint, *res;
    std::string s_port = std::to_string(port);
    std::memset(&hint, 0, sizeof(hint));
    hint.ai_family = AF_INET6; // can accept IPv4 address
    hint.ai_socktype = SOCK_STREAM;
    hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
    int e = getaddrinfo(bind_addr, s_port.c_str(), &hint, &res);
    if( e == EAI_FAMILY || e == EAI_ADDRFAMILY || e == EAI_NODATA ) {
      IPV4:
        logger::info() << "Binding to IPv6 fails, trying IPv4...";
        hint.ai_family = AF_INET;
        e = getaddrinfo(bind_addr, s_port.c_str(), &hint, &res);
    }
    if( e == EAI_SYSTEM ) {
        throw_unix_error(errno, "getaddrinfo");
    } else if( e != 0 ) {
        throw std::runtime_error(std::string("getaddrinfo: ") +
                                 gai_strerror(e));
    }

    int s = ::socket(res->ai_family,
                     res->ai_socktype | SOCK_NONBLOCK | SOCK_CLOEXEC,
                     res->ai_protocol);
    if( s == -1 ) {
        freeaddrinfo(res);

        // getaddrinfo may return IPv6 though the kernel disables IPv6.
        // The following is the workaround for it.
        if( hint.ai_family == AF_INET6 && errno == EAFNOSUPPORT )
            goto IPV4;

        throw_unix_error(errno, "socket");
    }

    // intentionally ignore errors
    int ok = 1;
    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &ok, sizeof(ok));

    if( freebind ) {
        if( setsockopt(s, IPPROTO_IP, IP_FREEBIND, &ok, sizeof(ok)) == -1 ) {
            ::close(s);
            freeaddrinfo(res);
            throw_unix_error(errno, "setsockopt(IP_FREEBIND)");
        }
    }

    if( bind(s, res->ai_addr, res->ai_addrlen) == -1 ) {
        ::close(s);
        freeaddrinfo(res);
        throw_unix_error(errno, "bind");
    }
    freeaddrinfo(res);

    if( listen(s, 4096) == -1 ) {
        ::close(s);
        throw_unix_error(errno, "listen");
    }
    return s;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %edx, %ebx
movl %esi, %ebp
movq %rdi, %r14
movl $0x1, %r15d
cmpw $0xa, %bp
jb 0x20d02
movl $0x2, %r15d
cmpw $0x64, %bp
jb 0x20d02
movzwl %bp, %eax
movl $0x3, %r15d
cmpl $0x3e8, %eax            # imm = 0x3E8
jb 0x20d02
cmpl $0x2710, %eax           # imm = 0x2710
movl $0x5, %r15d
sbbl $0x0, %r15d
movl %r15d, %esi
leaq 0x58(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movl $0x2d, %edx
callq 0x7580
movq (%r12), %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0xea9e
xorps %xmm0, %xmm0
leaq 0x68(%rsp), %rdx
movups %xmm0, 0x8(%rdx)
movups %xmm0, 0x18(%rdx)
movq $0x0, 0x28(%rdx)
movl $0x1, 0x8(%rdx)
movabsq $0xa00000401, %rax      # imm = 0xA00000401
movq %rax, (%rdx)
movq (%r12), %rsi
movq %rsp, %rcx
movq %r14, %rdi
callq 0x7630
movl %eax, %ebp
addl $0x9, %eax
cmpl $0x4, %eax
ja 0x20de5
movl $0x19, %ecx
btl %eax, %ecx
jae 0x20de5
leaq 0x25d04(%rip), %rax     # 0x46a88
movl (%rax), %eax
xorl %esi, %esi
cmpl $0x2, %eax
setge %sil
leaq 0x13cc7(%rip), %rdx     # 0x34a5d
leaq 0x8(%rsp), %rdi
callq 0x96e6
cmpb $0x1, 0x8(%rsp)
jne 0x20dbd
movq 0x10(%rsp), %rdi
leaq 0x15a57(%rip), %rsi     # 0x3680a
movl $0x25, %edx
callq 0x7570
leaq 0x8(%rsp), %rdi
callq 0x8f4e
leaq 0x68(%rsp), %rdx
movl $0x2, 0x4(%rdx)
movq 0x48(%rsp), %rsi
movq %rsp, %rcx
movq %r14, %rdi
callq 0x7630
movl %eax, %ebp
testl %ebp, %ebp
jne 0x20ee5
movq (%rsp), %rax
movl $0x80800, %esi          # imm = 0x80800
orl 0x8(%rax), %esi
movl 0x4(%rax), %edi
movl 0xc(%rax), %edx
callq 0x70b0
cmpl $-0x1, %eax
jne 0x20e37
movq (%rsp), %rdi
callq 0x7660
movl 0x6c(%rsp), %ebp
callq 0x7040
cmpl $0xa, %ebp
jne 0x20e29
cmpl $0x61, (%rax)
je 0x20d7d
movl (%rax), %edi
leaq 0x1586b(%rip), %rsi     # 0x3669d
callq 0x8d8d
movl %eax, %ebp
leaq 0x8(%rsp), %rcx
movl $0x1, (%rcx)
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x79c0
testb %bl, %bl
je 0x20e81
leaq 0x8(%rsp), %rcx
movl %ebp, %edi
xorl %esi, %esi
movl $0xf, %edx
movl $0x4, %r8d
callq 0x79c0
cmpl $-0x1, %eax
je 0x20fce
movq (%rsp), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
movl %ebp, %edi
callq 0x7060
cmpl $-0x1, %eax
je 0x20fae
movq (%rsp), %rdi
callq 0x7660
movl %ebp, %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x7550
cmpl $-0x1, %eax
je 0x20fbe
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
je 0x20ed1
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x7500
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $-0xb, %ebp
jne 0x20efd
callq 0x7040
movl (%rax), %edi
leaq 0x1578b(%rip), %rsi     # 0x36683
callq 0x8d8d
movl $0x10, %edi
callq 0x7220
movq %rax, %rbx
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x15775(%rip), %rsi     # 0x3668f
leaq 0x1577b(%rip), %rdx     # 0x3669c
leaq 0x28(%rsp), %rdi
callq 0x9760
movl %ebp, %edi
callq 0x7730
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x7a70
leaq 0x18(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x20f62
movups (%rcx), %xmm0
movups %xmm0, (%r12)
jmp 0x20f6f
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
movq 0x8(%rax), %rdx
leaq 0x8(%rsp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movb $0x1, %bpl
movq %rbx, %rdi
callq 0x78b0
xorl %ebp, %ebp
movq 0x25051(%rip), %rsi     # 0x45ff0
movq 0x24fca(%rip), %rdx     # 0x45f70
movq %rbx, %rdi
callq 0x7960
movl %ebp, %edi
callq 0x7a80
leaq 0x1588c(%rip), %rbx     # 0x36848
jmp 0x20fdc
movl %ebp, %edi
callq 0x7a80
leaq 0x15881(%rip), %rbx     # 0x3684d
jmp 0x20fe5
movl %ebp, %edi
callq 0x7a80
leaq 0x15854(%rip), %rbx     # 0x36830
movq (%rsp), %rdi
callq 0x7660
callq 0x7040
movl (%rax), %edi
movq %rbx, %rsi
callq 0x8d8d
jmp 0x2106a
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x21010
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x7500
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x21027
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x7500
testb %bpl, %bpl
jne 0x2105c
jmp 0x2106d
movq %rax, %r14
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x2105c
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x7500
jmp 0x2105c
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x8f4e
jmp 0x2106d
movq %rax, %r14
movq %rbx, %rdi
callq 0x7370
jmp 0x2106d
jmp 0x2106a
jmp 0x2106a
movq %rax, %r14
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
je 0x21084
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x7500
movq %r14, %rdi
callq 0x79a0