#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*/ error(char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ pgmReadHeader(int /*<<< orphan*/ *, int *, int *, int *,
                                 int *);
unsigned char *pgmRead(FILE *fp, int *ncols, int *nrows) {
  unsigned char *ptr;
  int magic, maxval;
  pgmReadHeader(fp, &magic, ncols, nrows, &maxval);
  ptr = malloc(*ncols * *nrows * sizeof(char));
  if (ptr == NULL)
    error("(pgmRead) Memory not allocated");
  fread(ptr, *ncols * *nrows, 1, fp);
  return ptr;
}


x86 assembly (clang, -O0):
<pgmRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%r8
call   1040 <pgmReadHeader@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
imul   (%rcx),%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a2 <pgmRead+0x62>
lea    0xe63(%rip),%rdi
call   1060 <error@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
imul   (%rax),%esi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgmRead>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    %r15,%rdx
mov    %r12,%rcx
call   1040 <pgmReadHeader@plt>
mov    (%r12),%ebp
imul   (%r15),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1195 <pgmRead+0x55>
lea    0xe78(%rip),%rdi
call   1060 <error@plt>
mov    (%r12),%ebp
imul   (%r15),%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"(pgmRead) Memory not allocated\00"
define i8* @pgmRead(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%10 = load i32*, i32** %4
%11 = load i32*, i32** %5
%12 = load i32*, i32** %6
%13 = call i32 @pgmReadHeader(i32* %10, i32* %8, i32* %11, i32* %12, i32* %9)
%14 = load i32*, i32** %5
%15 = load i32, i32* %14
%16 = load i32*, i32** %6
%17 = load i32, i32* %16
%18 = mul nsw i32 %15, %17
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 1
%21 = trunc i64 %20 to i32
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %3
%26 = call i32 @error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %3
%28 = load i8*, i8** %7
%29 = load i32*, i32** %5
%30 = load i32, i32* %29
%31 = load i32*, i32** %6
%32 = load i32, i32* %31
%33 = mul nsw i32 %30, %32
%34 = load i32*, i32** %4
%35 = call i32 @fread(i8* %28, i32 %33, i32 1, i32* %34)
%36 = load i8*, i8** %7
ret i8* %36
}

LLVM IR (-O2):
@.str = [31 x i8] c"(pgmRead) Memory not allocated\00"
define noalias i8* @pgmRead(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i32 @pgmReadHeader(i32* %0, i32* %4, i32* %1, i32* %2, i32* %5)
%9 = load i32, i32* %1
%10 = load i32, i32* %2
%11 = mul nsw i32 %10, %9
%12 = call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = call i32 @error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* %1
%17 = load i32, i32* %2
%18 = mul nsw i32 %17, %16
br label %19
19: ; preds = %14, %3
%20 = phi i32 [ %18, %14 ], [ %11, %3 ]
%21 = call i32 @fread(i8* %12, i32 %20, i32 1, i32* %0)
ret i8* %12
}