#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*/ LOG_PID;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ openlog(char const *, int /*<<< orphan*/, int);
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int daemonize(const char *pname, int facility) {
  pid_t pid;
  pid = fork();
  if (pid < 0)
    return -1;
  else if (pid > 0)
    _exit(0);
  if (setsid() < 0)
    ;
  return -1;
  signal(SIGHUP, SIG_IGN);
  pid = fork();
  if (pid < 0)
    return -1;
  else if (pid > 0)
    _exit(0);
  chdir("/");
  for (int i = 0; i < 64; i++) {
    close(i);
  }
  open("/dev/NULL", O_RDONLY);
  open("/dev/NULL", O_RDWR);
  open("/dev/NULL", O_RDWR);
  openlog(pname, LOG_PID, facility);
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1161 <daemonize+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <daemonize+0x65>
cmpq   $0x0,-0x20(%rbp)
jle    1173 <daemonize+0x43>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    1178 <daemonize+0x48>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0x0,%rax
jge    118e <daemonize+0x5e>
jmp    118e <daemonize+0x5e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
js     1146 <daemonize+0x16>
jne    114d <daemonize+0x1d>
xor    %eax,%eax
call   1040 <setsid@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@LOG_PID = global i32 0
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
define i32 @daemonize(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %22
11: ; preds = %2
%12 = load i64, i64* %6
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @_exit(i32 0)
unreachable
16: ; preds = %11
br label %17
17: ; preds = %16
%18 = call i64 (...) @setsid()
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
br label %21
21: ; preds = %20, %17
store i32 -1, i32* %3
br label %22
22: ; preds = %21, %10
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
@LOG_PID = global i32 0
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
define i32 @daemonize(i8* nocapture readnone %0, i32 %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = icmp eq i64 %3, 0
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = tail call i32 @_exit(i32 0)
unreachable
9: ; preds = %5
%10 = tail call i64 (...) @setsid()
br label %11
11: ; preds = %2, %9
ret i32 -1
}