#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 *, int);
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", (int)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    1233 <daemonize+0x93>
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),%rax
mov    %eax,%edx
lea    0xdfd(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d9c(%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    125e <daemonize+0xbe>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xdb2(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    1282 <daemonize+0xe2>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d33(%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    %ebx,%edx
call   1080 <fprintf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2d6a(%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 %31
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 = trunc i64 %24 to i32
%26 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i32*, i32** %5
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* @EXIT_SUCCESS
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %12
%32 = call i32 @umask(i32 0)
%33 = call i64 (...) @setsid()
store i64 %33, i64* %4
%34 = load i64, i64* %4
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %31
%40 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %39
%46 = load i32, i32* @STDIN_FILENO
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* @STDOUT_FILENO
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* @STDERR_FILENO
%51 = call i32 @close(i32 %50)
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 %20, 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 = trunc i64 %2 to i32
%16 = tail call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = tail call i32 @fclose(i32* %10)
%18 = load i32, i32* @EXIT_SUCCESS
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %7
%21 = tail call i32 @umask(i32 0)
%22 = tail call i64 (...) @setsid()
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %20
%28 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %27
%34 = load i32, i32* @STDIN_FILENO
%35 = tail call i32 @close(i32 %34)
%36 = load i32, i32* @STDOUT_FILENO
%37 = tail call i32 @close(i32 %36)
%38 = load i32, i32* @STDERR_FILENO
%39 = tail call i32 @close(i32 %38)
ret void
}