#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 ch_utf8_length(char const *);
int str_utf8_length(const char *s) {
  int n, l, off;
  n = 0;
  off = 0;
  while (*(s + off)) {
    l = ch_utf8_length(s + off);
    off += l;
    if (l == 1)
      n += 1;
    else {
      n += 2;
    }
  }
  return n;
}


x86 assembly (clang, -O0):
<str_utf8_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     117e <str_utf8_length+0x6e>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
call   1030 <ch_utf8_length@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    1170 <str_utf8_length+0x60>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1179 <str_utf8_length+0x69>
mov    -0xc(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0xc(%rbp)
jmp    112a <str_utf8_length+0x1a>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_utf8_length>:
push   %rbp
push   %r14
push   %rbx
cmpb   $0x0,(%rdi)
je     1144 <str_utf8_length+0x34>
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %ebp,%ebp
call   1030 <ch_utf8_length@plt>
add    %eax,%ebx
xor    %ecx,%ecx
cmp    $0x1,%eax
sete   %cl
sub    %ecx,%ebp
add    $0x2,%ebp
movslq %ebx,%rax
lea    (%r14,%rax,1),%rdi
cmpb   $0x0,(%r14,%rax,1)
jne    1120 <str_utf8_length+0x10>
jmp    1146 <str_utf8_length+0x36>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @str_utf8_length(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %17, label %4
4: ; preds = %1, %4
%5 = phi i8* [ %14, %4 ], [ %0, %1 ]
%6 = phi i32 [ %9, %4 ], [ 0, %1 ]
%7 = phi i32 [ %12, %4 ], [ 0, %1 ]
%8 = tail call i32 @ch_utf8_length(i8* %5)
%9 = add nsw i32 %8, %6
%10 = icmp eq i32 %8, 1
%11 = select i1 %10, i32 1, i32 2
%12 = add nuw nsw i32 %11, %7
%13 = sext i32 %9 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %4
17: ; preds = %4, %1
%18 = phi i32 [ 0, %1 ], [ %12, %4 ]
ret i32 %18
}