#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
void get2DArrayFromFile(int n, int m, int a[n][m]) {
  FILE *file;
  file = fopen("hw_4_4.txt", "r");
  if (file == NULL) {
    puts("Can't read file.\n");
  } else {
    fscanf(file, "%d ", &n);
    fscanf(file, "%d ", &m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        fscanf(file, "%d ", &a[i][j]);
    fclose(file);
    printf("Array read from file.\n");
  }
}


x86 assembly (clang, -O0):
<get2DArrayFromFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %rax,-0x28(%rbp)
lea    0xe90(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119c <get2DArrayFromFile+0x4c>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
jmp    1252 <get2DArrayFromFile+0x102>
mov    -0x18(%rbp),%rdi
lea    0xe78(%rip),%rsi
lea    -0x4(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe64(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1060 <fscanf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    123d <get2DArrayFromFile+0xed>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    122a <get2DArrayFromFile+0xda>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
movslq -0x1c(%rbp),%rax
imul   %rcx,%rax
shl    $0x2,%rax
add    %rax,%rdx
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe08(%rip),%rsi
call   1060 <fscanf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11de <get2DArrayFromFile+0x8e>
jmp    122f <get2DArrayFromFile+0xdf>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cb <get2DArrayFromFile+0x7b>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdd6(%rip),%rdi
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get2DArrayFromFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,0xc(%rsp)
mov    %esi,0x8(%rsp)
lea    0xe9e(%rip),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1239 <get2DArrayFromFile+0xf9>
mov    %rax,%rbx
lea    0xe9e(%rip),%r15
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <fscanf@plt>
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <fscanf@plt>
mov    0xc(%rsp),%ecx
test   %ecx,%ecx
jle    1216 <get2DArrayFromFile+0xd6>
mov    %ebp,%edx
mov    0x8(%rsp),%eax
shl    $0x2,%rdx
mov    %rdx,0x10(%rsp)
xor    %r13d,%r13d
lea    0xe5d(%rip),%r15
jmp    11e1 <get2DArrayFromFile+0xa1>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r13
movslq %ecx,%rdx
add    0x10(%rsp),%r14
cmp    %rdx,%r13
jge    1216 <get2DArrayFromFile+0xd6>
test   %eax,%eax
jle    11d0 <get2DArrayFromFile+0x90>
mov    %r14,%rbp
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1050 <fscanf@plt>
add    $0x1,%r12
movslq 0x8(%rsp),%rax
add    $0x4,%rbp
cmp    %rax,%r12
jl     11f0 <get2DArrayFromFile+0xb0>
mov    0xc(%rsp),%ecx
jmp    11d0 <get2DArrayFromFile+0x90>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xdfe(%rip),%rdi
call   1030 <puts@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcd(%rip),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"hw_4_4.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"Can't read file.\0A\00"
@.str.3 = [4 x i8] c"%d \00"
@.str.4 = [23 x i8] c"Array read from file.\0A\00"
define void @get2DArrayFromFile(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%10 = load i32, i32* %4
%11 = zext i32 %10 to i64
%12 = load i32, i32* %5
%13 = zext i32 %12 to i64
%14 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %55
19: ; preds = %3
%20 = load i32*, i32** %7
%21 = call i32 @fscanf(i32* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%22 = load i32*, i32** %7
%23 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %5)
store i32 0, i32* %8
br label %24
24: ; preds = %48, %19
%25 = load i32, i32* %8
%26 = load i32, i32* %4
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %51
28: ; preds = %24
store i32 0, i32* %9
br label %29
29: ; preds = %44, %28
%30 = load i32, i32* %9
%31 = load i32, i32* %5
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %47
33: ; preds = %29
%34 = load i32*, i32** %7
%35 = load i32*, i32** %6
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = mul nsw i64 %37, %13
%39 = getelementptr inbounds i32, i32* %35, i64 %38
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = call i32 @fscanf(i32* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %42)
br label %44
44: ; preds = %33
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
br label %29
47: ; preds = %29
br label %48
48: ; preds = %47
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
store i32 %50, i32* %8
br label %24
51: ; preds = %24
%52 = load i32*, i32** %7
%53 = call i32 @fclose(i32* %52)
%54 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %51, %17
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"hw_4_4.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"Can't read file.\0A\00"
@.str.3 = [4 x i8] c"%d \00"
@str = [22 x i8] c"Array read from file.\00"
define void @get2DArrayFromFile(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%6 = zext i32 %1 to i64
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %45
11: ; preds = %3
%12 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%13 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %11
%17 = load i32, i32* %5
br label %18
18: ; preds = %16, %31
%19 = phi i32 [ %14, %16 ], [ %32, %31 ]
%20 = phi i32 [ %17, %16 ], [ %33, %31 ]
%21 = phi i64 [ 0, %16 ], [ %34, %31 ]
%22 = icmp sgt i32 %20, 0
br i1 %22, label %23, label %31
23: ; preds = %18
%24 = mul nuw nsw i64 %21, %6
%25 = getelementptr inbounds i32, i32* %2, i64 %24
br label %37
26: ; preds = %31, %11
%27 = call i32 @fclose(i32* %7)
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %45
29: ; preds = %37
%30 = load i32, i32* %4
br label %31
31: ; preds = %29, %18
%32 = phi i32 [ %30, %29 ], [ %19, %18 ]
%33 = phi i32 [ %42, %29 ], [ %20, %18 ]
%34 = add nuw nsw i64 %21, 1
%35 = sext i32 %32 to i64
%36 = icmp slt i64 %34, %35
br i1 %36, label %18, label %26
37: ; preds = %23, %37
%38 = phi i64 [ 0, %23 ], [ %41, %37 ]
%39 = getelementptr inbounds i32, i32* %25, i64 %38
%40 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %39)
%41 = add nuw nsw i64 %38, 1
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = icmp slt i64 %41, %43
br i1 %44, label %37, label %29
45: ; preds = %26, %9
ret void
}