#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 saveArrayToFile(int *array, int length) {
  FILE *file;
  file = fopen("hw_4_1.txt", "w");
  if (file == NULL) {
    puts("Can't write file.\n");
  } else {
    fprintf(file, "%d ", 100);
    for (int i = 0; i < length; i++)
      fprintf(file, "%d ", array[i]);
    fclose(file);
    printf("File saved to file.\n");
  }
}


x86 assembly (clang, -O0):
<saveArrayToFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1192 <saveArrayToFile+0x42>
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
jmp    11f8 <saveArrayToFile+0xa8>
mov    -0x18(%rbp),%rdi
lea    0xe83(%rip),%rsi
mov    $0x64,%edx
call   1060 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e3 <saveArrayToFile+0x93>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe50(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ae <saveArrayToFile+0x5e>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<saveArrayToFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xeac(%rip),%rdi
lea    0xeb0(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b9 <saveArrayToFile+0x79>
mov    %rax,%r12
lea    0xeb1(%rip),%rsi
mov    %rax,%rdi
mov    $0x64,%edx
call   1050 <fprintf@plt>
test   %ebp,%ebp
jle    11a8 <saveArrayToFile+0x68>
mov    %ebp,%ebp
xor    %ebx,%ebx
lea    0xe95(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbx,4),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1190 <saveArrayToFile+0x50>
mov    %r12,%rdi
call   1040 <fclose@plt>
lea    0xe6d(%rip),%rdi
jmp    11c0 <saveArrayToFile+0x80>
lea    0xe4d(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"hw_4_1.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 @saveArrayToFile(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = 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* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %34
12: ; preds = %2
%13 = load i32*, i32** %5
%14 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 100)
store i32 0, i32* %6
br label %15
15: ; preds = %27, %12
%16 = load i32, i32* %6
%17 = load i32, i32* %4
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %30
19: ; preds = %15
%20 = load i32*, i32** %5
%21 = load i32*, i32** %3
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %19
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
br label %15
30: ; preds = %15
%31 = load i32*, i32** %5
%32 = call i32 @fclose(i32* %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %30, %10
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"hw_4_1.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 @saveArrayToFile(i32* nocapture %0, i32 %1) {
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %22
7: ; preds = %2
%8 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 100)
%9 = icmp sgt i32 %1, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = zext i32 %1 to i64
br label %15
12: ; preds = %15, %7
%13 = tail call i32 @fclose(i32* %3)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %22
15: ; preds = %10, %15
%16 = phi i64 [ 0, %10 ], [ %20, %15 ]
%17 = getelementptr inbounds i32, i32* %0, i64 %16
%18 = load i32, i32* %17
%19 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = add nuw nsw i64 %16, 1
%21 = icmp eq i64 %20, %11
br i1 %21, label %12, label %15
22: ; preds = %12, %5
ret void
}