#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 */
char *calloc(int, int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void writeScores(char *fileName, double *matrix, double *loadings, int K,
                 int nIND, int nSNP, int *order_v) {
  FILE *resultFile, *loadingsFile;
  int i, j;
  char *fileName_ext = calloc(256, sizeof(char));
  char *fileName_loadings = calloc(256, sizeof(char));
  strcpy(fileName_ext, fileName);
  strcat(fileName_ext, ".scores");
  printf("Opening file %s...\n", fileName_ext);
  if ((resultFile = fopen(fileName_ext, "w")) == NULL)
    printf("ERROR, unable to open %s\n", fileName_ext);
  for (i = 0; i < K; i++) {
    for (j = 0; j < nIND; j++) {
      fprintf(resultFile, "%f ", matrix[order_v[i] * nIND + j]);
    }
    fprintf(resultFile, "\n");
  }
  strcpy(fileName_loadings, fileName);
  strcat(fileName_loadings, ".loadings");
  printf("Opening file %s...\n", fileName_loadings);
  if ((loadingsFile = fopen(fileName_loadings, "w")) == NULL)
    printf("ERROR, unable to open %s\n", fileName_loadings);
  for (i = 0; i < nSNP; i++) {
    for (j = 0; j < K; j++) {
      fprintf(loadingsFile, "%f ", loadings[i * K + order_v[j]]);
    }
    fprintf(loadingsFile, "\n");
  }
}


x86 assembly (clang, -O0):
<writeScores>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
mov    $0x100,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x48(%rbp)
mov    $0x100,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x48(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x48(%rbp),%rsi
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
mov    -0x48(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1204 <writeScores+0xa4>
mov    -0x48(%rbp),%rsi
lea    0xe1f(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1288 <writeScores+0x128>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1268 <writeScores+0x108>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rbp),%rcx
movslq -0x3c(%rbp),%rdx
mov    (%rcx,%rdx,4),%ecx
imul   -0x20(%rbp),%ecx
add    -0x40(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xde5(%rip),%rsi
mov    $0x1,%al
call   1060 <fprintf@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    121e <writeScores+0xbe>
mov    -0x30(%rbp),%rdi
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    120b <writeScores+0xab>
mov    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x50(%rbp),%rdi
lea    0xd9c(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x50(%rbp),%rsi
lea    0xd58(%rip),%rdi
call   1040 <printf@plt>
mov    -0x50(%rbp),%rdi
lea    0xd5c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12e3 <writeScores+0x183>
mov    -0x50(%rbp),%rsi
lea    0xd40(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1367 <writeScores+0x207>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1347 <writeScores+0x1e7>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x3c(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
mov    0x10(%rbp),%rdx
movslq -0x40(%rbp),%rsi
add    (%rdx,%rsi,4),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xd06(%rip),%rsi
mov    $0x1,%al
call   1060 <fprintf@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    12fd <writeScores+0x19d>
mov    -0x38(%rbp),%rdi
lea    0xcc8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    12ea <writeScores+0x18a>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeScores>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r9d,0x10(%rsp)
mov    %r8d,0xc(%rsp)
mov    %ecx,%r14d
mov    %rdx,0x40(%rsp)
mov    %rsi,0x38(%rsp)
mov    %rdi,%rbx
mov    $0x100,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbp
mov    $0x100,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,0x30(%rsp)
mov    %rbp,%rdi
mov    %rbx,0x28(%rsp)
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xe2b(%rip),%rsi
mov    %rbp,%rdi
call   1090 <strcat@plt>
lea    0xe24(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
lea    0xe29(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1212 <writeScores+0xa2>
lea    0xe14(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    %r14d,%eax
mov    %rax,0x20(%rsp)
mov    %r14d,0x14(%rsp)
test   %r14d,%r14d
jle    12c5 <writeScores+0x155>
mov    0xc(%rsp),%eax
mov    %eax,%ebp
xor    %r12d,%r12d
movslq %eax,%r15
mov    %rbp,(%rsp)
mov    0x38(%rsp),%r13
jmp    1258 <writeScores+0xe8>
nop
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0x1,%r12
cmp    0x20(%rsp),%r12
je     12c5 <writeScores+0x155>
cmpl   $0x0,0xc(%rsp)
mov    0x80(%rsp),%rcx
lea    0xdcc(%rip),%rsi
jle    1240 <writeScores+0xd0>
xor    %r14d,%r14d
mov    %r12,0x18(%rsp)
cs nopw 0x0(%rax,%rax,1)
movslq (%rcx,%r12,4),%rax
imul   %r15,%rax
add    %r14,%rax
movsd  0x0(%r13,%rax,8),%xmm0
mov    %rbx,%rdi
mov    %rsi,%r12
mov    $0x1,%al
mov    %r15,%rbp
mov    %rcx,%r15
call   1070 <fprintf@plt>
mov    %r12,%rsi
mov    %r15,%rcx
mov    %rbp,%r15
mov    0x18(%rsp),%r12
mov    (%rsp),%rbp
add    $0x1,%r14
cmp    %r14d,%ebp
jne    1280 <writeScores+0x110>
jmp    1240 <writeScores+0xd0>
mov    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    0x28(%rsp),%rsi
call   1030 <strcpy@plt>
lea    0xd5e(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
lea    0xd1b(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xd20(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
je     1318 <writeScores+0x1a8>
cmpl   $0x0,0x10(%rsp)
jg     1332 <writeScores+0x1c2>
jmp    13d0 <writeScores+0x260>
lea    0xcff(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
cmpl   $0x0,0x10(%rsp)
jle    13d0 <writeScores+0x260>
xor    %eax,%eax
mov    %rax,(%rsp)
mov    0x40(%rsp),%r15
mov    0x20(%rsp),%rbx
jmp    136e <writeScores+0x1fe>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0xa,%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
mov    (%rsp),%rax
add    $0x1,%eax
mov    %rax,(%rsp)
cmp    0x10(%rsp),%eax
je     13d0 <writeScores+0x260>
cmpl   $0x0,0x14(%rsp)
mov    0x80(%rsp),%rcx
lea    0xcb6(%rip),%r12
jle    1350 <writeScores+0x1e0>
mov    (%rsp),%rax
imul   0x14(%rsp),%eax
mov    %eax,0x18(%rsp)
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq (%rcx,%rbp,4),%rax
mov    %rcx,%r14
movslq 0x18(%rsp),%rcx
add    %rax,%rcx
movsd  (%r15,%rcx,8),%xmm0
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x1,%al
call   1070 <fprintf@plt>
mov    %r14,%rcx
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    13a0 <writeScores+0x230>
jmp    1350 <writeScores+0x1e0>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [8 x i8] c".scores\00"
@.str.1 = [20 x i8] c"Opening file %s...\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [26 x i8] c"ERROR, unable to open %s\0A\00"
@.str.4 = [4 x i8] c"%f \00"
@.str.6 = [10 x i8] c".loadings\00"
define void @writeScores(i8* %0, double* nocapture %1, double* nocapture %2, i32 %3, i32 %4, i32 %5, i32* nocapture %6) {
%8 = tail call dereferenceable_or_null(256) i8* @calloc(i32 256, i32 1)
%9 = tail call dereferenceable_or_null(256) i8* @calloc(i32 256, i32 1)
%10 = tail call i32 @strcpy(i8* %8, i8* %0)
%11 = tail call i32 @strcat(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%13 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %17
17: ; preds = %15, %7
%18 = icmp sgt i32 %3, 0
br i1 %18, label %19, label %41
19: ; preds = %17
%20 = icmp sgt i32 %4, 0
%21 = zext i32 %3 to i64
br label %22
22: ; preds = %19, %37
%23 = phi i64 [ 0, %19 ], [ %39, %37 ]
br i1 %20, label %24, label %37
24: ; preds = %22
%25 = getelementptr inbounds i32, i32* %6, i64 %23
br label %26
26: ; preds = %24, %26
%27 = phi i32 [ 0, %24 ], [ %35, %26 ]
%28 = load i32, i32* %25
%29 = mul nsw i32 %28, %4
%30 = add nsw i32 %29, %27
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds double, double* %1, i64 %31
%33 = load double, double* %32
%34 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), double %33)
%35 = add nuw nsw i32 %27, 1
%36 = icmp eq i32 %35, %4
br i1 %36, label %37, label %26
37: ; preds = %26, %22
%38 = tail call i32 @fputc(i32 10, i32* %13)
%39 = add nuw nsw i64 %23, 1
%40 = icmp eq i64 %39, %21
br i1 %40, label %41, label %22
41: ; preds = %37, %17
%42 = tail call i32 @strcpy(i8* %9, i8* %0)
%43 = tail call i32 @strcat(i8* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%44 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%45 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %9)
br label %49
49: ; preds = %47, %41
%50 = icmp sgt i32 %5, 0
br i1 %50, label %51, label %72
51: ; preds = %49
%52 = zext i32 %3 to i64
br label %53
53: ; preds = %51, %68
%54 = phi i32 [ 0, %51 ], [ %70, %68 ]
br i1 %18, label %55, label %68
55: ; preds = %53
%56 = mul nsw i32 %54, %3
br label %57
57: ; preds = %55, %57
%58 = phi i64 [ 0, %55 ], [ %66, %57 ]
%59 = getelementptr inbounds i32, i32* %6, i64 %58
%60 = load i32, i32* %59
%61 = add nsw i32 %60, %56
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds double, double* %2, i64 %62
%64 = load double, double* %63
%65 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), double %64)
%66 = add nuw nsw i64 %58, 1
%67 = icmp eq i64 %66, %52
br i1 %67, label %68, label %57
68: ; preds = %57, %53
%69 = tail call i32 @fputc(i32 10, i32* %45)
%70 = add nuw nsw i32 %54, 1
%71 = icmp eq i32 %70, %5
br i1 %71, label %72, label %53
72: ; preds = %68, %49
ret void
}