#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int daemon(int nochdir, int noclose) {
  pid_t pid;
  if (!nochdir && chdir("/") != 0)
    return -1;
  if (!noclose) {
    int fd = open("/dev/null", O_RDWR);
    if (fd < 0)
      return -1;
    if (dup2(fd, 0) < 0 || dup2(fd, 1) < 0 || dup2(fd, 2) < 0) {
      close(fd);
      return -1;
    }
    close(fd);
  }
  pid = fork();
  if (pid < 0)
    return -1;
  if (pid > 0)
    _exit(0);
  if (setsid() < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11aa <daemon+0x3a>
lea    0xe71(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11aa <daemon+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <daemon+0x128>
cmpl   $0x0,-0xc(%rbp)
jne    1240 <daemon+0xd0>
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e2 <daemon+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <daemon+0x128>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jl     1224 <daemon+0xb4>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jl     1224 <daemon+0xb4>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1238 <daemon+0xc8>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <daemon+0x128>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1262 <daemon+0xf2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <daemon+0x128>
cmpq   $0x0,-0x18(%rbp)
jle    1274 <daemon+0x104>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    1291 <daemon+0x121>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <daemon+0x128>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
test   %edi,%edi
je     11a6 <daemon+0x36>
test   %ebp,%ebp
je     11be <daemon+0x4e>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     121a <daemon+0xaa>
jne    1231 <daemon+0xc1>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
jmp    1228 <daemon+0xb8>
lea    0xe53(%rip),%rdi
call   1040 <chdir@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1228 <daemon+0xb8>
jmp    1179 <daemon+0x9>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   1080 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1228 <daemon+0xb8>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     1221 <daemon+0xb1>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     1221 <daemon+0xb1>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     1221 <daemon+0xb1>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    117d <daemon+0xd>
mov    $0xffffffff,%ebx
jmp    1228 <daemon+0xb8>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
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"
@O_RDWR = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %10
store i32 -1, i32* %3
br label %56
14: ; preds = %10, %2
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %41, label %17
17: ; preds = %14
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %3
br label %56
23: ; preds = %17
%24 = load i32, i32* %7
%25 = call i64 @dup2(i32 %24, i32 0)
%26 = icmp slt i64 %25, 0
br i1 %26, label %35, label %27
27: ; preds = %23
%28 = load i32, i32* %7
%29 = call i64 @dup2(i32 %28, i32 1)
%30 = icmp slt i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i32, i32* %7
%33 = call i64 @dup2(i32 %32, i32 2)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31, %27, %23
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %3
br label %56
38: ; preds = %31
%39 = load i32, i32* %7
%40 = call i32 @close(i32 %39)
br label %41
41: ; preds = %38, %14
%42 = call i64 (...) @fork()
store i64 %42, i64* %6
%43 = load i64, i64* %6
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %41
store i32 -1, i32* %3
br label %56
46: ; preds = %41
%47 = load i64, i64* %6
%48 = icmp sgt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @_exit(i32 0)
unreachable
51: ; preds = %46
%52 = call i64 (...) @setsid()
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %51
store i32 -1, i32* %3
br label %56
55: ; preds = %51
store i32 0, i32* %3
br label %56
56: ; preds = %55, %54, %45, %35, %22, %13
%57 = load i32, i32* %3
ret i32 %57
}

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 = icmp eq i32 %0, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %37
7: ; preds = %4, %2
%8 = icmp eq i32 %1, 0
br i1 %8, label %9, label %26
9: ; preds = %7
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %37, label %13
13: ; preds = %9
%14 = tail call i64 @dup2(i32 %11, i32 0)
%15 = icmp slt i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = tail call i64 @dup2(i32 %11, i32 1)
%18 = icmp slt i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = tail call i64 @dup2(i32 %11, i32 2)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19, %16, %13
%23 = tail call i32 @close(i32 %11)
br label %37
24: ; preds = %19
%25 = tail call i32 @close(i32 %11)
br label %26
26: ; preds = %24, %7
%27 = tail call i64 (...) @fork()
%28 = icmp slt i64 %27, 0
br i1 %28, label %37, label %29
29: ; preds = %26
%30 = icmp eq i64 %27, 0
br i1 %30, label %33, label %31
31: ; preds = %29
%32 = tail call i32 @_exit(i32 0)
unreachable
33: ; preds = %29
%34 = tail call i64 (...) @setsid()
%35 = ashr i64 %34, 63
%36 = trunc i64 %35 to i32
br label %37
37: ; preds = %33, %26, %22, %9, %4
%38 = phi i32 [ -1, %4 ], [ -1, %9 ], [ -1, %22 ], [ -1, %26 ], [ %36, %33 ]
ret i32 %38
}