#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 *, char *);
int write_ppm(char *path, int height, int width, int grey_levels,
              unsigned char data[]) {
  FILE *file = fopen(path, "w");
  if (!file) {
    printf("Unable to open %s.\n", path);
    return -1;
  }
  fprintf(file, "%s\n%d %d\n%d\n", "P3", width, height, grey_levels);
  int i, j;
  for (j = 0; j < height; j++) {
    for (i = 0; i < width; i++) {
      fprintf(file, "%d %d %d  ", data[j * 3 * width + 3 * i + 0],
              data[j * 3 * width + 3 * i + 1], data[j * 3 * width + 3 * i + 2]);
      fprintf(file, "\n");
    }
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<write_ppm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1194 <write_ppm+0x54>
mov    -0x10(%rbp),%rsi
lea    0xe7f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <write_ppm+0x14a>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
lea    0xe6c(%rip),%rsi
lea    0xe72(%rip),%rdx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    127a <write_ppm+0x13a>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1267 <write_ppm+0x127>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
imul   $0x3,-0x38(%rbp),%ecx
imul   -0x18(%rbp),%ecx
imul   $0x3,-0x34(%rbp),%edx
add    %edx,%ecx
add    $0x0,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%edx
mov    -0x28(%rbp),%rax
imul   $0x3,-0x38(%rbp),%ecx
imul   -0x18(%rbp),%ecx
imul   $0x3,-0x34(%rbp),%esi
add    %esi,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%ecx
mov    -0x28(%rbp),%rax
imul   $0x3,-0x38(%rbp),%esi
imul   -0x18(%rbp),%esi
imul   $0x3,-0x34(%rbp),%r8d
add    %r8d,%esi
add    $0x2,%esi
movslq %esi,%rsi
movzbl (%rax,%rsi,1),%r8d
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d2 <write_ppm+0x92>
jmp    126c <write_ppm+0x12c>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11bf <write_ppm+0x7f>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ppm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,%rbx
mov    %ecx,%r13d
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%r12
lea    0xe8c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     125f <write_ppm+0x10f>
mov    %rax,%rbp
lea    0xe8a(%rip),%rsi
lea    0xe90(%rip),%rdx
mov    %rax,%rdi
mov    %r15d,%ecx
mov    %r14d,%r8d
mov    %r13d,%r9d
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,0x14(%rsp)
test   %r14d,%r14d
jle    1253 <write_ppm+0x103>
mov    %r15d,%eax
mov    %rax,0x18(%rsp)
lea    (%r15,%r15,2),%eax
mov    %eax,0x10(%rsp)
lea    0xe5b(%rip),%r13
movl   $0x0,0xc(%rsp)
xor    %r14d,%r14d
mov    %r15,0x20(%rsp)
jmp    11fc <write_ppm+0xac>
nopl   (%rax)
add    $0x1,%r14d
mov    0xc(%rsp),%eax
add    0x10(%rsp),%eax
mov    %eax,0xc(%rsp)
cmp    0x14(%rsp),%r14d
mov    0x20(%rsp),%r15
je     1253 <write_ppm+0x103>
test   %r15d,%r15d
jle    11e0 <write_ppm+0x90>
mov    0x18(%rsp),%r12
mov    0xc(%rsp),%eax
mov    %eax,%r15d
nopl   (%rax)
movslq %r15d,%r15
movzbl (%rbx,%r15,1),%edx
lea    0x1(%r15),%eax
cltq
movzbl (%rbx,%rax,1),%ecx
lea    0x2(%r15),%eax
cltq
movzbl (%rbx,%rax,1),%r8d
mov    %rbp,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xa,%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
add    $0x3,%r15d
add    $0xffffffffffffffff,%r12
jne    1210 <write_ppm+0xc0>
jmp    11e0 <write_ppm+0x90>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1273 <write_ppm+0x123>
lea    0xd9c(%rip),%rdi
mov    %r12,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [20 x i8] c"Unable to open %s.\0A\00"
@.str.2 = [13 x i8] c"%s\0A%d %d\0A%d\0A\00"
@.str.3 = [3 x i8] c"P3\00"
@.str.4 = [11 x i8] c"%d %d %d \00"
define i32 @write_ppm(i8* %0, i32 %1, i32 %2, i32 %3, i8* nocapture %4) {
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %48
10: ; preds = %5
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %2, i32 %1, i32 %3)
%12 = icmp sgt i32 %1, 0
br i1 %12, label %13, label %46
13: ; preds = %10
%14 = icmp sgt i32 %2, 0
%15 = zext i32 %2 to i64
br label %16
16: ; preds = %13, %43
%17 = phi i32 [ 0, %13 ], [ %44, %43 ]
br i1 %14, label %18, label %43
18: ; preds = %16
%19 = mul i32 %17, %2
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ 0, %18 ], [ %41, %20 ]
%22 = trunc i64 %21 to i32
%23 = add i32 %19, %22
%24 = mul i32 %23, 3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %4, i64 %25
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = add nsw i32 %24, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %4, i64 %30
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = add nsw i32 %24, 2
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %4, i64 %35
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 %33, i32 %38)
%40 = tail call i32 @fputc(i32 10, i32* %6)
%41 = add nuw nsw i64 %21, 1
%42 = icmp eq i64 %41, %15
br i1 %42, label %43, label %20
43: ; preds = %20, %16
%44 = add nuw nsw i32 %17, 1
%45 = icmp eq i32 %44, %1
br i1 %45, label %46, label %16
46: ; preds = %43, %10
%47 = tail call i32 @fclose(i32* %6)
br label %48
48: ; preds = %46, %8
%49 = phi i32 [ 0, %46 ], [ -1, %8 ]
ret i32 %49
}