#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int LOG_CONS;
int LOG_PID;
int /*<<< orphan*/ LOG_USER;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(void) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  openlog("MPPTC", LOG_PID | LOG_CONS, LOG_USER);
  printf("Daemon Started.\n");
  sid = setsid();
  if (sid < 0) {
    printf("ERROR :: Unable to create new SID. Exiting.");
    exit(EXIT_FAILURE);
  }
  printf("finished forking");
  if ((chdir("/")) < 0) {
    printf("ERROR :: Unable to change working directory. Exiting");
    exit(EXIT_FAILURE);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11ac <daemonize+0x2c>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11c5 <daemonize+0x45>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    0x2dec(%rip),%rax
or     (%rax),%esi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
lea    0xe12(%rip),%rdi
call   1080 <openlog@plt>
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    122f <daemonize+0xaf>
lea    0xdfb(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
lea    0xe12(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    126b <daemonize+0xeb>
lea    0xdfe(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     125b <daemonize+0xcb>
jne    1238 <daemonize+0xa8>
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2e1b(%rip),%rax
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
lea    0xe30(%rip),%rdi
call   1090 <openlog@plt>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     1246 <daemonize+0xb6>
lea    0xe3e(%rip),%rdi
call   1050 <printf@plt>
lea    0xe43(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     124f <daemonize+0xbf>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdb9(%rip),%rdi
jmp    1256 <daemonize+0xc6>
lea    0xdef(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [6 x i8] c"MPPTC\00"
@LOG_PID = global i32 0
@LOG_CONS = global i32 0
@LOG_USER = global i32 0
@.str.1 = [17 x i8] c"Daemon Started.\0A\00"
@.str.2 = [44 x i8] c"ERROR :: Unable to create new SID. Exiting.\00"
@.str.3 = [17 x i8] c"finished forking\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [53 x i8] c"ERROR :: Unable to change working directory. Exiting\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize() {
%1 = alloca i64
%2 = alloca i64
%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 %9
6: ; preds = %0
%7 = load i32, i32* @EXIT_FAILURE
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = load i64, i64* %1
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @EXIT_SUCCESS
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %9
%16 = call i32 @umask(i32 0)
%17 = load i32, i32* @LOG_PID
%18 = load i32, i32* @LOG_CONS
%19 = or i32 %17, %18
%20 = load i32, i32* @LOG_USER
%21 = call i32 @openlog(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%23 = call i64 (...) @setsid()
store i64 %23, i64* %2
%24 = load i64, i64* %2
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %15
%27 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %15
%31 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%32 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %30
%39 = load i32, i32* @STDIN_FILENO
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @STDOUT_FILENO
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* @STDERR_FILENO
%44 = call i32 @close(i32 %43)
ret i32 0
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [6 x i8] c"MPPTC\00"
@LOG_PID = global i32 0
@LOG_CONS = global i32 0
@LOG_USER = global i32 0
@.str.2 = [44 x i8] c"ERROR :: Unable to create new SID. Exiting.\00"
@.str.3 = [17 x i8] c"finished forking\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [53 x i8] c"ERROR :: Unable to change working directory. Exiting\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@str = [16 x i8] c"Daemon Started.\00"
define i32 @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @EXIT_FAILURE
%5 = tail call i32 @exit(i32 %4)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %11, label %8
8: ; preds = %6
%9 = load i32, i32* @EXIT_SUCCESS
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %6
%12 = tail call i32 @umask(i32 0)
%13 = load i32, i32* @LOG_PID
%14 = load i32, i32* @LOG_CONS
%15 = or i32 %14, %13
%16 = load i32, i32* @LOG_USER
%17 = tail call i32 @openlog(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%19 = tail call i64 (...) @setsid()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %11
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %11
%26 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %25
%34 = load i32, i32* @STDIN_FILENO
%35 = tail call i32 @close(i32 %34)
%36 = load i32, i32* @STDOUT_FILENO
%37 = tail call i32 @close(i32 %36)
%38 = load i32, i32* @STDERR_FILENO
%39 = tail call i32 @close(i32 %38)
ret i32 0
}