#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 */
char **calloc(int const, int);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strtok(char *, char *);
char **read(const char *fname, const int lines) {
  char **ret_lines = calloc(lines + 1, sizeof(char *) * 256);
  if (ret_lines == NULL) {
    printf("There was a malloc error with ret_lines");
    return NULL;
  }
  FILE *file;
  file = fopen(fname, "r");
  if (file) {
    char *buff = malloc(1024);
    size_t nread;
    if (buff == NULL) {
      printf("There was a malloc error");
      return NULL;
    }
    size_t idx = 0;
    while ((nread = fread(buff, 1, 1024, file)) > 0) {
      char tmp[1024];
      strcpy(tmp, buff);
      char *token = strtok(tmp, "\n");
      while (token) {
        ret_lines[idx] = token;
        idx++;
        token = strtok(NULL, "\n");
      }
    }
    if (ret_lines[idx - 1] != NULL)
      ret_lines[idx] = NULL;
    free(buff);
  } else
    return NULL;
  return ret_lines;
}


x86 assembly (clang, -O0):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
mov    $0x800,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ca <read+0x4a>
lea    0xe48(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    130a <read+0x18a>
mov    -0x10(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12f5 <read+0x175>
mov    $0x400,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121b <read+0x9b>
lea    0xe1f(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    130a <read+0x18a>
movq   $0x0,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1050 <fread@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jbe    12c0 <read+0x140>
lea    -0x440(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x440(%rbp),%rdi
lea    0xddb(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
je     12bb <read+0x13b>
mov    -0x448(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd97(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x448(%rbp)
jmp    1272 <read+0xf2>
jmp    1223 <read+0xa3>
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    $0x1,%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     12e7 <read+0x167>
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    1302 <read+0x182>
movq   $0x0,-0x8(%rbp)
jmp    130a <read+0x18a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
lea    0x1(%rsi),%edi
mov    $0x800,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     1259 <read+0xd9>
mov    %rax,%rbx
lea    0xe8b(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1288 <read+0x108>
mov    %rax,%r15
mov    $0x400,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     127c <read+0xfc>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1262 <read+0xe2>
mov    %rsp,%r12
lea    0xe3d(%rip),%r13
jmp    122a <read+0xaa>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
test   %rax,%rax
je     1262 <read+0xe2>
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r12,%rdi
nopl   0x0(%rax,%rax,1)
mov    %r13,%rsi
call   10a0 <strtok@plt>
test   %rax,%rax
je     1210 <read+0x90>
mov    %rax,(%rbx,%rbp,8)
add    $0x1,%rbp
xor    %edi,%edi
jmp    1240 <read+0xc0>
lea    0xda0(%rip),%rdi
jmp    1283 <read+0x103>
cmpq   $0x0,-0x8(%rbx,%rbp,8)
je     1272 <read+0xf2>
movq   $0x0,(%rbx,%rbp,8)
mov    %r14,%rdi
call   1030 <free@plt>
jmp    128a <read+0x10a>
lea    0xda5(%rip),%rdi
call   1060 <printf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"There was a malloc error with ret_lines\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"There was a malloc error\00"
@.str.3 = [2 x i8] c"\0A\00"
define i8** @read(i8* %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca [1024 x i8]
%12 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%13 = load i32, i32* %5
%14 = add nsw i32 %13, 1
%15 = call i8** @calloc(i32 %14, i32 2048)
store i8** %15, i8*** %6
%16 = load i8**, i8*** %6
%17 = icmp eq i8** %16, null
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i8** null, i8*** %3
br label %72
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %7
%23 = load i32*, i32** %7
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %69
25: ; preds = %20
%26 = call i8* @malloc(i32 1024)
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i8** null, i8*** %3
br label %72
31: ; preds = %25
store i64 0, i64* %10
br label %32
32: ; preds = %54, %31
%33 = load i8*, i8** %8
%34 = load i32*, i32** %7
%35 = call i64 @fread(i8* %33, i32 1, i32 1024, i32* %34)
store i64 %35, i64* %9
%36 = icmp ugt i64 %35, 0
br i1 %36, label %37, label %55
37: ; preds = %32
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%39 = load i8*, i8** %8
%40 = call i32 @strcpy(i8* %38, i8* %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%42 = call i8* @strtok(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %42, i8** %12
br label %43
43: ; preds = %46, %37
%44 = load i8*, i8** %12
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %54
46: ; preds = %43
%47 = load i8*, i8** %12
%48 = load i8**, i8*** %6
%49 = load i64, i64* %10
%50 = getelementptr inbounds i8*, i8** %48, i64 %49
store i8* %47, i8** %50
%51 = load i64, i64* %10
%52 = add i64 %51, 1
store i64 %52, i64* %10
%53 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %53, i8** %12
br label %43
54: ; preds = %43
br label %32
55: ; preds = %32
%56 = load i8**, i8*** %6
%57 = load i64, i64* %10
%58 = sub i64 %57, 1
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
%60 = load i8*, i8** %59
%61 = icmp ne i8* %60, null
br i1 %61, label %62, label %66
62: ; preds = %55
%63 = load i8**, i8*** %6
%64 = load i64, i64* %10
%65 = getelementptr inbounds i8*, i8** %63, i64 %64
store i8* null, i8** %65
br label %66
66: ; preds = %62, %55
%67 = load i8*, i8** %8
%68 = call i32 @free(i8* %67)
br label %70
69: ; preds = %20
store i8** null, i8*** %3
br label %72
70: ; preds = %66
%71 = load i8**, i8*** %6
store i8** %71, i8*** %3
br label %72
72: ; preds = %70, %69, %29, %18
%73 = load i8**, i8*** %3
ret i8** %73
}

LLVM IR (-O2):
@.str = [40 x i8] c"There was a malloc error with ret_lines\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"There was a malloc error\00"
@.str.3 = [2 x i8] c"\0A\00"
define noalias i8** @read(i8* nocapture %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = add nsw i32 %1, 1
%5 = tail call i8** @calloc(i32 %4, i32 2048)
%6 = icmp eq i8** %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %48
9: ; preds = %2
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %48, label %12
12: ; preds = %9
%13 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%14 = icmp eq i8* %13, null
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = call i64 @fread(i8* %13, i32 1, i32 1024, i32* %10)
%17 = icmp eq i64 %16, 0
br i1 %17, label %38, label %18
18: ; preds = %15
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %22
20: ; preds = %12
%21 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %48
22: ; preds = %18, %34
%23 = phi i64 [ 0, %18 ], [ %35, %34 ]
%24 = call i32 @strcpy(i8* %19, i8* %13)
%25 = call i8* @strtok(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %34, label %27
27: ; preds = %22, %27
%28 = phi i8* [ %32, %27 ], [ %25, %22 ]
%29 = phi i64 [ %31, %27 ], [ %23, %22 ]
%30 = getelementptr inbounds i8*, i8** %5, i64 %29
store i8* %28, i8** %30
%31 = add i64 %29, 1
%32 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %27
34: ; preds = %27, %22
%35 = phi i64 [ %23, %22 ], [ %31, %27 ]
%36 = call i64 @fread(i8* %13, i32 1, i32 1024, i32* %10)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %22
38: ; preds = %34, %15
%39 = phi i64 [ 0, %15 ], [ %35, %34 ]
%40 = add i64 %39, -1
%41 = getelementptr inbounds i8*, i8** %5, i64 %40
%42 = load i8*, i8** %41
%43 = icmp eq i8* %42, null
br i1 %43, label %46, label %44
44: ; preds = %38
%45 = getelementptr inbounds i8*, i8** %5, i64 %39
store i8* null, i8** %45
br label %46
46: ; preds = %44, %38
%47 = call i32 @free(i8* %13)
br label %48
48: ; preds = %20, %46, %9, %7
%49 = phi i8** [ null, %7 ], [ null, %9 ], [ %5, %46 ], [ null, %20 ]
ret i8** %49
}