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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
size_t read(int, char *, int);
ssize_t readline(int fd, void *ptr, size_t maxlen) {
  ssize_t n, num_read;
  char c;
  char *p;
  p = ptr;
  for (n = 1; n < maxlen; ++n) {
    num_read = read(fd, &c, 1);
    if (num_read == -1) {
      if (errno == EINTR)
        continue;
      else
        return -1;
    } else if (num_read == 0) {
      *p = '\0';
      return (n - 1);
    } else {
      *p++ = c;
      if (c == '\n')
        break;
    }
  }
  *p = '\0';
  return n;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11f6 <readline+0xe6>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    118d <readline+0x7d>
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <readline+0x70>
jmp    11e5 <readline+0xd5>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1205 <readline+0xf5>
cmpq   $0x0,-0x30(%rbp)
jne    11b0 <readline+0xa0>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1205 <readline+0xf5>
mov    -0x31(%rbp),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    11d6 <readline+0xc6>
jmp    11f6 <readline+0xe6>
jmp    11db <readline+0xcb>
jmp    11e0 <readline+0xd0>
jmp    11e5 <readline+0xd5>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1133 <readline+0x23>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    $0x1,%ebx
cmp    $0x2,%rdx
jb     119b <readline+0x8b>
mov    %rdx,%r15
mov    %edi,%ebp
mov    $0x1,%ebx
lea    0x7(%rsp),%r12
mov    0x2e99(%rip),%r13
jmp    1169 <readline+0x59>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2e89(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    11b1 <readline+0xa1>
add    $0x1,%rbx
cmp    %rbx,%r15
je     1198 <readline+0x88>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffffffffffff,%rax
je     1150 <readline+0x40>
test   %rax,%rax
je     11ba <readline+0xaa>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14)
add    $0x1,%r14
cmpb   $0xa,0x7(%rsp)
jne    1160 <readline+0x50>
jmp    119b <readline+0x8b>
mov    %r15,%rbx
movb   $0x0,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffffffffffff,%rbx
jmp    119f <readline+0x8f>
movb   $0x0,(%r14)
add    $0xffffffffffffffff,%rbx
jmp    119f <readline+0x8f>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i64 @readline(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8
%11 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i8*, i8** %6
store i8* %12, i8** %11
store i64 1, i64* %8
br label %13
13: ; preds = %46, %3
%14 = load i64, i64* %8
%15 = load i64, i64* %7
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %49
17: ; preds = %13
%18 = load i32, i32* %5
%19 = call i64 @read(i32 %18, i8* %10, i32 1)
store i64 %19, i64* %9
%20 = load i64, i64* %9
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %46
27: ; preds = %22
store i64 -1, i64* %4
br label %52
28: ; preds = %17
%29 = load i64, i64* %9
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i8*, i8** %11
store i8 0, i8* %32
%33 = load i64, i64* %8
%34 = sub i64 %33, 1
store i64 %34, i64* %4
br label %52
35: ; preds = %28
%36 = load i8, i8* %10
%37 = load i8*, i8** %11
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %11
store i8 %36, i8* %37
%39 = load i8, i8* %10
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 10
br i1 %41, label %42, label %43
42: ; preds = %35
br label %49
43: ; preds = %35
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45, %26
%47 = load i64, i64* %8
%48 = add i64 %47, 1
store i64 %48, i64* %8
br label %13
49: ; preds = %42, %13
%50 = load i8*, i8** %11
store i8 0, i8* %50
%51 = load i64, i64* %8
store i64 %51, i64* %4
br label %52
52: ; preds = %49, %31, %27
%53 = load i64, i64* %4
ret i64 %53
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i64 @readline(i32 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = alloca i8
%5 = icmp ugt i64 %2, 1
br i1 %5, label %6, label %25
6: ; preds = %3, %21
%7 = phi i8* [ %22, %21 ], [ %1, %3 ]
%8 = phi i64 [ %23, %21 ], [ 1, %3 ]
%9 = call i64 @read(i32 %0, i8* %4, i32 1)
switch i64 %9, label %16 [
i64 -1, label %10
i64 0, label %14
]
10: ; preds = %6
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %21, label %28
14: ; preds = %6
store i8 0, i8* %7
%15 = add i64 %8, -1
br label %28
16: ; preds = %6
%17 = load i8, i8* %4
%18 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %17, i8* %7
%19 = load i8, i8* %4
%20 = icmp eq i8 %19, 10
br i1 %20, label %25, label %21
21: ; preds = %16, %10
%22 = phi i8* [ %7, %10 ], [ %18, %16 ]
%23 = add nuw i64 %8, 1
%24 = icmp eq i64 %23, %2
br i1 %24, label %25, label %6
25: ; preds = %21, %16, %3
%26 = phi i64 [ 1, %3 ], [ %8, %16 ], [ %2, %21 ]
%27 = phi i8* [ %1, %3 ], [ %18, %16 ], [ %22, %21 ]
store i8 0, i8* %27
br label %28
28: ; preds = %10, %25, %14
%29 = phi i64 [ %15, %14 ], [ %26, %25 ], [ -1, %10 ]
ret i64 %29
}