#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 int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ assert(char *);
int unicode_is_well_formed_utf32_imp(int *, size_t, int);
bool unicode_is_well_formed_utf32(char *string, size_t string_length) {
  assert(string);
  if (string_length == 0)
    return true;
  if (string_length % 4 != 0)
    return false;
  uint32_t *data = (uint32_t *)string;
  if (data[0] == 0x0000FEFF)
    return unicode_is_well_formed_utf32_imp(&data[1], string_length / 4 - 1,
                                            false);
  else if (data[0] == 0xFFFE0000)
    return unicode_is_well_formed_utf32_imp(&data[1], string_length / 4 - 1,
                                            true);
  else
    return unicode_is_well_formed_utf32_imp(data, string_length / 4, true) ||
           unicode_is_well_formed_utf32_imp(data, string_length / 4, false);
}


x86 assembly (clang, -O0):
<unicode_is_well_formed_utf32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1150 <unicode_is_well_formed_utf32+0x30>
movl   $0x1,-0x4(%rbp)
jmp    122c <unicode_is_well_formed_utf32+0x10c>
mov    -0x18(%rbp),%rax
and    $0x3,%rax
cmp    $0x0,%rax
je     116e <unicode_is_well_formed_utf32+0x4e>
movl   $0x0,-0x4(%rbp)
jmp    122c <unicode_is_well_formed_utf32+0x10c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpl   $0xfeff,(%rax)
jne    11a9 <unicode_is_well_formed_utf32+0x89>
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0x18(%rbp),%rsi
shr    $0x2,%rsi
sub    $0x1,%rsi
xor    %edx,%edx
call   1030 <unicode_is_well_formed_utf32_imp@plt>
mov    %eax,-0x4(%rbp)
jmp    122c <unicode_is_well_formed_utf32+0x10c>
mov    -0x20(%rbp),%rax
cmpl   $0xfffe0000,(%rax)
jne    11df <unicode_is_well_formed_utf32+0xbf>
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0x18(%rbp),%rsi
shr    $0x2,%rsi
sub    $0x1,%rsi
mov    $0x1,%edx
call   1030 <unicode_is_well_formed_utf32_imp@plt>
mov    %eax,-0x4(%rbp)
jmp    122c <unicode_is_well_formed_utf32+0x10c>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shr    $0x2,%rsi
mov    $0x1,%edx
call   1030 <unicode_is_well_formed_utf32_imp@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
jne    1221 <unicode_is_well_formed_utf32+0x101>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shr    $0x2,%rsi
xor    %edx,%edx
call   1030 <unicode_is_well_formed_utf32_imp@plt>
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%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_utf32>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <assert@plt>
test   %rbx,%rbx
je     1162 <unicode_is_well_formed_utf32+0x42>
xor    %ebp,%ebp
test   $0x3,%bl
jne    1167 <unicode_is_well_formed_utf32+0x47>
mov    (%r14),%eax
cmp    $0xfffe0000,%eax
je     116e <unicode_is_well_formed_utf32+0x4e>
cmp    $0xfeff,%eax
jne    118e <unicode_is_well_formed_utf32+0x6e>
add    $0x4,%r14
shr    $0x2,%rbx
add    $0xffffffffffffffff,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
jmp    1185 <unicode_is_well_formed_utf32+0x65>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
add    $0x4,%r14
shr    $0x2,%rbx
add    $0xffffffffffffffff,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <unicode_is_well_formed_utf32_imp@plt>
shr    $0x2,%rbx
mov    $0x1,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <unicode_is_well_formed_utf32_imp@plt>
test   %eax,%eax
jne    1167 <unicode_is_well_formed_utf32+0x47>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <unicode_is_well_formed_utf32_imp@plt>
xor    %ebp,%ebp
test   %eax,%eax
setne  %bpl
jmp    1167 <unicode_is_well_formed_utf32+0x47>

