#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int LOG_CONS;
int /*<<< orphan*/ LOG_DAEMON;
int LOG_PID;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _SC_OPEN_MAX;
scalar_t__ chdir(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char const *, int, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
pid_t daemonize(const char *wd, const char *log_ident) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  if (setsid() < 0) {
    exit(EXIT_FAILURE);
  }
  signal(SIGCHLD, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_FAILURE);
  }
  umask(0);
  if (wd && (chdir(wd)) < 0) {
    exit(EXIT_FAILURE);
  }
  int i;
  for (i = sysconf(_SC_OPEN_MAX); i > 0; i--) {
    close(i);
  }
  if (log_ident) {
    openlog(log_ident, LOG_PID | LOG_CONS, LOG_DAEMON);
  }
  return pid;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11c4 <daemonize+0x34>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    11dd <daemonize+0x4d>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    11fc <daemonize+0x6c>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    124e <daemonize+0xbe>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1267 <daemonize+0xd7>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
cmpq   $0x0,-0x8(%rbp)
je     129a <daemonize+0x10a>
mov    -0x8(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    129a <daemonize+0x10a>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    12cb <daemonize+0x13b>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    12ab <daemonize+0x11b>
cmpq   $0x0,-0x10(%rbp)
je     12fa <daemonize+0x16a>
mov    -0x10(%rbp),%rdi
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
mov    0x2cc6(%rip),%rax
or     (%rax),%esi
mov    0x2ced(%rip),%rax
mov    (%rax),%edx
call   1090 <openlog@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1271 <daemonize+0xe1>
jne    127f <daemonize+0xef>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     1271 <daemonize+0xe1>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rbx
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1271 <daemonize+0xe1>
jne    1271 <daemonize+0xe1>
xor    %edi,%edi
call   1070 <umask@plt>
test   %r15,%r15
je     120f <daemonize+0x7f>
mov    %r15,%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     1271 <daemonize+0xe1>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
test   %eax,%eax
jle    1241 <daemonize+0xb1>
mov    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
call   1040 <close@plt>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
ja     1230 <daemonize+0xa0>
test   %r14,%r14
je     1269 <daemonize+0xd9>
mov    0x2d6b(%rip),%rax
mov    0x2d5c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1090 <openlog@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@_SC_OPEN_MAX = global i32 0
@LOG_PID = global i32 0
@LOG_CONS = global i32 0
@LOG_DAEMON = global i32 0
define i64 @daemonize(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = call i64 (...) @fork()
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = load i64, i64* %5
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @EXIT_SUCCESS
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %13
%20 = call i64 (...) @setsid()
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %19
%26 = load i32, i32* @SIGCHLD
%27 = load i32, i32* @SIG_IGN
%28 = call i32 @signal(i32 %26, i32 %27)
%29 = load i32, i32* @SIGHUP
%30 = load i32, i32* @SIG_IGN
%31 = call i32 @signal(i32 %29, i32 %30)
%32 = call i64 (...) @fork()
store i64 %32, i64* %5
%33 = load i64, i64* %5
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %25
%39 = load i64, i64* %5
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %38
%45 = call i32 @umask(i32 0)
%46 = load i8*, i8** %3
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = load i8*, i8** %3
%50 = call i64 @chdir(i8* %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %48, %44
%56 = load i32, i32* @_SC_OPEN_MAX
%57 = call i32 @sysconf(i32 %56)
store i32 %57, i32* %6
br label %58
58: ; preds = %64, %55
%59 = load i32, i32* %6
%60 = icmp sgt i32 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %58
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61
%65 = load i32, i32* %6
%66 = add nsw i32 %65, -1
store i32 %66, i32* %6
br label %58
67: ; preds = %58
%68 = load i8*, i8** %4
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %77
70: ; preds = %67
%71 = load i8*, i8** %4
%72 = load i32, i32* @LOG_PID
%73 = load i32, i32* @LOG_CONS
%74 = or i32 %72, %73
%75 = load i32, i32* @LOG_DAEMON
%76 = call i32 @openlog(i8* %71, i32 %74, i32 %75)
br label %77
77: ; preds = %70, %67
%78 = load i64, i64* %5
ret i64 %78
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@_SC_OPEN_MAX = global i32 0
@LOG_PID = global i32 0
@LOG_CONS = global i32 0
@LOG_DAEMON = global i32 0
define i64 @daemonize(i8* %0, i8* %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %2
%9 = icmp eq i64 %3, 0
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = load i32, i32* @EXIT_SUCCESS
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %8
%14 = tail call i64 (...) @setsid()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %13
%20 = load i32, i32* @SIGCHLD
%21 = load i32, i32* @SIG_IGN
%22 = tail call i32 @signal(i32 %20, i32 %21)
%23 = load i32, i32* @SIGHUP
%24 = load i32, i32* @SIG_IGN
%25 = tail call i32 @signal(i32 %23, i32 %24)
%26 = tail call i64 (...) @fork()
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %19
%32 = icmp eq i64 %26, 0
br i1 %32, label %36, label %33
33: ; preds = %31
%34 = load i32, i32* @EXIT_FAILURE
%35 = tail call i32 @exit(i32 %34)
unreachable
36: ; preds = %31
%37 = tail call i32 @umask(i32 0)
%38 = icmp eq i8* %0, null
br i1 %38, label %45, label %39
39: ; preds = %36
%40 = tail call i64 @chdir(i8* %0)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %39, %36
%46 = load i32, i32* @_SC_OPEN_MAX
%47 = tail call i32 @sysconf(i32 %46)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %45, %49
%50 = phi i32 [ %52, %49 ], [ %47, %45 ]
%51 = tail call i32 @close(i32 %50)
%52 = add nsw i32 %50, -1
%53 = icmp ugt i32 %50, 1
br i1 %53, label %49, label %54
54: ; preds = %49, %45
%55 = icmp eq i8* %1, null
br i1 %55, label %62, label %56
56: ; preds = %54
%57 = load i32, i32* @LOG_PID
%58 = load i32, i32* @LOG_CONS
%59 = or i32 %58, %57
%60 = load i32, i32* @LOG_DAEMON
%61 = tail call i32 @openlog(i8* %1, i32 %59, i32 %60)
br label %62
62: ; preds = %56, %54
ret i64 0
}