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

/* Variables and functions */
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int chdir(char *);
int fork();
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
int setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int os_daemon(int nochdir, int noclose) {
  pid_t pid = -1;
  int ret = -1;
  FILE *ptr = NULL;
  ret = fork();
  switch (ret) {
  case -1:
    return (-1);
  case 0:
    break;
  default:
    _exit(0);
  }
  pid = setsid();
  if (pid == -1) {
    ret = -1;
    goto out;
  }
  if (!nochdir)
    ret = chdir("/");
  if (!noclose) {
    ptr = freopen("/dev/null", "r", stdin);
    ptr = freopen("/dev/null", "w", stdout);
    ptr = freopen("/dev/null", "w", stderr);
  }
  ret = 0;
out:
  return ret;
}


x86 assembly (clang, -O0):
<os_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0xffffffff,%eax
je     11a2 <os_daemon+0x52>
jmp    1192 <os_daemon+0x42>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     11ae <os_daemon+0x5e>
jmp    11b3 <os_daemon+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <os_daemon+0x11a>
jmp    11ba <os_daemon+0x6a>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11da <os_daemon+0x8a>
movl   $0xffffffff,-0x14(%rbp)
jmp    1264 <os_daemon+0x114>
cmpl   $0x0,-0x8(%rbp)
jne    11f3 <os_daemon+0xa3>
lea    0xe15(%rip),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    125d <os_daemon+0x10d>
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
lea    0xdf5(%rip),%rdi
lea    0xdf8(%rip),%rsi
call   1050 <freopen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
lea    0xdd5(%rip),%rdi
lea    0xdda(%rip),%rsi
call   1050 <freopen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
lea    0xdb5(%rip),%rdi
lea    0xdba(%rip),%rsi
call   1050 <freopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_daemon>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11e3 <os_daemon+0x93>
test   %ebx,%ebx
jne    11ea <os_daemon+0x9a>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     11de <os_daemon+0x8e>
test   %ebp,%ebp
jne    1187 <os_daemon+0x37>
lea    0xe7e(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
test   %r14d,%r14d
jne    11e3 <os_daemon+0x93>
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
lea    0xe64(%rip),%rbp
lea    0xe67(%rip),%rsi
mov    %rbp,%rdi
call   1050 <freopen@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
lea    0xe51(%rip),%r14
mov    %rbp,%rdi
mov    %r14,%rsi
call   1050 <freopen@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r14,%rsi
call   1050 <freopen@plt>
jmp    11e3 <os_daemon+0x93>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
define i32 @os_daemon(i32 %0, i32 %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %4 [
i32 -1, label %22
i32 0, label %6
]
4: ; preds = %2
%5 = tail call i32 @_exit(i32 0)
unreachable
6: ; preds = %2
%7 = tail call i32 (...) @setsid()
%8 = icmp eq i32 %7, -1
br i1 %8, label %22, label %9
9: ; preds = %6
%10 = icmp eq i32 %0, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %9
%14 = icmp eq i32 %1, 0
br i1 %14, label %15, label %22
15: ; preds = %13
%16 = load i32, i32* @stdin
%17 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @stdout
%19 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @stderr
%21 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %6, %15, %13, %2
%23 = phi i32 [ %3, %2 ], [ -1, %6 ], [ 0, %15 ], [ 0, %13 ]
ret i32 %23
}