#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 */
char *calloc(int, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ stdin;
int read_choice(int num_shells) {
  char *choice_str = calloc(65, sizeof(char));
  fgets(choice_str, 64, stdin);
  if (choice_str[0] == '\n') {
    free(choice_str);
    return 0;
  }
  int choice_num;
  int elems = sscanf(choice_str, "%d", &choice_num);
  free(choice_str);
  if (elems == 1 && 0 <= choice_num && choice_num < num_shells) {
    return choice_num;
  }
  printf("incorrect choice -- defaulting to 0\n");
  return 0;
}


x86 assembly (clang, -O0):
<read_choice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x41,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
mov    $0x40,%esi
call   1050 <fgets@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11aa <read_choice+0x5a>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1209 <read_choice+0xb9>
mov    -0x10(%rbp),%rdi
lea    0xe4b(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1070 <sscanf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x1,-0x18(%rbp)
jne    11f6 <read_choice+0xa6>
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jg     11f6 <read_choice+0xa6>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11f6 <read_choice+0xa6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1209 <read_choice+0xb9>
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_choice>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    $0x41,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1050 <fgets@plt>
cmpb   $0xa,(%rbx)
jne    1192 <read_choice+0x42>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11ce <read_choice+0x7e>
lea    0xe67(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <sscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0x1,%ebp
jne    11c2 <read_choice+0x72>
mov    0xc(%rsp),%eax
test   %eax,%eax
js     11c2 <read_choice+0x72>
cmp    %r14d,%eax
jl     11d0 <read_choice+0x80>
lea    0xe3a(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [37 x i8] c"incorrect choice -- defaulting to 0\0A\00"
define i32 @read_choice(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = call i8* @calloc(i32 65, i32 1)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %8, i32 64, i32 %9)
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 10
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load i8*, i8** %4
%18 = call i32 @free(i8* %17)
store i32 0, i32* %2
br label %37
19: ; preds = %1
%20 = load i8*, i8** %4
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
store i32 %21, i32* %6
%22 = load i8*, i8** %4
%23 = call i32 @free(i8* %22)
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %35
26: ; preds = %19
%27 = load i32, i32* %5
%28 = icmp sle i32 0, %27
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i32, i32* %5
%31 = load i32, i32* %3
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i32, i32* %5
store i32 %34, i32* %2
br label %37
35: ; preds = %29, %26, %19
%36 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %37
37: ; preds = %35, %33, %16
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [3 x i8] c"%d\00"
@str = [36 x i8] c"incorrect choice -- defaulting to 0\00"
define i32 @read_choice(i32 %0) {
%2 = alloca i32
%3 = tail call dereferenceable_or_null(65) i8* @calloc(i32 65, i32 1)
%4 = load i32, i32* @stdin
%5 = tail call i32 @fgets(i8* %3, i32 64, i32 %4)
%6 = load i8, i8* %3
%7 = icmp eq i8 %6, 10
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @free(i8* %3)
br label %24
10: ; preds = %1
%11 = bitcast i32* %2 to i8*
%12 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%13 = call i32 @free(i8* %3)
%14 = icmp eq i32 %12, 1
%15 = load i32, i32* %2
%16 = icmp sgt i32 %15, -1
%17 = select i1 %14, i1 %16, i1 false
%18 = icmp slt i32 %15, %0
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %22, label %20
20: ; preds = %10
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %10, %20
%23 = phi i32 [ 0, %20 ], [ %15, %10 ]
br label %24
24: ; preds = %22, %8
%25 = phi i32 [ 0, %8 ], [ %23, %22 ]
ret i32 %25
}