#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*/ assert(char *);
int unicode_code_point_byte_header_utf8(char);
bool unicode_is_well_formed_utf8(char *string, size_t string_length) {
  assert(string);
  unsigned int runner = 0;
  int header;
  for (size_t k = 0; k < string_length; k++) {
    if (runner == 0) {
      header = unicode_code_point_byte_header_utf8(string[k]);
      if (header <= 0)
        return false;
      runner = header - 1;
    } else {
      header = unicode_code_point_byte_header_utf8(string[k]);
      if (header != -1)
        return false;
      runner--;
    }
  }
  return runner == 0;
}


x86 assembly (clang, -O0):
<unicode_is_well_formed_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <assert@plt>
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11e1 <unicode_is_well_formed_utf8+0xc1>
cmpl   $0x0,-0x1c(%rbp)
jne    1198 <unicode_is_well_formed_utf8+0x78>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <unicode_code_point_byte_header_utf8@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     118a <unicode_is_well_formed_utf8+0x6a>
movl   $0x0,-0x4(%rbp)
jmp    11f0 <unicode_is_well_formed_utf8+0xd0>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cb <unicode_is_well_formed_utf8+0xab>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <unicode_code_point_byte_header_utf8@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     11c2 <unicode_is_well_formed_utf8+0xa2>
movl   $0x0,-0x4(%rbp)
jmp    11f0 <unicode_is_well_formed_utf8+0xd0>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d0 <unicode_is_well_formed_utf8+0xb0>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1148 <unicode_is_well_formed_utf8+0x28>
cmpl   $0x0,-0x1c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unicode_is_well_formed_utf8>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <assert@plt>
test   %r14,%r14
je     1169 <unicode_is_well_formed_utf8+0x49>
xor    %ebx,%ebx
xor    %ebp,%ebp
jmp    1152 <unicode_is_well_formed_utf8+0x32>
nopl   (%rax)
mov    %eax,%ebp
test   %eax,%eax
jle    1165 <unicode_is_well_formed_utf8+0x45>
add    $0xffffffff,%ebp
add    $0x1,%rbx
cmp    %rbx,%r14
je     1170 <unicode_is_well_formed_utf8+0x50>
movsbl (%r15,%rbx,1),%edi
call   1040 <unicode_code_point_byte_header_utf8@plt>
test   %ebp,%ebp
je     1140 <unicode_is_well_formed_utf8+0x20>
cmp    $0xffffffff,%eax
je     1146 <unicode_is_well_formed_utf8+0x26>
xor    %eax,%eax
jmp    1177 <unicode_is_well_formed_utf8+0x57>
mov    $0x1,%eax
jmp    1177 <unicode_is_well_formed_utf8+0x57>
xor    %eax,%eax
test   %ebp,%ebp
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @unicode_is_well_formed_utf8(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i8*, i8** %4
%10 = call i32 @assert(i8* %9)
store i32 0, i32* %6
store i64 0, i64* %8
br label %11
11: ; preds = %43, %2
%12 = load i64, i64* %8
%13 = load i64, i64* %5
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %46
15: ; preds = %11
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %15
%19 = load i8*, i8** %4
%20 = load i64, i64* %8
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i8, i8* %21
%23 = call i32 @unicode_code_point_byte_header_utf8(i8 signext %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %18
store i32 0, i32* %3
br label %50
27: ; preds = %18
%28 = load i32, i32* %7
%29 = sub nsw i32 %28, 1
store i32 %29, i32* %6
br label %42
30: ; preds = %15
%31 = load i8*, i8** %4
%32 = load i64, i64* %8
%33 = getelementptr inbounds i8, i8* %31, i64 %32
%34 = load i8, i8* %33
%35 = call i32 @unicode_code_point_byte_header_utf8(i8 signext %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp ne i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %30
store i32 0, i32* %3
br label %50
39: ; preds = %30
%40 = load i32, i32* %6
%41 = add i32 %40, -1
store i32 %41, i32* %6
br label %42
42: ; preds = %39, %27
br label %43
43: ; preds = %42
%44 = load i64, i64* %8
%45 = add i64 %44, 1
store i64 %45, i64* %8
br label %11
46: ; preds = %11
%47 = load i32, i32* %6
%48 = icmp eq i32 %47, 0
%49 = zext i1 %48 to i32
store i32 %49, i32* %3
br label %50
50: ; preds = %46, %38, %26
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
define i32 @unicode_is_well_formed_utf8(i8* %0, i64 %1) {
%3 = tail call i32 @assert(i8* %0)
%4 = icmp eq i64 %1, 0
br i1 %4, label %24, label %5
5: ; preds = %2, %16
%6 = phi i64 [ %19, %16 ], [ 0, %2 ]
%7 = phi i32 [ %18, %16 ], [ 0, %2 ]
%8 = icmp eq i32 %7, 0
%9 = getelementptr inbounds i8, i8* %0, i64 %6
%10 = load i8, i8* %9
%11 = tail call i32 @unicode_code_point_byte_header_utf8(i8 signext %10)
br i1 %8, label %12, label %14
12: ; preds = %5
%13 = icmp slt i32 %11, 1
br i1 %13, label %24, label %16
14: ; preds = %5
%15 = icmp eq i32 %11, -1
br i1 %15, label %16, label %24
16: ; preds = %14, %12
%17 = phi i32 [ %11, %12 ], [ %7, %14 ]
%18 = add i32 %17, -1
%19 = add nuw i64 %6, 1
%20 = icmp eq i64 %19, %1
br i1 %20, label %21, label %5
21: ; preds = %16
%22 = icmp eq i32 %18, 0
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %12, %14, %2, %21
%25 = phi i32 [ 1, %2 ], [ %23, %21 ], [ 0, %14 ], [ 0, %12 ]
ret i32 %25
}