#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*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int next_character(char *buffer, int length) {
  int expected_character_length = 0, buffer_i = 0;
  unsigned char byte;
  while (buffer_i < length) {
    byte = (unsigned char)buffer[buffer_i];
    if (expected_character_length == 0) {
      if ((byte & 128) == 0) {
        expected_character_length = 1;
      } else if ((byte & 224) == 192) {
        expected_character_length = 2;
      } else if ((byte & 240) == 224) {
        expected_character_length = 3;
      } else if ((byte & 248) == 240) {
        expected_character_length = 4;
      } else if ((byte & 252) == 248) {
        expected_character_length = 5;
      } else if ((byte & 254) == 252) {
        expected_character_length = 6;
      } else {
        fprintf(stderr,
                "BCA: next_characters(\"%s\", %d): UTF-8 encoding error: "
                "byte value out of sequence: %xh\n",
                buffer, length, byte);
        return -1;
      }
    } else {
      if ((byte & 192) != 128) {
        fprintf(stderr,
                "BCA: next_character(): UTF-8 encoding error:"
                "byte value out of sequence: %xh (expected byte %d of a %d "
                "byte value)\n",
                byte, buffer_i + 1, expected_character_length);
        return -1;
      }
    }
    buffer_i++;
    if (buffer_i == expected_character_length)
      return buffer_i;
  }
  return -1;
}


