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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
int dup(int);
int execv(char const *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int fileno(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ stdout;
FILE *run_program(const char *program_path, char **args) {
  pid_t pid;
  int pipes[2];
  if (pipe(pipes)) {
    perror("pipe failed");
    exit(EXIT_FAILURE);
  }
  pid = fork();
  switch (pid) {
  case 0: {
    int old;
    close(fileno(stdout));
    old = dup(pipes[1]);
    close(pipes[0]);
    close(pipes[1]);
    if (execv(program_path, args) == -1) {
      perror("execv failed");
      exit(EXIT_FAILURE);
    }
  }
  case -1:
    perror("fork failed");
    exit(EXIT_FAILURE);
  default: {
    close(pipes[1]);
    return fdopen(pipes[0], "r");
  }
  }
}


x86 assembly (clang, -O0):
<run_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11cd <run_program+0x3d>
lea    0xe46(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0xffffffff,%eax
je     1260 <run_program+0xd0>
jmp    11eb <run_program+0x5b>
mov    -0x24(%rbp),%eax
test   %eax,%eax
jne    127a <run_program+0xea>
jmp    11fb <run_program+0x6b>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <dup@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <execv@plt>
cmp    $0xffffffff,%eax
jne    125b <run_program+0xcb>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1260 <run_program+0xd0>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
lea    0xd99(%rip),%rsi
call   1070 <fdopen@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_program>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jne    11d7 <run_program+0x47>
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     1228 <run_program+0x98>
test   %eax,%eax
je     11e0 <run_program+0x50>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
lea    0xe5b(%rip),%rsi
call   1070 <fdopen@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe22(%rip),%rdi
jmp    122f <run_program+0x9f>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1030 <dup@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <execv@plt>
cmp    $0xffffffff,%eax
jne    1228 <run_program+0x98>
lea    0xde6(%rip),%rdi
jmp    122f <run_program+0x9f>
lea    0xdea(%rip),%rdi
call   1080 <perror@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"pipe failed\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@.str.1 = [13 x i8] c"execv failed\00"
@.str.2 = [12 x i8] c"fork failed\00"
@.str.3 = [2 x i8] c"r\00"
define i32* @run_program(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = call i32 (...) @fork()
store i32 %16, i32* %5
%17 = load i32, i32* %5
switch i32 %17, label %44 [
i32 0, label %18
i32 -1, label %40
]
18: ; preds = %15
%19 = load i32, i32* @stdout
%20 = call i32 @fileno(i32 %19)
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @dup(i32 %23)
store i32 %24, i32* %7
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %3
%32 = load i8**, i8*** %4
%33 = call i32 @execv(i8* %31, i8** %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %18
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %18
br label %40
40: ; preds = %15, %39
%41 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %15
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32* @fdopen(i32 %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret i32* %50
}

LLVM IR (-O2):
@.str = [12 x i8] c"pipe failed\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@.str.1 = [13 x i8] c"execv failed\00"
@.str.2 = [12 x i8] c"fork failed\00"
@.str.3 = [2 x i8] c"r\00"
define noalias i32* @run_program(i8* %0, i8** %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = call i32 (...) @fork()
switch i32 %13, label %35 [
i32 0, label %14
i32 -1, label %31
]
14: ; preds = %12
%15 = load i32, i32* @stdout
%16 = call i32 @fileno(i32 %15)
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @dup(i32 %19)
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %18
%24 = call i32 @close(i32 %23)
%25 = call i32 @execv(i8* %0, i8** %1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %14
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %14, %12
%32 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %12
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %5
%40 = call i32* @fdopen(i32 %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret i32* %40
}