#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 uchar_extent(char const);
size_t ustrlen(char const *s) {
  int ext;
  size_t ret = 0;
  while (*s) {
    ext = uchar_extent(*s);
    if (ext > 0)
      s += ext;
    else if (ext == -1)
      s += 1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<ustrlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1180 <ustrlen+0x70>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <uchar_extent@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1160 <ustrlen+0x50>
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    117b <ustrlen+0x6b>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1176 <ustrlen+0x66>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    117b <ustrlen+0x6b>
jmp    1124 <ustrlen+0x14>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ustrlen>:
push   %rbx
mov    (%rdi),%al
test   %al,%al
je     1143 <ustrlen+0x33>
mov    %rdi,%rbx
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <uchar_extent@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
sete   %cl
test   %eax,%eax
cltq
cmovg  %rax,%rcx
movzbl (%rbx,%rcx,1),%eax
add    %rcx,%rbx
test   %al,%al
jne    1120 <ustrlen+0x10>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
define i64 @ustrlen(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %16, label %4
4: ; preds = %1, %4
%5 = phi i8 [ %14, %4 ], [ %2, %1 ]
%6 = phi i8* [ %13, %4 ], [ %0, %1 ]
%7 = tail call i32 @uchar_extent(i8 signext %5)
%8 = icmp sgt i32 %7, 0
%9 = sext i32 %7 to i64
%10 = icmp eq i32 %7, -1
%11 = zext i1 %10 to i64
%12 = select i1 %8, i64 %9, i64 %11
%13 = getelementptr i8, i8* %6, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %4
16: ; preds = %4, %1
ret i64 0
}