// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movq %rdi, %rbx
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 -0x38(%rbp), %rcx
callq 0x2330
testl %eax, %eax
jne 0x75e4
movq -0x38(%rbp), %r13
testq %r13, %r13
sete %r15b
je 0x759b
movzbl %r14b, %eax
shll $0xb, %eax
orl $0x80000, %eax          # imm = 0x80000
movl %eax, -0x3c(%rbp)
leaq -0x2c(%rbp), %r12
movl 0x8(%r13), %esi
orl -0x3c(%rbp), %esi
movl 0x4(%r13), %edi
movl 0xc(%r13), %edx
callq 0x2390
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x7533
movl $0x1, -0x2c(%rbp)
testq %rbx, %rbx
jne 0x74e1
movl %r14d, %edi
movl $0x1, %esi
movl $0x2, %edx
movq %r12, %rcx
movl $0x4, %r8d
callq 0x2090
testl %eax, %eax
js 0x754a
movl %r14d, %edi
movl $0x1, %esi
movl $0xf, %edx
movq %r12, %rcx
movl $0x4, %r8d
callq 0x2090
testl %eax, %eax
js 0x7553
movl %r14d, %edi
movl $0x1, %esi
movl $0x9, %edx
movq %r12, %rcx
movl $0x4, %r8d
callq 0x2090
testl %eax, %eax
js 0x755c
movl %r14d, %edi
movl $0x6, %esi
movl $0x1, %edx
movq %r12, %rcx
movl $0x4, %r8d
callq 0x2090
testl %eax, %eax
js 0x7541
movl $0x2, %r12d
testq %rbx, %rbx
jne 0x7576
movq 0x18(%r13), %rsi
movl 0x10(%r13), %edx
movl %r14d, %edi
callq 0x2270
testl %eax, %eax
jns 0x7576
leaq 0x31eb(%rip), %rdi      # 0xa711
callq 0x22a0
movl $0x4, %r12d
jmp 0x7576
leaq 0x3220(%rip), %rdi      # 0xa75a
callq 0x22a0
jmp 0x757c
leaq 0x32cb(%rip), %rdi      # 0xa813
jmp 0x7563
leaq 0x3268(%rip), %rdi      # 0xa7b9
jmp 0x7563
leaq 0x327d(%rip), %rdi      # 0xa7d7
jmp 0x7563
leaq 0x3292(%rip), %rdi      # 0xa7f5
callq 0x22a0
movl %r14d, %edi
callq 0x2160
movl $0x1, %r12d
cmpl $0x4, %r12d
jne 0x7593
movq 0x28(%r13), %r13
testq %r13, %r13
sete %r15b
leaq -0x2c(%rbp), %r12
jne 0x7450
jmp 0x75a1
cmpl $0x2, %r12d
je 0x75a1
jmp 0x7603
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testb $0x1, %r15b
jne 0x75ed
movq -0x38(%rbp), %rdi
testq %rbx, %rbx
je 0x75ce
movq 0x18(%rdi), %rsi
movl 0x10(%rdi), %edx
movl %r14d, %edi
callq 0x2310
testl %eax, %eax
js 0x75f7
movq -0x38(%rbp), %rdi
callq 0x2380
jmp 0x7609
callq 0x2380
movl $0x1, %esi
movl $0xd, %edi
callq 0x21d0
jmp 0x7609
movl %eax, %edi
callq 0x23c0
jmp 0x7603
movl %r14d, %edi
callq 0x241c
jmp 0x7603
leaq -0x38(%rbp), %rdi
movl %r14d, %esi
callq 0x23f0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq