#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 */
double *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *, char *);
double *read_vector(char *filename, int tam) {
  FILE *file;
  double *v;
  int i = 0;
  file = fopen(filename, "r");
  if (file == NULL) {
    printf("Error. Cannot open file %s", filename);
    return NULL;
  }
  fscanf(file, "%d", &tam);
  v = calloc(tam, sizeof(double *));
  if (v == NULL) {
    printf("Out of memory for %s\n", filename);
    return NULL;
  }
  for (i = 0; i < tam; i = i + 1) {
    fscanf(file, "%lf", &v[i]);
  }
  fclose(file);
  return v;
}


x86 assembly (clang, -O0):
<read_vector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a2 <read_vector+0x52>
mov    -0x10(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1244 <read_vector+0xf4>
mov    -0x20(%rbp),%rdi
lea    0xe70(%rip),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x14(%rbp),%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f1 <read_vector+0xa1>
mov    -0x10(%rbp),%rsi
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1244 <read_vector+0xf4>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1233 <read_vector+0xe3>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
movslq -0x2c(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11f8 <read_vector+0xa8>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_vector>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
mov    %esi,0xc(%rsp)
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ec <read_vector+0x9c>
mov    %rax,%r12
lea    0xe9e(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     11f5 <read_vector+0xa5>
mov    %rax,%r14
test   %ebx,%ebx
jle    11e2 <read_vector+0x92>
lea    0xe85(%rip),%r15
mov    %r14,%r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <fscanf@plt>
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
add    $0x8,%r13
cmp    %rax,%rbx
jl     11c0 <read_vector+0x70>
mov    %r12,%rdi
call   1030 <fclose@plt>
jmp    1207 <read_vector+0xb7>
lea    0xe0f(%rip),%rdi
jmp    11fc <read_vector+0xac>
lea    0xe24(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"Error. Cannot open file %s\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [22 x i8] c"Out of memory for %s\0A\00"
@.str.4 = [4 x i8] c"%lf\00"
define double* @read_vector(i8* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca double*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store double* null, double** %3
br label %45
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = call i32 (i32*, i8*, ...) @fscanf(i32* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%19 = load i32, i32* %5
%20 = call double* @calloc(i32 %19, i32 8)
store double* %20, double** %7
%21 = load double*, double** %7
%22 = icmp eq double* %21, null
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i8*, i8** %4
%25 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %24)
store double* null, double** %3
br label %45
26: ; preds = %16
store i32 0, i32* %8
br label %27
27: ; preds = %38, %26
%28 = load i32, i32* %8
%29 = load i32, i32* %5
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %41
31: ; preds = %27
%32 = load i32*, i32** %6
%33 = load double*, double** %7
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double, double* %33, i64 %35
%37 = call i32 (i32*, i8*, ...) @fscanf(i32* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), double* %36)
br label %38
38: ; preds = %31
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
br label %27
41: ; preds = %27
%42 = load i32*, i32** %6
%43 = call i32 @fclose(i32* %42)
%44 = load double*, double** %7
store double* %44, double** %3
br label %45
45: ; preds = %41, %23, %13
%46 = load double*, double** %3
ret double* %46
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"Error. Cannot open file %s\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [22 x i8] c"Out of memory for %s\0A\00"
@.str.4 = [4 x i8] c"%lf\00"
define double* @read_vector(i8* %0, i32 %1) {
%3 = alloca i32
store i32 %1, i32* %3
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %27
8: ; preds = %2
%9 = call i32 (i32*, i8*, ...) @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%10 = load i32, i32* %3
%11 = call double* @calloc(i32 %10, i32 8)
%12 = icmp eq double* %11, null
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = icmp sgt i32 %10, 0
br i1 %14, label %17, label %25
15: ; preds = %8
%16 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %27
17: ; preds = %13, %17
%18 = phi i64 [ %21, %17 ], [ 0, %13 ]
%19 = getelementptr inbounds double, double* %11, i64 %18
%20 = call i32 (i32*, i8*, ...) @fscanf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), double* %19)
%21 = add nuw nsw i64 %18, 1
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %21, %23
br i1 %24, label %17, label %25
25: ; preds = %17, %13
%26 = call i32 @fclose(i32* %4)
br label %27
27: ; preds = %25, %15, %6
%28 = phi double* [ null, %6 ], [ null, %15 ], [ %11, %25 ]
ret double* %28
}