#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_utf8(const char *input) {
  uint8_t *s = (uint8_t *)input;
  uint32_t codepoint;
  uint32_t state = 0;
  for (; *s; ++s)
    if (decode(&state, &codepoint, *s) == 1)
      break;
  return state == 0;
}


x86 assembly (clang, -O0):
<lily_is_valid_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1172 <lily_is_valid_utf8+0x62>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <decode@plt>
cmp    $0x1,%eax
jne    115c <lily_is_valid_utf8+0x4c>
jmp    1172 <lily_is_valid_utf8+0x62>
jmp    1161 <lily_is_valid_utf8+0x51>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    112c <lily_is_valid_utf8+0x1c>
cmpq   $0x0,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lily_is_valid_utf8>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
mov    (%rdi),%rdx
test   %rdx,%rdx
je     1168 <lily_is_valid_utf8+0x58>
mov    %rdi,%rbx
add    $0x8,%rbx
mov    %rsp,%r14
lea    0x8(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <decode@plt>
cmp    $0x1,%eax
je     115c <lily_is_valid_utf8+0x4c>
mov    (%rbx),%rdx
add    $0x8,%rbx
test   %rdx,%rdx
jne    1140 <lily_is_valid_utf8+0x30>
xor    %eax,%eax
cmpq   $0x0,(%rsp)
sete   %al
jmp    116d <lily_is_valid_utf8+0x5d>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @lily_is_valid_utf8(i8* %0) {
%2 = alloca i8*
%3 = alloca i64*
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = bitcast i8* %6 to i64*
store i64* %7, i64** %3
store i64 0, i64* %5
br label %8
8: ; preds = %19, %1
%9 = load i64*, i64** %3
%10 = load i64, i64* %9
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = load i64*, i64** %3
%14 = load i64, i64* %13
%15 = call i32 @decode(i64* %5, i64* %4, i64 %14)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %12
br label %22
18: ; preds = %12
br label %19
19: ; preds = %18
%20 = load i64*, i64** %3
%21 = getelementptr inbounds i64, i64* %20, i32 1
store i64* %21, i64** %3
br label %8
22: ; preds = %17, %8
%23 = load i64, i64* %5
%24 = icmp eq i64 %23, 0
%25 = zext i1 %24 to i32
ret i32 %25
}

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