#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 */
scalar_t__ ENXIO;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCNOTTY;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void zerossh_detach_tty(void) {
  int fd;
  fd = open("/dev/tty", O_RDWR);
  if (fd < 0 && errno != ENXIO) {
    perror("open /dev/tty");
    abort();
  }
  ioctl(fd, TIOCNOTTY);
  close(fd);
}


x86 assembly (clang, -O0):
<zerossh_detach_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11a7 <zerossh_detach_tty+0x57>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
je     11a7 <zerossh_detach_tty+0x57>
lea    0xe65(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1040 <ioctl@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zerossh_detach_tty>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xea4(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1182 <zerossh_detach_tty+0x32>
mov    0x2e5d(%rip),%rax
mov    (%rax),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
jne    119a <zerossh_detach_tty+0x4a>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1050 <close@plt>
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENXIO = global i64 0
@.str.1 = [14 x i8] c"open /dev/tty\00"
@TIOCNOTTY = global i32 0
define void @zerossh_detach_tty() {
%1 = alloca i32
%2 = load i32, i32* @O_RDWR
%3 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %2)
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %0
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENXIO
%9 = icmp ne i64 %7, %8
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (...) @abort()
unreachable
13: ; preds = %6, %0
%14 = load i32, i32* %1
%15 = load i32, i32* @TIOCNOTTY
%16 = call i32 @ioctl(i32 %14, i32 %15)
%17 = load i32, i32* %1
%18 = call i32 @close(i32 %17)
ret void
}

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