#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 *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int, float);
void writeoutput(float *vect, int grid_rows, int grid_cols, int layers,
                 char *file) {
  int i, j, k, index = 0;
  FILE *fp;
  char str[(256)];
  if ((fp = fopen(file, "w")) == 0)
    printf("The file was not opened\n");
  for (i = 0; i < grid_rows; i++)
    for (j = 0; j < grid_cols; j++)
      for (k = 0; k < layers; k++) {
        sprintf(str, "%d\t%g\n", index,
                vect[i * grid_cols + j + k * grid_rows * grid_cols]);
        fputs(str, fp);
        index++;
      }
  fclose(fp);
}


x86 assembly (clang, -O0):
<writeoutput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    119d <writeoutput+0x4d>
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    125c <writeoutput+0x10c>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1249 <writeoutput+0xf9>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1236 <writeoutput+0xe6>
lea    -0x140(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
imul   -0x10(%rbp),%ecx
add    -0x28(%rbp),%ecx
mov    -0x2c(%rbp),%esi
imul   -0xc(%rbp),%esi
imul   -0x10(%rbp),%esi
add    %esi,%ecx
movslq %ecx,%rcx
movss  (%rax,%rcx,4),%xmm0
lea    0xe11(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x140(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ca <writeoutput+0x7a>
jmp    123b <writeoutput+0xeb>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11b7 <writeoutput+0x67>
jmp    124e <writeoutput+0xfe>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a4 <writeoutput+0x54>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeoutput>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
mov    %ecx,0x18(%rsp)
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%r13
lea    0xe8c(%rip),%rsi
mov    %r8,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1190 <writeoutput+0x40>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,0xc(%rsp)
test   %r14d,%r14d
jle    128d <writeoutput+0x13d>
mov    0x18(%rsp),%eax
mov    %rax,0x30(%rsp)
mov    %ebx,%r14d
imul   0xc(%rsp),%r14d
movl   $0x0,0x10(%rsp)
lea    0xe43(%rip),%r12
lea    0x40(%rsp),%rcx
movl   $0x0,0x14(%rsp)
xor    %r15d,%r15d
mov    %ebx,0x1c(%rsp)
mov    %rbp,0x28(%rsp)
mov    %r13,0x38(%rsp)
jmp    11f9 <writeoutput+0xa9>
nop
mov    0x14(%rsp),%edx
add    $0x1,%edx
add    %ebx,0x10(%rsp)
mov    %edx,0x14(%rsp)
cmp    0xc(%rsp),%edx
je     128d <writeoutput+0x13d>
test   %ebx,%ebx
jle    11e0 <writeoutput+0x90>
xor    %edx,%edx
mov    0x10(%rsp),%esi
jmp    122b <writeoutput+0xdb>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x24(%rsp),%edx
add    $0x1,%edx
mov    0x20(%rsp),%esi
add    $0x1,%esi
mov    0x1c(%rsp),%ebx
cmp    %ebx,%edx
mov    0x28(%rsp),%rbp
je     11e0 <writeoutput+0x90>
cmpl   $0x0,0x18(%rsp)
mov    %edx,0x24(%rsp)
mov    %esi,0x20(%rsp)
jle    1210 <writeoutput+0xc0>
mov    0x30(%rsp),%r13
mov    %esi,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rbx
mov    0x38(%rsp),%rdx
movss  (%rdx,%rbx,4),%xmm0
mov    %rcx,%rbp
mov    %rcx,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1070 <sprintf@plt>
mov    %rbp,%rdi
mov    0x28(%rsp),%rsi
call   1050 <fputs@plt>
mov    %rbp,%rcx
add    $0x1,%r15d
add    %r14d,%ebx
add    $0xffffffffffffffff,%r13
jne    1250 <writeoutput+0x100>
jmp    1210 <writeoutput+0xc0>
mov    %rbp,%rdi
add    $0x148,%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 = [25 x i8] c"The file was not opened\0A\00"
@.str.2 = [7 x i8] c"%d\09%g\0A\00"
define void @writeoutput(float* %0, i32 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca float*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32*
%16 = alloca [256 x i8]
store float* %0, float** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
store i32 0, i32* %14
%17 = load i8*, i8** %10
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %15
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %5
%21 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %5
store i32 0, i32* %11
br label %23
23: ; preds = %69, %22
%24 = load i32, i32* %11
%25 = load i32, i32* %7
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %72
27: ; preds = %23
store i32 0, i32* %12
br label %28
28: ; preds = %65, %27
%29 = load i32, i32* %12
%30 = load i32, i32* %8
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %68
32: ; preds = %28
store i32 0, i32* %13
br label %33
33: ; preds = %61, %32
%34 = load i32, i32* %13
%35 = load i32, i32* %9
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %64
37: ; preds = %33
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%39 = load i32, i32* %14
%40 = load float*, float** %6
%41 = load i32, i32* %11
%42 = load i32, i32* %8
%43 = mul nsw i32 %41, %42
%44 = load i32, i32* %12
%45 = add nsw i32 %43, %44
%46 = load i32, i32* %13
%47 = load i32, i32* %7
%48 = mul nsw i32 %46, %47
%49 = load i32, i32* %8
%50 = mul nsw i32 %48, %49
%51 = add nsw i32 %45, %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds float, float* %40, i64 %52
%54 = load float, float* %53
%55 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %39, float %54)
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%57 = load i32*, i32** %15
%58 = call i32 @fputs(i8* %56, i32* %57)
%59 = load i32, i32* %14
%60 = add nsw i32 %59, 1
store i32 %60, i32* %14
br label %61
61: ; preds = %37
%62 = load i32, i32* %13
%63 = add nsw i32 %62, 1
store i32 %63, i32* %13
br label %33
64: ; preds = %33
br label %65
65: ; preds = %64
%66 = load i32, i32* %12
%67 = add nsw i32 %66, 1
store i32 %67, i32* %12
br label %28
68: ; preds = %28
br label %69
69: ; preds = %68
%70 = load i32, i32* %11
%71 = add nsw i32 %70, 1
store i32 %71, i32* %11
br label %23
72: ; preds = %23
%73 = load i32*, i32** %15
%74 = call i32 @fclose(i32* %73)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.2 = [7 x i8] c"%d\09%g\0A\00"
@str = [24 x i8] c"The file was not opened\00"
define void @writeoutput(float* nocapture %0, i32 %1, i32 %2, i32 %3, i8* nocapture %4) {
%6 = alloca [256 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%8 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %12
12: ; preds = %10, %5
%13 = icmp sgt i32 %1, 0
br i1 %13, label %14, label %48
14: ; preds = %12
%15 = icmp sgt i32 %2, 0
%16 = icmp sgt i32 %3, 0
%17 = zext i32 %3 to i64
br label %18
18: ; preds = %14, %44
%19 = phi i32 [ 0, %14 ], [ %46, %44 ]
%20 = phi i32 [ 0, %14 ], [ %45, %44 ]
br i1 %15, label %21, label %44
21: ; preds = %18, %40
%22 = phi i32 [ %41, %40 ], [ %20, %18 ]
%23 = phi i32 [ %42, %40 ], [ 0, %18 ]
br i1 %16, label %24, label %40
24: ; preds = %21, %24
%25 = phi i64 [ %38, %24 ], [ 0, %21 ]
%26 = phi i32 [ %37, %24 ], [ %22, %21 ]
%27 = trunc i64 %25 to i32
%28 = mul i32 %27, %1
%29 = add i32 %28, %19
%30 = mul i32 %29, %2
%31 = add i32 %30, %23
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds float, float* %0, i64 %32
%34 = load float, float* %33
%35 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %26, float %34)
%36 = call i32 @fputs(i8* %7, i32* %8)
%37 = add nsw i32 %26, 1
%38 = add nuw nsw i64 %25, 1
%39 = icmp eq i64 %38, %17
br i1 %39, label %40, label %24
40: ; preds = %24, %21
%41 = phi i32 [ %22, %21 ], [ %37, %24 ]
%42 = add nuw nsw i32 %23, 1
%43 = icmp eq i32 %42, %2
br i1 %43, label %44, label %21
44: ; preds = %40, %18
%45 = phi i32 [ %20, %18 ], [ %41, %40 ]
%46 = add nuw nsw i32 %19, 1
%47 = icmp eq i32 %46, %1
br i1 %47, label %48, label %18
48: ; preds = %44, %12
%49 = tail call i32 @fclose(i32* %8)
ret void
}