#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*/ 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) {
      exit(EXIT_FAILURE);
    }
    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   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11c0 <daemonize+0x30>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    1225 <daemonize+0x95>
mov    -0x8(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f8 <daemonize+0x68>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
lea    0xdf9(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1250 <daemonize+0xc0>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdae(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1274 <daemonize+0xe4>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   1050 <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   10b0 <fork@plt>
test   %rax,%rax
js     1211 <daemonize+0x81>
jne    11fa <daemonize+0x6a>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     1211 <daemonize+0x81>
lea    0xe46(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     1211 <daemonize+0x81>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <close@plt>
mov    %rax,%rbx
lea    0xdfc(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
jne    121f <daemonize+0x8f>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %rax,%r14
lea    0xdd9(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
call   1070 <fprintf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [3 x i8] c"%d\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [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 %32
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 %23
20: ; preds = %15
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %15
%24 = load i32*, i32** %5
%25 = load i64, i64* %3
%26 = trunc i64 %25 to i32
%27 = call i32 @fprintf(i32* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32*, i32** %5
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* @EXIT_SUCCESS
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %12
%33 = call i32 @umask(i32 0)
%34 = call i64 (...) @setsid()
store i64 %34, i64* %4
%35 = load i64, i64* %4
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %40
%47 = load i32, i32* @STDIN_FILENO
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* @STDOUT_FILENO
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* @STDERR_FILENO
%52 = call i32 @close(i32 %51)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [3 x i8] c"%d\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [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 %21, 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 %15
12: ; preds = %9
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %9
%16 = trunc i64 %2 to i32
%17 = tail call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = tail call i32 @fclose(i32* %10)
%19 = load i32, i32* @EXIT_SUCCESS
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %7
%22 = tail call i32 @umask(i32 0)
%23 = tail call i64 (...) @setsid()
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %21
%29 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @EXIT_FAILURE
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %28
%35 = load i32, i32* @STDIN_FILENO
%36 = tail call i32 @close(i32 %35)
%37 = load i32, i32* @STDOUT_FILENO
%38 = tail call i32 @close(i32 %37)
%39 = load i32, i32* @STDERR_FILENO
%40 = tail call i32 @close(i32 %39)
ret void
}