// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x7a78
movl 0x1aacb(%rip), %eax     # 0x224b4
orl $0x20, %eax
movl %eax, 0x1aac2(%rip)     # 0x224b4
movq -0x20(%rbp), %rdi
leaq 0xee27(%rip), %rsi      # 0x16824
callq 0x66f0
cmpl $0x0, %eax
jne 0x7a11
movl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x7a29
movq -0x20(%rbp), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x69f0
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, 0x17b1e(%rip)     # 0x1f550
cmpl $0x0, 0x17b17(%rip)     # 0x1f550
jge 0x7a76
movq 0x175ae(%rip), %rax     # 0x1eff0
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
callq 0x6630
movl (%rax), %edi
callq 0x64b0
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq %rax, %rcx
leaq 0xedb7(%rip), %rsi      # 0x16826
movb $0x0, %al
callq 0x6970
jmp 0x7a78
cmpq $0x0, -0x10(%rbp)
je 0x7ae7
movq -0x10(%rbp), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x69f0
movl %eax, 0x1aa43(%rip)     # 0x224d8
cmpl $0x0, %eax
jge 0x7ac3
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
callq 0x6630
movl (%rax), %edi
callq 0x64b0
movq -0x50(%rbp), %rsi
movq %rax, %rdx
leaq 0xed7f(%rip), %rdi      # 0x1683b
movb $0x0, %al
callq 0x6c50
movl 0x1aa0f(%rip), %edi     # 0x224d8
xorl %esi, %esi
callq 0x6080
movq -0x28(%rbp), %rdi
callq 0x7c30
movl 0x1a9f9(%rip), %eax     # 0x224d8
movl %eax, -0x4(%rbp)
jmp 0x7c1f
leaq -0x2c(%rbp), %rdi
leaq -0x30(%rbp), %rsi
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x64f0
cmpl $0x0, %eax
jge 0x7b21
callq 0x6630
movl (%rax), %edi
callq 0x64b0
movq %rax, %rsi
leaq 0xed3c(%rip), %rdi      # 0x16856
movb $0x0, %al
callq 0x6c50
callq 0x68e0
movl %eax, 0x1a9b0(%rip)     # 0x224dc
movl %eax, %ecx
movl %ecx, -0x54(%rbp)
subl $-0x1, %eax
je 0x7b44
jmp 0x7b38
movl -0x54(%rbp), %eax
testl %eax, %eax
je 0x7b66
jmp 0x7bf4
callq 0x6630
movl (%rax), %edi
callq 0x64b0
movq %rax, %rsi
leaq 0xed10(%rip), %rdi      # 0x1686a
movb $0x0, %al
callq 0x6c50
jmp 0x7c16
movl 0x179e4(%rip), %edi     # 0x1f550
callq 0x6110
movl -0x2c(%rbp), %edi
callq 0x6110
callq 0x60e0
movl -0x30(%rbp), %edi
xorl %esi, %esi
callq 0x6080
movl -0x30(%rbp), %edi
movl $0x1, %esi
callq 0x6080
movl -0x30(%rbp), %edi
movl $0x2, %esi
callq 0x6080
movl -0x30(%rbp), %edi
movl $0x540e, %esi           # imm = 0x540E
xorl %eax, %eax
movl %eax, %edx
movb $0x0, %al
callq 0x6120
cmpl $0x0, %eax
jge 0x7bd7
callq 0x6630
movl (%rax), %edi
callq 0x64b0
movq %rax, %rsi
leaq 0xecaa(%rip), %rdi      # 0x1687a
movb $0x0, %al
callq 0x6c50
cmpl $0x2, -0x30(%rbp)
jle 0x7be5
movl -0x30(%rbp), %edi
callq 0x6110
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x7de0
jmp 0x7c16
movl -0x30(%rbp), %edi
callq 0x6110
movl -0x2c(%rbp), %eax
movl %eax, 0x1a8d3(%rip)     # 0x224d8
movl $0x11, %edi
leaq 0x45f(%rip), %rsi       # 0x8070
callq 0x6070
movl 0x1a8bc(%rip), %eax     # 0x224d8
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)