#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*/ STDIN_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ do_fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ setsid();
void daemonize() {
  do_fork();
  if (setsid() < 0)
    perror("Failed to setsid()");
  do_fork();
  if (chdir("/"))
    perror("Failed to free working directory");
  close(STDIN_FILENO);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
mov    $0x0,%al
call   1030 <do_fork@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    1178 <daemonize+0x28>
lea    0xe8d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1030 <do_fork@plt>
lea    0xe8d(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11a1 <daemonize+0x51>
lea    0xe79(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   1030 <do_fork@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     118b <daemonize+0x3b>
xor    %eax,%eax
call   1030 <do_fork@plt>
lea    0xea1(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    1199 <daemonize+0x49>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
pop    %rax
jmp    1050 <close@plt>
lea    0xe6e(%rip),%rdi
call   1070 <perror@plt>
jmp    1164 <daemonize+0x14>
lea    0xe75(%rip),%rdi
call   1070 <perror@plt>
jmp    117c <daemonize+0x2c>

LLVM IR (-O0):
@.str = [19 x i8] c"Failed to setsid()\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [33 x i8] c"Failed to free working directory\00"
@STDIN_FILENO = global i32 0
define void @daemonize() {
%1 = call i32 (...) @do_fork()
%2 = call i64 (...) @setsid()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = call i32 (...) @do_fork()
%8 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = load i32, i32* @STDIN_FILENO
%14 = call i32 @close(i32 %13)
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"Failed to setsid()\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [33 x i8] c"Failed to free working directory\00"
@STDIN_FILENO = global i32 0
define void @daemonize() {
%1 = tail call i32 (...) @do_fork()
%2 = tail call i64 (...) @setsid()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = tail call i32 (...) @do_fork()
%8 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = load i32, i32* @STDIN_FILENO
%14 = tail call i32 @close(i32 %13)
ret void
}