// File: /workspace/github_c_makefile_star2_O2/goghcrow[P]dubbo_ab/socket.c
// Function: socket_ctor
static int socket_ctor(const char *host, const char *port, bool nonblock)
{
    bool isServer = host == NULL;

    struct addrinfo hints, *servinfo, *p;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */

    // !!! sync for client
    int status = getaddrinfo(host, port, &hints, &servinfo);
    if (status != 0)
    {
        // gai_strerror --> getaddrinfo_strerror
        fprintf(stderr, "ERROR getaddrinfo: %s\n", gai_strerror(status));
        return -1;
    }

    int sockfd = -1;

    // 绑定到第一个能用的
    for (p = servinfo; p != NULL; p = p->ai_next)
    {
#ifdef __APPLE__
        sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
#else
        // linux 内核高版本可以直接设置 SOCK_NONBLOCK | SOCK_CLOEXEC, 节省一次fcntl系统调用
        sockfd = socket(p->ai_family, p->ai_socktype | (nonblock ? SOCK_NONBLOCK : 0) | SOCK_CLOEXEC, p->ai_protocol);
#endif

        if (sockfd == -1)
        {
            perror("ERROR socket");
            continue;
        }

#ifdef __APPLE__
        if (nonblock)
        {
            socket_setNonblock(sockfd);
        }
#endif

        int yes = 1;
        if (isServer)
        {
            if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0)
            {
                perror("ERROR setsockopt SO_REUSEADDR");
                close(sockfd);
                return -1;
            }

#ifdef SO_REUSEPORT
            if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) < 0)
            {
                perror("ERROR setsockopt SO_REUSEPORT");
                close(sockfd);
                return -1;
            }
#endif

            if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(int)) < 0)
            {

                perror("ERROR setsockopt SO_KEEPALIVE");
                close(sockfd);
                return -1;
            }
        }

        if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(int)) < 0)
        {
            perror("ERROR setsockopt TCP_NODELAY");
            close(sockfd);
            return -1;
        }

        // SO_REUSEADDR needs to be set before bind().
        // However, not all options need to be set before bind(), or even before connect().
        if (isServer && bind(sockfd, p->ai_addr, p->ai_addrlen) < 0)
        {
            perror("ERROR bind");
            continue;
        }

        break;
    }

    if (p == NULL)
    {
        // 注意bind失败要close
        fprintf(stderr, "ERROR failed to socket and bind\n");
        close(sockfd);
        return -1;
    }

    if (isServer)
    {
        // server 已经不用, client connect 仍然用
        freeaddrinfo(servinfo);
        signal(SIGPIPE, SIG_IGN);
        /*
        if (listen(sockfd, SOMAXCONN) < 0)
        {
            freeaddrinfo(servinfo);
            perror("ERROR listen");
            close(sockfd);
            return -1;
        }
        */
    }
    else
    {
        if (connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen) < 0)
        {
            perror("ERROR connect");
            freeaddrinfo(servinfo);
            close(sockfd);
            return -1;
        }
        freeaddrinfo(servinfo);
    }

    return sockfd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebx
movq %rdi, %r13
xorps %xmm0, %xmm0
leaq -0x70(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
pushq $0x1
popq %rax
movl %eax, 0x8(%rdx)
movl %eax, (%rdx)
leaq -0x30(%rbp), %r15
movq %r15, %rcx
callq 0x2330
testl %eax, %eax
jne 0x6256
movzbl %bl, %r12d
shll $0xb, %r12d
orl $0x80000, %r12d         # imm = 0x80000
pushq $-0x1
popq %rbx
leaq -0x38(%rbp), %r14
movl %r12d, -0x34(%rbp)
movq %r13, -0x40(%rbp)
movq (%r15), %r15
testq %r15, %r15
je 0x627d
movl 0x8(%r15), %esi
orl %r12d, %esi
movl 0x4(%r15), %edi
movl 0xc(%r15), %edx
callq 0x2390
movl %eax, %ebx
cmpl $-0x1, %eax
je 0x6207
movl $0x1, -0x38(%rbp)
testq %r13, %r13
jne 0x61be
movl %ebx, %edi
pushq $0x1
popq %r13
movl %r13d, %esi
pushq $0x2
popq %rdx
movq %r14, %rcx
pushq $0x4
popq %r12
movl %r12d, %r8d
callq 0x2090
testl %eax, %eax
js 0x62bb
movl %ebx, %edi
movl %r13d, %esi
pushq $0xf
popq %rdx
movq %r14, %rcx
movl %r12d, %r8d
callq 0x2090
testl %eax, %eax
js 0x62c4
movl %ebx, %edi
movl %r13d, %esi
pushq $0x9
popq %rdx
movq %r14, %rcx
movl %r12d, %r8d
callq 0x2090
testl %eax, %eax
movq -0x40(%rbp), %r13
movl -0x34(%rbp), %r12d
js 0x62cd
movl %ebx, %edi
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
pushq $0x4
popq %r8
callq 0x2090
testl %eax, %eax
js 0x629b
testq %r13, %r13
jne 0x6235
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %ebx, %edi
callq 0x2270
testl %eax, %eax
jns 0x6210
leaq 0x3514(%rip), %rdi      # 0x970d
callq 0x22a0
addq $0x28, %r15
jmp 0x6126
leaq 0x3548(%rip), %rdi      # 0x9756
jmp 0x61f9
movq -0x30(%rbp), %rdi
callq 0x2380
pushq $0xd
popq %rdi
pushq $0x1
popq %rsi
callq 0x21d0
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
movl %ebx, %edi
callq 0x2310
testl %eax, %eax
js 0x62a4
movq -0x30(%rbp), %rdi
callq 0x2380
jmp 0x6224
movq 0x8d9b(%rip), %rcx      # 0xeff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x2120
leaq 0x3530(%rip), %rsi      # 0x979e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x62e0
movq 0x8d74(%rip), %rax      # 0xeff8
movq (%rax), %rcx
leaq 0x359e(%rip), %rdi      # 0x982c
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x2320
jmp 0x62d9
leaq 0x356d(%rip), %rdi      # 0x980f
jmp 0x62d4
leaq 0x35a2(%rip), %rdi      # 0x984d
callq 0x22a0
movq -0x30(%rbp), %rdi
callq 0x2380
jmp 0x62d9
leaq 0x34f3(%rip), %rdi      # 0x97b5
jmp 0x62d4
leaq 0x3508(%rip), %rdi      # 0x97d3
jmp 0x62d4
leaq 0x351d(%rip), %rdi      # 0x97f1
callq 0x22a0
movl %ebx, %edi
callq 0x2160
pushq $-0x1
popq %rbx
jmp 0x6224