#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*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *);
void write_graphics(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");
    return;
  }
  fprintf(fid, "P2\n");
  fprintf(fid, "%d %d\n", data_cols, data_rows);
  fprintf(fid, "%d\n", data_range);
  if (major == 0) {
    for (i = 0; i < data_rows; i++) {
      for (j = 0; j < data_cols; j++) {
        fprintf(fid, "%d ", (int)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, "%d ", (int)input[j * data_rows + i]);
      }
      fprintf(fid, "\n");
    }
  }
  fclose(fid);
}


x86 assembly (clang, -O0):
<write_graphics>:
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    0xe97(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118e <write_graphics+0x4e>
lea    0xe7e(%rip),%rdi
call   1040 <printf@plt>
jmp    12e2 <write_graphics+0x1a2>
mov    -0x28(%rbp),%rdi
lea    0xe96(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1258 <write_graphics+0x118>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1253 <write_graphics+0x113>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1233 <write_graphics+0xf3>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
imul   -0x18(%rbp),%ecx
add    -0x30(%rbp),%ecx
movslq %ecx,%rcx
cvttss2si (%rax,%rcx,4),%edx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11f1 <write_graphics+0xb1>
mov    -0x28(%rbp),%rdi
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11de <write_graphics+0x9e>
jmp    12d9 <write_graphics+0x199>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12d4 <write_graphics+0x194>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12b4 <write_graphics+0x174>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
cvttss2si (%rax,%rcx,4),%edx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1272 <write_graphics+0x132>
mov    -0x28(%rbp),%rdi
lea    0xd72(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    125f <write_graphics+0x11f>
jmp    12d9 <write_graphics+0x199>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_graphics>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%r12d
mov    %r8d,%r13d
mov    %ecx,%ebx
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     127e <write_graphics+0x11e>
mov    %rax,%rbp
lea    0xe67(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
lea    0xe52(%rip),%rsi
mov    %rbp,%rdi
mov    %ebx,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xe3f(%rip),%rsi
mov    %rbp,%rdi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %r13d,%r13d
mov    %ebx,0xc(%rsp)
je     1298 <write_graphics+0x138>
test   %r15d,%r15d
jle    131e <write_graphics+0x1be>
mov    %r15d,%eax
mov    0xc(%rsp),%ecx
mov    %rcx,0x20(%rsp)
mov    %rax,0x10(%rsp)
lea    0x0(,%rax,4),%r13
lea    0xe00(%rip),%rbx
xor    %eax,%eax
jmp    1245 <write_graphics+0xe5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
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     131e <write_graphics+0x1be>
mov    %rax,0x18(%rsp)
cmpl   $0x0,0xc(%rsp)
jle    1220 <write_graphics+0xc0>
mov    0x20(%rsp),%r15
mov    %r14,%r12
nopl   0x0(%rax)
cvttss2si (%r12),%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    %r13,%r12
add    $0xffffffffffffffff,%r15
jne    1260 <write_graphics+0x100>
jmp    1220 <write_graphics+0xc0>
lea    0xd8c(%rip),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
test   %r15d,%r15d
jle    131e <write_graphics+0x1be>
mov    0xc(%rsp),%eax
movslq %eax,%rcx
mov    %r15d,%edx
mov    %rdx,0x10(%rsp)
mov    %eax,%r15d
shl    $0x2,%rcx
mov    %rcx,0x18(%rsp)
lea    0xd4a(%rip),%rbx
xor    %r12d,%r12d
jmp    12ed <write_graphics+0x18d>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
add    $0x1,%r12
add    0x18(%rsp),%r14
cmp    0x10(%rsp),%r12
je     131e <write_graphics+0x1be>
cmpl   $0x0,0xc(%rsp)
jle    12d0 <write_graphics+0x170>
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
cvttss2si (%r14,%r13,4),%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%r13
cmp    %r13,%r15
jne    1300 <write_graphics+0x1a0>
jmp    12d0 <write_graphics+0x170>
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"P2\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@.str.5 = [4 x i8] c"%d \00"
@.str.6 = [2 x i8] c"\0A\00"
define void @write_graphics(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 %22
20: ; preds = %6
%21 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %103
22: ; preds = %6
%23 = load i32*, i32** %13
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32*, i32** %13
%26 = load i32, i32* %10
%27 = load i32, i32* %9
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %27)
%29 = load i32*, i32** %13
%30 = load i32, i32* %12
%31 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %11
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %67
34: ; preds = %22
store i32 0, i32* %14
br label %35
35: ; preds = %63, %34
%36 = load i32, i32* %14
%37 = load i32, i32* %9
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %66
39: ; preds = %35
store i32 0, i32* %15
br label %40
40: ; preds = %57, %39
%41 = load i32, i32* %15
%42 = load i32, i32* %10
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %60
44: ; preds = %40
%45 = load i32*, i32** %13
%46 = load float*, float** %8
%47 = load i32, i32* %14
%48 = load i32, i32* %10
%49 = mul nsw i32 %47, %48
%50 = load i32, i32* %15
%51 = add nsw i32 %49, %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds float, float* %46, i64 %52
%54 = load float, float* %53
%55 = fptosi float %54 to i32
%56 = call i32 (i32*, i8*, ...) @fprintf(i32* %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %44
%58 = load i32, i32* %15
%59 = add nsw i32 %58, 1
store i32 %59, i32* %15
br label %40
60: ; preds = %40
%61 = load i32*, i32** %13
%62 = call i32 (i32*, i8*, ...) @fprintf(i32* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %63
63: ; preds = %60
%64 = load i32, i32* %14
%65 = add nsw i32 %64, 1
store i32 %65, i32* %14
br label %35
66: ; preds = %35
br label %100
67: ; preds = %22
store i32 0, i32* %14
br label %68
68: ; preds = %96, %67
%69 = load i32, i32* %14
%70 = load i32, i32* %9
%71 = icmp slt i32 %69, %70
br i1 %71, label %72, label %99
72: ; preds = %68
store i32 0, i32* %15
br label %73
73: ; preds = %90, %72
%74 = load i32, i32* %15
%75 = load i32, i32* %10
%76 = icmp slt i32 %74, %75
br i1 %76, label %77, label %93
77: ; preds = %73
%78 = load i32*, i32** %13
%79 = load float*, float** %8
%80 = load i32, i32* %15
%81 = load i32, i32* %9
%82 = mul nsw i32 %80, %81
%83 = load i32, i32* %14
%84 = add nsw i32 %82, %83
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds float, float* %79, i64 %85
%87 = load float, float* %86
%88 = fptosi float %87 to i32
%89 = call i32 (i32*, i8*, ...) @fprintf(i32* %78, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %88)
br label %90
90: ; preds = %77
%91 = load i32, i32* %15
%92 = add nsw i32 %91, 1
store i32 %92, i32* %15
br label %73
93: ; preds = %73
%94 = load i32*, i32** %13
%95 = call i32 (i32*, i8*, ...) @fprintf(i32* %94, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %96
96: ; preds = %93
%97 = load i32, i32* %14
%98 = add nsw i32 %97, 1
store i32 %98, i32* %14
br label %68
99: ; preds = %68
br label %100
100: ; preds = %99, %66
%101 = load i32*, i32** %13
%102 = call i32 @fclose(i32* %101)
br label %103
103: ; preds = %100, %20
ret void
}

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