// File: /workspace/github_c_makefile_star2_O0/alexgirao[P]aimant/aimant.c
// Function: enqueue_til_settle
static int enqueue_til_settle(struct buffer_queue *q, struct cat_tap *input, int id, int msec_to_settle, char *buf, int bufsz)
{
	fd_set rfds[1];
	struct timeval tv[1];
	int r, bytes_read;
	int bql_before = buffer_queue_len(q);

	assert(input->fd >= 0);

	DEBUG_INFO("enqueue_til_settle: queue len=%i, input->fd=%i, msec_to_settle=%i", bql_before, input->fd, msec_to_settle);

	FD_ZERO(rfds);

	for (;;) {
		FD_SET(input->fd, rfds);

		tv->tv_sec = 0;
		tv->tv_usec = msec_to_settle * 1000;

		r = select(input->fd + 1, rfds, NULL, NULL, tv);
		if (r < 0) {
			int save_errno = errno;
			assert(r == -1);
			if (errno == EINTR) {
				DEBUG_INFO("select() received an EINTR, retrying");
				continue;
			}
			DEBUG("select(), errno=%i", save_errno);
			errno = save_errno;
			perror("select()");
			exit(1);
		} else if (r) {
			int n;
			n = cat_tap_read(input, buf, bufsz-1);
			if (n < 0) {
				int save_errno = errno;
				assert(n == -1);
				if (errno == EAGAIN) {
					DEBUG_INFO("cat_tap_read(input, buf, bufsz-1) got EAGAIN");
					continue;
				}
				if (errno == EINTR) {
					DEBUG_INFO("cat_tap_read(input, buf, bufsz-1) got EINTR");
					continue;
				}
				DEBUG("cat_tap_read(), errno=%i", save_errno);
				errno = save_errno;
				perror("cat_tap_read()");
				exit(1);
			}
			if (n) {
				buf[n] = 0;
				q->enqueue = buffer_new(q->enqueue, id, buf, n);
				DEBUG_INFO("enqueue_til_settle: enqueued %i bytes", n);
				bytes_read += n;
			} else {
				DEBUG_INFO("enqueue_til_settle: input->fd got EOF, something went wrong");
				assert(input->got_eof);
				break;
			}
			if (buffer_queue_len(q) - bql_before >= 100) {
				DEBUG("enqueue_til_settle: input tap is not settling, exiting with %i buffers enqueued and errno=EAGAIN to avoid resource exhaustion", buffer_queue_len(q));
				errno = EAGAIN;
				return -1;
			}
		} else {
			/* timeout
			 */
			DEBUG_INFO("enqueue_til_settle: settled (timeout happened), new queue size is %i", buffer_queue_len(q));
			break;
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3440
movl %eax, -0xcc(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jl 0x654b
jmp 0x656a
leaq 0xa6e6(%rip), %rdi      # 0x10c38
leaq 0x9b03(%rip), %rsi      # 0x1005c
movl $0x225, %edx            # imm = 0x225
leaq 0xa6e2(%rip), %rcx      # 0x10c47
callq 0x21a0
jmp 0x656c
leaq -0xb0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl $0x0, -0xd0(%rbp)
movl -0xd0(%rbp), %eax
cmpq $0x10, %rax
jae 0x65b6
movq -0xd8(%rbp), %rax
movl -0xd0(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0x6584
jmp 0x65b8
jmp 0x65ba
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xb0(%rbp,%rax,8), %rcx
movq %rcx, -0xb0(%rbp,%rax,8)
movq $0x0, -0xc0(%rbp)
imull $0x3e8, -0x20(%rbp), %eax # imm = 0x3E8
cltq
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %edi
addl $0x1, %edi
leaq -0xb0(%rbp), %rsi
leaq -0xc0(%rbp), %r8
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x2280
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jge 0x66df
callq 0x2070
movl (%rax), %eax
movl %eax, -0xdc(%rbp)
cmpl $-0x1, -0xc4(%rbp)
jne 0x6660
jmp 0x667f
leaq 0xb50e(%rip), %rdi      # 0x11b75
leaq 0x99ee(%rip), %rsi      # 0x1005c
movl $0x234, %edx            # imm = 0x234
leaq 0xa5cd(%rip), %rcx      # 0x10c47
callq 0x21a0
callq 0x2070
cmpl $0x4, (%rax)
jne 0x668e
jmp 0x65ba
jmp 0x6690
movl -0xdc(%rbp), %ecx
leaq 0x99bf(%rip), %rdi      # 0x1005c
movl $0x239, %esi            # imm = 0x239
leaq 0x9e60(%rip), %rdx      # 0x10509
movb $0x0, %al
callq 0xdfe0
movl -0xdc(%rbp), %eax
movl %eax, -0xe8(%rbp)
callq 0x2070
movl -0xe8(%rbp), %ecx
movl %ecx, (%rax)
leaq 0x9e4c(%rip), %rdi      # 0x1051c
callq 0x2310
movl $0x1, %edi
callq 0x2350
cmpl $0x0, -0xc4(%rbp)
je 0x6885
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x2c(%rbp), %edx
subl $0x1, %edx
callq 0x2d80
movl %eax, -0xe0(%rbp)
cmpl $0x0, -0xe0(%rbp)
jge 0x67b8
callq 0x2070
movl (%rax), %eax
movl %eax, -0xe4(%rbp)
cmpl $-0x1, -0xe0(%rbp)
jne 0x672a
jmp 0x6749
leaq 0x9b6f(%rip), %rdi      # 0x102a0
leaq 0x9924(%rip), %rsi      # 0x1005c
movl $0x242, %edx            # imm = 0x242
leaq 0xa503(%rip), %rcx      # 0x10c47
callq 0x21a0
callq 0x2070
cmpl $0xb, (%rax)
jne 0x6758
jmp 0x65ba
callq 0x2070
cmpl $0x4, (%rax)
jne 0x6767
jmp 0x65ba
jmp 0x6769
movl -0xe4(%rbp), %ecx
leaq 0x98e6(%rip), %rdi      # 0x1005c
movl $0x24b, %esi            # imm = 0x24B
leaq 0xa2e5(%rip), %rdx      # 0x10a67
movb $0x0, %al
callq 0xdfe0
movl -0xe4(%rbp), %eax
movl %eax, -0xec(%rbp)
callq 0x2070
movl -0xec(%rbp), %ecx
movl %ecx, (%rax)
leaq 0xa2d7(%rip), %rdi      # 0x10a80
callq 0x2310
movl $0x1, %edi
callq 0x2350
cmpl $0x0, -0xe0(%rbp)
je 0x6807
movq -0x28(%rbp), %rax
movslq -0xe0(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl -0x1c(%rbp), %esi
movq -0x28(%rbp), %rdx
movl -0xe0(%rbp), %ecx
callq 0x3520
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0xe0(%rbp), %eax
addl -0xc8(%rbp), %eax
movl %eax, -0xc8(%rbp)
jmp 0x6834
movq -0x18(%rbp), %rax
cmpl $0x0, 0x54(%rax)
je 0x6813
jmp 0x6832
leaq 0xa484(%rip), %rdi      # 0x10c9e
leaq 0x983b(%rip), %rsi      # 0x1005c
movl $0x257, %edx            # imm = 0x257
leaq 0xa41a(%rip), %rcx      # 0x10c47
callq 0x21a0
jmp 0x688e
movq -0x10(%rbp), %rdi
callq 0x3440
subl -0xcc(%rbp), %eax
cmpl $0x64, %eax
jl 0x6883
jmp 0x684a
movq -0x10(%rbp), %rdi
callq 0x3440
movl %eax, %ecx
leaq 0x9800(%rip), %rdi      # 0x1005c
movl $0x25b, %esi            # imm = 0x25B
leaq 0xa445(%rip), %rdx      # 0x10cad
movb $0x0, %al
callq 0xdfe0
callq 0x2070
movl $0xb, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6895
jmp 0x6887
jmp 0x688e
jmp 0x6889
jmp 0x65ba
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)