// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdi, %r13
leaq -0x3c(%rbp), %rax
movl $0x1, (%rax)
pushq $0x5
popq %rcx
pushq $0x4
popq %r15
pushq $-0x1
popq %rax
movq %rax, -0x48(%rbp)
movq %rdi, -0x50(%rbp)
subq $0x1, %rcx
jb 0x6357
movq %rcx, -0x58(%rbp)
movq 0x40(%r13), %r14
addq $0x38, %r14
movq $0x0, -0x30(%rbp)
xorl %r12d, %r12d
cmpq 0x38(%r13), %r12
jae 0x62ac
cmpl $-0x1, -0x38(%r14)
jne 0x62a0
movzwl -0x34(%r14), %edi
movl -0x4(%r14), %esi
xorl %edx, %edx
callq 0x2080
cmpl $-0x1, %eax
je 0x62da
movl %eax, %ebx
movl %eax, %edi
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
leaq -0x3c(%rbp), %rcx
movl %r15d, %r8d
callq 0x2300
cmpl $-0x1, %eax
je 0x62ee
movl %ebx, %edi
pushq $0x3
popq %rsi
xorl %eax, %eax
callq 0x2170
orl $0x800, %eax            # imm = 0x800
movl %ebx, %edi
movl %r15d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x2170
cmpl $-0x1, %eax
je 0x6305
leaq -0x34(%r14), %r15
leaq -0x20(%r14), %rsi
leaq 0x390a(%rip), %rdi      # 0x9a8a
movq %rsi, -0x38(%rbp)
movl %ebx, %edx
xorl %eax, %eax
callq 0x6706
movl -0x24(%r14), %edx
movl %ebx, %edi
movq %r15, %rsi
callq 0x2050
cmpl $-0x1, %eax
je 0x61d8
cmpl $0x1, -0x4(%r14)
jne 0x61cb
movl (%r14), %edx
leaq 0x38fc(%rip), %rdi      # 0x9aad
movq -0x38(%rbp), %rsi
xorl %eax, %eax
callq 0x6706
movl (%r14), %esi
movl %ebx, %edi
callq 0x2230
cmpl $-0x1, %eax
je 0x6239
movl %ebx, -0x38(%r14)
pushq $0x4
popq %r15
jmp 0x62a0
callq 0x2030
movq %rax, %r15
movl (%rax), %r13d
cmpl $0x62, %r13d
je 0x61fe
movl %r13d, %edi
leaq 0x38a6(%rip), %rsi      # 0x9a99
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x65bd
movl %ebx, %edi
callq 0x2330
cmpl $-0x1, %eax
jne 0x621f
movl (%r15), %edi
leaq 0x383c(%rip), %rsi      # 0x9a50
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x65bd
pushq $0x1
popq %rax
movq %rax, -0x30(%rbp)
cmpl $0x62, %r13d
movq -0x50(%rbp), %r13
pushq $0x4
popq %r15
je 0x62a0
jmp 0x6344
callq 0x2030
movq %rax, -0x30(%rbp)
movl (%rax), %r13d
cmpl $0x62, %r13d
pushq $0x4
popq %r15
je 0x6267
movl (%r14), %ecx
movl %r13d, %edi
leaq 0x388c(%rip), %rsi      # 0x9ae8
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x65bd
movl %ebx, %edi
callq 0x2330
cmpl $-0x1, %eax
jne 0x628b
movq -0x30(%rbp), %rax
movl (%rax), %edi
leaq 0x37d0(%rip), %rsi      # 0x9a50
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x65bd
pushq $0x1
popq %rax
movq %rax, -0x30(%rbp)
cmpl $0x62, %r13d
movq -0x50(%rbp), %r13
jne 0x6344
incq %r12
addq $0x60, %r14
jmp 0x60fc
cmpq $0x0, -0x30(%rbp)
je 0x6369
xorl %edi, %edi
leaq 0x384a(%rip), %rsi      # 0x9b0a
xorl %eax, %eax
callq 0x65bd
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x22a0
movq -0x58(%rbp), %rcx
jmp 0x60db
callq 0x2030
movl (%rax), %edi
addq $-0x20, %r14
leaq 0x3728(%rip), %rsi      # 0x9a14
jmp 0x633a
callq 0x2030
movq %rax, %r15
movl (%rax), %edi
addq $-0x20, %r14
leaq 0x372a(%rip), %rsi      # 0x9a2d
jmp 0x631a
callq 0x2030
movq %rax, %r15
movl (%rax), %edi
addq $-0x20, %r14
leaq 0x374e(%rip), %rsi      # 0x9a68
movq %r14, %rdx
xorl %eax, %eax
callq 0x65bd
movl %ebx, %edi
callq 0x2330
cmpl $-0x1, %eax
jne 0x6344
movl (%r15), %edi
leaq 0x3716(%rip), %rsi      # 0x9a50
movq %r14, %rdx
xorl %eax, %eax
callq 0x65bd
movq -0x48(%rbp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x37cc(%rip), %rsi      # 0x9b2a
xorl %edi, %edi
xorl %eax, %eax
callq 0x65bd
jmp 0x6344
movq $0x0, -0x48(%rbp)
jmp 0x6344