#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 */
scalar_t__ strlen(char *);
char *shortenstr(char *str, int len) {
  int i = 0;
  int slen = (int)strlen(str);
  if (slen < len + 1)
    return str;
  while (i++ < (slen - len))
    str++;
  str[0] = '+';
  return str;
}


x86 assembly (clang, -O0):
<shortenstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1150 <shortenstr+0x40>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    118e <shortenstr+0x7e>
jmp    1155 <shortenstr+0x45>
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
sub    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jge    117f <shortenstr+0x6f>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1155 <shortenstr+0x45>
mov    -0x10(%rbp),%rax
movb   $0x2b,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shortenstr>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
call   1030 <strlen@plt>
cmp    %ebx,%eax
jle    1133 <shortenstr+0x23>
not    %ebx
add    %eax,%ebx
movb   $0x2b,0x1(%rbx,%r14,1)
add    %rbx,%r14
add    $0x1,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @shortenstr(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
%8 = load i8*, i8** %4
%9 = call i64 @strlen(i8* %8)
%10 = trunc i64 %9 to i32
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = load i32, i32* %5
%13 = add nsw i32 %12, 1
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i8*, i8** %4
store i8* %16, i8** %3
br label %32
17: ; preds = %2
br label %18
18: ; preds = %25, %17
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
store i32 %20, i32* %6
%21 = load i32, i32* %7
%22 = load i32, i32* %5
%23 = sub nsw i32 %21, %22
%24 = icmp slt i32 %19, %23
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %4
br label %18
28: ; preds = %18
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i64 0
store i8 43, i8* %30
%31 = load i8*, i8** %4
store i8* %31, i8** %3
br label %32
32: ; preds = %28, %15
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
define i8* @shortenstr(i8* %0, i32 %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = trunc i64 %3 to i32
%5 = icmp sgt i32 %4, %1
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = xor i32 %1, -1
%8 = add i32 %7, %4
%9 = zext i32 %8 to i64
%10 = add nuw nsw i64 %9, 1
%11 = getelementptr i8, i8* %0, i64 %10
store i8 43, i8* %11
br label %12
12: ; preds = %2, %6
%13 = phi i8* [ %11, %6 ], [ %0, %2 ]
ret i8* %13
}