#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*/);
scalar_t__ u_char_width(int /*<<< orphan*/);
int /*<<< orphan*/ u_decode(char const **, char const *, char const *);
size_t u_width_impl(const char *string, size_t n, bool use_n) {
  assert(string != NULL);
  size_t width = 0;
  const char *p = string;
  const char *end = p + n;
  while (((use_n) ? (p) < (end) : *(p) != '\0'))
    width += u_char_width(u_decode(&p, p, end));
  return width;
}


x86 assembly (clang, -O0):
<u_width_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1193 <u_width_impl+0x63>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
jmp    11a8 <u_width_impl+0x78>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    $0x0,%eax
je     11d9 <u_width_impl+0xa9>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    -0x28(%rbp),%rdi
call   1040 <u_decode@plt>
mov    %eax,%edi
call   1050 <u_char_width@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1171 <u_width_impl+0x41>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u_width_impl>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
xor    %edi,%edi
test   %rbp,%rbp
setne  %dil
call   1030 <assert@plt>
mov    %rbp,(%rsp)
add    %rbp,%rbx
xor    %ebp,%ebp
mov    %rsp,%r15
jmp    117a <u_width_impl+0x4a>
nopl   0x0(%rax)
cmpb   $0x0,(%rsi)
je     1188 <u_width_impl+0x58>
mov    %r15,%rdi
mov    %rbx,%rdx
call   1040 <u_decode@plt>
mov    %eax,%edi
call   1050 <u_char_width@plt>
add    %rax,%rbp
mov    (%rsp),%rsi
test   %r14d,%r14d
je     1160 <u_width_impl+0x30>
cmp    %rbx,%rsi
jb     1165 <u_width_impl+0x35>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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