#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*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemon2() {
  int pid;
  int i;
  if (pid = fork())
    exit(0);
  else if (pid < 0)
    exit(1);
  setsid();
  if (pid = fork())
    exit(0);
  else if (pid < 0)
    exit(1);
  for (i = 0; i < 3; i++) {
    close(i);
  }
  chdir("/tmp");
  umask(0);
}


x86 assembly (clang, -O0):
<daemon2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     1182 <daemon2+0x22>
xor    %edi,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    1196 <daemon2+0x36>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    119b <daemon2+0x3b>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     11bc <daemon2+0x5c>
xor    %edi,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    11d0 <daemon2+0x70>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11d5 <daemon2+0x75>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11fc <daemon2+0x9c>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11dc <daemon2+0x7c>
lea    0xdfd(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon2>:
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11ad <daemon2+0x4d>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11ad <daemon2+0x4d>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
lea    0xe60(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
pop    %rax
jmp    1060 <umask@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

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