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

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *const, char *const *);
int /*<<< orphan*/ exit(int);
int fork();
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
size_t read(int, char *, int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int run_proc_a(char **out, char *const cmd[]) {
  int pid, ret;
  int pipes[2];
  char *buffer = (char *)malloc(5120);
  if (pipe(pipes) != 0) {
    perror("Error creating pipes");
    return 1;
  }
  if ((pid = fork()) == -1) {
    perror("Error forking");
    return 1;
  }
  if (pid == 0) {
    if (dup2(pipes[1], STDOUT_FILENO) == -1) {
      perror("Error duplicating stdout");
      exit(1);
    }
    close(STDERR_FILENO);
    execvp(cmd[0], cmd);
  }
  int child_status;
  waitpid(pid, &child_status, 0);
  if (child_status != 0) {
    return 2;
  }
  size_t read_length = read(pipes[0], buffer, 5120);
  if (read_length == 5120) {
    perror("Buffer not large enough");
    return 1;
  }
  *out = buffer;
  return 0;
}


x86 assembly (clang, -O0):
<run_proc_a>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x1400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11e9 <run_proc_a+0x49>
lea    0xe28(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <run_proc_a+0x13d>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1214 <run_proc_a+0x74>
lea    0xe12(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <run_proc_a+0x13d>
cmpl   $0x0,-0x1c(%rbp)
jne    126c <run_proc_a+0xcc>
mov    -0x24(%rbp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    124e <run_proc_a+0xae>
lea    0xde4(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <execvp@plt>
mov    -0x1c(%rbp),%edi
lea    -0x34(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmpl   $0x0,-0x34(%rbp)
je     1290 <run_proc_a+0xf0>
movl   $0x2,-0x4(%rbp)
jmp    12dd <run_proc_a+0x13d>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x1400,%edx
call   1060 <read@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x1400,-0x40(%rbp)
jne    12cb <run_proc_a+0x12b>
lea    0xd82(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <run_proc_a+0x13d>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_proc_a>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    $0x1400,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jne    1255 <run_proc_a+0xb5>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11eb <run_proc_a+0x4b>
cmp    $0xffffffff,%ebp
jne    121b <run_proc_a+0x7b>
lea    0xe2c(%rip),%rdi
jmp    1265 <run_proc_a+0xc5>
mov    0x14(%rsp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     127a <run_proc_a+0xda>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   10a0 <execvp@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    $0x2,%eax
cmpl   $0x0,0xc(%rsp)
jne    126f <run_proc_a+0xcf>
mov    0x10(%rsp),%edi
mov    %r15,%rsi
mov    $0x1400,%edx
call   1060 <read@plt>
cmp    $0x1400,%rax
je     125e <run_proc_a+0xbe>
mov    %r15,(%r14)
xor    %eax,%eax
jmp    126f <run_proc_a+0xcf>
lea    0xda4(%rip),%rdi
jmp    1265 <run_proc_a+0xc5>
lea    0xdd7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"Error creating pipes\00"
@.str.1 = [14 x i8] c"Error forking\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [25 x i8] c"Error duplicating stdout\00"
@STDERR_FILENO = global i32 0
@.str.3 = [24 x i8] c"Buffer not large enough\00"
define i32 @run_proc_a(i8** %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i32]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i64
store i8** %0, i8*** %4
store i8** %1, i8*** %5
%12 = call i64 @malloc(i32 5120)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %9
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%15 = call i64 @pipe(i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %62
19: ; preds = %2
%20 = call i32 (...) @fork()
store i32 %20, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %62
24: ; preds = %19
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %24
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%29 = load i32, i32* %28
%30 = load i32, i32* @STDOUT_FILENO
%31 = call i32 @dup2(i32 %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = load i32, i32* @STDERR_FILENO
%38 = call i32 @close(i32 %37)
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 0
%41 = load i8*, i8** %40
%42 = load i8**, i8*** %5
%43 = call i32 @execvp(i8* %41, i8** %42)
br label %44
44: ; preds = %36, %24
%45 = load i32, i32* %6
%46 = call i32 @waitpid(i32 %45, i32* %10, i32 0)
%47 = load i32, i32* %10
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
store i32 2, i32* %3
br label %62
50: ; preds = %44
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%52 = load i32, i32* %51
%53 = load i8*, i8** %9
%54 = call i64 @read(i32 %52, i8* %53, i32 5120)
store i64 %54, i64* %11
%55 = load i64, i64* %11
%56 = icmp eq i64 %55, 5120
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %62
59: ; preds = %50
%60 = load i8*, i8** %9
%61 = load i8**, i8*** %4
store i8* %60, i8** %61
store i32 0, i32* %3
br label %62
62: ; preds = %59, %57, %49, %22, %17
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [21 x i8] c"Error creating pipes\00"
@.str.1 = [14 x i8] c"Error forking\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [25 x i8] c"Error duplicating stdout\00"
@STDERR_FILENO = global i32 0
@.str.3 = [24 x i8] c"Buffer not large enough\00"
define i32 @run_proc_a(i8** nocapture writeonly %0, i8** %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = tail call i64 @malloc(i32 5120)
%7 = inttoptr i64 %6 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %45
13: ; preds = %2
%14 = call i32 (...) @fork()
switch i32 %14, label %31 [
i32 -1, label %15
i32 0, label %17
]
15: ; preds = %13
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %45
17: ; preds = %13
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%19 = load i32, i32* %18
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i32, i32* @STDERR_FILENO
%28 = call i32 @close(i32 %27)
%29 = load i8*, i8** %1
%30 = call i32 @execvp(i8* %29, i8** %1)
br label %31
31: ; preds = %13, %26
%32 = bitcast i32* %4 to i8*
%33 = call i32 @waitpid(i32 %14, i32* %4, i32 0)
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %31
%37 = load i32, i32* %8
%38 = call i64 @read(i32 %37, i8* %7, i32 5120)
%39 = icmp eq i64 %38, 5120
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %43
42: ; preds = %36
store i8* %7, i8** %0
br label %43
43: ; preds = %40, %42, %31
%44 = phi i32 [ 2, %31 ], [ 1, %40 ], [ 0, %42 ]
br label %45
45: ; preds = %43, %15, %11
%46 = phi i32 [ 1, %11 ], [ 1, %15 ], [ %44, %43 ]
ret i32 %46
}