#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 fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ readDIMs(int /*<<< orphan*/ *, int *, int *, int *);
int readRGB(FILE *imgfile, char **image_data, int *width, int *height) {
  int intensity = 0;
  int arraysze = 0;
  char *imagedata = NULL;
  if (readDIMs(imgfile, width, height, &intensity) != 0) {
    printf("could not read DIMS\n");
    return -1;
  }
  printf("read ASCII RGB\n");
  printf("w:%d h:%d i:%d\n", *width, *height, intensity);
  if (*width < 1) {
    printf("Invalid width\n");
    return -1;
  }
  if (*height < 1) {
    printf("Invalid height\n");
    return -1;
  }
  arraysze = (*width) * (*height) * 3;
  imagedata = (char *)malloc(sizeof(char) * arraysze);
  printf("arraysze:%d\n", arraysze);
  if (imagedata == NULL) {
    printf("Failed to allocate memory, Possibly out of memory\n");
    return -1;
  }
  memset(imagedata, 0, sizeof(char) * arraysze);
  if (fread(imagedata, sizeof(char), arraysze, imgfile) != arraysze) {
    printf("failed to read imagedata\n");
    return -1;
  }
  *image_data = imagedata;
  return arraysze;
}


x86 assembly (clang, -O0):
<readRGB>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1060 <readDIMs@plt>
cmp    $0x0,%rax
je     11b7 <readRGB+0x67>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <readRGB+0x19a>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
mov    -0x2c(%rbp),%ecx
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
cmpl   $0x1,(%rax)
jge    1209 <readRGB+0xb9>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <readRGB+0x19a>
mov    -0x28(%rbp),%rax
cmpl   $0x1,(%rax)
jge    1230 <readRGB+0xe0>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <readRGB+0x19a>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x28(%rbp),%rcx
imul   (%rcx),%eax
imul   $0x3,%eax,%eax
mov    %eax,-0x30(%rbp)
movslq -0x30(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%esi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
jne    128c <readRGB+0x13c>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <readRGB+0x19a>
mov    -0x38(%rbp),%rdi
movslq -0x30(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x30(%rbp),%eax
je     12d9 <readRGB+0x189>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <readRGB+0x19a>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readRGB>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rcx
mov    %rdx,%rsi
mov    %rbp,%rdx
call   1070 <readDIMs@plt>
test   %rax,%rax
je     11ae <readRGB+0x4e>
lea    0xefd(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%esi
mov    0x0(%rbp),%edx
mov    0x4(%rsp),%ecx
lea    0xe36(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%eax
test   %eax,%eax
jle    122d <readRGB+0xcd>
mov    0x0(%rbp),%ecx
test   %ecx,%ecx
jle    1239 <readRGB+0xd9>
imul   %ecx,%eax
lea    (%rax,%rax,2),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0xe1b(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %rbx,%rbx
je     1245 <readRGB+0xe5>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    %ebp,%eax
jne    1251 <readRGB+0xf1>
mov    %rbx,(%r14)
jmp    11a1 <readRGB+0x41>
lea    0xe52(%rip),%rdi
jmp    1197 <readRGB+0x37>
lea    0xe37(%rip),%rdi
jmp    1197 <readRGB+0x37>
lea    0xdf9(%rip),%rdi
jmp    1197 <readRGB+0x37>
lea    0xdd4(%rip),%rdi
jmp    1197 <readRGB+0x37>

LLVM IR (-O0):
@.str = [21 x i8] c"could not read DIMS\0A\00"
@.str.1 = [16 x i8] c"read ASCII RGB\0A\00"
@.str.2 = [16 x i8] c"w:%d h:%d i:%d\0A\00"
@.str.3 = [15 x i8] c"Invalid width\0A\00"
@.str.4 = [16 x i8] c"Invalid height\0A\00"
@.str.5 = [13 x i8] c"arraysze:%d\0A\00"
@.str.6 = [51 x i8] c"Failed to allocate memory, Possibly out of memory\0A\00"
@.str.7 = [26 x i8] c"failed to read imagedata\0A\00"
define i32 @readRGB(i32* %0, i8** %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32* %0, i32** %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32 0, i32* %10
store i32 0, i32* %11
store i8* null, i8** %12
%13 = load i32*, i32** %6
%14 = load i32*, i32** %8
%15 = load i32*, i32** %9
%16 = call i64 @readDIMs(i32* %13, i32* %14, i32* %15, i32* %10)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %77
20: ; preds = %4
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32*, i32** %8
%23 = load i32, i32* %22
%24 = load i32*, i32** %9
%25 = load i32, i32* %24
%26 = load i32, i32* %10
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %25, i32 %26)
%28 = load i32*, i32** %8
%29 = load i32, i32* %28
%30 = icmp slt i32 %29, 1
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
br label %77
33: ; preds = %20
%34 = load i32*, i32** %9
%35 = load i32, i32* %34
%36 = icmp slt i32 %35, 1
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %5
br label %77
39: ; preds = %33
%40 = load i32*, i32** %8
%41 = load i32, i32* %40
%42 = load i32*, i32** %9
%43 = load i32, i32* %42
%44 = mul nsw i32 %41, %43
%45 = mul nsw i32 %44, 3
store i32 %45, i32* %11
%46 = load i32, i32* %11
%47 = sext i32 %46 to i64
%48 = mul i64 1, %47
%49 = trunc i64 %48 to i32
%50 = call i64 @malloc(i32 %49)
%51 = inttoptr i64 %50 to i8*
store i8* %51, i8** %12
%52 = load i32, i32* %11
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = load i8*, i8** %12
%55 = icmp eq i8* %54, null
br i1 %55, label %56, label %58
56: ; preds = %39
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %5
br label %77
58: ; preds = %39
%59 = load i8*, i8** %12
%60 = load i32, i32* %11
%61 = sext i32 %60 to i64
%62 = mul i64 1, %61
%63 = trunc i64 %62 to i32
%64 = call i32 @memset(i8* %59, i32 0, i32 %63)
%65 = load i8*, i8** %12
%66 = load i32, i32* %11
%67 = load i32*, i32** %6
%68 = call i32 @fread(i8* %65, i32 1, i32 %66, i32* %67)
%69 = load i32, i32* %11
%70 = icmp ne i32 %68, %69
br i1 %70, label %71, label %73
71: ; preds = %58
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %5
br label %77
73: ; preds = %58
%74 = load i8*, i8** %12
%75 = load i8**, i8*** %7
store i8* %74, i8** %75
%76 = load i32, i32* %11
store i32 %76, i32* %5
br label %77
77: ; preds = %73, %71, %56, %37, %31, %18
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
@.str.2 = [16 x i8] c"w:%d h:%d i:%d\0A\00"
@.str.5 = [13 x i8] c"arraysze:%d\0A\00"
@str = [15 x i8] c"read ASCII RGB\00"
@str.8 = [25 x i8] c"failed to read imagedata\00"
@str.9 = [50 x i8] c"Failed to allocate memory, Possibly out of memory\00"
@str.10 = [15 x i8] c"Invalid height\00"
@str.11 = [14 x i8] c"Invalid width\00"
@str.12 = [20 x i8] c"could not read DIMS\00"
define i32 @readRGB(i32* %0, i8** nocapture writeonly %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%7 = call i64 @readDIMs(i32* %0, i32* %2, i32* %3, i32* %5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.12, i64 0, i64 0))
br label %42
11: ; preds = %4
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%13 = load i32, i32* %2
%14 = load i32, i32* %3
%15 = load i32, i32* %5
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %14, i32 %15)
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0))
br label %42
21: ; preds = %11
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
br label %42
26: ; preds = %21
%27 = mul i32 %17, 3
%28 = mul i32 %27, %22
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %28)
%32 = icmp eq i64 %29, 0
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.9, i64 0, i64 0))
br label %42
35: ; preds = %26
%36 = call i32 @memset(i8* %30, i32 0, i32 %28)
%37 = call i32 @fread(i8* %30, i32 1, i32 %28, i32* %0)
%38 = icmp eq i32 %37, %28
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.8, i64 0, i64 0))
br label %42
41: ; preds = %35
store i8* %30, i8** %1
br label %42
42: ; preds = %41, %39, %33, %24, %19, %9
%43 = phi i32 [ -1, %9 ], [ -1, %19 ], [ -1, %24 ], [ -1, %33 ], [ -1, %39 ], [ %28, %41 ]
ret i32 %43
}