// File: /workspace/github_c_makefile_star2_O2/alexgirao[P]aimant/aimant.c
// Function: wait_til_ready
int wait_til_ready(int fd, int msec, int ready_for_read)
{
	int eintr_count = 0;
	fd_set fds[1], *prfds = NULL, *pwfds = NULL;
	struct timeval start[1];
	struct timeval tv[1];
	long dmsec;

	get_current_timeval(start);

	DEBUG_INFO("wait_til_ready(fd=%i, msec=%i, ready_for_read=%i) begin", fd, msec, ready_for_read);

	FD_ZERO(fds);

	if (ready_for_read) prfds = fds;
	else pwfds = fds;

	for (;;) {
		int r;

		FD_SET(fd, fds);

		if (msec) {
			struct timeval current[1];
			get_current_timeval(current);
			if ((dmsec = DELTA_MSEC(current, start)) > msec) {
				DEBUG("wait_til_ready(): timeout (time elapsed)");
				errno = EAGAIN;
				return -1;
			}
			tv->tv_sec = 0;
			tv->tv_usec = (msec - dmsec) * 1000;
		} else {
			dmsec = 0;
			tv->tv_sec = 0;
			tv->tv_usec = 0;
		}

		DEBUG_INFO("wait_til_ready(): select(fd+1) for %li milliseconds", msec - dmsec);
		if ((r = select(fd+1, prfds, pwfds, NULL, tv)) < 0) {
			int save_errno = errno;
			assert(r == -1);
			if (errno == EINTR) {
				eintr_count++;
				if (eintr_count == 10) {
					DEBUG("wait_til_ready(): too much EINTR, giving up select");
					errno = save_errno;
					return -1;
				}
				DEBUG_INFO("wait_til_ready(): got EINTR");
				continue;
			}
			DEBUG("wait_til_ready(): select(), errno=%i", save_errno);
			errno = save_errno;
			perror("select()");
			return -1;
		} else if (r) {
			DEBUG_INFO("wait_til_ready(): ready");
			break;
		}
		DEBUG("wait_til_ready(): timeout");
		errno = EAGAIN;
		return -1;
	}

	DEBUG_INFO("wait_til_ready(fd=%i, msec=%i, ready_for_read=%i) done", fd, msec, ready_for_read);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %edx, %r15d
movl %esi, -0x2c(%rbp)
movl %edi, %ebx
leaq -0x80(%rbp), %rdi
callq 0x5c08
xorl %eax, %eax
cmpq $0x10, %rax
je 0x329c
andq $0x0, -0x100(%rbp,%rax,8)
incq %rax
jmp 0x3288
xorl %eax, %eax
testl %r15d, %r15d
leaq -0x100(%rbp), %rcx
movl $0x0, %edx
cmoveq %rcx, %rdx
movq %rdx, -0x50(%rbp)
cmoveq %rax, %rcx
movq %rcx, -0x58(%rbp)
pushq $0x40
popq %rcx
movl %ebx, %eax
cltd
idivl %ecx
movslq %eax, %rcx
movq %rcx, -0x60(%rbp)
shll $0x6, %eax
movl %ebx, %ecx
subl %eax, %ecx
pushq $0x1
popq %r14
shlq %cl, %r14
movslq -0x2c(%rbp), %rax
movq %rax, -0x48(%rbp)
incl %ebx
movl %ebx, %r15d
pushq $0xa
popq %r12
movq -0x60(%rbp), %rax
orq %r14, -0x100(%rbp,%rax,8)
cmpl $0x0, -0x2c(%rbp)
je 0x3346
leaq -0x70(%rbp), %rdi
callq 0x5c08
movq -0x70(%rbp), %rax
subq -0x80(%rbp), %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
addq -0x68(%rbp), %rax
subq -0x78(%rbp), %rax
cqto
movl $0x3e8, %ecx            # imm = 0x3E8
idivq %rcx
movq -0x48(%rbp), %rcx
subq %rax, %rcx
jl 0x33cd
andq $0x0, -0x40(%rbp)
imulq $0x3e8, %rcx, %rax      # imm = 0x3E8
movq %rax, -0x38(%rbp)
jmp 0x334d
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movl %r15d, %edi
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
xorl %ecx, %ecx
leaq -0x40(%rbp), %r8
callq 0x2280
movl %eax, %ebx
testl %eax, %eax
jns 0x33b2
callq 0x2080
cmpl $-0x1, %ebx
jne 0x3435
movq %rax, %r13
movl (%rax), %ebx
cmpl $0x4, %ebx
jne 0x3407
decl %r12d
jne 0x32eb
leaq 0x6d27(%rip), %rdi      # 0xa0bc
leaq 0x7188(%rip), %rdx      # 0xa524
movl $0x1d9, %esi            # imm = 0x1D9
xorl %eax, %eax
callq 0x8e04
movl $0x4, (%r13)
jmp 0x33f2
xorl %eax, %eax
testl %ebx, %ebx
jne 0x33f5
leaq 0x6cfd(%rip), %rdi      # 0xa0bc
leaq 0x71bf(%rip), %rdx      # 0xa585
movl $0x1e8, %esi            # imm = 0x1E8
jmp 0x33e0
leaq 0x6ce8(%rip), %rdi      # 0xa0bc
leaq 0x70fe(%rip), %rdx      # 0xa4d9
movl $0x1c6, %esi            # imm = 0x1C6
xorl %eax, %eax
callq 0x8e04
callq 0x2080
movl $0xb, (%rax)
pushq $-0x1
popq %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6cae(%rip), %rdi      # 0xa0bc
leaq 0x7142(%rip), %rdx      # 0xa557
movl $0x1e0, %esi            # imm = 0x1E0
movl %ebx, %ecx
xorl %eax, %eax
callq 0x8e04
movl %ebx, (%r13)
leaq 0x714e(%rip), %rdi      # 0xa57c
callq 0x2300
jmp 0x33f2
leaq 0x8688(%rip), %rdi      # 0xbac4
leaq 0x6c79(%rip), %rsi      # 0xa0bc
leaq 0x70b8(%rip), %rcx      # 0xa502
movl $0x1d5, %edx            # imm = 0x1D5
callq 0x21b0