#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 my_read(int, char *);
ssize_t readline(int fd, void *vptr, size_t maxlen) {
  int 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) {
      if (n == 1)
        return (0);
      else
        break;
    } else
      return (-1);
  }
  *ptr = 0;
  return (n);
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11c7 <readline+0xb7>
mov    -0x8(%rbp),%edi
lea    -0x21(%rbp),%rsi
call   1030 <my_read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x1,%eax
jne    1183 <readline+0x73>
mov    -0x21(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    117e <readline+0x6e>
jmp    11c7 <readline+0xb7>
jmp    11b4 <readline+0xa4>
cmpl   $0x0,-0x20(%rbp)
jne    11a8 <readline+0x98>
cmpl   $0x1,-0x1c(%rbp)
jne    11a3 <readline+0x93>
movl   $0x0,-0x4(%rbp)
jmp    11d4 <readline+0xc4>
jmp    11c7 <readline+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d4 <readline+0xc4>
jmp    11b9 <readline+0xa9>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1132 <readline+0x22>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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     1194 <readline+0x84>
mov    %rdx,%r15
mov    %edi,%r13d
mov    $0x1,%ebp
sub    %rdx,%rbp
xor    %eax,%eax
lea    0x7(%rsp),%r12
xchg   %ax,%ax
mov    %rax,%rbx
mov    %r13d,%edi
mov    %r12,%rsi
call   1030 <my_read@plt>
cmp    $0x1,%eax
jne    116e <readline+0x5e>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
cmp    $0xa,%al
je     1186 <readline+0x76>
lea    0x1(%rbx),%rax
mov    %rbp,%rcx
add    %rax,%rcx
jne    1140 <readline+0x30>
jmp    118a <readline+0x7a>
test   %eax,%eax
jne    117f <readline+0x6f>
test   %ebx,%ebx
je     11a9 <readline+0x99>
add    %rbx,%r14
add    $0x1,%rbx
jmp    1194 <readline+0x84>
mov    $0xffffffff,%ebx
jmp    1198 <readline+0x88>
lea    0x1(%rbx),%r15
add    %rbx,%r14
add    $0x1,%r14
mov    %r15d,%ebx
movb   $0x0,(%r14)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
jmp    1198 <readline+0x88>

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

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