#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 int /*<<< orphan*/ mode_t;

/* Variables and functions */
int _create_new_session();
int _fork_off_to_child();
int _ignore_signals();
int _redirect_std_streams();
scalar_t__ chdir(char const *);
int errno;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(mode_t mask, const char *__restrict dir, bool close_fds) {
  int err;
  err = _fork_off_to_child();
  if (err < 0)
    return err;
  err = _create_new_session();
  if (err < 0)
    return err;
  err = _fork_off_to_child();
  if (err < 0)
    return err;
  err = _ignore_signals();
  if (err < 0)
    return err;
  umask(mask);
  if (chdir(dir) < 0)
    return -errno;
  if (close_fds) {
    err = _redirect_std_streams();
    if (err < 0)
      return err;
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1080 <_fork_off_to_child@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1191 <daemonize+0x31>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <daemonize+0xf1>
mov    $0x0,%al
call   1030 <_create_new_session@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b0 <daemonize+0x50>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <daemonize+0xf1>
mov    $0x0,%al
call   1080 <_fork_off_to_child@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11cf <daemonize+0x6f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <daemonize+0xf1>
mov    $0x0,%al
call   1060 <_ignore_signals@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ee <daemonize+0x8e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <daemonize+0xf1>
mov    -0x8(%rbp),%edi
call   1070 <umask@plt>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    121c <daemonize+0xbc>
xor    %eax,%eax
mov    0x2dce(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <daemonize+0xf1>
cmpl   $0x0,-0x14(%rbp)
je     124a <daemonize+0xea>
mov    $0x0,%al
call   1050 <_redirect_std_streams@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1245 <daemonize+0xe5>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <daemonize+0xf1>
jmp    124a <daemonize+0xea>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1080 <_fork_off_to_child@plt>
test   %eax,%eax
js     11cb <daemonize+0x6b>
xor    %eax,%eax
call   1030 <_create_new_session@plt>
test   %eax,%eax
js     11cb <daemonize+0x6b>
xor    %eax,%eax
call   1080 <_fork_off_to_child@plt>
test   %eax,%eax
js     11cb <daemonize+0x6b>
xor    %eax,%eax
call   1060 <_ignore_signals@plt>
test   %eax,%eax
js     11cb <daemonize+0x6b>
mov    %ebp,%edi
call   1070 <umask@plt>
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     11c0 <daemonize+0x60>
test   %r14d,%r14d
je     11bc <daemonize+0x5c>
xor    %eax,%eax
call   1050 <_redirect_std_streams@plt>
test   %eax,%eax
js     11cb <daemonize+0x6b>
xor    %eax,%eax
jmp    11cb <daemonize+0x6b>
mov    0x2e19(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @daemonize(i32 %0, i8* noalias %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = call i32 (...) @_fork_off_to_child()
store i32 %9, i32* %8
%10 = load i32, i32* %8
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = load i32, i32* %8
store i32 %13, i32* %4
br label %52
14: ; preds = %3
%15 = call i32 (...) @_create_new_session()
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %8
store i32 %19, i32* %4
br label %52
20: ; preds = %14
%21 = call i32 (...) @_fork_off_to_child()
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i32, i32* %8
store i32 %25, i32* %4
br label %52
26: ; preds = %20
%27 = call i32 (...) @_ignore_signals()
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %52
32: ; preds = %26
%33 = load i32, i32* %5
%34 = call i32 @umask(i32 %33)
%35 = load i8*, i8** %6
%36 = call i64 @chdir(i8* %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i32, i32* @errno
%40 = sub nsw i32 0, %39
store i32 %40, i32* %4
br label %52
41: ; preds = %32
%42 = load i32, i32* %7
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %51
44: ; preds = %41
%45 = call i32 (...) @_redirect_std_streams()
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i32, i32* %8
store i32 %49, i32* %4
br label %52
50: ; preds = %44
br label %51
51: ; preds = %50, %41
store i32 0, i32* %4
br label %52
52: ; preds = %51, %48, %38, %30, %24, %18, %12
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @daemonize(i32 %0, i8* noalias %1, i32 %2) {
%4 = tail call i32 (...) @_fork_off_to_child()
%5 = icmp slt i32 %4, 0
br i1 %5, label %28, label %6
6: ; preds = %3
%7 = tail call i32 (...) @_create_new_session()
%8 = icmp slt i32 %7, 0
br i1 %8, label %28, label %9
9: ; preds = %6
%10 = tail call i32 (...) @_fork_off_to_child()
%11 = icmp slt i32 %10, 0
br i1 %11, label %28, label %12
12: ; preds = %9
%13 = tail call i32 (...) @_ignore_signals()
%14 = icmp slt i32 %13, 0
br i1 %14, label %28, label %15
15: ; preds = %12
%16 = tail call i32 @umask(i32 %0)
%17 = tail call i64 @chdir(i8* %1)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
br label %28
22: ; preds = %15
%23 = icmp eq i32 %2, 0
br i1 %23, label %27, label %24
24: ; preds = %22
%25 = tail call i32 (...) @_redirect_std_streams()
%26 = icmp slt i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %24, %22
br label %28
28: ; preds = %24, %12, %9, %6, %3, %27, %19
%29 = phi i32 [ %21, %19 ], [ 0, %27 ], [ %4, %3 ], [ %7, %6 ], [ %10, %9 ], [ %13, %12 ], [ %25, %24 ]
ret i32 %29
}