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


x86 assembly (clang, -O0):
<ler_string_arquivo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movb   $0x40,-0x11(%rbp)
movl   $0x0,-0x18(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
je     1197 <ler_string_arquivo+0x77>
mov    -0x8(%rbp),%rdi
lea    0xea9(%rip),%rsi
lea    -0x11(%rbp),%rdx
call   1030 <fscanf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
shl    $0x3,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x11(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113f <ler_string_arquivo+0x1f>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ler_string_arquivo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
movb   $0x40,0x7(%rsp)
mov    $0xffffffffffffffff,%rbx
mov    $0x1,%ebp
xor    %r13d,%r13d
lea    0xeb7(%rip),%r15
lea    0x7(%rsp),%r12
xchg   %ax,%ax
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <fscanf@plt>
mov    %r13,%rdi
mov    %ebp,%esi
call   1040 <realloc@plt>
mov    %rax,%r13
movzbl 0x7(%rsp),%eax
mov    %al,0x1(%r13,%rbx,1)
add    $0x1,%rbx
add    $0x8,%ebp
cmpb   $0xa,0x7(%rsp)
jne    1150 <ler_string_arquivo+0x30>
mov    %ebx,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r13,%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_arquivo(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32
store i32* %0, i32** %2
store i8* null, i8** %3
store i8 64, i8* %4
store i32 0, i32* %5
br label %6
6: ; preds = %10, %1
%7 = load i8, i8* %4
%8 = sext i8 %7 to i32
%9 = icmp ne i32 %8, 10
br i1 %9, label %10, label %28
10: ; preds = %6
%11 = load i32*, i32** %2
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%13 = load i8*, i8** %3
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = mul i64 8, %15
%17 = add i64 %16, 1
%18 = trunc i64 %17 to i32
%19 = call i64 @realloc(i8* %13, i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %3
%21 = load i8, i8* %4
%22 = load i8*, i8** %3
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 %21, i8* %25
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
br label %6
28: ; preds = %6
%29 = load i8*, i8** %3
%30 = load i32, i32* %5
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %3
ret i8* %34
}

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