#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 findMax(unsigned char *, unsigned int, unsigned int);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
void encodePGM(const char *filename, unsigned char *image, unsigned int width,
               unsigned int height) {
  FILE *pgmimage;
  pgmimage = fopen(filename, "wb");
  fputs("P2\n", pgmimage);
  fprintf(pgmimage, "%d ", width);
  fprintf(pgmimage, "%d\n", height);
  fprintf(pgmimage, "%d\n", findMax(image, width, height));
  for (int i = 0; i < width * height; i++) {
    if (i % 17 == 0)
      fprintf(pgmimage, "\n");
    fprintf(pgmimage, "%d ", image[i]);
  }
  fclose(pgmimage);
}


x86 assembly (clang, -O0):
<encodePGM>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe7e(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe6f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xe5e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1070 <findMax@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
imul   -0x18(%rbp),%ecx
cmp    %ecx,%eax
jae    124a <encodePGM+0xfa>
mov    -0x24(%rbp),%eax
mov    $0x11,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    121e <encodePGM+0xce>
mov    -0x20(%rbp),%rdi
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11e6 <encodePGM+0x96>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encodePGM>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xe86(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r13
lea    0xe7a(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe65(%rip),%rsi
mov    %r13,%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe55(%rip),%r12
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rdi
mov    %r15d,%esi
mov    %ebp,%edx
call   1080 <findMax@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
imul   %r15d,%ebp
test   %ebp,%ebp
je     1248 <encodePGM+0xe8>
mov    %ebp,%ebp
lea    0xe15(%rip),%r15
xor    %ebx,%ebx
xor    %r12d,%r12d
jmp    121e <encodePGM+0xbe>
nopl   0x0(%rax)
movzbl (%r14,%r12,1),%edx
mov    %r13,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r12
add    $0xffffffff,%ebx
cmp    %r12,%rbp
je     1248 <encodePGM+0xe8>
mov    %r12d,%eax
mov    $0xf0f0f0f1,%ecx
imul   %rcx,%rax
shr    $0x24,%rax
mov    %eax,%ecx
shl    $0x4,%ecx
add    %eax,%ecx
add    %ebx,%ecx
jne    1200 <encodePGM+0xa0>
mov    $0xa,%edi
mov    %r13,%rsi
call   1040 <fputc@plt>
jmp    1200 <encodePGM+0xa0>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [4 x i8] c"P2\0A\00"
@.str.2 = [4 x i8] c"%d \00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @encodePGM(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %9
%13 = load i32*, i32** %9
%14 = call i32 @fputs(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %13)
%15 = load i32*, i32** %9
%16 = load i32, i32* %7
%17 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = load i32*, i32** %9
%19 = load i32, i32* %8
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = load i32*, i32** %9
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = call i32 @findMax(i8* %22, i32 %23, i32 %24)
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %25)
store i32 0, i32* %10
br label %27
27: ; preds = %49, %4
%28 = load i32, i32* %10
%29 = load i32, i32* %7
%30 = load i32, i32* %8
%31 = mul i32 %29, %30
%32 = icmp ult i32 %28, %31
br i1 %32, label %33, label %52
33: ; preds = %27
%34 = load i32, i32* %10
%35 = srem i32 %34, 17
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32*, i32** %9
%39 = call i32 (i32*, i8*, ...) @fprintf(i32* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %37, %33
%41 = load i32*, i32** %9
%42 = load i8*, i8** %6
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = call i32 (i32*, i8*, ...) @fprintf(i32* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %40
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %27
52: ; preds = %27
%53 = load i32*, i32** %9
%54 = call i32 @fclose(i32* %53)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [4 x i8] c"P2\0A\00"
@.str.2 = [4 x i8] c"%d \00"
@.str.3 = [4 x i8] c"%d\0A\00"
define void @encodePGM(i8* nocapture %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 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 3, i64 1, i32* %5)
%7 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%8 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %3)
%9 = tail call i32 @findMax(i8* %1, i32 %2, i32 %3)
%10 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%11 = mul i32 %3, %2
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %4
%14 = zext i32 %11 to i64
br label %17
15: ; preds = %24, %4
%16 = tail call i32 @fclose(i32* %5)
ret void
17: ; preds = %13, %24
%18 = phi i64 [ 0, %13 ], [ %29, %24 ]
%19 = trunc i64 %18 to i32
%20 = urem i32 %19, 17
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @fputc(i32 10, i32* %5)
br label %24
24: ; preds = %22, %17
%25 = getelementptr inbounds i8, i8* %1, i64 %18
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = add nuw nsw i64 %18, 1
%30 = icmp eq i64 %29, %14
br i1 %30, label %15, label %17
}