#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 */
typedef int /*<<< orphan*/ pem_password_cb;
typedef int /*<<< orphan*/ EVP_PKEY;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int BIO_read(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ PEM_F_B2I_PVK_BIO;
int /*<<< orphan*/ PEM_R_PVK_DATA_TOO_SHORT;
int /*<<< orphan*/ PEMerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *do_PVK_body(unsigned char const **, unsigned int,
                                unsigned int, int /*<<< orphan*/ *, void *);
int /*<<< orphan*/ do_PVK_header(unsigned char const **, int,
                                 int /*<<< orphan*/, unsigned int *,
                                 unsigned int *);
int /*<<< orphan*/ explicit_bzero(unsigned char *, int);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u) {
  unsigned char pvk_hdr[24], *buf = NULL;
  const unsigned char *p;
  int buflen;
  EVP_PKEY *ret = NULL;
  unsigned int saltlen, keylen;
  if (BIO_read(in, pvk_hdr, 24) != 24) {
    PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
    return NULL;
  }
  p = pvk_hdr;
  if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
    return 0;
  buflen = (int)keylen + saltlen;
  buf = malloc(buflen);
  if (!buf) {
    PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
    return 0;
  }
  p = buf;
  if (BIO_read(in, buf, buflen) != buflen) {
    PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
    goto err;
  }
  ret = do_PVK_body(&p, saltlen, keylen, cb, u);
err:
  if (buf) {
    explicit_bzero(buf, buflen);
    free(buf);
  }
  return ret;
}


