// File: /workspace/github_c_makefile_star2_O3/qq-smart[P]qq_sh_server/core/qq_listening.c
// Function: qq_open_listening_sockets
qq_int_t
qq_open_listening_sockets(qq_cycle_t *cycle)
{
    int              reuseaddr = 1;
    qq_uint_t        i, tries, failed;
    qq_err_t         err;
    qq_socket_t      s;
    qq_listening_t  *ls;

    for (tries = 5; tries; tries--) {
        failed = 0;

        ls = cycle->listening;
        for (i = 0; i < cycle->nlistening; i++) {
            if (ls[i].fd != (qq_socket_t) -1) {
                continue;
            }

            s = socket(ls[i].sockaddr.sin_family, ls[i].type, 0);
            if (s == (qq_socket_t) -1) {
                qq_log_error(errno, "create sockets %s failed", ls[i].addr_text);
                return QQ_ERROR;
            }

            if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
                          (const void *) &reuseaddr, sizeof(int))
                == -1)
            {
                qq_log_error(errno, "setsockopt(SO_REUSEADDR) %s failed", ls[i].addr_text);

                if (close(s) == -1) {
                    qq_log_error(errno, "close sockets %s failed", ls[i].addr_text);
                }
                return QQ_ERROR;
            }

#if (QQ_HAVE_REUSEPORT)
            int  reuseport = 1;
            if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT,
                          (const void *) &reuseport, sizeof(int))
                == -1)
            {
                qq_log_error(errno, "setsockopt(SO_REUSEPORT) %s failed", ls[i].addr_text);

                if (close(s) == -1) {
                    qq_log_error(errno, "close sockets %s failed", ls[i].addr_text);
                }
                return QQ_ERROR;
            }
#endif

            if (qq_nonblocking(s) == -1) {
                qq_log_error(errno, "set sockets nonblocking %s failed", ls[i].addr_text);
                if (close(s) == -1) {
                    qq_log_error(errno, "close sockets %s failed", ls[i].addr_text);
                }
                return QQ_ERROR;
            }

            qq_log_debug("bind() %s #%d ", ls[i].addr_text, s);

            if (bind(s, (struct sockaddr *) &ls[i].sockaddr, ls[i].socklen) == -1) {
                err = errno;

                if (err != QQ_EADDRINUSE) {
                    qq_log_error(err, "bind() to %s failed", ls[i].addr_text);
                }

                if (close(s) == -1) {
                    qq_log_error(errno, "close sockets %s failed", ls[i].addr_text);
                }

                if (err != QQ_EADDRINUSE) {
                    return QQ_ERROR;
                }

                failed = 1;

                continue;
            }

            if (ls[i].type != SOCK_STREAM) {
                ls[i].fd = s;
                continue;
            }

            qq_log_debug("qq_open_listening_sockets() -> listen() to %s, backlog %d ",
                ls[i].addr_text, ls[i].backlog);

            if (listen(s, ls[i].backlog) == -1) {
                err = errno;
                if (err != QQ_EADDRINUSE) {
                    qq_log_error(err, "listen() to %s, backlog %d failed",
                                 ls[i].addr_text, ls[i].backlog);
                }

                if (close(s) == -1) {
                    qq_log_error(errno, "close sockets %s failed", ls[i].addr_text);
                }

                if (err != QQ_EADDRINUSE) {
                    return QQ_ERROR;
                }

                failed = 1;

                continue;
            }

            ls[i].fd = s;
        }

        if (failed == 0) {
            break;
        }

        qq_log_error(0, "try again to bind() after 500ms");
        usleep(500000);
    }

    if (failed) {
        qq_log_error(0, "still could not bind()");
        return QQ_ERROR;
    }

    return QQ_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r12
