// File: /workspace/github_c_makefile_star2_O1/qq-smart[P]qq_sh_server/event/qq_event.c
// Function: qq_event_init
qq_int_t
qq_event_init(qq_cycle_t *cycle)
{
    qq_uint_t           i;
    qq_event_t         *rev, *wev;
    qq_listening_t     *ls;
    qq_connection_t    *c, *next;

    qq_queue_init(&qq_posted_accept_events);
    qq_queue_init(&qq_posted_events);

    if (qq_event_timer_init() == QQ_ERROR) {
        return QQ_ERROR;
    }
	
    if (qq_epoll_init() == QQ_ERROR) {
        return QQ_ERROR;
    }

    cycle->connection_n = QQ_CONNECTION_NUMBER;
    cycle->connections = qq_alloc(sizeof(qq_connection_t) * cycle->connection_n);
    if (cycle->connections == NULL) {
        qq_log_error(0, "connection malloc(%d) failed", cycle->connection_n);
        return QQ_ERROR;
    }
    c = cycle->connections;

    cycle->read_events = qq_alloc(sizeof(qq_event_t) * cycle->connection_n);
    if (cycle->read_events == NULL) {
        qq_log_error(0, "read_events malloc(%d) failed", cycle->connection_n);
        return QQ_ERROR;
    }
    rev = cycle->read_events;
    for (i = 0; i < cycle->connection_n; i++) {
        rev[i].closed = 1;
        rev[i].instance = 1;
    }

    cycle->write_events = qq_alloc(sizeof(qq_event_t) * cycle->connection_n);
    if (cycle->write_events == NULL) {
        qq_log_error(0, "write_events malloc(%d) failed", cycle->connection_n);
        return QQ_ERROR;
    }
    wev = cycle->write_events;
    for (i = 0; i < cycle->connection_n; i++) {
        wev[i].closed = 1;
    }

    i = cycle->connection_n;
    next = NULL;
    do {
        i--;

        c[i].data = next;
        c[i].read = &cycle->read_events[i];
        c[i].write = &cycle->write_events[i];
        c[i].fd = (qq_socket_t) -1;

        next = &c[i];
    } while (i);
    cycle->free_connections = next;
    cycle->free_connection_n = cycle->connection_n;

    ls = cycle->listening;
    for (i = 0; i < cycle->nlistening; i++) {
        c = qq_get_connection(ls[i].fd);
        if (c == NULL) {
            qq_log_error(0, "qq_get_connection() failed");
            return QQ_ERROR;
        }

        c->type = ls[i].type;
        c->listening = &ls[i];
        ls[i].connection = c;

        rev = c->read;
        rev->accept = 1;

        rev->handler = (c->type == SOCK_STREAM) ? qq_event_accept
                                                : qq_event_recvmsg;

        if (qq_epoll_add_event(rev, QQ_READ_EVENT) == QQ_ERROR) {
            qq_log_error(0, "qq_epoll_add_event() failed");
            return QQ_ERROR;
        }
    }

    return QQ_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0xc194(%rip), %rax      # 0xf2c8
movq %rax, (%rax)
movq %rax, 0x8(%rax)
leaq 0xc196(%rip), %rax      # 0xf2d8
movq %rax, (%rax)
movq %rax, 0x8(%rax)
callq 0x3a90
movq $-0x1, %r14
cmpq $-0x1, %rax
je 0x332a
callq 0x2ae8
cmpq $-0x1, %rax
je 0x332a
movq $0x4e20, 0x8(%rbx)      # imm = 0x4E20
movl $0x30d400, %edi         # imm = 0x30D400
callq 0x243c
movq %rax, 0x10(%rbx)
movq 0x8(%rbx), %rdx
testq %rax, %rax
je 0x32d8
movq %rax, %r14
imulq $0x58, %rdx, %rdi
callq 0x243c
movq %rax, 0x28(%rbx)
movq 0x8(%rbx), %rdx
testq %rax, %rax
je 0x32ea
testq %rdx, %rdx
je 0x31cf
addq $0x8, %rax
xorl %ecx, %ecx
orl $0x10004, (%rax)        # imm = 0x10004
incq %rcx
movq 0x8(%rbx), %rdx
addq $0x58, %rax
cmpq %rdx, %rcx
jb 0x31b9
imulq $0x58, %rdx, %rdi
callq 0x243c
movq %rax, 0x30(%rbx)
movq 0x8(%rbx), %rdx
testq %rax, %rax
je 0x32f3
testq %rdx, %rdx
je 0x3208
addq $0x8, %rax
xorl %ecx, %ecx
orb $0x1, 0x2(%rax)
incq %rcx
movq 0x8(%rbx), %rdx
addq $0x58, %rax
cmpq %rdx, %rcx
jb 0x31f4
imulq $0x58, %rdx, %rax
addq $-0x58, %rax
movq 0x30(%rbx), %rcx
addq %rax, %rcx
addq 0x28(%rbx), %rax
leaq (%rdx,%rdx,4), %rsi
shlq $0x5, %rsi
leaq (%rsi,%r14), %r8
addq $-0xa0, %r8
xorl %edi, %edi
movq %rdx, %rsi
movq %rdi, (%r8)
movq %rax, 0x8(%r8)
movq %rcx, 0x10(%r8)
addq $-0x58, %rcx
addq $-0x58, %rax
movl $0xffffffff, 0x18(%r8)  # imm = 0xFFFFFFFF
movq %r8, %rdi
addq $-0xa0, %r8
decq %rsi
jne 0x3233
movq %r14, 0x18(%rbx)
movq %rdx, 0x20(%rbx)
cmpq $0x0, 0x38(%rbx)
je 0x3305
movq 0x40(%rbx), %r15
xorl %r14d, %r14d
leaq 0x4fc(%rip), %r12       # 0x377a
xorl %r13d, %r13d
movl (%r15), %edi
callq 0x2738
testq %rax, %rax
je 0x330a
movl 0x34(%r15), %ecx
movl %ecx, 0x48(%rax)
movq %r15, 0x38(%rax)
movq %rax, 0x58(%r15)
movq 0x8(%rax), %rdi
orl $0x2, 0x8(%rdi)
cmpl $0x1, 0x48(%rax)
movq %r12, %rax
jne 0x32b5
leaq 0x14b(%rip), %rax       # 0x3400
movq %rax, 0x10(%rdi)
movl $0x1, %esi
callq 0x2bcb
cmpq $-0x1, %rax
je 0x3313
incq %r13
addq $0x60, %r15
cmpq 0x38(%rbx), %r13
jb 0x3281
jmp 0x332a
leaq 0x72b6(%rip), %rsi      # 0xa595
xorl %edi, %edi
xorl %eax, %eax
callq 0x6c5d
jmp 0x332a
leaq 0x72c1(%rip), %rsi      # 0xa5b2
jmp 0x32fa
leaq 0x72d6(%rip), %rsi      # 0xa5d0
xorl %edi, %edi
xorl %eax, %eax
callq 0x6c5d
jmp 0x3323
xorl %r14d, %r14d
jmp 0x332a
leaq 0x72de(%rip), %rsi      # 0xa5ef
jmp 0x331a
leaq 0x72f0(%rip), %rsi      # 0xa60a
xorl %edi, %edi
xorl %eax, %eax
callq 0x6c5d
movq $-0x1, %r14
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq