#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 EOF;
scalar_t__ feof(int /*<<< orphan*/);
int getchar();
int isatty(int /*<<< orphan*/);
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ stdin;
int key() {
  int c;
  static int tty = -1;
  if (tty < 0)
    tty = isatty(0);
  if (feof(stdin) || (c = getchar()) == EOF) {
    c = '\n';
    if (tty)
      putchar('\n');
  }
  if (!tty)
    putchar(c);
  return c;
}


x86 assembly (clang, -O0):
<key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0x0,0x2ef1(%rip)
jge    1162 <key+0x22>
xor    %edi,%edi
call   1040 <isatty@plt>
mov    %eax,0x2ede(%rip)
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
call   1060 <feof@plt>
cmp    $0x0,%rax
jne    1193 <key+0x53>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x4(%rbp)
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%eax
jne    11b6 <key+0x76>
movl   $0xa,-0x4(%rbp)
cmpl   $0x0,0x2e9f(%rip)
je     11b1 <key+0x71>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11b6 <key+0x76>
cmpl   $0x0,0x2e83(%rip)
jne    11cb <key+0x8b>
mov    -0x4(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<key>:
push   %rbx
cmpl   $0x0,0x2ef8(%rip)
jns    1157 <key+0x17>
xor    %edi,%edi
call   1040 <isatty@plt>
mov    %eax,0x2ee9(%rip)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
call   1060 <feof@plt>
test   %rax,%rax
jne    117e <key+0x3e>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
mov    0x2e66(%rip),%rax
cmp    (%rax),%ebx
jne    1196 <key+0x56>
cmpl   $0x0,0x2ebb(%rip)
je     11a1 <key+0x61>
mov    $0xa,%ebx
mov    $0xa,%edi
call   1030 <putchar@plt>
cmpl   $0x0,0x2ea3(%rip)
jne    11ad <key+0x6d>
jmp    11a6 <key+0x66>
mov    $0xa,%ebx
mov    %ebx,%edi
call   1030 <putchar@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@key.tty = internal global i32 -1
@stdin = global i32 0
@EOF = global i32 0
define i32 @key() {
%1 = alloca i32
%2 = load i32, i32* @key.tty
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = call i32 @isatty(i32 0)
store i32 %5, i32* @key.tty
br label %6
6: ; preds = %4, %0
%7 = load i32, i32* @stdin
%8 = call i64 @feof(i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = call i32 (...) @getchar()
store i32 %11, i32* %1
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %20
14: ; preds = %10, %6
store i32 10, i32* %1
%15 = load i32, i32* @key.tty
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @putchar(i32 10)
br label %19
19: ; preds = %17, %14
br label %20
20: ; preds = %19, %10
%21 = load i32, i32* @key.tty
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i32, i32* %1
%25 = call i32 @putchar(i32 %24)
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@key.tty = internal unnamed_addr global i32 -1
@stdin = global i32 0
@EOF = global i32 0
define i32 @key() {
%1 = load i32, i32* @key.tty
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @isatty(i32 0)
store i32 %4, i32* @key.tty
br label %5
5: ; preds = %3, %0
%6 = load i32, i32* @stdin
%7 = tail call i64 @feof(i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = tail call i32 (...) @getchar()
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %18
13: ; preds = %9, %5
%14 = load i32, i32* @key.tty
%15 = icmp eq i32 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = tail call i32 @putchar(i32 10)
br label %18
18: ; preds = %16, %9
%19 = phi i32 [ %10, %9 ], [ 10, %16 ]
%20 = load i32, i32* @key.tty
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %13, %18
%23 = phi i32 [ %19, %18 ], [ 10, %13 ]
%24 = tail call i32 @putchar(i32 %23)
br label %25
25: ; preds = %22, %18
%26 = phi i32 [ %23, %22 ], [ %19, %18 ]
ret i32 %26
}