#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, ...);
void *malloc(int);
double **load_matrix(char *fname, int *N, int *dim) {
  FILE *f;
  double **x;
  int i, j, k;
  double tmp;
  double sum;
  *N = 0;
  *dim = 0;
  k = 0;
  f = fopen(fname, "r");
  while (!feof(f)) {
    i = fscanf(f, "%*[^\n]%*[\n]");
    k++;
  }
  fclose(f);
  *N = k;
  k = 0;
  f = fopen(fname, "r");
  while (!feof(f)) {
    i = fscanf(f, "%lg", &tmp);
    k++;
  }
  fclose(f);
  *dim = (k / *N);
  f = fopen(fname, "r");
  x = malloc(*N * sizeof(double *));
  for (i = 0; i < *N; i++) {
    x[i] = malloc((*dim + 1) * sizeof(double));
    sum = 0;
    for (j = 0; j < *dim; j++) {
      k = fscanf(f, "%lg", &tmp);
      x[i][j] = tmp;
      sum = sum + tmp;
    }
    x[i][*dim] = sum;
  }
  fclose(f);
  return x;
}


x86 assembly (clang, -O0):
<load_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b1 <load_matrix+0x61>
jmp    11d4 <load_matrix+0x84>
mov    -0x20(%rbp),%rdi
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1193 <load_matrix+0x43>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x34(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe08(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    121f <load_matrix+0xcf>
jmp    1246 <load_matrix+0xf6>
mov    -0x20(%rbp),%rdi
lea    0xde4(%rip),%rsi
lea    -0x40(%rbp),%rdx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1201 <load_matrix+0xb1>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    -0x10(%rbp),%rcx
cltd
idivl  (%rcx)
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
lea    0xd94(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    135f <load_matrix+0x20f>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jge    1334 <load_matrix+0x1e4>
mov    -0x20(%rbp),%rdi
lea    0xd1f(%rip),%rsi
lea    -0x40(%rbp),%rdx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0x34(%rbp)
movsd  -0x40(%rbp),%xmm0
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x30(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x48(%rbp),%xmm0
addsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12d5 <load_matrix+0x185>
movsd  -0x48(%rbp),%xmm0
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1292 <load_matrix+0x142>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r15
mov    %rsi,0x10(%rsp)
movl   $0x0,(%rsi)
mov    %rdx,0x8(%rsp)
movl   $0x0,(%rdx)
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1050 <feof@plt>
xor    %ebx,%ebx
mov    $0x0,%ebp
test   %eax,%eax
jne    11cc <load_matrix+0x7c>
lea    0xe62(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <fscanf@plt>
add    $0x1,%ebp
mov    %r13,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     11b0 <load_matrix+0x60>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    0x10(%rsp),%rax
mov    %ebp,(%rax)
lea    0xe1e(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    122f <load_matrix+0xdf>
lea    0xe0e(%rip),%r14
lea    0x28(%rsp),%r13
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1040 <fscanf@plt>
add    $0x1,%ebx
mov    %rbp,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     1210 <load_matrix+0xc0>
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
cltd
mov    0x10(%rsp),%rbp
idivl  0x0(%rbp)
mov    0x8(%rsp),%rcx
mov    %eax,(%rcx)
lea    0xdb0(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
mov    %rax,%r13
mov    0x0(%rbp),%r12d
lea    0x0(,%r12,8),%edi
call   1060 <malloc@plt>
mov    %rax,0x20(%rsp)
test   %r12d,%r12d
jle    1342 <load_matrix+0x1f2>
mov    0x8(%rsp),%rax
mov    (%rax),%r15d
lea    0xd85(%rip),%rbp
xor    %r14d,%r14d
jmp    12ab <load_matrix+0x15b>
xchg   %ax,%ax
xorps  %xmm0,%xmm0
movslq %r15d,%rax
movsd  %xmm0,(%rdx,%rax,8)
add    $0x1,%r14
movslq %r12d,%rax
cmp    %rax,%r14
jge    1342 <load_matrix+0x1f2>
lea    0x8(,%r15,8),%edi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    0x20(%rsp),%rax
mov    %rdx,(%rax,%r14,8)
test   %r15d,%r15d
jle    1290 <load_matrix+0x140>
mov    %r14,0x30(%rsp)
xorps  %xmm0,%xmm0
mov    %r13,%r14
mov    0x8(%rsp),%r13
xor    %r12d,%r12d
nopl   0x0(%rax)
movsd  %xmm0,0x18(%rsp)
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rdx,%rbx
lea    0x28(%rsp),%rdx
xor    %eax,%eax
call   1040 <fscanf@plt>
mov    %rbx,%rdx
movsd  0x28(%rsp),%xmm0
movsd  %xmm0,(%rbx,%r12,8)
movsd  0x18(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x18(%rsp)
movsd  0x18(%rsp),%xmm0
add    $0x1,%r12
movslq 0x0(%r13),%r15
cmp    %r15,%r12
jl     12e0 <load_matrix+0x190>
mov    0x10(%rsp),%rax
mov    (%rax),%r12d
mov    %r14,%r13
mov    0x30(%rsp),%r14
jmp    1293 <load_matrix+0x143>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    0x20(%rsp),%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"%*[^\0A]%*[\0A]\00"
@.str.2 = [4 x i8] c"%lg\00"
define double** @load_matrix(i8* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca double**
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca double
%13 = alloca double
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%14 = load i32*, i32** %5
store i32 0, i32* %14
%15 = load i32*, i32** %6
store i32 0, i32* %15
store i32 0, i32* %11
%16 = load i8*, i8** %4
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %7
br label %18
18: ; preds = %23, %3
%19 = load i32*, i32** %7
%20 = call i32 @feof(i32* %19)
%21 = icmp ne i32 %20, 0
%22 = xor i1 %21, true
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32*, i32** %7
%25 = call i32 (i32*, i8*, ...) @fscanf(i32* %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 %25, i32* %9
%26 = load i32, i32* %11
%27 = add nsw i32 %26, 1
store i32 %27, i32* %11
br label %18
28: ; preds = %18
%29 = load i32*, i32** %7
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* %11
%32 = load i32*, i32** %5
store i32 %31, i32* %32
store i32 0, i32* %11
%33 = load i8*, i8** %4
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %34, i32** %7
br label %35
35: ; preds = %40, %28
%36 = load i32*, i32** %7
%37 = call i32 @feof(i32* %36)
%38 = icmp ne i32 %37, 0
%39 = xor i1 %38, true
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i32*, i32** %7
%42 = call i32 (i32*, i8*, ...) @fscanf(i32* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double* %12)
store i32 %42, i32* %9
%43 = load i32, i32* %11
%44 = add nsw i32 %43, 1
store i32 %44, i32* %11
br label %35
45: ; preds = %35
%46 = load i32*, i32** %7
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* %11
%49 = load i32*, i32** %5
%50 = load i32, i32* %49
%51 = sdiv i32 %48, %50
%52 = load i32*, i32** %6
store i32 %51, i32* %52
%53 = load i8*, i8** %4
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %54, i32** %7
%55 = load i32*, i32** %5
%56 = load i32, i32* %55
%57 = sext i32 %56 to i64
%58 = mul i64 %57, 8
%59 = trunc i64 %58 to i32
%60 = call i8* @malloc(i32 %59)
%61 = bitcast i8* %60 to double**
store double** %61, double*** %8
store i32 0, i32* %9
br label %62
62: ; preds = %114, %45
%63 = load i32, i32* %9
%64 = load i32*, i32** %5
%65 = load i32, i32* %64
%66 = icmp slt i32 %63, %65
br i1 %66, label %67, label %117
67: ; preds = %62
%68 = load i32*, i32** %6
%69 = load i32, i32* %68
%70 = add nsw i32 %69, 1
%71 = sext i32 %70 to i64
%72 = mul i64 %71, 8
%73 = trunc i64 %72 to i32
%74 = call i8* @malloc(i32 %73)
%75 = bitcast i8* %74 to double*
%76 = load double**, double*** %8
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds double*, double** %76, i64 %78
store double* %75, double** %79
store double 0.000000e+00, double* %13
store i32 0, i32* %10
br label %80
80: ; preds = %100, %67
%81 = load i32, i32* %10
%82 = load i32*, i32** %6
%83 = load i32, i32* %82
%84 = icmp slt i32 %81, %83
br i1 %84, label %85, label %103
85: ; preds = %80
%86 = load i32*, i32** %7
%87 = call i32 (i32*, i8*, ...) @fscanf(i32* %86, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double* %12)
store i32 %87, i32* %11
%88 = load double, double* %12
%89 = load double**, double*** %8
%90 = load i32, i32* %9
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds double*, double** %89, i64 %91
%93 = load double*, double** %92
%94 = load i32, i32* %10
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds double, double* %93, i64 %95
store double %88, double* %96
%97 = load double, double* %13
%98 = load double, double* %12
%99 = fadd double %97, %98
store double %99, double* %13
br label %100
100: ; preds = %85
%101 = load i32, i32* %10
%102 = add nsw i32 %101, 1
store i32 %102, i32* %10
br label %80
103: ; preds = %80
%104 = load double, double* %13
%105 = load double**, double*** %8
%106 = load i32, i32* %9
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds double*, double** %105, i64 %107
%109 = load double*, double** %108
%110 = load i32*, i32** %6
%111 = load i32, i32* %110
%112 = sext i32 %111 to i64
%113 = getelementptr inbounds double, double* %109, i64 %112
store double %104, double* %113
br label %114
114: ; preds = %103
%115 = load i32, i32* %9
%116 = add nsw i32 %115, 1
store i32 %116, i32* %9
br label %62
117: ; preds = %62
%118 = load i32*, i32** %7
%119 = call i32 @fclose(i32* %118)
%120 = load double**, double*** %8
ret double** %120
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"%*[^\0A]%*[\0A]\00"
@.str.2 = [4 x i8] c"%lg\00"
define noalias double** @load_matrix(i8* nocapture %0, i32* nocapture %1, i32* nocapture %2) {
%4 = alloca double
%5 = bitcast double* %4 to i8*
store i32 0, i32* %1
store i32 0, i32* %2
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @feof(i32* %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %3, %9
%10 = phi i32 [ %12, %9 ], [ 0, %3 ]
%11 = tail call i32 (i32*, i8*, ...) @fscanf(i32* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%12 = add nuw nsw i32 %10, 1
%13 = tail call i32 @feof(i32* %6)
%14 = icmp eq i32 %13, 0
br i1 %14, label %9, label %15
15: ; preds = %9, %3
%16 = phi i32 [ 0, %3 ], [ %12, %9 ]
%17 = tail call i32 @fclose(i32* %6)
store i32 %16, i32* %1
%18 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @feof(i32* %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %15, %21
%22 = phi i32 [ %24, %21 ], [ 0, %15 ]
%23 = call i32 (i32*, i8*, ...) @fscanf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double* %4)
%24 = add nuw nsw i32 %22, 1
%25 = call i32 @feof(i32* %18)
%26 = icmp eq i32 %25, 0
br i1 %26, label %21, label %27
27: ; preds = %21, %15
%28 = phi i32 [ 0, %15 ], [ %24, %21 ]
%29 = call i32 @fclose(i32* %18)
%30 = load i32, i32* %1
%31 = sdiv i32 %28, %30
store i32 %31, i32* %2
%32 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* %1
%34 = shl i32 %33, 3
%35 = call i8* @malloc(i32 %34)
%36 = bitcast i8* %35 to double**
%37 = icmp sgt i32 %33, 0
br i1 %37, label %38, label %73
38: ; preds = %27
%39 = load i32, i32* %2
br label %40
40: ; preds = %38, %64
%41 = phi i32 [ %33, %38 ], [ %65, %64 ]
%42 = phi i32 [ %39, %38 ], [ %66, %64 ]
%43 = phi i64 [ 0, %38 ], [ %70, %64 ]
%44 = shl i32 %42, 3
%45 = add i32 %44, 8
%46 = call i8* @malloc(i32 %45)
%47 = getelementptr inbounds double*, double** %36, i64 %43
%48 = bitcast double** %47 to i8**
store i8* %46, i8** %48
%49 = icmp sgt i32 %42, 0
%50 = bitcast i8* %46 to double*
br i1 %49, label %51, label %64
51: ; preds = %40, %51
%52 = phi i64 [ %58, %51 ], [ 0, %40 ]
%53 = phi double [ %57, %51 ], [ 0.000000e+00, %40 ]
%54 = call i32 (i32*, i8*, ...) @fscanf(i32* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double* %4)
%55 = load double, double* %4
%56 = getelementptr inbounds double, double* %50, i64 %52
store double %55, double* %56
%57 = fadd double %53, %55
%58 = add nuw nsw i64 %52, 1
%59 = load i32, i32* %2
%60 = sext i32 %59 to i64
%61 = icmp slt i64 %58, %60
br i1 %61, label %51, label %62
62: ; preds = %51
%63 = load i32, i32* %1
br label %64
64: ; preds = %40, %62
%65 = phi i32 [ %63, %62 ], [ %41, %40 ]
%66 = phi i32 [ %59, %62 ], [ %42, %40 ]
%67 = phi double [ %57, %62 ], [ 0.000000e+00, %40 ]
%68 = sext i32 %66 to i64
%69 = getelementptr inbounds double, double* %50, i64 %68
store double %67, double* %69
%70 = add nuw nsw i64 %43, 1
%71 = sext i32 %65 to i64
%72 = icmp slt i64 %70, %71
br i1 %72, label %40, label %73
73: ; preds = %64, %27
%74 = call i32 @fclose(i32* %32)
ret double** %36
}