#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ execv(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int main(void) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    exit(EXIT_FAILURE);
  }
  if ((chdir("/")) < 0) {
    exit(EXIT_FAILURE);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  while (1) {
    pid_t cid;
    cid = fork();
    if (cid == 0) {
      execv("/Users/neil/Desktop/beeps", NULL);
      exit(EXIT_FAILURE);
    } else if (cid < 0) {
      exit(EXIT_FAILURE);
    }
    waitpid(cid, NULL, 0);
    sleep(2);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11c3 <main+0x33>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11dc <main+0x4c>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1060 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1207 <main+0x77>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xdf2(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    122b <main+0x9b>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1289 <main+0xf9>
lea    0xd90(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <execv@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jge    12a2 <main+0x112>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    12a7 <main+0x117>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
jmp    1255 <main+0xc5>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jns    11ab <main+0x1b>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jne    1229 <main+0x99>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     119d <main+0xd>
lea    0xe39(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     119d <main+0xd>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1237 <main+0xa7>
test   %rax,%rax
js     119d <main+0xd>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
jmp    1200 <main+0x70>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xdc4(%rip),%rdi
xor    %esi,%esi
call   10a0 <execv@plt>
jmp    119d <main+0xd>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [26 x i8] c"/Users/neil/Desktop/beeps\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i64, i64* %2
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EXIT_SUCCESS
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %11
%18 = call i32 @umask(i32 0)
%19 = call i64 (...) @setsid()
store i64 %19, i64* %3
%20 = load i64, i64* %3
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %17
%26 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %25
%32 = load i32, i32* @STDIN_FILENO
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* @STDOUT_FILENO
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* @STDERR_FILENO
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %31, %53
%39 = call i64 (...) @fork()
store i64 %39, i64* %4
%40 = load i64, i64* %4
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = call i32 @execv(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32* null)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %38
%47 = load i64, i64* %4
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %46
br label %53
53: ; preds = %52
%54 = load i64, i64* %4
%55 = call i32 @waitpid(i64 %54, i32* null, i32 0)
%56 = call i32 @sleep(i32 2)
br label %38
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [26 x i8] c"/Users/neil/Desktop/beeps\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @EXIT_FAILURE
%5 = tail call i32 @exit(i32 %4)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %11, label %8
8: ; preds = %6
%9 = load i32, i32* @EXIT_SUCCESS
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %6
%12 = tail call i32 @umask(i32 0)
%13 = tail call i64 (...) @setsid()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %11
%19 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %18
%25 = load i32, i32* @STDIN_FILENO
%26 = tail call i32 @close(i32 %25)
%27 = load i32, i32* @STDOUT_FILENO
%28 = tail call i32 @close(i32 %27)
%29 = load i32, i32* @STDERR_FILENO
%30 = tail call i32 @close(i32 %29)
%31 = tail call i64 (...) @fork()
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %43, %24
%34 = tail call i32 @execv(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32* null)
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %24, %43
%38 = phi i64 [ %46, %43 ], [ %31, %24 ]
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @EXIT_FAILURE
%42 = tail call i32 @exit(i32 %41)
unreachable
43: ; preds = %37
%44 = tail call i32 @waitpid(i64 %38, i32* null, i32 0)
%45 = tail call i32 @sleep(i32 2)
%46 = tail call i64 (...) @fork()
%47 = icmp eq i64 %46, 0
br i1 %47, label %33, label %37
}