#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 /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int do_daemonize(void) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "*** fork() error: %s.\n", strerror(errno));
    return -1;
  } else if (pid > 0) {
    exit(0);
  } else {
    int fd;
    setsid();
    chdir("/tmp");
    if ((fd = open("/dev/null", O_RDWR)) >= 0) {
      dup2(fd, 0);
      dup2(fd, 1);
      dup2(fd, 2);
      if (fd > 2)
        close(fd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11e5 <do_daemonize+0x55>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe2c(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <do_daemonize+0xe7>
cmpq   $0x0,-0x10(%rbp)
jle    11f7 <do_daemonize+0x67>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
lea    0xe12(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    0xe02(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jl     1266 <do_daemonize+0xd6>
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    1261 <do_daemonize+0xd1>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    1266 <do_daemonize+0xd6>
jmp    126b <do_daemonize+0xdb>
jmp    1270 <do_daemonize+0xe0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1206 <do_daemonize+0x76>
jne    123c <do_daemonize+0xac>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe62(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1233 <do_daemonize+0xa3>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
cmp    $0x3,%ebp
jb     1233 <do_daemonize+0xa3>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1233 <do_daemonize+0xa3>
mov    0x2dcb(%rip),%rax
mov    (%rax),%ebx
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xddc(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"*** fork() error: %s.\0A\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @do_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* @stderr
%5 = load i32, i32* @errno
%6 = tail call i8* @strerror(i32 %5)
%7 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %25
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 = tail call i32 (...) @setsid()
%14 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %25
18: ; preds = %12
%19 = tail call i32 @dup2(i32 %16, i32 0)
%20 = tail call i32 @dup2(i32 %16, i32 1)
%21 = tail call i32 @dup2(i32 %16, i32 2)
%22 = icmp ugt i32 %16, 2
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = tail call i32 @close(i32 %16)
br label %25
25: ; preds = %12, %23, %18, %3
%26 = phi i32 [ -1, %3 ], [ 0, %18 ], [ 0, %23 ], [ 0, %12 ]
ret i32 %26
}