#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*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_f_base64();
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ *BIO_new_mem_buf(void *, size_t);
int /*<<< orphan*/ *BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
size_t BIO_read(int /*<<< orphan*/ *, unsigned char *, size_t);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(size_t);
size_t pem_base64_estimate_decoded_data_len(unsigned char const *, size_t);
scalar_t__ realloc(unsigned char *, size_t);
int pem_base64_decode(const unsigned char *b64msg, size_t b64msg_len,
                      unsigned char **binbuf, size_t *binbuf_len) {
  BIO *bio;
  BIO *b64;
  size_t allocated_len =
      pem_base64_estimate_decoded_data_len(b64msg, b64msg_len);
  *binbuf = (unsigned char *)malloc(allocated_len);
  bio = BIO_new_mem_buf((void *)b64msg, b64msg_len);
  b64 = BIO_new(BIO_f_base64());
  bio = BIO_push(b64, bio);
  *binbuf_len = BIO_read(bio, *binbuf, b64msg_len);
  assert(*binbuf_len <= allocated_len);
  BIO_free_all(bio);
  if (*binbuf_len <= 0) {
    free(*binbuf);
    *binbuf = NULL;
    *binbuf_len = 0;
    return 0;
  } else if (allocated_len != *binbuf_len) {
    *binbuf = (unsigned char *)realloc(*binbuf, *binbuf_len);
  }
  return 1;
}


x86 assembly (clang, -O0):
<pem_base64_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <pem_base64_estimate_decoded_data_len@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <BIO_new_mem_buf@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   10a0 <BIO_new@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <BIO_push@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <BIO_read@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
cmp    -0x40(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x30(%rbp),%rdi
call   10d0 <BIO_free_all@plt>
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
ja     129b <pem_base64_decode+0xeb>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12d5 <pem_base64_decode+0x125>
mov    -0x40(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
je     12c9 <pem_base64_decode+0x119>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   10b0 <realloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    12ce <pem_base64_decode+0x11e>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pem_base64_decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%rbp
call   10c0 <pem_base64_estimate_decoded_data_len@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1090 <malloc@plt>
mov    %rax,(%r12)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <BIO_new_mem_buf@plt>
mov    %rax,%rbp
xor    %r15d,%r15d
xor    %eax,%eax
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   10a0 <BIO_new@plt>
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <BIO_push@plt>
mov    %rax,%rbp
mov    (%r12),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1050 <BIO_read@plt>
mov    %rax,(%r14)
xor    %edi,%edi
cmp    %r13,%rax
setbe  %dil
call   1070 <assert@plt>
mov    %rbp,%rdi
call   10d0 <BIO_free_all@plt>
mov    (%r14),%rsi
test   %rsi,%rsi
je     1252 <pem_base64_decode+0xa2>
mov    $0x1,%r15d
cmp    %rsi,%r13
je     126a <pem_base64_decode+0xba>
mov    (%r12),%rdi
call   10b0 <realloc@plt>
mov    %rax,(%r12)
jmp    126a <pem_base64_decode+0xba>
mov    (%r12),%rdi
call   1030 <free@plt>
movq   $0x0,(%r12)
movq   $0x0,(%r14)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pem_base64_decode(i8* %0, i64 %1, i8** %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8**
%9 = alloca i64*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8** %2, i8*** %8
store i64* %3, i64** %9
%13 = load i8*, i8** %6
%14 = load i64, i64* %7
%15 = call i64 @pem_base64_estimate_decoded_data_len(i8* %13, i64 %14)
store i64 %15, i64* %12
%16 = load i64, i64* %12
%17 = call i64 @malloc(i64 %16)
%18 = inttoptr i64 %17 to i8*
%19 = load i8**, i8*** %8
store i8* %18, i8** %19
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = call i32* @BIO_new_mem_buf(i8* %20, i64 %21)
store i32* %22, i32** %10
%23 = call i32 (...) @BIO_f_base64()
%24 = call i32* @BIO_new(i32 %23)
store i32* %24, i32** %11
%25 = load i32*, i32** %11
%26 = load i32*, i32** %10
%27 = call i32* @BIO_push(i32* %25, i32* %26)
store i32* %27, i32** %10
%28 = load i32*, i32** %10
%29 = load i8**, i8*** %8
%30 = load i8*, i8** %29
%31 = load i64, i64* %7
%32 = call i64 @BIO_read(i32* %28, i8* %30, i64 %31)
%33 = load i64*, i64** %9
store i64 %32, i64* %33
%34 = load i64*, i64** %9
%35 = load i64, i64* %34
%36 = load i64, i64* %12
%37 = icmp ule i64 %35, %36
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i32*, i32** %10
%41 = call i32 @BIO_free_all(i32* %40)
%42 = load i64*, i64** %9
%43 = load i64, i64* %42
%44 = icmp ule i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %4
%46 = load i8**, i8*** %8
%47 = load i8*, i8** %46
%48 = call i32 @free(i8* %47)
%49 = load i8**, i8*** %8
store i8* null, i8** %49
%50 = load i64*, i64** %9
store i64 0, i64* %50
store i32 0, i32* %5
br label %66
51: ; preds = %4
%52 = load i64, i64* %12
%53 = load i64*, i64** %9
%54 = load i64, i64* %53
%55 = icmp ne i64 %52, %54
br i1 %55, label %56, label %64
56: ; preds = %51
%57 = load i8**, i8*** %8
%58 = load i8*, i8** %57
%59 = load i64*, i64** %9
%60 = load i64, i64* %59
%61 = call i64 @realloc(i8* %58, i64 %60)
%62 = inttoptr i64 %61 to i8*
%63 = load i8**, i8*** %8
store i8* %62, i8** %63
br label %64
64: ; preds = %56, %51
br label %65
65: ; preds = %64
store i32 1, i32* %5
br label %66
66: ; preds = %65, %45
%67 = load i32, i32* %5
ret i32 %67
}

LLVM IR (-O2):
define i32 @pem_base64_decode(i8* %0, i64 %1, i8** nocapture %2, i64* nocapture %3) {
%5 = tail call i64 @pem_base64_estimate_decoded_data_len(i8* %0, i64 %1)
%6 = tail call i64 @malloc(i64 %5)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %2
%8 = tail call i32* @BIO_new_mem_buf(i8* %0, i64 %1)
%9 = tail call i32 (...) @BIO_f_base64()
%10 = tail call i32* @BIO_new(i32 %9)
%11 = tail call i32* @BIO_push(i32* %10, i32* %8)
%12 = load i8*, i8** %2
%13 = tail call i64 @BIO_read(i32* %11, i8* %12, i64 %1)
store i64 %13, i64* %3
%14 = icmp ule i64 %13, %5
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = tail call i32 @BIO_free_all(i32* %11)
%18 = load i64, i64* %3
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %4
%21 = load i8*, i8** %2
%22 = tail call i32 @free(i8* %21)
store i8* null, i8** %2
store i64 0, i64* %3
br label %29
23: ; preds = %4
%24 = icmp eq i64 %5, %18
br i1 %24, label %29, label %25
25: ; preds = %23
%26 = load i8*, i8** %2
%27 = tail call i64 @realloc(i8* %26, i64 %18)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %2
br label %29
29: ; preds = %25, %23, %20
%30 = phi i32 [ 0, %20 ], [ 1, %23 ], [ 1, %25 ]
ret i32 %30
}