#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ daemon(int /*<<< orphan*/, int /*<<< orphan*/);
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *get_env(char *, char const **);
int /*<<< orphan*/ stderr;
void daemonize(const char *envp[]) {
  const char *daemon_string = get_env("daemon", envp);
  if (daemon_string && daemon_string[0] == '1') {
    const char *log_redirect = get_env("daemon_log_redirect", envp);
    int fd = -1;
    if (log_redirect && log_redirect[0] == '1')
      fd = dup(2);
    if (daemon(0, 0) < 0) {
      fprintf(stderr, "DOWN-ROOT: daemonization failed\n");
    } else if (fd >= 3) {
      dup2(fd, 2);
      close(fd);
    }
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe89(%rip),%rdi
call   1080 <get_env@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1234 <daemonize+0xd4>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
jne    1234 <daemonize+0xd4>
mov    -0x8(%rbp),%rsi
lea    0xe61(%rip),%rdi
call   1080 <get_env@plt>
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11de <daemonize+0x7e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
jne    11de <daemonize+0x7e>
mov    $0x2,%edi
call   1040 <dup@plt>
mov    %eax,-0x1c(%rbp)
xor    %esi,%esi
mov    %esi,%edi
call   1070 <daemon@plt>
cmp    $0x0,%rax
jge    120b <daemonize+0xab>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
jmp    122f <daemonize+0xcf>
cmpl   $0x3,-0x1c(%rbp)
jl     122a <daemonize+0xca>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    122f <daemonize+0xcf>
jmp    1234 <daemonize+0xd4>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
mov    %rdi,%rbx
lea    0xe95(%rip),%rdi
mov    %rbx,%rsi
call   1080 <get_env@plt>
test   %rax,%rax
je     11ce <daemonize+0x6e>
cmpb   $0x31,(%rax)
jne    11ce <daemonize+0x6e>
lea    0xe83(%rip),%rdi
mov    %rbx,%rsi
call   1080 <get_env@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11a7 <daemonize+0x47>
cmpb   $0x31,(%rax)
jne    11a7 <daemonize+0x47>
mov    $0x2,%edi
call   1040 <dup@plt>
mov    %eax,%ebx
xor    %edi,%edi
xor    %esi,%esi
call   1070 <daemon@plt>
test   %rax,%rax
js     11d0 <daemonize+0x70>
cmp    $0x3,%ebx
jl     11ce <daemonize+0x6e>
mov    %ebx,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1050 <close@plt>
pop    %rbx
ret
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
pop    %rbx
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"daemon\00"
@.str.1 = [20 x i8] c"daemon_log_redirect\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"DOWN-ROOT: daemonization failed\0A\00"
define void @daemonize(i8** %0) {
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8** %0, i8*** %2
%6 = load i8**, i8*** %2
%7 = call i8* @get_env(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %6)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %45
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 49
br i1 %15, label %16, label %45
16: ; preds = %10
%17 = load i8**, i8*** %2
%18 = call i8* @get_env(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8** %17)
store i8* %18, i8** %4
store i32 -1, i32* %5
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %29
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 49
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @dup(i32 2)
store i32 %28, i32* %5
br label %29
29: ; preds = %27, %21, %16
%30 = call i64 @daemon(i32 0, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %44
35: ; preds = %29
%36 = load i32, i32* %5
%37 = icmp sge i32 %36, 3
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i32, i32* %5
%40 = call i32 @dup2(i32 %39, i32 2)
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %38, %35
br label %44
44: ; preds = %43, %32
br label %45
45: ; preds = %44, %10, %1
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"daemon\00"
@.str.1 = [20 x i8] c"daemon_log_redirect\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"DOWN-ROOT: daemonization failed\0A\00"
define void @daemonize(i8** %0) {
%2 = tail call i8* @get_env(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %27, label %4
4: ; preds = %1
%5 = load i8, i8* %2
%6 = icmp eq i8 %5, 49
br i1 %6, label %7, label %27
7: ; preds = %4
%8 = tail call i8* @get_env(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8** %0)
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = load i8, i8* %8
%12 = icmp eq i8 %11, 49
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @dup(i32 2)
br label %15
15: ; preds = %13, %10, %7
%16 = phi i32 [ %14, %13 ], [ -1, %10 ], [ -1, %7 ]
%17 = tail call i64 @daemon(i32 0, i32 0)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %27
22: ; preds = %15
%23 = icmp sgt i32 %16, 2
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = tail call i32 @dup2(i32 %16, i32 2)
%26 = tail call i32 @close(i32 %16)
br label %27
27: ; preds = %19, %24, %22, %4, %1
ret void
}