#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*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int writeppm(char *name, int xres, int yres, unsigned char *imgdata) {
  FILE *ofp;
  int i;
  ofp = fopen(name, "wb");
  if (ofp == NULL) {
    return 1;
  }
  fprintf(ofp, "P6\n");
  fprintf(ofp, "%d %d\n", xres, yres);
  fprintf(ofp, "255\n");
  for (i = 0; i < yres; i++) {
    fwrite(&imgdata[(yres - i - 1) * 3 * xres], 1, (3 * xres), ofp);
  }
  fclose(ofp);
  return 0;
}


x86 assembly (clang, -O0):
<writeppm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1181 <writeppm+0x41>
movl   $0x1,-0x4(%rbp)
jmp    1219 <writeppm+0xd9>
mov    -0x28(%rbp),%rdi
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1209 <writeppm+0xc9>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%eax
sub    -0x2c(%rbp),%eax
sub    $0x1,%eax
imul   $0x3,%eax,%eax
imul   -0x14(%rbp),%eax
cltq
add    %rax,%rdi
imul   $0x3,-0x14(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11c4 <writeppm+0x84>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeppm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r13d
mov    %esi,%ebx
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11fa <writeppm+0xba>
mov    %rax,%r12
lea    0xe91(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
lea    0xe7c(%rip),%rsi
mov    %r12,%rdi
mov    %ebx,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe6d(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1060 <fwrite@plt>
test   %r13d,%r13d
jle    11ee <writeppm+0xae>
lea    (%rbx,%rbx,2),%ebp
mov    %r13d,%r15d
add    $0xffffffff,%r13d
imul   %ebx,%r13d
lea    0x0(%r13,%r13,2),%ebx
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rbx
lea    (%r14,%rbx,1),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r12,%rcx
call   1060 <fwrite@plt>
sub    %ebp,%ebx
add    $0xffffffffffffffff,%r15
jne    11d0 <writeppm+0x90>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11ff <writeppm+0xbf>
mov    $0x1,%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 = [4 x i8] c"P6\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [5 x i8] c"255\0A\00"
define i32 @writeppm(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
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %10
%14 = load i32*, i32** %10
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 1, i32* %5
br label %51
17: ; preds = %4
%18 = load i32*, i32** %10
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32*, i32** %10
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22)
%24 = load i32*, i32** %10
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %11
br label %26
26: ; preds = %45, %17
%27 = load i32, i32* %11
%28 = load i32, i32* %8
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %48
30: ; preds = %26
%31 = load i8*, i8** %9
%32 = load i32, i32* %8
%33 = load i32, i32* %11
%34 = sub nsw i32 %32, %33
%35 = sub nsw i32 %34, 1
%36 = mul nsw i32 %35, 3
%37 = load i32, i32* %7
%38 = mul nsw i32 %36, %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %31, i64 %39
%41 = load i32, i32* %7
%42 = mul nsw i32 3, %41
%43 = load i32*, i32** %10
%44 = call i32 @fwrite(i8* %40, i32 1, i32 %42, i32* %43)
br label %45
45: ; preds = %30
%46 = load i32, i32* %11
%47 = add nsw i32 %46, 1
store i32 %47, i32* %11
br label %26
48: ; preds = %26
%49 = load i32*, i32** %10
%50 = call i32 @fclose(i32* %49)
store i32 0, i32* %5
br label %51
51: ; preds = %48, %16
%52 = load i32, i32* %5
ret i32 %52
}

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