#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*/ LOG_INFO;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ LOG_USER;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ closelog();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  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);
  int i = 3;
  openlog(*argv, LOG_PID, LOG_USER);
  syslog(LOG_INFO, "Starting the big loop.\n");
  while (i--) {
    syslog(LOG_INFO, "Big loop iterations left: %d\n", i);
    sleep(5);
  }
  syslog(LOG_INFO, "Big loop ended. Closing daemon.");
  closelog();
  exit(EXIT_SUCCESS);
}


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)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11da <main+0x3a>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    11f3 <main+0x53>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    121e <main+0x7e>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xddb(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1242 <main+0xa2>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
movl   $0x3,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
call   1090 <openlog@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x24(%rbp)
cmp    $0x0,%eax
je     12e5 <main+0x145>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xd4b(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
jmp    12a8 <main+0x108>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
lea    0xd43(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     11dd <main+0x3d>
jne    12b2 <main+0x112>
xor    %edi,%edi
call   1070 <umask@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     11dd <main+0x3d>
lea    0xe2d(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jns    11eb <main+0x4b>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    (%rbx),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
call   1090 <openlog@plt>
mov    0x2d7a(%rip),%rbx
mov    (%rbx),%edi
lea    0xdc3(%rip),%rsi
xor    %eax,%eax
call   1080 <syslog@plt>
mov    (%rbx),%edi
lea    0xdcb(%rip),%r14
mov    %r14,%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    (%rbx),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    (%rbx),%edi
lea    0xd94(%rip),%rsi
xor    %eax,%eax
call   1080 <syslog@plt>
xor    %eax,%eax
call   1030 <closelog@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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
@LOG_PID = global i32 0
@LOG_USER = global i32 0
@LOG_INFO = global i32 0
@.str.1 = [24 x i8] c"Starting the big loop.\0A\00"
@.str.2 = [30 x i8] c"Big loop iterations left: %d\0A\00"
@.str.3 = [32 x i8] c"Big loop ended. Closing daemon.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i64, i64* %6
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %15
%22 = call i32 @umask(i32 0)
%23 = call i64 (...) @setsid()
store i64 %23, i64* %7
%24 = load i64, i64* %7
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %29
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @STDERR_FILENO
%41 = call i32 @close(i32 %40)
store i32 3, i32* %8
%42 = load i8**, i8*** %5
%43 = load i8*, i8** %42
%44 = load i32, i32* @LOG_PID
%45 = load i32, i32* @LOG_USER
%46 = call i32 @openlog(i8* %43, i32 %44, i32 %45)
%47 = load i32, i32* @LOG_INFO
%48 = call i32 (i32, i8*, ...) @syslog(i32 %47, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %53, %35
%50 = load i32, i32* %8
%51 = add nsw i32 %50, -1
store i32 %51, i32* %8
%52 = icmp ne i32 %50, 0
br i1 %52, label %53, label %58
53: ; preds = %49
%54 = load i32, i32* @LOG_INFO
%55 = load i32, i32* %8
%56 = call i32 (i32, i8*, ...) @syslog(i32 %54, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %55)
%57 = call i32 @sleep(i32 5)
br label %49
58: ; preds = %49
%59 = load i32, i32* @LOG_INFO
%60 = call i32 (i32, i8*, ...) @syslog(i32 %59, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 (...) @closelog()
%62 = load i32, i32* @EXIT_SUCCESS
%63 = call i32 @exit(i32 %62)
unreachable
}

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
@LOG_PID = global i32 0
@LOG_USER = global i32 0
@LOG_INFO = global i32 0
@.str.1 = [24 x i8] c"Starting the big loop.\0A\00"
@.str.2 = [30 x i8] c"Big loop iterations left: %d\0A\00"
@.str.3 = [32 x i8] c"Big loop ended. Closing daemon.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %2
%9 = icmp eq i64 %3, 0
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = load i32, i32* @EXIT_SUCCESS
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %8
%14 = tail call i32 @umask(i32 0)
%15 = tail call i64 (...) @setsid()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %13
%21 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %20
%27 = load i32, i32* @STDIN_FILENO
%28 = tail call i32 @close(i32 %27)
%29 = load i32, i32* @STDOUT_FILENO
%30 = tail call i32 @close(i32 %29)
%31 = load i32, i32* @STDERR_FILENO
%32 = tail call i32 @close(i32 %31)
%33 = load i8*, i8** %1
%34 = load i32, i32* @LOG_PID
%35 = load i32, i32* @LOG_USER
%36 = tail call i32 @openlog(i8* %33, i32 %34, i32 %35)
%37 = load i32, i32* @LOG_INFO
%38 = tail call i32 (i32, i8*, ...) @syslog(i32 %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* @LOG_INFO
%40 = tail call i32 (i32, i8*, ...) @syslog(i32 %39, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 2)
%41 = tail call i32 @sleep(i32 5)
%42 = load i32, i32* @LOG_INFO
%43 = tail call i32 (i32, i8*, ...) @syslog(i32 %42, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 1)
%44 = tail call i32 @sleep(i32 5)
%45 = load i32, i32* @LOG_INFO
%46 = tail call i32 (i32, i8*, ...) @syslog(i32 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 0)
%47 = tail call i32 @sleep(i32 5)
%48 = load i32, i32* @LOG_INFO
%49 = tail call i32 (i32, i8*, ...) @syslog(i32 %48, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%50 = tail call i32 (...) @closelog()
%51 = load i32, i32* @EXIT_SUCCESS
%52 = tail call i32 @exit(i32 %51)
unreachable
}