// File: /workspace/github_c_makefile_star2_O0/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
subq $0x110, %rsp            # imm = 0x110
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xb0(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x9010
leaq -0xa0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl $0x0, -0xdc(%rbp)
movl -0xdc(%rbp), %eax
cmpq $0x10, %rax
jae 0x3ae7
movq -0xe8(%rbp), %rax
movl -0xdc(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
jmp 0x3ab5
jmp 0x3ae9
cmpl $0x0, -0x10(%rbp)
je 0x3aff
leaq -0xa0(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x3b0d
leaq -0xa0(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x3b0f
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x8(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xa0(%rbp,%rax,8), %rcx
movq %rcx, -0xa0(%rbp,%rax,8)
cmpl $0x0, -0xc(%rbp)
je 0x3bf2
leaq -0x100(%rbp), %rdi
callq 0x9010
movq -0x100(%rbp), %rax
subq -0xc0(%rbp), %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
addq -0xf8(%rbp), %rax
subq -0xb8(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, -0xd8(%rbp)
movslq -0xc(%rbp), %rcx
cmpq %rcx, %rax
jle 0x3bcc
jmp 0x3b9b
leaq 0xc4ba(%rip), %rdi      # 0x1005c
movl $0x1c6, %esi            # imm = 0x1C6
leaq 0xc8cb(%rip), %rdx      # 0x10479
movb $0x0, %al
callq 0xdfe0
callq 0x2070
movl $0xb, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3d70
movq $0x0, -0xd0(%rbp)
movslq -0xc(%rbp), %rax
subq -0xd8(%rbp), %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0xc8(%rbp)
jmp 0x3c13
movq $0x0, -0xd8(%rbp)
movq $0x0, -0xd0(%rbp)
movq $0x0, -0xc8(%rbp)
movl -0x8(%rbp), %edi
addl $0x1, %edi
movq -0xa8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
leaq -0xd0(%rbp), %r8
xorl %eax, %eax
movl %eax, %ecx
callq 0x2280
movl %eax, -0xec(%rbp)
cmpl $0x0, %eax
jge 0x3d2c
callq 0x2070
movl (%rax), %eax
movl %eax, -0x104(%rbp)
cmpl $-0x1, -0xec(%rbp)
jne 0x3c5e
jmp 0x3c7d
leaq 0xdf10(%rip), %rdi      # 0x11b75
leaq 0xc3f0(%rip), %rsi      # 0x1005c
movl $0x1d5, %edx            # imm = 0x1D5
leaq 0xc82a(%rip), %rcx      # 0x104a2
callq 0x21a0
callq 0x2070
cmpl $0x4, (%rax)
jne 0x3cdc
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
cmpl $0xa, -0x14(%rbp)
jne 0x3cd7
jmp 0x3c98
leaq 0xc3bd(%rip), %rdi      # 0x1005c
movl $0x1d9, %esi            # imm = 0x1D9
leaq 0xc819(%rip), %rdx      # 0x104c4
movb $0x0, %al
callq 0xdfe0
movl -0x104(%rbp), %eax
movl %eax, -0x108(%rbp)
callq 0x2070
movl -0x108(%rbp), %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3d70
jmp 0x3b0f
jmp 0x3cde
movl -0x104(%rbp), %ecx
leaq 0xc371(%rip), %rdi      # 0x1005c
movl $0x1e0, %esi            # imm = 0x1E0
leaq 0xc800(%rip), %rdx      # 0x104f7
movb $0x0, %al
callq 0xdfe0
movl -0x104(%rbp), %eax
movl %eax, -0x10c(%rbp)
callq 0x2070
movl -0x10c(%rbp), %ecx
movl %ecx, (%rax)
leaq 0xc7fe(%rip), %rdi      # 0x1051c
callq 0x2310
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3d70
cmpl $0x0, -0xec(%rbp)
je 0x3d37
jmp 0x3d69
jmp 0x3d39
jmp 0x3d3b
leaq 0xc31a(%rip), %rdi      # 0x1005c
movl $0x1e8, %esi            # imm = 0x1E8
leaq 0xc7d7(%rip), %rdx      # 0x10525
movb $0x0, %al
callq 0xdfe0
callq 0x2070
movl $0xb, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3d70
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopl (%rax)