#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 */
size_t EACCES;
scalar_t__ calloc(int, size_t);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
size_t truncate_buffer(char **target, size_t bufferlength,
                       size_t newbufferlength) {
  if (newbufferlength <= bufferlength) {
    return newbufferlength;
  }
  char *old = *target;
  *target = (char *)calloc(1, newbufferlength);
  if (!*target) {
    *target = old;
    return -EACCES;
  }
  memcpy(*target, old, bufferlength);
  free(old);
  return newbufferlength;
}


x86 assembly (clang, -O0):
<truncate_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     115f <truncate_buffer+0x2f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d5 <truncate_buffer+0xa5>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11b0 <truncate_buffer+0x80>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
mov    0x2e44(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    11d5 <truncate_buffer+0xa5>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<truncate_buffer>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
cmp    %rsi,%rdx
jbe    1187 <truncate_buffer+0x57>
mov    %rsi,%r15
mov    %rdi,%r12
mov    (%rdi),%r14
mov    $0x1,%edi
mov    %rbx,%rsi
call   1040 <calloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     1177 <truncate_buffer+0x47>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1187 <truncate_buffer+0x57>
mov    %r14,(%r12)
mov    0x2e66(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EACCES = global i64 0
define i64 @truncate_buffer(i8** %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
%10 = load i64, i64* %6
%11 = icmp ule i64 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = load i64, i64* %7
store i64 %13, i64* %4
br label %38
14: ; preds = %3
%15 = load i8**, i8*** %5
%16 = load i8*, i8** %15
store i8* %16, i8** %8
%17 = load i64, i64* %7
%18 = call i64 @calloc(i32 1, i64 %17)
%19 = inttoptr i64 %18 to i8*
%20 = load i8**, i8*** %5
store i8* %19, i8** %20
%21 = load i8**, i8*** %5
%22 = load i8*, i8** %21
%23 = icmp ne i8* %22, null
br i1 %23, label %29, label %24
24: ; preds = %14
%25 = load i8*, i8** %8
%26 = load i8**, i8*** %5
store i8* %25, i8** %26
%27 = load i64, i64* @EACCES
%28 = sub i64 0, %27
store i64 %28, i64* %4
br label %38
29: ; preds = %14
%30 = load i8**, i8*** %5
%31 = load i8*, i8** %30
%32 = load i8*, i8** %8
%33 = load i64, i64* %6
%34 = call i32 @memcpy(i8* %31, i8* %32, i64 %33)
%35 = load i8*, i8** %8
%36 = call i32 @free(i8* %35)
%37 = load i64, i64* %7
store i64 %37, i64* %4
br label %38
38: ; preds = %29, %24, %12
%39 = load i64, i64* %4
ret i64 %39
}

LLVM IR (-O2):
@EACCES = global i64 0
define i64 @truncate_buffer(i8** nocapture %0, i64 %1, i64 %2) {
%4 = icmp ugt i64 %2, %1
br i1 %4, label %5, label %16
5: ; preds = %3
%6 = load i8*, i8** %0
%7 = tail call i64 @calloc(i32 1, i64 %2)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %0
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %13
10: ; preds = %5
store i8* %6, i8** %0
%11 = load i64, i64* @EACCES
%12 = sub i64 0, %11
br label %16
13: ; preds = %5
%14 = tail call i32 @memcpy(i8* %8, i8* %6, i64 %1)
%15 = tail call i32 @free(i8* %6)
br label %16
16: ; preds = %10, %13, %3
%17 = phi i64 [ %2, %3 ], [ %2, %13 ], [ %12, %10 ]
ret i64 %17
}