#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 */
struct termios {
  int dummy;
};

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int fork();
int getppid();
int grantpt(int);
int kill(int, int /*<<< orphan*/);
int open(char *, int);
int posix_openpt(int);
int /*<<< orphan*/ printf(char *, char *);
char *ptsname(int);
scalar_t__ setupslave(char *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ testslave(char *, struct termios *, int);
int unlockpt(int);
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main() {
  int masterfd, slavefd, pid;
  char *slavedevice;
  int orig_stdout = -1;
  masterfd = posix_openpt(O_RDWR | O_NOCTTY);
  if (masterfd == -1 || grantpt(masterfd) == -1 || unlockpt(masterfd) == -1 ||
      (slavedevice = ptsname(masterfd)) == NULL) {
    return 1;
  }
  printf("slave device is: %s\n", slavedevice);
  slavefd = open(slavedevice, O_RDWR | O_NOCTTY);
  if (slavefd < 0) {
    return 2;
  }
  close(slavefd);
  if ((pid = fork()) < 0) {
    ;
    return 3;
  }
  if (pid == 0) {
    if (setupslave(slavedevice, orig_stdout) == 0) {
      struct termios orig_termios;
      tcgetattr(1, &orig_termios);
      int ppid = getppid();
      while (1) {
        testslave(slavedevice, &orig_termios, orig_stdout);
        if (kill(ppid, 0) == -1) {
          return 0;
        }
      }
    }
  } else if (waitpid(pid, NULL, 0) == -1) {
    ;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    0x2dd2(%rip),%rax
or     (%rax),%edi
call   10e0 <posix_openpt@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     1252 <main+0x72>
mov    -0x8(%rbp),%edi
call   10c0 <grantpt@plt>
cmp    $0xffffffff,%eax
je     1252 <main+0x72>
mov    -0x8(%rbp),%edi
call   1060 <unlockpt@plt>
cmp    $0xffffffff,%eax
je     1252 <main+0x72>
mov    -0x8(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    125e <main+0x7e>
movl   $0x1,-0x4(%rbp)
jmp    1362 <main+0x182>
mov    -0x18(%rbp),%rsi
lea    0xd97(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    0x2d56(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12a2 <main+0xc2>
movl   $0x2,-0x4(%rbp)
jmp    1362 <main+0x182>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    12c9 <main+0xe9>
movl   $0x3,-0x4(%rbp)
jmp    1362 <main+0x182>
cmpl   $0x0,-0x10(%rbp)
jne    133a <main+0x15a>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   10f0 <setupslave@plt>
cmp    $0x0,%rax
jne    1335 <main+0x155>
mov    $0x1,%edi
lea    -0x20(%rbp),%rsi
call   10a0 <tcgetattr@plt>
mov    $0x0,%al
call   10d0 <getppid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1080 <testslave@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1070 <kill@plt>
cmp    $0xffffffff,%eax
jne    1330 <main+0x150>
movl   $0x0,-0x4(%rbp)
jmp    1362 <main+0x182>
jmp    1301 <main+0x121>
jmp    135b <main+0x17b>
mov    -0x10(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1356 <main+0x176>
jmp    1356 <main+0x176>
jmp    135b <main+0x17b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2df5(%rip),%r15
mov    0x2dde(%rip),%r12
mov    (%r12),%edi
or     (%r15),%edi
call   10e0 <posix_openpt@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
je     12f0 <main+0x110>
mov    %eax,%ebp
mov    %eax,%edi
call   10c0 <grantpt@plt>
cmp    $0xffffffff,%eax
je     12f0 <main+0x110>
mov    %ebp,%edi
call   1060 <unlockpt@plt>
cmp    $0xffffffff,%eax
je     12f0 <main+0x110>
mov    %ebp,%edi
call   1030 <ptsname@plt>
test   %rax,%rax
je     12f0 <main+0x110>
mov    %rax,%r14
lea    0xdb0(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    (%r12),%esi
or     (%r15),%esi
mov    %r14,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     128e <main+0xae>
mov    %eax,%edi
call   1050 <close@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
js     1295 <main+0xb5>
je     129c <main+0xbc>
xor    %ebx,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
jmp    12f0 <main+0x110>
mov    $0x2,%ebx
jmp    12f0 <main+0x110>
mov    $0x3,%ebx
jmp    12f0 <main+0x110>
mov    %r14,%rdi
mov    $0xffffffff,%esi
call   10f0 <setupslave@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    12f0 <main+0x110>
lea    0x8(%rsp),%rbx
mov    $0x1,%edi
mov    %rbx,%rsi
call   10a0 <tcgetattr@plt>
xor    %eax,%eax
call   10d0 <getppid@plt>
mov    %eax,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0xffffffff,%edx
call   1080 <testslave@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <kill@plt>
cmp    $0xffffffff,%eax
jne    12d0 <main+0xf0>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [21 x i8] c"slave device is: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %6
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @posix_openpt(i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %27, label %15
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i32 @grantpt(i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %27, label %19
19: ; preds = %15
%20 = load i32, i32* %2
%21 = call i32 @unlockpt(i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i32, i32* %2
%25 = call i8* @ptsname(i32 %24)
store i8* %25, i8** %5
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %23, %19, %15, %0
store i32 1, i32* %1
br label %73
28: ; preds = %23
%29 = load i8*, i8** %5
%30 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %5
%32 = load i32, i32* @O_RDWR
%33 = load i32, i32* @O_NOCTTY
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %31, i32 %34)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %28
store i32 2, i32* %1
br label %73
39: ; preds = %28
%40 = load i32, i32* %3
%41 = call i32 @close(i32 %40)
%42 = call i32 (...) @fork()
store i32 %42, i32* %4
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 3, i32* %1
br label %73
45: ; preds = %39
%46 = load i32, i32* %4
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %66
48: ; preds = %45
%49 = load i8*, i8** %5
%50 = load i32, i32* %6
%51 = call i64 @setupslave(i8* %49, i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %65
53: ; preds = %48
%54 = call i32 @tcgetattr(i32 1, %struct.termios* %7)
%55 = call i32 (...) @getppid()
store i32 %55, i32* %8
br label %56
56: ; preds = %53, %64
%57 = load i8*, i8** %5
%58 = load i32, i32* %6
%59 = call i32 @testslave(i8* %57, %struct.termios* %7, i32 %58)
%60 = load i32, i32* %8
%61 = call i32 @kill(i32 %60, i32 0)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %64
63: ; preds = %56
store i32 0, i32* %1
br label %73
64: ; preds = %56
br label %56
65: ; preds = %48
br label %72
66: ; preds = %45
%67 = load i32, i32* %4
%68 = call i32 @waitpid(i32 %67, i32* null, i32 0)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %71
70: ; preds = %66
br label %71
71: ; preds = %70, %66
br label %72
72: ; preds = %71, %65
store i32 0, i32* %1
br label %73
73: ; preds = %72, %63, %44, %38, %27
%74 = load i32, i32* %1
ret i32 %74
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [21 x i8] c"slave device is: %s\0A\00"
define i32 @main() {
%1 = alloca %struct.termios
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %3, %2
%5 = tail call i32 @posix_openpt(i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %43, label %7
7: ; preds = %0
%8 = tail call i32 @grantpt(i32 %5)
%9 = icmp eq i32 %8, -1
br i1 %9, label %43, label %10
10: ; preds = %7
%11 = tail call i32 @unlockpt(i32 %5)
%12 = icmp eq i32 %11, -1
br i1 %12, label %43, label %13
13: ; preds = %10
%14 = tail call i8* @ptsname(i32 %5)
%15 = icmp eq i8* %14, null
br i1 %15, label %43, label %16
16: ; preds = %13
%17 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %14)
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_NOCTTY
%20 = or i32 %19, %18
%21 = tail call i32 @open(i8* %14, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %43, label %23
23: ; preds = %16
%24 = tail call i32 @close(i32 %21)
%25 = tail call i32 (...) @fork()
%26 = icmp slt i32 %25, 0
br i1 %26, label %43, label %27
27: ; preds = %23
%28 = icmp eq i32 %25, 0
br i1 %28, label %29, label %41
29: ; preds = %27
%30 = tail call i64 @setupslave(i8* %14, i32 -1)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %29
%33 = bitcast %struct.termios* %1 to i8*
%34 = call i32 @tcgetattr(i32 1, %struct.termios* %1)
%35 = call i32 (...) @getppid()
br label %36
36: ; preds = %36, %32
%37 = call i32 @testslave(i8* %14, %struct.termios* %1, i32 -1)
%38 = call i32 @kill(i32 %35, i32 0)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %36
40: ; preds = %36
br label %43
41: ; preds = %27
%42 = tail call i32 @waitpid(i32 %25, i32* null, i32 0)
br label %43
43: ; preds = %41, %29, %23, %16, %0, %7, %10, %13, %40
%44 = phi i32 [ 0, %40 ], [ 1, %13 ], [ 1, %10 ], [ 1, %7 ], [ 1, %0 ], [ 2, %16 ], [ 3, %23 ], [ 0, %29 ], [ 0, %41 ]
ret i32 %44
}