// File: /workspace/github_cpp_makefile_star2_O1/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 0x2408(%rip), %rsi      # 0xb1dc
movl $0x1, %ecx
movq %r15, %rdi
xorl %edx, %edx
callq 0x6710
movq %rax, %r12
leaq 0x1(%rax), %r13
cmpq $0x7, %r13
ja 0x8dfa
cmpq $0x1, %r13
jne 0x8f69
movq %rbx, 0x28(%rsp)
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x64d0
notq %r12
addq 0x8(%r15), %r12
movq %rsp, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x64d0
movq (%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 0x6610
cmpq %r13, (%rbp)
je 0x9013
movq %rax, %r12
movslq %r12d, %rax
cmpq %r12, %rax
jne 0x901f
movl (%r15), %eax
cmpl $0x22, %eax
je 0x901f
testl %eax, %eax
jne 0x8e7e
movl %ebx, (%r15)
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8e91
callq 0x6480
leal -0x10000(%r12), %eax
cmpl $0xffff0001, %eax       # imm = 0xFFFF0001
jb 0x8fca
movl $0x2, %edi
movl $0x80801, %esi          # imm = 0x80801
xorl %edx, %edx
callq 0x60f0
cmpl $-0x1, %eax
je 0x8fd1
movl %eax, %ebp
movslq 0x1c(%r14), %rax
testq %rax, %rax
jle 0x8eee
movq %rsp, %rcx
movq %rax, (%rcx)
movl %ebp, %edi
movl $0x1, %esi
movl $0x8, %edx
movl $0x8, %r8d
callq 0x6770
testl %eax, %eax
jne 0x8fdb
cmpb $0x1, 0x18(%r14)
jne 0x8f1d
movq %rsp, %rcx
movl $0x1, (%rcx)
movl %ebp, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x6770
testl %eax, %eax
jne 0x8fec
cmpb $0x1, 0x19(%r14)
jne 0x8f4c
movq %rsp, %rcx
movl $0x1, (%rcx)
movl %ebp, %edi
movl $0x1, %esi
movl $0xf, %edx
movl $0x4, %r8d
callq 0x6770
testl %eax, %eax
jne 0x8ffd
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq 0x30(%rsp), %rdi
rolw $0x8, %r12w
cmpb $0x0, (%rdi)
je 0x8f75
callq 0x64b0
jmp 0x8f77
callq 0x6b32
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x8fb9
xorl %eax, %eax
movq %rsp, %rdx
movw $0x2, (%rdx)
movw %r12w, 0x2(%rdx)
movl %eax, 0x4(%rdx)
movq $0x0, 0x8(%rdx)
movl 0x20(%r14), %r8d
movq 0x28(%rsp), %rdi
movl %ebp, %esi
movl $0x10, %ecx
callq 0x6800
movl %eax, %ebx
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8fb9
callq 0x6480
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6bee
jmp 0x900c
movq %r15, %rdi
callq 0x6bc6
jmp 0x900c
movl %ebp, %edi
callq 0x6830
movq %r15, %rdi
callq 0x6b4e
jmp 0x900c
movl %ebp, %edi
callq 0x6830
movq %r15, %rdi
callq 0x6b76
jmp 0x900c
movl %ebp, %edi
callq 0x6830
movq %r15, %rdi
callq 0x6b9e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x8fa6
leaq 0x2321(%rip), %rdi      # 0xb33b
callq 0x6300
leaq 0x2315(%rip), %rdi      # 0xb33b
callq 0x6650
jmp 0x9032
movq %rax, %r14
jmp 0x906f
movq %rax, %rbx
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9048
callq 0x6480
movq %rbx, %rdi
callq 0x6750
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x905c
movl %ebx, (%r15)
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x906f
callq 0x6480
movq %r14, %rdi
callq 0x61a0
movq 0x5f5a(%rip), %rax      # 0xefd8
movq (%rax), %rcx
leaq 0x2156(%rip), %rdi      # 0xb1de
movl $0x29, %esi
movl $0x1, %edx
callq 0x6820
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
callq 0x66e0
jmp 0x8fa6
jmp 0x9032