#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*/ *ReadVal(int /*<<< orphan*/ *, char *, int);
void *atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read_image(char *file_name, char **image_data, int *width, int *height) {
  FILE *fp = NULL;
  int fType = 5;
  int w = 0, h = 0, maxVal = 0;
  int arraySize = 0;
  char buf[64 + 1];
  char *data = NULL;
  size_t readSize = 0;
  if (file_name == NULL) {
    printf("NULL file name.\n");
    return -1;
  }
  if (*file_name == 0) {
    printf("Empty file name.\n");
    return -1;
  }
  if (image_data == NULL) {
    printf("NULL pointer for image data pointer.\n");
    return -1;
  }
  if (width == NULL) {
    printf("NULL pointer for image width.\n");
    return -1;
  }
  if (height == NULL) {
    printf("NULL pointer for image height.\n");
    return -1;
  }
  if (*image_data != NULL) {
    printf("Image data pointer may already be allocated.\n");
    return -1;
  }
  fp = fopen(file_name, "rb");
  if (fp == NULL) {
    printf("Failed to open image file \"%s\"\n", file_name);
    return -1;
  }
  memset(buf, 0, (64 + 1) * sizeof(char));
  ReadVal(fp, buf, 64);
  if (buf[0] != 'P') {
    printf("Missing magic byte.\n");
    return -1;
  }
  if ((buf[1] < '0') || (buf[1] > '9')) {
    printf("Invalid magic number.\n");
    return -1;
  }
  fType = buf[1] - '0';
  if ((fType < 2) || (fType > 6) || (fType == 4)) {
    printf("Unsupported file type.\n");
    return -1;
  }
  ReadVal(fp, buf, 64);
  w = atoi(buf);
  if (w < 1) {
    printf("Invalid width.\n");
    return -1;
  }
  ReadVal(fp, buf, 64);
  h = atoi(buf);
  if (h < 1) {
    printf("Invalid height.\n");
    return -1;
  }
  ReadVal(fp, buf, 64);
  maxVal = atoi(buf);
  if (maxVal < 1) {
    printf("Invalid Maximum value.\n");
    return -1;
  }
  arraySize = w * h;
  if ((fType == 3) || (fType == 6)) {
    arraySize *= 3;
  }
  data = (char *)malloc(sizeof(char) * arraySize);
  if (data == NULL) {
    printf("Out of memory, could not allocate %d bytes.\n",
           (int)(sizeof(char)) * arraySize);
    arraySize = -1;
    goto cleanup;
  }
  if ((fType == 5) || (fType == 6)) {
    readSize = fread(data, sizeof(char), arraySize, fp);
  } else {
    int idx;
    for (idx = 0; idx < arraySize; ++idx) {
      if (ReadVal(fp, buf, 64) == NULL) {
        break;
      }
      data[idx] = atoi(buf);
    }
    if (idx != arraySize) {
      printf("Error reading ascii data.\n");
      arraySize = -1;
      free(data);
      data = NULL;
      goto cleanup;
    }
  }
  *width = w;
  *height = h;
  *image_data = data;
cleanup:
  if (fp != NULL) {
    fclose(fp);
    fp = NULL;
  }
  return arraySize;
}


