// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rsi, %r15
cmpq $0x0, (%rdi)
je 0xa9ef
movq %rdi, %rbx
movl 0x4(%r15), %eax
testl %eax, %eax
jle 0xa9f1
incl %eax
movq (%rbx), %rbx
cmpq $0x0, 0x18(%rbx)
je 0xa9ef
addq $0x18, %rbx
decl %eax
cmpl $0x1, %eax
jg 0xa9d8
jmp 0xa9f1
xorl %ebx, %ebx
testq %rbx, %rbx
je 0xaa38
leaq 0x10(%rsp), %rdi
callq 0x4280
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xaac7
leaq 0x8(%rsp), %rdi
callq 0x4280
testl %eax, %eax
je 0xaa4b
callq 0x40a0
movq %rax, %rbx
movl (%rax), %ebp
movl 0x10(%rsp), %edi
callq 0x4270
movl 0x14(%rsp), %edi
jmp 0xaac0
callq 0x40a0
movl $0x16, (%rax)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0xaac7
callq 0x4520
cmpl $-0x1, %eax
je 0xaaa9
movl %eax, %ebp
testl %eax, %eax
je 0xab88
movl 0x10(%rsp), %edi
callq 0x4270
movl 0xc(%rsp), %edi
callq 0x4270
movl 0xc(%r15), %esi
subl 0x4(%r15), %esi
movq (%rbx), %rdi
movq %rdi, 0x18(%rsp)
testq %rdi, %rdi
je 0xaad7
testl %esi, %esi
js 0xaadb
leal 0x2(%rsi), %ecx
leaq 0x18(%rsp), %rax
movq (%rax), %rax
cmpq $0x0, 0x18(%rax)
je 0xaad7
addq $0x18, %rax
decl %ecx
cmpl $0x1, %ecx
jg 0xaa92
jmp 0xaae0
callq 0x40a0
movq %rax, %rbx
movl (%rax), %ebp
movl 0x8(%rsp), %edi
callq 0x4270
movl 0xc(%rsp), %edi
callq 0x4270
movl %ebp, (%rbx)
movl %r12d, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0xaae0
leaq 0x18(%rsp), %rax
incl %esi
testq %rax, %rax
je 0xaaec
movq (%rax), %r15
jmp 0xaaef
xorl %r15d, %r15d
movl 0x14(%rsp), %edx
movl $0x1, %ecx
callq 0x9dcd
movl 0x14(%rsp), %edi
callq 0x4270
movl 0x8(%rsp), %edi
leaq 0x18(%rsp), %rsi
callq 0x9a22
movq %rax, %r14
movl 0x8(%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)
movq 0x20(%rdi), %rax
testq %rax, %rax
je 0xab46
movq %r14, 0x18(%rax)
movq %r14, %rax
movq 0x18(%r14), %r14
testq %r14, %r14
jne 0xab46
movq %r15, 0x18(%rax)
testq %r15, %r15
je 0xab5f
movq %rax, 0x20(%r15)
movq %rdi, %rcx
testq %rcx, %rcx
je 0xab80
movq %rcx, %rax
movq 0x18(%rcx), %rcx
cmpq %r15, %rcx
jne 0xab62
movq $0x0, 0x18(%rax)
callq 0x9d7b
xorl %r12d, %r12d
jmp 0xaac7
movl 0x10(%rsp), %edi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x41b0
movl 0xc(%rsp), %edi
movl $0x1, %esi
callq 0x41b0
movl 0xc(%rsp), %edi
movl $0x2, %esi
callq 0x41b0
movl 0x10(%rsp,%rbx,4), %edi
callq 0x4270
movl 0x8(%rsp,%rbx,4), %edi
callq 0x4270
incq %rbx
cmpq $0x1, %rbx
je 0xabb1
leaq 0x4af4(%rip), %rdi      # 0xf6c7
leaq 0x4af2(%rip), %rsi      # 0xf6cc
leaq 0x4aee(%rip), %rdx      # 0xf6cf
movq %r14, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x44f0
movl $0x7f, %edi
callq 0x44b0