#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 *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
int ask_menu(char *prompt, int n_choices, char *choice[], int the_default,
             int *out) {
  char buffer[1024];
  char *clean;
  int i;
  *out = -1;
  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;
    }
    for (i = 0; i < n_choices; i++) {
      if (!strcmp(choice[i], clean)) {
        *out = i;
        return 0;
      }
    }
    printf("Your choice (%s) did not match any of the choices:\n", clean);
    for (i = 0; i < n_choices; i++) {
      if (i > 0)
        printf(", ");
      printf("%s", choice[i]);
    }
    printf("\n");
  }
}


x86 assembly (clang, -O0):
<ask_menu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x10(%rbp),%rsi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x430(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    11b8 <ask_menu+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <ask_menu+0x1a0>
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    11f5 <ask_menu+0xa5>
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12f0 <ask_menu+0x1a0>
movl   $0x0,-0x43c(%rbp)
mov    -0x43c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1263 <ask_menu+0x113>
mov    -0x20(%rbp),%rax
movslq -0x43c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x438(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    124a <ask_menu+0xfa>
mov    -0x43c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12f0 <ask_menu+0x1a0>
jmp    124f <ask_menu+0xff>
mov    -0x43c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x43c(%rbp)
jmp    11ff <ask_menu+0xaf>
mov    -0x438(%rbp),%rsi
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x43c(%rbp)
mov    -0x43c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12dd <ask_menu+0x18d>
cmpl   $0x0,-0x43c(%rbp)
jle    12ac <ask_menu+0x15c>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movslq -0x43c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x43c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x43c(%rbp)
jmp    1282 <ask_menu+0x132>
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1177 <ask_menu+0x27>
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ask_menu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %r8,%rbx
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %rdi,%rsi
movl   $0xffffffff,(%r8)
lea    0xe72(%rip),%rdi
mov    %rsi,0x18(%rsp)
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    $0xffffffff,%ecx
test   %eax,%eax
je     130a <ask_menu+0x1aa>
mov    %r14d,0xc(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r15d,%r12d
lea    0x20(%rsp),%rbx
lea    0xe5f(%rip),%r13
mov    %r13,%r14
mov    %rbx,%rdi
call   1060 <clean_string@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    131e <ask_menu+0x1be>
test   %r15d,%r15d
jle    12a0 <ask_menu+0x140>
mov    %r15d,%ebx
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%rbp,%r15,8),%rdi
mov    %r13,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     12fe <ask_menu+0x19e>
add    $0x1,%r15
cmp    %r15,%r12
jne    1210 <ask_menu+0xb0>
lea    0xdce(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%r15d
test   %ebx,%ebx
mov    %r14,%r13
lea    0xdb2(%rip),%r14
jle    12bb <ask_menu+0x15b>
mov    0x0(%rbp),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x1,%r15d
je     12bb <ask_menu+0x15b>
mov    $0x1,%ebx
nopl   0x0(%rax)
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%rbp,%rbx,8),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1270 <ask_menu+0x110>
jmp    12bb <ask_menu+0x15b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xd5c(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%r13
lea    0xd45(%rip),%r14
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
mov    0x18(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
test   %eax,%eax
jne    11d8 <ask_menu+0x78>
mov    $0xffffffff,%ecx
jmp    130a <ask_menu+0x1aa>
mov    %r15d,%ecx
mov    0x10(%rsp),%rax
mov    %ecx,(%rax)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0xc(%rsp),%ecx
jmp    1301 <ask_menu+0x1a1>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
@.str.1 = [52 x i8] c"Your choice (%s) did not match any of the choices:\0A\00"
@.str.2 = [3 x i8] c", \00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @ask_menu(i8* %0, i32 %1, i8** %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i32*
%12 = alloca [1024 x i8]
%13 = alloca i8*
%14 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8** %2, i8*** %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%15 = load i32*, i32** %11
store i32 -1, i32* %15
br label %16
16: ; preds = %5, %76
%17 = load i8*, i8** %7
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%20 = load i32, i32* @stdin
%21 = call i32 @fgets(i8* %19, i32 1024, i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %16
store i32 -1, i32* %6
br label %78
24: ; preds = %16
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%26 = call i8* @clean_string(i8* %25)
store i8* %26, i8** %13
%27 = load i8*, i8** %13
%28 = call i32 @strlen(i8* %27)
%29 = icmp slt i32 %28, 1
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* %10
%32 = load i32*, i32** %11
store i32 %31, i32* %32
store i32 0, i32* %6
br label %78
33: ; preds = %24
store i32 0, i32* %14
br label %34
34: ; preds = %51, %33
%35 = load i32, i32* %14
%36 = load i32, i32* %8
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %54
38: ; preds = %34
%39 = load i8**, i8*** %9
%40 = load i32, i32* %14
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %39, i64 %41
%43 = load i8*, i8** %42
%44 = load i8*, i8** %13
%45 = call i32 @strcmp(i8* %43, i8* %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %38
%48 = load i32, i32* %14
%49 = load i32*, i32** %11
store i32 %48, i32* %49
store i32 0, i32* %6
br label %78
50: ; preds = %38
br label %51
51: ; preds = %50
%52 = load i32, i32* %14
%53 = add nsw i32 %52, 1
store i32 %53, i32* %14
br label %34
54: ; preds = %34
%55 = load i8*, i8** %13
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* %55)
store i32 0, i32* %14
br label %57
57: ; preds = %73, %54
%58 = load i32, i32* %14
%59 = load i32, i32* %8
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %76
61: ; preds = %57
%62 = load i32, i32* %14
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %66
66: ; preds = %64, %61
%67 = load i8**, i8*** %9
%68 = load i32, i32* %14
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8*, i8** %67, i64 %69
%71 = load i8*, i8** %70
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %71)
br label %73
73: ; preds = %66
%74 = load i32, i32* %14
%75 = add nsw i32 %74, 1
store i32 %75, i32* %14
br label %57
76: ; preds = %57
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %16
78: ; preds = %47, %30, %23
%79 = load i32, i32* %6
ret i32 %79
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
@.str.1 = [52 x i8] c"Your choice (%s) did not match any of the choices:\0A\00"
@.str.2 = [3 x i8] c", \00"
define i32 @ask_menu(i8* %0, i32 %1, i8** nocapture %2, i32 %3, i32* nocapture writeonly %4) {
%6 = alloca [1024 x i8]
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i32 -1, i32* %4
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %7, i32 1024, i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %56, label %12
12: ; preds = %5
%13 = icmp sgt i32 %1, 0
%14 = zext i32 %1 to i64
%15 = zext i32 %1 to i64
%16 = icmp eq i32 %1, 1
br label %17
17: ; preds = %12, %48
%18 = call i8* @clean_string(i8* %7)
%19 = call i32 @strlen(i8* %18)
%20 = icmp slt i32 %19, 1
br i1 %20, label %54, label %21
21: ; preds = %17
br i1 %13, label %24, label %22
22: ; preds = %21
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %48
24: ; preds = %21, %32
%25 = phi i64 [ %33, %32 ], [ 0, %21 ]
%26 = getelementptr inbounds i8*, i8** %2, i64 %25
%27 = load i8*, i8** %26
%28 = call i32 @strcmp(i8* dereferenceable(1) %27, i8* dereferenceable(1) %18)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = trunc i64 %25 to i32
br label %54
32: ; preds = %24
%33 = add nuw nsw i64 %25, 1
%34 = icmp eq i64 %33, %14
br i1 %34, label %35, label %24
35: ; preds = %32
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br i1 %13, label %37, label %48
37: ; preds = %35
%38 = load i8*, i8** %2
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %38)
br i1 %16, label %48, label %40
40: ; preds = %37, %40
%41 = phi i64 [ %46, %40 ], [ 1, %37 ]
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%43 = getelementptr inbounds i8*, i8** %2, i64 %41
%44 = load i8*, i8** %43
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %44)
%46 = add nuw nsw i64 %41, 1
%47 = icmp eq i64 %46, %15
br i1 %47, label %48, label %40
48: ; preds = %40, %37, %22, %35
%49 = call i32 @putchar(i32 10)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%51 = load i32, i32* @stdin
%52 = call i32 @fgets(i8* %7, i32 1024, i32 %51)
%53 = icmp eq i32 %52, 0
br i1 %53, label %56, label %17
54: ; preds = %17, %30
%55 = phi i32 [ %31, %30 ], [ %3, %17 ]
store i32 %55, i32* %4
br label %56
56: ; preds = %48, %54, %5
%57 = phi i32 [ -1, %5 ], [ 0, %54 ], [ -1, %48 ]
ret i32 %57
}