LLVM IR (-O0):
define i32 @unicode_is_well_formed_utf32(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = call i32 @assert(i8* %7)
%9 = load i64, i64* %5
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %3
br label %58
12: ; preds = %2
%13 = load i64, i64* %5
%14 = urem i64 %13, 4
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 0, i32* %3
br label %58
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = bitcast i8* %18 to i32*
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = getelementptr inbounds i32, i32* %20, i64 0
%22 = load i32, i32* %21
%23 = icmp eq i32 %22, 65279
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = load i32*, i32** %6
%26 = getelementptr inbounds i32, i32* %25, i64 1
%27 = load i64, i64* %5
%28 = udiv i64 %27, 4
%29 = sub i64 %28, 1
%30 = call i32 @unicode_is_well_formed_utf32_imp(i32* %26, i64 %29, i32 0)
store i32 %30, i32* %3
br label %58
31: ; preds = %17
%32 = load i32*, i32** %6
%33 = getelementptr inbounds i32, i32* %32, i64 0
%34 = load i32, i32* %33
%35 = icmp eq i32 %34, -131072
br i1 %35, label %36, label %43
36: ; preds = %31
%37 = load i32*, i32** %6
%38 = getelementptr inbounds i32, i32* %37, i64 1
%39 = load i64, i64* %5
%40 = udiv i64 %39, 4
%41 = sub i64 %40, 1
%42 = call i32 @unicode_is_well_formed_utf32_imp(i32* %38, i64 %41, i32 1)
store i32 %42, i32* %3
br label %58
43: ; preds = %31
%44 = load i32*, i32** %6
%45 = load i64, i64* %5
%46 = udiv i64 %45, 4
%47 = call i32 @unicode_is_well_formed_utf32_imp(i32* %44, i64 %46, i32 1)
%48 = icmp ne i32 %47, 0
br i1 %48, label %55, label %49
49: ; preds = %43
%50 = load i32*, i32** %6
%51 = load i64, i64* %5
%52 = udiv i64 %51, 4
%53 = call i32 @unicode_is_well_formed_utf32_imp(i32* %50, i64 %52, i32 0)
%54 = icmp ne i32 %53, 0
br label %55
55: ; preds = %49, %43
%56 = phi i1 [ true, %43 ], [ %54, %49 ]
%57 = zext i1 %56 to i32
store i32 %57, i32* %3
br label %58
58: ; preds = %55, %36, %24, %16, %11
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
define i32 @unicode_is_well_formed_utf32(i8* %0, i64 %1) {
%3 = tail call i32 @assert(i8* %0)
%4 = icmp eq i64 %1, 0
br i1 %4, label %31, label %5
5: ; preds = %2
%6 = and i64 %1, 3
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %31
8: ; preds = %5
%9 = bitcast i8* %0 to i32*
%10 = load i32, i32* %9
switch i32 %10, label %23 [
i32 65279, label %11
i32 -131072, label %17
]
11: ; preds = %8
%12 = getelementptr inbounds i8, i8* %0, i64 4
%13 = bitcast i8* %12 to i32*
%14 = lshr i64 %1, 2
%15 = add nsw i64 %14, -1
%16 = tail call i32 @unicode_is_well_formed_utf32_imp(i32* %13, i64 %15, i32 0)
br label %31
17: ; preds = %8
%18 = getelementptr inbounds i8, i8* %0, i64 4
%19 = bitcast i8* %18 to i32*
%20 = lshr i64 %1, 2
%21 = add nsw i64 %20, -1
%22 = tail call i32 @unicode_is_well_formed_utf32_imp(i32* %19, i64 %21, i32 1)
br label %31
23: ; preds = %8
%24 = lshr i64 %1, 2
%25 = tail call i32 @unicode_is_well_formed_utf32_imp(i32* %9, i64 %24, i32 1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = tail call i32 @unicode_is_well_formed_utf32_imp(i32* %9, i64 %24, i32 0)
%29 = icmp ne i32 %28, 0
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %11, %17, %27, %23, %5, %2
%32 = phi i32 [ 1, %2 ], [ 0, %5 ], [ %16, %11 ], [ %22, %17 ], [ 1, %23 ], [ %30, %27 ]
ret i32 %32
}