#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
void usl_daemonize(void) {
  pid_t pid;
  FILE *f;
  pid = fork();
  if (pid < 0)
    exit(1);
  if (pid > 0)
    _exit(0);
  setsid();
  pid = fork();
  if (pid < 0)
    exit(1);
  if (pid > 0)
    _exit(0);
  if (chdir("/") < 0) {
    perror("chdir");
    exit(1);
  }
  f = freopen("/dev/null", "r", stdin);
  if (f == NULL) {
    perror("freopen");
    exit(1);
  }
  f = freopen("/dev/null", "w", stdout);
  if (f == NULL) {
    perror("freopen");
    exit(1);
  }
  f = freopen("/dev/null", "w", stderr);
  if (f == NULL) {
    perror("freopen");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<usl_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1198 <usl_daemonize+0x28>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11aa <usl_daemonize+0x3a>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11d1 <usl_daemonize+0x61>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11e3 <usl_daemonize+0x73>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xe16(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    120f <usl_daemonize+0x9f>
lea    0xe02(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    0xde9(%rip),%rdi
lea    0xde0(%rip),%rsi
call   1050 <freopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1250 <usl_daemonize+0xe0>
lea    0xdd1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
lea    0xda8(%rip),%rdi
lea    0xdb3(%rip),%rsi
call   1050 <freopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1291 <usl_daemonize+0x121>
lea    0xd90(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%edx
lea    0xd67(%rip),%rdi
lea    0xd72(%rip),%rsi
call   1050 <freopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12d2 <usl_daemonize+0x162>
lea    0xd4f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usl_daemonize>:
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1223 <usl_daemonize+0xb3>
jne    122d <usl_daemonize+0xbd>
xor    %eax,%eax
call   1060 <setsid@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1223 <usl_daemonize+0xb3>
jne    122d <usl_daemonize+0xbd>
lea    0xe55(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     1234 <usl_daemonize+0xc4>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
lea    0xe43(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   1050 <freopen@plt>
test   %rax,%rax
je     121a <usl_daemonize+0xaa>
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
lea    0xe22(%rip),%rdi
lea    0xe2d(%rip),%rsi
call   1050 <freopen@plt>
test   %rax,%rax
je     121a <usl_daemonize+0xaa>
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
lea    0xe01(%rip),%rdi
lea    0xe0c(%rip),%rsi
call   1050 <freopen@plt>
test   %rax,%rax
je     121a <usl_daemonize+0xaa>
pop    %rax
ret
lea    0xdf1(%rip),%rdi
jmp    123b <usl_daemonize+0xcb>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xdc7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [6 x i8] c"chdir\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@.str.3 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.4 = [8 x i8] c"freopen\00"
@.str.5 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
define void @usl_daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @exit(i32 1)
unreachable
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = tail call i32 @_exit(i32 0)
unreachable
9: ; preds = %5
%10 = tail call i32 (...) @setsid()
%11 = tail call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = icmp eq i64 %11, 0
br i1 %16, label %19, label %17
17: ; preds = %15
%18 = tail call i32 @_exit(i32 0)
unreachable
19: ; preds = %15
%20 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %19
%26 = load i32, i32* @stdin
%27 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = load i32, i32* @stdout
%34 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%45 = tail call i32 @exit(i32 1)
unreachable
46: ; preds = %39
ret void
}