x86 assembly (clang, -O0):
<b2i_PVK_bio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x18,%edx
call   1040 <BIO_read@plt>
cmp    $0x18,%eax
je     11d3 <b2i_PVK_bio+0x63>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    12cd <b2i_PVK_bio+0x15d>
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x18,%esi
xor    %edx,%edx
lea    -0x64(%rbp),%rcx
lea    -0x68(%rbp),%r8
call   1060 <do_PVK_header@plt>
cmp    $0x0,%eax
jne    1209 <b2i_PVK_bio+0x99>
movq   $0x0,-0x8(%rbp)
jmp    12cd <b2i_PVK_bio+0x15d>
mov    -0x68(%rbp),%eax
add    -0x64(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    124d <b2i_PVK_bio+0xdd>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    12cd <b2i_PVK_bio+0x15d>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   1040 <BIO_read@plt>
cmp    -0x54(%rbp),%eax
je     128a <b2i_PVK_bio+0x11a>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
jmp    12a5 <b2i_PVK_bio+0x135>
mov    -0x64(%rbp),%esi
mov    -0x68(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    -0x50(%rbp),%rdi
call   1080 <do_PVK_body@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12c5 <b2i_PVK_bio+0x155>
mov    -0x48(%rbp),%rdi
mov    -0x54(%rbp),%esi
call   1090 <explicit_bzero@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x60(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b2i_PVK_bio>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x18,%edx
call   1040 <BIO_read@plt>
cmp    $0x18,%eax
jne    1213 <b2i_PVK_bio+0xa3>
mov    %rbp,0x8(%rsp)
lea    0x8(%rsp),%rdi
xor    %ebp,%ebp
lea    0x4(%rsp),%rcx
mov    %rsp,%r8
mov    $0x18,%esi
xor    %edx,%edx
call   1060 <do_PVK_header@plt>
test   %eax,%eax
je     126c <b2i_PVK_bio+0xfc>
mov    0x4(%rsp),%r13d
add    (%rsp),%r13d
mov    %r13d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     122e <b2i_PVK_bio+0xbe>
mov    %rax,%r12
mov    %rax,0x8(%rsp)
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r13d,%edx
call   1040 <BIO_read@plt>
cmp    %r13d,%eax
jne    1240 <b2i_PVK_bio+0xd0>
mov    0x4(%rsp),%esi
mov    (%rsp),%edx
lea    0x8(%rsp),%rdi
mov    %r15,%rcx
mov    %r14,%r8
call   1080 <do_PVK_body@plt>
mov    %rax,%rbp
jmp    1259 <b2i_PVK_bio+0xe9>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
xor    %ebp,%ebp
jmp    126c <b2i_PVK_bio+0xfc>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    0x2d9a(%rip),%rax
jmp    1223 <b2i_PVK_bio+0xb3>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
xor    %ebp,%ebp
mov    %r12,%rdi
mov    %r13d,%esi
call   1090 <explicit_bzero@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PEM_F_B2I_PVK_BIO = global i32 0
@PEM_R_PVK_DATA_TOO_SHORT = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32* @b2i_PVK_bio(i32* %0, i32* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [24 x i8]
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i8* null, i8** %9
store i32* null, i32** %12
%15 = load i32*, i32** %5
%16 = getelementptr inbounds [24 x i8], [24 x i8]* %8, i64 0, i64 0
%17 = call i32 @BIO_read(i32* %15, i8* %16, i32 24)
%18 = icmp ne i32 %17, 24
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = load i32, i32* @PEM_F_B2I_PVK_BIO
%21 = load i32, i32* @PEM_R_PVK_DATA_TOO_SHORT
%22 = call i32 @PEMerr(i32 %20, i32 %21)
store i32* null, i32** %4
br label %69
23: ; preds = %3
%24 = getelementptr inbounds [24 x i8], [24 x i8]* %8, i64 0, i64 0
store i8* %24, i8** %10
%25 = call i32 @do_PVK_header(i8** %10, i32 24, i32 0, i32* %13, i32* %14)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %23
store i32* null, i32** %4
br label %69
28: ; preds = %23
%29 = load i32, i32* %14
%30 = load i32, i32* %13
%31 = add i32 %29, %30
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %9
%34 = load i8*, i8** %9
%35 = icmp ne i8* %34, null
br i1 %35, label %40, label %36
36: ; preds = %28
%37 = load i32, i32* @PEM_F_B2I_PVK_BIO
%38 = load i32, i32* @ERR_R_MALLOC_FAILURE
%39 = call i32 @PEMerr(i32 %37, i32 %38)
store i32* null, i32** %4
br label %69
40: ; preds = %28
%41 = load i8*, i8** %9
store i8* %41, i8** %10
%42 = load i32*, i32** %5
%43 = load i8*, i8** %9
%44 = load i32, i32* %11
%45 = call i32 @BIO_read(i32* %42, i8* %43, i32 %44)
%46 = load i32, i32* %11
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %52
48: ; preds = %40
%49 = load i32, i32* @PEM_F_B2I_PVK_BIO
%50 = load i32, i32* @PEM_R_PVK_DATA_TOO_SHORT
%51 = call i32 @PEMerr(i32 %49, i32 %50)
br label %58
52: ; preds = %40
%53 = load i32, i32* %13
%54 = load i32, i32* %14
%55 = load i32*, i32** %6
%56 = load i8*, i8** %7
%57 = call i32* @do_PVK_body(i8** %10, i32 %53, i32 %54, i32* %55, i8* %56)
store i32* %57, i32** %12
br label %58
58: ; preds = %52, %48
%59 = load i8*, i8** %9
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %67
61: ; preds = %58
%62 = load i8*, i8** %9
%63 = load i32, i32* %11
%64 = call i32 @explicit_bzero(i8* %62, i32 %63)
%65 = load i8*, i8** %9
%66 = call i32 @free(i8* %65)
br label %67
67: ; preds = %61, %58
%68 = load i32*, i32** %12
store i32* %68, i32** %4
br label %69
69: ; preds = %67, %36, %27, %19
%70 = load i32*, i32** %4
ret i32* %70
}

LLVM IR (-O2):
@PEM_F_B2I_PVK_BIO = global i32 0
@PEM_R_PVK_DATA_TOO_SHORT = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32* @b2i_PVK_bio(i32* %0, i32* %1, i8* %2) {
%4 = alloca [24 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = getelementptr inbounds [24 x i8], [24 x i8]* %4, i64 0, i64 0
%9 = bitcast i8** %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = call i32 @BIO_read(i32* %0, i8* %8, i32 24)
%13 = icmp eq i32 %12, 24
br i1 %13, label %18, label %14
14: ; preds = %3
%15 = load i32, i32* @PEM_F_B2I_PVK_BIO
%16 = load i32, i32* @PEM_R_PVK_DATA_TOO_SHORT
%17 = call i32 @PEMerr(i32 %15, i32 %16)
br label %46
18: ; preds = %3
store i8* %8, i8** %5
%19 = call i32 @do_PVK_header(i8** %5, i32 24, i32 0, i32* %6, i32* %7)
%20 = icmp eq i32 %19, 0
br i1 %20, label %46, label %21
21: ; preds = %18
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = add i32 %23, %22
%25 = call i8* @malloc(i32 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @PEM_F_B2I_PVK_BIO
%29 = load i32, i32* @ERR_R_MALLOC_FAILURE
%30 = call i32 @PEMerr(i32 %28, i32 %29)
br label %46
31: ; preds = %21
store i8* %25, i8** %5
%32 = call i32 @BIO_read(i32* %0, i8* %25, i32 %24)
%33 = icmp eq i32 %32, %24
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = load i32, i32* @PEM_F_B2I_PVK_BIO
%36 = load i32, i32* @PEM_R_PVK_DATA_TOO_SHORT
%37 = call i32 @PEMerr(i32 %35, i32 %36)
br label %42
38: ; preds = %31
%39 = load i32, i32* %6
%40 = load i32, i32* %7
%41 = call i32* @do_PVK_body(i8** %5, i32 %39, i32 %40, i32* %1, i8* %2)
br label %42
42: ; preds = %34, %38
%43 = phi i32* [ null, %34 ], [ %41, %38 ]
%44 = call i32 @explicit_bzero(i8* %25, i32 %24)
%45 = call i32 @free(i8* %25)
br label %46
46: ; preds = %18, %42, %27, %14
%47 = phi i32* [ null, %14 ], [ %43, %42 ], [ null, %27 ], [ null, %18 ]
ret i32* %47
}