#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 */
typedef int ssize_t;

/* Variables and functions */
scalar_t__ errno;
int read(int const, char *, int);
int /*<<< orphan*/ *strchr(char *, char);
ssize_t ab_getline(int const fd, char *const buffer, size_t const len) {
  size_t i;
  ssize_t readed;
  if (len == 0 || buffer == NULL) {
    return (0);
  }
  for (i = 0, readed = read(fd, &buffer[i], 1); readed == 1;
       readed = read(fd, &buffer[i], 1)) {
    if (strchr("\r\n", buffer[i]) != NULL) {
      errno = 0;
      break;
    }
    ++i;
    if (i >= len) {
      break;
    }
  }
  buffer[i] = '\000';
  if (readed < 1) {
    return (-1);
  } else {
    return (ssize_t)(i);
  }
}


x86 assembly (clang, -O0):
<ab_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1149 <ab_getline+0x29>
cmpq   $0x0,-0x10(%rbp)
jne    1155 <ab_getline+0x35>
movl   $0x0,-0x4(%rbp)
jmp    121e <ab_getline+0xfe>
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    11f5 <ab_getline+0xd5>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
lea    0xe72(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11b4 <ab_getline+0x94>
mov    0x2e38(%rip),%rax
movq   $0x0,(%rax)
jmp    11f5 <ab_getline+0xd5>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11d3 <ab_getline+0xb3>
jmp    11f5 <ab_getline+0xd5>
jmp    11d8 <ab_getline+0xb8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
jmp    1175 <ab_getline+0x55>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x1,-0x24(%rbp)
jge    1217 <ab_getline+0xf7>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <ab_getline+0xfe>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ab_getline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %eax,%eax
test   %rsi,%rsi
je     11c6 <ab_getline+0xa6>
mov    %rdx,%r14
test   %rdx,%rdx
je     11c6 <ab_getline+0xa6>
mov    %rsi,%rbx
mov    %edi,%r12d
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    1197 <ab_getline+0x77>
mov    $0x1,%ebp
lea    0xea0(%rip),%r15
movsbl -0x1(%rbx,%rbp,1),%esi
mov    %r15,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
jne    11a9 <ab_getline+0x89>
cmp    %rbp,%r14
je     11be <ab_getline+0x9e>
lea    (%rbx,%rbp,1),%rsi
mov    %r12d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
add    $0x1,%rbp
cmp    $0x1,%eax
je     1160 <ab_getline+0x40>
add    $0xffffffffffffffff,%rbp
jmp    1199 <ab_getline+0x79>
xor    %ebp,%ebp
movb   $0x0,(%rbx,%rbp,1)
test   %eax,%eax
mov    $0xffffffff,%eax
cmovg  %ebp,%eax
jmp    11c6 <ab_getline+0xa6>
mov    0x2e30(%rip),%rax
movq   $0x0,(%rax)
add    $0xffffffffffffffff,%rbp
mov    %rbp,%r14
movb   $0x0,(%rbx,%r14,1)
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
@errno = global i64 0
define i32 @ab_getline(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
%11 = icmp eq i64 %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 0, i32* %4
br label %57
16: ; preds = %12
store i64 0, i64* %8
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i64, i64* %8
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = call i32 @read(i32 %17, i8* %20, i32 1)
store i32 %21, i32* %9
br label %22
22: ; preds = %41, %16
%23 = load i32, i32* %9
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %47
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = load i64, i64* %8
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i8, i8* %28
%30 = call i32* @strchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %29)
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %25
store i64 0, i64* @errno
br label %47
33: ; preds = %25
%34 = load i64, i64* %8
%35 = add i64 %34, 1
store i64 %35, i64* %8
%36 = load i64, i64* %8
%37 = load i64, i64* %7
%38 = icmp uge i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %33
br label %47
40: ; preds = %33
br label %41
41: ; preds = %40
%42 = load i32, i32* %5
%43 = load i8*, i8** %6
%44 = load i64, i64* %8
%45 = getelementptr inbounds i8, i8* %43, i64 %44
%46 = call i32 @read(i32 %42, i8* %45, i32 1)
store i32 %46, i32* %9
br label %22
47: ; preds = %39, %32, %22
%48 = load i8*, i8** %6
%49 = load i64, i64* %8
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 0, i8* %50
%51 = load i32, i32* %9
%52 = icmp slt i32 %51, 1
br i1 %52, label %53, label %54
53: ; preds = %47
store i32 -1, i32* %4
br label %57
54: ; preds = %47
%55 = load i64, i64* %8
%56 = trunc i64 %55 to i32
store i32 %56, i32* %4
br label %57
57: ; preds = %54, %53, %15
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
@errno = global i64 0
define i32 @ab_getline(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
%5 = icmp eq i8* %1, null
%6 = or i1 %5, %4
br i1 %6, label %37, label %7
7: ; preds = %3
%8 = tail call i32 @read(i32 %0, i8* %1, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %30
10: ; preds = %7
%11 = add i64 %2, -1
br label %12
12: ; preds = %10, %22
%13 = phi i64 [ %20, %22 ], [ 0, %10 ]
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = load i8, i8* %14
%16 = tail call i32* @strchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %15)
%17 = icmp eq i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %12
store i64 0, i64* @errno
br label %26
19: ; preds = %12
%20 = add nuw i64 %13, 1
%21 = icmp eq i64 %13, %11
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = getelementptr inbounds i8, i8* %1, i64 %20
%24 = tail call i32 @read(i32 %0, i8* %23, i32 1)
%25 = icmp eq i32 %24, 1
br i1 %25, label %12, label %30
26: ; preds = %19, %18
%27 = phi i64 [ %13, %18 ], [ %2, %19 ]
%28 = getelementptr inbounds i8, i8* %1, i64 %27
store i8 0, i8* %28
%29 = trunc i64 %27 to i32
br label %37
30: ; preds = %22, %7
%31 = phi i32 [ %8, %7 ], [ %24, %22 ]
%32 = phi i64 [ 0, %7 ], [ %20, %22 ]
%33 = getelementptr inbounds i8, i8* %1, i64 %32
store i8 0, i8* %33
%34 = icmp slt i32 %31, 1
%35 = trunc i64 %32 to i32
%36 = select i1 %34, i32 -1, i32 %35
br label %37
37: ; preds = %30, %26, %3
%38 = phi i32 [ 0, %3 ], [ %29, %26 ], [ %36, %30 ]
ret i32 %38
}