#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*/ O_WRONLY;
int STDOUT_FILENO;
int close(int);
int dup2(int, int);
int errno;
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
char *strerror(int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int fork_exec(char *file, char *arg1, char *arg2, char *arg3, int dev_null) {
  char *argv[5];
  pid_t pid;
  int stat;
  argv[0] = file;
  argv[1] = arg1;
  argv[2] = arg2;
  argv[3] = arg3;
  argv[4] = NULL;
  pid = fork();
  switch (pid) {
  case -1:
    fprintf(stdout, "fork failed: %s\n", strerror(errno));
    return -errno;
  case 0:
    if (dev_null) {
      int dev_null_fd;
      int rv;
      dev_null_fd = open("/dev/null", O_WRONLY);
      if (dev_null_fd < 0) {
        fprintf(stdout, "/dev/null: %s\n", strerror(errno));
        return -errno;
      }
      rv = close(STDOUT_FILENO);
      if (rv < 0) {
        fprintf(stdout, "failed to close stdout: %s\n", strerror(errno));
        return -errno;
      }
      rv = dup2(dev_null_fd, STDOUT_FILENO);
      if (rv != STDOUT_FILENO) {
        fprintf(stdout, "failed to dup /dev/null to stdout: %s\n",
                strerror(errno));
        return -errno;
      }
    }
    execvp(file, argv);
    fprintf(stdout, "exec failed: %s\n", strerror(errno));
    return -errno;
  default:
    waitpid(pid, &stat, 0);
    return 0;
  }
}


x86 assembly (clang, -O0):
<fork_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    %eax,-0x74(%rbp)
sub    $0xffffffff,%eax
je     11f5 <fork_exec+0x75>
jmp    11e5 <fork_exec+0x65>
mov    -0x74(%rbp),%eax
test   %eax,%eax
je     1234 <fork_exec+0xb4>
jmp    13bb <fork_exec+0x23b>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x78(%rbp),%edi
mov    %rax,%rdx
lea    0xde4(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2dae(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13d0 <fork_exec+0x250>
cmpl   $0x0,-0x2c(%rbp)
je     1369 <fork_exec+0x1e9>
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
lea    0xdc3(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
jge    129f <fork_exec+0x11f>
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    %rax,%rdx
lea    0xd94(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2d43(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13d0 <fork_exec+0x250>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0x70(%rbp)
jge    12f9 <fork_exec+0x179>
mov    0x2d0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x80(%rbp)
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x80(%rbp),%edi
mov    %rax,%rdx
lea    0xd49(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2ce9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13d0 <fork_exec+0x250>
mov    -0x6c(%rbp),%edi
mov    0x2ced(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    %eax,-0x70(%rbp)
mov    -0x70(%rbp),%eax
mov    0x2cd9(%rip),%rcx
cmp    (%rcx),%eax
je     1364 <fork_exec+0x1e4>
mov    0x2caa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x84(%rbp),%edi
mov    %rax,%rdx
lea    0xcfa(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2c7e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13d0 <fork_exec+0x250>
jmp    1369 <fork_exec+0x1e9>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1080 <execvp@plt>
mov    0x2c53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x88(%rbp)
mov    0x2c4c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x88(%rbp),%edi
mov    %rax,%rdx
lea    0xcca(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2c27(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13d0 <fork_exec+0x250>
mov    -0x64(%rbp),%edi
lea    -0x68(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_exec>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %r8d,%ebp
mov    %rdi,%rbx
mov    %rdi,0x10(%rsp)
mov    %rsi,0x18(%rsp)
mov    %rdx,0x20(%rsp)
mov    %rcx,0x28(%rsp)
movq   $0x0,0x30(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11e3 <fork_exec+0x63>
cmp    $0xffffffff,%eax
jne    1256 <fork_exec+0xd6>
mov    0x2e0a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e09(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
lea    0xe22(%rip),%rsi
jmp    12c9 <fork_exec+0x149>
test   %ebp,%ebp
je     1228 <fork_exec+0xa8>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0xe1a(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1268 <fork_exec+0xe8>
mov    %eax,%ebp
mov    0x2de7(%rip),%r14
mov    (%r14),%edi
call   1040 <close@plt>
test   %eax,%eax
js     1289 <fork_exec+0x109>
mov    (%r14),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
cmp    (%r14),%eax
jne    12aa <fork_exec+0x12a>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <execvp@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%ebx
mov    0x2d93(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
lea    0xe19(%rip),%rsi
jmp    12c9 <fork_exec+0x149>
lea    0xc(%rsp),%rsi
xor    %ebx,%ebx
mov    %eax,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
jmp    12d8 <fork_exec+0x158>
mov    0x2d61(%rip),%rax
mov    (%rax),%ebx
mov    0x2d60(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
lea    0xd94(%rip),%rsi
jmp    12c9 <fork_exec+0x149>
mov    0x2d40(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3f(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
lea    0xd82(%rip),%rsi
jmp    12c9 <fork_exec+0x149>
mov    0x2d1f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d1e(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
lea    0xd7d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
xor    %ebx,%ebx
sub    0x0(%rbp),%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [17 x i8] c"fork failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@.str.2 = [15 x i8] c"/dev/null: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [28 x i8] c"failed to close stdout: %s\0A\00"
@.str.4 = [39 x i8] c"failed to dup /dev/null to stdout: %s\0A\00"
@.str.5 = [17 x i8] c"exec failed: %s\0A\00"
define i32 @fork_exec(i8* %0, i8* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca [5 x i8*]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%17 = load i8*, i8** %7
%18 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i64 0, i64 0
store i8* %17, i8** %18
%19 = load i8*, i8** %8
%20 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i64 0, i64 1
store i8* %19, i8** %20
%21 = load i8*, i8** %9
%22 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i64 0, i64 2
store i8* %21, i8** %22
%23 = load i8*, i8** %10
%24 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i64 0, i64 3
store i8* %23, i8** %24
%25 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i64 0, i64 4
store i8* null, i8** %25
%26 = call i32 (...) @fork()
store i32 %26, i32* %13
%27 = load i32, i32* %13
switch i32 %27, label %87 [
i32 -1, label %28
i32 0, label %35
]
28: ; preds = %5
%29 = load i32, i32* @stdout
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @errno
%34 = sub nsw i32 0, %33
store i32 %34, i32* %6
br label %90
35: ; preds = %5
%36 = load i32, i32* %11
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %77
38: ; preds = %35
%39 = load i32, i32* @O_WRONLY
%40 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %39)
store i32 %40, i32* %15
%41 = load i32, i32* %15
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %38
%44 = load i32, i32* @stdout
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = load i32, i32* @errno
%49 = sub nsw i32 0, %48
store i32 %49, i32* %6
br label %90
50: ; preds = %38
%51 = load i32, i32* @STDOUT_FILENO
%52 = call i32 @close(i32 %51)
store i32 %52, i32* %16
%53 = load i32, i32* %16
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %62
55: ; preds = %50
%56 = load i32, i32* @stdout
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %58)
%60 = load i32, i32* @errno
%61 = sub nsw i32 0, %60
store i32 %61, i32* %6
br label %90
62: ; preds = %50
%63 = load i32, i32* %15
%64 = load i32, i32* @STDOUT_FILENO
%65 = call i32 @dup2(i32 %63, i32 %64)
store i32 %65, i32* %16
%66 = load i32, i32* %16
%67 = load i32, i32* @STDOUT_FILENO
%68 = icmp ne i32 %66, %67
br i1 %68, label %69, label %76
69: ; preds = %62
%70 = load i32, i32* @stdout
%71 = load i32, i32* @errno
%72 = call i8* @strerror(i32 %71)
%73 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %72)
%74 = load i32, i32* @errno
%75 = sub nsw i32 0, %74
store i32 %75, i32* %6
br label %90
76: ; preds = %62
br label %77
77: ; preds = %76, %35
%78 = load i8*, i8** %7
%79 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i64 0, i64 0
%80 = call i32 @execvp(i8* %78, i8** %79)
%81 = load i32, i32* @stdout
%82 = load i32, i32* @errno
%83 = call i8* @strerror(i32 %82)
%84 = call i32 @fprintf(i32 %81, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %83)
%85 = load i32, i32* @errno
%86 = sub nsw i32 0, %85
store i32 %86, i32* %6
br label %90
87: ; preds = %5
%88 = load i32, i32* %13
%89 = call i32 @waitpid(i32 %88, i32* %14, i32 0)
store i32 0, i32* %6
br label %90
90: ; preds = %87, %77, %69, %55, %43, %28
%91 = load i32, i32* %6
ret i32 %91
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [17 x i8] c"fork failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@.str.2 = [15 x i8] c"/dev/null: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [28 x i8] c"failed to close stdout: %s\0A\00"
@.str.4 = [39 x i8] c"failed to dup /dev/null to stdout: %s\0A\00"
@.str.5 = [17 x i8] c"exec failed: %s\0A\00"
define i32 @fork_exec(i8* %0, i8* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca [5 x i8*]
%7 = alloca i32
%8 = bitcast [5 x i8*]* %6 to i8*
%9 = bitcast i32* %7 to i8*
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 0
store i8* %0, i8** %10
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 1
store i8* %1, i8** %11
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 2
store i8* %2, i8** %12
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 3
store i8* %3, i8** %13
%14 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 4
store i8* null, i8** %14
%15 = tail call i32 (...) @fork()
switch i32 %15, label %67 [
i32 -1, label %16
i32 0, label %23
]
16: ; preds = %5
%17 = load i32, i32* @stdout
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
br label %69
23: ; preds = %5
%24 = icmp eq i32 %4, 0
br i1 %24, label %59, label %25
25: ; preds = %23
%26 = load i32, i32* @O_WRONLY
%27 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %25
%30 = load i32, i32* @stdout
%31 = load i32, i32* @errno
%32 = tail call i8* @strerror(i32 %31)
%33 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
br label %69
36: ; preds = %25
%37 = load i32, i32* @STDOUT_FILENO
%38 = tail call i32 @close(i32 %37)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %36
%41 = load i32, i32* @stdout
%42 = load i32, i32* @errno
%43 = tail call i8* @strerror(i32 %42)
%44 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = load i32, i32* @errno
%46 = sub nsw i32 0, %45
br label %69
47: ; preds = %36
%48 = load i32, i32* @STDOUT_FILENO
%49 = tail call i32 @dup2(i32 %27, i32 %48)
%50 = load i32, i32* @STDOUT_FILENO
%51 = icmp eq i32 %49, %50
br i1 %51, label %59, label %52
52: ; preds = %47
%53 = load i32, i32* @stdout
%54 = load i32, i32* @errno
%55 = tail call i8* @strerror(i32 %54)
%56 = tail call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @errno
%58 = sub nsw i32 0, %57
br label %69
59: ; preds = %47, %23
%60 = call i32 @execvp(i8* %0, i8** %10)
%61 = load i32, i32* @stdout
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %63)
%65 = load i32, i32* @errno
%66 = sub nsw i32 0, %65
br label %69
67: ; preds = %5
%68 = call i32 @waitpid(i32 %15, i32* %7, i32 0)
br label %69
69: ; preds = %29, %40, %52, %67, %59, %16
%70 = phi i32 [ 0, %67 ], [ %66, %59 ], [ %22, %16 ], [ %35, %29 ], [ %46, %40 ], [ %58, %52 ]
ret i32 %70
}