#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 execvp(char *, char **);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ print_pf_key_and_read_protected_file(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
scalar_t__ wait(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  if (argc < 2 || strcmp(argv[1], "skip-execve")) {
    puts("--- [main] Re-starting myself using execvp() ---");
    fflush(stdout);
    char *new_argv[] = {argv[0], "skip-execve", NULL};
    int ret = execvp(argv[0], new_argv);
    if (ret < 0) {
      perror("execvp error");
      return 1;
    }
  }
  int pid = fork();
  if (pid < 0) {
    perror("fork error");
    return 1;
  } else if (pid == 0) {
    if (print_pf_key_and_read_protected_file("child") < 0) {
      fprintf(stderr, "child could not read the protected file\n");
      return 1;
    }
  } else {
    if (wait(NULL) < 0) {
      perror("wait error");
      return 1;
    }
    if (print_pf_key_and_read_protected_file("parent") < 0) {
      fprintf(stderr, "parent could not read the protected file\n");
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11ce <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe41(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1240 <main+0xb0>
lea    0xe37(%rip),%rdi
call   1040 <puts@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
lea    0xe06(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <execvp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    123b <main+0xab>
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <main+0x195>
jmp    1240 <main+0xb0>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    126c <main+0xdc>
lea    0xdef(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <main+0x195>
cmpl   $0x0,-0x38(%rbp)
jne    12b2 <main+0x122>
lea    0xdd8(%rip),%rdi
call   1030 <print_pf_key_and_read_protected_file@plt>
cmp    $0x0,%rax
jge    12ad <main+0x11d>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <main+0x195>
jmp    1319 <main+0x189>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
cmp    $0x0,%rax
jge    12dd <main+0x14d>
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <main+0x195>
lea    0xdab(%rip),%rdi
call   1030 <print_pf_key_and_read_protected_file@plt>
cmp    $0x0,%rax
jge    1314 <main+0x184>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <main+0x195>
jmp    1319 <main+0x189>
jmp    131e <main+0x18e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jl     11b2 <main+0x22>
mov    0x8(%rbx),%rdi
lea    0xe58(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11f8 <main+0x68>
lea    0xe53(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    (%rbx),%rdi
mov    %rdi,(%rsp)
lea    0xe26(%rip),%rax
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rsi
call   1090 <execvp@plt>
test   %eax,%eax
js     1279 <main+0xe9>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     126b <main+0xdb>
je     1236 <main+0xa6>
xor    %ebx,%ebx
xor    %edi,%edi
call   10a0 <wait@plt>
test   %rax,%rax
js     1287 <main+0xf7>
lea    0xe75(%rip),%rdi
call   1030 <print_pf_key_and_read_protected_file@plt>
test   %rax,%rax
jns    1263 <main+0xd3>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
jmp    1259 <main+0xc9>
lea    0xe18(%rip),%rdi
call   1030 <print_pf_key_and_read_protected_file@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    1263 <main+0xd3>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdd8(%rip),%rdi
call   1080 <perror@plt>
jmp    125e <main+0xce>
lea    0xdbd(%rip),%rdi
call   1080 <perror@plt>
jmp    125e <main+0xce>
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
jmp    125e <main+0xce>

LLVM IR (-O0):
@.str = [12 x i8] c"skip-execve\00"
@.str.1 = [49 x i8] c"--- [main] Re-starting myself using execvp() ---\00"
@stdout = global i32 0
@.str.2 = [13 x i8] c"execvp error\00"
@.str.3 = [11 x i8] c"fork error\00"
@.str.4 = [6 x i8] c"child\00"
@stderr = global i32 0
@.str.5 = [41 x i8] c"child could not read the protected file\0A\00"
@.str.6 = [11 x i8] c"wait error\00"
@.str.7 = [7 x i8] c"parent\00"
@.str.8 = [42 x i8] c"parent could not read the protected file\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [3 x i8*]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %17, label %11
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %37
17: ; preds = %11, %2
%18 = call i32 @puts(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @stdout
%20 = call i32 @fflush(i32 %19)
%21 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 0
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
store i8* %24, i8** %21
%25 = getelementptr inbounds i8*, i8** %21, i64 1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %25
%26 = getelementptr inbounds i8*, i8** %25, i64 1
store i8* null, i8** %26
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 0
%29 = load i8*, i8** %28
%30 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 0
%31 = call i32 @execvp(i8* %29, i8** %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %17
%35 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %67
36: ; preds = %17
br label %37
37: ; preds = %36, %11
%38 = call i32 (...) @fork()
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %67
43: ; preds = %37
%44 = load i32, i32* %8
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %43
%47 = call i64 @print_pf_key_and_read_protected_file(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %67
52: ; preds = %46
br label %65
53: ; preds = %43
%54 = call i64 @wait(i32* null)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %67
58: ; preds = %53
%59 = call i64 @print_pf_key_and_read_protected_file(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i32, i32* @stderr
%63 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %67
64: ; preds = %58
br label %65
65: ; preds = %64, %52
br label %66
66: ; preds = %65
store i32 0, i32* %3
br label %67
67: ; preds = %66, %61, %56, %49, %41, %34
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [12 x i8] c"skip-execve\00"
@.str.1 = [49 x i8] c"--- [main] Re-starting myself using execvp() ---\00"
@stdout = global i32 0
@.str.2 = [13 x i8] c"execvp error\00"
@.str.3 = [11 x i8] c"fork error\00"
@.str.4 = [6 x i8] c"child\00"
@stderr = global i32 0
@.str.5 = [41 x i8] c"child could not read the protected file\0A\00"
@.str.6 = [11 x i8] c"wait error\00"
@.str.7 = [7 x i8] c"parent\00"
@.str.8 = [42 x i8] c"parent could not read the protected file\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i8*]
%4 = icmp slt i32 %0, 2
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %5, %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @stdout
%13 = tail call i32 @fflush(i32 %12)
%14 = bitcast [3 x i8*]* %3 to i8*
%15 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%16 = load i8*, i8** %1
store i8* %16, i8** %15
%17 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %17
%18 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
store i8* null, i8** %18
%19 = call i32 @execvp(i8* %16, i8** %15)
%20 = icmp slt i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %10
br label %24
22: ; preds = %10
%23 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %48
24: ; preds = %21, %5
%25 = call i32 (...) @fork()
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %48
29: ; preds = %24
%30 = icmp eq i32 %25, 0
br i1 %30, label %31, label %37
31: ; preds = %29
%32 = call i64 @print_pf_key_and_read_protected_file(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %48
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
br label %48
37: ; preds = %29
%38 = call i64 @wait(i32* null)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %48
42: ; preds = %37
%43 = call i64 @print_pf_key_and_read_protected_file(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.8, i64 0, i64 0))
br label %48
48: ; preds = %22, %27, %34, %40, %45, %31, %42
%49 = phi i32 [ 1, %22 ], [ 1, %27 ], [ 1, %34 ], [ 1, %40 ], [ 1, %45 ], [ 0, %31 ], [ 0, %42 ]
ret i32 %49
}