#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 EOF;
int /*<<< orphan*/ errhandler(int, char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int **imatrix(int /*<<< orphan*/, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ sscanf(char *, char *, int *, ...);
int **read_pgm(const char *filename) {
  FILE *input = NULL;
  char line[1024];
  int levels;
  int xsize, ysize;
  int i1, j1;
  int **pic = NULL;
  if ((input = fopen(filename, "rb")) == 0) {
    char szBuff[80];
    sprintf(szBuff, "Can not read file %s !!!", filename);
    errhandler(
        439,
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/grantathon/computational_fluid_dynamics/"
        "refs/heads/master/03_assignment/helper.c",
        szBuff);
  }
  if (fread(line, 1, 3, input) != 3) {
    fclose(input);
    errhandler(
        446,
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/grantathon/computational_fluid_dynamics/"
        "refs/heads/master/03_assignment/helper.c",
        "Error Wrong Magic field!");
  }
  do
    fgets(line, sizeof line, input);
  while (*line == '#');
  sscanf(line, "%d %d\n", &xsize, &ysize);
  printf("Image size: %d x %d\n", xsize, ysize);
  fgets(line, sizeof line, input);
  sscanf(line, "%d\n", &levels);
  pic = imatrix(0, xsize + 1, 0, ysize + 1);
  printf("Image initialised...\n");
  for (j1 = 1; j1 < ysize + 1; j1++) {
    for (i1 = 1; i1 < xsize + 1; i1++) {
      int byte;
      fscanf(input, "%d", &byte);
      if (byte == EOF) {
        fclose(input);
        errhandler(
            479,
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/grantathon/computational_fluid_dynamics/"
            "refs/heads/master/03_assignment/helper.c",
            "read failed");
      } else {
        pic[i1][ysize + 1 - j1] = byte;
      }
    }
  }
  for (i1 = 0; i1 < xsize + 2; i1++) {
    pic[i1][0] = 0;
  }
  for (i1 = 0; i1 < xsize + 2; i1++) {
    pic[i1][ysize + 1] = 0;
  }
  for (j1 = 0; j1 < ysize + 2; j1++) {
    pic[0][j1] = 0;
    pic[xsize + 1][j1] = 0;
  }
  fclose(input);
  return pic;
}


x86 assembly (clang, -O0):
<read_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x490,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x430(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    120f <read_pgm+0x6f>
lea    -0x480(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe11(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x480(%rbp),%rdx
mov    $0x1b7,%edi
lea    0xe12(%rip),%rsi
call   1050 <errhandler@plt>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1040 <fread@plt>
cmp    $0x3,%eax
je     1253 <read_pgm+0xb3>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    $0x1be,%edi
lea    0xdd5(%rip),%rsi
lea    0xe79(%rip),%rdx
call   1050 <errhandler@plt>
jmp    1258 <read_pgm+0xb8>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1090 <fgets@plt>
movsbl -0x410(%rbp),%eax
cmp    $0x23,%eax
je     1258 <read_pgm+0xb8>
lea    -0x410(%rbp),%rdi
lea    0xe55(%rip),%rsi
lea    -0x418(%rbp),%rdx
lea    -0x41c(%rbp),%rcx
mov    $0x0,%al
call   10a0 <sscanf@plt>
mov    -0x418(%rbp),%esi
mov    -0x41c(%rbp),%edx
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1090 <fgets@plt>
lea    -0x410(%rbp),%rdi
lea    0xe06(%rip),%rsi
lea    -0x414(%rbp),%rdx
mov    $0x0,%al
call   10a0 <sscanf@plt>
mov    -0x418(%rbp),%esi
add    $0x1,%esi
mov    -0x41c(%rbp),%ecx
add    $0x1,%ecx
xor    %edx,%edx
mov    %edx,%edi
call   1030 <imatrix@plt>
mov    %rax,-0x430(%rbp)
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x424(%rbp)
mov    -0x424(%rbp),%eax
mov    -0x41c(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    140e <read_pgm+0x26e>
movl   $0x1,-0x420(%rbp)
mov    -0x420(%rbp),%eax
mov    -0x418(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    13f5 <read_pgm+0x255>
mov    -0x10(%rbp),%rdi
lea    0xdaa(%rip),%rsi
lea    -0x484(%rbp),%rdx
call   1080 <fscanf@plt>
mov    -0x484(%rbp),%eax
mov    0x2c67(%rip),%rcx
cmp    (%rcx),%eax
jne    13af <read_pgm+0x20f>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    $0x1df,%edi
lea    0xc7e(%rip),%rsi
lea    0xd70(%rip),%rdx
call   1050 <errhandler@plt>
jmp    13dc <read_pgm+0x23c>
mov    -0x484(%rbp),%edx
mov    -0x430(%rbp),%rax
movslq -0x420(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x41c(%rbp),%ecx
add    $0x1,%ecx
sub    -0x424(%rbp),%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    13e1 <read_pgm+0x241>
mov    -0x420(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x420(%rbp)
jmp    1346 <read_pgm+0x1a6>
jmp    13fa <read_pgm+0x25a>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    1325 <read_pgm+0x185>
movl   $0x0,-0x420(%rbp)
mov    -0x420(%rbp),%eax
mov    -0x418(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jge    145b <read_pgm+0x2bb>
mov    -0x430(%rbp),%rax
movslq -0x420(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movl   $0x0,(%rax)
mov    -0x420(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x420(%rbp)
jmp    1418 <read_pgm+0x278>
movl   $0x0,-0x420(%rbp)
mov    -0x420(%rbp),%eax
mov    -0x418(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jge    14b5 <read_pgm+0x315>
mov    -0x430(%rbp),%rax
movslq -0x420(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x41c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x420(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x420(%rbp)
jmp    1465 <read_pgm+0x2c5>
movl   $0x0,-0x424(%rbp)
mov    -0x424(%rbp),%eax
mov    -0x41c(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jge    1527 <read_pgm+0x387>
mov    -0x430(%rbp),%rax
mov    (%rax),%rax
movslq -0x424(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x430(%rbp),%rax
mov    -0x418(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x424(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    14bf <read_pgm+0x31f>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x430(%rbp),%rax
add    $0x490,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pgm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    %rdi,%rbp
lea    0xe35(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1203 <read_pgm+0x53>
lea    0xe24(%rip),%rsi
lea    0x30(%rsp),%r14
mov    %r14,%rdi
mov    %rbp,%rdx
call   10d0 <sprintf@plt>
lea    0xe26(%rip),%rsi
mov    $0x1b7,%edi
mov    %r14,%rdx
call   1060 <errhandler@plt>
lea    0x30(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    $0x3,%eax
je     123f <read_pgm+0x8f>
mov    %rbx,%rdi
call   1070 <fclose@plt>
lea    0xdee(%rip),%rsi
lea    0xe92(%rip),%rdx
mov    $0x1be,%edi
call   1060 <errhandler@plt>
lea    0x30(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   10a0 <fgets@plt>
cmpb   $0x23,0x30(%rsp)
je     1250 <read_pgm+0xa0>
lea    0xe72(%rip),%rsi
lea    0x30(%rsp),%rbp
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %rbp,%rdi
xor    %eax,%eax
call   10b0 <sscanf@plt>
mov    0x10(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe51(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   10a0 <fgets@plt>
lea    0xe2f(%rip),%rsi
lea    0x2c(%rsp),%rdx
mov    %rbp,%rdi
xor    %eax,%eax
call   10b0 <sscanf@plt>
mov    0x10(%rsp),%esi
add    $0x1,%esi
mov    0xc(%rsp),%ecx
add    $0x1,%ecx
xor    %edi,%edi
xor    %edx,%edx
call   1030 <imatrix@plt>
mov    %rax,0x18(%rsp)
lea    0xe25(%rip),%rdi
call   1040 <puts@plt>
mov    0xc(%rsp),%ecx
test   %ecx,%ecx
jle    13ae <read_pgm+0x1fe>
mov    $0x1,%esi
mov    0x10(%rsp),%eax
lea    0xdf5(%rip),%r15
lea    0x14(%rsp),%r12
mov    0x2cd5(%rip),%rbp
jmp    1336 <read_pgm+0x186>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0xc(%rsp),%ecx
mov    0x20(%rsp),%rsi
lea    0x1(%rsi),%edx
cmp    %ecx,%esi
mov    %edx,%esi
jge    13b2 <read_pgm+0x202>
test   %eax,%eax
jle    1329 <read_pgm+0x179>
mov    $0x1,%r13d
mov    %rsi,0x20(%rsp)
sub    %esi,%r13d
xor    %r14d,%r14d
jmp    1375 <read_pgm+0x1c5>
nopl   (%rax)
mov    0x18(%rsp),%rcx
mov    0x8(%rcx,%r14,8),%rcx
mov    0xc(%rsp),%edx
add    %r13d,%edx
movslq %edx,%rdx
mov    %eax,(%rcx,%rdx,4)
movslq 0x10(%rsp),%rax
add    $0x1,%r14
cmp    %rax,%r14
jge    1320 <read_pgm+0x170>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1090 <fscanf@plt>
mov    0x14(%rsp),%eax
cmp    0x0(%rbp),%eax
jne    1350 <read_pgm+0x1a0>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    $0x1df,%edi
lea    0xc7c(%rip),%rsi
lea    0xd58(%rip),%rdx
call   1060 <errhandler@plt>
jmp    1367 <read_pgm+0x1b7>
mov    0x10(%rsp),%eax
cmp    $0xffffffff,%eax
mov    0x18(%rsp),%rbp
jl     1420 <read_pgm+0x270>
mov    $0xffffffffffffffff,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rbp,%rax,8),%rcx
movl   $0x0,(%rcx)
movslq 0x10(%rsp),%rcx
add    $0x1,%rax
cmp    %rcx,%rax
jle    13d0 <read_pgm+0x220>
cmp    $0xffffffff,%ecx
jl     1420 <read_pgm+0x270>
mov    $0xffffffffffffffff,%rax
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rbp,%rax,8),%rcx
movslq 0xc(%rsp),%rdx
movl   $0x0,0x4(%rcx,%rdx,4)
movslq 0x10(%rsp),%rcx
add    $0x1,%rax
cmp    %rcx,%rax
jle    1400 <read_pgm+0x250>
cmpl   $0xffffffff,0xc(%rsp)
jl     1468 <read_pgm+0x2b8>
mov    0x0(%rbp),%rax
mov    $0xffffffffffffffff,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movl   $0x0,0x4(%rax,%rcx,4)
movslq 0x10(%rsp),%rdx
mov    0x8(%rbp,%rdx,8),%rdx
movl   $0x0,0x4(%rdx,%rcx,4)
movslq 0xc(%rsp),%rdx
add    $0x1,%rcx
cmp    %rdx,%rcx
jle    1440 <read_pgm+0x290>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    %rbp,%rax
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"Can not read file %s !!!\00"
@.str.2 = [171 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/grantathon/computational_fluid_dynamics/refs/heads/master/03_assignment/helper.c\00"
@.str.3 = [25 x i8] c"Error Wrong Magic field!\00"
@.str.4 = [7 x i8] c"%d %d\0A\00"
@.str.5 = [21 x i8] c"Image size: %d x %d\0A\00"
@.str.6 = [4 x i8] c"%d\0A\00"
@.str.7 = [22 x i8] c"Image initialised...\0A\00"
@.str.8 = [3 x i8] c"%d\00"
@EOF = global i32 0
@.str.9 = [12 x i8] c"read failed\00"
define i32** @read_pgm(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32**
%11 = alloca [80 x i8]
%12 = alloca i32
store i8* %0, i8** %2
store i32* null, i32** %3
store i32** null, i32*** %10
%13 = load i8*, i8** %2
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %3
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %22
16: ; preds = %1
%17 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%18 = load i8*, i8** %2
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%21 = call i32 @errhandler(i32 439, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %16, %1
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%24 = load i32*, i32** %3
%25 = call i32 @fread(i8* %23, i32 1, i32 3, i32* %24)
%26 = icmp ne i32 %25, 3
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i32*, i32** %3
%29 = call i32 @fclose(i32* %28)
%30 = call i32 @errhandler(i32 446, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %27, %22
br label %32
32: ; preds = %36, %31
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%34 = load i32*, i32** %3
%35 = call i32 @fgets(i8* %33, i32 1024, i32* %34)
br label %36
36: ; preds = %32
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 35
br i1 %40, label %32, label %41
41: ; preds = %36
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%43 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %42, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32* %6, i32* %7)
%44 = load i32, i32* %6
%45 = load i32, i32* %7
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %44, i32 %45)
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%48 = load i32*, i32** %3
%49 = call i32 @fgets(i8* %47, i32 1024, i32* %48)
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%51 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32* %5)
%52 = load i32, i32* %6
%53 = add nsw i32 %52, 1
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
%56 = call i32** @imatrix(i32 0, i32 %53, i32 0, i32 %55)
store i32** %56, i32*** %10
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %9
br label %58
58: ; preds = %97, %41
%59 = load i32, i32* %9
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
%62 = icmp slt i32 %59, %61
br i1 %62, label %63, label %100
63: ; preds = %58
store i32 1, i32* %8
br label %64
64: ; preds = %93, %63
%65 = load i32, i32* %8
%66 = load i32, i32* %6
%67 = add nsw i32 %66, 1
%68 = icmp slt i32 %65, %67
br i1 %68, label %69, label %96
69: ; preds = %64
%70 = load i32*, i32** %3
%71 = call i32 @fscanf(i32* %70, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32* %12)
%72 = load i32, i32* %12
%73 = load i32, i32* @EOF
%74 = icmp eq i32 %72, %73
br i1 %74, label %75, label %79
75: ; preds = %69
%76 = load i32*, i32** %3
%77 = call i32 @fclose(i32* %76)
%78 = call i32 @errhandler(i32 479, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %92
79: ; preds = %69
%80 = load i32, i32* %12
%81 = load i32**, i32*** %10
%82 = load i32, i32* %8
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i32*, i32** %81, i64 %83
%85 = load i32*, i32** %84
%86 = load i32, i32* %7
%87 = add nsw i32 %86, 1
%88 = load i32, i32* %9
%89 = sub nsw i32 %87, %88
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i32, i32* %85, i64 %90
store i32 %80, i32* %91
br label %92
92: ; preds = %79, %75
br label %93
93: ; preds = %92
%94 = load i32, i32* %8
%95 = add nsw i32 %94, 1
store i32 %95, i32* %8
br label %64
96: ; preds = %64
br label %97
97: ; preds = %96
%98 = load i32, i32* %9
%99 = add nsw i32 %98, 1
store i32 %99, i32* %9
br label %58
100: ; preds = %58
store i32 0, i32* %8
br label %101
101: ; preds = %113, %100
%102 = load i32, i32* %8
%103 = load i32, i32* %6
%104 = add nsw i32 %103, 2
%105 = icmp slt i32 %102, %104
br i1 %105, label %106, label %116
106: ; preds = %101
%107 = load i32**, i32*** %10
%108 = load i32, i32* %8
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds i32*, i32** %107, i64 %109
%111 = load i32*, i32** %110
%112 = getelementptr inbounds i32, i32* %111, i64 0
store i32 0, i32* %112
br label %113
113: ; preds = %106
%114 = load i32, i32* %8
%115 = add nsw i32 %114, 1
store i32 %115, i32* %8
br label %101
116: ; preds = %101
store i32 0, i32* %8
br label %117
117: ; preds = %132, %116
%118 = load i32, i32* %8
%119 = load i32, i32* %6
%120 = add nsw i32 %119, 2
%121 = icmp slt i32 %118, %120
br i1 %121, label %122, label %135
122: ; preds = %117
%123 = load i32**, i32*** %10
%124 = load i32, i32* %8
%125 = sext i32 %124 to i64
%126 = getelementptr inbounds i32*, i32** %123, i64 %125
%127 = load i32*, i32** %126
%128 = load i32, i32* %7
%129 = add nsw i32 %128, 1
%130 = sext i32 %129 to i64
%131 = getelementptr inbounds i32, i32* %127, i64 %130
store i32 0, i32* %131
br label %132
132: ; preds = %122
%133 = load i32, i32* %8
%134 = add nsw i32 %133, 1
store i32 %134, i32* %8
br label %117
135: ; preds = %117
store i32 0, i32* %9
br label %136
136: ; preds = %157, %135
%137 = load i32, i32* %9
%138 = load i32, i32* %7
%139 = add nsw i32 %138, 2
%140 = icmp slt i32 %137, %139
br i1 %140, label %141, label %160
141: ; preds = %136
%142 = load i32**, i32*** %10
%143 = getelementptr inbounds i32*, i32** %142, i64 0
%144 = load i32*, i32** %143
%145 = load i32, i32* %9
%146 = sext i32 %145 to i64
%147 = getelementptr inbounds i32, i32* %144, i64 %146
store i32 0, i32* %147
%148 = load i32**, i32*** %10
%149 = load i32, i32* %6
%150 = add nsw i32 %149, 1
%151 = sext i32 %150 to i64
%152 = getelementptr inbounds i32*, i32** %148, i64 %151
%153 = load i32*, i32** %152
%154 = load i32, i32* %9
%155 = sext i32 %154 to i64
%156 = getelementptr inbounds i32, i32* %153, i64 %155
store i32 0, i32* %156
br label %157
157: ; preds = %141
%158 = load i32, i32* %9
%159 = add nsw i32 %158, 1
store i32 %159, i32* %9
br label %136
160: ; preds = %136
%161 = load i32*, i32** %3
%162 = call i32 @fclose(i32* %161)
%163 = load i32**, i32*** %10
ret i32** %163
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"Can not read file %s !!!\00"
@.str.2 = [171 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/grantathon/computational_fluid_dynamics/refs/heads/master/03_assignment/helper.c\00"
@.str.3 = [25 x i8] c"Error Wrong Magic field!\00"
@.str.4 = [7 x i8] c"%d %d\0A\00"
@.str.5 = [21 x i8] c"Image size: %d x %d\0A\00"
@.str.6 = [4 x i8] c"%d\0A\00"
@.str.8 = [3 x i8] c"%d\00"
@EOF = global i32 0
@.str.9 = [12 x i8] c"read failed\00"
@str = [21 x i8] c"Image initialised...\00"
define i32** @read_pgm(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [80 x i8]
%7 = alloca i32
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%9 = bitcast i32* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = bitcast i32* %5 to i8*
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%16 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%17 = call i32 @errhandler(i32 439, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %18
18: ; preds = %14, %1
%19 = call i32 @fread(i8* %8, i32 1, i32 3, i32* %12)
%20 = icmp eq i32 %19, 3
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = call i32 @fclose(i32* %12)
%23 = call i32 @errhandler(i32 446, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %21, %18
br label %25
25: ; preds = %24, %25
%26 = call i32 @fgets(i8* %8, i32 1024, i32* %12)
%27 = load i8, i8* %8
%28 = icmp eq i8 %27, 35
br i1 %28, label %25, label %29
29: ; preds = %25
%30 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32* %4, i32* %5)
%31 = load i32, i32* %4
%32 = load i32, i32* %5
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %31, i32 %32)
%34 = call i32 @fgets(i8* %8, i32 1024, i32* %12)
%35 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32* %3)
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 1
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
%40 = call i32** @imatrix(i32 0, i32 %37, i32 0, i32 %39)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%42 = load i32, i32* %5
%43 = icmp slt i32 %42, 1
br i1 %43, label %44, label %46
44: ; preds = %29
%45 = load i32, i32* %4
br label %56
46: ; preds = %29
%47 = bitcast i32* %7 to i8*
%48 = load i32, i32* %4
br label %49
49: ; preds = %46, %82
%50 = phi i32 [ %42, %46 ], [ %83, %82 ]
%51 = phi i32 [ %48, %46 ], [ %84, %82 ]
%52 = phi i32 [ 1, %46 ], [ %85, %82 ]
%53 = icmp slt i32 %51, 1
br i1 %53, label %82, label %54
54: ; preds = %49
%55 = sub nsw i32 1, %52
br label %59
56: ; preds = %82, %44
%57 = phi i32 [ %45, %44 ], [ %84, %82 ]
%58 = icmp sgt i32 %57, -2
br i1 %58, label %89, label %97
59: ; preds = %54, %75
%60 = phi i64 [ 1, %54 ], [ %76, %75 ]
%61 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32* %7)
%62 = load i32, i32* %7
%63 = load i32, i32* @EOF
%64 = icmp eq i32 %62, %63
br i1 %64, label %65, label %68
65: ; preds = %59
%66 = call i32 @fclose(i32* %12)
%67 = call i32 @errhandler(i32 479, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %75
68: ; preds = %59
%69 = getelementptr inbounds i32*, i32** %40, i64 %60
%70 = load i32*, i32** %69
%71 = load i32, i32* %5
%72 = add i32 %55, %71
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %70, i64 %73
store i32 %62, i32* %74
br label %75
75: ; preds = %68, %65
%76 = add nuw nsw i64 %60, 1
%77 = load i32, i32* %4
%78 = sext i32 %77 to i64
%79 = icmp slt i64 %60, %78
br i1 %79, label %59, label %80
80: ; preds = %75
%81 = load i32, i32* %5
br label %82
82: ; preds = %80, %49
%83 = phi i32 [ %81, %80 ], [ %50, %49 ]
%84 = phi i32 [ %77, %80 ], [ %51, %49 ]
%85 = add nuw nsw i32 %52, 1
%86 = icmp slt i32 %52, %83
br i1 %86, label %49, label %56
87: ; preds = %89
%88 = icmp sgt i32 %94, -2
br i1 %88, label %102, label %97
89: ; preds = %56, %89
%90 = phi i64 [ %93, %89 ], [ 0, %56 ]
%91 = getelementptr inbounds i32*, i32** %40, i64 %90
%92 = load i32*, i32** %91
store i32 0, i32* %92
%93 = add nuw nsw i64 %90, 1
%94 = load i32, i32* %4
%95 = sext i32 %94 to i64
%96 = icmp sgt i64 %90, %95
br i1 %96, label %87, label %89
97: ; preds = %102, %56, %87
%98 = load i32, i32* %5
%99 = icmp sgt i32 %98, -2
br i1 %99, label %100, label %127
100: ; preds = %97
%101 = load i32*, i32** %40
br label %114
102: ; preds = %87, %102
%103 = phi i64 [ %110, %102 ], [ 0, %87 ]
%104 = getelementptr inbounds i32*, i32** %40, i64 %103
%105 = load i32*, i32** %104
%106 = load i32, i32* %5
%107 = add nsw i32 %106, 1
%108 = sext i32 %107 to i64
%109 = getelementptr inbounds i32, i32* %105, i64 %108
store i32 0, i32* %109
%110 = add nuw nsw i64 %103, 1
%111 = load i32, i32* %4
%112 = sext i32 %111 to i64
%113 = icmp sgt i64 %103, %112
br i1 %113, label %97, label %102
114: ; preds = %100, %114
%115 = phi i64 [ 0, %100 ], [ %123, %114 ]
%116 = getelementptr inbounds i32, i32* %101, i64 %115
store i32 0, i32* %116
%117 = load i32, i32* %4
%118 = add nsw i32 %117, 1
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds i32*, i32** %40, i64 %119
%121 = load i32*, i32** %120
%122 = getelementptr inbounds i32, i32* %121, i64 %115
store i32 0, i32* %122
%123 = add nuw nsw i64 %115, 1
%124 = load i32, i32* %5
%125 = sext i32 %124 to i64
%126 = icmp sgt i64 %115, %125
br i1 %126, label %127, label %114
127: ; preds = %114, %97
%128 = call i32 @fclose(i32* %12)
ret i32** %40
}