// File: /workspace/github_c_makefile_star2_O1/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, %r12d
movl %esi, %ebx
movl %edi, %r13d
leaq -0x80(%rbp), %rdi
callq 0x67b0
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 %r12d, %r12d
movl $0x0, %ecx
cmoveq %r15, %rcx
movq %rcx, -0x48(%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, %r14d
shlq %cl, %r14
sarl $0x6, %eax
cltq
movq %rax, -0x58(%rbp)
movl %ebx, -0x30(%rbp)
movslq %ebx, %rbx
incl %r13d
movq %r13, -0x50(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x58(%rbp), %rax
orq %r14, -0x100(%rbp,%rax,8)
cmpl $0x0, -0x30(%rbp)
je 0x3deb
leaq -0x70(%rbp), %rdi
callq 0x67b0
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, %r13
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %r13
addq %rax, %r13
movq %rbx, %rax
subq %r13, %rax
jge 0x3df4
leaq 0x72f1(%rip), %rdi      # 0xb0bc
movl $0x1c6, %esi            # imm = 0x1C6
leaq 0x7702(%rip), %rdx      # 0xb4d9
xorl %eax, %eax
callq 0x9a2c
callq 0x2080
movl $0xb, (%rax)
jmp 0x3e07
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
jmp 0x3e15
movq $0x0, -0x40(%rbp)
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0x38(%rbp)
movl $0x1, %eax
cmpq %r13, %rbx
jl 0x3ebe
movq -0x50(%rbp), %rdi
movq %r15, %rsi
movq -0x48(%rbp), %rdx
xorl %ecx, %ecx
leaq -0x40(%rbp), %r8
callq 0x2290
testl %eax, %eax
js 0x3e66
movl $0x7, %eax
jne 0x3ebe
leaq 0x727b(%rip), %rdi      # 0xb0bc
movl $0x1e8, %esi            # imm = 0x1E8
leaq 0x7738(%rip), %rdx      # 0xb585
xorl %eax, %eax
callq 0x9a2c
callq 0x2080
movl $0xb, (%rax)
movl $0x1, %eax
jmp 0x3ebe
movl %eax, %r12d
callq 0x2080
cmpl $-0x1, %r12d
jne 0x3ef3
movq %rax, %r13
movl (%rax), %edi
cmpl $0x4, %edi
jne 0x3ec9
movl -0x2c(%rbp), %ecx
incl %ecx
movl $0x8, %eax
movl %ecx, -0x2c(%rbp)
cmpl $0xa, %ecx
jne 0x3ebe
leaq 0x7225(%rip), %rdi      # 0xb0bc
movl $0x1d9, %esi            # imm = 0x1D9
leaq 0x7681(%rip), %rdx      # 0xb524
xorl %eax, %eax
callq 0x9a2c
movl $0x4, (%r13)
movl $0x1, %eax
movl $0xa, -0x2c(%rbp)
cmpl $0x8, %eax
je 0x3d6c
jmp 0x3ed3
movq %r13, %rsi
callq 0x2531
jmp 0x3e5f
cmpl $0x7, %eax
jne 0x3edc
xorl %eax, %eax
jmp 0x3ee1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8bbd(%rip), %rdi      # 0xcab7
leaq 0x71bb(%rip), %rsi      # 0xb0bc
leaq 0x75fa(%rip), %rcx      # 0xb502
movl $0x1d5, %edx            # imm = 0x1D5
callq 0x21b0