#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ X509_NAME;
struct TYPE_3__ {
  int length;
  int /*<<< orphan*/ data;
};
typedef TYPE_1__ ASN1_STRING;
typedef int /*<<< orphan*/ ASN1_OBJECT;

/* Variables and functions */
TYPE_1__ *X509_NAME_ENTRY_get_data(int /*<<< orphan*/);
int /*<<< orphan*/ X509_NAME_get_entry(int /*<<< orphan*/ *, int);
int X509_NAME_get_index_by_OBJ(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                               int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int X509_NAME_get_text_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj,
                              char *buf, int len) {
  int i;
  ASN1_STRING *data;
  i = X509_NAME_get_index_by_OBJ(name, obj, -1);
  if (i < 0)
    return (-1);
  data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
  i = (data->length > (len - 1)) ? (len - 1) : data->length;
  if (buf == NULL)
    return (data->length);
  if (i >= 0) {
    memcpy(buf, data->data, i);
    buf[i] = '\0';
  }
  return (i);
}


x86 assembly (clang, -O0):
<X509_NAME_get_text_by_OBJ>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0xffffffff,%edx
call   1060 <X509_NAME_get_index_by_OBJ@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1182 <X509_NAME_get_text_by_OBJ+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <X509_NAME_get_text_by_OBJ+0xd2>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1050 <X509_NAME_get_entry@plt>
mov    %eax,%edi
call   1030 <X509_NAME_ENTRY_get_data@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11bb <X509_NAME_get_text_by_OBJ+0x7b>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11c4 <X509_NAME_get_text_by_OBJ+0x84>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e3 <X509_NAME_get_text_by_OBJ+0xa3>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1212 <X509_NAME_get_text_by_OBJ+0xd2>
cmpl   $0x0,-0x28(%rbp)
jl     120c <X509_NAME_get_text_by_OBJ+0xcc>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<X509_NAME_get_text_by_OBJ>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rdi,%rbx
mov    $0xffffffff,%ebp
mov    $0xffffffff,%edx
call   1060 <X509_NAME_get_index_by_OBJ@plt>
test   %eax,%eax
js     119f <X509_NAME_get_text_by_OBJ+0x5f>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <X509_NAME_get_entry@plt>
mov    %eax,%edi
call   1030 <X509_NAME_ENTRY_get_data@plt>
mov    (%rax),%ebp
lea    -0x1(%r15),%ebx
cmp    %r15d,%ebp
cmovl  %ebp,%ebx
test   %r14,%r14
je     119f <X509_NAME_get_text_by_OBJ+0x5f>
test   %ebx,%ebx
js     119d <X509_NAME_get_text_by_OBJ+0x5d>
mov    0x4(%rax),%esi
mov    %r14,%rdi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
define i32 @X509_NAME_get_text_by_OBJ(i32* %0, i32* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.TYPE_3__*
store i32* %0, i32** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i32*, i32** %6
%13 = load i32*, i32** %7
%14 = call i32 @X509_NAME_get_index_by_OBJ(i32* %12, i32* %13, i32 -1)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %60
18: ; preds = %4
%19 = load i32*, i32** %6
%20 = load i32, i32* %10
%21 = call i32 @X509_NAME_get_entry(i32* %19, i32 %20)
%22 = call %struct.TYPE_3__* @X509_NAME_ENTRY_get_data(i32 %21)
store %struct.TYPE_3__* %22, %struct.TYPE_3__** %11
%23 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* %9
%27 = sub nsw i32 %26, 1
%28 = icmp sgt i32 %25, %27
br i1 %28, label %29, label %32
29: ; preds = %18
%30 = load i32, i32* %9
%31 = sub nsw i32 %30, 1
br label %36
32: ; preds = %18
%33 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %33, i32 0, i32 0
%35 = load i32, i32* %34
br label %36
36: ; preds = %32, %29
%37 = phi i32 [ %31, %29 ], [ %35, %32 ]
store i32 %37, i32* %10
%38 = load i8*, i8** %8
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%42 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %41, i32 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %5
br label %60
44: ; preds = %36
%45 = load i32, i32* %10
%46 = icmp sge i32 %45, 0
br i1 %46, label %47, label %58
47: ; preds = %44
%48 = load i8*, i8** %8
%49 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%50 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load i32, i32* %10
%53 = call i32 @memcpy(i8* %48, i32 %51, i32 %52)
%54 = load i8*, i8** %8
%55 = load i32, i32* %10
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
store i8 0, i8* %57
br label %58
58: ; preds = %47, %44
%59 = load i32, i32* %10
store i32 %59, i32* %5
br label %60
60: ; preds = %58, %40, %17
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
define i32 @X509_NAME_get_text_by_OBJ(i32* %0, i32* %1, i8* %2, i32 %3) {
%5 = tail call i32 @X509_NAME_get_index_by_OBJ(i32* %0, i32* %1, i32 -1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %4
%8 = tail call i32 @X509_NAME_get_entry(i32* %0, i32 %5)
%9 = tail call %struct.TYPE_3__* @X509_NAME_ENTRY_get_data(i32 %8)
%10 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %9, i64 0, i32 0
%11 = load i32, i32* %10
%12 = icmp slt i32 %11, %3
%13 = add nsw i32 %3, -1
%14 = select i1 %12, i32 %11, i32 %13
%15 = icmp eq i8* %2, null
br i1 %15, label %24, label %16
16: ; preds = %7
%17 = icmp sgt i32 %14, -1
br i1 %17, label %18, label %24
18: ; preds = %16
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %9, i64 0, i32 1
%20 = load i32, i32* %19
%21 = tail call i32 @memcpy(i8* %2, i32 %20, i32 %14)
%22 = zext i32 %14 to i64
%23 = getelementptr inbounds i8, i8* %2, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %7, %16, %18, %4
%25 = phi i32 [ -1, %4 ], [ %14, %18 ], [ %14, %16 ], [ %11, %7 ]
ret i32 %25
}