#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 */
struct cpl_allocator {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ cpl_pool_free(struct cpl_allocator *, void *);
void *cpl_pool_malloc(struct cpl_allocator *, size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *cpl_pool_realloc(struct cpl_allocator *pAllocator, void *ptr, size_t sz) {
  void *new_ptr = cpl_pool_malloc(pAllocator, sz);
  if (new_ptr) {
    memcpy(new_ptr, ptr, sz);
    cpl_pool_free(pAllocator, ptr);
    ptr = new_ptr;
  }
  return ptr;
}


x86 assembly (clang, -O0):
<cpl_pool_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <cpl_pool_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1186 <cpl_pool_realloc+0x56>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <cpl_pool_free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpl_pool_realloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rdx,%rsi
call   1030 <cpl_pool_malloc@plt>
test   %rax,%rax
je     116d <cpl_pool_realloc+0x3d>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <cpl_pool_free@plt>
mov    %rbx,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.cpl_allocator = type { i32 }
define i8* @cpl_pool_realloc(%struct.cpl_allocator* %0, i8* %1, i64 %2) {
%4 = alloca %struct.cpl_allocator*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store %struct.cpl_allocator* %0, %struct.cpl_allocator** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load %struct.cpl_allocator*, %struct.cpl_allocator** %4
%9 = load i64, i64* %6
%10 = call i8* @cpl_pool_malloc(%struct.cpl_allocator* %8, i64 %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %22
13: ; preds = %3
%14 = load i8*, i8** %7
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = call i32 @memcpy(i8* %14, i8* %15, i64 %16)
%18 = load %struct.cpl_allocator*, %struct.cpl_allocator** %4
%19 = load i8*, i8** %5
%20 = call i32 @cpl_pool_free(%struct.cpl_allocator* %18, i8* %19)
%21 = load i8*, i8** %7
store i8* %21, i8** %5
br label %22
22: ; preds = %13, %3
%23 = load i8*, i8** %5
ret i8* %23
}

LLVM IR (-O2):
%struct.cpl_allocator = type { i32 }
define i8* @cpl_pool_realloc(%struct.cpl_allocator* %0, i8* %1, i64 %2) {
%4 = tail call i8* @cpl_pool_malloc(%struct.cpl_allocator* %0, i64 %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = tail call i32 @memcpy(i8* %4, i8* %1, i64 %2)
%8 = tail call i32 @cpl_pool_free(%struct.cpl_allocator* %0, i8* %1)
br label %9
9: ; preds = %6, %3
%10 = phi i8* [ %4, %6 ], [ %1, %3 ]
ret i8* %10
}