// File: SeTriones[P]Anubis/base_nio_handler.cpp
// Function: base_nio_handler::read_data(int, void*, int, timeval*)
int base_nio_handler::read_data(int fd, void * buf, int buf_len, timeval* timeout) {
    
    if (!(timeout->tv_sec >= 0 && timeout->tv_usec >=0))
        return -1;    

    struct timeval start_time, end_time;
    gettimeofday(&start_time, NULL);
        
    pollfd read_fd;
    read_fd.fd = fd;
    read_fd.events = POLLIN;
    int poll_ret = poll(&read_fd, 1, timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
    if (poll_ret <= 0 || !(read_fd.revents & POLLIN)) {
        fprintf(stderr, "rd ret=%d, errno=%d\n", poll_ret, errno);
        if (read_fd.revents & POLLIN) {
            fprintf(stderr, "POLLIN\n");
        }
        if (read_fd.revents & POLLPRI) {
            fprintf(stderr, "POLLPRI\n");
        }
        if (read_fd.revents & POLLOUT) {
            fprintf(stderr, "POLLOUT\n");
        }
        if (read_fd.revents & POLLRDHUP) {
            fprintf(stderr, "POLLRDHUP\n");
        }
        if (read_fd.revents & POLLERR) {
            fprintf(stderr, "POLLERR\n");
        }
        if (read_fd.revents & POLLHUP) {
            fprintf(stderr, "POLLHUP\n");
        }
        if (read_fd.revents & POLLNVAL) {
            fprintf(stderr, "POLLNVAL\n");
        }
        if (read_fd.revents & POLLRDNORM) {
            fprintf(stderr, "POLLRDNORM\n");
        }
        if (read_fd.revents & POLLRDBAND) {
            fprintf(stderr, "POLLRDBAND\n");
        }
        if (read_fd.revents & POLLWRNORM) {
            fprintf(stderr, "POLLWRNORM\n");
        }
        if (read_fd.revents & POLLWRBAND) {
            fprintf(stderr, "POLLWRBAND\n");
        }
        return -1; 
    }
    int len = read(fd,buf,buf_len);
    gettimeofday(&end_time,NULL);
    long cost = 1000000 * (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_usec - start_time.tv_usec);
    if (timeout->tv_sec > 0) {
        timeout->tv_usec += timeout->tv_sec * 1000000;
        timeout->tv_sec = 0;
    }
    timeout->tv_usec -= cost; 
    //fprintf(stderr,"ori tv_sec=%ld,tv_usec=%ld,cost=%ld\n",timeout->tv_sec,timeout->tv_usec,cost);
    if (len == 0) {
        _INFO("read_len=0,disconnect by peer");
    }
    return len;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jl 0x3662
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jge 0x366e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x39d2
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2380
movl -0x14(%rbp), %eax
movl %eax, -0x58(%rbp)
movw $0x1, -0x54(%rbp)
movq -0x30(%rbp), %rax
imulq $0x3e8, (%rax), %rax    # imm = 0x3E8
movq %rax, -0xd0(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, %rcx
movq -0xd0(%rbp), %rax
addq %rcx, %rax
movl %eax, %edx
leaq -0x58(%rbp), %rdi
movl $0x1, %esi
callq 0x2360
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jle 0x36e1
movswl -0x52(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x38bd
movq 0x98f0(%rip), %rax      # 0xcfd8
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0xd4(%rbp)
callq 0x2040
movq -0xe0(%rbp), %rdi
movl -0xd4(%rbp), %edx
movl (%rax), %ecx
leaq 0x59f8(%rip), %rsi      # 0x910e
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x3741
movq 0x98a8(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x59e9(%rip), %rsi      # 0x9123
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x3765
movq 0x9884(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x59cd(%rip), %rsi      # 0x912b
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x3789
movq 0x9860(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x59b2(%rip), %rsi      # 0x9134
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x37af
movq 0x983a(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x5995(%rip), %rsi      # 0x913d
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x37d3
movq 0x9816(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x597c(%rip), %rsi      # 0x9148
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x37f7
movq 0x97f2(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x5961(%rip), %rsi      # 0x9151
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x381b
movq 0x97ce(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x5946(%rip), %rsi      # 0x915a
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x383f
movq 0x97aa(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x592c(%rip), %rsi      # 0x9164
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x3865
movq 0x9784(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x5912(%rip), %rsi      # 0x9170
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0x388b
movq 0x975e(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x58f8(%rip), %rsi      # 0x917c
movb $0x0, %al
callq 0x2310
movswl -0x52(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0x38b1
movq 0x9738(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x58de(%rip), %rsi      # 0x9188
movb $0x0, %al
callq 0x2310
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x39d2
movl -0x14(%rbp), %edi
movq -0x20(%rbp), %rsi
movslq -0x24(%rbp), %rdx
callq 0x2320
movl %eax, -0x60(%rbp)
leaq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2380
movq -0x50(%rbp), %rax
subq -0x40(%rbp), %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
movq -0x48(%rbp), %rcx
subq -0x38(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jle 0x3927
movq -0x30(%rbp), %rax
imulq $0xf4240, (%rax), %rcx  # imm = 0xF4240
movq -0x30(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
movq -0x68(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x8(%rax)
cmpl $0x0, -0x60(%rbp)
jne 0x39cc
leaq -0x70(%rbp), %rdi
callq 0x21e0
leaq -0x70(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x2120
leaq -0xc0(%rbp), %rdi
movl $0x12, %esi
leaq 0x5694(%rip), %rdx      # 0x9004
leaq -0xa8(%rbp), %rcx
callq 0x2190
movq 0x9655(%rip), %rax      # 0xcfd8
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
leaq -0xc0(%rbp), %rax
movq %rax, -0xe8(%rbp)
callq 0x21c0
movq -0xf0(%rbp), %rdi
movq -0xe8(%rbp), %rdx
movq %rax, %rcx
leaq 0x57dc(%rip), %rsi      # 0x9194
leaq 0x57bc(%rip), %r8       # 0x917b
movb $0x0, %al
callq 0x2310
movl %eax, -0xc4(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nop