#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 getchar();
int is_val_ok(int, int const, int const);
int /*<<< orphan*/ myflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int read_char(const char *prompt, const int lo_val, const int hi_val,
              int (*char_ok)(int ch)) {
  int retval, input, val_ok;
  val_ok = 0;
  do {
    printf("\n%s", prompt);
    if (lo_val != hi_val)
      printf("<%c-%c>+<Enter>: ", lo_val, hi_val);
    input = getchar();
    retval = char_ok(input);
    if (retval != 0) {
      if (lo_val != hi_val) {
        val_ok = is_val_ok(input, lo_val, hi_val);
        if (!val_ok)
          printf("Invalid selection - use <%c> to <%c>...!", lo_val, hi_val);
      } else
        val_ok = 1;
    } else
      printf("Invalid character input!");
    myflush(stdin);
  } while (retval == 0 || !val_ok);
  return input;
}


x86 assembly (clang, -O0):
<read_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     118f <read_char+0x4f>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%edi
call   *%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11fb <read_char+0xbb>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     11ef <read_char+0xaf>
mov    -0x20(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <is_val_ok@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11ea <read_char+0xaa>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f6 <read_char+0xb6>
movl   $0x1,-0x24(%rbp)
jmp    1209 <read_char+0xc9>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1040 <myflush@plt>
mov    $0x1,%al
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x25(%rbp)
je     1232 <read_char+0xf2>
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    115d <read_char+0x1d>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,(%rsp)
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r15
xor    %r13d,%r13d
nopl   0x0(%rax)
lea    0xe99(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    %ebp,%ebx
je     1187 <read_char+0x47>
lea    0xe88(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%r12d
mov    %eax,%edi
call   *(%rsp)
mov    %eax,%r14d
test   %eax,%eax
je     11e0 <read_char+0xa0>
mov    $0x1,%r13d
cmp    %ebp,%ebx
je     11ee <read_char+0xae>
mov    %r12d,%edi
mov    %ebx,%esi
mov    %ebp,%edx
call   1060 <is_val_ok@plt>
mov    %eax,%r13d
test   %eax,%eax
jne    11ee <read_char+0xae>
xor    %r13d,%r13d
lea    0xe52(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11ee <read_char+0xae>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0xe58(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1040 <myflush@plt>
test   %r14d,%r14d
je     1160 <read_char+0x20>
test   %r13d,%r13d
je     1160 <read_char+0x20>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"\0A%s\00"
@.str.1 = [18 x i8] c"<%c-%c>+<Enter>: \00"
@.str.2 = [41 x i8] c"Invalid selection - use <%c> to <%c>...!\00"
@.str.3 = [25 x i8] c"Invalid character input!\00"
@stdin = global i32 0
define i32 @read_char(i8* %0, i32 %1, i32 %2, i32 (i32)* nocapture %3) {
%5 = icmp eq i32 %1, %2
br label %6
6: ; preds = %23, %4
%7 = phi i32 [ 0, %4 ], [ %24, %23 ]
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
br i1 %5, label %11, label %9
9: ; preds = %6
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %2)
br label %11
11: ; preds = %9, %6
%12 = tail call i32 (...) @getchar()
%13 = tail call i32 %3(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %11
br i1 %5, label %23, label %16
16: ; preds = %15
%17 = tail call i32 @is_val_ok(i32 %12, i32 %1, i32 %2)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %2)
br label %23
21: ; preds = %11
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %15, %19, %16, %21
%24 = phi i32 [ %17, %16 ], [ 0, %19 ], [ %7, %21 ], [ 1, %15 ]
%25 = load i32, i32* @stdin
%26 = tail call i32 @myflush(i32 %25)
%27 = icmp eq i32 %24, 0
%28 = select i1 %14, i1 true, i1 %27
br i1 %28, label %6, label %29
29: ; preds = %23
ret i32 %12
}