#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 */
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ scanf(char *, char *);
char *ler_string() {
  char *string = NULL;
  char valor = '@';
  int contador = 0;
  while (valor != 10) {
    scanf("%c", &valor);
    string = (char *)realloc(string, sizeof(char) * contador + 1);
    string[contador] = valor;
    contador++;
  }
  string[contador - 1] = '\0';
  return string;
}


x86 assembly (clang, -O0):
<ler_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movq   $0x0,-0x8(%rbp)
movb   $0x40,-0x9(%rbp)
movl   $0x0,-0x10(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
je     118f <ler_string+0x6f>
lea    0xeb1(%rip),%rdi
lea    -0x9(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%rdi
movslq -0x10(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1030 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    113b <ler_string+0x1b>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ler_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movb   $0x40,0x7(%rsp)
movabs $0xffffffff00000000,%r13
ff ff ff
xor    %ebp,%ebp
lea    0x7(%rsp),%r15
movabs $0x100000000,%r14
xor    %ebx,%ebx
nopl   (%rax)
add    %r14,%r13
lea    0xea6(%rip),%rdi
mov    %r15,%rsi
call   1040 <scanf@plt>
lea    0x1(%rbp),%r12
mov    %rbx,%rdi
mov    %r12d,%esi
call   1030 <realloc@plt>
mov    %rax,%rbx
movzbl 0x7(%rsp),%eax
mov    %al,(%rbx,%rbp,1)
cmpb   $0xa,0x7(%rsp)
mov    %r12,%rbp
jne    1150 <ler_string+0x30>
sar    $0x20,%r13
movb   $0x0,(%rbx,%r13,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%c\00"
define i8* @ler_string() {
%1 = alloca i8
store i8 64, i8* %1
br label %2
2: ; preds = %0, %2
%3 = phi i64 [ 0, %0 ], [ %6, %2 ]
%4 = phi i8* [ null, %0 ], [ %9, %2 ]
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
%6 = add i64 %3, 1
%7 = trunc i64 %6 to i32
%8 = call i64 @realloc(i8* %4, i32 %7)
%9 = inttoptr i64 %8 to i8*
%10 = load i8, i8* %1
%11 = getelementptr inbounds i8, i8* %9, i64 %3
store i8 %10, i8* %11
%12 = load i8, i8* %1
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %2
14: ; preds = %2
%15 = inttoptr i64 %8 to i8*
%16 = shl i64 %3, 32
%17 = ashr exact i64 %16, 32
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 0, i8* %18
ret i8* %15
}