#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 atoi(char *);
char *calloc(int, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *);
char *strchr(char *, char);
int strlen(char *);
char *lines_from_file(FILE *fp, int *n_lines, int *max_str_len) {
  char line[256];
  if (!fgets(line, 256, fp)) {
    printf("File read error\n");
    return NULL;
  }
  *n_lines = atoi(line);
  char *content = calloc(*n_lines * 256, sizeof(char));
  if (!content) {
    printf("Failed to allocate file content buffer\n");
    return NULL;
  }
  for (int i = 0; i < *n_lines; ++i) {
    if (!fgets(content + i * 256, 256, fp)) {
      printf("Error reading from file\n");
      free(content);
      return NULL;
    }
    int len = strlen(content + i * 256);
    if (len > *max_str_len) {
      *max_str_len = len;
    }
    char *new_line = strchr(content + i * 256, '\n');
    if (new_line) {
      *new_line = '\0';
    }
  }
  char *matrix = malloc((*n_lines * (*max_str_len) + *n_lines) * sizeof(char));
  if (!matrix) {
    printf("Failed to allocate matrix buffer\n");
    free(content);
    return NULL;
  }
  for (int i = 0; i < *n_lines; ++i) {
    memcpy(matrix + i * (*max_str_len), content + i * 256, (*max_str_len) + 1);
  }
  free(content);
  return matrix;
}


