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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int setsid();
int /*<<< orphan*/ stderr;
extern int daemonize(const char *opt_pid_file_path) {
  int fd, i;
  FILE *pidfile;
  pid_t pid;
  switch (pid = fork()) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    if (!(pidfile = fopen(opt_pid_file_path, "w+"))) {
      fprintf(stderr, "Could not open pid file\n");
      exit(1);
    }
    fprintf(pidfile, "%d", pid);
    fclose(pidfile);
    exit(0);
  }
  if (setsid() == -1)
    return -1;
  (void)chdir("/");
  if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
    (void)dup2(fd, STDIN_FILENO);
    (void)dup2(fd, STDOUT_FILENO);
    (void)dup2(fd, STDERR_FILENO);
    if (fd > 2)
      (void)close(fd);
  }
  for (i = 3; i < 255; i++)
    close(i);
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x24(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     11d9 <daemonize+0x39>
jmp    11c9 <daemonize+0x29>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     11e5 <daemonize+0x45>
jmp    11ea <daemonize+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <daemonize+0x175>
jmp    124f <daemonize+0xaf>
mov    -0x10(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    122a <daemonize+0x8a>
mov    0x2dc1(%rip),%rax
movslq (%rax),%rdi
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    126b <daemonize+0xcb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <daemonize+0x175>
lea    0xdad(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
lea    0xd9a(%rip),%rdi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     12e4 <daemonize+0x144>
mov    -0x14(%rbp),%edi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmpl   $0x2,-0x14(%rbp)
jle    12df <daemonize+0x13f>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    12e4 <daemonize+0x144>
movl   $0x3,-0x18(%rbp)
cmpl   $0xff,-0x18(%rbp)
jge    130e <daemonize+0x16e>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12eb <daemonize+0x14b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     126b <daemonize+0xbb>
test   %ebx,%ebx
jne    1275 <daemonize+0xc5>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     1266 <daemonize+0xb6>
lea    0xe37(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    0xe24(%rip),%rdi
xor    %edx,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1247 <daemonize+0x97>
mov    %eax,%ebx
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    $0x3,%ebx
jl     1247 <daemonize+0x97>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x3,%ebx
nopl   0x0(%rax)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    $0xff,%ebx
jne    1250 <daemonize+0xa0>
xor    %ebx,%ebx
jmp    126b <daemonize+0xbb>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd84(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
jne    12b3 <daemonize+0x103>
mov    0x2d40(%rip),%rax
movslq (%rax),%rcx
lea    0xd69(%rip),%rdi
mov    $0x18,%esi
mov    $0x1,%edx
call   10c0 <fwrite@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rax,%r14
lea    0xd5f(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Could not open pid file\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [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
define i32 @daemonize(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
%8 = call i32 (...) @fork()
store i32 %8, i32* %7
switch i32 %8, label %11 [
i32 -1, label %9
i32 0, label %10
]
9: ; preds = %1
store i32 -1, i32* %2
br label %64
10: ; preds = %1
br label %28
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %21, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = sext i32 %16 to i64
%18 = inttoptr i64 %17 to i32*
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %11
%22 = load i32*, i32** %6
%23 = load i32, i32* %7
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32*, i32** %6
%26 = call i32 @fclose(i32* %25)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %10
%29 = call i32 (...) @setsid()
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 -1, i32* %2
br label %64
32: ; preds = %28
%33 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* @O_RDWR
%35 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %34, i32 0)
store i32 %35, i32* %4
%36 = icmp ne i32 %35, -1
br i1 %36, label %37, label %53
37: ; preds = %32
%38 = load i32, i32* %4
%39 = load i32, i32* @STDIN_FILENO
%40 = call i32 @dup2(i32 %38, i32 %39)
%41 = load i32, i32* %4
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i32 @dup2(i32 %41, i32 %42)
%44 = load i32, i32* %4
%45 = load i32, i32* @STDERR_FILENO
%46 = call i32 @dup2(i32 %44, i32 %45)
%47 = load i32, i32* %4
%48 = icmp sgt i32 %47, 2
br i1 %48, label %49, label %52
49: ; preds = %37
%50 = load i32, i32* %4
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49, %37
br label %53
53: ; preds = %52, %32
store i32 3, i32* %5
br label %54
54: ; preds = %60, %53
%55 = load i32, i32* %5
%56 = icmp slt i32 %55, 255
br i1 %56, label %57, label %63
57: ; preds = %54
%58 = load i32, i32* %5
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57
%61 = load i32, i32* %5
%62 = add nsw i32 %61, 1
store i32 %62, i32* %5
br label %54
63: ; preds = %54
store i32 0, i32* %2
br label %64
64: ; preds = %63, %31, %9
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Could not open pid file\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [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
define i32 @daemonize(i8* nocapture %0) {
%2 = tail call i32 (...) @fork()
switch i32 %2, label %3 [
i32 -1, label %40
i32 0, label %16
]
3: ; preds = %1
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i32*
%10 = tail call i64 @fwrite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 24, i64 1, i32* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %3
%13 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%14 = tail call i32 @fclose(i32* %4)
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %1
%17 = tail call i32 (...) @setsid()
%18 = icmp eq i32 %17, -1
br i1 %18, label %40, label %19
19: ; preds = %16
%20 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @O_RDWR
%22 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %34, label %24
24: ; preds = %19
%25 = load i32, i32* @STDIN_FILENO
%26 = tail call i32 @dup2(i32 %22, i32 %25)
%27 = load i32, i32* @STDOUT_FILENO
%28 = tail call i32 @dup2(i32 %22, i32 %27)
%29 = load i32, i32* @STDERR_FILENO
%30 = tail call i32 @dup2(i32 %22, i32 %29)
%31 = icmp sgt i32 %22, 2
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = tail call i32 @close(i32 %22)
br label %34
34: ; preds = %24, %32, %19
br label %35
35: ; preds = %34, %35
%36 = phi i32 [ %38, %35 ], [ 3, %34 ]
%37 = tail call i32 @close(i32 %36)
%38 = add nuw nsw i32 %36, 1
%39 = icmp eq i32 %38, 255
br i1 %39, label %40, label %35
40: ; preds = %35, %16, %1
%41 = phi i32 [ %2, %1 ], [ -1, %16 ], [ 0, %35 ]
ret i32 %41
}