#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ chdir(char *);
scalar_t__ close(int);
scalar_t__ dup2(int, int);
int fork();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
int daemonize(int nochdir, int noclose) {
  int fd;
  switch (fork()) {
  case -1:
    return (-1);
  case 0:
    break;
  default:
    _exit(EXIT_SUCCESS);
  }
  if (setsid() == -1)
    return (-1);
  if (nochdir == 0) {
    if (chdir("/") != 0) {
      perror("chdir");
      return (-1);
    }
  }
  if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
    if (dup2(fd, STDIN_FILENO) < 0) {
      perror("dup2 stdin");
      return (-1);
    }
    if (dup2(fd, STDOUT_FILENO) < 0) {
      perror("dup2 stdout");
      return (-1);
    }
    if (dup2(fd, STDERR_FILENO) < 0) {
      perror("dup2 stderr");
      return (-1);
    }
    if (fd > STDERR_FILENO) {
      if (close(fd) < 0) {
        perror("close");
        return (-1);
      }
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11b8 <daemonize+0x38>
jmp    11a8 <daemonize+0x28>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11c4 <daemonize+0x44>
jmp    11c9 <daemonize+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <daemonize+0x1c3>
jmp    11d7 <daemonize+0x57>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11f3 <daemonize+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <daemonize+0x1c3>
cmpl   $0x0,-0x8(%rbp)
jne    1230 <daemonize+0xb0>
lea    0xdfc(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     122b <daemonize+0xab>
lea    0xde8(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <daemonize+0x1c3>
jmp    1230 <daemonize+0xb0>
cmpl   $0x0,-0xc(%rbp)
jne    133c <daemonize+0x1bc>
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
lea    0xdbe(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     133c <daemonize+0x1bc>
mov    -0x10(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1290 <daemonize+0x110>
lea    0xd93(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <daemonize+0x1c3>
mov    -0x10(%rbp),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    12c3 <daemonize+0x143>
lea    0xd6b(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <daemonize+0x1c3>
mov    -0x10(%rbp),%edi
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    12f6 <daemonize+0x176>
lea    0xd44(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <daemonize+0x1c3>
mov    -0x10(%rbp),%eax
mov    0x2cd0(%rip),%rcx
cmp    (%rcx),%eax
jle    1337 <daemonize+0x1b7>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    1332 <daemonize+0x1b2>
lea    0xd14(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <daemonize+0x1c3>
jmp    1337 <daemonize+0x1b7>
jmp    133c <daemonize+0x1bc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11d0 <daemonize+0x50>
test   %ebx,%ebx
jne    1282 <daemonize+0x102>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11d0 <daemonize+0x50>
test   %ebp,%ebp
jne    11c9 <daemonize+0x49>
lea    0xe45(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    1251 <daemonize+0xd1>
xor    %ebx,%ebx
test   %r14d,%r14d
je     11d7 <daemonize+0x57>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
lea    0xe21(%rip),%rdi
xor    %ebx,%ebx
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11d0 <daemonize+0x50>
mov    %eax,%ebp
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
test   %rax,%rax
js     125a <daemonize+0xda>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
test   %rax,%rax
js     1263 <daemonize+0xe3>
mov    0x2da8(%rip),%r14
mov    (%r14),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
test   %rax,%rax
js     126c <daemonize+0xec>
cmp    (%r14),%ebp
jle    11d0 <daemonize+0x50>
mov    %ebp,%edi
call   1060 <close@plt>
test   %rax,%rax
jns    11d0 <daemonize+0x50>
lea    0xde6(%rip),%rdi
jmp    1273 <daemonize+0xf3>
lea    0xdaa(%rip),%rdi
jmp    1273 <daemonize+0xf3>
lea    0xdb1(%rip),%rdi
jmp    1273 <daemonize+0xf3>
lea    0xdb3(%rip),%rdi
jmp    1273 <daemonize+0xf3>
lea    0xdb6(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11d0 <daemonize+0x50>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [6 x i8] c"chdir\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@.str.3 = [11 x i8] c"dup2 stdin\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [12 x i8] c"dup2 stdout\00"
@STDERR_FILENO = global i32 0
@.str.5 = [12 x i8] c"dup2 stderr\00"
@.str.6 = [6 x i8] c"close\00"
define i32 @daemonize(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = call i32 (...) @fork()
switch i32 %7, label %10 [
i32 -1, label %8
i32 0, label %9
]
8: ; preds = %2
store i32 -1, i32* %3
br label %67
9: ; preds = %2
br label %13
10: ; preds = %2
%11 = load i32, i32* @EXIT_SUCCESS
%12 = call i32 @_exit(i32 %11)
unreachable
13: ; preds = %9
%14 = call i32 (...) @setsid()
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 -1, i32* %3
br label %67
17: ; preds = %13
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
25: ; preds = %20
br label %26
26: ; preds = %25, %17
%27 = load i32, i32* %5
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %66
29: ; preds = %26
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 0)
store i32 %31, i32* %6
%32 = icmp ne i32 %31, -1
br i1 %32, label %33, label %66
33: ; preds = %29
%34 = load i32, i32* %6
%35 = load i32, i32* @STDIN_FILENO
%36 = call i64 @dup2(i32 %34, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
40: ; preds = %33
%41 = load i32, i32* %6
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i64 @dup2(i32 %41, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
47: ; preds = %40
%48 = load i32, i32* %6
%49 = load i32, i32* @STDERR_FILENO
%50 = call i64 @dup2(i32 %48, i32 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
54: ; preds = %47
%55 = load i32, i32* %6
%56 = load i32, i32* @STDERR_FILENO
%57 = icmp sgt i32 %55, %56
br i1 %57, label %58, label %65
58: ; preds = %54
%59 = load i32, i32* %6
%60 = call i64 @close(i32 %59)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
64: ; preds = %58
br label %65
65: ; preds = %64, %54
br label %66
66: ; preds = %65, %29, %26
store i32 0, i32* %3
br label %67
67: ; preds = %66, %62, %52, %45, %38, %23, %16, %8
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [6 x i8] c"chdir\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@.str.3 = [11 x i8] c"dup2 stdin\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [12 x i8] c"dup2 stdout\00"
@STDERR_FILENO = global i32 0
@.str.5 = [12 x i8] c"dup2 stderr\00"
@.str.6 = [6 x i8] c"close\00"
define i32 @daemonize(i32 %0, i32 %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %4 [
i32 -1, label %42
i32 0, label %7
]
4: ; preds = %2
%5 = load i32, i32* @EXIT_SUCCESS
%6 = tail call i32 @_exit(i32 %5)
unreachable
7: ; preds = %2
%8 = tail call i32 (...) @setsid()
%9 = icmp eq i32 %8, -1
br i1 %9, label %42, label %10
10: ; preds = %7
%11 = icmp eq i32 %0, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %39
15: ; preds = %12, %10
%16 = icmp eq i32 %1, 0
br i1 %16, label %17, label %42
17: ; preds = %15
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %42, label %21
21: ; preds = %17
%22 = load i32, i32* @STDIN_FILENO
%23 = tail call i64 @dup2(i32 %19, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %39, label %25
25: ; preds = %21
%26 = load i32, i32* @STDOUT_FILENO
%27 = tail call i64 @dup2(i32 %19, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %39, label %29
29: ; preds = %25
%30 = load i32, i32* @STDERR_FILENO
%31 = tail call i64 @dup2(i32 %19, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %29
%34 = load i32, i32* @STDERR_FILENO
%35 = icmp sgt i32 %19, %34
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = tail call i64 @close(i32 %19)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36, %29, %25, %21, %12
%40 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %12 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), %21 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), %25 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), %29 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), %36 ]
%41 = tail call i32 @perror(i8* %40)
br label %42
42: ; preds = %39, %15, %17, %36, %33, %7, %2
%43 = phi i32 [ %3, %2 ], [ -1, %7 ], [ 0, %33 ], [ 0, %36 ], [ 0, %17 ], [ 0, %15 ], [ -1, %39 ]
ret i32 %43
}