// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x1000, %rsp           # imm = 0x1000
movq %rdi, %r14
movl 0x30(%rdi), %edi
testl %edi, %edi
js 0x307c
cmpl (%r14), %edi
jne 0x309b
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 0x2fb3
je 0x305b
addl %ebx, 0x28(%r15)
jmp 0x306a
callq 0x2080
cmpl $-0x1, %ebx
jne 0x30ba
movq %rax, %r15
movl (%rax), %r12d
cmpl $0x4, %r12d
je 0x306a
cmpl $0xb, %r12d
je 0x306a
cmpl $0x20, %r12d
jne 0x30f8
movl 0x20(%r14), %ecx
leaq 0x70cc(%rip), %rdi      # 0xa0bc
leaq 0x7412(%rip), %rdx      # 0xa409
movl $0x158, %esi            # imm = 0x158
xorl %eax, %eax
callq 0x8e04
movl 0x38(%r14), %edi
testl %edi, %edi
js 0x3052
leaq -0x1020(%rbp), %rsi
movl $0xfff, %edx            # imm = 0xFFF
callq 0x2200
testl %eax, %eax
jle 0x3052
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, -0x1020(%rbp,%rax)
movl 0x20(%r14), %ecx
leaq 0x7084(%rip), %rdi      # 0xa0bc
leaq 0x73e0(%rip), %rdx      # 0xa41f
leaq -0x1020(%rbp), %r8
movl $0x15e, %esi            # imm = 0x15E
xorl %eax, %eax
callq 0x8e04
movl $0x20, (%r15)
jmp 0x306a
cmpl $0x0, 0x40(%r14)
jne 0x30d9
movl $0x1, 0x40(%r14)
movl %ebx, %eax
addq $0x1000, %rsp           # imm = 0x1000
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7325(%rip), %rdi      # 0xa3a8
leaq 0x7032(%rip), %rsi      # 0xa0bc
leaq 0x732e(%rip), %rcx      # 0xa3bf
movl $0x146, %edx            # imm = 0x146
callq 0x21b0
leaq 0x734c(%rip), %rdi      # 0xa3ee
leaq 0x7013(%rip), %rsi      # 0xa0bc
leaq 0x730f(%rip), %rcx      # 0xa3bf
movl $0x147, %edx            # imm = 0x147
callq 0x21b0
leaq 0x723f(%rip), %rdi      # 0xa300
leaq 0x6ff4(%rip), %rsi      # 0xa0bc
leaq 0x72f0(%rip), %rcx      # 0xa3bf
movl $0x154, %edx            # imm = 0x154
callq 0x21b0
leaq 0x7256(%rip), %rdi      # 0xa336
leaq 0x6fd5(%rip), %rsi      # 0xa0bc
leaq 0x72d1(%rip), %rcx      # 0xa3bf
movl $0x16d, %edx            # imm = 0x16D
callq 0x21b0
leaq 0x6fbd(%rip), %rdi      # 0xa0bc
leaq 0x73a9(%rip), %rdx      # 0xa4af
movl $0x164, %esi            # imm = 0x164
movl %r12d, %ecx
xorl %eax, %eax
callq 0x8e04
movl %r12d, (%r15)
leaq 0x73a7(%rip), %rdi      # 0xa4c6
callq 0x2300
pushq $0x1
popq %rdi
callq 0x2340