#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*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ setsid();
pid_t daemonize(void) {
  pid_t pid;
  int i;
  pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(1);
  }
  if (pid)
    return pid;
  if (setsid() < 0) {
    perror("setsid");
    exit(1);
  }
  for (i = 0; i <= 2; i++)
    (void)close(i);
  (void)chdir("/");
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1194 <daemonize+0x34>
lea    0xe7b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     11ac <daemonize+0x4c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    120e <daemonize+0xae>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    11d3 <daemonize+0x73>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jg     11fa <daemonize+0x9a>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11da <daemonize+0x7a>
lea    0xe0b(%rip),%rdi
call   1030 <chdir@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11aa <daemonize+0x4a>
mov    %rax,%rbx
jne    11a5 <daemonize+0x45>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     11b3 <daemonize+0x53>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
lea    0xe6c(%rip),%rdi
call   1030 <chdir@plt>
mov    %rbx,%rax
pop    %rbx
ret
lea    0xe4f(%rip),%rdi
jmp    11ba <daemonize+0x5a>
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [2 x i8] c"/\00"
define i64 @daemonize() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i32
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i64, i64* %2
store i64 %14, i64* %1
br label %33
15: ; preds = %10
%16 = call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15
store i32 0, i32* %3
br label %22
22: ; preds = %28, %21
%23 = load i32, i32* %3
%24 = icmp sle i32 %23, 2
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %25
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %22
31: ; preds = %22
%32 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i64 0, i64* %1
br label %33
33: ; preds = %31, %13
%34 = load i64, i64* %1
ret i64 %34
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [2 x i8] c"/\00"
define i64 @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %19
8: ; preds = %6
%9 = tail call i64 (...) @setsid()
%10 = icmp slt i64 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = tail call i32 @close(i32 0)
%13 = tail call i32 @close(i32 1)
%14 = tail call i32 @close(i32 2)
%15 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %19
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %6, %11
ret i64 %1
}