leaq -0x3c(%rbp), %rax
movl $0x1, (%rax)
movl $0x5, %eax
xorl %ebx, %ebx
movq %rdi, -0x48(%rbp)
cmpq $0x0, 0x38(%r12)
je 0x68a9
movq %rax, -0x50(%rbp)
movq 0x40(%r12), %r14
addq $0x38, %r14
xorl %r13d, %r13d
movq $0x0, -0x38(%rbp)
cmpl $-0x1, -0x38(%r14)
jne 0x67c9
movzwl -0x34(%r14), %edi
movl -0x4(%r14), %esi
xorl %edx, %edx
callq 0x2080
cmpl $-0x1, %eax
je 0x6827
movl %eax, %ebx
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x3c(%rbp), %rcx
movl $0x4, %r8d
callq 0x22f0
cmpl $-0x1, %eax
je 0x684c
movl %ebx, %edi
movl $0x3, %esi
xorl %eax, %eax
callq 0x2170
orl $0x800, %eax            # imm = 0x800
movl %ebx, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x2170
cmpl $-0x1, %eax
je 0x6863
leaq -0x34(%r14), %r15
leaq -0x20(%r14), %rsi
leaq 0x4397(%rip), %rdi      # 0xaa87
movq %rsi, -0x30(%rbp)
movl %ebx, %edx
xorl %eax, %eax
callq 0x6c74
movl -0x24(%r14), %edx
movl %ebx, %edi
movq %r15, %rsi
callq 0x2050
cmpl $-0x1, %eax
je 0x6744
cmpl $0x1, -0x4(%r14)
jne 0x673b
movl (%r14), %edx
leaq 0x4389(%rip), %rdi      # 0xaaaa
movq -0x30(%rbp), %rsi
xorl %eax, %eax
callq 0x6c74
movl (%r14), %esi
movl %ebx, %edi
callq 0x2220
cmpl $-0x1, %eax
je 0x676c
movl %ebx, -0x38(%r14)
jmp 0x67c9
callq 0x2030
movq %rax, %r15
movl (%rax), %r12d
cmpl $0x62, %r12d
je 0x6795
movl %r12d, %edi
leaq 0x4337(%rip), %rsi      # 0xaa96
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x6b27
jmp 0x6795
callq 0x2030
movq %rax, %r15
movl (%rax), %r12d
cmpl $0x62, %r12d
je 0x6795
movl (%r14), %ecx
movl %r12d, %edi
leaq 0x435b(%rip), %rsi      # 0xaae5
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x6b27
movl %ebx, %edi
callq 0x2320
cmpl $-0x1, %eax
jne 0x67b6
movl (%r15), %edi
leaq 0x42a2(%rip), %rsi      # 0xaa4d
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x6b27
movl $0x1, %eax
movq %rax, -0x38(%rbp)
cmpl $0x62, %r12d
movq -0x48(%rbp), %r12
jne 0x6843
incq %r13
addq $0x60, %r14
cmpq 0x38(%r12), %r13
jb 0x666b
cmpq $0x0, -0x38(%rbp)
movl $0x0, %ebx
movq -0x50(%rbp), %r14
je 0x68a9
xorl %edi, %edi
leaq 0x430f(%rip), %rsi      # 0xab07
xorl %eax, %eax
callq 0x6b27
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2290
decq %r14
movq %r14, %rax
jne 0x6647
leaq 0x430b(%rip), %rsi      # 0xab27
xorl %edi, %edi
xorl %eax, %eax
callq 0x6b27
jmp 0x6843
callq 0x2030
movl (%rax), %edi
addq $-0x20, %r14
leaq 0x41d8(%rip), %rsi      # 0xaa11
movq %r14, %rdx
xorl %eax, %eax
callq 0x6b27
movq $-0x1, %rbx
jmp 0x68a9
callq 0x2030
movq %rax, %r15
movl (%rax), %edi
addq $-0x20, %r14
leaq 0x41c9(%rip), %rsi      # 0xaa2a
jmp 0x6878
callq 0x2030
movq %rax, %r15
movl (%rax), %edi
addq $-0x20, %r14
leaq 0x41ed(%rip), %rsi      # 0xaa65
movq %r14, %rdx
xorl %eax, %eax
callq 0x6b27
movl %ebx, %edi
callq 0x2320
movq $-0x1, %rbx
cmpl $-0x1, %eax
jne 0x68a9
movl (%r15), %edi
leaq 0x41ae(%rip), %rsi      # 0xaa4d
movq %r14, %rdx
xorl %eax, %eax
callq 0x6b27
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq