#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
char *realloc(char *, int);
char *read_line(int fd) {
  char *buffer;
  int buffer_size;
  char *p;
  int bytes_read;
  char character;
  int length = 0;
  buffer = malloc(20);
  if (buffer == NULL) {
    perror("Unable to allocate necessary memory");
    exit(EXIT_FAILURE);
  }
  buffer_size = 20;
  p = buffer;
  while (1) {
    bytes_read = read(fd, &character, 1);
    if (bytes_read == -1) {
      perror("Read error");
      exit(EXIT_FAILURE);
    }
    if (bytes_read == 0) {
      if (length > 0) {
        *p = '\0';
      } else {
        free(buffer);
        buffer = NULL;
      }
      break;
    }
    if (character == '\n') {
      *p = '\n';
      p++;
      *p = '\0';
      break;
    }
    *p = character;
    p++;
    length++;
    if (length >= buffer_size - 1) {
      buffer_size += 20;
      buffer = realloc(buffer, buffer_size);
      if (buffer == NULL) {
        perror("Unable to allocate necessary memory");
        exit(EXIT_FAILURE);
      }
      p = buffer + length;
    }
  }
  return buffer;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a5 <read_line+0x45>
lea    0xe6e(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movl   $0x14,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ec <read_line+0x8c>
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1222 <read_line+0xc2>
cmpl   $0x0,-0x2c(%rbp)
jle    120c <read_line+0xac>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    121d <read_line+0xbd>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
jmp    12cf <read_line+0x16f>
movsbl -0x25(%rbp),%eax
cmp    $0xa,%eax
jne    124e <read_line+0xee>
mov    -0x20(%rbp),%rax
movb   $0xa,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    12cf <read_line+0x16f>
mov    -0x25(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     12ca <read_line+0x16a>
mov    -0x14(%rbp),%eax
add    $0x14,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12bb <read_line+0x15b>
lea    0xd58(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    11b4 <read_line+0x54>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r13d
mov    $0x14,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1258 <read_line+0xf8>
mov    %rax,%r15
xor    %ebx,%ebx
mov    $0x14,%eax
mov    %rax,0x10(%rsp)
mov    $0x14,%eax
lea    -0x1(%rax),%r14d
lea    0xf(%rsp),%r12
mov    %r15,%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     120f <read_line+0xaf>
cmp    $0xffffffff,%eax
je     123e <read_line+0xde>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     1219 <read_line+0xb9>
mov    %al,0x0(%rbp)
add    $0x1,%rbp
add    $0x1,%rbx
cmp    %ebx,%r14d
jg     11b0 <read_line+0x50>
mov    0x10(%rsp),%r14
add    $0x14,%r14d
mov    %r15,%rdi
mov    %r14d,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     1258 <read_line+0xf8>
mov    %rax,%r15
mov    %rax,%rbp
add    %rbx,%rbp
mov    %r14,0x10(%rsp)
add    $0xffffffff,%r14d
jmp    11b0 <read_line+0x50>
test   %ebx,%ebx
je     1221 <read_line+0xc1>
movb   $0x0,0x0(%rbp)
jmp    122c <read_line+0xcc>
movw   $0xa,0x0(%rbp)
jmp    122c <read_line+0xcc>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xddf(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xda1(%rip),%rdi
jmp    1245 <read_line+0xe5>

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

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