#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 */
double DBL_DIG;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, double, double);
int saveMatrix(int dim, double *mat, const char *fileName) {
  long i;
  FILE *fp;
  fp = fopen(fileName, "w");
  if (fp == NULL)
    return -1;
  for (i = 0; i < dim; i++) {
    fprintf(fp, "%.*e\n", DBL_DIG, mat[i]);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<saveMatrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%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)
cmpq   $0x0,-0x28(%rbp)
jne    116e <saveMatrix+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <saveMatrix+0xa0>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jge    11c0 <saveMatrix+0x90>
mov    -0x28(%rbp),%rdi
mov    0x2e56(%rip),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
lea    0xe58(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1176 <saveMatrix+0x46>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<saveMatrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xeb9(%rip),%rsi
mov    %rdx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     119c <saveMatrix+0x6c>
mov    %rax,%r12
test   %ebp,%ebp
jle    1190 <saveMatrix+0x60>
movslq %ebp,%rbp
xor    %ebx,%ebx
mov    0x2e81(%rip),%r13
lea    0xe94(%rip),%r15
xchg   %ax,%ax
movsd  0x0(%r13),%xmm0
movsd  (%r14,%rbx,8),%xmm1
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <saveMatrix+0x40>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11a1 <saveMatrix+0x71>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [6 x i8] c"%.*e\0A\00"
@DBL_DIG = global double 0.000000e+00
define i32 @saveMatrix(i32 %0, double* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca double*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
store i32 %0, i32* %5
store double* %1, double** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %7
%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 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %35
15: ; preds = %3
store i64 0, i64* %8
br label %16
16: ; preds = %29, %15
%17 = load i64, i64* %8
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = icmp slt i64 %17, %19
br i1 %20, label %21, label %32
21: ; preds = %16
%22 = load i32*, i32** %9
%23 = load double, double* @DBL_DIG
%24 = load double*, double** %6
%25 = load i64, i64* %8
%26 = getelementptr inbounds double, double* %24, i64 %25
%27 = load double, double* %26
%28 = call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), double %23, double %27)
br label %29
29: ; preds = %21
%30 = load i64, i64* %8
%31 = add nsw i64 %30, 1
store i64 %31, i64* %8
br label %16
32: ; preds = %16
%33 = load i32*, i32** %9
%34 = call i32 @fclose(i32* %33)
store i32 0, i32* %4
br label %35
35: ; preds = %32, %14
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [6 x i8] c"%.*e\0A\00"
@DBL_DIG = global double 0.000000e+00
define i32 @saveMatrix(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 = icmp eq i32* %4, null
br i1 %5, label %19, label %6
6: ; preds = %3
%7 = sext i32 %0 to i64
%8 = icmp sgt i32 %0, 0
br i1 %8, label %9, label %17
9: ; preds = %6, %9
%10 = phi i64 [ %15, %9 ], [ 0, %6 ]
%11 = load double, double* @DBL_DIG
%12 = getelementptr inbounds double, double* %1, i64 %10
%13 = load double, double* %12
%14 = tail call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), double %11, double %13)
%15 = add nuw nsw i64 %10, 1
%16 = icmp eq i64 %15, %7
br i1 %16, label %17, label %9
17: ; preds = %9, %6
%18 = tail call i32 @fclose(i32* %4)
br label %19
19: ; preds = %3, %17
%20 = phi i32 [ 0, %17 ], [ -1, %3 ]
ret i32 %20
}