#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 int 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*/);
int 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()) == -1) {
    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    -0x18(%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    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    122d <get_heartbeat_apps+0x7d>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
je     12c7 <get_heartbeat_apps+0x117>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movslq -0x19024(%rbp),%rax
lea    -0x19020(%rbp),%rdi
imul   $0x64,%rax,%rax
add    %rax,%rdi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12b9 <get_heartbeat_apps+0x109>
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    1249 <get_heartbeat_apps+0x99>
lea    -0x10(%rbp),%rdi
call   10c0 <wait@plt>
jmp    132a <get_heartbeat_apps+0x17a>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
lea    0xd48(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edx
lea    0xd32(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   10b0 <execl@plt>
cmp    $0xffffffff,%eax
jne    1325 <get_heartbeat_apps+0x175>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
lea    0xd2b(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    132a <get_heartbeat_apps+0x17a>
mov    -0x18(%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    12bc <get_heartbeat_apps+0x10c>
xor    %r15d,%r15d
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     1266 <get_heartbeat_apps+0xb6>
cmp    $0xffffffff,%eax
je     12ce <get_heartbeat_apps+0x11e>
mov    0x4(%rsp),%edi
xor    %r15d,%r15d
xor    %esi,%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x2ddb(%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
nopl   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    12a1 <get_heartbeat_apps+0xf1>
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
mov    %rdi,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   10b0 <execl@plt>
cmp    $0xffffffff,%eax
je     12e0 <get_heartbeat_apps+0x130>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    %r15d,%eax
add    $0x19018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xd34(%rip),%rsi
jmp    12f0 <get_heartbeat_apps+0x140>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xd2f(%rip),%rsi
jmp    12f0 <get_heartbeat_apps+0x140>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%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 i32
%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 i32 (...) @fork()
store i32 %16, i32* %3
%17 = icmp eq i32 %16, -1
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 i32, i32* %3
%24 = icmp ne i32 %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 i32 (...) @fork()
switch i32 %16, label %21 [
i32 -1, label %17
i32 0, label %45
]
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = load i32, i32* %8
%23 = call i32 @dup2(i32 %22, i32 0)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* @stdin
%28 = call i64 @fgets(i8* %7, i32 100, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %42, label %30
30: ; preds = %21, %30
%31 = phi i64 [ %35, %30 ], [ 0, %21 ]
%32 = phi i8* [ %36, %30 ], [ %7, %21 ]
%33 = call i32 @atoi(i8* %32)
%34 = getelementptr inbounds i32, i32* %0, i64 %31
store i32 %33, i32* %34
%35 = add nuw nsw i64 %31, 1
%36 = getelementptr inbounds [1024 x [100 x i8]], [1024 x [100 x i8]]* %4, i64 0, i64 %35, i64 0
%37 = load i32, i32* @stdin
%38 = call i64 @fgets(i8* %36, i32 100, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %30
40: ; preds = %30
%41 = trunc i64 %35 to i32
br label %42
42: ; preds = %40, %21
%43 = phi i32 [ 0, %21 ], [ %41, %40 ]
%44 = call i32 @wait(i32* %2)
br label %58
45: ; preds = %15
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @dup2(i32 %47, i32 1)
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
%51 = call i32 @getenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%52 = 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 %51, i8* null)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %58
54: ; preds = %45
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %45, %42
%59 = phi i32 [ %43, %42 ], [ 0, %45 ]
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
ret i32 %59
}