#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 */
scalar_t__ argvalid(int, long);
long read_number();
int /*<<< orphan*/ writeln(char *);
int /*<<< orphan*/ writestr(char const *);
long readval(const char *prompt, int type) {
  long val;
  for (;;) {
    writestr(prompt);
    writestr(" ");
    val = read_number();
    if (argvalid(type, val))
      return val;
    writeln("Invalid value.");
  }
}


x86 assembly (clang, -O0):
<readval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <writestr@plt>
lea    0xea1(%rip),%rdi
call   1050 <writestr@plt>
mov    $0x0,%al
call   1060 <read_number@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <argvalid@plt>
cmp    $0x0,%rax
je     118f <readval+0x4f>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
lea    0xe6c(%rip),%rdi
call   1030 <writeln@plt>
jmp    114f <readval+0xf>

x86 assembly (clang, -O2):
<readval>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r12d
mov    %rdi,%rbx
call   1050 <writestr@plt>
lea    0xea6(%rip),%rdi
call   1050 <writestr@plt>
xor    %eax,%eax
call   1060 <read_number@plt>
mov    %rax,%rbp
mov    %r12d,%edi
mov    %rax,%rsi
call   1040 <argvalid@plt>
test   %rax,%rax
jne    11c2 <readval+0x82>
lea    0xe82(%rip),%r14
lea    0xe79(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <writeln@plt>
mov    %rbx,%rdi
call   1050 <writestr@plt>
mov    %r15,%rdi
call   1050 <writestr@plt>
xor    %eax,%eax
call   1060 <read_number@plt>
mov    %rax,%rbp
mov    %r12d,%edi
mov    %rax,%rsi
call   1040 <argvalid@plt>
test   %rax,%rax
je     1190 <readval+0x50>
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [15 x i8] c"Invalid value.\00"
define i64 @readval(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
br label %6
6: ; preds = %17, %2
%7 = load i8*, i8** %3
%8 = call i32 @writestr(i8* %7)
%9 = call i32 @writestr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = call i64 (...) @read_number()
store i64 %10, i64* %5
%11 = load i32, i32* %4
%12 = load i64, i64* %5
%13 = call i64 @argvalid(i32 %11, i64 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %6
%16 = load i64, i64* %5
ret i64 %16
17: ; preds = %6
%18 = call i32 @writeln(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %6
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.1 = [15 x i8] c"Invalid value.\00"
define i64 @readval(i8* %0, i32 %1) {
%3 = tail call i32 @writestr(i8* %0)
%4 = tail call i32 @writestr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = tail call i64 (...) @read_number()
%6 = tail call i64 @argvalid(i32 %1, i64 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %10, %2
%9 = phi i64 [ %5, %2 ], [ %14, %10 ]
ret i64 %9
10: ; preds = %2, %10
%11 = tail call i32 @writeln(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @writestr(i8* %0)
%13 = tail call i32 @writestr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = tail call i64 (...) @read_number()
%15 = tail call i64 @argvalid(i32 %1, i64 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %10, label %8
}