#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, int);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int viWritePPM(char *filename, int width, int height, unsigned char *pixels) {
  FILE *f;
  if (f = fopen(filename, "wb")) {
    int q;
    fprintf(f, "P6\n%d %d\n255\n", width, height);
    for (q = 0; q < height; q++) {
      unsigned char *adr;
      adr = pixels + 3 * width * (height - 1 - q);
      fwrite(adr, 3 * width, 1, f);
    }
    fclose(f);
    return 1;
  } else {
    printf("Error: could not open file <%s>.\n", filename);
    return 0;
  }
}


x86 assembly (clang, -O0):
<viWritePPM>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1204 <viWritePPM+0xb4>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe6e(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11ef <viWritePPM+0x9f>
mov    -0x20(%rbp),%rax
imul   $0x3,-0x14(%rbp),%ecx
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
sub    -0x2c(%rbp),%edx
imul   %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
imul   $0x3,-0x14(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a1 <viWritePPM+0x51>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    121b <viWritePPM+0xcb>
mov    -0x10(%rbp),%rsi
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<viWritePPM>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%rbp
lea    0xe93(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11df <viWritePPM+0x8f>
mov    %rax,%r13
lea    0xe82(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
mov    %r15d,%ecx
call   1050 <fprintf@plt>
test   %r15d,%r15d
jle    11d0 <viWritePPM+0x80>
lea    (%rbx,%rbx,2),%r12d
mov    %rbx,%rax
mov    %r15d,%ebx
add    $0xffffffff,%r15d
imul   %eax,%r15d
lea    (%r15,%r15,2),%ebp
nopl   0x0(%rax)
movslq %ebp,%rbp
lea    (%r14,%rbp,1),%rdi
mov    %r12d,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
sub    %r12d,%ebp
add    $0xffffffffffffffff,%rbx
jne    11b0 <viWritePPM+0x60>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
jmp    11f0 <viWritePPM+0xa0>
lea    0xe2b(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
@.str.2 = [34 x i8] c"Error: could not open file <%s>.\0A\00"
define i32 @viWritePPM(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = load i8*, i8** %6
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %47
16: ; preds = %4
%17 = load i32*, i32** %10
%18 = load i32, i32* %7
%19 = load i32, i32* %8
%20 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
store i32 0, i32* %11
br label %21
21: ; preds = %41, %16
%22 = load i32, i32* %11
%23 = load i32, i32* %8
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %44
25: ; preds = %21
%26 = load i8*, i8** %9
%27 = load i32, i32* %7
%28 = mul nsw i32 3, %27
%29 = load i32, i32* %8
%30 = sub nsw i32 %29, 1
%31 = load i32, i32* %11
%32 = sub nsw i32 %30, %31
%33 = mul nsw i32 %28, %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %26, i64 %34
store i8* %35, i8** %12
%36 = load i8*, i8** %12
%37 = load i32, i32* %7
%38 = mul nsw i32 3, %37
%39 = load i32*, i32** %10
%40 = call i32 @fwrite(i8* %36, i32 %38, i32 1, i32* %39)
br label %41
41: ; preds = %25
%42 = load i32, i32* %11
%43 = add nsw i32 %42, 1
store i32 %43, i32* %11
br label %21
44: ; preds = %21
%45 = load i32*, i32** %10
%46 = call i32 @fclose(i32* %45)
store i32 1, i32* %5
br label %50
47: ; preds = %4
%48 = load i8*, i8** %6
%49 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %48)
store i32 0, i32* %5
br label %50
50: ; preds = %47, %44
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
@.str.2 = [34 x i8] c"Error: could not open file <%s>.\0A\00"
define i32 @viWritePPM(i8* %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %26, label %7
7: ; preds = %4
%8 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %2)
%9 = icmp sgt i32 %2, 0
br i1 %9, label %10, label %24
10: ; preds = %7
%11 = mul nsw i32 %1, 3
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %10, %13
%14 = phi i64 [ 0, %10 ], [ %22, %13 ]
%15 = trunc i64 %14 to i32
%16 = xor i32 %15, -1
%17 = add i32 %16, %2
%18 = mul nsw i32 %17, %11
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %3, i64 %19
%21 = tail call i32 @fwrite(i8* %20, i32 %11, i32 1, i32* %5)
%22 = add nuw nsw i64 %14, 1
%23 = icmp eq i64 %22, %12
br i1 %23, label %24, label %13
24: ; preds = %13, %7
%25 = tail call i32 @fclose(i32* %5)
br label %28
26: ; preds = %4
%27 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %28
28: ; preds = %26, %24
%29 = phi i32 [ 1, %24 ], [ 0, %26 ]
ret i32 %29
}