#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 BUFSIZ;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *openfile(char *, char *);
void opentty(void) {
  char buf[BUFSIZ];
  static FILE *tty = NULL;
  if (tty == NULL)
    tty = openfile("/dev/tty", "r");
  if (fgets(buf, BUFSIZ, tty) == NULL || buf[0] == 'q')
    exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<opentty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x8(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x10(%rbp)
cmpq   $0x0,0x2edc(%rip)
jne    118c <opentty+0x5c>
lea    0xe87(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1030 <openfile@plt>
mov    %rax,0x2ebc(%rip)
mov    -0x18(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2ea8(%rip),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11bf <opentty+0x8f>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x71,%eax
jne    11cd <opentty+0x9d>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opentty>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2ea2(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2eec(%rip),%rdx
test   %rdx,%rdx
jne    1181 <opentty+0x51>
lea    0xe98(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1030 <openfile@plt>
mov    %rax,%rdx
mov    %rax,0x2eca(%rip)
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <fgets@plt>
test   %rax,%rax
je     119c <opentty+0x6c>
cmpb   $0x71,(%rbx)
je     119c <opentty+0x6c>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@opentty.tty = internal global i32* null
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
define void @opentty() {
%1 = alloca i8*
%2 = alloca i64
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = call i8* @llvm.stacksave()
store i8* %5, i8** %1
%6 = alloca i8, i64 %4
store i64 %4, i64* %2
%7 = load i32*, i32** @opentty.tty
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32* @openfile(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** @opentty.tty
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* @BUFSIZ
%13 = load i32*, i32** @opentty.tty
%14 = call i32* @fgets(i8* %6, i32 %12, i32* %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = getelementptr inbounds i8, i8* %6, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 113
br i1 %20, label %21, label %24
21: ; preds = %16, %11
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %16
%25 = load i8*, i8** %1
ret void
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@opentty.tty = internal unnamed_addr global i32* null
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
define void @opentty() {
%1 = load i32, i32* @BUFSIZ
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = load i32*, i32** @opentty.tty
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32* @openfile(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** @opentty.tty
%8 = load i32, i32* @BUFSIZ
br label %9
9: ; preds = %6, %0
%10 = phi i32* [ %7, %6 ], [ %4, %0 ]
%11 = phi i32 [ %8, %6 ], [ %1, %0 ]
%12 = call i32* @fgets(i8* %3, i32 %11, i32* %10)
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = load i8, i8* %3
%16 = icmp eq i8 %15, 113
br i1 %16, label %17, label %20
17: ; preds = %14, %9
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %14
ret void
}