#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 atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int execl(char *, char *, int /*<<< orphan*/, char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getenv(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ wait(int *);
int get_heartbeat_apps(int *apps) {
  pid_t pid;
  int rv;
  int commpipe[2];
  char string[1024][100];
  int count = 0;
  if (pipe(commpipe)) {
    fprintf(stderr, "Pipe error!\n");
    exit(1);
  }
  if ((pid = fork()) < 0) {
    fprintf(stderr, "Fork error. Exiting.\n");
    exit(1);
  }
  if (pid) {
    dup2(commpipe[0], 0);
    close(commpipe[1]);
    while (fgets(string[count], 100, stdin)) {
      apps[count] = atoi(string[count]);
      count++;
    }
    wait(&rv);
  } else {
    dup2(commpipe[1], 1);
    close(commpipe[0]);
    if (execl("/bin/ls", "/bin/ls", getenv("HEARTBEAT_ENABLED_DIR"),
              (char *)NULL) == -1) {
      fprintf(stderr, "execl Error!");
      exit(1);
    }
  }
  close(commpipe[0]);
  return count;
}


x86 assembly (clang, -O0):
<get_heartbeat_apps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x19030,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x19024(%rbp)
lea    -0x1c(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11fb <get_heartbeat_apps+0x4b>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    122f <get_heartbeat_apps+0x7f>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     12ca <get_heartbeat_apps+0x11a>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movslq -0x19024(%rbp),%rax
lea    -0x19020(%rbp),%rdi
imul   $0x64,%rax,%rax
add    %rax,%rdi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12bc <get_heartbeat_apps+0x10c>
movslq -0x19024(%rbp),%rax
lea    -0x19020(%rbp),%rdi
imul   $0x64,%rax,%rax
add    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
movslq -0x19024(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x19024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x19024(%rbp)
jmp    124c <get_heartbeat_apps+0x9c>
lea    -0x14(%rbp),%rdi
call   10c0 <wait@plt>
jmp    132d <get_heartbeat_apps+0x17d>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
lea    0xd45(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edx
lea    0xd2f(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   10b0 <execl@plt>
cmp    $0xffffffff,%eax
jne    1328 <get_heartbeat_apps+0x178>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
lea    0xd28(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    132d <get_heartbeat_apps+0x17d>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x19024(%rbp),%eax
add    $0x19030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_heartbeat_apps>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x19018,%rsp
mov    %rdi,%r14
lea    0x4(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    12bf <get_heartbeat_apps+0x10f>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12d1 <get_heartbeat_apps+0x121>
je     1266 <get_heartbeat_apps+0xb6>
mov    0x4(%rsp),%edi
xor    %r15d,%r15d
xor    %esi,%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x2de0(%rip),%r12
mov    (%r12),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     125a <get_heartbeat_apps+0xaa>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <atoi@plt>
mov    %eax,(%r14,%r15,4)
add    $0x1,%r15
add    $0x64,%rbx
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1070 <fgets@plt>
test   %rax,%rax
jne    1230 <get_heartbeat_apps+0x80>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
jmp    12a4 <get_heartbeat_apps+0xf4>
mov    0x8(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
lea    0xda7(%rip),%rdi
call   1030 <getenv@plt>
lea    0xd93(%rip),%rdi
xor    %r15d,%r15d
mov    %rdi,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   10b0 <execl@plt>
cmp    $0xffffffff,%eax
je     12e3 <get_heartbeat_apps+0x133>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    %r15d,%eax
add    $0x19018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
lea    0xd31(%rip),%rsi
jmp    12f3 <get_heartbeat_apps+0x143>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    0xd2c(%rip),%rsi
jmp    12f3 <get_heartbeat_apps+0x143>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"Pipe error!\0A\00"
@.str.1 = [22 x i8] c"Fork error. Exiting.\0A\00"
@stdin = global i32 0
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [22 x i8] c"HEARTBEAT_ENABLED_DIR\00"
@.str.4 = [13 x i8] c"execl Error!\00"
define i32 @get_heartbeat_apps(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca [1024 x [100 x i8]]
%7 = alloca i32
store i32* %0, i32** %2
store i32 0, i32* %7
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i64 (...) @fork()
store i64 %16, i64* %3
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = load i64, i64* %3
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %54
25: ; preds = %22
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @dup2(i32 %27, i32 0)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %40, %25
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1024 x [100 x i8]], [1024 x [100 x i8]]* %6, i64 0, i64 %34
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %35, i64 0, i64 0
%37 = load i32, i32* @stdin
%38 = call i64 @fgets(i8* %36, i32 100, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %52
40: ; preds = %32
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1024 x [100 x i8]], [1024 x [100 x i8]]* %6, i64 0, i64 %42
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %43, i64 0, i64 0
%45 = call i32 @atoi(i8* %44)
%46 = load i32*, i32** %2
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
store i32 %45, i32* %49
%50 = load i32, i32* %7
%51 = add nsw i32 %50, 1
store i32 %51, i32* %7
br label %32
52: ; preds = %32
%53 = call i32 @wait(i32* %4)
br label %69
54: ; preds = %22
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @dup2(i32 %56, i32 1)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = call i32 @getenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%62 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %61, i8* null)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %68
64: ; preds = %54
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %54
br label %69
69: ; preds = %68, %52
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %7
ret i32 %73
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c"Pipe error!\0A\00"
@.str.1 = [22 x i8] c"Fork error. Exiting.\0A\00"
@stdin = global i32 0
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [22 x i8] c"HEARTBEAT_ENABLED_DIR\00"
@.str.4 = [13 x i8] c"execl Error!\00"
define i32 @get_heartbeat_apps(i32* nocapture writeonly %0) {
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca [1024 x [100 x i8]]
%5 = bitcast i32* %2 to i8*
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [1024 x [100 x i8]], [1024 x [100 x i8]]* %4, i64 0, i64 0, i64 0
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = icmp eq i64 %16, 0
br i1 %23, label %48, label %24
24: ; preds = %22
%25 = load i32, i32* %8
%26 = call i32 @dup2(i32 %25, i32 0)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* @stdin
%31 = call i64 @fgets(i8* %7, i32 100, i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %45, label %33
33: ; preds = %24, %33
%34 = phi i64 [ %38, %33 ], [ 0, %24 ]
%35 = phi i8* [ %39, %33 ], [ %7, %24 ]
%36 = call i32 @atoi(i8* %35)
%37 = getelementptr inbounds i32, i32* %0, i64 %34
store i32 %36, i32* %37
%38 = add nuw nsw i64 %34, 1
%39 = getelementptr inbounds [1024 x [100 x i8]], [1024 x [100 x i8]]* %4, i64 0, i64 %38, i64 0
%40 = load i32, i32* @stdin
%41 = call i64 @fgets(i8* %39, i32 100, i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %33
43: ; preds = %33
%44 = trunc i64 %38 to i32
br label %45
45: ; preds = %43, %24
%46 = phi i32 [ 0, %24 ], [ %44, %43 ]
%47 = call i32 @wait(i32* %2)
br label %61
48: ; preds = %22
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @dup2(i32 %50, i32 1)
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
%54 = call i32 @getenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %54, i8* null)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %61
57: ; preds = %48
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %48, %45
%62 = phi i32 [ %46, %45 ], [ 0, %48 ]
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
ret i32 %62
}