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

/* Variables and functions */
int /*<<< orphan*/ execl(char *, char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
int forkpty(int *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
            int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int amaster, w_status, status;
  char buf[256];
  printf("++++ Calling forkpty\n");
  int pid = forkpty(&amaster, NULL, NULL, NULL);
  switch (pid) {
  case 0:
    execl("/bin/ls", "ls", "--color", "-l", (char *)NULL);
    exit(1);
  case -1:
    printf("Error creatingh child process");
    exit(1);
  default:
    w_status = waitpid(pid, &status, 0);
    if (w_status == -1) {
      printf("Error waiting for child process");
      exit(1);
    }
    printf("++++ Command output\n");
    memset(buf, '\0', 256);
    while (read(amaster, buf, sizeof(buf) - 1) > 0) {
      printf("%s", buf);
      memset(buf, '\0', 256);
    }
    printf("++++ End PTY output\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe70(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x12c(%rbp)
call   1030 <printf@plt>
mov    -0x12c(%rbp),%eax
mov    %eax,%ecx
lea    -0x14(%rbp),%rdi
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1070 <forkpty@plt>
mov    %eax,-0x124(%rbp)
mov    -0x124(%rbp),%eax
mov    %eax,-0x128(%rbp)
sub    $0xffffffff,%eax
je     1217 <main+0xa7>
jmp    11d4 <main+0x64>
mov    -0x128(%rbp),%eax
test   %eax,%eax
jne    122f <main+0xbf>
jmp    11e7 <main+0x77>
lea    0xe28(%rip),%rdi
lea    0xe26(%rip),%rsi
lea    0xe22(%rip),%rdx
lea    0xe23(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1090 <execl@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x124(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1265 <main+0xf5>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
cmp    $0x0,%eax
jle    12d0 <main+0x160>
lea    -0x120(%rbp),%rsi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
jmp    1286 <main+0x116>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x118,%rsp
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <forkpty@plt>
cmp    $0xffffffff,%eax
je     125a <main+0xda>
test   %eax,%eax
je     1263 <main+0xe3>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1291 <main+0x111>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1241 <main+0xc1>
lea    0xe47(%rip),%r14
lea    0x10(%rsp),%rbx
nop
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     1210 <main+0x90>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdb2(%rip),%rdi
jmp    1298 <main+0x118>
lea    0xd96(%rip),%rdi
lea    0xd94(%rip),%rsi
lea    0xd90(%rip),%rdx
lea    0xd91(%rip),%rcx
xor    %r8d,%r8d
call   10a0 <execl@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd99(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"++++ Calling forkpty\0A\00"
@.str.1 = [8 x i8] c"/bin/ls\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [8 x i8] c"--color\00"
@.str.4 = [3 x i8] c"-l\00"
@.str.5 = [30 x i8] c"Error creatingh child process\00"
@.str.6 = [32 x i8] c"Error waiting for child process\00"
@.str.7 = [21 x i8] c"++++ Command output\0A\00"
@.str.8 = [3 x i8] c"%s\00"
@.str.9 = [21 x i8] c"++++ End PTY output\0A\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 i32
%9 = alloca [256 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @forkpty(i32* %6, i32* null, i32* null, i32* null)
store i32 %12, i32* %10
%13 = load i32, i32* %10
switch i32 %13, label %20 [
i32 0, label %14
i32 -1, label %17
]
14: ; preds = %2
%15 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* null)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %10
%22 = call i32 @waitpid(i32 %21, i32* %8, i32 0)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i8 signext 0, i32 256)
br label %32
32: ; preds = %37, %28
%33 = load i32, i32* %6
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 255)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %32
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %38)
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%41 = call i32 @memset(i8* %40, i8 signext 0, i32 256)
br label %32
42: ; preds = %32
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0))
br label %44
44: ; preds = %42
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"/bin/ls\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [8 x i8] c"--color\00"
@.str.4 = [3 x i8] c"-l\00"
@.str.5 = [30 x i8] c"Error creatingh child process\00"
@.str.6 = [32 x i8] c"Error waiting for child process\00"
@.str.8 = [3 x i8] c"%s\00"
@str = [21 x i8] c"++++ Calling forkpty\00"
@str.10 = [20 x i8] c"++++ Command output\00"
@str.11 = [20 x i8] c"++++ End PTY output\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%10 = call i32 @forkpty(i32* %3, i32* null, i32* null, i32* null)
switch i32 %10, label %17 [
i32 0, label %11
i32 -1, label %14
]
11: ; preds = %2
%12 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* null)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = call i32 @waitpid(i32 %10, i32* %4, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0))
%25 = call i32 @memset(i8* %8, i8 signext 0, i32 256)
%26 = load i32, i32* %3
%27 = call i32 @read(i32 %26, i8* %8, i32 255)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %23, %29
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %8)
%31 = call i32 @memset(i8* %8, i8 signext 0, i32 256)
%32 = load i32, i32* %3
%33 = call i32 @read(i32 %32, i8* %8, i32 255)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %29, label %35
35: ; preds = %29, %23
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.11, i64 0, i64 0))
ret i32 0
}