// File: SeTriones[P]Anubis/base_nio_handler.cpp
// Function: base_nio_handler::writen_timeout(int, void const*, int, int)
int base_nio_handler::writen_timeout(int fd, const void *buf, int buf_len, int to) {
	int left = buf_len;
	int n;
    //fd_set wset;
    pollfd write_fd;
    write_fd.fd = fd;
    write_fd.events = POLLOUT;
    int poll_ret;

	int real_to = to == -1 ? send_timeout : to;
    timeval timeout = {
        0,
        real_to * 1000
    };

    while (left > 0) {
        poll_ret = poll(&write_fd, 1, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
        if (poll_ret <= 0 || !(write_fd.revents & POLLOUT)) {
            fprintf(stderr, "wr ret=%d, errno=%d\n", poll_ret, errno);
            if (write_fd.revents & POLLIN) {
                fprintf(stderr, "POLLIN\n");
            }
            if (write_fd.revents & POLLPRI) {
                fprintf(stderr, "POLLPRI\n");
            }
            if (write_fd.revents & POLLOUT) {
                fprintf(stderr, "POLLOUT\n");
            }
            if (write_fd.revents & POLLRDHUP) {
                fprintf(stderr, "POLLRDHUP\n");
            }
            if (write_fd.revents & POLLERR) {
                fprintf(stderr, "POLLERR\n");
            }
            if (write_fd.revents & POLLHUP) {
                fprintf(stderr, "POLLHUP\n");
            }
            if (write_fd.revents & POLLNVAL) {
                fprintf(stderr, "POLLNVAL\n");
            }
            if (write_fd.revents & POLLRDNORM) {
                fprintf(stderr, "POLLRDNORM\n");
            }
            if (write_fd.revents & POLLRDBAND) {
                fprintf(stderr, "POLLRDBAND\n");
            }
            if (write_fd.revents & POLLWRNORM) {
                fprintf(stderr, "POLLWRNORM\n");
            }
            if (write_fd.revents & POLLWRBAND) {
                fprintf(stderr, "POLLWRBAND\n");
            }
            return -1;
        }
        /*
        FD_ZERO(&wset);
        FD_SET(fd, &wset);
        if (select(fd + 1, NULL, &wset, NULL, &timeout) <= 0)
            return -1;
        */
		if ((n = write(fd, buf, left)) <= 0)
			return buf_len - left;

		buf = (char *)buf + n;
		left -= n;
    }
    return buf_len;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x38(%rbp)
movw $0x4, -0x34(%rbp)
cmpl $-0x1, -0x28(%rbp)
jne 0x3b69
movq -0x58(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x5c(%rbp)
jmp 0x3b6f
movl -0x28(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
imull $0x3e8, -0x40(%rbp), %eax # imm = 0x3E8
cltq
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x3df0
imulq $0x3e8, -0x50(%rbp), %rax # imm = 0x3E8
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, %rcx
movq -0x68(%rbp), %rax
addq %rcx, %rax
movl %eax, %edx
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x2360
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jle 0x3be1
movswl -0x32(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x3dae
movq 0x93f0(%rip), %rax      # 0xcfd8
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x6c(%rbp)
callq 0x2040
movq -0x78(%rbp), %rdi
movl -0x6c(%rbp), %edx
movl (%rax), %ecx
leaq 0x55c0(%rip), %rsi      # 0x91ca
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x3c35
movq 0x93b4(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x54f5(%rip), %rsi      # 0x9123
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x3c59
movq 0x9390(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x54d9(%rip), %rsi      # 0x912b
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x3c7d
movq 0x936c(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x54be(%rip), %rsi      # 0x9134
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x3ca3
movq 0x9346(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x54a1(%rip), %rsi      # 0x913d
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x3cc7
movq 0x9322(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x5488(%rip), %rsi      # 0x9148
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x3ceb
movq 0x92fe(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x546d(%rip), %rsi      # 0x9151
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x3d0f
movq 0x92da(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x5452(%rip), %rsi      # 0x915a
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x3d33
movq 0x92b6(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x5438(%rip), %rsi      # 0x9164
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x3d59
movq 0x9290(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x541e(%rip), %rsi      # 0x9170
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0x3d7f
movq 0x926a(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x5404(%rip), %rsi      # 0x917c
movb $0x0, %al
callq 0x2310
movswl -0x32(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0x3da5
movq 0x9244(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
leaq 0x53ea(%rip), %rsi      # 0x9188
movb $0x0, %al
callq 0x2310
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3df6
movl -0x14(%rbp), %edi
movq -0x20(%rbp), %rsi
movslq -0x2c(%rbp), %rdx
callq 0x20d0
movl %eax, -0x30(%rbp)
cmpl $0x0, %eax
jg 0x3dd1
movl -0x24(%rbp), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3df6
movq -0x20(%rbp), %rax
movslq -0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movl -0x30(%rbp), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3b8a
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)