#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 *readfString(FILE *fp, char end) {
  char *string = NULL;
  char value = '@';
  int counter = 0;
  while (value != end) {
    fscanf(fp, "%c", &value);
    if (value != 10) {
      string = (char *)realloc(string, sizeof(char) * counter + 1);
      string[counter] = value;
      counter++;
    }
  }
  string[counter - 1] = '\0';
  return string;
}


x86 assembly (clang, -O0):
<readfString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movq   $0x0,-0x18(%rbp)
movb   $0x40,-0x19(%rbp)
movl   $0x0,-0x20(%rbp)
movsbl -0x19(%rbp),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
je     11ad <readfString+0x8d>
mov    -0x8(%rbp),%rdi
lea    0xea0(%rip),%rsi
lea    -0x19(%rbp),%rdx
call   1030 <fscanf@plt>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
je     11a8 <readfString+0x88>
mov    -0x18(%rbp),%rdi
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x19(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1145 <readfString+0x25>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%r12
movb   $0x40,0xf(%rsp)
xor    %eax,%eax
mov    %rax,0x10(%rsp)
lea    0xeb9(%rip),%rbp
lea    0xf(%rsp),%rbx
xor    %r14d,%r14d
jmp    1165 <readfString+0x45>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %r15b,%al
je     11ab <readfString+0x8b>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <fscanf@plt>
mov    $0xa,%al
cmpb   $0xa,0xf(%rsp)
je     1160 <readfString+0x40>
mov    %r15d,%r13d
movslq 0x10(%rsp),%r15
lea    0x1(%r15),%esi
mov    %r14,%rdi
mov    %rsi,0x10(%rsp)
call   1040 <realloc@plt>
mov    %rax,%r14
movzbl 0xf(%rsp),%eax
mov    %al,(%r14,%r15,1)
mov    %r13d,%r15d
movzbl 0xf(%rsp),%eax
jmp    1160 <readfString+0x40>
movslq 0x10(%rsp),%rax
movb   $0x0,-0x1(%rax,%r14,1)
mov    %r14,%rax
add    $0x18,%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* @readfString(i32* %0, i8 signext %1) {
%3 = alloca i32*
%4 = alloca i8
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
store i32* %0, i32** %3
store i8 %1, i8* %4
store i8* null, i8** %5
store i8 64, i8* %6
store i32 0, i32* %7
br label %8
8: ; preds = %36, %2
%9 = load i8, i8* %6
%10 = sext i8 %9 to i32
%11 = load i8, i8* %4
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %10, %12
br i1 %13, label %14, label %37
14: ; preds = %8
%15 = load i32*, i32** %3
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %6)
%17 = load i8, i8* %6
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 10
br i1 %19, label %20, label %36
20: ; preds = %14
%21 = load i8*, i8** %5
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = mul i64 1, %23
%25 = add i64 %24, 1
%26 = trunc i64 %25 to i32
%27 = call i64 @realloc(i8* %21, i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %5
%29 = load i8, i8* %6
%30 = load i8*, i8** %5
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 %29, i8* %33
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
br label %36
36: ; preds = %20, %14
br label %8
37: ; preds = %8
%38 = load i8*, i8** %5
%39 = load i32, i32* %7
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 0, i8* %42
%43 = load i8*, i8** %5
ret i8* %43
}

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