#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void write(char *filename, unsigned char *array) {
  FILE *fp;
  fp = fopen(filename, "w");
  int box = 2048;
  fprintf(fp, "P2\n");
  fprintf(fp, "%d %d \n", box, box);
  fprintf(fp, "%d \n", 255);
  int a, b;
  for (a = 0; a < box; a++)
    for (b = 0; b < box; b++) {
      fprintf(fp, "%d \n", array[a * box + b]);
    }
}


x86 assembly (clang, -O0):
<write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xec5(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x800,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xeac(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xe98(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe89(%rip),%rsi
mov    $0xff,%edx
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11fa <write+0xda>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11e7 <write+0xc7>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
add    -0x24(%rbp),%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xe37(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a6 <write+0x86>
jmp    11ec <write+0xcc>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1193 <write+0x73>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r13
lea    0xebd(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,%r14
lea    0xeb0(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1050 <fwrite@plt>
lea    0xe9b(%rip),%rsi
mov    %r14,%rdi
mov    $0x800,%edx
mov    $0x800,%ecx
xor    %eax,%eax
call   1030 <fprintf@plt>
lea    0xe83(%rip),%r15
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0xff,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x0(%r13,%rbx,1),%edx
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
add    $0x1,%rbx
cmp    $0x800,%rbx
jne    11b0 <write+0x80>
add    $0x1,%r12
add    $0x800,%r13
cmp    $0x800,%r12
jne    11a0 <write+0x70>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"P2\0A\00"
@.str.2 = [8 x i8] c"%d %d \0A\00"
@.str.3 = [5 x i8] c"%d \0A\00"
define void @write(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %5
store i32 2048, i32* %6
%11 = load i32*, i32** %5
%12 = call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32*, i32** %5
%14 = load i32, i32* %6
%15 = load i32, i32* %6
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %15)
%17 = load i32*, i32** %5
%18 = call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 255)
store i32 0, i32* %7
br label %19
19: ; preds = %45, %2
%20 = load i32, i32* %7
%21 = load i32, i32* %6
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %48
23: ; preds = %19
store i32 0, i32* %8
br label %24
24: ; preds = %41, %23
%25 = load i32, i32* %8
%26 = load i32, i32* %6
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %44
28: ; preds = %24
%29 = load i32*, i32** %5
%30 = load i8*, i8** %4
%31 = load i32, i32* %7
%32 = load i32, i32* %6
%33 = mul nsw i32 %31, %32
%34 = load i32, i32* %8
%35 = add nsw i32 %33, %34
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %30, i64 %36
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %28
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %24
44: ; preds = %24
br label %45
45: ; preds = %44
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %19
48: ; preds = %19
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"P2\0A\00"
@.str.2 = [8 x i8] c"%d %d \0A\00"
@.str.3 = [5 x i8] c"%d \0A\00"
define void @write(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 3, i64 1, i32* %3)
%5 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 2048, i32 2048)
%6 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 255)
br label %7
7: ; preds = %2, %19
%8 = phi i64 [ 0, %2 ], [ %20, %19 ]
%9 = shl nsw i64 %8, 11
br label %10
10: ; preds = %7, %10
%11 = phi i64 [ 0, %7 ], [ %17, %10 ]
%12 = add nuw nsw i64 %11, %9
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = add nuw nsw i64 %11, 1
%18 = icmp eq i64 %17, 2048
br i1 %18, label %19, label %10
19: ; preds = %10
%20 = add nuw nsw i64 %8, 1
%21 = icmp eq i64 %20, 2048
br i1 %21, label %22, label %7
22: ; preds = %19
ret void
}