#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 */
void *calloc(int, int);
int /*<<< orphan*/ fail(int, char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *get_line(int /*<<< orphan*/ *);
char *strtok(char *, char *);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
int **read_array(char *filename, int *dim_ptr) {
  FILE *fp = fopen(filename, "r");
  if (fp == NULL) {
    fail(1, "read_array", "Cannot open file.");
  }
  char *line = get_line(fp);
  int dim = (int)strtol(line, NULL, 10);
  if (dim == 0) {
    fail(2, "read_array", "Invalid file format.");
  }
  int **arr = calloc(dim, sizeof(int *));
  char *tok;
  int j;
  for (int i = 0; i < dim; i++) {
    arr[i] = calloc(dim, sizeof(int));
    line = get_line(fp);
    j = 0;
    for (tok = strtok(line, " "); tok; tok = strtok(NULL, " ")) {
      if (j < dim) {
        arr[i][j++] = (int)strtol(tok, NULL, 10);
      }
    }
  }
  fclose(fp);
  if (dim_ptr != NULL) {
    *dim_ptr = dim;
  }
  return (arr);
}


x86 assembly (clang, -O0):
<read_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <read_array+0x47>
mov    $0x1,%edi
lea    0xe57(%rip),%rsi
lea    0xe5b(%rip),%rdx
call   1030 <fail@plt>
mov    -0x18(%rbp),%rdi
call   1040 <get_line@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11fb <read_array+0x8b>
mov    $0x2,%edi
lea    0xe13(%rip),%rsi
lea    0xe29(%rip),%rdx
call   1030 <fail@plt>
mov    -0x24(%rbp),%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12dc <read_array+0x16c>
mov    -0x24(%rbp),%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rdi
call   1040 <get_line@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12c9 <read_array+0x159>
mov    -0x3c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12ab <read_array+0x13b>
mov    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x3c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x3c(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    12b0 <read_array+0x140>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd79(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    1263 <read_array+0xf3>
jmp    12ce <read_array+0x15e>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1213 <read_array+0xa3>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x10(%rbp)
je     12f9 <read_array+0x189>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_array>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
lea    0xe78(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11ad <read_array+0x3d>
lea    0xe66(%rip),%rsi
lea    0xe6a(%rip),%rdx
mov    $0x1,%edi
call   1030 <fail@plt>
mov    %r12,%rdi
call   1040 <get_line@plt>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,%rbx
test   %ebx,%ebx
je     1280 <read_array+0x110>
mov    %ebx,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,(%rsp)
test   %ebx,%ebx
jle    12af <read_array+0x13f>
mov    %rbp,0x8(%rsp)
mov    %ebx,%eax
mov    %rax,0x10(%rsp)
xor    %r15d,%r15d
lea    0xe37(%rip),%r13
jmp    1212 <read_array+0xa2>
nop
add    $0x1,%r15
cmp    0x10(%rsp),%r15
mov    %r14,%r12
je     12aa <read_array+0x13a>
mov    %ebx,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%rbp
mov    (%rsp),%rax
mov    %rbp,(%rax,%r15,8)
mov    %r12,%r14
mov    %r12,%rdi
call   1040 <get_line@plt>
mov    %rax,%rdi
mov    %r13,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
je     1200 <read_array+0x90>
xor    %r12d,%r12d
jmp    125f <read_array+0xef>
nopl   0x0(%rax)
xor    %edi,%edi
mov    %r13,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
je     1200 <read_array+0x90>
cmp    %ebx,%r12d
jge    1250 <read_array+0xe0>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
movslq %r12d,%rcx
add    $0x1,%r12d
mov    %eax,0x0(%rbp,%rcx,4)
jmp    1250 <read_array+0xe0>
lea    0xd7b(%rip),%rsi
lea    0xd91(%rip),%rdx
mov    $0x2,%edi
call   1030 <fail@plt>
mov    %ebx,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,(%rsp)
jmp    12af <read_array+0x13f>
mov    0x8(%rsp),%rbp
mov    %r12,%rdi
call   1050 <fclose@plt>
test   %rbp,%rbp
je     12bf <read_array+0x14f>
mov    %ebx,0x0(%rbp)
mov    (%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [11 x i8] c"read_array\00"
@.str.2 = [18 x i8] c"Cannot open file.\00"
@.str.3 = [21 x i8] c"Invalid file format.\00"
@.str.4 = [2 x i8] c" \00"
define i32** @read_array(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32**
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @fail(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32*, i32** %5
%20 = call i8* @get_line(i32* %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = call i64 @strtol(i8* %21, i32* null, i32 10)
%23 = trunc i64 %22 to i32
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @fail(i32 2, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %18
%29 = load i32, i32* %7
%30 = call i8* @calloc(i32 %29, i32 8)
%31 = bitcast i8* %30 to i32**
store i32** %31, i32*** %8
store i32 0, i32* %11
br label %32
32: ; preds = %72, %28
%33 = load i32, i32* %11
%34 = load i32, i32* %7
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %75
36: ; preds = %32
%37 = load i32, i32* %7
%38 = call i8* @calloc(i32 %37, i32 4)
%39 = bitcast i8* %38 to i32*
%40 = load i32**, i32*** %8
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32*, i32** %40, i64 %42
store i32* %39, i32** %43
%44 = load i32*, i32** %5
%45 = call i8* @get_line(i32* %44)
store i8* %45, i8** %6
store i32 0, i32* %10
%46 = load i8*, i8** %6
%47 = call i8* @strtok(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %47, i8** %9
br label %48
48: ; preds = %69, %36
%49 = load i8*, i8** %9
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %71
51: ; preds = %48
%52 = load i32, i32* %10
%53 = load i32, i32* %7
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %68
55: ; preds = %51
%56 = load i8*, i8** %9
%57 = call i64 @strtol(i8* %56, i32* null, i32 10)
%58 = trunc i64 %57 to i32
%59 = load i32**, i32*** %8
%60 = load i32, i32* %11
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i32*, i32** %59, i64 %61
%63 = load i32*, i32** %62
%64 = load i32, i32* %10
%65 = add nsw i32 %64, 1
store i32 %65, i32* %10
%66 = sext i32 %64 to i64
%67 = getelementptr inbounds i32, i32* %63, i64 %66
store i32 %58, i32* %67
br label %68
68: ; preds = %55, %51
br label %69
69: ; preds = %68
%70 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %70, i8** %9
br label %48
71: ; preds = %48
br label %72
72: ; preds = %71
%73 = load i32, i32* %11
%74 = add nsw i32 %73, 1
store i32 %74, i32* %11
br label %32
75: ; preds = %32
%76 = load i32*, i32** %5
%77 = call i32 @fclose(i32* %76)
%78 = load i32*, i32** %4
%79 = icmp ne i32* %78, null
br i1 %79, label %80, label %83
80: ; preds = %75
%81 = load i32, i32* %7
%82 = load i32*, i32** %4
store i32 %81, i32* %82
br label %83
83: ; preds = %80, %75
%84 = load i32**, i32*** %8
ret i32** %84
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [11 x i8] c"read_array\00"
@.str.2 = [18 x i8] c"Cannot open file.\00"
@.str.3 = [21 x i8] c"Invalid file format.\00"
@.str.4 = [2 x i8] c" \00"
define noalias i32** @read_array(i8* nocapture %0, i32* writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @fail(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = tail call i8* @get_line(i32* %3)
%9 = tail call i64 @strtol(i8* nocapture %8, i32* null, i32 10)
%10 = trunc i64 %9 to i32
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = tail call i32 @fail(i32 2, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%14 = tail call i8* @calloc(i32 %10, i32 8)
%15 = bitcast i8* %14 to i32**
br label %22
16: ; preds = %7
%17 = tail call i8* @calloc(i32 %10, i32 8)
%18 = bitcast i8* %17 to i32**
%19 = icmp sgt i32 %10, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = and i64 %9, 4294967295
br label %26
22: ; preds = %49, %12, %16
%23 = phi i32** [ %15, %12 ], [ %18, %16 ], [ %18, %49 ]
%24 = tail call i32 @fclose(i32* %3)
%25 = icmp eq i32* %1, null
br i1 %25, label %53, label %52
26: ; preds = %20, %49
%27 = phi i64 [ 0, %20 ], [ %50, %49 ]
%28 = tail call i8* @calloc(i32 %10, i32 4)
%29 = getelementptr inbounds i32*, i32** %18, i64 %27
%30 = bitcast i32** %29 to i8**
store i8* %28, i8** %30
%31 = tail call i8* @get_line(i32* %3)
%32 = tail call i8* @strtok(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%33 = icmp eq i8* %32, null
%34 = bitcast i8* %28 to i32*
br i1 %33, label %49, label %35
35: ; preds = %26, %45
%36 = phi i32 [ %46, %45 ], [ 0, %26 ]
%37 = phi i8* [ %47, %45 ], [ %32, %26 ]
%38 = icmp slt i32 %36, %10
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = tail call i64 @strtol(i8* nocapture %37, i32* null, i32 10)
%41 = trunc i64 %40 to i32
%42 = add nsw i32 %36, 1
%43 = sext i32 %36 to i64
%44 = getelementptr inbounds i32, i32* %34, i64 %43
store i32 %41, i32* %44
br label %45
45: ; preds = %35, %39
%46 = phi i32 [ %42, %39 ], [ %36, %35 ]
%47 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %35
49: ; preds = %45, %26
%50 = add nuw nsw i64 %27, 1
%51 = icmp eq i64 %50, %21
br i1 %51, label %22, label %26
52: ; preds = %22
store i32 %10, i32* %1
br label %53
53: ; preds = %52, %22
ret i32** %23
}