#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 */
int read_wrapper(int, char *);
int readline(int fd, void *buf, int maxlen) {
  int n, count;
  char c;
  char *p = buf;
  for (n = 1; n < maxlen; n++) {
    count = read_wrapper(fd, &c);
    if (count == 1) {
      *p++ = c;
      if (c == '\n')
        break;
    } else if (count == 0) {
      if (n == 1)
        return 0;
      else
        break;
    } else {
      return -1;
    }
  }
  *p = 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    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c5 <readline+0xb5>
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
call   1030 <read_wrapper@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    1181 <readline+0x71>
mov    -0x1d(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    117c <readline+0x6c>
jmp    11c5 <readline+0xb5>
jmp    11b2 <readline+0xa2>
cmpl   $0x0,-0x1c(%rbp)
jne    11a6 <readline+0x96>
cmpl   $0x1,-0x18(%rbp)
jne    11a1 <readline+0x91>
movl   $0x0,-0x4(%rbp)
jmp    11d2 <readline+0xc2>
jmp    11c5 <readline+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <readline+0xc2>
jmp    11b7 <readline+0xa7>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1131 <readline+0x21>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%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,%edx
jl     1190 <readline+0x80>
mov    %edx,%r15d
mov    %edi,%ebp
lea    -0x1(%r15),%r13d
xor    %eax,%eax
lea    0x7(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r12,%rsi
call   1030 <read_wrapper@plt>
cmp    $0x1,%eax
jne    116a <readline+0x5a>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
cmp    $0xa,%al
je     1182 <readline+0x72>
lea    0x1(%rbx),%rax
cmp    %eax,%r13d
jne    1140 <readline+0x30>
jmp    1186 <readline+0x76>
test   %eax,%eax
jne    117b <readline+0x6b>
test   %ebx,%ebx
je     11a5 <readline+0x95>
add    %rbx,%r14
add    $0x1,%rbx
jmp    1190 <readline+0x80>
mov    $0xffffffff,%ebx
jmp    1194 <readline+0x84>
lea    0x1(%rbx),%r15d
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    1194 <readline+0x84>

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