#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*/ LOG_ERR;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(void) {
  pid_t pid;
  int fd;
  pid = fork();
  if (pid < 0) {
    syslog(LOG_ERR, "fork():%s", strerror(errno));
    return -1;
  }
  if (pid > 0)
    exit(0);
  fd = open("/dev/null", O_RDWR);
  if (fd < 0) {
    syslog(LOG_WARNING, "open():%s", strerror(errno));
  } else {
    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, 2);
    if (fd > 2)
      close(fd);
  }
  setsid();
  umask(0);
  chdir("/");
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11f5 <daemonize+0x55>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xe1c(%rip),%rsi
call   1080 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <daemonize+0x115>
cmpq   $0x0,-0x10(%rbp)
jle    1207 <daemonize+0x67>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1259 <daemonize+0xb9>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xdc5(%rip),%rsi
call   1080 <syslog@plt>
jmp    1294 <daemonize+0xf4>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
cmpl   $0x2,-0x14(%rbp)
jle    128f <daemonize+0xef>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    1294 <daemonize+0xf4>
mov    $0x0,%al
call   1060 <setsid@plt>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xd75(%rip),%rdi
call   1030 <chdir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     11fd <daemonize+0x5d>
jne    1272 <daemonize+0xd2>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     122b <daemonize+0x8b>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
cmp    $0x3,%ebx
jb     1252 <daemonize+0xb2>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1252 <daemonize+0xb2>
mov    0x2dbc(%rip),%rax
mov    (%rax),%ebx
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xde5(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1080 <syslog@plt>
mov    $0xffffffff,%ebx
jmp    126e <daemonize+0xce>
mov    0x2dae(%rip),%rax
mov    (%rax),%ebx
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdcb(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1080 <syslog@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <setsid@plt>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xdb5(%rip),%rdi
call   1030 <chdir@plt>
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [10 x i8] c"fork():%s\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@LOG_WARNING = global i32 0
@.str.2 = [10 x i8] c"open():%s\00"
@.str.3 = [2 x i8] c"/\00"
define i32 @daemonize() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @LOG_ERR
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = call i32 @syslog(i32 %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 -1, i32* %1
br label %44
12: ; preds = %0
%13 = load i64, i64* %2
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %12
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32, i32* @LOG_WARNING
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @syslog(i32 %23, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %40
27: ; preds = %17
%28 = load i32, i32* %3
%29 = call i32 @dup2(i32 %28, i32 0)
%30 = load i32, i32* %3
%31 = call i32 @dup2(i32 %30, i32 1)
%32 = load i32, i32* %3
%33 = call i32 @dup2(i32 %32, i32 2)
%34 = load i32, i32* %3
%35 = icmp sgt i32 %34, 2
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36, %27
br label %40
40: ; preds = %39, %22
%41 = call i32 (...) @setsid()
%42 = call i32 @umask(i32 0)
%43 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %44
44: ; preds = %40, %7
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [10 x i8] c"fork():%s\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@LOG_WARNING = global i32 0
@.str.2 = [10 x i8] c"open():%s\00"
@.str.3 = [2 x i8] c"/\00"
define i32 @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = load i32, i32* @LOG_ERR
%5 = load i32, i32* @errno
%6 = tail call i32 @strerror(i32 %5)
%7 = tail call i32 @syslog(i32 %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %32
8: ; preds = %0
%9 = icmp eq i64 %1, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %8
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @LOG_WARNING
%18 = load i32, i32* @errno
%19 = tail call i32 @strerror(i32 %18)
%20 = tail call i32 @syslog(i32 %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %28
21: ; preds = %12
%22 = tail call i32 @dup2(i32 %14, i32 0)
%23 = tail call i32 @dup2(i32 %14, i32 1)
%24 = tail call i32 @dup2(i32 %14, i32 2)
%25 = icmp ugt i32 %14, 2
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 @close(i32 %14)
br label %28
28: ; preds = %21, %26, %16
%29 = tail call i32 (...) @setsid()
%30 = tail call i32 @umask(i32 0)
%31 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %28, %3
%33 = phi i32 [ -1, %3 ], [ 0, %28 ]
ret i32 %33
}