#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 const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int writeppm(const char *name, int xres, int yres, unsigned char *imgdata) {
  FILE *ofp;
  int y, xbytes;
  xbytes = 3 * xres;
  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 (y = 0; y < yres; y++) {
    if (fwrite(&imgdata[(yres - y - 1) * xbytes], 1, xbytes, ofp) != xbytes) {
      fclose(ofp);
      return 5;
    }
  }
  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)
imul   $0x3,-0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe98(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1188 <writeppm+0x48>
movl   $0x1,-0x4(%rbp)
jmp    123f <writeppm+0xff>
mov    -0x28(%rbp),%rdi
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    122f <writeppm+0xef>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%eax
sub    -0x2c(%rbp),%eax
sub    $0x1,%eax
imul   -0x30(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x30(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
cmp    -0x30(%rbp),%eax
je     121c <writeppm+0xdc>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x5,-0x4(%rbp)
jmp    123f <writeppm+0xff>
jmp    1221 <writeppm+0xe1>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11cb <writeppm+0x8b>
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     11f5 <writeppm+0xb5>
mov    %rax,%r12
lea    0xe91(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
lea    0xe7c(%rip),%rsi
xor    %ebp,%ebp
mov    %r12,%rdi
mov    %ebx,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe6b(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1060 <fwrite@plt>
test   %r13d,%r13d
jle    1201 <writeppm+0xc1>
lea    (%rbx,%rbx,2),%ebp
mov    %r13d,%r15d
add    $0xffffffff,%r13d
imul   %ebx,%r13d
lea    0x0(%r13,%r13,2),%ebx
nopl   (%rax)
movslq %ebx,%rdi
add    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r12,%rcx
call   1060 <fwrite@plt>
cmp    %ebp,%eax
jne    11fc <writeppm+0xbc>
sub    %ebp,%ebx
add    $0xffffffffffffffff,%r15
jne    11d0 <writeppm+0x90>
xor    %ebp,%ebp
jmp    1201 <writeppm+0xc1>
mov    $0x1,%ebp
jmp    1209 <writeppm+0xc9>
mov    $0x5,%ebp
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %ebp,%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
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = load i32, i32* %7
%14 = mul nsw i32 3, %13
store i32 %14, i32* %12
%15 = load i8*, i8** %6
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %10
%17 = load i32*, i32** %10
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %4
store i32 1, i32* %5
br label %58
20: ; preds = %4
%21 = load i32*, i32** %10
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32*, i32** %10
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32*, i32** %10
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %11
br label %29
29: ; preds = %52, %20
%30 = load i32, i32* %11
%31 = load i32, i32* %8
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %55
33: ; preds = %29
%34 = load i8*, i8** %9
%35 = load i32, i32* %8
%36 = load i32, i32* %11
%37 = sub nsw i32 %35, %36
%38 = sub nsw i32 %37, 1
%39 = load i32, i32* %12
%40 = mul nsw i32 %38, %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %34, i64 %41
%43 = load i32, i32* %12
%44 = load i32*, i32** %10
%45 = call i32 @fwrite(i8* %42, i32 1, i32 %43, i32* %44)
%46 = load i32, i32* %12
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %33
%49 = load i32*, i32** %10
%50 = call i32 @fclose(i32* %49)
store i32 5, i32* %5
br label %58
51: ; preds = %33
br label %52
52: ; preds = %51
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
br label %29
55: ; preds = %29
%56 = load i32*, i32** %10
%57 = call i32 @fclose(i32* %56)
store i32 0, i32* %5
br label %58
58: ; preds = %55, %48, %19
%59 = load i32, i32* %5
ret i32 %59
}

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 = mul nsw i32 %1, 3
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %31, label %8
8: ; preds = %4
%9 = 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* %6)
%10 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %2)
%11 = 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* %6)
%12 = icmp sgt i32 %2, 0
br i1 %12, label %13, label %28
13: ; preds = %8
%14 = zext i32 %2 to i64
br label %17
15: ; preds = %17
%16 = icmp eq i64 %27, %14
br i1 %16, label %28, label %17
17: ; preds = %13, %15
%18 = phi i64 [ 0, %13 ], [ %27, %15 ]
%19 = trunc i64 %18 to i32
%20 = xor i32 %19, -1
%21 = add i32 %20, %2
%22 = mul nsw i32 %21, %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %3, i64 %23
%25 = tail call i32 @fwrite(i8* %24, i32 1, i32 %5, i32* %6)
%26 = icmp eq i32 %25, %5
%27 = add nuw nsw i64 %18, 1
br i1 %26, label %15, label %28
28: ; preds = %15, %17, %8
%29 = phi i32 [ 0, %8 ], [ 5, %17 ], [ 0, %15 ]
%30 = tail call i32 @fclose(i32* %6)
br label %31
31: ; preds = %28, %4
%32 = phi i32 [ 1, %4 ], [ %29, %28 ]
ret i32 %32
}