#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 int /*<<< orphan*/ uid_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ geteuid();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setreuid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
void create_daemon(void) {
  pid_t pid;
  uid_t euid;
  pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(1);
  } else if (pid != 0) {
    exit(0);
  }
  close(0);
  close(1);
  close(2);
  open("/dev/null", O_RDWR);
  open("/dev/null", O_RDWR);
  open("/dev/null", O_RDWR);
  if (chdir("/")) {
  }
  (void)setsid();
  euid = geteuid();
  if (setreuid(euid, euid) < 0)
    die("setreuid");
}


x86 assembly (clang, -O0):
<create_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11d2 <create_daemon+0x32>
lea    0xe3d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
je     11e3 <create_daemon+0x43>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    11e8 <create_daemon+0x48>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
lea    0xdf2(%rip),%rdi
call   1080 <open@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    0xddd(%rip),%rdi
call   1080 <open@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    0xdc8(%rip),%rdi
call   1080 <open@plt>
lea    0xdc6(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     125d <create_daemon+0xbd>
jmp    125d <create_daemon+0xbd>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1070 <setreuid@plt>
cmp    $0x0,%rax
jge    128f <create_daemon+0xef>
lea    0xd87(%rip),%rdi
call   1090 <die@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_daemon>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    1240 <create_daemon+0xa0>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2e13(%rip),%r14
mov    (%r14),%esi
lea    0xe26(%rip),%rbx
mov    %rbx,%rdi
call   1080 <open@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
lea    0xe0b(%rip),%rdi
call   1030 <chdir@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
xor    %eax,%eax
call   1040 <geteuid@plt>
mov    %eax,%edi
mov    %eax,%esi
call   1070 <setreuid@plt>
test   %rax,%rax
js     122d <create_daemon+0x8d>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xddd(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1090 <die@plt>
cmp    $0xffffffff,%eax
jne    125b <create_daemon+0xbb>
lea    0xdb4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [9 x i8] c"setreuid\00"
define void @create_daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @fork()
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* %1
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %9
br label %15
15: ; preds = %14
%16 = call i32 @close(i32 0)
%17 = call i32 @close(i32 1)
%18 = call i32 @close(i32 2)
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %15
br label %28
28: ; preds = %27, %15
%29 = call i32 (...) @setsid()
%30 = call i32 (...) @geteuid()
store i32 %30, i32* %2
%31 = load i32, i32* %2
%32 = load i32, i32* %2
%33 = call i64 @setreuid(i32 %31, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %28
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [9 x i8] c"setreuid\00"
define void @create_daemon() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %5 [
i32 -1, label %2
i32 0, label %7
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @exit(i32 1)
unreachable
5: ; preds = %0
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = tail call i32 @close(i32 0)
%9 = tail call i32 @close(i32 1)
%10 = tail call i32 @close(i32 2)
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 (...) @setsid()
%19 = tail call i32 (...) @geteuid()
%20 = tail call i64 @setreuid(i32 %19, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %7
%23 = tail call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %7
ret void
}