#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 const j;
typedef int const i;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void write(float *u, int timestep, int x_max, int y_max, int z_max) {
  int i, j;
  char szFilename[255];
  sprintf(szFilename, "%04d.txt", timestep);
  printf("Writing file %s...\n", szFilename);
  FILE *file = fopen(szFilename, "w");
  const int k = z_max / 3;
  for (j = 0; j < y_max; j++) {
    for (i = 0; i < x_max; i++)
      fprintf(file, "%f ", u[((i) + x_max * ((j) + y_max * (k)))]);
    fprintf(file, "\n");
  }
  fclose(file);
}


x86 assembly (clang, -O0):
<write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe83(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x120(%rbp),%rsi
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x128(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x12c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1259 <write+0x109>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1236 <write+0xe6>
mov    -0x128(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%edx
mov    -0x20(%rbp),%esi
mov    -0x14(%rbp),%r8d
imul   -0x12c(%rbp),%r8d
ff
add    %r8d,%esi
imul   %esi,%edx
add    %edx,%ecx
movslq %ecx,%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdfe(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11da <write+0x8a>
mov    -0x128(%rbp),%rdi
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c7 <write+0x77>
mov    -0x128(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %r8d,%r12d
mov    %ecx,%ebp
mov    %edx,0xc(%rsp)
mov    %esi,%edx
mov    %rdi,%r15
lea    0xe7a(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
lea    0xe6f(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xe74(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %ebp,%ebp
jle    1262 <write+0x102>
movslq %r12d,%rax
imul   $0x55555556,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
imul   %ebp,%eax
cltq
movslq 0xc(%rsp),%rcx
mov    %ebp,%edx
mov    %rdx,0x10(%rsp)
mov    %ecx,%r14d
imul   %rcx,%rax
lea    (%r15,%rax,4),%r15
shl    $0x2,%rcx
mov    %rcx,0x18(%rsp)
xor    %r13d,%r13d
lea    0xe1d(%rip),%rbp
jmp    122d <write+0xcd>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0x1,%r13
add    0x18(%rsp),%r15
cmp    0x10(%rsp),%r13
je     1262 <write+0x102>
cmpl   $0x0,0xc(%rsp)
jle    1210 <write+0xb0>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
movss  (%r15,%r12,4),%xmm0
cvtss2sd %xmm0,%xmm0
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x1,%al
call   1060 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%r14
jne    1240 <write+0xe0>
jmp    1210 <write+0xb0>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%04d.txt\00"
@.str.1 = [20 x i8] c"Writing file %s...\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [4 x i8] c"%f \00"
@.str.4 = [2 x i8] c"\0A\00"
define void @write(float* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca float*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [255 x i8]
%14 = alloca i32*
%15 = alloca i32
store float* %0, float** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%16 = getelementptr inbounds [255 x i8], [255 x i8]* %13, i64 0, i64 0
%17 = load i32, i32* %7
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [255 x i8], [255 x i8]* %13, i64 0, i64 0
%20 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [255 x i8], [255 x i8]* %13, i64 0, i64 0
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %14
%23 = load i32, i32* %10
%24 = sdiv i32 %23, 3
store i32 %24, i32* %15
store i32 0, i32* %12
br label %25
25: ; preds = %57, %5
%26 = load i32, i32* %12
%27 = load i32, i32* %9
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %60
29: ; preds = %25
store i32 0, i32* %11
br label %30
30: ; preds = %51, %29
%31 = load i32, i32* %11
%32 = load i32, i32* %8
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %54
34: ; preds = %30
%35 = load i32*, i32** %14
%36 = load float*, float** %6
%37 = load i32, i32* %11
%38 = load i32, i32* %8
%39 = load i32, i32* %12
%40 = load i32, i32* %9
%41 = load i32, i32* %15
%42 = mul nsw i32 %40, %41
%43 = add nsw i32 %39, %42
%44 = mul nsw i32 %38, %43
%45 = add nsw i32 %37, %44
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds float, float* %36, i64 %46
%48 = load float, float* %47
%49 = fpext float %48 to double
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double %49)
br label %51
51: ; preds = %34
%52 = load i32, i32* %11
%53 = add nsw i32 %52, 1
store i32 %53, i32* %11
br label %30
54: ; preds = %30
%55 = load i32*, i32** %14
%56 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %54
%58 = load i32, i32* %12
%59 = add nsw i32 %58, 1
store i32 %59, i32* %12
br label %25
60: ; preds = %25
%61 = load i32*, i32** %14
%62 = call i32 @fclose(i32* %61)
ret void
}

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