#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 *, ...);
int txtarray_write1(const char *filename, const char *header, double *data,
                    int m, int n) {
  FILE *fp;
  int i, j;
  double *x;
  fp = fopen(filename, "w");
  if (fp == NULL) {
    return -1;
  }
  fprintf(fp, "# %d %d\n", m, n);
  if (header != NULL)
    fprintf(fp, "# %s\n", header);
  else
    fprintf(fp, "# header\n");
  for (i = 0; i < m; i++) {
    x = &data[n * i];
    for (j = 0; j < n; j++) {
      fprintf(fp, " %0.12e", x[j]);
    }
    fprintf(fp, "\n");
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<txtarray_write1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeaa(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1176 <txtarray_write1+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <txtarray_write1+0x132>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpq   $0x0,-0x18(%rbp)
je     11b4 <txtarray_write1+0x84>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11c6 <txtarray_write1+0x96>
mov    -0x30(%rbp),%rdi
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1252 <txtarray_write1+0x122>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%ecx
imul   -0x34(%rbp),%ecx
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1232 <txtarray_write1+0x102>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xdfe(%rip),%rsi
mov    $0x1,%al
call   1040 <fprintf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11f9 <txtarray_write1+0xc9>
mov    -0x30(%rbp),%rdi
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11cd <txtarray_write1+0x9d>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<txtarray_write1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r14d
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %rsi,%r12
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11bd <txtarray_write1+0x6d>
mov    %rax,%rbp
lea    0xe7d(%rip),%rsi
mov    %rax,%rdi
mov    %r15d,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r12,%r12
mov    %r14d,0x4(%rsp)
je     11c7 <txtarray_write1+0x77>
lea    0xe65(%rip),%rsi
mov    %rbp,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r15d,%r15d
jg     11e5 <txtarray_write1+0x95>
jmp    125e <txtarray_write1+0x10e>
mov    $0xffffffff,%eax
jmp    1268 <txtarray_write1+0x118>
lea    0xe43(%rip),%rdi
mov    $0x9,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1070 <fwrite@plt>
test   %r15d,%r15d
jle    125e <txtarray_write1+0x10e>
mov    0x4(%rsp),%eax
movslq %eax,%rcx
mov    %r15d,%edx
mov    %rdx,0x8(%rsp)
mov    %eax,%r14d
shl    $0x3,%rcx
mov    %rcx,0x10(%rsp)
xor    %r13d,%r13d
lea    0xe11(%rip),%r15
jmp    122d <txtarray_write1+0xdd>
nopl   0x0(%rax)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
add    $0x1,%r13
add    0x10(%rsp),%rbx
cmp    0x8(%rsp),%r13
je     125e <txtarray_write1+0x10e>
cmpl   $0x0,0x4(%rsp)
jle    1210 <txtarray_write1+0xc0>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
movsd  (%rbx,%r12,8),%xmm0
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%r14
jne    1240 <txtarray_write1+0xf0>
jmp    1210 <txtarray_write1+0xc0>
mov    %rbp,%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"w\00"
@.str.1 = [9 x i8] c"# %d %d\0A\00"
@.str.2 = [6 x i8] c"# %s\0A\00"
@.str.3 = [10 x i8] c"# header\0A\00"
@.str.4 = [8 x i8] c" %0.12e\00"
@.str.5 = [2 x i8] c"\0A\00"
define i32 @txtarray_write1(i8* %0, i8* %1, double* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca double*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca double*
store i8* %0, i8** %7
store i8* %1, i8** %8
store double* %2, double** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%16 = load i8*, i8** %7
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %12
%18 = load i32*, i32** %12
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %5
store i32 -1, i32* %6
br label %71
21: ; preds = %5
%22 = load i32*, i32** %12
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %24)
%26 = load i8*, i8** %8
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = load i32*, i32** %12
%30 = load i8*, i8** %8
%31 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %35
32: ; preds = %21
%33 = load i32*, i32** %12
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %32, %28
store i32 0, i32* %13
br label %36
36: ; preds = %65, %35
%37 = load i32, i32* %13
%38 = load i32, i32* %10
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %68
40: ; preds = %36
%41 = load double*, double** %9
%42 = load i32, i32* %11
%43 = load i32, i32* %13
%44 = mul nsw i32 %42, %43
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds double, double* %41, i64 %45
store double* %46, double** %15
store i32 0, i32* %14
br label %47
47: ; preds = %59, %40
%48 = load i32, i32* %14
%49 = load i32, i32* %11
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %62
51: ; preds = %47
%52 = load i32*, i32** %12
%53 = load double*, double** %15
%54 = load i32, i32* %14
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds double, double* %53, i64 %55
%57 = load double, double* %56
%58 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), double %57)
br label %59
59: ; preds = %51
%60 = load i32, i32* %14
%61 = add nsw i32 %60, 1
store i32 %61, i32* %14
br label %47
62: ; preds = %47
%63 = load i32*, i32** %12
%64 = call i32 (i32*, i8*, ...) @fprintf(i32* %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %65
65: ; preds = %62
%66 = load i32, i32* %13
%67 = add nsw i32 %66, 1
store i32 %67, i32* %13
br label %36
68: ; preds = %36
%69 = load i32*, i32** %12
%70 = call i32 @fclose(i32* %69)
store i32 0, i32* %6
br label %71
71: ; preds = %68, %20
%72 = load i32, i32* %6
ret i32 %72
}

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