// File: /workspace/github_c_makefile_star2_O1/odknt[P]st/st.c
// Function: externalpipe
void
externalpipe(const Arg *arg)
{
	int to[2];
	char buf[UTF_SIZ];
	void (*oldsigpipe)(int);
	Glyph *bp, *end;
	int lastpos, n, newline;

	if (pipe(to) == -1)
		return;

	switch (fork()) {
	case -1:
		close(to[0]);
		close(to[1]);
		return;
	case 0:
		dup2(to[0], STDIN_FILENO);
		close(to[0]);
		close(to[1]);
		execvp(((char **)arg->v)[0], (char **)arg->v);
		fprintf(stderr, "st: execvp %s\n", ((char **)arg->v)[0]);
		perror("failed");
		exit(0);
	}

	close(to[0]);
	/* ignore sigpipe for now, in case child exists early */
	oldsigpipe = signal(SIGPIPE, SIG_IGN);
	newline = 0;
	for (n = 0; n < term.row; n++) {
		bp = term.line[n];
		lastpos = MIN(tlinelen(n) + 1, term.col) - 1;
		if (lastpos < 0)
			break;
		end = &bp[lastpos + 1];
		for (; bp < end; ++bp)
			if (xwrite(to[1], buf, utf8encode(bp->u, buf)) < 0)
				break;
		if ((newline = term.line[n][lastpos].mode & ATTR_WRAP))
			continue;
		if (xwrite(to[1], "\n", 1) < 0)
			break;
		newline = 0;
	}
	if (newline)
		(void)xwrite(to[1], "\n", 1);
	close(to[1]);
	/* restore */
	signal(SIGPIPE, oldsigpipe);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0x4(%rsp), %rdi
callq 0x64f0
cmpl $-0x1, %eax
je 0x8d95
callq 0x6920
movl 0x4(%rsp), %edi
cmpl $-0x1, %eax
je 0x8d87
testl %eax, %eax
je 0x8da4
callq 0x60f0
movl $0x1, %esi
movl $0xd, %edi
callq 0x6060
movq %rax, %rbx
cmpl $0x0, 0xf843(%rip)      # 0x18450
jle 0x8d6f
xorl %edi, %edi
callq 0x785d
incl %eax
movl 0xf832(%rip), %ecx      # 0x18454
cmpl %ecx, %eax
cmovgel %ecx, %eax
testl %eax, %eax
jle 0x8d6f
movq %rbx, 0x10(%rsp)
xorl %r14d, %r14d
leaq 0xc(%rsp), %r13
movq 0xf815(%rip), %rcx      # 0x18458
movq %r14, 0x20(%rsp)
movq (%rcx,%r14,8), %r14
movl %eax, %eax
shlq $0x4, %rax
movq %rax, 0x18(%rsp)
leaq (%r14,%rax), %r15
movl 0x8(%rsp), %ebp
movl (%r14), %edi
movq %r13, %rsi
callq 0x71b4
testq %rax, %rax
je 0x8c91
movq %rax, %rbx
movq %rax, %r12
movl %ebp, %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x69f0
testq %rax, %rax
js 0x8c95
addq %rax, %r13
subq %rax, %r12
jne 0x8c75
jmp 0x8c98
movb $0x1, %al
jmp 0x8ca3
movq %rax, %rbx
testq %rbx, %rbx
setns %al
leaq 0xc(%rsp), %r13
testb %al, %al
je 0x8cb0
addq $0x10, %r14
cmpq %r15, %r14
jb 0x8c5b
movq 0xf7a1(%rip), %rax      # 0x18458
movq 0x20(%rsp), %r14
movq (%rax,%r14,8), %rax
movq 0x18(%rsp), %rcx
movzwl -0xc(%rax,%rcx), %ebx
andl $0x100, %ebx            # imm = 0x100
jne 0x8d0c
movl $0x1, %r12d
movl 0x8(%rsp), %ebp
leaq 0x7974(%rip), %r13      # 0x10657
movl %ebp, %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x69f0
testq %rax, %rax
js 0x8d00
subq %rax, %r12
addq %rax, %r13
testq %r12, %r12
jne 0x8ce3
xorl %ebx, %ebx
testq %rax, %rax
leaq 0xc(%rsp), %r13
js 0x8d38
incq %r14
movslq 0xf73a(%rip), %rax      # 0x18450
cmpq %rax, %r14
jge 0x8d38
movl %r14d, %edi
callq 0x785d
incl %eax
movl 0xf729(%rip), %ecx      # 0x18454
cmpl %ecx, %eax
cmovgel %ecx, %eax
testl %eax, %eax
jg 0x8c3c
testl %ebx, %ebx
movq 0x10(%rsp), %rbx
je 0x8d6f
movl $0x1, %r14d
leaq 0x7909(%rip), %r15      # 0x10657
movl 0x8(%rsp), %ebp
movl %ebp, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x69f0
testq %rax, %rax
js 0x8d6f
subq %rax, %r14
addq %rax, %r15
testq %r14, %r14
jne 0x8d52
movl 0x8(%rsp), %edi
callq 0x60f0
movl $0xd, %edi
movq %rbx, %rsi
callq 0x6060
jmp 0x8d95
callq 0x60f0
movl 0x8(%rsp), %edi
callq 0x60f0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4(%rsp), %rsi
movq %rbx, %rdx
callq 0x6c35