#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 sl_readline(int fd, char *buffer, int buflen) {
  int nread = 0;
  if (!buffer)
    return -1;
  while (nread < (buflen - 1)) {
    if (read(fd, buffer + nread, 1) != 1) {
      return -1;
    }
    if (buffer[nread] == '\n') {
      break;
    }
    nread++;
  }
  buffer[nread] = '\0';
  return nread;
}


x86 assembly (clang, -O0):
<sl_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1140 <sl_readline+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bd <sl_readline+0xad>
jmp    1145 <sl_readline+0x35>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11ab <sl_readline+0x9b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1183 <sl_readline+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bd <sl_readline+0xad>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    119d <sl_readline+0x8d>
jmp    11ab <sl_readline+0x9b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1145 <sl_readline+0x35>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sl_readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%eax
test   %rsi,%rsi
je     1175 <sl_readline+0x65>
mov    %edx,%r15d
mov    %rsi,%r13
xor    %ebx,%ebx
cmp    $0x2,%edx
jl     116b <sl_readline+0x5b>
mov    %edi,%r12d
add    $0xffffffff,%r15d
mov    %r15d,%r14d
xor    %ebx,%ebx
xchg   %ax,%ax
lea    (%rbx,%r13,1),%rbp
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1184 <sl_readline+0x74>
cmpb   $0xa,0x0(%rbp)
je     116b <sl_readline+0x5b>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1140 <sl_readline+0x30>
mov    %r15d,%ebx
mov    %ebx,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1175 <sl_readline+0x65>

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

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