#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonify(void) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0)
    return -1;
  if (pid > 0)
    _exit(0);
  umask(0);
  sid = setsid();
  if (sid < 0)
    return -1;
  if (chdir("/") < 0)
    return -1;
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  return 0;
}


x86 assembly (clang, -O0):
<daemonify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    118a <daemonify+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <daemonify+0xb8>
cmpq   $0x0,-0x10(%rbp)
jle    119c <daemonify+0x3c>
xor    %edi,%edi
call   1030 <_exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11c5 <daemonify+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <daemonify+0xb8>
lea    0xe34(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    11e7 <daemonify+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <daemonify+0xb8>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonify>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     11c4 <daemonify+0x64>
jne    11c8 <daemonify+0x68>
xor    %edi,%edi
call   1070 <umask@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     11c4 <daemonify+0x64>
lea    0xe72(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     11c4 <daemonify+0x64>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonify() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%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 %8
7: ; preds = %0
store i32 -1, i32* %1
br label %30
8: ; preds = %0
%9 = load i64, i64* %2
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @_exit(i32 0)
unreachable
13: ; preds = %8
%14 = call i32 @umask(i32 0)
%15 = call i64 (...) @setsid()
store i64 %15, i64* %3
%16 = load i64, i64* %3
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 -1, i32* %1
br label %30
19: ; preds = %13
%20 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 -1, i32* %1
br label %30
23: ; preds = %19
%24 = load i32, i32* @STDIN_FILENO
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* @STDOUT_FILENO
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* @STDERR_FILENO
%29 = call i32 @close(i32 %28)
store i32 0, i32* %1
br label %30
30: ; preds = %23, %22, %18, %7
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonify() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %21, label %3
3: ; preds = %0
%4 = icmp eq i64 %1, 0
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i32 @_exit(i32 0)
unreachable
7: ; preds = %3
%8 = tail call i32 @umask(i32 0)
%9 = tail call i64 (...) @setsid()
%10 = icmp slt i64 %9, 0
br i1 %10, label %21, label %11
11: ; preds = %7
%12 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp slt i64 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %11
%15 = load i32, i32* @STDIN_FILENO
%16 = tail call i32 @close(i32 %15)
%17 = load i32, i32* @STDOUT_FILENO
%18 = tail call i32 @close(i32 %17)
%19 = load i32, i32* @STDERR_FILENO
%20 = tail call i32 @close(i32 %19)
br label %21
21: ; preds = %11, %7, %0, %14
%22 = phi i32 [ 0, %14 ], [ -1, %0 ], [ -1, %7 ], [ -1, %11 ]
ret i32 %22
}