x86 assembly (clang, -O0):
<lines_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%eax
jne    11de <lines_from_file+0x4e>
lea    0xe34(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1412 <lines_from_file+0x282>
lea    -0x120(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
shl    $0x8,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    1233 <lines_from_file+0xa3>
lea    0xdf0(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1412 <lines_from_file+0x282>
movl   $0x0,-0x12c(%rbp)
mov    -0x12c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jge    1335 <lines_from_file+0x1a5>
mov    -0x128(%rbp),%rdi
mov    -0x12c(%rbp),%eax
shl    $0x8,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%eax
jne    12a0 <lines_from_file+0x110>
lea    0xdb7(%rip),%rdi
call   1060 <printf@plt>
mov    -0x128(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1412 <lines_from_file+0x282>
mov    -0x128(%rbp),%rdi
mov    -0x12c(%rbp),%eax
shl    $0x8,%eax
cltq
add    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,-0x130(%rbp)
mov    -0x130(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jle    12de <lines_from_file+0x14e>
mov    -0x130(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x128(%rbp),%rdi
mov    -0x12c(%rbp),%eax
shl    $0x8,%eax
cltq
add    %rax,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
je     131c <lines_from_file+0x18c>
mov    -0x138(%rbp),%rax
movb   $0x0,(%rax)
jmp    1321 <lines_from_file+0x191>
mov    -0x12c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x12c(%rbp)
jmp    123d <lines_from_file+0xad>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    -0x20(%rbp),%rcx
imul   (%rcx),%eax
mov    -0x18(%rbp),%rcx
add    (%rcx),%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x140(%rbp)
cmpq   $0x0,-0x140(%rbp)
jne    138f <lines_from_file+0x1ff>
lea    0xce1(%rip),%rdi
call   1060 <printf@plt>
mov    -0x128(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1412 <lines_from_file+0x282>
movl   $0x0,-0x144(%rbp)
mov    -0x144(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jge    13fb <lines_from_file+0x26b>
mov    -0x140(%rbp),%rdi
mov    -0x144(%rbp),%eax
mov    -0x20(%rbp),%rcx
imul   (%rcx),%eax
cltq
add    %rax,%rdi
mov    -0x128(%rbp),%rsi
mov    -0x144(%rbp),%eax
shl    $0x8,%eax
cltq
add    %rax,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
add    $0x1,%edx
call   1090 <memcpy@plt>
mov    -0x144(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x144(%rbp)
jmp    1399 <lines_from_file+0x209>
mov    -0x128(%rbp),%rdi
call   1030 <free@plt>
mov    -0x140(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lines_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r13
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %r13,%rdx
call   1070 <fgets@plt>
test   %eax,%eax
je     1254 <lines_from_file+0xc4>
lea    0x10(%rsp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
mov    %eax,(%r14)
mov    %eax,%edi
shl    $0x8,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
test   %rax,%rax
je     12e9 <lines_from_file+0x159>
test   %ebp,%ebp
mov    %rax,0x8(%rsp)
jle    1260 <lines_from_file+0xd0>
xor    %r15d,%r15d
mov    %rax,%rbx
jmp    1213 <lines_from_file+0x83>
nopl   0x0(%rax)
add    $0x1,%r15
movslq (%r14),%rbp
add    $0x100,%rbx
cmp    %rbp,%r15
jge    1260 <lines_from_file+0xd0>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r13,%rdx
call   1070 <fgets@plt>
test   %eax,%eax
je     12f7 <lines_from_file+0x167>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    (%r12),%eax
jle    123d <lines_from_file+0xad>
mov    %eax,(%r12)
mov    %rbx,%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1200 <lines_from_file+0x70>
movb   $0x0,(%rax)
jmp    1200 <lines_from_file+0x70>
lea    0xda5(%rip),%rdi
jmp    12f0 <lines_from_file+0x160>
mov    (%r12),%r13d
mov    %r13d,%edi
imul   %ebp,%edi
add    %ebp,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1300 <lines_from_file+0x170>
mov    %rax,%r15
test   %ebp,%ebp
jle    12dd <lines_from_file+0x14d>
add    $0x1,%r13d
mov    %r15,%rdi
mov    0x8(%rsp),%rsi
mov    %r13d,%edx
call   1090 <memcpy@plt>
cmpl   $0x2,(%r14)
jl     12dd <lines_from_file+0x14d>
mov    0x8(%rsp),%rbp
add    $0x100,%rbp
mov    $0x1,%ebx
nopl   0x0(%rax)
mov    (%r12),%edx
mov    %ebx,%eax
imul   %edx,%eax
movslq %eax,%rdi
add    %r15,%rdi
add    $0x1,%edx
mov    %rbp,%rsi
call   1090 <memcpy@plt>
add    $0x1,%rbx
movslq (%r14),%rax
add    $0x100,%rbp
cmp    %rax,%rbx
jl     12b0 <lines_from_file+0x120>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    1319 <lines_from_file+0x189>
lea    0xd20(%rip),%rdi
call   1040 <puts@plt>
jmp    1316 <lines_from_file+0x186>
lea    0xd5a(%rip),%rdi
jmp    1307 <lines_from_file+0x177>
lea    0xd30(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"File read error\0A\00"
@.str.1 = [40 x i8] c"Failed to allocate file content buffer\0A\00"
@.str.2 = [25 x i8] c"Error reading from file\0A\00"
@.str.3 = [34 x i8] c"Failed to allocate matrix buffer\0A\00"
define i8* @lines_from_file(i32* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [256 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%16 = load i32*, i32** %5
%17 = call i32 @fgets(i8* %15, i32 256, i32* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %3
%20 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %130
21: ; preds = %3
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%23 = call i32 @atoi(i8* %22)
%24 = load i32*, i32** %6
store i32 %23, i32* %24
%25 = load i32*, i32** %6
%26 = load i32, i32* %25
%27 = mul nsw i32 %26, 256
%28 = call i8* @calloc(i32 %27, i32 1)
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = icmp ne i8* %29, null
br i1 %30, label %33, label %31
31: ; preds = %21
%32 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %4
br label %130
33: ; preds = %21
store i32 0, i32* %10
br label %34
34: ; preds = %78, %33
%35 = load i32, i32* %10
%36 = load i32*, i32** %6
%37 = load i32, i32* %36
%38 = icmp slt i32 %35, %37
br i1 %38, label %39, label %81
39: ; preds = %34
%40 = load i8*, i8** %9
%41 = load i32, i32* %10
%42 = mul nsw i32 %41, 256
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
%45 = load i32*, i32** %5
%46 = call i32 @fgets(i8* %44, i32 256, i32* %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %39
%49 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%50 = load i8*, i8** %9
%51 = call i32 @free(i8* %50)
store i8* null, i8** %4
br label %130
52: ; preds = %39
%53 = load i8*, i8** %9
%54 = load i32, i32* %10
%55 = mul nsw i32 %54, 256
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
%58 = call i32 @strlen(i8* %57)
store i32 %58, i32* %11
%59 = load i32, i32* %11
%60 = load i32*, i32** %7
%61 = load i32, i32* %60
%62 = icmp sgt i32 %59, %61
br i1 %62, label %63, label %66
63: ; preds = %52
%64 = load i32, i32* %11
%65 = load i32*, i32** %7
store i32 %64, i32* %65
br label %66
66: ; preds = %63, %52
%67 = load i8*, i8** %9
%68 = load i32, i32* %10
%69 = mul nsw i32 %68, 256
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %67, i64 %70
%72 = call i8* @strchr(i8* %71, i8 signext 10)
store i8* %72, i8** %12
%73 = load i8*, i8** %12
%74 = icmp ne i8* %73, null
br i1 %74, label %75, label %77
75: ; preds = %66
%76 = load i8*, i8** %12
store i8 0, i8* %76
br label %77
77: ; preds = %75, %66
br label %78
78: ; preds = %77
%79 = load i32, i32* %10
%80 = add nsw i32 %79, 1
store i32 %80, i32* %10
br label %34
81: ; preds = %34
%82 = load i32*, i32** %6
%83 = load i32, i32* %82
%84 = load i32*, i32** %7
%85 = load i32, i32* %84
%86 = mul nsw i32 %83, %85
%87 = load i32*, i32** %6
%88 = load i32, i32* %87
%89 = add nsw i32 %86, %88
%90 = sext i32 %89 to i64
%91 = mul i64 %90, 1
%92 = trunc i64 %91 to i32
%93 = call i8* @malloc(i32 %92)
store i8* %93, i8** %13
%94 = load i8*, i8** %13
%95 = icmp ne i8* %94, null
br i1 %95, label %100, label %96
96: ; preds = %81
%97 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%98 = load i8*, i8** %9
%99 = call i32 @free(i8* %98)
store i8* null, i8** %4
br label %130
100: ; preds = %81
store i32 0, i32* %14
br label %101
101: ; preds = %123, %100
%102 = load i32, i32* %14
%103 = load i32*, i32** %6
%104 = load i32, i32* %103
%105 = icmp slt i32 %102, %104
br i1 %105, label %106, label %126
106: ; preds = %101
%107 = load i8*, i8** %13
%108 = load i32, i32* %14
%109 = load i32*, i32** %7
%110 = load i32, i32* %109
%111 = mul nsw i32 %108, %110
%112 = sext i32 %111 to i64
%113 = getelementptr inbounds i8, i8* %107, i64 %112
%114 = load i8*, i8** %9
%115 = load i32, i32* %14
%116 = mul nsw i32 %115, 256
%117 = sext i32 %116 to i64
%118 = getelementptr inbounds i8, i8* %114, i64 %117
%119 = load i32*, i32** %7
%120 = load i32, i32* %119
%121 = add nsw i32 %120, 1
%122 = call i32 @memcpy(i8* %113, i8* %118, i32 %121)
br label %123
123: ; preds = %106
%124 = load i32, i32* %14
%125 = add nsw i32 %124, 1
store i32 %125, i32* %14
br label %101
126: ; preds = %101
%127 = load i8*, i8** %9
%128 = call i32 @free(i8* %127)
%129 = load i8*, i8** %13
store i8* %129, i8** %4
br label %130
130: ; preds = %126, %96, %48, %31, %19
%131 = load i8*, i8** %4
ret i8* %131
}

LLVM IR (-O2):
@str = [16 x i8] c"File read error\00"
@str.4 = [39 x i8] c"Failed to allocate file content buffer\00"
@str.5 = [33 x i8] c"Failed to allocate matrix buffer\00"
@str.6 = [24 x i8] c"Error reading from file\00"
define i8* @lines_from_file(i32* nocapture %0, i32* nocapture %1, i32* nocapture %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i32 @fgets(i8* %5, i32 256, i32* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %76
10: ; preds = %3
%11 = call i32 @atoi(i8* %5)
store i32 %11, i32* %1
%12 = shl nsw i32 %11, 8
%13 = call i8* @calloc(i32 %12, i32 1)
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %10
%16 = icmp sgt i32 %11, 0
br i1 %16, label %19, label %42
17: ; preds = %10
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.4, i64 0, i64 0))
br label %76
19: ; preds = %15, %37
%20 = phi i64 [ %38, %37 ], [ 0, %15 ]
%21 = shl nsw i64 %20, 8
%22 = getelementptr inbounds i8, i8* %13, i64 %21
%23 = call i32 @fgets(i8* %22, i32 256, i32* %0)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.6, i64 0, i64 0))
%27 = call i32 @free(i8* %13)
br label %76
28: ; preds = %19
%29 = call i32 @strlen(i8* %22)
%30 = load i32, i32* %2
%31 = icmp sgt i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 %29, i32* %2
br label %33
33: ; preds = %32, %28
%34 = call i8* @strchr(i8* %22, i8 signext 10)
%35 = icmp eq i8* %34, null
br i1 %35, label %37, label %36
36: ; preds = %33
store i8 0, i8* %34
br label %37
37: ; preds = %36, %33
%38 = add nuw nsw i64 %20, 1
%39 = load i32, i32* %1
%40 = sext i32 %39 to i64
%41 = icmp slt i64 %38, %40
br i1 %41, label %19, label %42
42: ; preds = %37, %15
%43 = phi i32 [ %11, %15 ], [ %39, %37 ]
%44 = load i32, i32* %2
%45 = mul nsw i32 %44, %43
%46 = add nsw i32 %45, %43
%47 = call i8* @malloc(i32 %46)
%48 = icmp eq i8* %47, null
br i1 %48, label %56, label %49
49: ; preds = %42
%50 = icmp sgt i32 %43, 0
br i1 %50, label %51, label %59
51: ; preds = %49
%52 = add nsw i32 %44, 1
%53 = call i32 @memcpy(i8* %47, i8* %13, i32 %52)
%54 = load i32, i32* %1
%55 = icmp sgt i32 %54, 1
br i1 %55, label %61, label %59
56: ; preds = %42
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.5, i64 0, i64 0))
%58 = call i32 @free(i8* %13)
br label %76
59: ; preds = %61, %51, %49
%60 = call i32 @free(i8* %13)
br label %76
61: ; preds = %51, %61
%62 = phi i64 [ %72, %61 ], [ 1, %51 ]
%63 = load i32, i32* %2
%64 = trunc i64 %62 to i32
%65 = mul nsw i32 %63, %64
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %47, i64 %66
%68 = shl nsw i64 %62, 8
%69 = getelementptr inbounds i8, i8* %13, i64 %68
%70 = add nsw i32 %63, 1
%71 = call i32 @memcpy(i8* %67, i8* %69, i32 %70)
%72 = add nuw nsw i64 %62, 1
%73 = load i32, i32* %1
%74 = sext i32 %73 to i64
%75 = icmp slt i64 %72, %74
br i1 %75, label %61, label %59
76: ; preds = %25, %17, %59, %56, %8
%77 = phi i8* [ null, %8 ], [ null, %25 ], [ null, %17 ], [ %47, %59 ], [ null, %56 ]
ret i8* %77
}