#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;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strchr(char *, int);
char get_key(char *valid, char *prompt) {
  int ch, okay;
  while (1) {
    if (prompt)
      printf("%s ", prompt);
    fflush(stdout);
    while (ch = getchar(), ch == ' ' || ch == '\t')
      ;
    if (ch == EOF)
      exit(1);
    if (!strchr(valid, okay = ch))
      okay = 0;
    while (ch = getchar(), ch != '\n' && ch != EOF)
      ;
    if (ch == EOF)
      exit(1);
    if (okay)
      return okay;
    printf("Invalid input.\n");
  }
}


x86 assembly (clang, -O0):
<get_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117d <get_key+0x2d>
mov    -0x10(%rbp),%rsi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1,%al
cmpl   $0x20,-0x14(%rbp)
mov    %al,-0x19(%rbp)
je     11ae <get_key+0x5e>
cmpl   $0x9,-0x14(%rbp)
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11be <get_key+0x6e>
jmp    11c3 <get_key+0x73>
jmp    118b <get_key+0x3b>
mov    -0x14(%rbp),%eax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%eax
jne    11df <get_key+0x8f>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    %esi,-0x18(%rbp)
call   1030 <strchr@plt>
cmp    $0x0,%eax
jne    11fe <get_key+0xae>
movl   $0x0,-0x18(%rbp)
jmp    1203 <get_key+0xb3>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0xa,-0x14(%rbp)
mov    %al,-0x1a(%rbp)
je     122e <get_key+0xde>
mov    -0x14(%rbp),%eax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
test   $0x1,%al
jne    123e <get_key+0xee>
jmp    1243 <get_key+0xf3>
jmp    1203 <get_key+0xb3>
mov    -0x14(%rbp),%eax
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%eax
jne    125f <get_key+0x10f>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
je     1275 <get_key+0x125>
mov    -0x18(%rbp),%eax
movsbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1160 <get_key+0x10>

x86 assembly (clang, -O2):
<get_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r14
mov    0x2e70(%rip),%r12
lea    0xe85(%rip),%r15
test   %r13,%r13
je     11a1 <get_key+0x41>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xe69(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
nop
xor    %eax,%eax
call   1060 <getchar@plt>
cmp    $0x9,%eax
je     11b0 <get_key+0x50>
mov    %eax,%ebp
cmp    $0x20,%eax
je     11b0 <get_key+0x50>
cmp    (%r12),%ebp
je     1221 <get_key+0xc1>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <strchr@plt>
mov    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1060 <getchar@plt>
mov    (%r12),%ecx
cmp    $0xa,%eax
je     11f4 <get_key+0x94>
cmp    %ecx,%eax
jne    11e0 <get_key+0x80>
test   %ebx,%ebx
cmove  %ebx,%ebp
cmp    %ecx,%eax
je     1221 <get_key+0xc1>
test   %ebp,%ebp
jne    1210 <get_key+0xb0>
mov    %r15,%rdi
call   1030 <puts@plt>
test   %r13,%r13
jne    1190 <get_key+0x30>
jmp    11a1 <get_key+0x41>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%s \00"
@stdout = global i32 0
@EOF = global i32 0
@.str.1 = [16 x i8] c"Invalid input.\0A\00"
define signext i8 @get_key(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
br label %7
7: ; preds = %2, %62
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i8*, i8** %4
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %10, %7
%14 = load i32, i32* @stdout
%15 = call i32 @fflush(i32 %14)
br label %16
16: ; preds = %25, %13
%17 = call i32 (...) @getchar()
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 32
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 9
br label %23
23: ; preds = %20, %16
%24 = phi i1 [ true, %16 ], [ %22, %20 ]
br i1 %24, label %25, label %26
25: ; preds = %23
br label %16
26: ; preds = %23
%27 = load i32, i32* %5
%28 = load i32, i32* @EOF
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %26
%33 = load i8*, i8** %3
%34 = load i32, i32* %5
store i32 %34, i32* %6
%35 = call i32 @strchr(i8* %33, i32 %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %32
store i32 0, i32* %6
br label %38
38: ; preds = %37, %32
br label %39
39: ; preds = %49, %38
%40 = call i32 (...) @getchar()
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp ne i32 %41, 10
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32, i32* %5
%45 = load i32, i32* @EOF
%46 = icmp ne i32 %44, %45
br label %47
47: ; preds = %43, %39
%48 = phi i1 [ false, %39 ], [ %46, %43 ]
br i1 %48, label %49, label %50
49: ; preds = %47
br label %39
50: ; preds = %47
%51 = load i32, i32* %5
%52 = load i32, i32* @EOF
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %50
%57 = load i32, i32* %6
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i32, i32* %6
%61 = trunc i32 %60 to i8
ret i8 %61
62: ; preds = %56
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %7
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s \00"
@stdout = global i32 0
@EOF = global i32 0
@str = [15 x i8] c"Invalid input.\00"
define signext i8 @get_key(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br label %4
4: ; preds = %36, %2
br i1 %3, label %7, label %5
5: ; preds = %4
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %7
7: ; preds = %5, %4
%8 = load i32, i32* @stdout
%9 = tail call i32 @fflush(i32 %8)
br label %10
10: ; preds = %12, %7
%11 = tail call i32 (...) @getchar()
switch i32 %11, label %13 [
i32 32, label %12
i32 9, label %12
]
12: ; preds = %10, %10
br label %10
13: ; preds = %10
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %11, %14
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %13
%19 = tail call i32 @strchr(i8* %0, i32 %11)
%20 = icmp eq i32 %19, 0
br label %21
21: ; preds = %21, %18
%22 = tail call i32 (...) @getchar()
%23 = icmp ne i32 %22, 10
%24 = load i32, i32* @EOF
%25 = icmp ne i32 %22, %24
%26 = select i1 %23, i1 %25, i1 false
br i1 %26, label %21, label %27
27: ; preds = %21
%28 = select i1 %20, i32 0, i32 %11
%29 = icmp eq i32 %22, %24
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %27
%33 = icmp eq i32 %28, 0
br i1 %33, label %36, label %34
34: ; preds = %32
%35 = trunc i32 %28 to i8
ret i8 %35
36: ; preds = %32
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %4
}