#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int);
int read_pnm_header(int /*<<< orphan*/ *, int *, int *, int *);
unsigned char *load_ppm(const char *filename, int *width, int *height) {
  unsigned char *data = NULL;
  int max, w, h;
  size_t c;
  FILE *f = fopen(filename, "rb");
  if (!f)
    return NULL;
  if (read_pnm_header(f, &w, &h, &max) != 6) {
    fclose(f);
    return NULL;
  }
  data = (unsigned char *)malloc(3 * w * h);
  if (data == NULL) {
    printf("Cannot allocate %d bytes for memory.\n", (3 * w * h));
    exit(-1);
  }
  c = fread(data, 1, 3 * w * h, f);
  if (c != w * h * 3) {
    free(data);
    data = NULL;
  }
  *width = w;
  *height = h;
  return data;
}


x86 assembly (clang, -O0):
<load_ppm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11c8 <load_ppm+0x48>
movq   $0x0,-0x8(%rbp)
jmp    1299 <load_ppm+0x119>
mov    -0x48(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1070 <read_pnm_header@plt>
cmp    $0x6,%eax
je     11fc <load_ppm+0x7c>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1299 <load_ppm+0x119>
imul   $0x3,-0x30(%rbp),%edi
imul   -0x34(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1236 <load_ppm+0xb6>
imul   $0x3,-0x30(%rbp),%esi
imul   -0x34(%rbp),%esi
lea    0xddc(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rdi
imul   $0x3,-0x30(%rbp),%edx
imul   -0x34(%rbp),%edx
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%ecx
imul   -0x34(%rbp),%ecx
imul   $0x3,%ecx,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
je     127f <load_ppm+0xff>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_ppm>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe68(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1235 <load_ppm+0xb5>
mov    %rax,%rbx
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
lea    0x14(%rsp),%rcx
mov    %rax,%rdi
call   1070 <read_pnm_header@plt>
cmp    $0x6,%eax
jne    122d <load_ppm+0xad>
mov    0x10(%rsp),%eax
imul   0xc(%rsp),%eax
lea    (%rax,%rax,2),%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    0x10(%rsp),%eax
imul   0xc(%rsp),%eax
lea    (%rax,%rax,2),%edx
test   %r12,%r12
je     1247 <load_ppm+0xc7>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    0x10(%rsp),%ecx
mov    0xc(%rsp),%edx
mov    %ecx,%esi
imul   %edx,%esi
lea    (%rsi,%rsi,2),%esi
movslq %esi,%rsi
cmp    %rsi,%rax
je     1225 <load_ppm+0xa5>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%ecx
xor    %r12d,%r12d
mov    0xc(%rsp),%edx
mov    %ecx,(%r15)
mov    %edx,(%r14)
jmp    1238 <load_ppm+0xb8>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdb5(%rip),%rdi
mov    %edx,%esi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [38 x i8] c"Cannot allocate %d bytes for memory.\0A\00"
define i8* @load_ppm(i8* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i8* null, i8** %8
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %13
%16 = load i32*, i32** %13
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %3
store i8* null, i8** %4
br label %66
19: ; preds = %3
%20 = load i32*, i32** %13
%21 = call i32 @read_pnm_header(i32* %20, i32* %10, i32* %11, i32* %9)
%22 = icmp ne i32 %21, 6
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32*, i32** %13
%25 = call i32 @fclose(i32* %24)
store i8* null, i8** %4
br label %66
26: ; preds = %19
%27 = load i32, i32* %10
%28 = mul nsw i32 3, %27
%29 = load i32, i32* %11
%30 = mul nsw i32 %28, %29
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %42
35: ; preds = %26
%36 = load i32, i32* %10
%37 = mul nsw i32 3, %36
%38 = load i32, i32* %11
%39 = mul nsw i32 %37, %38
%40 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %26
%43 = load i8*, i8** %8
%44 = load i32, i32* %10
%45 = mul nsw i32 3, %44
%46 = load i32, i32* %11
%47 = mul nsw i32 %45, %46
%48 = load i32*, i32** %13
%49 = call i64 @fread(i8* %43, i32 1, i32 %47, i32* %48)
store i64 %49, i64* %12
%50 = load i64, i64* %12
%51 = load i32, i32* %10
%52 = load i32, i32* %11
%53 = mul nsw i32 %51, %52
%54 = mul nsw i32 %53, 3
%55 = sext i32 %54 to i64
%56 = icmp ne i64 %50, %55
br i1 %56, label %57, label %60
57: ; preds = %42
%58 = load i8*, i8** %8
%59 = call i32 @free(i8* %58)
store i8* null, i8** %8
br label %60
60: ; preds = %57, %42
%61 = load i32, i32* %10
%62 = load i32*, i32** %6
store i32 %61, i32* %62
%63 = load i32, i32* %11
%64 = load i32*, i32** %7
store i32 %63, i32* %64
%65 = load i8*, i8** %8
store i8* %65, i8** %4
br label %66
66: ; preds = %60, %23, %18
%67 = load i8*, i8** %4
ret i8* %67
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [38 x i8] c"Cannot allocate %d bytes for memory.\0A\00"
define i8* @load_ppm(i8* nocapture %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %48, label %12
12: ; preds = %3
%13 = call i32 @read_pnm_header(i32* %10, i32* %5, i32* %6, i32* %4)
%14 = icmp eq i32 %13, 6
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 @fclose(i32* %10)
br label %48
17: ; preds = %12
%18 = load i32, i32* %5
%19 = mul nsw i32 %18, 3
%20 = load i32, i32* %6
%21 = mul nsw i32 %19, %20
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
%24 = icmp eq i64 %22, 0
%25 = load i32, i32* %5
%26 = mul nsw i32 %25, 3
%27 = load i32, i32* %6
%28 = mul nsw i32 %26, %27
br i1 %24, label %29, label %32
29: ; preds = %17
%30 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %17
%33 = call i64 @fread(i8* %23, i32 1, i32 %28, i32* %10)
%34 = load i32, i32* %5
%35 = load i32, i32* %6
%36 = mul i32 %34, 3
%37 = mul i32 %36, %35
%38 = sext i32 %37 to i64
%39 = icmp eq i64 %33, %38
br i1 %39, label %44, label %40
40: ; preds = %32
%41 = call i32 @free(i8* %23)
%42 = load i32, i32* %5
%43 = load i32, i32* %6
br label %44
44: ; preds = %40, %32
%45 = phi i32 [ %43, %40 ], [ %35, %32 ]
%46 = phi i32 [ %42, %40 ], [ %34, %32 ]
%47 = phi i8* [ null, %40 ], [ %23, %32 ]
store i32 %46, i32* %1
store i32 %45, i32* %2
br label %48
48: ; preds = %3, %44, %15
%49 = phi i8* [ null, %15 ], [ %47, %44 ], [ null, %3 ]
ret i8* %49
}