#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
void spy_make_daemonize() {
  int fd;
  if (fork() != 0)
    exit(0);
  setsid();
  if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
    if (fd > STDERR_FILENO)
      close(fd);
  }
}


x86 assembly (clang, -O0):
<spy_make_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
je     1180 <spy_make_daemonize+0x20>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
je     11fc <spy_make_daemonize+0x9c>
mov    -0x4(%rbp),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%eax
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%eax
jle    11f7 <spy_make_daemonize+0x97>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
jmp    11fc <spy_make_daemonize+0x9c>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spy_make_daemonize>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    11e1 <spy_make_daemonize+0x81>
xor    %eax,%eax
call   1050 <setsid@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d9 <spy_make_daemonize+0x79>
mov    %eax,%ebx
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2e14(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
cmp    (%r14),%ebx
jle    11d9 <spy_make_daemonize+0x79>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @spy_make_daemonize() {
%1 = alloca i32
%2 = call i64 (...) @fork()
%3 = icmp ne i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = call i32 @exit(i32 0)
unreachable
6: ; preds = %0
%7 = call i32 (...) @setsid()
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8, i32 0)
store i32 %9, i32* %1
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %28
11: ; preds = %6
%12 = load i32, i32* %1
%13 = load i32, i32* @STDIN_FILENO
%14 = call i32 @dup2(i32 %12, i32 %13)
%15 = load i32, i32* %1
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = load i32, i32* %1
%19 = load i32, i32* @STDERR_FILENO
%20 = call i32 @dup2(i32 %18, i32 %19)
%21 = load i32, i32* %1
%22 = load i32, i32* @STDERR_FILENO
%23 = icmp sgt i32 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %11
%25 = load i32, i32* %1
%26 = call i32 @close(i32 %25)
br label %27
27: ; preds = %24, %11
br label %28
28: ; preds = %27, %6
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @spy_make_daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %5, label %3
3: ; preds = %0
%4 = tail call i32 @exit(i32 0)
unreachable
5: ; preds = %0
%6 = tail call i32 (...) @setsid()
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %21, label %10
10: ; preds = %5
%11 = load i32, i32* @STDIN_FILENO
%12 = tail call i32 @dup2(i32 %8, i32 %11)
%13 = load i32, i32* @STDOUT_FILENO
%14 = tail call i32 @dup2(i32 %8, i32 %13)
%15 = load i32, i32* @STDERR_FILENO
%16 = tail call i32 @dup2(i32 %8, i32 %15)
%17 = load i32, i32* @STDERR_FILENO
%18 = icmp sgt i32 %8, %17
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = tail call i32 @close(i32 %8)
br label %21
21: ; preds = %10, %19, %5
ret void
}