int readline(int readch, char *buffer, int len) {
  static int pos = 0;
  int rpos;
  if (readch > 0) {
    switch (readch) {
    case '\n':
      break;
    case '\r':
      rpos = pos;
      pos = 0;
      return rpos;
    default:
      if (pos < len - 1) {
        buffer[pos++] = readch;
        buffer[pos] = 0;
      }
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11ad <readline+0xad>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xa,%eax
je     113d <readline+0x3d>
jmp    112c <readline+0x2c>
mov    -0x1c(%rbp),%eax
sub    $0xd,%eax
je     1142 <readline+0x42>
jmp    1160 <readline+0x60>
jmp    11a8 <readline+0xa8>
mov    0x2edc(%rip),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,0x2ecf(%rip)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b4 <readline+0xb4>
mov    0x2ebe(%rip),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11a3 <readline+0xa3>
mov    -0x8(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    0x2ea1(%rip),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,0x2e96(%rip)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq 0x2e85(%rip),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a8 <readline+0xa8>
jmp    11ad <readline+0xad>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
mov    $0xffffffff,%eax
test   %edi,%edi
jle    1145 <readline+0x45>
cmp    $0xa,%edi
je     1145 <readline+0x45>
cmp    $0xd,%edi
jne    1124 <readline+0x24>
mov    0x2f0b(%rip),%eax
movl   $0x0,0x2f01(%rip)
ret
movslq 0x2ef9(%rip),%rcx
add    $0xffffffff,%edx
cmp    %edx,%ecx
jge    1145 <readline+0x45>
lea    0x1(%rcx),%rdx
mov    %edx,0x2ee8(%rip)
mov    %dil,(%rsi,%rcx,1)
movb   $0x0,0x1(%rsi,%rcx,1)
ret

LLVM IR (-O0):
@readline.pos = internal global i32 0
define i32 @readline(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %36
11: ; preds = %3
%12 = load i32, i32* %5
switch i32 %12, label %17 [
i32 10, label %13
i32 13, label %14
]
13: ; preds = %11
br label %35
14: ; preds = %11
%15 = load i32, i32* @readline.pos
store i32 %15, i32* %8
store i32 0, i32* @readline.pos
%16 = load i32, i32* %8
store i32 %16, i32* %4
br label %37
17: ; preds = %11
%18 = load i32, i32* @readline.pos
%19 = load i32, i32* %7
%20 = sub nsw i32 %19, 1
%21 = icmp slt i32 %18, %20
br i1 %21, label %22, label %34
22: ; preds = %17
%23 = load i32, i32* %5
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %6
%26 = load i32, i32* @readline.pos
%27 = add nsw i32 %26, 1
store i32 %27, i32* @readline.pos
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 %24, i8* %29
%30 = load i8*, i8** %6
%31 = load i32, i32* @readline.pos
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %22, %17
br label %35
35: ; preds = %34, %13
br label %36
36: ; preds = %35, %3
store i32 -1, i32* %4
br label %37
37: ; preds = %36, %14
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@readline.pos = internal unnamed_addr global i32 0
define i32 @readline(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = icmp sgt i32 %0, 0
br i1 %4, label %5, label %19
5: ; preds = %3
switch i32 %0, label %8 [
i32 10, label %19
i32 13, label %6
]
6: ; preds = %5
%7 = load i32, i32* @readline.pos
store i32 0, i32* @readline.pos
br label %19
8: ; preds = %5
%9 = load i32, i32* @readline.pos
%10 = add nsw i32 %2, -1
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %19
12: ; preds = %8
%13 = trunc i32 %0 to i8
%14 = add nsw i32 %9, 1
store i32 %14, i32* @readline.pos
%15 = sext i32 %9 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 %13, i8* %16
%17 = sext i32 %14 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %3, %8, %12, %5, %6
%20 = phi i32 [ %7, %6 ], [ -1, %5 ], [ -1, %12 ], [ -1, %8 ], [ -1, %3 ]
ret i32 %20
}