#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*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
void save2DArrayToFile(int n, int m, int a[n][m]) {
  FILE *file;
  file = fopen("hw_4_4.txt", "w");
  if (file == NULL) {
    puts("Can't write file.\n");
  } else {
    fprintf(file, "%d ", n);
    fprintf(file, "%d ", m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        fprintf(file, "%d ", a[i][j]);
    fclose(file);
    printf("File saved to file.\n");
  }
}


x86 assembly (clang, -O0):
<save2DArrayToFile>:
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 <save2DArrayToFile+0x4c>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
jmp    124c <save2DArrayToFile+0xfc>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe76(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe63(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1237 <save2DArrayToFile+0xe7>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1224 <save2DArrayToFile+0xd4>
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
imul   %rdx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe0f(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11dc <save2DArrayToFile+0x8c>
jmp    1229 <save2DArrayToFile+0xd9>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c9 <save2DArrayToFile+0x79>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xddd(%rip),%rdi
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save2DArrayToFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%r15d
lea    0xea2(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     121b <save2DArrayToFile+0xdb>
mov    %rax,%r13
lea    0xea3(%rip),%r12
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1050 <fprintf@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1050 <fprintf@plt>
test   %r15d,%r15d
jle    120a <save2DArrayToFile+0xca>
mov    %r14d,%ebp
mov    %r15d,%eax
mov    %rax,0x10(%rsp)
lea    0x0(,%rbp,4),%rax
mov    %rax,0x8(%rsp)
xor    %r12d,%r12d
lea    0xe60(%rip),%r15
mov    %r14d,0x4(%rsp)
jmp    11e5 <save2DArrayToFile+0xa5>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r12
add    0x8(%rsp),%rbx
cmp    0x10(%rsp),%r12
mov    0x4(%rsp),%r14d
je     120a <save2DArrayToFile+0xca>
test   %r14d,%r14d
jle    11d0 <save2DArrayToFile+0x90>
xor    %r14d,%r14d
nopl   (%rax)
mov    (%rbx,%r14,4),%edx
mov    %r13,%rdi
mov    %r15,%rsi
call   1050 <fprintf@plt>
add    $0x1,%r14
cmp    %r14,%rbp
jne    11f0 <save2DArrayToFile+0xb0>
jmp    11d0 <save2DArrayToFile+0x90>
mov    %r13,%rdi
call   1040 <fclose@plt>
lea    0xe0b(%rip),%rdi
jmp    1222 <save2DArrayToFile+0xe2>
lea    0xdeb(%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"w\00"
@.str.2 = [19 x i8] c"Can't write file.\0A\00"
@.str.3 = [4 x i8] c"%d \00"
@.str.4 = [21 x i8] c"File saved to file.\0A\00"
define void @save2DArrayToFile(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 ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %58
19: ; preds = %3
%20 = load i32*, i32** %7
%21 = load i32, i32* %4
%22 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = load i32*, i32** %7
%24 = load i32, i32* %5
%25 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %24)
store i32 0, i32* %8
br label %26
26: ; preds = %51, %19
%27 = load i32, i32* %8
%28 = load i32, i32* %4
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %54
30: ; preds = %26
store i32 0, i32* %9
br label %31
31: ; preds = %47, %30
%32 = load i32, i32* %9
%33 = load i32, i32* %5
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %50
35: ; preds = %31
%36 = load i32*, i32** %7
%37 = load i32*, i32** %6
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = mul nsw i64 %39, %13
%41 = getelementptr inbounds i32, i32* %37, i64 %40
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32, i32* %44
%46 = call i32 @fprintf(i32* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %35
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %31
50: ; preds = %31
br label %51
51: ; preds = %50
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %26
54: ; preds = %26
%55 = load i32*, i32** %7
%56 = call i32 @fclose(i32* %55)
%57 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %54, %17
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"hw_4_4.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"Can't write file.\0A\00"
@.str.3 = [4 x i8] c"%d \00"
@str = [20 x i8] c"File saved to file.\00"
define void @save2DArrayToFile(i32 %0, i32 %1, i32* nocapture %2) {
%4 = zext i32 %1 to i64
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %34
9: ; preds = %3
%10 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%11 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%12 = icmp sgt i32 %0, 0
br i1 %12, label %13, label %21
13: ; preds = %9
%14 = icmp sgt i32 %1, 0
%15 = zext i32 %0 to i64
br label %16
16: ; preds = %13, %24
%17 = phi i64 [ 0, %13 ], [ %25, %24 ]
br i1 %14, label %18, label %24
18: ; preds = %16
%19 = mul nuw nsw i64 %17, %4
%20 = getelementptr inbounds i32, i32* %2, i64 %19
br label %27
21: ; preds = %24, %9
%22 = tail call i32 @fclose(i32* %5)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %34
24: ; preds = %27, %16
%25 = add nuw nsw i64 %17, 1
%26 = icmp eq i64 %25, %15
br i1 %26, label %21, label %16
27: ; preds = %18, %27
%28 = phi i64 [ 0, %18 ], [ %32, %27 ]
%29 = getelementptr inbounds i32, i32* %20, i64 %28
%30 = load i32, i32* %29
%31 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = add nuw nsw i64 %28, 1
%33 = icmp eq i64 %32, %4
br i1 %33, label %24, label %27
34: ; preds = %21, %7
ret void
}