#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 uint32_t;
typedef int result;

/* Variables and functions */
int fcitx_utf8_get_char_extended(char const *, int);
__attribute__((visibility("default"))) uint32_t
fcitx_utf8_get_char_validated(const char *p, int max_len) {
  uint32_t result;
  if (max_len == 0)
    return -2;
  result = fcitx_utf8_get_char_extended(p, max_len);
  if (result & 0x80000000)
    return result;
  else if (!((result) < 0x110000 && (((result)&0xFFFFF800) != 0xD800) &&
             ((result) < 0xFDD0 || (result) > 0xFDEF) &&
             ((result)&0xFFFE) != 0xFFFE))
    return -1;
  else
    return result;
}


x86 assembly (clang, -O0):
<fcitx_utf8_get_char_validated>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1135 <fcitx_utf8_get_char_validated+0x25>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11bf <fcitx_utf8_get_char_validated+0xaf>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <fcitx_utf8_get_char_extended@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0x80000000,%eax
cmp    $0x0,%eax
je     1160 <fcitx_utf8_get_char_validated+0x50>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <fcitx_utf8_get_char_validated+0xaf>
cmpl   $0x110000,-0x18(%rbp)
jge    11ad <fcitx_utf8_get_char_validated+0x9d>
mov    -0x18(%rbp),%eax
and    $0xfffff800,%eax
cmp    $0xd800,%eax
je     11ad <fcitx_utf8_get_char_validated+0x9d>
cmpl   $0xfdd0,-0x18(%rbp)
jl     119a <fcitx_utf8_get_char_validated+0x8a>
cmpl   $0xfdef,-0x18(%rbp)
jle    11ad <fcitx_utf8_get_char_validated+0x9d>
mov    -0x18(%rbp),%eax
and    $0xfffe,%eax
cmp    $0xfffe,%eax
jne    11b9 <fcitx_utf8_get_char_validated+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <fcitx_utf8_get_char_validated+0xaf>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcitx_utf8_get_char_validated>:
test   %esi,%esi
je     1160 <fcitx_utf8_get_char_validated+0x50>
push   %rax
call   1030 <fcitx_utf8_get_char_extended@plt>
test   %eax,%eax
lea    0x8(%rsp),%rsp
js     115f <fcitx_utf8_get_char_validated+0x4f>
mov    %eax,%ecx
and    $0xfffff800,%ecx
mov    %eax,%edx
not    %edx
and    $0xfffe,%edx
cmp    $0x1,%edx
sbb    %edx,%edx
or     %eax,%edx
cmp    $0xd800,%ecx
mov    $0xffffffff,%ecx
cmove  %ecx,%edx
cmp    $0x110000,%eax
cmovae %ecx,%edx
add    $0xffff0230,%eax
cmp    $0x20,%eax
cmovb  %ecx,%edx
mov    %edx,%eax
ret
mov    $0xfffffffe,%eax
ret

LLVM IR (-O0):
define i32 @fcitx_utf8_get_char_validated(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %2
store i32 -2, i32* %3
br label %39
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = call i32 @fcitx_utf8_get_char_extended(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = and i32 %14, -2147483648
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = load i32, i32* %6
store i32 %18, i32* %3
br label %39
19: ; preds = %10
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 1114112
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = load i32, i32* %6
%24 = and i32 %23, -2048
%25 = icmp ne i32 %24, 55296
br i1 %25, label %26, label %36
26: ; preds = %22
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 64976
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* %6
%31 = icmp sgt i32 %30, 65007
br i1 %31, label %32, label %36
32: ; preds = %29, %26
%33 = load i32, i32* %6
%34 = and i32 %33, 65534
%35 = icmp ne i32 %34, 65534
br i1 %35, label %37, label %36
36: ; preds = %32, %29, %22, %19
store i32 -1, i32* %3
br label %39
37: ; preds = %32
%38 = load i32, i32* %6
store i32 %38, i32* %3
br label %39
39: ; preds = %37, %36, %17, %9
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
define i32 @fcitx_utf8_get_char_validated(i8* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %19, label %4
4: ; preds = %2
%5 = tail call i32 @fcitx_utf8_get_char_extended(i8* %0, i32 %1)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %19
7: ; preds = %4
%8 = icmp ugt i32 %5, 1114111
%9 = and i32 %5, -2048
%10 = icmp eq i32 %9, 55296
%11 = add nsw i32 %5, -64976
%12 = icmp ult i32 %11, 32
%13 = or i1 %8, %10
%14 = or i1 %12, %13
%15 = and i32 %5, 65534
%16 = icmp eq i32 %15, 65534
%17 = select i1 %14, i1 true, i1 %16
%18 = select i1 %17, i32 -1, i32 %5
br label %19
19: ; preds = %7, %4, %2
%20 = phi i32 [ -2, %2 ], [ %5, %4 ], [ %18, %7 ]
ret i32 %20
}