#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 */
char fgetc(int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int getval(int /*<<< orphan*/ *, char *, int);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int read_5(FILE *fp, char **image_data, int *width, int *height) {
  int arrsize;
  char *localdata;
  char rtp;
  char buff[20];
  int imgwid;
  int imght;
  int maxval;
  int idx = 0;
  rtp = fgetc(fp);
  if (rtp != ' ') {
    printf("Invalid File\n");
    return -1;
  }
  imgwid = getval(fp, buff, 20);
  if (imgwid == -1) {
    printf("Invalid Width %d\n", imgwid);
    return -1;
  }
  imght = getval(fp, buff, 20);
  if (imght == -1) {
    printf("Invalid Height %d\n", imght);
    return -1;
  }
  maxval = getval(fp, buff, 20);
  if (maxval == -1) {
    printf("Invalid Maximum %d\n", maxval);
    return -1;
  }
  printf("Width = %d, Height = %d, Max = %d\n", imgwid, imght, maxval);
  arrsize = imgwid * imght;
  localdata = malloc(arrsize * sizeof(char));
  if (localdata == NULL) {
    printf("You are out of memory. \n");
    return -1;
  }
  if (fread(localdata, sizeof(char), arrsize, fp) != (arrsize * sizeof(char))) {
    printf("Failed to read file.\n");
    free(localdata);
    localdata = NULL;
    return -1;
  }
  *image_data = localdata;
  *width = imgwid;
  *height = imght;
  return arrsize;
}


x86 assembly (clang, -O0):
<read_5>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %al,-0x39(%rbp)
movsbl -0x39(%rbp),%eax
cmp    $0x20,%eax
je     11b2 <read_5+0x52>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <read_5+0x1d5>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1030 <getval@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    11ee <read_5+0x8e>
mov    -0x54(%rbp),%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <read_5+0x1d5>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1030 <getval@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    122a <read_5+0xca>
mov    -0x58(%rbp),%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <read_5+0x1d5>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1030 <getval@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
jne    1266 <read_5+0x106>
mov    -0x5c(%rbp),%esi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <read_5+0x1d5>
mov    -0x54(%rbp),%esi
mov    -0x58(%rbp),%edx
mov    -0x5c(%rbp),%ecx
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x54(%rbp),%eax
imul   -0x58(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12bf <read_5+0x15f>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <read_5+0x1d5>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
cltq
movslq -0x2c(%rbp),%rcx
shl    $0x0,%rcx
cmp    %rcx,%rax
je     1312 <read_5+0x1b2>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <read_5+0x1d5>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x54(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_5>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
call   1080 <fgetc@plt>
cmp    $0x20,%al
jne    124b <read_5+0xdb>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
cmp    $0xffffffff,%eax
je     125e <read_5+0xee>
mov    %eax,%r13d
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
cmp    $0xffffffff,%eax
je     1267 <read_5+0xf7>
mov    %eax,%r14d
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
cmp    $0xffffffff,%eax
je     1270 <read_5+0x100>
mov    %rbp,0x8(%rsp)
lea    0xe3f(%rip),%rdi
mov    %r13d,%esi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14d,%ebp
imul   %r13d,%ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1299 <read_5+0x129>
mov    %rax,(%rsp)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1060 <fread@plt>
cmp    %ebp,%eax
jne    12a2 <read_5+0x132>
mov    (%rsp),%rax
mov    %rax,(%r12)
mov    %r13d,(%r15)
mov    0x8(%rsp),%rax
mov    %r14d,(%rax)
jmp    1288 <read_5+0x118>
lea    0xe37(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebp
jmp    1288 <read_5+0x118>
lea    0xd9b(%rip),%rdi
jmp    1277 <read_5+0x107>
lea    0xda4(%rip),%rdi
jmp    1277 <read_5+0x107>
lea    0xdae(%rip),%rdi
mov    $0xffffffff,%ebp
mov    $0xffffffff,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd1(%rip),%rdi
jmp    1252 <read_5+0xe2>
lea    0xdb3(%rip),%rdi
call   1050 <puts@plt>
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    $0xffffffff,%ebp
jmp    1288 <read_5+0x118>

LLVM IR (-O0):
@.str = [14 x i8] c"Invalid File\0A\00"
@.str.1 = [18 x i8] c"Invalid Width %d\0A\00"
@.str.2 = [19 x i8] c"Invalid Height %d\0A\00"
@.str.3 = [20 x i8] c"Invalid Maximum %d\0A\00"
@.str.4 = [35 x i8] c"Width = %d, Height = %d, Max = %d\0A\00"
@.str.5 = [25 x i8] c"You are out of memory. \0A\00"
@.str.6 = [22 x i8] c"Failed to read file.\0A\00"
define i32 @read_5(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 i8*
%12 = alloca i8
%13 = alloca [20 x i8]
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i32* %0, i32** %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32 0, i32* %17
%18 = load i32*, i32** %6
%19 = call signext i8 @fgetc(i32* %18)
store i8 %19, i8* %12
%20 = load i8, i8* %12
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 32
br i1 %22, label %23, label %25
23: ; preds = %4
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %91
25: ; preds = %4
%26 = load i32*, i32** %6
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%28 = call i32 @getval(i32* %26, i8* %27, i32 20)
store i32 %28, i32* %14
%29 = load i32, i32* %14
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i32, i32* %14
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %32)
store i32 -1, i32* %5
br label %91
34: ; preds = %25
%35 = load i32*, i32** %6
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%37 = call i32 @getval(i32* %35, i8* %36, i32 20)
store i32 %37, i32* %15
%38 = load i32, i32* %15
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = load i32, i32* %15
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %41)
store i32 -1, i32* %5
br label %91
43: ; preds = %34
%44 = load i32*, i32** %6
%45 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%46 = call i32 @getval(i32* %44, i8* %45, i32 20)
store i32 %46, i32* %16
%47 = load i32, i32* %16
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %43
%50 = load i32, i32* %16
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %50)
store i32 -1, i32* %5
br label %91
52: ; preds = %43
%53 = load i32, i32* %14
%54 = load i32, i32* %15
%55 = load i32, i32* %16
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %53, i32 %54, i32 %55)
%57 = load i32, i32* %14
%58 = load i32, i32* %15
%59 = mul nsw i32 %57, %58
store i32 %59, i32* %10
%60 = load i32, i32* %10
%61 = sext i32 %60 to i64
%62 = mul i64 %61, 1
%63 = trunc i64 %62 to i32
%64 = call i8* @malloc(i32 %63)
store i8* %64, i8** %11
%65 = load i8*, i8** %11
%66 = icmp eq i8* %65, null
br i1 %66, label %67, label %69
67: ; preds = %52
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %5
br label %91
69: ; preds = %52
%70 = load i8*, i8** %11
%71 = load i32, i32* %10
%72 = load i32*, i32** %6
%73 = call i32 @fread(i8* %70, i32 1, i32 %71, i32* %72)
%74 = sext i32 %73 to i64
%75 = load i32, i32* %10
%76 = sext i32 %75 to i64
%77 = mul i64 %76, 1
%78 = icmp ne i64 %74, %77
br i1 %78, label %79, label %83
79: ; preds = %69
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%81 = load i8*, i8** %11
%82 = call i32 @free(i8* %81)
store i8* null, i8** %11
store i32 -1, i32* %5
br label %91
83: ; preds = %69
%84 = load i8*, i8** %11
%85 = load i8**, i8*** %7
store i8* %84, i8** %85
%86 = load i32, i32* %14
%87 = load i32*, i32** %8
store i32 %86, i32* %87
%88 = load i32, i32* %15
%89 = load i32*, i32** %9
store i32 %88, i32* %89
%90 = load i32, i32* %10
store i32 %90, i32* %5
br label %91
91: ; preds = %83, %79, %67, %49, %40, %31, %23
%92 = load i32, i32* %5
ret i32 %92
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"Invalid Width %d\0A\00"
@.str.2 = [19 x i8] c"Invalid Height %d\0A\00"
@.str.3 = [20 x i8] c"Invalid Maximum %d\0A\00"
@.str.4 = [35 x i8] c"Width = %d, Height = %d, Max = %d\0A\00"
@str = [21 x i8] c"Failed to read file.\00"
@str.7 = [24 x i8] c"You are out of memory. \00"
@str.8 = [13 x i8] c"Invalid File\00"
define i32 @read_5(i32* %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca [20 x i8]
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%7 = tail call signext i8 @fgetc(i32* %0)
%8 = icmp eq i8 %7, 32
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
br label %40
11: ; preds = %4
%12 = call i32 @getval(i32* %0, i8* %6, i32 20)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 -1)
br label %40
16: ; preds = %11
%17 = call i32 @getval(i32* %0, i8* %6, i32 20)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 -1)
br label %40
21: ; preds = %16
%22 = call i32 @getval(i32* %0, i8* %6, i32 20)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 -1)
br label %40
26: ; preds = %21
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %12, i32 %17, i32 %22)
%28 = mul nsw i32 %17, %12
%29 = call i8* @malloc(i32 %28)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0))
br label %40
33: ; preds = %26
%34 = call i32 @fread(i8* %29, i32 1, i32 %28, i32* %0)
%35 = icmp eq i32 %34, %28
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%38 = call i32 @free(i8* %29)
br label %40
39: ; preds = %33
store i8* %29, i8** %1
store i32 %12, i32* %2
store i32 %17, i32* %3
br label %40
40: ; preds = %39, %36, %31, %24, %19, %14, %9
%41 = phi i32 [ -1, %9 ], [ -1, %14 ], [ -1, %19 ], [ -1, %24 ], [ -1, %31 ], [ -1, %36 ], [ %28, %39 ]
ret i32 %41
}