#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 /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ isprint(char);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ readDIMs(int /*<<< orphan*/ *, int *, int *, int *);
int readgrey(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 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);
  {
    int idxrd = 0;
    char c = 0;
    for (; idxrd < arraysze; ++idxrd) {
      fread(&c, 1, 1, imgfile);
      printf("DBG: Idx = %d, char = (%02x) %c\n", idxrd, c,
             (isprint(c) ? c : '.'));
      imagedata[idxrd] = c;
    }
  }
  *image_data = imagedata;
  return arraysze;
}


x86 assembly (clang, -O0):
<readgrey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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     11c7 <readgrey+0x67>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <readgrey+0x1f6>
lea    0xe47(%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    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
cmpl   $0x1,(%rax)
jge    1219 <readgrey+0xb9>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <readgrey+0x1f6>
mov    -0x28(%rbp),%rax
cmpl   $0x1,(%rax)
jge    1240 <readgrey+0xe0>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <readgrey+0x1f6>
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    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
jne    1299 <readgrey+0x139>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <readgrey+0x1f6>
mov    -0x38(%rbp),%rdi
movslq -0x30(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0x0,-0x3c(%rbp)
movb   $0x0,-0x3d(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1345 <readgrey+0x1e5>
mov    -0x10(%rbp),%rcx
lea    -0x3d(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x48(%rbp)
movsbl -0x3d(%rbp),%eax
mov    %eax,-0x44(%rbp)
movsbl -0x3d(%rbp),%edi
call   1080 <isprint@plt>
cmp    $0x0,%rax
je     1305 <readgrey+0x1a5>
movsbl -0x3d(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    1312 <readgrey+0x1b2>
mov    $0x2e,%eax
mov    %eax,-0x4c(%rbp)
jmp    1312 <readgrey+0x1b2>
mov    -0x44(%rbp),%edx
mov    -0x48(%rbp),%esi
mov    -0x4c(%rbp),%ecx
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x3d(%rbp),%dl
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    12b9 <readgrey+0x159>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readgrey>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r12
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rcx
mov    %rdx,%rsi
mov    %rbp,%rdx
call   1070 <readDIMs@plt>
test   %rax,%rax
je     11c9 <readgrey+0x59>
lea    0xee9(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%esi
mov    0x0(%rbp),%edx
mov    0xc(%rsp),%ecx
lea    0xe1b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%eax
test   %eax,%eax
jle    12ad <readgrey+0x13d>
mov    0x0(%rbp),%ebp
test   %ebp,%ebp
jle    12b9 <readgrey+0x149>
imul   %eax,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
lea    0xdfb(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r15,%r15
je     12c5 <readgrey+0x155>
mov    %r14,0x10(%rsp)
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
movb   $0x0,0xb(%rsp)
test   %ebp,%ebp
jle    12a0 <readgrey+0x130>
mov    %ebp,%r13d
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
lea    0xb(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
movsbl 0xb(%rsp),%ebx
mov    %ebx,%edi
call   1090 <isprint@plt>
test   %rax,%rax
mov    %ebx,%ecx
mov    $0x2e,%eax
cmove  %eax,%ecx
lea    0xd96(%rip),%rdi
mov    %r14d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %bl,(%r15,%r14,1)
add    $0x1,%r14
cmp    %r14,%r13
jne    1250 <readgrey+0xe0>
mov    0x10(%rsp),%rax
mov    %r15,(%rax)
jmp    11b8 <readgrey+0x48>
lea    0xdd5(%rip),%rdi
jmp    11ae <readgrey+0x3e>
lea    0xdba(%rip),%rdi
jmp    11ae <readgrey+0x3e>
lea    0xd7c(%rip),%rdi
jmp    11ae <readgrey+0x3e>

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

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.7 = [33 x i8] c"DBG: Idx = %d, char = (%02x) %c\0A\00"
@str = [10 x i8] c"read grey\00"
@str.8 = [50 x i8] c"Failed to allocate memory, Possibly out of memory\00"
@str.9 = [15 x i8] c"Invalid height\00"
@str.10 = [14 x i8] c"Invalid width\00"
@str.11 = [20 x i8] c"could not read DIMS\00"
define i32 @readgrey(i32* %0, i8** nocapture writeonly %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8
%7 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%8 = call i64 @readDIMs(i32* %0, i32* %2, i32* %3, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %4
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.11, i64 0, i64 0))
br label %54
12: ; preds = %4
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%14 = load i32, i32* %2
%15 = load i32, i32* %3
%16 = load i32, i32* %5
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %15, i32 %16)
%18 = load i32, i32* %2
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.10, i64 0, i64 0))
br label %54
22: ; preds = %12
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
br label %54
27: ; preds = %22
%28 = mul nsw i32 %23, %18
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %28)
%32 = icmp eq i64 %29, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.8, i64 0, i64 0))
br label %54
35: ; preds = %27
%36 = call i32 @memset(i8* %30, i32 0, i32 %28)
store i8 0, i8* %6
%37 = icmp sgt i32 %28, 0
br i1 %37, label %38, label %53
38: ; preds = %35
%39 = zext i32 %28 to i64
br label %40
40: ; preds = %38, %40
%41 = phi i64 [ 0, %38 ], [ %51, %40 ]
%42 = call i32 @fread(i8* %6, i32 1, i32 1, i32* %0)
%43 = load i8, i8* %6
%44 = sext i8 %43 to i32
%45 = call i64 @isprint(i8 signext %43)
%46 = icmp eq i64 %45, 0
%47 = select i1 %46, i32 46, i32 %44
%48 = trunc i64 %41 to i32
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i32 %48, i32 %44, i32 %47)
%50 = getelementptr inbounds i8, i8* %30, i64 %41
store i8 %43, i8* %50
%51 = add nuw nsw i64 %41, 1
%52 = icmp eq i64 %51, %39
br i1 %52, label %53, label %40
53: ; preds = %40, %35
store i8* %30, i8** %1
br label %54
54: ; preds = %53, %33, %25, %20, %10
%55 = phi i32 [ -1, %10 ], [ -1, %20 ], [ -1, %25 ], [ -1, %33 ], [ %28, %53 ]
ret i32 %55
}