#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__ uid_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
scalar_t__ check_process_running(char const *, scalar_t__ *);
scalar_t__ daemon(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
scalar_t__ getpid();
scalar_t__ getuid();
scalar_t__ setup_sigmask();
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ update_pidfile(char const *);
void daemon_init(const char *prog, int nofork) {
  uid_t uid;
  pid_t pid;
  uid = getuid();
  if (uid) {
    syslog(LOG_ERR, "daemon_init: Sorry, only root wants to run this.\n");
    exit(1);
  }
  if (check_process_running(prog, &pid) && (pid != getpid())) {
    syslog(LOG_ERR, "daemon_init: Process \"%s\" already running.\n", prog);
    exit(1);
  }
  if (setup_sigmask() < 0) {
    syslog(LOG_ERR, "daemon_init: Unable to set signal mask.\n");
    exit(1);
  }
  if (!nofork && daemon(0, 0)) {
    syslog(LOG_ERR, "daemon_init: Unable to daemonize.\n");
    exit(1);
  }
  update_pidfile(prog);
}


x86 assembly (clang, -O0):
<daemon_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c6 <daemon_init+0x46>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <check_process_running@plt>
cmp    $0x0,%rax
je     1221 <daemon_init+0xa1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     1221 <daemon_init+0xa1>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <setup_sigmask@plt>
cmp    $0x0,%rax
jge    1253 <daemon_init+0xd3>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1291 <daemon_init+0x111>
xor    %esi,%esi
mov    %esi,%edi
call   1070 <daemon@plt>
cmp    $0x0,%rax
je     1291 <daemon_init+0x111>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1090 <update_pidfile@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_init>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
jne    11ed <daemon_init+0x6d>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <check_process_running@plt>
test   %rax,%rax
je     11bd <daemon_init+0x3d>
mov    0x8(%rsp),%rbp
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    %rax,%rbp
jne    1211 <daemon_init+0x91>
xor    %eax,%eax
call   10a0 <setup_sigmask@plt>
test   %rax,%rax
js     11ff <daemon_init+0x7f>
test   %r14d,%r14d
jne    11dc <daemon_init+0x5c>
xor    %edi,%edi
xor    %esi,%esi
call   1070 <daemon@plt>
test   %rax,%rax
jne    1235 <daemon_init+0xb5>
mov    %rbx,%rdi
call   1090 <update_pidfile@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
jmp    1245 <daemon_init+0xc5>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
jmp    1245 <daemon_init+0xc5>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [50 x i8] c"daemon_init: Sorry, only root wants to run this.\0A\00"
@.str.1 = [44 x i8] c"daemon_init: Process \22%s\22 already running.\0A\00"
@.str.2 = [41 x i8] c"daemon_init: Unable to set signal mask.\0A\00"
@.str.3 = [35 x i8] c"daemon_init: Unable to daemonize.\0A\00"
define void @daemon_init(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = call i64 (...) @getuid()
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @LOG_ERR
%12 = call i32 (i32, i8*, ...) @syslog(i32 %11, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = call i64 @check_process_running(i8* %15, i64* %6)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = load i64, i64* %6
%20 = call i64 (...) @getpid()
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i32, i32* @LOG_ERR
%24 = load i8*, i8** %3
%25 = call i32 (i32, i8*, ...) @syslog(i32 %23, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18, %14
%28 = call i64 (...) @setup_sigmask()
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @LOG_ERR
%32 = call i32 (i32, i8*, ...) @syslog(i32 %31, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = load i32, i32* %4
%36 = icmp ne i32 %35, 0
br i1 %36, label %44, label %37
37: ; preds = %34
%38 = call i64 @daemon(i32 0, i32 0)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i32, i32* @LOG_ERR
%42 = call i32 (i32, i8*, ...) @syslog(i32 %41, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %37, %34
%45 = load i8*, i8** %3
%46 = call i32 @update_pidfile(i8* %45)
ret void
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [50 x i8] c"daemon_init: Sorry, only root wants to run this.\0A\00"
@.str.1 = [44 x i8] c"daemon_init: Process \22%s\22 already running.\0A\00"
@.str.2 = [41 x i8] c"daemon_init: Unable to set signal mask.\0A\00"
@.str.3 = [35 x i8] c"daemon_init: Unable to daemonize.\0A\00"
define void @daemon_init(i8* %0, i32 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = tail call i64 (...) @getuid()
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i32, i32* @LOG_ERR
%9 = tail call i32 (i32, i8*, ...) @syslog(i32 %8, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = call i64 @check_process_running(i8* %0, i64* %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %11
%15 = load i64, i64* %3
%16 = call i64 (...) @getpid()
%17 = icmp eq i64 %15, %16
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i32, i32* @LOG_ERR
%20 = call i32 (i32, i8*, ...) @syslog(i32 %19, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14, %11
%23 = call i64 (...) @setup_sigmask()
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @LOG_ERR
%27 = call i32 (i32, i8*, ...) @syslog(i32 %26, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = icmp eq i32 %1, 0
br i1 %30, label %31, label %38
31: ; preds = %29
%32 = call i64 @daemon(i32 0, i32 0)
%33 = icmp eq i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = load i32, i32* @LOG_ERR
%36 = call i32 (i32, i8*, ...) @syslog(i32 %35, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31, %29
%39 = call i32 @update_pidfile(i8* %0)
ret void
}