// File: /workspace/github_c_makefile_star2_O3/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 %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 0x7ad3
movq %rdx, %r12
orb $0x20, 0x15874(%rip)    # 0x1d314
cmpb $0x2d, (%rdx)
jne 0x7ab1
cmpb $0x0, 0x1(%r12)
je 0x7c30
movq %r12, %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x6a40
movl %eax, 0x10aa5(%rip)     # 0x18570
testl %eax, %eax
js 0x7c3f
testq %r15, %r15
je 0x7bbb
movq %r15, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x6a40
movl %eax, 0x15853(%rip)     # 0x1d344
testl %eax, %eax
js 0x7c5a
movl %eax, %edi
xorl %esi, %esi
callq 0x6070
leaq 0x10a77(%rip), %rax     # 0x18580
movq (%rax), %r14
movq %r14, %rdi
callq 0x6410
cmpq $0x1000, %rax           # imm = 0x1000
jae 0x7c7a
movq %rax, %r15
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x6a20
leaq (%rsp,%r15), %r12
addq $0x10, %r12
testq %rbx, %rbx
je 0x7b9a
movq (%rbx), %r14
testq %r14, %r14
je 0x7b9a
movl $0x1000, %r13d          # imm = 0x1000
subq %r15, %r13
addq $0x8, %rbx
movq %r14, %rdi
callq 0x6410
movq %rax, %r15
leaq -0x1(%r13), %rax
cmpq %rax, %r15
ja 0x7c4c
leaq 0x1(%r12), %rdi
movb $0x20, (%r12)
movq %r14, %rsi
movq %r15, %rdx
callq 0x6a20
addq %r15, %r12
incq %r12
notq %r15
addq %r15, %r13
movq (%rbx), %r14
addq $0x8, %rbx
testq %r14, %r14
jne 0x7b55
movb $0x0, (%r12)
leaq 0x10(%rsp), %rdi
callq 0x6250
testl %eax, %eax
je 0x7c19
leaq 0x9bbc(%rip), %rdi      # 0x11770
callq 0x6990
jmp 0x7c19
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x65a0
testl %eax, %eax
js 0x7c83
callq 0x6900
movl %eax, 0x15764(%rip)     # 0x1d348
testl %eax, %eax
je 0x7c98
cmpl $-0x1, %eax
je 0x7d03
movl 0x8(%rsp), %edi
callq 0x60f0
movl 0xc(%rsp), %eax
movl %eax, 0x1573c(%rip)     # 0x1d344
leaq 0x2bc(%rip), %rsi       # 0x7ecb
movl $0x11, %edi
callq 0x6060
movl 0x15725(%rip), %eax     # 0x1d344
addq $0x1010, %rsp           # imm = 0x1010
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movl $0x1, 0x10936(%rip)     # 0x18570
jmp 0x7ad3
movq %r12, %rdi
callq 0x6bad
jmp 0x7ad3
leaq 0x9afd(%rip), %rdi      # 0x11750
xorl %eax, %eax
callq 0x70a6
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x999d(%rip), %rdi      # 0x1160a
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x70a6
leaq 0x9ab4(%rip), %rdi      # 0x11735
jmp 0x7c53
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x998f(%rip), %rdi      # 0x11625
jmp 0x7d16
movl 0x108d2(%rip), %edi     # 0x18570
callq 0x60f0
movl 0xc(%rsp), %edi
callq 0x60f0
callq 0x60d0
movl 0x8(%rsp), %edi
xorl %esi, %esi
callq 0x6070
movl 0x8(%rsp), %edi
movl $0x1, %esi
callq 0x6070
movl 0x8(%rsp), %edi
movl $0x2, %esi
callq 0x6070
movl 0x8(%rsp), %edi
movl $0x540e, %esi           # imm = 0x540E
xorl %edx, %edx
xorl %eax, %eax
callq 0x6110
testl %eax, %eax
jns 0x7d20
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x9949(%rip), %rdi      # 0x1164a
jmp 0x7d16
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x9923(%rip), %rdi      # 0x11639
movq %rax, %rsi
xorl %eax, %eax
callq 0x70a6
movl 0x8(%rsp), %edi
cmpl $0x3, %edi
jl 0x7d2e
callq 0x60f0
callq 0x66e0
movq %rax, %r12
movl $0x0, (%rax)
callq 0x6800
movl %eax, %edi
callq 0x6350
testq %rax, %rax
jne 0x7d61
movl (%r12), %edi
testl %edi, %edi
jne 0x7dca
leaq 0x9a35(%rip), %rdi      # 0x11791
jmp 0x7c53
movq %rax, %r15
leaq 0x9a34(%rip), %rdi      # 0x1179f
callq 0x6690
movq %rax, %r12
testq %rax, %rax
jne 0x7d86
movq 0x28(%r15), %rax
cmpb $0x0, (%rax)
cmovneq %rax, %r14
movq %r14, %r12
testq %rbx, %rbx
jne 0x7ddb
leaq 0x1790e(%rip), %rax     # 0x1f6a0
movq (%rax), %rax
leaq 0x178fc(%rip), %rcx     # 0x1f698
movq (%rcx), %rcx
testq %rcx, %rcx
cmoveq %r12, %rcx
testq %rax, %rax
movq %rax, %r14
cmoveq %rcx, %r14
cmoveq %rax, %rcx
leaq 0x10(%rsp), %rbx
movq %r14, (%rbx)
movq %rcx, 0x8(%rbx)
movq $0x0, 0x10(%rbx)
jmp 0x7dde
callq 0x6570
leaq 0x99ad(%rip), %rdi      # 0x11783
jmp 0x7d16
movq (%rbx), %r14
leaq 0x99c0(%rip), %rdi      # 0x117a5
callq 0x68a0
leaq 0x99bc(%rip), %rdi      # 0x117ad
callq 0x68a0
leaq 0x99b6(%rip), %rdi      # 0x117b3
callq 0x68a0
movq (%r15), %rsi
leaq 0x99af(%rip), %rdi      # 0x117bb
movl $0x1, %edx
callq 0x6680
movq (%r15), %rsi
leaq 0x99a3(%rip), %rdi      # 0x117c3
movl $0x1, %edx
callq 0x6680
leaq 0x996e(%rip), %rdi      # 0x1179f
movq %r12, %rsi
movl $0x1, %edx
callq 0x6680
movq 0x20(%r15), %rsi
leaq 0x997f(%rip), %rdi      # 0x117c8
movl $0x1, %edx
callq 0x6680
leaq 0x10746(%rip), %rax     # 0x185a0
movq (%rax), %rsi
leaq 0x9969(%rip), %rdi      # 0x117cd
movl $0x1, %edx
callq 0x6680
movl $0x11, %edi
xorl %esi, %esi
callq 0x6060
movl $0x1, %edi
xorl %esi, %esi
callq 0x6060
movl $0x2, %edi
xorl %esi, %esi
callq 0x6060
movl $0x3, %edi
xorl %esi, %esi
callq 0x6060
movl $0xf, %edi
xorl %esi, %esi
callq 0x6060
movl $0xe, %edi
xorl %esi, %esi
callq 0x6060
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6190
movl $0x1, %edi
callq 0x6280