#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ log_err(char *);
int open(char *, int, ...);
scalar_t__ setsid();
int daemonise() {
  switch (fork()) {
  case 0:
    break;
  case -1:
    log_err("Daemon: Failed to fork\n");
    return -1;
  default:
    _exit(0);
  }
  if (setsid() < 0) {
    log_err("Daemon: setsid() failed\n");
    return -1;
  }
  for (int i = 0; i < 3; ++i)
    close(i);
  int fd;
  if ((fd = open("/dev/null", O_RDONLY)) == -1) {
    log_err("Daemon: Failed to open /dev/null\n");
    return -1;
  }
  if ((fd = open("/dev/null", O_WRONLY | O_TRUNC, 0644)) == -1) {
    log_err("Daemon: Failed to open /dev/null\n");
    return -1;
  }
  if ((fd = open("/dev/null", O_WRONLY | O_TRUNC, 0644)) == -1) {
    log_err("Daemon: Failed to open /dev/null\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     1197 <daemonise+0x37>
jmp    1182 <daemonise+0x22>
mov    -0x10(%rbp),%eax
test   %eax,%eax
jne    11af <daemonise+0x4f>
jmp    1192 <daemonise+0x32>
jmp    11b6 <daemonise+0x56>
lea    0xe62(%rip),%rdi
call   1070 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <daemonise+0x17a>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    11df <daemonise+0x7f>
lea    0xe4a(%rip),%rdi
call   1070 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <daemonise+0x17a>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1206 <daemonise+0xa6>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11e6 <daemonise+0x86>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1241 <daemonise+0xe1>
lea    0xe0b(%rip),%rdi
call   1070 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <daemonise+0x17a>
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    0x2da7(%rip),%rax
or     (%rax),%esi
lea    0xdd7(%rip),%rdi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    128a <daemonise+0x12a>
lea    0xdc2(%rip),%rdi
call   1070 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <daemonise+0x17a>
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
mov    0x2d5e(%rip),%rax
or     (%rax),%esi
lea    0xd8e(%rip),%rdi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    12d3 <daemonise+0x173>
lea    0xd79(%rip),%rdi
call   1070 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <daemonise+0x17a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonise>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     121b <daemonise+0xbb>
test   %eax,%eax
jne    1245 <daemonise+0xe5>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     1224 <daemonise+0xc4>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
lea    0xe73(%rip),%rbx
cmp    $0xffffffff,%eax
je     122b <daemonise+0xcb>
mov    0x2df4(%rip),%r14
mov    0x2e1d(%rip),%r15
mov    (%r15),%esi
or     (%r14),%esi
lea    0xe49(%rip),%rdi
xor    %ebp,%ebp
mov    $0x1a4,%edx
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     122b <daemonise+0xcb>
mov    (%r15),%esi
or     (%r14),%esi
lea    0xe29(%rip),%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
jne    1238 <daemonise+0xd8>
jmp    122b <daemonise+0xcb>
lea    0xdde(%rip),%rbx
jmp    122b <daemonise+0xcb>
lea    0xded(%rip),%rbx
mov    %rbx,%rdi
call   1070 <log_err@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Daemon: Failed to fork\0A\00"
@.str.1 = [25 x i8] c"Daemon: setsid() failed\0A\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@.str.3 = [34 x i8] c"Daemon: Failed to open /dev/null\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @daemonise() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
switch i32 %4, label %8 [
i32 0, label %5
i32 -1, label %6
]
5: ; preds = %0
br label %10
6: ; preds = %0
%7 = call i32 @log_err(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %48
8: ; preds = %0
%9 = call i32 @_exit(i32 0)
unreachable
10: ; preds = %5
%11 = call i64 (...) @setsid()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @log_err(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %48
15: ; preds = %10
store i32 0, i32* %2
br label %16
16: ; preds = %22, %15
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 3
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %19
%23 = load i32, i32* %2
%24 = add nsw i32 %23, 1
store i32 %24, i32* %2
br label %16
25: ; preds = %16
%26 = load i32, i32* @O_RDONLY
%27 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %26)
store i32 %27, i32* %3
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @log_err(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %48
31: ; preds = %25
%32 = load i32, i32* @O_WRONLY
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %34, i32 420)
store i32 %35, i32* %3
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @log_err(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %48
39: ; preds = %31
%40 = load i32, i32* @O_WRONLY
%41 = load i32, i32* @O_TRUNC
%42 = or i32 %40, %41
%43 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %42, i32 420)
store i32 %43, i32* %3
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @log_err(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %48
47: ; preds = %39
store i32 0, i32* %1
br label %48
48: ; preds = %47, %45, %37, %29, %13, %6
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@.str = [24 x i8] c"Daemon: Failed to fork\0A\00"
@.str.1 = [25 x i8] c"Daemon: setsid() failed\0A\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@.str.3 = [34 x i8] c"Daemon: Failed to open /dev/null\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @daemonise() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %2 [
i32 0, label %4
i32 -1, label %26
]
2: ; preds = %0
%3 = tail call i32 @_exit(i32 0)
unreachable
4: ; preds = %0
%5 = tail call i64 (...) @setsid()
%6 = icmp slt i64 %5, 0
br i1 %6, label %26, label %7
7: ; preds = %4
%8 = tail call i32 @close(i32 0)
%9 = tail call i32 @close(i32 1)
%10 = tail call i32 @close(i32 2)
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %26, label %14
14: ; preds = %7
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %16, %15
%18 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 420)
%19 = icmp eq i32 %18, -1
br i1 %19, label %26, label %20
20: ; preds = %14
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %22, %21
%24 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 420)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %20, %14, %7, %4, %0
%27 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), %7 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), %14 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), %20 ]
%28 = tail call i32 @log_err(i8* %27)
br label %29
29: ; preds = %26, %20
%30 = phi i32 [ 0, %20 ], [ -1, %26 ]
ret i32 %30
}