#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 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_return(int nochdir, int noclose) {
  pid_t pid = -1;
  int ret = -1;
  FILE *ptr = NULL;
  ret = fork();
  if (ret)
    return ret;
  pid = setsid();
  if (pid == -1) {
    ret = -1;
    goto out;
  }
  if (!nochdir)
    ret = chdir("/");
  if (!noclose) {
    ptr = freopen("/dev/null", "r", stdin);
    if (!ptr)
      goto out;
    ptr = freopen("/dev/null", "w", stdout);
    if (!ptr)
      goto out;
    ptr = freopen("/dev/null", "w", stderr);
    if (!ptr)
      goto out;
  }
  ret = 0;
out:
  return ret;
}


x86 assembly (clang, -O0):
<os_daemon_return>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1183 <os_daemon_return+0x43>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <os_daemon_return+0x128>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11a3 <os_daemon_return+0x63>
movl   $0xffffffff,-0x14(%rbp)
jmp    1262 <os_daemon_return+0x122>
cmpl   $0x0,-0x8(%rbp)
jne    11bc <os_daemon_return+0x7c>
lea    0xe4c(%rip),%rdi
call   1030 <chdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    125b <os_daemon_return+0x11b>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
lea    0xe2c(%rip),%rdi
lea    0xe2f(%rip),%rsi
call   1040 <freopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f6 <os_daemon_return+0xb6>
jmp    1262 <os_daemon_return+0x122>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
lea    0xdfc(%rip),%rdi
lea    0xe01(%rip),%rsi
call   1040 <freopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1226 <os_daemon_return+0xe6>
jmp    1262 <os_daemon_return+0x122>
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
lea    0xdcc(%rip),%rdi
lea    0xdd1(%rip),%rsi
call   1040 <freopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1256 <os_daemon_return+0x116>
jmp    1262 <os_daemon_return+0x122>
jmp    125b <os_daemon_return+0x11b>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_daemon_return>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11f2 <os_daemon_return+0xb2>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
je     1173 <os_daemon_return+0x33>
test   %ebp,%ebp
je     117a <os_daemon_return+0x3a>
test   %r14d,%r14d
jne    11f0 <os_daemon_return+0xb0>
jmp    118d <os_daemon_return+0x4d>
mov    $0xffffffff,%ebx
jmp    11f2 <os_daemon_return+0xb2>
lea    0xe7f(%rip),%rdi
call   1030 <chdir@plt>
mov    %eax,%ebx
test   %r14d,%r14d
jne    11f0 <os_daemon_return+0xb0>
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
lea    0xe65(%rip),%rdi
lea    0xe68(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     11f2 <os_daemon_return+0xb2>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
lea    0xe44(%rip),%rdi
lea    0xe49(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     11f2 <os_daemon_return+0xb2>
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
lea    0xe23(%rip),%rdi
lea    0xe28(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     11f2 <os_daemon_return+0xb2>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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_return(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
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %7
store i32 %13, i32* %3
br label %49
14: ; preds = %2
%15 = call i32 (...) @setsid()
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 -1, i32* %7
br label %47
19: ; preds = %14
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %23, i32* %7
br label %24
24: ; preds = %22, %19
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %46, label %27
27: ; preds = %24
%28 = load i32, i32* @stdin
%29 = 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 %28)
store i32* %29, i32** %8
%30 = load i32*, i32** %8
%31 = icmp ne i32* %30, null
br i1 %31, label %33, label %32
32: ; preds = %27
br label %47
33: ; preds = %27
%34 = load i32, i32* @stdout
%35 = 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 %34)
store i32* %35, i32** %8
%36 = load i32*, i32** %8
%37 = icmp ne i32* %36, null
br i1 %37, label %39, label %38
38: ; preds = %33
br label %47
39: ; preds = %33
%40 = load i32, i32* @stderr
%41 = 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 %40)
store i32* %41, i32** %8
%42 = load i32*, i32** %8
%43 = icmp ne i32* %42, null
br i1 %43, label %45, label %44
44: ; preds = %39
br label %47
45: ; preds = %39
br label %46
46: ; preds = %45, %24
store i32 0, i32* %7
br label %47
47: ; preds = %46, %44, %38, %32, %18
%48 = load i32, i32* %7
store i32 %48, i32* %3
br label %49
49: ; preds = %47, %12
%50 = load i32, i32* %3
ret i32 %50
}

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_return(i32 %0, i32 %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %28
5: ; preds = %2
%6 = tail call i32 (...) @setsid()
%7 = icmp eq i32 %6, -1
br i1 %7, label %28, label %8
8: ; preds = %5
%9 = icmp eq i32 %0, 0
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %8
%13 = phi i32 [ 0, %8 ], [ %11, %10 ]
%14 = icmp eq i32 %1, 0
br i1 %14, label %15, label %27
15: ; preds = %12
%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 = icmp eq i32* %17, null
br i1 %18, label %28, label %19
19: ; preds = %15
%20 = load i32, i32* @stdout
%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)
%22 = icmp eq i32* %21, null
br i1 %22, label %28, label %23
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = 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 %24)
%26 = icmp eq i32* %25, null
br i1 %26, label %28, label %27
27: ; preds = %23, %12
br label %28
28: ; preds = %27, %15, %19, %23, %5, %2
%29 = phi i32 [ %3, %2 ], [ 0, %27 ], [ %13, %23 ], [ %13, %19 ], [ %13, %15 ], [ -1, %5 ]
ret i32 %29
}