#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char const *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int daemonize(const char *path) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0)
    return 0;
  if (pid > 0)
    exit(EXIT_SUCCESS);
  if (chdir(path) < 0) {
    char *cwd;
    fprintf(stderr,
            "[Error]Daemon: Error: Could not change working directory.\n");
    if (!(cwd = getcwd(NULL, 0))) {
      fprintf(stderr,
              "[Error]Daemon: Error: Could not get working directory.\n");
      exit(EXIT_FAILURE);
    }
    fprintf(stderr, "[Error]Daemon: From: %s to %s.\n", cwd, path);
    fprintf(stderr, "[Error]Daemon: Reason: %s.\n", strerror(errno));
    free(cwd);
    exit(EXIT_FAILURE);
  }
  sid = setsid();
  if (sid < 0) {
    fprintf(stderr,
            "[Error]Daemon: Error: Could not create new session group.\n");
    return 0;
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  return 1;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11be <daemonize+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    130d <daemonize+0x17d>
cmpq   $0x0,-0x18(%rbp)
jle    11d7 <daemonize+0x47>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    12a3 <daemonize+0x113>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    123f <daemonize+0xaf>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    12dc <daemonize+0x14c>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    130d <daemonize+0x17d>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     120a <daemonize+0x7a>
jne    1211 <daemonize+0x81>
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     121f <daemonize+0x8f>
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     11f3 <daemonize+0x63>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    $0x1,%ebp
jmp    120a <daemonize+0x7a>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xeac(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2db2(%rip),%r14
mov    (%r14),%edi
lea    0xdd0(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    (%r14),%edi
test   %rax,%rax
jne    1258 <daemonize+0xc8>
lea    0xdec(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    129b <daemonize+0x10b>
mov    %rax,%rbp
lea    0xe11(%rip),%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r14),%ebx
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe0c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [59 x i8] c"[Error]Daemon: Error: Could not change working directory.\0A\00"
@.str.1 = [56 x i8] c"[Error]Daemon: Error: Could not get working directory.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [32 x i8] c"[Error]Daemon: From: %s to %s.\0A\00"
@.str.3 = [28 x i8] c"[Error]Daemon: Reason: %s.\0A\00"
@errno = global i32 0
@.str.4 = [59 x i8] c"[Error]Daemon: Error: Could not create new session group.\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
%7 = call i64 (...) @fork()
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %58
11: ; preds = %1
%12 = load i64, i64* %4
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EXIT_SUCCESS
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %11
%18 = load i8*, i8** %3
%19 = call i64 @chdir(i8* %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %44
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%24 = call i8* @getcwd(i32* null, i32 0)
store i8* %24, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %21
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %6
%34 = load i8*, i8** %3
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %33, i8* %34)
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %38)
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %17
%45 = call i64 (...) @setsid()
store i64 %45, i64* %5
%46 = load i64, i64* %5
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
br label %58
51: ; preds = %44
%52 = load i32, i32* @STDIN_FILENO
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* @STDOUT_FILENO
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* @STDERR_FILENO
%57 = call i32 @close(i32 %56)
store i32 1, i32* %2
br label %58
58: ; preds = %51, %48, %10
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [59 x i8] c"[Error]Daemon: Error: Could not change working directory.\0A\00"
@.str.1 = [56 x i8] c"[Error]Daemon: Error: Could not get working directory.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [32 x i8] c"[Error]Daemon: From: %s to %s.\0A\00"
@.str.3 = [28 x i8] c"[Error]Daemon: Reason: %s.\0A\00"
@errno = global i32 0
@.str.4 = [59 x i8] c"[Error]Daemon: Error: Could not create new session group.\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize(i8* %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %44, label %4
4: ; preds = %1
%5 = icmp eq i64 %2, 0
br i1 %5, label %9, label %6
6: ; preds = %4
%7 = load i32, i32* @EXIT_SUCCESS
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %4
%10 = tail call i64 @chdir(i8* %0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %31
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%15 = tail call i8* @getcwd(i32* null, i32 0)
%16 = icmp eq i8* %15, null
%17 = load i32, i32* @stderr
br i1 %16, label %18, label %22
18: ; preds = %12
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %15, i8* %0)
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = tail call i32 @free(i8* %15)
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %9
%32 = tail call i64 (...) @setsid()
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0))
br label %44
37: ; preds = %31
%38 = load i32, i32* @STDIN_FILENO
%39 = tail call i32 @close(i32 %38)
%40 = load i32, i32* @STDOUT_FILENO
%41 = tail call i32 @close(i32 %40)
%42 = load i32, i32* @STDERR_FILENO
%43 = tail call i32 @close(i32 %42)
br label %44
44: ; preds = %1, %37, %34
%45 = phi i32 [ 0, %34 ], [ 1, %37 ], [ 0, %1 ]
ret i32 %45
}