#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 utf8_enc_len(unsigned char);
int utf8_decode(unsigned char *str) {
  int len, ucs4 = 0;
  static int mask[6] = {0x7f, 0x1f, 0xf, 7, 3, 1};
  len = utf8_enc_len(*str);
  if (!len)
    return 0;
  ucs4 = *str++ & mask[len - 1];
  while (--len) {
    if ((*str >> 6) != 2)
      return 0;
    ucs4 <<= 6;
    ucs4 += *str++ & 0x3f;
  }
  return ucs4;
}


x86 assembly (clang, -O0):
<utf8_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1030 <utf8_enc_len@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1148 <utf8_decode+0x38>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <utf8_decode+0xc0>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movzbl (%rax),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rdx
lea    0x2ec6(%rip),%rcx
and    (%rcx,%rdx,4),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     11ca <utf8_decode+0xba>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
sar    $0x6,%eax
cmp    $0x2,%eax
je     11a1 <utf8_decode+0x91>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <utf8_decode+0xc0>
mov    -0x18(%rbp),%eax
shl    $0x6,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movzbl (%rax),%eax
and    $0x3f,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1170 <utf8_decode+0x60>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_decode>:
push   %rbx
mov    %rdi,%rbx
movzbl (%rdi),%edi
call   1030 <utf8_enc_len@plt>
test   %eax,%eax
je     1163 <utf8_decode+0x53>
mov    %eax,%ecx
movzbl (%rbx),%eax
movslq %ecx,%rdx
lea    0xed1(%rip),%rsi
and    -0x4(%rsi,%rdx,4),%eax
cmp    $0x1,%ecx
je     1161 <utf8_decode+0x51>
mov    %ecx,%ecx
mov    $0x1,%edx
nop
movzbl (%rbx,%rdx,1),%esi
mov    %esi,%edi
and    $0xffffffc0,%edi
cmp    $0x80,%edi
jne    1163 <utf8_decode+0x53>
shl    $0x6,%eax
and    $0x3f,%esi
or     %esi,%eax
add    $0x1,%rdx
cmp    %edx,%ecx
jne    1140 <utf8_decode+0x30>
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@utf8_decode.mask = internal global [6 x i32] [i32 127, i32 31, i32 15, i32 7, i32 3, i32 1]
define i32 @utf8_decode(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
%6 = load i8*, i8** %3
%7 = load i8, i8* %6
%8 = call i32 @utf8_enc_len(i8 zeroext %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %1
store i32 0, i32* %2
br label %46
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %3
%15 = load i8, i8* %13
%16 = zext i8 %15 to i32
%17 = load i32, i32* %4
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [6 x i32], [6 x i32]* @utf8_decode.mask, i64 0, i64 %19
%21 = load i32, i32* %20
%22 = and i32 %16, %21
store i32 %22, i32* %5
br label %23
23: ; preds = %34, %12
%24 = load i32, i32* %4
%25 = add nsw i32 %24, -1
store i32 %25, i32* %4
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = ashr i32 %30, 6
%32 = icmp ne i32 %31, 2
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 0, i32* %2
br label %46
34: ; preds = %27
%35 = load i32, i32* %5
%36 = shl i32 %35, 6
store i32 %36, i32* %5
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %3
%39 = load i8, i8* %37
%40 = zext i8 %39 to i32
%41 = and i32 %40, 63
%42 = load i32, i32* %5
%43 = add nsw i32 %42, %41
store i32 %43, i32* %5
br label %23
44: ; preds = %23
%45 = load i32, i32* %5
store i32 %45, i32* %2
br label %46
46: ; preds = %44, %33, %11
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@utf8_decode.mask = internal unnamed_addr constant [6 x i32] [i32 127, i32 31, i32 15, i32 7, i32 3, i32 1]
define i32 @utf8_decode(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = tail call i32 @utf8_enc_len(i8 zeroext %2)
%4 = icmp eq i32 %3, 0
br i1 %4, label %29, label %5
5: ; preds = %1
%6 = load i8, i8* %0
%7 = zext i8 %6 to i32
%8 = add nsw i32 %3, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [6 x i32], [6 x i32]* @utf8_decode.mask, i64 0, i64 %9
%11 = load i32, i32* %10
%12 = and i32 %11, %7
%13 = icmp eq i32 %8, 0
br i1 %13, label %29, label %14
14: ; preds = %5, %23
%15 = phi i32 [ %27, %23 ], [ %8, %5 ]
%16 = phi i8* [ %18, %23 ], [ %0, %5 ]
%17 = phi i32 [ %26, %23 ], [ %12, %5 ]
%18 = getelementptr inbounds i8, i8* %16, i64 1
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = and i32 %20, 192
%22 = icmp eq i32 %21, 128
br i1 %22, label %23, label %29
23: ; preds = %14
%24 = shl i32 %17, 6
%25 = and i32 %20, 63
%26 = or i32 %25, %24
%27 = add nsw i32 %15, -1
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %14
29: ; preds = %14, %23, %5, %1
%30 = phi i32 [ 0, %1 ], [ %12, %5 ], [ 0, %14 ], [ %26, %23 ]
ret i32 %30
}