#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*/ malloc_usable_size(void *);
int /*<<< orphan*/ stm_load_bytes(void *, void *, int /*<<< orphan*/);
int /*<<< orphan*/ tanger_stm_free(void *);
void *tanger_stm_malloc(size_t);
void *tanger_stm_realloc(void *ptr, size_t size) {
  void *p;
  if (ptr == NULL) {
    return tanger_stm_malloc(size);
  }
  if (size == 0) {
    tanger_stm_free(ptr);
    return NULL;
  }
  p = tanger_stm_malloc(size);
  stm_load_bytes(ptr, p, malloc_usable_size(ptr));
  tanger_stm_free(ptr);
  return p;
}


x86 assembly (clang, -O0):
<tanger_stm_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <tanger_stm_realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1060 <tanger_stm_malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11d4 <tanger_stm_realloc+0x94>
cmpq   $0x0,-0x18(%rbp)
jne    118e <tanger_stm_realloc+0x4e>
mov    -0x10(%rbp),%rdi
call   1040 <tanger_stm_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d4 <tanger_stm_realloc+0x94>
mov    -0x18(%rbp),%rdi
call   1060 <tanger_stm_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <malloc_usable_size@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <stm_load_bytes@plt>
mov    -0x10(%rbp),%rdi
call   1040 <tanger_stm_free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tanger_stm_realloc>:
test   %rdi,%rdi
je     117b <tanger_stm_realloc+0x3b>
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rsi,%rsi
je     1183 <tanger_stm_realloc+0x43>
mov    %rsi,%rdi
call   1060 <tanger_stm_malloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <malloc_usable_size@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <stm_load_bytes@plt>
mov    %rbx,%rdi
call   1040 <tanger_stm_free@plt>
jmp    118e <tanger_stm_realloc+0x4e>
mov    %rsi,%rdi
jmp    1060 <tanger_stm_malloc@plt>
mov    %rbx,%rdi
call   1040 <tanger_stm_free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @tanger_stm_realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i64, i64* %5
%11 = call i8* @tanger_stm_malloc(i64 %10)
store i8* %11, i8** %3
br label %29
12: ; preds = %2
%13 = load i64, i64* %5
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = call i32 @tanger_stm_free(i8* %16)
store i8* null, i8** %3
br label %29
18: ; preds = %12
%19 = load i64, i64* %5
%20 = call i8* @tanger_stm_malloc(i64 %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %4
%22 = load i8*, i8** %6
%23 = load i8*, i8** %4
%24 = call i32 @malloc_usable_size(i8* %23)
%25 = call i32 @stm_load_bytes(i8* %21, i8* %22, i32 %24)
%26 = load i8*, i8** %4
%27 = call i32 @tanger_stm_free(i8* %26)
%28 = load i8*, i8** %6
store i8* %28, i8** %3
br label %29
29: ; preds = %18, %15, %9
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
define i8* @tanger_stm_realloc(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @tanger_stm_malloc(i64 %1)
br label %15
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @tanger_stm_free(i8* %0)
br label %15
10: ; preds = %6
%11 = tail call i8* @tanger_stm_malloc(i64 %1)
%12 = tail call i32 @malloc_usable_size(i8* %0)
%13 = tail call i32 @stm_load_bytes(i8* %0, i8* %11, i32 %12)
%14 = tail call i32 @tanger_stm_free(i8* %0)
br label %15
15: ; preds = %10, %8, %4
%16 = phi i8* [ %5, %4 ], [ null, %8 ], [ %11, %10 ]
ret i8* %16
}