#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 */
scalar_t__ read(int /*<<< orphan*/, char *, int);
void readbuf(char *p, int len) {
  char *ep = p + len - 1;
  while (p < ep && read(0, p, 1)) {
    if (*p == '\n') {
      p++;
      break;
    }
    p++;
  }
  *p = 0;
}


x86 assembly (clang, -O0):
<readbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
cmp    -0x18(%rbp),%rcx
mov    %al,-0x19(%rbp)
jae    115f <readbuf+0x4f>
mov    -0x8(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    116f <readbuf+0x5f>
jmp    11a1 <readbuf+0x91>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1190 <readbuf+0x80>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11a1 <readbuf+0x91>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1132 <readbuf+0x22>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readbuf>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
movslq %esi,%r15
lea    (%rdi,%r15,1),%r14
add    $0xffffffffffffffff,%r14
cmp    %rdi,%r14
jbe    115c <readbuf+0x4c>
add    $0xffffffffffffffff,%r15
nopl   0x0(%rax)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
je     115c <readbuf+0x4c>
cmpb   $0xa,(%rbx)
je     1158 <readbuf+0x48>
add    $0x1,%rbx
add    $0xffffffffffffffff,%r15
jne    1130 <readbuf+0x20>
mov    %r14,%rbx
jmp    115c <readbuf+0x4c>
add    $0x1,%rbx
movb   $0x0,(%rbx)
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @readbuf(i8* %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = getelementptr i8, i8* %4, i64 -1
%6 = icmp ugt i8* %5, %0
br i1 %6, label %7, label %17
7: ; preds = %2, %15
%8 = phi i8* [ %14, %15 ], [ %0, %2 ]
%9 = tail call i64 @read(i32 0, i8* %8, i32 1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %7
%12 = load i8, i8* %8
%13 = icmp eq i8 %12, 10
%14 = getelementptr inbounds i8, i8* %8, i64 1
br i1 %13, label %17, label %15
15: ; preds = %11
%16 = icmp eq i8* %14, %5
br i1 %16, label %17, label %7
17: ; preds = %7, %15, %11, %2
%18 = phi i8* [ %0, %2 ], [ %8, %7 ], [ %5, %15 ], [ %14, %11 ]
store i8 0, i8* %18
ret void
}