// File: /workspace/github_c_makefile_star2_O1/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 0x69e8
movq %rax, -0x50(%rbp)
movq 0x40(%r12), %r14
addq $0x38, %r14
xorl %r13d, %r13d
movq $0x0, -0x38(%rbp)
cmpl $-0x1, -0x38(%r14)
jne 0x6908
movzwl -0x34(%r14), %edi
movl -0x4(%r14), %esi
xorl %edx, %edx
callq 0x2080
cmpl $-0x1, %eax
je 0x6966
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 0x698b
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 0x69a2
leaq -0x34(%r14), %r15
leaq -0x20(%r14), %rsi
leaq 0x4268(%rip), %rdi      # 0xaa97
movq %rsi, -0x30(%rbp)
movl %ebx, %edx
xorl %eax, %eax
callq 0x6daa
movl -0x24(%r14), %edx
movl %ebx, %edi
movq %r15, %rsi
callq 0x2050
cmpl $-0x1, %eax
je 0x6883
cmpl $0x1, -0x4(%r14)
jne 0x687a
movl (%r14), %edx
leaq 0x425a(%rip), %rdi      # 0xaaba
movq -0x30(%rbp), %rsi
xorl %eax, %eax
callq 0x6daa
movl (%r14), %esi
movl %ebx, %edi
callq 0x2220
cmpl $-0x1, %eax
je 0x68ab
movl %ebx, -0x38(%r14)
jmp 0x6908
callq 0x2030
movq %rax, %r15
movl (%rax), %r12d
cmpl $0x62, %r12d
je 0x68d4
movl %r12d, %edi
leaq 0x4208(%rip), %rsi      # 0xaaa6
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x6c5d
jmp 0x68d4
callq 0x2030
movq %rax, %r15
movl (%rax), %r12d
cmpl $0x62, %r12d
je 0x68d4
movl (%r14), %ecx
movl %r12d, %edi
leaq 0x422c(%rip), %rsi      # 0xaaf5
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x6c5d
movl %ebx, %edi
callq 0x2320
cmpl $-0x1, %eax
jne 0x68f5
movl (%r15), %edi
leaq 0x4173(%rip), %rsi      # 0xaa5d
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x6c5d
movl $0x1, %eax
movq %rax, -0x38(%rbp)
cmpl $0x62, %r12d
movq -0x48(%rbp), %r12
jne 0x6982
incq %r13
addq $0x60, %r14
cmpq 0x38(%r12), %r13
jb 0x67aa
cmpq $0x0, -0x38(%rbp)
movl $0x0, %ebx
movq -0x50(%rbp), %r14
je 0x69e8
xorl %edi, %edi
leaq 0x41e0(%rip), %rsi      # 0xab17
xorl %eax, %eax
callq 0x6c5d
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2290
decq %r14
movq %r14, %rax
jne 0x6786
leaq 0x41dc(%rip), %rsi      # 0xab37
xorl %edi, %edi
xorl %eax, %eax
callq 0x6c5d
jmp 0x6982
callq 0x2030
movl (%rax), %edi
addq $-0x20, %r14
leaq 0x40a9(%rip), %rsi      # 0xaa21
movq %r14, %rdx
xorl %eax, %eax
callq 0x6c5d
movq $-0x1, %rbx
jmp 0x69e8
callq 0x2030
movq %rax, %r15
movl (%rax), %edi
addq $-0x20, %r14
leaq 0x409a(%rip), %rsi      # 0xaa3a
jmp 0x69b7
callq 0x2030
movq %rax, %r15
movl (%rax), %edi
addq $-0x20, %r14
leaq 0x40be(%rip), %rsi      # 0xaa75
movq %r14, %rdx
xorl %eax, %eax
callq 0x6c5d
movl %ebx, %edi
callq 0x2320
movq $-0x1, %rbx
cmpl $-0x1, %eax
jne 0x69e8
movl (%r15), %edi
leaq 0x407f(%rip), %rsi      # 0xaa5d
movq %r14, %rdx
xorl %eax, %eax
callq 0x6c5d
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq