#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 */
scalar_t__ is_double_width(unsigned short);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ utf8_to_utf16(char *, unsigned short *);
unsigned text_width(char *utf8) {
  unsigned short utf16[strlen(utf8) + 1];
  utf8_to_utf16(utf8, utf16);
  unsigned i, w = 0;
  for (i = 0; utf16[i]; i++, w++) {
    if (is_double_width(utf16[i]))
      w++;
  }
  return w;
}


x86 assembly (clang, -O0):
<text_width>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    0xf(%rax,%rax,1),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x28(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <utf8_to_utf16@plt>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
cmpw   $0x0,(%rax,%rcx,2)
je     11d1 <text_width+0xa1>
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movzwl (%rax,%rcx,2),%edi
call   1040 <is_double_width@plt>
cmp    $0x0,%rax
je     11b5 <text_width+0x85>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ba <text_width+0x8a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1180 <text_width+0x50>
mov    -0x20(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<text_width>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%r12
lea    (%rax,%rax,1),%rbx
add    $0xf,%rbx
and    $0xfffffffffffffff0,%rbx
mov    %r12,%r14
sub    %rbx,%r14
mov    %r14,%rsp
neg    %rbx
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <utf8_to_utf16@plt>
movzwl (%r12,%rbx,1),%eax
xor    %ebx,%ebx
test   %ax,%ax
je     11a3 <text_width+0x73>
mov    $0x1,%r15d
xchg   %ax,%ax
movzwl %ax,%edi
call   1040 <is_double_width@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebx
add    $0x1,%ebx
mov    %r15d,%eax
movzwl (%r14,%rax,2),%eax
add    $0x1,%r15d
test   %ax,%ax
jne    1180 <text_width+0x50>
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @text_width(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 1
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i16, i64 %10
store i64 %10, i64* %4
%13 = load i8*, i8** %2
%14 = call i32 @utf8_to_utf16(i8* %13, i16* %12)
store i32 0, i32* %6
store i32 0, i32* %5
br label %15
15: ; preds = %32, %1
%16 = load i32, i32* %5
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i16, i16* %12, i64 %17
%19 = load i16, i16* %18
%20 = icmp ne i16 %19, 0
br i1 %20, label %21, label %37
21: ; preds = %15
%22 = load i32, i32* %5
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i16, i16* %12, i64 %23
%25 = load i16, i16* %24
%26 = call i64 @is_double_width(i16 zeroext %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = load i32, i32* %6
%30 = add i32 %29, 1
store i32 %30, i32* %6
br label %31
31: ; preds = %28, %21
br label %32
32: ; preds = %31
%33 = load i32, i32* %5
%34 = add i32 %33, 1
store i32 %34, i32* %5
%35 = load i32, i32* %6
%36 = add i32 %35, 1
store i32 %36, i32* %6
br label %15
37: ; preds = %15
%38 = load i32, i32* %6
%39 = load i8*, i8** %3
ret i32 %38
}

LLVM IR (-O2):
define i32 @text_width(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i16, i64 %4
%6 = call i32 @utf8_to_utf16(i8* %0, i16* %5)
%7 = load i16, i16* %5
%8 = icmp eq i16 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %1, %9
%10 = phi i16 [ %21, %9 ], [ %7, %1 ]
%11 = phi i32 [ %18, %9 ], [ 0, %1 ]
%12 = phi i32 [ %16, %9 ], [ 0, %1 ]
%13 = call i64 @is_double_width(i16 zeroext %10)
%14 = icmp ne i64 %13, 0
%15 = zext i1 %14 to i32
%16 = add i32 %12, 1
%17 = add i32 %11, 1
%18 = add i32 %17, %15
%19 = zext i32 %16 to i64
%20 = getelementptr inbounds i16, i16* %5, i64 %19
%21 = load i16, i16* %20
%22 = icmp eq i16 %21, 0
br i1 %22, label %23, label %9
23: ; preds = %9, %1
%24 = phi i32 [ 0, %1 ], [ %18, %9 ]
ret i32 %24
}