#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 */
char *fast_validate(char const *);
char *fast_validate_len(char const *, size_t);
int g_utf8_validate(const char *str, size_t max_len, const char **end) {
  const char *p;
  if (max_len < 0)
    p = fast_validate(str);
  else
    p = fast_validate_len(str, max_len);
  if (end)
    *end = p;
  if ((max_len >= 0 && p != str + max_len) || (max_len < 0 && *p != '\0'))
    return 0;
  else
    return 1;
}


x86 assembly (clang, -O0):
<g_utf8_validate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jae    1151 <g_utf8_validate+0x31>
mov    -0x10(%rbp),%rdi
call   1040 <fast_validate@plt>
mov    %rax,-0x28(%rbp)
jmp    1162 <g_utf8_validate+0x42>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <fast_validate_len@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1178 <g_utf8_validate+0x58>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x18(%rbp)
jb     1198 <g_utf8_validate+0x78>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jne    11b3 <g_utf8_validate+0x93>
cmpq   $0x0,-0x18(%rbp)
jae    11bf <g_utf8_validate+0x9f>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11bf <g_utf8_validate+0x9f>
movl   $0x0,-0x4(%rbp)
jmp    11c6 <g_utf8_validate+0xa6>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<g_utf8_validate>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <fast_validate_len@plt>
test   %r15,%r15
je     112b <g_utf8_validate+0x1b>
mov    %rax,(%r15)
add    %r14,%rbx
xor    %ecx,%ecx
cmp    %rbx,%rax
sete   %cl
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @g_utf8_validate(i8* %0, i64 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8** %2, i8*** %7
%9 = load i64, i64* %6
%10 = icmp ult i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = call i8* @fast_validate(i8* %12)
store i8* %13, i8** %8
br label %18
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = call i8* @fast_validate_len(i8* %15, i64 %16)
store i8* %17, i8** %8
br label %18
18: ; preds = %14, %11
%19 = load i8**, i8*** %7
%20 = icmp ne i8** %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i8*, i8** %8
%23 = load i8**, i8*** %7
store i8* %22, i8** %23
br label %24
24: ; preds = %21, %18
%25 = load i64, i64* %6
%26 = icmp uge i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i8*, i8** %8
%29 = load i8*, i8** %5
%30 = load i64, i64* %6
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = icmp ne i8* %28, %31
br i1 %32, label %41, label %33
33: ; preds = %27, %24
%34 = load i64, i64* %6
%35 = icmp ult i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = load i8*, i8** %8
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %36, %27
store i32 0, i32* %4
br label %43
42: ; preds = %36, %33
store i32 1, i32* %4
br label %43
43: ; preds = %42, %41
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
define i32 @g_utf8_validate(i8* %0, i64 %1, i8** writeonly %2) {
%4 = tail call i8* @fast_validate_len(i8* %0, i64 %1)
%5 = icmp eq i8** %2, null
br i1 %5, label %7, label %6
6: ; preds = %3
store i8* %4, i8** %2
br label %7
7: ; preds = %3, %6
%8 = getelementptr inbounds i8, i8* %0, i64 %1
%9 = icmp eq i8* %4, %8
%10 = zext i1 %9 to i32
ret i32 %10
}