#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 /*<<< orphan*/, int, char *);
int readline(char *buffer, int len) {
  int read_bytes = read(0, len, buffer);
  if (read_bytes < 0 || read_bytes >= len)
    return read_bytes;
  buffer[read_bytes] = '\0';
  if (read_bytes >= 1) {
    buffer[read_bytes - 1] = '\0';
  }
  return read_bytes - 1;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1146 <readline+0x36>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1151 <readline+0x41>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1181 <readline+0x71>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x1,-0x18(%rbp)
jl     1178 <readline+0x68>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
sub    $0x1,%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    %esi,%ebp
mov    %rdi,%rbx
xor    %edi,%edi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %eax,%eax
js     1142 <readline+0x32>
cmp    %ebp,%eax
jge    1142 <readline+0x32>
mov    %eax,%ecx
movb   $0x0,(%rbx,%rcx,1)
test   %eax,%eax
je     113d <readline+0x2d>
add    $0xffffffff,%eax
movb   $0x0,(%rbx,%rax,1)
jmp    1142 <readline+0x32>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @readline(i8* %0, i32 %1) {
%3 = tail call i32 @read(i32 0, i32 %1, i8* %0)
%4 = icmp sgt i32 %3, -1
%5 = icmp slt i32 %3, %1
%6 = and i1 %4, %5
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = zext i32 %3 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 0, i8* %9
%10 = icmp eq i32 %3, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = add nsw i32 %3, -1
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %7, %11, %2
%16 = phi i32 [ %3, %2 ], [ %12, %11 ], [ -1, %7 ]
ret i32 %16
}