#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*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dbg(char *, scalar_t__);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ logerr(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int);
void daemonize(void) {
  pid_t pid, sid;
  int fd;
  pid = fork();
  if (pid > 0)
    exit(0);
  if (pid < 0) {
    logerr("fork of daemon failed: %s", strerror(errno));
    exit(-1);
  }
  fd = open("/dev/null", O_RDWR);
  if (fd >= 0) {
    if (fd != STDIN_FILENO)
      dup2(fd, STDIN_FILENO);
    if (fd != STDOUT_FILENO)
      dup2(fd, STDOUT_FILENO);
    if (fd != STDERR_FILENO)
      dup2(fd, STDERR_FILENO);
    if (fd > STDERR_FILENO)
      close(fd);
  }
  if (fd < 0)
    logerr("fatal, could not open /dev/null: %s", strerror(errno));
  chdir("/");
  umask(022);
  sid = setsid();
  dbg("our session is %d", sid);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jle    11d5 <daemonize+0x25>
xor    %edi,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jge    1206 <daemonize+0x56>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xe09(%rip),%rdi
call   1090 <logerr@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    0xe04(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     12b0 <daemonize+0x100>
mov    -0x14(%rbp),%eax
mov    0x2d8e(%rip),%rcx
cmp    (%rcx),%eax
je     124b <daemonize+0x9b>
mov    -0x14(%rbp),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%eax
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%eax
je     126e <daemonize+0xbe>
mov    -0x14(%rbp),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%eax
mov    0x2d50(%rip),%rcx
cmp    (%rcx),%eax
je     1291 <daemonize+0xe1>
mov    -0x14(%rbp),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%eax
mov    0x2d2d(%rip),%rcx
cmp    (%rcx),%eax
jle    12ab <daemonize+0xfb>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    12b0 <daemonize+0x100>
cmpl   $0x0,-0x14(%rbp)
jge    12d6 <daemonize+0x126>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xd53(%rip),%rdi
call   1090 <logerr@plt>
lea    0xd6b(%rip),%rdi
call   1030 <chdir@plt>
mov    $0x12,%edi
call   1080 <umask@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xd48(%rip),%rdi
call   1070 <dbg@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
jg     127f <daemonize+0xcf>
js     1286 <daemonize+0xd6>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0xe40(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1230 <daemonize+0x80>
mov    %eax,%ebx
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
cmp    %esi,%ebx
je     11f9 <daemonize+0x49>
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
cmp    %esi,%ebx
je     120d <daemonize+0x5d>
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2db4(%rip),%r14
mov    (%r14),%esi
cmp    %esi,%ebx
je     124c <daemonize+0x9c>
mov    %ebx,%edi
call   1040 <dup2@plt>
cmp    (%r14),%ebx
jle    124c <daemonize+0x9c>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    124c <daemonize+0x9c>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xddf(%rip),%rdi
mov    %eax,%esi
call   1090 <logerr@plt>
lea    0xdf5(%rip),%rdi
call   1030 <chdir@plt>
mov    $0x12,%edi
call   1080 <umask@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xdda(%rip),%rdi
mov    %rax,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <dbg@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd65(%rip),%rdi
mov    %eax,%esi
call   1090 <logerr@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"fork of daemon failed: %s\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [36 x i8] c"fatal, could not open /dev/null: %s\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [18 x i8] c"our session is %d\00"
define void @daemonize() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i32
%4 = call i64 (...) @fork()
store i64 %4, i64* %1
%5 = load i64, i64* %1
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @exit(i32 0)
unreachable
9: ; preds = %0
%10 = load i64, i64* %1
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 @logerr(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %9
%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 sge i32 %20, 0
br i1 %21, label %22, label %54
22: ; preds = %17
%23 = load i32, i32* %3
%24 = load i32, i32* @STDIN_FILENO
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* %3
%28 = load i32, i32* @STDIN_FILENO
%29 = call i32 @dup2(i32 %27, i32 %28)
br label %30
30: ; preds = %26, %22
%31 = load i32, i32* %3
%32 = load i32, i32* @STDOUT_FILENO
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* %3
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
br label %38
38: ; preds = %34, %30
%39 = load i32, i32* %3
%40 = load i32, i32* @STDERR_FILENO
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i32, i32* %3
%44 = load i32, i32* @STDERR_FILENO
%45 = call i32 @dup2(i32 %43, i32 %44)
br label %46
46: ; preds = %42, %38
%47 = load i32, i32* %3
%48 = load i32, i32* @STDERR_FILENO
%49 = icmp sgt i32 %47, %48
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i32, i32* %3
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %50, %46
br label %54
54: ; preds = %53, %17
%55 = load i32, i32* %3
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = load i32, i32* @errno
%59 = call i32 @strerror(i32 %58)
%60 = call i32 @logerr(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %57, %54
%62 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @umask(i32 18)
%64 = call i64 (...) @setsid()
store i64 %64, i64* %2
%65 = load i64, i64* %2
%66 = call i32 @dbg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i64 %65)
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"fork of daemon failed: %s\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [36 x i8] c"fatal, could not open /dev/null: %s\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [18 x i8] c"our session is %d\00"
define void @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp sgt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @exit(i32 0)
unreachable
5: ; preds = %0
%6 = icmp slt i64 %1, 0
br i1 %6, label %7, label %12
7: ; preds = %5
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @logerr(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %5
%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 sgt i32 %14, -1
br i1 %15, label %16, label %35
16: ; preds = %12
%17 = load i32, i32* @STDIN_FILENO
%18 = icmp eq i32 %14, %17
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 @dup2(i32 %14, i32 %17)
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* @STDOUT_FILENO
%23 = icmp eq i32 %14, %22
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = tail call i32 @dup2(i32 %14, i32 %22)
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* @STDERR_FILENO
%28 = icmp eq i32 %14, %27
br i1 %28, label %39, label %29
29: ; preds = %26
%30 = tail call i32 @dup2(i32 %14, i32 %27)
%31 = load i32, i32* @STDERR_FILENO
%32 = icmp sgt i32 %14, %31
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = tail call i32 @close(i32 %14)
br label %39
35: ; preds = %12
%36 = load i32, i32* @errno
%37 = tail call i32 @strerror(i32 %36)
%38 = tail call i32 @logerr(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %26, %33, %29, %35
%40 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%41 = tail call i32 @umask(i32 18)
%42 = tail call i64 (...) @setsid()
%43 = tail call i32 @dbg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i64 %42)
ret void
}