#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_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 **, 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) {
    PEMerror(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) {
    PEMerror(ERR_R_MALLOC_FAILURE);
    goto err;
  }
  p = buf;
  if (BIO_read(in, buf, length) != (int)length) {
    PEMerror(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   1050 <BIO_read@plt>
cmp    $0x10,%eax
je     11d5 <do_b2i_bio+0x55>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1030 <PEMerror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d2 <do_b2i_bio+0x152>
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     1212 <do_b2i_bio+0x92>
movq   $0x0,-0x8(%rbp)
jmp    12d2 <do_b2i_bio+0x152>
mov    -0x3c(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1070 <blob_length@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    124d <do_b2i_bio+0xcd>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1030 <PEMerror@plt>
jmp    12c1 <do_b2i_bio+0x141>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1050 <BIO_read@plt>
cmp    -0x44(%rbp),%eax
je     1281 <do_b2i_bio+0x101>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1030 <PEMerror@plt>
jmp    12c1 <do_b2i_bio+0x141>
cmpl   $0x0,-0x48(%rbp)
je     12a6 <do_b2i_bio+0x126>
mov    -0x44(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    -0x20(%rbp),%rdi
call   1060 <b2i_dss@plt>
mov    %rax,-0x50(%rbp)
jmp    12bc <do_b2i_bio+0x13c>
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    12c1 <do_b2i_bio+0x141>
mov    -0x38(%rbp),%rdi
call   1040 <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   1050 <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    123a <do_b2i_bio+0xba>
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
mov    0x4(%rsp),%edx
call   1070 <blob_length@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     123e <do_b2i_bio+0xbe>
mov    %r14,0x10(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <BIO_read@plt>
cmp    %ebp,%eax
jne    1247 <do_b2i_bio+0xc7>
cmpl   $0x0,0x8(%rsp)
mov    0xc(%rsp),%edx
mov    0x4(%rsp),%ecx
lea    0x10(%rsp),%rdi
mov    %ebp,%esi
je     1259 <do_b2i_bio+0xd9>
call   1060 <b2i_dss@plt>
jmp    125e <do_b2i_bio+0xde>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1030 <PEMerror@plt>
xor    %ebx,%ebx
jmp    1269 <do_b2i_bio+0xe9>
mov    0x2da3(%rip),%rax
jmp    124e <do_b2i_bio+0xce>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1030 <PEMerror@plt>
xor    %ebx,%ebx
jmp    1261 <do_b2i_bio+0xe1>
call   1080 <b2i_rsa@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@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 %19, label %16
16: ; preds = %2
%17 = load i32, i32* @PEM_R_KEYBLOB_TOO_SHORT
%18 = call i32 @PEMerror(i32 %17)
br label %50
19: ; preds = %2
store i8* %10, i8** %4
%20 = call i64 @do_blob_header(i8** %4, i32 16, i32* %7, i32* %6, i32* %8, i32* %3)
%21 = icmp slt i64 %20, 1
br i1 %21, label %50, label %22
22: ; preds = %19
%23 = load i32, i32* %6
%24 = load i32, i32* %8
%25 = load i32, i32* %3
%26 = call i32 @blob_length(i32 %23, i32 %24, i32 %25)
%27 = call i8* @malloc(i32 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* @ERR_R_MALLOC_FAILURE
%31 = call i32 @PEMerror(i32 %30)
br label %47
32: ; preds = %22
store i8* %27, i8** %4
%33 = call i32 @BIO_read(i32* %0, i8* %27, i32 %26)
%34 = icmp eq i32 %33, %26
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = load i32, i32* @PEM_R_KEYBLOB_TOO_SHORT
%37 = call i32 @PEMerror(i32 %36)
br label %47
38: ; preds = %32
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, 0
%41 = load i32, i32* %6
%42 = load i32, i32* %3
br i1 %40, label %45, label %43
43: ; preds = %38
%44 = call i32* @b2i_dss(i8** %4, i32 %26, i32 %41, i32 %42)
br label %47
45: ; preds = %38
%46 = call i32* @b2i_rsa(i8** %4, i32 %26, i32 %41, i32 %42)
br label %47
47: ; preds = %43, %45, %35, %29
%48 = phi i32* [ null, %35 ], [ %44, %43 ], [ %46, %45 ], [ null, %29 ]
%49 = call i32 @free(i8* %27)
br label %50
50: ; preds = %19, %47, %16
%51 = phi i32* [ null, %16 ], [ %48, %47 ], [ null, %19 ]
ret i32* %51
}