#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void sysv_daemonize() {
  pid_t pid = fork();
  if (pid == -1) {
    perror("Unable to fork");
    exit(1);
  }
  if (pid > 0) {
    exit(0);
  }
  if (setsid() < 0) {
    perror("Unable to setsid");
    exit(1);
  }
  pid = fork();
  if (pid == -1) {
    perror("Unable to fork for a second time");
    exit(1);
  }
  if (pid > 0) {
    exit(0);
  }
  umask(0);
  if (chdir("/") == -1) {
    perror("Could not change to directory");
    exit(1);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  if (freopen("/dev/null", "r", stdin) == NULL) {
    perror("Unable to reopen stdin to /dev/null");
    exit(1);
  }
  if (freopen("/dev/null", "w", stdout) == NULL) {
    perror("Unable to reopen stdout to /dev/null");
    exit(1);
  }
  if (freopen("/dev/null", "w", stderr) == NULL) {
    perror("Unable to reopen stderr to /dev/null");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<sysv_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11b2 <sysv_daemonize+0x32>
lea    0xe5d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    11c3 <sysv_daemonize+0x43>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    11ea <sysv_daemonize+0x6a>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1214 <sysv_daemonize+0x94>
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    1225 <sysv_daemonize+0xa5>
xor    %edi,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xe0e(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1257 <sysv_daemonize+0xd7>
lea    0xdfb(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edx
lea    0xdec(%rip),%rdi
lea    0xdc9(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%rax
jne    12bd <sysv_daemonize+0x13d>
lea    0xdb5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edx
lea    0xdb0(%rip),%rdi
lea    0xdb3(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%rax
jne    12f9 <sysv_daemonize+0x179>
lea    0xd9f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edx
lea    0xd74(%rip),%rdi
lea    0xd77(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%rax
jne    1335 <sysv_daemonize+0x1b5>
lea    0xd88(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysv_daemonize>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     1273 <sysv_daemonize+0xf3>
test   %eax,%eax
jg     126c <sysv_daemonize+0xec>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     127c <sysv_daemonize+0xfc>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     1285 <sysv_daemonize+0x105>
test   %eax,%eax
jg     126c <sysv_daemonize+0xec>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xe72(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
je     128e <sysv_daemonize+0x10e>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    0xe66(%rip),%rdi
lea    0xe43(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     1297 <sysv_daemonize+0x117>
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
lea    0xe45(%rip),%rdi
lea    0xe48(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     12a0 <sysv_daemonize+0x120>
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
lea    0xe24(%rip),%rdi
lea    0xe27(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     12a9 <sysv_daemonize+0x129>
pop    %rax
ret
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xd86(%rip),%rdi
jmp    12b0 <sysv_daemonize+0x130>
lea    0xd8c(%rip),%rdi
jmp    12b0 <sysv_daemonize+0x130>
lea    0xd94(%rip),%rdi
jmp    12b0 <sysv_daemonize+0x130>
lea    0xdae(%rip),%rdi
jmp    12b0 <sysv_daemonize+0x130>
lea    0xdc5(%rip),%rdi
jmp    12b0 <sysv_daemonize+0x130>
lea    0xde2(%rip),%rdi
jmp    12b0 <sysv_daemonize+0x130>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Unable to fork\00"
@.str.1 = [17 x i8] c"Unable to setsid\00"
@.str.2 = [33 x i8] c"Unable to fork for a second time\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [30 x i8] c"Could not change to directory\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.5 = [10 x i8] c"/dev/null\00"
@.str.6 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.7 = [36 x i8] c"Unable to reopen stdin to /dev/null\00"
@.str.8 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.9 = [37 x i8] c"Unable to reopen stdout to /dev/null\00"
@stderr = global i32 0
@.str.10 = [37 x i8] c"Unable to reopen stderr to /dev/null\00"
define void @sysv_daemonize() {
%1 = alloca i32
%2 = call i32 (...) @fork()
store i32 %2, i32* %1
%3 = load i32, i32* %1
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* %1
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %8
%14 = call i64 (...) @setsid()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = call i32 (...) @fork()
store i32 %20, i32* %1
%21 = load i32, i32* %1
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = load i32, i32* %1
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %26
%32 = call i32 @umask(i32 0)
%33 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31
%39 = load i32, i32* @STDIN_FILENO
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @STDOUT_FILENO
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* @STDERR_FILENO
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* @stdin
%46 = call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %45)
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %51
48: ; preds = %38
%49 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %38
%52 = load i32, i32* @stdout
%53 = call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 %52)
%54 = icmp eq i32* %53, null
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.9, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %51
%59 = load i32, i32* @stderr
%60 = call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 %59)
%61 = icmp eq i32* %60, null
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.10, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %58
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"Unable to fork\00"
@.str.1 = [17 x i8] c"Unable to setsid\00"
@.str.2 = [33 x i8] c"Unable to fork for a second time\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [30 x i8] c"Could not change to directory\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.5 = [10 x i8] c"/dev/null\00"
@.str.6 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.7 = [36 x i8] c"Unable to reopen stdin to /dev/null\00"
@.str.8 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.9 = [37 x i8] c"Unable to reopen stdout to /dev/null\00"
@stderr = global i32 0
@.str.10 = [37 x i8] c"Unable to reopen stderr to /dev/null\00"
define void @sysv_daemonize() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %6
%11 = tail call i64 (...) @setsid()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = tail call i32 (...) @fork()
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = icmp sgt i32 %17, 0
br i1 %23, label %24, label %26
24: ; preds = %22
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %22
%27 = tail call i32 @umask(i32 0)
%28 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = load i32, i32* @STDIN_FILENO
%35 = tail call i32 @close(i32 %34)
%36 = load i32, i32* @STDOUT_FILENO
%37 = tail call i32 @close(i32 %36)
%38 = load i32, i32* @STDERR_FILENO
%39 = tail call i32 @close(i32 %38)
%40 = load i32, i32* @stdin
%41 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %40)
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %46
43: ; preds = %33
%44 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
%45 = tail call i32 @exit(i32 1)
unreachable
46: ; preds = %33
%47 = load i32, i32* @stdout
%48 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 %47)
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.9, i64 0, i64 0))
%52 = tail call i32 @exit(i32 1)
unreachable
53: ; preds = %46
%54 = load i32, i32* @stderr
%55 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 %54)
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.10, i64 0, i64 0))
%59 = tail call i32 @exit(i32 1)
unreachable
60: ; preds = %53
ret void
}