#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*/ bzero(void *, unsigned int);
int /*<<< orphan*/ free(void *);
void *malloc(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 = malloc((newsize));
  if (!newmem) {
    return 0;
  }
  bzero(newmem, newsize);
  memcpy(newmem, ptr, size);
  free((ptr));
  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     116b <pmem_realloc+0x2b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11bc <pmem_realloc+0x7c>
mov    -0x18(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118f <pmem_realloc+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    11bc <pmem_realloc+0x7c>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <bzero@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <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   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
cmp    %edx,%esi
jae    1189 <pmem_realloc+0x49>
mov    %edx,%ebp
mov    %esi,%r15d
mov    %edx,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1187 <pmem_realloc+0x47>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebp,%esi
call   1060 <bzero@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rbx
jmp    1189 <pmem_realloc+0x49>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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 %31
14: ; preds = %3
%15 = load i32, i32* %7
%16 = call i8* @malloc(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 %31
20: ; preds = %14
%21 = load i8*, i8** %8
%22 = load i32, i32* %7
%23 = call i32 @bzero(i8* %21, i32 %22)
%24 = load i8*, i8** %8
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = call i32 @memcpy(i8* %24, i8* %25, i32 %26)
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
%30 = load i8*, i8** %8
store i8* %30, i8** %4
br label %31
31: ; preds = %20, %19, %12
%32 = load i8*, i8** %4
ret i8* %32
}

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 %12
5: ; preds = %3
%6 = tail call i8* @malloc(i32 %2)
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %5
%9 = tail call i32 @bzero(i8* %6, i32 %2)
%10 = tail call i32 @memcpy(i8* %6, i8* %0, i32 %1)
%11 = tail call i32 @free(i8* %0)
br label %12
12: ; preds = %8, %5, %3
%13 = phi i8* [ %0, %3 ], [ %6, %8 ], [ null, %5 ]
ret i8* %13
}