#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;

/* Variables and functions */
int /*<<< orphan*/ SIZE_MAX;
int mbtowc(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int wcwidth(int /*<<< orphan*/);
size_t display_len(const char *s) {
  size_t r = 0;
  wchar_t wc;
  int len, w;
  for (; *s; s += len) {
    len = mbtowc(&wc, s, SIZE_MAX);
    if (len <= 0)
      break;
    w = wcwidth(wc);
    if (w < 0)
      break;
    r += (size_t)w;
  }
  return r;
}


x86 assembly (clang, -O0):
<display_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11a5 <display_len+0x85>
mov    -0x8(%rbp),%rsi
mov    0x2e8c(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rdi
call   1030 <mbtowc@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1169 <display_len+0x49>
jmp    11a5 <display_len+0x85>
mov    -0x14(%rbp),%edi
call   1040 <wcwidth@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1183 <display_len+0x63>
jmp    11a5 <display_len+0x85>
movslq -0x1c(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1134 <display_len+0x14>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<display_len>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmpb   $0x0,(%rdi)
je     1185 <display_len+0x65>
mov    %rdi,%rbx
xor    %r14d,%r14d
mov    0x2e9a(%rip),%r12
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <mbtowc@plt>
test   %eax,%eax
jle    1188 <display_len+0x68>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
call   1040 <wcwidth@plt>
test   %eax,%eax
js     1188 <display_len+0x68>
mov    %eax,%eax
add    %rax,%r14
mov    %ebp,%eax
cmpb   $0x0,(%rbx,%rax,1)
lea    (%rbx,%rax,1),%rbx
jne    1150 <display_len+0x30>
jmp    1188 <display_len+0x68>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@SIZE_MAX = global i32 0
define i64 @display_len(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %1, %16
%7 = phi i8* [ %20, %16 ], [ %0, %1 ]
%8 = phi i64 [ %18, %16 ], [ 0, %1 ]
%9 = load i32, i32* @SIZE_MAX
%10 = call i32 @mbtowc(i32* %2, i8* %7, i32 %9)
%11 = icmp slt i32 %10, 1
br i1 %11, label %23, label %12
12: ; preds = %6
%13 = load i32, i32* %2
%14 = call i32 @wcwidth(i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %12
%17 = zext i32 %14 to i64
%18 = add i64 %8, %17
%19 = zext i32 %10 to i64
%20 = getelementptr inbounds i8, i8* %7, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %6
23: ; preds = %16, %6, %12, %1
%24 = phi i64 [ 0, %1 ], [ %8, %12 ], [ %8, %6 ], [ %18, %16 ]
ret i64 %24
}