#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(int, char *, int);
int readline(int fd, char *buf, int maxlen) {
  int n, rc;
  char c;
  for (n = 1; n < maxlen; n++) {
    if ((rc = read(fd, &c, 1)) == 1) {
      *buf++ = c;
      if (c == '\n')
        break;
    } else if (rc == 0) {
      if (n == 1)
        return 0;
      else
        break;
    } else
      return -1;
  }
  *buf = '\0';
  return n;
}


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   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c1 <readline+0xb1>
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x1,%eax
jne    117d <readline+0x6d>
mov    -0x1d(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    1178 <readline+0x68>
jmp    11c1 <readline+0xb1>
jmp    11ae <readline+0x9e>
cmpl   $0x0,-0x1c(%rbp)
jne    11a2 <readline+0x92>
cmpl   $0x1,-0x18(%rbp)
jne    119d <readline+0x8d>
movl   $0x0,-0x4(%rbp)
jmp    11ce <readline+0xbe>
jmp    11c1 <readline+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ce <readline+0xbe>
jmp    11b3 <readline+0xa3>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <readline+0x19>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-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    %rsi,%r14
mov    $0x1,%ebx
cmp    $0x2,%edx
jl     1195 <readline+0x85>
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
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    116f <readline+0x5f>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
cmp    $0xa,%al
je     1187 <readline+0x77>
lea    0x1(%rbx),%rax
cmp    %eax,%r13d
jne    1140 <readline+0x30>
jmp    118b <readline+0x7b>
test   %eax,%eax
jne    1180 <readline+0x70>
test   %ebx,%ebx
je     11aa <readline+0x9a>
add    %rbx,%r14
add    $0x1,%rbx
jmp    1195 <readline+0x85>
mov    $0xffffffff,%ebx
jmp    1199 <readline+0x89>
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    1199 <readline+0x89>

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

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