#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 */
scalar_t__ EOF;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
unsigned int fwrite(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strerror(int);
int write_ppm(const char *fname, unsigned int xpix, unsigned int ypix,
              char *data) {
  FILE *fp;
  int errno = 0;
  if (fname == NULL)
    fname = "\0";
  fp = fopen(fname, "w");
  if (fp == NULL) {
    fprintf(stderr, "write_ppm failed to open %s: %s\n", fname,
            strerror(errno));
    return 1;
  }
  fprintf(fp, "P6\n");
  fprintf(fp, "%d %d 255\n", xpix, ypix);
  if (fwrite(data, sizeof(char), xpix * ypix * 3, fp) != xpix * ypix * 3) {
    perror("Write failed");
    return 2;
  }
  if (fclose(fp) == EOF) {
    perror("Close failed");
    return 3;
  }
  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    %rcx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1193 <write_ppm+0x33>
lea    0xe71(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f5 <write_ppm+0x95>
mov    0x2e1f(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129d <write_ppm+0x13d>
mov    -0x28(%rbp),%rdi
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
imul   $0x3,%eax,%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    -0x14(%rbp),%ecx
imul   -0x18(%rbp),%ecx
imul   $0x3,%ecx,%ecx
cmp    %ecx,%eax
je     1265 <write_ppm+0x105>
lea    0xde0(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    129d <write_ppm+0x13d>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
jne    1296 <write_ppm+0x136>
lea    0xdbc(%rip),%rdi
call   1060 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    129d <write_ppm+0x13d>
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
push   %rax
mov    %rcx,%r14
mov    %edx,%r12d
mov    %esi,%r13d
test   %rdi,%rdi
lea    0xe82(%rip),%rbp
cmovne %rdi,%rbp
lea    0xe79(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1213 <write_ppm+0xb3>
mov    %rax,%rbx
lea    0xe85(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe70(%rip),%rsi
xor    %r15d,%r15d
mov    %rbx,%rdi
mov    %r13d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
imul   %r12d,%r13d
lea    0x0(%r13,%r13,2),%ebp
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
cmp    %ebp,%eax
jne    1253 <write_ppm+0xf3>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%rax
jne    1241 <write_ppm+0xe1>
lea    0xe3b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x3,%r15d
jmp    1241 <write_ppm+0xe1>
mov    0x2dbe(%rip),%rax
movslq (%rax),%rbx
xor    %edi,%edi
call   1080 <strerror@plt>
lea    0xdd9(%rip),%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdda(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%r15d
jmp    1241 <write_ppm+0xe1>

LLVM IR (-O0):
@.str = [2 x i8] zeroinitializer
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"write_ppm failed to open %s: %s\0A\00"
@.str.3 = [4 x i8] c"P6\0A\00"
@.str.4 = [11 x i8] c"%d %d 255\0A\00"
@.str.5 = [13 x i8] c"Write failed\00"
@EOF = global i64 0
@.str.6 = [13 x i8] c"Close failed\00"
define i32 @write_ppm(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
store i32 0, i32* %11
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %4
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6
br label %15
15: ; preds = %14, %4
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %10
%18 = load i32*, i32** %10
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i32*
%24 = load i8*, i8** %6
%25 = load i32, i32* %11
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %24, i8* %26)
store i32 1, i32* %5
br label %57
28: ; preds = %15
%29 = load i32*, i32** %10
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32*, i32** %10
%32 = load i32, i32* %7
%33 = load i32, i32* %8
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 %33)
%35 = load i8*, i8** %9
%36 = load i32, i32* %7
%37 = load i32, i32* %8
%38 = mul i32 %36, %37
%39 = mul i32 %38, 3
%40 = load i32*, i32** %10
%41 = call i32 @fwrite(i8* %35, i32 1, i32 %39, i32* %40)
%42 = load i32, i32* %7
%43 = load i32, i32* %8
%44 = mul i32 %42, %43
%45 = mul i32 %44, 3
%46 = icmp ne i32 %41, %45
br i1 %46, label %47, label %49
47: ; preds = %28
%48 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %5
br label %57
49: ; preds = %28
%50 = load i32*, i32** %10
%51 = call i64 @fclose(i32* %50)
%52 = load i64, i64* @EOF
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 3, i32* %5
br label %57
56: ; preds = %49
store i32 0, i32* %5
br label %57
57: ; preds = %56, %54, %47, %20
%58 = load i32, i32* %5
ret i32 %58
}

LLVM IR (-O2):
@.str = [2 x i8] zeroinitializer
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"write_ppm failed to open %s: %s\0A\00"
@.str.3 = [4 x i8] c"P6\0A\00"
@.str.4 = [11 x i8] c"%d %d 255\0A\00"
@.str.5 = [13 x i8] c"Write failed\00"
@EOF = global i64 0
@.str.6 = [13 x i8] c"Close failed\00"
define i32 @write_ppm(i8* %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = icmp eq i8* %0, null
%6 = select i1 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %0
%7 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %15
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i32*
%13 = tail call i8* @strerror(i32 0)
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %6, i8* %13)
br label %30
15: ; preds = %4
%16 = 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.3, i64 0, i64 0), i64 3, i64 1, i32* %7)
%17 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %1, i32 %2)
%18 = mul i32 %1, 3
%19 = mul i32 %18, %2
%20 = tail call i32 @fwrite(i8* %3, i32 1, i32 %19, i32* %7)
%21 = icmp eq i32 %20, %19
br i1 %21, label %24, label %22
22: ; preds = %15
%23 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %30
24: ; preds = %15
%25 = tail call i64 @fclose(i32* %7)
%26 = load i64, i64* @EOF
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %30
30: ; preds = %24, %28, %22, %9
%31 = phi i32 [ 1, %9 ], [ 2, %22 ], [ 3, %28 ], [ 0, %24 ]
ret i32 %31
}