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

/* Variables and functions */
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getchoice(char *greet, char *choices[], FILE *in, FILE *out) {
  int chosen = 0;
  int selected;
  char **option;
  do {
    fprintf(out, "Choice: %s\n", greet);
    option = choices;
    while (*option) {
      fprintf(out, "%s\n", *option);
      option++;
    }
    do {
      selected = fgetc(in);
    } while (selected == '\n' || selected == '\r');
    option = choices;
    while (*option) {
      if (selected == *option[0]) {
        chosen = 1;
        break;
      }
      option++;
    }
    if (!chosen) {
      fprintf(out, "Incorrect choice, select again\n");
    }
  } while (!chosen);
  return selected;
}


x86 assembly (clang, -O0):
<getchoice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xeb2(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     1195 <getchoice+0x75>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rdx
lea    0xe8b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
jmp    115d <getchoice+0x3d>
jmp    119a <getchoice+0x7a>
mov    -0x18(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    $0x1,%al
cmpl   $0xa,-0x28(%rbp)
mov    %al,-0x31(%rbp)
je     11bf <getchoice+0x9f>
cmpl   $0xd,-0x28(%rbp)
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    119a <getchoice+0x7a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     1212 <getchoice+0xf2>
mov    -0x28(%rbp),%eax
mov    -0x30(%rbp),%rcx
mov    (%rcx),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1201 <getchoice+0xe1>
movl   $0x1,-0x24(%rbp)
jmp    1212 <getchoice+0xf2>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
jmp    11d2 <getchoice+0xb2>
cmpl   $0x0,-0x24(%rbp)
jne    122e <getchoice+0x10e>
mov    -0x20(%rbp),%rdi
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1233 <getchoice+0x113>
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    113f <getchoice+0x1f>
mov    -0x28(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getchoice>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,(%rsp)
lea    0x8(%rsi),%r12
lea    0xeb5(%rip),%rbp
jmp    1179 <getchoice+0x49>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1f,%esi
mov    $0x1,%edx
lea    0xe9b(%rip),%rdi
mov    %r14,%rcx
call   1050 <fwrite@plt>
mov    %r14,%rdi
lea    0xe7d(%rip),%rsi
mov    (%rsp),%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r15),%rdx
test   %rdx,%rdx
je     11c0 <getchoice+0x90>
mov    %r12,%r13
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x0(%r13),%rdx
add    $0x8,%r13
test   %rdx,%rdx
jne    11a0 <getchoice+0x70>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <fgetc@plt>
cmp    $0xa,%eax
je     11c0 <getchoice+0x90>
cmp    $0xd,%eax
je     11c0 <getchoice+0x90>
mov    (%r15),%rdx
mov    %r12,%rcx
test   %rdx,%rdx
je     1160 <getchoice+0x30>
nopl   (%rax)
movsbl (%rdx),%edx
cmp    %edx,%eax
je     11f8 <getchoice+0xc8>
mov    (%rcx),%rdx
add    $0x8,%rcx
test   %rdx,%rdx
jne    11e0 <getchoice+0xb0>
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, i32* %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8**
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 0, i32* %9
br label %12
12: ; preds = %65, %4
%13 = load i32*, i32** %8
%14 = load i8*, i8** %5
%15 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i8**, i8*** %6
store i8** %16, i8*** %11
br label %17
17: ; preds = %21, %12
%18 = load i8**, i8*** %11
%19 = load i8*, i8** %18
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = load i32*, i32** %8
%23 = load i8**, i8*** %11
%24 = load i8*, i8** %23
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i8**, i8*** %11
%27 = getelementptr inbounds i8*, i8** %26, i32 1
store i8** %27, i8*** %11
br label %17
28: ; preds = %17
br label %29
29: ; preds = %38, %28
%30 = load i32*, i32** %7
%31 = call i32 @fgetc(i32* %30)
store i32 %31, i32* %10
br label %32
32: ; preds = %29
%33 = load i32, i32* %10
%34 = icmp eq i32 %33, 10
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, 13
br label %38
38: ; preds = %35, %32
%39 = phi i1 [ true, %32 ], [ %37, %35 ]
br i1 %39, label %29, label %40
40: ; preds = %38
%41 = load i8**, i8*** %6
store i8** %41, i8*** %11
br label %42
42: ; preds = %55, %40
%43 = load i8**, i8*** %11
%44 = load i8*, i8** %43
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %58
46: ; preds = %42
%47 = load i32, i32* %10
%48 = load i8**, i8*** %11
%49 = getelementptr inbounds i8*, i8** %48, i64 0
%50 = load i8*, i8** %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %47, %52
br i1 %53, label %54, label %55
54: ; preds = %46
store i32 1, i32* %9
br label %58
55: ; preds = %46
%56 = load i8**, i8*** %11
%57 = getelementptr inbounds i8*, i8** %56, i32 1
store i8** %57, i8*** %11
br label %42
58: ; preds = %54, %42
%59 = load i32, i32* %9
%60 = icmp ne i32 %59, 0
br i1 %60, label %64, label %61
61: ; preds = %58
%62 = load i32*, i32** %8
%63 = call i32 (i32*, i8*, ...) @fprintf(i32* %62, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %61, %58
br label %65
65: ; preds = %64
%66 = load i32, i32* %9
%67 = icmp ne i32 %66, 0
%68 = xor i1 %67, true
br i1 %68, label %12, label %69
69: ; preds = %65
%70 = load i32, i32* %10
ret i32 %70
}

LLVM IR (-O2):
@.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** nocapture %1, i32* nocapture %2, i32* nocapture %3) {
br label %5
5: ; preds = %33, %4
%6 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = load i8*, i8** %1
%8 = icmp eq i8* %7, null
br i1 %8, label %16, label %9
9: ; preds = %5, %9
%10 = phi i8* [ %14, %9 ], [ %7, %5 ]
%11 = phi i8** [ %13, %9 ], [ %1, %5 ]
%12 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%13 = getelementptr inbounds i8*, i8** %11, i64 1
%14 = load i8*, i8** %13
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %9
16: ; preds = %9, %5
br label %17
17: ; preds = %19, %16
%18 = tail call i32 @fgetc(i32* %2)
switch i32 %18, label %20 [
i32 13, label %19
i32 10, label %19
]
19: ; preds = %17, %17
br label %17
20: ; preds = %17
%21 = load i8*, i8** %1
%22 = icmp eq i8* %21, null
br i1 %22, label %33, label %26
23: ; preds = %26
%24 = load i8*, i8** %32
%25 = icmp eq i8* %24, null
br i1 %25, label %33, label %26
26: ; preds = %20, %23
%27 = phi i8* [ %24, %23 ], [ %21, %20 ]
%28 = phi i8** [ %32, %23 ], [ %1, %20 ]
%29 = load i8, i8* %27
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %18, %30
%32 = getelementptr inbounds i8*, i8** %28, i64 1
br i1 %31, label %35, label %23
33: ; preds = %23, %20
%34 = tail call i64 @fwrite(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 31, i64 1, i32* %3)
br label %5
35: ; preds = %26
ret i32 %18
}