#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 decode_utf8(unsigned int *, unsigned int *, unsigned char const);
__attribute__((visibility("hidden"))) int
is_valid_utf8(const unsigned char source[], const int length) {
  int i;
  unsigned int codepoint, state = 0;
  for (i = 0; i < length; i++) {
    if (decode_utf8(&state, &codepoint, source[i]) == 12) {
      return 0;
    }
  }
  return state == 0;
}


x86 assembly (clang, -O0):
<is_valid_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    117a <is_valid_utf8+0x6a>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
movzbl (%rax,%rcx,1),%edx
call   1030 <decode_utf8@plt>
cmp    $0xc,%eax
jne    1167 <is_valid_utf8+0x57>
movl   $0x0,-0x4(%rbp)
jmp    1189 <is_valid_utf8+0x79>
jmp    116c <is_valid_utf8+0x5c>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112d <is_valid_utf8+0x1d>
cmpl   $0x0,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_utf8>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,(%rsp)
test   %esi,%esi
jle    116c <is_valid_utf8+0x5c>
mov    %rdi,%r13
mov    %esi,%ebx
xor    %r14d,%r14d
mov    %rsp,%r15
lea    0x4(%rsp),%r12
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzbl 0x0(%r13,%rbp,1),%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <decode_utf8@plt>
cmp    $0xc,%eax
je     1172 <is_valid_utf8+0x62>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1140 <is_valid_utf8+0x30>
xor    %r14d,%r14d
cmpl   $0x0,(%rsp)
sete   %r14b
jmp    1172 <is_valid_utf8+0x62>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define hidden i32 @is_valid_utf8(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %8
store i32 0, i32* %6
br label %9
9: ; preds = %23, %2
%10 = load i32, i32* %6
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %26
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = call i32 @decode_utf8(i32* %8, i32* %7, i8 zeroext %18)
%20 = icmp eq i32 %19, 12
br i1 %20, label %21, label %22
21: ; preds = %13
store i32 0, i32* %3
br label %30
22: ; preds = %13
br label %23
23: ; preds = %22
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
store i32 %25, i32* %6
br label %9
26: ; preds = %9
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 0
%29 = zext i1 %28 to i32
store i32 %29, i32* %3
br label %30
30: ; preds = %26, %21
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
define hidden i32 @is_valid_utf8(i8* nocapture %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %23
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %12
10: ; preds = %12
%11 = icmp eq i64 %18, %9
br i1 %11, label %19, label %12
12: ; preds = %8, %10
%13 = phi i64 [ 0, %8 ], [ %18, %10 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = call i32 @decode_utf8(i32* %4, i32* %3, i8 zeroext %15)
%17 = icmp eq i32 %16, 12
%18 = add nuw nsw i64 %13, 1
br i1 %17, label %23, label %10
19: ; preds = %10
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 0
%22 = zext i1 %21 to i32
br label %23
23: ; preds = %12, %2, %19
%24 = phi i32 [ %22, %19 ], [ 1, %2 ], [ 0, %12 ]
ret i32 %24
}