// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movq $0x5, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x98bb
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0x9887
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rcx
addq %rcx, %rax
cmpl $-0x1, (%rax)
je 0x950a
jmp 0x9876
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rcx
addq %rcx, %rax
movzwl 0x4(%rax), %edi
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rcx
addq %rcx, %rax
movl 0x34(%rax), %esi
xorl %edx, %edx
callq 0x2080
movl %eax, -0x38(%rbp)
cmpl $-0x1, -0x38(%rbp)
jne 0x956b
callq 0x2030
movl (%rax), %edi
movq -0x40(%rbp), %rdx
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rdx
addq $0x18, %rdx
leaq 0x64b8(%rip), %rsi      # 0xfa0f
movb $0x0, %al
callq 0x9cc0
movq $-0x1, -0x8(%rbp)
jmp 0x98e4
movl -0x38(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x14(%rbp), %rcx
movl $0x4, %r8d
callq 0x2310
cmpl $-0x1, %eax
jne 0x95f0
callq 0x2030
movl (%rax), %edi
movq -0x40(%rbp), %rdx
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rdx
addq $0x18, %rdx
leaq 0x647e(%rip), %rsi      # 0xfa28
movb $0x0, %al
callq 0x9cc0
movl -0x38(%rbp), %edi
callq 0x2350
cmpl $-0x1, %eax
jne 0x95e3
callq 0x2030
movl (%rax), %edi
movq -0x40(%rbp), %rdx
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rdx
addq $0x18, %rdx
leaq 0x646f(%rip), %rsi      # 0xfa4b
movb $0x0, %al
callq 0x9cc0
movq $-0x1, -0x8(%rbp)
jmp 0x98e4
movl -0x38(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x38(%rbp), %edi
movl $0x3, %esi
movb $0x0, %al
callq 0x2180
movl -0x4c(%rbp), %edi
movl %eax, %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x2180
cmpl $-0x1, %eax
jne 0x9685
callq 0x2030
movl (%rax), %edi
movq -0x40(%rbp), %rdx
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rdx
addq $0x18, %rdx
leaq 0x6424(%rip), %rsi      # 0xfa63
movb $0x0, %al
callq 0x9cc0
movl -0x38(%rbp), %edi
callq 0x2350
cmpl $-0x1, %eax
jne 0x9678
callq 0x2030
movl (%rax), %edi
movq -0x40(%rbp), %rdx
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rdx
addq $0x18, %rdx
leaq 0x63da(%rip), %rsi      # 0xfa4b
movb $0x0, %al
callq 0x9cc0
movq $-0x1, -0x8(%rbp)
jmp 0x98e4
movq -0x40(%rbp), %rsi
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rsi
addq $0x18, %rsi
movl -0x38(%rbp), %edx
leaq 0x63e6(%rip), %rdi      # 0xfa85
movb $0x0, %al
callq 0x9e70
movl -0x38(%rbp), %edi
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rcx
addq %rcx, %rax
addq $0x4, %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rcx
addq %rcx, %rax
movl 0x14(%rax), %edx
movq -0x48(%rbp), %rsi
callq 0x2060
cmpl $-0x1, %eax
jne 0x9761
callq 0x2030
movl (%rax), %eax
movl %eax, -0x34(%rbp)
cmpl $0x62, -0x34(%rbp)
je 0x970f
movl -0x34(%rbp), %edi
movq -0x40(%rbp), %rdx
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rdx
addq $0x18, %rdx
leaq 0x638c(%rip), %rsi      # 0xfa94
movb $0x0, %al
callq 0x9cc0
movl -0x38(%rbp), %edi
callq 0x2350
cmpl $-0x1, %eax
jne 0x9741
callq 0x2030
movl (%rax), %edi
movq -0x40(%rbp), %rdx
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rdx
addq $0x18, %rdx
leaq 0x6311(%rip), %rsi      # 0xfa4b
movb $0x0, %al
callq 0x9cc0
cmpl $0x62, -0x34(%rbp)
je 0x9754
movq $-0x1, -0x8(%rbp)
jmp 0x98e4
movq $0x1, -0x30(%rbp)
jmp 0x9876
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rcx
addq %rcx, %rax
cmpl $0x1, 0x34(%rax)
je 0x9789
movl -0x38(%rbp), %ecx
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rdx
addq %rdx, %rax
movl %ecx, (%rax)
jmp 0x9876
movq -0x40(%rbp), %rsi
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rsi
addq $0x18, %rsi
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rcx
addq %rcx, %rax
movl 0x38(%rax), %edx
leaq 0x62f9(%rip), %rdi      # 0xfaa8
movb $0x0, %al
callq 0x9e70
movl -0x38(%rbp), %edi
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rcx
addq %rcx, %rax
movl 0x38(%rax), %esi
callq 0x2230
cmpl $-0x1, %eax
jne 0x9865
callq 0x2030
movl (%rax), %eax
movl %eax, -0x34(%rbp)
cmpl $0x62, -0x34(%rbp)
je 0x9816
movl -0x34(%rbp), %edi
movq -0x40(%rbp), %rdx
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rdx
addq $0x18, %rdx
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rcx
addq %rcx, %rax
movl 0x38(%rax), %ecx
leaq 0x62d4(%rip), %rsi      # 0xfae3
movb $0x0, %al
callq 0x9cc0
movl -0x38(%rbp), %edi
callq 0x2350
cmpl $-0x1, %eax
jne 0x9848
callq 0x2030
movl (%rax), %edi
movq -0x40(%rbp), %rdx
imulq $0x60, -0x20(%rbp), %rax
addq %rax, %rdx
addq $0x18, %rdx
leaq 0x620a(%rip), %rsi      # 0xfa4b
movb $0x0, %al
callq 0x9cc0
cmpl $0x62, -0x34(%rbp)
je 0x985b
movq $-0x1, -0x8(%rbp)
jmp 0x98e4
movq $0x1, -0x30(%rbp)
jmp 0x9876
movl -0x38(%rbp), %ecx
movq -0x40(%rbp), %rax
imulq $0x60, -0x20(%rbp), %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x94e2
cmpq $0x0, -0x30(%rbp)
jne 0x9890
jmp 0x98bb
xorl %edi, %edi
leaq 0x626c(%rip), %rsi      # 0xfb05
movb $0x0, %al
callq 0x9cc0
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x22a0
movq -0x28(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x94bb
cmpq $0x0, -0x30(%rbp)
je 0x98dc
xorl %edi, %edi
leaq 0x625a(%rip), %rsi      # 0xfb25
movb $0x0, %al
callq 0x9cc0
movq $-0x1, -0x8(%rbp)
jmp 0x98e4
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nop