#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_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int make_daemon(void) {
  switch (fork()) {
  case 0:
    break;
  case -1:
    perror("fork()");
    return 1;
  default:
    exit(0);
  }
  setsid();
  chdir("/");
  switch (fork()) {
  case 0:
    break;
  case -1:
    perror("fork()");
    return 1;
  default:
    exit(0);
  }
  int devnull = open("/dev/null", O_RDWR);
  dup2(devnull, STDIN_FILENO);
  dup2(devnull, STDOUT_FILENO);
  dup2(devnull, STDERR_FILENO);
  if (devnull > 2)
    close(devnull);
  return 0;
}


x86 assembly (clang, -O0):
<make_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     11b7 <make_daemon+0x37>
jmp    11a2 <make_daemon+0x22>
mov    -0xc(%rbp),%eax
test   %eax,%eax
jne    11cf <make_daemon+0x4f>
jmp    11b2 <make_daemon+0x32>
jmp    11d6 <make_daemon+0x56>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <make_daemon+0x11f>
xor    %edi,%edi
call   1090 <exit@plt>
xor    %eax,%eax
mov    %al,-0x11(%rbp)
call   1060 <setsid@plt>
lea    0xe20(%rip),%rdi
call   1030 <chdir@plt>
mov    -0x11(%rbp),%al
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     121c <make_daemon+0x9c>
jmp    1207 <make_daemon+0x87>
mov    -0x10(%rbp),%eax
test   %eax,%eax
jne    1234 <make_daemon+0xb4>
jmp    1217 <make_daemon+0x97>
jmp    123b <make_daemon+0xbb>
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <make_daemon+0x11f>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
lea    0xdbe(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmpl   $0x2,-0x8(%rbp)
jle    1298 <make_daemon+0x118>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_daemon>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    1203 <make_daemon+0x83>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe69(%rip),%rdi
call   1030 <chdir@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    1203 <make_daemon+0x83>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
cmp    $0x3,%ebp
jl     1219 <make_daemon+0x99>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1219 <make_daemon+0x99>
cmp    $0xffffffff,%eax
jne    1222 <make_daemon+0xa2>
lea    0xdf1(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @make_daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @fork()
switch i32 %3, label %7 [
i32 0, label %4
i32 -1, label %5
]
4: ; preds = %0
br label %9
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %36
7: ; preds = %0
%8 = call i32 @exit(i32 0)
unreachable
9: ; preds = %4
%10 = call i32 (...) @setsid()
%11 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (...) @fork()
switch i32 %12, label %16 [
i32 0, label %13
i32 -1, label %14
]
13: ; preds = %9
br label %18
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %36
16: ; preds = %9
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %13
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %19)
store i32 %20, i32* %2
%21 = load i32, i32* %2
%22 = load i32, i32* @STDIN_FILENO
%23 = call i32 @dup2(i32 %21, i32 %22)
%24 = load i32, i32* %2
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = load i32, i32* %2
%28 = load i32, i32* @STDERR_FILENO
%29 = call i32 @dup2(i32 %27, i32 %28)
%30 = load i32, i32* %2
%31 = icmp sgt i32 %30, 2
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %32, %18
store i32 0, i32* %1
br label %36
36: ; preds = %35, %14, %5
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @make_daemon() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %4 [
i32 0, label %6
i32 -1, label %2
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %26
4: ; preds = %0
%5 = tail call i32 @exit(i32 0)
unreachable
6: ; preds = %0
%7 = tail call i32 (...) @setsid()
%8 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 (...) @fork()
switch i32 %9, label %12 [
i32 0, label %14
i32 -1, label %10
]
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %26
12: ; preds = %6
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %6
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @STDIN_FILENO
%18 = tail call i32 @dup2(i32 %16, i32 %17)
%19 = load i32, i32* @STDOUT_FILENO
%20 = tail call i32 @dup2(i32 %16, i32 %19)
%21 = load i32, i32* @STDERR_FILENO
%22 = tail call i32 @dup2(i32 %16, i32 %21)
%23 = icmp sgt i32 %16, 2
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = tail call i32 @close(i32 %16)
br label %26
26: ; preds = %14, %24, %10, %2
%27 = phi i32 [ 1, %2 ], [ 1, %10 ], [ 0, %24 ], [ 0, %14 ]
ret i32 %27
}