#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 */
void *PMEM_ALLOC(unsigned int);
int /*<<< orphan*/ PMEM_FREE(void *, unsigned int);
int /*<<< orphan*/ memcpy(void *, void *, unsigned int);
void *pmem_realloc(void *ptr, unsigned size, unsigned newsize) {
  if (size >= newsize) {
    return ptr;
  }
  void *newmem = PMEM_ALLOC(newsize);
  if (!newmem) {
    return 0;
  }
  memcpy(newmem, ptr, size);
  PMEM_FREE(ptr, size);
  return newmem;
}


x86 assembly (clang, -O0):
<pmem_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jb     115b <pmem_realloc+0x2b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a3 <pmem_realloc+0x73>
mov    -0x18(%rbp),%edi
call   1030 <PMEM_ALLOC@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117f <pmem_realloc+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    11a3 <pmem_realloc+0x73>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <PMEM_FREE@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pmem_realloc>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
cmp    %edx,%esi
jae    116a <pmem_realloc+0x3a>
mov    %esi,%ebp
mov    %edx,%edi
call   1030 <PMEM_ALLOC@plt>
test   %rax,%rax
je     1168 <pmem_realloc+0x38>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <PMEM_FREE@plt>
mov    %r14,%rbx
jmp    116a <pmem_realloc+0x3a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @pmem_realloc(i8* %0, i32 %1, i32 %2) {
%4 = icmp ult i32 %1, %2
br i1 %4, label %5, label %11
5: ; preds = %3
%6 = tail call i8* @PMEM_ALLOC(i32 %2)
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i32 @memcpy(i8* %6, i8* %0, i32 %1)
%10 = tail call i32 @PMEM_FREE(i8* %0, i32 %1)
br label %11
11: ; preds = %8, %5, %3
%12 = phi i8* [ %0, %3 ], [ %6, %8 ], [ null, %5 ]
ret i8* %12
}