#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 fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
int readVectorFromFile(int **v, int *n, char *file, char *err) {
  FILE *inputFile = fopen(file, "rt");
  int countReadedElements = 0, i = 0;
  if (inputFile == NULL) {
    strcpy(err, "Err. Could not open file.\n");
    return 0;
  }
  if (fscanf(inputFile, "%d", n) != 1) {
    fclose(inputFile);
    strcpy(err, "Err. Could not read number of elements.\n");
    return 0;
  }
  *v = (int *)malloc(sizeof(int) * (*n));
  for (i = 0; i < *n; i++) {
    if (fscanf(inputFile, "%d", &(*v)[i]) != 1) {
      break;
    }
    countReadedElements++;
  }
  fclose(inputFile);
  if (countReadedElements != *n) {
    strcpy(err, "Err. Number of declared elements does not match with the one "
                "found in file.\n");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<readVectorFromFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b1 <readVectorFromFile+0x61>
mov    -0x28(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a7 <readVectorFromFile+0x157>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe5e(%rip),%rsi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     11f3 <readVectorFromFile+0xa3>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a7 <readVectorFromFile+0x157>
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jge    126c <readVectorFromFile+0x11c>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
movslq -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xddc(%rip),%rsi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     1255 <readVectorFromFile+0x105>
jmp    126c <readVectorFromFile+0x11c>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1216 <readVectorFromFile+0xc6>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     12a0 <readVectorFromFile+0x150>
mov    -0x28(%rbp),%rdi
lea    0xdbb(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a7 <readVectorFromFile+0x157>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readVectorFromFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe95(%rip),%rsi
mov    %rdx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1229 <readVectorFromFile+0xd9>
mov    %rax,%rbx
lea    0xe98(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jne    1232 <readVectorFromFile+0xe2>
mov    (%r15),%edi
shl    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,(%r12)
xor    %ebp,%ebp
cmpl   $0x0,(%r15)
jle    120e <readVectorFromFile+0xbe>
lea    0xe66(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jne    120e <readVectorFromFile+0xbe>
mov    %r14,(%rsp)
mov    $0x1,%ecx
mov    $0x4,%r14d
lea    0xe40(%rip),%r13
xchg   %ax,%ax
mov    %rcx,%rbp
movslq (%r15),%rax
cmp    %rax,%rcx
jge    120a <readVectorFromFile+0xba>
mov    (%r12),%rdx
add    %r14,%rdx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1050 <fscanf@plt>
lea    0x1(%rbp),%rcx
add    $0x4,%r14
cmp    $0x1,%eax
je     11e0 <readVectorFromFile+0x90>
mov    (%rsp),%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
cmp    (%r15),%ebp
je     124b <readVectorFromFile+0xfb>
lea    0xe23(%rip),%rsi
jmp    1241 <readVectorFromFile+0xf1>
lea    0xdd3(%rip),%rsi
jmp    1241 <readVectorFromFile+0xf1>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xde0(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rt\00"
@.str.1 = [27 x i8] c"Err. Could not open file.\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [41 x i8] c"Err. Could not read number of elements.\0A\00"
@.str.4 = [77 x i8] c"Err. Number of declared elements does not match with the one found in file.\0A\00"
define i32 @readVectorFromFile(i32** %0, i32* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
store i32** %0, i32*** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = load i8*, i8** %8
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
store i32 0, i32* %11
store i32 0, i32* %12
%15 = load i32*, i32** %10
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i8*, i8** %9
%19 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %71
20: ; preds = %4
%21 = load i32*, i32** %10
%22 = load i32*, i32** %7
%23 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %22)
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32*, i32** %10
%27 = call i32 @fclose(i32* %26)
%28 = load i8*, i8** %9
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %5
br label %71
30: ; preds = %20
%31 = load i32*, i32** %7
%32 = load i32, i32* %31
%33 = sext i32 %32 to i64
%34 = mul i64 4, %33
%35 = trunc i64 %34 to i32
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i32*
%38 = load i32**, i32*** %6
store i32* %37, i32** %38
store i32 0, i32* %12
br label %39
39: ; preds = %57, %30
%40 = load i32, i32* %12
%41 = load i32*, i32** %7
%42 = load i32, i32* %41
%43 = icmp slt i32 %40, %42
br i1 %43, label %44, label %60
44: ; preds = %39
%45 = load i32*, i32** %10
%46 = load i32**, i32*** %6
%47 = load i32*, i32** %46
%48 = load i32, i32* %12
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %47, i64 %49
%51 = call i32 @fscanf(i32* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %50)
%52 = icmp ne i32 %51, 1
br i1 %52, label %53, label %54
53: ; preds = %44
br label %60
54: ; preds = %44
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
br label %57
57: ; preds = %54
%58 = load i32, i32* %12
%59 = add nsw i32 %58, 1
store i32 %59, i32* %12
br label %39
60: ; preds = %53, %39
%61 = load i32*, i32** %10
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %11
%64 = load i32*, i32** %7
%65 = load i32, i32* %64
%66 = icmp ne i32 %63, %65
br i1 %66, label %67, label %70
67: ; preds = %60
%68 = load i8*, i8** %9
%69 = call i32 @strcpy(i8* %68, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %5
br label %71
70: ; preds = %60
store i32 1, i32* %5
br label %71
71: ; preds = %70, %67, %25, %17
%72 = load i32, i32* %5
ret i32 %72
}

LLVM IR (-O2):
@.str = [3 x i8] c"rt\00"
@.str.1 = [27 x i8] c"Err. Could not open file.\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [41 x i8] c"Err. Could not read number of elements.\0A\00"
@.str.4 = [77 x i8] c"Err. Number of declared elements does not match with the one found in file.\0A\00"
define i32 @readVectorFromFile(i32** nocapture %0, i32* %1, i8* nocapture %2, i8* %3) {
%5 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %40, label %7
7: ; preds = %4
%8 = tail call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @fclose(i32* %5)
br label %40
12: ; preds = %7
%13 = load i32, i32* %1
%14 = shl i32 %13, 2
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i32*
store i32* %16, i32** %0
%17 = load i32, i32* %1
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %12
%20 = tail call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %16)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %35
22: ; preds = %19, %28
%23 = phi i64 [ %24, %28 ], [ 0, %19 ]
%24 = add nuw nsw i64 %23, 1
%25 = load i32, i32* %1
%26 = sext i32 %25 to i64
%27 = icmp slt i64 %24, %26
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i32*, i32** %0
%30 = getelementptr inbounds i32, i32* %29, i64 %24
%31 = tail call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %30)
%32 = icmp eq i32 %31, 1
br i1 %32, label %22, label %33
33: ; preds = %28, %22
%34 = trunc i64 %24 to i32
br label %35
35: ; preds = %19, %33, %12
%36 = phi i32 [ 0, %12 ], [ 0, %19 ], [ %34, %33 ]
%37 = tail call i32 @fclose(i32* %5)
%38 = load i32, i32* %1
%39 = icmp eq i32 %36, %38
br i1 %39, label %43, label %40
40: ; preds = %35, %4, %10
%41 = phi i8* [ getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), %10 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([77 x i8], [77 x i8]* @.str.4, i64 0, i64 0), %35 ]
%42 = tail call i32 @strcpy(i8* %3, i8* %41)
br label %43
43: ; preds = %40, %35
%44 = phi i32 [ 1, %35 ], [ 0, %40 ]
ret i32 %44
}