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

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, size_t);
ssize_t readline(int fd, char *buffer, size_t len) {
  int i;
  ssize_t ret = 0;
  ret = read(fd, buffer, len);
  if (ret < 0) {
    perror("read");
  } else if (ret > 0) {
    for (i = 0; i < ret; i++) {
      if (buffer[i] == '\n') {
        buffer[i + 1] = 0;
        lseek(fd, i - ret + 1, SEEK_CUR);
        break;
      }
    }
    if (i == ret) {
      lseek(fd, -ret, SEEK_CUR);
      ret = len + 1;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1178 <readline+0x48>
lea    0xe92(%rip),%rdi
call   1050 <perror@plt>
jmp    1224 <readline+0xf4>
cmpl   $0x0,-0x20(%rbp)
jle    121f <readline+0xef>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11ed <readline+0xbd>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11da <readline+0xaa>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x4(%rbp),%edi
mov    -0x1c(%rbp),%esi
sub    -0x20(%rbp),%esi
add    $0x1,%esi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
jmp    11ed <readline+0xbd>
jmp    11df <readline+0xaf>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1189 <readline+0x59>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    121a <readline+0xea>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
sub    -0x20(%rbp),%esi
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x20(%rbp)
jmp    121f <readline+0xef>
jmp    1224 <readline+0xf4>
mov    -0x20(%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    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r14d
call   1040 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
js     11b3 <readline+0x83>
test   %r13,%r13
je     1171 <readline+0x41>
mov    %eax,%r12d
neg    %r12d
xor    %ebp,%ebp
nopl   (%rax)
cmpb   $0xa,(%rbx,%rbp,1)
je     1176 <readline+0x46>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1160 <readline+0x30>
jmp    1199 <readline+0x69>
xor    %r15d,%r15d
jmp    11c2 <readline+0x92>
movb   $0x0,0x1(%rbx,%rbp,1)
mov    %ebp,%esi
sub    %r13d,%esi
add    $0x1,%esi
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
call   1030 <lseek@plt>
cmp    %ebp,%r13d
jne    11bf <readline+0x8f>
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %r12d,%esi
call   1030 <lseek@plt>
add    $0x1,%r15d
jmp    11c2 <readline+0x92>
lea    0xe46(%rip),%rdi
call   1050 <perror@plt>
mov    %r13d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@SEEK_CUR = global i32 0
define i32 @readline(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %8
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = call i32 @read(i32 %9, i8* %10, i64 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %65
17: ; preds = %3
%18 = load i32, i32* %8
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %64
20: ; preds = %17
store i32 0, i32* %7
br label %21
21: ; preds = %47, %20
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %50
25: ; preds = %21
%26 = load i8*, i8** %5
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 10
br i1 %32, label %33, label %46
33: ; preds = %25
%34 = load i8*, i8** %5
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* %4
%40 = load i32, i32* %7
%41 = load i32, i32* %8
%42 = sub nsw i32 %40, %41
%43 = add nsw i32 %42, 1
%44 = load i32, i32* @SEEK_CUR
%45 = call i32 @lseek(i32 %39, i32 %43, i32 %44)
br label %50
46: ; preds = %25
br label %47
47: ; preds = %46
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %21
50: ; preds = %33, %21
%51 = load i32, i32* %7
%52 = load i32, i32* %8
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %63
54: ; preds = %50
%55 = load i32, i32* %4
%56 = load i32, i32* %8
%57 = sub nsw i32 0, %56
%58 = load i32, i32* @SEEK_CUR
%59 = call i32 @lseek(i32 %55, i32 %57, i32 %58)
%60 = load i64, i64* %6
%61 = add i64 %60, 1
%62 = trunc i64 %61 to i32
store i32 %62, i32* %8
br label %63
63: ; preds = %54, %50
br label %64
64: ; preds = %63, %17
br label %65
65: ; preds = %64, %15
%66 = load i32, i32* %8
ret i32 %66
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@SEEK_CUR = global i32 0
define i32 @readline(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @read(i32 %0, i8* %1, i64 %2)
%5 = sub i32 0, %4
%6 = icmp slt i32 %4, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %35
9: ; preds = %3
%10 = icmp eq i32 %4, 0
br i1 %10, label %35, label %11
11: ; preds = %9
%12 = zext i32 %4 to i64
br label %13
13: ; preds = %11, %18
%14 = phi i64 [ 0, %11 ], [ %19, %18 ]
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = add nuw nsw i64 %14, 1
%20 = icmp eq i64 %19, %12
br i1 %20, label %30, label %13
21: ; preds = %13
%22 = trunc i64 %14 to i32
%23 = add nuw i32 %22, 1
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 0, i8* %25
%26 = sub i32 %23, %4
%27 = load i32, i32* @SEEK_CUR
%28 = tail call i32 @lseek(i32 %0, i32 %26, i32 %27)
%29 = icmp eq i32 %4, %22
br i1 %29, label %30, label %35
30: ; preds = %18, %21
%31 = load i32, i32* @SEEK_CUR
%32 = tail call i32 @lseek(i32 %0, i32 %5, i32 %31)
%33 = trunc i64 %2 to i32
%34 = add i32 %33, 1
br label %35
35: ; preds = %9, %30, %21, %7
%36 = phi i32 [ %4, %7 ], [ %34, %30 ], [ %4, %21 ], [ 0, %9 ]
ret i32 %36
}