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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
void daemonize(void) {
  int fd;
  FILE *fp;
  if (fork() != 0)
    exit(0);
  setsid();
  if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
    if (fd > STDERR_FILENO)
      close(fd);
  }
  fp = fopen("/var/run/redis.pid", "w");
  if (fp) {
    fprintf(fp, "%d\n", getpid());
    fclose(fp);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%rax
je     11c0 <daemonize+0x20>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe29(%rip),%rdi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
je     123c <daemonize+0x9c>
mov    -0x4(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x4(%rbp),%eax
mov    0x2da9(%rip),%rcx
cmp    (%rcx),%eax
jle    1237 <daemonize+0x97>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
jmp    123c <daemonize+0x9c>
lea    0xdc7(%rip),%rdi
lea    0xdd3(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1288 <daemonize+0xe8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%rdi
mov    %eax,%edx
lea    0xda5(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
jne    1260 <daemonize+0xc0>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdi
xor    %edx,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1216 <daemonize+0x76>
mov    %eax,%ebx
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
mov    0x2dd0(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    (%r14),%ebx
jle    1216 <daemonize+0x76>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xded(%rip),%rdi
lea    0xdf9(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1258 <daemonize+0xb8>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xde0(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1080 <fprintf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <fclose@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [19 x i8] c"/var/run/redis.pid\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define void @daemonize() {
%1 = alloca i32
%2 = alloca i32*
%3 = call i64 (...) @fork()
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = call i32 (...) @setsid()
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9, i32 0)
store i32 %10, i32* %1
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %29
12: ; preds = %7
%13 = load i32, i32* %1
%14 = load i32, i32* @STDIN_FILENO
%15 = call i32 @dup2(i32 %13, i32 %14)
%16 = load i32, i32* %1
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = load i32, i32* %1
%20 = load i32, i32* @STDERR_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = load i32, i32* %1
%23 = load i32, i32* @STDERR_FILENO
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %12
%26 = load i32, i32* %1
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %25, %12
br label %29
29: ; preds = %28, %7
%30 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %2
%31 = load i32*, i32** %2
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i32*, i32** %2
%35 = call i32 (...) @getpid()
%36 = call i32 @fprintf(i32* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = load i32*, i32** %2
%38 = call i32 @fclose(i32* %37)
br label %39
39: ; preds = %33, %29
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [19 x i8] c"/var/run/redis.pid\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define void @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %5, label %3
3: ; preds = %0
%4 = tail call i32 @exit(i32 0)
unreachable
5: ; preds = %0
%6 = tail call i32 (...) @setsid()
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %21, label %10
10: ; preds = %5
%11 = load i32, i32* @STDIN_FILENO
%12 = tail call i32 @dup2(i32 %8, i32 %11)
%13 = load i32, i32* @STDOUT_FILENO
%14 = tail call i32 @dup2(i32 %8, i32 %13)
%15 = load i32, i32* @STDERR_FILENO
%16 = tail call i32 @dup2(i32 %8, i32 %15)
%17 = load i32, i32* @STDERR_FILENO
%18 = icmp sgt i32 %8, %17
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = tail call i32 @close(i32 %8)
br label %21
21: ; preds = %10, %19, %5
%22 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = tail call i32 @fclose(i32* %22)
br label %28
28: ; preds = %24, %21
ret void
}