#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ TIOCGPTN;
int /*<<< orphan*/ TIOCSPTLCK;
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, int);
int /*<<< orphan*/ setpgrp();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, size_t);
int main(int argc, char **argv, char **envp) {
  int fd, fd2, ptn, bit;
  char ptname[100];
  char readbuf[1025], writebuf[1025];
  size_t readc, writec;
  pid_t readpid, writepid, shellpid;
  int exitstatus;
  fd = open("/dev/ptmx", O_RDWR);
  ioctl(fd, TIOCGPTN, &ptn);
  bit = 0;
  ioctl(fd, TIOCSPTLCK, &bit);
  snprintf(ptname, 99, "/dev/pts/%d", ptn);
  chmod(ptname, 0620);
  fd2 = open(ptname, O_RDWR);
  if (!(readpid = fork())) {
    while (1) {
      readc = read(0, readbuf, 1024);
      write(fd, readbuf, readc);
      if (!readc)
        close(fd);
    }
  }
  if (!(writepid = fork())) {
    while (1) {
      writec = read(fd, writebuf, 1024);
      write(1, writebuf, writec);
    }
  }
  if (!(shellpid = fork())) {
    dup2(fd2, 0);
    dup2(fd2, 1);
    dup2(fd2, 2);
    setpgrp();
    setsid();
    execvp(argv[1], argv + 1);
  }
  waitpid(shellpid, &exitstatus, 0);
  kill(readpid, SIGTERM);
  kill(writepid, SIGTERM);
  return exitstatus;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8d0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
call   1060 <ioctl@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
lea    -0x90(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    $0x63,%esi
lea    0xdae(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x90(%rbp),%rdi
mov    $0x190,%esi
call   10b0 <chmod@plt>
lea    -0x90(%rbp),%rdi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x8c4(%rbp)
cmp    $0x0,%eax
jne    12f0 <main+0x110>
jmp    12a5 <main+0xc5>
lea    -0x4a0(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %rax,-0x8b8(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x4a0(%rbp),%rsi
mov    -0x8b8(%rbp),%rdx
call   1030 <write@plt>
cmpq   $0x0,-0x8b8(%rbp)
jne    12eb <main+0x10b>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
jmp    12a5 <main+0xc5>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x8c8(%rbp)
cmp    $0x0,%eax
jne    1343 <main+0x163>
jmp    130b <main+0x12b>
mov    -0x1c(%rbp),%edi
lea    -0x8b0(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %rax,-0x8c0(%rbp)
lea    -0x8b0(%rbp),%rsi
mov    -0x8c0(%rbp),%rdx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    130b <main+0x12b>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x8cc(%rbp)
cmp    $0x0,%eax
jne    13a0 <main+0x1c0>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x20(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    $0x0,%al
call   10f0 <setpgrp@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10e0 <execvp@plt>
mov    -0x8cc(%rbp),%edi
lea    -0x8d0(%rbp),%rsi
xor    %edx,%edx
call   10c0 <waitpid@plt>
mov    -0x8c4(%rbp),%edi
mov    0x2c17(%rip),%rax
mov    (%rax),%esi
call   10a0 <kill@plt>
mov    -0x8c8(%rbp),%edi
mov    0x2c03(%rip),%rax
mov    (%rax),%esi
call   10a0 <kill@plt>
mov    -0x8d0(%rbp),%eax
add    $0x8d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x488,%rsp
mov    %rsi,%r14
mov    0x2ded(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xdfb(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,%r13d
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %r13d,%edi
call   1060 <ioctl@plt>
movl   $0x0,0x8(%rsp)
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r13d,%edi
call   1060 <ioctl@plt>
mov    0xc(%rsp),%ecx
lea    0xdbe(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x63,%esi
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    $0x190,%esi
call   10b0 <chmod@plt>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   10d0 <open@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     1331 <main+0x151>
mov    %eax,%ebx
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     136f <main+0x18f>
mov    %eax,%r15d
xor    %eax,%eax
call   1100 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    12eb <main+0x10b>
mov    %r12d,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %r12d,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %r12d,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
xor    %eax,%eax
call   10f0 <setpgrp@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
mov    0x8(%r14),%rdi
add    $0x8,%r14
mov    %r14,%rsi
call   10e0 <execvp@plt>
lea    0x80(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10c0 <waitpid@plt>
mov    0x2cd5(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %ebx,%edi
call   10a0 <kill@plt>
mov    0x0(%rbp),%esi
mov    %r15d,%edi
call   10a0 <kill@plt>
mov    0x80(%rsp),%eax
add    $0x488,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x80(%rsp),%rbx
nopl   0x0(%rax)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %rax,%rbp
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
test   %rbp,%rbp
jne    1340 <main+0x160>
mov    %r13d,%edi
call   1070 <close@plt>
jmp    1340 <main+0x160>
lea    0x80(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
jmp    1380 <main+0x1a0>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@TIOCGPTN = global i32 0
@TIOCSPTLCK = global i32 0
@.str.1 = [12 x i8] c"/dev/pts/%d\00"
@SIGTERM = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [100 x i8]
%13 = alloca [1025 x i8]
%14 = alloca [1025 x i8]
%15 = alloca i64
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = load i32, i32* @TIOCGPTN
%25 = call i32 @ioctl(i32 %23, i32 %24, i32* %10)
store i32 0, i32* %11
%26 = load i32, i32* %8
%27 = load i32, i32* @TIOCSPTLCK
%28 = call i32 @ioctl(i32 %26, i32 %27, i32* %11)
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%30 = load i32, i32* %10
%31 = call i32 @snprintf(i8* %29, i32 99, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%33 = call i32 @chmod(i8* %32, i32 400)
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%35 = load i32, i32* @O_RDWR
%36 = call i32 @open(i8* %34, i32 %35)
store i32 %36, i32* %9
%37 = call i32 (...) @fork()
store i32 %37, i32* %17
%38 = icmp ne i32 %37, 0
br i1 %38, label %53, label %39
39: ; preds = %3
br label %40
40: ; preds = %39, %52
%41 = getelementptr inbounds [1025 x i8], [1025 x i8]* %13, i64 0, i64 0
%42 = call i64 @read(i32 0, i8* %41, i32 1024)
store i64 %42, i64* %15
%43 = load i32, i32* %8
%44 = getelementptr inbounds [1025 x i8], [1025 x i8]* %13, i64 0, i64 0
%45 = load i64, i64* %15
%46 = call i32 @write(i32 %43, i8* %44, i64 %45)
%47 = load i64, i64* %15
%48 = icmp ne i64 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %40
%50 = load i32, i32* %8
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49, %40
br label %40
53: ; preds = %3
%54 = call i32 (...) @fork()
store i32 %54, i32* %18
%55 = icmp ne i32 %54, 0
br i1 %55, label %64, label %56
56: ; preds = %53
br label %57
57: ; preds = %56, %57
%58 = load i32, i32* %8
%59 = getelementptr inbounds [1025 x i8], [1025 x i8]* %14, i64 0, i64 0
%60 = call i64 @read(i32 %58, i8* %59, i32 1024)
store i64 %60, i64* %16
%61 = getelementptr inbounds [1025 x i8], [1025 x i8]* %14, i64 0, i64 0
%62 = load i64, i64* %16
%63 = call i32 @write(i32 1, i8* %61, i64 %62)
br label %57
64: ; preds = %53
%65 = call i32 (...) @fork()
store i32 %65, i32* %19
%66 = icmp ne i32 %65, 0
br i1 %66, label %82, label %67
67: ; preds = %64
%68 = load i32, i32* %9
%69 = call i32 @dup2(i32 %68, i32 0)
%70 = load i32, i32* %9
%71 = call i32 @dup2(i32 %70, i32 1)
%72 = load i32, i32* %9
%73 = call i32 @dup2(i32 %72, i32 2)
%74 = call i32 (...) @setpgrp()
%75 = call i32 (...) @setsid()
%76 = load i8**, i8*** %6
%77 = getelementptr inbounds i8*, i8** %76, i64 1
%78 = load i8*, i8** %77
%79 = load i8**, i8*** %6
%80 = getelementptr inbounds i8*, i8** %79, i64 1
%81 = call i32 @execvp(i8* %78, i8** %80)
br label %82
82: ; preds = %67, %64
%83 = load i32, i32* %19
%84 = call i32 @waitpid(i32 %83, i32* %20, i32 0)
%85 = load i32, i32* %17
%86 = load i32, i32* @SIGTERM
%87 = call i32 @kill(i32 %85, i32 %86)
%88 = load i32, i32* %18
%89 = load i32, i32* @SIGTERM
%90 = call i32 @kill(i32 %88, i32 %89)
%91 = load i32, i32* %20
ret i32 %91
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@TIOCGPTN = global i32 0
@TIOCSPTLCK = global i32 0
@.str.1 = [12 x i8] c"/dev/pts/%d\00"
@SIGTERM = global i32 0
define i32 @main(i32 %0, i8** %1, i8** nocapture readnone %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca [1025 x i8]
%8 = alloca [1025 x i8]
%9 = alloca i32
%10 = bitcast i32* %4 to i8*
%11 = bitcast i32* %5 to i8*
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%13 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%14 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%15 = bitcast i32* %9 to i8*
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @TIOCGPTN
%19 = call i32 @ioctl(i32 %17, i32 %18, i32* %4)
store i32 0, i32* %5
%20 = load i32, i32* @TIOCSPTLCK
%21 = call i32 @ioctl(i32 %17, i32 %20, i32* %5)
%22 = load i32, i32* %4
%23 = call i32 @snprintf(i8* %12, i32 99, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = call i32 @chmod(i8* %12, i32 400)
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* %12, i32 %25)
%27 = call i32 (...) @fork()
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %3, %35
%30 = call i64 @read(i32 0, i8* %13, i32 1024)
%31 = call i32 @write(i32 %17, i8* %13, i64 %30)
%32 = icmp eq i64 %30, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @close(i32 %17)
br label %35
35: ; preds = %33, %29
br label %29
36: ; preds = %3
%37 = call i32 (...) @fork()
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36, %39
%40 = call i64 @read(i32 %17, i8* %14, i32 1024)
%41 = call i32 @write(i32 1, i8* %14, i64 %40)
br label %39
42: ; preds = %36
%43 = call i32 (...) @fork()
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %54
45: ; preds = %42
%46 = call i32 @dup2(i32 %26, i32 0)
%47 = call i32 @dup2(i32 %26, i32 1)
%48 = call i32 @dup2(i32 %26, i32 2)
%49 = call i32 (...) @setpgrp()
%50 = call i32 (...) @setsid()
%51 = getelementptr inbounds i8*, i8** %1, i64 1
%52 = load i8*, i8** %51
%53 = call i32 @execvp(i8* %52, i8** %51)
br label %54
54: ; preds = %45, %42
%55 = call i32 @waitpid(i32 %43, i32* %9, i32 0)
%56 = load i32, i32* @SIGTERM
%57 = call i32 @kill(i32 %27, i32 %56)
%58 = load i32, i32* @SIGTERM
%59 = call i32 @kill(i32 %37, i32 %58)
%60 = load i32, i32* %9
ret i32 %60
}