#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 getchar();
int /*<<< orphan*/ printf(char *, ...);
int getchoice(char *greet, char *choices[]) {
  int chosen = 0;
  int selected;
  char **option;
  do {
    printf("Choice: %s\n", greet);
    option = choices;
    while (*option) {
      printf("%s\n", *option);
      option++;
    }
    selected = getchar();
    option = choices;
    while (*option) {
      if (selected == *option[0]) {
        chosen = 1;
        break;
      }
      option++;
    }
    if (!chosen) {
      printf("Incorrect choice, select again\n");
    }
  } while (!chosen);
  return selected;
}


x86 assembly (clang, -O0):
<getchoice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xebe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     1185 <getchoice+0x65>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    1151 <getchoice+0x31>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11d7 <getchoice+0xb7>
mov    -0x18(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11c6 <getchoice+0xa6>
movl   $0x1,-0x14(%rbp)
jmp    11d7 <getchoice+0xb7>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    1197 <getchoice+0x77>
cmpl   $0x0,-0x14(%rbp)
jne    11ef <getchoice+0xcf>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f4 <getchoice+0xd4>
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1137 <getchoice+0x17>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getchoice>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsi),%r13
lea    0xeb4(%rip),%r15
lea    0xeb9(%rip),%r12
jmp    1168 <getchoice+0x38>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%rdi
test   %rdi,%rdi
je     1192 <getchoice+0x62>
mov    %r13,%rbp
call   1030 <puts@plt>
mov    0x0(%rbp),%rdi
add    $0x8,%rbp
test   %rdi,%rdi
jne    1180 <getchoice+0x50>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    (%rbx),%rdx
mov    %r13,%rcx
test   %rdx,%rdx
je     1160 <getchoice+0x30>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl (%rdx),%edx
cmp    %edx,%eax
je     11c5 <getchoice+0x95>
mov    (%rcx),%rdx
add    $0x8,%rcx
test   %rdx,%rdx
jne    11b0 <getchoice+0x80>
jmp    1160 <getchoice+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [12 x i8] c"Choice: %s\0A\00"
@str = [31 x i8] c"Incorrect choice, select again\00"
define i32 @getchoice(i8* %0, i8** nocapture %1) {
br label %3
3: ; preds = %28, %2
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = load i8*, i8** %1
%6 = icmp eq i8* %5, null
br i1 %6, label %14, label %7
7: ; preds = %3, %7
%8 = phi i8* [ %12, %7 ], [ %5, %3 ]
%9 = phi i8** [ %11, %7 ], [ %1, %3 ]
%10 = tail call i32 @puts(i8* %8)
%11 = getelementptr inbounds i8*, i8** %9, i64 1
%12 = load i8*, i8** %11
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %7
14: ; preds = %7, %3
%15 = tail call i32 (...) @getchar()
%16 = load i8*, i8** %1
%17 = icmp eq i8* %16, null
br i1 %17, label %28, label %21
18: ; preds = %21
%19 = load i8*, i8** %27
%20 = icmp eq i8* %19, null
br i1 %20, label %28, label %21
21: ; preds = %14, %18
%22 = phi i8* [ %19, %18 ], [ %16, %14 ]
%23 = phi i8** [ %27, %18 ], [ %1, %14 ]
%24 = load i8, i8* %22
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %15, %25
%27 = getelementptr inbounds i8*, i8** %23, i64 1
br i1 %26, label %30, label %18
28: ; preds = %18, %14
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %3
30: ; preds = %21
ret i32 %15
}