#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*/ memcpy(void *, void *, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ xfree(void *);
void *xmalloc(size_t);
void *rsa_realloc(void *ptr, size_t old_size, size_t new_size) {
  int s;
  void *p = xmalloc(new_size);
  s = old_size;
  if (old_size > new_size)
    s = new_size;
  memcpy(p, ptr, s);
  memset(ptr, 0, old_size);
  xfree(ptr);
  return p;
}


x86 assembly (clang, -O0):
<rsa_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    117d <rsa_realloc+0x3d>
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
call   1030 <xfree@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_realloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%r12
cmp    %rbx,%r14
cmovbe %r14d,%ebx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <memset@plt>
mov    %r15,%rdi
call   1030 <xfree@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @rsa_realloc(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
%10 = call i8* @xmalloc(i64 %9)
store i8* %10, i8** %8
%11 = load i64, i64* %5
%12 = trunc i64 %11 to i32
store i32 %12, i32* %7
%13 = load i64, i64* %5
%14 = load i64, i64* %6
%15 = icmp ugt i64 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i64, i64* %6
%18 = trunc i64 %17 to i32
store i32 %18, i32* %7
br label %19
19: ; preds = %16, %3
%20 = load i8*, i8** %8
%21 = load i8*, i8** %4
%22 = load i32, i32* %7
%23 = call i32 @memcpy(i8* %20, i8* %21, i32 %22)
%24 = load i8*, i8** %4
%25 = load i64, i64* %5
%26 = call i32 @memset(i8* %24, i32 0, i64 %25)
%27 = load i8*, i8** %4
%28 = call i32 @xfree(i8* %27)
%29 = load i8*, i8** %8
ret i8* %29
}

LLVM IR (-O2):
define i8* @rsa_realloc(i8* %0, i64 %1, i64 %2) {
%4 = tail call i8* @xmalloc(i64 %2)
%5 = icmp ugt i64 %1, %2
%6 = select i1 %5, i64 %2, i64 %1
%7 = trunc i64 %6 to i32
%8 = tail call i32 @memcpy(i8* %4, i8* %0, i32 %7)
%9 = tail call i32 @memset(i8* %0, i32 0, i64 %1)
%10 = tail call i32 @xfree(i8* %0)
ret i8* %4
}