void get_utf8_sequence(const char *utf8, unsigned char *seq_length,
                       unsigned char *first_mask) {
  unsigned char c = *(const unsigned char *)utf8;
  unsigned char m;
  unsigned char n;
  if ((c & 0x80) == 0) {
    n = 1;
    m = 0x7F;
  } else if ((c & 0xE0) == 0xC0) {
    n = 2;
    m = 0x1F;
  } else if ((c & 0xF0) == 0xE0) {
    n = 3;
    m = 0x0F;
  } else if ((c & 0xF8) == 0xF0) {
    n = 4;
    m = 0x07;
  } else if ((c & 0xFC) == 0xF8) {
    n = 5;
    m = 0x03;
  } else if ((c & 0xFE) == 0xFC) {
    n = 6;
    m = 0x01;
  } else {
    n = 0;
    m = 0;
  }
  *seq_length = n;
  *first_mask = m;
}


x86 assembly (clang, -O0):
<get_utf8_sequence>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1138 <get_utf8_sequence+0x38>
movb   $0x1,-0x1b(%rbp)
movb   $0x7f,-0x1a(%rbp)
jmp    11fe <get_utf8_sequence+0xfe>
movzbl -0x19(%rbp),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    1159 <get_utf8_sequence+0x59>
movb   $0x2,-0x1b(%rbp)
movb   $0x1f,-0x1a(%rbp)
jmp    11f9 <get_utf8_sequence+0xf9>
movzbl -0x19(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    117a <get_utf8_sequence+0x7a>
movb   $0x3,-0x1b(%rbp)
movb   $0xf,-0x1a(%rbp)
jmp    11f4 <get_utf8_sequence+0xf4>
movzbl -0x19(%rbp),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    119b <get_utf8_sequence+0x9b>
movb   $0x4,-0x1b(%rbp)
movb   $0x7,-0x1a(%rbp)
jmp    11ef <get_utf8_sequence+0xef>
movzbl -0x19(%rbp),%eax
and    $0xfc,%eax
cmp    $0xf8,%eax
jne    11bc <get_utf8_sequence+0xbc>
movb   $0x5,-0x1b(%rbp)
movb   $0x3,-0x1a(%rbp)
jmp    11ea <get_utf8_sequence+0xea>
movzbl -0x19(%rbp),%eax
and    $0xfe,%eax
cmp    $0xfc,%eax
jne    11dd <get_utf8_sequence+0xdd>
movb   $0x6,-0x1b(%rbp)
movb   $0x1,-0x1a(%rbp)
jmp    11e5 <get_utf8_sequence+0xe5>
movb   $0x0,-0x1b(%rbp)
movb   $0x0,-0x1a(%rbp)
jmp    11ea <get_utf8_sequence+0xea>
jmp    11ef <get_utf8_sequence+0xef>
jmp    11f4 <get_utf8_sequence+0xf4>
jmp    11f9 <get_utf8_sequence+0xf9>
jmp    11fe <get_utf8_sequence+0xfe>
mov    -0x1b(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x1a(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_utf8_sequence>:
movzbl (%rdi),%eax
test   $0x80,%al
jne    110d <get_utf8_sequence+0xd>
mov    $0x1,%al
mov    $0x7f,%cl
jmp    116e <get_utf8_sequence+0x6e>
mov    %eax,%ecx
and    $0xffffffe0,%ecx
cmp    $0xc0,%ecx
jne    1120 <get_utf8_sequence+0x20>
mov    $0x2,%al
mov    $0x1f,%cl
jmp    116e <get_utf8_sequence+0x6e>
mov    %eax,%ecx
and    $0xfffffff0,%ecx
cmp    $0xe0,%ecx
jne    1133 <get_utf8_sequence+0x33>
mov    $0x3,%al
mov    $0xf,%cl
jmp    116e <get_utf8_sequence+0x6e>
mov    %eax,%ecx
and    $0xfffffff8,%ecx
cmp    $0xf0,%ecx
jne    1146 <get_utf8_sequence+0x46>
mov    $0x4,%al
mov    $0x7,%cl
jmp    116e <get_utf8_sequence+0x6e>
mov    %eax,%ecx
and    $0xfffffffc,%ecx
cmp    $0xf8,%ecx
jne    1159 <get_utf8_sequence+0x59>
mov    $0x5,%al
mov    $0x3,%cl
jmp    116e <get_utf8_sequence+0x6e>
and    $0xfffffffe,%eax
xor    %edi,%edi
cmp    $0xfc,%eax
sete   %cl
mov    $0x6,%eax
cmovne %edi,%eax
mov    %al,(%rsi)
mov    %cl,(%rdx)
ret

LLVM IR (-O0):
define void @get_utf8_sequence(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
store i8 %11, i8* %7
%12 = load i8, i8* %7
%13 = zext i8 %12 to i32
%14 = and i32 %13, 128
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i8 1, i8* %9
store i8 127, i8* %8
br label %53
17: ; preds = %3
%18 = load i8, i8* %7
%19 = zext i8 %18 to i32
%20 = and i32 %19, 224
%21 = icmp eq i32 %20, 192
br i1 %21, label %22, label %23
22: ; preds = %17
store i8 2, i8* %9
store i8 31, i8* %8
br label %52
23: ; preds = %17
%24 = load i8, i8* %7
%25 = zext i8 %24 to i32
%26 = and i32 %25, 240
%27 = icmp eq i32 %26, 224
br i1 %27, label %28, label %29
28: ; preds = %23
store i8 3, i8* %9
store i8 15, i8* %8
br label %51
29: ; preds = %23
%30 = load i8, i8* %7
%31 = zext i8 %30 to i32
%32 = and i32 %31, 248
%33 = icmp eq i32 %32, 240
br i1 %33, label %34, label %35
34: ; preds = %29
store i8 4, i8* %9
store i8 7, i8* %8
br label %50
35: ; preds = %29
%36 = load i8, i8* %7
%37 = zext i8 %36 to i32
%38 = and i32 %37, 252
%39 = icmp eq i32 %38, 248
br i1 %39, label %40, label %41
40: ; preds = %35
store i8 5, i8* %9
store i8 3, i8* %8
br label %49
41: ; preds = %35
%42 = load i8, i8* %7
%43 = zext i8 %42 to i32
%44 = and i32 %43, 254
%45 = icmp eq i32 %44, 252
br i1 %45, label %46, label %47
46: ; preds = %41
store i8 6, i8* %9
store i8 1, i8* %8
br label %48
47: ; preds = %41
store i8 0, i8* %9
store i8 0, i8* %8
br label %48
48: ; preds = %47, %46
br label %49
49: ; preds = %48, %40
br label %50
50: ; preds = %49, %34
br label %51
51: ; preds = %50, %28
br label %52
52: ; preds = %51, %22
br label %53
53: ; preds = %52, %16
%54 = load i8, i8* %9
%55 = load i8*, i8** %5
store i8 %54, i8* %55
%56 = load i8, i8* %8
%57 = load i8*, i8** %6
store i8 %56, i8* %57
ret void
}

LLVM IR (-O2):
define void @get_utf8_sequence(i8* nocapture %0, i8* nocapture writeonly %1, i8* nocapture writeonly %2) {
%4 = load i8, i8* %0
%5 = zext i8 %4 to i32
%6 = and i32 %5, 128
%7 = icmp eq i32 %6, 0
br i1 %7, label %25, label %8
8: ; preds = %3
%9 = and i32 %5, 224
%10 = icmp eq i32 %9, 192
br i1 %10, label %25, label %11
11: ; preds = %8
%12 = and i32 %5, 240
%13 = icmp eq i32 %12, 224
br i1 %13, label %25, label %14
14: ; preds = %11
%15 = and i32 %5, 248
%16 = icmp eq i32 %15, 240
br i1 %16, label %25, label %17
17: ; preds = %14
%18 = and i32 %5, 252
%19 = icmp eq i32 %18, 248
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = and i32 %5, 254
%22 = icmp eq i32 %21, 252
%23 = zext i1 %22 to i8
%24 = select i1 %22, i8 6, i8 0
br label %25
25: ; preds = %20, %17, %14, %11, %8, %3
%26 = phi i8 [ 127, %3 ], [ 31, %8 ], [ 15, %11 ], [ 7, %14 ], [ 3, %17 ], [ %23, %20 ]
%27 = phi i8 [ 1, %3 ], [ 2, %8 ], [ 3, %11 ], [ 4, %14 ], [ 5, %17 ], [ %24, %20 ]
store i8 %27, i8* %1
store i8 %26, i8* %2
ret void
}