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

/* Variables and functions */
int /*<<< orphan*/ assert(char const *);
int utf8_encoded_valid_unichar(char const *);
const char *utf8_is_valid(const char *str) {
  const uint8_t *p;
  assert(str);
  for (p = (const uint8_t *)str; *p;) {
    int len;
    len = utf8_encoded_valid_unichar((const char *)p);
    if (len < 0)
      return NULL;
    p += len;
  }
  return str;
}


x86 assembly (clang, -O0):
<utf8_is_valid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <assert@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1188 <utf8_is_valid+0x68>
mov    -0x18(%rbp),%rdi
call   1040 <utf8_encoded_valid_unichar@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    116e <utf8_is_valid+0x4e>
movq   $0x0,-0x8(%rbp)
jmp    1190 <utf8_is_valid+0x70>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    113d <utf8_is_valid+0x1d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_is_valid>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <assert@plt>
mov    %r14,%rbx
nop
cmpq   $0x0,(%rbx)
je     114c <utf8_is_valid+0x2c>
mov    %rbx,%rdi
call   1040 <utf8_encoded_valid_unichar@plt>
movslq %eax,%rcx
lea    (%rbx,%rcx,8),%rbx
test   %eax,%eax
jns    1130 <utf8_is_valid+0x10>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @utf8_is_valid(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = bitcast i8* %0 to i64*
br label %4
4: ; preds = %8, %1
%5 = phi i64* [ %3, %1 ], [ %13, %8 ]
%6 = load i64, i64* %5
%7 = icmp eq i64 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %4
%9 = bitcast i64* %5 to i8*
%10 = tail call i32 @utf8_encoded_valid_unichar(i8* %9)
%11 = icmp slt i32 %10, 0
%12 = sext i32 %10 to i64
%13 = getelementptr inbounds i64, i64* %5, i64 %12
br i1 %11, label %14, label %4
14: ; preds = %4, %8
%15 = phi i8* [ null, %8 ], [ %0, %4 ]
ret i8* %15
}