#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 *, ...);
void writeImage(char *filename, unsigned char *data, int width, int height) {
  FILE *file = fopen(filename, "w");
  fprintf(file, "P3\n");
  fprintf(file, "%d %d\n", width, height);
  fprintf(file, "255\n");
  int i;
  for (i = 0; i < width * height; i++) {
    data[i] = data[i] > 255 ? 255 : data[i];
    data[i] = data[i] < 0 ? 0 : data[i];
    int j;
    for (j = 0; j < 3; j++)
      fprintf(file, "%d\n", (int)data[i]);
  }
  fclose(file);
}


x86 assembly (clang, -O0):
<writeImage>:
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    -0x8(%rbp),%rdi
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe9d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe89(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe7e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
imul   -0x18(%rbp),%ecx
cmp    %ecx,%eax
jge    1280 <writeImage+0x150>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0xff,%eax
jle    11d3 <writeImage+0xa3>
mov    $0xff,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e2 <writeImage+0xb2>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jge    1211 <writeImage+0xe1>
xor    %eax,%eax
mov    %eax,-0x30(%rbp)
jmp    1220 <writeImage+0xf0>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movl   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x28(%rbp)
jge    126d <writeImage+0x13d>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xdb1(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1237 <writeImage+0x107>
jmp    1272 <writeImage+0x142>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119d <writeImage+0x6d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeImage>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0xe9c(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
lea    0xe87(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe78(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
imul   %r15d,%ebp
test   %ebp,%ebp
jle    11ff <writeImage+0xbf>
mov    %ebp,%r15d
lea    0xe50(%rip),%r12
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    11c0 <writeImage+0x80>
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"P3\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [5 x i8] c"255\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define void @writeImage(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %9
%14 = load i32*, i32** %9
%15 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32*, i32** %9
%17 = load i32, i32* %7
%18 = load i32, i32* %8
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %18)
%20 = load i32*, i32** %9
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %10
br label %22
22: ; preds = %89, %4
%23 = load i32, i32* %10
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = mul nsw i32 %24, %25
%27 = icmp slt i32 %23, %26
br i1 %27, label %28, label %92
28: ; preds = %22
%29 = load i8*, i8** %6
%30 = load i32, i32* %10
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = icmp sgt i32 %34, 255
br i1 %35, label %36, label %37
36: ; preds = %28
br label %44
37: ; preds = %28
%38 = load i8*, i8** %6
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
br label %44
44: ; preds = %37, %36
%45 = phi i32 [ 255, %36 ], [ %43, %37 ]
%46 = trunc i32 %45 to i8
%47 = load i8*, i8** %6
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 %46, i8* %50
%51 = load i8*, i8** %6
%52 = load i32, i32* %10
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %44
br label %66
59: ; preds = %44
%60 = load i8*, i8** %6
%61 = load i32, i32* %10
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = load i8, i8* %63
%65 = zext i8 %64 to i32
br label %66
66: ; preds = %59, %58
%67 = phi i32 [ 0, %58 ], [ %65, %59 ]
%68 = trunc i32 %67 to i8
%69 = load i8*, i8** %6
%70 = load i32, i32* %10
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8 %68, i8* %72
store i32 0, i32* %11
br label %73
73: ; preds = %85, %66
%74 = load i32, i32* %11
%75 = icmp slt i32 %74, 3
br i1 %75, label %76, label %88
76: ; preds = %73
%77 = load i32*, i32** %9
%78 = load i8*, i8** %6
%79 = load i32, i32* %10
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8, i8* %78, i64 %80
%82 = load i8, i8* %81
%83 = zext i8 %82 to i32
%84 = call i32 (i32*, i8*, ...) @fprintf(i32* %77, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %83)
br label %85
85: ; preds = %76
%86 = load i32, i32* %11
%87 = add nsw i32 %86, 1
store i32 %87, i32* %11
br label %73
88: ; preds = %73
br label %89
89: ; preds = %88
%90 = load i32, i32* %10
%91 = add nsw i32 %90, 1
store i32 %91, i32* %10
br label %22
92: ; preds = %22
%93 = load i32*, i32** %9
%94 = call i32 @fclose(i32* %93)
ret void
}

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