// File: /workspace/github_c_makefile_star2_O3/alexgirao[P]aimant/aimant.c
// Function: sink_write
int sink_write(struct sink *x, struct buffer *b)
{
	int n;

	/* since we are using non-blocking io, try to write directly
	 */
	assert(x->sp->child_fdin >= 0);
	assert(x->sp->child_fdin == x->fd);

#ifdef SIMULATE_PARTIAL_SINK_FEED
	if (b->pos == 0) {
		n = write(x->fd, b->buf->s, b->buf->len / 2);
	} else {
		n = write(x->fd, b->buf->s + b->pos, b->buf->len - b->pos);
	}
#else
	n = write(x->fd, b->buf->s + b->pos, b->buf->len - b->pos);
#endif
	if (n < 0) {
		int save_errno = errno;
		assert(n == -1);
		if (errno == EAGAIN) return -1;
		if (errno == EINTR) return -1;
		if (errno == EPIPE) {
			DEBUG("sink pid=%i got EPIPE", x->sp->pid);
			if (x->sp->child_fderr >= 0) {
				char buf[4096];
				int n;
				if ((n = read(x->sp->child_fderr, buf, sizeof(buf)-1)) > 0) {
					buf[n] = 0;
					DEBUG("sink pid=%i stderr=[%s]", x->sp->pid, buf);
				}
			}
			errno = save_errno;
			return -1;
		}
		DEBUG("write(), errno=%i", save_errno);
		errno = save_errno;
		perror("write()");
		exit(1);
	}
	if (n) {
		b->pos += n;
	} else {
		DEBUG_INFO("sink pid=%i got EOF", x->sp->pid);
		assert(x->got_eof == 0);
		x->got_eof = 1;
	}
	return n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x1008, %rsp           # imm = 0x1008
movq %rdi, %r14
movl 0x30(%rdi), %edi
testl %edi, %edi
js 0x3a86
cmpl (%r14), %edi
jne 0x3aa5
movq %rsi, %r15
movslq 0x28(%rsi), %rax
movq 0x18(%rsi), %rsi
addq %rax, %rsi
movslq 0x20(%r15), %rdx
subq %rax, %rdx
callq 0x2120
movq %rax, %rbx
testl %ebx, %ebx
js 0x39c3
je 0x3a67
addl %ebx, 0x28(%r15)
jmp 0x3a76
callq 0x2080
cmpl $-0x1, %ebx
jne 0x3ac4
movq %rax, %r15
movl (%rax), %edi
cmpl $0x4, %edi
je 0x3a76
cmpl $0xb, %edi
je 0x3a76
cmpl $0x20, %edi
jne 0x3b02
movl 0x20(%r14), %ecx
leaq 0x76c0(%rip), %rdi      # 0xb0bc
leaq 0x7a06(%rip), %rdx      # 0xb409
movl $0x158, %esi            # imm = 0x158
xorl %eax, %eax
callq 0x9818
movl 0x38(%r14), %edi
testl %edi, %edi
js 0x3a5e
leaq -0x1020(%rbp), %rsi
movl $0xfff, %edx            # imm = 0xFFF
callq 0x2200
testl %eax, %eax
jle 0x3a5e
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, -0x1020(%rbp,%rax)
movl 0x20(%r14), %ecx
leaq 0x7678(%rip), %rdi      # 0xb0bc
leaq 0x79d4(%rip), %rdx      # 0xb41f
leaq -0x1020(%rbp), %r8
movl $0x15e, %esi            # imm = 0x15E
xorl %eax, %eax
callq 0x9818
movl $0x20, (%r15)
jmp 0x3a76
cmpl $0x0, 0x40(%r14)
jne 0x3ae3
movl $0x1, 0x40(%r14)
movl %ebx, %eax
addq $0x1008, %rsp           # imm = 0x1008
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x791b(%rip), %rdi      # 0xb3a8
leaq 0x7628(%rip), %rsi      # 0xb0bc
leaq 0x7924(%rip), %rcx      # 0xb3bf
movl $0x146, %edx            # imm = 0x146
callq 0x21b0
leaq 0x7942(%rip), %rdi      # 0xb3ee
leaq 0x7609(%rip), %rsi      # 0xb0bc
leaq 0x7905(%rip), %rcx      # 0xb3bf
movl $0x147, %edx            # imm = 0x147
callq 0x21b0
leaq 0x7835(%rip), %rdi      # 0xb300
leaq 0x75ea(%rip), %rsi      # 0xb0bc
leaq 0x78e6(%rip), %rcx      # 0xb3bf
movl $0x154, %edx            # imm = 0x154
callq 0x21b0
leaq 0x784c(%rip), %rdi      # 0xb336
leaq 0x75cb(%rip), %rsi      # 0xb0bc
leaq 0x78c7(%rip), %rcx      # 0xb3bf
movl $0x16d, %edx            # imm = 0x16D
callq 0x21b0
movq %r15, %rsi
callq 0x24a3