#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*/ SIGHUP;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ handle_error(char *);
scalar_t__ setsid();
scalar_t__ signal(int /*<<< orphan*/, int /*<<< orphan*/ *);
void daemonize() {
  pid_t pid = -1;
  pid = fork();
  if (pid == 0) {
    pid_t pgid = setsid();
    if (pgid == -1) {
      handle_error("Error setting sid");
    }
    if (signal(SIGHUP, NULL) == SIG_ERR) {
      handle_error("Error on signal");
    }
    pid_t npid = -1;
    npid = fork();
    if (npid == 0) {
      return;
    } else if (npid > 0) {
      exit(0);
    } else {
      handle_error("Error on fork");
    }
  } else if (pid > 0) {
    exit(0);
  } else {
    handle_error("Error on fork");
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1211 <daemonize+0xc1>
mov    $0x0,%al
call   1030 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    1198 <daemonize+0x48>
lea    0xe6d(%rip),%rdi
call   1070 <handle_error@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <signal@plt>
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
jne    11c6 <daemonize+0x76>
lea    0xe51(%rip),%rdi
call   1070 <handle_error@plt>
movq   $0xffffffffffffffff,-0x18(%rbp)
ff
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e9 <daemonize+0x99>
jmp    1234 <daemonize+0xe4>
cmpq   $0x0,-0x18(%rbp)
jle    11fb <daemonize+0xab>
xor    %edi,%edi
call   1050 <exit@plt>
lea    0xe20(%rip),%rdi
call   1070 <handle_error@plt>
jmp    120c <daemonize+0xbc>
jmp    1234 <daemonize+0xe4>
cmpq   $0x0,-0x8(%rbp)
jle    1223 <daemonize+0xd3>
xor    %edi,%edi
call   1050 <exit@plt>
lea    0xdf8(%rip),%rdi
call   1070 <handle_error@plt>
jmp    1234 <daemonize+0xe4>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     116c <daemonize+0x1c>
jg     11bb <daemonize+0x6b>
lea    0xebc(%rip),%rdi
pop    %rax
jmp    1070 <handle_error@plt>
xor    %eax,%eax
call   1030 <setsid@plt>
cmp    $0xffffffffffffffff,%rax
jne    1185 <daemonize+0x35>
lea    0xe80(%rip),%rdi
call   1070 <handle_error@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <signal@plt>
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <daemonize+0x5d>
lea    0xe6a(%rip),%rdi
call   1070 <handle_error@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     11c2 <daemonize+0x72>
jle    115f <daemonize+0xf>
xor    %edi,%edi
call   1050 <exit@plt>
pop    %rax
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Error setting sid\00"
@SIGHUP = global i32 0
@SIG_ERR = global i64 0
@.str.1 = [16 x i8] c"Error on signal\00"
@.str.2 = [14 x i8] c"Error on fork\00"
define void @daemonize() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
store i64 -1, i64* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %1
%5 = load i64, i64* %1
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %34
7: ; preds = %0
%8 = call i64 (...) @setsid()
store i64 %8, i64* %2
%9 = load i64, i64* %2
%10 = icmp eq i64 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @handle_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
%14 = load i32, i32* @SIGHUP
%15 = call i64 @signal(i32 %14, i32* null)
%16 = load i64, i64* @SIG_ERR
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @handle_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
store i64 -1, i64* %3
%21 = call i64 (...) @fork()
store i64 %21, i64* %3
%22 = load i64, i64* %3
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
br label %42
25: ; preds = %20
%26 = load i64, i64* %3
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %25
%31 = call i32 @handle_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30
br label %33
33: ; preds = %32
br label %42
34: ; preds = %0
%35 = load i64, i64* %1
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %34
%40 = call i32 @handle_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39
br label %42
42: ; preds = %24, %41, %33
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"Error setting sid\00"
@SIGHUP = global i32 0
@SIG_ERR = global i64 0
@.str.1 = [16 x i8] c"Error on signal\00"
@.str.2 = [14 x i8] c"Error on fork\00"
define void @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %22
3: ; preds = %0
%4 = tail call i64 (...) @setsid()
%5 = icmp eq i64 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @handle_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %3
%9 = load i32, i32* @SIGHUP
%10 = tail call i64 @signal(i32 %9, i32* null)
%11 = load i64, i64* @SIG_ERR
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @handle_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %8
%16 = tail call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %28, label %18
18: ; preds = %15
%19 = icmp sgt i64 %16, 0
br i1 %19, label %20, label %26
20: ; preds = %18
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %0
%23 = icmp sgt i64 %1, 0
br i1 %23, label %24, label %26
24: ; preds = %22
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %22, %18
%27 = tail call i32 @handle_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %15
ret void
}