#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 */
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
char *clean_string(char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int ask_int(char *prompt, int min, int max, int the_default, int *out) {
  int n = max + 1;
  char buffer[1024];
  char *clean;
  int illegal;
  while (1) {
    printf("%s", prompt);
    if (!fgets(buffer, sizeof(buffer), stdin))
      return -1;
    clean = clean_string(buffer);
    if (strlen(clean) < 1) {
      *out = the_default;
      return 0;
    }
    illegal = (1 != sscanf(buffer, "%d", &n));
    if (illegal || n < min || n > max) {
      printf("Your choice (%s) was not an integer between %d and %d.\n\n",
             clean, min, max);
    } else {
      *out = n;
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<ask_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x430(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    11b6 <ask_int+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <ask_int+0x130>
lea    -0x430(%rbp),%rdi
call   1050 <clean_string@plt>
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%eax
jge    11f3 <ask_int+0xa3>
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1280 <ask_int+0x130>
lea    -0x430(%rbp),%rdi
lea    0xe02(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <sscanf@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x43c(%rbp)
cmpl   $0x0,-0x43c(%rbp)
jne    1246 <ask_int+0xf6>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1246 <ask_int+0xf6>
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    1266 <ask_int+0x116>
mov    -0x438(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127b <ask_int+0x12b>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1280 <ask_int+0x130>
jmp    1175 <ask_int+0x25>
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ask_int>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %r8,%r14
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r12
lea    0x1(%rbp),%eax
mov    %eax,(%rsp)
lea    0xe85(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    $0xffffffff,%ecx
test   %eax,%eax
je     125b <ask_int+0x10b>
mov    %r15d,0x4(%rsp)
mov    %r14,0x8(%rsp)
lea    0x10(%rsp),%r14
lea    0xe40(%rip),%r15
jmp    120c <ask_int+0xbc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xe2f(%rip),%rdi
mov    %r13,%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
test   %eax,%eax
je     1247 <ask_int+0xf7>
mov    %r14,%rdi
call   1050 <clean_string@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    124e <ask_int+0xfe>
mov    %r14,%rdi
lea    0xdd6(%rip),%rsi
mov    %rsp,%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    11d0 <ask_int+0x80>
mov    (%rsp),%eax
cmp    %ebx,%eax
jl     11d0 <ask_int+0x80>
cmp    %ebp,%eax
jg     11d0 <ask_int+0x80>
jmp    1252 <ask_int+0x102>
mov    $0xffffffff,%ecx
jmp    125b <ask_int+0x10b>
mov    0x4(%rsp),%eax
mov    0x8(%rsp),%rcx
mov    %eax,(%rcx)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [57 x i8] c"Your choice (%s) was not an integer between %d and %d.\0A\0A\00"
define i32 @ask_int(i8* %0, i32 %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca [1024 x i8]
%14 = alloca i8*
%15 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%16 = load i32, i32* %9
%17 = add nsw i32 %16, 1
store i32 %17, i32* %12
br label %18
18: ; preds = %5, %58
%19 = load i8*, i8** %7
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%22 = load i32, i32* @stdin
%23 = call i32 @fgets(i8* %21, i32 1024, i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %18
store i32 -1, i32* %6
br label %59
26: ; preds = %18
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%28 = call i8* @clean_string(i8* %27)
store i8* %28, i8** %14
%29 = load i8*, i8** %14
%30 = call i32 @strlen(i8* %29)
%31 = icmp slt i32 %30, 1
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32, i32* %10
%34 = load i32*, i32** %11
store i32 %33, i32* %34
store i32 0, i32* %6
br label %59
35: ; preds = %26
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%37 = call i32 @sscanf(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %12)
%38 = icmp ne i32 1, %37
%39 = zext i1 %38 to i32
store i32 %39, i32* %15
%40 = load i32, i32* %15
%41 = icmp ne i32 %40, 0
br i1 %41, label %50, label %42
42: ; preds = %35
%43 = load i32, i32* %12
%44 = load i32, i32* %8
%45 = icmp slt i32 %43, %44
br i1 %45, label %50, label %46
46: ; preds = %42
%47 = load i32, i32* %12
%48 = load i32, i32* %9
%49 = icmp sgt i32 %47, %48
br i1 %49, label %50, label %55
50: ; preds = %46, %42, %35
%51 = load i8*, i8** %14
%52 = load i32, i32* %8
%53 = load i32, i32* %9
%54 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i8* %51, i32 %52, i32 %53)
br label %58
55: ; preds = %46
%56 = load i32, i32* %12
%57 = load i32*, i32** %11
store i32 %56, i32* %57
store i32 0, i32* %6
br label %59
58: ; preds = %50
br label %18
59: ; preds = %55, %32, %25
%60 = load i32, i32* %6
ret i32 %60
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [57 x i8] c"Your choice (%s) was not an integer between %d and %d.\0A\0A\00"
define i32 @ask_int(i8* %0, i32 %1, i32 %2, i32 %3, i32* nocapture writeonly %4) {
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = bitcast i32* %6 to i8*
%9 = add nsw i32 %2, 1
store i32 %9, i32* %6
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%11 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = load i32, i32* @stdin
%13 = call i32 @fgets(i8* %10, i32 1024, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %35, label %15
15: ; preds = %5, %27
%16 = call i8* @clean_string(i8* %10)
%17 = call i32 @strlen(i8* %16)
%18 = icmp slt i32 %17, 1
br i1 %18, label %33, label %19
19: ; preds = %15
%20 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, %1
%25 = icmp sgt i32 %23, %2
%26 = or i1 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %22, %19
%28 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i8* %16, i32 %1, i32 %2)
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%30 = load i32, i32* @stdin
%31 = call i32 @fgets(i8* %10, i32 1024, i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %35, label %15
33: ; preds = %22, %15
%34 = phi i32 [ %3, %15 ], [ %23, %22 ]
store i32 %34, i32* %4
br label %35
35: ; preds = %27, %33, %5
%36 = phi i32 [ -1, %5 ], [ 0, %33 ], [ -1, %27 ]
ret i32 %36
}