#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*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int);
scalar_t__ fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int daemon(int nochdir, int noclose) {
  if (fork() != 0)
    _exit(0);
  if (nochdir == 0)
    (void)chdir("/");
  if (noclose == 0) {
    (void)close(0);
    (void)open("/dev/null", O_RDWR);
    (void)dup2(0, 1);
    (void)dup2(0, 2);
  }
  (void)setsid();
  if (fork() != 0)
    _exit(0);
  return (0);
}


x86 assembly (clang, -O0):
<daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
je     1196 <daemon+0x26>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x4(%rbp)
jne    11ac <daemon+0x3c>
lea    0xe59(%rip),%rdi
call   1040 <chdir@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11ea <daemon+0x7a>
xor    %edi,%edi
call   1060 <close@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdi
call   1080 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
je     1209 <daemon+0x99>
xor    %edi,%edi
call   1030 <_exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11ed <daemon+0x7d>
test   %ebp,%ebp
je     11a7 <daemon+0x37>
test   %ebx,%ebx
je     11b7 <daemon+0x47>
xor    %eax,%eax
call   1070 <setsid@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11ed <daemon+0x7d>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe52(%rip),%rdi
call   1040 <chdir@plt>
test   %ebx,%ebx
jne    118b <daemon+0x1b>
xor    %edi,%edi
call   1060 <close@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   1080 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
jmp    118b <daemon+0x1b>
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"
@O_RDWR = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%5 = call i64 (...) @fork()
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 @_exit(i32 0)
unreachable
9: ; preds = %2
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = call i32 @close(i32 0)
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = call i32 @dup2(i32 0, i32 1)
%22 = call i32 @dup2(i32 0, i32 2)
br label %23
23: ; preds = %17, %14
%24 = call i32 (...) @setsid()
%25 = call i64 (...) @fork()
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @_exit(i32 0)
unreachable
29: ; preds = %23
ret i32 0
}

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