#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_6(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;
  }
  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_6>:
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_6+0x52>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <read_6+0x1d8>
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_6+0x8e>
mov    -0x54(%rbp),%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <read_6+0x1d8>
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_6+0xca>
mov    -0x58(%rbp),%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <read_6+0x1d8>
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_6+0x106>
mov    -0x5c(%rbp),%esi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <read_6+0x1d8>
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
imul   $0x3,%eax,%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    12c2 <read_6+0x162>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <read_6+0x1d8>
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     1315 <read_6+0x1b5>
lea    0xd92(%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    1338 <read_6+0x1d8>
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_6>:
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    124e <read_6+0xde>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
cmp    $0xffffffff,%eax
je     1261 <read_6+0xf1>
mov    %eax,%r13d
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
cmp    $0xffffffff,%eax
je     126a <read_6+0xfa>
mov    %eax,%r14d
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1030 <getval@plt>
cmp    $0xffffffff,%eax
je     1273 <read_6+0x103>
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    %r13d,%eax
imul   %r14d,%eax
lea    (%rax,%rax,2),%ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     129c <read_6+0x12c>
mov    %rax,(%rsp)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1060 <fread@plt>
cmp    %ebp,%eax
jne    12a5 <read_6+0x135>
mov    (%rsp),%rax
mov    %rax,(%r12)
mov    %r13d,(%r15)
mov    0x8(%rsp),%rax
mov    %r14d,(%rax)
jmp    128b <read_6+0x11b>
lea    0xe34(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebp
jmp    128b <read_6+0x11b>
lea    0xd98(%rip),%rdi
jmp    127a <read_6+0x10a>
lea    0xda1(%rip),%rdi
jmp    127a <read_6+0x10a>
lea    0xdab(%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    0xdce(%rip),%rdi
jmp    1255 <read_6+0xe5>
lea    0xdb0(%rip),%rdi
call   1050 <puts@plt>
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    $0xffffffff,%ebp
jmp    128b <read_6+0x11b>

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_6(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 %92
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 %92
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 %92
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 %92
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
%60 = mul nsw i32 %59, 3
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = sext i32 %61 to i64
%63 = mul i64 %62, 1
%64 = trunc i64 %63 to i32
%65 = call i8* @malloc(i32 %64)
store i8* %65, i8** %11
%66 = load i8*, i8** %11
%67 = icmp eq i8* %66, null
br i1 %67, label %68, label %70
68: ; preds = %52
%69 = 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 %92
70: ; preds = %52
%71 = load i8*, i8** %11
%72 = load i32, i32* %10
%73 = load i32*, i32** %6
%74 = call i32 @fread(i8* %71, i32 1, i32 %72, i32* %73)
%75 = sext i32 %74 to i64
%76 = load i32, i32* %10
%77 = sext i32 %76 to i64
%78 = mul i64 %77, 1
%79 = icmp ne i64 %75, %78
br i1 %79, label %80, label %84
80: ; preds = %70
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%82 = load i8*, i8** %11
%83 = call i32 @free(i8* %82)
store i8* null, i8** %11
store i32 -1, i32* %5
br label %92
84: ; preds = %70
%85 = load i8*, i8** %11
%86 = load i8**, i8*** %7
store i8* %85, i8** %86
%87 = load i32, i32* %14
%88 = load i32*, i32** %8
store i32 %87, i32* %88
%89 = load i32, i32* %15
%90 = load i32*, i32** %9
store i32 %89, i32* %90
%91 = load i32, i32* %10
store i32 %91, i32* %5
br label %92
92: ; preds = %84, %80, %68, %49, %40, %31, %23
%93 = load i32, i32* %5
ret i32 %93
}

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_6(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 %41
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 %41
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 %41
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 %41
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 i32 %12, 3
%29 = mul i32 %28, %17
%30 = call i8* @malloc(i32 %29)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0))
br label %41
34: ; preds = %26
%35 = call i32 @fread(i8* %30, i32 1, i32 %29, i32* %0)
%36 = icmp eq i32 %35, %29
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%39 = call i32 @free(i8* %30)
br label %41
40: ; preds = %34
store i8* %30, i8** %1
store i32 %12, i32* %2
store i32 %17, i32* %3
br label %41
41: ; preds = %40, %37, %32, %24, %19, %14, %9
%42 = phi i32 [ -1, %9 ], [ -1, %14 ], [ -1, %19 ], [ -1, %24 ], [ -1, %32 ], [ -1, %37 ], [ %29, %40 ]
ret i32 %42
}