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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ FATAL(char *);
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, scalar_t__);
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(const char *path) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    FILE *file = fopen(path, "w");
    if (file == NULL) {
      FATAL("Invalid pid file\n");
    }
    fprintf(file, "%d", pid);
    fclose(file);
    exit(EXIT_SUCCESS);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    exit(EXIT_FAILURE);
  }
  if ((chdir("/")) < 0) {
    exit(EXIT_FAILURE);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11d0 <daemonize+0x30>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    1231 <daemonize+0x91>
mov    -0x8(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1206 <daemonize+0x66>
lea    0xe01(%rip),%rdi
call   1030 <FATAL@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdff(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   1090 <umask@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    125c <daemonize+0xbc>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xdb4(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    1280 <daemonize+0xe0>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     120a <daemonize+0x6a>
jne    1218 <daemonize+0x78>
xor    %edi,%edi
call   1090 <umask@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     120a <daemonize+0x6a>
lea    0xe48(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
js     120a <daemonize+0x6a>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <close@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    %rax,%rbx
lea    0xdde(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    123e <daemonize+0x9e>
lea    0xdc9(%rip),%rdi
call   1030 <FATAL@plt>
lea    0xdcf(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [18 x i8] c"Invalid pid file\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @daemonize(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32*
store i8* %0, i8** %2
%6 = call i64 (...) @fork()
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = load i64, i64* %3
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %30
15: ; preds = %12
%16 = load i8*, i8** %2
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %5
%18 = load i32*, i32** %5
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @FATAL(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i32*, i32** %5
%24 = load i64, i64* %3
%25 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 %24)
%26 = load i32*, i32** %5
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* @EXIT_SUCCESS
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %12
%31 = call i32 @umask(i32 0)
%32 = call i64 (...) @setsid()
store i64 %32, i64* %4
%33 = load i64, i64* %4
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %30
%39 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %38
%45 = load i32, i32* @STDIN_FILENO
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* @STDOUT_FILENO
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* @STDERR_FILENO
%50 = call i32 @close(i32 %49)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [18 x i8] c"Invalid pid file\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @daemonize(i8* nocapture %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %1
%8 = icmp eq i64 %2, 0
br i1 %8, label %19, label %9
9: ; preds = %7
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @FATAL(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = tail call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 %2)
%16 = tail call i32 @fclose(i32* %10)
%17 = load i32, i32* @EXIT_SUCCESS
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %7
%20 = tail call i32 @umask(i32 0)
%21 = tail call i64 (...) @setsid()
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %19
%27 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %26
%33 = load i32, i32* @STDIN_FILENO
%34 = tail call i32 @close(i32 %33)
%35 = load i32, i32* @STDOUT_FILENO
%36 = tail call i32 @close(i32 %35)
%37 = load i32, i32* @STDERR_FILENO
%38 = tail call i32 @close(i32 %37)
ret void
}