#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_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ set_signal_handler(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int daemonize() {
  int pid;
  int fd;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "Couldn't daemonize (fork failed: %m)");
    exit(EXIT_FAILURE);
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  setsid();
  set_signal_handler(SIGHUP, SIG_IGN);
  if ((pid = fork()) < 0) {
    fprintf(stderr, "Couldn't daemonize (fork failed: %m)");
    exit(EXIT_FAILURE);
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  chdir("/");
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  if ((fd = open("/dev/null", O_RDWR)) < 0) {
    return 0;
  } else {
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11ce <daemonize+0x3e>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jle    11e6 <daemonize+0x56>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    11eb <daemonize+0x5b>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
call   1030 <set_signal_handler@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    123f <daemonize+0xaf>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jle    1257 <daemonize+0xc7>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    125c <daemonize+0xcc>
lea    0xdc2(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
lea    0xd85(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    12bf <daemonize+0x12f>
movl   $0x0,-0x4(%rbp)
jmp    12f9 <daemonize+0x169>
mov    -0xc(%rbp),%edi
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2ccd(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1262 <daemonize+0xd2>
jne    1285 <daemonize+0xf5>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
call   1030 <set_signal_handler@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1262 <daemonize+0xd2>
jne    1285 <daemonize+0xf5>
lea    0xe3e(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2dad(%rip),%r14
mov    (%r14),%edi
call   1060 <close@plt>
mov    0x2de6(%rip),%r15
mov    (%r15),%edi
call   1060 <close@plt>
mov    0x2da7(%rip),%r12
mov    (%r12),%edi
call   1060 <close@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
lea    0xdfd(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1254 <daemonize+0xc4>
mov    %eax,%ebx
mov    (%r14),%esi
mov    %eax,%edi
call   1050 <dup2@plt>
mov    (%r15),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
mov    (%r12),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Couldn't daemonize (fork failed: %m)\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.1 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32, i32* %2
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EXIT_SUCCESS
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %11
br label %18
18: ; preds = %17
%19 = call i32 (...) @setsid()
%20 = load i32, i32* @SIGHUP
%21 = load i32, i32* @SIG_IGN
%22 = call i32 @set_signal_handler(i32 %20, i32 %21)
%23 = call i32 (...) @fork()
store i32 %23, i32* %2
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %18
%31 = load i32, i32* %2
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* @EXIT_SUCCESS
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %30
br label %37
37: ; preds = %36
%38 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* @STDIN_FILENO
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @STDOUT_FILENO
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* @STDERR_FILENO
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* @O_RDWR
%46 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %45)
store i32 %46, i32* %3
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %37
store i32 0, i32* %1
br label %60
49: ; preds = %37
%50 = load i32, i32* %3
%51 = load i32, i32* @STDIN_FILENO
%52 = call i32 @dup2(i32 %50, i32 %51)
%53 = load i32, i32* %3
%54 = load i32, i32* @STDOUT_FILENO
%55 = call i32 @dup2(i32 %53, i32 %54)
%56 = load i32, i32* %3
%57 = load i32, i32* @STDERR_FILENO
%58 = call i32 @dup2(i32 %56, i32 %57)
br label %59
59: ; preds = %49
store i32 0, i32* %1
br label %60
60: ; preds = %59, %48
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Couldn't daemonize (fork failed: %m)\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.1 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @daemonize() {
%1 = tail call i32 (...) @fork()
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = icmp eq i32 %1, 0
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = load i32, i32* @EXIT_SUCCESS
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %8
%14 = tail call i32 (...) @setsid()
%15 = load i32, i32* @SIGHUP
%16 = load i32, i32* @SIG_IGN
%17 = tail call i32 @set_signal_handler(i32 %15, i32 %16)
%18 = tail call i32 (...) @fork()
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %13
%26 = icmp eq i32 %18, 0
br i1 %26, label %30, label %27
27: ; preds = %25
%28 = load i32, i32* @EXIT_SUCCESS
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %25
%31 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @STDIN_FILENO
%33 = tail call i32 @close(i32 %32)
%34 = load i32, i32* @STDOUT_FILENO
%35 = tail call i32 @close(i32 %34)
%36 = load i32, i32* @STDERR_FILENO
%37 = tail call i32 @close(i32 %36)
%38 = load i32, i32* @O_RDWR
%39 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = icmp slt i32 %39, 0
br i1 %40, label %48, label %41
41: ; preds = %30
%42 = load i32, i32* @STDIN_FILENO
%43 = tail call i32 @dup2(i32 %39, i32 %42)
%44 = load i32, i32* @STDOUT_FILENO
%45 = tail call i32 @dup2(i32 %39, i32 %44)
%46 = load i32, i32* @STDERR_FILENO
%47 = tail call i32 @dup2(i32 %39, i32 %46)
br label %48
48: ; preds = %30, %41
ret i32 0
}