#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *);
void write_file(char *filename, float *input, int data_rows, int data_cols,
                int major, int data_range) {
  FILE *fid;
  int i, j;
  fid = fopen(filename, "w");
  if (fid == NULL) {
    printf("The file was not created/opened for writing\n");
    fflush(0);
    return;
  }
  if (major == 0) {
    for (i = 0; i < data_rows; i++) {
      for (j = 0; j < data_cols; j++) {
        fprintf(fid, "%f ", (float)input[i * data_cols + j]);
      }
      fprintf(fid, "\n");
    }
  } else {
    for (i = 0; i < data_rows; i++) {
      for (j = 0; j < data_cols; j++) {
        fprintf(fid, "%f ", (float)input[j * data_rows + i]);
      }
      fprintf(fid, "\n");
    }
  }
  fclose(fid);
}


x86 assembly (clang, -O0):
<write_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    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe87(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a5 <write_file+0x55>
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
jmp    12c2 <write_file+0x172>
cmpl   $0x0,-0x1c(%rbp)
jne    1234 <write_file+0xe4>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    122f <write_file+0xdf>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    120f <write_file+0xbf>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
imul   -0x18(%rbp),%ecx
add    -0x30(%rbp),%ecx
movslq %ecx,%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe35(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11c9 <write_file+0x79>
mov    -0x28(%rbp),%rdi
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b6 <write_file+0x66>
jmp    12b9 <write_file+0x169>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12b4 <write_file+0x164>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1294 <write_file+0x144>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdb0(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    124e <write_file+0xfe>
mov    -0x28(%rbp),%rdi
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    123b <write_file+0xeb>
jmp    12b9 <write_file+0x169>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%r12d
mov    %ecx,0xc(%rsp)
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1230 <write_file+0xd0>
mov    %rax,%rbp
test   %r12d,%r12d
je     1251 <write_file+0xf1>
test   %r15d,%r15d
jle    12d2 <write_file+0x172>
mov    %r15d,%eax
mov    0xc(%rsp),%ecx
mov    %rcx,0x20(%rsp)
mov    %rax,0x10(%rsp)
lea    0x0(,%rax,4),%r13
xor    %eax,%eax
lea    0xe3b(%rip),%r15
jmp    11f5 <write_file+0x95>
nopl   0x0(%rax)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
mov    0x18(%rsp),%rax
add    $0x1,%rax
add    $0x4,%r14
cmp    0x10(%rsp),%rax
je     12d2 <write_file+0x172>
mov    %rax,0x18(%rsp)
cmpl   $0x0,0xc(%rsp)
jle    11d0 <write_file+0x70>
mov    0x20(%rsp),%r12
mov    %r14,%rbx
nopl   0x0(%rax)
movss  (%rbx),%xmm0
cvtss2sd %xmm0,%xmm0
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x1,%al
call   1060 <fprintf@plt>
add    %r13,%rbx
add    $0xffffffffffffffff,%r12
jne    1210 <write_file+0xb0>
jmp    11d0 <write_file+0x70>
lea    0xdcf(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fflush@plt>
test   %r15d,%r15d
jle    12d2 <write_file+0x172>
mov    0xc(%rsp),%eax
movslq %eax,%rcx
mov    %r15d,%edx
mov    %rdx,0x10(%rsp)
mov    %eax,%r13d
shl    $0x2,%rcx
mov    %rcx,0x18(%rsp)
xor    %ebx,%ebx
lea    0xd88(%rip),%r15
jmp    129d <write_file+0x13d>
nopl   0x0(%rax)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
add    $0x1,%rbx
add    0x18(%rsp),%r14
cmp    0x10(%rsp),%rbx
je     12d2 <write_file+0x172>
cmpl   $0x0,0xc(%rsp)
jle    1280 <write_file+0x120>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
movss  (%r14,%r12,4),%xmm0
cvtss2sd %xmm0,%xmm0
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x1,%al
call   1060 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%r13
jne    12b0 <write_file+0x150>
jmp    1280 <write_file+0x120>
mov    %rbp,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [45 x i8] c"The file was not created/opened for writing\0A\00"
@.str.2 = [4 x i8] c"%f \00"
@.str.3 = [2 x i8] c"\0A\00"
define void @write_file(i8* %0, float* %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = alloca float*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %7
store float* %1, float** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%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** %13
%18 = load i32*, i32** %13
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %6
%21 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @fflush(i32 0)
br label %95
23: ; preds = %6
%24 = load i32, i32* %11
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %59
26: ; preds = %23
store i32 0, i32* %14
br label %27
27: ; preds = %55, %26
%28 = load i32, i32* %14
%29 = load i32, i32* %9
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %58
31: ; preds = %27
store i32 0, i32* %15
br label %32
32: ; preds = %49, %31
%33 = load i32, i32* %15
%34 = load i32, i32* %10
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %52
36: ; preds = %32
%37 = load i32*, i32** %13
%38 = load float*, float** %8
%39 = load i32, i32* %14
%40 = load i32, i32* %10
%41 = mul nsw i32 %39, %40
%42 = load i32, i32* %15
%43 = add nsw i32 %41, %42
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds float, float* %38, i64 %44
%46 = load float, float* %45
%47 = fpext float %46 to double
%48 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %47)
br label %49
49: ; preds = %36
%50 = load i32, i32* %15
%51 = add nsw i32 %50, 1
store i32 %51, i32* %15
br label %32
52: ; preds = %32
%53 = load i32*, i32** %13
%54 = call i32 (i32*, i8*, ...) @fprintf(i32* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %52
%56 = load i32, i32* %14
%57 = add nsw i32 %56, 1
store i32 %57, i32* %14
br label %27
58: ; preds = %27
br label %92
59: ; preds = %23
store i32 0, i32* %14
br label %60
60: ; preds = %88, %59
%61 = load i32, i32* %14
%62 = load i32, i32* %9
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %91
64: ; preds = %60
store i32 0, i32* %15
br label %65
65: ; preds = %82, %64
%66 = load i32, i32* %15
%67 = load i32, i32* %10
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %85
69: ; preds = %65
%70 = load i32*, i32** %13
%71 = load float*, float** %8
%72 = load i32, i32* %15
%73 = load i32, i32* %9
%74 = mul nsw i32 %72, %73
%75 = load i32, i32* %14
%76 = add nsw i32 %74, %75
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds float, float* %71, i64 %77
%79 = load float, float* %78
%80 = fpext float %79 to double
%81 = call i32 (i32*, i8*, ...) @fprintf(i32* %70, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %80)
br label %82
82: ; preds = %69
%83 = load i32, i32* %15
%84 = add nsw i32 %83, 1
store i32 %84, i32* %15
br label %65
85: ; preds = %65
%86 = load i32*, i32** %13
%87 = call i32 (i32*, i8*, ...) @fprintf(i32* %86, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %88
88: ; preds = %85
%89 = load i32, i32* %14
%90 = add nsw i32 %89, 1
store i32 %90, i32* %14
br label %60
91: ; preds = %60
br label %92
92: ; preds = %91, %58
%93 = load i32*, i32** %13
%94 = call i32 @fclose(i32* %93)
br label %95
95: ; preds = %92, %20
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%f \00"
@str = [44 x i8] c"The file was not created/opened for writing\00"
define void @write_file(i8* nocapture %0, float* nocapture %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @fflush(i32 0)
br label %62
12: ; preds = %6
%13 = icmp eq i32 %4, 0
%14 = icmp sgt i32 %2, 0
br i1 %13, label %21, label %15
15: ; preds = %12
br i1 %14, label %16, label %60
16: ; preds = %15
%17 = icmp sgt i32 %3, 0
%18 = zext i32 %2 to i64
%19 = zext i32 %2 to i64
%20 = zext i32 %3 to i64
br label %44
21: ; preds = %12
br i1 %14, label %22, label %60
22: ; preds = %21
%23 = icmp sgt i32 %3, 0
%24 = sext i32 %3 to i64
%25 = zext i32 %2 to i64
%26 = zext i32 %3 to i64
br label %27
27: ; preds = %22, %40
%28 = phi i64 [ 0, %22 ], [ %42, %40 ]
br i1 %23, label %29, label %40
29: ; preds = %27
%30 = mul nsw i64 %28, %24
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ 0, %29 ], [ %38, %31 ]
%33 = add nsw i64 %32, %30
%34 = getelementptr inbounds float, float* %1, i64 %33
%35 = load float, float* %34
%36 = fpext float %35 to double
%37 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %36)
%38 = add nuw nsw i64 %32, 1
%39 = icmp eq i64 %38, %26
br i1 %39, label %40, label %31
40: ; preds = %31, %27
%41 = tail call i32 @fputc(i32 10, i32* %7)
%42 = add nuw nsw i64 %28, 1
%43 = icmp eq i64 %42, %25
br i1 %43, label %60, label %27
44: ; preds = %16, %56
%45 = phi i64 [ 0, %16 ], [ %58, %56 ]
br i1 %17, label %46, label %56
46: ; preds = %44, %46
%47 = phi i64 [ %54, %46 ], [ 0, %44 ]
%48 = mul nsw i64 %47, %18
%49 = add nuw nsw i64 %48, %45
%50 = getelementptr inbounds float, float* %1, i64 %49
%51 = load float, float* %50
%52 = fpext float %51 to double
%53 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %52)
%54 = add nuw nsw i64 %47, 1
%55 = icmp eq i64 %54, %20
br i1 %55, label %56, label %46
56: ; preds = %46, %44
%57 = tail call i32 @fputc(i32 10, i32* %7)
%58 = add nuw nsw i64 %45, 1
%59 = icmp eq i64 %58, %19
br i1 %59, label %60, label %44
60: ; preds = %56, %40, %15, %21
%61 = tail call i32 @fclose(i32* %7)
br label %62
62: ; preds = %60, %9
ret void
}