#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*/ assert(int);
int getLookAhead();
scalar_t__ isCharType(int, unsigned int);
int /*<<< orphan*/ match(int);
size_t readString(char *buffer, size_t len, unsigned int char_class) {
  char *ptr = buffer;
  while (ptr + 1 < buffer + len) {
    int c = getLookAhead();
    if (isCharType(c, char_class)) {
      *ptr++ = ((char)(c));
      match(c);
    } else
      break;
  }
  if (len > 0)
    *ptr = '\0';
  ;
  assert(ptr >= buffer);
  return ((size_t)(ptr - buffer));
}


x86 assembly (clang, -O0):
<readString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    -0x8(%rbp),%rcx
add    -0x10(%rbp),%rcx
cmp    %rcx,%rax
jae    11c0 <readString+0x80>
mov    $0x0,%al
call   1040 <getLookAhead@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <isCharType@plt>
cmp    $0x0,%rax
je     11b6 <readString+0x76>
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x24(%rbp),%edi
call   1060 <match@plt>
jmp    11bb <readString+0x7b>
jmp    11c0 <readString+0x80>
jmp    115b <readString+0x1b>
cmpq   $0x0,-0x10(%rbp)
jbe    11d2 <readString+0x92>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdi,%rbp
cmp    $0x2,%rsi
jl     11a2 <readString+0x62>
mov    %edx,%r12d
lea    (%r15,%r14,1),%r13
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <getLookAhead@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12d,%esi
call   1030 <isCharType@plt>
test   %rax,%rax
je     11cb <readString+0x8b>
mov    %bpl,(%rbx)
mov    %ebp,%edi
call   1060 <match@plt>
lea    0x1(%rbx),%rbp
add    $0x2,%rbx
cmp    %r13,%rbx
mov    %rbp,%rbx
jb     1170 <readString+0x30>
test   %r14,%r14
je     11ab <readString+0x6b>
movb   $0x0,0x0(%rbp)
xor    %edi,%edi
sub    %r15,%rbp
setae  %dil
call   1050 <assert@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rbp
test   %r14,%r14
jne    11a7 <readString+0x67>
jmp    11ab <readString+0x6b>

LLVM IR (-O0):
define i64 @readString(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
store i8* %9, i8** %7
br label %10
10: ; preds = %31, %3
%11 = load i8*, i8** %7
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = load i8*, i8** %4
%14 = load i64, i64* %5
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = icmp ult i8* %12, %15
br i1 %16, label %17, label %32
17: ; preds = %10
%18 = call i32 (...) @getLookAhead()
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = load i32, i32* %6
%21 = call i64 @isCharType(i32 %19, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %17
%24 = load i32, i32* %8
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %7
store i8 %25, i8* %26
%28 = load i32, i32* %8
%29 = call i32 @match(i32 %28)
br label %31
30: ; preds = %17
br label %32
31: ; preds = %23
br label %10
32: ; preds = %30, %10
%33 = load i64, i64* %5
%34 = icmp ugt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = load i8*, i8** %7
store i8 0, i8* %36
br label %37
37: ; preds = %35, %32
%38 = load i8*, i8** %7
%39 = load i8*, i8** %4
%40 = icmp uge i8* %38, %39
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = load i8*, i8** %7
%44 = load i8*, i8** %4
%45 = ptrtoint i8* %43 to i64
%46 = ptrtoint i8* %44 to i64
%47 = sub i64 %45, %46
ret i64 %47
}

LLVM IR (-O2):
define i64 @readString(i8* %0, i64 %1, i32 %2) {
%4 = getelementptr inbounds i8, i8* %0, i64 %1
%5 = icmp sgt i64 %1, 1
br i1 %5, label %6, label %19
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %0, i64 1
br label %8
8: ; preds = %6, %14
%9 = phi i8* [ %17, %14 ], [ %7, %6 ]
%10 = phi i8* [ %9, %14 ], [ %0, %6 ]
%11 = tail call i32 (...) @getLookAhead()
%12 = tail call i64 @isCharType(i32 %11, i32 %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %8
%15 = trunc i32 %11 to i8
store i8 %15, i8* %10
%16 = tail call i32 @match(i32 %11)
%17 = getelementptr inbounds i8, i8* %9, i64 1
%18 = icmp ult i8* %17, %4
br i1 %18, label %8, label %19
19: ; preds = %14, %8, %3
%20 = phi i8* [ %0, %3 ], [ %10, %8 ], [ %9, %14 ]
%21 = icmp eq i64 %1, 0
br i1 %21, label %23, label %22
22: ; preds = %19
store i8 0, i8* %20
br label %23
23: ; preds = %22, %19
%24 = icmp uge i8* %20, %0
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
%27 = ptrtoint i8* %20 to i64
%28 = ptrtoint i8* %0 to i64
%29 = sub i64 %27, %28
ret i64 %29
}