#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_free(int /*<<< orphan*/ *);
size_t BIO_get_mem_data(int /*<<< orphan*/ *, char **);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_s_mem();
int /*<<< orphan*/ X509_print(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *ssl_x509_to_str(X509 *crt) {
  BIO *bio;
  char *p, *ret;
  size_t sz;
  bio = BIO_new(BIO_s_mem());
  if (!bio)
    return NULL;
  X509_print(bio, crt);
  sz = BIO_get_mem_data(bio, &p);
  if (!(ret = malloc(sz + 1))) {
    BIO_free(bio);
    return NULL;
  }
  memcpy(ret, p, sz);
  ret[sz] = '\0';
  BIO_free(bio);
  return ret;
}


x86 assembly (clang, -O0):
<ssl_x509_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <BIO_s_mem@plt>
mov    %eax,%edi
call   1050 <BIO_new@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a6 <ssl_x509_to_str+0x36>
movq   $0x0,-0x8(%rbp)
jmp    1223 <ssl_x509_to_str+0xb3>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <X509_print@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <BIO_get_mem_data@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11f5 <ssl_x509_to_str+0x85>
mov    -0x18(%rbp),%rdi
call   1060 <BIO_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1223 <ssl_x509_to_str+0xb3>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1060 <BIO_free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssl_x509_to_str>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <BIO_s_mem@plt>
mov    %eax,%edi
call   1050 <BIO_new@plt>
test   %rax,%rax
je     11e1 <ssl_x509_to_str+0x71>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
call   1090 <X509_print@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1080 <BIO_get_mem_data@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     11d7 <ssl_x509_to_str+0x67>
mov    %rax,%rbx
mov    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   1030 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
jmp    11d9 <ssl_x509_to_str+0x69>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1060 <BIO_free@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @ssl_x509_to_str(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i32* %0, i32** %3
%8 = call i32 (...) @BIO_s_mem()
%9 = call i32* @BIO_new(i32 %8)
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i8* null, i8** %2
br label %37
13: ; preds = %1
%14 = load i32*, i32** %4
%15 = load i32*, i32** %3
%16 = call i32 @X509_print(i32* %14, i32* %15)
%17 = load i32*, i32** %4
%18 = call i64 @BIO_get_mem_data(i32* %17, i8** %5)
store i64 %18, i64* %7
%19 = load i64, i64* %7
%20 = add i64 %19, 1
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %13
%24 = load i32*, i32** %4
%25 = call i32 @BIO_free(i32* %24)
store i8* null, i8** %2
br label %37
26: ; preds = %13
%27 = load i8*, i8** %6
%28 = load i8*, i8** %5
%29 = load i64, i64* %7
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
%31 = load i8*, i8** %6
%32 = load i64, i64* %7
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
%34 = load i32*, i32** %4
%35 = call i32 @BIO_free(i32* %34)
%36 = load i8*, i8** %6
store i8* %36, i8** %2
br label %37
37: ; preds = %26, %23, %12
%38 = load i8*, i8** %2
ret i8* %38
}

LLVM IR (-O2):
define i8* @ssl_x509_to_str(i32* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call i32 (...) @BIO_s_mem()
%5 = tail call i32* @BIO_new(i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %20, label %7
7: ; preds = %1
%8 = tail call i32 @X509_print(i32* %5, i32* %0)
%9 = call i64 @BIO_get_mem_data(i32* %5, i8** %2)
%10 = add i64 %9, 1
%11 = call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %17, label %13
13: ; preds = %7
%14 = load i8*, i8** %2
%15 = call i32 @memcpy(i8* %11, i8* %14, i64 %9)
%16 = getelementptr inbounds i8, i8* %11, i64 %9
store i8 0, i8* %16
br label %17
17: ; preds = %7, %13
%18 = phi i8* [ %11, %13 ], [ null, %7 ]
%19 = call i32 @BIO_free(i32* %5)
br label %20
20: ; preds = %17, %1
%21 = phi i8* [ null, %1 ], [ %18, %17 ]
ret i8* %21
}