#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_CONS;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int);
int setsid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(char *program_name, int facility) {
  pid_t pid;
  switch (pid = fork()) {
  case -1:
    fprintf(stderr, "error on fork(): %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  case 0:
    openlog(program_name, LOG_CONS, facility);
    break;
  default:
    exit(EXIT_SUCCESS);
  }
  if (setsid() == -1) {
    syslog(LOG_ERR, "error on setsid(): %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  switch (pid = fork()) {
  case -1:
    syslog(LOG_ERR, "error on fork(): %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  case 0:
    break;
  default:
    exit(EXIT_SUCCESS);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  if (chdir("/") == -1)
    syslog(LOG_WARNING, "error on chdir(): %s\n", strerror(errno));
  umask(0);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x10(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11dc <daemonize+0x3c>
jmp    11cc <daemonize+0x2c>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     1216 <daemonize+0x76>
jmp    1230 <daemonize+0x90>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xdfd(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1090 <openlog@plt>
jmp    123e <daemonize+0x9e>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1288 <daemonize+0xe8>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xda0(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x10(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
sub    $0xffffffff,%eax
je     12b5 <daemonize+0x115>
jmp    12a5 <daemonize+0x105>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     12ef <daemonize+0x14f>
jmp    12f4 <daemonize+0x154>
mov    0x2cd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd24(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1302 <daemonize+0x162>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2c97(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2c93(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
lea    0xcf9(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    136d <daemonize+0x1cd>
mov    0x2c98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2c74(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xcc6(%rip),%rsi
call   1080 <syslog@plt>
xor    %edi,%edi
call   1070 <umask@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    1259 <daemonize+0xb9>
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1090 <openlog@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
je     12bb <daemonize+0x11b>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    1288 <daemonize+0xe8>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
lea    0xe12(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    124c <daemonize+0xac>
mov    0x2db5(%rip),%rax
mov    (%rax),%ebx
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdec(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <syslog@plt>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <umask@plt>
cmp    $0xffffffff,%eax
jne    12ad <daemonize+0x10d>
mov    0x2d6b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd84(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
jmp    12e3 <daemonize+0x143>
cmp    $0xffffffff,%eax
jne    12ad <daemonize+0x10d>
mov    0x2cfc(%rip),%rax
mov    (%rax),%ebx
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd55(%rip),%rsi
jmp    12d9 <daemonize+0x139>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%ebx
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd3c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <syslog@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"error on fork(): %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@LOG_CONS = global i32 0
@EXIT_SUCCESS = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [23 x i8] c"error on setsid(): %s\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [2 x i8] c"/\00"
@LOG_WARNING = global i32 0
@.str.3 = [22 x i8] c"error on chdir(): %s\0A\00"
define void @daemonize(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = call i32 (...) @fork()
store i32 %6, i32* %5
switch i32 %6, label %19 [
i32 -1, label %7
i32 0, label %14
]
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = load i32, i32* @LOG_CONS
%17 = load i32, i32* %4
%18 = call i32 @openlog(i8* %15, i32 %16, i32 %17)
br label %22
19: ; preds = %2
%20 = load i32, i32* @EXIT_SUCCESS
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %14
%23 = call i32 (...) @setsid()
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i32, i32* @LOG_ERR
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @syslog(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %22
%33 = call i32 (...) @fork()
store i32 %33, i32* %5
switch i32 %33, label %42 [
i32 -1, label %34
i32 0, label %41
]
34: ; preds = %32
%35 = load i32, i32* @LOG_ERR
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @syslog(i32 %35, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %32
br label %45
42: ; preds = %32
%43 = load i32, i32* @EXIT_SUCCESS
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %41
%46 = load i32, i32* @STDIN_FILENO
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* @STDOUT_FILENO
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* @STDERR_FILENO
%51 = call i32 @close(i32 %50)
%52 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %59
54: ; preds = %45
%55 = load i32, i32* @LOG_WARNING
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 @syslog(i32 %55, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %54, %45
%60 = call i32 @umask(i32 0)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"error on fork(): %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@LOG_CONS = global i32 0
@EXIT_SUCCESS = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [23 x i8] c"error on setsid(): %s\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [2 x i8] c"/\00"
@LOG_WARNING = global i32 0
@.str.3 = [22 x i8] c"error on chdir(): %s\0A\00"
define void @daemonize(i8* %0, i32 %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %16 [
i32 -1, label %4
i32 0, label %11
]
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = load i32, i32* @LOG_CONS
%13 = tail call i32 @openlog(i8* %0, i32 %12, i32 %1)
%14 = tail call i32 (...) @setsid()
%15 = icmp eq i32 %14, -1
br i1 %15, label %19, label %26
16: ; preds = %2
%17 = load i32, i32* @EXIT_SUCCESS
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = load i32, i32* @LOG_ERR
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 @syslog(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %11
%27 = tail call i32 (...) @fork()
switch i32 %27, label %35 [
i32 -1, label %28
i32 0, label %38
]
28: ; preds = %26
%29 = load i32, i32* @LOG_ERR
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 @syslog(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %26
%36 = load i32, i32* @EXIT_SUCCESS
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %26
%39 = load i32, i32* @STDIN_FILENO
%40 = tail call i32 @close(i32 %39)
%41 = load i32, i32* @STDOUT_FILENO
%42 = tail call i32 @close(i32 %41)
%43 = load i32, i32* @STDERR_FILENO
%44 = tail call i32 @close(i32 %43)
%45 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %38
%48 = load i32, i32* @LOG_WARNING
%49 = load i32, i32* @errno
%50 = tail call i8* @strerror(i32 %49)
%51 = tail call i32 @syslog(i32 %48, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %47, %38
%53 = tail call i32 @umask(i32 0)
ret void
}