// File: shaovie[P]reactor/src/poller.cpp
// Function: poller::open(options const&)
int poller::open(const options &opt) {
    if (opt.poll_io_buf_size < 1) {
        fprintf(stderr, "reactor: poll_io_buf_size=%d < 1\n", opt.poll_io_buf_size);
        return -1;
    }
    if (opt.timer_init_size < 1) {
        fprintf(stderr, "reactor: timer_init_size=%d < 1\n", opt.timer_init_size);
        return -1;
    }
    if (opt.ready_events_size < 1) {
        fprintf(stderr, "reactor: ready_events_size=%d < 1\n", opt.ready_events_size);
        return -1;
    }
    if (opt.max_fd_estimate < 1) {
        fprintf(stderr, "reactor: max_fd_estimate=%d < 1\n", opt.max_fd_estimate);
        return -1;
    }
    int fd = ::epoll_create1(EPOLL_CLOEXEC);
    if (fd == -1) {
        fprintf(stderr, "reactor: epoll_create1 fail! %s\n", strerror(errno));
        return -1;
    }
    this->efd = fd;

    // The following failed operation, ignoring resource release
    this->poll_descs = new poll_desc_map(opt.max_fd_estimate);
    std::default_random_engine dre;
    dre.seed(this->efd);
    this->seq.store(dre(), std::memory_order_relaxed);

    this->ready_events_size = opt.ready_events_size;
    this->ready_events = new struct epoll_event[this->ready_events_size]();

    this->io_buf_size = opt.poll_io_buf_size;
    this->io_buf = new char[this->io_buf_size];

    this->timer = new timer_qheap(opt.timer_init_size);
    if (timer->open() == -1) {
        this->destroy();
        return -1;
    }

    if (this->add(timer, timer->get_fd(), ev_handler::ev_read) != 0) {
        fprintf(stderr, "reactor: add timer to poller fail! %s\n", strerror(errno));
        this->destroy();
        return -1;
    }

    this->async_sendq = new async_send(256);
    if (this->async_sendq->open(this) != 0) {
        this->destroy();
        return -1;
    }

    this->poll_sync_opterate = new poll_sync_opt(8);
    if (this->poll_sync_opterate->open(this) != 0) {
        this->destroy();
        return -1;
    }

    return 0;
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0xa0(%rsp), %rax
cmpl $0x1, 0x4(%rax)
jge 0x17d75
movq 0x12287(%rip), %rax     # 0x29fd0
movq (%rax), %rdi
movq 0xa0(%rsp), %rax
movl 0x4(%rax), %edx
leaq 0xb326(%rip), %rsi      # 0x23084
movb $0x0, %al
callq 0x15860
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x18273
movq 0xa0(%rsp), %rax
cmpl $0x1, 0x14(%rax)
jge 0x17db6
movq 0x12246(%rip), %rax     # 0x29fd0
movq (%rax), %rdi
movq 0xa0(%rsp), %rax
movl 0x14(%rax), %edx
leaq 0xb307(%rip), %rsi      # 0x230a6
movb $0x0, %al
callq 0x15860
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x18273
movq 0xa0(%rsp), %rax
cmpl $0x1, 0xc(%rax)
jge 0x17df7
movq 0x12205(%rip), %rax     # 0x29fd0
movq (%rax), %rdi
movq 0xa0(%rsp), %rax
movl 0xc(%rax), %edx
leaq 0xb2e7(%rip), %rsi      # 0x230c7
movb $0x0, %al
callq 0x15860
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x18273
movq 0xa0(%rsp), %rax
cmpl $0x1, 0x10(%rax)
jge 0x17e38
movq 0x121c4(%rip), %rax     # 0x29fd0
movq (%rax), %rdi
movq 0xa0(%rsp), %rax
movl 0x10(%rax), %edx
leaq 0xb2c9(%rip), %rsi      # 0x230ea
movb $0x0, %al
callq 0x15860
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x18273
movl $0x80000, %edi          # imm = 0x80000
callq 0x15640
movl %eax, 0x9c(%rsp)
cmpl $-0x1, 0x9c(%rsp)
jne 0x17e94
movq 0x12176(%rip), %rax     # 0x29fd0
movq (%rax), %rax
movq %rax, 0x70(%rsp)
callq 0x140c0
movl (%rax), %edi
callq 0x144d0
movq 0x70(%rsp), %rdi
movq %rax, %rdx
leaq 0xb28e(%rip), %rsi      # 0x2310b
movb $0x0, %al
callq 0x15860
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x18273
movq 0x78(%rsp), %rax
movl 0x9c(%rsp), %ecx
movl %ecx, 0x4(%rax)
movl $0x70, %edi
callq 0x15120
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x68(%rsp)
movq 0xa0(%rsp), %rax
movl 0x10(%rax), %esi
callq 0x15e20
jmp 0x17eca
movq 0x78(%rsp), %rax
movq 0x68(%rsp), %rcx
movq %rcx, 0x30(%rax)
leaq 0x80(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x15470
movq 0x78(%rsp), %rax
movq 0x48(%rsp), %rdi
movslq 0x4(%rax), %rsi
callq 0x15c10
movq 0x78(%rsp), %rax
movq 0x48(%rsp), %rdi
addq $0x48, %rax
movq %rax, 0x50(%rsp)
callq 0x14e50
movq 0x50(%rsp), %rcx
movq %rcx, 0xd0(%rsp)
movq %rax, 0xc8(%rsp)
movl $0x0, 0xc4(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x58(%rsp)
movl 0xc4(%rsp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x140d0
movl %eax, 0x64(%rsp)
jmp 0x17f59
movl 0x64(%rsp), %eax
movl %eax, 0xc0(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0xc8(%rsp), %rcx
movq %rcx, 0xb8(%rsp)
subl $0x3, %eax
je 0x17fa3
jmp 0x17f86
movl 0x44(%rsp), %eax
subl $0x5, %eax
je 0x17fb5
jmp 0x17f91
movq 0x58(%rsp), %rax
movq 0xb8(%rsp), %rcx
movq %rcx, (%rax)
jmp 0x17fcf
movq 0x58(%rsp), %rax
movq 0xb8(%rsp), %rcx
movq %rcx, (%rax)
jmp 0x17fcf
movq 0x58(%rsp), %rcx
movq 0xb8(%rsp), %rax
xchgq %rax, (%rcx)
jmp 0x17fcf
movq %rax, %rdi
callq 0x16de0
movq 0x78(%rsp), %rax
movq 0xa0(%rsp), %rcx
movl 0xc(%rcx), %ecx
movl %ecx, 0x8(%rax)
movslq 0x8(%rax), %rax
movl $0xc, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
movq %rax, 0x30(%rsp)
cmovoq %rax, %rdi
movq %rdi, 0x20(%rsp)
callq 0x14050
movq 0x20(%rsp), %rdx
movq %rax, %rdi
movq %rdi, 0x28(%rsp)
xorl %esi, %esi
callq 0x14900
movq 0x28(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rdx, 0x18(%rcx)
movq 0xa0(%rsp), %rdx
movl 0x4(%rdx), %edx
movl %edx, 0xc(%rcx)
movslq 0xc(%rcx), %rdi
testq %rdi, %rdi
cmovsq %rax, %rdi
callq 0x14050
movq %rax, %rcx
movq 0x78(%rsp), %rax
movq %rcx, 0x10(%rax)
movl $0x58, %edi
callq 0x15120
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
movq 0xa0(%rsp), %rax
movl 0x14(%rax), %esi
callq 0x157b0
jmp 0x18083
movq 0x78(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
callq 0x14300
cmpl $-0x1, %eax
jne 0x180ff
movq 0x78(%rsp), %rdi
callq 0x14f60
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x18273
movq 0x68(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
callq 0x15060
jmp 0x18282
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
callq 0x15060
jmp 0x18282
movq 0x78(%rsp), %rax
movq 0x20(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x20(%rax), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
movq 0x78(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %eax, %edx
movl $0x2001, %ecx           # imm = 0x2001
callq 0x15f80
cmpl $0x0, %eax
je 0x1817d
movq 0x11e97(%rip), %rax     # 0x29fd0
movq (%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x140c0
movl (%rax), %edi
callq 0x144d0
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0xafd0(%rip), %rsi      # 0x2312c
movb $0x0, %al
callq 0x15860
movq 0x78(%rsp), %rdi
callq 0x14f60
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x18273
movl $0x78, %edi
callq 0x15120
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
movl $0x100, %esi            # imm = 0x100
callq 0x15ee0
jmp 0x1819e
movq 0x78(%rsp), %rsi
movq 0x8(%rsp), %rax
movq %rax, 0x28(%rsi)
movq 0x28(%rsi), %rdi
callq 0x15540
cmpl $0x0, %eax
je 0x181f7
movq 0x78(%rsp), %rdi
callq 0x14f60
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x18273
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
callq 0x15060
jmp 0x18282
movl $0x78, %edi
callq 0x15120
movq %rax, %rdi
movq %rdi, %rax
movq %rax, (%rsp)
movl $0x8, %esi
callq 0x14960
jmp 0x18217
movq 0x78(%rsp), %rsi
movq (%rsp), %rax
movq %rax, 0x38(%rsi)
movq 0x38(%rsi), %rdi
callq 0x145e0
cmpl $0x0, %eax
je 0x18268
movq 0x78(%rsp), %rdi
callq 0x14f60
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x18273
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
callq 0x15060
jmp 0x18282
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xd8, %rsp
retq
movq 0x90(%rsp), %rdi
callq 0x15ce0
nop