#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*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_NOTICE;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemononize() {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    exit(3);
  }
  if (pid > 0) {
    exit(0);
  }
  if (setsid() < 0) {
    exit(3);
  }
  signal(SIGCHLD, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  pid = fork();
  if (pid < 0) {
    exit(3);
  }
  if (pid > 0) {
    exit(0);
  }
  umask(0);
  int n;
  for (n = sysconf(_SC_OPEN_MAX); n > 0; n--) {
    close(n);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  openlog("uberServer", LOG_PID, LOG_DAEMON);
  syslog(LOG_NOTICE, "DEAMON STARTED");
}


x86 assembly (clang, -O0):
<daemononize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11b8 <daemononize+0x28>
mov    $0x3,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11ca <daemononize+0x3a>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0x0,%rax
jge    11e5 <daemononize+0x55>
mov    $0x3,%edi
call   10a0 <exit@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1233 <daemononize+0xa3>
mov    $0x3,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    1245 <daemononize+0xb5>
xor    %edi,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    127d <daemononize+0xed>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    125d <daemononize+0xcd>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
mov    0x2d21(%rip),%rax
mov    (%rax),%edx
lea    0xd40(%rip),%rdi
call   1090 <openlog@plt>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
lea    0xd36(%rip),%rsi
call   1070 <syslog@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemononize>:
push   %rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     127f <daemononize+0xef>
jne    1289 <daemononize+0xf9>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
js     127f <daemononize+0xef>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rbx
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     127f <daemononize+0xef>
jne    1289 <daemononize+0xf9>
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
test   %eax,%eax
jle    1221 <daemononize+0x91>
mov    %eax,%ebx
nop
mov    %ebx,%edi
call   1030 <close@plt>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
ja     1210 <daemononize+0x80>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
lea    0xd9c(%rip),%rdi
call   1090 <openlog@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd92(%rip),%rsi
pop    %rbx
jmp    1070 <syslog@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@_SC_OPEN_MAX = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [11 x i8] c"uberServer\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
@LOG_NOTICE = global i32 0
@.str.1 = [15 x i8] c"DEAMON STARTED\00"
define void @daemononize() {
%1 = alloca i64
%2 = alloca i32
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = load i64, i64* %1
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @exit(i32 3)
unreachable
8: ; preds = %0
%9 = load i64, i64* %1
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %8
%14 = call i64 (...) @setsid()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @exit(i32 3)
unreachable
18: ; preds = %13
%19 = load i32, i32* @SIGCHLD
%20 = load i32, i32* @SIG_IGN
%21 = call i32 @signal(i32 %19, i32 %20)
%22 = load i32, i32* @SIGHUP
%23 = load i32, i32* @SIG_IGN
%24 = call i32 @signal(i32 %22, i32 %23)
%25 = call i64 (...) @fork()
store i64 %25, i64* %1
%26 = load i64, i64* %1
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %18
%29 = call i32 @exit(i32 3)
unreachable
30: ; preds = %18
%31 = load i64, i64* %1
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %30
%36 = call i32 @umask(i32 0)
%37 = load i32, i32* @_SC_OPEN_MAX
%38 = call i32 @sysconf(i32 %37)
store i32 %38, i32* %2
br label %39
39: ; preds = %45, %35
%40 = load i32, i32* %2
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %42
%46 = load i32, i32* %2
%47 = add nsw i32 %46, -1
store i32 %47, i32* %2
br label %39
48: ; preds = %39
%49 = load i32, i32* @STDIN_FILENO
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* @STDOUT_FILENO
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* @STDERR_FILENO
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* @LOG_PID
%56 = load i32, i32* @LOG_DAEMON
%57 = call i32 @openlog(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %55, i32 %56)
%58 = load i32, i32* @LOG_NOTICE
%59 = call i32 @syslog(i32 %58, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@_SC_OPEN_MAX = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [11 x i8] c"uberServer\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
@LOG_NOTICE = global i32 0
@.str.1 = [15 x i8] c"DEAMON STARTED\00"
define void @daemononize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @exit(i32 3)
unreachable
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %5
%10 = tail call i64 (...) @setsid()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @exit(i32 3)
unreachable
14: ; preds = %9
%15 = load i32, i32* @SIGCHLD
%16 = load i32, i32* @SIG_IGN
%17 = tail call i32 @signal(i32 %15, i32 %16)
%18 = load i32, i32* @SIGHUP
%19 = load i32, i32* @SIG_IGN
%20 = tail call i32 @signal(i32 %18, i32 %19)
%21 = tail call i64 (...) @fork()
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = tail call i32 @exit(i32 3)
unreachable
25: ; preds = %14
%26 = icmp eq i64 %21, 0
br i1 %26, label %29, label %27
27: ; preds = %25
%28 = tail call i32 @exit(i32 0)
unreachable
29: ; preds = %25
%30 = tail call i32 @umask(i32 0)
%31 = load i32, i32* @_SC_OPEN_MAX
%32 = tail call i32 @sysconf(i32 %31)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %29, %34
%35 = phi i32 [ %37, %34 ], [ %32, %29 ]
%36 = tail call i32 @close(i32 %35)
%37 = add nsw i32 %35, -1
%38 = icmp ugt i32 %35, 1
br i1 %38, label %34, label %39
39: ; preds = %34, %29
%40 = load i32, i32* @STDIN_FILENO
%41 = tail call i32 @close(i32 %40)
%42 = load i32, i32* @STDOUT_FILENO
%43 = tail call i32 @close(i32 %42)
%44 = load i32, i32* @STDERR_FILENO
%45 = tail call i32 @close(i32 %44)
%46 = load i32, i32* @LOG_PID
%47 = load i32, i32* @LOG_DAEMON
%48 = tail call i32 @openlog(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %46, i32 %47)
%49 = load i32, i32* @LOG_NOTICE
%50 = tail call i32 @syslog(i32 %49, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
ret void
}