#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_5__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ X509_EXTENSION;
typedef int /*<<< orphan*/ X509V3_CTX;
struct TYPE_5__ {
  unsigned char *data;
  long length;
};
typedef TYPE_1__ ASN1_OCTET_STRING;
typedef int /*<<< orphan*/ ASN1_OBJECT;

/* Variables and functions */
int /*<<< orphan*/ ASN1_OBJECT_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ ASN1_OCTET_STRING_free(TYPE_1__ *);
TYPE_1__ *ASN1_OCTET_STRING_new();
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ ERR_asprintf_error_data(char *, char const *);
int /*<<< orphan*/ *OBJ_txt2obj(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ X509V3_F_V3_GENERIC_EXTENSION;
int /*<<< orphan*/ X509V3_R_EXTENSION_NAME_ERROR;
int /*<<< orphan*/ X509V3_R_EXTENSION_VALUE_ERROR;
int /*<<< orphan*/ X509V3err(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *X509_EXTENSION_create_by_OBJ(int /*<<< orphan*/ *,
                                                 int /*<<< orphan*/ *, int,
                                                 TYPE_1__ *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *generic_asn1(char *, int /*<<< orphan*/ *, long *);
unsigned char *string_to_hex(char *, long *);
X509_EXTENSION *v3_generic_extension(const char *ext, char *value, int crit,
                                     int gen_type, X509V3_CTX *ctx) {
  unsigned char *ext_der = NULL;
  long ext_len;
  ASN1_OBJECT *obj = NULL;
  ASN1_OCTET_STRING *oct = NULL;
  X509_EXTENSION *extension = NULL;
  if (!(obj = OBJ_txt2obj(ext, 0))) {
    X509V3err(X509V3_F_V3_GENERIC_EXTENSION, X509V3_R_EXTENSION_NAME_ERROR);
    ERR_asprintf_error_data("name=%s", ext);
    goto err;
  }
  if (gen_type == 1)
    ext_der = string_to_hex(value, &ext_len);
  else if (gen_type == 2)
    ext_der = generic_asn1(value, ctx, &ext_len);
  if (ext_der == NULL) {
    X509V3err(X509V3_F_V3_GENERIC_EXTENSION, X509V3_R_EXTENSION_VALUE_ERROR);
    ERR_asprintf_error_data("value=%s", value);
    goto err;
  }
  if (!(oct = ASN1_OCTET_STRING_new())) {
    X509V3err(X509V3_F_V3_GENERIC_EXTENSION, ERR_R_MALLOC_FAILURE);
    goto err;
  }
  oct->data = ext_der;
  oct->length = ext_len;
  ext_der = NULL;
  extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
err:
  ASN1_OBJECT_free(obj);
  ASN1_OCTET_STRING_free(oct);
  free(ext_der);
  return extension;
}


x86 assembly (clang, -O0):
<v3_generic_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1050 <OBJ_txt2obj@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    121f <v3_generic_extension+0x7f>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
mov    -0x8(%rbp),%rsi
lea    0xdeb(%rip),%rdi
call   1030 <ERR_asprintf_error_data@plt>
jmp    1302 <v3_generic_extension+0x162>
cmpl   $0x1,-0x18(%rbp)
jne    123f <v3_generic_extension+0x9f>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <string_to_hex@plt>
mov    %rax,-0x28(%rbp)
jmp    1263 <v3_generic_extension+0xc3>
cmpl   $0x2,-0x18(%rbp)
jne    125e <v3_generic_extension+0xbe>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10b0 <generic_asn1@plt>
mov    %rax,-0x28(%rbp)
jmp    1263 <v3_generic_extension+0xc3>
cmpq   $0x0,-0x28(%rbp)
jne    129a <v3_generic_extension+0xfa>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
mov    -0x10(%rbp),%rsi
lea    0xd78(%rip),%rdi
call   1030 <ERR_asprintf_error_data@plt>
jmp    1302 <v3_generic_extension+0x162>
mov    $0x0,%al
call   1060 <ASN1_OCTET_STRING_new@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    12cb <v3_generic_extension+0x12b>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
jmp    1302 <v3_generic_extension+0x162>
mov    -0x28(%rbp),%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x8(%rax)
movq   $0x0,-0x28(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x40(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
call   1090 <X509_EXTENSION_create_by_OBJ@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   10a0 <ASN1_OBJECT_free@plt>
mov    -0x40(%rbp),%rdi
call   1070 <ASN1_OCTET_STRING_free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x48(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<v3_generic_extension>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,%r13
mov    %rdi,%rbx
xor    %esi,%esi
call   1050 <OBJ_txt2obj@plt>
mov    %rax,%r14
test   %rax,%rax
je     11df <v3_generic_extension+0x3f>
cmp    $0x2,%ebp
je     1202 <v3_generic_extension+0x62>
cmp    $0x1,%ebp
jne    124a <v3_generic_extension+0xaa>
mov    %rsp,%rsi
mov    %r13,%rdi
call   1080 <string_to_hex@plt>
jmp    1210 <v3_generic_extension+0x70>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
lea    0xe03(%rip),%rdi
mov    %rbx,%rsi
jmp    126b <v3_generic_extension+0xcb>
mov    %rsp,%rdx
mov    %r13,%rdi
mov    %r12,%rsi
call   10b0 <generic_asn1@plt>
mov    %rax,%r12
test   %rax,%rax
je     124a <v3_generic_extension+0xaa>
xor    %eax,%eax
call   1060 <ASN1_OCTET_STRING_new@plt>
test   %rax,%rax
je     12a1 <v3_generic_extension+0x101>
mov    %rax,%rbp
mov    %r12,(%rax)
mov    (%rsp),%rax
mov    %rax,0x8(%rbp)
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15d,%edx
mov    %rbp,%rcx
call   1090 <X509_EXTENSION_create_by_OBJ@plt>
mov    %rax,%rbx
jmp    1277 <v3_generic_extension+0xd7>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
lea    0xda0(%rip),%rdi
mov    %r13,%rsi
call   1030 <ERR_asprintf_error_data@plt>
xor    %r12d,%r12d
xor    %ebp,%ebp
xor    %ebx,%ebx
mov    %r14,%rdi
call   10a0 <ASN1_OBJECT_free@plt>
mov    %rbp,%rdi
call   1070 <ASN1_OCTET_STRING_free@plt>
mov    %r12,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
jmp    1273 <v3_generic_extension+0xd3>

LLVM IR (-O0):
%struct.TYPE_5__ = type { i8*, i64 }
@X509V3_F_V3_GENERIC_EXTENSION = global i32 0
@X509V3_R_EXTENSION_NAME_ERROR = global i32 0
@.str = [8 x i8] c"name=%s\00"
@X509V3_R_EXTENSION_VALUE_ERROR = global i32 0
@.str.1 = [9 x i8] c"value=%s\00"
@ERR_R_MALLOC_FAILURE = global i32 0
define i32* @v3_generic_extension(i8* %0, i8* %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca %struct.TYPE_5__*
%15 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32* %4, i32** %10
store i8* null, i8** %11
store i32* null, i32** %13
store %struct.TYPE_5__* null, %struct.TYPE_5__** %14
store i32* null, i32** %15
%16 = load i8*, i8** %6
%17 = call i32* @OBJ_txt2obj(i8* %16, i32 0)
store i32* %17, i32** %13
%18 = icmp ne i32* %17, null
br i1 %18, label %25, label %19
19: ; preds = %5
%20 = load i32, i32* @X509V3_F_V3_GENERIC_EXTENSION
%21 = load i32, i32* @X509V3_R_EXTENSION_NAME_ERROR
%22 = call i32 @X509V3err(i32 %20, i32 %21)
%23 = load i8*, i8** %6
%24 = call i32 @ERR_asprintf_error_data(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %66
25: ; preds = %5
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i8*, i8** %7
%30 = call i8* @string_to_hex(i8* %29, i64* %12)
store i8* %30, i8** %11
br label %39
31: ; preds = %25
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i8*, i8** %7
%36 = load i32*, i32** %10
%37 = call i8* @generic_asn1(i8* %35, i32* %36, i64* %12)
store i8* %37, i8** %11
br label %38
38: ; preds = %34, %31
br label %39
39: ; preds = %38, %28
%40 = load i8*, i8** %11
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = load i32, i32* @X509V3_F_V3_GENERIC_EXTENSION
%44 = load i32, i32* @X509V3_R_EXTENSION_VALUE_ERROR
%45 = call i32 @X509V3err(i32 %43, i32 %44)
%46 = load i8*, i8** %7
%47 = call i32 @ERR_asprintf_error_data(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %46)
br label %66
48: ; preds = %39
%49 = call %struct.TYPE_5__* (...) @ASN1_OCTET_STRING_new()
store %struct.TYPE_5__* %49, %struct.TYPE_5__** %14
%50 = icmp ne %struct.TYPE_5__* %49, null
br i1 %50, label %55, label %51
51: ; preds = %48
%52 = load i32, i32* @X509V3_F_V3_GENERIC_EXTENSION
%53 = load i32, i32* @ERR_R_MALLOC_FAILURE
%54 = call i32 @X509V3err(i32 %52, i32 %53)
br label %66
55: ; preds = %48
%56 = load i8*, i8** %11
%57 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%58 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %57, i32 0, i32 0
store i8* %56, i8** %58
%59 = load i64, i64* %12
%60 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%61 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %60, i32 0, i32 1
store i64 %59, i64* %61
store i8* null, i8** %11
%62 = load i32*, i32** %13
%63 = load i32, i32* %8
%64 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%65 = call i32* @X509_EXTENSION_create_by_OBJ(i32* null, i32* %62, i32 %63, %struct.TYPE_5__* %64)
store i32* %65, i32** %15
br label %66
66: ; preds = %55, %51, %42, %19
%67 = load i32*, i32** %13
%68 = call i32 @ASN1_OBJECT_free(i32* %67)
%69 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%70 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_5__* %69)
%71 = load i8*, i8** %11
%72 = call i32 @free(i8* %71)
%73 = load i32*, i32** %15
ret i32* %73
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i8*, i64 }
@X509V3_F_V3_GENERIC_EXTENSION = global i32 0
@X509V3_R_EXTENSION_NAME_ERROR = global i32 0
@.str = [8 x i8] c"name=%s\00"
@X509V3_R_EXTENSION_VALUE_ERROR = global i32 0
@.str.1 = [9 x i8] c"value=%s\00"
@ERR_R_MALLOC_FAILURE = global i32 0
define i32* @v3_generic_extension(i8* %0, i8* %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i64
%7 = bitcast i64* %6 to i8*
%8 = tail call i32* @OBJ_txt2obj(i8* %0, i32 0)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %15
10: ; preds = %5
%11 = load i32, i32* @X509V3_F_V3_GENERIC_EXTENSION
%12 = load i32, i32* @X509V3_R_EXTENSION_NAME_ERROR
%13 = tail call i32 @X509V3err(i32 %11, i32 %12)
%14 = tail call i32 @ERR_asprintf_error_data(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %40
15: ; preds = %5
switch i32 %3, label %23 [
i32 1, label %16
i32 2, label %18
]
16: ; preds = %15
%17 = call i8* @string_to_hex(i8* %1, i64* %6)
br label %20
18: ; preds = %15
%19 = call i8* @generic_asn1(i8* %1, i32* %4, i64* %6)
br label %20
20: ; preds = %18, %16
%21 = phi i8* [ %17, %16 ], [ %19, %18 ]
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %28
23: ; preds = %15, %20
%24 = load i32, i32* @X509V3_F_V3_GENERIC_EXTENSION
%25 = load i32, i32* @X509V3_R_EXTENSION_VALUE_ERROR
%26 = call i32 @X509V3err(i32 %24, i32 %25)
%27 = call i32 @ERR_asprintf_error_data(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %40
28: ; preds = %20
%29 = call %struct.TYPE_5__* (...) @ASN1_OCTET_STRING_new()
%30 = icmp eq %struct.TYPE_5__* %29, null
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* @X509V3_F_V3_GENERIC_EXTENSION
%33 = load i32, i32* @ERR_R_MALLOC_FAILURE
%34 = call i32 @X509V3err(i32 %32, i32 %33)
br label %40
35: ; preds = %28
%36 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %29, i64 0, i32 0
store i8* %21, i8** %36
%37 = load i64, i64* %6
%38 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %29, i64 0, i32 1
store i64 %37, i64* %38
%39 = call i32* @X509_EXTENSION_create_by_OBJ(i32* null, i32* %8, i32 %2, %struct.TYPE_5__* %29)
br label %40
40: ; preds = %35, %31, %23, %10
%41 = phi i8* [ null, %23 ], [ null, %35 ], [ %21, %31 ], [ null, %10 ]
%42 = phi %struct.TYPE_5__* [ null, %23 ], [ %29, %35 ], [ null, %31 ], [ null, %10 ]
%43 = phi i32* [ null, %23 ], [ %39, %35 ], [ null, %31 ], [ null, %10 ]
%44 = call i32 @ASN1_OBJECT_free(i32* %8)
%45 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_5__* %42)
%46 = call i32 @free(i8* %41)
ret i32* %43
}