// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %esi
xorl %edx, %edx
callq 0xcc90
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0xde9b
callq 0x40a0
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe149
leaq -0x28(%rbp), %rdi
callq 0x4280
cmpl $0x0, %eax
je 0xdeb5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe149
leaq -0x30(%rbp), %rdi
callq 0x4280
cmpl $0x0, %eax
je 0xdef9
callq 0x40a0
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x28(%rbp), %edi
callq 0x4270
movl -0x24(%rbp), %edi
callq 0x4270
movl -0x34(%rbp), %eax
movl %eax, -0x7c(%rbp)
callq 0x40a0
movl -0x7c(%rbp), %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe149
callq 0x4510
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x80(%rbp)
subl $-0x1, %eax
je 0xdf1a
jmp 0xdf0e
movl -0x80(%rbp), %eax
testl %eax, %eax
je 0xdf56
jmp 0xdfdb
callq 0x40a0
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x30(%rbp), %edi
callq 0x4270
movl -0x2c(%rbp), %edi
callq 0x4270
movl -0x3c(%rbp), %eax
movl %eax, -0x84(%rbp)
callq 0x40a0
movl -0x84(%rbp), %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe149
movl -0x28(%rbp), %edi
xorl %esi, %esi
callq 0x41b0
movl -0x2c(%rbp), %edi
movl $0x1, %esi
callq 0x41b0
movl -0x2c(%rbp), %edi
movl $0x2, %esi
callq 0x41b0
movl $0x0, -0x40(%rbp)
cmpl $0x2, -0x40(%rbp)
jge 0xdfac
movslq -0x40(%rbp), %rax
movl -0x28(%rbp,%rax,4), %edi
callq 0x4270
movslq -0x40(%rbp), %rax
movl -0x30(%rbp,%rax,4), %edi
callq 0x4270
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xdf81
movq -0x20(%rbp), %rcx
leaq 0x77f0(%rip), %rdi      # 0x157a7
leaq 0x77ee(%rip), %rsi      # 0x157ac
leaq 0x77ea(%rip), %rdx      # 0x157af
xorl %eax, %eax
movl %eax, %r8d
movb $0x0, %al
callq 0x44e0
movl $0x7f, %edi
callq 0x44b0
movl -0x28(%rbp), %edi
callq 0x4270
movl -0x2c(%rbp), %edi
callq 0x4270
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x4(%rcx), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rdi
movl -0x44(%rbp), %esi
xorl %edx, %edx
callq 0xcd50
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl -0x44(%rbp), %esi
movl -0x24(%rbp), %edx
movl $0x1, %ecx
callq 0xcbb0
movl -0x24(%rbp), %edi
callq 0x4270
movl -0x30(%rbp), %edi
leaq -0x59(%rbp), %rsi
callq 0xc7e0
movq %rax, -0x68(%rbp)
movl -0x30(%rbp), %edi
callq 0x4270
movl -0x38(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x4410
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x70(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0xe0a2
movq -0x68(%rbp), %rcx
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rax
movq %rcx, 0x18(%rax)
jmp 0xe0a4
movq -0x68(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xe0bf
jmp 0xe0b1
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0xe0a4
movq -0x58(%rbp), %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x18(%rax)
cmpq $0x0, -0x58(%rbp)
je 0xe0de
movq -0x68(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x78(%rbp)
movb %al, -0x85(%rbp)
je 0xe10a
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x58(%rbp), %rax
setne %al
movb %al, -0x85(%rbp)
movb -0x85(%rbp), %al
testb $0x1, %al
jne 0xe116
jmp 0xe126
jmp 0xe118
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0xe0e6
cmpq $0x0, -0x78(%rbp)
je 0xe142
movq -0x78(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x70(%rbp), %rdi
callq 0xcb10
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)