#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 EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int getchar();
char *malloc(int);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, int);
char *read_line(void) {
  char *line = malloc(sizeof(char) * (1024 + 1));
  int c;
  int i = 0;
  int length = 1024;
  if (line == NULL) {
    perror("Cannot allocate buffer");
    exit(EXIT_FAILURE);
  }
  while ((c = getchar()) != EOF && c != '\n') {
    if (i >= length) {
      char *tmp = realloc(line, length + 1024 + 1);
      if (tmp == NULL) {
        free(line);
        perror("Cannot reallocate buffer");
        exit(EXIT_FAILURE);
      }
      length += 1024;
    }
    line[i++] = c;
  }
  if (i == 0) {
    free(line);
    return NULL;
  }
  line[i] = '\0';
  return line;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x401,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x400,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a9 <read_line+0x49>
lea    0xe6a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11ae <read_line+0x4e>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
mov    0x2e25(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x29(%rbp)
je     11d8 <read_line+0x78>
cmpl   $0xa,-0x14(%rbp)
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11e8 <read_line+0x88>
jmp    1265 <read_line+0x105>
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     1246 <read_line+0xe6>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x400,%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123b <read_line+0xdb>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xdef(%rip),%rdi
call   1070 <perror@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,%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    11ae <read_line+0x4e>
cmpl   $0x0,-0x18(%rbp)
jne    1285 <read_line+0x125>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1299 <read_line+0x139>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x401,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1225 <read_line+0xc5>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%eax
je     11ec <read_line+0x8c>
mov    %eax,%ebp
mov    0x2e55(%rip),%r14
cmp    (%r14),%eax
je     11ec <read_line+0x8c>
mov    $0x400,%r12d
xor    %ebx,%ebx
movslq %r12d,%rax
cmp    %rax,%rbx
jl     11c4 <read_line+0x64>
lea    0x401(%r12),%esi
mov    %r15,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     1203 <read_line+0xa3>
add    $0x400,%r12d
mov    %bpl,(%r15,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%eax
je     11df <read_line+0x7f>
mov    %eax,%ebp
cmp    (%r14),%eax
jne    11a0 <read_line+0x40>
test   %ebx,%ebx
je     11ec <read_line+0x8c>
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
jmp    11f7 <read_line+0x97>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xdd4(%rip),%rdi
jmp    1212 <read_line+0xb2>

LLVM IR (-O0):
@.str = [23 x i8] c"Cannot allocate buffer\00"
@EXIT_FAILURE = global i32 0
@EOF = global i32 0
@.str.1 = [25 x i8] c"Cannot reallocate buffer\00"
define i8* @read_line() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = call i8* @malloc(i32 1025)
store i8* %7, i8** %2
store i32 0, i32* %4
store i32 1024, i32* %5
%8 = load i8*, i8** %2
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
br label %15
15: ; preds = %45, %14
%16 = call i32 (...) @getchar()
store i32 %16, i32* %3
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %3
%21 = icmp ne i32 %20, 10
br label %22
22: ; preds = %19, %15
%23 = phi i1 [ false, %15 ], [ %21, %19 ]
br i1 %23, label %24, label %53
24: ; preds = %22
%25 = load i32, i32* %4
%26 = load i32, i32* %5
%27 = icmp sge i32 %25, %26
br i1 %27, label %28, label %45
28: ; preds = %24
%29 = load i8*, i8** %2
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1024
%32 = add nsw i32 %31, 1
%33 = call i8* @realloc(i8* %29, i32 %32)
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %42
36: ; preds = %28
%37 = load i8*, i8** %2
%38 = call i32 @free(i8* %37)
%39 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %28
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1024
store i32 %44, i32* %5
br label %45
45: ; preds = %42, %24
%46 = load i32, i32* %3
%47 = trunc i32 %46 to i8
%48 = load i8*, i8** %2
%49 = load i32, i32* %4
%50 = add nsw i32 %49, 1
store i32 %50, i32* %4
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
store i8 %47, i8* %52
br label %15
53: ; preds = %22
%54 = load i32, i32* %4
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i8*, i8** %2
%58 = call i32 @free(i8* %57)
store i8* null, i8** %1
br label %65
59: ; preds = %53
%60 = load i8*, i8** %2
%61 = load i32, i32* %4
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
%64 = load i8*, i8** %2
store i8* %64, i8** %1
br label %65
65: ; preds = %59, %56
%66 = load i8*, i8** %1
ret i8* %66
}

LLVM IR (-O2):
@.str = [23 x i8] c"Cannot allocate buffer\00"
@EXIT_FAILURE = global i32 0
@EOF = global i32 0
@.str.1 = [25 x i8] c"Cannot reallocate buffer\00"
define i8* @read_line() {
%1 = tail call dereferenceable_or_null(1025) i8* @malloc(i32 1025)
%2 = icmp eq i8* %1, null
br i1 %2, label %9, label %3
3: ; preds = %0
%4 = tail call i32 (...) @getchar()
%5 = load i32, i32* @EOF
%6 = icmp ne i32 %4, %5
%7 = icmp ne i32 %4, 10
%8 = and i1 %7, %6
br i1 %8, label %13, label %43
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %3, %30
%14 = phi i64 [ %33, %30 ], [ 0, %3 ]
%15 = phi i32 [ %35, %30 ], [ %4, %3 ]
%16 = phi i32 [ %31, %30 ], [ 1024, %3 ]
%17 = sext i32 %16 to i64
%18 = icmp slt i64 %14, %17
br i1 %18, label %30, label %19
19: ; preds = %13
%20 = add nsw i32 %16, 1025
%21 = tail call i8* @realloc(i8* %1, i32 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = tail call i32 @free(i8* %1)
%25 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
%29 = add nsw i32 %16, 1024
br label %30
30: ; preds = %28, %13
%31 = phi i32 [ %29, %28 ], [ %16, %13 ]
%32 = trunc i32 %15 to i8
%33 = add nuw i64 %14, 1
%34 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 %32, i8* %34
%35 = tail call i32 (...) @getchar()
%36 = load i32, i32* @EOF
%37 = icmp ne i32 %35, %36
%38 = icmp ne i32 %35, 10
%39 = and i1 %38, %37
br i1 %39, label %13, label %40
40: ; preds = %30
%41 = trunc i64 %33 to i32
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %3, %40
%44 = tail call i32 @free(i8* %1)
br label %48
45: ; preds = %40
%46 = and i64 %33, 4294967295
%47 = getelementptr inbounds i8, i8* %1, i64 %46
store i8 0, i8* %47
br label %48
48: ; preds = %45, %43
%49 = phi i8* [ null, %43 ], [ %1, %45 ]
ret i8* %49
}