#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ die(int, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int read(int, char *, int);
char *realloc(char *, int);
char *readline(int fd) {
  ssize_t readno, allocno, readok;
  char *line, thechar;
  readno = 0;
  allocno = 80;
  line = malloc(allocno);
  if (!line)
    die(1, "memory full.");
  readok = read(fd, &thechar, 1);
  while (thechar != '\n' && thechar != '\r' && readok) {
    readno++;
    if (readno >= allocno) {
      allocno = allocno + 40;
      line = realloc(line, allocno);
      if (!line)
        die(1, "memory full.");
    }
    line[readno - 1] = thechar;
    readok = read(fd, &thechar, 1);
  }
  line[readno] = '\0';
  if (readok)
    return line;
  else {
    free(line);
    return NULL;
  }
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x50,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1191 <readline+0x41>
mov    $0x1,%edi
lea    0xe74(%rip),%rsi
call   1070 <die@plt>
mov    -0xc(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
movsbl -0x21(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x22(%rbp)
je     11d3 <readline+0x83>
movsbl -0x21(%rbp),%ecx
xor    %eax,%eax
cmp    $0xd,%ecx
mov    %al,-0x22(%rbp)
je     11d3 <readline+0x83>
cmpl   $0x0,-0x18(%rbp)
setne  %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    11e3 <readline+0x93>
jmp    125e <readline+0x10e>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1232 <readline+0xe2>
mov    -0x14(%rbp),%eax
add    $0x28,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122d <readline+0xdd>
mov    $0x1,%edi
lea    0xdd8(%rip),%rsi
call   1070 <die@plt>
jmp    1232 <readline+0xe2>
mov    -0x21(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
jmp    11a5 <readline+0x55>
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x18(%rbp)
je     1281 <readline+0x131>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1292 <readline+0x142>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    $0x50,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1181 <readline+0x31>
lea    0xe89(%rip),%rsi
mov    $0x1,%edi
call   1070 <die@plt>
lea    0x7(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     121a <readline+0xca>
mov    $0x50,%ebp
xor    %r12d,%r12d
jmp    11db <readline+0x8b>
mov    $0x1,%edi
lea    0xe4f(%rip),%rsi
call   1070 <die@plt>
xor    %r13d,%r13d
nopl   0x0(%rax)
mov    %r15b,0x0(%r13,%rbx,1)
mov    %r14d,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     121e <readline+0xce>
mov    %r12,%rbx
movzbl 0x7(%rsp),%r15d
cmp    $0xa,%r15b
je     1210 <readline+0xc0>
cmp    $0xd,%r15b
je     1210 <readline+0xc0>
lea    0x1(%rbx),%r12
cmp    %r12d,%ebp
jg     11c0 <readline+0x70>
add    $0x28,%ebp
mov    %r13,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     11a5 <readline+0x55>
mov    %rax,%r13
jmp    11c0 <readline+0x70>
mov    %ebx,%eax
movb   $0x0,0x0(%r13,%rax,1)
jmp    1232 <readline+0xe2>
xor    %eax,%eax
jmp    1221 <readline+0xd1>
mov    %r12d,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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