#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WNOHANG;
int WTERMSIG(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ do_read(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ send_output(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int run_program(char **argv) {
  int fds[2];
  int status, rc, pid;
  char buffer[80];
  if (pipe(fds) < 0) {
    perror("pipe");
    exit(1);
  }
  pid = fork();
  if (pid < 0) {
    perror("vfork");
    exit(1);
  }
  if (pid == 0) {
    dup2(fds[1], 1);
    dup2(fds[1], 2);
    close(fds[0]);
    execvp(argv[0], argv);
    perror(argv[0]);
    exit(1);
  }
  close(fds[1]);
  while (!(waitpid(pid, &status, WNOHANG))) {
    do_read(fds[0]);
  }
  do_read(fds[0]);
  close(fds[0]);
  if (WIFEXITED(status)) {
    rc = WEXITSTATUS(status);
    if (rc) {
      send_output(argv[0], 0, 0x03);
      sprintf(buffer, " died with exit status %d\n", rc);
      send_output(buffer, 0, 0x03);
    }
  } else {
    if (WIFSIGNALED(status)) {
      send_output(argv[0], 0, 0x03);
      sprintf(buffer, "died with signal %d\n", WTERMSIG(status));
      send_output(buffer, 0, 0x03);
      rc = 1;
    }
    rc = 0;
  }
  return rc;
}


x86 assembly (clang, -O0):
<run_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    1228 <run_program+0x38>
lea    0xde7(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1252 <run_program+0x62>
lea    0xdc2(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12a4 <run_program+0xb4>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   10d0 <execvp@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rsi
call   10b0 <waitpid@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12d6 <run_program+0xe6>
jmp    12e3 <run_program+0xf3>
mov    -0x10(%rbp),%edi
call   1030 <do_read@plt>
jmp    12ac <run_program+0xbc>
mov    -0x10(%rbp),%edi
call   1030 <do_read@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1355 <run_program+0x165>
mov    -0x14(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1350 <run_program+0x160>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1090 <send_output@plt>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xcd0(%rip),%rsi
call   10f0 <sprintf@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1090 <send_output@plt>
jmp    13ba <run_program+0x1ca>
mov    -0x14(%rbp),%edi
call   1070 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     13b3 <run_program+0x1c3>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1090 <send_output@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%edx
lea    0xc8f(%rip),%rsi
call   10f0 <sprintf@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1090 <send_output@plt>
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_program>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     1311 <run_program+0x121>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
js     131a <run_program+0x12a>
mov    0xc(%rsp),%edi
je     1323 <run_program+0x133>
mov    %eax,%ebp
call   1050 <close@plt>
mov    0x2db1(%rip),%r15
mov    (%r15),%edx
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
call   10b0 <waitpid@plt>
test   %eax,%eax
jne    126a <run_program+0x7a>
lea    0x4(%rsp),%rbx
nop
mov    0x8(%rsp),%edi
call   1030 <do_read@plt>
mov    (%r15),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <waitpid@plt>
test   %eax,%eax
je     1250 <run_program+0x60>
mov    0x8(%rsp),%edi
call   1030 <do_read@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
call   10e0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     12b8 <run_program+0xc8>
call   10a0 <WEXITSTATUS@plt>
test   %eax,%eax
je     1302 <run_program+0x112>
mov    %eax,%ebp
mov    (%r14),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1090 <send_output@plt>
lea    0xd5c(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %ebp,%edx
jmp    12ea <run_program+0xfa>
call   1070 <WIFSIGNALED@plt>
test   %rax,%rax
je     1302 <run_program+0x112>
mov    (%r14),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x3,%edx
call   1090 <send_output@plt>
mov    0x4(%rsp),%edi
call   1080 <WTERMSIG@plt>
lea    0xd43(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %eax,%edx
call   10f0 <sprintf@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1090 <send_output@plt>
jmp    1304 <run_program+0x114>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xce8(%rip),%rdi
jmp    1352 <run_program+0x162>
lea    0xce4(%rip),%rdi
jmp    1352 <run_program+0x162>
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   10d0 <execvp@plt>
mov    (%r14),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [6 x i8] c"vfork\00"
@WNOHANG = global i32 0
@.str.2 = [27 x i8] c" died with exit status %d\0A\00"
@.str.3 = [21 x i8] c"died with signal %d\0A\00"
define i32 @run_program(i8** %0) {
%2 = alloca i8**
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [80 x i8]
store i8** %0, i8*** %2
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = call i32 (...) @fork()
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %44
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @dup2(i32 %26, i32 1)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @dup2(i32 %29, i32 2)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = load i8**, i8*** %2
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = load i8**, i8*** %2
%38 = call i32 @execvp(i8* %36, i8** %37)
%39 = load i8**, i8*** %2
%40 = getelementptr inbounds i8*, i8** %39, i64 0
%41 = load i8*, i8** %40
%42 = call i32 @perror(i8* %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %21
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %54, %44
%49 = load i32, i32* %6
%50 = load i32, i32* @WNOHANG
%51 = call i32 @waitpid(i32 %49, i32* %4, i32 %50)
%52 = icmp ne i32 %51, 0
%53 = xor i1 %52, true
br i1 %53, label %54, label %58
54: ; preds = %48
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32 @do_read(i32 %56)
br label %48
58: ; preds = %48
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%60 = load i32, i32* %59
%61 = call i32 @do_read(i32 %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %4
%66 = call i64 @WIFEXITED(i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %84
68: ; preds = %58
%69 = load i32, i32* %4
%70 = call i32 @WEXITSTATUS(i32 %69)
store i32 %70, i32* %5
%71 = load i32, i32* %5
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %83
73: ; preds = %68
%74 = load i8**, i8*** %2
%75 = getelementptr inbounds i8*, i8** %74, i64 0
%76 = load i8*, i8** %75
%77 = call i32 @send_output(i8* %76, i32 0, i32 3)
%78 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%79 = load i32, i32* %5
%80 = call i32 @sprintf(i8* %78, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %79)
%81 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%82 = call i32 @send_output(i8* %81, i32 0, i32 3)
br label %83
83: ; preds = %73, %68
br label %100
84: ; preds = %58
%85 = load i32, i32* %4
%86 = call i64 @WIFSIGNALED(i32 %85)
%87 = icmp ne i64 %86, 0
br i1 %87, label %88, label %99
88: ; preds = %84
%89 = load i8**, i8*** %2
%90 = getelementptr inbounds i8*, i8** %89, i64 0
%91 = load i8*, i8** %90
%92 = call i32 @send_output(i8* %91, i32 0, i32 3)
%93 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%94 = load i32, i32* %4
%95 = call i32 @WTERMSIG(i32 %94)
%96 = call i32 @sprintf(i8* %93, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %95)
%97 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%98 = call i32 @send_output(i8* %97, i32 0, i32 3)
store i32 1, i32* %5
br label %99
99: ; preds = %88, %84
store i32 0, i32* %5
br label %100
100: ; preds = %99, %83
%101 = load i32, i32* %5
ret i32 %101
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [6 x i8] c"vfork\00"
@WNOHANG = global i32 0
@.str.2 = [27 x i8] c" died with exit status %d\0A\00"
@.str.3 = [21 x i8] c"died with signal %d\0A\00"
define i32 @run_program(i8** %0) {
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [80 x i8]
%5 = bitcast [2 x i32]* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = call i32 (...) @fork()
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = icmp eq i32 %15, 0
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%23 = load i32, i32* %22
br i1 %21, label %24, label %35
24: ; preds = %20
%25 = call i32 @dup2(i32 %23, i32 1)
%26 = load i32, i32* %22
%27 = call i32 @dup2(i32 %26, i32 2)
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
%30 = load i8*, i8** %0
%31 = call i32 @execvp(i8* %30, i8** %0)
%32 = load i8*, i8** %0
%33 = call i32 @perror(i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %20
%36 = call i32 @close(i32 %23)
%37 = load i32, i32* @WNOHANG
%38 = call i32 @waitpid(i32 %15, i32* %3, i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %35, %40
%41 = load i32, i32* %8
%42 = call i32 @do_read(i32 %41)
%43 = load i32, i32* @WNOHANG
%44 = call i32 @waitpid(i32 %15, i32* %3, i32 %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %40, label %46
46: ; preds = %40, %35
%47 = load i32, i32* %8
%48 = call i32 @do_read(i32 %47)
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %3
%52 = call i64 @WIFEXITED(i32 %51)
%53 = icmp eq i64 %52, 0
%54 = load i32, i32* %3
br i1 %53, label %62, label %55
55: ; preds = %46
%56 = call i32 @WEXITSTATUS(i32 %54)
%57 = icmp eq i32 %56, 0
br i1 %57, label %74, label %58
58: ; preds = %55
%59 = load i8*, i8** %0
%60 = call i32 @send_output(i8* %59, i32 0, i32 3)
%61 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %56)
br label %71
62: ; preds = %46
%63 = call i64 @WIFSIGNALED(i32 %54)
%64 = icmp eq i64 %63, 0
br i1 %64, label %74, label %65
65: ; preds = %62
%66 = load i8*, i8** %0
%67 = call i32 @send_output(i8* %66, i32 0, i32 3)
%68 = load i32, i32* %3
%69 = call i32 @WTERMSIG(i32 %68)
%70 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %58, %65
%72 = phi i32 [ 0, %65 ], [ %56, %58 ]
%73 = call i32 @send_output(i8* %7, i32 0, i32 3)
br label %74
74: ; preds = %71, %62, %55
%75 = phi i32 [ 0, %55 ], [ 0, %62 ], [ %72, %71 ]
ret i32 %75
}