// File: /workspace/github_c_makefile_star2_O1/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 0x79fc
movq %rdx, %r12
orb $0x20, 0x14964(%rip)    # 0x1c324
leaq 0x8bfe(%rip), %rsi      # 0x105c5
movq %rdx, %rdi
callq 0x6770
testl %eax, %eax
je 0x79e9
movq %r12, %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x6a60
jmp 0x79ee
movl $0x1, %eax
movl %eax, 0xfb8c(%rip)      # 0x17580
testl %eax, %eax
js 0x7b5d
testq %r15, %r15
je 0x7ae8
movq %r15, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x6a60
movl %eax, 0x1493a(%rip)     # 0x1c354
testl %eax, %eax
js 0x7b78
movl 0x1492c(%rip), %edi     # 0x1c354
xorl %esi, %esi
callq 0x6070
leaq 0xfb5a(%rip), %rax      # 0x17590
movq (%rax), %r14
movq %r14, %rdi
callq 0x6410
cmpq $0x1000, %rax           # imm = 0x1000
jae 0x7b98
movq %rax, %r15
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x6a40
leaq (%rsp,%r15), %r12
addq $0x10, %r12
testq %rbx, %rbx
je 0x7ac7
movq (%rbx), %r14
testq %r14, %r14
je 0x7ac7
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 0x7b6a
leaq 0x1(%r12), %rdi
movb $0x20, (%r12)
movq %r14, %rsi
movq %r15, %rdx
callq 0x6a40
addq %r15, %r12
incq %r12
notq %r15
addq %r15, %r13
movq (%rbx), %r14
addq $0x8, %rbx
testq %r14, %r14
jne 0x7a82
movb $0x0, (%r12)
leaq 0x10(%rsp), %rdi
callq 0x6250
testl %eax, %eax
je 0x7b46
leaq 0x8c61(%rip), %rdi      # 0x10742
callq 0x69b0
jmp 0x7b46
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x65a0
testl %eax, %eax
js 0x7ba1
callq 0x6920
movl %eax, 0x14847(%rip)     # 0x1c358
testl %eax, %eax
je 0x7bb6
cmpl $-0x1, %eax
je 0x7c21
movl 0x8(%rsp), %edi
callq 0x60f0
movl 0xc(%rsp), %eax
movl %eax, 0x1481f(%rip)     # 0x1c354
leaq 0x2b4(%rip), %rsi       # 0x7df0
movl $0x11, %edi
callq 0x6060
movl 0x14808(%rip), %eax     # 0x1c354
addq $0x1010, %rsp           # imm = 0x1010
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %r12, %rdi
callq 0x6bcd
jmp 0x79fc
leaq 0x8bb1(%rip), %rdi      # 0x10722
xorl %eax, %eax
callq 0x70c6
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x8a51(%rip), %rdi      # 0x105dc
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x70c6
leaq 0x8b68(%rip), %rdi      # 0x10707
jmp 0x7b71
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x8a43(%rip), %rdi      # 0x105f7
jmp 0x7c34
movl 0xf9c4(%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
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 0x7c3e
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x89fd(%rip), %rdi      # 0x1061c
jmp 0x7c34
callq 0x66e0
movl (%rax), %edi
callq 0x6570
leaq 0x89d7(%rip), %rdi      # 0x1060b
movq %rax, %rsi
xorl %eax, %eax
callq 0x70c6
movl 0x8(%rsp), %edi
cmpl $0x3, %edi
jl 0x7c4c
callq 0x60f0
callq 0x66e0
movq %rax, %r12
movl $0x0, (%rax)
callq 0x6810
movl %eax, %edi
callq 0x6350
testq %rax, %rax
jne 0x7c7f
movl (%r12), %edi
testl %edi, %edi
jne 0x7cd4
leaq 0x8ae9(%rip), %rdi      # 0x10763
jmp 0x7b71
movq %rax, %r15
leaq 0x8ae8(%rip), %rdi      # 0x10771
callq 0x6690
movq %rax, %r12
testq %rax, %rax
jne 0x7ca4
movq 0x28(%r15), %rax
cmpb $0x0, (%rax)
cmovneq %rax, %r14
movq %r14, %r12
testq %rbx, %rbx
jne 0x7ce5
leaq 0x16a00(%rip), %rax     # 0x1e6b0
movq (%rax), %rcx
leaq 0x169ee(%rip), %rax     # 0x1e6a8
movq (%rax), %rax
testq %rax, %rax
cmoveq %r12, %rax
testq %rcx, %rcx
movq %rcx, %r14
cmoveq %rax, %r14
cmoveq %rcx, %rax
jmp 0x7cea
callq 0x6570
leaq 0x8a75(%rip), %rdi      # 0x10755
jmp 0x7c34
movq (%rbx), %r14
xorl %eax, %eax
testq %rbx, %rbx
jne 0x7d03
leaq 0x10(%rsp), %rbx
movq %r14, (%rbx)
movq %rax, 0x8(%rbx)
movq $0x0, 0x10(%rbx)
leaq 0x8a6d(%rip), %rdi      # 0x10777
callq 0x68c0
leaq 0x8a69(%rip), %rdi      # 0x1077f
callq 0x68c0
leaq 0x8a63(%rip), %rdi      # 0x10785
callq 0x68c0
movq (%r15), %rsi
leaq 0x8a5c(%rip), %rdi      # 0x1078d
movl $0x1, %edx
callq 0x6680
movq (%r15), %rsi
leaq 0x8a50(%rip), %rdi      # 0x10795
movl $0x1, %edx
callq 0x6680
leaq 0x8a1b(%rip), %rdi      # 0x10771
movq %r12, %rsi
movl $0x1, %edx
callq 0x6680
movq 0x20(%r15), %rsi
leaq 0x8a2c(%rip), %rdi      # 0x1079a
movl $0x1, %edx
callq 0x6680
leaq 0xf831(%rip), %rax      # 0x175b0
movq (%rax), %rsi
leaq 0x8a16(%rip), %rdi      # 0x1079f
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