#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_5__ TYPE_1__;

/* Type definitions */
struct TYPE_5__ {
  int /*<<< orphan*/ *data;
};
typedef int /*<<< orphan*/ RSA;
typedef int /*<<< orphan*/ EVP_PKEY;
typedef TYPE_1__ BUF_MEM;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_printf(int /*<<< orphan*/ *, char *, char const *,
                              char const *);
int BIO_read(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ BUF_MEM_free(TYPE_1__ *);
int /*<<< orphan*/ BUF_MEM_grow_clean(TYPE_1__ *, int);
TYPE_1__ *BUF_MEM_new();
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_new();
int /*<<< orphan*/ EVP_PKEY_set1_RSA(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ *d2i_RSA_NET(int /*<<< orphan*/ *, unsigned char const **,
                                long, int /*<<< orphan*/ *, int);
EVP_PKEY *load_netscape_key(BIO *err, BIO *key, const char *file,
                            const char *key_descrip, int format) {
  EVP_PKEY *pkey;
  BUF_MEM *buf;
  RSA *rsa;
  const unsigned char *p;
  int size, i;
  buf = BUF_MEM_new();
  pkey = EVP_PKEY_new();
  size = 0;
  if (buf == NULL || pkey == NULL)
    goto error;
  for (;;) {
    if (!BUF_MEM_grow_clean(buf, size + 1024 * 10))
      goto error;
    i = BIO_read(key, &(buf->data[size]), 1024 * 10);
    size += i;
    if (i == 0)
      break;
    if (i < 0) {
      BIO_printf(err, "Error reading %s %s", key_descrip, file);
      goto error;
    }
  }
  p = (unsigned char *)buf->data;
  rsa = d2i_RSA_NET(NULL, &p, (long)size, NULL, (format == 8 ? 1 : 0));
  if (rsa == NULL)
    goto error;
  BUF_MEM_free(buf);
  EVP_PKEY_set1_RSA(pkey, rsa);
  return pkey;
error:
  BUF_MEM_free(buf);
  EVP_PKEY_free(pkey);
  return NULL;
}


x86 assembly (clang, -O0):
<load_netscape_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <BUF_MEM_new@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1090 <EVP_PKEY_new@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x54(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     11df <load_netscape_key+0x4f>
cmpq   $0x0,-0x38(%rbp)
jne    11e4 <load_netscape_key+0x54>
jmp    12d8 <load_netscape_key+0x148>
jmp    11e9 <load_netscape_key+0x59>
mov    -0x40(%rbp),%rdi
mov    -0x54(%rbp),%esi
add    $0x2800,%esi
call   10a0 <BUF_MEM_grow_clean@plt>
cmp    $0x0,%eax
jne    1209 <load_netscape_key+0x79>
jmp    12d8 <load_netscape_key+0x148>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
movslq -0x54(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x2800,%edx
call   1030 <BIO_read@plt>
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
add    -0x54(%rbp),%eax
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x58(%rbp)
jne    1244 <load_netscape_key+0xb4>
jmp    1270 <load_netscape_key+0xe0>
cmpl   $0x0,-0x58(%rbp)
jge    126b <load_netscape_key+0xdb>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xd9f(%rip),%rsi
call   1050 <BIO_printf@plt>
jmp    12d8 <load_netscape_key+0x148>
jmp    11e9 <load_netscape_key+0x59>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
movslq -0x54(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
xor    %r8d,%r8d
mov    $0x1,%eax
cmp    $0x8,%ecx
cmove  %eax,%r8d
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x50(%rbp),%rsi
mov    %rcx,%rdi
call   1070 <d2i_RSA_NET@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12b5 <load_netscape_key+0x125>
jmp    12d8 <load_netscape_key+0x148>
mov    -0x40(%rbp),%rdi
call   1080 <BUF_MEM_free@plt>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   10b0 <EVP_PKEY_set1_RSA@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f2 <load_netscape_key+0x162>
mov    -0x40(%rbp),%rdi
call   1080 <BUF_MEM_free@plt>
mov    -0x38(%rbp),%rdi
call   1060 <EVP_PKEY_free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_netscape_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,0x4(%rsp)
mov    %rcx,0x8(%rsp)
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%rbp
xor    %eax,%eax
call   1040 <BUF_MEM_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <EVP_PKEY_new@plt>
mov    %rax,%r14
test   %rbx,%rbx
je     126f <load_netscape_key+0xdf>
test   %r14,%r14
je     126f <load_netscape_key+0xdf>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
lea    0x2800(%r12),%esi
mov    %rbx,%rdi
call   10a0 <BUF_MEM_grow_clean@plt>
test   %eax,%eax
je     126f <load_netscape_key+0xdf>
movslq %r12d,%rsi
shl    $0x2,%rsi
add    (%rbx),%rsi
mov    %r15,%rdi
mov    $0x2800,%edx
call   1030 <BIO_read@plt>
add    %eax,%r12d
test   %eax,%eax
je     122d <load_netscape_key+0x9d>
jns    11e0 <load_netscape_key+0x50>
lea    0xde5(%rip),%rsi
mov    %rbp,%rdi
mov    0x8(%rsp),%rdx
mov    %r13,%rcx
call   1050 <BIO_printf@plt>
jmp    126f <load_netscape_key+0xdf>
mov    (%rbx),%rax
mov    %rax,0x10(%rsp)
movslq %r12d,%rdx
xor    %r8d,%r8d
cmpl   $0x8,0x4(%rsp)
sete   %r8b
lea    0x10(%rsp),%rsi
xor    %edi,%edi
xor    %ecx,%ecx
call   1070 <d2i_RSA_NET@plt>
test   %rax,%rax
je     126f <load_netscape_key+0xdf>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1080 <BUF_MEM_free@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   10b0 <EVP_PKEY_set1_RSA@plt>
jmp    1282 <load_netscape_key+0xf2>
mov    %rbx,%rdi
call   1080 <BUF_MEM_free@plt>
mov    %r14,%rdi
call   1060 <EVP_PKEY_free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i32* }
@.str = [20 x i8] c"Error reading %s %s\00"
define i32* @load_netscape_key(i32* %0, i32* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32*
%13 = alloca %struct.TYPE_5__*
%14 = alloca i32*
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
store i32* %0, i32** %7
store i32* %1, i32** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%18 = call %struct.TYPE_5__* (...) @BUF_MEM_new()
store %struct.TYPE_5__* %18, %struct.TYPE_5__** %13
%19 = call i32* (...) @EVP_PKEY_new()
store i32* %19, i32** %12
store i32 0, i32* %16
%20 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%21 = icmp eq %struct.TYPE_5__* %20, null
br i1 %21, label %25, label %22
22: ; preds = %5
%23 = load i32*, i32** %12
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %22, %5
br label %80
26: ; preds = %22
br label %27
27: ; preds = %57, %26
%28 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%29 = load i32, i32* %16
%30 = add nsw i32 %29, 10240
%31 = call i32 @BUF_MEM_grow_clean(%struct.TYPE_5__* %28, i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %27
br label %80
34: ; preds = %27
%35 = load i32*, i32** %8
%36 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%37 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %36, i32 0, i32 0
%38 = load i32*, i32** %37
%39 = load i32, i32* %16
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = call i32 @BIO_read(i32* %35, i32* %41, i32 10240)
store i32 %42, i32* %17
%43 = load i32, i32* %17
%44 = load i32, i32* %16
%45 = add nsw i32 %44, %43
store i32 %45, i32* %16
%46 = load i32, i32* %17
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %34
br label %58
49: ; preds = %34
%50 = load i32, i32* %17
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %49
%53 = load i32*, i32** %7
%54 = load i8*, i8** %10
%55 = load i8*, i8** %9
%56 = call i32 @BIO_printf(i32* %53, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %54, i8* %55)
br label %80
57: ; preds = %49
br label %27
58: ; preds = %48
%59 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%60 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %59, i32 0, i32 0
%61 = load i32*, i32** %60
%62 = bitcast i32* %61 to i8*
store i8* %62, i8** %15
%63 = load i32, i32* %16
%64 = sext i32 %63 to i64
%65 = load i32, i32* %11
%66 = icmp eq i32 %65, 8
%67 = zext i1 %66 to i64
%68 = select i1 %66, i32 1, i32 0
%69 = call i32* @d2i_RSA_NET(i32* null, i8** %15, i64 %64, i32* null, i32 %68)
store i32* %69, i32** %14
%70 = load i32*, i32** %14
%71 = icmp eq i32* %70, null
br i1 %71, label %72, label %73
72: ; preds = %58
br label %80
73: ; preds = %58
%74 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%75 = call i32 @BUF_MEM_free(%struct.TYPE_5__* %74)
%76 = load i32*, i32** %12
%77 = load i32*, i32** %14
%78 = call i32 @EVP_PKEY_set1_RSA(i32* %76, i32* %77)
%79 = load i32*, i32** %12
store i32* %79, i32** %6
br label %85
80: ; preds = %72, %52, %33, %25
%81 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%82 = call i32 @BUF_MEM_free(%struct.TYPE_5__* %81)
%83 = load i32*, i32** %12
%84 = call i32 @EVP_PKEY_free(i32* %83)
store i32* null, i32** %6
br label %85
85: ; preds = %80, %73
%86 = load i32*, i32** %6
ret i32* %86
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32* }
@.str = [20 x i8] c"Error reading %s %s\00"
define i32* @load_netscape_key(i32* %0, i32* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = bitcast i8** %6 to i8*
%8 = tail call %struct.TYPE_5__* (...) @BUF_MEM_new()
%9 = tail call i32* (...) @EVP_PKEY_new()
%10 = icmp eq %struct.TYPE_5__* %8, null
%11 = icmp eq i32* %9, null
%12 = select i1 %10, i1 true, i1 %11
br i1 %12, label %42, label %13
13: ; preds = %5
%14 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %8, i64 0, i32 0
br label %15
15: ; preds = %13, %27
%16 = phi i32 [ %25, %27 ], [ 0, %13 ]
%17 = add nsw i32 %16, 10240
%18 = tail call i32 @BUF_MEM_grow_clean(%struct.TYPE_5__* %8, i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %42, label %20
20: ; preds = %15
%21 = load i32*, i32** %14
%22 = sext i32 %16 to i64
%23 = getelementptr inbounds i32, i32* %21, i64 %22
%24 = tail call i32 @BIO_read(i32* %1, i32* %23, i32 10240)
%25 = add nsw i32 %24, %16
%26 = icmp eq i32 %24, 0
br i1 %26, label %31, label %27
27: ; preds = %20
%28 = icmp slt i32 %24, 0
br i1 %28, label %29, label %15
29: ; preds = %27
%30 = tail call i32 @BIO_printf(i32* %0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %2)
br label %42
31: ; preds = %20
%32 = bitcast %struct.TYPE_5__* %8 to i8**
%33 = load i8*, i8** %32
store i8* %33, i8** %6
%34 = sext i32 %25 to i64
%35 = icmp eq i32 %4, 8
%36 = zext i1 %35 to i32
%37 = call i32* @d2i_RSA_NET(i32* null, i8** %6, i64 %34, i32* null, i32 %36)
%38 = icmp eq i32* %37, null
br i1 %38, label %42, label %39
39: ; preds = %31
%40 = call i32 @BUF_MEM_free(%struct.TYPE_5__* %8)
%41 = call i32 @EVP_PKEY_set1_RSA(i32* %9, i32* %37)
br label %45
42: ; preds = %15, %31, %5, %29
%43 = call i32 @BUF_MEM_free(%struct.TYPE_5__* %8)
%44 = call i32 @EVP_PKEY_free(i32* %9)
br label %45
45: ; preds = %42, %39
%46 = phi i32* [ null, %42 ], [ %9, %39 ]
ret i32* %46
}