#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*/ copy_str(char *, scalar_t__, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
size_t utf8_nstrsnlen(char *, size_t);
size_t utf8_strsw(char *);
char *right_ellipsis(char str[], size_t max_width) {
  size_t width;
  size_t prefix;
  if (max_width == 0U) {
    return NULL;
  }
  width = utf8_strsw(str);
  if (width <= max_width) {
    return str;
  }
  if (max_width <= 3U) {
    copy_str(str, strlen(str) + 1U, "...");
    str[max_width] = '\0';
    return str;
  }
  prefix = utf8_nstrsnlen(str, max_width - 3U);
  strcpy(&str[prefix], "...");
  return str;
}


x86 assembly (clang, -O0):
<right_ellipsis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1178 <right_ellipsis+0x28>
movq   $0x0,-0x8(%rbp)
jmp    121d <right_ellipsis+0xcd>
mov    -0x10(%rbp),%rdi
call   1040 <utf8_strsw@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     11a0 <right_ellipsis+0x50>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121d <right_ellipsis+0xcd>
cmpq   $0x3,-0x18(%rbp)
ja     11ec <right_ellipsis+0x9c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
lea    0xe32(%rip),%rdx
call   1060 <copy_str@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121d <right_ellipsis+0xcd>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    $0x3,%rsi
call   1070 <utf8_nstrsnlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<right_ellipsis>:
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     1191 <right_ellipsis+0x41>
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <utf8_strsw@plt>
cmp    %rbx,%rax
jbe    11b5 <right_ellipsis+0x65>
cmp    $0x3,%rbx
ja     1196 <right_ellipsis+0x46>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rsi
lea    0xe7e(%rip),%rdx
mov    %r14,%rdi
call   1060 <copy_str@plt>
movb   $0x0,(%r14,%rbx,1)
jmp    11b5 <right_ellipsis+0x65>
xor    %r14d,%r14d
jmp    11b5 <right_ellipsis+0x65>
add    $0xfffffffffffffffd,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <utf8_nstrsnlen@plt>
lea    (%rax,%r14,1),%rdi
lea    0xe50(%rip),%rsi
call   1030 <strcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"...\00"
define i8* @right_ellipsis(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %42
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i64 @utf8_strsw(i8* %12)
store i64 %13, i64* %6
%14 = load i64, i64* %6
%15 = load i64, i64* %5
%16 = icmp ule i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = load i8*, i8** %4
store i8* %18, i8** %3
br label %42
19: ; preds = %11
%20 = load i64, i64* %5
%21 = icmp ule i64 %20, 3
br i1 %21, label %22, label %32
22: ; preds = %19
%23 = load i8*, i8** %4
%24 = load i8*, i8** %4
%25 = call i64 @strlen(i8* %24)
%26 = add nsw i64 %25, 1
%27 = call i32 @copy_str(i8* %23, i64 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%28 = load i8*, i8** %4
%29 = load i64, i64* %5
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8 0, i8* %30
%31 = load i8*, i8** %4
store i8* %31, i8** %3
br label %42
32: ; preds = %19
%33 = load i8*, i8** %4
%34 = load i64, i64* %5
%35 = sub i64 %34, 3
%36 = call i64 @utf8_nstrsnlen(i8* %33, i64 %35)
store i64 %36, i64* %7
%37 = load i8*, i8** %4
%38 = load i64, i64* %7
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = call i32 @strcpy(i8* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%41 = load i8*, i8** %4
store i8* %41, i8** %3
br label %42
42: ; preds = %32, %22, %17, %10
%43 = load i8*, i8** %3
ret i8* %43
}

LLVM IR (-O2):
@.str = [4 x i8] c"...\00"
define i8* @right_ellipsis(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %19, label %4
4: ; preds = %2
%5 = tail call i64 @utf8_strsw(i8* %0)
%6 = icmp ugt i64 %5, %1
br i1 %6, label %7, label %19
7: ; preds = %4
%8 = icmp ult i64 %1, 4
br i1 %8, label %9, label %14
9: ; preds = %7
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = add nsw i64 %10, 1
%12 = tail call i32 @copy_str(i8* %0, i64 %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds i8, i8* %0, i64 %1
store i8 0, i8* %13
br label %19
14: ; preds = %7
%15 = add i64 %1, -3
%16 = tail call i64 @utf8_nstrsnlen(i8* %0, i64 %15)
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = tail call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %4, %2, %14, %9
%20 = phi i8* [ %0, %9 ], [ %0, %14 ], [ null, %2 ], [ %0, %4 ]
ret i8* %20
}