#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 getArrayFromFile(int *array) {
  FILE *file;
  int length;
  file = fopen("hw_4_1.txt", "r");
  if (file == NULL) {
    puts("Can't read file.\n");
  } else {
    fscanf(file, "%d ", &length);
    for (int i = 0; i < length; i++)
      fscanf(file, "%d ", &array[i]);
    fclose(file);
    printf("Array read from file.\n");
  }
}


x86 assembly (clang, -O0):
<getArrayFromFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118f <getArrayFromFile+0x3f>
lea    0xe88(%rip),%rdi
call   1030 <puts@plt>
jmp    11f8 <getArrayFromFile+0xa8>
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <fscanf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e3 <getArrayFromFile+0x93>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe4f(%rip),%rsi
call   1060 <fscanf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11aa <getArrayFromFile+0x5a>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe30(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getArrayFromFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
lea    0xeae(%rip),%rdi
lea    0xeb2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d0 <getArrayFromFile+0x90>
mov    %rax,%r14
lea    0xeb2(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
cmpl   $0x0,0x4(%rsp)
jle    11b0 <getArrayFromFile+0x70>
xor    %ebx,%ebx
lea    0xe95(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <fscanf@plt>
add    $0x1,%rbx
movslq 0x4(%rsp),%rax
add    $0x4,%r12
cmp    %rax,%rbx
jl     1190 <getArrayFromFile+0x50>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe36(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <puts@plt>

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

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