// File: /workspace/github_c_makefile_star2_O2/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 0x62c9(%rip), %rdi      # 0x9660
movq %r14, %rsi
xorl %eax, %eax
callq 0x6706
movl 0x8(%r14), %eax
btl $0xa, %eax
jae 0x33d0
leaq 0xaefe(%rip), %rax      # 0xe2b0
movq (%rax), %rdi
callq 0x363c
testq %rax, %rax
jne 0x362d
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 0x6283(%rip), %rdi      # 0x9674
xorl %eax, %eax
callq 0x6706
leaq -0x50(%rbp), %r14
leaq -0x2c(%rbp), %r12
leaq 0x628d(%rip), %r13      # 0x9694
movl $0x10, -0x2c(%rbp)
movl 0x18(%rbx), %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x20a0
cmpl $-0x1, %eax
jne 0x34a7
callq 0x2030
movl (%rax), %r15d
cmpl $0xb, %r15d
je 0x35dc
movl %r15d, %edi
movq %r13, %rsi
xorl %eax, %eax
callq 0x65bd
cmpl $0x67, %r15d
je 0x3407
addl $-0x17, %r15d
cmpl $0x1, %r15d
ja 0x362d
leaq 0xae51(%rip), %rax      # 0xe2b0
movq (%rax), %r14
movq 0x40(%r14), %r15
addq $0x58, %r15
xorl %r12d, %r12d
pushq $0x1
popq %rbx
cmpq 0x38(%r14), %r12
jae 0x362d
movq (%r15), %rax
testq %rax, %rax
je 0x349e
movq 0x8(%rax), %rdi
testb $0x8, 0x8(%rdi)
je 0x349e
movq %rbx, %rsi
callq 0x2c57
cmpq $-0x1, %rax
je 0x362d
incq %r12
addq $0x60, %r15
jmp 0x3470
movl %eax, %r15d
movl %eax, %edi
callq 0x273c
testq %rax, %rax
je 0x35ec
movq %rax, %r14
movl $0x1, 0x48(%rax)
movq -0x40(%rbp), %rbx
movq 0x50(%rbx), %rdi
callq 0x7620
movq %rax, 0x40(%r14)
testq %rax, %rax
je 0x3625
movl -0x2c(%rbp), %esi
movq %rax, %rdi
callq 0x764e
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x3625
movl -0x2c(%rbp), %edx
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x21c0
pushq $0x3
popq %rsi
movl %r15d, %edi
xorl %eax, %eax
callq 0x2170
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %r15d, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x2170
cmpl $-0x1, %eax
je 0x3610
leaq 0x4a7e(%rip), %rax      # 0x7fb4
movq %rax, 0x28(%r14)
leaq 0x4b9f(%rip), %rax      # 0x80e0
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 0x3579
orb $0x20, 0x8(%rax)
movq 0x40(%r14), %rdi
pushq $0x16
popq %rsi
callq 0x7675
movq %rax, 0x60(%r14)
testq %rax, %rax
je 0x3625
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0xe(%rax)
movq 0x50(%r14), %rdi
movq 0x60(%r14), %rsi
callq 0x3ae8
movl %eax, 0x68(%r14)
testl %eax, %eax
je 0x3625
movq 0x60(%r14), %rsi
leaq 0x611d(%rip), %rdi      # 0x96da
movl %eax, %edx
xorl %eax, %eax
callq 0x6706
movq %r14, %rdi
callq 0x2d1c
cmpq $-0x1, %rax
je 0x3625
movq %r14, %rdi
callq *0x48(%rbx)
jmp 0x362d
leaq 0x609e(%rip), %rdi      # 0x9681
xorl %eax, %eax
callq 0x6706
jmp 0x362d
movl %r15d, %edi
callq 0x2330
cmpl $-0x1, %eax
jne 0x362d
callq 0x2030
movl (%rax), %edi
leaq 0x609d(%rip), %rsi      # 0x96a4
xorl %eax, %eax
callq 0x65bd
jmp 0x362d
callq 0x2030
movl (%rax), %edi
leaq 0x609b(%rip), %rsi      # 0x96b9
xorl %eax, %eax
callq 0x65bd
movq %r14, %rdi
callq 0x36a5
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq