#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_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
int setsid();
int daemonize() {
  int fd;
  switch (fork()) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    exit(EXIT_SUCCESS);
  }
  if (setsid() == -1) {
    return -1;
  }
  if (chdir("/") == -1) {
    return -1;
  }
  fd = open("/dev/null", O_RDWR);
  if (fd == -1) {
    return -1;
  }
  dup2(fd, STDIN_FILENO);
  dup2(fd, STDOUT_FILENO);
  dup2(fd, STDERR_FILENO);
  if (fd > 2) {
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     11a2 <daemonize+0x32>
jmp    1192 <daemonize+0x22>
mov    -0xc(%rbp),%eax
test   %eax,%eax
je     11ae <daemonize+0x3e>
jmp    11b3 <daemonize+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <daemonize+0x108>
jmp    11c1 <daemonize+0x51>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11dd <daemonize+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <daemonize+0x108>
lea    0xe1c(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11fe <daemonize+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <daemonize+0x108>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    0xdf4(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    122c <daemonize+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <daemonize+0x108>
mov    -0x8(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmpl   $0x2,-0x8(%rbp)
jle    1271 <daemonize+0x101>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1209 <daemonize+0x99>
test   %ebx,%ebx
jne    1212 <daemonize+0xa2>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1209 <daemonize+0x99>
lea    0xe5b(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
je     1209 <daemonize+0x99>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1209 <daemonize+0x99>
mov    %eax,%ebp
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
xor    %ebx,%ebx
cmp    $0x3,%ebp
jl     1209 <daemonize+0x99>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@.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
define i32 @daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @fork()
switch i32 %3, label %6 [
i32 -1, label %4
i32 0, label %5
]
4: ; preds = %0
store i32 -1, i32* %1
br label %39
5: ; preds = %0
br label %9
6: ; preds = %0
%7 = load i32, i32* @EXIT_SUCCESS
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %5
%10 = call i32 (...) @setsid()
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %9
store i32 -1, i32* %1
br label %39
13: ; preds = %9
%14 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 -1, i32* %1
br label %39
17: ; preds = %13
%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* %2
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %1
br label %39
23: ; preds = %17
%24 = load i32, i32* %2
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = load i32, i32* %2
%28 = load i32, i32* @STDOUT_FILENO
%29 = call i32 @dup2(i32 %27, i32 %28)
%30 = load i32, i32* %2
%31 = load i32, i32* @STDERR_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = load i32, i32* %2
%34 = icmp sgt i32 %33, 2
br i1 %34, label %35, label %38
35: ; preds = %23
%36 = load i32, i32* %2
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %23
store i32 0, i32* %1
br label %39
39: ; preds = %38, %22, %16, %12, %4
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@.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
define i32 @daemonize() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %2 [
i32 -1, label %25
i32 0, label %5
]
2: ; preds = %0
%3 = load i32, i32* @EXIT_SUCCESS
%4 = tail call i32 @exit(i32 %3)
unreachable
5: ; preds = %0
%6 = tail call i32 (...) @setsid()
%7 = icmp eq i32 %6, -1
br i1 %7, label %25, label %8
8: ; preds = %5
%9 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, -1
br i1 %10, label %25, label %11
11: ; preds = %8
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %25, label %15
15: ; preds = %11
%16 = load i32, i32* @STDIN_FILENO
%17 = tail call i32 @dup2(i32 %13, i32 %16)
%18 = load i32, i32* @STDOUT_FILENO
%19 = tail call i32 @dup2(i32 %13, i32 %18)
%20 = load i32, i32* @STDERR_FILENO
%21 = tail call i32 @dup2(i32 %13, i32 %20)
%22 = icmp sgt i32 %13, 2
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = tail call i32 @close(i32 %13)
br label %25
25: ; preds = %15, %23, %11, %8, %5, %0
%26 = phi i32 [ %1, %0 ], [ -1, %5 ], [ -1, %8 ], [ -1, %11 ], [ 0, %23 ], [ 0, %15 ]
ret i32 %26
}