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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
size_t read(int, char *, int);
ssize_t readline(int fd, void *vptr, size_t maxlen) {
  ssize_t n, rc;
  char c, *ptr;
  ptr = vptr;
  for (n = 1; n < maxlen; n++) {
  again:
    if ((rc = read(fd, &c, 1)) == 1) {
      *ptr++ = c;
      if (c == '\n')
        break;
    } else if (rc == 0) {
      *ptr = 0;
      return n - 1;
    } else {
      if (errno == EINTR)
        goto again;
      return -1;
    }
  }
  *ptr = 0;
  return n;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11f5 <readline+0xe5>
jmp    1146 <readline+0x36>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x1,%rax
jne    1190 <readline+0x80>
mov    -0x31(%rbp),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    118b <readline+0x7b>
jmp    11f5 <readline+0xe5>
jmp    11df <readline+0xcf>
cmpq   $0x0,-0x30(%rbp)
jne    11b3 <readline+0xa3>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1204 <readline+0xf4>
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
jne    11d2 <readline+0xc2>
jmp    1146 <readline+0x36>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1204 <readline+0xf4>
jmp    11e4 <readline+0xd4>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1133 <readline+0x23>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    $0x1,%r12d
cmp    $0x2,%rdx
jb     1196 <readline+0x86>
mov    %edi,%ebp
mov    $0x1,%r12d
lea    0xf(%rsp),%rbx
mov    0x2e9f(%rip),%r13
mov    0x2e90(%rip),%r15
mov    %rdx,0x10(%rsp)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
je     119c <readline+0x8c>
cmp    $0x1,%rax
je     1175 <readline+0x65>
mov    0x0(%r13),%rax
cmp    (%r15),%rax
je     1150 <readline+0x40>
jmp    11a6 <readline+0x96>
movzbl 0xf(%rsp),%eax
mov    %al,(%r14)
add    $0x1,%r14
cmp    $0xa,%al
je     1196 <readline+0x86>
add    $0x1,%r12
mov    0x10(%rsp),%rdx
cmp    %rdx,%r12
jne    1150 <readline+0x40>
mov    %rdx,%r12
movb   $0x0,(%r14)
jmp    11ad <readline+0x9d>
movb   $0x0,(%r14)
add    $0xffffffffffffffff,%r12
jmp    11ad <readline+0x9d>
mov    $0xffffffffffffffff,%r12
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i64 @readline(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8
%11 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i8*, i8** %6
store i8* %12, i8** %11
store i64 1, i64* %8
br label %13
13: ; preds = %45, %3
%14 = load i64, i64* %8
%15 = load i64, i64* %7
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %48
17: ; preds = %13
br label %18
18: ; preds = %42, %17
%19 = load i32, i32* %5
%20 = call i64 @read(i32 %19, i8* %10, i32 1)
store i64 %20, i64* %9
%21 = icmp eq i64 %20, 1
br i1 %21, label %22, label %31
22: ; preds = %18
%23 = load i8, i8* %10
%24 = load i8*, i8** %11
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %11
store i8 %23, i8* %24
%26 = load i8, i8* %10
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %30
29: ; preds = %22
br label %48
30: ; preds = %22
br label %44
31: ; preds = %18
%32 = load i64, i64* %9
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i8*, i8** %11
store i8 0, i8* %35
%36 = load i64, i64* %8
%37 = sub i64 %36, 1
store i64 %37, i64* %4
br label %51
38: ; preds = %31
%39 = load i64, i64* @errno
%40 = load i64, i64* @EINTR
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %38
br label %18
43: ; preds = %38
store i64 -1, i64* %4
br label %51
44: ; preds = %30
br label %45
45: ; preds = %44
%46 = load i64, i64* %8
%47 = add i64 %46, 1
store i64 %47, i64* %8
br label %13
48: ; preds = %29, %13
%49 = load i8*, i8** %11
store i8 0, i8* %49
%50 = load i64, i64* %8
store i64 %50, i64* %4
br label %51
51: ; preds = %48, %43, %34
%52 = load i64, i64* %4
ret i64 %52
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i64 @readline(i32 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = alloca i8
%5 = icmp ugt i64 %2, 1
br i1 %5, label %6, label %24
6: ; preds = %3, %21
%7 = phi i8* [ %13, %21 ], [ %1, %3 ]
%8 = phi i64 [ %22, %21 ], [ 1, %3 ]
br label %9
9: ; preds = %6, %17
%10 = call i64 @read(i32 %0, i8* %4, i32 1)
switch i64 %10, label %17 [
i64 1, label %11
i64 0, label %15
]
11: ; preds = %9
%12 = load i8, i8* %4
%13 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %12, i8* %7
%14 = icmp eq i8 %12, 10
br i1 %14, label %24, label %21
15: ; preds = %9
store i8 0, i8* %7
%16 = add i64 %8, -1
br label %27
17: ; preds = %9
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %9, label %27
21: ; preds = %11
%22 = add nuw i64 %8, 1
%23 = icmp eq i64 %22, %2
br i1 %23, label %24, label %6
24: ; preds = %21, %11, %3
%25 = phi i64 [ 1, %3 ], [ %8, %11 ], [ %2, %21 ]
%26 = phi i8* [ %1, %3 ], [ %13, %11 ], [ %13, %21 ]
store i8 0, i8* %26
br label %27
27: ; preds = %17, %24, %15
%28 = phi i64 [ %25, %24 ], [ %16, %15 ], [ -1, %17 ]
ret i64 %28
}