#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 scalar_t__ ssize_t;
typedef int int8_t;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ elog(char *);
scalar_t__ read(int, char *, int);
char *realloc(char *, int);
int8_t read_line(int fd, char **buffer, size_t *len) {
  size_t off = 0, l = *len;
  char *new, *buf = *buffer;
  int8_t ret = -1;
  ssize_t rd;
  while (1) {
    if (off >= l) {
      new = realloc(buf, 32 + (l << 1));
      if (!new) {
        elog("cannot read line");
        goto out;
      }
      buf = new;
      l = 32 + (l << 1);
    }
    rd = read(fd, buf + off, 1);
    if (rd == EAGAIN)
      continue;
    if (rd == 0) {
      buf[off] = '\0';
      break;
    }
    if (buf[off] == '\n') {
      buf[off] = '\0';
      break;
    }
    off++;
  }
  ret = 0;
out:
  *buffer = buf;
  *len = l;
  return ret;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     11c5 <read_line+0x95>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
add    $0x20,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ad <read_line+0x7d>
lea    0xe5d(%rip),%rdi
call   1040 <elog@plt>
jmp    1251 <read_line+0x121>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
add    $0x20,%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x38(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
jne    11f7 <read_line+0xc7>
jmp    1168 <read_line+0x38>
cmpq   $0x0,-0x48(%rbp)
jne    1213 <read_line+0xe3>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    124a <read_line+0x11a>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1239 <read_line+0x109>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    124a <read_line+0x11a>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1168 <read_line+0x38>
movl   $0x0,-0x3c(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%eax
add    $0x50,%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,0x4(%rsp)
mov    %rdx,0x10(%rsp)
mov    (%rdx),%r12
mov    %rsi,0x8(%rsp)
mov    (%rsi),%rbx
xor    %r15d,%r15d
mov    %r12,%r13
mov    %rbx,%rbp
jmp    118d <read_line+0x5d>
nopl   (%rax)
mov    %r13,%r12
mov    %rbp,%rbx
lea    (%rbx,%r15,1),%r14
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %r12,%r13
mov    %rbx,%rbp
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <read_line+0x90>
cmp    %r13,%r15
jb     1160 <read_line+0x30>
lea    0x20(,%r13,1),%r12
add    %r13,%r12
mov    %rbp,%rdi
mov    %r12d,%esi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1166 <read_line+0x36>
jmp    11d7 <read_line+0xa7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %rax,%rax
je     11cf <read_line+0x9f>
add    $0x1,%r15
cmpb   $0xa,(%r14)
jne    1155 <read_line+0x25>
movb   $0x0,(%r14)
xor    %eax,%eax
jmp    11ee <read_line+0xbe>
lea    0xe22(%rip),%rdi
call   1040 <elog@plt>
mov    $0xffffffff,%eax
mov    %r13,%r12
mov    %rbp,%rbx
mov    0x8(%rsp),%rcx
mov    %rbx,(%rcx)
mov    0x10(%rsp),%rcx
mov    %r12,(%rcx)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [17 x i8] c"cannot read line\00"
@EAGAIN = global i64 0
define i32 @read_line(i32 %0, i8** nocapture %1, i64* nocapture %2) {
%4 = load i64, i64* %2
%5 = load i8*, i8** %1
br label %6
6: ; preds = %31, %3
%7 = phi i64 [ %23, %31 ], [ %4, %3 ]
%8 = phi i8* [ %24, %31 ], [ %5, %3 ]
%9 = phi i64 [ %34, %31 ], [ 0, %3 ]
br label %10
10: ; preds = %6, %22
%11 = phi i64 [ %23, %22 ], [ %7, %6 ]
%12 = phi i8* [ %24, %22 ], [ %8, %6 ]
%13 = icmp ult i64 %9, %11
br i1 %13, label %22, label %14
14: ; preds = %10
%15 = shl i64 %11, 1
%16 = add i64 %15, 32
%17 = trunc i64 %16 to i32
%18 = tail call i8* @realloc(i8* %12, i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @elog(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %36
22: ; preds = %14, %10
%23 = phi i64 [ %11, %10 ], [ %16, %14 ]
%24 = phi i8* [ %12, %10 ], [ %18, %14 ]
%25 = getelementptr inbounds i8, i8* %24, i64 %9
%26 = tail call i64 @read(i32 %0, i8* %25, i32 1)
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %26, %27
br i1 %28, label %10, label %29
29: ; preds = %22
%30 = icmp eq i64 %26, 0
br i1 %30, label %35, label %31
31: ; preds = %29
%32 = load i8, i8* %25
%33 = icmp eq i8 %32, 10
%34 = add i64 %9, 1
br i1 %33, label %35, label %6
35: ; preds = %31, %29
store i8 0, i8* %25
br label %36
36: ; preds = %35, %20
%37 = phi i64 [ %23, %35 ], [ %11, %20 ]
%38 = phi i8* [ %24, %35 ], [ %12, %20 ]
%39 = phi i32 [ 0, %35 ], [ -1, %20 ]
store i8* %38, i8** %1
store i64 %37, i64* %2
ret i32 %39
}