#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;

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize() {
  pid_t process_id = 0;
  pid_t sid = 0;
  process_id = fork();
  if (process_id < 0) {
    printf("fork failed!\n");
    exit(1);
  }
  if (process_id > 0) {
    printf("process_id of child process %d \n", process_id);
    exit(0);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    exit(1);
  }
  chdir("/");
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movq   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11b6 <daemonize+0x46>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11da <daemonize+0x6a>
mov    -0x8(%rbp),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1201 <daemonize+0x91>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe27(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11d9 <daemonize+0x59>
jne    11ef <daemonize+0x6f>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     1207 <daemonize+0x87>
lea    0xe78(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
pop    %rax
jmp    1060 <close@plt>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe0a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"fork failed!\0A\00"
@.str.1 = [33 x i8] c"process_id of child process %d \0A\00"
@.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() {
%1 = alloca i64
%2 = alloca i64
store i64 0, i64* %1
store i64 0, i64* %2
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = load i64, i64* %1
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i64, i64* %1
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i64, i64* %1
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %13)
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %9
%17 = call i32 @umask(i32 0)
%18 = call i64 (...) @setsid()
store i64 %18, i64* %2
%19 = load i64, i64* %2
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* @STDOUT_FILENO
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @STDERR_FILENO
%30 = call i32 @close(i32 %29)
ret void
}

LLVM IR (-O2):
@.str.1 = [33 x i8] c"process_id of child process %d \0A\00"
@.str.2 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@str = [13 x i8] c"fork failed!\00"
define void @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %11, label %8
8: ; preds = %6
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %1)
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %6
%12 = tail call i32 @umask(i32 0)
%13 = tail call i64 (...) @setsid()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %11
%18 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @STDIN_FILENO
%20 = tail call i32 @close(i32 %19)
%21 = load i32, i32* @STDOUT_FILENO
%22 = tail call i32 @close(i32 %21)
%23 = load i32, i32* @STDERR_FILENO
%24 = tail call i32 @close(i32 %23)
ret void
}