x86 assembly (clang, -O0):
<read_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x5,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movq   $0x0,-0x98(%rbp)
movq   $0x0,-0xa0(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1211 <read_image+0x81>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    123b <read_image+0xab>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
cmpq   $0x0,-0x18(%rbp)
jne    1260 <read_image+0xd0>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
cmpq   $0x0,-0x20(%rbp)
jne    1285 <read_image+0xf5>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
cmpq   $0x0,-0x28(%rbp)
jne    12aa <read_image+0x11a>
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     12d2 <read_image+0x142>
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
mov    -0x10(%rbp),%rdi
lea    0xdd9(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    130f <read_image+0x17f>
mov    -0x10(%rbp),%rsi
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x41,%edx
call   1070 <memset@plt>
mov    -0x30(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x40,%edx
call   1090 <ReadVal@plt>
movsbl -0x90(%rbp),%eax
cmp    $0x50,%eax
je     1361 <read_image+0x1d1>
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
movsbl -0x8f(%rbp),%eax
cmp    $0x30,%eax
jl     1381 <read_image+0x1f1>
movsbl -0x8f(%rbp),%eax
cmp    $0x39,%eax
jle    139b <read_image+0x20b>
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
movsbl -0x8f(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x2,-0x34(%rbp)
jl     13c6 <read_image+0x236>
cmpl   $0x6,-0x34(%rbp)
jg     13c6 <read_image+0x236>
cmpl   $0x4,-0x34(%rbp)
jne    13e0 <read_image+0x250>
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
mov    -0x30(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x40,%edx
call   1090 <ReadVal@plt>
lea    -0x90(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x1,-0x38(%rbp)
jge    1428 <read_image+0x298>
lea    0xd08(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
mov    -0x30(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x40,%edx
call   1090 <ReadVal@plt>
lea    -0x90(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x1,-0x3c(%rbp)
jge    1470 <read_image+0x2e0>
lea    0xcd0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
mov    -0x30(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x40,%edx
call   1090 <ReadVal@plt>
lea    -0x90(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x1,-0x40(%rbp)
jge    14b8 <read_image+0x328>
lea    0xc99(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1650 <read_image+0x4c0>
mov    -0x38(%rbp),%eax
imul   -0x3c(%rbp),%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x3,-0x34(%rbp)
je     14d6 <read_image+0x346>
cmpl   $0x6,-0x34(%rbp)
jne    14dd <read_image+0x34d>
imul   $0x3,-0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
movslq -0x44(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    1521 <read_image+0x391>
mov    -0x44(%rbp),%esi
shl    $0x0,%esi
lea    0xc48(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    162e <read_image+0x49e>
cmpl   $0x5,-0x34(%rbp)
je     1535 <read_image+0x3a5>
cmpl   $0x6,-0x34(%rbp)
jne    1559 <read_image+0x3c9>
mov    -0x98(%rbp),%rdi
mov    -0x44(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0xa0(%rbp)
jmp    160e <read_image+0x47e>
movl   $0x0,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    15c9 <read_image+0x439>
mov    -0x30(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x40,%edx
call   1090 <ReadVal@plt>
cmp    $0x0,%rax
jne    1596 <read_image+0x406>
jmp    15c9 <read_image+0x439>
lea    -0x90(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %al,%dl
mov    -0x98(%rbp),%rax
movslq -0xa4(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    1563 <read_image+0x3d3>
mov    -0xa4(%rbp),%eax
cmp    -0x44(%rbp),%eax
je     1609 <read_image+0x479>
lea    0xba4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x44(%rbp)
mov    -0x98(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x98(%rbp)
jmp    162e <read_image+0x49e>
jmp    160e <read_image+0x47e>
mov    -0x38(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x3c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x98(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x30(%rbp)
je     164a <read_image+0x4ba>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
test   %rdi,%rdi
je     11e3 <read_image+0x43>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     11ec <read_image+0x4c>
mov    %rsi,%r13
test   %rsi,%rsi
je     11f5 <read_image+0x55>
mov    %rdx,%r12
test   %rdx,%rdx
je     11fe <read_image+0x5e>
mov    %rcx,%r14
test   %rcx,%rcx
je     1207 <read_image+0x67>
cmpq   $0x0,0x0(%r13)
je     1229 <read_image+0x89>
lea    0xf00(%rip),%rdi
jmp    120e <read_image+0x6e>
lea    0xf97(%rip),%rdi
jmp    120e <read_image+0x6e>
lea    0xf7d(%rip),%rdi
jmp    120e <read_image+0x6e>
lea    0xf4f(%rip),%rdi
jmp    120e <read_image+0x6e>
lea    0xf28(%rip),%rdi
jmp    120e <read_image+0x6e>
lea    0xf00(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd0(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1280 <read_image+0xe0>
mov    %rax,%r15
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x41,%edx
call   1080 <memset@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   10a0 <ReadVal@plt>
cmpb   $0x50,0x20(%rsp)
jne    1293 <read_image+0xf3>
movzbl 0x21(%rsp),%ebp
lea    -0x3a(%rbp),%eax
cmp    $0xf5,%al
ja     129f <read_image+0xff>
lea    0xe39(%rip),%rdi
jmp    120e <read_image+0x6e>
lea    0xd7c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1213 <read_image+0x73>
lea    0xe33(%rip),%rdi
jmp    120e <read_image+0x6e>
cmp    $0x34,%bpl
je     1372 <read_image+0x1d2>
lea    -0x37(%rbp),%eax
cmp    $0xfa,%al
jbe    1372 <read_image+0x1d2>
lea    0x20(%rsp),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   10a0 <ReadVal@plt>
mov    %rbx,%rdi
call   10c0 <atoi@plt>
test   %eax,%eax
jle    137e <read_image+0x1de>
mov    %rax,0x10(%rsp)
lea    0x20(%rsp),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   10a0 <ReadVal@plt>
mov    %rbx,%rdi
call   10c0 <atoi@plt>
mov    %rax,0x8(%rsp)
test   %eax,%eax
jle    138a <read_image+0x1ea>
lea    0x20(%rsp),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   10a0 <ReadVal@plt>
mov    %rbx,%rdi
call   10c0 <atoi@plt>
test   %eax,%eax
jle    1396 <read_image+0x1f6>
mov    0x8(%rsp),%rbx
imul   0x10(%rsp),%ebx
cmp    $0x36,%bpl
je     133f <read_image+0x19f>
cmp    $0x33,%bpl
jne    1342 <read_image+0x1a2>
lea    (%rbx,%rbx,2),%ebx
mov    %ebx,%edi
call   1090 <malloc@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     13a2 <read_image+0x202>
add    $0xffffffcb,%ebp
cmp    $0x2,%ebp
jae    13b9 <read_image+0x219>
mov    (%rsp),%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
jmp    1401 <read_image+0x261>
lea    0xd27(%rip),%rdi
jmp    120e <read_image+0x6e>
lea    0xd0c(%rip),%rdi
jmp    120e <read_image+0x6e>
lea    0xcf0(%rip),%rdi
jmp    120e <read_image+0x6e>
lea    0xccd(%rip),%rdi
jmp    120e <read_image+0x6e>
lea    0xc7a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%ebx
jmp    141a <read_image+0x27a>
test   %ebx,%ebx
jle    13fb <read_image+0x25b>
mov    %ebx,%eax
mov    %rax,0x18(%rsp)
xor    %ebp,%ebp
mov    %r15,%rdi
lea    0x20(%rsp),%rsi
mov    $0x40,%edx
call   10a0 <ReadVal@plt>
test   %rax,%rax
je     13fd <read_image+0x25d>
lea    0x20(%rsp),%rdi
call   10c0 <atoi@plt>
mov    (%rsp),%rcx
mov    %al,(%rcx,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,0x18(%rsp)
jne    13c6 <read_image+0x226>
jmp    1401 <read_image+0x261>
xor    %ebp,%ebp
cmp    %ebx,%ebp
jne    1427 <read_image+0x287>
mov    0x10(%rsp),%rax
mov    %eax,(%r12)
mov    0x8(%rsp),%rax
mov    %eax,(%r14)
mov    (%rsp),%rax
mov    %rax,0x0(%r13)
mov    %r15,%rdi
call   1060 <fclose@plt>
jmp    1218 <read_image+0x78>
lea    0xc22(%rip),%rdi
call   1040 <puts@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebx
jmp    141a <read_image+0x27a>

LLVM IR (-O0):
@.str = [17 x i8] c"NULL file name.\0A\00"
@.str.1 = [18 x i8] c"Empty file name.\0A\00"
@.str.2 = [38 x i8] c"NULL pointer for image data pointer.\0A\00"
@.str.3 = [31 x i8] c"NULL pointer for image width.\0A\00"
@.str.4 = [32 x i8] c"NULL pointer for image height.\0A\00"
@.str.5 = [46 x i8] c"Image data pointer may already be allocated.\0A\00"
@.str.6 = [3 x i8] c"rb\00"
@.str.7 = [32 x i8] c"Failed to open image file \22%s\22\0A\00"
@.str.8 = [21 x i8] c"Missing magic byte.\0A\00"
@.str.9 = [23 x i8] c"Invalid magic number.\0A\00"
@.str.10 = [24 x i8] c"Unsupported file type.\0A\00"
@.str.11 = [16 x i8] c"Invalid width.\0A\00"
@.str.12 = [17 x i8] c"Invalid height.\0A\00"
@.str.13 = [24 x i8] c"Invalid Maximum value.\0A\00"
@.str.14 = [45 x i8] c"Out of memory, could not allocate %d bytes.\0A\00"
@.str.15 = [27 x i8] c"Error reading ascii data.\0A\00"
define i32 @read_image(i8* %0, i8** %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca [65 x i8]
%17 = alloca i8*
%18 = alloca i64
%19 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* null, i32** %10
store i32 5, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
store i8* null, i8** %17
store i64 0, i64* %18
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %4
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
24: ; preds = %4
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
31: ; preds = %24
%32 = load i8**, i8*** %7
%33 = icmp eq i8** %32, null
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
36: ; preds = %31
%37 = load i32*, i32** %8
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
41: ; preds = %36
%42 = load i32*, i32** %9
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
46: ; preds = %41
%47 = load i8**, i8*** %7
%48 = load i8*, i8** %47
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
52: ; preds = %46
%53 = load i8*, i8** %6
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %54, i32** %10
%55 = load i32*, i32** %10
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i8*, i8** %6
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %58)
store i32 -1, i32* %5
br label %214
60: ; preds = %52
%61 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%62 = call i32 @memset(i8* %61, i32 0, i32 65)
%63 = load i32*, i32** %10
%64 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%65 = call i32* @ReadVal(i32* %63, i8* %64, i32 64)
%66 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = icmp ne i32 %68, 80
br i1 %69, label %70, label %72
70: ; preds = %60
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
72: ; preds = %60
%73 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 1
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp slt i32 %75, 48
br i1 %76, label %82, label %77
77: ; preds = %72
%78 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 1
%79 = load i8, i8* %78
%80 = sext i8 %79 to i32
%81 = icmp sgt i32 %80, 57
br i1 %81, label %82, label %84
82: ; preds = %77, %72
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
84: ; preds = %77
%85 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 1
%86 = load i8, i8* %85
%87 = sext i8 %86 to i32
%88 = sub nsw i32 %87, 48
store i32 %88, i32* %11
%89 = load i32, i32* %11
%90 = icmp slt i32 %89, 2
br i1 %90, label %97, label %91
91: ; preds = %84
%92 = load i32, i32* %11
%93 = icmp sgt i32 %92, 6
br i1 %93, label %97, label %94
94: ; preds = %91
%95 = load i32, i32* %11
%96 = icmp eq i32 %95, 4
br i1 %96, label %97, label %99
97: ; preds = %94, %91, %84
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
99: ; preds = %94
%100 = load i32*, i32** %10
%101 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%102 = call i32* @ReadVal(i32* %100, i8* %101, i32 64)
%103 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%104 = call i8* @atoi(i8* %103)
%105 = ptrtoint i8* %104 to i32
store i32 %105, i32* %12
%106 = load i32, i32* %12
%107 = icmp slt i32 %106, 1
br i1 %107, label %108, label %110
108: ; preds = %99
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.11, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
110: ; preds = %99
%111 = load i32*, i32** %10
%112 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%113 = call i32* @ReadVal(i32* %111, i8* %112, i32 64)
%114 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%115 = call i8* @atoi(i8* %114)
%116 = ptrtoint i8* %115 to i32
store i32 %116, i32* %13
%117 = load i32, i32* %13
%118 = icmp slt i32 %117, 1
br i1 %118, label %119, label %121
119: ; preds = %110
%120 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
121: ; preds = %110
%122 = load i32*, i32** %10
%123 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%124 = call i32* @ReadVal(i32* %122, i8* %123, i32 64)
%125 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%126 = call i8* @atoi(i8* %125)
%127 = ptrtoint i8* %126 to i32
store i32 %127, i32* %14
%128 = load i32, i32* %14
%129 = icmp slt i32 %128, 1
br i1 %129, label %130, label %132
130: ; preds = %121
%131 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.13, i64 0, i64 0))
store i32 -1, i32* %5
br label %214
132: ; preds = %121
%133 = load i32, i32* %12
%134 = load i32, i32* %13
%135 = mul nsw i32 %133, %134
store i32 %135, i32* %15
%136 = load i32, i32* %11
%137 = icmp eq i32 %136, 3
br i1 %137, label %141, label %138
138: ; preds = %132
%139 = load i32, i32* %11
%140 = icmp eq i32 %139, 6
br i1 %140, label %141, label %144
141: ; preds = %138, %132
%142 = load i32, i32* %15
%143 = mul nsw i32 %142, 3
store i32 %143, i32* %15
br label %144
144: ; preds = %141, %138
%145 = load i32, i32* %15
%146 = sext i32 %145 to i64
%147 = mul i64 1, %146
%148 = trunc i64 %147 to i32
%149 = call i64 @malloc(i32 %148)
%150 = inttoptr i64 %149 to i8*
store i8* %150, i8** %17
%151 = load i8*, i8** %17
%152 = icmp eq i8* %151, null
br i1 %152, label %153, label %157
153: ; preds = %144
%154 = load i32, i32* %15
%155 = mul nsw i32 1, %154
%156 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.14, i64 0, i64 0), i32 %155)
store i32 -1, i32* %15
br label %206
157: ; preds = %144
%158 = load i32, i32* %11
%159 = icmp eq i32 %158, 5
br i1 %159, label %163, label %160
160: ; preds = %157
%161 = load i32, i32* %11
%162 = icmp eq i32 %161, 6
br i1 %162, label %163, label %168
163: ; preds = %160, %157
%164 = load i8*, i8** %17
%165 = load i32, i32* %15
%166 = load i32*, i32** %10
%167 = call i64 @fread(i8* %164, i32 1, i32 %165, i32* %166)
store i64 %167, i64* %18
br label %199
168: ; preds = %160
store i32 0, i32* %19
br label %169
169: ; preds = %187, %168
%170 = load i32, i32* %19
%171 = load i32, i32* %15
%172 = icmp slt i32 %170, %171
br i1 %172, label %173, label %190
173: ; preds = %169
%174 = load i32*, i32** %10
%175 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%176 = call i32* @ReadVal(i32* %174, i8* %175, i32 64)
%177 = icmp eq i32* %176, null
br i1 %177, label %178, label %179
178: ; preds = %173
br label %190
179: ; preds = %173
%180 = getelementptr inbounds [65 x i8], [65 x i8]* %16, i64 0, i64 0
%181 = call i8* @atoi(i8* %180)
%182 = ptrtoint i8* %181 to i8
%183 = load i8*, i8** %17
%184 = load i32, i32* %19
%185 = sext i32 %184 to i64
%186 = getelementptr inbounds i8, i8* %183, i64 %185
store i8 %182, i8* %186
br label %187
187: ; preds = %179
%188 = load i32, i32* %19
%189 = add nsw i32 %188, 1
store i32 %189, i32* %19
br label %169
190: ; preds = %178, %169
%191 = load i32, i32* %19
%192 = load i32, i32* %15
%193 = icmp ne i32 %191, %192
br i1 %193, label %194, label %198
194: ; preds = %190
%195 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.15, i64 0, i64 0))
store i32 -1, i32* %15
%196 = load i8*, i8** %17
%197 = call i32 @free(i8* %196)
store i8* null, i8** %17
br label %206
198: ; preds = %190
br label %199
199: ; preds = %198, %163
%200 = load i32, i32* %12
%201 = load i32*, i32** %8
store i32 %200, i32* %201
%202 = load i32, i32* %13
%203 = load i32*, i32** %9
store i32 %202, i32* %203
%204 = load i8*, i8** %17
%205 = load i8**, i8*** %7
store i8* %204, i8** %205
br label %206
206: ; preds = %199, %194, %153
%207 = load i32*, i32** %10
%208 = icmp ne i32* %207, null
br i1 %208, label %209, label %212
209: ; preds = %206
%210 = load i32*, i32** %10
%211 = call i32 @fclose(i32* %210)
store i32* null, i32** %10
br label %212
212: ; preds = %209, %206
%213 = load i32, i32* %15
store i32 %213, i32* %5
br label %214
214: ; preds = %212, %130, %119, %108, %97, %82, %70, %57, %50, %44, %39, %34, %29, %22
%215 = load i32, i32* %5
ret i32 %215
}

LLVM IR (-O2):
@.str.6 = [3 x i8] c"rb\00"
@.str.7 = [32 x i8] c"Failed to open image file \22%s\22\0A\00"
@.str.14 = [45 x i8] c"Out of memory, could not allocate %d bytes.\0A\00"
@str = [26 x i8] c"Error reading ascii data.\00"
@str.16 = [23 x i8] c"Invalid Maximum value.\00"
@str.17 = [16 x i8] c"Invalid height.\00"
@str.18 = [15 x i8] c"Invalid width.\00"
@str.19 = [23 x i8] c"Unsupported file type.\00"
@str.20 = [22 x i8] c"Invalid magic number.\00"
@str.21 = [20 x i8] c"Missing magic byte.\00"
@str.22 = [45 x i8] c"Image data pointer may already be allocated.\00"
@str.23 = [31 x i8] c"NULL pointer for image height.\00"
@str.24 = [30 x i8] c"NULL pointer for image width.\00"
@str.25 = [37 x i8] c"NULL pointer for image data pointer.\00"
@str.26 = [17 x i8] c"Empty file name.\00"
@str.27 = [16 x i8] c"NULL file name.\00"
define i32 @read_image(i8* %0, i8** %1, i32* writeonly %2, i32* writeonly %3) {
%5 = alloca [65 x i8]
%6 = getelementptr inbounds [65 x i8], [65 x i8]* %5, i64 0, i64 0
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.27, i64 0, i64 0))
br label %126
10: ; preds = %4
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.26, i64 0, i64 0))
br label %126
15: ; preds = %10
%16 = icmp eq i8** %1, null
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.25, i64 0, i64 0))
br label %126
19: ; preds = %15
%20 = icmp eq i32* %2, null
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.24, i64 0, i64 0))
br label %126
23: ; preds = %19
%24 = icmp eq i32* %3, null
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.23, i64 0, i64 0))
br label %126
27: ; preds = %23
%28 = load i8*, i8** %1
%29 = icmp eq i8* %28, null
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.22, i64 0, i64 0))
br label %126
32: ; preds = %27
%33 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %0)
br label %126
37: ; preds = %32
%38 = call i32 @memset(i8* %6, i32 0, i32 65)
%39 = call i32* @ReadVal(i32* %33, i8* %6, i32 64)
%40 = load i8, i8* %6
%41 = icmp eq i8 %40, 80
br i1 %41, label %44, label %42
42: ; preds = %37
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.21, i64 0, i64 0))
br label %126
44: ; preds = %37
%45 = getelementptr inbounds [65 x i8], [65 x i8]* %5, i64 0, i64 1
%46 = load i8, i8* %45
%47 = add i8 %46, -58
%48 = icmp ult i8 %47, -10
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.20, i64 0, i64 0))
br label %126
51: ; preds = %44
%52 = zext i8 %46 to i32
%53 = add nsw i8 %46, -55
%54 = icmp ult i8 %53, -5
%55 = icmp eq i8 %46, 52
%56 = or i1 %55, %54
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.19, i64 0, i64 0))
br label %126
59: ; preds = %51
%60 = call i32* @ReadVal(i32* %33, i8* %6, i32 64)
%61 = call i8* @atoi(i8* %6)
%62 = ptrtoint i8* %61 to i64
%63 = trunc i64 %62 to i32
%64 = icmp slt i32 %63, 1
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.18, i64 0, i64 0))
br label %126
67: ; preds = %59
%68 = call i32* @ReadVal(i32* %33, i8* %6, i32 64)
%69 = call i8* @atoi(i8* %6)
%70 = ptrtoint i8* %69 to i64
%71 = trunc i64 %70 to i32
%72 = icmp slt i32 %71, 1
br i1 %72, label %73, label %75
73: ; preds = %67
%74 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.17, i64 0, i64 0))
br label %126
75: ; preds = %67
%76 = call i32* @ReadVal(i32* %33, i8* %6, i32 64)
%77 = call i8* @atoi(i8* %6)
%78 = ptrtoint i8* %77 to i64
%79 = trunc i64 %78 to i32
%80 = icmp slt i32 %79, 1
br i1 %80, label %81, label %83
81: ; preds = %75
%82 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.16, i64 0, i64 0))
br label %126
83: ; preds = %75
%84 = mul nsw i32 %71, %63
switch i8 %46, label %87 [
i8 54, label %85
i8 51, label %85
]
85: ; preds = %83, %83
%86 = mul nsw i32 %84, 3
br label %87
87: ; preds = %83, %85
%88 = phi i32 [ %86, %85 ], [ %84, %83 ]
%89 = call i64 @malloc(i32 %88)
%90 = inttoptr i64 %89 to i8*
%91 = icmp eq i64 %89, 0
br i1 %91, label %92, label %94
92: ; preds = %87
%93 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.14, i64 0, i64 0), i32 %88)
br label %123
94: ; preds = %87
%95 = add nsw i32 %52, -53
%96 = icmp ult i32 %95, 2
br i1 %96, label %101, label %97
97: ; preds = %94
%98 = icmp sgt i32 %88, 0
br i1 %98, label %99, label %116
99: ; preds = %97
%100 = zext i32 %88 to i64
br label %103
101: ; preds = %94
%102 = call i64 @fread(i8* %90, i32 1, i32 %88, i32* %33)
br label %122
103: ; preds = %99, %107
%104 = phi i64 [ 0, %99 ], [ %112, %107 ]
%105 = call i32* @ReadVal(i32* %33, i8* %6, i32 64)
%106 = icmp eq i32* %105, null
br i1 %106, label %114, label %107
107: ; preds = %103
%108 = call i8* @atoi(i8* %6)
%109 = ptrtoint i8* %108 to i64
%110 = trunc i64 %109 to i8
%111 = getelementptr inbounds i8, i8* %90, i64 %104
store i8 %110, i8* %111
%112 = add nuw nsw i64 %104, 1
%113 = icmp eq i64 %112, %100
br i1 %113, label %122, label %103
114: ; preds = %103
%115 = trunc i64 %104 to i32
br label %116
116: ; preds = %114, %97
%117 = phi i32 [ 0, %97 ], [ %115, %114 ]
%118 = icmp eq i32 %117, %88
br i1 %118, label %122, label %119
119: ; preds = %116
%120 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%121 = call i32 @free(i8* %90)
br label %123
122: ; preds = %107, %116, %101
store i32 %63, i32* %2
store i32 %71, i32* %3
store i8* %90, i8** %1
br label %123
123: ; preds = %119, %92, %122
%124 = phi i32 [ -1, %92 ], [ %88, %122 ], [ -1, %119 ]
%125 = call i32 @fclose(i32* %33)
br label %126
126: ; preds = %123, %81, %73, %65, %57, %49, %42, %35, %30, %25, %21, %17, %13, %8
%127 = phi i32 [ -1, %8 ], [ -1, %13 ], [ -1, %17 ], [ -1, %21 ], [ -1, %25 ], [ -1, %30 ], [ -1, %35 ], [ -1, %42 ], [ -1, %49 ], [ -1, %57 ], [ -1, %65 ], [ -1, %73 ], [ -1, %81 ], [ %124, %123 ]
ret i32 %127
}