#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sscanf(char *, char *, ...);
int readMat(double *mat, const char *filename) {
  int i;
  int matsize;
  char buf[1024];
  FILE *fp;
  char *result;
  fp = fopen(filename, "r");
  if (fp == NULL) {
    printf("ERROR : file open failure\n");
    exit(-1);
  }
  result = fgets(buf, sizeof(buf), fp);
  if (result == NULL) {
    printf("ERROR : file read failure\n");
    exit(-1);
  }
  sscanf(buf, "%d", &matsize);
  if (matsize <= 0) {
    printf("ERROR : file format failure\n");
    exit(-1);
  }
  for (i = 0; i < matsize * matsize; i++) {
    result = fgets(buf, sizeof(buf), fp);
    if (result == NULL) {
      printf("ERROR : file read failure\n");
      exit(-1);
    }
    sscanf(buf, "%lf", &(mat[i]));
  }
  fclose(fp);
  return (matsize);
}


x86 assembly (clang, -O0):
<readMat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    11ae <readMat+0x4e>
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    11f1 <readMat+0x91>
lea    0xe3b(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    -0x420(%rbp),%rdi
lea    0xe39(%rip),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1060 <sscanf@plt>
cmpl   $0x0,-0x18(%rbp)
jg     122a <readMat+0xca>
lea    0xe20(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
imul   -0x18(%rbp),%ecx
cmp    %ecx,%eax
jge    12b8 <readMat+0x158>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    1286 <readMat+0x126>
lea    0xda6(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1060 <sscanf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1231 <readMat+0xd1>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readMat>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rax
mov    %rdi,%r13
lea    0xe83(%rip),%rsi
mov    %rax,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     124c <readMat+0xec>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1236 <readMat+0xd6>
lea    0xe4f(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
xor    %eax,%eax
call   1060 <sscanf@plt>
cmpl   $0x0,0xc(%rsp)
jle    1255 <readMat+0xf5>
lea    0x10(%rsp),%r15
lea    0xe2a(%rip),%r12
xor    %ebx,%ebx
nopl   (%rax)
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1236 <readMat+0xd6>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <sscanf@plt>
add    $0x1,%rbx
mov    0xc(%rsp),%eax
imul   %eax,%eax
add    $0x8,%r13
cmp    %rax,%rbx
jb     11e0 <readMat+0x80>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xdec(%rip),%rdi
jmp    123d <readMat+0xdd>
lea    0xdad(%rip),%rdi
jmp    123d <readMat+0xdd>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.5 = [4 x i8] c"%lf\00"
@str.6 = [28 x i8] c"ERROR : file format failure\00"
@str.7 = [26 x i8] c"ERROR : file read failure\00"
@str.8 = [26 x i8] c"ERROR : file open failure\00"
define i32 @readMat(double* %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = call i8* @fgets(i8* %6, i32 1024, i32* %7)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %12
%19 = call i32 (i8*, i8*, ...) @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %18, %32
%26 = phi i64 [ %35, %32 ], [ 0, %18 ]
%27 = call i8* @fgets(i8* %6, i32 1024, i32* %7)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %25
%33 = getelementptr inbounds double, double* %0, i64 %26
%34 = call i32 (i8*, i8*, ...) @sscanf(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), double* %33)
%35 = add nuw nsw i64 %26, 1
%36 = load i32, i32* %3
%37 = mul nsw i32 %36, %36
%38 = zext i32 %37 to i64
%39 = icmp ult i64 %35, %38
br i1 %39, label %25, label %40
40: ; preds = %32
%41 = call i32 @fclose(i32* %7)
%42 = load i32, i32* %3
ret i32 %42
}