// File: /workspace/github_c_makefile_star2_O1/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, %rbx
leaq 0x7245(%rip), %rdi      # 0xa660
movq %rbx, %rsi
xorl %eax, %eax
callq 0x6daa
testb $0x4, 0x9(%rbx)
je 0x3447
leaq 0xbe7e(%rip), %rax      # 0xf2b0
movq (%rax), %rdi
callq 0x36c2
testq %rax, %rax
jne 0x36b3
andb $-0x5, 0x9(%rbx)
movq (%rbx), %r14
movq 0x38(%r14), %rax
movq %rbx, -0x38(%rbp)
andl $-0x21, 0x8(%rbx)
movq %rax, -0x40(%rbp)
leaq 0x18(%rax), %rsi
leaq 0x720f(%rip), %rdi      # 0xa674
xorl %eax, %eax
callq 0x6daa
leaq -0x50(%rbp), %rbx
leaq -0x2c(%rbp), %r12
leaq 0x7219(%rip), %r13      # 0xa694
movl $0x10, -0x2c(%rbp)
movl 0x18(%r14), %edi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x20a0
cmpl $-0x1, %eax
jne 0x3525
callq 0x2030
movl (%rax), %r15d
cmpl $0xb, %r15d
je 0x3662
movl %r15d, %edi
movq %r13, %rsi
xorl %eax, %eax
callq 0x6c5d
cmpl $0x67, %r15d
je 0x347b
addl $-0x17, %r15d
cmpl $0x1, %r15d
ja 0x36b3
leaq 0xbddc(%rip), %rax      # 0xf2b0
movq (%rax), %rbx
cmpq $0x0, 0x38(%rbx)
je 0x36b3
movq 0x40(%rbx), %r14
addq $0x58, %r14
xorl %r15d, %r15d
movq (%r14), %rax
testq %rax, %rax
je 0x3513
movq 0x8(%rax), %rdi
testb $0x8, 0x8(%rdi)
je 0x3513
movl $0x1, %esi
callq 0x2c96
cmpq $-0x1, %rax
je 0x36b3
incq %r15
addq $0x60, %r14
cmpq 0x38(%rbx), %r15
jb 0x34ed
jmp 0x36b3
movl %eax, %r15d
movl %eax, %edi
callq 0x2738
testq %rax, %rax
je 0x3672
movq %rax, %rbx
movl $0x1, 0x48(%rax)
movq -0x40(%rbp), %r14
movq 0x50(%r14), %rdi
callq 0x7dcc
movq %rax, 0x40(%rbx)
testq %rax, %rax
je 0x36ab
movl -0x2c(%rbp), %esi
movq %rax, %rdi
callq 0x7e00
movq %rax, 0x50(%rbx)
testq %rax, %rax
je 0x36ab
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 0x3696
leaq 0x53a8(%rip), %rax      # 0x8960
movq %rax, 0x28(%rbx)
leaq 0x54d9(%rip), %rax      # 0x8a9c
movq %rax, 0x30(%rbx)
movl -0x2c(%rbp), %eax
movl %eax, 0x58(%rbx)
movq %r14, 0x38(%rbx)
leaq 0x4(%r14), %rax
movq %rax, 0x70(%rbx)
movl 0x14(%r14), %eax
movl %eax, 0x78(%rbx)
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
orl $0x20, 0x8(%rcx)
movq -0x38(%rbp), %rcx
testb $0x20, 0x9(%rcx)
je 0x35fa
orb $0x20, 0x8(%rax)
movq 0x40(%rbx), %rdi
movl $0x16, %esi
callq 0x7e27
movq %rax, 0x60(%rbx)
testq %rax, %rax
je 0x36ab
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0xe(%rax)
movq 0x50(%rbx), %rdi
movq 0x60(%rbx), %rsi
callq 0x3b98
movl %eax, 0x68(%rbx)
testl %eax, %eax
je 0x36ab
movq 0x60(%rbx), %rsi
leaq 0x7098(%rip), %rdi      # 0xa6da
movl %eax, %edx
xorl %eax, %eax
callq 0x6daa
movq %rbx, %rdi
callq 0x2d6b
cmpq $-0x1, %rax
je 0x36ab
movq %rbx, %rdi
callq *0x48(%r14)
jmp 0x36b3
leaq 0x7018(%rip), %rdi      # 0xa681
xorl %eax, %eax
callq 0x6daa
jmp 0x36b3
movl %r15d, %edi
callq 0x2320
cmpl $-0x1, %eax
jne 0x36b3
callq 0x2030
movl (%rax), %edi
leaq 0x7017(%rip), %rsi      # 0xa6a4
xorl %eax, %eax
callq 0x6c5d
jmp 0x36b3
callq 0x2030
movl (%rax), %edi
leaq 0x7015(%rip), %rsi      # 0xa6b9
xorl %eax, %eax
callq 0x6c5d
movq %rbx, %rdi
callq 0x372c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq