#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int /*<<< orphan*/ *data;
  int /*<<< orphan*/ length;
};
typedef TYPE_1__ BUF_MEM;

/* Variables and functions */
int /*<<< orphan*/ OPENSSL_secure_free(int /*<<< orphan*/ *);
char *OPENSSL_secure_malloc(size_t);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
char *sec_alloc_realloc(BUF_MEM *str, size_t len) {
  char *ret;
  ret = OPENSSL_secure_malloc(len);
  if (str->data != NULL) {
    if (ret != NULL)
      memcpy(ret, str->data, str->length);
    OPENSSL_secure_free(str->data);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<sec_alloc_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <OPENSSL_secure_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     1189 <sec_alloc_realloc+0x59>
cmpq   $0x0,-0x18(%rbp)
je     117d <sec_alloc_realloc+0x4d>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <OPENSSL_secure_free@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sec_alloc_realloc>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <OPENSSL_secure_malloc@plt>
mov    %rax,%r14
mov    (%rbx),%rsi
test   %rsi,%rsi
je     1165 <sec_alloc_realloc+0x35>
test   %r14,%r14
je     115d <sec_alloc_realloc+0x2d>
mov    0x8(%rbx),%edx
mov    %r14,%rdi
call   1050 <memcpy@plt>
mov    (%rbx),%rsi
mov    %rsi,%rdi
call   1040 <OPENSSL_secure_free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32*, i32 }
define i8* @sec_alloc_realloc(%struct.TYPE_3__* %0, i64 %1) {
%3 = alloca %struct.TYPE_3__*
%4 = alloca i64
%5 = alloca i8*
store %struct.TYPE_3__* %0, %struct.TYPE_3__** %3
store i64 %1, i64* %4
%6 = load i64, i64* %4
%7 = call i8* @OPENSSL_secure_malloc(i64 %6)
store i8* %7, i8** %5
%8 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%9 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i32 0, i32 0
%10 = load i32*, i32** %9
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %29
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i8*, i8** %5
%17 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%18 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i32 0, i32 0
%19 = load i32*, i32** %18
%20 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%21 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = call i32 @memcpy(i8* %16, i32* %19, i32 %22)
br label %24
24: ; preds = %15, %12
%25 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%26 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %25, i32 0, i32 0
%27 = load i32*, i32** %26
%28 = call i32 @OPENSSL_secure_free(i32* %27)
br label %29
29: ; preds = %24, %2
%30 = load i8*, i8** %5
ret i8* %30
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32*, i32 }
define i8* @sec_alloc_realloc(%struct.TYPE_3__* nocapture %0, i64 %1) {
%3 = tail call i8* @OPENSSL_secure_malloc(i64 %1)
%4 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 0
%5 = load i32*, i32** %4
%6 = icmp eq i32* %5, null
br i1 %6, label %17, label %7
7: ; preds = %2
%8 = icmp eq i8* %3, null
br i1 %8, label %14, label %9
9: ; preds = %7
%10 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 1
%11 = load i32, i32* %10
%12 = tail call i32 @memcpy(i8* %3, i32* %5, i32 %11)
%13 = load i32*, i32** %4
br label %14
14: ; preds = %9, %7
%15 = phi i32* [ %13, %9 ], [ %5, %7 ]
%16 = tail call i32 @OPENSSL_secure_free(i32* %15)
br label %17
17: ; preds = %14, %2
ret i8* %3
}