// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movl %edx, %ebx
movl %esi, %ebp
movq %rdi, %r14
movl $0x1, %r15d
cmpw $0xa, %bp
jb 0x21611
movl $0x2, %r15d
cmpw $0x64, %bp
jb 0x21611
movzwl %bp, %eax
movl $0x3, %r15d
cmpl $0x3e8, %eax            # imm = 0x3E8
jb 0x21611
cmpl $0x2710, %eax           # imm = 0x2710
movl $0x5, %r15d
sbbl $0x0, %r15d
movl %r15d, %esi
leaq 0x40(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movl $0x2d, %edx
callq 0x7590
movq (%r12), %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0xeac2
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movabsq $0x10000000a, %rax      # imm = 0x10000000A
movq %rax, 0x4(%rdx)
movl $0x401, (%rdx)          # imm = 0x401
movq (%r12), %rsi
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
callq 0x7640
movl %eax, %r15d
addl $0x9, %eax
cmpl $0x4, %eax
ja 0x2168c
movl $0x19, %ecx
btl %eax, %ecx
jb 0x2168e
jmp 0x216f9
jmp 0x216f9
leaq 0x25403(%rip), %rax     # 0x46a98
movl (%rax), %eax
xorl %esi, %esi
cmpl $0x2, %eax
setge %sil
leaq 0x13372(%rip), %rdx     # 0x34a19
leaq 0x10(%rsp), %rdi
callq 0x9718
cmpb $0x1, 0x10(%rsp)
jne 0x216ce
movq 0x18(%rsp), %rdi
leaq 0x15180(%rip), %rsi     # 0x36844
movl $0x25, %edx
callq 0x7580
leaq 0x10(%rsp), %rdi
callq 0x8f94
leaq 0x70(%rsp), %rdx
movl $0x2, 0x4(%rdx)
movq 0x30(%rsp), %rsi
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
callq 0x7640
movl %eax, %r15d
testl %r15d, %r15d
jne 0x2180a
movq 0x8(%rsp), %rax
movl $0x80800, %esi          # imm = 0x80800
orl 0x8(%rax), %esi
movl 0x4(%rax), %edi
movl 0xc(%rax), %edx
callq 0x70b0
movl %eax, %r15d
cmpl $-0x1, %eax
je 0x217b2
leaq 0x10(%rsp), %rcx
movl $0x1, (%rcx)
movl %r15d, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x79d0
testb %bl, %bl
je 0x21770
leaq 0x10(%rsp), %rcx
movl %r15d, %edi
xorl %esi, %esi
movl $0xf, %edx
movl $0x4, %r8d
callq 0x79d0
cmpl $-0x1, %eax
je 0x218bd
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
movl %r15d, %edi
callq 0x7060
cmpl $-0x1, %eax
je 0x2189b
movq 0x8(%rsp), %rdi
callq 0x7670
movl %r15d, %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x7560
cmpl $-0x1, %eax
je 0x218ac
movl %r15d, %ebp
jmp 0x217d5
movq 0x8(%rsp), %rdi
callq 0x7670
cmpl $0xa, 0x74(%rsp)
jne 0x21888
callq 0x7040
cmpl $0x61, (%rax)
jne 0x21888
cmpl $-0x1, %r15d
je 0x2168e
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x217f6
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x7510
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $-0xb, %r15d
jne 0x21823
callq 0x7040
movl (%rax), %edi
leaq 0x14e9f(%rip), %rsi     # 0x366bd
callq 0x8dd3
movl $0x10, %edi
callq 0x7220
movq %rax, %rbx
leaq 0x60(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x14e88(%rip), %rsi     # 0x366c9
leaq 0x14e8e(%rip), %rdx     # 0x366d6
leaq 0x50(%rsp), %rdi
callq 0x97a0
movl %r15d, %edi
callq 0x7740
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x7a80
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x218e5
movups (%rcx), %xmm0
movups %xmm0, (%r15)
jmp 0x218f2
callq 0x7040
movl (%rax), %edi
leaq 0x14e41(%rip), %rsi     # 0x366d7
callq 0x8dd3
movl %r15d, %edi
callq 0x7a90
leaq 0x14fd8(%rip), %rbx     # 0x36882
jmp 0x218cc
movl %r15d, %edi
callq 0x7a90
leaq 0x14fcc(%rip), %rbx     # 0x36887
jmp 0x218d6
movl %r15d, %edi
callq 0x7a90
leaq 0x14f9e(%rip), %rbx     # 0x3686a
movq 0x8(%rsp), %rdi
callq 0x7670
callq 0x7040
movl (%rax), %edi
movq %rbx, %rsi
callq 0x8dd3
movq %rdx, 0x10(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x20(%rsp)
movq 0x8(%rax), %rdx
leaq 0x10(%rsp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movb $0x1, %bpl
movq %rbx, %rdi
callq 0x78c0
xorl %ebp, %ebp
movq 0x246ce(%rip), %rsi     # 0x45ff0
movq 0x24647(%rip), %rdx     # 0x45f70
movq %rbx, %rdi
callq 0x7970
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x21953
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x7510
jmp 0x21953
movq %rax, %r14
movb $0x1, %bpl
movq 0x50(%rsp), %rdi
cmpq %r12, %rdi
je 0x21972
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x7510
jmp 0x21972
movq %rax, %r14
movb $0x1, %bpl
testb %bpl, %bpl
je 0x21999
movq %rbx, %rdi
callq 0x7370
jmp 0x21999
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x8f94
jmp 0x21999
jmp 0x21996
jmp 0x21996
jmp 0x21996
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x219b0
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x7510
movq %r14, %rdi
callq 0x79b0