#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*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
scalar_t__ close(int);
scalar_t__ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ log_err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ log_set_err_pipe(int);
int open(char *, int /*<<< orphan*/);
void end_daemonize(int fd) {
  int dev_null;
  if (chdir("/") < 0) {
    log_err(errno, "Unable to change to root directory");
  }
  if ((dev_null = open("/dev/null", O_RDWR)) < 0) {
    log_err(errno, "Unable to open \"/dev/null\"");
  }
  if (dup2(dev_null, STDIN_FILENO) < 0) {
    log_err(errno, "Unable to dup \"/dev/null\" onto stdin");
  }
  if (dup2(dev_null, STDOUT_FILENO) < 0) {
    log_err(errno, "Unable to dup \"/dev/null\" onto stdout");
  }
  if (dup2(dev_null, STDERR_FILENO) < 0) {
    log_err(errno, "Unable to dup \"/dev/null\" onto stderr");
  }
  if (close(dev_null) < 0) {
    log_err(errno, "Unable to close \"/dev/null\"");
  }
  log_set_err_pipe(-1);
  if ((fd >= 0) && (close(fd) < 0)) {
    log_err(errno, "Unable to close write-pipe in grandchild process");
  }
  return;
}


x86 assembly (clang, -O0):
<end_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe8e(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1196 <end_daemonize+0x36>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1080 <log_err@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11cc <end_daemonize+0x6c>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
call   1080 <log_err@plt>
mov    -0x8(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    11fc <end_daemonize+0x9c>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1080 <log_err@plt>
mov    -0x8(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    122c <end_daemonize+0xcc>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
call   1080 <log_err@plt>
mov    -0x8(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    125c <end_daemonize+0xfc>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1080 <log_err@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    1283 <end_daemonize+0x123>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1080 <log_err@plt>
mov    $0xffffffff,%edi
call   1070 <log_set_err_pipe@plt>
cmpl   $0x0,-0x4(%rbp)
jl     12be <end_daemonize+0x15e>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    12be <end_daemonize+0x15e>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1080 <log_err@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<end_daemonize>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xe94(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
jns    118b <end_daemonize+0x2b>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
call   1080 <log_err@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11bb <end_daemonize+0x5b>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1080 <log_err@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
test   %rax,%rax
jns    11e5 <end_daemonize+0x85>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
call   1080 <log_err@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
test   %rax,%rax
jns    120f <end_daemonize+0xaf>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1080 <log_err@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
test   %rax,%rax
jns    1239 <end_daemonize+0xd9>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1080 <log_err@plt>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    125a <end_daemonize+0xfa>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1080 <log_err@plt>
mov    $0xffffffff,%edi
call   1070 <log_set_err_pipe@plt>
test   %ebx,%ebx
js     1274 <end_daemonize+0x114>
mov    %ebx,%edi
call   1050 <close@plt>
test   %rax,%rax
js     127b <end_daemonize+0x11b>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <log_err@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"Unable to change to root directory\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.3 = [27 x i8] c"Unable to open \22/dev/null\22\00"
@STDIN_FILENO = global i32 0
@.str.4 = [37 x i8] c"Unable to dup \22/dev/null\22 onto stdin\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [38 x i8] c"Unable to dup \22/dev/null\22 onto stdout\00"
@STDERR_FILENO = global i32 0
@.str.6 = [38 x i8] c"Unable to dup \22/dev/null\22 onto stderr\00"
@.str.7 = [28 x i8] c"Unable to close \22/dev/null\22\00"
@.str.8 = [49 x i8] c"Unable to close write-pipe in grandchild process\00"
define void @end_daemonize(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = call i32 @log_err(i32 %7, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %6, %1
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %10)
store i32 %11, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @errno
%15 = call i32 @log_err(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %13, %9
%17 = load i32, i32* %3
%18 = load i32, i32* @STDIN_FILENO
%19 = call i64 @dup2(i32 %17, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = call i32 @log_err(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %21, %16
%25 = load i32, i32* %3
%26 = load i32, i32* @STDOUT_FILENO
%27 = call i64 @dup2(i32 %25, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* @errno
%31 = call i32 @log_err(i32 %30, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
br label %32
32: ; preds = %29, %24
%33 = load i32, i32* %3
%34 = load i32, i32* @STDERR_FILENO
%35 = call i64 @dup2(i32 %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* @errno
%39 = call i32 @log_err(i32 %38, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
br label %40
40: ; preds = %37, %32
%41 = load i32, i32* %3
%42 = call i64 @close(i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = call i32 @log_err(i32 %45, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
br label %47
47: ; preds = %44, %40
%48 = call i32 @log_set_err_pipe(i32 -1)
%49 = load i32, i32* %2
%50 = icmp sge i32 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %47
%52 = load i32, i32* %2
%53 = call i64 @close(i32 %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32, i32* @errno
%57 = call i32 @log_err(i32 %56, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.8, i64 0, i64 0))
br label %58
58: ; preds = %55, %51, %47
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"Unable to change to root directory\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.3 = [27 x i8] c"Unable to open \22/dev/null\22\00"
@STDIN_FILENO = global i32 0
@.str.4 = [37 x i8] c"Unable to dup \22/dev/null\22 onto stdin\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [38 x i8] c"Unable to dup \22/dev/null\22 onto stdout\00"
@STDERR_FILENO = global i32 0
@.str.6 = [38 x i8] c"Unable to dup \22/dev/null\22 onto stderr\00"
@.str.7 = [28 x i8] c"Unable to close \22/dev/null\22\00"
@.str.8 = [49 x i8] c"Unable to close write-pipe in grandchild process\00"
define void @end_daemonize(i32 %0) {
%2 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @errno
%6 = tail call i32 @log_err(i32 %5, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %4, %1
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @errno
%13 = tail call i32 @log_err(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %11, %7
%15 = load i32, i32* @STDIN_FILENO
%16 = tail call i64 @dup2(i32 %9, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* @errno
%20 = tail call i32 @log_err(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %21
21: ; preds = %18, %14
%22 = load i32, i32* @STDOUT_FILENO
%23 = tail call i64 @dup2(i32 %9, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @errno
%27 = tail call i32 @log_err(i32 %26, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %25, %21
%29 = load i32, i32* @STDERR_FILENO
%30 = tail call i64 @dup2(i32 %9, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* @errno
%34 = tail call i32 @log_err(i32 %33, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
br label %35
35: ; preds = %32, %28
%36 = tail call i64 @close(i32 %9)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = tail call i32 @log_err(i32 %39, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
br label %41
41: ; preds = %38, %35
%42 = tail call i32 @log_set_err_pipe(i32 -1)
%43 = icmp sgt i32 %0, -1
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = tail call i64 @close(i32 %0)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* @errno
%49 = tail call i32 @log_err(i32 %48, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.8, i64 0, i64 0))
br label %50
50: ; preds = %47, %44, %41
ret void
}