// File: /workspace/github_c_makefile_star2_O1/alexgirao[P]aimant/aimant.c
// Function: sink_write_from_queue
int sink_write_from_queue(struct sink *x, struct buffer_queue *q)
{
	struct buffer *b;
	int total = 0;
	while ((b = buffer_queue_dequeue(q))) {
		int n;
		n = sink_write(x, b);
		if (n < 0) {
			int save_errno = errno;
			assert(n == -1);
			if (errno == EAGAIN) {
				DEBUG_INFO("sink is busy. how many buffers remaining? %i", buffer_queue_len(q));
				break;
			}
			if (errno == EINTR) {
				DEBUG_INFO("sink got interrupted (EINTR). how many buffers remaining? %i", buffer_queue_len(q));
				break;
			}
			/* errors beyond recovery
			 */
			q->dequeue = buffer_tail0(b, q->dequeue); /* reschedule */
			if (errno == EPIPE) {
				DEBUG("sink got unrecoverable error (EPIPE - broken pipe)");
				errno = save_errno;
				return -1;
			}
			DEBUG("sink_write(), errno=%i", save_errno);
			errno = save_errno;
			perror("sink_write()");
			return -1;
		} else if (n) {
			total += n;
			if (b->pos < b->buf->len) {
				DEBUG_INFO("sink consumed (so far) %i bytes of %i, %i buffers enqueued",
				      b->pos, b->buf->len, buffer_queue_len(q));
				break;
			}
			DEBUG_INFO("sink consumed all %i bytes, %i buffers enqueued", b->pos, buffer_queue_len(q));
			/* discard consumed buffer
			 */
			buffer_free(b);
			b = NULL;
		} else {
			assert(x->got_eof);
			break;
		}
	}
	if (b) q->dequeue = buffer_tail0(b, q->dequeue); /* reschedule */
	return total;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %r12
xorl %ebx, %ebx
movq %rdi, -0x30(%rbp)
movq %r14, %rdi
callq 0x38b3
testq %rax, %rax
je 0x3c71
movq %rax, %r15
movq %r12, %rdi
movq %rax, %rsi
callq 0x39cd
testl %eax, %eax
js 0x3bd4
je 0x3bff
addl %eax, %ebx
movl 0x28(%r15), %ecx
movl $0x3, %eax
cmpl 0x20(%r15), %ecx
jl 0x3c4f
movq %r15, %rdi
callq 0x396e
xorl %r15d, %r15d
xorl %eax, %eax
jmp 0x3c4f
movl %ebx, %r12d
callq 0x2080
movq %rax, %r13
movl (%rax), %ebx
movl $0x3, %eax
cmpl $0x4, %ebx
je 0x3c48
cmpl $0xb, %ebx
je 0x3c48
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x3c11
movl (%rax), %ecx
incl %ecx
jmp 0x3c13
movl $0x3, %eax
cmpl $0x0, 0x40(%r12)
jne 0x3c4f
jmp 0x3ca6
xorl %ecx, %ecx
movl %ecx, (%r15)
movq %rax, 0x8(%r15)
movq %r15, 0x8(%r14)
cmpl $0x20, (%r13)
jne 0x3c59
leaq 0x7490(%rip), %rdi      # 0xb0bc
movl $0x18c, %esi            # imm = 0x18C
leaq 0x783f(%rip), %rdx      # 0xb477
xorl %eax, %eax
callq 0x9a2c
movl %ebx, (%r13)
movl $0x1, %eax
movl %r12d, %ebx
movq -0x30(%rbp), %r12
testl %eax, %eax
je 0x3b8b
jmp 0x3c65
movl %ebx, %edi
movq %r13, %rsi
callq 0x24f4
jmp 0x3c43
cmpl $0x3, %eax
je 0x3c74
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x3c95
xorl %r15d, %r15d
testq %r15, %r15
je 0x3c95
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x3c88
movl (%rax), %ecx
incl %ecx
jmp 0x3c8a
xorl %ecx, %ecx
movl %ecx, (%r15)
movq %rax, 0x8(%r15)
movq %r15, 0x8(%r14)
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7821(%rip), %rdi      # 0xb4ce
leaq 0x7408(%rip), %rsi      # 0xb0bc
leaq 0x777c(%rip), %rcx      # 0xb437
movl $0x1a1, %edx            # imm = 0x1A1
callq 0x21b0