#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*/ SecureClearMem(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpagesize();
int /*<<< orphan*/ memcpy(char *, char *, int);
scalar_t__ posix_memalign(char **, int, int);
int /*<<< orphan*/ stderr;
char *SecureRealloc(char *OldMem, int OldSize, int NewSize, int Flags) {
  int MemSize;
  char *NewMem = NULL;
  int val = 0, PageSize;
  PageSize = getpagesize();
  MemSize = (NewSize / PageSize + 1) * PageSize;
  if (posix_memalign(&NewMem, PageSize, MemSize) == 0) {
    if (OldMem) {
      if (NewSize < OldSize)
        val = NewSize;
      else
        val = OldSize;
      memcpy(NewMem, OldMem, val);
      SecureClearMem(OldMem, OldSize);
    }
  } else {
    fprintf(stderr, "LIBUSEFUL ERROR: Failed to allocate secure memory");
    exit(3);
  }
  return (NewMem);
}


x86 assembly (clang, -O0):
<SecureRealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
cltd
idivl  -0x28(%rbp)
add    $0x1,%eax
imul   -0x28(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    -0x20(%rbp),%rdi
call   1080 <posix_memalign@plt>
cmp    $0x0,%rax
jne    1201 <SecureRealloc+0xa1>
cmpq   $0x0,-0x8(%rbp)
je     11fc <SecureRealloc+0x9c>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11da <SecureRealloc+0x7a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11e0 <SecureRealloc+0x80>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <SecureClearMem@plt>
jmp    1220 <SecureRealloc+0xc0>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SecureRealloc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   1060 <getpagesize@plt>
mov    %eax,%esi
mov    %ebp,%eax
cltd
idiv   %esi
lea    0x1(%rax),%edx
imul   %esi,%edx
lea    0x8(%rsp),%rdi
call   1080 <posix_memalign@plt>
test   %rax,%rax
jne    11d0 <SecureRealloc+0x70>
test   %rbx,%rbx
je     11c2 <SecureRealloc+0x62>
cmp    %r14d,%ebp
cmovge %r14d,%ebp
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <SecureClearMem@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [50 x i8] c"LIBUSEFUL ERROR: Failed to allocate secure memory\00"
define i8* @SecureRealloc(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i8* null, i8** %10
store i32 0, i32* %11
%13 = call i32 (...) @getpagesize()
store i32 %13, i32* %12
%14 = load i32, i32* %7
%15 = load i32, i32* %12
%16 = sdiv i32 %14, %15
%17 = add nsw i32 %16, 1
%18 = load i32, i32* %12
%19 = mul nsw i32 %17, %18
store i32 %19, i32* %9
%20 = load i32, i32* %12
%21 = load i32, i32* %9
%22 = call i64 @posix_memalign(i8** %10, i32 %20, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %44
24: ; preds = %4
%25 = load i8*, i8** %5
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %43
27: ; preds = %24
%28 = load i32, i32* %7
%29 = load i32, i32* %6
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32, i32* %7
store i32 %32, i32* %11
br label %35
33: ; preds = %27
%34 = load i32, i32* %6
store i32 %34, i32* %11
br label %35
35: ; preds = %33, %31
%36 = load i8*, i8** %10
%37 = load i8*, i8** %5
%38 = load i32, i32* %11
%39 = call i32 @memcpy(i8* %36, i8* %37, i32 %38)
%40 = load i8*, i8** %5
%41 = load i32, i32* %6
%42 = call i32 @SecureClearMem(i8* %40, i32 %41)
br label %43
43: ; preds = %35, %24
br label %48
44: ; preds = %4
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%47 = call i32 @exit(i32 3)
unreachable
48: ; preds = %43
%49 = load i8*, i8** %10
ret i8* %49
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [50 x i8] c"LIBUSEFUL ERROR: Failed to allocate secure memory\00"
define i8* @SecureRealloc(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%7 = tail call i32 (...) @getpagesize()
%8 = sdiv i32 %2, %7
%9 = add nsw i32 %8, 1
%10 = mul nsw i32 %9, %7
%11 = call i64 @posix_memalign(i8** %5, i32 %7, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %4
%14 = icmp eq i8* %0, null
br i1 %14, label %25, label %15
15: ; preds = %13
%16 = icmp slt i32 %2, %1
%17 = select i1 %16, i32 %2, i32 %1
%18 = load i8*, i8** %5
%19 = call i32 @memcpy(i8* %18, i8* %0, i32 %17)
%20 = call i32 @SecureClearMem(i8* %0, i32 %1)
br label %25
21: ; preds = %4
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 3)
unreachable
25: ; preds = %13, %15
%26 = load i8*, i8** %5
ret i8* %26
}