// File: /workspace/github_c_makefile_star2_O3/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 0xc19c(%rip), %rax      # 0xf2c8
movq %rax, (%rax)
movq %rax, 0x8(%rax)
leaq 0xc19e(%rip), %rax      # 0xf2d8
movq %rax, (%rax)
movq %rax, 0x8(%rax)
callq 0x3aa0
movq $-0x1, %r14
cmpq $-0x1, %rax
je 0x3324
callq 0x2adc
cmpq $-0x1, %rax
je 0x3324
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 0x31c5
movq %rax, %r14
imulq $0x58, %rdx, %rdi
callq 0x243c
movq %rax, 0x28(%rbx)
movq 0x8(%rbx), %rdx
testq %rax, %rax
je 0x31da
testq %rdx, %rdx
je 0x31e3
addq $0x8, %rax
xorl %ecx, %ecx
orl $0x10004, (%rax)        # imm = 0x10004
incq %rcx
movq 0x8(%rbx), %rdx
addq $0x58, %rax
cmpq %rdx, %rcx
jb 0x31a9
imulq $0x58, %rdx, %rdi
jmp 0x31e5
leaq 0x73c9(%rip), %rsi      # 0xa595
xorl %edi, %edi
xorl %eax, %eax
callq 0x6b27
jmp 0x3324
leaq 0x73d1(%rip), %rsi      # 0xa5b2
jmp 0x3223
xorl %edi, %edi
callq 0x243c
movq %rax, 0x30(%rbx)
movq 0x8(%rbx), %rdx
testq %rax, %rax
je 0x321c
testq %rdx, %rdx
je 0x3231
addq $0x8, %rax
xorl %edx, %edx
orb $0x1, 0x2(%rax)
incq %rdx
movq 0x8(%rbx), %rcx
addq $0x58, %rax
cmpq %rcx, %rdx
jb 0x3202
movq 0x30(%rbx), %rax
jmp 0x3233
leaq 0x73ad(%rip), %rsi      # 0xa5d0
xorl %edi, %edi
xorl %eax, %eax
callq 0x6b27
jmp 0x331d
xorl %ecx, %ecx
imulq $0x58, %rcx, %rdx
addq %rdx, %rax
addq $-0x58, %rax
addq $-0x58, %rdx
addq 0x28(%rbx), %rdx
leaq (%rcx,%rcx,4), %rsi
shlq $0x5, %rsi
leaq (%rsi,%r14), %r8
addq $-0xa0, %r8
xorl %edi, %edi
movq %rcx, %rsi
movq %rdi, (%r8)
movq %rdx, 0x8(%r8)
movq %rax, 0x10(%r8)
addq $-0x58, %rax
addq $-0x58, %rdx
movl $0xffffffff, 0x18(%r8)  # imm = 0xFFFFFFFF
movq %r8, %rdi
addq $-0xa0, %r8
decq %rsi
jne 0x325e
movq %r14, 0x18(%rbx)
movq %rcx, 0x20(%rbx)
cmpq $0x0, 0x38(%rbx)
je 0x32ff
movq 0x40(%rbx), %r15
xorl %r14d, %r14d
leaq 0x4eb(%rip), %r12       # 0x3790
xorl %r13d, %r13d
movl (%r15), %edi
callq 0x2738
testq %rax, %rax
je 0x3304
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 0x32dc
leaq 0x120(%rip), %rax       # 0x33fc
movq %rax, 0x10(%rdi)
movl $0x1, %esi
callq 0x2bbf
cmpq $-0x1, %rax
je 0x330d
incq %r13
addq $0x60, %r15
cmpq 0x38(%rbx), %r13
jb 0x32a8
jmp 0x3324
xorl %r14d, %r14d
jmp 0x3324
leaq 0x72e4(%rip), %rsi      # 0xa5ef
jmp 0x3314
leaq 0x72f6(%rip), %rsi      # 0xa60a
xorl %edi, %edi
xorl %eax, %eax
callq 0x6b27
movq $-0x1, %r14
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq