#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 /*<<< orphan*/ LOG_LOCAL1;
int LOG_NDELAY;
int /*<<< orphan*/ LOG_NOTICE;
int LOG_PID;
int /*<<< orphan*/ LOG_UPTO(int /*<<< orphan*/);
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*/ getuid();
int /*<<< orphan*/ openlog(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setlogmask(int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize() {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  setlogmask(LOG_UPTO(LOG_NOTICE));
  openlog("lm352emon", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
  syslog(LOG_NOTICE, "Program started by User %d", getuid());
  sid = setsid();
  if (sid < 0) {
    exit(EXIT_FAILURE);
  }
  if ((chdir("/")) < 0) {
    exit(EXIT_FAILURE);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11dc <daemonize+0x2c>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11f5 <daemonize+0x45>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1030 <LOG_UPTO@plt>
mov    %eax,%edi
call   1090 <setlogmask@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    0x2db7(%rip),%rax
or     (%rax),%esi
mov    0x2d66(%rip),%rax
or     (%rax),%esi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
lea    0xdc4(%rip),%rdi
call   10b0 <openlog@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xdaa(%rip),%rsi
call   10a0 <syslog@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1289 <daemonize+0xd9>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xd95(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    12ad <daemonize+0xfd>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1274 <daemonize+0xc4>
jne    1282 <daemonize+0xd2>
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2ddb(%rip),%rbx
mov    (%rbx),%edi
call   1030 <LOG_UPTO@plt>
mov    %eax,%edi
call   1090 <setlogmask@plt>
mov    0x2de6(%rip),%rax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d94(%rip),%rax
or     (%rax),%esi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
lea    0xdf2(%rip),%rdi
call   10b0 <openlog@plt>
mov    (%rbx),%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
lea    0xde7(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   10a0 <syslog@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     1274 <daemonize+0xc4>
lea    0xde6(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     1274 <daemonize+0xc4>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
pop    %rbx
jmp    1060 <close@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@LOG_NOTICE = global i32 0
@.str = [10 x i8] c"lm352emon\00"
@LOG_CONS = global i32 0
@LOG_PID = global i32 0
@LOG_NDELAY = global i32 0
@LOG_LOCAL1 = global i32 0
@.str.1 = [27 x i8] c"Program started by User %d\00"
@.str.2 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @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_NOTICE
%18 = call i32 @LOG_UPTO(i32 %17)
%19 = call i32 @setlogmask(i32 %18)
%20 = load i32, i32* @LOG_CONS
%21 = load i32, i32* @LOG_PID
%22 = or i32 %20, %21
%23 = load i32, i32* @LOG_NDELAY
%24 = or i32 %22, %23
%25 = load i32, i32* @LOG_LOCAL1
%26 = call i32 @openlog(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32, i32* @LOG_NOTICE
%28 = call i32 (...) @getuid()
%29 = call i32 @syslog(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = call i64 (...) @setsid()
store i64 %30, i64* %2
%31 = load i64, i64* %2
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %15
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %15
%37 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %36
%43 = load i32, i32* @STDIN_FILENO
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* @STDOUT_FILENO
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* @STDERR_FILENO
%48 = call i32 @close(i32 %47)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@LOG_NOTICE = global i32 0
@.str = [10 x i8] c"lm352emon\00"
@LOG_CONS = global i32 0
@LOG_PID = global i32 0
@LOG_NDELAY = global i32 0
@LOG_LOCAL1 = global i32 0
@.str.1 = [27 x i8] c"Program started by User %d\00"
@.str.2 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @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_NOTICE
%14 = tail call i32 @LOG_UPTO(i32 %13)
%15 = tail call i32 @setlogmask(i32 %14)
%16 = load i32, i32* @LOG_CONS
%17 = load i32, i32* @LOG_PID
%18 = or i32 %17, %16
%19 = load i32, i32* @LOG_NDELAY
%20 = or i32 %18, %19
%21 = load i32, i32* @LOG_LOCAL1
%22 = tail call i32 @openlog(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %21)
%23 = load i32, i32* @LOG_NOTICE
%24 = tail call i32 (...) @getuid()
%25 = tail call i32 @syslog(i32 %23, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = tail call i64 (...) @setsid()
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %11
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %11
%32 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %31
%38 = load i32, i32* @STDIN_FILENO
%39 = tail call i32 @close(i32 %38)
%40 = load i32, i32* @STDOUT_FILENO
%41 = tail call i32 @close(i32 %40)
%42 = load i32, i32* @STDERR_FILENO
%43 = tail call i32 @close(i32 %42)
ret void
}