#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 */
size_t my_read(int, char *);
ssize_t readline(int fd, char *vptr, size_t maxlen) {
  ssize_t n, rc;
  char c, *ptr;
  ptr = vptr;
  for (n = 1; n < maxlen; n++) {
    if ((rc = my_read(fd, &c)) == 1) {
      *ptr++ = c;
      if (c == '\n')
        break;
    } else if (rc == 0) {
      *ptr = 0;
      return (n - 1);
    } else
      return -1;
  }
  *ptr = 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    11cc <readline+0xbc>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
call   1030 <my_read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x1,%rax
jne    1186 <readline+0x76>
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    1181 <readline+0x71>
jmp    11cc <readline+0xbc>
jmp    11b6 <readline+0xa6>
cmpq   $0x0,-0x30(%rbp)
jne    11a9 <readline+0x99>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11db <readline+0xcb>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11db <readline+0xcb>
jmp    11bb <readline+0xab>
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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    $0x1,%ebx
cmp    $0x2,%rdx
jb     1170 <readline+0x60>
mov    %rdx,%r15
mov    %edi,%ebp
add    $0x1,%r14
mov    $0x1,%ebx
lea    0xf(%rsp),%r12
mov    %r14,%rax
mov    %rax,%r14
mov    %ebp,%edi
mov    %r12,%rsi
call   1030 <my_read@plt>
cmp    $0x1,%rax
jne    1184 <readline+0x74>
movzbl 0xf(%rsp),%eax
mov    %al,-0x1(%r14)
cmp    $0xa,%al
je     1170 <readline+0x60>
add    $0x1,%rbx
lea    0x1(%r14),%rax
cmp    %rbx,%r15
jne    1140 <readline+0x30>
mov    %r15,%rbx
movb   $0x0,(%r14)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
test   %rax,%rax
jne    1194 <readline+0x84>
movb   $0x0,-0x1(%r14)
add    $0xffffffffffffffff,%rbx
jmp    1174 <readline+0x64>
mov    $0xffffffffffffffff,%rbx
jmp    1174 <readline+0x64>

LLVM IR (-O0):
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 = %39, %3
%14 = load i64, i64* %8
%15 = load i64, i64* %7
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %42
17: ; preds = %13
%18 = load i32, i32* %5
%19 = call i64 @my_read(i32 %18, i8* %10)
store i64 %19, i64* %9
%20 = icmp eq i64 %19, 1
br i1 %20, label %21, label %30
21: ; preds = %17
%22 = load i8, i8* %10
%23 = load i8*, i8** %11
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %11
store i8 %22, i8* %23
%25 = load i8, i8* %10
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %29
28: ; preds = %21
br label %42
29: ; preds = %21
br label %38
30: ; preds = %17
%31 = load i64, i64* %9
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i8*, i8** %11
store i8 0, i8* %34
%35 = load i64, i64* %8
%36 = sub i64 %35, 1
store i64 %36, i64* %4
br label %45
37: ; preds = %30
store i64 -1, i64* %4
br label %45
38: ; preds = %29
br label %39
39: ; preds = %38
%40 = load i64, i64* %8
%41 = add i64 %40, 1
store i64 %41, i64* %8
br label %13
42: ; preds = %28, %13
%43 = load i8*, i8** %11
store i8 0, i8* %43
%44 = load i64, i64* %8
store i64 %44, i64* %4
br label %45
45: ; preds = %42, %37, %33
%46 = load i64, i64* %4
ret i64 %46
}

LLVM IR (-O2):
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 %19
6: ; preds = %3, %16
%7 = phi i8* [ %12, %16 ], [ %1, %3 ]
%8 = phi i64 [ %17, %16 ], [ 1, %3 ]
%9 = call i64 @my_read(i32 %0, i8* %4)
switch i64 %9, label %22 [
i64 1, label %10
i64 0, label %14
]
10: ; preds = %6
%11 = load i8, i8* %4
%12 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %11, i8* %7
%13 = icmp eq i8 %11, 10
br i1 %13, label %19, label %16
14: ; preds = %6
store i8 0, i8* %7
%15 = add i64 %8, -1
br label %22
16: ; preds = %10
%17 = add nuw i64 %8, 1
%18 = icmp eq i64 %17, %2
br i1 %18, label %19, label %6
19: ; preds = %16, %10, %3
%20 = phi i64 [ 1, %3 ], [ %8, %10 ], [ %2, %16 ]
%21 = phi i8* [ %1, %3 ], [ %12, %10 ], [ %12, %16 ]
store i8 0, i8* %21
br label %22
22: ; preds = %6, %19, %14
%23 = phi i64 [ %20, %19 ], [ %15, %14 ], [ -1, %6 ]
ret i64 %23
}