#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 */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t const);
size_t utf8_nstrsnlen(char const *, size_t const);
size_t utf8_strsw(char const *);
void truncate_with_ellipsis(const char msg[], size_t width, char buffer[]) {
  const size_t screen_len = utf8_strsw(msg);
  const size_t screen_left_len = (width - 3) / 2;
  const size_t screen_right_len = (width - 3) - screen_left_len;
  const size_t left = utf8_nstrsnlen(msg, screen_left_len);
  const size_t right = utf8_nstrsnlen(msg, screen_len - screen_right_len);
  strncpy(buffer, msg, left);
  strcpy(buffer + left, "...");
  strcpy(buffer + left + 3, msg + right);
  assert(utf8_strsw(buffer) == width);
}


x86 assembly (clang, -O0):
<truncate_with_ellipsis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <utf8_strsw@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x3,%rax
shr    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x3,%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <utf8_nstrsnlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
sub    -0x30(%rbp),%rsi
call   1070 <utf8_nstrsnlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rdi
add    -0x38(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
add    -0x38(%rbp),%rdi
add    $0x3,%rdi
mov    -0x8(%rbp),%rsi
add    -0x40(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1050 <utf8_strsw@plt>
cmp    -0x10(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<truncate_with_ellipsis>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <utf8_strsw@plt>
mov    %rax,%r12
lea    -0x3(%r14),%rsi
shr    %rsi
mov    %rsi,%rbp
sub    %r14,%rbp
mov    %rbx,%rdi
call   1070 <utf8_nstrsnlen@plt>
mov    %rax,%r13
lea    (%r12,%rbp,1),%rsi
add    $0x3,%rsi
mov    %rbx,%rdi
call   1070 <utf8_nstrsnlen@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1030 <strncpy@plt>
lea    (%r15,%r13,1),%rdi
lea    0xe50(%rip),%rsi
call   1040 <strcpy@plt>
lea    (%r15,%r13,1),%rdi
add    $0x3,%rdi
add    %rbx,%r12
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1050 <utf8_strsw@plt>
xor    %edi,%edi
cmp    %r14,%rax
sete   %dil
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <assert@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"...\00"
define void @truncate_with_ellipsis(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%12 = load i8*, i8** %4
%13 = call i64 @utf8_strsw(i8* %12)
store i64 %13, i64* %7
%14 = load i64, i64* %5
%15 = sub i64 %14, 3
%16 = udiv i64 %15, 2
store i64 %16, i64* %8
%17 = load i64, i64* %5
%18 = sub i64 %17, 3
%19 = load i64, i64* %8
%20 = sub i64 %18, %19
store i64 %20, i64* %9
%21 = load i8*, i8** %4
%22 = load i64, i64* %8
%23 = call i64 @utf8_nstrsnlen(i8* %21, i64 %22)
store i64 %23, i64* %10
%24 = load i8*, i8** %4
%25 = load i64, i64* %7
%26 = load i64, i64* %9
%27 = sub i64 %25, %26
%28 = call i64 @utf8_nstrsnlen(i8* %24, i64 %27)
store i64 %28, i64* %11
%29 = load i8*, i8** %6
%30 = load i8*, i8** %4
%31 = load i64, i64* %10
%32 = call i32 @strncpy(i8* %29, i8* %30, i64 %31)
%33 = load i8*, i8** %6
%34 = load i64, i64* %10
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%37 = load i8*, i8** %6
%38 = load i64, i64* %10
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = getelementptr inbounds i8, i8* %39, i64 3
%41 = load i8*, i8** %4
%42 = load i64, i64* %11
%43 = getelementptr inbounds i8, i8* %41, i64 %42
%44 = call i32 @strcpy(i8* %40, i8* %43)
%45 = load i8*, i8** %6
%46 = call i64 @utf8_strsw(i8* %45)
%47 = load i64, i64* %5
%48 = icmp eq i64 %46, %47
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"...\00"
define void @truncate_with_ellipsis(i8* %0, i64 %1, i8* %2) {
%4 = tail call i64 @utf8_strsw(i8* %0)
%5 = add i64 %1, -3
%6 = lshr i64 %5, 1
%7 = sub i64 3, %1
%8 = add i64 %6, %7
%9 = tail call i64 @utf8_nstrsnlen(i8* %0, i64 %6)
%10 = add i64 %8, %4
%11 = tail call i64 @utf8_nstrsnlen(i8* %0, i64 %10)
%12 = tail call i32 @strncpy(i8* %2, i8* %0, i64 %9)
%13 = getelementptr inbounds i8, i8* %2, i64 %9
%14 = tail call i32 @strcpy(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds i8, i8* %13, i64 3
%16 = getelementptr inbounds i8, i8* %0, i64 %11
%17 = tail call i32 @strcpy(i8* %15, i8* %16)
%18 = tail call i64 @utf8_strsw(i8* %2)
%19 = icmp eq i64 %18, %1
%20 = zext i1 %19 to i32
%21 = tail call i32 @assert(i32 %20)
ret void
}