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


x86 assembly (clang, -O0):
<ppmRead>:
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 <ppmReadHeader@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
imul   (%rcx),%eax
imul   $0x3,%eax,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a5 <ppmRead+0x65>
lea    0xe60(%rip),%rdi
call   1060 <error@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
imul   (%rcx),%eax
imul   $0x3,%eax,%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):
<ppmRead>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    %r12,%rdx
mov    %r15,%rcx
call   1040 <ppmReadHeader@plt>
mov    (%r12),%eax
imul   (%r15),%eax
lea    (%rax,%rax,2),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    119b <ppmRead+0x5b>
lea    0xe75(%rip),%rdi
call   1060 <error@plt>
mov    (%r12),%eax
imul   (%r15),%eax
lea    (%rax,%rax,2),%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"(ppmRead) Memory not allocated\00"
define i8* @ppmRead(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 @ppmReadHeader(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 = mul nsw i32 %18, 3
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 1
%22 = trunc i64 %21 to i32
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %3
%27 = call i32 @error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %3
%29 = load i8*, i8** %7
%30 = load i32*, i32** %5
%31 = load i32, i32* %30
%32 = load i32*, i32** %6
%33 = load i32, i32* %32
%34 = mul nsw i32 %31, %33
%35 = mul nsw i32 %34, 3
%36 = load i32*, i32** %4
%37 = call i32 @fread(i8* %29, i32 %35, i32 1, i32* %36)
%38 = load i8*, i8** %7
ret i8* %38
}

LLVM IR (-O2):
@.str = [31 x i8] c"(ppmRead) Memory not allocated\00"
define noalias i8* @ppmRead(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 @ppmReadHeader(i32* %0, i32* %4, i32* %1, i32* %2, i32* %5)
%9 = load i32, i32* %1
%10 = load i32, i32* %2
%11 = mul i32 %9, 3
%12 = mul i32 %11, %10
%13 = call i8* @malloc(i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %3
%16 = call i32 @error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %1
%18 = load i32, i32* %2
%19 = mul i32 %17, 3
%20 = mul i32 %19, %18
br label %21
21: ; preds = %15, %3
%22 = phi i32 [ %20, %15 ], [ %12, %3 ]
%23 = call i32 @fread(i8* %13, i32 %22, i32 1, i32* %0)
ret i8* %13
}