#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 *,
                              int);
int /*<<< orphan*/ *X509_get_subject_name(int /*<<< orphan*/ *);
char *conn_getcommonname(X509 *peercert) {
  static char cnbuf[256];
  X509_NAME *xname;
  int r;
  xname = X509_get_subject_name(peercert);
  if (!xname)
    return NULL;
  r = X509_NAME_get_text_by_NID(xname, NID_commonName, cnbuf, 256);
  cnbuf[255] = 0;
  if (r < 1)
    return NULL;
  return cnbuf;
}


x86 assembly (clang, -O0):
<conn_getcommonname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <X509_get_subject_name@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1151 <conn_getcommonname+0x31>
movq   $0x0,-0x8(%rbp)
jmp    119b <conn_getcommonname+0x7b>
mov    -0x18(%rbp),%rdi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
lea    0x2edb(%rip),%rdx
mov    $0x100,%ecx
call   1030 <X509_NAME_get_text_by_NID@plt>
mov    %eax,-0x1c(%rbp)
movb   $0x0,0x2fc6(%rip)
cmpl   $0x1,-0x1c(%rbp)
jge    1190 <conn_getcommonname+0x70>
movq   $0x0,-0x8(%rbp)
jmp    119b <conn_getcommonname+0x7b>
lea    0x2ea9(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conn_getcommonname>:
push   %rbx
call   1040 <X509_get_subject_name@plt>
test   %rax,%rax
je     115e <conn_getcommonname+0x3e>
mov    0x2eae(%rip),%rcx
mov    (%rcx),%esi
lea    0x2f05(%rip),%rbx
mov    %rax,%rdi
mov    %rbx,%rdx
mov    $0x100,%ecx
call   1030 <X509_NAME_get_text_by_NID@plt>
mov    %eax,%ecx
movb   $0x0,0x2feb(%rip)
xor    %eax,%eax
test   %ecx,%ecx
cmovg  %rbx,%rax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@conn_getcommonname.cnbuf = internal global [256 x i8] zeroinitializer
@NID_commonName = global i32 0
define i8* @conn_getcommonname(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = call i32* @X509_get_subject_name(i32* %6)
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %19
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = load i32, i32* @NID_commonName
%14 = call i32 @X509_NAME_get_text_by_NID(i32* %12, i32 %13, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @conn_getcommonname.cnbuf, i64 0, i64 0), i32 256)
store i32 %14, i32* %5
store i8 0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @conn_getcommonname.cnbuf, i64 0, i64 255)
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %11
store i8* null, i8** %2
br label %19
18: ; preds = %11
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @conn_getcommonname.cnbuf, i64 0, i64 0), i8** %2
br label %19
19: ; preds = %18, %17, %10
%20 = load i8*, i8** %2
ret i8* %20
}

LLVM IR (-O2):
@conn_getcommonname.cnbuf = internal global [256 x i8] zeroinitializer
@NID_commonName = global i32 0
define i8* @conn_getcommonname(i32* %0) {
%2 = tail call i32* @X509_get_subject_name(i32* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %9, label %4
4: ; preds = %1
%5 = load i32, i32* @NID_commonName
%6 = tail call i32 @X509_NAME_get_text_by_NID(i32* %2, i32 %5, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @conn_getcommonname.cnbuf, i64 0, i64 0), i32 256)
store i8 0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @conn_getcommonname.cnbuf, i64 0, i64 255)
%7 = icmp slt i32 %6, 1
%8 = select i1 %7, i8* null, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @conn_getcommonname.cnbuf, i64 0, i64 0)
br label %9
9: ; preds = %4, %1
%10 = phi i8* [ null, %1 ], [ %8, %4 ]
ret i8* %10
}