#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*/ *stderr;
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) {
    fprintf(stderr, "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    0xea7(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118a <write_graphics+0x5a>
mov    0x2e6c(%rip),%rax
mov    (%rax),%rdi
lea    0xe84(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    12de <write_graphics+0x1ae>
mov    -0x28(%rbp),%rdi
lea    0xe9a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe74(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1254 <write_graphics+0x124>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    124f <write_graphics+0x11f>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    122f <write_graphics+0xff>
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    0xe20(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11ed <write_graphics+0xbd>
mov    -0x28(%rbp),%rdi
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11da <write_graphics+0xaa>
jmp    12d5 <write_graphics+0x1a5>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12d0 <write_graphics+0x1a0>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12b0 <write_graphics+0x180>
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    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    126e <write_graphics+0x13e>
mov    -0x28(%rbp),%rdi
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    125b <write_graphics+0x12b>
jmp    12d5 <write_graphics+0x1a5>
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    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     126e <write_graphics+0x11e>
mov    %rax,%rbp
lea    0xea4(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe8f(%rip),%rsi
mov    %rbp,%rdi
mov    %ebx,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe7c(%rip),%rsi
mov    %rbp,%rdi
mov    %r12d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r13d,%r13d
mov    %ebx,0xc(%rsp)
je     129c <write_graphics+0x14c>
test   %r15d,%r15d
jle    131e <write_graphics+0x1ce>
mov    %r15d,%eax
mov    0xc(%rsp),%ecx
mov    %rcx,0x20(%rsp)
mov    %rax,0x10(%rsp)
lea    0x0(,%rax,4),%r13
lea    0xe3d(%rip),%rbx
xor    %eax,%eax
jmp    1235 <write_graphics+0xe5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
mov    0x18(%rsp),%rax
add    $0x1,%rax
add    $0x4,%r14
cmp    0x10(%rsp),%rax
je     131e <write_graphics+0x1ce>
mov    %rax,0x18(%rsp)
cmpl   $0x0,0xc(%rsp)
jle    1210 <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   1050 <fprintf@plt>
add    %r13,%r12
add    $0xffffffffffffffff,%r15
jne    1250 <write_graphics+0x100>
jmp    1210 <write_graphics+0xc0>
mov    0x2d6b(%rip),%rax
mov    (%rax),%rcx
lea    0xd83(%rip),%rdi
mov    $0x2c,%esi
mov    $0x1,%edx
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fwrite@plt>
test   %r15d,%r15d
jle    131e <write_graphics+0x1ce>
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    0xd73(%rip),%rbx
xor    %r12d,%r12d
jmp    12ed <write_graphics+0x19d>
nopl   0x0(%rax)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
add    $0x1,%r12
add    0x18(%rsp),%r14
cmp    0x10(%rsp),%r12
je     131e <write_graphics+0x1ce>
cmpl   $0x0,0xc(%rsp)
jle    12d0 <write_graphics+0x180>
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
cvttss2si (%r14,%r13,4),%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r13
cmp    %r13,%r15
jne    1300 <write_graphics+0x1b0>
jmp    12d0 <write_graphics+0x180>
mov    %rbp,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

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

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