#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 /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
void makeDaemon() {
  int fd, r, pid;
  pid = fork();
  if (pid != 0)
    exit(0);
  setsid();
  close(0);
  close(1);
  close(2);
  fd = open("/dev/null", O_RDWR);
  r = dup(fd);
  r = dup(fd);
}


x86 assembly (clang, -O0):
<makeDaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1183 <makeDaemon+0x23>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <dup@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <dup@plt>
mov    %eax,-0x8(%rbp)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeDaemon>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11b4 <makeDaemon+0x54>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <dup@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1030 <dup@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @makeDaemon() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %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 = tail call i32 @close(i32 0)
%8 = tail call i32 @close(i32 1)
%9 = tail call i32 @close(i32 2)
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = tail call i32 @dup(i32 %11)
%13 = tail call i32 @dup(i32 %11)
ret void
}