#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*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int already_running();
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(void) {
  umask(0);
  if (chdir("/") < 0) {
    perror("Fatal: chdir(2) failed");
    exit(EXIT_FAILURE);
  }
  openlog("opend", 0, 0);
  pid_t pid;
  if ((pid = fork()) < 0) {
    perror("Fatal: fork(2) failed");
    exit(EXIT_FAILURE);
  }
  if (pid != 0) {
    exit(EXIT_SUCCESS);
  }
  setsid();
  if ((pid = fork()) < 0) {
    perror("Fatal: Couldn't fork after creating session");
    exit(EXIT_FAILURE);
  }
  if (pid != 0) {
    exit(EXIT_SUCCESS);
  }
  int check;
  if ((check = already_running()) == 1) {
    fprintf(stderr, "Another instance of "
                    "opend"
                    " was detected in the system.\n");
    fprintf(stderr, "See "
                    "/var/run/"
                    "opend"
                    ".pid"
                    " to get its PID.\n");
    fprintf(stderr, "Terminating...\n");
    exit(EXIT_SUCCESS);
  } else if (check == -1) {
    fprintf(stderr, "Fatal: Error checking for other instances: %s\n",
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  int sink = open("/dev/null", O_RDWR, 0);
  if (sink < 0) {
    fprintf(stderr, "Fatal: Couldn't open /dev/null: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (dup2(sink, STDIN_FILENO) < 0) {
    fprintf(stderr, "Fatal: Couldn't dup() /dev/null into stdin: %s\n",
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (dup2(sink, STDOUT_FILENO) < 0) {
    fprintf(stderr, "Fatal: Couldn't dup() /dev/null into stdout: %s\n",
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (dup2(sink, STDERR_FILENO) < 0) {
    fprintf(stderr, "Couldn't dup() /dev/null into stderr: %s\n",
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (sink != STDIN_FILENO && sink != STDOUT_FILENO && sink != STDERR_FILENO) {
    close(sink);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
xor    %edi,%edi
call   1090 <umask@plt>
lea    0xe1a(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    120f <daemonize+0x3f>
lea    0xe06(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xe03(%rip),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   10c0 <openlog@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jge    124e <daemonize+0x7e>
lea    0xde4(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     1267 <daemonize+0x97>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jge    129d <daemonize+0xcd>
lea    0xdab(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     12b6 <daemonize+0xe6>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1050 <already_running@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x1,%eax
jne    131c <daemonize+0x14c>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1362 <daemonize+0x192>
mov    0x2ca3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c5d(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    1367 <daemonize+0x197>
mov    0x2c72(%rip),%rax
mov    (%rax),%esi
lea    0xd88(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    13c7 <daemonize+0x1f7>
mov    0x2c3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2c2a(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2bf8(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%edi
mov    0x2bd7(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    141e <daemonize+0x24e>
mov    0x2be7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2bd3(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xd24(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2ba1(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%edi
mov    0x2bc8(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    1475 <daemonize+0x2a5>
mov    0x2b90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2b7c(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xcfd(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2b4a(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%edi
mov    0x2b41(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    14cc <daemonize+0x2fc>
mov    0x2b39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2b25(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xcd7(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2af3(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%eax
mov    0x2ad2(%rip),%rcx
cmp    (%rcx),%eax
je     150a <daemonize+0x33a>
mov    -0x10(%rbp),%eax
mov    0x2b08(%rip),%rcx
cmp    (%rcx),%eax
je     150a <daemonize+0x33a>
mov    -0x10(%rbp),%eax
mov    0x2ac6(%rip),%rcx
cmp    (%rcx),%eax
je     150a <daemonize+0x33a>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
call   1090 <umask@plt>
lea    0xe1a(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     12ee <daemonize+0x11e>
lea    0xe1e(%rip),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <openlog@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     12f7 <daemonize+0x127>
jne    136b <daemonize+0x19b>
xor    %eax,%eax
call   1070 <setsid@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     1300 <daemonize+0x130>
jne    136b <daemonize+0x19b>
xor    %eax,%eax
call   1050 <already_running@plt>
cmp    $0xffffffff,%eax
je     1311 <daemonize+0x141>
cmp    $0x1,%eax
je     1334 <daemonize+0x164>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1379 <daemonize+0x1a9>
mov    %eax,%ebx
mov    0x2d30(%rip),%r14
mov    (%r14),%esi
mov    %eax,%edi
call   1040 <dup2@plt>
test   %rax,%rax
js     1399 <daemonize+0x1c9>
mov    0x2d5e(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
test   %rax,%rax
js     13b9 <daemonize+0x1e9>
mov    0x2d14(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
test   %rax,%rax
js     13d9 <daemonize+0x209>
cmp    (%r14),%ebx
je     12e2 <daemonize+0x112>
cmp    (%r15),%ebx
je     12e2 <daemonize+0x112>
cmp    (%r12),%ebx
je     12e2 <daemonize+0x112>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd0d(%rip),%rdi
jmp    1307 <daemonize+0x137>
lea    0xd21(%rip),%rdi
jmp    1307 <daemonize+0x137>
lea    0xd2e(%rip),%rdi
call   10b0 <perror@plt>
jmp    1403 <daemonize+0x233>
mov    0x2cb8(%rip),%rax
mov    (%rax),%ebx
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xda1(%rip),%rsi
jmp    13f7 <daemonize+0x227>
mov    0x2c95(%rip),%rbx
mov    (%rbx),%edi
lea    0xd1d(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd44(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd5c(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2c3e(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2c50(%rip),%rax
mov    (%rax),%ebx
mov    0x2c3f(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd72(%rip),%rsi
jmp    13f7 <daemonize+0x227>
mov    0x2c30(%rip),%rax
mov    (%rax),%ebx
mov    0x2c1f(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd76(%rip),%rsi
jmp    13f7 <daemonize+0x227>
mov    0x2c10(%rip),%rax
mov    (%rax),%ebx
mov    0x2bff(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd86(%rip),%rsi
jmp    13f7 <daemonize+0x227>
mov    0x2bf0(%rip),%rax
mov    (%rax),%ebx
mov    0x2bdf(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd97(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2bae(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [23 x i8] c"Fatal: chdir(2) failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [6 x i8] c"opend\00"
@.str.3 = [22 x i8] c"Fatal: fork(2) failed\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [44 x i8] c"Fatal: Couldn't fork after creating session\00"
@stderr = global i32 0
@.str.5 = [55 x i8] c"Another instance of opend was detected in the system.\0A\00"
@.str.6 = [40 x i8] c"See /var/run/opend.pid to get its PID.\0A\00"
@.str.7 = [16 x i8] c"Terminating...\0A\00"
@.str.8 = [47 x i8] c"Fatal: Error checking for other instances: %s\0A\00"
@errno = global i32 0
@.str.9 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.10 = [36 x i8] c"Fatal: Couldn't open /dev/null: %s\0A\00"
@STDIN_FILENO = global i32 0
@.str.11 = [48 x i8] c"Fatal: Couldn't dup() /dev/null into stdin: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.12 = [49 x i8] c"Fatal: Couldn't dup() /dev/null into stdout: %s\0A\00"
@STDERR_FILENO = global i32 0
@.str.13 = [42 x i8] c"Couldn't dup() /dev/null into stderr: %s\0A\00"
define void @daemonize() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @umask(i32 0)
%5 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = call i32 @openlog(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 0)
%13 = call i64 (...) @fork()
store i64 %13, i64* %1
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = load i64, i64* %1
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %19
%26 = call i32 (...) @setsid()
%27 = call i64 (...) @fork()
store i64 %27, i64* %1
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %25
%34 = load i64, i64* %1
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* @EXIT_SUCCESS
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %33
%40 = call i32 (...) @already_running()
store i32 %40, i32* %2
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %51
42: ; preds = %39
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0))
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%49 = load i32, i32* @EXIT_SUCCESS
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %39
%52 = load i32, i32* %2
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %61
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.8, i64 0, i64 0), i8* %57)
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %51
br label %62
62: ; preds = %61
%63 = load i32, i32* @O_RDWR
%64 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i32 %63, i32 0)
store i32 %64, i32* %3
%65 = load i32, i32* %3
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %74
67: ; preds = %62
%68 = load i32, i32* @stderr
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.10, i64 0, i64 0), i8* %70)
%72 = load i32, i32* @EXIT_FAILURE
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %62
%75 = load i32, i32* %3
%76 = load i32, i32* @STDIN_FILENO
%77 = call i64 @dup2(i32 %75, i32 %76)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %86
79: ; preds = %74
%80 = load i32, i32* @stderr
%81 = load i32, i32* @errno
%82 = call i8* @strerror(i32 %81)
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.11, i64 0, i64 0), i8* %82)
%84 = load i32, i32* @EXIT_FAILURE
%85 = call i32 @exit(i32 %84)
unreachable
86: ; preds = %74
%87 = load i32, i32* %3
%88 = load i32, i32* @STDOUT_FILENO
%89 = call i64 @dup2(i32 %87, i32 %88)
%90 = icmp slt i64 %89, 0
br i1 %90, label %91, label %98
91: ; preds = %86
%92 = load i32, i32* @stderr
%93 = load i32, i32* @errno
%94 = call i8* @strerror(i32 %93)
%95 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.12, i64 0, i64 0), i8* %94)
%96 = load i32, i32* @EXIT_FAILURE
%97 = call i32 @exit(i32 %96)
unreachable
98: ; preds = %86
%99 = load i32, i32* %3
%100 = load i32, i32* @STDERR_FILENO
%101 = call i64 @dup2(i32 %99, i32 %100)
%102 = icmp slt i64 %101, 0
br i1 %102, label %103, label %110
103: ; preds = %98
%104 = load i32, i32* @stderr
%105 = load i32, i32* @errno
%106 = call i8* @strerror(i32 %105)
%107 = call i32 (i32, i8*, ...) @fprintf(i32 %104, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.13, i64 0, i64 0), i8* %106)
%108 = load i32, i32* @EXIT_FAILURE
%109 = call i32 @exit(i32 %108)
unreachable
110: ; preds = %98
%111 = load i32, i32* %3
%112 = load i32, i32* @STDIN_FILENO
%113 = icmp ne i32 %111, %112
br i1 %113, label %114, label %125
114: ; preds = %110
%115 = load i32, i32* %3
%116 = load i32, i32* @STDOUT_FILENO
%117 = icmp ne i32 %115, %116
br i1 %117, label %118, label %125
118: ; preds = %114
%119 = load i32, i32* %3
%120 = load i32, i32* @STDERR_FILENO
%121 = icmp ne i32 %119, %120
br i1 %121, label %122, label %125
122: ; preds = %118
%123 = load i32, i32* %3
%124 = call i32 @close(i32 %123)
br label %125
125: ; preds = %122, %118, %114, %110
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [23 x i8] c"Fatal: chdir(2) failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [6 x i8] c"opend\00"
@.str.3 = [22 x i8] c"Fatal: fork(2) failed\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [44 x i8] c"Fatal: Couldn't fork after creating session\00"
@stderr = global i32 0
@.str.5 = [55 x i8] c"Another instance of opend was detected in the system.\0A\00"
@.str.6 = [40 x i8] c"See /var/run/opend.pid to get its PID.\0A\00"
@.str.7 = [16 x i8] c"Terminating...\0A\00"
@.str.8 = [47 x i8] c"Fatal: Error checking for other instances: %s\0A\00"
@errno = global i32 0
@.str.9 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.10 = [36 x i8] c"Fatal: Couldn't open /dev/null: %s\0A\00"
@STDIN_FILENO = global i32 0
@.str.11 = [48 x i8] c"Fatal: Couldn't dup() /dev/null into stdin: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.12 = [49 x i8] c"Fatal: Couldn't dup() /dev/null into stdout: %s\0A\00"
@STDERR_FILENO = global i32 0
@.str.13 = [42 x i8] c"Couldn't dup() /dev/null into stderr: %s\0A\00"
define void @daemonize() {
%1 = tail call i32 @umask(i32 0)
%2 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i32 @openlog(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 0)
%10 = tail call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %8
%17 = icmp eq i64 %10, 0
br i1 %17, label %21, label %18
18: ; preds = %16
%19 = load i32, i32* @EXIT_SUCCESS
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %16
%22 = tail call i32 (...) @setsid()
%23 = tail call i64 (...) @fork()
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = icmp eq i64 %23, 0
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = load i32, i32* @EXIT_SUCCESS
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %29
%35 = tail call i32 (...) @already_running()
switch i32 %35, label %52 [
i32 1, label %36
i32 -1, label %45
]
36: ; preds = %34
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0))
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%43 = load i32, i32* @EXIT_SUCCESS
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %34
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = tail call i8* @strerror(i32 %47)
%49 = tail call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.8, i64 0, i64 0), i8* %48)
%50 = load i32, i32* @EXIT_FAILURE
%51 = tail call i32 @exit(i32 %50)
unreachable
52: ; preds = %34
%53 = load i32, i32* @O_RDWR
%54 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i32 %53, i32 0)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %52
%57 = load i32, i32* @stderr
%58 = load i32, i32* @errno
%59 = tail call i8* @strerror(i32 %58)
%60 = tail call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.10, i64 0, i64 0), i8* %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = tail call i32 @exit(i32 %61)
unreachable
63: ; preds = %52
%64 = load i32, i32* @STDIN_FILENO
%65 = tail call i64 @dup2(i32 %54, i32 %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %74
67: ; preds = %63
%68 = load i32, i32* @stderr
%69 = load i32, i32* @errno
%70 = tail call i8* @strerror(i32 %69)
%71 = tail call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.11, i64 0, i64 0), i8* %70)
%72 = load i32, i32* @EXIT_FAILURE
%73 = tail call i32 @exit(i32 %72)
unreachable
74: ; preds = %63
%75 = load i32, i32* @STDOUT_FILENO
%76 = tail call i64 @dup2(i32 %54, i32 %75)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %85
78: ; preds = %74
%79 = load i32, i32* @stderr
%80 = load i32, i32* @errno
%81 = tail call i8* @strerror(i32 %80)
%82 = tail call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.12, i64 0, i64 0), i8* %81)
%83 = load i32, i32* @EXIT_FAILURE
%84 = tail call i32 @exit(i32 %83)
unreachable
85: ; preds = %74
%86 = load i32, i32* @STDERR_FILENO
%87 = tail call i64 @dup2(i32 %54, i32 %86)
%88 = icmp slt i64 %87, 0
br i1 %88, label %89, label %96
89: ; preds = %85
%90 = load i32, i32* @stderr
%91 = load i32, i32* @errno
%92 = tail call i8* @strerror(i32 %91)
%93 = tail call i32 (i32, i8*, ...) @fprintf(i32 %90, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.13, i64 0, i64 0), i8* %92)
%94 = load i32, i32* @EXIT_FAILURE
%95 = tail call i32 @exit(i32 %94)
unreachable
96: ; preds = %85
%97 = load i32, i32* @STDIN_FILENO
%98 = icmp eq i32 %54, %97
%99 = load i32, i32* @STDOUT_FILENO
%100 = icmp eq i32 %54, %99
%101 = select i1 %98, i1 true, i1 %100
%102 = load i32, i32* @STDERR_FILENO
%103 = icmp eq i32 %54, %102
%104 = select i1 %101, i1 true, i1 %103
br i1 %104, label %107, label %105
105: ; preds = %96
%106 = tail call i32 @close(i32 %54)
br label %107
107: ; preds = %105, %96
ret void
}