#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 **alloc_square_matrix(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ stderr;
int **read_square_matrix(const char *file, int *n) {
  FILE *fp;
  fp = fopen(file, "r");
  if (fp == NULL) {
    fprintf(stderr, "Error while reading the matrix.\n");
    exit(1);
  }
  fscanf(fp, "%d\n", n);
  int **m = alloc_square_matrix(*n);
  for (int i = 0; i < *n; ++i) {
    for (int j = 0; j < *n; ++j) {
      fscanf(fp, "%d", &m[i][j]);
    }
  }
  return m;
}


x86 assembly (clang, -O0):
<read_square_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119e <read_square_matrix+0x4e>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe76(%rip),%rsi
call   1030 <fscanf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <alloc_square_matrix@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    1235 <read_square_matrix+0xe5>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    1222 <read_square_matrix+0xd2>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
movslq -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe18(%rip),%rsi
call   1030 <fscanf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11de <read_square_matrix+0x8e>
jmp    1227 <read_square_matrix+0xd7>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c8 <read_square_matrix+0x78>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_square_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
lea    0xe98(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     120e <read_square_matrix+0xbe>
lea    0xea6(%rip),%rsi
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
mov    %rbx,%rdx
call   1030 <fscanf@plt>
mov    (%rbx),%edi
call   1040 <alloc_square_matrix@plt>
mov    %rbx,0x10(%rsp)
mov    (%rbx),%edx
test   %edx,%edx
jle    11ff <read_square_matrix+0xaf>
xor    %r15d,%r15d
lea    0xe7e(%rip),%r14
mov    0x8(%rsp),%r13
jmp    11bc <read_square_matrix+0x6c>
movslq %edx,%rcx
add    $0x1,%r15
cmp    %rcx,%r15
jge    11ff <read_square_matrix+0xaf>
test   %edx,%edx
jle    11b0 <read_square_matrix+0x60>
xor    %ebp,%ebp
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,%rbx
mov    (%rax,%r15,8),%rdx
add    %rbp,%rdx
mov    %r13,%rdi
mov    %r14,%rsi
call   1030 <fscanf@plt>
mov    %rbx,%rax
add    $0x1,%r12
mov    0x10(%rsp),%rcx
movslq (%rcx),%rdx
add    $0x4,%rbp
cmp    %rdx,%r12
jl     11d0 <read_square_matrix+0x80>
jmp    11b0 <read_square_matrix+0x60>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"Error while reading the matrix.\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [3 x i8] c"%d\00"
define i32** @read_square_matrix(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32**
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32*, i32** %5
%19 = load i32*, i32** %4
%20 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %19)
%21 = load i32*, i32** %4
%22 = load i32, i32* %21
%23 = call i32** @alloc_square_matrix(i32 %22)
store i32** %23, i32*** %6
store i32 0, i32* %7
br label %24
24: ; preds = %50, %17
%25 = load i32, i32* %7
%26 = load i32*, i32** %4
%27 = load i32, i32* %26
%28 = icmp slt i32 %25, %27
br i1 %28, label %29, label %53
29: ; preds = %24
store i32 0, i32* %8
br label %30
30: ; preds = %46, %29
%31 = load i32, i32* %8
%32 = load i32*, i32** %4
%33 = load i32, i32* %32
%34 = icmp slt i32 %31, %33
br i1 %34, label %35, label %49
35: ; preds = %30
%36 = load i32*, i32** %5
%37 = load i32**, i32*** %6
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32*, i32** %37, i64 %39
%41 = load i32*, i32** %40
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = call i32 @fscanf(i32* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %44)
br label %46
46: ; preds = %35
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
store i32 %48, i32* %8
br label %30
49: ; preds = %30
br label %50
50: ; preds = %49
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 1
store i32 %52, i32* %7
br label %24
53: ; preds = %24
%54 = load i32**, i32*** %6
ret i32** %54
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"Error while reading the matrix.\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [3 x i8] c"%d\00"
define i32** @read_square_matrix(i8* nocapture %0, i32* %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 %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = tail call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%11 = load i32, i32* %1
%12 = tail call i32** @alloc_square_matrix(i32 %11)
%13 = load i32, i32* %1
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %9, %22
%16 = phi i32 [ %23, %22 ], [ %13, %9 ]
%17 = phi i64 [ %25, %22 ], [ 0, %9 ]
%18 = icmp sgt i32 %16, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = getelementptr inbounds i32*, i32** %12, i64 %17
br label %27
21: ; preds = %22, %9
ret i32** %12
22: ; preds = %27, %15
%23 = phi i32 [ %16, %15 ], [ %33, %27 ]
%24 = sext i32 %23 to i64
%25 = add nuw nsw i64 %17, 1
%26 = icmp slt i64 %25, %24
br i1 %26, label %15, label %21
27: ; preds = %19, %27
%28 = phi i64 [ 0, %19 ], [ %32, %27 ]
%29 = load i32*, i32** %20
%30 = getelementptr inbounds i32, i32* %29, i64 %28
%31 = tail call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %30)
%32 = add nuw nsw i64 %28, 1
%33 = load i32, i32* %1
%34 = sext i32 %33 to i64
%35 = icmp slt i64 %32, %34
br i1 %35, label %27, label %22
}