#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*/ secure_zero(void *, size_t);
void *secure_realloc(void *ptr, size_t old_size, size_t new_size) {
  void *new_ptr = malloc(new_size);
  if (new_ptr)
    memcpy(new_ptr, ptr, old_size < new_size ? old_size : new_size);
  secure_zero(ptr, old_size);
  free(ptr);
  return new_ptr;
}


x86 assembly (clang, -O0):
<secure_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b0 <secure_realloc+0x70>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1197 <secure_realloc+0x57>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    119f <secure_realloc+0x5f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <secure_zero@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secure_realloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rdx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     1176 <secure_realloc+0x36>
cmp    %rbx,%r15
cmovb  %r15,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <secure_zero@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @secure_realloc(i8* %0, i64 %1, i64 %2) {
%4 = tail call i8* @malloc(i64 %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = icmp ult i64 %1, %2
%8 = select i1 %7, i64 %1, i64 %2
%9 = tail call i32 @memcpy(i8* %4, i8* %0, i64 %8)
br label %10
10: ; preds = %6, %3
%11 = tail call i32 @secure_zero(i8* %0, i64 %1)
%12 = tail call i32 @free(i8* %0)
ret i8* %4
}