#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 */
struct rlimit {
  int rlim_max;
};
typedef int rlim_t;

/* Variables and functions */
int /*<<< orphan*/ EPERM;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ RLIMIT_NOFILE;
int RLIM_INFINITY;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ geteuid();
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int open(char *, int /*<<< orphan*/);
int setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize() {
  if (geteuid() != 0) {
    errno = EPERM;
    return -1;
  }
  switch (fork()) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    exit(0);
  }
  if (setsid() == -1)
    return -1;
  switch (fork()) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    exit(0);
  }
  umask(0);
  if (chdir("/") < 0)
    return -1;
  struct rlimit rlim;
  if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
    return -1;
  if (rlim.rlim_max == RLIM_INFINITY)
    rlim.rlim_max = 1024;
  for (rlim_t i = 0; i < rlim.rlim_max; i++)
    close(i);
  int fd0 = open("/dev/null", O_RDWR);
  int fd1 = dup(0);
  int fd2 = dup(0);
  if (fd0 != 0 || fd1 != 1 || fd2 != 2)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1050 <geteuid@plt>
cmp    $0x0,%rax
je     11d7 <daemonize+0x37>
mov    0x2df8(%rip),%rax
mov    (%rax),%ecx
mov    0x2e07(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1367 <daemonize+0x1c7>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     1201 <daemonize+0x61>
jmp    11f1 <daemonize+0x51>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     120d <daemonize+0x6d>
jmp    1212 <daemonize+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    1367 <daemonize+0x1c7>
jmp    1219 <daemonize+0x79>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1235 <daemonize+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    1367 <daemonize+0x1c7>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
sub    $0xffffffff,%eax
je     125f <daemonize+0xbf>
jmp    124f <daemonize+0xaf>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     126b <daemonize+0xcb>
jmp    1270 <daemonize+0xd0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1367 <daemonize+0x1c7>
jmp    1277 <daemonize+0xd7>
xor    %edi,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xd7b(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    12a0 <daemonize+0x100>
movl   $0xffffffff,-0x4(%rbp)
jmp    1367 <daemonize+0x1c7>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   10b0 <getrlimit@plt>
cmp    $0x0,%rax
jge    12c8 <daemonize+0x128>
movl   $0xffffffff,-0x4(%rbp)
jmp    1367 <daemonize+0x1c7>
mov    -0x8(%rbp),%eax
mov    0x2cf6(%rip),%rcx
cmp    (%rcx),%eax
jne    12e1 <daemonize+0x141>
movl   $0x400,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    130a <daemonize+0x16a>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12e8 <daemonize+0x148>
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
lea    0xce8(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x14(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1354 <daemonize+0x1b4>
cmpl   $0x1,-0x14(%rbp)
jne    1354 <daemonize+0x1b4>
cmpl   $0x2,-0x18(%rbp)
je     1360 <daemonize+0x1c0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1367 <daemonize+0x1c7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <geteuid@plt>
test   %rax,%rax
je     11cb <daemonize+0x2b>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    0x2e11(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    12b9 <daemonize+0x119>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     12b9 <daemonize+0x119>
test   %eax,%eax
jne    12c0 <daemonize+0x120>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     12b9 <daemonize+0x119>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     12b9 <daemonize+0x119>
test   %eax,%eax
jne    12c0 <daemonize+0x120>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xde0(%rip),%rdi
call   1030 <chdir@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
js     12b9 <daemonize+0x119>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   10b0 <getrlimit@plt>
test   %rax,%rax
js     1263 <daemonize+0xc3>
mov    (%rsp),%eax
mov    0x2d72(%rip),%rcx
cmp    (%rcx),%eax
jne    126a <daemonize+0xca>
movl   $0x400,(%rsp)
jmp    126e <daemonize+0xce>
mov    $0xffffffff,%eax
jmp    12b9 <daemonize+0x119>
test   %eax,%eax
jle    127f <daemonize+0xdf>
xor    %ebx,%ebx
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    (%rsp),%ebx
jl     1270 <daemonize+0xd0>
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
lea    0xd73(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,%ecx
xor    $0x1,%ebp
or     %ebx,%ebp
xor    $0x2,%ecx
xor    %eax,%eax
or     %ebp,%ecx
setne  %al
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@EPERM = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"/\00"
@RLIMIT_NOFILE = global i32 0
@RLIM_INFINITY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @daemonize() {
%1 = alloca %struct.rlimit
%2 = tail call i64 (...) @geteuid()
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = load i32, i32* @EPERM
store i32 %5, i32* @errno
br label %54
6: ; preds = %0
%7 = tail call i32 (...) @fork()
switch i32 %7, label %8 [
i32 -1, label %54
i32 0, label %10
]
8: ; preds = %6
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %6
%11 = tail call i32 (...) @setsid()
%12 = icmp eq i32 %11, -1
br i1 %12, label %54, label %13
13: ; preds = %10
%14 = tail call i32 (...) @fork()
switch i32 %14, label %15 [
i32 -1, label %54
i32 0, label %17
]
15: ; preds = %13
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %13
%18 = tail call i32 @umask(i32 0)
%19 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp slt i64 %19, 0
br i1 %20, label %54, label %21
21: ; preds = %17
%22 = bitcast %struct.rlimit* %1 to i8*
%23 = load i32, i32* @RLIMIT_NOFILE
%24 = call i64 @getrlimit(i32 %23, %struct.rlimit* %1)
%25 = icmp slt i64 %24, 0
br i1 %25, label %52, label %26
26: ; preds = %21
%27 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @RLIM_INFINITY
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 1024, i32* %27
br label %34
32: ; preds = %26
%33 = icmp sgt i32 %28, 0
br i1 %33, label %34, label %35
34: ; preds = %31, %32
br label %46
35: ; preds = %46, %32
%36 = load i32, i32* @O_RDWR
%37 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = call i32 @dup(i32 0)
%39 = call i32 @dup(i32 0)
%40 = icmp ne i32 %37, 0
%41 = icmp ne i32 %38, 1
%42 = select i1 %40, i1 true, i1 %41
%43 = icmp ne i32 %39, 2
%44 = select i1 %42, i1 true, i1 %43
%45 = sext i1 %44 to i32
br label %52
46: ; preds = %34, %46
%47 = phi i32 [ %49, %46 ], [ 0, %34 ]
%48 = call i32 @close(i32 %47)
%49 = add nuw nsw i32 %47, 1
%50 = load i32, i32* %27
%51 = icmp slt i32 %49, %50
br i1 %51, label %46, label %35
52: ; preds = %21, %35
%53 = phi i32 [ %45, %35 ], [ -1, %21 ]
br label %54
54: ; preds = %17, %13, %10, %6, %52, %4
%55 = phi i32 [ -1, %4 ], [ %53, %52 ], [ %7, %6 ], [ -1, %10 ], [ %14, %13 ], [ -1, %17 ]
ret i32 %55
}