#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*/ ell;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *trunc_string(const char *src, char *dst, int len) {
  static const char ell[] = "...";
  assert(len > sizeof(ell));
  if (dst[len - 1] = '\0', strncpy(dst, src, len), dst[len - 1] != '\0')
    strcpy(dst + len - sizeof(ell), ell);
  return dst;
}


x86 assembly (clang, -O0):
<trunc_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11ae <trunc_string+0x7e>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xe57(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trunc_string>:
push   %r15
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %edi,%edi
cmp    $0x5,%edx
setae  %dil
call   1050 <assert@plt>
movslq %ebx,%rbx
movb   $0x0,-0x1(%rbx,%r14,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
cmpb   $0x0,-0x1(%rbx,%r14,1)
je     117d <trunc_string+0x4d>
lea    (%r14,%rbx,1),%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xe88(%rip),%rsi
call   1040 <strcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@trunc_string.ell = internal constant [4 x i8] c"...\00"
define i8* @trunc_string(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = load i32, i32* %6
%8 = sext i32 %7 to i64
%9 = icmp ugt i64 %8, 4
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8*, i8** %5
%13 = load i32, i32* %6
%14 = sub nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
store i8 0, i8* %16
%17 = load i8*, i8** %5
%18 = load i8*, i8** %4
%19 = load i32, i32* %6
%20 = call i32 @strncpy(i8* %17, i8* %18, i32 %19)
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %3
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = getelementptr inbounds i8, i8* %33, i64 -4
%35 = call i32 @strcpy(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @trunc_string.ell, i64 0, i64 0))
br label %36
36: ; preds = %29, %3
%37 = load i8*, i8** %5
ret i8* %37
}

LLVM IR (-O2):
@trunc_string.ell = internal constant [4 x i8] c"...\00"
define i8* @trunc_string(i8* %0, i8* returned %1, i32 %2) {
%4 = icmp ugt i32 %2, 4
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = add nsw i32 %2, -1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
store i8 0, i8* %9
%10 = tail call i32 @strncpy(i8* %1, i8* %0, i32 %2)
%11 = load i8, i8* %9
%12 = icmp eq i8 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %3
%14 = sext i32 %2 to i64
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = getelementptr inbounds i8, i8* %15, i64 -4
%17 = tail call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @trunc_string.ell, i64 0, i64 0))
br label %18
18: ; preds = %13, %3
ret i8* %1
}