#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 fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ readDIMs(int /*<<< orphan*/ *, int *, int *, int *);
int readASCIIgrey(FILE *imgfile, char **image_data, int *width, int *height) {
  int intensity = 0;
  int arraysze = 0;
  char *imagedata = NULL;
  if (readDIMs(imgfile, width, height, &intensity) != 0) {
    printf("could not read DIMS\n");
    return -1;
  }
  printf("read ASCII grey\n");
  printf("w:%d h:%d i:%d\n", *width, *height, intensity);
  if (*width < 1) {
    printf("Invalid width\n");
    return -1;
  }
  if (*height < 1) {
    printf("Invalid height\n");
    return -1;
  }
  arraysze = (*width) * (*height);
  imagedata = (char *)malloc(sizeof(char) * arraysze);
  printf("arraysze:%d\n", arraysze);
  if (imagedata == NULL) {
    printf("Failed to allocate memory, Possibly out of memory\n");
    return -1;
  }
  memset(imagedata, 0, sizeof(char) * arraysze);
  if (fread(imagedata, sizeof(char), arraysze, imgfile) != arraysze) {
    printf("failed to read imagedata\n");
    return -1;
  }
  *image_data = imagedata;
  return arraysze;
}


x86 assembly (clang, -O0):
<readASCIIgrey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1060 <readDIMs@plt>
cmp    $0x0,%rax
je     11b7 <readASCIIgrey+0x67>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <readASCIIgrey+0x197>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
mov    -0x2c(%rbp),%ecx
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
cmpl   $0x1,(%rax)
jge    1209 <readASCIIgrey+0xb9>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <readASCIIgrey+0x197>
mov    -0x28(%rbp),%rax
cmpl   $0x1,(%rax)
jge    1230 <readASCIIgrey+0xe0>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <readASCIIgrey+0x197>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x28(%rbp),%rcx
imul   (%rcx),%eax
mov    %eax,-0x30(%rbp)
movslq -0x30(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
jne    1289 <readASCIIgrey+0x139>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <readASCIIgrey+0x197>
mov    -0x38(%rbp),%rdi
movslq -0x30(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x30(%rbp),%eax
je     12d6 <readASCIIgrey+0x186>
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <readASCIIgrey+0x197>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readASCIIgrey>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rcx
mov    %rdx,%rsi
mov    %rbp,%rdx
call   1070 <readDIMs@plt>
test   %rax,%rax
je     11ae <readASCIIgrey+0x4e>
lea    0xefe(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%esi
mov    0x0(%rbp),%edx
mov    0x4(%rsp),%ecx
lea    0xe36(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%eax
test   %eax,%eax
jle    122a <readASCIIgrey+0xca>
mov    0x0(%rbp),%ebp
test   %ebp,%ebp
jle    1236 <readASCIIgrey+0xd6>
imul   %eax,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0xe1e(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %rbx,%rbx
je     1242 <readASCIIgrey+0xe2>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    %ebp,%eax
jne    124e <readASCIIgrey+0xee>
mov    %rbx,(%r14)
jmp    11a1 <readASCIIgrey+0x41>
lea    0xe56(%rip),%rdi
jmp    1197 <readASCIIgrey+0x37>
lea    0xe3b(%rip),%rdi
jmp    1197 <readASCIIgrey+0x37>
lea    0xdfd(%rip),%rdi
jmp    1197 <readASCIIgrey+0x37>
lea    0xdd8(%rip),%rdi
jmp    1197 <readASCIIgrey+0x37>

LLVM IR (-O0):
@.str = [21 x i8] c"could not read DIMS\0A\00"
@.str.1 = [17 x i8] c"read ASCII grey\0A\00"
@.str.2 = [16 x i8] c"w:%d h:%d i:%d\0A\00"
@.str.3 = [15 x i8] c"Invalid width\0A\00"
@.str.4 = [16 x i8] c"Invalid height\0A\00"
@.str.5 = [13 x i8] c"arraysze:%d\0A\00"
@.str.6 = [51 x i8] c"Failed to allocate memory, Possibly out of memory\0A\00"
@.str.7 = [26 x i8] c"failed to read imagedata\0A\00"
define i32 @readASCIIgrey(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 i32
%12 = alloca i8*
store i32* %0, i32** %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32 0, i32* %10
store i32 0, i32* %11
store i8* null, i8** %12
%13 = load i32*, i32** %6
%14 = load i32*, i32** %8
%15 = load i32*, i32** %9
%16 = call i64 @readDIMs(i32* %13, i32* %14, i32* %15, i32* %10)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %76
20: ; preds = %4
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32*, i32** %8
%23 = load i32, i32* %22
%24 = load i32*, i32** %9
%25 = load i32, i32* %24
%26 = load i32, i32* %10
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %25, i32 %26)
%28 = load i32*, i32** %8
%29 = load i32, i32* %28
%30 = icmp slt i32 %29, 1
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
br label %76
33: ; preds = %20
%34 = load i32*, i32** %9
%35 = load i32, i32* %34
%36 = icmp slt i32 %35, 1
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %5
br label %76
39: ; preds = %33
%40 = load i32*, i32** %8
%41 = load i32, i32* %40
%42 = load i32*, i32** %9
%43 = load i32, i32* %42
%44 = mul nsw i32 %41, %43
store i32 %44, i32* %11
%45 = load i32, i32* %11
%46 = sext i32 %45 to i64
%47 = mul i64 1, %46
%48 = trunc i64 %47 to i32
%49 = call i64 @malloc(i32 %48)
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %12
%51 = load i32, i32* %11
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %51)
%53 = load i8*, i8** %12
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %57
55: ; preds = %39
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %5
br label %76
57: ; preds = %39
%58 = load i8*, i8** %12
%59 = load i32, i32* %11
%60 = sext i32 %59 to i64
%61 = mul i64 1, %60
%62 = trunc i64 %61 to i32
%63 = call i32 @memset(i8* %58, i32 0, i32 %62)
%64 = load i8*, i8** %12
%65 = load i32, i32* %11
%66 = load i32*, i32** %6
%67 = call i32 @fread(i8* %64, i32 1, i32 %65, i32* %66)
%68 = load i32, i32* %11
%69 = icmp ne i32 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %57
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %5
br label %76
72: ; preds = %57
%73 = load i8*, i8** %12
%74 = load i8**, i8*** %7
store i8* %73, i8** %74
%75 = load i32, i32* %11
store i32 %75, i32* %5
br label %76
76: ; preds = %72, %70, %55, %37, %31, %18
%77 = load i32, i32* %5
ret i32 %77
}

LLVM IR (-O2):
@.str.2 = [16 x i8] c"w:%d h:%d i:%d\0A\00"
@.str.5 = [13 x i8] c"arraysze:%d\0A\00"
@str = [16 x i8] c"read ASCII grey\00"
@str.8 = [25 x i8] c"failed to read imagedata\00"
@str.9 = [50 x i8] c"Failed to allocate memory, Possibly out of memory\00"
@str.10 = [15 x i8] c"Invalid height\00"
@str.11 = [14 x i8] c"Invalid width\00"
@str.12 = [20 x i8] c"could not read DIMS\00"
define i32 @readASCIIgrey(i32* %0, i8** nocapture writeonly %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%7 = call i64 @readDIMs(i32* %0, i32* %2, i32* %3, i32* %5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.12, i64 0, i64 0))
br label %41
11: ; preds = %4
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%13 = load i32, i32* %2
%14 = load i32, i32* %3
%15 = load i32, i32* %5
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %14, i32 %15)
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0))
br label %41
21: ; preds = %11
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
br label %41
26: ; preds = %21
%27 = mul nsw i32 %22, %17
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%31 = icmp eq i64 %28, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.9, i64 0, i64 0))
br label %41
34: ; preds = %26
%35 = call i32 @memset(i8* %29, i32 0, i32 %27)
%36 = call i32 @fread(i8* %29, i32 1, i32 %27, i32* %0)
%37 = icmp eq i32 %36, %27
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.8, i64 0, i64 0))
br label %41
40: ; preds = %34
store i8* %29, i8** %1
br label %41
41: ; preds = %40, %38, %32, %24, %19, %9
%42 = phi i32 [ -1, %9 ], [ -1, %19 ], [ -1, %24 ], [ -1, %32 ], [ -1, %38 ], [ %27, %40 ]
ret i32 %42
}