#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*/ PPM_scan_ws(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char **);
int fscanf(int /*<<< orphan*/ *, char *, int *, int *, int *);
unsigned char getc(int /*<<< orphan*/ *);
scalar_t__ isdigit(unsigned char);
int /*<<< orphan*/ isspace(unsigned char);
unsigned char **malloc(int);
int ReadPPM3(const char *fname, unsigned char (**data)[3], int *w, int *h) {
  FILE *fp = fopen(fname, "r");
  unsigned char buf[2] = {0};
  unsigned char c;
  int maxval;
  int i, r, g, b;
  *data = NULL;
  if (!fp)
    goto close_fail;
  fread(buf, 1, 2, fp);
  if (buf[0] != 'P' || buf[1] != '3')
    goto close_fail;
  *w = *h = 0;
  maxval = 0;
  c = getc(fp);
  PPM_scan_ws(&c, fp);
  while (isdigit(c)) {
    *w = 10 * (*w) + c - '0';
    c = getc(fp);
  }
  PPM_scan_ws(&c, fp);
  while (isdigit(c)) {
    *h = 10 * (*h) + c - '0';
    c = getc(fp);
  }
  PPM_scan_ws(&c, fp);
  while (isdigit(c)) {
    maxval = 10 * maxval + c - '0';
    c = getc(fp);
  }
  if (!isspace(c) || maxval > 65536 || maxval <= 0)
    goto close_fail;
  if (*w <= 0 || *h <= 0)
    goto close_fail;
  *data = malloc((*w) * (*h) * sizeof **data);
  if (*data == NULL)
    goto nospace_fail;
  for (i = 0; i < (*w) * (*h); ++i) {
    if (fscanf(fp, "%d%d%d", &r, &g, &b) != 3)
      goto corrupt_fail;
    if (r < 0 || r > maxval || g < 0 || g > maxval || b < 0 || b > maxval)
      goto corrupt_fail;
    (*data)[i][0] = (r + 1) * 256 / (maxval + 1) - 1;
    (*data)[i][0] = (g + 1) * 256 / (maxval + 1) - 1;
    (*data)[i][0] = (b + 1) * 256 / (maxval + 1) - 1;
  }
  fclose(fp);
  return 0;
close_fail:
  if (*data)
    free(*data);
  *data = NULL;
  if (fp)
    fclose(fp);
  return -1;
corrupt_fail:
  fclose(fp);
  return -2;
nospace_fail:
  fclose(fp);
  return -3;
}


