#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 EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(void) {
  pid_t pid = fork();
  if (pid < 0) {
    exit(1);
  } else if (pid != 0) {
    exit(EXIT_SUCCESS);
  } else {
    umask(0);
    if ((chdir("/")) < 0) {
      exit(EXIT_FAILURE);
    }
    pid = fork();
    if (pid < 0) {
      exit(1);
    } else if (pid != 0) {
      ((void)0);
      exit(EXIT_SUCCESS);
    }
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1168 <daemonize+0x28>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     1181 <daemonize+0x41>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
xor    %edi,%edi
call   1040 <umask@plt>
lea    0xe71(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    11ac <daemonize+0x6c>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11cc <daemonize+0x8c>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     11e5 <daemonize+0xa5>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    11ea <daemonize+0xaa>
jmp    11ef <daemonize+0xaf>
jmp    11f4 <daemonize+0xb4>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     1177 <daemonize+0x37>
jne    1181 <daemonize+0x41>
xor    %edi,%edi
call   1040 <umask@plt>
lea    0xea3(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     118f <daemonize+0x4f>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     1177 <daemonize+0x37>
jne    1181 <daemonize+0x41>
pop    %rax
ret
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

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

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