x86 assembly (clang, -O0):
<next_character>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12cb <next_character+0x1bb>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x1d(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    125c <next_character+0x14c>
movzbl -0x1d(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    116f <next_character+0x5f>
movl   $0x1,-0x18(%rbp)
jmp    1257 <next_character+0x147>
movzbl -0x1d(%rbp),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    118f <next_character+0x7f>
movl   $0x2,-0x18(%rbp)
jmp    1252 <next_character+0x142>
movzbl -0x1d(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    11af <next_character+0x9f>
movl   $0x3,-0x18(%rbp)
jmp    124d <next_character+0x13d>
movzbl -0x1d(%rbp),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    11cf <next_character+0xbf>
movl   $0x4,-0x18(%rbp)
jmp    1248 <next_character+0x138>
movzbl -0x1d(%rbp),%eax
and    $0xfc,%eax
cmp    $0xf8,%eax
jne    11ef <next_character+0xdf>
movl   $0x5,-0x18(%rbp)
jmp    1243 <next_character+0x133>
movzbl -0x1d(%rbp),%eax
and    $0xfe,%eax
cmp    $0xfc,%eax
jne    120f <next_character+0xff>
movl   $0x6,-0x18(%rbp)
jmp    123e <next_character+0x12e>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
movzbl -0x1d(%rbp),%r8d
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <next_character+0x1c2>
jmp    1243 <next_character+0x133>
jmp    1248 <next_character+0x138>
jmp    124d <next_character+0x13d>
jmp    1252 <next_character+0x142>
jmp    1257 <next_character+0x147>
jmp    12a6 <next_character+0x196>
movzbl -0x1d(%rbp),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
je     12a1 <next_character+0x191>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
movzbl -0x1d(%rbp),%edx
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%r8d
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <next_character+0x1c2>
jmp    12a6 <next_character+0x196>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    12c6 <next_character+0x1b6>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d2 <next_character+0x1c2>
jmp    112d <next_character+0x1d>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_character>:
push   %rbx
mov    %esi,%r9d
mov    %rdi,%r10
xor    %ecx,%ecx
test   %esi,%esi
mov    $0x0,%esi
cmovg  %r9d,%esi
xor    %r8d,%r8d
mov    $0xffffffff,%ebx
jmp    114e <next_character+0x3e>
xchg   %ax,%ax
mov    %eax,%edx
and    $0xc0,%dl
cmp    $0x80,%dl
jne    11f4 <next_character+0xe4>
add    $0x1,%rcx
mov    %r8d,%eax
cmp    %rax,%rcx
je     11f0 <next_character+0xe0>
cmp    %rcx,%rsi
je     1210 <next_character+0x100>
movzbl (%r10,%rcx,1),%eax
test   %r8d,%r8d
jne    1130 <next_character+0x20>
mov    $0x1,%r8d
test   $0x80,%al
je     113e <next_character+0x2e>
mov    %eax,%edi
and    $0xffffffe0,%edi
mov    $0x2,%r8d
cmp    $0xc0,%edi
je     113e <next_character+0x2e>
mov    %eax,%edi
and    $0xfffffff0,%edi
mov    $0x3,%r8d
cmp    $0xe0,%edi
je     113e <next_character+0x2e>
mov    %eax,%edi
and    $0xfffffff8,%edi
mov    $0x4,%r8d
cmp    $0xf0,%edi
je     113e <next_character+0x2e>
mov    %eax,%edi
and    $0xfffffffc,%edi
mov    $0x5,%r8d
cmp    $0xf8,%edi
je     113e <next_character+0x2e>
mov    %eax,%edi
and    $0xfffffffe,%edi
mov    $0x6,%r8d
cmp    $0xfc,%edi
je     113e <next_character+0x2e>
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%edi
lea    0xe22(%rip),%rsi
mov    %r10,%rdx
mov    %r9d,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    1210 <next_character+0x100>
mov    %ecx,%ebx
jmp    1210 <next_character+0x100>
mov    0x2de5(%rip),%rdx
mov    (%rdx),%edi
add    $0x1,%ecx
lea    0xe50(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [87 x i8] c"BCA: next_characters(\22%s\22, %d): UTF-8 encoding error: byte value out of sequence: %xh\0A\00"
@.str.1 = [115 x i8] c"BCA: next_character(): UTF-8 encoding error:byte value out of sequence: %xh (expected byte %d of a %d byte value)\0A\00"
define i32 @next_character(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %92, %2
%10 = load i32, i32* %7
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %93
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
store i8 %18, i8* %8
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %70
21: ; preds = %13
%22 = load i8, i8* %8
%23 = zext i8 %22 to i32
%24 = and i32 %23, 128
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 1, i32* %6
br label %69
27: ; preds = %21
%28 = load i8, i8* %8
%29 = zext i8 %28 to i32
%30 = and i32 %29, 224
%31 = icmp eq i32 %30, 192
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 2, i32* %6
br label %68
33: ; preds = %27
%34 = load i8, i8* %8
%35 = zext i8 %34 to i32
%36 = and i32 %35, 240
%37 = icmp eq i32 %36, 224
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 3, i32* %6
br label %67
39: ; preds = %33
%40 = load i8, i8* %8
%41 = zext i8 %40 to i32
%42 = and i32 %41, 248
%43 = icmp eq i32 %42, 240
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 4, i32* %6
br label %66
45: ; preds = %39
%46 = load i8, i8* %8
%47 = zext i8 %46 to i32
%48 = and i32 %47, 252
%49 = icmp eq i32 %48, 248
br i1 %49, label %50, label %51
50: ; preds = %45
store i32 5, i32* %6
br label %65
51: ; preds = %45
%52 = load i8, i8* %8
%53 = zext i8 %52 to i32
%54 = and i32 %53, 254
%55 = icmp eq i32 %54, 252
br i1 %55, label %56, label %57
56: ; preds = %51
store i32 6, i32* %6
br label %64
57: ; preds = %51
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %4
%60 = load i32, i32* %5
%61 = load i8, i8* %8
%62 = zext i8 %61 to i32
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str, i64 0, i64 0), i8* %59, i32 %60, i32 %62)
store i32 -1, i32* %3
br label %94
64: ; preds = %56
br label %65
65: ; preds = %64, %50
br label %66
66: ; preds = %65, %44
br label %67
67: ; preds = %66, %38
br label %68
68: ; preds = %67, %32
br label %69
69: ; preds = %68, %26
br label %84
70: ; preds = %13
%71 = load i8, i8* %8
%72 = zext i8 %71 to i32
%73 = and i32 %72, 192
%74 = icmp ne i32 %73, 128
br i1 %74, label %75, label %83
75: ; preds = %70
%76 = load i32, i32* @stderr
%77 = load i8, i8* %8
%78 = zext i8 %77 to i32
%79 = load i32, i32* %7
%80 = add nsw i32 %79, 1
%81 = load i32, i32* %6
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([115 x i8], [115 x i8]* @.str.1, i64 0, i64 0), i32 %78, i32 %80, i32 %81)
store i32 -1, i32* %3
br label %94
83: ; preds = %70
br label %84
84: ; preds = %83, %69
%85 = load i32, i32* %7
%86 = add nsw i32 %85, 1
store i32 %86, i32* %7
%87 = load i32, i32* %7
%88 = load i32, i32* %6
%89 = icmp eq i32 %87, %88
br i1 %89, label %90, label %92
90: ; preds = %84
%91 = load i32, i32* %7
store i32 %91, i32* %3
br label %94
92: ; preds = %84
br label %9
93: ; preds = %9
store i32 -1, i32* %3
br label %94
94: ; preds = %93, %90, %75, %57
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [87 x i8] c"BCA: next_characters(\22%s\22, %d): UTF-8 encoding error: byte value out of sequence: %xh\0A\00"
@.str.1 = [115 x i8] c"BCA: next_character(): UTF-8 encoding error:byte value out of sequence: %xh (expected byte %d of a %d byte value)\0A\00"
define i32 @next_character(i8* %0, i32 %1) {
%3 = call i32 @llvm.smax.i32(i32 %1, i32 0)
%4 = zext i32 %3 to i64
br label %5
5: ; preds = %45, %2
%6 = phi i64 [ %47, %45 ], [ 0, %2 ]
%7 = phi i32 [ %46, %45 ], [ 0, %2 ]
%8 = icmp eq i64 %6, %4
br i1 %8, label %52, label %9
9: ; preds = %5
%10 = getelementptr inbounds i8, i8* %0, i64 %6
%11 = load i8, i8* %10
%12 = icmp eq i32 %7, 0
br i1 %12, label %13, label %36
13: ; preds = %9
%14 = zext i8 %11 to i32
%15 = and i32 %14, 128
%16 = icmp eq i32 %15, 0
br i1 %16, label %45, label %17
17: ; preds = %13
%18 = and i32 %14, 224
%19 = icmp eq i32 %18, 192
br i1 %19, label %45, label %20
20: ; preds = %17
%21 = and i32 %14, 240
%22 = icmp eq i32 %21, 224
br i1 %22, label %45, label %23
23: ; preds = %20
%24 = and i32 %14, 248
%25 = icmp eq i32 %24, 240
br i1 %25, label %45, label %26
26: ; preds = %23
%27 = and i32 %14, 252
%28 = icmp eq i32 %27, 248
br i1 %28, label %45, label %29
29: ; preds = %26
%30 = and i32 %14, 254
%31 = icmp eq i32 %30, 252
br i1 %31, label %45, label %32
32: ; preds = %29
%33 = zext i8 %11 to i32
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1, i32 %33)
br label %52
36: ; preds = %9
%37 = and i8 %11, -64
%38 = icmp eq i8 %37, -128
br i1 %38, label %45, label %39
39: ; preds = %36
%40 = trunc i64 %6 to i32
%41 = zext i8 %11 to i32
%42 = load i32, i32* @stderr
%43 = add nuw nsw i32 %40, 1
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([115 x i8], [115 x i8]* @.str.1, i64 0, i64 0), i32 %41, i32 %43, i32 %7)
br label %52
45: ; preds = %29, %26, %23, %20, %17, %13, %36
%46 = phi i32 [ %7, %36 ], [ 1, %13 ], [ 2, %17 ], [ 3, %20 ], [ 4, %23 ], [ 5, %26 ], [ 6, %29 ]
%47 = add nuw nsw i64 %6, 1
%48 = zext i32 %46 to i64
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %5
50: ; preds = %45
%51 = trunc i64 %47 to i32
br label %52
52: ; preds = %5, %50, %39, %32
%53 = phi i32 [ -1, %32 ], [ -1, %39 ], [ %51, %50 ], [ -1, %5 ]
ret i32 %53
}