#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*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, double const);
void writeMat(const int matsize, const double *mat, const char *filename) {
  int i, j;
  FILE *fp;
  fp = fopen(filename, "w");
  fprintf(fp, "%d\n", matsize);
  for (i = 0; i < matsize; i++) {
    for (j = 0; j < matsize; j++) {
      fprintf(fp, "%.10f\n", mat[i * matsize + j]);
    }
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<writeMat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
cvtsi2sdl -0x4(%rbp),%xmm0
lea    0xe9b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11d9 <writeMat+0xa9>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11c6 <writeMat+0x96>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
imul   -0x4(%rbp),%ecx
add    -0x20(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe53(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1186 <writeMat+0x56>
jmp    11cb <writeMat+0x9b>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1173 <writeMat+0x43>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeMat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xeb9(%rip),%rsi
mov    %rdx,%rdi
call   1050 <fopen@plt>
mov    %rax,%r14
cvtsi2sd %ebp,%xmm0
lea    0xea5(%rip),%rsi
mov    %rax,%rdi
call   1040 <fprintf@plt>
test   %ebp,%ebp
jle    11c7 <writeMat+0x97>
mov    %ebp,%ebp
lea    0x0(,%rbp,8),%rax
mov    %rax,(%rsp)
xor    %r13d,%r13d
lea    0xe85(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%rbx,%r12,8),%xmm0
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%rbp
jne    11a0 <writeMat+0x70>
add    $0x1,%r13
add    (%rsp),%rbx
cmp    %rbp,%r13
jne    1190 <writeMat+0x60>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [7 x i8] c"%.10f\0A\00"
define void @writeMat(i32 %0, double* %1, i8* %2) {
%4 = alloca i32
%5 = alloca double*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i32 %0, i32* %4
store double* %1, double** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %6
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = load i32, i32* %4
%14 = sitofp i32 %13 to double
%15 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double %14)
store i32 0, i32* %7
br label %16
16: ; preds = %41, %3
%17 = load i32, i32* %7
%18 = load i32, i32* %4
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %44
20: ; preds = %16
store i32 0, i32* %8
br label %21
21: ; preds = %37, %20
%22 = load i32, i32* %8
%23 = load i32, i32* %4
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %40
25: ; preds = %21
%26 = load i32*, i32** %9
%27 = load double*, double** %5
%28 = load i32, i32* %7
%29 = load i32, i32* %4
%30 = mul nsw i32 %28, %29
%31 = load i32, i32* %8
%32 = add nsw i32 %30, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds double, double* %27, i64 %33
%35 = load double, double* %34
%36 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), double %35)
br label %37
37: ; preds = %25
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
br label %21
40: ; preds = %21
br label %41
41: ; preds = %40
%42 = load i32, i32* %7
%43 = add nsw i32 %42, 1
store i32 %43, i32* %7
br label %16
44: ; preds = %16
%45 = load i32*, i32** %9
%46 = call i32 @fclose(i32* %45)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [7 x i8] c"%.10f\0A\00"
define void @writeMat(i32 %0, double* nocapture %1, i8* nocapture %2) {
%4 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = sitofp i32 %0 to double
%6 = tail call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double %5)
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %25
8: ; preds = %3
%9 = zext i32 %0 to i64
%10 = zext i32 %0 to i64
br label %11
11: ; preds = %22, %8
%12 = phi i64 [ 0, %8 ], [ %23, %22 ]
%13 = mul nsw i64 %12, %9
br label %14
14: ; preds = %11, %14
%15 = phi i64 [ 0, %11 ], [ %20, %14 ]
%16 = add nuw nsw i64 %15, %13
%17 = getelementptr inbounds double, double* %1, i64 %16
%18 = load double, double* %17
%19 = tail call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), double %18)
%20 = add nuw nsw i64 %15, 1
%21 = icmp eq i64 %20, %10
br i1 %21, label %22, label %14
22: ; preds = %14
%23 = add nuw nsw i64 %12, 1
%24 = icmp eq i64 %23, %10
br i1 %24, label %25, label %11
25: ; preds = %22, %3
%26 = tail call i32 @fclose(i32* %4)
ret void
}