#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 /*<<< orphan*/ verify_utf8_sequence(unsigned char const *, int *);
int is_valid_utf8(const char *string, size_t string_len) {
  int len = 0;
  const char *string_end = string + string_len;
  while (string < string_end) {
    if (!verify_utf8_sequence((const unsigned char *)string, &len)) {
      return 0;
    }
    string += len;
  }
  return 1;
}


x86 assembly (clang, -O0):
<is_valid_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1179 <is_valid_utf8+0x69>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <verify_utf8_sequence@plt>
cmp    $0x0,%eax
jne    1163 <is_valid_utf8+0x53>
movl   $0x0,-0x4(%rbp)
jmp    1180 <is_valid_utf8+0x70>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1133 <is_valid_utf8+0x23>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_utf8>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
mov    $0x1,%r14d
test   %rsi,%rsi
jle    1161 <is_valid_utf8+0x51>
mov    %rsi,%rbx
mov    %rdi,%rbp
add    %rdi,%rbx
lea    0x4(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r15,%rsi
call   1030 <verify_utf8_sequence@plt>
test   %eax,%eax
je     115e <is_valid_utf8+0x4e>
movslq 0x4(%rsp),%rax
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1140 <is_valid_utf8+0x30>
jmp    1161 <is_valid_utf8+0x51>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @is_valid_utf8(i8* %0, i64 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = getelementptr inbounds i8, i8* %0, i64 %1
%6 = icmp sgt i64 %1, 0
br i1 %6, label %7, label %16
7: ; preds = %2, %11
%8 = phi i8* [ %14, %11 ], [ %0, %2 ]
%9 = call i32 @verify_utf8_sequence(i8* %8, i32* %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %7
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %8, i64 %13
%15 = icmp ult i8* %14, %5
br i1 %15, label %7, label %16
16: ; preds = %7, %11, %2
%17 = phi i32 [ 1, %2 ], [ 1, %11 ], [ 0, %7 ]
ret i32 %17
}