#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 */
size_t BIO_read(int /*<<< orphan*/ *, unsigned char *, size_t);
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 *, size_t);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(size_t);
EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u) {
  unsigned char pvk_hdr[24], *buf = NULL;
  const unsigned char *p;
  size_t 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 = 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,%rax
je     11d4 <b2i_PVK_bio+0x64>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d5 <b2i_PVK_bio+0x165>
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    120a <b2i_PVK_bio+0x9a>
movq   $0x0,-0x8(%rbp)
jmp    12d5 <b2i_PVK_bio+0x165>
mov    -0x68(%rbp),%eax
add    -0x64(%rbp),%eax
mov    %eax,%eax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1252 <b2i_PVK_bio+0xe2>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d5 <b2i_PVK_bio+0x165>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x58(%rbp),%rdx
call   1040 <BIO_read@plt>
cmp    -0x58(%rbp),%rax
je     1291 <b2i_PVK_bio+0x121>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
jmp    12ac <b2i_PVK_bio+0x13c>
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     12cd <b2i_PVK_bio+0x15d>
mov    -0x48(%rbp),%rdi
mov    -0x58(%rbp),%rsi
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    $0x18,%edx
mov    %rbp,%rsi
call   1040 <BIO_read@plt>
cmp    $0x18,%rax
jne    1214 <b2i_PVK_bio+0xa4>
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     126d <b2i_PVK_bio+0xfd>
mov    0x4(%rsp),%r13d
add    (%rsp),%r13d
mov    %r13,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     122f <b2i_PVK_bio+0xbf>
mov    %rax,%r12
mov    %rax,0x8(%rsp)
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r13,%rdx
call   1040 <BIO_read@plt>
cmp    %r13,%rax
jne    1241 <b2i_PVK_bio+0xd1>
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    125a <b2i_PVK_bio+0xea>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
xor    %ebp,%ebp
jmp    126d <b2i_PVK_bio+0xfd>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    0x2d99(%rip),%rax
jmp    1224 <b2i_PVK_bio+0xb4>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
xor    %ebp,%ebp
mov    %r12,%rdi
mov    %r13,%rsi
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 i64
%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 i64 @BIO_read(i32* %15, i8* %16, i64 24)
%18 = icmp ne i64 %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 %70
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 %70
28: ; preds = %23
%29 = load i32, i32* %14
%30 = load i32, i32* %13
%31 = add i32 %29, %30
%32 = zext i32 %31 to i64
store i64 %32, i64* %11
%33 = load i64, i64* %11
%34 = call i8* @malloc(i64 %33)
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = icmp ne i8* %35, null
br i1 %36, label %41, label %37
37: ; preds = %28
%38 = load i32, i32* @PEM_F_B2I_PVK_BIO
%39 = load i32, i32* @ERR_R_MALLOC_FAILURE
%40 = call i32 @PEMerr(i32 %38, i32 %39)
store i32* null, i32** %4
br label %70
41: ; preds = %28
%42 = load i8*, i8** %9
store i8* %42, i8** %10
%43 = load i32*, i32** %5
%44 = load i8*, i8** %9
%45 = load i64, i64* %11
%46 = call i64 @BIO_read(i32* %43, i8* %44, i64 %45)
%47 = load i64, i64* %11
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %53
49: ; preds = %41
%50 = load i32, i32* @PEM_F_B2I_PVK_BIO
%51 = load i32, i32* @PEM_R_PVK_DATA_TOO_SHORT
%52 = call i32 @PEMerr(i32 %50, i32 %51)
br label %59
53: ; preds = %41
%54 = load i32, i32* %13
%55 = load i32, i32* %14
%56 = load i32*, i32** %6
%57 = load i8*, i8** %7
%58 = call i32* @do_PVK_body(i8** %10, i32 %54, i32 %55, i32* %56, i8* %57)
store i32* %58, i32** %12
br label %59
59: ; preds = %53, %49
%60 = load i8*, i8** %9
%61 = icmp ne i8* %60, null
br i1 %61, label %62, label %68
62: ; preds = %59
%63 = load i8*, i8** %9
%64 = load i64, i64* %11
%65 = call i32 @explicit_bzero(i8* %63, i64 %64)
%66 = load i8*, i8** %9
%67 = call i32 @free(i8* %66)
br label %68
68: ; preds = %62, %59
%69 = load i32*, i32** %12
store i32* %69, i32** %4
br label %70
70: ; preds = %68, %37, %27, %19
%71 = load i32*, i32** %4
ret i32* %71
}

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 i64 @BIO_read(i32* %0, i8* %8, i64 24)
%13 = icmp eq i64 %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 %47
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 %47, label %21
21: ; preds = %18
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = add i32 %23, %22
%25 = zext i32 %24 to i64
%26 = call i8* @malloc(i64 %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = load i32, i32* @PEM_F_B2I_PVK_BIO
%30 = load i32, i32* @ERR_R_MALLOC_FAILURE
%31 = call i32 @PEMerr(i32 %29, i32 %30)
br label %47
32: ; preds = %21
store i8* %26, i8** %5
%33 = call i64 @BIO_read(i32* %0, i8* %26, i64 %25)
%34 = icmp eq i64 %33, %25
br i1 %34, label %39, label %35
35: ; preds = %32
%36 = load i32, i32* @PEM_F_B2I_PVK_BIO
%37 = load i32, i32* @PEM_R_PVK_DATA_TOO_SHORT
%38 = call i32 @PEMerr(i32 %36, i32 %37)
br label %43
39: ; preds = %32
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = call i32* @do_PVK_body(i8** %5, i32 %40, i32 %41, i32* %1, i8* %2)
br label %43
43: ; preds = %35, %39
%44 = phi i32* [ null, %35 ], [ %42, %39 ]
%45 = call i32 @explicit_bzero(i8* %26, i64 %25)
%46 = call i32 @free(i8* %26)
br label %47
47: ; preds = %18, %43, %28, %14
%48 = phi i32* [ null, %14 ], [ %44, %43 ], [ null, %28 ], [ null, %18 ]
ret i32* %48
}