#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 atoi(char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ get_line(char *, int);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
size_t strlen(char *);
char **prompt_for_menu(void) {
  static char buf[32];
  char **items;
  int i, num_items;
  size_t item_len;
  fputs("Number of menu items: ", stdout);
  fgets(buf, sizeof buf, stdin);
  num_items = atoi(buf);
  if (num_items < 1 || num_items > 10) {
    fputs("Invalid item count\n", stderr);
    return NULL;
  }
  items = malloc((num_items + 1) * sizeof *items);
  items[num_items] = NULL;
  for (i = 0; i < num_items; ++i) {
    printf("Enter item %d: ", i);
    get_line(buf, sizeof buf);
    item_len = strlen(buf);
    items[i] = malloc(item_len + 1);
    memcpy(items[i], buf, item_len + 1);
  }
  return items;
}


x86 assembly (clang, -O0):
<prompt_for_menu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1060 <fputs@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
lea    0x2ec3(%rip),%rdi
mov    $0x20,%esi
call   1070 <fgets@plt>
lea    0x2eb2(%rip),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jl     11da <prompt_for_menu+0x5a>
cmpl   $0xa,-0x18(%rbp)
jle    11fc <prompt_for_menu+0x7c>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdi
call   1060 <fputs@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b7 <prompt_for_menu+0x137>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rdi
shl    $0x3,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12af <prompt_for_menu+0x12f>
mov    -0x14(%rbp),%esi
lea    0xdec(%rip),%rdi
call   1050 <printf@plt>
lea    0x2e25(%rip),%rdi
mov    $0x20,%esi
call   1030 <get_line@plt>
lea    0x2e14(%rip),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
lea    0x2dd4(%rip),%rsi
call   1080 <memcpy@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1229 <prompt_for_menu+0xa9>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prompt_for_menu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
call   1060 <fputs@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
lea    0x2ec0(%rip),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   10a0 <atoi@plt>
lea    -0xb(%rax),%ecx
cmp    $0xfffffff5,%ecx
ja     11e7 <prompt_for_menu+0x67>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    0xe3a(%rip),%rdi
call   1060 <fputs@plt>
xor    %r14d,%r14d
jmp    1266 <prompt_for_menu+0xe6>
mov    %eax,%ebp
lea    0x8(,%rbp,8),%rdi
call   1090 <malloc@plt>
mov    %rax,%r14
movq   $0x0,(%rax,%rbp,8)
xor    %ebx,%ebx
lea    0xe21(%rip),%r15
lea    0x2e5f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    %r12,%rdi
mov    $0x20,%esi
call   1030 <get_line@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%r13
mov    %r13,%rdi
call   1090 <malloc@plt>
mov    %rax,(%r14,%rbx,8)
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1080 <memcpy@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1220 <prompt_for_menu+0xa0>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@prompt_for_menu.buf = internal global [32 x i8] zeroinitializer
@.str = [23 x i8] c"Number of menu items: \00"
@stdout = global i32 0
@stdin = global i32 0
@.str.1 = [20 x i8] c"Invalid item count\0A\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"Enter item %d: \00"
define i8** @prompt_for_menu() {
%1 = alloca i8**
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = load i32, i32* @stdout
%7 = call i32 @fputs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @prompt_for_menu.buf, i64 0, i64 0), i32 32, i32 %8)
%10 = call i32 @atoi(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @prompt_for_menu.buf, i64 0, i64 0))
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 1
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 10
br i1 %15, label %16, label %19
16: ; preds = %13, %0
%17 = load i32, i32* @stderr
%18 = call i32 @fputs(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i8** null, i8*** %1
br label %59
19: ; preds = %13
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 8
%24 = call i8* @malloc(i64 %23)
%25 = bitcast i8* %24 to i8**
store i8** %25, i8*** %2
%26 = load i8**, i8*** %2
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %26, i64 %28
store i8* null, i8** %29
store i32 0, i32* %3
br label %30
30: ; preds = %54, %19
%31 = load i32, i32* %3
%32 = load i32, i32* %4
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %57
34: ; preds = %30
%35 = load i32, i32* %3
%36 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = call i32 @get_line(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @prompt_for_menu.buf, i64 0, i64 0), i32 32)
%38 = call i64 @strlen(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @prompt_for_menu.buf, i64 0, i64 0))
store i64 %38, i64* %5
%39 = load i64, i64* %5
%40 = add i64 %39, 1
%41 = call i8* @malloc(i64 %40)
%42 = load i8**, i8*** %2
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8*, i8** %42, i64 %44
store i8* %41, i8** %45
%46 = load i8**, i8*** %2
%47 = load i32, i32* %3
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %46, i64 %48
%50 = load i8*, i8** %49
%51 = load i64, i64* %5
%52 = add i64 %51, 1
%53 = call i32 @memcpy(i8* %50, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @prompt_for_menu.buf, i64 0, i64 0), i64 %52)
br label %54
54: ; preds = %34
%55 = load i32, i32* %3
%56 = add nsw i32 %55, 1
store i32 %56, i32* %3
br label %30
57: ; preds = %30
%58 = load i8**, i8*** %2
store i8** %58, i8*** %1
br label %59
59: ; preds = %57, %16
%60 = load i8**, i8*** %1
ret i8** %60
}

LLVM IR (-O2):
@prompt_for_menu.buf = internal global [32 x i8] zeroinitializer
@.str = [23 x i8] c"Number of menu items: \00"
@stdout = global i32 0
@stdin = global i32 0
@.str.1 = [20 x i8] c"Invalid item count\0A\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"Enter item %d: \00"
define noalias i8** @prompt_for_menu() {
%1 = load i32, i32* @stdout
%2 = tail call i32 @fputs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = load i32, i32* @stdin
%4 = tail call i32 @fgets(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @prompt_for_menu.buf, i64 0, i64 0), i32 32, i32 %3)
%5 = tail call i32 @atoi(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @prompt_for_menu.buf, i64 0, i64 0))
%6 = add i32 %5, -11
%7 = icmp ult i32 %6, -10
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = tail call i32 @fputs(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %31
11: ; preds = %0
%12 = add nuw nsw i32 %5, 1
%13 = zext i32 %12 to i64
%14 = shl nuw nsw i64 %13, 3
%15 = tail call i8* @malloc(i64 %14)
%16 = bitcast i8* %15 to i8**
%17 = zext i32 %5 to i64
%18 = getelementptr inbounds i8*, i8** %16, i64 %17
store i8* null, i8** %18
br label %19
19: ; preds = %11, %19
%20 = phi i64 [ 0, %11 ], [ %29, %19 ]
%21 = trunc i64 %20 to i32
%22 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = tail call i32 @get_line(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @prompt_for_menu.buf, i64 0, i64 0), i32 32)
%24 = tail call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @prompt_for_menu.buf, i64 0, i64 0))
%25 = add i64 %24, 1
%26 = tail call i8* @malloc(i64 %25)
%27 = getelementptr inbounds i8*, i8** %16, i64 %20
store i8* %26, i8** %27
%28 = tail call i32 @memcpy(i8* %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @prompt_for_menu.buf, i64 0, i64 0), i64 %25)
%29 = add nuw nsw i64 %20, 1
%30 = icmp eq i64 %29, %17
br i1 %30, label %31, label %19
31: ; preds = %19, %8
%32 = phi i8** [ null, %8 ], [ %16, %19 ]
ret i8** %32
}