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

/* Variables and functions */
int /*<<< orphan*/ PEM_F_DO_B2I;
int /*<<< orphan*/ PEM_R_KEYBLOB_HEADER_PARSE_ERROR;
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 **, unsigned int, unsigned int *,
                          unsigned int *, int *, int *);
EVP_PKEY *do_b2i(const unsigned char **in, unsigned int length, int ispub) {
  const unsigned char *p = *in;
  unsigned int bitlen, magic;
  int isdss;
  if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
    PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
    return NULL;
  }
  length -= 16;
  if (length < blob_length(bitlen, isdss, ispub)) {
    PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT);
    return NULL;
  }
  if (isdss)
    return b2i_dss(&p, length, bitlen, ispub);
  else
    return b2i_rsa(&p, length, bitlen, ispub);
}


x86 assembly (clang, -O0):
<do_b2i>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rdx
lea    -0x24(%rbp),%rcx
lea    -0x2c(%rbp),%r8
lea    -0x18(%rbp),%r9
call   1070 <do_blob_header@plt>
cmp    $0x0,%rax
jg     11b7 <do_b2i+0x67>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    1240 <do_b2i+0xf0>
mov    -0x14(%rbp),%eax
sub    $0x10,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <blob_length@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jae    1205 <do_b2i+0xb5>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    1240 <do_b2i+0xf0>
cmpl   $0x0,-0x2c(%rbp)
je     122a <do_b2i+0xda>
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    -0x20(%rbp),%rdi
call   1030 <b2i_dss@plt>
mov    %rax,-0x8(%rbp)
jmp    1240 <do_b2i+0xf0>
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    -0x20(%rbp),%rdi
call   1060 <b2i_rsa@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_b2i>:
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %edx,0x4(%rsp)
mov    (%rdi),%rax
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rdi
lea    0x1c(%rsp),%rdx
lea    0xc(%rsp),%rcx
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
call   1070 <do_blob_header@plt>
test   %rax,%rax
jle    11b0 <do_b2i+0x60>
add    $0xfffffff0,%ebx
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
mov    0x4(%rsp),%edx
call   1040 <blob_length@plt>
cmp    %eax,%ebx
jae    11cb <do_b2i+0x7b>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
jmp    11c0 <do_b2i+0x70>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
xor    %eax,%eax
jmp    11ed <do_b2i+0x9d>
cmpl   $0x0,0x8(%rsp)
mov    0xc(%rsp),%edx
mov    0x4(%rsp),%ecx
lea    0x10(%rsp),%rdi
mov    %ebx,%esi
je     11e8 <do_b2i+0x98>
call   1030 <b2i_dss@plt>
jmp    11ed <do_b2i+0x9d>
call   1060 <b2i_rsa@plt>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@PEM_F_DO_B2I = global i32 0
@PEM_R_KEYBLOB_HEADER_PARSE_ERROR = global i32 0
@PEM_R_KEYBLOB_TOO_SHORT = global i32 0
define i32* @do_b2i(i8** %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i8**, i8*** %5
%13 = load i8*, i8** %12
store i8* %13, i8** %8
%14 = load i32, i32* %6
%15 = call i64 @do_blob_header(i8** %8, i32 %14, i32* %10, i32* %9, i32* %11, i32* %7)
%16 = icmp sle i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = load i32, i32* @PEM_F_DO_B2I
%19 = load i32, i32* @PEM_R_KEYBLOB_HEADER_PARSE_ERROR
%20 = call i32 @PEMerr(i32 %18, i32 %19)
store i32* null, i32** %4
br label %47
21: ; preds = %3
%22 = load i32, i32* %6
%23 = sub i32 %22, 16
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = load i32, i32* %9
%26 = load i32, i32* %11
%27 = load i32, i32* %7
%28 = call i32 @blob_length(i32 %25, i32 %26, i32 %27)
%29 = icmp ult i32 %24, %28
br i1 %29, label %30, label %34
30: ; preds = %21
%31 = load i32, i32* @PEM_F_DO_B2I
%32 = load i32, i32* @PEM_R_KEYBLOB_TOO_SHORT
%33 = call i32 @PEMerr(i32 %31, i32 %32)
store i32* null, i32** %4
br label %47
34: ; preds = %21
%35 = load i32, i32* %11
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i32, i32* %6
%39 = load i32, i32* %9
%40 = load i32, i32* %7
%41 = call i32* @b2i_dss(i8** %8, i32 %38, i32 %39, i32 %40)
store i32* %41, i32** %4
br label %47
42: ; preds = %34
%43 = load i32, i32* %6
%44 = load i32, i32* %9
%45 = load i32, i32* %7
%46 = call i32* @b2i_rsa(i8** %8, i32 %43, i32 %44, i32 %45)
store i32* %46, i32** %4
br label %47
47: ; preds = %42, %37, %30, %17
%48 = load i32*, i32** %4
ret i32* %48
}

LLVM IR (-O2):
@PEM_F_DO_B2I = global i32 0
@PEM_R_KEYBLOB_HEADER_PARSE_ERROR = global i32 0
@PEM_R_KEYBLOB_TOO_SHORT = global i32 0
define i32* @do_b2i(i8** nocapture %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %2, i32* %4
%9 = bitcast i8** %5 to i8*
%10 = load i8*, i8** %0
store i8* %10, i8** %5
%11 = bitcast i32* %6 to i8*
%12 = bitcast i32* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = call i64 @do_blob_header(i8** %5, i32 %1, i32* %7, i32* %6, i32* %8, i32* %4)
%15 = icmp slt i64 %14, 1
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* @PEM_F_DO_B2I
%18 = load i32, i32* @PEM_R_KEYBLOB_HEADER_PARSE_ERROR
%19 = call i32 @PEMerr(i32 %17, i32 %18)
br label %40
20: ; preds = %3
%21 = add i32 %1, -16
%22 = load i32, i32* %6
%23 = load i32, i32* %8
%24 = load i32, i32* %4
%25 = call i32 @blob_length(i32 %22, i32 %23, i32 %24)
%26 = icmp ult i32 %21, %25
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = load i32, i32* @PEM_F_DO_B2I
%29 = load i32, i32* @PEM_R_KEYBLOB_TOO_SHORT
%30 = call i32 @PEMerr(i32 %28, i32 %29)
br label %40
31: ; preds = %20
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, 0
%34 = load i32, i32* %6
%35 = load i32, i32* %4
br i1 %33, label %38, label %36
36: ; preds = %31
%37 = call i32* @b2i_dss(i8** %5, i32 %21, i32 %34, i32 %35)
br label %40
38: ; preds = %31
%39 = call i32* @b2i_rsa(i8** %5, i32 %21, i32 %34, i32 %35)
br label %40
40: ; preds = %38, %36, %27, %16
%41 = phi i32* [ null, %16 ], [ null, %27 ], [ %37, %36 ], [ %39, %38 ]
ret i32* %41
}