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

/* Variables and functions */
int /*<<< orphan*/ EX_USAGE;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork_pipe_child(int);
int fork_pty_child(int *, char **);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int **, int);
scalar_t__ waiting_for_child(int);
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char **argv) {
  int master_fd;
  printf("begin - - -\n");
  if (argc < 2) {
    printf("Usage: %s cmd [args...]\n", argv[0]);
    exit(EX_USAGE);
  }
  pid_t pty_child_pid = fork_pty_child(&master_fd, argv);
  pid_t pipe_child_pid = fork_pipe_child(master_fd);
  int log_fd = open("io/output", O_CREAT | O_RDWR, 0666);
  printf("pipe_child=%d\n", pipe_child_pid);
  printf("pty_child=%d\n", pty_child_pid);
  while (waiting_for_child(pty_child_pid)) {
    int num_to_read = 100;
    int buf[num_to_read];
    int num_read = read(master_fd, &buf, num_to_read);
    write(log_fd, buf, num_read);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11d1 <main+0x51>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdi
call   10a0 <fork_pty_child@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <fork_pipe_child@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    0x2de4(%rip),%rax
or     (%rax),%esi
lea    0xe21(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1030 <waiting_for_child@plt>
cmp    $0x0,%rax
je     129d <main+0x11d>
movl   $0x64,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x48(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1060 <read@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x3c(%rbp),%edx
call   1040 <write@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
jmp    1234 <main+0xb4>
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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
lea    0xe95(%rip),%rdi
call   1040 <puts@plt>
cmp    $0x1,%ebx
jle    126c <main+0xdc>
lea    -0x24(%rbp),%rdi
mov    %r14,%rsi
call   10b0 <fork_pty_child@plt>
mov    %eax,%r14d
mov    -0x24(%rbp),%edi
call   1090 <fork_pipe_child@plt>
mov    %eax,%r15d
mov    0x2e1e(%rip),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe2d(%rip),%rdi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,%r12d
lea    0xe23(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe21(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
call   1030 <waiting_for_child@plt>
test   %rax,%rax
je     125d <main+0xcd>
mov    %rsp,%r15
lea    -0x190(%rsp),%rbx
ff
mov    %rbx,%rsp
mov    -0x24(%rbp),%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %r15,%rsp
jmp    1220 <main+0x90>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xd8a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"begin - - -\0A\00"
@.str.1 = [25 x i8] c"Usage: %s cmd [args...]\0A\00"
@EX_USAGE = global i32 0
@.str.2 = [10 x i8] c"io/output\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.3 = [15 x i8] c"pipe_child=%d\0A\00"
@.str.4 = [14 x i8] c"pty_child=%d\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 i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %24
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EX_USAGE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = call i32 @fork_pty_child(i32* %6, i8** %25)
store i32 %26, i32* %7
%27 = load i32, i32* %6
%28 = call i32 @fork_pipe_child(i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* @O_CREAT
%30 = load i32, i32* @O_RDWR
%31 = or i32 %29, %30
%32 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 438)
store i32 %32, i32* %9
%33 = load i32, i32* %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %41, %24
%38 = load i32, i32* %7
%39 = call i64 @waiting_for_child(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %54
41: ; preds = %37
store i32 100, i32* %10
%42 = load i32, i32* %10
%43 = zext i32 %42 to i64
%44 = call i8* @llvm.stacksave()
store i8* %44, i8** %11
%45 = alloca i32, i64 %43
store i64 %43, i64* %12
%46 = load i32, i32* %6
%47 = bitcast i32* %45 to i32**
%48 = load i32, i32* %10
%49 = call i32 @read(i32 %46, i32** %47, i32 %48)
store i32 %49, i32* %13
%50 = load i32, i32* %9
%51 = load i32, i32* %13
%52 = call i32 @write(i32 %50, i32* %45, i32 %51)
%53 = load i8*, i8** %11
br label %37
54: ; preds = %37
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str.1 = [25 x i8] c"Usage: %s cmd [args...]\0A\00"
@EX_USAGE = global i32 0
@.str.2 = [10 x i8] c"io/output\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.3 = [15 x i8] c"pipe_child=%d\0A\00"
@.str.4 = [14 x i8] c"pty_child=%d\0A\00"
@str = [12 x i8] c"begin - - -\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EX_USAGE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = call i32 @fork_pty_child(i32* %3, i8** %1)
%14 = load i32, i32* %3
%15 = call i32 @fork_pipe_child(i32 %14)
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_RDWR
%18 = or i32 %17, %16
%19 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 438)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%22 = call i64 @waiting_for_child(i32 %13)
%23 = icmp eq i64 %22, 0
br i1 %23, label %34, label %24
24: ; preds = %12, %24
%25 = call i8* @llvm.stacksave()
%26 = alloca [100 x i32]
%27 = getelementptr inbounds [100 x i32], [100 x i32]* %26, i64 0, i64 0
%28 = load i32, i32* %3
%29 = bitcast [100 x i32]* %26 to i32**
%30 = call i32 @read(i32 %28, i32** %29, i32 100)
%31 = call i32 @write(i32 %19, i32* %27, i32 %30)
%32 = call i64 @waiting_for_child(i32 %13)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %24
34: ; preds = %24, %12
ret i32 0
}