#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 *buffer, int nbytes) {
  int i, r;
  if (fd < 0 || buffer == NULL)
    return -1;
  for (i = 0; i < nbytes; i++) {
    if ((r = read(fd, buffer + i, 1)) < 0) {
      return (-1);
    } else if (r == 0) {
      break;
    } else if (buffer[i] == '\n') {
      i++;
      break;
    }
  }
  return i;
}


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)
cmpl   $0x0,-0x8(%rbp)
jl     1137 <readline+0x27>
cmpq   $0x0,-0x10(%rbp)
jne    1143 <readline+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <readline+0xcb>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d5 <readline+0xc5>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1186 <readline+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <readline+0xcb>
cmpl   $0x0,-0x1c(%rbp)
jne    1195 <readline+0x85>
jmp    11d5 <readline+0xc5>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11b8 <readline+0xa8>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11d5 <readline+0xc5>
jmp    11bd <readline+0xad>
jmp    11c2 <readline+0xb2>
jmp    11c7 <readline+0xb7>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    114a <readline+0x3a>
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    $0xffffffff,%r14d
test   %edi,%edi
js     1178 <readline+0x68>
mov    %rsi,%r15
test   %rsi,%rsi
je     1178 <readline+0x68>
test   %edx,%edx
jle    116d <readline+0x5d>
mov    %edi,%r12d
mov    %edx,%r13d
xor    %ebp,%ebp
nopl   0x0(%rax)
lea    (%r15,%rbp,1),%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1178 <readline+0x68>
je     1175 <readline+0x65>
cmpb   $0xa,(%rbx)
je     1172 <readline+0x62>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1140 <readline+0x30>
mov    %r13d,%r14d
jmp    1178 <readline+0x68>
xor    %r14d,%r14d
jmp    1178 <readline+0x68>
add    $0x1,%ebp
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @readline(i32 %0, i8* %1, i32 %2) {
%4 = icmp slt i32 %0, 0
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
br i1 %6, label %29, label %7
7: ; preds = %3
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %29
9: ; preds = %7
%10 = zext i32 %2 to i64
br label %11
11: ; preds = %9, %24
%12 = phi i64 [ 0, %9 ], [ %25, %24 ]
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = tail call i32 @read(i32 %0, i8* %13, i32 1)
%15 = icmp slt i32 %14, 0
br i1 %15, label %29, label %16
16: ; preds = %11
%17 = icmp eq i32 %14, 0
br i1 %17, label %27, label %18
18: ; preds = %16
%19 = load i8, i8* %13
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = trunc i64 %12 to i32
%23 = add nuw nsw i32 %22, 1
br label %29
24: ; preds = %18
%25 = add nuw nsw i64 %12, 1
%26 = icmp eq i64 %25, %10
br i1 %26, label %29, label %11
27: ; preds = %16
%28 = trunc i64 %12 to i32
br label %29
29: ; preds = %24, %11, %27, %7, %21, %3
%30 = phi i32 [ -1, %3 ], [ %23, %21 ], [ 0, %7 ], [ %28, %27 ], [ %2, %24 ], [ -1, %11 ]
ret i32 %30
}