// File: roberty65[P]bServer/frame/testServer/src/otEchod.cpp
// Function: main
int main(int argc, char **argv)
{
        int fd, efd;

        if (argc < 2) {
                fprintf(stderr, "Usage: %s address\n", argv[0]);
                return 0;
        }

        if ((fd = beyondy::XbsServer(argv[1], 1024, O_NONBLOCK)) < 0) {
                fprintf(stderr, "xbsServer failed: %m\n");
                return 1;
        }

        if ((efd = epoll_create(1024)) < 0) {
                fprintf(stderr, "epoll_create failed: %m\n");
                return 2;
        }

        int epoll_size = 1024;
        struct epoll_event *epoll_events = (struct epoll_event *)malloc(epoll_size * sizeof(struct epoll_event));
        if (epoll_events == NULL) {
                fprintf(stderr, "new epoll events failed: %m\n");
                return 3;
        }

        if (events_watch_add(efd, fd, EPOLLIN) < 0)
			return 4;

        while (true) {
                long wtime = 1000; /* 1s */
                int cnt = epoll_wait(efd, epoll_events, epoll_size, wtime);
                if (cnt > 0) for (int i = 0; i < cnt; ++i) {
                        struct epoll_event *ee = &epoll_events[i];
                        if (ee->data.fd == fd) {
                                int cfd = accept(fd, NULL, NULL);
                                if (cfd >= 0) events_watch_add(efd, cfd, EPOLLIN);
                        }
                        else if (handle_events(ee->data.fd, ee->events) < 0) {
							events_watch_del(efd, ee->data.fd);
							close(ee->data.fd);
                        }
                }
        }

        return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1397
movq 0x3c6d(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x1c80(%rip), %rsi      # 0x3004
movb $0x0, %al
callq 0x11c0
movl $0x0, -0x4(%rbp)
jmp 0x154c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x800, %edx            # imm = 0x800
movl $0x3, %ecx
callq 0x1e90
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
jge 0x13df
movq 0x3c1e(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
leaq 0x1c4b(%rip), %rsi      # 0x3017
movb $0x0, %al
callq 0x11c0
movl $0x1, -0x4(%rbp)
jmp 0x154c
movl $0x400, %edi            # imm = 0x400
callq 0x1230
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jge 0x1415
movq 0x3be8(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
leaq 0x1c2b(%rip), %rsi      # 0x302d
movb $0x0, %al
callq 0x11c0
movl $0x2, -0x4(%rbp)
jmp 0x154c
movl $0x400, -0x1c(%rbp)     # imm = 0x400
movslq -0x1c(%rbp), %rax
imulq $0xc, %rax, %rdi
callq 0x1190
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x1458
movq 0x3ba5(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
leaq 0x1c01(%rip), %rsi      # 0x3046
movb $0x0, %al
callq 0x11c0
movl $0x3, -0x4(%rbp)
jmp 0x154c
movl -0x18(%rbp), %edi
movl -0x14(%rbp), %esi
movl $0x1, %edx
callq 0x1560
cmpl $0x0, %eax
jge 0x1479
movl $0x4, -0x4(%rbp)
jmp 0x154c
jmp 0x147b
movq $0x3e8, -0x30(%rbp)     # imm = 0x3E8
movl -0x18(%rbp), %edi
movq -0x28(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
callq 0x10a0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jle 0x1547
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x1545
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x1501
movl -0x14(%rbp), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x1060
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jl 0x14ff
movl -0x18(%rbp), %edi
movl -0x44(%rbp), %esi
movl $0x1, %edx
callq 0x1560
jmp 0x1535
movq -0x40(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x40(%rbp), %rax
movl (%rax), %esi
callq 0x15d0
cmpl $0x0, %eax
jge 0x1533
movl -0x18(%rbp), %edi
movq -0x40(%rbp), %rax
movl 0x4(%rax), %esi
callq 0x16d0
movq -0x40(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x1240
jmp 0x1535
jmp 0x1537
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x14ac
jmp 0x1547
jmp 0x147b
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)