#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;
scalar_t__ chdir(char const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ nxweb_die(char *);
int /*<<< orphan*/ nxweb_open_log_file(char const *);
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void nxweb_continue_as_daemon(const char *work_dir, const char *log_file) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  sid = setsid();
  if (sid < 0) {
    nxweb_die("setsid() failed");
  }
  umask(0);
  if (work_dir && chdir(work_dir) < 0) {
    nxweb_die("chdir(work_dir) failed");
  }
  if (log_file)
    nxweb_open_log_file(log_file);
}


x86 assembly (clang, -O0):
<nxweb_continue_as_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11a4 <nxweb_continue_as_daemon+0x34>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    11bd <nxweb_continue_as_daemon+0x4d>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11df <nxweb_continue_as_daemon+0x6f>
lea    0xe26(%rip),%rdi
call   1060 <nxweb_die@plt>
xor    %edi,%edi
call   1050 <umask@plt>
cmpq   $0x0,-0x8(%rbp)
je     1210 <nxweb_continue_as_daemon+0xa0>
mov    -0x8(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1210 <nxweb_continue_as_daemon+0xa0>
lea    0xe05(%rip),%rdi
call   1060 <nxweb_die@plt>
cmpq   $0x0,-0x10(%rbp)
je     1224 <nxweb_continue_as_daemon+0xb4>
mov    -0x10(%rbp),%rdi
call   1080 <nxweb_open_log_file@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nxweb_continue_as_daemon>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11e6 <nxweb_continue_as_daemon+0x76>
jne    11f4 <nxweb_continue_as_daemon+0x84>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
jns    11a0 <nxweb_continue_as_daemon+0x30>
lea    0xe65(%rip),%rdi
call   1060 <nxweb_die@plt>
xor    %edi,%edi
call   1050 <umask@plt>
test   %rbx,%rbx
je     11b9 <nxweb_continue_as_daemon+0x49>
mov    %rbx,%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     11cd <nxweb_continue_as_daemon+0x5d>
test   %r14,%r14
je     11de <nxweb_continue_as_daemon+0x6e>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1080 <nxweb_open_log_file@plt>
lea    0xe3c(%rip),%rdi
call   1060 <nxweb_die@plt>
test   %r14,%r14
jne    11be <nxweb_continue_as_daemon+0x4e>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [16 x i8] c"setsid() failed\00"
@.str.1 = [23 x i8] c"chdir(work_dir) failed\00"
define void @nxweb_continue_as_daemon(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
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()
store i64 %20, i64* %6
%21 = load i64, i64* %6
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @nxweb_die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = call i32 @umask(i32 0)
%27 = load i8*, i8** %3
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i8*, i8** %3
%31 = call i64 @chdir(i8* %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @nxweb_die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %29, %25
%36 = load i8*, i8** %4
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i8*, i8** %4
%40 = call i32 @nxweb_open_log_file(i8* %39)
br label %41
41: ; preds = %38, %35
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [16 x i8] c"setsid() failed\00"
@.str.1 = [23 x i8] c"chdir(work_dir) failed\00"
define void @nxweb_continue_as_daemon(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 %18
16: ; preds = %13
%17 = tail call i32 @nxweb_die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = tail call i32 @umask(i32 0)
%20 = icmp eq i8* %0, null
br i1 %20, label %26, label %21
21: ; preds = %18
%22 = tail call i64 @chdir(i8* %0)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @nxweb_die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %21, %18
%27 = icmp eq i8* %1, null
br i1 %27, label %30, label %28
28: ; preds = %26
%29 = tail call i32 @nxweb_open_log_file(i8* %1)
br label %30
30: ; preds = %28, %26
ret void
}