#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__ strlen(char *);
int util_utf8toutf16_len(unsigned char *utf8) {
  int len, out_len, trailing_bytes;
  unsigned char *src = utf8;
  len = (int)strlen((char *)utf8);
  out_len = 0;
  while (src < utf8 + len) {
    trailing_bytes = 0;
    if ((*src & 0xE0) == 0xC0)
      trailing_bytes = 1;
    else if ((*src & 0xF0) == 0xE0)
      trailing_bytes = 2;
    else if ((*src & 0xF8) == 0xF0)
      trailing_bytes = 3;
    if (src + trailing_bytes > utf8 + len)
      return -1;
    out_len += 2;
    if (trailing_bytes == 3)
      out_len += 2;
    src += (1 + trailing_bytes);
  }
  out_len += 1;
  return out_len;
}


x86 assembly (clang, -O0):
<util_utf8toutf16_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x14(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1224 <util_utf8toutf16_len+0x114>
movl   $0x0,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    1179 <util_utf8toutf16_len+0x69>
movl   $0x1,-0x1c(%rbp)
jmp    11c4 <util_utf8toutf16_len+0xb4>
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    119c <util_utf8toutf16_len+0x8c>
movl   $0x2,-0x1c(%rbp)
jmp    11bf <util_utf8toutf16_len+0xaf>
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    11ba <util_utf8toutf16_len+0xaa>
movl   $0x3,-0x1c(%rbp)
jmp    11bf <util_utf8toutf16_len+0xaf>
jmp    11c4 <util_utf8toutf16_len+0xb4>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    -0x10(%rbp),%rcx
movslq -0x14(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jbe    11ef <util_utf8toutf16_len+0xdf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <util_utf8toutf16_len+0x123>
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jne    120b <util_utf8toutf16_len+0xfb>
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1137 <util_utf8toutf16_len+0x27>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_utf8toutf16_len>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %rax,%rcx
shl    $0x20,%rcx
test   %rcx,%rcx
jle    11b6 <util_utf8toutf16_len+0xa6>
movslq %eax,%rcx
add    %rbx,%rcx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%rbx),%esi
mov    %esi,%edx
and    $0xffffffe0,%edx
cmp    $0xc0,%edx
jne    1160 <util_utf8toutf16_len+0x50>
mov    $0x1,%esi
xor    %edx,%edx
jmp    1191 <util_utf8toutf16_len+0x81>
nopl   0x0(%rax)
mov    %esi,%edx
and    $0xfffffff0,%edx
cmp    $0xe0,%edx
jne    1180 <util_utf8toutf16_len+0x70>
mov    $0x2,%esi
xor    %edx,%edx
jmp    1191 <util_utf8toutf16_len+0x81>
cs nopw 0x0(%rax,%rax,1)
and    $0xfffffff8,%esi
xor    %edx,%edx
cmp    $0xf0,%esi
sete   %dl
lea    (%rdx,%rdx,2),%esi
mov    %esi,%esi
lea    (%rbx,%rsi,1),%rdi
cmp    %rcx,%rdi
ja     11bd <util_utf8toutf16_len+0xad>
movzbl %dl,%edx
lea    (%rax,%rdx,2),%eax
add    $0x2,%eax
add    %rsi,%rbx
add    $0x1,%rbx
cmp    %rcx,%rbx
jb     1140 <util_utf8toutf16_len+0x30>
or     $0x1,%eax
pop    %rbx
ret
mov    $0x1,%eax
pop    %rbx
ret
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @util_utf8toutf16_len(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
store i8* %8, i8** %7
%9 = load i8*, i8** %3
%10 = call i64 @strlen(i8* %9)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %4
store i32 0, i32* %5
br label %12
12: ; preds = %61, %1
%13 = load i8*, i8** %7
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = icmp ult i8* %13, %17
br i1 %18, label %19, label %67
19: ; preds = %12
store i32 0, i32* %6
%20 = load i8*, i8** %7
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = and i32 %22, 224
%24 = icmp eq i32 %23, 192
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 1, i32* %6
br label %42
26: ; preds = %19
%27 = load i8*, i8** %7
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = and i32 %29, 240
%31 = icmp eq i32 %30, 224
br i1 %31, label %32, label %33
32: ; preds = %26
store i32 2, i32* %6
br label %41
33: ; preds = %26
%34 = load i8*, i8** %7
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = and i32 %36, 248
%38 = icmp eq i32 %37, 240
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 3, i32* %6
br label %40
40: ; preds = %39, %33
br label %41
41: ; preds = %40, %32
br label %42
42: ; preds = %41, %25
%43 = load i8*, i8** %7
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i8*, i8** %3
%48 = load i32, i32* %4
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = icmp ugt i8* %46, %50
br i1 %51, label %52, label %53
52: ; preds = %42
store i32 -1, i32* %2
br label %71
53: ; preds = %42
%54 = load i32, i32* %5
%55 = add nsw i32 %54, 2
store i32 %55, i32* %5
%56 = load i32, i32* %6
%57 = icmp eq i32 %56, 3
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 2
store i32 %60, i32* %5
br label %61
61: ; preds = %58, %53
%62 = load i32, i32* %6
%63 = add nsw i32 1, %62
%64 = load i8*, i8** %7
%65 = sext i32 %63 to i64
%66 = getelementptr inbounds i8, i8* %64, i64 %65
store i8* %66, i8** %7
br label %12
67: ; preds = %12
%68 = load i32, i32* %5
%69 = add nsw i32 %68, 1
store i32 %69, i32* %5
%70 = load i32, i32* %5
store i32 %70, i32* %2
br label %71
71: ; preds = %67, %52
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
define i32 @util_utf8toutf16_len(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = shl i64 %2, 32
%4 = ashr exact i64 %3, 32
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = icmp sgt i64 %3, 0
br i1 %6, label %7, label %36
7: ; preds = %1, %27
%8 = phi i8* [ %32, %27 ], [ %0, %1 ]
%9 = phi i32 [ %29, %27 ], [ 0, %1 ]
%10 = load i8, i8* %8
%11 = zext i8 %10 to i32
%12 = and i32 %11, 224
%13 = icmp eq i32 %12, 192
br i1 %13, label %21, label %14
14: ; preds = %7
%15 = and i32 %11, 240
%16 = icmp eq i32 %15, 224
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = and i32 %11, 248
%19 = icmp eq i32 %18, 240
%20 = select i1 %19, i32 3, i32 0
br label %21
21: ; preds = %17, %14, %7
%22 = phi i1 [ false, %7 ], [ false, %14 ], [ %19, %17 ]
%23 = phi i32 [ 1, %7 ], [ 2, %14 ], [ %20, %17 ]
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %8, i64 %24
%26 = icmp ugt i8* %25, %5
br i1 %26, label %36, label %27
27: ; preds = %21
%28 = select i1 %22, i32 4, i32 2
%29 = add nuw nsw i32 %28, %9
%30 = add nuw nsw i32 %23, 1
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %8, i64 %31
%33 = icmp ult i8* %32, %5
br i1 %33, label %7, label %34
34: ; preds = %27
%35 = or i32 %29, 1
br label %36
36: ; preds = %21, %1, %34
%37 = phi i32 [ 1, %1 ], [ %35, %34 ], [ -1, %21 ]
ret i32 %37
}