// File: /workspace/github_c_makefile_star2_O2/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 %rdx, %rbx
movq %rsi, %r12
movl 0x4(%rsi), %esi
xorl %edx, %edx
callq 0x96e1
testq %rax, %rax
je 0xa06f
movq %rax, %r14
leaq 0x14(%rsp), %rdi
callq 0x4280
pushq $-0x1
popq %rbp
testl %eax, %eax
jne 0xa17c
leaq 0xc(%rsp), %rdi
callq 0x4280
testl %eax, %eax
je 0xa082
callq 0x40a0
movq %rax, %rbx
movl (%rax), %r14d
movl 0x14(%rsp), %edi
callq 0x4270
movl 0x18(%rsp), %edi
jmp 0xa174
callq 0x40a0
movl $0x16, (%rax)
pushq $-0x1
popq %rbp
jmp 0xa17c
callq 0x4520
cmpl $-0x1, %eax
je 0xa15c
movl %eax, %r15d
testl %eax, %eax
je 0xa18b
movl 0x14(%rsp), %edi
callq 0x4270
movl 0x10(%rsp), %edi
callq 0x4270
movl 0xc(%r12), %ebp
subl 0x4(%r12), %ebp
incl %ebp
movq (%r14), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x9737
movq %rax, %rbx
movq (%r14), %rdi
movl 0x18(%rsp), %edx
pushq $0x1
popq %rcx
movl %ebp, %esi
callq 0x9648
movl 0x18(%rsp), %edi
callq 0x4270
movl 0xc(%rsp), %edi
leaq 0x1f(%rsp), %rsi
callq 0x92f0
movq %rax, %r12
movl 0xc(%rsp), %edi
callq 0x4270
movl %r15d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4410
movq (%r14), %rdi
movq %r12, (%r14)
movq 0x20(%rdi), %rax
movq %rax, 0x20(%r12)
testq %rax, %rax
je 0xa120
movq %r12, 0x18(%rax)
movq %r12, %rax
movq 0x18(%r12), %r12
testq %r12, %r12
jne 0xa120
movq %rbx, 0x18(%rax)
testq %rbx, %rbx
je 0xa13a
movq %rax, 0x20(%rbx)
movq %rdi, %rcx
testq %rcx, %rcx
je 0xa158
movq %rcx, %rax
movq 0x18(%rcx), %rcx
cmpq %rbx, %rcx
jne 0xa13d
andq $0x0, 0x18(%rax)
callq 0x95f9
xorl %ebp, %ebp
jmp 0xa17c
callq 0x40a0
movq %rax, %rbx
movl (%rax), %r14d
movl 0xc(%rsp), %edi
callq 0x4270
movl 0x10(%rsp), %edi
callq 0x4270
movl %r14d, (%rbx)
movl %ebp, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl 0x14(%rsp), %edi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x41b0
movl 0x10(%rsp), %edi
pushq $0x1
popq %rsi
callq 0x41b0
movl 0x10(%rsp), %edi
pushq $0x2
popq %rsi
callq 0x41b0
cmpq $0x2, %r14
je 0xa1d0
movl 0x14(%rsp,%r14,4), %edi
callq 0x4270
movl 0xc(%rsp,%r14,4), %edi
callq 0x4270
incq %r14
jmp 0xa1b1
leaq 0x44e1(%rip), %rdi      # 0xe6b8
leaq 0x44df(%rip), %rsi      # 0xe6bd
leaq 0x44db(%rip), %rdx      # 0xe6c0
movq %rbx, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x44f0
pushq $0x7f
popq %rdi
callq 0x44b0