#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*/ error(char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ pgmWrite(int /*<<< orphan*/ *, unsigned char *, int, int);
void pgmWriteFile(char *fname, unsigned char *img, int ncols, int nrows) {
  FILE *fp;
  if ((fp = fopen(fname, "wb")) == NULL)
    error("(pgmWriteFile) Can't open file named '%s' for writing\n", fname);
  pgmWrite(fp, img, ncols, nrows);
  fclose(fp);
}


x86 assembly (clang, -O0):
<pgmWriteFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1184 <pgmWriteFile+0x44>
mov    -0x8(%rbp),%rsi
lea    0xe84(%rip),%rdi
call   1040 <error@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1060 <pgmWrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgmWriteFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1177 <pgmWriteFile+0x37>
lea    0xe94(%rip),%rdi
mov    %rbx,%rsi
call   1040 <error@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
call   1060 <pgmWrite@plt>
mov    %rbp,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [55 x i8] c"(pgmWriteFile) Can't open file named '%s' for writing\0A\00"
define void @pgmWriteFile(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i8*, i8** %5
%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 eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = load i8*, i8** %5
%15 = call i32 @error(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %4
%17 = load i32*, i32** %9
%18 = load i8*, i8** %6
%19 = load i32, i32* %7
%20 = load i32, i32* %8
%21 = call i32 @pgmWrite(i32* %17, i8* %18, i32 %19, i32 %20)
%22 = load i32*, i32** %9
%23 = call i32 @fclose(i32* %22)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [55 x i8] c"(pgmWriteFile) Can't open file named '%s' for writing\0A\00"
define void @pgmWriteFile(i8* %0, i8* %1, i32 %2, i32 %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 %7, label %9
7: ; preds = %4
%8 = tail call i32 @error(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %4
%10 = tail call i32 @pgmWrite(i32* %5, i8* %1, i32 %2, i32 %3)
%11 = tail call i32 @fclose(i32* %5)
ret void
}