// File: /workspace/github_c_makefile_star2_O3/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, %ebx
movl %esi, %r14d
movl %edi, %r13d
leaq -0x80(%rbp), %rdi
callq 0x6595
xorps %xmm0, %xmm0
leaq -0x100(%rbp), %r15
movaps %xmm0, 0x70(%r15)
movaps %xmm0, 0x60(%r15)
movaps %xmm0, 0x50(%r15)
movaps %xmm0, 0x40(%r15)
movaps %xmm0, 0x30(%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ecx
cmoveq %r15, %rcx
movq %rcx, -0x50(%rbp)
movaps %xmm0, (%r15)
cmoveq %rax, %r15
leal 0x3f(%r13), %eax
testl %r13d, %r13d
cmovnsl %r13d, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %r13d, %ecx
subl %edx, %ecx
movl $0x1, %r12d
shlq %cl, %r12
sarl $0x6, %eax
cltq
movq %rax, -0x60(%rbp)
movl %r14d, -0x2c(%rbp)
movslq %r14d, %rax
movq %rax, -0x48(%rbp)
incl %r13d
movq %r13, -0x58(%rbp)
movl $0xa, %r14d
movq -0x60(%rbp), %rax
orq %r12, -0x100(%rbp,%rax,8)
cmpl $0x0, -0x2c(%rbp)
je 0x3d49
leaq -0x70(%rbp), %rdi
callq 0x6595
movq -0x70(%rbp), %rax
subq -0x80(%rbp), %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
addq -0x68(%rbp), %rax
subq -0x78(%rbp), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
movq -0x48(%rbp), %rax
subq %rdx, %rax
jl 0x3dd0
movq $0x0, -0x40(%rbp)
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0x38(%rbp)
jmp 0x3d50
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movq -0x58(%rbp), %rdi
movq %r15, %rsi
movq -0x50(%rbp), %rdx
xorl %ecx, %ecx
leaq -0x40(%rbp), %r8
callq 0x2280
movl %eax, %ebx
testl %eax, %eax
jns 0x3db5
callq 0x2080
cmpl $-0x1, %ebx
jne 0x3e16
movq %rax, %r13
movl (%rax), %edi
cmpl $0x4, %edi
jne 0x3e0c
decl %r14d
jne 0x3cda
leaq 0x7324(%rip), %rdi      # 0xb0bc
leaq 0x7785(%rip), %rdx      # 0xb524
movl $0x1d9, %esi            # imm = 0x1D9
xorl %eax, %eax
callq 0x9818
movl $0x4, (%r13)
jmp 0x3df5
xorl %eax, %eax
testl %ebx, %ebx
jne 0x3dfa
leaq 0x72fa(%rip), %rdi      # 0xb0bc
leaq 0x77bc(%rip), %rdx      # 0xb585
movl $0x1e8, %esi            # imm = 0x1E8
jmp 0x3de3
leaq 0x72e5(%rip), %rdi      # 0xb0bc
leaq 0x76fb(%rip), %rdx      # 0xb4d9
movl $0x1c6, %esi            # imm = 0x1C6
xorl %eax, %eax
callq 0x9818
callq 0x2080
movl $0xb, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rsi
callq 0x2521
jmp 0x3df5
leaq 0x8c82(%rip), %rdi      # 0xca9f
leaq 0x7298(%rip), %rsi      # 0xb0bc
leaq 0x76d7(%rip), %rcx      # 0xb502
movl $0x1d5, %edx            # imm = 0x1D5
callq 0x21b0