#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 */
struct passwd {
  int /*<<< orphan*/ pw_uid;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_NOTICE;
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);
scalar_t__ geteuid;
struct passwd *getpwnam(char *);
scalar_t__ getuid();
scalar_t__ setsid();
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize() {
  if ((getuid() == 0) || (geteuid == 0)) {
    struct passwd *pw = getpwnam("daemon");
    if (pw) {
      syslog(LOG_NOTICE, "Setting User To: "
                         "daemon");
      setuid(pw->pw_uid);
    }
  }
  if ((chdir("/")) < 0) {
    syslog(LOG_ERR, "ERROR: chdir() failed");
    exit(1);
  }
  umask(0);
  if (setsid() < 0) {
    syslog(LOG_ERR, "ERROR: setsid() failed");
    exit(1);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    $0x0,%rax
je     11ba <daemonize+0x2a>
mov    0x2e30(%rip),%rax
cmpq   $0x0,(%rax)
jne    11fa <daemonize+0x6a>
lea    0xe50(%rip),%rdi
call   1070 <getpwnam@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11f5 <daemonize+0x65>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1090 <syslog@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   10b0 <setuid@plt>
jmp    11fa <daemonize+0x6a>
lea    0xe17(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    122f <daemonize+0x9f>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    1266 <daemonize+0xd6>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     11aa <daemonize+0x1a>
mov    0x2e3c(%rip),%rax
cmpq   $0x0,(%rax)
jne    11da <daemonize+0x4a>
lea    0xe60(%rip),%rdi
call   1070 <getpwnam@plt>
test   %rax,%rax
je     11da <daemonize+0x4a>
mov    %rax,%rbx
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1090 <syslog@plt>
mov    (%rbx),%edi
call   10b0 <setuid@plt>
lea    0xe37(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     1229 <daemonize+0x99>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     123b <daemonize+0xab>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
pop    %rbx
jmp    1050 <close@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
jmp    124b <daemonize+0xbb>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@geteuid = global i64 0
@.str = [7 x i8] c"daemon\00"
@LOG_NOTICE = global i32 0
@.str.1 = [24 x i8] c"Setting User To: daemon\00"
@.str.2 = [2 x i8] c"/\00"
@LOG_ERR = global i32 0
@.str.3 = [22 x i8] c"ERROR: chdir() failed\00"
@.str.4 = [23 x i8] c"ERROR: setsid() failed\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @daemonize() {
%1 = alloca %struct.passwd*
%2 = call i64 (...) @getuid()
%3 = icmp eq i64 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = load i64, i64* @geteuid
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %19
7: ; preds = %4, %0
%8 = call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store %struct.passwd* %8, %struct.passwd** %1
%9 = load %struct.passwd*, %struct.passwd** %1
%10 = icmp ne %struct.passwd* %9, null
br i1 %10, label %11, label %18
11: ; preds = %7
%12 = load i32, i32* @LOG_NOTICE
%13 = call i32 @syslog(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%14 = load %struct.passwd*, %struct.passwd** %1
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @setuid(i32 %16)
br label %18
18: ; preds = %11, %7
br label %19
19: ; preds = %18, %4
%20 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @LOG_ERR
%24 = call i32 @syslog(i32 %23, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = call i32 @umask(i32 0)
%28 = call i64 (...) @setsid()
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* @LOG_ERR
%32 = call i32 @syslog(i32 %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = load i32, i32* @STDIN_FILENO
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* @STDOUT_FILENO
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* @STDERR_FILENO
%40 = call i32 @close(i32 %39)
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@geteuid = global i64 0
@.str = [7 x i8] c"daemon\00"
@LOG_NOTICE = global i32 0
@.str.1 = [24 x i8] c"Setting User To: daemon\00"
@.str.2 = [2 x i8] c"/\00"
@LOG_ERR = global i32 0
@.str.3 = [22 x i8] c"ERROR: chdir() failed\00"
@.str.4 = [23 x i8] c"ERROR: setsid() failed\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @daemonize() {
%1 = tail call i64 (...) @getuid()
%2 = icmp eq i64 %1, 0
%3 = load i64, i64* @geteuid
%4 = icmp eq i64 %3, 0
%5 = select i1 %2, i1 true, i1 %4
br i1 %5, label %6, label %15
6: ; preds = %0
%7 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = load i32, i32* @LOG_NOTICE
%11 = tail call i32 @syslog(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i32 @setuid(i32 %13)
br label %15
15: ; preds = %6, %9, %0
%16 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @LOG_ERR
%20 = tail call i32 @syslog(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = tail call i32 @umask(i32 0)
%24 = tail call i64 (...) @setsid()
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @LOG_ERR
%28 = tail call i32 @syslog(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = load i32, i32* @STDIN_FILENO
%32 = tail call i32 @close(i32 %31)
%33 = load i32, i32* @STDOUT_FILENO
%34 = tail call i32 @close(i32 %33)
%35 = load i32, i32* @STDERR_FILENO
%36 = tail call i32 @close(i32 %35)
ret void
}