#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ child(int, char **);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ fatal(char *);
scalar_t__ fork();
scalar_t__ grantpt(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ parent(char *, int, int);
scalar_t__ ptsname(int);
int /*<<< orphan*/ setgid(int);
int /*<<< orphan*/ setuid(int);
scalar_t__ strncmp(char *, char *, int);
scalar_t__ unlockpt(int);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  pid_t pid;
  int seg_fault_on_exit = 0;
  int parent_ptty;
  int child_ptty;
  char *child_devname = NULL;
  if (argc < 2) {
    usage();
  }
  if (strncmp(argv[1], "-d", 2) == 0) {
    seg_fault_on_exit = 1;
    argc--;
    argv++;
  }
  if (argc < 2) {
    usage();
  }
  parent_ptty = open("/dev/ptmx", O_RDWR);
  if (parent_ptty < 0) {
    fatal("Cannot create parent ptty\n");
  }
  if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
      ((child_devname = (char *)ptsname(parent_ptty)) == 0)) {
    fatal("Problem with /dev/ptmx\n");
  }
  pid = fork();
  if (pid < 0) {
    fatal("Failed to fork\n");
  } else if (pid == 0) {
    child_ptty = open(child_devname, O_RDWR);
    if (child_ptty < 0) {
      fatal("Problem with child ptty\n");
    }
    close(parent_ptty);
    dup2(child_ptty, 1);
    dup2(child_ptty, 2);
    close(child_ptty);
    child(argc - 1, &argv[1]);
  } else {
    setgid(1007);
    setuid(1007);
    parent(argv[1], seg_fault_on_exit, parent_ptty);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1216 <main+0x36>
mov    $0x0,%al
call   10a0 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xddb(%rip),%rsi
mov    $0x2,%edx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
jne    1255 <main+0x75>
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1266 <main+0x86>
mov    $0x0,%al
call   10a0 <usage@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
lea    0xd8d(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1294 <main+0xb4>
lea    0xd7e(%rip),%rdi
call   10f0 <fatal@plt>
mov    -0x20(%rbp),%edi
call   10d0 <grantpt@plt>
cmp    $0x0,%rax
jne    12ce <main+0xee>
mov    -0x20(%rbp),%edi
call   1090 <unlockpt@plt>
cmp    $0x0,%rax
jne    12ce <main+0xee>
mov    -0x20(%rbp),%edi
call   1040 <ptsname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12da <main+0xfa>
lea    0xd53(%rip),%rdi
call   10f0 <fatal@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1301 <main+0x121>
lea    0xd49(%rip),%rdi
call   10f0 <fatal@plt>
jmp    13a5 <main+0x1c5>
cmpq   $0x0,-0x18(%rbp)
jne    1379 <main+0x199>
mov    -0x30(%rbp),%rdi
mov    0x2cd1(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1337 <main+0x157>
lea    0xd1e(%rip),%rdi
call   10f0 <fatal@plt>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1070 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    $0x2,%esi
call   1070 <dup2@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x8(%rbp),%edi
sub    $0x1,%edi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1030 <child@plt>
jmp    13a0 <main+0x1c0>
mov    $0x3ef,%edi
call   10b0 <setgid@plt>
mov    $0x3ef,%edi
call   10e0 <setuid@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1050 <parent@plt>
jmp    13a5 <main+0x1c5>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r15d
cmp    $0x1,%edi
jg     11fd <main+0x1d>
xor    %eax,%eax
call   10a0 <usage@plt>
lea    0x8(%rbx),%r14
mov    0x8(%rbx),%rdi
lea    0xdf4(%rip),%rsi
mov    $0x2,%edx
call   1060 <strncmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
cmovne %rbx,%r14
sub    %ebp,%r15d
cmp    $0x1,%r15d
jg     1233 <main+0x53>
xor    %eax,%eax
call   10a0 <usage@plt>
mov    0x2dae(%rip),%r13
mov    0x0(%r13),%esi
lea    0xdbe(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    125c <main+0x7c>
lea    0xdb6(%rip),%rdi
call   10f0 <fatal@plt>
mov    %ebx,%edi
call   10d0 <grantpt@plt>
test   %rax,%rax
jne    1278 <main+0x98>
mov    %ebx,%edi
call   1090 <unlockpt@plt>
test   %rax,%rax
je     132e <main+0x14e>
lea    0xda9(%rip),%rdi
call   10f0 <fatal@plt>
xor    %r12d,%r12d
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
js     12b8 <main+0xd8>
je     12c6 <main+0xe6>
mov    $0x3ef,%edi
call   10b0 <setgid@plt>
mov    $0x3ef,%edi
call   10e0 <setuid@plt>
mov    0x8(%r14),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1050 <parent@plt>
jmp    131d <main+0x13d>
lea    0xd81(%rip),%rdi
call   10f0 <fatal@plt>
jmp    131d <main+0x13d>
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   10c0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12e4 <main+0x104>
lea    0xd71(%rip),%rdi
call   10f0 <fatal@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1070 <dup2@plt>
mov    %ebp,%edi
call   1080 <close@plt>
add    $0xffffffff,%r15d
add    $0x8,%r14
mov    %r15d,%edi
mov    %r14,%rsi
call   1030 <child@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1040 <ptsname@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1287 <main+0xa7>
jmp    1278 <main+0x98>

LLVM IR (-O0):
@.str = [3 x i8] c"-d\00"
@.str.1 = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@.str.2 = [27 x i8] c"Cannot create parent ptty\0A\00"
@.str.3 = [24 x i8] c"Problem with /dev/ptmx\0A\00"
@.str.4 = [16 x i8] c"Failed to fork\0A\00"
@.str.5 = [25 x i8] c"Problem with child ptty\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i8* null, i8** %10
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (...) @usage()
br label %15
15: ; preds = %13, %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i64 @strncmp(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %15
store i32 1, i32* %7
%22 = load i32, i32* %4
%23 = add nsw i32 %22, -1
store i32 %23, i32* %4
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i32 1
store i8** %25, i8*** %5
br label %26
26: ; preds = %21, %15
%27 = load i32, i32* %4
%28 = icmp slt i32 %27, 2
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 (...) @usage()
br label %31
31: ; preds = %29, %26
%32 = load i32, i32* @O_RDWR
%33 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @fatal(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %31
%39 = load i32, i32* %8
%40 = call i64 @grantpt(i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %51, label %42
42: ; preds = %38
%43 = load i32, i32* %8
%44 = call i64 @unlockpt(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %42
%47 = load i32, i32* %8
%48 = call i64 @ptsname(i32 %47)
%49 = inttoptr i64 %48 to i8*
store i8* %49, i8** %10
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %53
51: ; preds = %46, %42, %38
%52 = call i32 @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %46
%54 = call i64 (...) @fork()
store i64 %54, i64* %6
%55 = load i64, i64* %6
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %94
59: ; preds = %53
%60 = load i64, i64* %6
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %84
62: ; preds = %59
%63 = load i8*, i8** %10
%64 = load i32, i32* @O_RDWR
%65 = call i32 @open(i8* %63, i32 %64)
store i32 %65, i32* %9
%66 = load i32, i32* %9
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %62
%69 = call i32 @fatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %70
70: ; preds = %68, %62
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %9
%74 = call i32 @dup2(i32 %73, i32 1)
%75 = load i32, i32* %9
%76 = call i32 @dup2(i32 %75, i32 2)
%77 = load i32, i32* %9
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %4
%80 = sub nsw i32 %79, 1
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 1
%83 = call i32 @child(i32 %80, i8** %82)
br label %93
84: ; preds = %59
%85 = call i32 @setgid(i32 1007)
%86 = call i32 @setuid(i32 1007)
%87 = load i8**, i8*** %5
%88 = getelementptr inbounds i8*, i8** %87, i64 1
%89 = load i8*, i8** %88
%90 = load i32, i32* %7
%91 = load i32, i32* %8
%92 = call i32 @parent(i8* %89, i32 %90, i32 %91)
br label %93
93: ; preds = %84, %70
br label %94
94: ; preds = %93, %57
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"-d\00"
@.str.1 = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@.str.2 = [27 x i8] c"Cannot create parent ptty\0A\00"
@.str.3 = [24 x i8] c"Problem with /dev/ptmx\0A\00"
@.str.4 = [16 x i8] c"Failed to fork\0A\00"
@.str.5 = [25 x i8] c"Problem with child ptty\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (...) @usage()
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%10 = icmp eq i64 %9, 0
%11 = zext i1 %10 to i32
%12 = select i1 %10, i8** %7, i8** %1
%13 = sext i1 %10 to i32
%14 = add nsw i32 %13, %0
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %6
%17 = tail call i32 (...) @usage()
br label %18
18: ; preds = %16, %6
%19 = load i32, i32* @O_RDWR
%20 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @fatal(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = tail call i64 @grantpt(i32 %20)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = tail call i64 @unlockpt(i32 %20)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = tail call i64 @ptsname(i32 %20)
%32 = inttoptr i64 %31 to i8*
%33 = icmp eq i64 %31, 0
br i1 %33, label %34, label %36
34: ; preds = %30, %27, %24
%35 = tail call i32 @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = phi i8* [ null, %34 ], [ %32, %30 ]
%38 = tail call i64 (...) @fork()
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = tail call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %64
42: ; preds = %36
%43 = icmp eq i64 %38, 0
br i1 %43, label %44, label %58
44: ; preds = %42
%45 = load i32, i32* @O_RDWR
%46 = tail call i32 @open(i8* %37, i32 %45)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = tail call i32 @fatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = tail call i32 @close(i32 %20)
%52 = tail call i32 @dup2(i32 %46, i32 1)
%53 = tail call i32 @dup2(i32 %46, i32 2)
%54 = tail call i32 @close(i32 %46)
%55 = add nsw i32 %14, -1
%56 = getelementptr inbounds i8*, i8** %12, i64 1
%57 = tail call i32 @child(i32 %55, i8** %56)
br label %64
58: ; preds = %42
%59 = tail call i32 @setgid(i32 1007)
%60 = tail call i32 @setuid(i32 1007)
%61 = getelementptr inbounds i8*, i8** %12, i64 1
%62 = load i8*, i8** %61
%63 = tail call i32 @parent(i8* %62, i32 %11, i32 %20)
br label %64
64: ; preds = %50, %58, %40
ret i32 0
}