#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 */
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ redirect(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stdin;
int daemonize(char *outfile) {
  pid_t pid;
  if (outfile != NULL) {
    if (redirect(outfile))
      return 1;
  }
  if ((pid = fork()) != 0) {
    if (pid < 0)
      return -1;
    exit(0);
  }
  setsid();
  fclose(stdin);
  if (chdir("/") != 0) {
    ;
  }
  if ((pid = fork()) != 0) {
    if (pid < 0)
      return -1;
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119b <daemonize+0x3b>
mov    -0x10(%rbp),%rdi
call   1030 <redirect@plt>
cmp    $0x0,%rax
je     1196 <daemonize+0x36>
movl   $0x1,-0x4(%rbp)
jmp    1238 <daemonize+0xd8>
jmp    119b <daemonize+0x3b>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11ce <daemonize+0x6e>
cmpq   $0x0,-0x18(%rbp)
jge    11c7 <daemonize+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <daemonize+0xd8>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1040 <fclose@plt>
lea    0xe16(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     11fe <daemonize+0x9e>
jmp    11fe <daemonize+0x9e>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1231 <daemonize+0xd1>
cmpq   $0x0,-0x18(%rbp)
jge    122a <daemonize+0xca>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <daemonize+0xd8>
xor    %edi,%edi
call   1070 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
test   %rdi,%rdi
je     1175 <daemonize+0x15>
call   1030 <redirect@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    11b7 <daemonize+0x57>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    11b0 <daemonize+0x50>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1040 <fclose@plt>
lea    0xe61(%rip),%rdi
call   1050 <chdir@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11b7 <daemonize+0x57>
mov    $0xffffffff,%ebx
jns    11bb <daemonize+0x5b>
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1070 <exit@plt>

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

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