#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*/ str;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ getstr(char *, int);
int /*<<< orphan*/ printf(char *, char *, int);
int which(char *prompt, int max) {
  char str[41];
  int i;
  while (1) {
    printf("%s which (1-%d): ", prompt, max);
    str[0] = 0;
    getstr(str, sizeof(str) - 1);
    i = atoi(str);
    if (i > 0 && i <= max)
      return (i - 1);
  }
}


x86 assembly (clang, -O0):
<which>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xeb3(%rip),%rdi
call   1030 <printf@plt>
movb   $0x0,-0x40(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x28,%esi
call   1040 <getstr@plt>
lea    -0x40(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jle    1192 <which+0x62>
mov    -0x44(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     1192 <which+0x62>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
add    $0x50,%rsp
pop    %rbp
ret
jmp    113f <which+0xf>

x86 assembly (clang, -O2):
<which>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xeb9(%rip),%r15
mov    %rsp,%r12
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <printf@plt>
movb   $0x0,(%rsp)
mov    %r12,%rdi
mov    $0x28,%esi
call   1040 <getstr@plt>
mov    %r12,%rdi
call   1050 <atoi@plt>
test   %eax,%eax
jle    1150 <which+0x20>
cmp    %ebx,%eax
jg     1150 <which+0x20>
add    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"%s which (1-%d): \00"
define i32 @which(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [41 x i8]
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
br label %7
7: ; preds = %2, %25
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %9)
%11 = getelementptr inbounds [41 x i8], [41 x i8]* %5, i64 0, i64 0
store i8 0, i8* %11
%12 = getelementptr inbounds [41 x i8], [41 x i8]* %5, i64 0, i64 0
%13 = call i32 @getstr(i8* %12, i32 40)
%14 = getelementptr inbounds [41 x i8], [41 x i8]* %5, i64 0, i64 0
%15 = call i32 @atoi(i8* %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %7
%19 = load i32, i32* %6
%20 = load i32, i32* %4
%21 = icmp sle i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %6
%24 = sub nsw i32 %23, 1
ret i32 %24
25: ; preds = %18, %7
br label %7
}

LLVM IR (-O2):
@.str = [18 x i8] c"%s which (1-%d): \00"
define i32 @which(i8* %0, i32 %1) {
%3 = alloca [41 x i8]
%4 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %5, %2
%6 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
store i8 0, i8* %4
%7 = call i32 @getstr(i8* %4, i32 40)
%8 = call i32 @atoi(i8* %4)
%9 = icmp slt i32 %8, 1
%10 = icmp sgt i32 %8, %1
%11 = or i1 %9, %10
br i1 %11, label %5, label %12
12: ; preds = %5
%13 = add nsw i32 %8, -1
ret i32 %13
}