#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 *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
int /*<<< orphan*/ secure_clear(void *, size_t);
void *xmalloc(size_t);
void *secure_resize(void *ptr, size_t oldlen, size_t newlen) {
  void *newptr = xmalloc(newlen);
  if (ptr) {
    memcpy(newptr, ptr, (((oldlen) < (newlen)) ? (oldlen) : (newlen)));
    secure_clear(ptr, oldlen);
    free(ptr);
  }
  return newptr;
}


x86 assembly (clang, -O0):
<secure_resize>:
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   1050 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11c6 <secure_resize+0x86>
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_resize+0x57>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    119f <secure_resize+0x5f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <secure_clear@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

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

LLVM IR (-O0):
define i8* @secure_resize(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* @xmalloc(i64 %8)
store i8* %9, i8** %7
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %30
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)
%25 = load i8*, i8** %4
%26 = load i64, i64* %5
%27 = call i32 @secure_clear(i8* %25, i64 %26)
%28 = load i8*, i8** %4
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %22, %3
%31 = load i8*, i8** %7
ret i8* %31
}

LLVM IR (-O2):
define i8* @secure_resize(i8* %0, i64 %1, i64 %2) {
%4 = tail call i8* @xmalloc(i64 %2)
%5 = icmp eq i8* %0, null
br i1 %5, label %12, 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)
%10 = tail call i32 @secure_clear(i8* %0, i64 %1)
%11 = tail call i32 @free(i8* %0)
br label %12
12: ; preds = %6, %3
ret i8* %4
}