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

/* Variables and functions */
int atoi(char *);
scalar_t__ get_user_input(char *, int, char const **);
int /*<<< orphan*/ printf(char *, ...);
unsigned char display_user_choice_prompt(const char *prompt_msg,
                                         unsigned char last_option) {
  const char *err_msg = NULL;
  char ans[4];
  int num = 1;
  do {
    if (err_msg != NULL) {
      printf("Error: %s\n", err_msg);
      err_msg = NULL;
    }
    if (num < 1 || num > last_option) {
      printf("Error: Invalid choice (%d)\n", num);
    }
    printf("%s", prompt_msg);
  } while (get_user_input(ans, sizeof(ans), &err_msg) == 0 || err_msg != NULL ||
           (num = atoi(ans)) < 1 || num > last_option);
  return (unsigned char)num;
}


x86 assembly (clang, -O0):
<display_user_choice_prompt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1176 <display_user_choice_prompt+0x46>
mov    -0x18(%rbp),%rsi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
cmpl   $0x1,-0x20(%rbp)
jl     118f <display_user_choice_prompt+0x5f>
mov    -0x20(%rbp),%eax
movzbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
jle    11a0 <display_user_choice_prompt+0x70>
mov    -0x20(%rbp),%esi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rsi
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x1c(%rbp),%rdi
mov    $0x4,%esi
lea    -0x18(%rbp),%rdx
call   1030 <get_user_input@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
je     1211 <display_user_choice_prompt+0xe1>
mov    $0x1,%al
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jne    1211 <display_user_choice_prompt+0xe1>
lea    -0x1c(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
mov    $0x1,%al
cmp    $0x1,%ecx
mov    %al,-0x21(%rbp)
jl     1211 <display_user_choice_prompt+0xe1>
mov    -0x20(%rbp),%eax
movzbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1151 <display_user_choice_prompt+0x21>
mov    -0x20(%rbp),%eax
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<display_user_choice_prompt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
mov    $0x1,%ebp
lea    0xed1(%rip),%r12
lea    0x14(%rsp),%r13
lea    0x8(%rsp),%r14
mov    %esi,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %ebp,%ebp
setle  %al
cmp    %esi,%ebp
setg   %r15b
or     %al,%r15b
xor    %esi,%esi
test   %rsi,%rsi
je     119c <display_user_choice_prompt+0x6c>
lea    0xe74(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movq   $0x0,0x8(%rsp)
test   %r15b,%r15b
je     11b1 <display_user_choice_prompt+0x81>
lea    0xe63(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13,%rdi
mov    $0x4,%esi
mov    %r14,%rdx
call   1030 <get_user_input@plt>
mov    0x8(%rsp),%rsi
test   %rax,%rax
je     1180 <display_user_choice_prompt+0x50>
test   %rsi,%rsi
jne    1180 <display_user_choice_prompt+0x50>
mov    %r13,%rdi
call   1050 <atoi@plt>
mov    %eax,%ebp
test   %eax,%eax
mov    0x10(%rsp),%esi
jle    1170 <display_user_choice_prompt+0x40>
cmp    %esi,%ebp
jg     1170 <display_user_choice_prompt+0x40>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"Error: %s\0A\00"
@.str.1 = [28 x i8] c"Error: Invalid choice (%d)\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define zeroext i8 @display_user_choice_prompt(i8* %0, i8 zeroext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i8*
%6 = alloca [4 x i8]
%7 = alloca i32
store i8* %0, i8** %3
store i8 %1, i8* %4
store i8* null, i8** %5
store i32 1, i32* %7
br label %8
8: ; preds = %44, %2
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i8*, i8** %5
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %12)
store i8* null, i8** %5
br label %14
14: ; preds = %11, %8
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 1
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = load i32, i32* %7
%19 = load i8, i8* %4
%20 = zext i8 %19 to i32
%21 = icmp sgt i32 %18, %20
br i1 %21, label %22, label %25
22: ; preds = %17, %14
%23 = load i32, i32* %7
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %17
%26 = load i8*, i8** %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%30 = call i64 @get_user_input(i8* %29, i32 4, i8** %5)
%31 = icmp eq i64 %30, 0
br i1 %31, label %44, label %32
32: ; preds = %28
%33 = load i8*, i8** %5
%34 = icmp ne i8* %33, null
br i1 %34, label %44, label %35
35: ; preds = %32
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %7
%38 = icmp slt i32 %37, 1
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = load i32, i32* %7
%41 = load i8, i8* %4
%42 = zext i8 %41 to i32
%43 = icmp sgt i32 %40, %42
br label %44
44: ; preds = %39, %35, %32, %28
%45 = phi i1 [ true, %35 ], [ true, %32 ], [ true, %28 ], [ %43, %39 ]
br i1 %45, label %8, label %46
46: ; preds = %44
%47 = load i32, i32* %7
%48 = trunc i32 %47 to i8
ret i8 %48
}

LLVM IR (-O2):
@.str = [11 x i8] c"Error: %s\0A\00"
@.str.1 = [28 x i8] c"Error: Invalid choice (%d)\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define zeroext i8 @display_user_choice_prompt(i8* %0, i8 zeroext %1) {
%3 = alloca i8*
%4 = alloca [4 x i8]
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%7 = zext i8 %1 to i32
br label %8
8: ; preds = %28, %2
%9 = phi i32 [ 1, %2 ], [ %29, %28 ]
%10 = icmp slt i32 %9, 1
%11 = icmp sgt i32 %9, %7
%12 = or i1 %10, %11
br label %13
13: ; preds = %8, %21
%14 = phi i8* [ %25, %21 ], [ null, %8 ]
%15 = icmp eq i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %14)
store i8* null, i8** %3
br label %18
18: ; preds = %16, %13
br i1 %12, label %19, label %21
19: ; preds = %18
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %21
21: ; preds = %18, %19
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%23 = call i64 @get_user_input(i8* %6, i32 4, i8** %3)
%24 = icmp eq i64 %23, 0
%25 = load i8*, i8** %3
%26 = icmp ne i8* %25, null
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %13, label %28
28: ; preds = %21
%29 = call i32 @atoi(i8* %6)
%30 = icmp slt i32 %29, 1
%31 = icmp sgt i32 %29, %7
%32 = or i1 %30, %31
br i1 %32, label %8, label %33
33: ; preds = %28
%34 = trunc i32 %29 to i8
ret i8 %34
}