#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*/ fatal(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char *, float *);
void readinput(float *vect, int grid_rows, int grid_cols, int layers,
               char *file) {
  int i, j, k;
  FILE *fp;
  char str[(256)];
  float val;
  if ((fp = fopen(file, "r")) == 0)
    fatal("The file was not opened");
  for (i = 0; i <= grid_rows - 1; i++)
    for (j = 0; j <= grid_cols - 1; j++)
      for (k = 0; k <= layers - 1; k++) {
        if (fgets(str, (256), fp) == NULL)
          fatal("Error reading file\n");
        if (feof(fp))
          fatal("not enough lines in file");
        if ((sscanf(str, "%f", &val) != 1))
          fatal("invalid file format");
        vect[i * grid_cols + j + k * grid_rows * grid_cols] = val;
      }
  fclose(fp);
}


x86 assembly (clang, -O0):
<readinput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11a6 <readinput+0x46>
lea    0xe61(%rip),%rdi
call   1080 <fatal@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     12c6 <readinput+0x166>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     12b3 <readinput+0x153>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     12a0 <readinput+0x140>
lea    -0x140(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1219 <readinput+0xb9>
lea    0xe06(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x38(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1238 <readinput+0xd8>
lea    0xdfb(%rip),%rdi
call   1080 <fatal@plt>
lea    -0x140(%rbp),%rdi
lea    0xe01(%rip),%rsi
lea    -0x144(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     1267 <readinput+0x107>
lea    0xde8(%rip),%rdi
call   1080 <fatal@plt>
movss  -0x144(%rbp),%xmm0
ff
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
imul   -0x10(%rbp),%ecx
add    -0x28(%rbp),%ecx
mov    -0x2c(%rbp),%edx
imul   -0xc(%rbp),%edx
imul   -0x10(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11dd <readinput+0x7d>
jmp    12a5 <readinput+0x145>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11c5 <readinput+0x65>
jmp    12b8 <readinput+0x158>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ad <readinput+0x4d>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readinput>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
mov    %ecx,0xc(%rsp)
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%r13
lea    0xe7b(%rip),%rsi
mov    %r8,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
je     119f <readinput+0x3f>
test   %r12d,%r12d
jg     11b4 <readinput+0x54>
jmp    12ba <readinput+0x15a>
lea    0xe5c(%rip),%rdi
call   1080 <fatal@plt>
test   %r12d,%r12d
jle    12ba <readinput+0x15a>
mov    0xc(%rsp),%eax
mov    %rax,0x28(%rsp)
mov    %r15d,%r14d
imul   %r12d,%r14d
movl   $0x0,0x8(%rsp)
lea    0x30(%rsp),%rbx
xor    %eax,%eax
mov    %r15d,0x1c(%rsp)
mov    %r12d,0x14(%rsp)
jmp    11fa <readinput+0x9a>
nop
mov    0x18(%rsp),%eax
add    $0x1,%eax
add    %r15d,0x8(%rsp)
mov    0x14(%rsp),%r12d
cmp    %r12d,%eax
je     12ba <readinput+0x15a>
mov    %eax,0x18(%rsp)
test   %r15d,%r15d
jle    11e0 <readinput+0x80>
xor    %eax,%eax
mov    0x8(%rsp),%ecx
mov    %ecx,0x10(%rsp)
jmp    1226 <readinput+0xc6>
nop
mov    0x20(%rsp),%eax
add    $0x1,%eax
addl   $0x1,0x10(%rsp)
mov    0x1c(%rsp),%r15d
cmp    %r15d,%eax
je     11e0 <readinput+0x80>
mov    %eax,0x20(%rsp)
cmpl   $0x0,0xc(%rsp)
jle    1210 <readinput+0xb0>
mov    0x28(%rsp),%r15
mov    0x10(%rsp),%eax
mov    %eax,%r12d
jmp    1259 <readinput+0xf9>
nop
movss  0x24(%rsp),%xmm0
movslq %r12d,%r12
movss  %xmm0,0x0(%r13,%r12,4)
add    %r14d,%r12d
add    $0xffffffffffffffff,%r15
je     1210 <readinput+0xb0>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %rbp,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    127a <readinput+0x11a>
lea    0xda5(%rip),%rdi
call   1080 <fatal@plt>
mov    %rbp,%rdi
call   1050 <feof@plt>
test   %rax,%rax
je     1293 <readinput+0x133>
lea    0xda0(%rip),%rdi
call   1080 <fatal@plt>
mov    %rbx,%rdi
lea    0xdaa(%rip),%rsi
lea    0x24(%rsp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     1240 <readinput+0xe0>
lea    0xd97(%rip),%rdi
call   1080 <fatal@plt>
jmp    1240 <readinput+0xe0>
mov    %rbp,%rdi
call   1030 <fclose@plt>
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"The file was not opened\00"
@.str.2 = [20 x i8] c"Error reading file\0A\00"
@.str.3 = [25 x i8] c"not enough lines in file\00"
@.str.4 = [3 x i8] c"%f\00"
@.str.5 = [20 x i8] c"invalid file format\00"
define void @readinput(float* %0, i32 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca float*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca [256 x i8]
%16 = alloca float
store float* %0, float** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%17 = load i8*, i8** %10
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %14
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %5
%21 = call i32 @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %5
store i32 0, i32* %11
br label %23
23: ; preds = %83, %22
%24 = load i32, i32* %11
%25 = load i32, i32* %7
%26 = sub nsw i32 %25, 1
%27 = icmp sle i32 %24, %26
br i1 %27, label %28, label %86
28: ; preds = %23
store i32 0, i32* %12
br label %29
29: ; preds = %79, %28
%30 = load i32, i32* %12
%31 = load i32, i32* %8
%32 = sub nsw i32 %31, 1
%33 = icmp sle i32 %30, %32
br i1 %33, label %34, label %82
34: ; preds = %29
store i32 0, i32* %13
br label %35
35: ; preds = %75, %34
%36 = load i32, i32* %13
%37 = load i32, i32* %9
%38 = sub nsw i32 %37, 1
%39 = icmp sle i32 %36, %38
br i1 %39, label %40, label %78
40: ; preds = %35
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%42 = load i32*, i32** %14
%43 = call i32* @fgets(i8* %41, i32 256, i32* %42)
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %45, %40
%48 = load i32*, i32** %14
%49 = call i64 @feof(i32* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @fatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%55 = call i32 @sscanf(i8* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), float* %16)
%56 = icmp ne i32 %55, 1
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %59
59: ; preds = %57, %53
%60 = load float, float* %16
%61 = load float*, float** %6
%62 = load i32, i32* %11
%63 = load i32, i32* %8
%64 = mul nsw i32 %62, %63
%65 = load i32, i32* %12
%66 = add nsw i32 %64, %65
%67 = load i32, i32* %13
%68 = load i32, i32* %7
%69 = mul nsw i32 %67, %68
%70 = load i32, i32* %8
%71 = mul nsw i32 %69, %70
%72 = add nsw i32 %66, %71
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds float, float* %61, i64 %73
store float %60, float* %74
br label %75
75: ; preds = %59
%76 = load i32, i32* %13
%77 = add nsw i32 %76, 1
store i32 %77, i32* %13
br label %35
78: ; preds = %35
br label %79
79: ; preds = %78
%80 = load i32, i32* %12
%81 = add nsw i32 %80, 1
store i32 %81, i32* %12
br label %29
82: ; preds = %29
br label %83
83: ; preds = %82
%84 = load i32, i32* %11
%85 = add nsw i32 %84, 1
store i32 %85, i32* %11
br label %23
86: ; preds = %23
%87 = load i32*, i32** %14
%88 = call i32 @fclose(i32* %87)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"The file was not opened\00"
@.str.2 = [20 x i8] c"Error reading file\0A\00"
@.str.3 = [25 x i8] c"not enough lines in file\00"
@.str.4 = [3 x i8] c"%f\00"
@.str.5 = [20 x i8] c"invalid file format\00"
define void @readinput(float* nocapture writeonly %0, i32 %1, i32 %2, i32 %3, i8* nocapture %4) {
%6 = alloca [256 x i8]
%7 = alloca float
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%9 = bitcast float* %7 to i8*
%10 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = tail call i32 @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %5
%15 = icmp sgt i32 %1, 0
br i1 %15, label %16, label %57
16: ; preds = %14
%17 = icmp sgt i32 %2, 0
%18 = icmp sgt i32 %3, 0
%19 = zext i32 %3 to i64
br label %20
20: ; preds = %16, %54
%21 = phi i32 [ 0, %16 ], [ %55, %54 ]
br i1 %17, label %22, label %54
22: ; preds = %20, %51
%23 = phi i32 [ %52, %51 ], [ 0, %20 ]
br i1 %18, label %24, label %51
24: ; preds = %22, %40
%25 = phi i64 [ %49, %40 ], [ 0, %22 ]
%26 = call i32* @fgets(i8* %8, i32 256, i32* %10)
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = call i64 @feof(i32* %10)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i32 @fatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), float* %7)
%37 = icmp eq i32 %36, 1
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = call i32 @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
%41 = load float, float* %7
%42 = trunc i64 %25 to i32
%43 = mul i32 %42, %1
%44 = add i32 %43, %21
%45 = mul i32 %44, %2
%46 = add i32 %45, %23
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds float, float* %0, i64 %47
store float %41, float* %48
%49 = add nuw nsw i64 %25, 1
%50 = icmp eq i64 %49, %19
br i1 %50, label %51, label %24
51: ; preds = %40, %22
%52 = add nuw nsw i32 %23, 1
%53 = icmp eq i32 %52, %2
br i1 %53, label %54, label %22
54: ; preds = %51, %20
%55 = add nuw nsw i32 %21, 1
%56 = icmp eq i32 %55, %1
br i1 %56, label %57, label %20
57: ; preds = %54, %14
%58 = call i32 @fclose(i32* %10)
ret void
}