// File: /workspace/github_c_makefile_star2_O0/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
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3490
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x3a24
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x3610
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jge 0x39bc
callq 0x2070
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x28(%rbp)
jne 0x38da
jmp 0x38f9
leaq 0xc9bf(%rip), %rdi      # 0x102a0
leaq 0xc774(%rip), %rsi      # 0x1005c
movl $0x17f, %edx            # imm = 0x17F
leaq 0xcae3(%rip), %rcx      # 0x103d7
callq 0x21a0
callq 0x2070
cmpl $0xb, (%rax)
jne 0x3908
jmp 0x3a24
callq 0x2070
cmpl $0x4, (%rax)
jne 0x3917
jmp 0x3a24
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x2eb0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
callq 0x2070
cmpl $0x20, (%rax)
jne 0x3975
jmp 0x393f
leaq 0xc716(%rip), %rdi      # 0x1005c
movl $0x18c, %esi            # imm = 0x18C
leaq 0xcac5(%rip), %rdx      # 0x10417
movb $0x0, %al
callq 0xdfe0
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
callq 0x2070
movl -0x30(%rbp), %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3a4d
jmp 0x3977
movl -0x2c(%rbp), %ecx
leaq 0xc6db(%rip), %rdi      # 0x1005c
movl $0x190, %esi            # imm = 0x190
leaq 0xcabd(%rip), %rdx      # 0x1044a
movb $0x0, %al
callq 0xdfe0
movl -0x2c(%rbp), %eax
movl %eax, -0x34(%rbp)
callq 0x2070
movl -0x34(%rbp), %ecx
movl %ecx, (%rax)
leaq 0xcab6(%rip), %rdi      # 0x10461
callq 0x2310
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3a4d
cmpl $0x0, -0x28(%rbp)
je 0x39f0
movl -0x28(%rbp), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movl 0x28(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x39dd
jmp 0x3a24
movq -0x20(%rbp), %rdi
callq 0x3580
movq $0x0, -0x20(%rbp)
jmp 0x3a1d
movq -0x10(%rbp), %rax
cmpl $0x0, 0x40(%rax)
je 0x39fc
jmp 0x3a1b
leaq 0xca6b(%rip), %rdi      # 0x1046e
leaq 0xc652(%rip), %rsi      # 0x1005c
movl $0x1a1, %edx            # imm = 0x1A1
leaq 0xc9c1(%rip), %rcx      # 0x103d7
callq 0x21a0
jmp 0x3a24
jmp 0x3a1f
jmp 0x3897
cmpq $0x0, -0x20(%rbp)
je 0x3a47
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x2eb0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)