#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;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
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 getppid();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsid();
void daemonize(void) {
  pid_t pid;
  pid_t sid;
  if (getppid() == 1)
    return;
  if (chdir("/") < 0)
    do {
      perror("chdir()");
      exit(EXIT_FAILURE);
    } while (0);
  pid = fork();
  if (pid == (pid_t)-1)
    do {
      perror("fork()");
      exit(EXIT_FAILURE);
    } while (0);
  if (pid)
    exit(EXIT_SUCCESS);
  sid = setsid();
  if (sid == (pid_t)-1)
    do {
      perror("setsid()");
      exit(EXIT_FAILURE);
    } while (0);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  open("/dev/null", O_RDONLY);
  open("/dev/null", O_WRONLY);
  open("/dev/null", O_WRONLY);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <getppid@plt>
cmp    $0x1,%eax
jne    119d <daemonize+0x1d>
jmp    12be <daemonize+0x13e>
lea    0xe5c(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    11d2 <daemonize+0x52>
jmp    11b8 <daemonize+0x38>
lea    0xe43(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0xffffffffffffffff,-0x8(%rbp)
jne    1207 <daemonize+0x87>
jmp    11ed <daemonize+0x6d>
lea    0xe16(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     1220 <daemonize+0xa0>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    1255 <daemonize+0xd5>
jmp    123b <daemonize+0xbb>
lea    0xdcf(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
lea    0xd8b(%rip),%rdi
call   1060 <open@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%esi
lea    0xd76(%rip),%rdi
call   1060 <open@plt>
mov    0x2cf8(%rip),%rax
mov    (%rax),%esi
lea    0xd61(%rip),%rdi
call   1060 <open@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <getppid@plt>
cmp    $0x1,%eax
jne    1198 <daemonize+0x18>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe61(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     123b <daemonize+0xbb>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jne    122c <daemonize+0xac>
xor    %eax,%eax
call   1050 <setsid@plt>
cmp    $0xffffffffffffffff,%rax
je     1244 <daemonize+0xc4>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    0xe1a(%rip),%rbx
mov    %rbx,%rdi
call   1060 <open@plt>
mov    0x2d99(%rip),%r14
mov    (%r14),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <open@plt>
cmp    $0xffffffffffffffff,%rax
jne    125e <daemonize+0xde>
lea    0xdd1(%rip),%rdi
jmp    124b <daemonize+0xcb>
lea    0xdc0(%rip),%rdi
jmp    124b <daemonize+0xcb>
lea    0xdc6(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [8 x i8] c"chdir()\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"fork()\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [9 x i8] c"setsid()\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.4 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define void @daemonize() {
%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 %54
6: ; preds = %0
%7 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %6
br label %10
10: ; preds = %9
%11 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; No predecessors!
br label %15
15: ; preds = %14, %6
%16 = call i64 (...) @fork()
store i64 %16, i64* %1
%17 = load i64, i64* %1
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %15
br label %20
20: ; preds = %19
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; No predecessors!
br label %25
25: ; preds = %24, %15
%26 = load i64, i64* %1
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @EXIT_SUCCESS
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %25
%32 = call i64 (...) @setsid()
store i64 %32, i64* %2
%33 = load i64, i64* %2
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %31
br label %36
36: ; preds = %35
%37 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; No predecessors!
br label %41
41: ; preds = %40, %31
%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 = load i32, i32* @O_RDONLY
%49 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i32, i32* @O_WRONLY
%51 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %50)
%52 = load i32, i32* @O_WRONLY
%53 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %41, %5
ret void
}

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