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


x86 assembly (clang, -O0):
<ttyin>:
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    118e <ttyin+0x5e>
lea    0xe87(%rip),%rdi
lea    0xe89(%rip),%rsi
mov    $0x0,%al
call   1030 <efopen@plt>
mov    %rax,0x2eba(%rip)
mov    -0x18(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    0x2ea6(%rip),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11c1 <ttyin+0x91>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x71,%eax
jne    11c8 <ttyin+0x98>
xor    %edi,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    -0x8(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyin>:
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    1183 <ttyin+0x53>
lea    0xe98(%rip),%rdi
lea    0xe9a(%rip),%rsi
xor    %eax,%eax
call   1030 <efopen@plt>
mov    %rax,%rdx
mov    %rax,0x2ec8(%rip)
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <fgets@plt>
test   %rax,%rax
je     11a1 <ttyin+0x71>
movsbl (%rbx),%eax
cmp    $0x71,%eax
je     11a1 <ttyin+0x71>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@ttyin.tty = internal global i32* null
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @ttyin() {
%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** @ttyin.tty
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32* (i8*, i8*, ...) bitcast (i32* (...)* @efopen to i32* (i8*, i8*, ...)*)(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** @ttyin.tty
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* @BUFSIZ
%13 = load i32*, i32** @ttyin.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 %23
21: ; preds = %16, %11
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %16
%24 = getelementptr inbounds i8, i8* %6, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = load i8*, i8** %1
ret i32 %26
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@ttyin.tty = internal unnamed_addr global i32* null
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @ttyin() {
%1 = load i32, i32* @BUFSIZ
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = load i32*, i32** @ttyin.tty
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32* (i8*, i8*, ...) bitcast (i32* (...)* @efopen to i32* (i8*, i8*, ...)*)(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** @ttyin.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 %19
17: ; preds = %14, %9
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %14
%20 = sext i8 %15 to i32
ret i32 %20
}