#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 */
int BIO_write(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ PEM_F_I2B_PVK_BIO;
int /*<<< orphan*/ PEM_R_BIO_WRITE_FAILURE;
int /*<<< orphan*/ PEMerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ free(unsigned char *);
int i2b_PVK(unsigned char **, int /*<<< orphan*/ *, int, int /*<<< orphan*/ *,
            void *);
int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel, pem_password_cb *cb,
                void *u) {
  unsigned char *tmp = NULL;
  int outlen, wrlen;
  outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
  if (outlen < 0)
    return -1;
  wrlen = BIO_write(out, tmp, outlen);
  free(tmp);
  if (wrlen == outlen) {
    PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);
    return outlen;
  }
  return -1;
}


x86 assembly (clang, -O0):
<i2b_PVK_bio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    -0x38(%rbp),%rdi
call   1040 <i2b_PVK@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1194 <i2b_PVK_bio+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <i2b_PVK_bio+0xa5>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1060 <BIO_write@plt>
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jne    11de <i2b_PVK_bio+0x9e>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e5 <i2b_PVK_bio+0xa5>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2b_PVK_bio>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1040 <i2b_PVK@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11a3 <i2b_PVK_bio+0x63>
mov    %eax,%ebx
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1060 <BIO_write@plt>
mov    %eax,%r14d
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
cmp    %ebx,%r14d
jne    11a3 <i2b_PVK_bio+0x63>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PEM_F_I2B_PVK_BIO = global i32 0
@PEM_R_BIO_WRITE_FAILURE = global i32 0
define i32 @i2b_PVK_bio(i32* %0, i32* %1, i32 %2, i32* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %7
store i32* %1, i32** %8
store i32 %2, i32* %9
store i32* %3, i32** %10
store i8* %4, i8** %11
store i8* null, i8** %12
%15 = load i32*, i32** %8
%16 = load i32, i32* %9
%17 = load i32*, i32** %10
%18 = load i8*, i8** %11
%19 = call i32 @i2b_PVK(i8** %12, i32* %15, i32 %16, i32* %17, i8* %18)
store i32 %19, i32* %13
%20 = load i32, i32* %13
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %5
store i32 -1, i32* %6
br label %39
23: ; preds = %5
%24 = load i32*, i32** %7
%25 = load i8*, i8** %12
%26 = load i32, i32* %13
%27 = call i32 @BIO_write(i32* %24, i8* %25, i32 %26)
store i32 %27, i32* %14
%28 = load i8*, i8** %12
%29 = call i32 @free(i8* %28)
%30 = load i32, i32* %14
%31 = load i32, i32* %13
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %38
33: ; preds = %23
%34 = load i32, i32* @PEM_F_I2B_PVK_BIO
%35 = load i32, i32* @PEM_R_BIO_WRITE_FAILURE
%36 = call i32 @PEMerr(i32 %34, i32 %35)
%37 = load i32, i32* %13
store i32 %37, i32* %6
br label %39
38: ; preds = %23
store i32 -1, i32* %6
br label %39
39: ; preds = %38, %33, %22
%40 = load i32, i32* %6
ret i32 %40
}

LLVM IR (-O2):
@PEM_F_I2B_PVK_BIO = global i32 0
@PEM_R_BIO_WRITE_FAILURE = global i32 0
define i32 @i2b_PVK_bio(i32* %0, i32* %1, i32 %2, i32* %3, i8* %4) {
%6 = alloca i8*
%7 = bitcast i8** %6 to i8*
store i8* null, i8** %6
%8 = call i32 @i2b_PVK(i8** %6, i32* %1, i32 %2, i32* %3, i8* %4)
%9 = icmp slt i32 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %5
%11 = load i8*, i8** %6
%12 = call i32 @BIO_write(i32* %0, i8* %11, i32 %8)
%13 = load i8*, i8** %6
%14 = call i32 @free(i8* %13)
%15 = icmp eq i32 %12, %8
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i32, i32* @PEM_F_I2B_PVK_BIO
%18 = load i32, i32* @PEM_R_BIO_WRITE_FAILURE
%19 = call i32 @PEMerr(i32 %17, i32 %18)
br label %20
20: ; preds = %10, %5, %16
%21 = phi i32 [ %8, %16 ], [ -1, %5 ], [ -1, %10 ]
ret i32 %21
}