#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
size_t wcswidth(int /*<<< orphan*/ *, int);
scalar_t__ xmalloc(int);
int /*<<< orphan*/ xmbstowcs(int /*<<< orphan*/ *, char const *, size_t,
                             int /*<<< orphan*/ *);
size_t strwidth(const char *s) {
  wchar_t *ucs;
  size_t size;
  size_t width;
  assert(s != NULL);
  size = xmbstowcs(NULL, s, -1, NULL) + 1;
  ucs = (wchar_t *)xmalloc(sizeof(wchar_t) * size);
  xmbstowcs(ucs, s, size, NULL);
  width = wcswidth(ucs, 2048);
  free(ucs);
  return width;
}


x86 assembly (clang, -O0):
<strwidth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    $0xffffffffffffffff,%rdx
mov    %rcx,%rdi
call   1060 <xmbstowcs@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <xmbstowcs@plt>
mov    -0x10(%rbp),%rdi
mov    $0x800,%esi
call   1040 <wcswidth@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strwidth>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0xffffffffffffffff,%rdx
xor    %ecx,%ecx
call   1060 <xmbstowcs@plt>
lea    0x1(%rax),%ecx
movslq %ecx,%r14
lea    0x4(,%rax,4),%edi
call   1050 <xmalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1060 <xmbstowcs@plt>
mov    %rbx,%rdi
mov    $0x800,%esi
call   1040 <wcswidth@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @strwidth(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i8*, i8** %2
%11 = call i32 @xmbstowcs(i32* null, i8* %10, i64 -1, i32* null)
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
store i64 %13, i64* %4
%14 = load i64, i64* %4
%15 = mul i64 4, %14
%16 = trunc i64 %15 to i32
%17 = call i64 @xmalloc(i32 %16)
%18 = inttoptr i64 %17 to i32*
store i32* %18, i32** %3
%19 = load i32*, i32** %3
%20 = load i8*, i8** %2
%21 = load i64, i64* %4
%22 = call i32 @xmbstowcs(i32* %19, i8* %20, i64 %21, i32* null)
%23 = load i32*, i32** %3
%24 = call i64 @wcswidth(i32* %23, i32 2048)
store i64 %24, i64* %5
%25 = load i32*, i32** %3
%26 = call i32 @free(i32* %25)
%27 = load i64, i64* %5
ret i64 %27
}

LLVM IR (-O2):
define i64 @strwidth(i8* %0) {
%2 = icmp ne i8* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 @xmbstowcs(i32* null, i8* %0, i64 -1, i32* null)
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
%8 = shl i32 %6, 2
%9 = tail call i64 @xmalloc(i32 %8)
%10 = inttoptr i64 %9 to i32*
%11 = tail call i32 @xmbstowcs(i32* %10, i8* %0, i64 %7, i32* null)
%12 = tail call i64 @wcswidth(i32* %10, i32 2048)
%13 = tail call i32 @free(i32* %10)
ret i64 %12
}