// File: /workspace/github_cpp_makefile_star2_O3/shaovie[P]reactor/src/acceptor.cpp
// Function: acceptor::tcp_open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, options const&)
int acceptor::tcp_open(const std::string &addr, const options &opt) {
    auto p = addr.find(":");
    if (p == addr.npos || (p > 0 && p < 7)) {
        fprintf(stderr, "reactor: accepor open fail! addr invalid\n");
        return -1;
    }
    std::string ip = addr.substr(0, p);
    int port = 0;
    try {
        port = std::stoi(addr.substr(p+1, addr.length() - p - 1)); // throw
    } catch(...) {
        fprintf(stderr, "reactor: accepor open fail! addr invalid\n");
        return -1;
    }
    if (port < 1 || port > 65535) {
        fprintf(stderr, "reactor: accepor open fail! port invalid\n");
        return -1;
    }

    int fd = ::socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
    if (fd == -1) {
        fprintf(stderr, "reactor: create listen socket fail! %s\n", strerror(errno));
        return -1;
    }
    // `sysctl -a | grep net.ipv4.tcp_rmem` 返回 min default max
    // 默认内核会在min,max之间动态调整, default是初始值, 如果设置了SO_RCVBUF, 
    // 缓冲区大小不变成固定值, 内核也不会进行动态调整了.
    //
    // 必须在listen/connect之前调用
    // must < `sysctl -a | grep net.core.rmem_max`
    if (opt.rcvbuf_size > 0 && socket::set_rcvbuf(fd, opt.rcvbuf_size) != 0) {
        socket::close(fd);
        fprintf(stderr, "reactor: set rcvbuf fail! %s\n", strerror(errno));
        return -1;
    }
    if (opt.reuse_addr && socket::reuseaddr(fd, 1) != 0) {
        socket::close(fd);
        fprintf(stderr, "reactor: set reuseaddr fail! %s\n", strerror(errno));
        return -1;
    }
    if (opt.reuse_port && socket::reuseport(fd, 1) != 0) {
        socket::close(fd);
        fprintf(stderr, "reactor: set reuseport fail! %s\n", strerror(errno));
        return -1;
    }
    struct sockaddr_in laddr;
    ::memset(reinterpret_cast<void *>(&laddr), 0, sizeof(laddr));
    inet_address iaddr(port, ip.c_str());
    laddr = *reinterpret_cast<sockaddr_in *>(iaddr.get_addr());
    return this->listen(fd,
        reinterpret_cast<sockaddr *>(&laddr), sizeof(laddr),
        opt.backlog);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x24f8(%rip), %rsi      # 0xb1dc
movl $0x1, %ecx
movq %r15, %rdi
xorl %edx, %edx
callq 0x6700
movq %rax, %r12
leaq 0x1(%rax), %r13
cmpq $0x7, %r13
ja 0x8d0a
cmpq $0x1, %r13
jne 0x8e7b
movq %rbx, 0x8(%rsp)
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x64c0
notq %r12
addq 0x8(%r15), %r12
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x64c0
movq 0x10(%rsp), %r13
callq 0x6040
movq %rax, %r15
movl (%rax), %ebx
movl $0x0, (%rax)
leaq 0x50(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
movl $0xa, %edx
callq 0x6600
cmpq %r13, (%rbp)
je 0x8f27
movq %rax, %r12
movslq %r12d, %rax
cmpq %r12, %rax
jne 0x8f33
movl (%r15), %eax
cmpl $0x22, %eax
je 0x8f33
testl %eax, %eax
jne 0x8d91
movl %ebx, (%r15)
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8da4
callq 0x6470
leal -0x10000(%r12), %eax
cmpl $0xffff0001, %eax       # imm = 0xFFFF0001
jb 0x8ede
movl $0x2, %edi
movl $0x80801, %esi          # imm = 0x80801
xorl %edx, %edx
callq 0x60e0
cmpl $-0x1, %eax
je 0x8ee5
movl %eax, %ebp
movslq 0x1c(%r14), %rax
testq %rax, %rax
jle 0x8e03
leaq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movl %ebp, %edi
movl $0x1, %esi
movl $0x8, %edx
movl $0x8, %r8d
callq 0x6760
testl %eax, %eax
jne 0x8eef
cmpb $0x1, 0x18(%r14)
jne 0x8e34
leaq 0x10(%rsp), %rcx
movl $0x1, (%rcx)
movl %ebp, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x6760
testl %eax, %eax
jne 0x8f00
cmpb $0x1, 0x19(%r14)
jne 0x8e65
leaq 0x10(%rsp), %rcx
movl $0x1, (%rcx)
movl %ebp, %edi
movl $0x1, %esi
movl $0xf, %edx
movl $0x4, %r8d
callq 0x6760
testl %eax, %eax
jne 0x8f11
movq 0x30(%rsp), %rdi
rolw $0x8, %r12w
cmpb $0x0, (%rdi)
je 0x8e87
callq 0x64a0
jmp 0x8e89
callq 0x6b21
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x8ecd
xorl %eax, %eax
leaq 0x10(%rsp), %rdx
movw $0x2, (%rdx)
movw %r12w, 0x2(%rdx)
movl %eax, 0x4(%rdx)
movq $0x0, 0x8(%rdx)
movl 0x20(%r14), %r8d
movq 0x8(%rsp), %rdi
movl %ebp, %esi
movl $0x10, %ecx
callq 0x67f0
movl %eax, %ebx
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8ecd
callq 0x6470
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6bdd
jmp 0x8f20
movq %r15, %rdi
callq 0x6bb5
jmp 0x8f20
movl %ebp, %edi
callq 0x6820
movq %r15, %rdi
callq 0x6b3d
jmp 0x8f20
movl %ebp, %edi
callq 0x6820
movq %r15, %rdi
callq 0x6b65
jmp 0x8f20
movl %ebp, %edi
callq 0x6820
movq %r15, %rdi
callq 0x6b8d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x8eba
leaq 0x240d(%rip), %rdi      # 0xb33b
callq 0x62e0
leaq 0x2401(%rip), %rdi      # 0xb33b
callq 0x6650
jmp 0x8f46
movq %rax, %r14
jmp 0x8f83
movq %rax, %rbx
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8f5c
callq 0x6470
movq %rbx, %rdi
callq 0x6740
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x8f70
movl %ebx, (%r15)
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8f83
callq 0x6470
movq %r14, %rdi
callq 0x6190
movq 0x6046(%rip), %rax      # 0xefd8
movq (%rax), %rcx
leaq 0x2242(%rip), %rdi      # 0xb1de
movl $0x29, %esi
movl $0x1, %edx
callq 0x6810
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
callq 0x66d0
jmp 0x8eba
jmp 0x8f46