#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(int);
scalar_t__ fork();
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int getppid();
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonise(void) {
  pid_t pid, sid;
  if (getppid() == 1)
    return;
  pid = fork();
  if (pid < 0) {
    exit(1);
  } else if (pid > 0) {
    exit(0);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    exit(1);
  }
  freopen("/dev/null", "r", stdin);
  freopen("/dev/null", "w", stdout);
  freopen("/dev/null", "w", stderr);
}


x86 assembly (clang, -O0):
<daemonise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <getppid@plt>
cmp    $0x1,%eax
jne    117d <daemonise+0x1d>
jmp    122f <daemonise+0xcf>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    119d <daemonise+0x3d>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11af <daemonise+0x4f>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11b4 <daemonise+0x54>
xor    %edi,%edi
call   1050 <umask@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11db <daemonise+0x7b>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
lea    0xe15(%rip),%rdi
lea    0xe18(%rip),%rsi
call   1030 <freopen@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    0xdf9(%rip),%rdi
lea    0xdfe(%rip),%rsi
call   1030 <freopen@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
lea    0xddd(%rip),%rdi
lea    0xde2(%rip),%rsi
call   1030 <freopen@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonise>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <getppid@plt>
cmp    $0x1,%eax
jne    1178 <daemonise+0x18>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11ee <daemonise+0x8e>
jne    11f8 <daemonise+0x98>
xor    %edi,%edi
call   1050 <umask@plt>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
js     11ee <daemonise+0x8e>
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
lea    0xe57(%rip),%rbx
lea    0xe5a(%rip),%rsi
mov    %rbx,%rdi
call   1030 <freopen@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
lea    0xe44(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <freopen@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <freopen@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
define void @daemonise() {
%1 = alloca i64
%2 = alloca i64
%3 = call i32 (...) @getppid()
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %6
5: ; preds = %0
br label %32
6: ; preds = %0
%7 = call i64 (...) @fork()
store i64 %7, i64* %1
%8 = load i64, i64* %1
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %6
%13 = load i64, i64* %1
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %12
br label %18
18: ; preds = %17
%19 = call i32 @umask(i32 0)
%20 = call i64 (...) @setsid()
store i64 %20, i64* %2
%21 = load i64, i64* %2
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = load i32, i32* @stdin
%27 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @stdout
%29 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @stderr
%31 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %25, %5
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
define void @daemonise() {
%1 = tail call i32 (...) @getppid()
%2 = icmp eq i32 %1, 1
br i1 %2, label %25, label %3
3: ; preds = %0
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %3
%9 = icmp eq i64 %4, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %8
%13 = tail call i32 @umask(i32 0)
%14 = tail call i64 (...) @setsid()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = load i32, i32* @stdin
%20 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* @stdout
%22 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @stderr
%24 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %0, %18
ret void
}