// File: /workspace/github_cpp_makefile_star2_O2/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 0x2502(%rip), %rsi      # 0xe1de
movq %r15, %rdi
xorl %edx, %edx
callq 0x9be0
movq %rax, %r12
leaq 0x1(%rax), %r13
cmpq $0x7, %r13
ja 0xbcfd
cmpq $0x1, %r13
jne 0xbdf2
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x9700
notq %r12
addq 0x8(%r15), %r12
movq %rsp, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x9700
movq %rsp, %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x9b90
movl %eax, %r15d
movq %rsp, %rdi
callq 0x92d0
leal -0x10000(%r15), %eax
cmpl $0xffff0001, %eax       # imm = 0xFFFF0001
jb 0xbe22
pushq $0x2
popq %rdi
movl $0x80801, %esi          # imm = 0x80801
xorl %edx, %edx
callq 0x9150
cmpl $-0x1, %eax
je 0xbe43
movl %eax, %ebp
movslq 0x1c(%r14), %rsi
testq %rsi, %rsi
jle 0xbd83
movl %ebp, %edi
callq 0x98a0
testl %eax, %eax
jne 0xbe62
cmpb $0x1, 0x18(%r14)
jne 0xbd9c
pushq $0x1
popq %rsi
movl %ebp, %edi
callq 0x93e0
testl %eax, %eax
jne 0xbe88
cmpb $0x1, 0x19(%r14)
jne 0xbdb5
pushq $0x1
popq %rsi
movl %ebp, %edi
callq 0x9860
testl %eax, %eax
jne 0xbeae
movq 0x28(%rsp), %rdx
movzwl %r15w, %esi
leaq 0x48(%rsp), %rdi
callq 0x9710
movups 0x48(%rsp), %xmm0
movq %rsp, %rdx
movaps %xmm0, (%rdx)
movl 0x20(%r14), %r8d
pushq $0x10
popq %rcx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x9b80
movl %eax, %ebx
leaq 0x28(%rsp), %rdi
callq 0x92d0
jmp 0xbe11
movq 0x61d7(%rip), %rax      # 0x11fd0
movq (%rax), %rcx
leaq 0x23dd(%rip), %rdi      # 0xe1e0
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x9bb0
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x61a7(%rip), %rax      # 0x11fd0
movq (%rax), %rcx
leaq 0x23d7(%rip), %rdi      # 0xe20a
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x9bb0
jmp 0xbedf
movq 0x6186(%rip), %rax      # 0x11fd0
movq (%rax), %rbx
callq 0x9060
movl (%rax), %edi
callq 0x91d0
leaq 0x23d4(%rip), %rsi      # 0xe234
jmp 0xbed2
movl %ebp, %edi
callq 0x9bc0
movq 0x6160(%rip), %rax      # 0x11fd0
movq (%rax), %rbx
callq 0x9060
movl (%rax), %edi
callq 0x91d0
leaq 0x23d6(%rip), %rsi      # 0xe25c
jmp 0xbed2
movl %ebp, %edi
callq 0x9bc0
movq 0x613a(%rip), %rax      # 0x11fd0
movq (%rax), %rbx
callq 0x9060
movl (%rax), %edi
callq 0x91d0
leaq 0x23ce(%rip), %rsi      # 0xe27a
jmp 0xbed2
movl %ebp, %edi
callq 0x9bc0
movq 0x6114(%rip), %rax      # 0x11fd0
movq (%rax), %rbx
callq 0x9060
movl (%rax), %edi
callq 0x91d0
leaq 0x23c9(%rip), %rsi      # 0xe29b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x9940
pushq $-0x1
popq %rbx
jmp 0xbde6
movq %rax, %rbx
movq %rsp, %rdi
callq 0x92d0
jmp 0xbef7
movq %rax, %rbx
movq %rbx, %rdi
callq 0x9270
movq 0x60ca(%rip), %rax      # 0x11fd0
movq (%rax), %rcx
leaq 0x22d0(%rip), %rdi      # 0xe1e0
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x9bb0
callq 0x9a30
jmp 0xbedf
jmp 0xbf26
jmp 0xbf26
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x92d0
movq %rbx, %rdi
callq 0x9aa0
nop