// File: /workspace/github_c_makefile_star2_O3/mcaimi[P]st/st.c
// Function: ttynew
int
ttynew(char *line, char *cmd, char *out, char **args)
{
  int m, s;

  if (out) {
    term.mode |= MODE_PRINT;
    iofd = (!strcmp(out, "-")) ?
        1 : open(out, O_WRONLY | O_CREAT, 0666);
    if (iofd < 0) {
      fprintf(stderr, "Error opening %s:%s\n",
        out, strerror(errno));
    }
  }

  if (line) {
    if ((cmdfd = open(line, O_RDWR)) < 0)
      die("open line '%s' failed: %s\n", line, strerror(errno));
    dup2(cmdfd, 0);
    stty(args);
    return cmdfd;
  }

  /* seems to work fine on linux, openbsd and freebsd */
  if (openpty(&m, &s, NULL, NULL, NULL) < 0)
    die("openpty failed: %s\n", strerror(errno));

  switch (pid = fork()) {
  case -1:
    die("fork failed %s\n", strerror(errno));
    break;
  case 0:
    close(iofd);
    close(m);
    setsid(); /* create a new process group */
    dup2(s, 0);
    dup2(s, 1);
    dup2(s, 2);
    if (ioctl(s, TIOCSCTTY, NULL) < 0)
      die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
    if (s > 2)
      close(s);
#ifdef __OpenBSD__
    if (pledge("stdio getpw proc exec ps rpath wpath tty", NULL) == -1)
      die("Failed to execute pledge() system call.\n");
#endif
    execsh(cmd, args);
    break;
  default:
#ifdef __OpenBSD__
    if (pledge("stdio getpw proc exec ps rpath wpath tty", NULL) == -1)
      die("Failed to execute pledge() system call\n");
#endif
    close(s);
    cmdfd = m;
    signal(SIGCHLD, sigchld);
    break;
  }
  return cmdfd;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1010, %rsp           # imm = 0x1010
movq %rcx, %rbx
movq %rsi, %r14
movq %rdi, %r15
testq %rdx, %rdx
je 0x7aab
movq %rdx, %r12
orb $0x20, 0x16c7c(%rip)    # 0x1e6f4
cmpb $0x2d, (%rdx)
jne 0x7a89
cmpb $0x0, 0x1(%r12)
je 0x7c08
movq %r12, %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x69f0
movl %eax, 0x15aad(%rip)     # 0x1d550
testl %eax, %eax
js 0x7c17
testq %r15, %r15
je 0x7b93
movq %r15, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x69f0
movl %eax, 0x16c4f(%rip)     # 0x1e718
testl %eax, %eax
js 0x7c32
movl %eax, %edi
xorl %esi, %esi
callq 0x6080
leaq 0x15a8f(%rip), %rax     # 0x1d570
movq (%rax), %r14
movq %r14, %rdi
callq 0x63b0
cmpq $0x1000, %rax           # imm = 0x1000
jae 0x7c52
movq %rax, %r15
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x69d0
leaq (%rsp,%r15), %r12
addq $0x10, %r12
testq %rbx, %rbx
je 0x7b72
movq (%rbx), %r14
testq %r14, %r14
je 0x7b72
movl $0x1000, %r13d          # imm = 0x1000
subq %r15, %r13
addq $0x8, %rbx
movq %r14, %rdi
callq 0x63b0
movq %rax, %r15
leaq -0x1(%r13), %rax
cmpq %rax, %r15
ja 0x7c24
leaq 0x1(%r12), %rdi
movb $0x20, (%r12)
movq %r14, %rsi
movq %r15, %rdx
callq 0x69d0
addq %r15, %r12
incq %r12
notq %r15
addq %r15, %r13
movq (%rbx), %r14
addq $0x8, %rbx
testq %r14, %r14
jne 0x7b2d
movb $0x0, (%r12)
leaq 0x10(%rsp), %rdi
callq 0x6240
testl %eax, %eax
je 0x7bf1
leaq 0xabea(%rip), %rdi      # 0x12776
callq 0x6950
jmp 0x7bf1
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x64e0
testl %eax, %eax
js 0x7c5b
callq 0x68d0
movl %eax, 0x16b60(%rip)     # 0x1e71c
testl %eax, %eax
je 0x7c70
cmpl $-0x1, %eax
je 0x7cdb
movl 0x8(%rsp), %edi
callq 0x6110
movl 0xc(%rsp), %eax
movl %eax, 0x16b38(%rip)     # 0x1e718
leaq 0x2b1(%rip), %rsi       # 0x7e98
movl $0x11, %edi
callq 0x6070
movl 0x16b21(%rip), %eax     # 0x1e718
addq $0x1010, %rsp           # imm = 0x1010
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movl $0x1, 0x1593e(%rip)     # 0x1d550
jmp 0x7aab
movq %r12, %rdi
callq 0x6c67
jmp 0x7aab
leaq 0xab2b(%rip), %rdi      # 0x12756
xorl %eax, %eax
callq 0x714f
callq 0x6620
movl (%rax), %edi
callq 0x64b0
leaq 0xa9d4(%rip), %rdi      # 0x12619
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x714f
leaq 0xaae2(%rip), %rdi      # 0x1273b
jmp 0x7c2b
callq 0x6620
movl (%rax), %edi
callq 0x64b0
leaq 0xa9c6(%rip), %rdi      # 0x12634
jmp 0x7cee
movl 0x158da(%rip), %edi     # 0x1d550
callq 0x6110
movl 0xc(%rsp), %edi
callq 0x6110
callq 0x60e0
movl 0x8(%rsp), %edi
xorl %esi, %esi
callq 0x6080
movl 0x8(%rsp), %edi
movl $0x1, %esi
callq 0x6080
movl 0x8(%rsp), %edi
movl $0x2, %esi
callq 0x6080
movl 0x8(%rsp), %edi
movl $0x540e, %esi           # imm = 0x540E
xorl %edx, %edx
xorl %eax, %eax
callq 0x6120
testl %eax, %eax
jns 0x7cf8
callq 0x6620
movl (%rax), %edi
callq 0x64b0
leaq 0xa97f(%rip), %rdi      # 0x12658
jmp 0x7cee
callq 0x6620
movl (%rax), %edi
callq 0x64b0
leaq 0xa95a(%rip), %rdi      # 0x12648
movq %rax, %rsi
xorl %eax, %eax
callq 0x714f
movl 0x8(%rsp), %edi
cmpl $0x3, %edi
jl 0x7d06
callq 0x6110
callq 0x6620
movq %rax, %r12
movl $0x0, (%rax)
callq 0x67a0
movl %eax, %edi
callq 0x6320
testq %rax, %rax
jne 0x7d39
movl (%r12), %edi
testl %edi, %edi
jne 0x7d97
leaq 0xaa63(%rip), %rdi      # 0x12797
jmp 0x7c2b
movq %rax, %r15
leaq 0xaa62(%rip), %rdi      # 0x127a5
callq 0x65e0
movq %rax, %r12
testq %rax, %rax
jne 0x7d5e
movq 0x28(%r15), %rax
cmpb $0x0, (%rax)
cmovneq %rax, %r14
movq %r14, %r12
testq %rbx, %rbx
jne 0x7da8
leaq 0x18d3e(%rip), %rax     # 0x20aa8
leaq 0x18d2f(%rip), %rcx     # 0x20aa0
movq (%rcx), %r14
testq %r14, %r14
cmoveq %r12, %r14
movq (%rax), %rax
testq %rax, %rax
cmovneq %rax, %r14
leaq 0x10(%rsp), %rbx
movq %r14, (%rbx)
movq $0x0, 0x8(%rbx)
jmp 0x7dab
callq 0x64b0
leaq 0xa9e6(%rip), %rdi      # 0x12789
jmp 0x7cee
movq (%rbx), %r14
leaq 0xa9f9(%rip), %rdi      # 0x127ab
callq 0x6870
leaq 0xa9f5(%rip), %rdi      # 0x127b3
callq 0x6870
leaq 0xa9ef(%rip), %rdi      # 0x127b9
callq 0x6870
movq (%r15), %rsi
leaq 0xa9e8(%rip), %rdi      # 0x127c1
movl $0x1, %edx
callq 0x65d0
movq (%r15), %rsi
leaq 0xa9dc(%rip), %rdi      # 0x127c9
movl $0x1, %edx
callq 0x65d0
leaq 0xa9a7(%rip), %rdi      # 0x127a5
movq %r12, %rsi
movl $0x1, %edx
callq 0x65d0
movq 0x20(%r15), %rsi
leaq 0xa9b8(%rip), %rdi      # 0x127ce
movl $0x1, %edx
callq 0x65d0
leaq 0x15771(%rip), %rax     # 0x1d598
movq (%rax), %rsi
leaq 0xa9a2(%rip), %rdi      # 0x127d3
movl $0x1, %edx
callq 0x65d0
movl $0x11, %edi
xorl %esi, %esi
callq 0x6070
movl $0x1, %edi
xorl %esi, %esi
callq 0x6070
movl $0x2, %edi
xorl %esi, %esi
callq 0x6070
movl $0x3, %edi
xorl %esi, %esi
callq 0x6070
movl $0xf, %edi
xorl %esi, %esi
callq 0x6070
movl $0xe, %edi
xorl %esi, %esi
callq 0x6070
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6190
movl $0x1, %edi
callq 0x6260