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

/* Variables and functions */
size_t mbstowcs(int *, char const *, size_t);
size_t wcslen(int *);
int /*<<< orphan*/ wcsncat(int *, char *, size_t);
int wcwidth(int);
size_t render_filename(wchar_t *buffer, size_t len, size_t width,
                       const char *filename) {
  size_t wc_count = mbstowcs(NULL, filename, 0);
  if (wc_count == (size_t)-1) {
    buffer[0] = L'\0';
    if (len > 3)
      len = 3;
    wcsncat(buffer, L"???", len);
    return wcslen(buffer);
  }
  wchar_t name[wc_count + 1];
  mbstowcs(name, filename, wc_count);
  size_t char_count = 0, display_count = 0;
  for (size_t i = 0; i < wc_count; i++) {
    int w = wcwidth(name[i]);
    if (w == -1)
      continue;
    if (w + display_count > width)
      break;
    if (char_count < len)
      buffer[char_count] = name[i];
    display_count += w;
    char_count++;
  }
  buffer[char_count] = L'\0';
  return char_count;
}


x86 assembly (clang, -O0):
<render_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1030 <mbstowcs@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    11ba <render_filename+0x7a>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x3,-0x18(%rbp)
jbe    1194 <render_filename+0x54>
movq   $0x3,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe5d(%rip),%rsi
call   1040 <wcsncat@plt>
mov    -0x10(%rbp),%rdi
call   1050 <wcslen@plt>
mov    %rax,-0x8(%rbp)
jmp    12c0 <render_filename+0x180>
mov    -0x30(%rbp),%rcx
mov    %rcx,%rax
add    $0x1,%rax
mov    %rsp,%rdx
mov    %rdx,-0x38(%rbp)
lea    0x13(,%rcx,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <mbstowcs@plt>
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    12a2 <render_filename+0x162>
mov    -0x68(%rbp),%rax
mov    -0x58(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1060 <wcwidth@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
jne    123e <render_filename+0xfe>
jmp    1291 <render_filename+0x151>
movslq -0x5c(%rbp),%rax
add    -0x50(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1255 <render_filename+0x115>
jmp    12a2 <render_filename+0x162>
mov    -0x48(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1279 <render_filename+0x139>
mov    -0x68(%rbp),%rax
mov    -0x58(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
movslq -0x5c(%rbp),%rax
add    -0x50(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    120e <render_filename+0xce>
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<render_filename>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdx,-0x48(%rbp)
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edi,%edi
mov    %rcx,%rsi
xor    %edx,%edx
call   1030 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
je     11fa <render_filename+0xba>
mov    %rax,%r13
mov    %rbx,-0x30(%rbp)
mov    %rsp,-0x38(%rbp)
mov    %rsp,%rbx
lea    0x13(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <mbstowcs@plt>
test   %r13,%r13
je     1232 <render_filename+0xf2>
mov    %r15,-0x40(%rbp)
xor    %r15d,%r15d
xor    %r12d,%r12d
xor    %r14d,%r14d
jmp    11cd <render_filename+0x8d>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14
add    $0x1,%r15
cmp    %r15,%r13
je     1235 <render_filename+0xf5>
mov    (%rbx,%r15,4),%edi
call   1060 <wcwidth@plt>
cmp    $0xffffffff,%eax
je     11c4 <render_filename+0x84>
cltq
add    %rax,%r12
cmp    -0x48(%rbp),%r12
ja     1235 <render_filename+0xf5>
cmp    -0x40(%rbp),%r14
jae    11c0 <render_filename+0x80>
mov    (%rbx,%r15,4),%eax
mov    -0x30(%rbp),%rcx
mov    %eax,(%rcx,%r14,4)
jmp    11c0 <render_filename+0x80>
movl   $0x0,(%rbx)
cmp    $0x3,%r15
mov    $0x3,%edx
cmovb  %r15,%rdx
lea    0xdec(%rip),%rsi
mov    %rbx,%rdi
call   1040 <wcsncat@plt>
mov    %rbx,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <wcslen@plt>
xor    %r14d,%r14d
mov    -0x30(%rbp),%rax
movl   $0x0,(%rax,%r14,4)
mov    -0x38(%rbp),%rsp
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i32] [i32 63, i32 63, i32 63, i32 0]
define i64 @render_filename(i32* %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i32
store i32* %0, i32** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%17 = load i8*, i8** %9
%18 = call i64 @mbstowcs(i32* null, i8* %17, i64 0)
store i64 %18, i64* %10
%19 = load i64, i64* %10
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %33
21: ; preds = %4
%22 = load i32*, i32** %6
%23 = getelementptr inbounds i32, i32* %22, i64 0
store i32 0, i32* %23
%24 = load i64, i64* %7
%25 = icmp ugt i64 %24, 3
br i1 %25, label %26, label %27
26: ; preds = %21
store i64 3, i64* %7
br label %27
27: ; preds = %26, %21
%28 = load i32*, i32** %6
%29 = load i64, i64* %7
%30 = call i32 @wcsncat(i32* %28, i8* bitcast ([4 x i32]* @.str to i8*), i64 %29)
%31 = load i32*, i32** %6
%32 = call i64 @wcslen(i32* %31)
store i64 %32, i64* %5
br label %88
33: ; preds = %4
%34 = load i64, i64* %10
%35 = add i64 %34, 1
%36 = call i8* @llvm.stacksave()
store i8* %36, i8** %11
%37 = alloca i32, i64 %35
store i64 %35, i64* %12
%38 = load i8*, i8** %9
%39 = load i64, i64* %10
%40 = call i64 @mbstowcs(i32* %37, i8* %38, i64 %39)
store i64 0, i64* %13
store i64 0, i64* %14
store i64 0, i64* %15
br label %41
41: ; preds = %79, %33
%42 = load i64, i64* %15
%43 = load i64, i64* %10
%44 = icmp ult i64 %42, %43
br i1 %44, label %45, label %82
45: ; preds = %41
%46 = load i64, i64* %15
%47 = getelementptr inbounds i32, i32* %37, i64 %46
%48 = load i32, i32* %47
%49 = call i32 @wcwidth(i32 %48)
store i32 %49, i32* %16
%50 = load i32, i32* %16
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %45
br label %79
53: ; preds = %45
%54 = load i32, i32* %16
%55 = sext i32 %54 to i64
%56 = load i64, i64* %14
%57 = add i64 %55, %56
%58 = load i64, i64* %8
%59 = icmp ugt i64 %57, %58
br i1 %59, label %60, label %61
60: ; preds = %53
br label %82
61: ; preds = %53
%62 = load i64, i64* %13
%63 = load i64, i64* %7
%64 = icmp ult i64 %62, %63
br i1 %64, label %65, label %72
65: ; preds = %61
%66 = load i64, i64* %15
%67 = getelementptr inbounds i32, i32* %37, i64 %66
%68 = load i32, i32* %67
%69 = load i32*, i32** %6
%70 = load i64, i64* %13
%71 = getelementptr inbounds i32, i32* %69, i64 %70
store i32 %68, i32* %71
br label %72
72: ; preds = %65, %61
%73 = load i32, i32* %16
%74 = sext i32 %73 to i64
%75 = load i64, i64* %14
%76 = add i64 %75, %74
store i64 %76, i64* %14
%77 = load i64, i64* %13
%78 = add i64 %77, 1
store i64 %78, i64* %13
br label %79
79: ; preds = %72, %52
%80 = load i64, i64* %15
%81 = add i64 %80, 1
store i64 %81, i64* %15
br label %41
82: ; preds = %60, %41
%83 = load i32*, i32** %6
%84 = load i64, i64* %13
%85 = getelementptr inbounds i32, i32* %83, i64 %84
store i32 0, i32* %85
%86 = load i64, i64* %13
store i64 %86, i64* %5
%87 = load i8*, i8** %11
br label %88
88: ; preds = %82, %27
%89 = load i64, i64* %5
ret i64 %89
}

LLVM IR (-O2):
@.str = [4 x i32] [i32 63, i32 63, i32 63, i32 0]
define i64 @render_filename(i32* %0, i64 %1, i64 %2, i8* %3) {
%5 = tail call i64 @mbstowcs(i32* null, i8* %3, i64 0)
%6 = icmp eq i64 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %4
store i32 0, i32* %0
%8 = icmp ult i64 %1, 3
%9 = select i1 %8, i64 %1, i64 3
%10 = tail call i32 @wcsncat(i32* %0, i8* bitcast ([4 x i32]* @.str to i8*), i64 %9)
%11 = tail call i64 @wcslen(i32* %0)
br label %45
12: ; preds = %4
%13 = add nuw i64 %5, 1
%14 = tail call i8* @llvm.stacksave()
%15 = alloca i32, i64 %13
%16 = call i64 @mbstowcs(i32* %15, i8* %3, i64 %5)
%17 = icmp eq i64 %5, 0
br i1 %17, label %42, label %18
18: ; preds = %12, %37
%19 = phi i64 [ %40, %37 ], [ 0, %12 ]
%20 = phi i64 [ %39, %37 ], [ 0, %12 ]
%21 = phi i64 [ %38, %37 ], [ 0, %12 ]
%22 = getelementptr inbounds i32, i32* %15, i64 %19
%23 = load i32, i32* %22
%24 = call i32 @wcwidth(i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %37, label %26
26: ; preds = %18
%27 = sext i32 %24 to i64
%28 = add i64 %20, %27
%29 = icmp ugt i64 %28, %2
br i1 %29, label %42, label %30
30: ; preds = %26
%31 = icmp ult i64 %21, %1
br i1 %31, label %32, label %35
32: ; preds = %30
%33 = load i32, i32* %22
%34 = getelementptr inbounds i32, i32* %0, i64 %21
store i32 %33, i32* %34
br label %35
35: ; preds = %32, %30
%36 = add i64 %21, 1
br label %37
37: ; preds = %35, %18
%38 = phi i64 [ %21, %18 ], [ %36, %35 ]
%39 = phi i64 [ %20, %18 ], [ %28, %35 ]
%40 = add nuw i64 %19, 1
%41 = icmp eq i64 %40, %5
br i1 %41, label %42, label %18
42: ; preds = %37, %26, %12
%43 = phi i64 [ 0, %12 ], [ %21, %26 ], [ %38, %37 ]
%44 = getelementptr inbounds i32, i32* %0, i64 %43
store i32 0, i32* %44
br label %45
45: ; preds = %42, %7
%46 = phi i64 [ %11, %7 ], [ %43, %42 ]
ret i64 %46
}