#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*/ exit(int);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ realloc(char *, int);
void read_string(FILE *input, char *buffer, int buffer_length) {
  int length = 0;
  char termination = '\n';
  char c = '\0';
  while (!feof(input)) {
    if (length > buffer_length) {
      buffer_length *= 2;
      buffer = (char *)realloc(buffer, buffer_length);
      if (buffer <= (char *)0) {
        perror("realloc failed:");
        exit(-1);
      }
    }
    c = (getc(input));
    if (c == termination)
      break;
    buffer[length++] = c;
  }
  buffer[length] = '\0';
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movb   $0xa,-0x19(%rbp)
movb   $0x0,-0x1a(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1190 <read_string+0x40>
jmp    1219 <read_string+0xc9>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11db <read_string+0x8b>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
ja     11d6 <read_string+0x86>
lea    0xe39(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
jmp    11db <read_string+0x8b>
mov    -0x8(%rbp),%rdi
call   1070 <getc@plt>
mov    %al,-0x1a(%rbp)
movsbl -0x1a(%rbp),%eax
movsbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
jne    11fc <read_string+0xac>
jmp    1219 <read_string+0xc9>
mov    -0x1a(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1172 <read_string+0x22>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <feof@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    11ac <read_string+0x5c>
nopw   0x0(%rax,%rax,1)
movslq %ebp,%rax
cmp    %rax,%rbx
jle    118c <read_string+0x3c>
add    %ebp,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11be <read_string+0x6e>
mov    %r14,%rdi
call   1070 <getc@plt>
cmp    $0xa,%al
je     11ac <read_string+0x5c>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <feof@plt>
test   %eax,%eax
je     1170 <read_string+0x20>
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3b(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"realloc failed:\00"
define void @read_string(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i8 10, i8* %8
store i8 0, i8* %9
br label %10
10: ; preds = %41, %3
%11 = load i32*, i32** %4
%12 = call i32 @feof(i32* %11)
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %48
15: ; preds = %10
%16 = load i32, i32* %7
%17 = load i32, i32* %6
%18 = icmp sgt i32 %16, %17
br i1 %18, label %19, label %32
19: ; preds = %15
%20 = load i32, i32* %6
%21 = mul nsw i32 %20, 2
store i32 %21, i32* %6
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = call i64 @realloc(i8* %22, i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = icmp ule i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %19
br label %32
32: ; preds = %31, %15
%33 = load i32*, i32** %4
%34 = call signext i8 @getc(i32* %33)
store i8 %34, i8* %9
%35 = load i8, i8* %9
%36 = sext i8 %35 to i32
%37 = load i8, i8* %8
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %36, %38
br i1 %39, label %40, label %41
40: ; preds = %32
br label %48
41: ; preds = %32
%42 = load i8, i8* %9
%43 = load i8*, i8** %5
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
store i8 %42, i8* %47
br label %10
48: ; preds = %40, %10
%49 = load i8*, i8** %5
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"realloc failed:\00"
define void @read_string(i32* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32 @feof(i32* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %30
6: ; preds = %3, %25
%7 = phi i64 [ %26, %25 ], [ 0, %3 ]
%8 = phi i8* [ %22, %25 ], [ %1, %3 ]
%9 = phi i32 [ %21, %25 ], [ %2, %3 ]
%10 = sext i32 %9 to i64
%11 = icmp sgt i64 %7, %10
br i1 %11, label %12, label %20
12: ; preds = %6
%13 = shl nsw i32 %9, 1
%14 = tail call i64 @realloc(i8* %8, i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %12, %6
%21 = phi i32 [ %13, %12 ], [ %9, %6 ]
%22 = phi i8* [ %15, %12 ], [ %8, %6 ]
%23 = tail call signext i8 @getc(i32* %0)
%24 = icmp eq i8 %23, 10
br i1 %24, label %30, label %25
25: ; preds = %20
%26 = add nuw i64 %7, 1
%27 = getelementptr inbounds i8, i8* %22, i64 %7
store i8 %23, i8* %27
%28 = tail call i32 @feof(i32* %0)
%29 = icmp eq i32 %28, 0
br i1 %29, label %6, label %30
30: ; preds = %25, %20, %3
%31 = phi i64 [ 0, %3 ], [ %7, %20 ], [ %26, %25 ]
%32 = phi i8* [ %1, %3 ], [ %22, %20 ], [ %22, %25 ]
%33 = and i64 %31, 4294967295
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 0, i8* %34
ret void
}