// File: /workspace/github_c_makefile_star2_O2/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 0xb1f0(%rip), %rax      # 0xe2c8
movq %rax, (%rax)
movq %rax, 0x8(%rax)
leaq 0xb1f2(%rip), %rax      # 0xe2d8
movq %rax, (%rax)
movq %rax, 0x8(%rax)
callq 0x39e8
pushq $-0x1
popq %r14
cmpq $-0x1, %rax
je 0x3296
callq 0x2abc
cmpq $-0x1, %rax
je 0x3296
movq $0x4e20, 0x8(%rbx)      # imm = 0x4E20
movl $0x30d400, %edi         # imm = 0x30D400
callq 0x244c
movq %rax, 0x10(%rbx)
movq 0x8(%rbx), %rdx
testq %rax, %rax
je 0x3197
movq %rax, %r15
imulq $0x58, %rdx, %rdi
callq 0x244c
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x3275
addq $0x8, %rax
xorl %ecx, %ecx
movq 0x8(%rbx), %rdx
cmpq %rdx, %rcx
jae 0x3165
orl $0x10004, (%rax)        # imm = 0x10004
incq %rcx
addq $0x58, %rax
jmp 0x314d
imulq $0x58, %rdx, %rdi
callq 0x244c
movq %rax, 0x30(%rbx)
testq %rax, %rax
je 0x3282
addq $0x8, %rax
xorl %edx, %edx
movq 0x8(%rbx), %rcx
cmpq %rcx, %rdx
jae 0x31a3
orb $0x1, 0x2(%rax)
incq %rdx
addq $0x58, %rax
jmp 0x3181
leaq 0x63f7(%rip), %rsi      # 0x9595
jmp 0x328d
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %rdx
imulq $0x58, %rcx, %rsi
addq %rsi, %rdx
addq $-0x58, %rdx
addq %rsi, %rax
addq $-0x58, %rax
imulq $0xa0, %rcx, %rsi
leaq (%r15,%rsi), %r8
addq $-0xa0, %r8
xorl %edi, %edi
movq %rcx, %rsi
orl $-0x1, 0x18(%r8)
movq %rdi, (%r8)
movq %rax, 0x8(%r8)
movq %rdx, 0x10(%r8)
addq $-0x58, %rdx
addq $-0x58, %rax
movq %r8, %rdi
addq $-0xa0, %r8
decq %rsi
jne 0x31d4
movq %r15, 0x18(%rbx)
movq %rcx, 0x20(%rbx)
movq 0x40(%rbx), %r12
pushq $-0x1
popq %r13
xorl %r14d, %r14d
pushq $0x1
popq %r15
incq %r13
cmpq 0x38(%rbx), %r13
jae 0x3296
movl (%r12), %edi
callq 0x273c
testq %rax, %rax
je 0x32a8
movl 0x34(%r12), %ecx
movl %ecx, 0x48(%rax)
movq %r12, 0x38(%rax)
movq %rax, 0x58(%r12)
movq 0x8(%rax), %rdi
orl $0x2, 0x8(%rdi)
cmpl $0x1, 0x48(%rax)
leaq 0x49b(%rip), %rax       # 0x36e8
jne 0x3256
leaq 0x126(%rip), %rax       # 0x337c
addq $0x60, %r12
movq %rax, 0x10(%rdi)
movq %r15, %rsi
callq 0x2b90
cmpq $-0x1, %rax
jne 0x3212
leaq 0x6397(%rip), %rsi      # 0x960a
jmp 0x32af
movq 0x8(%rbx), %rdx
leaq 0x6332(%rip), %rsi      # 0x95b2
jmp 0x328d
movq 0x8(%rbx), %rdx
leaq 0x6343(%rip), %rsi      # 0x95d0
xorl %edi, %edi
xorl %eax, %eax
callq 0x65bd
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6340(%rip), %rsi      # 0x95ef
xorl %edi, %edi
xorl %eax, %eax
callq 0x65bd
pushq $-0x1
popq %r14
jmp 0x3296