#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 *, ...);
int /*<<< orphan*/ printf(char *, double);
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]);
        printf("read %f\n", 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    0xea2(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1197 <read_mat+0x57>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1040 <fscanf@plt>
jmp    124d <read_mat+0x10d>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1040 <fscanf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%eax
jge    1248 <read_mat+0x108>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    1235 <read_mat+0xf5>
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    0xe14(%rip),%rsi
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x30(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xdf5(%rip),%rdi
call   1030 <printf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11ce <read_mat+0x8e>
jmp    123a <read_mat+0xfa>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b8 <read_mat+0x78>
jmp    124d <read_mat+0x10d>
mov    -0x28(%rbp),%rdi
call   1050 <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,%rbx
mov    %rsi,%r13
mov    %rdi,%r14
lea    0xe9f(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbp
lea    0xe96(%rip),%rsi
mov    %rax,%rdi
mov    %r14,(%rsp)
mov    %r14,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1040 <fscanf@plt>
test   %r15d,%r15d
je     123a <read_mat+0xfa>
mov    (%rsp),%rax
mov    (%rax),%ecx
test   %ecx,%ecx
jle    123a <read_mat+0xfa>
mov    0x0(%r13),%eax
lea    0xe66(%rip),%r14
lea    0xe63(%rip),%r12
xor    %r15d,%r15d
mov    %rbp,0x8(%rsp)
mov    %r13,0x10(%rsp)
jmp    11dc <read_mat+0x9c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r15
movslq %ecx,%rdx
cmp    %rdx,%r15
jge    123a <read_mat+0xfa>
test   %eax,%eax
jle    11d0 <read_mat+0x90>
xor    %r13d,%r13d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx,%r15,8),%rdx
add    %r13,%rdx
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <fscanf@plt>
mov    (%rbx,%r15,8),%rax
movsd  (%rax,%r13,1),%xmm0
mov    %r12,%rdi
call   1030 <printf@plt>
add    $0x1,%rbp
mov    0x10(%rsp),%rax
movslq (%rax),%rax
add    $0x8,%r13
cmp    %rax,%rbp
jl     11f0 <read_mat+0xb0>
mov    (%rsp),%rcx
mov    (%rcx),%ecx
mov    0x8(%rsp),%rbp
jmp    11d0 <read_mat+0x90>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <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"
@.str.4 = [9 x i8] c"read %f\0A\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 %65
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 = %61, %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 %64
30: ; preds = %25
store i32 0, i32* %11
br label %31
31: ; preds = %57, %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 %60
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)
%47 = load double**, double*** %7
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds double*, double** %47, i64 %49
%51 = load double*, double** %50
%52 = load i32, i32* %11
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds double, double* %51, i64 %53
%55 = load double, double* %54
%56 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), double %55)
br label %57
57: ; preds = %36
%58 = load i32, i32* %11
%59 = add nsw i32 %58, 1
store i32 %59, i32* %11
br label %31
60: ; preds = %31
br label %61
61: ; preds = %60
%62 = load i32, i32* %10
%63 = add nsw i32 %62, 1
store i32 %63, i32* %10
br label %25
64: ; preds = %25
br label %65
65: ; preds = %64, %15
%66 = load i32*, i32** %9
%67 = call i32 @close(i32* %66)
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"
@.str.4 = [9 x i8] c"read %f\0A\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 %41, label %8
8: ; preds = %4
%9 = load i32, i32* %0
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %41
11: ; preds = %8
%12 = load i32, i32* %1
br label %13
13: ; preds = %11, %35
%14 = phi i32 [ %9, %11 ], [ %36, %35 ]
%15 = phi i32 [ %12, %11 ], [ %37, %35 ]
%16 = phi i64 [ 0, %11 ], [ %38, %35 ]
%17 = icmp sgt i32 %15, 0
br i1 %17, label %18, label %35
18: ; preds = %13
%19 = getelementptr inbounds double*, double** %2, i64 %16
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ 0, %18 ], [ %29, %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 = load double*, double** %19
%26 = getelementptr inbounds double, double* %25, i64 %21
%27 = load double, double* %26
%28 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), double %27)
%29 = add nuw nsw i64 %21, 1
%30 = load i32, i32* %1
%31 = sext i32 %30 to i64
%32 = icmp slt i64 %29, %31
br i1 %32, label %20, label %33
33: ; preds = %20
%34 = load i32, i32* %0
br label %35
35: ; preds = %33, %13
%36 = phi i32 [ %34, %33 ], [ %14, %13 ]
%37 = phi i32 [ %30, %33 ], [ %15, %13 ]
%38 = add nuw nsw i64 %16, 1
%39 = sext i32 %36 to i64
%40 = icmp slt i64 %38, %39
br i1 %40, label %13, label %41
41: ; preds = %35, %4, %8
%42 = tail call i32 @close(i32* %5)
ret void
}