readline(p, n) register char *p;
register int n;
{
  char c;
  while (n-- > 0) {
    if (read(0, &c, 1) <= 0)
      return (-1);
    c &= 0177;
    if (c == '\n' || c == '\r') {
      *p = '\0';
      return (0);
    }
    *p++ = c;
  }
  return (-1);
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    11aa <readline+0x9a>
xor    %edi,%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <read@plt>
cmp    $0x0,%eax
jg     115a <readline+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <readline+0xa1>
movsbl -0x15(%rbp),%eax
and    $0x7f,%eax
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
je     117e <readline+0x6e>
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
jne    1191 <readline+0x81>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11b1 <readline+0xa1>
mov    -0x15(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    111f <readline+0xf>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %esi,%esi
jle    1190 <readline+0x80>
mov    %esi,%ebp
mov    %rdi,%rbx
add    $0x1,%ebp
lea    0x7(%rsp),%r15
jmp    113e <readline+0x2e>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
jbe    1190 <readline+0x80>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <read@plt>
test   %eax,%eax
jle    1190 <readline+0x80>
movzbl 0x7(%rsp),%ecx
mov    %ecx,%eax
and    $0x7f,%al
mov    %al,0x7(%rsp)
cmp    $0x9,%cl
jg     1180 <readline+0x70>
cmp    $0x8a,%cl
je     118a <readline+0x7a>
cmp    $0x8d,%cl
jne    1130 <readline+0x20>
jmp    118a <readline+0x7a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0xd,%cl
je     118a <readline+0x7a>
cmp    $0xa,%cl
jne    1130 <readline+0x20>
movb   $0x0,(%rbx)
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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