#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 /*<<< orphan*/ cerebro_err_output(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int fd_read_line(int, char *, unsigned int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int _readline(int fd, char *buf, unsigned int buflen) {
  int len;
  if (fd <= 0 || !buf || !buflen) {
    ;
    return -1;
  }
  if ((len = fd_read_line(fd, buf, buflen)) < 0) {
    do {
      cerebro_err_output("fd_read_line: %s", strerror(errno));
    } while (0);
    return -1;
  }
  if (len >= (buflen - 1)) {
    ;
    return -1;
  }
  return len;
}


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)
jle    1161 <_readline+0x31>
cmpq   $0x0,-0x10(%rbp)
je     1161 <_readline+0x31>
cmpl   $0x0,-0x14(%rbp)
jne    116d <_readline+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <_readline+0xa8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <fd_read_line@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11b5 <_readline+0x85>
jmp    118d <_readline+0x5d>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe5c(%rip),%rdi
call   1030 <cerebro_err_output@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <_readline+0xa8>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jb     11d2 <_readline+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <_readline+0xa8>
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   %rbx
push   %rax
mov    $0xffffffff,%ebx
test   %edi,%edi
jle    117d <_readline+0x4d>
test   %rsi,%rsi
je     117d <_readline+0x4d>
mov    %edx,%ebp
test   %edx,%edx
je     117d <_readline+0x4d>
mov    %ebp,%edx
call   1040 <fd_read_line@plt>
test   %eax,%eax
js     1161 <_readline+0x31>
add    $0xffffffff,%ebp
cmp    %ebp,%eax
mov    $0xffffffff,%ebx
cmovb  %eax,%ebx
jmp    117d <_readline+0x4d>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe8a(%rip),%rdi
mov    %eax,%esi
call   1030 <cerebro_err_output@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"fd_read_line: %s\00"
@errno = global i32 0
define i32 @_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
%9 = load i32, i32* %5
%10 = icmp sle i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %14, %11, %3
store i32 -1, i32* %4
br label %38
18: ; preds = %14
%19 = load i32, i32* %5
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = call i32 @fd_read_line(i32 %19, i8* %20, i32 %21)
store i32 %22, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %18
br label %25
25: ; preds = %24
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 @cerebro_err_output(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25
store i32 -1, i32* %4
br label %38
30: ; preds = %18
%31 = load i32, i32* %8
%32 = load i32, i32* %7
%33 = sub i32 %32, 1
%34 = icmp uge i32 %31, %33
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 -1, i32* %4
br label %38
36: ; preds = %30
%37 = load i32, i32* %8
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %35, %29, %17
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@.str = [17 x i8] c"fd_read_line: %s\00"
@errno = global i32 0
define i32 @_readline(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %0, 0
%5 = icmp ne i8* %1, null
%6 = and i1 %4, %5
%7 = icmp ne i32 %2, 0
%8 = and i1 %6, %7
br i1 %8, label %9, label %20
9: ; preds = %3
%10 = tail call i32 @fd_read_line(i32 %0, i8* %1, i32 %2)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @cerebro_err_output(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %20
16: ; preds = %9
%17 = add i32 %2, -1
%18 = icmp ult i32 %10, %17
%19 = select i1 %18, i32 %10, i32 -1
br label %20
20: ; preds = %16, %3, %12
%21 = phi i32 [ -1, %12 ], [ -1, %3 ], [ %19, %16 ]
ret i32 %21
}