#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getdtablesize();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ umask(int);
int daemonize(char *directory) {
  int status = 0;
  int fd, i;
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    return -1;
  }
  if (pid > 0) {
    exit(0);
  }
  if (setsid() < 0) {
    status = -1;
  }
  if (directory) {
    if (chdir(directory)) {
      status = -1;
    }
  }
  for (i = getdtablesize(); i >= 0; --i)
    close(i);
  fd = open("/dev/null", O_RDWR, 0);
  if (fd != -1) {
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
    if (fd > 2) {
      close(fd);
    }
  }
  umask(027);
  return status;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11c5 <daemonize+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <daemonize+0x131>
cmpq   $0x0,-0x28(%rbp)
jle    11d7 <daemonize+0x47>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    11ef <daemonize+0x5f>
movl   $0xffffffff,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1219 <daemonize+0x89>
mov    -0x10(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     1214 <daemonize+0x84>
movl   $0xffffffff,-0x14(%rbp)
jmp    1219 <daemonize+0x89>
mov    $0x0,%al
call   10b0 <getdtablesize@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1243 <daemonize+0xb3>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1223 <daemonize+0x93>
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
lea    0xdad(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     12b1 <daemonize+0x121>
mov    -0x18(%rbp),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmpl   $0x2,-0x18(%rbp)
jle    12ac <daemonize+0x11c>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    12b1 <daemonize+0x121>
mov    $0x17,%edi
call   1070 <umask@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1252 <daemonize+0xc2>
jne    1260 <daemonize+0xd0>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
test   %rbp,%rbp
je     11d0 <daemonize+0x40>
mov    %rbp,%rdi
call   1030 <chdir@plt>
neg    %rax
sbb    %eax,%eax
or     %ebx,%eax
mov    %eax,%ebx
xor    %eax,%eax
call   10b0 <getdtablesize@plt>
test   %eax,%eax
js     11ec <daemonize+0x5c>
mov    %eax,%ebp
nopl   (%rax)
mov    %ebp,%edi
call   1050 <close@plt>
add    $0xffffffff,%ebp
jb     11e0 <daemonize+0x50>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    0xe04(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1246 <daemonize+0xb6>
mov    %eax,%ebp
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
cmp    $0x3,%ebp
jl     1246 <daemonize+0xb6>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0x17,%edi
call   1070 <umask@plt>
jmp    1257 <daemonize+0xc7>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1090 <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 i32 @daemonize(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %3
store i32 0, i32* %4
%8 = call i64 (...) @fork()
store i64 %8, i64* %7
%9 = load i64, i64* %7
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %65
12: ; preds = %1
%13 = load i64, i64* %7
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %12
%18 = call i64 (...) @setsid()
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 -1, i32* %4
br label %21
21: ; preds = %20, %17
%22 = load i8*, i8** %3
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i8*, i8** %3
%26 = call i64 @chdir(i8* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %4
br label %29
29: ; preds = %28, %24
br label %30
30: ; preds = %29, %21
%31 = call i32 (...) @getdtablesize()
store i32 %31, i32* %6
br label %32
32: ; preds = %38, %30
%33 = load i32, i32* %6
%34 = icmp sge i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35
%39 = load i32, i32* %6
%40 = add nsw i32 %39, -1
store i32 %40, i32* %6
br label %32
41: ; preds = %32
%42 = load i32, i32* @O_RDWR
%43 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %42, i32 0)
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = icmp ne i32 %44, -1
br i1 %45, label %46, label %62
46: ; preds = %41
%47 = load i32, i32* %5
%48 = load i32, i32* @STDIN_FILENO
%49 = call i32 @dup2(i32 %47, i32 %48)
%50 = load i32, i32* %5
%51 = load i32, i32* @STDOUT_FILENO
%52 = call i32 @dup2(i32 %50, i32 %51)
%53 = load i32, i32* %5
%54 = load i32, i32* @STDERR_FILENO
%55 = call i32 @dup2(i32 %53, i32 %54)
%56 = load i32, i32* %5
%57 = icmp sgt i32 %56, 2
br i1 %57, label %58, label %61
58: ; preds = %46
%59 = load i32, i32* %5
%60 = call i32 @close(i32 %59)
br label %61
61: ; preds = %58, %46
br label %62
62: ; preds = %61, %41
%63 = call i32 @umask(i32 23)
%64 = load i32, i32* %4
store i32 %64, i32* %2
br label %65
65: ; preds = %62, %11
%66 = load i32, i32* %2
ret i32 %66
}

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