#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 */
scalar_t__ EOF;
double *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *, int *, double *);
int /*<<< orphan*/ fwrite(double *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  double *A;
  double Aij;
  FILE *fd_in;
  FILE *fd_out;
  int N;
  int i, j;
  fd_in = fopen(argv[1], "r");
  N = 0;
  while (1) {
    if (fscanf(fd_in, "%i %i %le\n", &i, &j, &Aij) == EOF) {
      break;
    }
    if (i > N)
      N = i;
    if (j > N)
      N = j;
  }
  rewind(fd_in);
  printf("N = %i\n", N);
  A = calloc(N * N, sizeof(double));
  while (1) {
    if (fscanf(fd_in, "%i %i %le\n", &i, &j, &Aij) == EOF) {
      break;
    }
    A[(i - 1) * N + (j - 1)] = Aij;
  }
  fclose(fd_in);
  fd_out = fopen(argv[2], "wb");
  Aij = N;
  fwrite(&Aij, 1, sizeof(double), fd_out);
  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      fwrite(&A[i * N + j], 1, sizeof(double), fd_out);
    }
  }
  fclose(fd_out);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe6b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe52(%rip),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1060 <fscanf@plt>
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%rax
jne    11d6 <main+0x66>
jmp    11ff <main+0x8f>
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jle    11e8 <main+0x78>
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jle    11fa <main+0x8a>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    11a5 <main+0x35>
mov    -0x28(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x34(%rbp),%esi
lea    0xdfb(%rip),%rdi
call   1040 <printf@plt>
mov    -0x34(%rbp),%edi
imul   -0x34(%rbp),%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdcb(%rip),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1060 <fscanf@plt>
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%rax
jne    125d <main+0xed>
jmp    1285 <main+0x115>
movsd  -0x20(%rbp),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%ecx
sub    $0x1,%ecx
imul   -0x34(%rbp),%ecx
mov    -0x3c(%rbp),%edx
sub    $0x1,%edx
add    %edx,%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    122c <main+0xbc>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd78(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cvtsi2sdl -0x34(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x30(%rbp),%rcx
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
call   1090 <fwrite@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1338 <main+0x1c8>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1325 <main+0x1b5>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%eax
imul   -0x34(%rbp),%eax
add    -0x3c(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1090 <fwrite@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    12e1 <main+0x171>
jmp    132a <main+0x1ba>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    12ce <main+0x15e>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    0x8(%rsi),%rdi
lea    0xe72(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r15
lea    0xe65(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
lea    0x8(%rsp),%r8
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    0x2e2f(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
je     1211 <main+0xa1>
xor    %ebx,%ebx
lea    0xe39(%rip),%r12
lea    0x4(%rsp),%r13
mov    %rsp,%rbp
lea    0x8(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
mov    0x4(%rsp),%eax
cmp    %ebx,%eax
cmovg  %eax,%ebx
mov    (%rsp),%eax
cmp    %ebx,%eax
cmovg  %eax,%ebx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
mov    %r14,%r8
call   1060 <fscanf@plt>
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%rax
jne    11e0 <main+0x70>
mov    %r15,%rdi
call   1050 <rewind@plt>
lea    0xded(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
imul   %ebx,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,%r12
lea    0xdc2(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
lea    0x8(%rsp),%r8
mov    %r15,%rdi
call   1060 <fscanf@plt>
mov    0x2d8c(%rip),%rbp
cmp    0x0(%rbp),%rax
je     12b1 <main+0x141>
movslq %ebx,%r13
lea    0x8(%rsp),%r14
nopw   0x0(%rax,%rax,1)
movsd  0x8(%rsp),%xmm0
movslq 0x4(%rsp),%rax
add    $0xffffffffffffffff,%rax
imul   %r13,%rax
movslq (%rsp),%rcx
add    %rax,%rcx
movsd  %xmm0,-0x8(%r12,%rcx,8)
mov    %r15,%rdi
lea    0xd67(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
mov    %r14,%r8
call   1060 <fscanf@plt>
cmp    0x0(%rbp),%rax
jne    1270 <main+0x100>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    0x10(%rsp),%rax
mov    0x10(%rax),%rdi
lea    0xd4c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  %xmm0,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
movl   $0x0,0x4(%rsp)
test   %ebx,%ebx
jle    1359 <main+0x1e9>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movl   $0x0,(%rsp)
xor    %ecx,%ecx
nopl   0x0(%rax)
imul   %ebx,%eax
add    %ecx,%eax
cltq
lea    (%r12,%rax,8),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    (%rsp),%ecx
add    $0x1,%ecx
mov    %ecx,(%rsp)
mov    0x4(%rsp),%eax
cmp    %ebx,%ecx
jl     1320 <main+0x1b0>
add    $0x1,%eax
mov    %eax,0x4(%rsp)
cmp    %ebx,%eax
jl     1310 <main+0x1a0>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x18,%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 = [11 x i8] c"%i %i %le\0A\00"
@EOF = global i64 0
@.str.2 = [8 x i8] c"N = %i\0A\00"
@.str.3 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca double*
%7 = alloca double
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %8
store i32 0, i32* %10
br label %17
17: ; preds = %2, %35
%18 = load i32*, i32** %8
%19 = call i64 @fscanf(i32* %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32* %11, i32* %12, double* %7)
%20 = load i64, i64* @EOF
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %17
br label %36
23: ; preds = %17
%24 = load i32, i32* %11
%25 = load i32, i32* %10
%26 = icmp sgt i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i32, i32* %11
store i32 %28, i32* %10
br label %29
29: ; preds = %27, %23
%30 = load i32, i32* %12
%31 = load i32, i32* %10
%32 = icmp sgt i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i32, i32* %12
store i32 %34, i32* %10
br label %35
35: ; preds = %33, %29
br label %17
36: ; preds = %22
%37 = load i32*, i32** %8
%38 = call i32 @rewind(i32* %37)
%39 = load i32, i32* %10
%40 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %10
%42 = load i32, i32* %10
%43 = mul nsw i32 %41, %42
%44 = call double* @calloc(i32 %43, i32 8)
store double* %44, double** %6
br label %45
45: ; preds = %36, %51
%46 = load i32*, i32** %8
%47 = call i64 @fscanf(i32* %46, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32* %11, i32* %12, double* %7)
%48 = load i64, i64* @EOF
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %45
br label %63
51: ; preds = %45
%52 = load double, double* %7
%53 = load double*, double** %6
%54 = load i32, i32* %11
%55 = sub nsw i32 %54, 1
%56 = load i32, i32* %10
%57 = mul nsw i32 %55, %56
%58 = load i32, i32* %12
%59 = sub nsw i32 %58, 1
%60 = add nsw i32 %57, %59
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds double, double* %53, i64 %61
store double %52, double* %62
br label %45
63: ; preds = %50
%64 = load i32*, i32** %8
%65 = call i32 @fclose(i32* %64)
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 2
%68 = load i8*, i8** %67
%69 = call i32* @fopen(i8* %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %69, i32** %9
%70 = load i32, i32* %10
%71 = sitofp i32 %70 to double
store double %71, double* %7
%72 = load i32*, i32** %9
%73 = call i32 @fwrite(double* %7, i32 1, i32 8, i32* %72)
store i32 0, i32* %11
br label %74
74: ; preds = %98, %63
%75 = load i32, i32* %11
%76 = load i32, i32* %10
%77 = icmp slt i32 %75, %76
br i1 %77, label %78, label %101
78: ; preds = %74
store i32 0, i32* %12
br label %79
79: ; preds = %94, %78
%80 = load i32, i32* %12
%81 = load i32, i32* %10
%82 = icmp slt i32 %80, %81
br i1 %82, label %83, label %97
83: ; preds = %79
%84 = load double*, double** %6
%85 = load i32, i32* %11
%86 = load i32, i32* %10
%87 = mul nsw i32 %85, %86
%88 = load i32, i32* %12
%89 = add nsw i32 %87, %88
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds double, double* %84, i64 %90
%92 = load i32*, i32** %9
%93 = call i32 @fwrite(double* %91, i32 1, i32 8, i32* %92)
br label %94
94: ; preds = %83
%95 = load i32, i32* %12
%96 = add nsw i32 %95, 1
store i32 %96, i32* %12
br label %79
97: ; preds = %79
br label %98
98: ; preds = %97
%99 = load i32, i32* %11
%100 = add nsw i32 %99, 1
store i32 %100, i32* %11
br label %74
101: ; preds = %74
%102 = load i32*, i32** %9
%103 = call i32 @fclose(i32* %102)
%104 = load i32, i32* %3
ret i32 %104
}

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