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

/* Variables and functions */
int /*<<< orphan*/ BIO_flush(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int BIO_read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ BIO_s_mem();
int PEM_write_bio_X509(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *d2i_X509(int /*<<< orphan*/ *, unsigned char const **, int);
int dumpcert(const void *derdata, int len, char *pemdata, int pem_maxlen) {
  int retVal = 0;
  BIO *bio = BIO_new(BIO_s_mem());
  X509 *aX509 = d2i_X509(NULL, (const unsigned char **)&derdata, len);
  if (aX509 != NULL) {
    retVal = PEM_write_bio_X509(bio, aX509);
    BIO_flush(bio);
    if (retVal > 0)
      retVal = BIO_read(bio, pemdata, pem_maxlen);
  }
  BIO_free(bio);
  return retVal;
}


x86 assembly (clang, -O0):
<dumpcert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1080 <BIO_s_mem@plt>
mov    %eax,%edi
call   1050 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x8(%rbp),%rsi
call   1070 <d2i_X509@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11f9 <dumpcert+0x89>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <PEM_write_bio_X509@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <BIO_flush@plt>
cmpl   $0x0,-0x20(%rbp)
jle    11f4 <dumpcert+0x84>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <BIO_read@plt>
mov    %eax,-0x20(%rbp)
jmp    11f9 <dumpcert+0x89>
mov    -0x28(%rbp),%rdi
call   1060 <BIO_free@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dumpcert>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,0x8(%rsp)
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <BIO_s_mem@plt>
mov    %eax,%edi
call   1050 <BIO_new@plt>
mov    %rax,%rbp
lea    0x8(%rsp),%rsi
xor    %edi,%edi
mov    %r12d,%edx
call   1070 <d2i_X509@plt>
test   %rax,%rax
je     11da <dumpcert+0x6a>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1040 <PEM_write_bio_X509@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1090 <BIO_flush@plt>
test   %ebx,%ebx
jle    11da <dumpcert+0x6a>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <BIO_read@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1060 <BIO_free@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dumpcert(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
%12 = call i32 (...) @BIO_s_mem()
%13 = call i32* @BIO_new(i32 %12)
store i32* %13, i32** %10
%14 = load i32, i32* %6
%15 = call i32* @d2i_X509(i32* null, i8** %5, i32 %14)
store i32* %15, i32** %11
%16 = load i32*, i32** %11
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %32
18: ; preds = %4
%19 = load i32*, i32** %10
%20 = load i32*, i32** %11
%21 = call i32 @PEM_write_bio_X509(i32* %19, i32* %20)
store i32 %21, i32* %9
%22 = load i32*, i32** %10
%23 = call i32 @BIO_flush(i32* %22)
%24 = load i32, i32* %9
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %18
%27 = load i32*, i32** %10
%28 = load i8*, i8** %7
%29 = load i32, i32* %8
%30 = call i32 @BIO_read(i32* %27, i8* %28, i32 %29)
store i32 %30, i32* %9
br label %31
31: ; preds = %26, %18
br label %32
32: ; preds = %31, %4
%33 = load i32*, i32** %10
%34 = call i32 @BIO_free(i32* %33)
%35 = load i32, i32* %9
ret i32 %35
}

LLVM IR (-O2):
define i32 @dumpcert(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
store i8* %0, i8** %5
%6 = tail call i32 (...) @BIO_s_mem()
%7 = tail call i32* @BIO_new(i32 %6)
%8 = call i32* @d2i_X509(i32* null, i8** %5, i32 %1)
%9 = icmp eq i32* %8, null
br i1 %9, label %16, label %10
10: ; preds = %4
%11 = call i32 @PEM_write_bio_X509(i32* %7, i32* %8)
%12 = call i32 @BIO_flush(i32* %7)
%13 = icmp sgt i32 %11, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @BIO_read(i32* %7, i8* %2, i32 %3)
br label %16
16: ; preds = %10, %14, %4
%17 = phi i32 [ %15, %14 ], [ %11, %10 ], [ 0, %4 ]
%18 = call i32 @BIO_free(i32* %7)
ret i32 %17
}