#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 */

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EINVAL;
scalar_t__ errno;
int read(int, char *, int);
int readline(int fd, char *buf, int nbytes) {
  int numread = 0;
  int returnval;
  while (numread < nbytes - 1) {
    returnval = read(fd, buf + numread, 1);
    if ((returnval == -1) && (errno == EINTR))
      continue;
    if ((returnval == 0) && (numread == 0))
      return 0;
    if (returnval == 0)
      break;
    if (returnval == -1)
      return -1;
    numread++;
    if (buf[numread - 1] == '\n') {
      buf[numread] = '\0';
      return numread;
    }
  }
  errno = EINVAL;
  return -1;
}


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   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1202 <readline+0xf2>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    117e <readline+0x6e>
mov    0x2e7a(%rip),%rax
mov    (%rax),%rax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
jne    117e <readline+0x6e>
jmp    1129 <readline+0x19>
cmpl   $0x0,-0x1c(%rbp)
jne    119e <readline+0x8e>
cmpl   $0x0,-0x18(%rbp)
jne    119e <readline+0x8e>
movl   $0x0,-0x4(%rbp)
jmp    121d <readline+0x10d>
cmpl   $0x0,-0x1c(%rbp)
jne    11ad <readline+0x9d>
jmp    1202 <readline+0xf2>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c3 <readline+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <readline+0x10d>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11fd <readline+0xed>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <readline+0x10d>
jmp    1129 <readline+0x19>
mov    0x2dc7(%rip),%rax
mov    (%rax),%rcx
mov    0x2dcd(%rip),%rax
mov    %rcx,(%rax)
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   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %edi,%ebp
add    $0xffffffff,%r15d
xor    %r12d,%r12d
test   %r15d,%r15d
cmovle %r12d,%r15d
mov    0x2eab(%rip),%r13
mov    0x2e9c(%rip),%r14
mov    %rsi,(%rsp)
cmp    %r15,%r12
je     1193 <readline+0x83>
lea    (%rsi,%r12,1),%rbx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    1170 <readline+0x60>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1150 <readline+0x40>
jmp    11a1 <readline+0x91>
nop
test   %eax,%eax
jne    1179 <readline+0x69>
test   %r12,%r12
je     11b9 <readline+0xa9>
test   %eax,%eax
mov    (%rsp),%rsi
je     1193 <readline+0x83>
add    $0x1,%r12
cmpb   $0xa,(%rbx)
jne    1140 <readline+0x30>
mov    %r12d,%eax
movb   $0x0,(%rsi,%rax,1)
jmp    11a7 <readline+0x97>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x0(%r13)
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r12d,%r12d
jmp    11a7 <readline+0x97>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EINVAL = global i64 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
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %61, %28, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = sub nsw i32 %12, 1
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %62
15: ; preds = %10
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @read(i32 %16, i8* %20, i32 1)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %15
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
br label %10
29: ; preds = %24, %15
%30 = load i32, i32* %9
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 0, i32* %4
br label %64
36: ; preds = %32, %29
%37 = load i32, i32* %9
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36
br label %62
40: ; preds = %36
%41 = load i32, i32* %9
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %40
store i32 -1, i32* %4
br label %64
44: ; preds = %40
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
%47 = load i8*, i8** %6
%48 = load i32, i32* %8
%49 = sub nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 10
br i1 %54, label %55, label %61
55: ; preds = %44
%56 = load i8*, i8** %6
%57 = load i32, i32* %8
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
%60 = load i32, i32* %8
store i32 %60, i32* %4
br label %64
61: ; preds = %44
br label %10
62: ; preds = %39, %10
%63 = load i64, i64* @EINVAL
store i64 %63, i64* @errno
store i32 -1, i32* %4
br label %64
64: ; preds = %62, %55, %43, %35
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EINVAL = global i64 0
define i32 @readline(i32 %0, i8* nocapture %1, i32 %2) {
%4 = add i32 %2, -1
%5 = call i32 @llvm.smax.i32(i32 %4, i32 0)
%6 = zext i32 %5 to i64
br label %7
7: ; preds = %23, %3
%8 = phi i64 [ %24, %23 ], [ 0, %3 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = icmp eq i64 %8, %6
br i1 %10, label %31, label %11
11: ; preds = %7, %14
%12 = tail call i32 @read(i32 %0, i8* %9, i32 1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %11, label %33
18: ; preds = %11
%19 = icmp eq i32 %12, 0
%20 = icmp eq i64 %8, 0
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %33, label %22
22: ; preds = %18
br i1 %19, label %31, label %23
23: ; preds = %22
%24 = add nuw nsw i64 %8, 1
%25 = load i8, i8* %9
%26 = icmp eq i8 %25, 10
br i1 %26, label %27, label %7
27: ; preds = %23
%28 = trunc i64 %24 to i32
%29 = and i64 %24, 4294967295
%30 = getelementptr inbounds i8, i8* %1, i64 %29
store i8 0, i8* %30
br label %33
31: ; preds = %22, %7
%32 = load i64, i64* @EINVAL
store i64 %32, i64* @errno
br label %33
33: ; preds = %18, %14, %31, %27
%34 = phi i32 [ -1, %31 ], [ %28, %27 ], [ -1, %14 ], [ 0, %18 ]
ret i32 %34
}