#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*/ exit(int /*<<< orphan*/);
scalar_t__ feof(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int scanf(char *, ...);
int /*<<< orphan*/ stdin;
unsigned short readunsignedshort(const char *prompt) {
  for (;;) {
    printf("%s", prompt);
    unsigned short enteredval;
    int transferred = scanf("%ho", &enteredval);
    if (transferred != 1) {
      if (feof(stdin)) {
        printf("Input terminated, program will exit\n");
        exit(0);
      }
      printf("Invalid Input");
      scanf("%*[^\n]%*c");
      continue;
    }
    return enteredval;
  }
}


x86 assembly (clang, -O0):
<readunsignedshort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe9e(%rip),%rdi
lea    -0xa(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
je     11cb <readunsignedshort+0x8b>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
call   1040 <feof@plt>
cmp    $0x0,%rax
je     11aa <readunsignedshort+0x6a>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
jmp    114c <readunsignedshort+0xc>
movzwl -0xa(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readunsignedshort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe9b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe8d(%rip),%rdi
lea    0x6(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmp    $0x1,%eax
je     11f6 <readunsignedshort+0xa6>
lea    0xe79(%rip),%r14
lea    0xe80(%rip),%r15
lea    0xe64(%rip),%r12
lea    0xe60(%rip),%r13
lea    0x6(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1050 <feof@plt>
test   %rax,%rax
jne    120a <readunsignedshort+0xba>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <scanf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    11b0 <readunsignedshort+0x60>
movzwl 0x6(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0e(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [4 x i8] c"%ho\00"
@stdin = global i32 0
@.str.2 = [37 x i8] c"Input terminated, program will exit\0A\00"
@.str.3 = [14 x i8] c"Invalid Input\00"
@.str.4 = [10 x i8] c"%*[^\0A]%*c\00"
define zeroext i16 @readunsignedshort(i8* %0) {
%2 = alloca i8*
%3 = alloca i16
%4 = alloca i32
store i8* %0, i8** %2
br label %5
5: ; preds = %18, %1
%6 = load i8*, i8** %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i16* %3)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 1
br i1 %10, label %11, label %21
11: ; preds = %5
%12 = load i32, i32* @stdin
%13 = call i64 @feof(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %11
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %5
21: ; preds = %5
%22 = load i16, i16* %3
ret i16 %22
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [4 x i8] c"%ho\00"
@stdin = global i32 0
@.str.3 = [14 x i8] c"Invalid Input\00"
@.str.4 = [10 x i8] c"%*[^\0A]%*c\00"
@str = [36 x i8] c"Input terminated, program will exit\00"
define zeroext i16 @readunsignedshort(i8* %0) {
%2 = alloca i16
%3 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = bitcast i16* %2 to i8*
%5 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i16* %2)
%6 = icmp eq i32 %5, 1
br i1 %6, label %20, label %7
7: ; preds = %1, %14
%8 = load i32, i32* @stdin
%9 = call i64 @feof(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %7
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%18 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i16* %2)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %7
20: ; preds = %14, %1
%21 = load i16, i16* %2
ret i16 %21
}