#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 scalar_t__ uint8_t;
typedef scalar_t__ uint32_t;

/* Variables and functions */
int decode(scalar_t__ *, scalar_t__ *, scalar_t__);
int lily_is_valid_sized_utf8(const char *input, int size) {
  uint8_t *s = (uint8_t *)input;
  uint8_t *end = s + size;
  uint32_t codepoint;
  uint32_t state = 0;
  for (; *s; ++s)
    if (decode(&state, &codepoint, *s) == 1)
      break;
  return (state == 0) && (s == end);
}


x86 assembly (clang, -O0):
<lily_is_valid_sized_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1188 <lily_is_valid_sized_utf8+0x78>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <decode@plt>
cmp    $0x1,%eax
jne    1172 <lily_is_valid_sized_utf8+0x62>
jmp    1188 <lily_is_valid_sized_utf8+0x78>
jmp    1177 <lily_is_valid_sized_utf8+0x67>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    1142 <lily_is_valid_sized_utf8+0x32>
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x31(%rbp)
jne    11a6 <lily_is_valid_sized_utf8+0x96>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lily_is_valid_sized_utf8>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
movq   $0x0,(%rsp)
mov    (%rdi),%rdx
test   %rdx,%rdx
je     1167 <lily_is_valid_sized_utf8+0x57>
mov    %rsp,%r12
lea    0x8(%rsp),%rbx
mov    %r15,%rbp
nopl   (%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <decode@plt>
cmp    $0x1,%eax
je     115d <lily_is_valid_sized_utf8+0x4d>
mov    0x8(%rbp),%rdx
add    $0x8,%rbp
test   %rdx,%rdx
jne    1140 <lily_is_valid_sized_utf8+0x30>
cmpq   $0x0,(%rsp)
sete   %al
jmp    116c <lily_is_valid_sized_utf8+0x5c>
mov    $0x1,%al
mov    %r15,%rbp
movslq %r14d,%rcx
lea    (%r15,%rcx,8),%rcx
cmp    %rcx,%rbp
sete   %cl
and    %al,%cl
movzbl %cl,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @lily_is_valid_sized_utf8(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64*
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = bitcast i8* %9 to i64*
store i64* %10, i64** %5
%11 = load i64*, i64** %5
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i64, i64* %11, i64 %13
store i64* %14, i64** %6
store i64 0, i64* %8
br label %15
15: ; preds = %26, %2
%16 = load i64*, i64** %5
%17 = load i64, i64* %16
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %15
%20 = load i64*, i64** %5
%21 = load i64, i64* %20
%22 = call i32 @decode(i64* %8, i64* %7, i64 %21)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %25
24: ; preds = %19
br label %29
25: ; preds = %19
br label %26
26: ; preds = %25
%27 = load i64*, i64** %5
%28 = getelementptr inbounds i64, i64* %27, i32 1
store i64* %28, i64** %5
br label %15
29: ; preds = %24, %15
%30 = load i64, i64* %8
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i64*, i64** %5
%34 = load i64*, i64** %6
%35 = icmp eq i64* %33, %34
br label %36
36: ; preds = %32, %29
%37 = phi i1 [ false, %29 ], [ %35, %32 ]
%38 = zext i1 %37 to i32
ret i32 %38
}

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