// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdi, %rbx
movq %rsp, %rdi
callq 0x6440
cmpl $-0x1, %eax
je 0x869b
callq 0x68d0
movl (%rsp), %edi
cmpl $-0x1, %eax
je 0x865e
testl %eax, %eax
je 0x86aa
callq 0x6110
pushq $0xd
popq %rdi
pushq $0x1
popq %rsi
callq 0x6070
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
leaq 0x14(%rsp), %r12
xorl %ebp, %ebp
movslq 0x160da(%rip), %rax     # 0x1e698
cmpq %rax, %r15
jge 0x866e
movl %r15d, %edi
callq 0x7307
incl %eax
movl 0x160c5(%rip), %ecx     # 0x1e69c
cmpl %ecx, %eax
cmovgel %ecx, %eax
testl %eax, %eax
jle 0x866e
movq 0x160b5(%rip), %rcx     # 0x1e6a0
movq (%rcx,%r15,8), %r13
movl %eax, %ebx
shlq $0x4, %rbx
leaq (%rbx,%r13), %r14
cmpq %r14, %r13
jae 0x8624
movl 0x4(%rsp), %ebp
movl (%r13), %edi
movq %r12, %rsi
callq 0x6cf6
movl %ebp, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x8721
addq $0x10, %r13
testq %rax, %rax
jns 0x85f9
movq 0x16075(%rip), %rax     # 0x1e6a0
movq (%rax,%r15,8), %rax
movzwl -0xc(%rax,%rbx), %ebp
andl $0x100, %ebp            # imm = 0x100
jne 0x8656
movl 0x4(%rsp), %edi
leaq 0x9030(%rip), %rsi      # 0x11677
pushq $0x1
popq %rdx
callq 0x8721
xorl %ebp, %ebp
testq %rax, %rax
js 0x8685
incq %r15
jmp 0x85b7
callq 0x6110
movl 0x4(%rsp), %edi
callq 0x6110
jmp 0x869b
testl %ebp, %ebp
je 0x8685
movl 0x4(%rsp), %edi
leaq 0x8ffa(%rip), %rsi      # 0x11677
pushq $0x1
popq %rdx
callq 0x8721
movl 0x4(%rsp), %edi
callq 0x6110
pushq $0xd
popq %rdi
movq 0x8(%rsp), %rsi
callq 0x6070
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %esi, %esi
callq 0x6080
movl (%rsp), %edi
callq 0x6110
movl 0x4(%rsp), %edi
callq 0x6110
leaq 0x14e9f(%rip), %rax     # 0x1d568
movq (%rax), %rdi
leaq 0x18(%rsp), %rsi
movq %rdi, (%rsi)
leaq 0x8fd0(%rip), %rax      # 0x116ab
movq %rax, 0x8(%rsi)
movq (%rbx), %rax
movq %rax, 0x10(%rsi)
andq $0x0, 0x18(%rsi)
callq 0x6190
movq 0x148f9(%rip), %rax     # 0x1cff0
movq (%rax), %rdi
movq (%rbx), %rax
movq (%rax), %rdx
leaq 0x8fa7(%rip), %rsi      # 0x116ae
xorl %eax, %eax
callq 0x6970
leaq 0x9250(%rip), %rdi      # 0x11965
callq 0x6950
xorl %edi, %edi
callq 0x61b0