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

/* Variables and functions */
int BIO_read(int /*<<< orphan*/ *, unsigned char *, unsigned int);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ PEM_F_DO_B2I_BIO;
int /*<<< orphan*/ PEM_R_KEYBLOB_TOO_SHORT;
int /*<<< orphan*/ PEMerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *b2i_dss(unsigned char const **, unsigned int, unsigned int,
                            int);
int /*<<< orphan*/ *b2i_rsa(unsigned char const **, unsigned int, unsigned int,
                            int);
unsigned int blob_length(unsigned int, int, int);
scalar_t__ do_blob_header(unsigned char const **, int, unsigned int *,
                          unsigned int *, int *, int *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(unsigned int);
EVP_PKEY *do_b2i_bio(BIO *in, int ispub) {
  const unsigned char *p;
  unsigned char hdr_buf[16], *buf = NULL;
  unsigned int bitlen, magic, length;
  int isdss;
  EVP_PKEY *ret = NULL;
  if (BIO_read(in, hdr_buf, 16) != 16) {
    PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
    return NULL;
  }
  p = hdr_buf;
  if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
    return NULL;
  length = blob_length(bitlen, isdss, ispub);
  buf = malloc(length);
  if (!buf) {
    PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE);
    goto err;
  }
  p = buf;
  if (BIO_read(in, buf, length) != (int)length) {
    PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
    goto err;
  }
  if (isdss)
    ret = b2i_dss(&p, length, bitlen, ispub);
  else
    ret = b2i_rsa(&p, length, bitlen, ispub);
err:
  free(buf);
  return ret;
}


x86 assembly (clang, -O0):
<do_b2i_bio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1040 <BIO_read@plt>
cmp    $0x10,%eax
je     11de <do_b2i_bio+0x5e>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1070 <PEMerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ed <do_b2i_bio+0x16d>
lea    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x10,%esi
lea    -0x40(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
lea    -0x48(%rbp),%r8
lea    -0x14(%rbp),%r9
call   10a0 <do_blob_header@plt>
cmp    $0x0,%rax
jg     121b <do_b2i_bio+0x9b>
movq   $0x0,-0x8(%rbp)
jmp    12ed <do_b2i_bio+0x16d>
mov    -0x3c(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1060 <blob_length@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    125f <do_b2i_bio+0xdf>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
call   1070 <PEMerr@plt>
jmp    12dc <do_b2i_bio+0x15c>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1040 <BIO_read@plt>
cmp    -0x44(%rbp),%eax
je     129c <do_b2i_bio+0x11c>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
call   1070 <PEMerr@plt>
jmp    12dc <do_b2i_bio+0x15c>
cmpl   $0x0,-0x48(%rbp)
je     12c1 <do_b2i_bio+0x141>
mov    -0x44(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    -0x20(%rbp),%rdi
call   1050 <b2i_dss@plt>
mov    %rax,-0x50(%rbp)
jmp    12d7 <do_b2i_bio+0x157>
mov    -0x44(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    -0x20(%rbp),%rdi
call   1080 <b2i_rsa@plt>
mov    %rax,-0x50(%rbp)
jmp    12dc <do_b2i_bio+0x15c>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_b2i_bio>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
mov    %esi,0x4(%rsp)
lea    0x20(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x10,%edx
call   1040 <BIO_read@plt>
cmp    $0x10,%eax
jne    122c <do_b2i_bio+0xac>
mov    %rbp,0x10(%rsp)
lea    0x10(%rsp),%rdi
lea    0x1c(%rsp),%rdx
lea    0xc(%rsp),%rcx
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    $0x10,%esi
call   10a0 <do_blob_header@plt>
test   %rax,%rax
jle    1243 <do_b2i_bio+0xc3>
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
mov    0x4(%rsp),%edx
call   1060 <blob_length@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1247 <do_b2i_bio+0xc7>
mov    %r14,0x10(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <BIO_read@plt>
cmp    %ebp,%eax
jne    1259 <do_b2i_bio+0xd9>
cmpl   $0x0,0x8(%rsp)
mov    0xc(%rsp),%edx
mov    0x4(%rsp),%ecx
lea    0x10(%rsp),%rdi
mov    %ebp,%esi
je     1274 <do_b2i_bio+0xf4>
call   1050 <b2i_dss@plt>
jmp    1279 <do_b2i_bio+0xf9>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
call   1070 <PEMerr@plt>
xor    %ebx,%ebx
jmp    1284 <do_b2i_bio+0x104>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    0x2d91(%rip),%rax
jmp    1269 <do_b2i_bio+0xe9>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
call   1070 <PEMerr@plt>
xor    %ebx,%ebx
jmp    127c <do_b2i_bio+0xfc>
call   1080 <b2i_rsa@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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