#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ pubkeyhash;
typedef int /*<<< orphan*/ X509;
struct TYPE_3__ {
  char const *data;
  int length;
};
typedef int /*<<< orphan*/ EVP_PKEY;
typedef TYPE_1__ BUF_MEM;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_FLAGS_BASE64_NO_NL;
int /*<<< orphan*/ BIO_f_base64();
int /*<<< orphan*/ BIO_flush(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_get_mem_ptr(int /*<<< orphan*/ *, TYPE_1__ **);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ *BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_s_mem();
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_write(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA256(unsigned char *, int, unsigned char *);
int /*<<< orphan*/ *X509_get_pubkey(int /*<<< orphan*/ *);
int /*<<< orphan*/ i2d_PUBKEY_bio(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *x509_to_curl_ppk_string(X509 *x509) {
  BIO *bio = BIO_new(BIO_s_mem());
  EVP_PKEY *pubkey = X509_get_pubkey(x509);
  i2d_PUBKEY_bio(bio, pubkey);
  EVP_PKEY_free(pubkey);
  BUF_MEM *mem;
  BIO_get_mem_ptr(bio, &mem);
  unsigned char pubkeyhash[32];
  SHA256((unsigned char *)mem->data, mem->length, pubkeyhash);
  BIO_free(bio);
  bio = BIO_new(BIO_s_mem());
  BIO *b64 = BIO_new(BIO_f_base64());
  bio = BIO_push(b64, bio);
  BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  BIO_write(bio, pubkeyhash, sizeof(pubkeyhash));
  BIO_flush(bio);
  BIO_get_mem_ptr(bio, &mem);
  const char *prefix = "sha256//";
  char *ret = malloc(strlen(prefix) + mem->length + 1);
  memcpy(ret, prefix, strlen(prefix));
  memcpy(&ret[strlen(prefix)], mem->data, mem->length);
  ret[strlen(prefix) + mem->length] = 0;
  BIO_free_all(bio);
  return ret;
}


x86 assembly (clang, -O0):
<x509_to_curl_ppk_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1120 <BIO_s_mem@plt>
mov    %eax,%edi
call   10c0 <BIO_new@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <X509_get_pubkey@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10e0 <i2d_PUBKEY_bio@plt>
mov    -0x18(%rbp),%rdi
call   1070 <EVP_PKEY_free@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1100 <BIO_get_mem_ptr@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <SHA256@plt>
mov    -0x10(%rbp),%rdi
call   10d0 <BIO_free@plt>
mov    $0x0,%al
call   1120 <BIO_s_mem@plt>
mov    %eax,%edi
call   10c0 <BIO_new@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   10c0 <BIO_new@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <BIO_push@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
call   1080 <BIO_set_flags@plt>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1110 <BIO_write@plt>
mov    -0x10(%rbp),%rdi
call   1130 <BIO_flush@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1100 <BIO_get_mem_ptr@plt>
lea    0xd09(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x20(%rbp),%rcx
movslq 0x8(%rcx),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rsi
mov    %rax,%rdx
call   10a0 <memcpy@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x68(%rbp),%rdi
add    %rax,%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
movslq 0x8(%rax),%rdx
call   10a0 <memcpy@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
mov    -0x20(%rbp),%rdx
movslq 0x8(%rdx),%rdx
add    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   10f0 <BIO_free_all@plt>
mov    -0x58(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x509_to_curl_ppk_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1110 <BIO_s_mem@plt>
mov    %eax,%edi
call   10b0 <BIO_new@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1080 <X509_get_pubkey@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
call   10d0 <i2d_PUBKEY_bio@plt>
mov    %rbx,%rdi
call   1060 <EVP_PKEY_free@plt>
lea    0x8(%rsp),%r14
mov    %r12,%rdi
mov    %r14,%rsi
call   10f0 <BIO_get_mem_ptr@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%rdi
mov    0x8(%rax),%esi
lea    0x10(%rsp),%r15
mov    %r15,%rdx
call   1030 <SHA256@plt>
mov    %r12,%rdi
call   10c0 <BIO_free@plt>
xor    %eax,%eax
call   1110 <BIO_s_mem@plt>
mov    %eax,%edi
call   10b0 <BIO_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <BIO_f_base64@plt>
mov    %eax,%edi
call   10b0 <BIO_new@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <BIO_push@plt>
mov    %rax,%r12
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1070 <BIO_set_flags@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x20,%edx
call   1100 <BIO_write@plt>
mov    %r12,%rdi
call   1120 <BIO_flush@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   10f0 <BIO_get_mem_ptr@plt>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%edi
add    $0x9,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
lea    0xd18(%rip),%rsi
mov    $0x8,%edx
mov    %rax,%rdi
call   1090 <memcpy@plt>
lea    0x8(%rbx),%rdi
mov    0x8(%rsp),%rax
mov    (%rax),%rsi
movslq 0x8(%rax),%rdx
call   1090 <memcpy@plt>
mov    0x8(%rsp),%rax
movslq 0x8(%rax),%rax
movb   $0x0,0x8(%rax,%rbx,1)
mov    %r12,%rdi
call   10e0 <BIO_free_all@plt>
mov    %rbx,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i8*, i32 }
@BIO_FLAGS_BASE64_NO_NL = global i32 0
@.str = [9 x i8] c"sha256//\00"
define i8* @x509_to_curl_ppk_string(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca %struct.TYPE_3__*
%6 = alloca [32 x i8]
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
store i32* %0, i32** %2
%10 = call i32 (...) @BIO_s_mem()
%11 = call i32* @BIO_new(i32 %10)
store i32* %11, i32** %3
%12 = load i32*, i32** %2
%13 = call i32* @X509_get_pubkey(i32* %12)
store i32* %13, i32** %4
%14 = load i32*, i32** %3
%15 = load i32*, i32** %4
%16 = call i32 @i2d_PUBKEY_bio(i32* %14, i32* %15)
%17 = load i32*, i32** %4
%18 = call i32 @EVP_PKEY_free(i32* %17)
%19 = load i32*, i32** %3
%20 = call i32 @BIO_get_mem_ptr(i32* %19, %struct.TYPE_3__** %5)
%21 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%28 = call i32 @SHA256(i8* %23, i32 %26, i8* %27)
%29 = load i32*, i32** %3
%30 = call i32 @BIO_free(i32* %29)
%31 = call i32 (...) @BIO_s_mem()
%32 = call i32* @BIO_new(i32 %31)
store i32* %32, i32** %3
%33 = call i32 (...) @BIO_f_base64()
%34 = call i32* @BIO_new(i32 %33)
store i32* %34, i32** %7
%35 = load i32*, i32** %7
%36 = load i32*, i32** %3
%37 = call i32* @BIO_push(i32* %35, i32* %36)
store i32* %37, i32** %3
%38 = load i32*, i32** %3
%39 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%40 = call i32 @BIO_set_flags(i32* %38, i32 %39)
%41 = load i32*, i32** %3
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%43 = call i32 @BIO_write(i32* %41, i8* %42, i32 32)
%44 = load i32*, i32** %3
%45 = call i32 @BIO_flush(i32* %44)
%46 = load i32*, i32** %3
%47 = call i32 @BIO_get_mem_ptr(i32* %46, %struct.TYPE_3__** %5)
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %8
%48 = load i8*, i8** %8
%49 = call i64 @strlen(i8* %48)
%50 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%51 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %50, i32 0, i32 1
%52 = load i32, i32* %51
%53 = sext i32 %52 to i64
%54 = add i64 %49, %53
%55 = add i64 %54, 1
%56 = trunc i64 %55 to i32
%57 = call i8* @malloc(i32 %56)
store i8* %57, i8** %9
%58 = load i8*, i8** %9
%59 = load i8*, i8** %8
%60 = load i8*, i8** %8
%61 = call i64 @strlen(i8* %60)
%62 = call i32 @memcpy(i8* %58, i8* %59, i64 %61)
%63 = load i8*, i8** %9
%64 = load i8*, i8** %8
%65 = call i64 @strlen(i8* %64)
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%68 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %67, i32 0, i32 0
%69 = load i8*, i8** %68
%70 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%71 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %70, i32 0, i32 1
%72 = load i32, i32* %71
%73 = sext i32 %72 to i64
%74 = call i32 @memcpy(i8* %66, i8* %69, i64 %73)
%75 = load i8*, i8** %9
%76 = load i8*, i8** %8
%77 = call i64 @strlen(i8* %76)
%78 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%79 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %78, i32 0, i32 1
%80 = load i32, i32* %79
%81 = sext i32 %80 to i64
%82 = add i64 %77, %81
%83 = getelementptr inbounds i8, i8* %75, i64 %82
store i8 0, i8* %83
%84 = load i32*, i32** %3
%85 = call i32 @BIO_free_all(i32* %84)
%86 = load i8*, i8** %9
ret i8* %86
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i8*, i32 }
@BIO_FLAGS_BASE64_NO_NL = global i32 0
@.str = [9 x i8] c"sha256//\00"
define i8* @x509_to_curl_ppk_string(i32* %0) {
%2 = alloca %struct.TYPE_3__*
%3 = alloca [32 x i8]
%4 = tail call i32 (...) @BIO_s_mem()
%5 = tail call i32* @BIO_new(i32 %4)
%6 = tail call i32* @X509_get_pubkey(i32* %0)
%7 = tail call i32 @i2d_PUBKEY_bio(i32* %5, i32* %6)
%8 = tail call i32 @EVP_PKEY_free(i32* %6)
%9 = bitcast %struct.TYPE_3__** %2 to i8*
%10 = call i32 @BIO_get_mem_ptr(i32* %5, %struct.TYPE_3__** %2)
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%12 = load %struct.TYPE_3__*, %struct.TYPE_3__** %2
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %12, i64 0, i32 1
%16 = load i32, i32* %15
%17 = call i32 @SHA256(i8* %14, i32 %16, i8* %11)
%18 = call i32 @BIO_free(i32* %5)
%19 = call i32 (...) @BIO_s_mem()
%20 = call i32* @BIO_new(i32 %19)
%21 = call i32 (...) @BIO_f_base64()
%22 = call i32* @BIO_new(i32 %21)
%23 = call i32* @BIO_push(i32* %22, i32* %20)
%24 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%25 = call i32 @BIO_set_flags(i32* %23, i32 %24)
%26 = call i32 @BIO_write(i32* %23, i8* %11, i32 32)
%27 = call i32 @BIO_flush(i32* %23)
%28 = call i32 @BIO_get_mem_ptr(i32* %23, %struct.TYPE_3__** %2)
%29 = load %struct.TYPE_3__*, %struct.TYPE_3__** %2
%30 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %29, i64 0, i32 1
%31 = load i32, i32* %30
%32 = add i32 %31, 9
%33 = call i8* @malloc(i32 %32)
%34 = call i32 @memcpy(i8* %33, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i64 8)
%35 = getelementptr inbounds i8, i8* %33, i64 8
%36 = load %struct.TYPE_3__*, %struct.TYPE_3__** %2
%37 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %36, i64 0, i32 0
%38 = load i8*, i8** %37
%39 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %36, i64 0, i32 1
%40 = load i32, i32* %39
%41 = sext i32 %40 to i64
%42 = call i32 @memcpy(i8* %35, i8* %38, i64 %41)
%43 = load %struct.TYPE_3__*, %struct.TYPE_3__** %2
%44 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %43, i64 0, i32 1
%45 = load i32, i32* %44
%46 = sext i32 %45 to i64
%47 = add nsw i64 %46, 8
%48 = getelementptr inbounds i8, i8* %33, i64 %47
store i8 0, i8* %48
%49 = call i32 @BIO_free_all(i32* %23)
ret i8* %33
}