// File: /workspace/github_c_makefile_star2_O3/qq-smart[P]qq_sh_server/event/qq_event_accept.c
// Function: qq_event_accept
void
qq_event_accept(qq_event_t *ev)
{
    socklen_t          socklen;
    qq_err_t           err;
    qq_socket_t        s;
    qq_event_t        *rev, *wev;
    struct sockaddr    sa;
    qq_listening_t    *ls;
    qq_connection_t   *c, *lc;

    qq_log_debug("qq_event_accept(%p)", ev);

    if (ev->timedout) {
        if (qq_enable_accept_events((qq_cycle_t *) qq_cycle) != QQ_OK) {
            return;
        }
        ev->timedout = 0;
    }

    lc = ev->data;
    ls = lc->listening;
    ev->ready = 0;

    qq_log_debug("accept on %s", ls->addr_text);

    for (;;) {
        socklen = sizeof(struct sockaddr);

        s = accept(lc->fd, &sa, &socklen);
        if (s == (qq_socket_t) -1) {
            err = errno;

            if (err == QQ_EAGAIN) {
                qq_log_debug("accept() not ready");
                return;
            }

            qq_log_error(err, "accept() failed");

            if (err == QQ_ECONNABORTED) {
                continue;
            }

            if (err == QQ_EMFILE || err == QQ_ENFILE) {
                if (qq_disable_accept_events((qq_cycle_t *) qq_cycle)
                    != QQ_OK)
                {
                    return;
                }
            }
            return;
        }

        c = qq_get_connection(s);
        if (c == NULL) {
            if (close(s) == -1) {
                qq_log_error(errno, "close sockets failed");
            }
            return;
        }
        c->type = SOCK_STREAM;

        c->pool = qq_create_pool(ls->pool_size);
        if (c->pool == NULL) {
            qq_close_accepted_connection(c);
            return;
        }

        c->sockaddr = qq_palloc(c->pool, socklen);
        if (c->sockaddr == NULL) {
            qq_close_accepted_connection(c);
            return;
        }
        memcpy(c->sockaddr, &sa, socklen);

        if (qq_nonblocking(s) == -1) {
            qq_log_error(errno, "set sockets nonblocking_n failed");
            qq_close_accepted_connection(c);
            return;
        }

        c->recv = qq_tcp_recv;
        c->send = qq_tcp_send;

        c->socklen = socklen;
        c->listening = ls;
        c->local_sockaddr = (struct sockaddr*) &ls->sockaddr;
        c->local_socklen = ls->socklen;

        rev = c->read;
        wev = c->write;
        wev->ready = 1;

        if (ev->deferred_accept) {
            rev->ready = 1;
        }

        c->addr_text = qq_pnalloc(c->pool, QQ_SOCKADDR_STRLEN);
        if (c->addr_text == NULL) {
            qq_close_accepted_connection(c);
            return;
        }
        memset(c->addr_text, 0, QQ_SOCKADDR_STRLEN);
        c->addr_text_len = qq_sock_ntop((struct sockaddr_in *)c->sockaddr, c->addr_text);
        if (c->addr_text_len == 0) {
            qq_close_accepted_connection(c);
            return;
        }        
        qq_log_debug("connection address: %s, address len: %d",
            c->addr_text, c->addr_text_len);

        if (qq_epoll_add_connection(c) == QQ_ERROR) {
            qq_close_accepted_connection(c);
            return;
        }

        ls->handler(c);
        return;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
leaq 0x7249(%rip), %rdi      # 0xa660
movq %r14, %rsi
xorl %eax, %eax
callq 0x6c74
movl 0x8(%r14), %eax
btl $0xa, %eax
jae 0x3450
leaq 0xbe7e(%rip), %rax      # 0xf2b0
movq (%rax), %rdi
callq 0x36d3
testq %rax, %rax
jne 0x36c4
movl $0xfffffbff, %eax       # imm = 0xFFFFFBFF
andl 0x8(%r14), %eax
movl %eax, 0x8(%r14)
movq (%r14), %rbx
movq 0x38(%rbx), %rcx
andl $-0x21, %eax
movq %r14, -0x38(%rbp)
movl %eax, 0x8(%r14)
movq %rcx, -0x40(%rbp)
leaq 0x18(%rcx), %rsi
leaq 0x7203(%rip), %rdi      # 0xa674
xorl %eax, %eax
callq 0x6c74
leaq -0x50(%rbp), %r14
leaq -0x2c(%rbp), %r12
leaq 0x720d(%rip), %r13      # 0xa694
movl $0x10, -0x2c(%rbp)
movl 0x18(%rbx), %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x20a0
cmpl $-0x1, %eax
jne 0x3535
callq 0x2030
movl (%rax), %r15d
cmpl $0xb, %r15d
je 0x3673
movl %r15d, %edi
movq %r13, %rsi
xorl %eax, %eax
callq 0x6b27
cmpl $0x67, %r15d
je 0x3487
addl $-0x17, %r15d
cmpl $0x1, %r15d
ja 0x36c4
leaq 0xbdd1(%rip), %rax      # 0xf2b0
movq (%rax), %rbx
movq 0x38(%rbx), %rax
testq %rax, %rax
je 0x36c4
movq 0x40(%rbx), %r14
addq $0x58, %r14
xorl %r15d, %r15d
movq (%r14), %rcx
testq %rcx, %rcx
je 0x3524
movq 0x8(%rcx), %rdi
testb $0x8, 0x8(%rdi)
je 0x3524
movl $0x1, %esi
callq 0x2c8a
cmpq $-0x1, %rax
je 0x36c4
movq 0x38(%rbx), %rax
incq %r15
addq $0x60, %r14
cmpq %rax, %r15
jb 0x34fa
jmp 0x36c4
movl %eax, %r15d
movl %eax, %edi
callq 0x2738
testq %rax, %rax
je 0x3683
movq %rax, %r14
movl $0x1, 0x48(%rax)
movq -0x40(%rbp), %rbx
movq 0x50(%rbx), %rdi
callq 0x7ca0
movq %rax, 0x40(%r14)
testq %rax, %rax
je 0x36bc
movl -0x2c(%rbp), %esi
movq %rax, %rdi
callq 0x7cd0
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x36bc
movl -0x2c(%rbp), %edx
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x21c0
movl %r15d, %edi
movl $0x3, %esi
xorl %eax, %eax
callq 0x2170
orl $0x800, %eax            # imm = 0x800
movl %r15d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x2170
cmpl $-0x1, %eax
je 0x36a7
leaq 0x5204(%rip), %rax      # 0x87cc
movq %rax, 0x28(%r14)
leaq 0x5331(%rip), %rax      # 0x8904
movq %rax, 0x30(%r14)
movl -0x2c(%rbp), %eax
movl %eax, 0x58(%r14)
movq %rbx, 0x38(%r14)
leaq 0x4(%rbx), %rax
movq %rax, 0x70(%r14)
movl 0x14(%rbx), %eax
movl %eax, 0x78(%r14)
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
orl $0x20, 0x8(%rcx)
movq -0x38(%rbp), %rcx
testb $0x20, 0x9(%rcx)
je 0x360b
orb $0x20, 0x8(%rax)
movq 0x40(%r14), %rdi
movl $0x16, %esi
callq 0x7cf7
movq %rax, 0x60(%r14)
testq %rax, %rax
je 0x36bc
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0xe(%rax)
movq 0x50(%r14), %rdi
movq 0x60(%r14), %rsi
callq 0x3ba8
movl %eax, 0x68(%r14)
testl %eax, %eax
je 0x36bc
movq 0x60(%r14), %rsi
leaq 0x7086(%rip), %rdi      # 0xa6da
movl %eax, %edx
xorl %eax, %eax
callq 0x6c74
movq %r14, %rdi
callq 0x2d57
cmpq $-0x1, %rax
je 0x36bc
movq %r14, %rdi
callq *0x48(%rbx)
jmp 0x36c4
leaq 0x7007(%rip), %rdi      # 0xa681
xorl %eax, %eax
callq 0x6c74
jmp 0x36c4
movl %r15d, %edi
callq 0x2320
cmpl $-0x1, %eax
jne 0x36c4
callq 0x2030
movl (%rax), %edi
leaq 0x7006(%rip), %rsi      # 0xa6a4
xorl %eax, %eax
callq 0x6b27
jmp 0x36c4
callq 0x2030
movl (%rax), %edi
leaq 0x7004(%rip), %rsi      # 0xa6b9
xorl %eax, %eax
callq 0x6b27
movq %r14, %rdi
callq 0x3742
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq