#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*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, ...);
float *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
float *load_matrix(char *filename, int *n, int *m) {
  float *matrix;
  FILE *in;
  int n_row, n_cols;
  int i;
  int j;
  if (!(in = fopen(filename, "r"))) {
    printf("Problem opening file %s\n", filename);
    exit(1);
  }
  fscanf(in, "%d %d\n", &n_row, &n_cols);
  matrix = malloc(n_row * n_cols * sizeof(float));
  for (i = 0; i < n_row; i++) {
    for (j = 0; j < n_cols; j++) {
      fscanf(in, "%f", &matrix[i * n_cols + j]);
    }
  }
  *n = n_row;
  *m = n_cols;
  return matrix;
}


x86 assembly (clang, -O0):
<load_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    119c <load_matrix+0x4c>
mov    -0x8(%rbp),%rsi
lea    0xe75(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    0xe74(%rip),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    -0x2c(%rbp),%eax
imul   -0x30(%rbp),%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    123e <load_matrix+0xee>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    122b <load_matrix+0xdb>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x34(%rbp),%eax
imul   -0x30(%rbp),%eax
add    -0x38(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11e8 <load_matrix+0x98>
jmp    1230 <load_matrix+0xe0>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d5 <load_matrix+0x85>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     122c <load_matrix+0xdc>
mov    %rax,%r12
mov    %rbx,0x8(%rsp)
mov    %rbp,0x10(%rsp)
lea    0xe8b(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
mov    %rax,%rdi
xor    %eax,%eax
call   1040 <fscanf@plt>
mov    0x4(%rsp),%ebx
mov    (%rsp),%ebp
mov    %ebx,%edi
imul   %ebp,%edi
shl    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
jle    120c <load_matrix+0xbc>
lea    0xe5e(%rip),%r13
xor    %r14d,%r14d
jmp    11d9 <load_matrix+0x89>
nopl   0x0(%rax)
add    $0x1,%r14d
cmp    %ebx,%r14d
jge    120c <load_matrix+0xbc>
test   %ebp,%ebp
jle    11d0 <load_matrix+0x80>
xor    %ebx,%ebx
nop
imul   %r14d,%ebp
movslq %ebp,%rax
add    %rbx,%rax
lea    (%r15,%rax,4),%rdx
mov    %r12,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1040 <fscanf@plt>
mov    (%rsp),%ebp
add    $0x1,%rbx
cmp    %ebp,%ebx
jl     11e0 <load_matrix+0x90>
mov    0x4(%rsp),%ebx
jmp    11d0 <load_matrix+0x80>
mov    0x8(%rsp),%rax
mov    %ebx,(%rax)
mov    0x10(%rsp),%rax
mov    %ebp,(%rax)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcf(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [25 x i8] c"Problem opening file %s\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [3 x i8] c"%f\00"
define float* @load_matrix(i8* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca float*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%13 = load i8*, i8** %4
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %20, label %16
16: ; preds = %3
%17 = load i8*, i8** %4
%18 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %3
%21 = load i32*, i32** %8
%22 = call i32 (i32*, i8*, ...) @fscanf(i32* %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %9, i32* %10)
%23 = load i32, i32* %9
%24 = load i32, i32* %10
%25 = mul nsw i32 %23, %24
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 4
%28 = trunc i64 %27 to i32
%29 = call float* @malloc(i32 %28)
store float* %29, float** %7
store i32 0, i32* %11
br label %30
30: ; preds = %54, %20
%31 = load i32, i32* %11
%32 = load i32, i32* %9
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %57
34: ; preds = %30
store i32 0, i32* %12
br label %35
35: ; preds = %50, %34
%36 = load i32, i32* %12
%37 = load i32, i32* %10
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %53
39: ; preds = %35
%40 = load i32*, i32** %8
%41 = load float*, float** %7
%42 = load i32, i32* %11
%43 = load i32, i32* %10
%44 = mul nsw i32 %42, %43
%45 = load i32, i32* %12
%46 = add nsw i32 %44, %45
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds float, float* %41, i64 %47
%49 = call i32 (i32*, i8*, ...) @fscanf(i32* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), float* %48)
br label %50
50: ; preds = %39
%51 = load i32, i32* %12
%52 = add nsw i32 %51, 1
store i32 %52, i32* %12
br label %35
53: ; preds = %35
br label %54
54: ; preds = %53
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
br label %30
57: ; preds = %30
%58 = load i32, i32* %9
%59 = load i32*, i32** %5
store i32 %58, i32* %59
%60 = load i32, i32* %10
%61 = load i32*, i32** %6
store i32 %60, i32* %61
%62 = load float*, float** %7
ret float* %62
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [25 x i8] c"Problem opening file %s\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [3 x i8] c"%f\00"
define float* @load_matrix(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %3
%14 = call i32 (i32*, i8*, ...) @fscanf(i32* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %4, i32* %5)
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = shl i32 %15, 2
%18 = mul i32 %17, %16
%19 = call float* @malloc(i32 %18)
%20 = icmp sgt i32 %15, 0
br i1 %20, label %21, label %44
21: ; preds = %13, %39
%22 = phi i32 [ %40, %39 ], [ %15, %13 ]
%23 = phi i32 [ %41, %39 ], [ %16, %13 ]
%24 = phi i32 [ %42, %39 ], [ 0, %13 ]
%25 = icmp sgt i32 %23, 0
br i1 %25, label %26, label %39
26: ; preds = %21, %26
%27 = phi i32 [ %35, %26 ], [ %23, %21 ]
%28 = phi i32 [ %34, %26 ], [ 0, %21 ]
%29 = mul nsw i32 %27, %24
%30 = add nsw i32 %29, %28
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds float, float* %19, i64 %31
%33 = call i32 (i32*, i8*, ...) @fscanf(i32* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), float* %32)
%34 = add nuw nsw i32 %28, 1
%35 = load i32, i32* %5
%36 = icmp slt i32 %34, %35
br i1 %36, label %26, label %37
37: ; preds = %26
%38 = load i32, i32* %4
br label %39
39: ; preds = %37, %21
%40 = phi i32 [ %38, %37 ], [ %22, %21 ]
%41 = phi i32 [ %35, %37 ], [ %23, %21 ]
%42 = add nuw nsw i32 %24, 1
%43 = icmp slt i32 %42, %40
br i1 %43, label %21, label %44
44: ; preds = %39, %13
%45 = phi i32 [ %16, %13 ], [ %41, %39 ]
%46 = phi i32 [ %15, %13 ], [ %40, %39 ]
store i32 %46, i32* %1
store i32 %45, i32* %2
ret float* %19
}