#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*/ *);
void *getval(int /*<<< orphan*/ *, char *, int);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int read_2(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;
  }
  for (idx = 0; idx < arrsize; idx++) {
    rtp = getval(fp, buff, arrsize);
    localdata[idx] = rtp;
  }
  *image_data = localdata;
  *width = imgwid;
  *height = imght;
  return arrsize;
}


x86 assembly (clang, -O0):
<read_2>:
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   1050 <fgetc@plt>
mov    %al,-0x39(%rbp)
movsbl -0x39(%rbp),%eax
cmp    $0x20,%eax
je     1192 <read_2+0x52>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <read_2+0x1c4>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1030 <getval@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    11ce <read_2+0x8e>
mov    -0x54(%rbp),%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <read_2+0x1c4>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1030 <getval@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    120a <read_2+0xca>
mov    -0x58(%rbp),%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <read_2+0x1c4>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1030 <getval@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
jne    1246 <read_2+0x106>
mov    -0x5c(%rbp),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <read_2+0x1c4>
mov    -0x54(%rbp),%esi
mov    -0x58(%rbp),%edx
mov    -0x5c(%rbp),%ecx
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <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   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    129f <read_2+0x15f>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <read_2+0x1c4>
movl   $0x0,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12e1 <read_2+0x1a1>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <getval@plt>
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%dl
mov    -0x38(%rbp),%rax
movslq -0x60(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    12a6 <read_2+0x166>
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_2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%rbx
call   1060 <fgetc@plt>
cmp    $0x20,%al
jne    126c <read_2+0x11c>
lea    0x30(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
mov    %rax,%r15
cmp    $0xffffffff,%r15d
je     127f <read_2+0x12f>
lea    0x30(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
mov    %rax,%rbp
cmp    $0xffffffff,%ebp
je     1288 <read_2+0x138>
lea    0x30(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
cmp    $0xffffffff,%eax
je     1291 <read_2+0x141>
lea    0xe63(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,0x20(%rsp)
mov    %r15,0x28(%rsp)
imul   %r15d,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     12ba <read_2+0x16a>
mov    %rax,%r15
mov    %r13,0x8(%rsp)
mov    %r12,0x10(%rsp)
mov    %r14,0x18(%rsp)
test   %ebp,%ebp
jle    124a <read_2+0xfa>
mov    %ebp,%r14d
xor    %r13d,%r13d
lea    0x30(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <getval@plt>
mov    %al,(%r15,%r13,1)
add    $0x1,%r13
cmp    %r13,%r14
jne    1230 <read_2+0xe0>
mov    0x8(%rsp),%rax
mov    %r15,(%rax)
mov    0x10(%rsp),%rax
mov    0x28(%rsp),%rcx
mov    %ecx,(%rax)
mov    0x18(%rsp),%rax
mov    0x20(%rsp),%rcx
mov    %ecx,(%rax)
jmp    12a9 <read_2+0x159>
lea    0xe01(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
jmp    12a9 <read_2+0x159>
lea    0xd7a(%rip),%rdi
jmp    1298 <read_2+0x148>
lea    0xd83(%rip),%rdi
jmp    1298 <read_2+0x148>
lea    0xd8d(%rip),%rdi
mov    $0xffffffff,%ebp
mov    $0xffffffff,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9b(%rip),%rdi
jmp    1273 <read_2+0x123>

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"
define i32 @read_2(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 %99
25: ; preds = %4
%26 = load i32*, i32** %6
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%28 = call i8* @getval(i32* %26, i8* %27, i32 20)
%29 = ptrtoint i8* %28 to i32
store i32 %29, i32* %14
%30 = load i32, i32* %14
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = load i32, i32* %14
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %33)
store i32 -1, i32* %5
br label %99
35: ; preds = %25
%36 = load i32*, i32** %6
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%38 = call i8* @getval(i32* %36, i8* %37, i32 20)
%39 = ptrtoint i8* %38 to i32
store i32 %39, i32* %15
%40 = load i32, i32* %15
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = load i32, i32* %15
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %43)
store i32 -1, i32* %5
br label %99
45: ; preds = %35
%46 = load i32*, i32** %6
%47 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%48 = call i8* @getval(i32* %46, i8* %47, i32 20)
%49 = ptrtoint i8* %48 to i32
store i32 %49, i32* %16
%50 = load i32, i32* %16
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %45
%53 = load i32, i32* %16
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %53)
store i32 -1, i32* %5
br label %99
55: ; preds = %45
%56 = load i32, i32* %14
%57 = load i32, i32* %15
%58 = load i32, i32* %16
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %56, i32 %57, i32 %58)
%60 = load i32, i32* %14
%61 = load i32, i32* %15
%62 = mul nsw i32 %60, %61
store i32 %62, i32* %10
%63 = load i32, i32* %10
%64 = sext i32 %63 to i64
%65 = mul i64 %64, 1
%66 = trunc i64 %65 to i32
%67 = call i8* @malloc(i32 %66)
store i8* %67, i8** %11
%68 = load i8*, i8** %11
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %72
70: ; preds = %55
%71 = 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 %99
72: ; preds = %55
store i32 0, i32* %17
br label %73
73: ; preds = %88, %72
%74 = load i32, i32* %17
%75 = load i32, i32* %10
%76 = icmp slt i32 %74, %75
br i1 %76, label %77, label %91
77: ; preds = %73
%78 = load i32*, i32** %6
%79 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%80 = load i32, i32* %10
%81 = call i8* @getval(i32* %78, i8* %79, i32 %80)
%82 = ptrtoint i8* %81 to i8
store i8 %82, i8* %12
%83 = load i8, i8* %12
%84 = load i8*, i8** %11
%85 = load i32, i32* %17
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %84, i64 %86
store i8 %83, i8* %87
br label %88
88: ; preds = %77
%89 = load i32, i32* %17
%90 = add nsw i32 %89, 1
store i32 %90, i32* %17
br label %73
91: ; preds = %73
%92 = load i8*, i8** %11
%93 = load i8**, i8*** %7
store i8* %92, i8** %93
%94 = load i32, i32* %14
%95 = load i32*, i32** %8
store i32 %94, i32* %95
%96 = load i32, i32* %15
%97 = load i32*, i32** %9
store i32 %96, i32* %97
%98 = load i32, i32* %10
store i32 %98, i32* %5
br label %99
99: ; preds = %91, %70, %52, %42, %32, %23
%100 = load i32, i32* %5
ret i32 %100
}

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 = [24 x i8] c"You are out of memory. \00"
@str.6 = [13 x i8] c"Invalid File\00"
define i32 @read_2(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.6, i64 0, i64 0))
br label %52
11: ; preds = %4
%12 = call i8* @getval(i32* %0, i8* %6, i32 20)
%13 = ptrtoint i8* %12 to i64
%14 = trunc i64 %13 to i32
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = 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 %52
18: ; preds = %11
%19 = call i8* @getval(i32* %0, i8* %6, i32 20)
%20 = ptrtoint i8* %19 to i64
%21 = trunc i64 %20 to i32
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = 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 %52
25: ; preds = %18
%26 = call i8* @getval(i32* %0, i8* %6, i32 20)
%27 = ptrtoint i8* %26 to i64
%28 = trunc i64 %27 to i32
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = 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 %52
32: ; preds = %25
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %14, i32 %21, i32 %28)
%34 = mul nsw i32 %21, %14
%35 = call i8* @malloc(i32 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %41, label %37
37: ; preds = %32
%38 = icmp sgt i32 %34, 0
br i1 %38, label %39, label %51
39: ; preds = %37
%40 = zext i32 %34 to i64
br label %43
41: ; preds = %32
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %52
43: ; preds = %39, %43
%44 = phi i64 [ 0, %39 ], [ %49, %43 ]
%45 = call i8* @getval(i32* %0, i8* %6, i32 %34)
%46 = ptrtoint i8* %45 to i64
%47 = trunc i64 %46 to i8
%48 = getelementptr inbounds i8, i8* %35, i64 %44
store i8 %47, i8* %48
%49 = add nuw nsw i64 %44, 1
%50 = icmp eq i64 %49, %40
br i1 %50, label %51, label %43
51: ; preds = %43, %37
store i8* %35, i8** %1
store i32 %14, i32* %2
store i32 %21, i32* %3
br label %52
52: ; preds = %51, %41, %30, %23, %16, %9
%53 = phi i32 [ -1, %9 ], [ -1, %16 ], [ -1, %23 ], [ -1, %30 ], [ -1, %41 ], [ %34, %51 ]
ret i32 %53
}