#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ detachFromTerminal();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
void demonize(char *arg) {
  int childpid;
  (void)signal(SIGHUP, SIG_IGN);
  (void)signal(SIGCHLD, SIG_IGN);
  (void)signal(SIGQUIT, SIG_IGN);
  if ((childpid = fork()) < 0) {
    printf("An error occurred while demonizing %s...\n", arg);
  } else {
    if (!childpid) {
      printf("Becoming a daemon...\n");
      detachFromTerminal();
    } else {
      exit(EXIT_SUCCESS);
    }
  }
}


x86 assembly (clang, -O0):
<demonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11cb <demonize+0x7b>
mov    -0x8(%rbp),%rsi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1202 <demonize+0xb2>
cmpl   $0x0,-0xc(%rbp)
jne    11ef <demonize+0x9f>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <detachFromTerminal@plt>
jmp    11fd <demonize+0xad>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1202 <demonize+0xb2>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<demonize>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rbx
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     11c5 <demonize+0x65>
jne    11dd <demonize+0x7d>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <detachFromTerminal@plt>
lea    0xe34(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <printf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIGCHLD = global i32 0
@SIGQUIT = global i32 0
@.str = [42 x i8] c"An error occurred while demonizing %s...\0A\00"
@.str.1 = [22 x i8] c"Becoming a daemon...\0A\00"
@EXIT_SUCCESS = global i32 0
define void @demonize(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i32, i32* @SIGHUP
%5 = load i32, i32* @SIG_IGN
%6 = call i32 @signal(i32 %4, i32 %5)
%7 = load i32, i32* @SIGCHLD
%8 = load i32, i32* @SIG_IGN
%9 = call i32 @signal(i32 %7, i32 %8)
%10 = load i32, i32* @SIGQUIT
%11 = load i32, i32* @SIG_IGN
%12 = call i32 @signal(i32 %10, i32 %11)
%13 = call i32 (...) @fork()
store i32 %13, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %28
18: ; preds = %1
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 (...) @detachFromTerminal()
br label %27
24: ; preds = %18
%25 = load i32, i32* @EXIT_SUCCESS
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %21
br label %28
28: ; preds = %27, %15
ret void
}

LLVM IR (-O2):
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIGCHLD = global i32 0
@SIGQUIT = global i32 0
@.str = [42 x i8] c"An error occurred while demonizing %s...\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [21 x i8] c"Becoming a daemon...\00"
define void @demonize(i8* %0) {
%2 = load i32, i32* @SIGHUP
%3 = load i32, i32* @SIG_IGN
%4 = tail call i32 @signal(i32 %2, i32 %3)
%5 = load i32, i32* @SIGCHLD
%6 = load i32, i32* @SIG_IGN
%7 = tail call i32 @signal(i32 %5, i32 %6)
%8 = load i32, i32* @SIGQUIT
%9 = load i32, i32* @SIG_IGN
%10 = tail call i32 @signal(i32 %8, i32 %9)
%11 = tail call i32 (...) @fork()
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %23
15: ; preds = %1
%16 = icmp eq i32 %11, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 (...) @detachFromTerminal()
br label %23
20: ; preds = %15
%21 = load i32, i32* @EXIT_SUCCESS
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %17, %13
ret void
}