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

/* Variables and functions */
int /*<<< orphan*/ NID_commonName;
int X509_NAME_get_text_by_NID(int /*<<< orphan*/ *, int /*<<< orphan*/, char *,
                              size_t);
int /*<<< orphan*/ *X509_get_subject_name(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
char *ssl_x509_subject_cn(X509 *crt, size_t *psz) {
  X509_NAME *ptr;
  char *cn;
  size_t sz;
  ptr = X509_get_subject_name(crt);
  if (!ptr)
    return NULL;
  sz = X509_NAME_get_text_by_NID(ptr, NID_commonName, NULL, 0) + 1;
  if ((sz == 0) || !(cn = malloc(sz)))
    return NULL;
  if (X509_NAME_get_text_by_NID(ptr, NID_commonName, cn, sz) == -1) {
    free(cn);
    return NULL;
  }
  *psz = sz;
  return cn;
}


x86 assembly (clang, -O0):
<ssl_x509_subject_cn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <X509_get_subject_name@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1175 <ssl_x509_subject_cn+0x35>
movq   $0x0,-0x8(%rbp)
jmp    1212 <ssl_x509_subject_cn+0xd2>
mov    -0x20(%rbp),%rdi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1030 <X509_NAME_get_text_by_NID@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11b9 <ssl_x509_subject_cn+0x79>
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11c6 <ssl_x509_subject_cn+0x86>
movq   $0x0,-0x8(%rbp)
jmp    1212 <ssl_x509_subject_cn+0xd2>
mov    -0x20(%rbp),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1030 <X509_NAME_get_text_by_NID@plt>
cmp    $0xffffffff,%eax
jne    11ff <ssl_x509_subject_cn+0xbf>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1212 <ssl_x509_subject_cn+0xd2>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssl_x509_subject_cn>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
call   1050 <X509_get_subject_name@plt>
test   %rax,%rax
je     11ae <ssl_x509_subject_cn+0x6e>
mov    %rax,%rbx
mov    0x2e80(%rip),%r13
mov    0x0(%r13),%esi
xor    %r15d,%r15d
mov    %rax,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <X509_NAME_get_text_by_NID@plt>
inc    %eax
je     11b1 <ssl_x509_subject_cn+0x71>
movslq %eax,%r12
mov    %r12,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ae <ssl_x509_subject_cn+0x6e>
mov    %rax,%r15
mov    0x0(%r13),%esi
mov    %rbx,%rdi
mov    %rax,%rdx
mov    %r12,%rcx
call   1030 <X509_NAME_get_text_by_NID@plt>
cmp    $0xffffffff,%eax
je     11a6 <ssl_x509_subject_cn+0x66>
mov    %r12,(%r14)
jmp    11b1 <ssl_x509_subject_cn+0x71>
mov    %r15,%rdi
call   1040 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@NID_commonName = global i32 0
define i8* @ssl_x509_subject_cn(i32* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
store i32* %0, i32** %4
store i64* %1, i64** %5
%9 = load i32*, i32** %4
%10 = call i32* @X509_get_subject_name(i32* %9)
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i8* null, i8** %3
br label %41
14: ; preds = %2
%15 = load i32*, i32** %6
%16 = load i32, i32* @NID_commonName
%17 = call i32 @X509_NAME_get_text_by_NID(i32* %15, i32 %16, i8* null, i64 0)
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %14
%23 = load i64, i64* %8
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %7
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %22, %14
store i8* null, i8** %3
br label %41
27: ; preds = %22
%28 = load i32*, i32** %6
%29 = load i32, i32* @NID_commonName
%30 = load i8*, i8** %7
%31 = load i64, i64* %8
%32 = call i32 @X509_NAME_get_text_by_NID(i32* %28, i32 %29, i8* %30, i64 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i8*, i8** %7
%36 = call i32 @free(i8* %35)
store i8* null, i8** %3
br label %41
37: ; preds = %27
%38 = load i64, i64* %8
%39 = load i64*, i64** %5
store i64 %38, i64* %39
%40 = load i8*, i8** %7
store i8* %40, i8** %3
br label %41
41: ; preds = %37, %34, %26, %13
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
@NID_commonName = global i32 0
define i8* @ssl_x509_subject_cn(i32* %0, i64* nocapture writeonly %1) {
%3 = tail call i32* @X509_get_subject_name(i32* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %21, label %5
5: ; preds = %2
%6 = load i32, i32* @NID_commonName
%7 = tail call i32 @X509_NAME_get_text_by_NID(i32* %3, i32 %6, i8* null, i64 0)
%8 = add nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = icmp eq i32 %8, 0
br i1 %10, label %21, label %11
11: ; preds = %5
%12 = tail call i8* @malloc(i64 %9)
%13 = icmp eq i8* %12, null
br i1 %13, label %21, label %14
14: ; preds = %11
%15 = load i32, i32* @NID_commonName
%16 = tail call i32 @X509_NAME_get_text_by_NID(i32* %3, i32 %15, i8* %12, i64 %9)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @free(i8* %12)
br label %21
20: ; preds = %14
store i64 %9, i64* %1
br label %21
21: ; preds = %5, %11, %2, %20, %18
%22 = phi i8* [ null, %18 ], [ %12, %20 ], [ null, %2 ], [ null, %11 ], [ null, %5 ]
ret i8* %22
}