#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_ENTRY;
typedef int /*<<< orphan*/ X509_NAME;
typedef int /*<<< orphan*/ ASN1_STRING;

/* Variables and functions */
int ASN1_STRING_to_UTF8(unsigned char **, int /*<<< orphan*/ *);
int /*<<< orphan*/ OPENSSL_free(unsigned char *);
int /*<<< orphan*/ *X509_NAME_ENTRY_get_data(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_NAME_get_entry(int /*<<< orphan*/ *, int);
int X509_NAME_get_index_by_NID(int /*<<< orphan*/ *, int, int);
size_t snprintf(char *, size_t, char *, unsigned char *);
void NIDToCString(X509_NAME *x509Name, int nid, char *cNid, size_t buflen) {
  if (x509Name) {
    size_t bufRemaining = buflen;
    char *pOut = cNid;
    int lastpos = X509_NAME_get_index_by_NID(x509Name, nid, -1);
    while (lastpos != -1) {
      X509_NAME_ENTRY *xEntry = X509_NAME_get_entry(x509Name, lastpos);
      if (xEntry) {
        ASN1_STRING
        *asnStr = X509_NAME_ENTRY_get_data(xEntry);
        unsigned char *asnOut = 0;
        int asnLen = ASN1_STRING_to_UTF8(&asnOut, asnStr);
        if (asnLen != -1) {
          size_t bytesWritten = snprintf(pOut, bufRemaining, "%s", asnOut);
          bufRemaining -= bytesWritten;
          pOut += bytesWritten;
          OPENSSL_free(asnOut);
        }
      }
      lastpos = X509_NAME_get_index_by_NID(x509Name, nid, lastpos);
    }
  }
}


x86 assembly (clang, -O0):
<NIDToCString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     125b <NIDToCString+0xfb>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0xffffffff,%edx
call   1060 <X509_NAME_get_index_by_NID@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
je     1256 <NIDToCString+0xf6>
mov    -0x8(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1050 <X509_NAME_get_entry@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     123f <NIDToCString+0xdf>
mov    -0x40(%rbp),%rdi
call   1030 <X509_NAME_ENTRY_get_data@plt>
mov    %rax,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x48(%rbp),%rsi
lea    -0x50(%rbp),%rdi
call   1080 <ASN1_STRING_to_UTF8@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
je     123a <NIDToCString+0xda>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x50(%rbp),%rcx
lea    0xdf3(%rip),%rdx
call   1040 <snprintf@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x60(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x50(%rbp),%rdi
call   1070 <OPENSSL_free@plt>
jmp    123f <NIDToCString+0xdf>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1060 <X509_NAME_get_index_by_NID@plt>
mov    %eax,-0x34(%rbp)
jmp    11a6 <NIDToCString+0x46>
jmp    125b <NIDToCString+0xfb>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NIDToCString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     120a <NIDToCString+0xaa>
mov    %rcx,%r14
mov    %rdx,%r12
mov    %esi,%r15d
mov    %rdi,%rbp
mov    $0xffffffff,%edx
call   1060 <X509_NAME_get_index_by_NID@plt>
cmp    $0xffffffff,%eax
je     120a <NIDToCString+0xaa>
mov    %eax,%ebx
mov    %rsp,%r13
jmp    11b4 <NIDToCString+0x54>
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %ebx,%edx
call   1060 <X509_NAME_get_index_by_NID@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     120a <NIDToCString+0xaa>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1050 <X509_NAME_get_entry@plt>
test   %rax,%rax
je     11a0 <NIDToCString+0x40>
mov    %rax,%rdi
call   1030 <X509_NAME_ENTRY_get_data@plt>
movq   $0x0,(%rsp)
mov    %r13,%rdi
mov    %rax,%rsi
call   1080 <ASN1_STRING_to_UTF8@plt>
cmp    $0xffffffff,%eax
je     11a0 <NIDToCString+0x40>
mov    (%rsp),%rcx
mov    %r12,%rdi
mov    %r14,%rsi
lea    0xe0c(%rip),%rdx
call   1040 <snprintf@plt>
sub    %rax,%r14
add    %rax,%r12
mov    (%rsp),%rdi
call   1070 <OPENSSL_free@plt>
jmp    11a0 <NIDToCString+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define void @NIDToCString(i32* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i64
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%17 = load i32*, i32** %5
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %61
19: ; preds = %4
%20 = load i64, i64* %8
store i64 %20, i64* %9
%21 = load i8*, i8** %7
store i8* %21, i8** %10
%22 = load i32*, i32** %5
%23 = load i32, i32* %6
%24 = call i32 @X509_NAME_get_index_by_NID(i32* %22, i32 %23, i32 -1)
store i32 %24, i32* %11
br label %25
25: ; preds = %55, %19
%26 = load i32, i32* %11
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %60
28: ; preds = %25
%29 = load i32*, i32** %5
%30 = load i32, i32* %11
%31 = call i32* @X509_NAME_get_entry(i32* %29, i32 %30)
store i32* %31, i32** %12
%32 = load i32*, i32** %12
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %55
34: ; preds = %28
%35 = load i32*, i32** %12
%36 = call i32* @X509_NAME_ENTRY_get_data(i32* %35)
store i32* %36, i32** %13
store i8* null, i8** %14
%37 = load i32*, i32** %13
%38 = call i32 @ASN1_STRING_to_UTF8(i8** %14, i32* %37)
store i32 %38, i32* %15
%39 = load i32, i32* %15
%40 = icmp ne i32 %39, -1
br i1 %40, label %41, label %54
41: ; preds = %34
%42 = load i8*, i8** %10
%43 = load i64, i64* %9
%44 = load i8*, i8** %14
%45 = call i64 @snprintf(i8* %42, i64 %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %44)
store i64 %45, i64* %16
%46 = load i64, i64* %16
%47 = load i64, i64* %9
%48 = sub i64 %47, %46
store i64 %48, i64* %9
%49 = load i64, i64* %16
%50 = load i8*, i8** %10
%51 = getelementptr inbounds i8, i8* %50, i64 %49
store i8* %51, i8** %10
%52 = load i8*, i8** %14
%53 = call i32 @OPENSSL_free(i8* %52)
br label %54
54: ; preds = %41, %34
br label %55
55: ; preds = %54, %28
%56 = load i32*, i32** %5
%57 = load i32, i32* %6
%58 = load i32, i32* %11
%59 = call i32 @X509_NAME_get_index_by_NID(i32* %56, i32 %57, i32 %58)
store i32 %59, i32* %11
br label %25
60: ; preds = %25
br label %61
61: ; preds = %60, %4
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define void @NIDToCString(i32* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = icmp eq i32* %0, null
br i1 %6, label %37, label %7
7: ; preds = %4
%8 = tail call i32 @X509_NAME_get_index_by_NID(i32* %0, i32 %1, i32 -1)
%9 = icmp eq i32 %8, -1
br i1 %9, label %37, label %10
10: ; preds = %7
%11 = bitcast i8** %5 to i8*
br label %12
12: ; preds = %10, %32
%13 = phi i64 [ %3, %10 ], [ %34, %32 ]
%14 = phi i8* [ %2, %10 ], [ %33, %32 ]
%15 = phi i32 [ %8, %10 ], [ %35, %32 ]
%16 = call i32* @X509_NAME_get_entry(i32* %0, i32 %15)
%17 = icmp eq i32* %16, null
br i1 %17, label %32, label %18
18: ; preds = %12
%19 = call i32* @X509_NAME_ENTRY_get_data(i32* %16)
store i8* null, i8** %5
%20 = call i32 @ASN1_STRING_to_UTF8(i8** %5, i32* %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %29, label %22
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = call i64 @snprintf(i8* %14, i64 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = sub i64 %13, %24
%26 = getelementptr inbounds i8, i8* %14, i64 %24
%27 = load i8*, i8** %5
%28 = call i32 @OPENSSL_free(i8* %27)
br label %29
29: ; preds = %22, %18
%30 = phi i8* [ %26, %22 ], [ %14, %18 ]
%31 = phi i64 [ %25, %22 ], [ %13, %18 ]
br label %32
32: ; preds = %29, %12
%33 = phi i8* [ %30, %29 ], [ %14, %12 ]
%34 = phi i64 [ %31, %29 ], [ %13, %12 ]
%35 = call i32 @X509_NAME_get_index_by_NID(i32* %0, i32 %1, i32 %15)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %12
37: ; preds = %32, %7, %4
ret void
}