#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 */
scalar_t__ is_cont_byte(unsigned char const);
scalar_t__ isascii(unsigned char const);
int /*<<< orphan*/ utf8_decode(unsigned char const **, int);
int utf8_validity_check(const unsigned char *data, int maxlen) {
  const unsigned char *p = data;
  if (maxlen < 1 || maxlen > 4) {
    return 0;
  }
  while (*p != '\0') {
    if (isascii(*p)) {
      ++p;
      continue;
    } else if (is_cont_byte(*p)) {
      return 0;
    } else if (!utf8_decode(&p, maxlen)) {
      return 0;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<utf8_validity_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x14(%rbp)
jl     115b <utf8_validity_check+0x2b>
cmpl   $0x4,-0x14(%rbp)
jle    1167 <utf8_validity_check+0x37>
movl   $0x0,-0x4(%rbp)
jmp    11fc <utf8_validity_check+0xcc>
jmp    116c <utf8_validity_check+0x3c>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     11f5 <utf8_validity_check+0xc5>
mov    -0x20(%rbp),%rax
movzbl (%rax),%edi
call   1050 <isascii@plt>
cmp    $0x0,%rax
je     11a3 <utf8_validity_check+0x73>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    116c <utf8_validity_check+0x3c>
mov    -0x20(%rbp),%rax
movzbl (%rax),%edi
call   1030 <is_cont_byte@plt>
cmp    $0x0,%rax
je     11c5 <utf8_validity_check+0x95>
movl   $0x0,-0x4(%rbp)
jmp    11fc <utf8_validity_check+0xcc>
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdi
call   1040 <utf8_decode@plt>
cmp    $0x0,%eax
jne    11e6 <utf8_validity_check+0xb6>
movl   $0x0,-0x4(%rbp)
jmp    11fc <utf8_validity_check+0xcc>
jmp    11eb <utf8_validity_check+0xbb>
jmp    11f0 <utf8_validity_check+0xc0>
jmp    116c <utf8_validity_check+0x3c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_validity_check>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,0x8(%rsp)
lea    -0x5(%rbx),%eax
xor    %ebp,%ebp
cmp    $0xfffffffc,%eax
jb     11a9 <utf8_validity_check+0x79>
mov    (%rdi),%al
test   %al,%al
je     11a4 <utf8_validity_check+0x74>
lea    0x8(%rsp),%r14
jmp    1170 <utf8_validity_check+0x40>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rcx
mov    %rcx,0x8(%rsp)
movzbl (%rcx),%eax
test   %al,%al
je     11a4 <utf8_validity_check+0x74>
movzbl %al,%edi
call   1050 <isascii@plt>
mov    0x8(%rsp),%rcx
test   %rax,%rax
jne    1160 <utf8_validity_check+0x30>
movzbl (%rcx),%edi
call   1030 <is_cont_byte@plt>
test   %rax,%rax
jne    11a9 <utf8_validity_check+0x79>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <utf8_decode@plt>
test   %eax,%eax
je     11a9 <utf8_validity_check+0x79>
mov    0x8(%rsp),%rcx
jmp    1169 <utf8_validity_check+0x39>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @utf8_validity_check(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
store i8* %7, i8** %6
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 1
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 4
br i1 %12, label %13, label %14
13: ; preds = %10, %2
store i32 0, i32* %3
br label %43
14: ; preds = %10
br label %15
15: ; preds = %41, %25, %14
%16 = load i8*, i8** %6
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %42
20: ; preds = %15
%21 = load i8*, i8** %6
%22 = load i8, i8* %21
%23 = call i64 @isascii(i8 zeroext %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i8*, i8** %6
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %6
br label %15
28: ; preds = %20
%29 = load i8*, i8** %6
%30 = load i8, i8* %29
%31 = call i64 @is_cont_byte(i8 zeroext %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 0, i32* %3
br label %43
34: ; preds = %28
%35 = load i32, i32* %5
%36 = call i32 @utf8_decode(i8** %6, i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %34
store i32 0, i32* %3
br label %43
39: ; preds = %34
br label %40
40: ; preds = %39
br label %41
41: ; preds = %40
br label %15
42: ; preds = %15
store i32 1, i32* %3
br label %43
43: ; preds = %42, %38, %33, %13
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
define i32 @utf8_validity_check(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* %0, i8** %3
%5 = add i32 %1, -5
%6 = icmp ult i32 %5, -4
br i1 %6, label %30, label %7
7: ; preds = %2
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %30, label %10
10: ; preds = %7, %17
%11 = phi i8 [ %19, %17 ], [ %8, %7 ]
%12 = call i64 @isascii(i8 zeroext %11)
%13 = icmp eq i64 %12, 0
%14 = load i8*, i8** %3
br i1 %13, label %21, label %15
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %14, i64 1
store i8* %16, i8** %3
br label %17
17: ; preds = %28, %15
%18 = phi i8* [ %29, %28 ], [ %16, %15 ]
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %30, label %10
21: ; preds = %10
%22 = load i8, i8* %14
%23 = call i64 @is_cont_byte(i8 zeroext %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = call i32 @utf8_decode(i8** %3, i32 %1)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = load i8*, i8** %3
br label %17
30: ; preds = %21, %25, %17, %7, %2
%31 = phi i32 [ 0, %2 ], [ 1, %7 ], [ 0, %21 ], [ 0, %25 ], [ 1, %17 ]
ret i32 %31
}