#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 */
scalar_t__ calloc(int, int);
scalar_t__ fgets(unsigned char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(unsigned char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned char *);
scalar_t__ realloc(unsigned char **, int);
int /*<<< orphan*/ stderr;
unsigned char *strdup(unsigned char *);
unsigned char **readitems(unsigned char *fname, int *pn) {
  int n = 0;
  unsigned char **s = NULL;
  FILE *fd = fopen(fname, "r");
  if (fd == NULL)
    fprintf(stderr, "fopen %s failed\n", fname);
  else {
    int nalloc = 1 << 16;
    s = (unsigned char **)calloc(sizeof(unsigned char *), nalloc);
    unsigned char buf[32000];
    while (fgets(buf, 32000, fd)) {
      if (n >= nalloc) {
        nalloc <<= 1;
        s = (unsigned char **)realloc(s, sizeof(unsigned char *) * nalloc);
      }
      s[n++] = strdup(buf);
    }
  }
  *pn = n;
  return s;
}


x86 assembly (clang, -O0):
<readitems>:
push   %rbp
mov    %rsp,%rbp
sub    $0x7d30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe73(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bf <readitems+0x5f>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe4d(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1251 <readitems+0xf1>
movl   $0x10000,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
mov    $0x8,%edi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
lea    -0x7d30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x7d00,%esi
call   1030 <fgets@plt>
cmp    $0x0,%rax
je     124c <readitems+0xec>
mov    -0x14(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jl     1222 <readitems+0xc2>
mov    -0x2c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
lea    -0x7d30(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    11d7 <readitems+0x77>
jmp    1251 <readitems+0xf1>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x7d30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readitems>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x7d18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     121d <readitems+0xbd>
mov    %rax,%r15
mov    $0x10000,%ebx
mov    $0x8,%edi
mov    $0x10000,%esi
call   1040 <calloc@plt>
mov    %rax,%r12
lea    0x10(%rsp),%rdi
mov    $0x7d00,%esi
mov    %r15,%rdx
call   1030 <fgets@plt>
test   %rax,%rax
je     1238 <readitems+0xd8>
mov    %r14,0x8(%rsp)
xor    %ebp,%ebp
lea    0x10(%rsp),%r13
jmp    11f5 <readitems+0x95>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1080 <strdup@plt>
mov    %rax,(%r12,%rbp,8)
add    $0x1,%rbp
mov    %r13,%rdi
mov    $0x7d00,%esi
mov    %r15,%rdx
call   1030 <fgets@plt>
test   %rax,%rax
je     1216 <readitems+0xb6>
movslq %ebx,%rax
cmp    %rax,%rbp
jl     11d0 <readitems+0x70>
lea    (%rbx,%rbx,1),%r14d
shl    $0x4,%ebx
mov    %r12,%rdi
mov    %ebx,%esi
call   1060 <realloc@plt>
mov    %rax,%r12
mov    %r14d,%ebx
jmp    11d0 <readitems+0x70>
mov    0x8(%rsp),%r14
jmp    123a <readitems+0xda>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
xor    %r12d,%r12d
xor    %ebp,%ebp
mov    %ebp,(%r14)
mov    %r12,%rax
add    $0x7d18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"fopen %s failed\0A\00"
define i8** @readitems(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [32000 x i8]
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %5
store i8** null, i8*** %6
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %50
18: ; preds = %2
store i32 65536, i32* %8
%19 = load i32, i32* %8
%20 = call i64 @calloc(i32 8, i32 %19)
%21 = inttoptr i64 %20 to i8**
store i8** %21, i8*** %6
br label %22
22: ; preds = %41, %18
%23 = getelementptr inbounds [32000 x i8], [32000 x i8]* %9, i64 0, i64 0
%24 = load i32*, i32** %7
%25 = call i64 @fgets(i8* %23, i32 32000, i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %49
27: ; preds = %22
%28 = load i32, i32* %5
%29 = load i32, i32* %8
%30 = icmp sge i32 %28, %29
br i1 %30, label %31, label %41
31: ; preds = %27
%32 = load i32, i32* %8
%33 = shl i32 %32, 1
store i32 %33, i32* %8
%34 = load i8**, i8*** %6
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = mul i64 8, %36
%38 = trunc i64 %37 to i32
%39 = call i64 @realloc(i8** %34, i32 %38)
%40 = inttoptr i64 %39 to i8**
store i8** %40, i8*** %6
br label %41
41: ; preds = %31, %27
%42 = getelementptr inbounds [32000 x i8], [32000 x i8]* %9, i64 0, i64 0
%43 = call i8* @strdup(i8* %42)
%44 = load i8**, i8*** %6
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8*, i8** %44, i64 %47
store i8* %43, i8** %48
br label %22
49: ; preds = %22
br label %50
50: ; preds = %49, %14
%51 = load i32, i32* %5
%52 = load i32*, i32** %4
store i32 %51, i32* %52
%53 = load i8**, i8*** %6
ret i8** %53
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"fopen %s failed\0A\00"
define i8** @readitems(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [32000 x i8]
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %39
9: ; preds = %2
%10 = tail call i64 @calloc(i32 8, i32 65536)
%11 = inttoptr i64 %10 to i8**
%12 = getelementptr inbounds [32000 x i8], [32000 x i8]* %3, i64 0, i64 0
%13 = call i64 @fgets(i8* %12, i32 32000, i32* %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %36, label %15
15: ; preds = %9, %26
%16 = phi i64 [ %30, %26 ], [ 0, %9 ]
%17 = phi i32 [ %28, %26 ], [ 65536, %9 ]
%18 = phi i8** [ %27, %26 ], [ %11, %9 ]
%19 = sext i32 %17 to i64
%20 = icmp slt i64 %16, %19
br i1 %20, label %26, label %21
21: ; preds = %15
%22 = shl i32 %17, 1
%23 = shl i32 %17, 4
%24 = call i64 @realloc(i8** %18, i32 %23)
%25 = inttoptr i64 %24 to i8**
br label %26
26: ; preds = %21, %15
%27 = phi i8** [ %25, %21 ], [ %18, %15 ]
%28 = phi i32 [ %22, %21 ], [ %17, %15 ]
%29 = call i8* @strdup(i8* %12)
%30 = add nuw i64 %16, 1
%31 = getelementptr inbounds i8*, i8** %27, i64 %16
store i8* %29, i8** %31
%32 = call i64 @fgets(i8* %12, i32 32000, i32* %4)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %15
34: ; preds = %26
%35 = trunc i64 %30 to i32
br label %36
36: ; preds = %34, %9
%37 = phi i8** [ %11, %9 ], [ %27, %34 ]
%38 = phi i32 [ 0, %9 ], [ %35, %34 ]
br label %39
39: ; preds = %36, %6
%40 = phi i8** [ null, %6 ], [ %37, %36 ]
%41 = phi i32 [ 0, %6 ], [ %38, %36 ]
store i32 %41, i32* %1
ret i8** %40
}