#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_pgm(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) != 5) {
    fclose(f);
    return NULL;
  }
  data = (unsigned char *)malloc(w * h);
  if (data == NULL) {
    printf("Cannot allocate %d bytes for memory.\n", (w * h));
    exit(-1);
  }
  c = fread(data, 1, w * h, f);
  if (c != w * h) {
    free(data);
    data = NULL;
  }
  *width = w;
  *height = h;
  return data;
}


x86 assembly (clang, -O0):
<load_pgm>:
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_pgm+0x48>
movq   $0x0,-0x8(%rbp)
jmp    1293 <load_pgm+0x113>
mov    -0x48(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1070 <read_pnm_header@plt>
cmp    $0x5,%eax
je     11fc <load_pgm+0x7c>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1293 <load_pgm+0x113>
mov    -0x30(%rbp),%edi
imul   -0x34(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1234 <load_pgm+0xb4>
mov    -0x30(%rbp),%esi
imul   -0x34(%rbp),%esi
lea    0xdde(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -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
movslq %ecx,%rcx
cmp    %rcx,%rax
je     1279 <load_pgm+0xf9>
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_pgm>:
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     122c <load_pgm+0xac>
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    $0x5,%eax
jne    1224 <load_pgm+0xa4>
mov    0xc(%rsp),%edi
imul   0x10(%rsp),%edi
call   1080 <malloc@plt>
mov    0xc(%rsp),%edx
imul   0x10(%rsp),%edx
test   %rax,%rax
je     123e <load_pgm+0xbe>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    0x10(%rsp),%ecx
mov    0xc(%rsp),%edx
mov    %edx,%esi
imul   %ecx,%esi
movslq %esi,%rsi
cmp    %rsi,%rax
je     121c <load_pgm+0x9c>
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    122f <load_pgm+0xaf>
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    0xdbe(%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_pgm(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 %62
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, 5
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 %62
26: ; preds = %19
%27 = load i32, i32* %10
%28 = load i32, i32* %11
%29 = mul nsw i32 %27, %28
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %40
34: ; preds = %26
%35 = load i32, i32* %10
%36 = load i32, i32* %11
%37 = mul nsw i32 %35, %36
%38 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %26
%41 = load i8*, i8** %8
%42 = load i32, i32* %10
%43 = load i32, i32* %11
%44 = mul nsw i32 %42, %43
%45 = load i32*, i32** %13
%46 = call i64 @fread(i8* %41, i32 1, i32 %44, i32* %45)
store i64 %46, i64* %12
%47 = load i64, i64* %12
%48 = load i32, i32* %10
%49 = load i32, i32* %11
%50 = mul nsw i32 %48, %49
%51 = sext i32 %50 to i64
%52 = icmp ne i64 %47, %51
br i1 %52, label %53, label %56
53: ; preds = %40
%54 = load i8*, i8** %8
%55 = call i32 @free(i8* %54)
store i8* null, i8** %8
br label %56
56: ; preds = %53, %40
%57 = load i32, i32* %10
%58 = load i32*, i32** %6
store i32 %57, i32* %58
%59 = load i32, i32* %11
%60 = load i32*, i32** %7
store i32 %59, i32* %60
%61 = load i8*, i8** %8
store i8* %61, i8** %4
br label %62
62: ; preds = %56, %23, %18
%63 = load i8*, i8** %4
ret i8* %63
}

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_pgm(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 %45, label %12
12: ; preds = %3
%13 = call i32 @read_pnm_header(i32* %10, i32* %5, i32* %6, i32* %4)
%14 = icmp eq i32 %13, 5
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 @fclose(i32* %10)
br label %45
17: ; preds = %12
%18 = load i32, i32* %5
%19 = load i32, i32* %6
%20 = mul nsw i32 %19, %18
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
%24 = load i32, i32* %5
%25 = load i32, i32* %6
%26 = mul nsw i32 %25, %24
br i1 %23, label %27, label %30
27: ; preds = %17
%28 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %17
%31 = call i64 @fread(i8* %22, i32 1, i32 %26, i32* %10)
%32 = load i32, i32* %5
%33 = load i32, i32* %6
%34 = mul nsw i32 %33, %32
%35 = sext i32 %34 to i64
%36 = icmp eq i64 %31, %35
br i1 %36, label %41, label %37
37: ; preds = %30
%38 = call i32 @free(i8* %22)
%39 = load i32, i32* %5
%40 = load i32, i32* %6
br label %41
41: ; preds = %37, %30
%42 = phi i32 [ %40, %37 ], [ %33, %30 ]
%43 = phi i32 [ %39, %37 ], [ %32, %30 ]
%44 = phi i8* [ null, %37 ], [ %22, %30 ]
store i32 %43, i32* %1
store i32 %42, i32* %2
br label %45
45: ; preds = %3, %41, %15
%46 = phi i8* [ null, %15 ], [ %44, %41 ], [ null, %3 ]
ret i8* %46
}