#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*/ LOG_ERR;
int /*<<< orphan*/ LOG_NOTICE;
int LOG_NOWAIT;
int LOG_PID;
int /*<<< orphan*/ LOG_USER;
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*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char const *, int, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
pid_t create_daemon(const char *bin) {
  pid_t pid, sid;
  pid = fork();
  if (pid != 0) {
    return pid;
  }
  umask(0);
  openlog(bin, LOG_NOWAIT | LOG_PID, LOG_USER);
  syslog(LOG_NOTICE, "Daemon successfully started");
  sid = setsid();
  if (sid < 0) {
    syslog(LOG_ERR, "Could not create process group");
    exit(EXIT_FAILURE);
  }
  if ((chdir("/")) < 0) {
    syslog(LOG_ERR, "Could not change working directory\n");
    exit(EXIT_FAILURE);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  return pid;
}


x86 assembly (clang, -O0):
<create_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11af <create_daemon+0x2f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1293 <create_daemon+0x113>
xor    %edi,%edi
call   1060 <umask@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
call   1080 <openlog@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1070 <syslog@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1228 <create_daemon+0xa8>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
call   1070 <syslog@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xe0c(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1261 <create_daemon+0xe1>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1070 <syslog@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_daemon>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1220 <create_daemon+0xa0>
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2e20(%rip),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1080 <openlog@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
call   1070 <syslog@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     122b <create_daemon+0xab>
lea    0xe4f(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     123d <create_daemon+0xbd>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
jmp    124d <create_daemon+0xcd>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
call   1070 <syslog@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@LOG_NOWAIT = global i32 0
@LOG_PID = global i32 0
@LOG_USER = global i32 0
@LOG_NOTICE = global i32 0
@.str = [28 x i8] c"Daemon successfully started\00"
@LOG_ERR = global i32 0
@.str.1 = [31 x i8] c"Could not create process group\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [36 x i8] c"Could not change working directory\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i64 @create_daemon(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %3
%6 = call i64 (...) @fork()
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load i64, i64* %4
store i64 %10, i64* %2
br label %45
11: ; preds = %1
%12 = call i32 @umask(i32 0)
%13 = load i8*, i8** %3
%14 = load i32, i32* @LOG_NOWAIT
%15 = load i32, i32* @LOG_PID
%16 = or i32 %14, %15
%17 = load i32, i32* @LOG_USER
%18 = call i32 @openlog(i8* %13, i32 %16, i32 %17)
%19 = load i32, i32* @LOG_NOTICE
%20 = call i32 @syslog(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%21 = call i64 (...) @setsid()
store i64 %21, i64* %5
%22 = load i64, i64* %5
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %11
%25 = load i32, i32* @LOG_ERR
%26 = call i32 @syslog(i32 %25, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %11
%30 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* @LOG_ERR
%34 = call i32 @syslog(i32 %33, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %29
%38 = load i32, i32* @STDIN_FILENO
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @STDOUT_FILENO
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* @STDERR_FILENO
%43 = call i32 @close(i32 %42)
%44 = load i64, i64* %4
store i64 %44, i64* %2
br label %45
45: ; preds = %37, %9
%46 = load i64, i64* %2
ret i64 %46
}

LLVM IR (-O2):
@LOG_NOWAIT = global i32 0
@LOG_PID = global i32 0
@LOG_USER = global i32 0
@LOG_NOTICE = global i32 0
@.str = [28 x i8] c"Daemon successfully started\00"
@LOG_ERR = global i32 0
@.str.1 = [31 x i8] c"Could not create process group\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [36 x i8] c"Could not change working directory\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i64 @create_daemon(i8* %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %35
4: ; preds = %1
%5 = tail call i32 @umask(i32 0)
%6 = load i32, i32* @LOG_NOWAIT
%7 = load i32, i32* @LOG_PID
%8 = or i32 %7, %6
%9 = load i32, i32* @LOG_USER
%10 = tail call i32 @openlog(i8* %0, i32 %8, i32 %9)
%11 = load i32, i32* @LOG_NOTICE
%12 = tail call i32 @syslog(i32 %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%13 = tail call i64 (...) @setsid()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %4
%16 = load i32, i32* @LOG_ERR
%17 = tail call i32 @syslog(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %4
%21 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i32, i32* @LOG_ERR
%25 = tail call i32 @syslog(i32 %24, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %20
%29 = load i32, i32* @STDIN_FILENO
%30 = tail call i32 @close(i32 %29)
%31 = load i32, i32* @STDOUT_FILENO
%32 = tail call i32 @close(i32 %31)
%33 = load i32, i32* @STDERR_FILENO
%34 = tail call i32 @close(i32 %33)
br label %35
35: ; preds = %1, %28
ret i64 %2
}