#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');
    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    $0x30,%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)
cmpl   $0xa,-0x28(%rbp)
je     119a <getchoice+0x7a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     11f8 <getchoice+0xd8>
mov    -0x28(%rbp),%eax
mov    -0x30(%rbp),%rcx
mov    (%rcx),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11e7 <getchoice+0xc7>
movl   $0x1,-0x24(%rbp)
jmp    11f8 <getchoice+0xd8>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
jmp    11b8 <getchoice+0x98>
cmpl   $0x0,-0x24(%rbp)
jne    1214 <getchoice+0xf4>
mov    -0x20(%rbp),%rdi
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1219 <getchoice+0xf9>
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    113f <getchoice+0x1f>
mov    -0x28(%rbp),%eax
add    $0x30,%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>
mov    (%r15),%rdx
mov    %r12,%rcx
test   %rdx,%rdx
je     1160 <getchoice+0x30>
nopl   0x0(%rax,%rax,1)
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 = %60, %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 = %32, %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 %29, label %35
35: ; preds = %32
%36 = load i8**, i8*** %6
store i8** %36, i8*** %11
br label %37
37: ; preds = %50, %35
%38 = load i8**, i8*** %11
%39 = load i8*, i8** %38
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %53
41: ; preds = %37
%42 = load i32, i32* %10
%43 = load i8**, i8*** %11
%44 = getelementptr inbounds i8*, i8** %43, i64 0
%45 = load i8*, i8** %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %42, %47
br i1 %48, label %49, label %50
49: ; preds = %41
store i32 1, i32* %9
br label %53
50: ; preds = %41
%51 = load i8**, i8*** %11
%52 = getelementptr inbounds i8*, i8** %51, i32 1
store i8** %52, i8*** %11
br label %37
53: ; preds = %49, %37
%54 = load i32, i32* %9
%55 = icmp ne i32 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %53
%57 = load i32*, i32** %8
%58 = call i32 (i32*, i8*, ...) @fprintf(i32* %57, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %56, %53
br label %60
60: ; preds = %59
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 0
%63 = xor i1 %62, true
br i1 %63, label %12, label %64
64: ; preds = %60
%65 = load i32, i32* %10
ret i32 %65
}

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 = %16, %17
%18 = tail call i32 @fgetc(i32* %2)
%19 = icmp eq i32 %18, 10
br i1 %19, label %17, label %20
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
}