#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*/ EXIT_SUCCESS;
int FOPEN_MAX;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ setsid();
void othello_daemonize(void) {
  int fd;
  chdir("/");
  if (fork() != 0) {
    exit(EXIT_SUCCESS);
  }
  setsid();
  if (fork() != 0) {
    exit(EXIT_SUCCESS);
  }
  for (fd = 0; fd < FOPEN_MAX; fd++) {
    close(fd);
  }
}


x86 assembly (clang, -O0):
<othello_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
call   1030 <chdir@plt>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     1183 <othello_daemonize+0x33>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     11a9 <othello_daemonize+0x59>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%eax
jge    11d8 <othello_daemonize+0x88>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11b0 <othello_daemonize+0x60>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<othello_daemonize>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
call   1030 <chdir@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11a7 <othello_daemonize+0x57>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11a7 <othello_daemonize+0x57>
mov    0x2e62(%rip),%r14
cmpl   $0x0,(%r14)
jle    119f <othello_daemonize+0x4f>
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
cmp    (%r14),%ebx
jl     1190 <othello_daemonize+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

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