// File: /workspace/github_c_makefile_star2_O3/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, %r12d
movq %rdi, %r14
xorps %xmm0, %xmm0
leaq -0x70(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x1, %eax
movl %eax, 0x8(%rdx)
movl %eax, (%rdx)
leaq -0x30(%rbp), %rcx
callq 0x2330
testl %eax, %eax
jne 0x7483
movq -0x30(%rbp), %r15
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %r15, %r15
je 0x748c
movzbl %r12b, %r12d
shll $0xb, %r12d
orl $0x80000, %r12d         # imm = 0x80000
leaq -0x34(%rbp), %r13
leaq 0x34eb(%rip), %rax      # 0xa813
movq %rax, -0x40(%rbp)
movl 0x8(%r15), %esi
orl %r12d, %esi
movl 0x4(%r15), %edi
movl 0xc(%r15), %edx
callq 0x2390
movl %eax, %ebx
cmpl $-0x1, %eax
je 0x7411
movl $0x1, -0x34(%rbp)
testq %r14, %r14
jne 0x7443
movl %ebx, %edi
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rcx
movl $0x4, %r8d
callq 0x2090
testl %eax, %eax
js 0x7495
movl %ebx, %edi
movl $0x1, %esi
movl $0xf, %edx
movq %r13, %rcx
movl $0x4, %r8d
callq 0x2090
testl %eax, %eax
js 0x749e
movl %ebx, %edi
movl $0x1, %esi
movl $0x9, %edx
movq %r13, %rcx
movl $0x4, %r8d
callq 0x2090
testl %eax, %eax
js 0x74a7
movl %ebx, %edi
movl $0x6, %esi
movl $0x1, %edx
movq %r13, %rcx
movl $0x4, %r8d
callq 0x2090
testl %eax, %eax
js 0x74b2
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %ebx, %edi
callq 0x2270
testl %eax, %eax
jns 0x741a
leaq 0x3314(%rip), %rdi      # 0xa711
callq 0x22a0
movq 0x28(%r15), %r15
testq %r15, %r15
jne 0x732c
jmp 0x748c
leaq 0x3342(%rip), %rdi      # 0xa75a
jmp 0x73fd
movq -0x30(%rbp), %rdi
callq 0x2380
movl $0x1, %esi
movl $0xd, %edi
callq 0x21d0
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x34(%rbp), %rcx
movl %ebx, %edi
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
callq 0x2090
testl %eax, %eax
js 0x74b2
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
movl %ebx, %edi
callq 0x2310
testl %eax, %eax
js 0x74cc
movq -0x30(%rbp), %rdi
callq 0x2380
jmp 0x7432
movl %eax, %edi
callq 0x23c0
jmp 0x74c2
movl %ebx, %edi
callq 0x241c
jmp 0x74c2
leaq 0x331d(%rip), %rax      # 0xa7b9
jmp 0x74ae
leaq 0x3332(%rip), %rax      # 0xa7d7
jmp 0x74ae
leaq 0x3347(%rip), %rax      # 0xa7f5
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x22a0
movl %ebx, %edi
callq 0x2160
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x7434
leaq -0x30(%rbp), %rdi
movl %ebx, %esi
callq 0x23f0
jmp 0x74c2