#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 scalar_t__ *nlns;
typedef size_t i;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void write_array_to_file(char *fname, double *a, int ncol, int nlns) {
  int i, j;
  FILE *datei;
  datei = fopen(fname, "w");
  if (datei != NULL) {
    for (j = 0; j < nlns; ++j) {
      for (i = 0; i < ncol; ++i) {
        fprintf(datei, " %e", (a[(i) * (nlns) + (j)]));
      }
      fprintf(datei, "\n");
    }
    fclose(datei);
  }
}


x86 assembly (clang, -O0):
<write_array_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11ea <write_array_to_file+0xba>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11e1 <write_array_to_file+0xb1>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c1 <write_array_to_file+0x91>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
imul   -0x18(%rbp),%ecx
add    -0x20(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe56(%rip),%rsi
mov    $0x1,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117f <write_array_to_file+0x4f>
mov    -0x28(%rbp),%rdi
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    116c <write_array_to_file+0x3c>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_array_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebx
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
lea    0xea0(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1203 <write_array_to_file+0xc3>
mov    %rax,%rbp
test   %ebx,%ebx
jle    11ed <write_array_to_file+0xad>
mov    %ebx,%eax
mov    0xc(%rsp),%ecx
mov    %rcx,0x18(%rsp)
mov    %rax,0x20(%rsp)
lea    0x0(,%rax,8),%r13
xor    %r14d,%r14d
lea    0xe6b(%rip),%rbx
jmp    11be <write_array_to_file+0x7e>
nopl   0x0(%rax)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
add    $0x1,%r14
addq   $0x8,0x10(%rsp)
cmp    0x20(%rsp),%r14
je     11ed <write_array_to_file+0xad>
cmpl   $0x0,0xc(%rsp)
jle    11a0 <write_array_to_file+0x60>
mov    0x18(%rsp),%r12
mov    0x10(%rsp),%r15
nop
movsd  (%r15),%xmm0
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
add    %r13,%r15
add    $0xffffffffffffffff,%r12
jne    11d0 <write_array_to_file+0x90>
jmp    11a0 <write_array_to_file+0x60>
mov    %rbp,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
add    $0x28,%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 = [4 x i8] c" %e\00"
@.str.2 = [2 x i8] c"\0A\00"
define void @write_array_to_file(i8* %0, double* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca double*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i8* %0, i8** %5
store double* %1, double** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %11
%14 = load i32*, i32** %11
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %50
16: ; preds = %4
store i32 0, i32* %10
br label %17
17: ; preds = %44, %16
%18 = load i32, i32* %10
%19 = load i32, i32* %8
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %47
21: ; preds = %17
store i32 0, i32* %9
br label %22
22: ; preds = %38, %21
%23 = load i32, i32* %9
%24 = load i32, i32* %7
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %41
26: ; preds = %22
%27 = load i32*, i32** %11
%28 = load double*, double** %6
%29 = load i32, i32* %9
%30 = load i32, i32* %8
%31 = mul nsw i32 %29, %30
%32 = load i32, i32* %10
%33 = add nsw i32 %31, %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %28, i64 %34
%36 = load double, double* %35
%37 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double %36)
br label %38
38: ; preds = %26
%39 = load i32, i32* %9
%40 = add nsw i32 %39, 1
store i32 %40, i32* %9
br label %22
41: ; preds = %22
%42 = load i32*, i32** %11
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %41
%45 = load i32, i32* %10
%46 = add nsw i32 %45, 1
store i32 %46, i32* %10
br label %17
47: ; preds = %17
%48 = load i32*, i32** %11
%49 = call i32 @fclose(i32* %48)
br label %50
50: ; preds = %47, %4
ret void
}

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