#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 /*<<< orphan*/ fflush(int /*<<< orphan*/);
char fgetc(int /*<<< orphan*/);
int kbhit();
int /*<<< orphan*/ nonblock(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
char option_selected(void) {
  char c;
  int key_hit = 0;
  nonblock(1);
  while (!key_hit) {
    usleep(1);
    key_hit = kbhit();
    if (key_hit != 0) {
      c = fgetc(stdin);
      if (c == '1' || c == '2' || c == 'q')
        key_hit = 1;
      else {
        key_hit = 0;
        printf("\nInvalid Input\n");
        printf(" To select press [1/2/q]: ");
        fflush(stdout);
      }
    }
  }
  nonblock(0);
  printf("\n");
  return c;
}


x86 assembly (clang, -O0):
<option_selected>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
mov    $0x1,%edi
call   1030 <nonblock@plt>
cmpl   $0x0,-0x8(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    118f <option_selected+0x2f>
jmp    1228 <option_selected+0xc8>
mov    $0x1,%edi
call   1080 <usleep@plt>
mov    $0x0,%al
call   1050 <kbhit@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1223 <option_selected+0xc3>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1060 <fgetc@plt>
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0x31,%eax
je     11e5 <option_selected+0x85>
movsbl -0x1(%rbp),%eax
cmp    $0x32,%eax
je     11e5 <option_selected+0x85>
movsbl -0x1(%rbp),%eax
cmp    $0x71,%eax
jne    11f1 <option_selected+0x91>
movl   $0x1,-0x8(%rbp)
jmp    121e <option_selected+0xbe>
movl   $0x0,-0x8(%rbp)
lea    0xe01(%rip),%rdi
call   1040 <printf@plt>
lea    0xe05(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    1223 <option_selected+0xc3>
jmp    1179 <option_selected+0x19>
xor    %edi,%edi
call   1030 <nonblock@plt>
lea    0xdd8(%rip),%rdi
call   1040 <printf@plt>
movsbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<option_selected>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0x1,%edi
call   1040 <nonblock@plt>
mov    0x2e4e(%rip),%r12
lea    0xe7a(%rip),%r14
lea    0xe58(%rip),%r15
mov    0x2e29(%rip),%r13
nop
mov    $0x1,%edi
call   10a0 <usleep@plt>
xor    %eax,%eax
call   1070 <kbhit@plt>
test   %eax,%eax
je     11b0 <option_selected+0x30>
mov    (%r12),%edi
call   1080 <fgetc@plt>
mov    %eax,%ebx
lea    -0x31(%rbx),%eax
cmp    $0x2,%al
jb     11f7 <option_selected+0x77>
cmp    $0x71,%bl
je     11f7 <option_selected+0x77>
mov    %r14,%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
call   1060 <printf@plt>
mov    0x0(%r13),%edi
call   1090 <fflush@plt>
jmp    11b0 <option_selected+0x30>
xor    %edi,%edi
call   1040 <nonblock@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [16 x i8] c"\0AInvalid Input\0A\00"
@.str.1 = [27 x i8] c" To select press [1/2/q]: \00"
@stdout = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
define signext i8 @option_selected() {
%1 = alloca i8
%2 = alloca i32
store i32 0, i32* %2
%3 = call i32 @nonblock(i32 1)
br label %4
4: ; preds = %34, %0
%5 = load i32, i32* %2
%6 = icmp ne i32 %5, 0
%7 = xor i1 %6, true
br i1 %7, label %8, label %35
8: ; preds = %4
%9 = call i32 @usleep(i32 1)
%10 = call i32 (...) @kbhit()
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %34
13: ; preds = %8
%14 = load i32, i32* @stdin
%15 = call signext i8 @fgetc(i32 %14)
store i8 %15, i8* %1
%16 = load i8, i8* %1
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 49
br i1 %18, label %27, label %19
19: ; preds = %13
%20 = load i8, i8* %1
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 50
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i8, i8* %1
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 113
br i1 %26, label %27, label %28
27: ; preds = %23, %19, %13
store i32 1, i32* %2
br label %33
28: ; preds = %23
store i32 0, i32* %2
%29 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
br label %33
33: ; preds = %28, %27
br label %34
34: ; preds = %33, %8
br label %4
35: ; preds = %4
%36 = call i32 @nonblock(i32 0)
%37 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%38 = load i8, i8* %1
ret i8 %38
}

LLVM IR (-O2):
@stdin = global i32 0
@.str.1 = [27 x i8] c" To select press [1/2/q]: \00"
@stdout = global i32 0
@str = [15 x i8] c"\0AInvalid Input\00"
define signext i8 @option_selected() {
%1 = tail call i32 @nonblock(i32 1)
br label %2
2: ; preds = %14, %0
%3 = tail call i32 @usleep(i32 1)
%4 = tail call i32 (...) @kbhit()
%5 = icmp eq i32 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = load i32, i32* @stdin
%8 = tail call signext i8 @fgetc(i32 %7)
switch i8 %8, label %9 [
i8 113, label %15
i8 50, label %15
i8 49, label %15
]
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @stdout
%13 = tail call i32 @fflush(i32 %12)
br label %14
14: ; preds = %9, %2
br label %2
15: ; preds = %6, %6, %6
%16 = tail call i32 @nonblock(i32 0)
%17 = tail call i32 @putchar(i32 10)
ret i8 %8
}