#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*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ yap_message(char *);
void *yap_fullrealloc(void *ptr, size_t oldsize, size_t size) {
  void *p;
  if (size <= oldsize) {
    yap_message("yap_realloc() trying to shrink ... ignored\n");
    return ptr;
  }
  p = malloc(size);
  memset(p, 0, size);
  memcpy(p, ptr, oldsize);
  free(ptr);
  return p;
}


x86 assembly (clang, -O0):
<yap_fullrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     118b <yap_fullrealloc+0x3b>
lea    0xe87(%rip),%rdi
call   1070 <yap_message@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c9 <yap_fullrealloc+0x79>
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<yap_fullrealloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
cmp    %rsi,%rdx
jbe    1199 <yap_fullrealloc+0x49>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%r15
jmp    11a5 <yap_fullrealloc+0x55>
lea    0xe60(%rip),%rdi
call   1070 <yap_message@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [44 x i8] c"yap_realloc() trying to shrink ... ignored\0A\00"
define i8* @yap_fullrealloc(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
%10 = load i64, i64* %6
%11 = icmp ule i64 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = call i32 @yap_message(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%14 = load i8*, i8** %5
store i8* %14, i8** %4
br label %28
15: ; preds = %3
%16 = load i64, i64* %7
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %8
%18 = load i8*, i8** %8
%19 = load i64, i64* %7
%20 = call i32 @memset(i8* %18, i32 0, i64 %19)
%21 = load i8*, i8** %8
%22 = load i8*, i8** %5
%23 = load i64, i64* %6
%24 = call i32 @memcpy(i8* %21, i8* %22, i64 %23)
%25 = load i8*, i8** %5
%26 = call i32 @free(i8* %25)
%27 = load i8*, i8** %8
store i8* %27, i8** %4
br label %28
28: ; preds = %15, %12
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
@.str = [44 x i8] c"yap_realloc() trying to shrink ... ignored\0A\00"
define i8* @yap_fullrealloc(i8* %0, i64 %1, i64 %2) {
%4 = icmp ugt i64 %2, %1
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i32 @yap_message(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %12
7: ; preds = %3
%8 = tail call i8* @malloc(i64 %2)
%9 = tail call i32 @memset(i8* %8, i32 0, i64 %2)
%10 = tail call i32 @memcpy(i8* %8, i8* %0, i64 %1)
%11 = tail call i32 @free(i8* %0)
br label %12
12: ; preds = %7, %5
%13 = phi i8* [ %0, %5 ], [ %8, %7 ]
ret i8* %13
}