#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*/ FILE;

/* Variables and functions */
int BUFSIZ;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *const, char *const *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int grantpt(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int posix_openpt(int);
char *ptsname(int);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, char *, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int unlockpt(int);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *const argv[]) {
  int masterfd, slavefd;
  char *slavedevice;
  int child_pid, child_retval;
  char line[BUFSIZ];
  if (argc < 2) {
    fprintf(stderr, "Usage: ./pty <program> [<arg>*]\n");
    return 0;
  }
  masterfd = posix_openpt(O_RDWR | O_NOCTTY);
  if (masterfd == -1 || grantpt(masterfd) == -1 || unlockpt(masterfd) == -1 ||
      (slavedevice = ptsname(masterfd)) == NULL) {
    fprintf(stderr, "Unable to open pty.\n");
    return -1;
  }
  if ((child_pid = fork()) == -1) {
    fprintf(stderr, "Unable to fork.\n");
    return -1;
  }
  if (child_pid) {
    FILE *masterfp;
    setvbuf(stdout, (char *)NULL, _IONBF, 0);
    masterfp = fdopen(masterfd, "r");
    while (fgets(line, BUFSIZ, masterfp) != NULL) {
      int i;
      for (i = 0; line[i]; i++) {
        if (line[i] == '\r' && line[i + 1] == '\n' && line[i + 2] == '\0') {
          line[i] = '\n';
          line[i + 1] = '\0';
        }
      }
      fputs(line, stdout);
    }
    wait(&child_retval);
    if (child_retval) {
      fprintf(stdout, "\nProgram terminated with exit code %d.\n",
              child_retval);
    }
    return child_retval;
  } else {
    slavefd = open(slavedevice, O_RDWR | O_NOCTTY);
    if (slavefd < 0) {
      fprintf(stderr, "Unable to open slave end.\n");
      return -1;
    }
    dup2(slavefd, 1);
    close(masterfd);
    close(slavefd);
    execvp(argv[1], &argv[1]);
    perror("Failed to execute program");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1276 <main+0x76>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1515 <main+0x315>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    0x2d4a(%rip),%rax
or     (%rax),%edi
call   1100 <posix_openpt@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     12d2 <main+0xd2>
mov    -0x14(%rbp),%edi
call   10f0 <grantpt@plt>
cmp    $0xffffffff,%eax
je     12d2 <main+0xd2>
mov    -0x14(%rbp),%edi
call   1080 <unlockpt@plt>
cmp    $0xffffffff,%eax
je     12d2 <main+0xd2>
mov    -0x14(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    12fc <main+0xfc>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xd3f(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1515 <main+0x315>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1339 <main+0x139>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
lea    0xd17(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1515 <main+0x315>
cmpl   $0x0,-0x24(%rbp)
je     1477 <main+0x277>
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
mov    0x2c8d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   10b0 <setvbuf@plt>
mov    -0x14(%rbp),%edi
lea    0xcdd(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x58(%rbp),%rdi
mov    0x2c42(%rip),%rax
mov    (%rax),%esi
mov    -0x48(%rbp),%rdx
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1438 <main+0x238>
movl   $0x0,-0x4c(%rbp)
mov    -0x58(%rbp),%rax
movslq -0x4c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     1421 <main+0x221>
mov    -0x58(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    140e <main+0x20e>
mov    -0x58(%rbp),%rax
mov    -0x4c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    140e <main+0x20e>
mov    -0x58(%rbp),%rax
mov    -0x4c(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    140e <main+0x20e>
mov    -0x58(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x4c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1413 <main+0x213>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    139a <main+0x19a>
mov    -0x58(%rbp),%rdi
mov    0x2b8c(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
jmp    1373 <main+0x173>
lea    -0x28(%rbp),%rdi
call   1110 <wait@plt>
cmpl   $0x0,-0x28(%rbp)
je     1465 <main+0x265>
mov    0x2b66(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xbeb(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1515 <main+0x315>
mov    -0x20(%rbp),%rdi
mov    0x2b66(%rip),%rax
mov    (%rax),%esi
mov    0x2b45(%rip),%rax
or     (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    14c9 <main+0x2c9>
mov    0x2b22(%rip),%rax
mov    (%rax),%edi
lea    0xbc2(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1515 <main+0x315>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10e0 <execvp@plt>
lea    0xb8a(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%r15
mov    0x2da5(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x1,%edi
jg     1254 <main+0x54>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    12e9 <main+0xe9>
mov    %rsi,%r14
mov    %r15,-0x30(%rbp)
mov    0x2d86(%rip),%rax
mov    0x2d67(%rip),%r13
mov    0x0(%r13),%edi
or     (%rax),%edi
call   1100 <posix_openpt@plt>
cmp    $0xffffffff,%eax
je     12c8 <main+0xc8>
mov    %eax,%r15d
mov    %eax,%edi
call   10f0 <grantpt@plt>
cmp    $0xffffffff,%eax
je     12c8 <main+0xc8>
mov    %r15d,%edi
call   1080 <unlockpt@plt>
cmp    $0xffffffff,%eax
je     12c8 <main+0xc8>
mov    %r15d,%edi
call   1030 <ptsname@plt>
test   %rax,%rax
je     12c8 <main+0xc8>
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
call   1120 <fork@plt>
test   %eax,%eax
je     12fe <main+0xfe>
cmp    $0xffffffff,%eax
jne    132e <main+0x12e>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    0xd70(%rip),%rsi
jmp    12d8 <main+0xd8>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xd49(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    -0x30(%rbp),%r15
mov    %r15,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%r13),%esi
mov    0x2cdf(%rip),%rax
or     (%rax),%esi
mov    -0x40(%rbp),%rdi
call   10c0 <open@plt>
test   %eax,%eax
jns    1418 <main+0x218>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
lea    0xd45(%rip),%rsi
jmp    12d8 <main+0xd8>
mov    0x2c83(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2ca0(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
xor    %esi,%esi
xor    %ecx,%ecx
call   10b0 <setvbuf@plt>
lea    0xcf2(%rip),%rsi
mov    %r15d,%edi
call   10a0 <fdopen@plt>
mov    %rax,%r15
mov    (%r12),%esi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     13e5 <main+0x1e5>
xor    %ecx,%ecx
jmp    1387 <main+0x187>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rax
mov    %rax,%rcx
mov    %rcx,%rax
movzbl (%rbx,%rcx,1),%ecx
test   %cl,%cl
je     13c0 <main+0x1c0>
cmp    $0xd,%cl
jne    1380 <main+0x180>
lea    0x1(%rax),%rcx
cmpb   $0xa,0x1(%rbx,%rax,1)
jne    1387 <main+0x187>
cmpb   $0x0,0x2(%rax,%rbx,1)
jne    1387 <main+0x187>
movw   $0xa,(%rbx,%rax,1)
jmp    1387 <main+0x187>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
mov    %r15,%rdx
call   1070 <fgets@plt>
mov    $0x0,%ecx
test   %rax,%rax
jne    1387 <main+0x187>
lea    -0x34(%rbp),%rdi
call   1110 <wait@plt>
mov    -0x34(%rbp),%edx
test   %edx,%edx
mov    -0x30(%rbp),%r15
je     12e9 <main+0xe9>
mov    0x0(%r13),%edi
lea    0xc41(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    -0x34(%rbp),%r14d
jmp    12e9 <main+0xe9>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x8(%r14),%rdi
add    $0x8,%r14
mov    %r14,%rsi
call   10e0 <execvp@plt>
lea    0xc40(%rip),%rdi
call   10d0 <perror@plt>
jmp    12df <main+0xdf>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Usage: ./pty <program> [<arg>*]\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [21 x i8] c"Unable to open pty.\0A\00"
@.str.2 = [17 x i8] c"Unable to fork.\0A\00"
@stdout = global i32 0
@_IONBF = global i32 0
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [40 x i8] c"\0AProgram terminated with exit code %d.\0A\00"
@.str.5 = [27 x i8] c"Unable to open slave end.\0A\00"
@.str.6 = [26 x i8] c"Failed to execute program\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* @BUFSIZ
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %11
%19 = alloca i8, i64 %17
store i64 %17, i64* %12
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 2
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
store i32 1, i32* %13
br label %147
25: ; preds = %2
%26 = load i32, i32* @O_RDWR
%27 = load i32, i32* @O_NOCTTY
%28 = or i32 %26, %27
%29 = call i32 @posix_openpt(i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, -1
br i1 %31, label %44, label %32
32: ; preds = %25
%33 = load i32, i32* %6
%34 = call i32 @grantpt(i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %44, label %36
36: ; preds = %32
%37 = load i32, i32* %6
%38 = call i32 @unlockpt(i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i32, i32* %6
%42 = call i8* @ptsname(i32 %41)
store i8* %42, i8** %8
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %47
44: ; preds = %40, %36, %32, %25
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %147
47: ; preds = %40
%48 = call i32 (...) @fork()
store i32 %48, i32* %9
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %147
53: ; preds = %47
%54 = load i32, i32* %9
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %122
56: ; preds = %53
%57 = load i32, i32* @stdout
%58 = load i32, i32* @_IONBF
%59 = call i32 @setvbuf(i32 %57, i8* null, i32 %58, i32 0)
%60 = load i32, i32* %6
%61 = call i32* @fdopen(i32 %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %61, i32** %14
br label %62
62: ; preds = %109, %56
%63 = load i32, i32* @BUFSIZ
%64 = load i32*, i32** %14
%65 = call i32* @fgets(i8* %19, i32 %63, i32* %64)
%66 = icmp ne i32* %65, null
br i1 %66, label %67, label %112
67: ; preds = %62
store i32 0, i32* %15
br label %68
68: ; preds = %106, %67
%69 = load i32, i32* %15
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %19, i64 %70
%72 = load i8, i8* %71
%73 = icmp ne i8 %72, 0
br i1 %73, label %74, label %109
74: ; preds = %68
%75 = load i32, i32* %15
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %19, i64 %76
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = icmp eq i32 %79, 13
br i1 %80, label %81, label %105
81: ; preds = %74
%82 = load i32, i32* %15
%83 = add nsw i32 %82, 1
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %19, i64 %84
%86 = load i8, i8* %85
%87 = sext i8 %86 to i32
%88 = icmp eq i32 %87, 10
br i1 %88, label %89, label %105
89: ; preds = %81
%90 = load i32, i32* %15
%91 = add nsw i32 %90, 2
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %19, i64 %92
%94 = load i8, i8* %93
%95 = sext i8 %94 to i32
%96 = icmp eq i32 %95, 0
br i1 %96, label %97, label %105
97: ; preds = %89
%98 = load i32, i32* %15
%99 = sext i32 %98 to i64
%100 = getelementptr inbounds i8, i8* %19, i64 %99
store i8 10, i8* %100
%101 = load i32, i32* %15
%102 = add nsw i32 %101, 1
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds i8, i8* %19, i64 %103
store i8 0, i8* %104
br label %105
105: ; preds = %97, %89, %81, %74
br label %106
106: ; preds = %105
%107 = load i32, i32* %15
%108 = add nsw i32 %107, 1
store i32 %108, i32* %15
br label %68
109: ; preds = %68
%110 = load i32, i32* @stdout
%111 = call i32 @fputs(i8* %19, i32 %110)
br label %62
112: ; preds = %62
%113 = call i32 @wait(i32* %10)
%114 = load i32, i32* %10
%115 = icmp ne i32 %114, 0
br i1 %115, label %116, label %120
116: ; preds = %112
%117 = load i32, i32* @stdout
%118 = load i32, i32* %10
%119 = call i32 (i32, i8*, ...) @fprintf(i32 %117, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %118)
br label %120
120: ; preds = %116, %112
%121 = load i32, i32* %10
store i32 %121, i32* %3
store i32 1, i32* %13
br label %147
122: ; preds = %53
%123 = load i8*, i8** %8
%124 = load i32, i32* @O_RDWR
%125 = load i32, i32* @O_NOCTTY
%126 = or i32 %124, %125
%127 = call i32 @open(i8* %123, i32 %126)
store i32 %127, i32* %7
%128 = load i32, i32* %7
%129 = icmp slt i32 %128, 0
br i1 %129, label %130, label %133
130: ; preds = %122
%131 = load i32, i32* @stderr
%132 = call i32 (i32, i8*, ...) @fprintf(i32 %131, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %147
133: ; preds = %122
%134 = load i32, i32* %7
%135 = call i32 @dup2(i32 %134, i32 1)
%136 = load i32, i32* %6
%137 = call i32 @close(i32 %136)
%138 = load i32, i32* %7
%139 = call i32 @close(i32 %138)
%140 = load i8**, i8*** %5
%141 = getelementptr inbounds i8*, i8** %140, i64 1
%142 = load i8*, i8** %141
%143 = load i8**, i8*** %5
%144 = getelementptr inbounds i8*, i8** %143, i64 1
%145 = call i32 @execvp(i8* %142, i8** %144)
%146 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %147
147: ; preds = %133, %130, %120, %50, %44, %22
%148 = load i8*, i8** %11
%149 = load i32, i32* %3
ret i32 %149
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Usage: ./pty <program> [<arg>*]\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [21 x i8] c"Unable to open pty.\0A\00"
@.str.2 = [17 x i8] c"Unable to fork.\0A\00"
@stdout = global i32 0
@_IONBF = global i32 0
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [40 x i8] c"\0AProgram terminated with exit code %d.\0A\00"
@.str.5 = [27 x i8] c"Unable to open slave end.\0A\00"
@.str.6 = [26 x i8] c"Failed to execute program\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = icmp slt i32 %0, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %94
13: ; preds = %2
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_NOCTTY
%16 = or i32 %15, %14
%17 = tail call i32 @posix_openpt(i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %28, label %19
19: ; preds = %13
%20 = tail call i32 @grantpt(i32 %17)
%21 = icmp eq i32 %20, -1
br i1 %21, label %28, label %22
22: ; preds = %19
%23 = tail call i32 @unlockpt(i32 %17)
%24 = icmp eq i32 %23, -1
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = tail call i8* @ptsname(i32 %17)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25, %22, %19, %13
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %94
31: ; preds = %25
%32 = tail call i32 (...) @fork()
switch i32 %32, label %36 [
i32 -1, label %33
i32 0, label %77
]
33: ; preds = %31
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %94
36: ; preds = %31
%37 = load i32, i32* @stdout
%38 = load i32, i32* @_IONBF
%39 = tail call i32 @setvbuf(i32 %37, i8* null, i32 %38, i32 0)
%40 = tail call i32* @fdopen(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* @BUFSIZ
%42 = call i32* @fgets(i8* %8, i32 %41, i32* %40)
%43 = icmp eq i32* %42, null
br i1 %43, label %69, label %44
44: ; preds = %36, %67
%45 = phi i64 [ %68, %67 ], [ 0, %36 ]
%46 = getelementptr inbounds i8, i8* %8, i64 %45
%47 = load i8, i8* %46
switch i8 %47, label %48 [
i8 0, label %61
i8 13, label %50
]
48: ; preds = %44
%49 = add nuw i64 %45, 1
br label %67
50: ; preds = %44
%51 = add nuw i64 %45, 1
%52 = getelementptr inbounds i8, i8* %8, i64 %51
%53 = load i8, i8* %52
%54 = icmp eq i8 %53, 10
br i1 %54, label %55, label %67
55: ; preds = %50
%56 = add nuw nsw i64 %45, 2
%57 = getelementptr inbounds i8, i8* %8, i64 %56
%58 = load i8, i8* %57
%59 = icmp eq i8 %58, 0
br i1 %59, label %60, label %67
60: ; preds = %55
store i8 10, i8* %46
store i8 0, i8* %52
br label %67
61: ; preds = %44
%62 = load i32, i32* @stdout
%63 = call i32 @fputs(i8* %8, i32 %62)
%64 = load i32, i32* @BUFSIZ
%65 = call i32* @fgets(i8* %8, i32 %64, i32* %40)
%66 = icmp eq i32* %65, null
br i1 %66, label %69, label %67
67: ; preds = %61, %48, %50, %55, %60
%68 = phi i64 [ 0, %61 ], [ %49, %48 ], [ %51, %50 ], [ %51, %55 ], [ %51, %60 ]
br label %44
69: ; preds = %61, %36
%70 = call i32 @wait(i32* %3)
%71 = load i32, i32* %3
%72 = icmp eq i32 %71, 0
br i1 %72, label %94, label %73
73: ; preds = %69
%74 = load i32, i32* @stdout
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %71)
%76 = load i32, i32* %3
br label %94
77: ; preds = %31
%78 = load i32, i32* @O_RDWR
%79 = load i32, i32* @O_NOCTTY
%80 = or i32 %79, %78
%81 = tail call i32 @open(i8* %26, i32 %80)
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %77
%84 = load i32, i32* @stderr
%85 = tail call i32 (i32, i8*, ...) @fprintf(i32 %84, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %94
86: ; preds = %77
%87 = tail call i32 @dup2(i32 %81, i32 1)
%88 = tail call i32 @close(i32 %17)
%89 = tail call i32 @close(i32 %81)
%90 = getelementptr inbounds i8*, i8** %1, i64 1
%91 = load i8*, i8** %90
%92 = tail call i32 @execvp(i8* %91, i8** %90)
%93 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %94
94: ; preds = %69, %73, %86, %83, %33, %28, %10
%95 = phi i32 [ 0, %10 ], [ -1, %28 ], [ -1, %33 ], [ -1, %83 ], [ -1, %86 ], [ %76, %73 ], [ 0, %69 ]
ret i32 %95
}