// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1018, %rsp           # imm = 0x1018
movq %rcx, %rbx
movq %rsi, %r14
movq %rdi, %r15
testq %rdx, %rdx
je 0x7454
movq %rdx, %r12
orb $0x20, 0x14f01(%rip)    # 0x1c324
cmpb $0x2d, (%rdx)
jne 0x7434
cmpb $0x0, 0x1(%r12)
je 0x75a4
pushq $0x41
popq %rsi
movq %r12, %rdi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x6a50
movl %eax, 0x10134(%rip)     # 0x17580
testl %eax, %eax
js 0x75b3
testq %r15, %r15
je 0x7530
pushq $0x2
popq %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x6a50
movl %eax, 0x14ee4(%rip)     # 0x1c354
testl %eax, %eax
js 0x75f3
movl %eax, %edi
xorl %esi, %esi
callq 0x6070
leaq 0x10108(%rip), %rax     # 0x17590
movq (%rax), %r15
movq %r15, %rdi
callq 0x6410
cmpq $0x1000, %rax           # imm = 0x1000
jae 0x7613
movq %rax, %r14
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x6a30
leaq (%rsp,%r14), %r12
addq $0x10, %r12
movl $0x1000, %r13d          # imm = 0x1000
subq %r14, %r13
testq %rbx, %rbx
je 0x750f
movq (%rbx), %r14
testq %r14, %r14
je 0x750f
movq %r14, %rdi
callq 0x6410
movq %rax, %r15
leaq -0x1(%r13), %rax
cmpq %rax, %r15
ja 0x75e5
leaq 0x1(%r12), %rdi
movb $0x20, (%r12)
movq %r14, %rsi
movq %r15, %rdx
callq 0x6a30
addq %r15, %r12
incq %r12
notq %r15
addq %r15, %r13
addq $0x8, %rbx
jmp 0x74c3
movb $0x0, (%r12)
leaq 0x10(%rsp), %rdi
callq 0x6250
testl %eax, %eax
je 0x758c
leaq 0x9217(%rip), %rdi      # 0x10740
callq 0x69a0
jmp 0x758c
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x65a0
testl %eax, %eax
js 0x761c
callq 0x6910
movl %eax, 0x14dff(%rip)     # 0x1c358
testl %eax, %eax
je 0x7631
cmpl $-0x1, %eax
je 0x7698
movl 0x8(%rsp), %edi
callq 0x60f0
movl 0xc(%rsp), %eax
movl %eax, 0x14dd7(%rip)     # 0x1c354
leaq 0x2ba(%rip), %rsi       # 0x783e
pushq $0x11
popq %rdi
callq 0x6060
movl 0x14dc2(%rip), %eax     # 0x1c354
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, 0xffd2(%rip)      # 0x17580
jmp 0x7454
movq 0xfa36(%rip), %rax      # 0x16ff0
movq (%rax), %r13
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x8ff5(%rip), %rsi      # 0x105c5
movq %r13, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x69d0
jmp 0x7454
leaq 0x9134(%rip), %rdi      # 0x10720
xorl %eax, %eax
callq 0x6bf6
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x8fd4(%rip), %rdi      # 0x105da
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6bf6
leaq 0x90eb(%rip), %rdi      # 0x10705
jmp 0x75ec
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x8fc6(%rip), %rdi      # 0x105f5
jmp 0x76ab
movl 0xff49(%rip), %edi      # 0x17580
callq 0x60f0
movl 0xc(%rsp), %edi
callq 0x60f0
callq 0x60d0
movl 0x8(%rsp), %edi
xorl %esi, %esi
callq 0x6070
movl 0x8(%rsp), %edi
pushq $0x1
popq %rsi
callq 0x6070
movl 0x8(%rsp), %edi
pushq $0x2
popq %rsi
callq 0x6070
movl 0x8(%rsp), %edi
movl $0x540e, %esi           # imm = 0x540E
xorl %edx, %edx
xorl %eax, %eax
callq 0x6110
testl %eax, %eax
jns 0x76b5
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x8f84(%rip), %rdi      # 0x1061a
jmp 0x76ab
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x8f5e(%rip), %rdi      # 0x10609
movq %rax, %rsi
xorl %eax, %eax
callq 0x6bf6
movl 0x8(%rsp), %edi
cmpl $0x3, %edi
jl 0x76c3
callq 0x60f0
callq 0x66e0
movq %rax, %r12
andl $0x0, (%rax)
callq 0x6800
movl %eax, %edi
callq 0x6350
testq %rax, %rax
jne 0x76f3
movl (%r12), %edi
testl %edi, %edi
jne 0x7759
leaq 0x9073(%rip), %rdi      # 0x10761
jmp 0x75ec
movq %rax, %r15
leaq 0x9072(%rip), %rdi      # 0x1076f
callq 0x6690
movq %rax, %r12
testq %rax, %rax
jne 0x7718
movq 0x28(%r15), %rax
cmpb $0x0, (%rax)
cmovneq %rax, %r14
movq %r14, %r12
testq %rbx, %rbx
jne 0x776a
leaq 0x16f8c(%rip), %rax     # 0x1e6b0
movq (%rax), %rax
leaq 0x16f7a(%rip), %rcx     # 0x1e6a8
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)
andq $0x0, 0x10(%rbx)
jmp 0x776d
callq 0x6570
leaq 0x8fee(%rip), %rdi      # 0x10753
jmp 0x76ab
movq (%rbx), %r14
leaq 0x9001(%rip), %rdi      # 0x10775
callq 0x68b0
leaq 0x8ffd(%rip), %rdi      # 0x1077d
callq 0x68b0
leaq 0x8ff7(%rip), %rdi      # 0x10783
callq 0x68b0
movq (%r15), %rsi
leaq 0x8ff0(%rip), %rdi      # 0x1078b
pushq $0x1
popq %rbp
movl %ebp, %edx
callq 0x6680
movq (%r15), %rsi
leaq 0x8fe4(%rip), %rdi      # 0x10793
movl %ebp, %edx
callq 0x6680
leaq 0x8fb2(%rip), %rdi      # 0x1076f
movq %r12, %rsi
movl %ebp, %edx
callq 0x6680
movq 0x20(%r15), %rsi
leaq 0x8fc6(%rip), %rdi      # 0x10798
movl %ebp, %edx
callq 0x6680
leaq 0xfdd0(%rip), %rax      # 0x175b0
movq (%rax), %rsi
leaq 0x8fb3(%rip), %rdi      # 0x1079d
movl %ebp, %edx
callq 0x6680
pushq $0x11
popq %rdi
xorl %esi, %esi
callq 0x6060
movl %ebp, %edi
xorl %esi, %esi
callq 0x6060
pushq $0x2
popq %rdi
xorl %esi, %esi
callq 0x6060
pushq $0x3
popq %rdi
xorl %esi, %esi
callq 0x6060
pushq $0xf
popq %rdi
xorl %esi, %esi
callq 0x6060
pushq $0xe
popq %rdi
xorl %esi, %esi
callq 0x6060
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6190
movl %ebp, %edi
callq 0x6280