#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 */
typedef int uint8_t;
typedef int const uint32_t;

/* Variables and functions */
int utf8_valid(int const *);
uint32_t utf8_to_32(const uint8_t *c) {
  switch (utf8_valid(c)) {
  case 0:
    return 0;
  case 1:
    return *c;
  case 2:
    return ((c[0] & 0x1f) << 6) | (c[1] & 0x3f);
  case 3:
    return ((c[0] & 0x0f) << 12) | ((c[1] & 0x3f) << 6) | (c[2] & 0x3f);
  case 4:
    return ((c[0] & 0x07) << 18) | ((c[1] & 0x3f) << 12) |
           ((c[2] & 0x3f) << 6) | (c[3] & 0x3f);
  }
  return 0;
}


x86 assembly (clang, -O0):
<utf8_to_32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <utf8_valid@plt>
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x4,%eax
ja     11ef <utf8_to_32+0xdf>
mov    -0x18(%rbp),%rax
lea    0xec1(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x0,-0x4(%rbp)
jmp    11f6 <utf8_to_32+0xe6>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <utf8_to_32+0xe6>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
and    $0x1f,%eax
shl    $0x6,%eax
mov    -0x10(%rbp),%rcx
mov    0x4(%rcx),%ecx
and    $0x3f,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <utf8_to_32+0xe6>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
and    $0xf,%eax
shl    $0xc,%eax
mov    -0x10(%rbp),%rcx
mov    0x4(%rcx),%ecx
and    $0x3f,%ecx
shl    $0x6,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%ecx
and    $0x3f,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <utf8_to_32+0xe6>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
and    $0x7,%eax
shl    $0x12,%eax
mov    -0x10(%rbp),%rcx
mov    0x4(%rcx),%ecx
and    $0x3f,%ecx
shl    $0xc,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%ecx
and    $0x3f,%ecx
shl    $0x6,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%rcx
mov    0xc(%rcx),%ecx
and    $0x3f,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <utf8_to_32+0xe6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_to_32>:
push   %rbx
mov    %rdi,%rbx
call   1030 <utf8_valid@plt>
add    $0xffffffff,%eax
cmp    $0x3,%eax
ja     1135 <utf8_to_32+0x25>
lea    0xed8(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    (%rbx),%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret
mov    (%rbx),%ecx
mov    0x4(%rbx),%eax
and    $0x1f,%ecx
shl    $0x6,%ecx
jmp    115c <utf8_to_32+0x4c>
mov    (%rbx),%eax
mov    0x4(%rbx),%ecx
shl    $0xc,%eax
movzwl %ax,%eax
and    $0x3f,%ecx
shl    $0x6,%ecx
or     %eax,%ecx
mov    0x8(%rbx),%eax
and    $0x3f,%eax
or     %ecx,%eax
pop    %rbx
ret
mov    (%rbx),%eax
mov    0x4(%rbx),%ecx
and    $0x7,%eax
shl    $0x12,%eax
and    $0x3f,%ecx
shl    $0xc,%ecx
or     %eax,%ecx
mov    0x8(%rbx),%edx
and    $0x3f,%edx
shl    $0x6,%edx
or     %ecx,%edx
mov    0xc(%rbx),%eax
and    $0x3f,%eax
or     %edx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @utf8_to_32(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
store i32* %0, i32** %3
%4 = load i32*, i32** %3
%5 = call i32 @utf8_valid(i32* %4)
switch i32 %5, label %61 [
i32 0, label %6
i32 1, label %7
i32 2, label %10
i32 3, label %21
i32 4, label %38
]
6: ; preds = %1
store i32 0, i32* %2
br label %62
7: ; preds = %1
%8 = load i32*, i32** %3
%9 = load i32, i32* %8
store i32 %9, i32* %2
br label %62
10: ; preds = %1
%11 = load i32*, i32** %3
%12 = getelementptr inbounds i32, i32* %11, i64 0
%13 = load i32, i32* %12
%14 = and i32 %13, 31
%15 = shl i32 %14, 6
%16 = load i32*, i32** %3
%17 = getelementptr inbounds i32, i32* %16, i64 1
%18 = load i32, i32* %17
%19 = and i32 %18, 63
%20 = or i32 %15, %19
store i32 %20, i32* %2
br label %62
21: ; preds = %1
%22 = load i32*, i32** %3
%23 = getelementptr inbounds i32, i32* %22, i64 0
%24 = load i32, i32* %23
%25 = and i32 %24, 15
%26 = shl i32 %25, 12
%27 = load i32*, i32** %3
%28 = getelementptr inbounds i32, i32* %27, i64 1
%29 = load i32, i32* %28
%30 = and i32 %29, 63
%31 = shl i32 %30, 6
%32 = or i32 %26, %31
%33 = load i32*, i32** %3
%34 = getelementptr inbounds i32, i32* %33, i64 2
%35 = load i32, i32* %34
%36 = and i32 %35, 63
%37 = or i32 %32, %36
store i32 %37, i32* %2
br label %62
38: ; preds = %1
%39 = load i32*, i32** %3
%40 = getelementptr inbounds i32, i32* %39, i64 0
%41 = load i32, i32* %40
%42 = and i32 %41, 7
%43 = shl i32 %42, 18
%44 = load i32*, i32** %3
%45 = getelementptr inbounds i32, i32* %44, i64 1
%46 = load i32, i32* %45
%47 = and i32 %46, 63
%48 = shl i32 %47, 12
%49 = or i32 %43, %48
%50 = load i32*, i32** %3
%51 = getelementptr inbounds i32, i32* %50, i64 2
%52 = load i32, i32* %51
%53 = and i32 %52, 63
%54 = shl i32 %53, 6
%55 = or i32 %49, %54
%56 = load i32*, i32** %3
%57 = getelementptr inbounds i32, i32* %56, i64 3
%58 = load i32, i32* %57
%59 = and i32 %58, 63
%60 = or i32 %55, %59
store i32 %60, i32* %2
br label %62
61: ; preds = %1
store i32 0, i32* %2
br label %62
62: ; preds = %61, %38, %21, %10, %7, %6
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
define i32 @utf8_to_32(i32* %0) {
%2 = tail call i32 @utf8_valid(i32* %0)
switch i32 %2, label %44 [
i32 4, label %26
i32 1, label %3
i32 2, label %5
i32 3, label %13
]
3: ; preds = %1
%4 = load i32, i32* %0
br label %44
5: ; preds = %1
%6 = load i32, i32* %0
%7 = shl i32 %6, 6
%8 = and i32 %7, 1984
%9 = getelementptr inbounds i32, i32* %0, i64 1
%10 = load i32, i32* %9
%11 = and i32 %10, 63
%12 = or i32 %8, %11
br label %44
13: ; preds = %1
%14 = load i32, i32* %0
%15 = shl i32 %14, 12
%16 = and i32 %15, 61440
%17 = getelementptr inbounds i32, i32* %0, i64 1
%18 = load i32, i32* %17
%19 = shl i32 %18, 6
%20 = and i32 %19, 4032
%21 = or i32 %20, %16
%22 = getelementptr inbounds i32, i32* %0, i64 2
%23 = load i32, i32* %22
%24 = and i32 %23, 63
%25 = or i32 %21, %24
br label %44
26: ; preds = %1
%27 = load i32, i32* %0
%28 = shl i32 %27, 18
%29 = and i32 %28, 1835008
%30 = getelementptr inbounds i32, i32* %0, i64 1
%31 = load i32, i32* %30
%32 = shl i32 %31, 12
%33 = and i32 %32, 258048
%34 = or i32 %33, %29
%35 = getelementptr inbounds i32, i32* %0, i64 2
%36 = load i32, i32* %35
%37 = shl i32 %36, 6
%38 = and i32 %37, 4032
%39 = or i32 %34, %38
%40 = getelementptr inbounds i32, i32* %0, i64 3
%41 = load i32, i32* %40
%42 = and i32 %41, 63
%43 = or i32 %39, %42
br label %44
44: ; preds = %1, %26, %13, %5, %3
%45 = phi i32 [ %43, %26 ], [ %25, %13 ], [ %12, %5 ], [ %4, %3 ], [ 0, %1 ]
ret i32 %45
}