#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 = 0;
  char **option;
  do {
    printf("Choices: %s.\n", greet);
    option = choices;
    while (*option) {
      printf("%s\n", *option);
      ++option;
    }
    do {
      selected = getchar();
    } while (selected == '\n');
    option = choices;
    while (*option) {
      if (selected == *option[0]) {
        chosen = 1;
        break;
      }
      ++option;
    }
    if (!chosen)
      printf("Wrong choice, try again later.\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)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xeb7(%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     118c <getchoice+0x6c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    1158 <getchoice+0x38>
jmp    1191 <getchoice+0x71>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
je     1191 <getchoice+0x71>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11ed <getchoice+0xcd>
mov    -0x18(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11dc <getchoice+0xbc>
movl   $0x1,-0x14(%rbp)
jmp    11ed <getchoice+0xcd>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    11ad <getchoice+0x8d>
cmpl   $0x0,-0x14(%rbp)
jne    1205 <getchoice+0xe5>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    120a <getchoice+0xea>
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    113e <getchoice+0x1e>
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    0xebb(%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     11a0 <getchoice+0x70>
mov    %r13,%rbp
call   1030 <puts@plt>
mov    0x0(%rbp),%rdi
add    $0x8,%rbp
test   %rdi,%rdi
jne    1180 <getchoice+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%eax
je     11a0 <getchoice+0x70>
mov    (%rbx),%rdx
mov    %r13,%rcx
test   %rdx,%rdx
je     1160 <getchoice+0x30>
nopw   0x0(%rax,%rax,1)
movsbl (%rdx),%edx
cmp    %edx,%eax
je     11d5 <getchoice+0xa5>
mov    (%rcx),%rdx
add    $0x8,%rcx
test   %rdx,%rdx
jne    11c0 <getchoice+0x90>
jmp    1160 <getchoice+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Choices: %s.\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [32 x i8] c"Wrong choice, try again later.\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
store i32 0, i32* %6
br label %8
8: ; preds = %52, %2
%9 = load i8*, i8** %3
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 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
br label %23
23: ; preds = %25, %22
%24 = call i32 (...) @getchar()
store i32 %24, i32* %6
br label %25
25: ; preds = %23
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 10
br i1 %27, label %23, label %28
28: ; preds = %25
%29 = load i8**, i8*** %4
store i8** %29, i8*** %7
br label %30
30: ; preds = %43, %28
%31 = load i8**, i8*** %7
%32 = load i8*, i8** %31
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %46
34: ; preds = %30
%35 = load i32, i32* %6
%36 = load i8**, i8*** %7
%37 = getelementptr inbounds i8*, i8** %36, i64 0
%38 = load i8*, i8** %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %35, %40
br i1 %41, label %42, label %43
42: ; preds = %34
store i32 1, i32* %5
br label %46
43: ; preds = %34
%44 = load i8**, i8*** %7
%45 = getelementptr inbounds i8*, i8** %44, i32 1
store i8** %45, i8*** %7
br label %30
46: ; preds = %42, %30
%47 = load i32, i32* %5
%48 = icmp ne i32 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %46
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
br label %52
52: ; preds = %51
%53 = load i32, i32* %5
%54 = icmp ne i32 %53, 0
%55 = xor i1 %54, true
br i1 %55, label %8, label %56
56: ; preds = %52
%57 = load i32, i32* %6
ret i32 %57
}

LLVM IR (-O2):
@.str = [14 x i8] c"Choices: %s.\0A\00"
@str = [31 x i8] c"Wrong choice, try again later.\00"
define i32 @getchoice(i8* %0, i8** nocapture %1) {
br label %3
3: ; preds = %31, %2
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 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
br label %15
15: ; preds = %14, %15
%16 = tail call i32 (...) @getchar()
%17 = icmp eq i32 %16, 10
br i1 %17, label %15, label %18
18: ; preds = %15
%19 = load i8*, i8** %1
%20 = icmp eq i8* %19, null
br i1 %20, label %31, label %24
21: ; preds = %24
%22 = load i8*, i8** %30
%23 = icmp eq i8* %22, null
br i1 %23, label %31, label %24
24: ; preds = %18, %21
%25 = phi i8* [ %22, %21 ], [ %19, %18 ]
%26 = phi i8** [ %30, %21 ], [ %1, %18 ]
%27 = load i8, i8* %25
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %16, %28
%30 = getelementptr inbounds i8*, i8** %26, i64 1
br i1 %29, label %33, label %21
31: ; preds = %21, %18
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %3
33: ; preds = %24
ret i32 %16
}