// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]spiped/liball/../lib/proto/proto_pipe.c
// Function: callback_pipe_read
static int
callback_pipe_read(void * cookie, int status)
{
	struct pipe_cookie * P = cookie;
	uint8_t * inbuf;
	size_t inlen;
	size_t inpos = 0;
	size_t outpos = 0;
	size_t loop_inlen;
	ssize_t loop_outlen;

	/* Did we read EOF? */
	if (status == 1)
		goto eof;

	/* Did the read fail? */
	if (status == -1)
		goto fail;

	/* Get data. */
	netbuf_read_peek(P->R, &inbuf, &inlen);

	/* Process as many packets as possible. */
	while (inlen > 0) {
		/* Stop processing if we don't have space for more output. */
		if (outpos + P->full_buflen > OUTBUFSIZE)
			break;

		/* How many bytes should we process this time? */
		loop_inlen = (inlen > P->full_buflen) ? P->full_buflen : inlen;

		/*
		 * If we don't have enough data to decrypt, leave it until the
		 * next time callback_pipe_read() is called.
		 */
		if ((P->decr) && (loop_inlen < PCRYPT_ESZ))
			break;

		/* Encrypt or decrypt the data. */
		if (P->decr) {
			if ((loop_outlen = proto_crypt_dec(&inbuf[inpos],
			    &P->outbuf[outpos], P->k)) == -1)
				goto fail;
		} else {
			proto_crypt_enc(&inbuf[inpos], loop_inlen,
			    &P->outbuf[outpos], P->k);
			loop_outlen = PCRYPT_ESZ;
		}

		/* We've processed this data. */
		inlen -= loop_inlen;
		inpos += loop_inlen;
		outpos += (size_t)loop_outlen;
	}

	/* Let netbuf layer know what we've used. */
	netbuf_read_consume(P->R, inpos);

	/* Write the encrypted or decrypted data. */
	P->wlen = (ssize_t)outpos;
	if ((P->write_cookie = network_write(P->s_out, P->outbuf,
	    (size_t)P->wlen, (size_t)P->wlen, callback_pipe_write,
	    P)) == NULL)
		goto err0;

	/* Success! */
	return (0);

eof:
	/* We aren't going to write any more. */
	if (shutdown(P->s_out, SHUT_WR)) {
		switch (errno) {
		case EBADF:
		case EINVAL:
		case ENOTSOCK:
			/* Should never happen. */
			goto err0;
		case ENOTCONN:
		case ECONNRESET:
			/* Simultaneous closes; not a problem. */
			break;
		default:
			/*
			 * Unexpected error; treat this as a broken connection
			 * but don't die in case the error originates from some
			 * sort of network issue rather than from an error in
			 * spiped itself.
			 */
			goto fail;
		}
	}

	/* Record that we have reached EOF. */
	*(P->status) = 0;

	/* Inform the upstream that our status has changed. */
	return ((P->callback)(P->cookie));

fail:
	/* Record that this connection is broken. */
	*(P->status) = -1;

	/* Inform the upstream that our status has changed. */
	return ((P->callback)(P->cookie));

err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %r8
cmpl $-0x1, %esi
je 0x9a7b
cmpl $0x1, %esi
movq %r8, %r13
jne 0x99ca
movl 0x1c(%r8), %edi
pushq $0x1
popq %rsi
callq 0x2090
movq %r13, %r8
testl %eax, %eax
je 0x99af
callq 0x2370
movl (%rax), %eax
cmpl $0x9, %eax
je 0x9ad1
cmpl $0x16, %eax
je 0x9ad1
cmpl $0x58, %eax
je 0x9ad1
cmpl $0x6b, %eax
movq %r13, %r8
je 0x99af
cmpl $0x68, %eax
jne 0x9a7b
movq 0x10(%r8), %rax
andl $0x0, (%rax)
movq 0x8(%r8), %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmpq *(%r8)
movq 0x2150(%r8), %rdi
leaq 0x8(%rsp), %rsi
movq %rsp, %rbx
movq %rbx, %rdx
callq 0xa253
movq %r13, %r8
movq (%rbx), %rcx
leaq 0x30(%r13), %rbx
xorl %r15d, %r15d
xorl %r14d, %r14d
testq %rcx, %rcx
je 0x9a8b
movq 0x2170(%r8), %r12
leaq (%r12,%r14), %rax
cmpq $0x2120, %rax           # imm = 0x2120
ja 0x9a8b
cmpq %r12, %rcx
cmovbq %rcx, %r12
movl 0x20(%r8), %eax
testl %eax, %eax
setne %cl
cmpq $0x424, %r12            # imm = 0x424
setb %dl
testb %cl, %dl
jne 0x9a8b
movq 0x8(%rsp), %rdi
addq %r15, %rdi
leaq (%rbx,%r14), %rdx
movq 0x28(%r8), %rcx
testl %eax, %eax
je 0x9a55
movq %rdx, %rsi
movq %rcx, %rdx
callq 0x3d8d
cmpq $-0x1, %rax
movq %r13, %r8
jne 0x9a65
jmp 0x9a7b
movq %r12, %rsi
callq 0x3c8b
movl $0x424, %eax            # imm = 0x424
movq %r13, %r8
movq (%rsp), %rcx
subq %r12, %rcx
movq %rcx, (%rsp)
addq %r12, %r15
addq %rax, %r14
jmp 0x99f1
movq 0x10(%r8), %rax
orl $-0x1, (%rax)
movq 0x8(%r8), %rdi
callq *(%r8)
jmp 0x9ad4
movq 0x2150(%r8), %rdi
movq %r15, %rsi
movq %r8, %r15
callq 0xa4cb
movq %r14, 0x2160(%r15)
movl 0x1c(%r15), %edi
leaq 0x69(%rip), %r8         # 0x9b18
movq %rbx, %rsi
movq %r14, %rdx
movq %r14, %rcx
movq %r15, %r9
callq 0xa8c4
movq %rax, %rcx
movq %rax, 0x2158(%r15)
xorl %eax, %eax
testq %rcx, %rcx
jne 0x9ad4
pushq $-0x1
popq %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq