#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ewc(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int setsid();
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int);
void daemon_create() {
  pid_t pid, sid;
  if ((pid = fork()) > 0) {
    FILE *temp_pid;
    if ((temp_pid = fopen("/tmp/joystick-control-pid", "w")) == NULL)
      ewc(EXIT_FAILURE, "Failed to open the /tmp/joystick-control-pid for "
                        "saving the session ID.");
    fprintf(temp_pid, "%d", pid);
    fclose(temp_pid);
    ewc(EXIT_SUCCESS, "Created a child successfully, PID: %d.", pid);
  } else if (pid < 0)
    ewc(EXIT_FAILURE, "Error: Created a child unsuccessfully.");
  if ((sid = setsid()) == -1)
    ewc(EXIT_FAILURE, "Granted a session ID unsuccessfully.");
  fprintf(stdout, "Session ID: %d.\n", sid);
  fprintf(stdout, "Changing PWD to / ...\n");
  if ((chdir("/")) < 0)
    ewc(EXIT_FAILURE, "Failed to change PWD to /.");
  fprintf(stdout, "Closing FDs 0, 1, 2 ...\n");
  close(0);
  close(1);
  close(2);
  umask(027);
}


x86 assembly (clang, -O0):
<daemon_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jle    1222 <daemon_create+0x92>
lea    0xe4e(%rip),%rdi
lea    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11e3 <daemon_create+0x53>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1090 <ewc@plt>
mov    -0x10(%rbp),%rax
mov    %eax,%edi
mov    -0x4(%rbp),%edx
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1090 <ewc@plt>
jmp    1248 <daemon_create+0xb8>
cmpl   $0x0,-0x4(%rbp)
jge    1243 <daemon_create+0xb3>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1090 <ewc@plt>
jmp    1248 <daemon_create+0xb8>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1272 <daemon_create+0xe2>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1090 <ewc@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xe58(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    12d0 <daemon_create+0x140>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1090 <ewc@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    $0x17,%edi
call   1080 <umask@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_create>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jle    1207 <daemon_create+0x77>
mov    %eax,%ebp
lea    0xe59(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d2 <daemon_create+0x42>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
xor    %eax,%eax
call   1090 <ewc@plt>
lea    0xe8b(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <ewc@plt>
jmp    1220 <daemon_create+0x90>
jns    1220 <daemon_create+0x90>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
xor    %eax,%eax
call   1090 <ewc@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1245 <daemon_create+0xb5>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
xor    %eax,%eax
call   1090 <ewc@plt>
mov    0x2d8c(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe84(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xe82(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xe8b(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jns    1298 <daemon_create+0x108>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
xor    %eax,%eax
call   1090 <ewc@plt>
mov    0x0(%rbp),%edi
lea    0xe7d(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    $0x17,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <umask@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"/tmp/joystick-control-pid\00"
@.str.1 = [2 x i8] c"w\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [72 x i8] c"Failed to open the /tmp/joystick-control-pid for saving the session ID.\00"
@.str.3 = [3 x i8] c"%d\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [39 x i8] c"Created a child successfully, PID: %d.\00"
@.str.5 = [39 x i8] c"Error: Created a child unsuccessfully.\00"
@.str.6 = [37 x i8] c"Granted a session ID unsuccessfully.\00"
@stdout = global i32 0
@.str.7 = [17 x i8] c"Session ID: %d.\0A\00"
@.str.8 = [23 x i8] c"Changing PWD to / ...\0A\00"
@.str.9 = [2 x i8] c"/\00"
@.str.10 = [27 x i8] c"Failed to change PWD to /.\00"
@.str.11 = [25 x i8] c"Closing FDs 0, 1, 2 ...\0A\00"
define void @daemon_create() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = call i32 (...) @fork()
store i32 %4, i32* %1
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %22
6: ; preds = %0
%7 = call i32* @fopen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 (i32, i8*, ...) @ewc(i32 %10, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %9, %6
%13 = load i32*, i32** %3
%14 = ptrtoint i32* %13 to i32
%15 = load i32, i32* %1
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = load i32*, i32** %3
%18 = call i32 @fclose(i32* %17)
%19 = load i32, i32* @EXIT_SUCCESS
%20 = load i32, i32* %1
%21 = call i32 (i32, i8*, ...) @ewc(i32 %19, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %20)
br label %29
22: ; preds = %0
%23 = load i32, i32* %1
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 (i32, i8*, ...) @ewc(i32 %26, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %25, %22
br label %29
29: ; preds = %28, %12
%30 = call i32 (...) @setsid()
store i32 %30, i32* %2
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 (i32, i8*, ...) @ewc(i32 %33, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
br label %35
35: ; preds = %32, %29
%36 = load i32, i32* @stdout
%37 = load i32, i32* %2
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %37)
%39 = load i32, i32* @stdout
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
%41 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %35
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 (i32, i8*, ...) @ewc(i32 %44, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
br label %46
46: ; preds = %43, %35
%47 = load i32, i32* @stdout
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.11, i64 0, i64 0))
%49 = call i32 @close(i32 0)
%50 = call i32 @close(i32 1)
%51 = call i32 @close(i32 2)
%52 = call i32 @umask(i32 23)
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"/tmp/joystick-control-pid\00"
@.str.1 = [2 x i8] c"w\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [72 x i8] c"Failed to open the /tmp/joystick-control-pid for saving the session ID.\00"
@.str.3 = [3 x i8] c"%d\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [39 x i8] c"Created a child successfully, PID: %d.\00"
@.str.5 = [39 x i8] c"Error: Created a child unsuccessfully.\00"
@.str.6 = [37 x i8] c"Granted a session ID unsuccessfully.\00"
@stdout = global i32 0
@.str.7 = [17 x i8] c"Session ID: %d.\0A\00"
@.str.8 = [23 x i8] c"Changing PWD to / ...\0A\00"
@.str.9 = [2 x i8] c"/\00"
@.str.10 = [27 x i8] c"Failed to change PWD to /.\00"
@.str.11 = [25 x i8] c"Closing FDs 0, 1, 2 ...\0A\00"
define void @daemon_create() {
%1 = tail call i32 (...) @fork()
%2 = icmp sgt i32 %1, 0
br i1 %2, label %3, label %16
3: ; preds = %0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 (i32, i8*, ...) @ewc(i32 %7, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %6, %3
%10 = ptrtoint i32* %4 to i64
%11 = trunc i64 %10 to i32
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%13 = tail call i32 @fclose(i32* %4)
%14 = load i32, i32* @EXIT_SUCCESS
%15 = tail call i32 (i32, i8*, ...) @ewc(i32 %14, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %1)
br label %21
16: ; preds = %0
%17 = icmp slt i32 %1, 0
br i1 %17, label %18, label %21
18: ; preds = %16
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 (i32, i8*, ...) @ewc(i32 %19, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %21
21: ; preds = %16, %18, %9
%22 = tail call i32 (...) @setsid()
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 (i32, i8*, ...) @ewc(i32 %25, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
br label %27
27: ; preds = %24, %21
%28 = load i32, i32* @stdout
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %22)
%30 = load i32, i32* @stdout
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
%32 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 (i32, i8*, ...) @ewc(i32 %35, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
br label %37
37: ; preds = %34, %27
%38 = load i32, i32* @stdout
%39 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.11, i64 0, i64 0))
%40 = tail call i32 @close(i32 0)
%41 = tail call i32 @close(i32 1)
%42 = tail call i32 @close(i32 2)
%43 = tail call i32 @umask(i32 23)
ret void
}