// File: /workspace/github_c_makefile_star2_O3/anachronic[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 <= HISTSIZE + 2; n++) {
		bp = TLINE_HIST(n);
		lastpos = MIN(tlinehistlen(n) + 1, term.col) - 1;
		if (lastpos < 0)
			break;
        if (lastpos == 0)
            continue;
		end = &bp[lastpos + 1];
		for (; bp < end; ++bp)
			if (xwrite(to[1], buf, utf8encode(bp->u, buf)) < 0)
				break;
		if ((newline = TLINE_HIST(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 $0x38, %rsp
movq %rdi, %rbx
leaq 0x4(%rsp), %rdi
callq 0x53f0
cmpl $-0x1, %eax
je 0x79ec
callq 0x57d0
movl 0x4(%rsp), %edi
cmpl $-0x1, %eax
je 0x79de
testl %eax, %eax
je 0x79fb
callq 0x50f0
movl $0x1, %esi
movl $0xd, %edi
callq 0x5060
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
xorl %ebx, %ebx
movslq 0x1109f(%rip), %rax     # 0x188e0
movl $0x7d2, %ecx            # imm = 0x7D2
subl %eax, %ecx
movslq %ecx, %rcx
leaq -0x3e80(,%r14,8), %rdx
addq 0x1108e(%rip), %rdx     # 0x188e8
cmpq %rcx, %r14
leaq 0x1107c(%rip), %rcx     # 0x188e0
leaq 0x10(%rcx,%r14,8), %r12
leaq -0x18(%rdx,%rax,8), %rax
cmovleq %r12, %rax
movq (%rax), %r15
movl %r14d, %edi
callq 0x6088
incl %eax
movl 0x1105f(%rip), %ecx     # 0x188e4
cmpl %ecx, %eax
cmovgel %ecx, %eax
testl %eax, %eax
jle 0x7992
movl %eax, %ecx
decl %ecx
je 0x7982
movl %ecx, 0xc(%rsp)
movq %r12, 0x28(%rsp)
leaq -0x7d0(%r14), %rcx
movq %rcx, 0x20(%rsp)
movl %eax, %eax
shlq $0x4, %rax
addq %r15, %rax
movq %rax, 0x10(%rsp)
movl 0x8(%rsp), %ebp
movl (%r15), %edi
leaq 0x34(%rsp), %r12
movq %r12, %rsi
callq 0x5ff4
testq %rax, %rax
je 0x7900
movq %rax, %rbx
movq %r12, %r13
movq %rax, %r12
movl %ebp, %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x5870
testq %rax, %rax
js 0x790b
addq %rax, %r13
subq %rax, %r12
jne 0x78e1
testq %rbx, %rbx
js 0x790b
addq $0x10, %r15
cmpq 0x10(%rsp), %r15
jb 0x78bf
movslq 0x10fce(%rip), %rax     # 0x188e0
movl $0x7d2, %ecx            # imm = 0x7D2
subl %eax, %ecx
movslq %ecx, %rcx
movq 0x20(%rsp), %rdx
shlq $0x3, %rdx
addq 0x10fbc(%rip), %rdx     # 0x188e8
cmpq %rcx, %r14
leaq -0x18(%rdx,%rax,8), %rax
movq 0x28(%rsp), %rcx
cmovgq %rax, %rcx
movq (%rcx), %rax
movl 0xc(%rsp), %ecx
shlq $0x4, %rcx
movzwl 0x4(%rax,%rcx), %ebx
andl $0x200, %ebx            # imm = 0x200
jne 0x7982
movl $0x1, %r12d
movl 0x8(%rsp), %ebp
leaq 0x9d1f(%rip), %rbx      # 0x11685
movl %ebp, %edi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x5870
testq %rax, %rax
js 0x79c4
addq %rax, %rbx
subq %rax, %r12
jne 0x7966
xorl %ebx, %ebx
incq %r14
cmpq $0x7d3, %r14            # imm = 0x7D3
jne 0x783a
testl %ebx, %ebx
je 0x79c4
movl $0x1, %r14d
leaq 0x9ce2(%rip), %r15      # 0x11685
movl 0x8(%rsp), %ebp
movl %ebp, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x5870
testq %rax, %rax
js 0x79c4
subq %rax, %r14
addq %rax, %r15
testq %r14, %r14
jne 0x79a7
movl 0x8(%rsp), %edi
callq 0x50f0
movl $0xd, %edi
movq 0x18(%rsp), %rsi
callq 0x5060
jmp 0x79ec
callq 0x50f0
movl 0x8(%rsp), %edi
callq 0x50f0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4(%rsp), %rsi
movq %rbx, %rdx
callq 0x5a75