// File: /workspace/github_c_makefile_star2_O3/bobrippling[P]tim/list.c
// Function: list_filter
int list_filter(
		list_t **pl, const region_t *region,
		const char *cmd)
{
	pl = list_seekp(pl, region->begin.y, false);

	if(!pl){
		errno = EINVAL;
		return -1;
	}

	int child_in[2], child_out[2];
	if(pipe(child_in))
		return -1;
	if(pipe(child_out)){
		const int e = errno;
		close(child_in[0]);
		close(child_in[1]);
		errno = e;
		return -1;
	}

	pid_t pid = fork();
	switch(pid){
		case -1:
		{
			const int e = errno;
			close(child_out[0]);
			close(child_out[1]);
			errno = e;
			return -1;
		}
		case 0:
			dup2(child_in[0], 0);
			dup2(child_out[1], 1);
			dup2(child_out[1], 2); /* capture stderr too */
			for(int i = 0; i < 2; i++)
				close(child_in[i]), close(child_out[i]);

			execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
			exit(127);
	}

	/* parent */
	close(child_in[0]), close(child_out[1]);

	const unsigned region_height = region->end.y - region->begin.y + 1;

	list_t **phead = pl;
	list_t *tail = list_seek(*phead, region_height, false);

	/* write our lines to child_in */
	list_write_fd(
			*pl, region_height,
			child_in[1], /*eol:*/true);

	/* writes done */
	close(child_in[1]);

	/* read from child_out */
	bool eol;
	list_t *l_read = list_new_fd(child_out[0], &eol);
	close(child_out[0]);

	/* reap */
	(void)waitpid(pid, NULL, 0);

	list_t *const gone = *pl;

	*pl = l_read;
	l_read->prev = gone->prev;
	if(gone->prev)
		gone->prev->next = l_read;
	for(; l_read->next; l_read = l_read->next);
	l_read->next = tail;
	if(tail)
		tail->prev = l_read;

	list_t *gone_tail;
	for(gone_tail = gone;
			gone_tail && gone_tail->next != tail;
			gone_tail = gone_tail->next);

	if(gone_tail){
		gone_tail->next = NULL;
		list_free(gone);
	}/* else `gone' was at the end already */

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq (%rdi), %rax
testq %rax, %rax
je 0xa7fd
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movl 0x4(%rsi), %ecx
testl %ecx, %ecx
jle 0xa7bf
incl %ecx
movq %rax, %rbx
movq 0x18(%rax), %rax
testq %rax, %rax
je 0xa7fd
decl %ecx
cmpl $0x1, %ecx
jg 0xa7a8
addq $0x18, %rbx
leaq 0x14(%rsp), %rdi
callq 0x4280
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xa80e
leaq 0xc(%rsp), %rdi
callq 0x4280
testl %eax, %eax
je 0xa81e
callq 0x40a0
movq %rax, %rbx
movl (%rax), %ebp
movl 0x14(%rsp), %edi
callq 0x4270
movl 0x18(%rsp), %edi
jmp 0xa88d
callq 0x40a0
movl $0x16, (%rax)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r12d, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x4520
cmpl $-0x1, %eax
je 0xa876
movl %eax, %ebp
testl %eax, %eax
je 0xa931
movl 0x14(%rsp), %edi
callq 0x4270
movl 0x10(%rsp), %edi
callq 0x4270
movl 0xc(%r15), %eax
subl 0x4(%r15), %eax
leal 0x1(%rax), %esi
movq (%rbx), %rdi
testq %rdi, %rdi
je 0xa899
movq %rdi, %r15
testl %eax, %eax
js 0xa89c
addl $0x2, %eax
movq %rdi, %r15
movq 0x18(%r15), %r15
testq %r15, %r15
je 0xa899
decl %eax
cmpl $0x1, %eax
jg 0xa864
jmp 0xa89c
callq 0x40a0
movq %rax, %rbx
movl (%rax), %ebp
movl 0xc(%rsp), %edi
callq 0x4270
movl 0x10(%rsp), %edi
callq 0x4270
movl %ebp, (%rbx)
jmp 0xa80e
xorl %r15d, %r15d
movl 0x18(%rsp), %edx
movl $0x1, %ecx
callq 0x9c22
movl 0x18(%rsp), %edi
callq 0x4270
movl 0xc(%rsp), %edi
leaq 0x1f(%rsp), %rsi
callq 0x987e
movq %rax, %r14
movl 0xc(%rsp), %edi
callq 0x4270
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4410
movq (%rbx), %rdi
movq %r14, (%rbx)
movq 0x20(%rdi), %rax
movq %rax, 0x20(%r14)
testq %rax, %rax
je 0xa8ef
movq %r14, 0x18(%rax)
movq %r14, %rax
movq 0x18(%r14), %r14
testq %r14, %r14
jne 0xa8ef
movq %r15, 0x18(%rax)
testq %r15, %r15
je 0xa908
movq %rax, 0x20(%r15)
movq %rdi, %rcx
testq %rcx, %rcx
je 0xa929
movq %rcx, %rax
movq 0x18(%rcx), %rcx
cmpq %r15, %rcx
jne 0xa90b
movq $0x0, 0x18(%rax)
callq 0x9bd0
xorl %r12d, %r12d
jmp 0xa80e
movl 0x14(%rsp), %edi
xorl %esi, %esi
callq 0x41b0
movl 0x10(%rsp), %edi
movl $0x1, %esi
callq 0x41b0
movl 0x10(%rsp), %edi
movl $0x2, %esi
callq 0x41b0
movl 0x14(%rsp), %edi
callq 0x4270
movl 0xc(%rsp), %edi
callq 0x4270
movl 0x18(%rsp), %edi
callq 0x4270
movl 0x10(%rsp), %edi
callq 0x4270
leaq 0x3d35(%rip), %rdi      # 0xe6b8
leaq 0x3d33(%rip), %rsi      # 0xe6bd
leaq 0x3d2f(%rip), %rdx      # 0xe6c0
movq %r14, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x44f0
movl $0x7f, %edi
callq 0x44b0