#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*/ SIGTSTP;
int /*<<< orphan*/ SIGTTIN;
int /*<<< orphan*/ SIGTTOU;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
scalar_t__ geteuid();
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
void daemonize(void) {
  pid_t pid;
  pid_t sid;
  if (getuid() != geteuid()) {
    fprintf(stderr, "this program does not work as setuid\n");
    exit(1);
  }
  pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(1);
  }
  if (pid)
    exit(0);
  sid = setsid();
  if (sid < 0) {
    perror("setsid");
    exit(1);
  }
  if (!freopen("/dev/null", "r", stdin) || !freopen("/dev/null", "w", stdout) ||
      !freopen("/dev/null", "w", stderr)) {
    perror("/dev/null");
    exit(1);
  }
  signal(SIGTSTP, SIG_IGN);
  signal(SIGTTOU, SIG_IGN);
  signal(SIGTTIN, SIG_IGN);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
cmp    %rcx,%rax
je     11d9 <daemonize+0x49>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1205 <daemonize+0x75>
lea    0xe30(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     1217 <daemonize+0x87>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1243 <daemonize+0xb3>
lea    0xdf7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
lea    0xddf(%rip),%rdi
lea    0xde2(%rip),%rsi
call   1050 <freopen@plt>
cmp    $0x0,%eax
je     12b2 <daemonize+0x122>
mov    0x2d41(%rip),%rax
mov    (%rax),%edx
lea    0xdba(%rip),%rdi
lea    0xdbf(%rip),%rsi
call   1050 <freopen@plt>
cmp    $0x0,%eax
je     12b2 <daemonize+0x122>
mov    0x2d24(%rip),%rax
mov    (%rax),%edx
lea    0xd95(%rip),%rdi
lea    0xd9a(%rip),%rsi
call   1050 <freopen@plt>
cmp    $0x0,%eax
jne    12c8 <daemonize+0x138>
lea    0xd79(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
mov    0x2cd9(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
mov    0x2cc2(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <geteuid@plt>
cmp    %rax,%rbx
jne    1272 <daemonize+0xe2>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1291 <daemonize+0x101>
jne    129a <daemonize+0x10a>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     12a1 <daemonize+0x111>
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
lea    0xe51(%rip),%rdi
lea    0xe54(%rip),%rsi
call   1050 <freopen@plt>
test   %eax,%eax
je     1269 <daemonize+0xd9>
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
lea    0xe31(%rip),%rdi
lea    0xe36(%rip),%rsi
call   1050 <freopen@plt>
test   %eax,%eax
je     1269 <daemonize+0xd9>
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
lea    0xe11(%rip),%rdi
lea    0xe16(%rip),%rsi
call   1050 <freopen@plt>
test   %eax,%eax
je     1269 <daemonize+0xd9>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    0x2d87(%rip),%rbx
mov    (%rbx),%esi
call   1070 <signal@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1070 <signal@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
pop    %rbx
jmp    1070 <signal@plt>
lea    0xdc2(%rip),%rdi
jmp    12a8 <daemonize+0x118>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
lea    0xd7e(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd8e(%rip),%rdi
jmp    12a8 <daemonize+0x118>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xd83(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"this program does not work as setuid\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [7 x i8] c"setsid\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@.str.4 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.5 = [2 x i8] c"w\00"
@stdout = global i32 0
@SIGTSTP = global i32 0
@SIG_IGN = global i32 0
@SIGTTOU = global i32 0
@SIGTTIN = global i32 0
define void @daemonize() {
%1 = alloca i64
%2 = alloca i64
%3 = call i64 (...) @getuid()
%4 = call i64 (...) @geteuid()
%5 = icmp ne i64 %3, %4
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i64 (...) @fork()
store i64 %11, i64* %1
%12 = load i64, i64* %1
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = load i64, i64* %1
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %17
%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 %29
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = load i32, i32* @stdin
%31 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %29
%34 = load i32, i32* @stdout
%35 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %37, %33, %29
%42 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %37
%45 = load i32, i32* @SIGTSTP
%46 = load i32, i32* @SIG_IGN
%47 = call i32 @signal(i32 %45, i32 %46)
%48 = load i32, i32* @SIGTTOU
%49 = load i32, i32* @SIG_IGN
%50 = call i32 @signal(i32 %48, i32 %49)
%51 = load i32, i32* @SIGTTIN
%52 = load i32, i32* @SIG_IGN
%53 = call i32 @signal(i32 %51, i32 %52)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"this program does not work as setuid\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [7 x i8] c"setsid\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@.str.4 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.5 = [2 x i8] c"w\00"
@stdout = global i32 0
@SIGTSTP = global i32 0
@SIG_IGN = global i32 0
@SIGTTOU = global i32 0
@SIGTTIN = global i32 0
define void @daemonize() {
%1 = tail call i64 (...) @getuid()
%2 = tail call i64 (...) @geteuid()
%3 = icmp eq i64 %1, %2
br i1 %3, label %8, label %4
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = icmp eq i64 %9, 0
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %14
%19 = tail call i64 (...) @setsid()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %18
%25 = load i32, i32* @stdin
%26 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %24
%29 = load i32, i32* @stdout
%30 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32, %28, %24
%37 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %32
%40 = load i32, i32* @SIGTSTP
%41 = load i32, i32* @SIG_IGN
%42 = tail call i32 @signal(i32 %40, i32 %41)
%43 = load i32, i32* @SIGTTOU
%44 = load i32, i32* @SIG_IGN
%45 = tail call i32 @signal(i32 %43, i32 %44)
%46 = load i32, i32* @SIGTTIN
%47 = load i32, i32* @SIG_IGN
%48 = tail call i32 @signal(i32 %46, i32 %47)
ret void
}