#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 /*<<< orphan*/ app(char *, int /*<<< orphan*/);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ *getenv(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ sys(char *, int /*<<< orphan*/);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("usage:\n");
    printf("  application num_beats, log_file\n");
    return -1;
  }
  if (getenv("HEARTBEAT_ENABLED_DIR") == NULL) {
    fprintf(stderr, "ERROR: need to define environment variable "
                    "HEARTBEAT_ENABLED_DIR (see README)\n");
    return 1;
  }
  int rv;
  pid_t pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork error. Exiting.\n");
    exit(1);
  }
  if (pid == 0) {
    sys(argv[2], atoi(argv[1]));
    return 0;
  } else {
    app(argv[2], atoi(argv[1]));
    wait(&rv);
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11d4 <main+0x44>
lea    0xe49(%rip),%rdi
call   1040 <printf@plt>
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x11f>
lea    0xe50(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
jne    120b <main+0x7b>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12af <main+0x11f>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1240 <main+0xb0>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    127b <main+0xeb>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
call   10b0 <sys@plt>
movl   $0x0,-0x4(%rbp)
jmp    12af <main+0x11f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1060 <app@plt>
lea    -0x14(%rbp),%rdi
call   1090 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    11e4 <main+0x54>
mov    %rsi,%rbx
lea    0xe5d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1203 <main+0x73>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     122e <main+0x9e>
mov    %rax,%r14
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rbx
call   1070 <atoi@plt>
mov    %rbx,%rdi
mov    %eax,%esi
test   %r14,%r14
je     121f <main+0x8f>
call   1060 <app@plt>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
jmp    1224 <main+0x94>
lea    0xe90(%rip),%rdi
call   1040 <puts@plt>
lea    0xe8b(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
jmp    1226 <main+0x96>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    1226 <main+0x96>
call   10b0 <sys@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"usage:\0A\00"
@.str.1 = [35 x i8] c" application num_beats, log_file\0A\00"
@.str.2 = [22 x i8] c"HEARTBEAT_ENABLED_DIR\00"
@stderr = global i32 0
@.str.3 = [79 x i8] c"ERROR: need to define environment variable HEARTBEAT_ENABLED_DIR (see README)\0A\00"
@.str.4 = [22 x i8] c"Fork error. Exiting.\0A\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
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 3
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %49
13: ; preds = %2
%14 = call i32* @getenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %49
19: ; preds = %13
%20 = call i64 (...) @fork()
store i64 %20, i64* %7
%21 = load i64, i64* %7
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = load i64, i64* %7
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %27
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
%38 = call i32 @sys(i8* %33, i32 %37)
store i32 0, i32* %3
br label %49
39: ; preds = %27
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = call i32 @atoi(i8* %45)
%47 = call i32 @app(i8* %42, i32 %46)
%48 = call i32 @wait(i32* %6)
store i32 0, i32* %3
br label %49
49: ; preds = %39, %30, %16, %10
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str.2 = [22 x i8] c"HEARTBEAT_ENABLED_DIR\00"
@stderr = global i32 0
@.str.3 = [79 x i8] c"ERROR: need to define environment variable HEARTBEAT_ENABLED_DIR (see README)\0A\00"
@.str.4 = [22 x i8] c"Fork error. Exiting.\0A\00"
@str = [7 x i8] c"usage:\00"
@str.5 = [34 x i8] c" application num_beats, log_file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 3
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.5, i64 0, i64 0))
br label %35
8: ; preds = %2
%9 = tail call i32* @getenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0))
br label %35
14: ; preds = %8
%15 = bitcast i32* %3 to i8*
%16 = tail call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = icmp eq i64 %16, 0
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = tail call i32 @atoi(i8* %27)
br i1 %23, label %29, label %31
29: ; preds = %22
%30 = tail call i32 @sys(i8* %25, i32 %28)
br label %34
31: ; preds = %22
%32 = tail call i32 @app(i8* %25, i32 %28)
%33 = call i32 @wait(i32* %3)
br label %34
34: ; preds = %31, %29
br label %35
35: ; preds = %34, %11, %5
%36 = phi i32 [ -1, %5 ], [ 1, %11 ], [ 0, %34 ]
ret i32 %36
}