#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*/ feof(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));
  while (!feof(inputFile)) {
    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    0xe7d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c1 <readVectorFromFile+0x61>
mov    -0x28(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bf <readVectorFromFile+0x15f>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe4e(%rip),%rsi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     1203 <readVectorFromFile+0xa3>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bf <readVectorFromFile+0x15f>
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    123d <readVectorFromFile+0xdd>
jmp    1284 <readVectorFromFile+0x124>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x38(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x38(%rbp)
cltq
shl    $0x2,%rax
add    %rax,%rdx
lea    0xdbb(%rip),%rsi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     1276 <readVectorFromFile+0x116>
jmp    1284 <readVectorFromFile+0x124>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    121f <readVectorFromFile+0xbf>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     12b8 <readVectorFromFile+0x158>
mov    -0x28(%rbp),%rdi
lea    0xda3(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bf <readVectorFromFile+0x15f>
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    0xe85(%rip),%rsi
mov    %rdx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     122a <readVectorFromFile+0xca>
mov    %rax,%rbx
lea    0xe88(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jne    1233 <readVectorFromFile+0xd3>
mov    (%r15),%edi
shl    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,(%r12)
mov    %rbx,%rdi
call   1060 <feof@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    120f <readVectorFromFile+0xaf>
mov    %r14,(%rsp)
xor    %r14d,%r14d
lea    0xe49(%rip),%r13
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rdx
add    %r14,%rdx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jne    120b <readVectorFromFile+0xab>
add    $0x1,%rbp
mov    %rbx,%rdi
call   1060 <feof@plt>
add    $0x4,%r14
test   %eax,%eax
je     11e0 <readVectorFromFile+0x80>
mov    (%rsp),%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
cmp    (%r15),%ebp
je     124c <readVectorFromFile+0xec>
lea    0xe22(%rip),%rsi
jmp    1242 <readVectorFromFile+0xe2>
lea    0xdd2(%rip),%rsi
jmp    1242 <readVectorFromFile+0xe2>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xddf(%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 %69
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 %69
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
br label %39
39: ; preds = %55, %30
%40 = load i32*, i32** %10
%41 = call i32 @feof(i32* %40)
%42 = icmp ne i32 %41, 0
%43 = xor i1 %42, true
br i1 %43, label %44, label %58
44: ; preds = %39
%45 = load i32*, i32** %10
%46 = load i32**, i32*** %6
%47 = load i32*, i32** %46
%48 = load i32, i32* %12
%49 = add nsw i32 %48, 1
store i32 %49, i32* %12
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds i32, i32* %47, i64 %50
%52 = call i32 @fscanf(i32* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %51)
%53 = icmp ne i32 %52, 1
br i1 %53, label %54, label %55
54: ; preds = %44
br label %58
55: ; preds = %44
%56 = load i32, i32* %11
%57 = add nsw i32 %56, 1
store i32 %57, i32* %11
br label %39
58: ; preds = %54, %39
%59 = load i32*, i32** %10
%60 = call i32 @fclose(i32* %59)
%61 = load i32, i32* %11
%62 = load i32*, i32** %7
%63 = load i32, i32* %62
%64 = icmp ne i32 %61, %63
br i1 %64, label %65, label %68
65: ; preds = %58
%66 = load i8*, i8** %9
%67 = call i32 @strcpy(i8* %66, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %5
br label %69
68: ; preds = %58
store i32 1, i32* %5
br label %69
69: ; preds = %68, %65, %25, %17
%70 = load i32, i32* %5
ret i32 %70
}

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 %37, 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 %37
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 = tail call i32 @feof(i32* %5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %12, %25
%20 = phi i64 [ %26, %25 ], [ 0, %12 ]
%21 = load i32*, i32** %0
%22 = getelementptr inbounds i32, i32* %21, i64 %20
%23 = tail call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %22)
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = add nuw i64 %20, 1
%27 = tail call i32 @feof(i32* %5)
%28 = icmp eq i32 %27, 0
br i1 %28, label %19, label %29
29: ; preds = %19, %25
%30 = phi i64 [ %26, %25 ], [ %20, %19 ]
%31 = trunc i64 %30 to i32
br label %32
32: ; preds = %29, %12
%33 = phi i32 [ 0, %12 ], [ %31, %29 ]
%34 = tail call i32 @fclose(i32* %5)
%35 = load i32, i32* %1
%36 = icmp eq i32 %33, %35
br i1 %36, label %40, label %37
37: ; preds = %32, %4, %10
%38 = 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), %32 ]
%39 = tail call i32 @strcpy(i8* %3, i8* %38)
br label %40
40: ; preds = %37, %32
%41 = phi i32 [ 1, %32 ], [ 0, %37 ]
ret i32 %41
}