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

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_gets(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_s_mem();
int X509_NAME_print_ex(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                       int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ XN_FLAG_ONELINE;
int safer_X509_NAME_oneline(X509_NAME *name, char *buf, size_t sz) {
  BIO *bio = BIO_new(BIO_s_mem());
  if (bio) {
    int len = X509_NAME_print_ex(bio, name, 0, XN_FLAG_ONELINE);
    BIO_gets(bio, buf, (int)sz);
    BIO_free(bio);
    return len;
  } else {
    buf[0] = '\0';
    return -1;
  }
}


x86 assembly (clang, -O0):
<safer_X509_NAME_oneline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   1070 <BIO_s_mem@plt>
mov    %eax,%edi
call   1030 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11c3 <safer_X509_NAME_oneline+0x73>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e58(%rip),%rax
mov    (%rax),%ecx
xor    %edx,%edx
call   1060 <X509_NAME_print_ex@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
call   1050 <BIO_gets@plt>
mov    -0x28(%rbp),%rdi
call   1040 <BIO_free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <safer_X509_NAME_oneline+0x81>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safer_X509_NAME_oneline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %eax,%eax
call   1070 <BIO_s_mem@plt>
mov    %eax,%edi
call   1030 <BIO_new@plt>
test   %rax,%rax
je     11a9 <safer_X509_NAME_oneline+0x59>
mov    %rax,%rbx
mov    0x2e6a(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1060 <X509_NAME_print_ex@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <BIO_gets@plt>
mov    %rbx,%rdi
call   1040 <BIO_free@plt>
mov    %ebp,%eax
jmp    11b2 <safer_X509_NAME_oneline+0x62>
movb   $0x0,(%r15)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@XN_FLAG_ONELINE = global i32 0
define i32 @safer_X509_NAME_oneline(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = call i32 (...) @BIO_s_mem()
%11 = call i32* @BIO_new(i32 %10)
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %27
14: ; preds = %3
%15 = load i32*, i32** %8
%16 = load i32*, i32** %5
%17 = load i32, i32* @XN_FLAG_ONELINE
%18 = call i32 @X509_NAME_print_ex(i32* %15, i32* %16, i32 0, i32 %17)
store i32 %18, i32* %9
%19 = load i32*, i32** %8
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = trunc i64 %21 to i32
%23 = call i32 @BIO_gets(i32* %19, i8* %20, i32 %22)
%24 = load i32*, i32** %8
%25 = call i32 @BIO_free(i32* %24)
%26 = load i32, i32* %9
store i32 %26, i32* %4
br label %30
27: ; preds = %3
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i64 0
store i8 0, i8* %29
store i32 -1, i32* %4
br label %30
30: ; preds = %27, %14
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@XN_FLAG_ONELINE = global i32 0
define i32 @safer_X509_NAME_oneline(i32* %0, i8* %1, i64 %2) {
%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 %13, label %7
7: ; preds = %3
%8 = load i32, i32* @XN_FLAG_ONELINE
%9 = tail call i32 @X509_NAME_print_ex(i32* %5, i32* %0, i32 0, i32 %8)
%10 = trunc i64 %2 to i32
%11 = tail call i32 @BIO_gets(i32* %5, i8* %1, i32 %10)
%12 = tail call i32 @BIO_free(i32* %5)
br label %14
13: ; preds = %3
store i8 0, i8* %1
br label %14
14: ; preds = %13, %7
%15 = phi i32 [ %9, %7 ], [ -1, %13 ]
ret i32 %15
}