#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*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCNOTTY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
void processDaemonize() {
  int i;
  for (i = 0; i < 3; ++i) {
    close(i);
  }
  open("/", O_RDONLY);
  dup2(0, 1);
  dup2(0, 2);
  i = open("/dev/tty", O_RDWR);
  if (i >= 0) {
    ioctl(i, TIOCNOTTY, 0);
    close(i);
  }
}


x86 assembly (clang, -O0):
<processDaemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
cmpl   $0x3,-0x4(%rbp)
jge    116f <processDaemonize+0x2f>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    114f <processDaemonize+0xf>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     11d9 <processDaemonize+0x99>
mov    -0x4(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<processDaemonize>:
push   %rbx
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1060 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11be <processDaemonize+0x7e>
mov    %eax,%ebx
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1050 <close@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.1 = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@TIOCNOTTY = global i32 0
define void @processDaemonize() {
%1 = alloca i32
store i32 0, i32* %1
br label %2
2: ; preds = %8, %0
%3 = load i32, i32* %1
%4 = icmp slt i32 %3, 3
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = load i32, i32* %1
%7 = call i32 @close(i32 %6)
br label %8
8: ; preds = %5
%9 = load i32, i32* %1
%10 = add nsw i32 %9, 1
store i32 %10, i32* %1
br label %2
11: ; preds = %2
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = call i32 @dup2(i32 0, i32 1)
%15 = call i32 @dup2(i32 0, i32 2)
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 %17, i32* %1
%18 = load i32, i32* %1
%19 = icmp sge i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %11
%21 = load i32, i32* %1
%22 = load i32, i32* @TIOCNOTTY
%23 = call i32 @ioctl(i32 %21, i32 %22, i32 0)
%24 = load i32, i32* %1
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %20, %11
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.1 = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@TIOCNOTTY = global i32 0
define void @processDaemonize() {
%1 = tail call i32 @close(i32 0)
%2 = tail call i32 @close(i32 1)
%3 = tail call i32 @close(i32 2)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @dup2(i32 0, i32 1)
%7 = tail call i32 @dup2(i32 0, i32 2)
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* @TIOCNOTTY
%13 = tail call i32 @ioctl(i32 %9, i32 %12, i32 0)
%14 = tail call i32 @close(i32 %9)
br label %15
15: ; preds = %11, %0
ret void
}