#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCNOTTY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
scalar_t__ stdout;
void detach() {
  int rc, fd;
  if ((rc = fork()) > 0)
    exit(0);
  else if (rc < 0) {
    perror("detach");
    exit(EXIT_FAILURE);
  }
  if ((fd = open("/dev/tty", O_RDWR, 0)) == -1) {
    printf("couldn't open tty, assuming still okay...\n");
    fflush((FILE *)stdout);
    return;
  }
  ioctl(fd, TIOCNOTTY, 0);
  close(fd);
  setsid();
}


x86 assembly (clang, -O0):
<detach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jle    11b2 <detach+0x22>
xor    %edi,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    11d6 <detach+0x46>
lea    0xe3d(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    11db <detach+0x4b>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0xe1c(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    121e <detach+0x8e>
lea    0xe0b(%rip),%rdi
call   1030 <printf@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%rdi
call   1070 <fflush@plt>
jmp    1240 <detach+0xb0>
mov    -0x8(%rbp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detach>:
push   %rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jg     11f9 <detach+0x69>
js     1200 <detach+0x70>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11dd <detach+0x4d>
mov    %eax,%ebx
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
jmp    1060 <setsid@plt>
lea    0xe2c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%rdi
pop    %rbx
jmp    1070 <fflush@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xdf9(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"detach\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.2 = [43 x i8] c"couldn't open tty, assuming still okay...\0A\00"
@stdout = global i64 0
@TIOCNOTTY = global i32 0
define void @detach() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @fork()
store i32 %3, i32* %1
%4 = icmp sgt 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 %14
10: ; preds = %7
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %7
br label %15
15: ; preds = %14
%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, i32 0)
store i32 %17, i32* %2
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%21 = load i64, i64* @stdout
%22 = inttoptr i64 %21 to i32*
%23 = call i32 @fflush(i32* %22)
br label %31
24: ; preds = %15
%25 = load i32, i32* %2
%26 = load i32, i32* @TIOCNOTTY
%27 = call i32 @ioctl(i32 %25, i32 %26, i32 0)
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
%30 = call i32 (...) @setsid()
br label %31
31: ; preds = %24, %19
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"detach\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@stdout = global i64 0
@TIOCNOTTY = global i32 0
@str = [42 x i8] c"couldn't open tty, assuming still okay...\00"
define void @detach() {
%1 = tail call i32 (...) @fork()
%2 = icmp sgt i32 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @exit(i32 0)
unreachable
5: ; preds = %0
%6 = icmp slt i32 %1, 0
br i1 %6, label %7, label %11
7: ; preds = %5
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %5
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%17 = load i64, i64* @stdout
%18 = inttoptr i64 %17 to i32*
%19 = tail call i32 @fflush(i32* %18)
br label %25
20: ; preds = %11
%21 = load i32, i32* @TIOCNOTTY
%22 = tail call i32 @ioctl(i32 %13, i32 %21, i32 0)
%23 = tail call i32 @close(i32 %13)
%24 = tail call i32 (...) @setsid()
br label %25
25: ; preds = %20, %15
ret void
}