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

/* Variables and functions */
scalar_t__ ASN1_STRING_data(int /*<<< orphan*/ *);
scalar_t__ ASN1_STRING_length(int /*<<< orphan*/ *);
int /*<<< orphan*/ Log(int, char *);
int /*<<< orphan*/ NID_commonName;
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 /*<<< orphan*/, int);
int /*<<< orphan*/ X509_get_subject_name(int /*<<< orphan*/ *);
size_t strlen(char *);
char *get_common_name(const X509 *cert) {
  int common_name_loc;
  X509_NAME_ENTRY *common_name_entry = NULL;
  ASN1_STRING *common_name_asn1 = NULL;
  char *common_name_str = NULL;
  common_name_loc = X509_NAME_get_index_by_NID(
      X509_get_subject_name((X509 *)cert), NID_commonName, -1);
  if (common_name_loc < 0) {
    Log(4, "Error finding position of Common Name field in cert");
    return NULL;
  }
  common_name_entry =
      X509_NAME_get_entry(X509_get_subject_name((X509 *)cert), common_name_loc);
  if (common_name_entry == NULL) {
    Log(4, "Error extracting Common Name from cert");
    return NULL;
  }
  common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
  if (common_name_asn1 == NULL) {
    Log(4, "Error converting Common Name");
    return NULL;
  }
  common_name_str = (char *)ASN1_STRING_data(common_name_asn1);
  if ((size_t)ASN1_STRING_length(common_name_asn1) != strlen(common_name_str)) {
    Log(4, "Malformed Common Name in cert");
    return NULL;
  }
  return common_name_str;
}


