#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 EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int monitor(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *const, char *);
int target(char *const *);
int main(int argc, char *const *argv) {
  for (int i = 0; i < argc; ++i) {
    if (strcmp(argv[i], "--version") == 0) {
      printf("Build Monitor 0.0.1-unknown\n");
      return EXIT_SUCCESS;
    }
  }
  if (argc < 2) {
    fprintf(stderr, "Expected a program name as argument\n");
    return EXIT_FAILURE;
  }
  pid_t pid;
  pid = fork();
  if (pid == 0) {
    return target(&argv[1]);
  } else if (pid > 0) {
    return monitor(pid);
  } else {
    perror("fork");
    return EXIT_FAILURE;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11eb <main+0x7b>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe54(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11d8 <main+0x68>
lea    0xe48(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <main+0x10a>
jmp    11dd <main+0x6d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    118d <main+0x1d>
cmpl   $0x2,-0x8(%rbp)
jge    121b <main+0xab>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <main+0x10a>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1246 <main+0xd6>
mov    -0x10(%rbp),%rdi
add    $0x8,%rdi
call   1050 <target@plt>
mov    %eax,-0x4(%rbp)
jmp    127a <main+0x10a>
cmpq   $0x0,-0x20(%rbp)
jle    1262 <main+0xf2>
mov    -0x20(%rbp),%rdi
call   1080 <monitor@plt>
mov    %eax,-0x4(%rbp)
jmp    127a <main+0x10a>
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edi,%edi
jle    11b0 <main+0x40>
mov    %rsi,%r12
mov    %edi,%r14d
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0xe73(%rip),%r15
nopl   (%rax)
mov    (%r12,%rbx,8),%rdi
mov    %r15,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11ce <main+0x5e>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1190 <main+0x20>
cmp    $0x1,%r14d
jg     11ec <main+0x7c>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e0c(%rip),%rax
jmp    11e1 <main+0x71>
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     120a <main+0x9a>
jle    121e <main+0xae>
mov    %rax,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <monitor@plt>
add    $0x8,%r12
mov    %r12,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <target@plt>
lea    0xe0a(%rip),%rdi
call   1070 <perror@plt>
jmp    11c5 <main+0x55>

LLVM IR (-O0):
@.str = [10 x i8] c"--version\00"
@.str.1 = [29 x i8] c"Build Monitor 0.0.1-unknown\0A\00"
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str.2 = [37 x i8] c"Expected a program name as argument\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [5 x i8] c"fork\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
br label %8
8: ; preds = %24, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %27
12: ; preds = %8
%13 = load i8**, i8*** %5
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8*, i8** %13, i64 %15
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_SUCCESS
store i32 %22, i32* %3
br label %51
23: ; preds = %12
br label %24
24: ; preds = %23
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
store i32 %26, i32* %6
br label %8
27: ; preds = %8
%28 = load i32, i32* %4
%29 = icmp slt i32 %28, 2
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
store i32 %33, i32* %3
br label %51
34: ; preds = %27
%35 = call i64 (...) @fork()
store i64 %35, i64* %7
%36 = load i64, i64* %7
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = call i32 @target(i8** %40)
store i32 %41, i32* %3
br label %51
42: ; preds = %34
%43 = load i64, i64* %7
%44 = icmp sgt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i64, i64* %7
%47 = call i32 @monitor(i64 %46)
store i32 %47, i32* %3
br label %51
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
store i32 %50, i32* %3
br label %51
51: ; preds = %48, %45, %38, %30, %20
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [10 x i8] c"--version\00"
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str.2 = [37 x i8] c"Expected a program name as argument\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [5 x i8] c"fork\00"
@str = [28 x i8] c"Build Monitor 0.0.1-unknown\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %20
4: ; preds = %2
%5 = zext i32 %0 to i64
br label %8
6: ; preds = %8
%7 = icmp eq i64 %14, %5
br i1 %7, label %18, label %8
8: ; preds = %4, %6
%9 = phi i64 [ 0, %4 ], [ %14, %6 ]
%10 = getelementptr inbounds i8*, i8** %1, i64 %9
%11 = load i8*, i8** %10
%12 = tail call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
%14 = add nuw nsw i64 %9, 1
br i1 %13, label %15, label %6
15: ; preds = %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_SUCCESS
br label %37
18: ; preds = %6
%19 = icmp slt i32 %0, 2
br i1 %19, label %20, label %24
20: ; preds = %2, %18
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
br label %37
24: ; preds = %18
%25 = tail call i64 (...) @fork()
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = tail call i32 @target(i8** %28)
br label %37
30: ; preds = %24
%31 = icmp sgt i64 %25, 0
br i1 %31, label %32, label %34
32: ; preds = %30
%33 = tail call i32 @monitor(i64 %25)
br label %37
34: ; preds = %30
%35 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
br label %37
37: ; preds = %15, %27, %32, %34, %20
%38 = phi i32 [ %17, %15 ], [ %23, %20 ], [ %29, %27 ], [ %33, %32 ], [ %36, %34 ]
ret i32 %38
}