// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, %ebp
movl %ecx, %r12d
movq %rdx, %r14
movl %esi, %r15d
movl %esi, 0xc(%rsp)
movw $0x4, 0x10(%rsp)
cmpl $-0x1, %r8d
jne 0x3242
movl 0x4(%rdi), %ebp
pushq $0x1
popq %r13
movl %r12d, 0x14(%rsp)
testl %r12d, %r12d
jle 0x328e
leaq 0xc(%rsp), %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x2330
movl %eax, %ebx
testl %eax, %eax
jle 0x32ac
testb $0x4, 0x12(%rsp)
je 0x32ac
movl %r12d, %edx
movl %r15d, %edi
movq %r14, %rsi
callq 0x20b0
testl %eax, %eax
jle 0x3294
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, %r14
subl %eax, %r12d
jmp 0x324b
movl 0x14(%rsp), %ebx
jmp 0x329b
movl 0x14(%rsp), %ebx
subl %r12d, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6d25(%rip), %r15      # 0x9fd8
movq (%r15), %r14
callq 0x2040
movl (%rax), %ecx
leaq 0x2f06(%rip), %rsi      # 0x61ca
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x22e0
movzwl 0x12(%rsp), %eax
testb $0x1, %al
jne 0x334c
testb $0x2, %al
jne 0x336b
testb $0x4, %al
jne 0x338a
btl $0xd, %eax
jb 0x33a9
testb $0x8, %al
jne 0x33c8
testb $0x10, %al
jne 0x33e7
testb $0x20, %al
jne 0x3406
testb $0x40, %al
jne 0x3425
testb %al, %al
js 0x3444
btl $0x8, %eax
jb 0x3463
pushq $-0x1
popq %rbx
btl $0x9, %eax
jae 0x329b
movq (%r15), %rcx
leaq 0x2e4c(%rip), %rdi      # 0x6188
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
jmp 0x329b
movq (%r15), %rcx
leaq 0x2dcd(%rip), %rdi      # 0x6123
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
movzwl 0x12(%rsp), %eax
jmp 0x32d9
movq (%r15), %rcx
leaq 0x2db6(%rip), %rdi      # 0x612b
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
movzwl 0x12(%rsp), %eax
jmp 0x32e1
movq (%r15), %rcx
leaq 0x2da0(%rip), %rdi      # 0x6134
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
movzwl 0x12(%rsp), %eax
jmp 0x32e9
movq (%r15), %rcx
leaq 0x2d8a(%rip), %rdi      # 0x613d
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
movzwl 0x12(%rsp), %eax
jmp 0x32f3
movq (%r15), %rcx
leaq 0x2d76(%rip), %rdi      # 0x6148
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
movzwl 0x12(%rsp), %eax
jmp 0x32fb
movq (%r15), %rcx
leaq 0x2d60(%rip), %rdi      # 0x6151
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
movzwl 0x12(%rsp), %eax
jmp 0x3303
movq (%r15), %rcx
leaq 0x2d4a(%rip), %rdi      # 0x615a
pushq $0x9
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
movzwl 0x12(%rsp), %eax
jmp 0x330b
movq (%r15), %rcx
leaq 0x2d35(%rip), %rdi      # 0x6164
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
movzwl 0x12(%rsp), %eax
jmp 0x3313
movq (%r15), %rcx
leaq 0x2d22(%rip), %rdi      # 0x6170
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
movzwl 0x12(%rsp), %eax
jmp 0x331b
movq (%r15), %rcx
leaq 0x2d0f(%rip), %rdi      # 0x617c
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x23f0
movzwl 0x12(%rsp), %eax
jmp 0x3325