#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_R_KEYBLOB_HEADER_PARSE_ERROR;
int /*<<< orphan*/ PEM_R_KEYBLOB_TOO_SHORT;
int /*<<< orphan*/ PEMerror(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) {
    PEMerror(PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
    return NULL;
  }
  length -= 16;
  if (length < blob_length(bitlen, isdss, ispub)) {
    PEMerror(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     11ae <do_b2i+0x5e>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1030 <PEMerror@plt>
movq   $0x0,-0x8(%rbp)
jmp    122e <do_b2i+0xde>
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   1050 <blob_length@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jae    11f3 <do_b2i+0xa3>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1030 <PEMerror@plt>
movq   $0x0,-0x8(%rbp)
jmp    122e <do_b2i+0xde>
cmpl   $0x0,-0x2c(%rbp)
je     1218 <do_b2i+0xc8>
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    -0x20(%rbp),%rdi
call   1040 <b2i_dss@plt>
mov    %rax,-0x8(%rbp)
jmp    122e <do_b2i+0xde>
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    11a7 <do_b2i+0x57>
add    $0xfffffff0,%ebx
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
mov    0x4(%rsp),%edx
call   1050 <blob_length@plt>
cmp    %eax,%ebx
jae    11b9 <do_b2i+0x69>
mov    0x2e33(%rip),%rax
jmp    11ae <do_b2i+0x5e>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1030 <PEMerror@plt>
xor    %eax,%eax
jmp    11db <do_b2i+0x8b>
cmpl   $0x0,0x8(%rsp)
mov    0xc(%rsp),%edx
mov    0x4(%rsp),%ecx
lea    0x10(%rsp),%rdi
mov    %ebx,%esi
je     11d6 <do_b2i+0x86>
call   1040 <b2i_dss@plt>
jmp    11db <do_b2i+0x8b>
call   1060 <b2i_rsa@plt>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@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 %20
17: ; preds = %3
%18 = load i32, i32* @PEM_R_KEYBLOB_HEADER_PARSE_ERROR
%19 = call i32 @PEMerror(i32 %18)
store i32* null, i32** %4
br label %45
20: ; preds = %3
%21 = load i32, i32* %6
%22 = sub i32 %21, 16
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = load i32, i32* %9
%25 = load i32, i32* %11
%26 = load i32, i32* %7
%27 = call i32 @blob_length(i32 %24, i32 %25, i32 %26)
%28 = icmp ult i32 %23, %27
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i32, i32* @PEM_R_KEYBLOB_TOO_SHORT
%31 = call i32 @PEMerror(i32 %30)
store i32* null, i32** %4
br label %45
32: ; preds = %20
%33 = load i32, i32* %11
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32, i32* %6
%37 = load i32, i32* %9
%38 = load i32, i32* %7
%39 = call i32* @b2i_dss(i8** %8, i32 %36, i32 %37, i32 %38)
store i32* %39, i32** %4
br label %45
40: ; preds = %32
%41 = load i32, i32* %6
%42 = load i32, i32* %9
%43 = load i32, i32* %7
%44 = call i32* @b2i_rsa(i8** %8, i32 %41, i32 %42, i32 %43)
store i32* %44, i32** %4
br label %45
45: ; preds = %40, %35, %29, %17
%46 = load i32*, i32** %4
ret i32* %46
}

LLVM IR (-O2):
@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 %19
16: ; preds = %3
%17 = load i32, i32* @PEM_R_KEYBLOB_HEADER_PARSE_ERROR
%18 = call i32 @PEMerror(i32 %17)
br label %38
19: ; preds = %3
%20 = add i32 %1, -16
%21 = load i32, i32* %6
%22 = load i32, i32* %8
%23 = load i32, i32* %4
%24 = call i32 @blob_length(i32 %21, i32 %22, i32 %23)
%25 = icmp ult i32 %20, %24
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32, i32* @PEM_R_KEYBLOB_TOO_SHORT
%28 = call i32 @PEMerror(i32 %27)
br label %38
29: ; preds = %19
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, 0
%32 = load i32, i32* %6
%33 = load i32, i32* %4
br i1 %31, label %36, label %34
34: ; preds = %29
%35 = call i32* @b2i_dss(i8** %5, i32 %20, i32 %32, i32 %33)
br label %38
36: ; preds = %29
%37 = call i32* @b2i_rsa(i8** %5, i32 %20, i32 %32, i32 %33)
br label %38
38: ; preds = %36, %34, %26, %16
%39 = phi i32* [ null, %16 ], [ null, %26 ], [ %35, %34 ], [ %37, %36 ]
ret i32* %39
}