#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*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
void pgmWrite(FILE *fp, unsigned char *img, int ncols, int nrows) {
  int i;
  fprintf(fp, "P5\n");
  fprintf(fp, "%d %d\n", ncols, nrows);
  fprintf(fp, "255\n");
  for (i = 0; i < nrows; i++) {
    fwrite(img, ncols, 1, fp);
    img += ncols;
  }
}


x86 assembly (clang, -O0):
<pgmWrite>:
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    0xebf(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xeab(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x8(%rbp),%rdi
lea    0xea0(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11b9 <pgmWrite+0x99>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fwrite@plt>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1179 <pgmWrite+0x59>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgmWrite>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%r15
lea    0xec6(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fwrite@plt>
lea    0xeb1(%rip),%rsi
mov    %r15,%rdi
mov    %r14d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1030 <fprintf@plt>
lea    0xea2(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fwrite@plt>
test   %ebx,%ebx
jle    11ab <pgmWrite+0x8b>
movslq %r14d,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fwrite@plt>
add    %r12,%rbp
add    $0xffffffff,%ebx
jne    1190 <pgmWrite+0x70>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"P5\0A\00"
@.str.1 = [7 x i8] c"%d %d\0A\00"
@.str.2 = [5 x i8] c"255\0A\00"
define void @pgmWrite(i32* %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = 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, i64 0, i64 0), i64 3, i64 1, i32* %0)
%6 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %3)
%7 = 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.2, i64 0, i64 0), i64 4, i64 1, i32* %0)
%8 = icmp sgt i32 %3, 0
br i1 %8, label %9, label %18
9: ; preds = %4
%10 = sext i32 %2 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i32 [ 0, %9 ], [ %16, %11 ]
%13 = phi i8* [ %1, %9 ], [ %15, %11 ]
%14 = tail call i32 @fwrite(i8* %13, i32 %2, i32 1, i32* %0)
%15 = getelementptr inbounds i8, i8* %13, i64 %10
%16 = add nuw nsw i32 %12, 1
%17 = icmp eq i32 %16, %3
br i1 %17, label %18, label %11
18: ; preds = %11, %4
ret void
}