// File: /workspace/github_c_makefile_star2_O3/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
movq %rsp, %rdi
callq 0x64f0
cmpl $-0x1, %eax
je 0x9037
callq 0x6900
movl (%rsp), %edi
cmpl $-0x1, %eax
je 0x9029
testl %eax, %eax
je 0x9046
callq 0x60f0
movl $0x1, %esi
movl $0xd, %edi
callq 0x6060
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x1056b(%rip)     # 0x19440
jle 0x900f
xorl %edi, %edi
callq 0x793d
incl %eax
movl 0x1055a(%rip), %ecx     # 0x19444
cmpl %ecx, %eax
cmovgel %ecx, %eax
testl %eax, %eax
jle 0x900f
xorl %r14d, %r14d
movq 0x10547(%rip), %rcx     # 0x19448
movq %r14, 0x18(%rsp)
movq (%rcx,%r14,8), %r14
movl %eax, %r15d
shlq $0x4, %r15
leaq (%r14,%r15), %rax
movq %rax, 0x8(%rsp)
movl 0x4(%rsp), %ebp
movl (%r14), %edi
leaq 0x24(%rsp), %r12
movq %r12, %rsi
callq 0x7194
testq %rax, %rax
je 0x8f5b
movq %rax, %rbx
movq %r12, %r13
movq %rax, %r12
movl %ebp, %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x69d0
testq %rax, %rax
js 0x8f66
addq %rax, %r13
subq %rax, %r12
jne 0x8f3c
testq %rbx, %rbx
js 0x8f66
addq $0x10, %r14
cmpq 0x8(%rsp), %r14
jb 0x8f1a
movq 0x104db(%rip), %rax     # 0x19448
movq 0x18(%rsp), %r14
movq (%rax,%r14,8), %rax
movzwl -0xc(%rax,%r15), %ebx
andl $0x100, %ebx            # imm = 0x100
jne 0x8fb1
movl $0x1, %r12d
movl 0x4(%rsp), %ebp
leaq 0x86f0(%rip), %rbx      # 0x11685
movl %ebp, %edi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x69d0
testq %rax, %rax
js 0x900f
addq %rax, %rbx
subq %rax, %r12
jne 0x8f95
xorl %ebx, %ebx
incq %r14
movslq 0x10485(%rip), %rax     # 0x19440
cmpq %rax, %r14
jge 0x8fdd
movl %r14d, %edi
callq 0x793d
incl %eax
movl 0x10474(%rip), %ecx     # 0x19444
cmpl %ecx, %eax
cmovgel %ecx, %eax
testl %eax, %eax
jg 0x8efa
testl %ebx, %ebx
je 0x900f
movl $0x1, %r14d
leaq 0x8697(%rip), %r15      # 0x11685
movl 0x4(%rsp), %ebp
movl %ebp, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x69d0
testq %rax, %rax
js 0x900f
subq %rax, %r14
addq %rax, %r15
testq %r14, %r14
jne 0x8ff2
movl 0x4(%rsp), %edi
callq 0x60f0
movl $0xd, %edi
movq 0x10(%rsp), %rsi
callq 0x6060
jmp 0x9037
callq 0x60f0
movl 0x4(%rsp), %edi
callq 0x60f0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsp, %rsi
movq %rbx, %rdx
callq 0x6c15