#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_encoded_expected_len(char const *);
int utf8_encoded_to_unichar(char const *);
int utf8_unichar_to_encoded_len(int);
int /*<<< orphan*/ utf8_unichar_valid_range(int);
int utf8_encoded_valid_unichar(const char *str) {
  int len;
  int unichar;
  int i;
  len = utf8_encoded_expected_len(str);
  if (len == 0)
    return -1;
  if (len == 1)
    return 1;
  for (i = 0; i < len; i++)
    if ((str[i] & 0x80) != 0x80)
      return -1;
  unichar = utf8_encoded_to_unichar(str);
  if (utf8_unichar_to_encoded_len(unichar) != len)
    return -1;
  if (!utf8_unichar_valid_range(unichar))
    return -1;
  return len;
}


x86 assembly (clang, -O0):
<utf8_encoded_valid_unichar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <utf8_encoded_expected_len@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    116e <utf8_encoded_valid_unichar+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <utf8_encoded_valid_unichar+0xde>
cmpl   $0x1,-0x14(%rbp)
jne    1184 <utf8_encoded_valid_unichar+0x44>
movl   $0x1,-0x4(%rbp)
jmp    121e <utf8_encoded_valid_unichar+0xde>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d2 <utf8_encoded_valid_unichar+0x92>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0x80,%eax
cmp    $0x80,%eax
je     11bf <utf8_encoded_valid_unichar+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <utf8_encoded_valid_unichar+0xde>
jmp    11c4 <utf8_encoded_valid_unichar+0x84>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118b <utf8_encoded_valid_unichar+0x4b>
mov    -0x10(%rbp),%rdi
call   1030 <utf8_encoded_to_unichar@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <utf8_unichar_to_encoded_len@plt>
cmp    -0x14(%rbp),%eax
je     11fb <utf8_encoded_valid_unichar+0xbb>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <utf8_encoded_valid_unichar+0xde>
mov    -0x18(%rbp),%edi
call   1040 <utf8_unichar_valid_range@plt>
cmp    $0x0,%eax
jne    1218 <utf8_encoded_valid_unichar+0xd8>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <utf8_encoded_valid_unichar+0xde>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_encoded_valid_unichar>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1050 <utf8_encoded_expected_len@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
je     11a4 <utf8_encoded_valid_unichar+0x64>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    1165 <utf8_encoded_valid_unichar+0x25>
mov    $0x1,%r14d
jmp    11a4 <utf8_encoded_valid_unichar+0x64>
jl     117f <utf8_encoded_valid_unichar+0x3f>
mov    %ebp,%eax
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
cmpb   $0x0,(%rbx,%rcx,1)
jns    11a4 <utf8_encoded_valid_unichar+0x64>
add    $0x1,%rcx
cmp    %rcx,%rax
jne    1170 <utf8_encoded_valid_unichar+0x30>
mov    %rbx,%rdi
call   1030 <utf8_encoded_to_unichar@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <utf8_unichar_to_encoded_len@plt>
cmp    %ebp,%eax
jne    11a4 <utf8_encoded_valid_unichar+0x64>
mov    %ebx,%edi
call   1040 <utf8_unichar_valid_range@plt>
cmp    $0x1,%eax
sbb    %r14d,%r14d
or     %ebp,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @utf8_encoded_valid_unichar(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @utf8_encoded_expected_len(i8* %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %50
12: ; preds = %1
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 1, i32* %2
br label %50
16: ; preds = %12
store i32 0, i32* %6
br label %17
17: ; preds = %32, %16
%18 = load i32, i32* %6
%19 = load i32, i32* %4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %35
21: ; preds = %17
%22 = load i8*, i8** %3
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = and i32 %27, 128
%29 = icmp ne i32 %28, 128
br i1 %29, label %30, label %31
30: ; preds = %21
store i32 -1, i32* %2
br label %50
31: ; preds = %21
br label %32
32: ; preds = %31
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %17
35: ; preds = %17
%36 = load i8*, i8** %3
%37 = call i32 @utf8_encoded_to_unichar(i8* %36)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = call i32 @utf8_unichar_to_encoded_len(i32 %38)
%40 = load i32, i32* %4
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %35
store i32 -1, i32* %2
br label %50
43: ; preds = %35
%44 = load i32, i32* %5
%45 = call i32 @utf8_unichar_valid_range(i32 %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %43
store i32 -1, i32* %2
br label %50
48: ; preds = %43
%49 = load i32, i32* %4
store i32 %49, i32* %2
br label %50
50: ; preds = %48, %47, %42, %30, %15, %11
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
define i32 @utf8_encoded_valid_unichar(i8* %0) {
%2 = tail call i32 @utf8_encoded_expected_len(i8* %0)
switch i32 %2, label %3 [
i32 0, label %24
i32 1, label %7
]
3: ; preds = %1
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %16
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %10
7: ; preds = %1
br label %24
8: ; preds = %10
%9 = icmp eq i64 %15, %6
br i1 %9, label %16, label %10
10: ; preds = %5, %8
%11 = phi i64 [ 0, %5 ], [ %15, %8 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = icmp slt i8 %13, 0
%15 = add nuw nsw i64 %11, 1
br i1 %14, label %8, label %24
16: ; preds = %8, %3
%17 = tail call i32 @utf8_encoded_to_unichar(i8* %0)
%18 = tail call i32 @utf8_unichar_to_encoded_len(i32 %17)
%19 = icmp eq i32 %18, %2
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = tail call i32 @utf8_unichar_valid_range(i32 %17)
%22 = icmp eq i32 %21, 0
%23 = select i1 %22, i32 -1, i32 %2
br label %24
24: ; preds = %10, %20, %16, %1, %7
%25 = phi i32 [ 1, %7 ], [ -1, %1 ], [ -1, %16 ], [ %23, %20 ], [ -1, %10 ]
ret i32 %25
}