#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 /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, double *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
double **read_matrix(char *filename, int n) {
  double **matrix = (double **)malloc(sizeof(double *) * n);
  FILE *fp;
  int i = 0;
  int j = 0;
  fp = fopen(filename, "r");
  assert(matrix != NULL);
  if (!fp) {
    printf("És possible que l'arxiu no existeixi\n");
    perror("fopen");
    exit(1);
  }
  for (i = 0; i < n; i++) {
    matrix[i] = (double *)malloc(sizeof(double) * n);
    assert(matrix[i] != NULL);
    for (j = 0; j < n; j++) {
      assert(1 == fscanf(fp, "%lf", &matrix[i][j]));
    }
  }
  fclose(fp);
  return matrix;
}


x86 assembly (clang, -O0):
<read_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1203 <read_matrix+0x83>
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
lea    0xe35(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12bf <read_matrix+0x13f>
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12ac <read_matrix+0x12c>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
movslq -0x28(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
lea    0xdac(%rip),%rsi
call   1050 <fscanf@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1255 <read_matrix+0xd5>
jmp    12b1 <read_matrix+0x131>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    120a <read_matrix+0x8a>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%rbp
lea    0x0(,%r15,8),%edi
mov    %edi,0xc(%rsp)
call   1070 <malloc@plt>
mov    %rax,%r13
lea    0xe51(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbp
xor    %edi,%edi
test   %r13,%r13
setne  %dil
call   1060 <assert@plt>
test   %rbp,%rbp
je     125f <read_matrix+0xdf>
test   %r15d,%r15d
jle    1245 <read_matrix+0xc5>
mov    %r15d,%eax
mov    %rax,0x10(%rsp)
lea    0x0(,%rax,8),%rbx
xor    %r14d,%r14d
lea    0xe18(%rip),%r15
mov    0xc(%rsp),%edi
call   1070 <malloc@plt>
mov    %rax,0x0(%r13,%r14,8)
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
xor    %r12d,%r12d
nop
mov    0x0(%r13,%r14,8),%rdx
add    %r12,%rdx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1050 <fscanf@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1060 <assert@plt>
add    $0x8,%r12
cmp    %r12,%rbx
jne    1210 <read_matrix+0x90>
add    $0x1,%r14
cmp    0x10(%rsp),%r14
jne    11f0 <read_matrix+0x70>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r13,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda6(%rip),%rdi
call   1030 <puts@plt>
lea    0xd90(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [39 x i8] c"\C3\89s possible que l'arxiu no existeixi\0A\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [4 x i8] c"%lf\00"
define double** @read_matrix(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca double**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = sext i32 %9 to i64
%11 = mul i64 8, %10
%12 = trunc i64 %11 to i32
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to double**
store double** %14, double*** %5
store i32 0, i32* %7
store i32 0, i32* %8
%15 = load i8*, i8** %3
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load double**, double*** %5
%18 = icmp ne double** %17, null
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i32*, i32** %6
%22 = icmp ne i32* %21, null
br i1 %22, label %27, label %23
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %2
store i32 0, i32* %7
br label %28
28: ; preds = %73, %27
%29 = load i32, i32* %7
%30 = load i32, i32* %4
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %76
32: ; preds = %28
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = mul i64 8, %34
%36 = trunc i64 %35 to i32
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to double*
%39 = load double**, double*** %5
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double*, double** %39, i64 %41
store double* %38, double** %42
%43 = load double**, double*** %5
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds double*, double** %43, i64 %45
%47 = load double*, double** %46
%48 = icmp ne double* %47, null
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
store i32 0, i32* %8
br label %51
51: ; preds = %69, %32
%52 = load i32, i32* %8
%53 = load i32, i32* %4
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %72
55: ; preds = %51
%56 = load i32*, i32** %6
%57 = load double**, double*** %5
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds double*, double** %57, i64 %59
%61 = load double*, double** %60
%62 = load i32, i32* %8
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds double, double* %61, i64 %63
%65 = call i32 @fscanf(i32* %56, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double* %64)
%66 = icmp eq i32 1, %65
%67 = zext i1 %66 to i32
%68 = call i32 @assert(i32 %67)
br label %69
69: ; preds = %55
%70 = load i32, i32* %8
%71 = add nsw i32 %70, 1
store i32 %71, i32* %8
br label %51
72: ; preds = %51
br label %73
73: ; preds = %72
%74 = load i32, i32* %7
%75 = add nsw i32 %74, 1
store i32 %75, i32* %7
br label %28
76: ; preds = %28
%77 = load i32*, i32** %6
%78 = call i32 @fclose(i32* %77)
%79 = load double**, double*** %5
ret double** %79
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [4 x i8] c"%lf\00"
@str = [38 x i8] c"\C3\89s possible que l'arxiu no existeixi\00"
define double** @read_matrix(i8* nocapture %0, i32 %1) {
%3 = shl i32 %1, 3
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to double**
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp ne i64 %4, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp eq i32* %6, null
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = icmp sgt i32 %1, 0
br i1 %12, label %13, label %40
13: ; preds = %11
%14 = zext i32 %1 to i64
br label %19
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %37, %13
%20 = phi i64 [ 0, %13 ], [ %38, %37 ]
%21 = tail call i64 @malloc(i32 %3)
%22 = inttoptr i64 %21 to double*
%23 = getelementptr inbounds double*, double** %5, i64 %20
store double* %22, double** %23
%24 = icmp ne i64 %21, 0
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
br label %27
27: ; preds = %19, %27
%28 = phi i64 [ 0, %19 ], [ %35, %27 ]
%29 = load double*, double** %23
%30 = getelementptr inbounds double, double* %29, i64 %28
%31 = tail call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double* %30)
%32 = icmp eq i32 %31, 1
%33 = zext i1 %32 to i32
%34 = tail call i32 @assert(i32 %33)
%35 = add nuw nsw i64 %28, 1
%36 = icmp eq i64 %35, %14
br i1 %36, label %37, label %27
37: ; preds = %27
%38 = add nuw nsw i64 %20, 1
%39 = icmp eq i64 %38, %14
br i1 %39, label %40, label %19
40: ; preds = %37, %11
%41 = tail call i32 @fclose(i32* %6)
ret double** %5
}