x86 assembly (clang, -O0):
<get_common_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <X509_get_subject_name@plt>
mov    %eax,%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edx
call   10a0 <X509_NAME_get_index_by_NID@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ed <get_common_name+0x6d>
mov    $0x4,%edi
lea    0xe25(%rip),%rsi
call   1090 <Log@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bc <get_common_name+0x13c>
mov    -0x10(%rbp),%rdi
call   1070 <X509_get_subject_name@plt>
mov    %eax,%edi
mov    -0x14(%rbp),%esi
call   1080 <X509_NAME_get_entry@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122d <get_common_name+0xad>
mov    $0x4,%edi
lea    0xe19(%rip),%rsi
call   1090 <Log@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bc <get_common_name+0x13c>
mov    -0x20(%rbp),%rdi
call   1040 <X509_NAME_ENTRY_get_data@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1263 <get_common_name+0xe3>
mov    $0x4,%edi
lea    0xe0a(%rip),%rsi
call   1090 <Log@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bc <get_common_name+0x13c>
mov    -0x28(%rbp),%rdi
call   1060 <ASN1_STRING_data@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <ASN1_STRING_length@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
je     12b4 <get_common_name+0x134>
mov    $0x4,%edi
lea    0xdd6(%rip),%rsi
call   1090 <Log@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bc <get_common_name+0x13c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_common_name>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1070 <X509_get_subject_name@plt>
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
mov    $0xffffffff,%edx
call   10a0 <X509_NAME_get_index_by_NID@plt>
test   %eax,%eax
js     11f8 <get_common_name+0x78>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1070 <X509_get_subject_name@plt>
mov    %eax,%edi
mov    %ebp,%esi
call   1080 <X509_NAME_get_entry@plt>
test   %rax,%rax
je     1201 <get_common_name+0x81>
mov    %rax,%rdi
call   1040 <X509_NAME_ENTRY_get_data@plt>
test   %rax,%rax
je     120a <get_common_name+0x8a>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <ASN1_STRING_data@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <ASN1_STRING_length@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %rax,%rbp
je     121d <get_common_name+0x9d>
lea    0xe82(%rip),%rsi
jmp    1211 <get_common_name+0x91>
lea    0xe01(%rip),%rsi
jmp    1211 <get_common_name+0x91>
lea    0xe2c(%rip),%rsi
jmp    1211 <get_common_name+0x91>
lea    0xe4a(%rip),%rsi
mov    $0x4,%edi
call   1090 <Log@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@NID_commonName = global i32 0
@.str = [52 x i8] c"Error finding position of Common Name field in cert\00"
@.str.1 = [39 x i8] c"Error extracting Common Name from cert\00"
@.str.2 = [29 x i8] c"Error converting Common Name\00"
@.str.3 = [30 x i8] c"Malformed Common Name in cert\00"
define i8* @get_common_name(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
store i32* %0, i32** %3
store i32* null, i32** %5
store i32* null, i32** %6
store i8* null, i8** %7
%8 = load i32*, i32** %3
%9 = call i32 @X509_get_subject_name(i32* %8)
%10 = load i32, i32* @NID_commonName
%11 = call i32 @X509_NAME_get_index_by_NID(i32 %9, i32 %10, i32 -1)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @Log(i32 4, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %45
16: ; preds = %1
%17 = load i32*, i32** %3
%18 = call i32 @X509_get_subject_name(i32* %17)
%19 = load i32, i32* %4
%20 = call i32* @X509_NAME_get_entry(i32 %18, i32 %19)
store i32* %20, i32** %5
%21 = load i32*, i32** %5
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 @Log(i32 4, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %45
25: ; preds = %16
%26 = load i32*, i32** %5
%27 = call i32* @X509_NAME_ENTRY_get_data(i32* %26)
store i32* %27, i32** %6
%28 = load i32*, i32** %6
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @Log(i32 4, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %45
32: ; preds = %25
%33 = load i32*, i32** %6
%34 = call i64 @ASN1_STRING_data(i32* %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %7
%36 = load i32*, i32** %6
%37 = call i64 @ASN1_STRING_length(i32* %36)
%38 = load i8*, i8** %7
%39 = call i64 @strlen(i8* %38)
%40 = icmp ne i64 %37, %39
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = call i32 @Log(i32 4, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %2
br label %45
43: ; preds = %32
%44 = load i8*, i8** %7
store i8* %44, i8** %2
br label %45
45: ; preds = %43, %41, %30, %23, %14
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@NID_commonName = global i32 0
@.str = [52 x i8] c"Error finding position of Common Name field in cert\00"
@.str.1 = [39 x i8] c"Error extracting Common Name from cert\00"
@.str.2 = [29 x i8] c"Error converting Common Name\00"
@.str.3 = [30 x i8] c"Malformed Common Name in cert\00"
define i8* @get_common_name(i32* %0) {
%2 = tail call i32 @X509_get_subject_name(i32* %0)
%3 = load i32, i32* @NID_commonName
%4 = tail call i32 @X509_NAME_get_index_by_NID(i32 %2, i32 %3, i32 -1)
%5 = icmp slt i32 %4, 0
br i1 %5, label %19, label %6
6: ; preds = %1
%7 = tail call i32 @X509_get_subject_name(i32* %0)
%8 = tail call i32* @X509_NAME_get_entry(i32 %7, i32 %4)
%9 = icmp eq i32* %8, null
br i1 %9, label %19, label %10
10: ; preds = %6
%11 = tail call i32* @X509_NAME_ENTRY_get_data(i32* %8)
%12 = icmp eq i32* %11, null
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = tail call i64 @ASN1_STRING_data(i32* %11)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i64 @ASN1_STRING_length(i32* %11)
%17 = tail call i64 @strlen(i8* dereferenceable(1) %15)
%18 = icmp eq i64 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %13, %10, %6, %1
%20 = phi i8* [ getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), %6 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), %10 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), %13 ]
%21 = tail call i32 @Log(i32 4, i8* %20)
br label %22
22: ; preds = %19, %13
%23 = phi i8* [ %15, %13 ], [ null, %19 ]
ret i8* %23
}