#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*/ drawArrows(int **, int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
void readBMPFileAndConvertToMatrix() {
  int width = 512;
  int height = 512;
  int image[width * height][3];
  FILE *streamIn;
  int i, j;
  streamIn = fopen("lena.bmp", "r");
  if (streamIn == (FILE *)0) {
    printf("File opening error ocurred. Exiting program.\n");
    exit(0);
  }
  int byte;
  for (i = 0; i < 54; i++) {
    byte = getc(streamIn);
  }
  int **matrix = (int **)malloc(width * sizeof(int *));
  for (i = 0; i < width; i++)
    matrix[i] = (int *)malloc(height * sizeof(int));
  for (i = 0; i < width; i++) {
    for (j = 0; j < height; j++) {
      image[width * j + i][2] = getc(streamIn);
      image[width * j + i][1] = getc(streamIn);
      image[width * j + i][0] = getc(streamIn);
      matrix[i][j] = (image[width * j + i][0] + image[width * j + i][1] +
                      image[width * j + i][2]) /
                     3;
    }
  }
  drawArrows(matrix, width, height);
  fclose(streamIn);
}


x86 assembly (clang, -O0):
<readBMPFileAndConvertToMatrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x200,-0x4(%rbp)
movl   $0x200,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
imul   %ecx,%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    (%rax,%rax,2),%rcx
lea    0xf(,%rcx,4),%rdx
movabs $0x1ffffffff0,%rcx
and    %rcx,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    0xe39(%rip),%rdi
lea    0xe3b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f5 <readBMPFileAndConvertToMatrix+0x85>
lea    0xe22(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x36,-0x24(%rbp)
jge    1220 <readBMPFileAndConvertToMatrix+0xb0>
mov    -0x20(%rbp),%rdi
call   1090 <getc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11fc <readBMPFileAndConvertToMatrix+0x8c>
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1272 <readBMPFileAndConvertToMatrix+0x102>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    123a <readBMPFileAndConvertToMatrix+0xca>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1392 <readBMPFileAndConvertToMatrix+0x222>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    137f <readBMPFileAndConvertToMatrix+0x20f>
mov    -0x20(%rbp),%rdi
call   1090 <getc@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
mov    -0x4(%rbp),%edx
imul   -0x28(%rbp),%edx
add    -0x24(%rbp),%edx
movslq %edx,%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rdi
call   1090 <getc@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
mov    -0x4(%rbp),%edx
imul   -0x28(%rbp),%edx
add    -0x24(%rbp),%edx
movslq %edx,%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%rdi
call   1090 <getc@plt>
mov    -0x40(%rbp),%rcx
mov    %eax,%edx
mov    -0x4(%rbp),%eax
imul   -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
cltq
imul   $0xc,%rax,%rsi
mov    %rcx,%rax
add    %rsi,%rax
mov    %edx,(%rax)
mov    -0x4(%rbp),%eax
imul   -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
cltq
imul   $0xc,%rax,%rdx
mov    %rcx,%rax
add    %rdx,%rax
mov    (%rax),%eax
mov    -0x4(%rbp),%edx
imul   -0x28(%rbp),%edx
add    -0x24(%rbp),%edx
movslq %edx,%rdx
imul   $0xc,%rdx,%rsi
mov    %rcx,%rdx
add    %rsi,%rdx
add    0x4(%rdx),%eax
mov    -0x4(%rbp),%edx
imul   -0x28(%rbp),%edx
add    -0x24(%rbp),%edx
movslq %edx,%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rcx
add    0x8(%rcx),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    128c <readBMPFileAndConvertToMatrix+0x11c>
jmp    1384 <readBMPFileAndConvertToMatrix+0x214>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1279 <readBMPFileAndConvertToMatrix+0x109>
mov    -0x38(%rbp),%rdi
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1030 <drawArrows@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readBMPFileAndConvertToMatrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1272 <readBMPFileAndConvertToMatrix+0x102>
mov    %rax,%r15
mov    $0x36,%ebx
nop
mov    %r15,%rdi
call   1090 <getc@plt>
add    $0xffffffff,%ebx
jne    11a0 <readBMPFileAndConvertToMatrix+0x30>
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    $0x800,%edi
call   1060 <malloc@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
cmp    $0x200,%rbx
jne    11c0 <readBMPFileAndConvertToMatrix+0x50>
xor    %r12d,%r12d
xchg   %ax,%ax
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
call   1090 <getc@plt>
mov    %eax,%r13d
mov    %r15,%rdi
call   1090 <getc@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1090 <getc@plt>
add    %r13d,%ebx
add    %eax,%ebx
movslq %ebx,%rax
imul   $0x55555556,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
mov    (%r14,%r12,8),%rcx
mov    %eax,(%rcx,%rbp,4)
add    $0x1,%rbp
cmp    $0x200,%rbp
jne    11f0 <readBMPFileAndConvertToMatrix+0x80>
add    $0x1,%r12
cmp    $0x200,%r12
jne    11e0 <readBMPFileAndConvertToMatrix+0x70>
mov    %r14,%rdi
mov    $0x200,%esi
mov    $0x200,%edx
call   1030 <drawArrows@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
lea    0xd92(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"lena.bmp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [46 x i8] c"File opening error ocurred. Exiting program.\0A\00"
define void @readBMPFileAndConvertToMatrix() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32**
store i32 512, i32* %1
store i32 512, i32* %2
%10 = load i32, i32* %1
%11 = load i32, i32* %2
%12 = mul nsw i32 %10, %11
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %3
%15 = alloca [3 x i32], i64 %13
store i64 %13, i64* %4
%16 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %5
%17 = load i32*, i32** %5
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %0
%20 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %0
store i32 0, i32* %6
br label %23
23: ; preds = %29, %22
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 54
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = load i32*, i32** %5
%28 = call i32 @getc(i32* %27)
store i32 %28, i32* %8
br label %29
29: ; preds = %26
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %23
32: ; preds = %23
%33 = load i32, i32* %1
%34 = sext i32 %33 to i64
%35 = mul i64 %34, 8
%36 = trunc i64 %35 to i32
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i32**
store i32** %38, i32*** %9
store i32 0, i32* %6
br label %39
39: ; preds = %54, %32
%40 = load i32, i32* %6
%41 = load i32, i32* %1
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %57
43: ; preds = %39
%44 = load i32, i32* %2
%45 = sext i32 %44 to i64
%46 = mul i64 %45, 4
%47 = trunc i64 %46 to i32
%48 = call i64 @malloc(i32 %47)
%49 = inttoptr i64 %48 to i32*
%50 = load i32**, i32*** %9
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32*, i32** %50, i64 %52
store i32* %49, i32** %53
br label %54
54: ; preds = %43
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
br label %39
57: ; preds = %39
store i32 0, i32* %6
br label %58
58: ; preds = %140, %57
%59 = load i32, i32* %6
%60 = load i32, i32* %1
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %143
62: ; preds = %58
store i32 0, i32* %7
br label %63
63: ; preds = %136, %62
%64 = load i32, i32* %7
%65 = load i32, i32* %2
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %139
67: ; preds = %63
%68 = load i32*, i32** %5
%69 = call i32 @getc(i32* %68)
%70 = load i32, i32* %1
%71 = load i32, i32* %7
%72 = mul nsw i32 %70, %71
%73 = load i32, i32* %6
%74 = add nsw i32 %72, %73
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds [3 x i32], [3 x i32]* %15, i64 %75
%77 = getelementptr inbounds [3 x i32], [3 x i32]* %76, i64 0, i64 2
store i32 %69, i32* %77
%78 = load i32*, i32** %5
%79 = call i32 @getc(i32* %78)
%80 = load i32, i32* %1
%81 = load i32, i32* %7
%82 = mul nsw i32 %80, %81
%83 = load i32, i32* %6
%84 = add nsw i32 %82, %83
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds [3 x i32], [3 x i32]* %15, i64 %85
%87 = getelementptr inbounds [3 x i32], [3 x i32]* %86, i64 0, i64 1
store i32 %79, i32* %87
%88 = load i32*, i32** %5
%89 = call i32 @getc(i32* %88)
%90 = load i32, i32* %1
%91 = load i32, i32* %7
%92 = mul nsw i32 %90, %91
%93 = load i32, i32* %6
%94 = add nsw i32 %92, %93
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds [3 x i32], [3 x i32]* %15, i64 %95
%97 = getelementptr inbounds [3 x i32], [3 x i32]* %96, i64 0, i64 0
store i32 %89, i32* %97
%98 = load i32, i32* %1
%99 = load i32, i32* %7
%100 = mul nsw i32 %98, %99
%101 = load i32, i32* %6
%102 = add nsw i32 %100, %101
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds [3 x i32], [3 x i32]* %15, i64 %103
%105 = getelementptr inbounds [3 x i32], [3 x i32]* %104, i64 0, i64 0
%106 = load i32, i32* %105
%107 = load i32, i32* %1
%108 = load i32, i32* %7
%109 = mul nsw i32 %107, %108
%110 = load i32, i32* %6
%111 = add nsw i32 %109, %110
%112 = sext i32 %111 to i64
%113 = getelementptr inbounds [3 x i32], [3 x i32]* %15, i64 %112
%114 = getelementptr inbounds [3 x i32], [3 x i32]* %113, i64 0, i64 1
%115 = load i32, i32* %114
%116 = add nsw i32 %106, %115
%117 = load i32, i32* %1
%118 = load i32, i32* %7
%119 = mul nsw i32 %117, %118
%120 = load i32, i32* %6
%121 = add nsw i32 %119, %120
%122 = sext i32 %121 to i64
%123 = getelementptr inbounds [3 x i32], [3 x i32]* %15, i64 %122
%124 = getelementptr inbounds [3 x i32], [3 x i32]* %123, i64 0, i64 2
%125 = load i32, i32* %124
%126 = add nsw i32 %116, %125
%127 = sdiv i32 %126, 3
%128 = load i32**, i32*** %9
%129 = load i32, i32* %6
%130 = sext i32 %129 to i64
%131 = getelementptr inbounds i32*, i32** %128, i64 %130
%132 = load i32*, i32** %131
%133 = load i32, i32* %7
%134 = sext i32 %133 to i64
%135 = getelementptr inbounds i32, i32* %132, i64 %134
store i32 %127, i32* %135
br label %136
136: ; preds = %67
%137 = load i32, i32* %7
%138 = add nsw i32 %137, 1
store i32 %138, i32* %7
br label %63
139: ; preds = %63
br label %140
140: ; preds = %139
%141 = load i32, i32* %6
%142 = add nsw i32 %141, 1
store i32 %142, i32* %6
br label %58
143: ; preds = %58
%144 = load i32**, i32*** %9
%145 = load i32, i32* %1
%146 = load i32, i32* %2
%147 = call i32 @drawArrows(i32** %144, i32 %145, i32 %146)
%148 = load i32*, i32** %5
%149 = call i32 @fclose(i32* %148)
%150 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"lena.bmp\00"
@.str.1 = [2 x i8] c"r\00"
@str = [45 x i8] c"File opening error ocurred. Exiting program.\00"
define void @readBMPFileAndConvertToMatrix() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 0)
unreachable
6: ; preds = %0, %6
%7 = phi i32 [ %9, %6 ], [ 0, %0 ]
%8 = tail call i32 @getc(i32* %1)
%9 = add nuw nsw i32 %7, 1
%10 = icmp eq i32 %9, 54
br i1 %10, label %11, label %6
11: ; preds = %6
%12 = tail call i64 @malloc(i32 4096)
%13 = inttoptr i64 %12 to i32**
br label %14
14: ; preds = %11, %14
%15 = phi i64 [ 0, %11 ], [ %19, %14 ]
%16 = tail call i64 @malloc(i32 2048)
%17 = inttoptr i64 %16 to i32*
%18 = getelementptr inbounds i32*, i32** %13, i64 %15
store i32* %17, i32** %18
%19 = add nuw nsw i64 %15, 1
%20 = icmp eq i64 %19, 512
br i1 %20, label %21, label %14
21: ; preds = %14, %36
%22 = phi i64 [ %37, %36 ], [ 0, %14 ]
%23 = getelementptr inbounds i32*, i32** %13, i64 %22
br label %24
24: ; preds = %21, %24
%25 = phi i64 [ 0, %21 ], [ %34, %24 ]
%26 = tail call i32 @getc(i32* %1)
%27 = tail call i32 @getc(i32* %1)
%28 = tail call i32 @getc(i32* %1)
%29 = add i32 %27, %26
%30 = add i32 %29, %28
%31 = sdiv i32 %30, 3
%32 = load i32*, i32** %23
%33 = getelementptr inbounds i32, i32* %32, i64 %25
store i32 %31, i32* %33
%34 = add nuw nsw i64 %25, 1
%35 = icmp eq i64 %34, 512
br i1 %35, label %36, label %24
36: ; preds = %24
%37 = add nuw nsw i64 %22, 1
%38 = icmp eq i64 %37, 512
br i1 %38, label %39, label %21
39: ; preds = %36
%40 = tail call i32 @drawArrows(i32** %13, i32 512, i32 512)
%41 = tail call i32 @fclose(i32* %1)
ret void
}