#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 scalar_t__ ret_ptr;

/* Variables and functions */
int /*<<< orphan*/ __softboundmpx_copy_metadata(void *, void *, size_t);
int /*<<< orphan*/ __softboundmpx_store_return_metadata(void *, char *);
void *realloc(void *, size_t);
__attribute__((__weak__, __always___)) void *softboundmpx_realloc(void *ptr,
                                                                  size_t size) {
  void *ret_ptr = realloc(ptr, size);
  __softboundmpx_store_return_metadata(ret_ptr, (char *)(ret_ptr) + size);
  if (ret_ptr != ptr) {
    __softboundmpx_copy_metadata(ret_ptr, ptr, size);
  }
  return ret_ptr;
}


x86 assembly (clang, -O0):
<softboundmpx_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    -0x10(%rbp),%rsi
call   1050 <__softboundmpx_store_return_metadata@plt>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     1181 <softboundmpx_realloc+0x51>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <__softboundmpx_copy_metadata@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<softboundmpx_realloc>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <realloc@plt>
mov    %rax,%rbx
lea    (%rax,%r14,1),%rsi
mov    %rax,%rdi
call   1050 <__softboundmpx_store_return_metadata@plt>
cmp    %r15,%rbx
je     1162 <softboundmpx_realloc+0x32>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <__softboundmpx_copy_metadata@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define weak i8* @softboundmpx_realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i8*, i8** %3
%7 = load i64, i64* %4
%8 = call i8* @realloc(i8* %6, i64 %7)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = load i8*, i8** %5
%11 = load i64, i64* %4
%12 = getelementptr inbounds i8, i8* %10, i64 %11
%13 = call i32 @__softboundmpx_store_return_metadata(i8* %9, i8* %12)
%14 = load i8*, i8** %5
%15 = load i8*, i8** %3
%16 = icmp ne i8* %14, %15
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = load i8*, i8** %3
%20 = load i64, i64* %4
%21 = call i32 @__softboundmpx_copy_metadata(i8* %18, i8* %19, i64 %20)
br label %22
22: ; preds = %17, %2
%23 = load i8*, i8** %5
ret i8* %23
}

LLVM IR (-O2):
define weak i8* @softboundmpx_realloc(i8* %0, i64 %1) {
%3 = tail call i8* @realloc(i8* %0, i64 %1)
%4 = getelementptr inbounds i8, i8* %3, i64 %1
%5 = tail call i32 @__softboundmpx_store_return_metadata(i8* %3, i8* %4)
%6 = icmp eq i8* %3, %0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @__softboundmpx_copy_metadata(i8* %3, i8* %0, i64 %1)
br label %9
9: ; preds = %7, %2
ret i8* %3
}