#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*/ wstring;
typedef char wchar_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ assert(char const *);
char *bm_strdup(char const *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
size_t mbstowcs(char *, char *, int);
int strlen(char const *);
int wcswidth(char *, int);
int32_t bm_utf8_string_screen_width(const char *string) {
  assert(string);
  char *mstr;
  if (!(mstr = bm_strdup(string)))
    return strlen(string);
  char *s;
  for (s = mstr; *s; ++s)
    if (*s == '\t')
      *s = ' ';
  int num_char = mbstowcs(NULL, mstr, 0) + 1;
  wchar_t *wstring = malloc((num_char + 1) * sizeof(wstring[0]));
  if (mbstowcs(wstring, mstr, num_char) == (size_t)(-1)) {
    free(wstring);
    int len = strlen(mstr);
    free(mstr);
    return len;
  }
  int32_t length = wcswidth(wstring, num_char);
  free(wstring);
  free(mstr);
  return length;
}


x86 assembly (clang, -O0):
<bm_utf8_string_screen_width>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1090 <bm_strdup@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ad <bm_utf8_string_screen_width+0x3d>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x4(%rbp)
jmp    1286 <bm_utf8_string_screen_width+0x116>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     11ef <bm_utf8_string_screen_width+0x7f>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x9,%eax
jne    11d9 <bm_utf8_string_screen_width+0x69>
mov    -0x20(%rbp),%rax
movb   $0x20,(%rax)
jmp    11de <bm_utf8_string_screen_width+0x6e>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11b5 <bm_utf8_string_screen_width+0x45>
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
call   1050 <mbstowcs@plt>
add    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
jne    125f <bm_utf8_string_screen_width+0xef>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1286 <bm_utf8_string_screen_width+0x116>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <wcswidth@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bm_utf8_string_screen_width>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1070 <assert@plt>
mov    %rbx,%rdi
call   1090 <bm_strdup@plt>
test   %rax,%rax
je     11f0 <bm_utf8_string_screen_width+0x80>
mov    %rax,%r14
jmp    1197 <bm_utf8_string_screen_width+0x27>
xchg   %ax,%ax
movb   $0x20,(%rax)
add    $0x1,%rax
movzbl (%rax),%ecx
cmp    $0x9,%cl
je     1190 <bm_utf8_string_screen_width+0x20>
test   %cl,%cl
jne    1193 <bm_utf8_string_screen_width+0x23>
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <mbstowcs@plt>
lea    0x1(%rax),%ebp
lea    0x2(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
je     11fc <bm_utf8_string_screen_width+0x8c>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <wcswidth@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
jmp    1218 <bm_utf8_string_screen_width+0xa8>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <strlen@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bm_utf8_string_screen_width(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i32 @assert(i8* %10)
%12 = load i8*, i8** %3
%13 = call i8* @bm_strdup(i8* %12)
store i8* %13, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %2
br label %68
18: ; preds = %1
%19 = load i8*, i8** %4
store i8* %19, i8** %5
br label %20
20: ; preds = %32, %18
%21 = load i8*, i8** %5
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 9
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i8*, i8** %5
store i8 32, i8* %30
br label %31
31: ; preds = %29, %24
br label %32
32: ; preds = %31
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %5
br label %20
35: ; preds = %20
%36 = load i8*, i8** %4
%37 = call i64 @mbstowcs(i8* null, i8* %36, i32 0)
%38 = add i64 %37, 1
%39 = trunc i64 %38 to i32
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = mul i64 %42, 1
%44 = trunc i64 %43 to i32
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %7
%46 = load i8*, i8** %7
%47 = load i8*, i8** %4
%48 = load i32, i32* %6
%49 = call i64 @mbstowcs(i8* %46, i8* %47, i32 %48)
%50 = icmp eq i64 %49, -1
br i1 %50, label %51, label %59
51: ; preds = %35
%52 = load i8*, i8** %7
%53 = call i32 @free(i8* %52)
%54 = load i8*, i8** %4
%55 = call i32 @strlen(i8* %54)
store i32 %55, i32* %8
%56 = load i8*, i8** %4
%57 = call i32 @free(i8* %56)
%58 = load i32, i32* %8
store i32 %58, i32* %2
br label %68
59: ; preds = %35
%60 = load i8*, i8** %7
%61 = load i32, i32* %6
%62 = call i32 @wcswidth(i8* %60, i32 %61)
store i32 %62, i32* %9
%63 = load i8*, i8** %7
%64 = call i32 @free(i8* %63)
%65 = load i8*, i8** %4
%66 = call i32 @free(i8* %65)
%67 = load i32, i32* %9
store i32 %67, i32* %2
br label %68
68: ; preds = %59, %51, %15
%69 = load i32, i32* %2
ret i32 %69
}

LLVM IR (-O2):
define i32 @bm_utf8_string_screen_width(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = tail call i8* @bm_strdup(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @strlen(i8* %0)
br label %29
7: ; preds = %1, %11
%8 = phi i8* [ %12, %11 ], [ %3, %1 ]
%9 = load i8, i8* %8
switch i8 %9, label %11 [
i8 0, label %13
i8 9, label %10
]
10: ; preds = %7
store i8 32, i8* %8
br label %11
11: ; preds = %7, %10
%12 = getelementptr inbounds i8, i8* %8, i64 1
br label %7
13: ; preds = %7
%14 = tail call i64 @mbstowcs(i8* null, i8* %3, i32 0)
%15 = trunc i64 %14 to i32
%16 = add i32 %15, 1
%17 = add i32 %15, 2
%18 = tail call i8* @malloc(i32 %17)
%19 = tail call i64 @mbstowcs(i8* %18, i8* %3, i32 %16)
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = tail call i32 @free(i8* %18)
%23 = tail call i32 @strlen(i8* %3)
%24 = tail call i32 @free(i8* %3)
br label %29
25: ; preds = %13
%26 = tail call i32 @wcswidth(i8* %18, i32 %16)
%27 = tail call i32 @free(i8* %18)
%28 = tail call i32 @free(i8* %3)
br label %29
29: ; preds = %21, %25, %5
%30 = phi i32 [ %6, %5 ], [ %23, %21 ], [ %26, %25 ]
ret i32 %30
}