#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 = '\0';
      break;
    }
    *p = character;
    p++;
    length++;
    if (length >= buffer_size) {
      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    12b7 <read_line+0x157>
movsbl -0x25(%rbp),%eax
cmp    $0xa,%eax
jne    123b <read_line+0xdb>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    12b7 <read_line+0x157>
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
cmp    -0x14(%rbp),%eax
jl     12b2 <read_line+0x152>
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    12a3 <read_line+0x143>
lea    0xd70(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d44(%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
push   %rax
mov    %edi,%r12d
mov    $0x14,%r13d
mov    $0x14,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1230 <read_line+0xd0>
mov    %rax,%r14
xor    %ebp,%ebp
lea    0x7(%rsp),%r15
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11f0 <read_line+0x90>
cmp    $0xffffffff,%eax
je     1216 <read_line+0xb6>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     11f4 <read_line+0x94>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%rbp
cmp    %ebp,%r13d
jg     11a0 <read_line+0x40>
add    $0x14,%r13d
mov    %r14,%rdi
mov    %r13d,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     1230 <read_line+0xd0>
mov    %rax,%r14
mov    %rax,%rbx
add    %rbp,%rbx
jmp    11a0 <read_line+0x40>
test   %ebp,%ebp
je     1209 <read_line+0xa9>
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    11f7 <read_line+0x97>
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xdc9(%rip),%rdi
jmp    121d <read_line+0xbd>

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, %72
%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 %73
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 %45
43: ; preds = %39
%44 = load i8*, i8** %5
store i8 0, i8* %44
br label %73
45: ; preds = %39
%46 = load i8, i8* %7
%47 = load i8*, i8** %5
store i8 %46, i8* %47
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %5
%50 = load i32, i32* %8
%51 = add nsw i32 %50, 1
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = load i32, i32* %4
%54 = icmp sge i32 %52, %53
br i1 %54, label %55, label %72
55: ; preds = %45
%56 = load i32, i32* %4
%57 = add nsw i32 %56, 20
store i32 %57, i32* %4
%58 = load i8*, i8** %3
%59 = load i32, i32* %4
%60 = call i8* @realloc(i8* %58, i32 %59)
store i8* %60, i8** %3
%61 = load i8*, i8** %3
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %67
63: ; preds = %55
%64 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %55
%68 = load i8*, i8** %3
%69 = load i32, i32* %8
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
store i8* %71, i8** %5
br label %72
72: ; preds = %67, %45
br label %18
73: ; preds = %43, %38
%74 = load i8*, i8** %3
ret i8* %74
}

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 %42
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 = %42, %27
%10 = phi i64 [ %29, %27 ], [ %43, %42 ]
%11 = phi i8* [ %28, %27 ], [ %45, %42 ]
%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 %47
21: ; preds = %17
%22 = tail call i32 @free(i8* %46)
br label %47
23: ; preds = %9
%24 = load i8, i8* %2
%25 = icmp eq i8 %24, 10
br i1 %25, label %26, label %27
26: ; preds = %23
store i8 0, i8* %11
br label %47
27: ; preds = %23
store i8 %24, i8* %11
%28 = getelementptr inbounds i8, i8* %11, i64 1
%29 = add nuw i64 %10, 1
%30 = trunc i64 %29 to i32
%31 = icmp sgt i32 %44, %30
br i1 %31, label %9, label %32
32: ; preds = %27
%33 = add nuw nsw i32 %44, 20
%34 = tail call i8* @realloc(i8* %46, i32 %33)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = getelementptr inbounds i8, i8* %34, i64 %29
br label %42
42: ; preds = %1, %40
%43 = phi i64 [ %29, %40 ], [ 0, %1 ]
%44 = phi i32 [ %33, %40 ], [ 20, %1 ]
%45 = phi i8* [ %41, %40 ], [ %3, %1 ]
%46 = phi i8* [ %34, %40 ], [ %3, %1 ]
br label %9
47: ; preds = %20, %21, %26
%48 = phi i8* [ %46, %20 ], [ null, %21 ], [ %46, %26 ]
ret i8* %48
}