// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpq $0x0, (%r8)
js 0x38e6
movq %r8, %rbx
cmpq $0x0, 0x8(%r8)
js 0x38e6
movl %ecx, %r12d
movq %rdx, %r15
movl %esi, %ebp
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x2360
leaq 0x8(%rsp), %rdi
movl %ebp, (%rdi)
movw $0x1, 0x4(%rdi)
movabsq $0x20c49ba5e353f7cf, %rax # imm = 0x20C49BA5E353F7CF
imulq 0x8(%rbx)
imull $0x3e8, (%rbx), %eax    # imm = 0x3E8
movq %rdx, %rcx
shrq $0x3f, %rcx
shrq $0x7, %rdx
addl %ecx, %edx
addl %eax, %edx
movl $0x1, %esi
callq 0x2340
testl %eax, %eax
jle 0x38f6
testb $0x1, 0xe(%rsp)
je 0x38f6
movslq %r12d, %rdx
movl %ebp, %edi
movq %r15, %rsi
callq 0x2300
movq %rax, %r14
leaq 0x20(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x2360
movq (%r15), %rax
subq 0x10(%rsp), %rax
imulq $-0xf4240, %rax, %rcx   # imm = 0xFFF0BDC0
movq 0x18(%rsp), %rax
subq 0x8(%r15), %rax
addq %rcx, %rax
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
testq %rdx, %rdx
jle 0x38da
imulq $0xf4240, %rdx, %rdx    # imm = 0xF4240
addq %rdx, %rcx
movq $0x0, (%rbx)
addq %rcx, %rax
movq %rax, 0x8(%rbx)
testl %r14d, %r14d
je 0x390f
movl %r14d, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8(%rsp), %rsi
leaq 0x20(%rsp), %rbx
movl %eax, %edi
movq %rbx, %rdx
callq 0x28a8
movl (%rbx), %r14d
jmp 0x38e6
callq 0x2835
xorl %r14d, %r14d
jmp 0x38e6
nop