#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;
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ umask(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);
  }
  while (1) {
    printf("Hello Daemon!\n");
    sleep(3);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11a3 <main+0x33>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11bc <main+0x4c>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1070 <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    11e7 <main+0x77>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe12(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    120b <main+0x9b>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1210 <main+0xa0>
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3,%edi
call   1080 <sleep@plt>
jmp    1210 <main+0xa0>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11c4 <main+0x54>
jne    11d2 <main+0x62>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     11c4 <main+0x54>
lea    0xe67(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     11c4 <main+0x54>
lea    0xe58(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1080 <sleep@plt>
jmp    11b0 <main+0x40>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [15 x i8] c"Hello Daemon!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @EXIT_FAILURE
%9 = call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @EXIT_SUCCESS
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %10
%17 = call i32 @umask(i32 0)
%18 = call i64 (...) @setsid()
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %16
%25 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, 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 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %24
br label %31
31: ; preds = %30, %31
%32 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @sleep(i32 3)
br label %31
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@str = [14 x i8] c"Hello Daemon!\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, %24
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @sleep(i32 3)
br label %24
}