#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 */

/* Variables and functions */
int *malloc(int);
int *realloc(int *, int);
int /*<<< orphan*/ scanf(char *, int *);
int *ler_referencia(void) {
  int *referencias = malloc(sizeof(int));
  int valor;
  int indice;
  indice = 0;
  valor = 1;
  while (valor > 0) {
    scanf("%d", &valor);
    referencias = realloc(referencias, sizeof(int) * (indice + 1));
    referencias[indice] = valor;
    indice = indice + 1;
  }
  return referencias;
}


x86 assembly (clang, -O0):
<ler_referencia>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x4,%edi
call   1030 <malloc@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x1,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11a5 <ler_referencia+0x75>
lea    0xe9b(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1154 <ler_referencia+0x24>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ler_referencia>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0x4,%edi
call   1030 <malloc@plt>
mov    %rax,%r13
movl   $0x1,0xc(%rsp)
xor    %ebx,%ebx
lea    0xea5(%rip),%r14
lea    0xc(%rsp),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <scanf@plt>
lea    0x4(%rbx),%r12
mov    %r13,%rdi
mov    %r12d,%esi
call   1040 <realloc@plt>
mov    %rax,%r13
mov    0xc(%rsp),%eax
mov    %eax,0x0(%r13,%rbx,1)
cmpl   $0x0,0xc(%rsp)
mov    %r12,%rbx
jg     1160 <ler_referencia+0x30>
mov    %r13,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define i32* @ler_referencia() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = call i32* @malloc(i32 4)
store i32* %4, i32** %1
store i32 0, i32* %3
store i32 1, i32* %2
br label %5
5: ; preds = %8, %0
%6 = load i32, i32* %2
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %24
8: ; preds = %5
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%10 = load i32*, i32** %1
%11 = load i32, i32* %3
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 4, %13
%15 = trunc i64 %14 to i32
%16 = call i32* @realloc(i32* %10, i32 %15)
store i32* %16, i32** %1
%17 = load i32, i32* %2
%18 = load i32*, i32** %1
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %18, i64 %20
store i32 %17, i32* %21
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %5
24: ; preds = %5
%25 = load i32*, i32** %1
ret i32* %25
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i32* @ler_referencia() {
%1 = alloca i32
%2 = tail call i32* @malloc(i32 4)
%3 = bitcast i32* %1 to i8*
store i32 1, i32* %1
br label %4
4: ; preds = %0, %4
%5 = phi i64 [ 0, %0 ], [ %8, %4 ]
%6 = phi i32* [ %2, %0 ], [ %11, %4 ]
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%8 = add nuw i64 %5, 1
%9 = trunc i64 %8 to i32
%10 = shl i32 %9, 2
%11 = call i32* @realloc(i32* %6, i32 %10)
%12 = load i32, i32* %1
%13 = getelementptr inbounds i32, i32* %11, i64 %5
store i32 %12, i32* %13
%14 = load i32, i32* %1
%15 = icmp sgt i32 %14, 0
br i1 %15, label %4, label %16
16: ; preds = %4
ret i32* %11
}