x86 assembly (clang, -O0):
<ReadPPM3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
lea    -0x32(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1070 <memset@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x30(%rbp)
jne    1207 <ReadPPM3+0x57>
jmp    14eb <ReadPPM3+0x33b>
lea    -0x32(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1050 <fread@plt>
movzbl -0x32(%rbp),%eax
cmp    $0x50,%eax
jne    1238 <ReadPPM3+0x88>
movzbl -0x31(%rbp),%eax
cmp    $0x33,%eax
je     123d <ReadPPM3+0x8d>
jmp    14eb <ReadPPM3+0x33b>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   10d0 <getc@plt>
mov    %al,-0x33(%rbp)
mov    -0x30(%rbp),%rsi
lea    -0x33(%rbp),%rdi
call   10c0 <PPM_scan_ws@plt>
movzbl -0x33(%rbp),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
je     12ab <ReadPPM3+0xfb>
mov    -0x20(%rbp),%rax
imul   $0xa,(%rax),%ecx
movzbl -0x33(%rbp),%eax
add    %eax,%ecx
sub    $0x30,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdi
call   10d0 <getc@plt>
mov    %al,-0x33(%rbp)
jmp    1271 <ReadPPM3+0xc1>
mov    -0x30(%rbp),%rsi
lea    -0x33(%rbp),%rdi
call   10c0 <PPM_scan_ws@plt>
movzbl -0x33(%rbp),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
je     12f2 <ReadPPM3+0x142>
mov    -0x28(%rbp),%rax
imul   $0xa,(%rax),%ecx
movzbl -0x33(%rbp),%eax
add    %eax,%ecx
sub    $0x30,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdi
call   10d0 <getc@plt>
mov    %al,-0x33(%rbp)
jmp    12b8 <ReadPPM3+0x108>
mov    -0x30(%rbp),%rsi
lea    -0x33(%rbp),%rdi
call   10c0 <PPM_scan_ws@plt>
movzbl -0x33(%rbp),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
je     1333 <ReadPPM3+0x183>
imul   $0xa,-0x38(%rbp),%eax
movzbl -0x33(%rbp),%ecx
add    %ecx,%eax
sub    $0x30,%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   10d0 <getc@plt>
mov    %al,-0x33(%rbp)
jmp    12ff <ReadPPM3+0x14f>
movzbl -0x33(%rbp),%edi
call   1090 <isspace@plt>
cmp    $0x0,%eax
je     135c <ReadPPM3+0x1ac>
cmpl   $0x10000,-0x38(%rbp)
jg     135c <ReadPPM3+0x1ac>
cmpl   $0x0,-0x38(%rbp)
jg     1361 <ReadPPM3+0x1b1>
jmp    14eb <ReadPPM3+0x33b>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jle    137b <ReadPPM3+0x1cb>
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
jg     1380 <ReadPPM3+0x1d0>
jmp    14eb <ReadPPM3+0x33b>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x28(%rbp),%rcx
imul   (%rcx),%eax
cltq
imul   $0x3,%rax,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    13b7 <ReadPPM3+0x207>
jmp    1545 <ReadPPM3+0x395>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
mov    -0x28(%rbp),%rdx
imul   (%rdx),%ecx
cmp    %ecx,%eax
jge    14d6 <ReadPPM3+0x326>
mov    -0x30(%rbp),%rdi
lea    0xc21(%rip),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x44(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1080 <fscanf@plt>
cmp    $0x3,%eax
je     1400 <ReadPPM3+0x250>
jmp    1530 <ReadPPM3+0x380>
cmpl   $0x0,-0x40(%rbp)
jl     1442 <ReadPPM3+0x292>
mov    -0x40(%rbp),%eax
cmp    -0x38(%rbp),%eax
jg     1442 <ReadPPM3+0x292>
cmpl   $0x0,-0x44(%rbp)
jl     1442 <ReadPPM3+0x292>
mov    -0x44(%rbp),%eax
cmp    -0x38(%rbp),%eax
jg     1442 <ReadPPM3+0x292>
cmpl   $0x0,-0x48(%rbp)
jl     1442 <ReadPPM3+0x292>
mov    -0x48(%rbp),%eax
cmp    -0x38(%rbp),%eax
jle    1447 <ReadPPM3+0x297>
jmp    1530 <ReadPPM3+0x380>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
shl    $0x8,%eax
mov    -0x38(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
sub    $0x1,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x3c(%rbp),%rdx
imul   $0x3,%rdx,%rdx
add    %rdx,%rax
mov    %cl,(%rax)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
shl    $0x8,%eax
mov    -0x38(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
sub    $0x1,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x3c(%rbp),%rdx
imul   $0x3,%rdx,%rdx
add    %rdx,%rax
mov    %cl,(%rax)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
shl    $0x8,%eax
mov    -0x38(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
sub    $0x1,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x3c(%rbp),%rdx
imul   $0x3,%rdx,%rdx
add    %rdx,%rax
mov    %cl,(%rax)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    13be <ReadPPM3+0x20e>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1555 <ReadPPM3+0x3a5>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1505 <ReadPPM3+0x355>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x30(%rbp)
je     1524 <ReadPPM3+0x374>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1555 <ReadPPM3+0x3a5>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1555 <ReadPPM3+0x3a5>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xfffffffd,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadPPM3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r13
lea    0xe42(%rip),%rsi
call   10a0 <fopen@plt>
movw   $0x0,0x6(%rsp)
movq   $0x0,0x0(%r13)
test   %rax,%rax
je     1271 <ReadPPM3+0xd1>
mov    %rax,%rbx
lea    0x6(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
cmpb   $0x50,0x6(%rsp)
jne    1462 <ReadPPM3+0x2c2>
cmpb   $0x33,0x7(%rsp)
jne    1462 <ReadPPM3+0x2c2>
movl   $0x0,(%r15)
movl   $0x0,(%r12)
mov    %rbx,%rdi
call   10c0 <getc@plt>
mov    %al,0x5(%rsp)
lea    0x5(%rsp),%rdi
mov    %rbx,%rsi
call   10b0 <PPM_scan_ws@plt>
movzbl 0x5(%rsp),%edi
nopl   0x0(%rax,%rax,1)
call   1040 <isdigit@plt>
test   %rax,%rax
je     127c <ReadPPM3+0xdc>
mov    (%r12),%eax
lea    (%rax,%rax,4),%eax
movzbl 0x5(%rsp),%ecx
lea    (%rcx,%rax,2),%eax
add    $0xffffffd0,%eax
mov    %eax,(%r12)
mov    %rbx,%rdi
call   10c0 <getc@plt>
mov    %al,0x5(%rsp)
movzbl %al,%edi
jmp    1240 <ReadPPM3+0xa0>
mov    $0xffffffff,%r13d
jmp    1486 <ReadPPM3+0x2e6>
lea    0x5(%rsp),%rdi
mov    %rbx,%rsi
call   10b0 <PPM_scan_ws@plt>
movzbl 0x5(%rsp),%edi
xchg   %ax,%ax
call   1040 <isdigit@plt>
test   %rax,%rax
je     12bf <ReadPPM3+0x11f>
mov    (%r15),%eax
lea    (%rax,%rax,4),%eax
movzbl 0x5(%rsp),%ecx
lea    (%rcx,%rax,2),%eax
add    $0xffffffd0,%eax
mov    %eax,(%r15)
mov    %rbx,%rdi
call   10c0 <getc@plt>
mov    %al,0x5(%rsp)
movzbl %al,%edi
jmp    1290 <ReadPPM3+0xf0>
lea    0x5(%rsp),%rdi
mov    %rbx,%rsi
call   10b0 <PPM_scan_ws@plt>
movzbl 0x5(%rsp),%edi
call   1040 <isdigit@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1308 <ReadPPM3+0x168>
nopl   (%rax)
lea    0x0(%rbp,%rbp,4),%eax
movzbl 0x5(%rsp),%ecx
lea    (%rcx,%rax,2),%ebp
add    $0xffffffd0,%ebp
mov    %rbx,%rdi
call   10c0 <getc@plt>
mov    %al,0x5(%rsp)
movzbl %al,%edi
call   1040 <isdigit@plt>
test   %rax,%rax
jne    12e0 <ReadPPM3+0x140>
movzbl 0x5(%rsp),%edi
call   1080 <isspace@plt>
test   %eax,%eax
je     1462 <ReadPPM3+0x2c2>
cmp    $0x10000,%ebp
jg     1462 <ReadPPM3+0x2c2>
test   %ebp,%ebp
jle    1462 <ReadPPM3+0x2c2>
mov    (%r12),%r14d
test   %r14d,%r14d
jle    1462 <ReadPPM3+0x2c2>
mov    (%r15),%eax
test   %eax,%eax
jle    1462 <ReadPPM3+0x2c2>
imul   %eax,%r14d
lea    (%r14,%r14,2),%edi
call   1090 <malloc@plt>
mov    %rax,0x0(%r13)
test   %rax,%rax
je     1498 <ReadPPM3+0x2f8>
test   %r14d,%r14d
jle    14a0 <ReadPPM3+0x300>
mov    %r13,0x18(%rsp)
lea    0x1(%rbp),%eax
mov    %eax,0x10(%rsp)
xor    %edi,%edi
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %rdi,0x20(%rsp)
mov    %rbx,%rdi
lea    0xc73(%rip),%rsi
lea    0x14(%rsp),%rdx
lea    0xc(%rsp),%rcx
lea    0x8(%rsp),%r8
call   1070 <fscanf@plt>
mov    $0xfffffffe,%r13d
cmp    $0x3,%eax
jne    147e <ReadPPM3+0x2de>
mov    0x14(%rsp),%eax
test   %eax,%eax
js     147e <ReadPPM3+0x2de>
cmp    %ebp,%eax
jg     147e <ReadPPM3+0x2de>
mov    0xc(%rsp),%ecx
test   %ecx,%ecx
js     147e <ReadPPM3+0x2de>
cmp    %ebp,%ecx
jg     147e <ReadPPM3+0x2de>
mov    0x8(%rsp),%ecx
test   %ecx,%ecx
js     147e <ReadPPM3+0x2de>
cmp    %ebp,%ecx
mov    0x20(%rsp),%rdi
jg     147e <ReadPPM3+0x2de>
shl    $0x8,%eax
add    $0x100,%eax
cltd
mov    0x10(%rsp),%r9d
idiv   %r9d
add    $0xff,%al
mov    0x18(%rsp),%r8
mov    (%r8),%rcx
mov    %al,(%rcx,%rdi,1)
mov    0xc(%rsp),%eax
shl    $0x8,%eax
add    $0x100,%eax
cltd
idiv   %r9d
add    $0xff,%al
mov    (%r8),%rcx
mov    %al,(%rcx,%rdi,1)
mov    0x8(%rsp),%eax
shl    $0x8,%eax
add    $0x100,%eax
cltd
idiv   %r9d
add    $0xff,%al
mov    (%r8),%rcx
mov    %al,(%rcx,%rdi,1)
add    $0x1,%r14
movslq (%r12),%rax
movslq (%r15),%rcx
imul   %rax,%rcx
add    $0x3,%rdi
xor    %r13d,%r13d
cmp    %rcx,%r14
jl     1380 <ReadPPM3+0x1e0>
jmp    147e <ReadPPM3+0x2de>
mov    0x0(%r13),%rdi
test   %rdi,%rdi
je     1470 <ReadPPM3+0x2d0>
call   1030 <free@plt>
movq   $0x0,0x0(%r13)
mov    $0xffffffff,%r13d
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xfffffffd,%r13d
jmp    147e <ReadPPM3+0x2de>
xor    %r13d,%r13d
jmp    147e <ReadPPM3+0x2de>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [7 x i8] c"%d%d%d\00"
define i32 @ReadPPM3(i8* %0, [3 x i8]** %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [3 x i8]**
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca [2 x i8]
%12 = alloca i8
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %6
store [3 x i8]** %1, [3 x i8]*** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%18 = load i8*, i8** %6
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %10
%20 = bitcast [2 x i8]* %11 to i8*
%21 = load [3 x i8]**, [3 x i8]*** %7
store [3 x i8]* null, [3 x i8]** %21
%22 = load i32*, i32** %10
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %4
br label %216
25: ; preds = %4
%26 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%27 = load i32*, i32** %10
%28 = call i32 @fread(i8* %26, i32 1, i32 2, i32* %27)
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = icmp ne i32 %31, 80
br i1 %32, label %38, label %33
33: ; preds = %25
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 1
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = icmp ne i32 %36, 51
br i1 %37, label %38, label %39
38: ; preds = %33, %25
br label %216
39: ; preds = %33
%40 = load i32*, i32** %9
store i32 0, i32* %40
%41 = load i32*, i32** %8
store i32 0, i32* %41
store i32 0, i32* %13
%42 = load i32*, i32** %10
%43 = call zeroext i8 @getc(i32* %42)
store i8 %43, i8* %12
%44 = load i32*, i32** %10
%45 = call i32 @PPM_scan_ws(i8* %12, i32* %44)
br label %46
46: ; preds = %50, %39
%47 = load i8, i8* %12
%48 = call i64 @isdigit(i8 zeroext %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %61
50: ; preds = %46
%51 = load i32*, i32** %8
%52 = load i32, i32* %51
%53 = mul nsw i32 10, %52
%54 = load i8, i8* %12
%55 = zext i8 %54 to i32
%56 = add nsw i32 %53, %55
%57 = sub nsw i32 %56, 48
%58 = load i32*, i32** %8
store i32 %57, i32* %58
%59 = load i32*, i32** %10
%60 = call zeroext i8 @getc(i32* %59)
store i8 %60, i8* %12
br label %46
61: ; preds = %46
%62 = load i32*, i32** %10
%63 = call i32 @PPM_scan_ws(i8* %12, i32* %62)
br label %64
64: ; preds = %68, %61
%65 = load i8, i8* %12
%66 = call i64 @isdigit(i8 zeroext %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %79
68: ; preds = %64
%69 = load i32*, i32** %9
%70 = load i32, i32* %69
%71 = mul nsw i32 10, %70
%72 = load i8, i8* %12
%73 = zext i8 %72 to i32
%74 = add nsw i32 %71, %73
%75 = sub nsw i32 %74, 48
%76 = load i32*, i32** %9
store i32 %75, i32* %76
%77 = load i32*, i32** %10
%78 = call zeroext i8 @getc(i32* %77)
store i8 %78, i8* %12
br label %64
79: ; preds = %64
%80 = load i32*, i32** %10
%81 = call i32 @PPM_scan_ws(i8* %12, i32* %80)
br label %82
82: ; preds = %86, %79
%83 = load i8, i8* %12
%84 = call i64 @isdigit(i8 zeroext %83)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %95
86: ; preds = %82
%87 = load i32, i32* %13
%88 = mul nsw i32 10, %87
%89 = load i8, i8* %12
%90 = zext i8 %89 to i32
%91 = add nsw i32 %88, %90
%92 = sub nsw i32 %91, 48
store i32 %92, i32* %13
%93 = load i32*, i32** %10
%94 = call zeroext i8 @getc(i32* %93)
store i8 %94, i8* %12
br label %82
95: ; preds = %82
%96 = load i8, i8* %12
%97 = call i32 @isspace(i8 zeroext %96)
%98 = icmp ne i32 %97, 0
br i1 %98, label %99, label %105
99: ; preds = %95
%100 = load i32, i32* %13
%101 = icmp sgt i32 %100, 65536
br i1 %101, label %105, label %102
102: ; preds = %99
%103 = load i32, i32* %13
%104 = icmp sle i32 %103, 0
br i1 %104, label %105, label %106
105: ; preds = %102, %99, %95
br label %216
106: ; preds = %102
%107 = load i32*, i32** %8
%108 = load i32, i32* %107
%109 = icmp sle i32 %108, 0
br i1 %109, label %114, label %110
110: ; preds = %106
%111 = load i32*, i32** %9
%112 = load i32, i32* %111
%113 = icmp sle i32 %112, 0
br i1 %113, label %114, label %115
114: ; preds = %110, %106
br label %216
115: ; preds = %110
%116 = load i32*, i32** %8
%117 = load i32, i32* %116
%118 = load i32*, i32** %9
%119 = load i32, i32* %118
%120 = mul nsw i32 %117, %119
%121 = sext i32 %120 to i64
%122 = mul i64 %121, 3
%123 = trunc i64 %122 to i32
%124 = call i8** @malloc(i32 %123)
%125 = bitcast i8** %124 to [3 x i8]*
%126 = load [3 x i8]**, [3 x i8]*** %7
store [3 x i8]* %125, [3 x i8]** %126
%127 = load [3 x i8]**, [3 x i8]*** %7
%128 = load [3 x i8]*, [3 x i8]** %127
%129 = icmp eq [3 x i8]* %128, null
br i1 %129, label %130, label %131
130: ; preds = %115
br label %236
131: ; preds = %115
store i32 0, i32* %14
br label %132
132: ; preds = %210, %131
%133 = load i32, i32* %14
%134 = load i32*, i32** %8
%135 = load i32, i32* %134
%136 = load i32*, i32** %9
%137 = load i32, i32* %136
%138 = mul nsw i32 %135, %137
%139 = icmp slt i32 %133, %138
br i1 %139, label %140, label %213
140: ; preds = %132
%141 = load i32*, i32** %10
%142 = call i32 @fscanf(i32* %141, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* %15, i32* %16, i32* %17)
%143 = icmp ne i32 %142, 3
br i1 %143, label %144, label %145
144: ; preds = %140
br label %233
145: ; preds = %140
%146 = load i32, i32* %15
%147 = icmp slt i32 %146, 0
br i1 %147, label %166, label %148
148: ; preds = %145
%149 = load i32, i32* %15
%150 = load i32, i32* %13
%151 = icmp sgt i32 %149, %150
br i1 %151, label %166, label %152
152: ; preds = %148
%153 = load i32, i32* %16
%154 = icmp slt i32 %153, 0
br i1 %154, label %166, label %155
155: ; preds = %152
%156 = load i32, i32* %16
%157 = load i32, i32* %13
%158 = icmp sgt i32 %156, %157
br i1 %158, label %166, label %159
159: ; preds = %155
%160 = load i32, i32* %17
%161 = icmp slt i32 %160, 0
br i1 %161, label %166, label %162
162: ; preds = %159
%163 = load i32, i32* %17
%164 = load i32, i32* %13
%165 = icmp sgt i32 %163, %164
br i1 %165, label %166, label %167
166: ; preds = %162, %159, %155, %152, %148, %145
br label %233
167: ; preds = %162
%168 = load i32, i32* %15
%169 = add nsw i32 %168, 1
%170 = mul nsw i32 %169, 256
%171 = load i32, i32* %13
%172 = add nsw i32 %171, 1
%173 = sdiv i32 %170, %172
%174 = sub nsw i32 %173, 1
%175 = trunc i32 %174 to i8
%176 = load [3 x i8]**, [3 x i8]*** %7
%177 = load [3 x i8]*, [3 x i8]** %176
%178 = load i32, i32* %14
%179 = sext i32 %178 to i64
%180 = getelementptr inbounds [3 x i8], [3 x i8]* %177, i64 %179
%181 = getelementptr inbounds [3 x i8], [3 x i8]* %180, i64 0, i64 0
store i8 %175, i8* %181
%182 = load i32, i32* %16
%183 = add nsw i32 %182, 1
%184 = mul nsw i32 %183, 256
%185 = load i32, i32* %13
%186 = add nsw i32 %185, 1
%187 = sdiv i32 %184, %186
%188 = sub nsw i32 %187, 1
%189 = trunc i32 %188 to i8
%190 = load [3 x i8]**, [3 x i8]*** %7
%191 = load [3 x i8]*, [3 x i8]** %190
%192 = load i32, i32* %14
%193 = sext i32 %192 to i64
%194 = getelementptr inbounds [3 x i8], [3 x i8]* %191, i64 %193
%195 = getelementptr inbounds [3 x i8], [3 x i8]* %194, i64 0, i64 0
store i8 %189, i8* %195
%196 = load i32, i32* %17
%197 = add nsw i32 %196, 1
%198 = mul nsw i32 %197, 256
%199 = load i32, i32* %13
%200 = add nsw i32 %199, 1
%201 = sdiv i32 %198, %200
%202 = sub nsw i32 %201, 1
%203 = trunc i32 %202 to i8
%204 = load [3 x i8]**, [3 x i8]*** %7
%205 = load [3 x i8]*, [3 x i8]** %204
%206 = load i32, i32* %14
%207 = sext i32 %206 to i64
%208 = getelementptr inbounds [3 x i8], [3 x i8]* %205, i64 %207
%209 = getelementptr inbounds [3 x i8], [3 x i8]* %208, i64 0, i64 0
store i8 %203, i8* %209
br label %210
210: ; preds = %167
%211 = load i32, i32* %14
%212 = add nsw i32 %211, 1
store i32 %212, i32* %14
br label %132
213: ; preds = %132
%214 = load i32*, i32** %10
%215 = call i32 @fclose(i32* %214)
store i32 0, i32* %5
br label %239
216: ; preds = %114, %105, %38, %24
%217 = load [3 x i8]**, [3 x i8]*** %7
%218 = load [3 x i8]*, [3 x i8]** %217
%219 = icmp ne [3 x i8]* %218, null
br i1 %219, label %220, label %225
220: ; preds = %216
%221 = load [3 x i8]**, [3 x i8]*** %7
%222 = load [3 x i8]*, [3 x i8]** %221
%223 = bitcast [3 x i8]* %222 to i8**
%224 = call i32 @free(i8** %223)
br label %225
225: ; preds = %220, %216
%226 = load [3 x i8]**, [3 x i8]*** %7
store [3 x i8]* null, [3 x i8]** %226
%227 = load i32*, i32** %10
%228 = icmp ne i32* %227, null
br i1 %228, label %229, label %232
229: ; preds = %225
%230 = load i32*, i32** %10
%231 = call i32 @fclose(i32* %230)
br label %232
232: ; preds = %229, %225
store i32 -1, i32* %5
br label %239
233: ; preds = %166, %144
%234 = load i32*, i32** %10
%235 = call i32 @fclose(i32* %234)
store i32 -2, i32* %5
br label %239
236: ; preds = %130
%237 = load i32*, i32** %10
%238 = call i32 @fclose(i32* %237)
store i32 -3, i32* %5
br label %239
239: ; preds = %236, %233, %232, %213
%240 = load i32, i32* %5
ret i32 %240
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [7 x i8] c"%d%d%d\00"
define i32 @ReadPPM3(i8* nocapture %0, [3 x i8]** nocapture %1, i32* nocapture %2, i32* nocapture %3) {
%5 = alloca i16
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = bitcast i16* %5 to i8*
store i16 0, i16* %5
%12 = bitcast i32* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = bitcast i32* %9 to i8*
store [3 x i8]* null, [3 x i8]** %1
%15 = icmp eq i32* %10, null
br i1 %15, label %156, label %16
16: ; preds = %4
%17 = bitcast i16* %5 to [2 x i8]*
%18 = call i32 @fread(i8* %11, i32 1, i32 2, i32* %10)
%19 = load i8, i8* %11
%20 = icmp ne i8 %19, 80
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %17, i64 0, i64 1
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 51
%24 = select i1 %20, i1 true, i1 %23
br i1 %24, label %146, label %25
25: ; preds = %16
store i32 0, i32* %3
store i32 0, i32* %2
%26 = tail call zeroext i8 @getc(i32* %10)
store i8 %26, i8* %6
%27 = call i32 @PPM_scan_ws(i8* %6, i32* %10)
%28 = load i8, i8* %6
%29 = call i64 @isdigit(i8 zeroext %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %41, label %31
31: ; preds = %25, %31
%32 = load i32, i32* %2
%33 = mul nsw i32 %32, 10
%34 = load i8, i8* %6
%35 = zext i8 %34 to i32
%36 = add i32 %33, -48
%37 = add i32 %36, %35
store i32 %37, i32* %2
%38 = call zeroext i8 @getc(i32* %10)
store i8 %38, i8* %6
%39 = call i64 @isdigit(i8 zeroext %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %31
41: ; preds = %31, %25
%42 = call i32 @PPM_scan_ws(i8* %6, i32* %10)
%43 = load i8, i8* %6
%44 = call i64 @isdigit(i8 zeroext %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %56, label %46
46: ; preds = %41, %46
%47 = load i32, i32* %3
%48 = mul nsw i32 %47, 10
%49 = load i8, i8* %6
%50 = zext i8 %49 to i32
%51 = add i32 %48, -48
%52 = add i32 %51, %50
store i32 %52, i32* %3
%53 = call zeroext i8 @getc(i32* %10)
store i8 %53, i8* %6
%54 = call i64 @isdigit(i8 zeroext %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %46
56: ; preds = %46, %41
%57 = call i32 @PPM_scan_ws(i8* %6, i32* %10)
%58 = load i8, i8* %6
%59 = call i64 @isdigit(i8 zeroext %58)
%60 = icmp eq i64 %59, 0
br i1 %60, label %71, label %61
61: ; preds = %56, %61
%62 = phi i32 [ %67, %61 ], [ 0, %56 ]
%63 = mul nsw i32 %62, 10
%64 = load i8, i8* %6
%65 = zext i8 %64 to i32
%66 = add i32 %63, -48
%67 = add i32 %66, %65
%68 = call zeroext i8 @getc(i32* %10)
store i8 %68, i8* %6
%69 = call i64 @isdigit(i8 zeroext %68)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %61
71: ; preds = %61, %56
%72 = phi i32 [ 0, %56 ], [ %67, %61 ]
%73 = load i8, i8* %6
%74 = call i32 @isspace(i8 zeroext %73)
%75 = icmp eq i32 %74, 0
%76 = icmp sgt i32 %72, 65536
%77 = select i1 %75, i1 true, i1 %76
%78 = icmp slt i32 %72, 1
%79 = select i1 %77, i1 true, i1 %78
br i1 %79, label %146, label %80
80: ; preds = %71
%81 = load i32, i32* %2
%82 = icmp slt i32 %81, 1
br i1 %82, label %146, label %83
83: ; preds = %80
%84 = load i32, i32* %3
%85 = icmp slt i32 %84, 1
br i1 %85, label %146, label %86
86: ; preds = %83
%87 = mul i32 %81, 3
%88 = mul i32 %87, %84
%89 = call i8** @malloc(i32 %88)
%90 = bitcast [3 x i8]** %1 to i8***
store i8** %89, i8*** %90
%91 = icmp eq i8** %89, null
br i1 %91, label %153, label %92
92: ; preds = %86
%93 = mul nsw i32 %84, %81
%94 = icmp sgt i32 %93, 0
br i1 %94, label %95, label %153
95: ; preds = %92
%96 = add nuw nsw i32 %72, 1
br label %97
97: ; preds = %95, %116
%98 = phi i64 [ 0, %95 ], [ %140, %116 ]
%99 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* %7, i32* %8, i32* %9)
%100 = icmp ne i32 %99, 3
%101 = load i32, i32* %7
%102 = icmp slt i32 %101, 0
%103 = select i1 %100, i1 true, i1 %102
br i1 %103, label %153, label %104
104: ; preds = %97
%105 = icmp sgt i32 %101, %72
%106 = load i32, i32* %8
%107 = icmp slt i32 %106, 0
%108 = select i1 %105, i1 true, i1 %107
br i1 %108, label %153, label %109
109: ; preds = %104
%110 = icmp sgt i32 %106, %72
%111 = load i32, i32* %9
%112 = icmp slt i32 %111, 0
%113 = select i1 %110, i1 true, i1 %112
%114 = icmp sgt i32 %111, %72
%115 = select i1 %113, i1 true, i1 %114
br i1 %115, label %153, label %116
116: ; preds = %109
%117 = shl i32 %101, 8
%118 = add i32 %117, 256
%119 = sdiv i32 %118, %96
%120 = trunc i32 %119 to i8
%121 = add i8 %120, -1
%122 = load [3 x i8]*, [3 x i8]** %1
%123 = getelementptr inbounds [3 x i8], [3 x i8]* %122, i64 %98, i64 0
store i8 %121, i8* %123
%124 = load i32, i32* %8
%125 = shl i32 %124, 8
%126 = add i32 %125, 256
%127 = sdiv i32 %126, %96
%128 = trunc i32 %127 to i8
%129 = add i8 %128, -1
%130 = load [3 x i8]*, [3 x i8]** %1
%131 = getelementptr inbounds [3 x i8], [3 x i8]* %130, i64 %98, i64 0
store i8 %129, i8* %131
%132 = load i32, i32* %9
%133 = shl i32 %132, 8
%134 = add i32 %133, 256
%135 = sdiv i32 %134, %96
%136 = trunc i32 %135 to i8
%137 = add i8 %136, -1
%138 = load [3 x i8]*, [3 x i8]** %1
%139 = getelementptr inbounds [3 x i8], [3 x i8]* %138, i64 %98, i64 0
store i8 %137, i8* %139
%140 = add nuw nsw i64 %98, 1
%141 = load i32, i32* %2
%142 = load i32, i32* %3
%143 = mul nsw i32 %142, %141
%144 = sext i32 %143 to i64
%145 = icmp slt i64 %140, %144
br i1 %145, label %97, label %153
146: ; preds = %80, %83, %71, %16
%147 = load [3 x i8]*, [3 x i8]** %1
%148 = icmp eq [3 x i8]* %147, null
br i1 %148, label %152, label %149
149: ; preds = %146
%150 = bitcast [3 x i8]* %147 to i8**
%151 = call i32 @free(i8** %150)
br label %152
152: ; preds = %146, %149
store [3 x i8]* null, [3 x i8]** %1
br label %153
153: ; preds = %97, %109, %104, %116, %86, %92, %152
%154 = phi i32 [ -1, %152 ], [ 0, %92 ], [ -3, %86 ], [ 0, %116 ], [ -2, %104 ], [ -2, %109 ], [ -2, %97 ]
%155 = call i32 @fclose(i32* %10)
br label %156
156: ; preds = %153, %4
%157 = phi i32 [ -1, %4 ], [ %154, %153 ]
ret i32 %157
}