#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;
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ clearenv();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int get_open_max();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int);
int daemonize(void) {
  int maxfd;
  int tmpfd;
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    return -1;
  } else if (pid) {
    exit(0);
  }
  setsid();
  clearenv();
  pid = fork();
  if (pid < 0) {
    return -1;
  } else if (pid) {
    exit(0);
  }
  if (chdir("/usr/local/share/adivix/srv") < 0) {
    return -1;
  }
  if (chroot("/usr/local/share/adivix/srv") < 0) {
    return -1;
  }
  umask(0337);
  maxfd = get_open_max();
  for (tmpfd = 0; tmpfd < maxfd; tmpfd++) {
    close(tmpfd);
  }
  tmpfd = open("/dev/null", O_RDWR, 0);
  if (tmpfd >= 0) {
    dup2(tmpfd, 0);
    dup2(tmpfd, 1);
    dup2(tmpfd, 2);
  }
  if (tmpfd > 2) {
    close(tmpfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11da <daemonize+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <daemonize+0x16a>
cmpq   $0x0,-0x18(%rbp)
je     11ec <daemonize+0x3c>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    11f1 <daemonize+0x41>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    $0x0,%al
call   1040 <clearenv@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1221 <daemonize+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <daemonize+0x16a>
cmpq   $0x0,-0x18(%rbp)
je     1233 <daemonize+0x83>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    1238 <daemonize+0x88>
lea    0xdc1(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    125a <daemonize+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <daemonize+0x16a>
lea    0xd9f(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
jge    127c <daemonize+0xcc>
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <daemonize+0x16a>
mov    $0xdf,%edi
call   1090 <umask@plt>
mov    $0x0,%al
call   10b0 <get_open_max@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12b9 <daemonize+0x109>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1297 <daemonize+0xe7>
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
lea    0xd53(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1301 <daemonize+0x151>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
cmpl   $0x2,-0xc(%rbp)
jle    1313 <daemonize+0x163>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
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
xor    %eax,%eax
call   10d0 <fork@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     129b <daemonize+0xeb>
jne    12a3 <daemonize+0xf3>
xor    %eax,%eax
call   1080 <setsid@plt>
xor    %eax,%eax
call   1040 <clearenv@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     129b <daemonize+0xeb>
jne    12a3 <daemonize+0xf3>
lea    0xe05(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
js     129b <daemonize+0xeb>
lea    0xdf0(%rip),%rdi
call   1030 <chroot@plt>
test   %rax,%rax
js     129b <daemonize+0xeb>
mov    $0xdf,%edi
call   1090 <umask@plt>
xor    %r14d,%r14d
xor    %eax,%eax
call   10b0 <get_open_max@plt>
test   %eax,%eax
jle    124e <daemonize+0x9e>
mov    %eax,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1240 <daemonize+0x90>
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
lea    0xdbe(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     129b <daemonize+0xeb>
mov    %eax,%ebp
xor    %r14d,%r14d
mov    %eax,%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
cmp    $0x3,%ebp
jb     129b <daemonize+0xeb>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   10c0 <exit@plt>

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

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