char *readline(fd) {
  static char linebuf[256];
  int cc;
  char *p;
  cc = read(fd, linebuf, sizeof(linebuf) - 1);
  if (cc <= 0)
    return 0;
  p = strchr(linebuf, '\n');
  if (p == 0)
    p = linebuf + sizeof(linebuf) - 1;
  else {
    p++;
    lseek(fd, (long)(p - linebuf) - cc, 1);
  }
  *p = 0;
  return linebuf;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    0x2f0b(%rip),%rsi
mov    $0xff,%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     116b <readline+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    11e3 <readline+0xb3>
lea    0x2ede(%rip),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a1 <readline+0x71>
lea    0x2ebe(%rip),%rax
add    $0xff,%rax
mov    %rax,-0x18(%rbp)
jmp    11d1 <readline+0xa1>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
lea    0x2e95(%rip),%rax
sub    %rax,%rsi
movslq -0x10(%rbp),%rax
sub    %rax,%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1040 <lseek@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2e71(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
lea    0x2f0f(%rip),%rsi
xor    %r15d,%r15d
mov    $0xff,%edx
xor    %eax,%eax
call   1050 <read@plt>
test   %eax,%eax
jle    119d <readline+0x6d>
mov    %eax,%ebx
lea    0x2ef3(%rip),%r15
mov    %r15,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1192 <readline+0x62>
mov    %rax,%rbp
add    $0x1,%rbp
mov    %ebx,%eax
add    %r15,%rax
mov    %rbp,%rsi
sub    %rax,%rsi
mov    %r14d,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <lseek@plt>
jmp    1199 <readline+0x69>
lea    0x2fb6(%rip),%rbp
movb   $0x0,0x0(%rbp)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@readline.linebuf = internal global [256 x i8] zeroinitializer
define i8* @readline(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %6, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @readline.linebuf, i64 0, i64 0), i64 255)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp sle i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %29
11: ; preds = %1
%12 = call i8* @strchr(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @readline.linebuf, i64 0, i64 0), i32 10)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %11
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @readline.linebuf, i64 1, i64 -1), i8** %5
br label %27
16: ; preds = %11
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %5
%19 = load i32, i32* %3
%20 = load i8*, i8** %5
%21 = ptrtoint i8* %20 to i64
%22 = sub i64 %21, ptrtoint ([256 x i8]* @readline.linebuf to i64)
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = sub nsw i64 %22, %24
%26 = call i32 (i32, i64, i32, ...) bitcast (i32 (...)* @lseek to i32 (i32, i64, i32, ...)*)(i32 %19, i64 %25, i32 1)
br label %27
27: ; preds = %16, %15
%28 = load i8*, i8** %5
store i8 0, i8* %28
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @readline.linebuf, i64 0, i64 0), i8** %2
br label %29
29: ; preds = %27, %10
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
@readline.linebuf = internal global [256 x i8] zeroinitializer
define i8* @readline(i32 %0) {
%2 = tail call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @readline.linebuf, i64 0, i64 0), i64 255)
%3 = icmp slt i32 %2, 1
br i1 %3, label %16, label %4
4: ; preds = %1
%5 = tail call i8* @strchr(i8* dereferenceable(1) getelementptr inbounds ([256 x i8], [256 x i8]* @readline.linebuf, i64 0, i64 0), i32 10)
%6 = icmp eq i8* %5, null
br i1 %6, label %14, label %7
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %5, i64 1
%9 = ptrtoint i8* %8 to i64
%10 = zext i32 %2 to i64
%11 = add i64 %10, ptrtoint ([256 x i8]* @readline.linebuf to i64)
%12 = sub i64 %9, %11
%13 = tail call i32 (i32, i64, i32, ...) bitcast (i32 (...)* @lseek to i32 (i32, i64, i32, ...)*)(i32 %0, i64 %12, i32 1)
br label %14
14: ; preds = %4, %7
%15 = phi i8* [ %8, %7 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @readline.linebuf, i64 0, i64 255), %4 ]
store i8 0, i8* %15
br label %16
16: ; preds = %1, %14
%17 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @readline.linebuf, i64 0, i64 0), %14 ], [ null, %1 ]
ret i8* %17
}