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

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int dup(int);
int /*<<< orphan*/ execv(char *const, char *const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int setenv(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ vfork();
scalar_t__ wait(int *);
int main(int argc, const char **argv, const char **envp) {
  pid_t child_pid;
  int newfd = dup(1);
  if (newfd < 0) {
    perror("dup failed");
    return 1;
  }
  char fd_argv[12];
  snprintf(fd_argv, 12, "%d", newfd);
  char *const new_argv[] = {"./exec_victim", fd_argv, NULL};
  int ret = setenv("IN_EXECVE", "1", 1);
  if (ret < 0) {
    perror("setenv failed");
    return 1;
  }
  child_pid = vfork();
  if (child_pid == 0) {
    execv(new_argv[0], new_argv);
    perror("execve failed");
    return 1;
  } else if (child_pid > 0) {
    int status;
    pid_t pid = wait(&status);
    if (pid < 0) {
      perror("wait failed");
      return 1;
    }
    if (WIFEXITED(status))
      printf("child exited with status: %d\n", WEXITSTATUS(status));
  } else {
    perror("fork failed");
    return 1;
  }
  puts("test completed successfully");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x1,%edi
call   1070 <dup@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f9 <main+0x49>
lea    0xe18(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x18c>
lea    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    $0xc,%esi
lea    0xdff(%rip),%rdx
call   1060 <snprintf@plt>
lea    0xdf6(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
lea    0xde9(%rip),%rdi
lea    0xdec(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    1269 <main+0xb9>
lea    0xdd0(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x18c>
mov    $0x0,%al
call   10a0 <vfork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12a4 <main+0xf4>
mov    -0x50(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   10d0 <execv@plt>
lea    0xda3(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x18c>
cmpq   $0x0,-0x20(%rbp)
jle    130c <main+0x15c>
lea    -0x58(%rbp),%rdi
call   10c0 <wait@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jge    12df <main+0x12f>
lea    0xd76(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x18c>
mov    -0x58(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1307 <main+0x157>
mov    -0x58(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd4e(%rip),%rdi
call   1050 <printf@plt>
jmp    1324 <main+0x174>
lea    0xd5b(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x18c>
jmp    1329 <main+0x179>
lea    0xd4a(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x40,%rsp
mov    $0x1,%ebx
mov    $0x1,%edi
call   1070 <dup@plt>
test   %eax,%eax
js     127a <main+0xca>
lea    0xe38(%rip),%rdx
lea    0x34(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
lea    0xe20(%rip),%rax
mov    %rax,0x10(%rsp)
mov    %rbx,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0xe14(%rip),%rdi
lea    0xe17(%rip),%rsi
mov    $0x1,%ebx
mov    $0x1,%edx
call   1040 <setenv@plt>
test   %eax,%eax
js     1283 <main+0xd3>
xor    %eax,%eax
call   10a0 <vfork@plt>
test   %rax,%rax
je     1291 <main+0xe1>
jle    12ab <main+0xfb>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
test   %rax,%rax
js     12b4 <main+0x104>
mov    0xc(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %rax,%rax
je     1264 <main+0xb4>
mov    0xc(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
lea    0xdf3(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0xe0f(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
ret
lea    0xd7f(%rip),%rdi
jmp    128a <main+0xda>
lea    0xd9e(%rip),%rdi
call   1090 <perror@plt>
jmp    1272 <main+0xc2>
lea    0xd76(%rip),%rdi
lea    0x10(%rsp),%rsi
call   10d0 <execv@plt>
lea    0xd8d(%rip),%rdi
jmp    12bb <main+0x10b>
lea    0xdbc(%rip),%rdi
jmp    12bb <main+0x10b>
lea    0xd89(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebx
jmp    1272 <main+0xc2>

LLVM IR (-O0):
@.str = [11 x i8] c"dup failed\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [14 x i8] c"./exec_victim\00"
@.str.3 = [10 x i8] c"IN_EXECVE\00"
@.str.4 = [2 x i8] c"1\00"
@.str.5 = [14 x i8] c"setenv failed\00"
@.str.6 = [14 x i8] c"execve failed\00"
@.str.7 = [12 x i8] c"wait failed\00"
@.str.8 = [30 x i8] c"child exited with status: %d\0A\00"
@.str.9 = [12 x i8] c"fork failed\00"
@.str.10 = [28 x i8] c"test completed successfully\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i32
%10 = alloca [12 x i8]
%11 = alloca [3 x i8*]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%15 = call i32 @dup(i32 1)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %66
20: ; preds = %3
%21 = getelementptr inbounds [12 x i8], [12 x i8]* %10, i64 0, i64 0
%22 = load i32, i32* %9
%23 = call i32 @snprintf(i8* %21, i32 12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 0
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8** %24
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = getelementptr inbounds [12 x i8], [12 x i8]* %10, i64 0, i64 0
store i8* %26, i8** %25
%27 = getelementptr inbounds i8*, i8** %25, i64 1
store i8* null, i8** %27
%28 = call i32 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
store i32 %28, i32* %12
%29 = load i32, i32* %12
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %4
br label %66
33: ; preds = %20
%34 = call i64 (...) @vfork()
store i64 %34, i64* %8
%35 = load i64, i64* %8
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 0
%39 = load i8*, i8** %38
%40 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 0
%41 = call i32 @execv(i8* %39, i8** %40)
%42 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %4
br label %66
43: ; preds = %33
%44 = load i64, i64* %8
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %61
46: ; preds = %43
%47 = call i64 @wait(i32* %13)
store i64 %47, i64* %14
%48 = load i64, i64* %14
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %4
br label %66
52: ; preds = %46
%53 = load i32, i32* %13
%54 = call i64 @WIFEXITED(i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %52
%57 = load i32, i32* %13
%58 = call i32 @WEXITSTATUS(i32 %57)
%59 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %56, %52
br label %63
61: ; preds = %43
%62 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %4
br label %66
63: ; preds = %60
br label %64
64: ; preds = %63
%65 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
store i32 0, i32* %4
br label %66
66: ; preds = %64, %61, %50, %37, %31, %18
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
@.str = [11 x i8] c"dup failed\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [14 x i8] c"./exec_victim\00"
@.str.3 = [10 x i8] c"IN_EXECVE\00"
@.str.4 = [2 x i8] c"1\00"
@.str.5 = [14 x i8] c"setenv failed\00"
@.str.6 = [14 x i8] c"execve failed\00"
@.str.7 = [12 x i8] c"wait failed\00"
@.str.8 = [30 x i8] c"child exited with status: %d\0A\00"
@.str.9 = [12 x i8] c"fork failed\00"
@.str.10 = [28 x i8] c"test completed successfully\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = alloca [12 x i8]
%5 = alloca [3 x i8*]
%6 = alloca i32
%7 = tail call i32 @dup(i32 1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %50
11: ; preds = %3
%12 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%13 = call i32 @snprintf(i8* %12, i32 12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%14 = bitcast [3 x i8*]* %5 to i8*
%15 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 1
store i8* %12, i8** %16
%17 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 2
store i8* null, i8** %17
%18 = call i32 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %48
22: ; preds = %11
%23 = call i64 (...) @vfork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @execv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8** %15)
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %48
28: ; preds = %22
%29 = icmp sgt i64 %23, 0
br i1 %29, label %30, label %44
30: ; preds = %28
%31 = bitcast i32* %6 to i8*
%32 = call i64 @wait(i32* %6)
%33 = icmp slt i64 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %30
%35 = load i32, i32* %6
%36 = call i64 @WIFEXITED(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %46, label %38
38: ; preds = %34
%39 = load i32, i32* %6
%40 = call i32 @WEXITSTATUS(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0), i32 %40)
br label %46
42: ; preds = %30
%43 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %48
44: ; preds = %28
%45 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %48
46: ; preds = %34, %38
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
br label %48
48: ; preds = %42, %46, %44, %25, %20
%49 = phi i32 [ 1, %20 ], [ 1, %25 ], [ 0, %46 ], [ 1, %42 ], [ 1, %44 ]
br label %50
50: ; preds = %48, %9
%51 = phi i32 [ 1, %9 ], [ %49, %48 ]
ret i32 %51
}