#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*/ _sfree(unsigned int *);
int /*<<< orphan*/ tr_change(void *, int);
void *trim(void *addr, int len) {
  int old_len = ((long *)addr)[-2];
  unsigned *tail = (unsigned *)((char *)addr + len + 8);
  if (old_len < len + 64)
    return addr;
  tr_change(addr, len);
  ((long *)addr)[-2] = len;
  ((long *)tail)[-2] = old_len - len - 8;
  _sfree(tail);
  return addr;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x10(%rax),%rax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
add    $0x40,%ecx
cmp    %ecx,%eax
jge    116b <trim+0x4b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a8 <trim+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <tr_change@plt>
movslq -0x14(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,-0x10(%rax)
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
sub    $0x8,%eax
movslq %eax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,-0x10(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <_sfree@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    -0x10(%rdi),%ebp
lea    0x40(%r14),%eax
cmp    %ebp,%eax
jg     1169 <trim+0x49>
movslq %r14d,%r12
lea    (%rbx,%r12,1),%r15
add    $0x8,%r15
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <tr_change@plt>
mov    %r12,-0x10(%rbx)
sub    %r14d,%ebp
add    $0xfffffff8,%ebp
movslq %ebp,%rax
mov    %rax,-0x8(%rbx,%r12,1)
mov    %r15,%rdi
call   1040 <_sfree@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @trim(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
%8 = load i8*, i8** %4
%9 = bitcast i8* %8 to i64*
%10 = getelementptr inbounds i64, i64* %9, i64 -2
%11 = load i64, i64* %10
%12 = trunc i64 %11 to i32
store i32 %12, i32* %6
%13 = load i8*, i8** %4
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = getelementptr inbounds i8, i8* %16, i64 8
%18 = bitcast i8* %17 to i32*
store i32* %18, i32** %7
%19 = load i32, i32* %6
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 64
%22 = icmp slt i32 %19, %21
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = load i8*, i8** %4
store i8* %24, i8** %3
br label %45
25: ; preds = %2
%26 = load i8*, i8** %4
%27 = load i32, i32* %5
%28 = call i32 @tr_change(i8* %26, i32 %27)
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = load i8*, i8** %4
%32 = bitcast i8* %31 to i64*
%33 = getelementptr inbounds i64, i64* %32, i64 -2
store i64 %30, i64* %33
%34 = load i32, i32* %6
%35 = load i32, i32* %5
%36 = sub nsw i32 %34, %35
%37 = sub nsw i32 %36, 8
%38 = sext i32 %37 to i64
%39 = load i32*, i32** %7
%40 = bitcast i32* %39 to i64*
%41 = getelementptr inbounds i64, i64* %40, i64 -2
store i64 %38, i64* %41
%42 = load i32*, i32** %7
%43 = call i32 @_sfree(i32* %42)
%44 = load i8*, i8** %4
store i8* %44, i8** %3
br label %45
45: ; preds = %25, %23
%46 = load i8*, i8** %3
ret i8* %46
}

LLVM IR (-O2):
define i8* @trim(i8* returned %0, i32 %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 -16
%4 = bitcast i8* %3 to i64*
%5 = load i64, i64* %4
%6 = trunc i64 %5 to i32
%7 = sext i32 %1 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = getelementptr inbounds i8, i8* %8, i64 8
%10 = add nsw i32 %1, 64
%11 = icmp sgt i32 %10, %6
br i1 %11, label %21, label %12
12: ; preds = %2
%13 = bitcast i8* %9 to i32*
%14 = tail call i32 @tr_change(i8* %0, i32 %1)
store i64 %7, i64* %4
%15 = sub i32 -8, %1
%16 = add i32 %15, %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %9, i64 -16
%19 = bitcast i8* %18 to i64*
store i64 %17, i64* %19
%20 = tail call i32 @_sfree(i32* %13)
br label %21
21: ; preds = %2, %12
ret i8* %0
}