// File: /workspace/github_c_makefile_star2_O3/mcaimi[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(shell, (char *const []) {
        shell,
        "-c",
        (char *)arg->v,
        0
      });

      fprintf(stderr, "st: execvp %s ", ((char **)arg->v)[0]);
      perror("failed");
      exit(0);
  }

  close(to[0]);

  /* ignore sigpipe for now, in case child exits 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 $0x48, %rsp
movq %rdi, %rbx
movq %rsp, %rdi
callq 0x6440
cmpl $-0x1, %eax
je 0x9115
callq 0x68d0
movl (%rsp), %edi
cmpl $-0x1, %eax
je 0x9107
testl %eax, %eax
je 0x9124
callq 0x6110
movl $0x1, %esi
movl $0xd, %edi
callq 0x6070
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x156e5(%rip)     # 0x1e698
jle 0x90ed
xorl %edi, %edi
callq 0x7987
incl %eax
movl 0x156d4(%rip), %ecx     # 0x1e69c
cmpl %ecx, %eax
cmovgel %ecx, %eax
testl %eax, %eax
jle 0x90ed
xorl %r14d, %r14d
movq 0x156c1(%rip), %rcx     # 0x1e6a0
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 0x723d
testq %rax, %rax
je 0x9039
movq %rax, %rbx
movq %r12, %r13
movq %rax, %r12
movl %ebp, %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x6990
testq %rax, %rax
js 0x9044
addq %rax, %r13
subq %rax, %r12
jne 0x901a
testq %rbx, %rbx
js 0x9044
addq $0x10, %r14
cmpq 0x8(%rsp), %r14
jb 0x8ff8
movq 0x15655(%rip), %rax     # 0x1e6a0
movq 0x18(%rsp), %r14
movq (%rax,%r14,8), %rax
movzwl -0xc(%rax,%r15), %ebx
andl $0x100, %ebx            # imm = 0x100
jne 0x908f
movl $0x1, %r12d
movl 0x4(%rsp), %ebp
leaq 0x9634(%rip), %rbx      # 0x126a7
movl %ebp, %edi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x6990
testq %rax, %rax
js 0x90ed
addq %rax, %rbx
subq %rax, %r12
jne 0x9073
xorl %ebx, %ebx
incq %r14
movslq 0x155ff(%rip), %rax     # 0x1e698
cmpq %rax, %r14
jge 0x90bb
movl %r14d, %edi
callq 0x7987
incl %eax
movl 0x155ee(%rip), %ecx     # 0x1e69c
cmpl %ecx, %eax
cmovgel %ecx, %eax
testl %eax, %eax
jg 0x8fd8
testl %ebx, %ebx
je 0x90ed
movl $0x1, %r14d
leaq 0x95db(%rip), %r15      # 0x126a7
movl 0x4(%rsp), %ebp
movl %ebp, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x6990
testq %rax, %rax
js 0x90ed
subq %rax, %r14
addq %rax, %r15
testq %r14, %r14
jne 0x90d0
movl 0x4(%rsp), %edi
callq 0x6110
movl $0xd, %edi
movq 0x10(%rsp), %rsi
callq 0x6070
jmp 0x9115
callq 0x6110
movl 0x4(%rsp), %edi
callq 0x6110
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsp, %rsi
leaq 0x28(%rsp), %rdx
movq %rbx, %rcx
callq 0x6ced