#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 */
unsigned int decode_utf8_char(unsigned char, char const **, char const *);
int get_length_mbs_utf8(int flags, const char *src, int srclen) {
  int ret = 0;
  unsigned int res;
  const char *srcend = src + srclen;
  while (src < srcend) {
    unsigned char ch = *src++;
    if (ch < 0x80) {
      ret++;
      continue;
    }
    if ((res = decode_utf8_char(ch, &src, srcend)) <= 0x10ffff) {
      if (res > 0xffff)
        ret++;
      ret++;
    } else if (flags & 0x00000008)
      return -2;
  }
  return ret;
}


x86 assembly (clang, -O0):
<get_length_mbs_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11e1 <get_length_mbs_utf8+0xd1>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x29(%rbp)
movzbl -0x29(%rbp),%eax
cmp    $0x80,%eax
jge    1177 <get_length_mbs_utf8+0x67>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1138 <get_length_mbs_utf8+0x28>
mov    -0x29(%rbp),%al
mov    -0x28(%rbp),%rdx
lea    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1030 <decode_utf8_char@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x10ffff,%eax
ja     11bc <get_length_mbs_utf8+0xac>
cmpl   $0xffff,-0x1c(%rbp)
jbe    11ae <get_length_mbs_utf8+0x9e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11dc <get_length_mbs_utf8+0xcc>
mov    -0x8(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     11d7 <get_length_mbs_utf8+0xc7>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e7 <get_length_mbs_utf8+0xd7>
jmp    11dc <get_length_mbs_utf8+0xcc>
jmp    1138 <get_length_mbs_utf8+0x28>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_length_mbs_utf8>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1170 <get_length_mbs_utf8+0x60>
mov    %edi,%r14d
movslq %edx,%rbp
add    %rsi,%rbp
xor    %ebx,%ebx
mov    %rsp,%r15
jmp    113f <get_length_mbs_utf8+0x2f>
cmp    $0x10000,%eax
sbb    $0xffffffff,%ebx
add    $0x1,%ebx
mov    (%rsp),%rsi
cmp    %rbp,%rsi
jae    1172 <get_length_mbs_utf8+0x62>
lea    0x1(%rsi),%rax
mov    %rax,(%rsp)
movzbl (%rsi),%eax
test   %al,%al
jns    1133 <get_length_mbs_utf8+0x23>
movzbl %al,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1030 <decode_utf8_char@plt>
cmp    $0x10ffff,%eax
jbe    112b <get_length_mbs_utf8+0x1b>
test   $0x8,%r14b
je     1136 <get_length_mbs_utf8+0x26>
mov    $0xfffffffe,%ebx
jmp    1172 <get_length_mbs_utf8+0x62>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @get_length_mbs_utf8(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8* %15, i8** %10
br label %16
16: ; preds = %50, %27, %3
%17 = load i8*, i8** %6
%18 = load i8*, i8** %10
%19 = icmp ult i8* %17, %18
br i1 %19, label %20, label %51
20: ; preds = %16
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %6
%23 = load i8, i8* %21
store i8 %23, i8* %11
%24 = load i8, i8* %11
%25 = zext i8 %24 to i32
%26 = icmp slt i32 %25, 128
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %16
30: ; preds = %20
%31 = load i8, i8* %11
%32 = load i8*, i8** %10
%33 = call i32 @decode_utf8_char(i8 zeroext %31, i8** %6, i8* %32)
store i32 %33, i32* %9
%34 = icmp ule i32 %33, 1114111
br i1 %34, label %35, label %44
35: ; preds = %30
%36 = load i32, i32* %9
%37 = icmp ugt i32 %36, 65535
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
br label %41
41: ; preds = %38, %35
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %50
44: ; preds = %30
%45 = load i32, i32* %5
%46 = and i32 %45, 8
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %44
store i32 -2, i32* %4
br label %53
49: ; preds = %44
br label %50
50: ; preds = %49, %41
br label %16
51: ; preds = %16
%52 = load i32, i32* %8
store i32 %52, i32* %4
br label %53
53: ; preds = %51, %48
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
define i32 @get_length_mbs_utf8(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = sext i32 %2 to i64
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %32
8: ; preds = %3
%9 = and i32 %0, 8
%10 = icmp eq i32 %9, 0
br label %11
11: ; preds = %8, %28
%12 = phi i8* [ %1, %8 ], [ %30, %28 ]
%13 = phi i32 [ 0, %8 ], [ %29, %28 ]
%14 = getelementptr inbounds i8, i8* %12, i64 1
store i8* %14, i8** %4
%15 = load i8, i8* %12
%16 = icmp sgt i8 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = add nsw i32 %13, 1
br label %28
19: ; preds = %11
%20 = call i32 @decode_utf8_char(i8 zeroext %15, i8** %4, i8* %6)
%21 = icmp ult i32 %20, 1114112
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = icmp ugt i32 %20, 65535
%24 = zext i1 %23 to i32
%25 = add i32 %13, 1
%26 = add i32 %25, %24
br label %28
27: ; preds = %19
br i1 %10, label %28, label %32
28: ; preds = %22, %27, %17
%29 = phi i32 [ %18, %17 ], [ %26, %22 ], [ %13, %27 ]
%30 = load i8*, i8** %4
%31 = icmp ult i8* %30, %6
br i1 %31, label %11, label %32
32: ; preds = %28, %27, %3
%33 = phi i32 [ 0, %3 ], [ -2, %27 ], [ %29, %28 ]
ret i32 %33
}