#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ _SC_OPEN_MAX;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int lockPidFile();
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize() {
  int maxfd, fd;
  switch (fork()) {
  case -1:
    syslog(LOG_ERR, "Failed to fork : %m");
    return -1;
  case 0:
    break;
  default:
    exit(EXIT_SUCCESS);
  }
  if (setsid() < 0)
    return -1;
  switch (fork()) {
  case -1:
    syslog(LOG_ERR, "Failed to fork (2) : %m");
    return -1;
  case 0:
    break;
  default:
    exit(EXIT_SUCCESS);
  }
  umask(0);
  if (chdir("/") < 0)
    syslog(LOG_ERR, "Failed to chdir '/' : %m");
  maxfd = sysconf(_SC_OPEN_MAX);
  maxfd = maxfd != -1 ? maxfd : 4096;
  for (fd = 0; fd < maxfd; fd++)
    close(fd);
  if (!freopen("/dev/null", "r", stdin))
    syslog(LOG_DEBUG, "Failed to reopen stdin : %m");
  if (!freopen("/dev/null", "w", stdout))
    syslog(LOG_DEBUG, "Failed to reopen stout : %m");
  if (!freopen("/dev/null", "w", stderr))
    syslog(LOG_DEBUG, "Failed to reopen sterr : %m");
  return lockPidFile();
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11d2 <daemonize+0x32>
jmp    11c2 <daemonize+0x22>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11f3 <daemonize+0x53>
jmp    11f8 <daemonize+0x58>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1080 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cd <daemonize+0x22d>
jmp    1206 <daemonize+0x66>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    1223 <daemonize+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cd <daemonize+0x22d>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     124d <daemonize+0xad>
jmp    123d <daemonize+0x9d>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     126e <daemonize+0xce>
jmp    1273 <daemonize+0xd3>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
call   1080 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cd <daemonize+0x22d>
jmp    1281 <daemonize+0xe1>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xd9d(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    12b3 <daemonize+0x113>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xd80(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     12d9 <daemonize+0x139>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    12e6 <daemonize+0x146>
mov    $0x1000,%eax
mov    %eax,-0x18(%rbp)
jmp    12e6 <daemonize+0x146>
mov    -0x18(%rbp),%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1315 <daemonize+0x175>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12f3 <daemonize+0x153>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edx
lea    0xd22(%rip),%rdi
lea    0xd25(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%eax
jne    134f <daemonize+0x1af>
mov    0x2c77(%rip),%rax
mov    (%rax),%edi
lea    0xd09(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2c72(%rip),%rax
mov    (%rax),%edx
lea    0xce8(%rip),%rdi
lea    0xd09(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%eax
jne    1389 <daemonize+0x1e9>
mov    0x2c3d(%rip),%rax
mov    (%rax),%edi
lea    0xced(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2c40(%rip),%rax
mov    (%rax),%edx
lea    0xcae(%rip),%rdi
lea    0xccf(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%eax
jne    13c3 <daemonize+0x223>
mov    0x2c03(%rip),%rax
mov    (%rax),%edi
lea    0xccf(%rip),%rsi
call   1080 <syslog@plt>
mov    $0x0,%al
call   10b0 <lockPidFile@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     11c9 <daemonize+0x29>
cmp    $0xffffffff,%eax
jne    131a <daemonize+0x17a>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
jmp    11f9 <daemonize+0x59>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     11fe <daemonize+0x5e>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     120a <daemonize+0x6a>
cmp    $0xffffffff,%eax
jne    131a <daemonize+0x17a>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1080 <syslog@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xe14(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
jns    1237 <daemonize+0x97>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
cmp    $0xffffffff,%eax
mov    $0x1000,%ebp
cmovne %eax,%ebp
test   %ebp,%ebp
jle    126e <daemonize+0xce>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1260 <daemonize+0xc0>
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
lea    0xdc9(%rip),%rdi
lea    0xdcc(%rip),%rsi
call   1040 <freopen@plt>
test   %eax,%eax
jne    12a3 <daemonize+0x103>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edx
lea    0xd94(%rip),%rdi
lea    0xdb5(%rip),%rsi
call   1040 <freopen@plt>
test   %eax,%eax
jne    12d8 <daemonize+0x138>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edx
lea    0xd5f(%rip),%rdi
lea    0xd80(%rip),%rsi
call   1040 <freopen@plt>
test   %eax,%eax
jne    130d <daemonize+0x16d>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
call   1080 <syslog@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    10b0 <lockPidFile@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [20 x i8] c"Failed to fork : %m\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [24 x i8] c"Failed to fork (2) : %m\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [25 x i8] c"Failed to chdir '/' : %m\00"
@_SC_OPEN_MAX = global i32 0
@.str.4 = [10 x i8] c"/dev/null\00"
@.str.5 = [2 x i8] c"r\00"
@stdin = global i32 0
@LOG_DEBUG = global i32 0
@.str.6 = [28 x i8] c"Failed to reopen stdin : %m\00"
@.str.7 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.8 = [28 x i8] c"Failed to reopen stout : %m\00"
@stderr = global i32 0
@.str.9 = [28 x i8] c"Failed to reopen sterr : %m\00"
define i32 @daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
switch i32 %4, label %9 [
i32 -1, label %5
i32 0, label %8
]
5: ; preds = %0
%6 = load i32, i32* @LOG_ERR
%7 = call i32 @syslog(i32 %6, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %75
8: ; preds = %0
br label %12
9: ; preds = %0
%10 = load i32, i32* @EXIT_SUCCESS
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %8
%13 = call i64 (...) @setsid()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 -1, i32* %1
br label %75
16: ; preds = %12
%17 = call i32 (...) @fork()
switch i32 %17, label %22 [
i32 -1, label %18
i32 0, label %21
]
18: ; preds = %16
%19 = load i32, i32* @LOG_ERR
%20 = call i32 @syslog(i32 %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %75
21: ; preds = %16
br label %25
22: ; preds = %16
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %21
%26 = call i32 @umask(i32 0)
%27 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @LOG_ERR
%31 = call i32 @syslog(i32 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %29, %25
%33 = load i32, i32* @_SC_OPEN_MAX
%34 = call i32 @sysconf(i32 %33)
store i32 %34, i32* %2
%35 = load i32, i32* %2
%36 = icmp ne i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = load i32, i32* %2
br label %40
39: ; preds = %32
br label %40
40: ; preds = %39, %37
%41 = phi i32 [ %38, %37 ], [ 4096, %39 ]
store i32 %41, i32* %2
store i32 0, i32* %3
br label %42
42: ; preds = %49, %40
%43 = load i32, i32* %3
%44 = load i32, i32* %2
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %52
46: ; preds = %42
%47 = load i32, i32* %3
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %46
%50 = load i32, i32* %3
%51 = add nsw i32 %50, 1
store i32 %51, i32* %3
br label %42
52: ; preds = %42
%53 = load i32, i32* @stdin
%54 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %52
%57 = load i32, i32* @LOG_DEBUG
%58 = call i32 @syslog(i32 %57, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
br label %59
59: ; preds = %56, %52
%60 = load i32, i32* @stdout
%61 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %66, label %63
63: ; preds = %59
%64 = load i32, i32* @LOG_DEBUG
%65 = call i32 @syslog(i32 %64, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %66
66: ; preds = %63, %59
%67 = load i32, i32* @stderr
%68 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 %67)
%69 = icmp ne i32 %68, 0
br i1 %69, label %73, label %70
70: ; preds = %66
%71 = load i32, i32* @LOG_DEBUG
%72 = call i32 @syslog(i32 %71, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
br label %73
73: ; preds = %70, %66
%74 = call i32 (...) @lockPidFile()
store i32 %74, i32* %1
br label %75
75: ; preds = %73, %18, %15, %5
%76 = load i32, i32* %1
ret i32 %76
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [20 x i8] c"Failed to fork : %m\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [24 x i8] c"Failed to fork (2) : %m\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [25 x i8] c"Failed to chdir '/' : %m\00"
@_SC_OPEN_MAX = global i32 0
@.str.4 = [10 x i8] c"/dev/null\00"
@.str.5 = [2 x i8] c"r\00"
@stdin = global i32 0
@LOG_DEBUG = global i32 0
@.str.6 = [28 x i8] c"Failed to reopen stdin : %m\00"
@.str.7 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.8 = [28 x i8] c"Failed to reopen stout : %m\00"
@stderr = global i32 0
@.str.9 = [28 x i8] c"Failed to reopen sterr : %m\00"
define i32 @daemonize() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %5 [
i32 -1, label %2
i32 0, label %8
]
2: ; preds = %0
%3 = load i32, i32* @LOG_ERR
%4 = tail call i32 @syslog(i32 %3, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %60
5: ; preds = %0
%6 = load i32, i32* @EXIT_SUCCESS
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i64 (...) @setsid()
%10 = icmp slt i64 %9, 0
br i1 %10, label %60, label %11
11: ; preds = %8
%12 = tail call i32 (...) @fork()
switch i32 %12, label %16 [
i32 -1, label %13
i32 0, label %19
]
13: ; preds = %11
%14 = load i32, i32* @LOG_ERR
%15 = tail call i32 @syslog(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %60
16: ; preds = %11
%17 = load i32, i32* @EXIT_SUCCESS
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = tail call i32 @umask(i32 0)
%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 %26
23: ; preds = %19
%24 = load i32, i32* @LOG_ERR
%25 = tail call i32 @syslog(i32 %24, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %23, %19
%27 = load i32, i32* @_SC_OPEN_MAX
%28 = tail call i32 @sysconf(i32 %27)
%29 = icmp eq i32 %28, -1
%30 = select i1 %29, i32 4096, i32 %28
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %26, %32
%33 = phi i32 [ %35, %32 ], [ 0, %26 ]
%34 = tail call i32 @close(i32 %33)
%35 = add nuw nsw i32 %33, 1
%36 = icmp eq i32 %35, %30
br i1 %36, label %37, label %32
37: ; preds = %32, %26
%38 = load i32, i32* @stdin
%39 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32, i32* @LOG_DEBUG
%43 = tail call i32 @syslog(i32 %42, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %41, %37
%45 = load i32, i32* @stdout
%46 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32, i32* @LOG_DEBUG
%50 = tail call i32 @syslog(i32 %49, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %51
51: ; preds = %48, %44
%52 = load i32, i32* @stderr
%53 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 %52)
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32, i32* @LOG_DEBUG
%57 = tail call i32 @syslog(i32 %56, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
br label %58
58: ; preds = %55, %51
%59 = tail call i32 (...) @lockPidFile()
br label %60
60: ; preds = %8, %58, %13, %2
%61 = phi i32 [ %59, %58 ], [ -1, %13 ], [ -1, %2 ], [ -1, %8 ]
ret i32 %61
}