#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 */
struct stat {
  int /*<<< orphan*/ member_0;
};
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*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ log_message(char *, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
int stat(char *, struct stat *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  fprintf(stdout, "Initiating daemon...\n");
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    printf("Daemon forked.\n");
    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);
  struct stat st = {0};
  if (stat("/tmp/bora", &st) == -1) {
    mkdir("/tmp/bora", 0755);
  }
  char *filename = "/tmp/bora/gbsd.log";
  int daemon_pid = getpid();
  int uptime = 0;
  while (1) {
    log_message(filename, "Daemon PID: %d, Uptime: %d s", daemon_pid, uptime++);
    sleep(1);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    122f <main+0x4f>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1254 <main+0x74>
lea    0xdd5(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
xor    %edi,%edi
call   10b0 <umask@plt>
mov    $0x0,%al
call   1090 <setsid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    127f <main+0x9f>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
lea    0xda0(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    12a3 <main+0xc3>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   1080 <close@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   1080 <close@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   1080 <close@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
lea    0xd44(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   10c0 <stat@plt>
cmp    $0xffffffff,%eax
jne    1307 <main+0x127>
lea    0xd2b(%rip),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
lea    0xd24(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    %eax,-0x38(%rbp)
lea    0xd06(%rip),%rsi
call   10d0 <log_message@plt>
mov    $0x1,%edi
call   10f0 <sleep@plt>
jmp    1323 <main+0x143>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
call   1090 <fprintf@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     12d1 <main+0x101>
jne    12df <main+0x10f>
xor    %edi,%edi
call   10a0 <umask@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
test   %rax,%rax
js     12d1 <main+0x101>
lea    0xdf1(%rip),%rdi
call   1060 <chdir@plt>
test   %rax,%rax
js     12d1 <main+0x101>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
movl   $0x0,0x8(%rsp)
lea    0xdac(%rip),%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
jne    128c <main+0xbc>
lea    0xd96(%rip),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%r12d
lea    0xd83(%rip),%r14
lea    0xd8f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
lea    0x1(%rbx),%ebp
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
mov    %ebx,%ecx
call   10c0 <log_message@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    %ebp,%ebx
jmp    12b0 <main+0xe0>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xd6c(%rip),%rdi
call   1040 <puts@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stdout = global i32 0
@.str = [22 x i8] c"Initiating daemon...\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"Daemon forked.\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.3 = [10 x i8] c"/tmp/bora\00"
@.str.4 = [19 x i8] c"/tmp/bora/gbsd.log\00"
@.str.5 = [29 x i8] c"Daemon PID: %d, Uptime: %d s\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 %struct.stat
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @stdout
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%14 = call i64 (...) @fork()
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i64, i64* %6
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_SUCCESS
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %20
%28 = call i32 @umask(i32 0)
%29 = call i64 (...) @setsid()
store i64 %29, i64* %7
%30 = load i64, i64* %7
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %27
%36 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %35
%42 = load i32, i32* @STDIN_FILENO
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* @STDOUT_FILENO
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* @STDERR_FILENO
%47 = call i32 @close(i32 %46)
%48 = bitcast %struct.stat* %8 to i8*
%49 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), %struct.stat* %8)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %41
%52 = call i32 @mkdir(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 493)
br label %53
53: ; preds = %51, %41
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8** %9
%54 = call i32 (...) @getpid()
store i32 %54, i32* %10
store i32 0, i32* %11
br label %55
55: ; preds = %53, %55
%56 = load i8*, i8** %9
%57 = load i32, i32* %10
%58 = load i32, i32* %11
%59 = add nsw i32 %58, 1
store i32 %59, i32* %11
%60 = call i32 @log_message(i8* %56, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %57, i32 %58)
%61 = call i32 @sleep(i32 1)
br label %55
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stdout = global i32 0
@.str = [22 x i8] c"Initiating daemon...\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.2 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.3 = [10 x i8] c"/tmp/bora\00"
@.str.4 = [19 x i8] c"/tmp/bora/gbsd.log\00"
@.str.5 = [29 x i8] c"Daemon PID: %d, Uptime: %d s\00"
@str = [15 x i8] c"Daemon forked.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @stdout
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%6 = tail call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = icmp eq i64 %6, 0
br i1 %12, label %17, label %13
13: ; preds = %11
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %11
%18 = tail call i32 @umask(i32 0)
%19 = tail call i64 (...) @setsid()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %17
%25 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %24
%31 = load i32, i32* @STDIN_FILENO
%32 = tail call i32 @close(i32 %31)
%33 = load i32, i32* @STDOUT_FILENO
%34 = tail call i32 @close(i32 %33)
%35 = load i32, i32* @STDERR_FILENO
%36 = tail call i32 @close(i32 %35)
%37 = bitcast %struct.stat* %3 to i8*
%38 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
store i32 0, i32* %38
%39 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), %struct.stat* %3)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %30
%42 = tail call i32 @mkdir(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 493)
br label %43
43: ; preds = %41, %30
%44 = tail call i32 (...) @getpid()
br label %45
45: ; preds = %45, %43
%46 = phi i32 [ 0, %43 ], [ %47, %45 ]
%47 = add nuw nsw i32 %46, 1
%48 = tail call i32 @log_message(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %44, i32 %46)
%49 = tail call i32 @sleep(i32 1)
br label %45
}