int readline(fd, ptr, maxlen)
register int fd;
register char *ptr;
register int maxlen;
{
  int n, rc;
  char c;
  for (n = 1; n < maxlen; n++) {
    if ((rc = read(fd, &c, 1)) == 1) {
      *ptr++ = c;
      if (c == '\n')
        break;
    } else if (rc == 0) {
      if (n == 1)
        return (0);
      else
        break;
    } else
      return (-1);
  }
  *ptr = 0;
  return (n);
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c3 <readline+0xb3>
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x1,%eax
jne    117f <readline+0x6f>
mov    -0x1d(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    117a <readline+0x6a>
jmp    11c3 <readline+0xb3>
jmp    11b0 <readline+0xa0>
cmpl   $0x0,-0x1c(%rbp)
jne    11a4 <readline+0x94>
cmpl   $0x1,-0x18(%rbp)
jne    119f <readline+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <readline+0xc0>
jmp    11c3 <readline+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <readline+0xc0>
jmp    11b5 <readline+0xa5>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <readline+0x19>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    $0x1,%ebx
cmp    $0x2,%edx
jl     1197 <readline+0x87>
mov    %edx,%r15d
mov    %edi,%ebp
lea    -0x1(%r15),%r13d
lea    0x7(%rsp),%r12
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1171 <readline+0x61>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
cmp    $0xa,%al
je     1189 <readline+0x79>
lea    0x1(%rbx),%rax
cmp    %eax,%r13d
jne    1140 <readline+0x30>
jmp    118d <readline+0x7d>
test   %eax,%eax
jne    1182 <readline+0x72>
test   %ebx,%ebx
je     11ac <readline+0x9c>
add    %rbx,%r14
add    $0x1,%rbx
jmp    1197 <readline+0x87>
mov    $0xffffffff,%ebx
jmp    119b <readline+0x8b>
lea    0x1(%rbx),%r15d
add    %rbx,%r14
add    $0x1,%r14
mov    %r15d,%ebx
movb   $0x0,(%r14)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
jmp    119b <readline+0x8b>

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

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