#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(int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ u_decode(char const **, char const *, char const *);
size_t u_n_chars(const char *str) {
  assert(str != NULL);
  size_t n = 0;
  for (const char *p = str, *end = str + strlen(str); *p != '\0'; n++)
    u_decode(&p, p, end);
  return n;
}


x86 assembly (clang, -O0):
<u_n_chars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b1 <u_n_chars+0x81>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x18(%rbp),%rdi
call   1050 <u_decode@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    117f <u_n_chars+0x4f>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u_n_chars>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1040 <assert@plt>
mov    %r14,(%rsp)
mov    %r14,%rdi
call   1030 <strlen@plt>
cmpb   $0x0,(%r14)
je     118e <u_n_chars+0x5e>
movslq %eax,%r15
add    %r14,%r15
xor    %ebx,%ebx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <u_decode@plt>
add    $0x1,%rbx
mov    (%rsp),%r14
cmpb   $0x0,(%r14)
jne    1170 <u_n_chars+0x40>
jmp    1190 <u_n_chars+0x60>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i64 @u_n_chars(i8* %0) {
%2 = alloca i8*
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = bitcast i8** %2 to i8*
store i8* %0, i8** %2
%7 = tail call i32 @strlen(i8* %0)
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %14, %1
%13 = phi i64 [ 0, %1 ], [ %18, %14 ]
ret i64 %13
14: ; preds = %1, %14
%15 = phi i8* [ %19, %14 ], [ %0, %1 ]
%16 = phi i64 [ %18, %14 ], [ 0, %1 ]
%17 = call i32 @u_decode(i8** %2, i8* %15, i8* %9)
%18 = add i64 %16, 1
%19 = load i8*, i8** %2
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %12, label %14
}