#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);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int wcstombs(char *, int /*<<< orphan*/ *, size_t);
int wcswidth(int /*<<< orphan*/ *, int);
scalar_t__ wcwidth(int /*<<< orphan*/);
scalar_t__ xmalloc(size_t);
int /*<<< orphan*/ xmbstowcs(int /*<<< orphan*/ *, char const *, size_t,
                             int /*<<< orphan*/ *);
char *xstrtail(const char *str, const int len) {
  wchar_t *ucs;
  wchar_t *ucs_tail;
  size_t size;
  int width;
  char *tail;
  assert(str != NULL);
  assert(len > 0);
  size = xmbstowcs(NULL, str, -1, NULL) + 1;
  ucs = (wchar_t *)xmalloc(sizeof(wchar_t) * size);
  xmbstowcs(ucs, str, size, NULL);
  ucs_tail = ucs;
  width = wcswidth(ucs, 2048);
  assert(width >= 0);
  while (width > len)
    width -= wcwidth(*ucs_tail++);
  size = wcstombs(NULL, ucs_tail, 0) + 1;
  tail = (char *)xmalloc(size);
  wcstombs(tail, ucs_tail, size);
  free(ucs);
  return tail;
}


x86 assembly (clang, -O0):
<xstrtail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
setg   %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,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
shl    $0x2,%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <xmbstowcs@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x800,%esi
call   1040 <wcswidth@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    1246 <xstrtail+0xd6>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%edi
call   1080 <wcwidth@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
jmp    1212 <xstrtail+0xa2>
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1090 <wcstombs@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1090 <wcstombs@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrtail>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
test   %r15d,%r15d
setg   %dil
call   1070 <assert@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xffffffffffffffff,%rdx
xor    %ecx,%ecx
call   1060 <xmbstowcs@plt>
movslq %eax,%rbp
lea    0x4(,%rbp,4),%rdi
add    $0x1,%rbp
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1060 <xmbstowcs@plt>
mov    %r14,%rdi
mov    $0x800,%esi
call   1040 <wcswidth@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1070 <assert@plt>
mov    %r14,%rbx
cmp    %r15d,%ebp
jle    1212 <xstrtail+0xa2>
mov    %r14,%rbx
nopl   0x0(%rax)
mov    (%rbx),%edi
add    $0x4,%rbx
call   1080 <wcwidth@plt>
sub    %eax,%ebp
cmp    %r15d,%ebp
jg     1200 <xstrtail+0x90>
xor    %edi,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1090 <wcstombs@plt>
add    $0x1,%eax
movslq %eax,%rbp
mov    %rbp,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1090 <wcstombs@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @xstrtail(i8* %0, i32 %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp sgt i32 %1, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i32 @xmbstowcs(i32* null, i8* %0, i64 -1, i32* null)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = shl nsw i64 %11, 2
%13 = tail call i64 @xmalloc(i64 %12)
%14 = inttoptr i64 %13 to i32*
%15 = tail call i32 @xmbstowcs(i32* %14, i8* %0, i64 %11, i32* null)
%16 = tail call i32 @wcswidth(i32* %14, i32 2048)
%17 = xor i32 %16, -1
%18 = lshr i32 %17, 31
%19 = tail call i32 @assert(i32 %18)
%20 = icmp sgt i32 %16, %1
br i1 %20, label %21, label %30
21: ; preds = %2, %21
%22 = phi i32* [ %24, %21 ], [ %14, %2 ]
%23 = phi i32 [ %28, %21 ], [ %16, %2 ]
%24 = getelementptr inbounds i32, i32* %22, i64 1
%25 = load i32, i32* %22
%26 = tail call i64 @wcwidth(i32 %25)
%27 = trunc i64 %26 to i32
%28 = sub i32 %23, %27
%29 = icmp sgt i32 %28, %1
br i1 %29, label %21, label %30
30: ; preds = %21, %2
%31 = phi i32* [ %14, %2 ], [ %24, %21 ]
%32 = tail call i32 @wcstombs(i8* null, i32* %31, i64 0)
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = tail call i64 @xmalloc(i64 %34)
%36 = inttoptr i64 %35 to i8*
%37 = tail call i32 @wcstombs(i8* %36, i32* %31, i64 %34)
%38 = tail call i32 @free(i32* %14)
ret i8* %36
}