#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 fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void export_8bit_pgm(unsigned char *buf, int width, int height,
                     char *filename) {
  FILE *f;
  if (!(f = fopen(filename, "wb"))) {
    printf("ERR:8bit_pgm\n");
    return;
  }
  fprintf(f, "P5\n%d %d\n255\n", width, height);
  if (fwrite(buf, 1, width * height, f) != width * height) {
    printf("ERR:export_float_pgm:fwrite\n");
    return;
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<export_8bit_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1195 <export_8bit_pgm+0x45>
lea    0xe78(%rip),%rdi
call   1040 <printf@plt>
jmp    11ed <export_8bit_pgm+0x9d>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe6b(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
imul   -0x10(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    -0xc(%rbp),%ecx
imul   -0x10(%rbp),%ecx
cmp    %ecx,%eax
je     11e4 <export_8bit_pgm+0x94>
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
jmp    11ed <export_8bit_pgm+0x9d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<export_8bit_pgm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r15
lea    0xe9b(%rip),%rsi
mov    %rcx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b3 <export_8bit_pgm+0x63>
mov    %rax,%r14
lea    0xe87(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1050 <fprintf@plt>
imul   %ebp,%ebx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
cmp    %ebx,%eax
jne    11bc <export_8bit_pgm+0x6c>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xe57(%rip),%rdi
jmp    11c3 <export_8bit_pgm+0x73>
lea    0xe5b(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [14 x i8] c"ERR:8bit_pgm\0A\00"
@.str.2 = [14 x i8] c"P5\0A%d %d\0A255\0A\00"
@.str.3 = [29 x i8] c"ERR:export_float_pgm:fwrite\0A\00"
define void @export_8bit_pgm(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%10 = load i8*, i8** %8
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = icmp ne i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %4
%14 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %35
15: ; preds = %4
%16 = load i32*, i32** %9
%17 = load i32, i32* %6
%18 = load i32, i32* %7
%19 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %18)
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = mul nsw i32 %21, %22
%24 = load i32*, i32** %9
%25 = call i32 @fwrite(i8* %20, i32 1, i32 %23, i32* %24)
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = mul nsw i32 %26, %27
%29 = icmp ne i32 %25, %28
br i1 %29, label %30, label %32
30: ; preds = %15
%31 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %35
32: ; preds = %15
%33 = load i32*, i32** %9
%34 = call i32 @fclose(i32* %33)
br label %35
35: ; preds = %32, %30, %13
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.2 = [14 x i8] c"P5\0A%d %d\0A255\0A\00"
@str = [13 x i8] c"ERR:8bit_pgm\00"
@str.4 = [28 x i8] c"ERR:export_float_pgm:fwrite\00"
define void @export_8bit_pgm(i8* nocapture %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %18
9: ; preds = %4
%10 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %2)
%11 = mul nsw i32 %2, %1
%12 = tail call i32 @fwrite(i8* %0, i32 1, i32 %11, i32* %5)
%13 = icmp eq i32 %12, %11
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.4, i64 0, i64 0))
br label %18
16: ; preds = %9
%17 = tail call i32 @fclose(i32* %5)
br label %18
18: ; preds = %16, %14, %7
ret void
}