#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*/ close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, ...);
void read_mat(int *n, int *m, double **matrix, int count) {
  FILE *fp;
  fp = fopen("matrix.dat", "r");
  if (count == 0) {
    fscanf(fp, "%d %d", n, m);
  } else {
    int i, j;
    fscanf(fp, "%d %d", n, m);
    for (i = 0; i < *n; i++) {
      for (j = 0; j < *m; j++) {
        fscanf(fp, "%lf", &matrix[i][j]);
      }
    }
  }
  close(fp);
}


x86 assembly (clang, -O0):
<read_mat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    0xeb2(%rip),%rdi
lea    0xeb6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1187 <read_mat+0x57>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe92(%rip),%rsi
mov    $0x0,%al
call   1030 <fscanf@plt>
jmp    121c <read_mat+0xec>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe73(%rip),%rsi
mov    $0x0,%al
call   1030 <fscanf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%eax
jge    1217 <read_mat+0xe7>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    1204 <read_mat+0xd4>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
movslq -0x30(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1030 <fscanf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11be <read_mat+0x8e>
jmp    1209 <read_mat+0xd9>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a8 <read_mat+0x78>
jmp    121c <read_mat+0xec>
mov    -0x28(%rbp),%rdi
call   1040 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_mat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,%rbp
lea    0xeaf(%rip),%rdi
lea    0xeb3(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0xea6(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,0x8(%rsp)
mov    %rbp,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1030 <fscanf@plt>
test   %r15d,%r15d
je     11fa <read_mat+0xca>
mov    0x8(%rsp),%rax
mov    (%rax),%ecx
test   %ecx,%ecx
jle    11fa <read_mat+0xca>
mov    (%r12),%eax
lea    0xe7c(%rip),%r14
xor    %ebp,%ebp
mov    %rbx,0x10(%rsp)
jmp    11ac <read_mat+0x7c>
add    $0x1,%rbp
movslq %ecx,%rdx
cmp    %rdx,%rbp
jge    11fa <read_mat+0xca>
test   %eax,%eax
jle    11a0 <read_mat+0x70>
xor    %r15d,%r15d
mov    %r12,%rbx
mov    %r13,%r12
xor    %r13d,%r13d
nopl   0x0(%rax)
mov    (%r12,%rbp,8),%rdx
add    %r15,%rdx
mov    0x10(%rsp),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <fscanf@plt>
add    $0x1,%r13
movslq (%rbx),%rax
add    $0x8,%r15
cmp    %rax,%r13
jl     11c0 <read_mat+0x90>
mov    0x8(%rsp),%rcx
mov    (%rcx),%ecx
mov    %r12,%r13
mov    %rbx,%r12
mov    0x10(%rsp),%rbx
jmp    11a0 <read_mat+0x70>
mov    %rbx,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>

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

LLVM IR (-O2):
@.str = [11 x i8] c"matrix.dat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"%d %d\00"
@.str.3 = [4 x i8] c"%lf\00"
define void @read_mat(i32* %0, i32* %1, double** nocapture %2, i32 %3) {
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32 %3, 0
%7 = tail call i32 (i32*, i8*, ...) @fscanf(i32* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %0, i32* %1)
br i1 %6, label %37, label %8
8: ; preds = %4
%9 = load i32, i32* %0
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %37
11: ; preds = %8
%12 = load i32, i32* %1
br label %13
13: ; preds = %11, %31
%14 = phi i32 [ %9, %11 ], [ %32, %31 ]
%15 = phi i32 [ %12, %11 ], [ %33, %31 ]
%16 = phi i64 [ 0, %11 ], [ %34, %31 ]
%17 = icmp sgt i32 %15, 0
br i1 %17, label %18, label %31
18: ; preds = %13
%19 = getelementptr inbounds double*, double** %2, i64 %16
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ 0, %18 ], [ %25, %20 ]
%22 = load double*, double** %19
%23 = getelementptr inbounds double, double* %22, i64 %21
%24 = tail call i32 (i32*, i8*, ...) @fscanf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double* %23)
%25 = add nuw nsw i64 %21, 1
%26 = load i32, i32* %1
%27 = sext i32 %26 to i64
%28 = icmp slt i64 %25, %27
br i1 %28, label %20, label %29
29: ; preds = %20
%30 = load i32, i32* %0
br label %31
31: ; preds = %29, %13
%32 = phi i32 [ %30, %29 ], [ %14, %13 ]
%33 = phi i32 [ %26, %29 ], [ %15, %13 ]
%34 = add nuw nsw i64 %16, 1
%35 = sext i32 %32 to i64
%36 = icmp slt i64 %34, %35
br i1 %36, label %13, label %37
37: ; preds = %31, %4, %8
%38 = tail call i32 @close(i32* %5)
ret void
}