// File: /workspace/github_c_makefile_star2_O0/odknt[P]st/st.c
// Function: ttynew
int
ttynew(const char *line, char *cmd, const 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", NULL) == -1)
			die("pledge\n");
#endif
		execsh(cmd, args);
		break;
	default:
#ifdef __OpenBSD__
		if (pledge("stdio rpath tty proc", NULL) == -1)
			die("pledge\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 0x7a48
movl 0x186fb(%rip), %eax     # 0x200b4
orl $0x20, %eax
movl %eax, 0x186f2(%rip)     # 0x200b4
movq -0x20(%rbp), %rdi
leaq 0xcea8(%rip), %rsi      # 0x14875
callq 0x6770
cmpl $0x0, %eax
jne 0x79e1
movl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x79f9
movq -0x20(%rbp), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x6a50
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, 0x11b7e(%rip)     # 0x19580
cmpl $0x0, 0x11b77(%rip)     # 0x19580
jge 0x7a46
movq 0x115de(%rip), %rax     # 0x18ff0
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
callq 0x66e0
movl (%rax), %edi
callq 0x6570
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq %rax, %rcx
leaq 0xce38(%rip), %rsi      # 0x14877
movb $0x0, %al
callq 0x69d0
jmp 0x7a48
cmpq $0x0, -0x10(%rbp)
je 0x7ab7
movq -0x10(%rbp), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x6a50
movl %eax, 0x1867f(%rip)     # 0x200e4
cmpl $0x0, %eax
jge 0x7a93
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
callq 0x66e0
movl (%rax), %edi
callq 0x6570
movq -0x50(%rbp), %rsi
movq %rax, %rdx
leaq 0xce00(%rip), %rdi      # 0x1488c
movb $0x0, %al
callq 0x6c20
movl 0x1864b(%rip), %edi     # 0x200e4
xorl %esi, %esi
callq 0x6070
movq -0x28(%rbp), %rdi
callq 0x7c00
movl 0x18635(%rip), %eax     # 0x200e4
movl %eax, -0x4(%rbp)
jmp 0x7bef
leaq -0x2c(%rbp), %rdi
leaq -0x30(%rbp), %rsi
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x65a0
cmpl $0x0, %eax
jge 0x7af1
callq 0x66e0
movl (%rax), %edi
callq 0x6570
movq %rax, %rsi
leaq 0xcdbd(%rip), %rdi      # 0x148a7
movb $0x0, %al
callq 0x6c20
callq 0x6920
movl %eax, 0x185ec(%rip)     # 0x200e8
movl %eax, %ecx
movl %ecx, -0x54(%rbp)
subl $-0x1, %eax
je 0x7b14
jmp 0x7b08
movl -0x54(%rbp), %eax
testl %eax, %eax
je 0x7b36
jmp 0x7bc4
callq 0x66e0
movl (%rax), %edi
callq 0x6570
movq %rax, %rsi
leaq 0xcd91(%rip), %rdi      # 0x148bb
movb $0x0, %al
callq 0x6c20
jmp 0x7be6
movl 0x11a44(%rip), %edi     # 0x19580
callq 0x60f0
movl -0x2c(%rbp), %edi
callq 0x60f0
callq 0x60d0
movl -0x30(%rbp), %edi
xorl %esi, %esi
callq 0x6070
movl -0x30(%rbp), %edi
movl $0x1, %esi
callq 0x6070
movl -0x30(%rbp), %edi
movl $0x2, %esi
callq 0x6070
movl -0x30(%rbp), %edi
movl $0x540e, %esi           # imm = 0x540E
xorl %eax, %eax
movl %eax, %edx
movb $0x0, %al
callq 0x6110
cmpl $0x0, %eax
jge 0x7ba7
callq 0x66e0
movl (%rax), %edi
callq 0x6570
movq %rax, %rsi
leaq 0xcd2c(%rip), %rdi      # 0x148cc
movb $0x0, %al
callq 0x6c20
cmpl $0x2, -0x30(%rbp)
jle 0x7bb5
movl -0x30(%rbp), %edi
callq 0x60f0
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x7db0
jmp 0x7be6
movl -0x30(%rbp), %edi
callq 0x60f0
movl -0x2c(%rbp), %eax
movl %eax, 0x1850f(%rip)     # 0x200e4
movl $0x11, %edi
leaq 0x46f(%rip), %rsi       # 0x8050
callq 0x6060
movl 0x184f8(%rip), %eax     # 0x200e4
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)