#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_3(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 * 3;
  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_3>:
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_3+0x52>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <read_3+0x1c7>
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_3+0x8e>
mov    -0x54(%rbp),%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <read_3+0x1c7>
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_3+0xca>
mov    -0x58(%rbp),%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <read_3+0x1c7>
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_3+0x106>
mov    -0x5c(%rbp),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <read_3+0x1c7>
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
imul   $0x3,%eax,%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    12a2 <read_3+0x162>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <read_3+0x1c7>
movl   $0x0,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12e4 <read_3+0x1a4>
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    12a9 <read_3+0x169>
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_3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,%rbx
call   1060 <fgetc@plt>
cmp    $0x20,%al
jne    126d <read_3+0x11d>
lea    0x30(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
mov    %rax,%rbp
cmp    $0xffffffff,%ebp
je     1281 <read_3+0x131>
lea    0x30(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
mov    %rax,%r15
cmp    $0xffffffff,%r15d
je     128a <read_3+0x13a>
lea    0x30(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
cmp    $0xffffffff,%eax
je     1293 <read_3+0x143>
mov    %r14,0x28(%rsp)
lea    0xe5e(%rip),%rdi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,0x20(%rsp)
mov    %ebp,%eax
mov    %r15,0x18(%rsp)
imul   %r15d,%eax
lea    (%rax,%rax,2),%r14d
mov    %r14d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     12be <read_3+0x16e>
mov    %rax,%r15
mov    %r12,0x8(%rsp)
mov    %r13,0x10(%rsp)
test   %r14d,%r14d
jle    124b <read_3+0xfb>
mov    %r14d,%ebp
xor    %r13d,%r13d
lea    0x30(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <getval@plt>
mov    %al,(%r15,%r13,1)
add    $0x1,%r13
cmp    %r13,%rbp
jne    1230 <read_3+0xe0>
mov    0x8(%rsp),%rax
mov    %r15,(%rax)
mov    0x10(%rsp),%rax
mov    0x20(%rsp),%rcx
mov    %ecx,(%rax)
mov    0x28(%rsp),%rax
mov    0x18(%rsp),%rcx
mov    %ecx,(%rax)
jmp    12ac <read_3+0x15c>
lea    0xe00(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%r14d
jmp    12ac <read_3+0x15c>
lea    0xd78(%rip),%rdi
jmp    129a <read_3+0x14a>
lea    0xd81(%rip),%rdi
jmp    129a <read_3+0x14a>
lea    0xd8b(%rip),%rdi
mov    $0xffffffff,%r14d
mov    $0xffffffff,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd97(%rip),%rdi
jmp    1274 <read_3+0x124>

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_3(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 %100
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 %100
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 %100
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 %100
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
%63 = mul nsw i32 %62, 3
store i32 %63, i32* %10
%64 = load i32, i32* %10
%65 = sext i32 %64 to i64
%66 = mul i64 %65, 1
%67 = trunc i64 %66 to i32
%68 = call i8* @malloc(i32 %67)
store i8* %68, i8** %11
%69 = load i8*, i8** %11
%70 = icmp eq i8* %69, null
br i1 %70, label %71, label %73
71: ; preds = %55
%72 = 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 %100
73: ; preds = %55
store i32 0, i32* %17
br label %74
74: ; preds = %89, %73
%75 = load i32, i32* %17
%76 = load i32, i32* %10
%77 = icmp slt i32 %75, %76
br i1 %77, label %78, label %92
78: ; preds = %74
%79 = load i32*, i32** %6
%80 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%81 = load i32, i32* %10
%82 = call i8* @getval(i32* %79, i8* %80, i32 %81)
%83 = ptrtoint i8* %82 to i8
store i8 %83, i8* %12
%84 = load i8, i8* %12
%85 = load i8*, i8** %11
%86 = load i32, i32* %17
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %85, i64 %87
store i8 %84, i8* %88
br label %89
89: ; preds = %78
%90 = load i32, i32* %17
%91 = add nsw i32 %90, 1
store i32 %91, i32* %17
br label %74
92: ; preds = %74
%93 = load i8*, i8** %11
%94 = load i8**, i8*** %7
store i8* %93, i8** %94
%95 = load i32, i32* %14
%96 = load i32*, i32** %8
store i32 %95, i32* %96
%97 = load i32, i32* %15
%98 = load i32*, i32** %9
store i32 %97, i32* %98
%99 = load i32, i32* %10
store i32 %99, i32* %5
br label %100
100: ; preds = %92, %71, %52, %42, %32, %23
%101 = load i32, i32* %5
ret i32 %101
}

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_3(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 %53
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 %53
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 %53
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 %53
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 i32 %14, 3
%35 = mul i32 %34, %21
%36 = call i8* @malloc(i32 %35)
%37 = icmp eq i8* %36, null
br i1 %37, label %42, label %38
38: ; preds = %32
%39 = icmp sgt i32 %35, 0
br i1 %39, label %40, label %52
40: ; preds = %38
%41 = zext i32 %35 to i64
br label %44
42: ; preds = %32
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %53
44: ; preds = %40, %44
%45 = phi i64 [ 0, %40 ], [ %50, %44 ]
%46 = call i8* @getval(i32* %0, i8* %6, i32 %35)
%47 = ptrtoint i8* %46 to i64
%48 = trunc i64 %47 to i8
%49 = getelementptr inbounds i8, i8* %36, i64 %45
store i8 %48, i8* %49
%50 = add nuw nsw i64 %45, 1
%51 = icmp eq i64 %50, %41
br i1 %51, label %52, label %44
52: ; preds = %44, %38
store i8* %36, i8** %1
store i32 %14, i32* %2
store i32 %21, i32* %3
br label %53
53: ; preds = %52, %42, %30, %23, %16, %9
%54 = phi i32 [ -1, %9 ], [ -1, %16 ], [ -1, %23 ], [ -1, %30 ], [ -1, %42 ], [ %35, %52 ]
ret i32 %54
}