#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 ask_string(char const *, int, char *, int);
int /*<<< orphan*/ beep();
scalar_t__ errno;
int strlen(char *);
int strtol(char *, char **, int /*<<< orphan*/);
bool ask_integer(const char *prompt, int color, int *output) {
  char temp[21];
  temp[0] = 0;
  while (true) {
    errno = 0;
    int str = ask_string(prompt, color, temp, 20);
    if (!str)
      return false;
    char *endptr = temp;
    int num = strtol(temp, &endptr, 0);
    if (strlen(temp) == 0)
      return false;
    if ((errno != 0) || ((endptr - temp) < strlen(temp))) {
      beep();
    } else {
      *output = num;
      return true;
    }
  }
}


x86 assembly (clang, -O0):
<ask_integer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movb   $0x0,-0x40(%rbp)
mov    0x2e82(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x40(%rbp),%rdx
mov    $0x14,%ecx
call   1050 <ask_string@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    1193 <ask_integer+0x53>
movl   $0x0,-0x4(%rbp)
jmp    122c <ask_integer+0xec>
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %eax,-0x54(%rbp)
lea    -0x40(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jne    11cb <ask_integer+0x8b>
movl   $0x0,-0x4(%rbp)
jmp    122c <ask_integer+0xec>
mov    0x2e0e(%rip),%rax
cmpq   $0x0,(%rax)
jne    1206 <ask_integer+0xc6>
mov    -0x50(%rbp),%rax
lea    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x60(%rbp)
lea    -0x40(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
jge    1212 <ask_integer+0xd2>
mov    $0x0,%al
call   1040 <beep@plt>
jmp    1227 <ask_integer+0xe7>
mov    -0x54(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    122c <ask_integer+0xec>
jmp    1157 <ask_integer+0x17>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ask_integer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r13
movb   $0x0,0x10(%rsp)
mov    0x2e7d(%rip),%r12
movq   $0x0,(%r12)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdx
mov    $0x14,%ecx
call   1050 <ask_string@plt>
test   %eax,%eax
je     1206 <ask_integer+0xc6>
mov    %rbx,0x8(%rsp)
jmp    11b8 <ask_integer+0x78>
nopl   0x0(%rax)
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <beep@plt>
movq   $0x0,(%r12)
mov    %r13,%rdi
mov    %r15d,%esi
mov    %rbp,%rdx
mov    $0x14,%ecx
call   1050 <ask_string@plt>
test   %eax,%eax
je     1208 <ask_integer+0xc8>
mov    %rbp,(%rsp)
xor    %ebx,%ebx
mov    %rbp,%rdi
mov    %rsp,%rsi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     1208 <ask_integer+0xc8>
cmpq   $0x0,(%r12)
jne    1190 <ask_integer+0x50>
mov    (%rsp),%rbx
sub    %rbp,%rbx
mov    %rbp,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1190 <ask_integer+0x50>
mov    0x8(%rsp),%rax
mov    %r14d,(%rax)
mov    $0x1,%ebx
jmp    1208 <ask_integer+0xc8>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
define i32 @ask_integer(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [21 x i8]
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%12 = getelementptr inbounds [21 x i8], [21 x i8]* %8, i64 0, i64 0
store i8 0, i8* %12
br label %13
13: ; preds = %3, %47
store i64 0, i64* @errno
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%16 = getelementptr inbounds [21 x i8], [21 x i8]* %8, i64 0, i64 0
%17 = call i32 @ask_string(i8* %14, i32 %15, i8* %16, i32 20)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %13
store i32 0, i32* %4
br label %48
21: ; preds = %13
%22 = getelementptr inbounds [21 x i8], [21 x i8]* %8, i64 0, i64 0
store i8* %22, i8** %10
%23 = getelementptr inbounds [21 x i8], [21 x i8]* %8, i64 0, i64 0
%24 = call i32 @strtol(i8* %23, i8** %10, i32 0)
store i32 %24, i32* %11
%25 = getelementptr inbounds [21 x i8], [21 x i8]* %8, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 0, i32* %4
br label %48
29: ; preds = %21
%30 = load i64, i64* @errno
%31 = icmp ne i64 %30, 0
br i1 %31, label %42, label %32
32: ; preds = %29
%33 = load i8*, i8** %10
%34 = getelementptr inbounds [21 x i8], [21 x i8]* %8, i64 0, i64 0
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = getelementptr inbounds [21 x i8], [21 x i8]* %8, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
%40 = sext i32 %39 to i64
%41 = icmp slt i64 %37, %40
br i1 %41, label %42, label %44
42: ; preds = %32, %29
%43 = call i32 (...) @beep()
br label %47
44: ; preds = %32
%45 = load i32, i32* %11
%46 = load i32*, i32** %7
store i32 %45, i32* %46
store i32 1, i32* %4
br label %48
47: ; preds = %42
br label %13
48: ; preds = %44, %28, %20
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@errno = global i64 0
define i32 @ask_integer(i8* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca [21 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [21 x i8], [21 x i8]* %4, i64 0, i64 0
store i8 0, i8* %6
store i64 0, i64* @errno
%7 = call i32 @ask_string(i8* %0, i32 %1, i8* %6, i32 20)
%8 = icmp eq i32 %7, 0
br i1 %8, label %33, label %9
9: ; preds = %3
%10 = bitcast i8** %5 to i8*
%11 = ptrtoint [21 x i8]* %4 to i64
br label %12
12: ; preds = %9, %29
store i8* %6, i8** %5
%13 = call i32 @strtol(i8* %6, i8** %5, i32 0)
%14 = call i32 @strlen(i8* %6)
%15 = icmp eq i32 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = load i8*, i8** %5
%21 = ptrtoint i8* %20 to i64
%22 = sub i64 %21, %11
%23 = call i32 @strlen(i8* %6)
%24 = sext i32 %23 to i64
%25 = icmp slt i64 %22, %24
br i1 %25, label %29, label %26
26: ; preds = %19
store i32 %13, i32* %2
br label %27
27: ; preds = %12, %26
%28 = phi i32 [ 1, %26 ], [ 0, %12 ]
br label %33
29: ; preds = %16, %19
%30 = call i32 (...) @beep()
store i64 0, i64* @errno
%31 = call i32 @ask_string(i8* %0, i32 %1, i8* %6, i32 20)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %12
33: ; preds = %29, %3, %27
%34 = phi i32 [ %28, %27 ], [ 0, %3 ], [ 0, %29 ]
ret i32 %34
}