#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 int /*<<< orphan*/ wchar_t;
typedef int /*<<< orphan*/ state;
typedef int /*<<< orphan*/ mbstate_t;

/* Variables and functions */
scalar_t__ mbrtowc(int /*<<< orphan*/ *, char *, unsigned int,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
unsigned int strlen(char *);
int wcwidth(int /*<<< orphan*/);
int cell_len(char *str) {
  unsigned len = strlen(str);
  unsigned j;
  wchar_t wch;
  size_t rc;
  int width;
  int cnt = 0;
  mbstate_t state;
  memset(&state, 0, sizeof(state));
  for (j = 0; j < len; j++) {
    rc = (int)mbrtowc(&wch, str + j, len - j, &state);
    if (rc == (size_t)-1 || rc == (size_t)-2)
      break;
    j += rc - 1;
    if ((width = wcwidth(wch)) < 0)
      break;
    cnt += width;
  }
  return cnt;
}


x86 assembly (clang, -O0):
<cell_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    1201 <cell_len+0xc1>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%eax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
sub    -0x10(%rbp),%edx
lea    -0x14(%rbp),%rdi
lea    -0x2c(%rbp),%rcx
call   1040 <mbrtowc@plt>
cltq
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
je     11bb <cell_len+0x7b>
cmpq   $0xfffffffffffffffe,-0x20(%rbp)
jne    11c0 <cell_len+0x80>
jmp    1201 <cell_len+0xc1>
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
mov    -0x10(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <wcwidth@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11ea <cell_len+0xaa>
jmp    1201 <cell_len+0xc1>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1176 <cell_len+0x36>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cell_len>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %rsp,%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
test   %r13d,%r13d
je     11bc <cell_len+0x7c>
xor    %ebp,%ebp
mov    %rsp,%r12
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%esi
add    %r14,%rsi
mov    %r13d,%edx
sub    %r15d,%edx
lea    0x4(%rsp),%rdi
mov    %r12,%rcx
call   1040 <mbrtowc@plt>
mov    %rax,%rbx
not    %eax
test   $0xfffffffe,%eax
je     11bc <cell_len+0x7c>
mov    0x4(%rsp),%edi
call   1060 <wcwidth@plt>
test   %eax,%eax
js     11bc <cell_len+0x7c>
add    %eax,%ebp
add    %ebx,%r15d
cmp    %r15d,%r13d
ja     1180 <cell_len+0x40>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @cell_len(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %3
store i32 0, i32* %8
%12 = call i32 @memset(i32* %9, i32 0, i32 4)
store i32 0, i32* %4
br label %13
13: ; preds = %49, %1
%14 = load i32, i32* %4
%15 = load i32, i32* %3
%16 = icmp ult i32 %14, %15
br i1 %16, label %17, label %52
17: ; preds = %13
%18 = load i8*, i8** %2
%19 = load i32, i32* %4
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i32, i32* %3
%23 = load i32, i32* %4
%24 = sub i32 %22, %23
%25 = call i64 @mbrtowc(i32* %5, i8* %21, i32 %24, i32* %9)
%26 = trunc i64 %25 to i32
%27 = sext i32 %26 to i64
store i64 %27, i64* %6
%28 = load i64, i64* %6
%29 = icmp eq i64 %28, -1
br i1 %29, label %33, label %30
30: ; preds = %17
%31 = load i64, i64* %6
%32 = icmp eq i64 %31, -2
br i1 %32, label %33, label %34
33: ; preds = %30, %17
br label %52
34: ; preds = %30
%35 = load i64, i64* %6
%36 = sub i64 %35, 1
%37 = load i32, i32* %4
%38 = zext i32 %37 to i64
%39 = add i64 %38, %36
%40 = trunc i64 %39 to i32
store i32 %40, i32* %4
%41 = load i32, i32* %5
%42 = call i32 @wcwidth(i32 %41)
store i32 %42, i32* %7
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %34
br label %52
45: ; preds = %34
%46 = load i32, i32* %7
%47 = load i32, i32* %8
%48 = add nsw i32 %47, %46
store i32 %48, i32* %8
br label %49
49: ; preds = %45
%50 = load i32, i32* %4
%51 = add i32 %50, 1
store i32 %51, i32* %4
br label %13
52: ; preds = %44, %33, %13
%53 = load i32, i32* %8
ret i32 %53
}

LLVM IR (-O2):
define i32 @cell_len(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = tail call i32 @strlen(i8* %0)
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = call i32 @memset(i32* %3, i32 0, i32 4)
%8 = icmp eq i32 %4, 0
br i1 %8, label %27, label %9
9: ; preds = %1, %22
%10 = phi i32 [ %24, %22 ], [ 0, %1 ]
%11 = phi i32 [ %25, %22 ], [ 0, %1 ]
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = sub i32 %4, %11
%15 = call i64 @mbrtowc(i32* %2, i8* %13, i32 %14, i32* %3)
%16 = and i64 %15, 4294967294
%17 = icmp eq i64 %16, 4294967294
br i1 %17, label %27, label %18
18: ; preds = %9
%19 = load i32, i32* %2
%20 = call i32 @wcwidth(i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %18
%23 = trunc i64 %15 to i32
%24 = add nsw i32 %20, %10
%25 = add i32 %11, %23
%26 = icmp ugt i32 %4, %25
br i1 %26, label %9, label %27
27: ; preds = %22, %9, %18, %1
%28 = phi i32 [ 0, %1 ], [ %10, %18 ], [ %10, %9 ], [ %24, %22 ]
ret i32 %28
}