#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*/ Panic(int, char *);
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ setpgrp();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void Daemonize(void) {
  int rc;
  close(2);
  close(1);
  close(0);
  setsid();
  setpgrp();
  umask(0);
  rc = fork();
  if (rc < 0)
    Panic(0x0001, "cannot daemonize [%m]");
  else if (rc > 0)
    _exit(0);
}


x86 assembly (clang, -O0):
<Daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x2,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   1080 <setpgrp@plt>
xor    %edi,%edi
call   1060 <umask@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11d2 <Daemonize+0x62>
mov    $0x1,%edi
lea    0xe38(%rip),%rsi
call   1070 <Panic@plt>
jmp    11e8 <Daemonize+0x78>
cmpl   $0x0,-0x4(%rbp)
jle    11e3 <Daemonize+0x73>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    11e8 <Daemonize+0x78>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Daemonize>:
push   %rax
mov    $0x2,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   1080 <setpgrp@plt>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     11b0 <Daemonize+0x40>
jne    11c2 <Daemonize+0x52>
pop    %rax
ret
lea    0xe49(%rip),%rsi
mov    $0x1,%edi
pop    %rax
jmp    1070 <Panic@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"cannot daemonize [%m]\00"
define void @Daemonize() {
%1 = alloca i32
%2 = call i32 @close(i32 2)
%3 = call i32 @close(i32 1)
%4 = call i32 @close(i32 0)
%5 = call i32 (...) @setsid()
%6 = call i32 (...) @setpgrp()
%7 = call i32 @umask(i32 0)
%8 = call i32 (...) @fork()
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @Panic(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %19
13: ; preds = %0
%14 = load i32, i32* %1
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @_exit(i32 0)
unreachable
18: ; preds = %13
br label %19
19: ; preds = %18, %11
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"cannot daemonize [%m]\00"
define void @Daemonize() {
%1 = tail call i32 @close(i32 2)
%2 = tail call i32 @close(i32 1)
%3 = tail call i32 @close(i32 0)
%4 = tail call i32 (...) @setsid()
%5 = tail call i32 (...) @setpgrp()
%6 = tail call i32 @umask(i32 0)
%7 = tail call i32 (...) @fork()
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @Panic(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %15
11: ; preds = %0
%12 = icmp eq i32 %7, 0
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = tail call i32 @_exit(i32 0)
unreachable
15: ; preds = %11, %9
ret void
}