#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 *, ...);
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 = 0;
  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);
  else {
    ERR_asprintf_error_data("Unexpected generic extension type %d", gen_type);
    goto err;
  }
  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,-0x30(%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    1229 <v3_generic_extension+0x89>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
mov    -0x8(%rbp),%rsi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1030 <ERR_asprintf_error_data@plt>
jmp    1329 <v3_generic_extension+0x189>
cmpl   $0x1,-0x18(%rbp)
jne    1249 <v3_generic_extension+0xa9>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <string_to_hex@plt>
mov    %rax,-0x28(%rbp)
jmp    1288 <v3_generic_extension+0xe8>
cmpl   $0x2,-0x18(%rbp)
jne    126d <v3_generic_extension+0xcd>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10b0 <generic_asn1@plt>
mov    %rax,-0x28(%rbp)
jmp    1283 <v3_generic_extension+0xe3>
mov    -0x18(%rbp),%esi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1030 <ERR_asprintf_error_data@plt>
jmp    1329 <v3_generic_extension+0x189>
jmp    1288 <v3_generic_extension+0xe8>
cmpq   $0x0,-0x28(%rbp)
jne    12c1 <v3_generic_extension+0x121>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
mov    -0x10(%rbp),%rsi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1030 <ERR_asprintf_error_data@plt>
jmp    1329 <v3_generic_extension+0x189>
mov    $0x0,%al
call   1060 <ASN1_OCTET_STRING_new@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    12f2 <v3_generic_extension+0x152>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
jmp    1329 <v3_generic_extension+0x189>
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,%r13
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%rbx
movq   $0x0,(%rsp)
xor    %esi,%esi
call   1050 <OBJ_txt2obj@plt>
mov    %rax,%r14
test   %rax,%rax
je     11eb <v3_generic_extension+0x4b>
cmp    $0x2,%ebp
je     1214 <v3_generic_extension+0x74>
cmp    $0x1,%ebp
jne    1260 <v3_generic_extension+0xc0>
mov    %rsp,%rsi
mov    %r12,%rdi
call   1080 <string_to_hex@plt>
jmp    1222 <v3_generic_extension+0x82>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
lea    0xdf7(%rip),%rdi
xor    %r13d,%r13d
mov    %rbx,%rsi
jmp    1299 <v3_generic_extension+0xf9>
mov    %rsp,%rdx
mov    %r12,%rdi
mov    %r13,%rsi
call   10b0 <generic_asn1@plt>
mov    %rax,%r13
test   %rax,%rax
je     1275 <v3_generic_extension+0xd5>
xor    %eax,%eax
call   1060 <ASN1_OCTET_STRING_new@plt>
test   %rax,%rax
je     12ce <v3_generic_extension+0x12e>
mov    %rax,%rbp
mov    %r13,(%rax)
mov    (%rsp),%rax
mov    %rax,0x8(%rbp)
xor    %r13d,%r13d
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15d,%edx
mov    %rbp,%rcx
call   1090 <X509_EXTENSION_create_by_OBJ@plt>
mov    %rax,%rbx
jmp    12a4 <v3_generic_extension+0x104>
lea    0xda1(%rip),%rdi
xor    %r13d,%r13d
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <ERR_asprintf_error_data@plt>
jmp    12a0 <v3_generic_extension+0x100>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
lea    0xd9a(%rip),%rdi
xor    %r13d,%r13d
mov    %r12,%rsi
xor    %eax,%eax
call   1030 <ERR_asprintf_error_data@plt>
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    %r13,%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    0x2cfb(%rip),%rax
mov    (%rax),%edi
mov    0x2d0a(%rip),%rax
mov    (%rax),%esi
call   10c0 <X509V3err@plt>
jmp    12a0 <v3_generic_extension+0x100>

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"
@.str.1 = [37 x i8] c"Unexpected generic extension type %d\00"
@X509V3_R_EXTENSION_VALUE_ERROR = global i32 0
@.str.2 = [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 i64 0, i64* %12
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 (i8*, ...) @ERR_asprintf_error_data(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %69
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 %42
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 %41
38: ; preds = %31
%39 = load i32, i32* %9
%40 = call i32 (i8*, ...) @ERR_asprintf_error_data(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %39)
br label %69
41: ; preds = %34
br label %42
42: ; preds = %41, %28
%43 = load i8*, i8** %11
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %51
45: ; preds = %42
%46 = load i32, i32* @X509V3_F_V3_GENERIC_EXTENSION
%47 = load i32, i32* @X509V3_R_EXTENSION_VALUE_ERROR
%48 = call i32 @X509V3err(i32 %46, i32 %47)
%49 = load i8*, i8** %7
%50 = call i32 (i8*, ...) @ERR_asprintf_error_data(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %49)
br label %69
51: ; preds = %42
%52 = call %struct.TYPE_5__* (...) @ASN1_OCTET_STRING_new()
store %struct.TYPE_5__* %52, %struct.TYPE_5__** %14
%53 = icmp ne %struct.TYPE_5__* %52, null
br i1 %53, label %58, label %54
54: ; preds = %51
%55 = load i32, i32* @X509V3_F_V3_GENERIC_EXTENSION
%56 = load i32, i32* @ERR_R_MALLOC_FAILURE
%57 = call i32 @X509V3err(i32 %55, i32 %56)
br label %69
58: ; preds = %51
%59 = load i8*, i8** %11
%60 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%61 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %60, i32 0, i32 0
store i8* %59, i8** %61
%62 = load i64, i64* %12
%63 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%64 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %63, i32 0, i32 1
store i64 %62, i64* %64
store i8* null, i8** %11
%65 = load i32*, i32** %13
%66 = load i32, i32* %8
%67 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%68 = call i32* @X509_EXTENSION_create_by_OBJ(i32* null, i32* %65, i32 %66, %struct.TYPE_5__* %67)
store i32* %68, i32** %15
br label %69
69: ; preds = %58, %54, %45, %38, %19
%70 = load i32*, i32** %13
%71 = call i32 @ASN1_OBJECT_free(i32* %70)
%72 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%73 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_5__* %72)
%74 = load i8*, i8** %11
%75 = call i32 @free(i8* %74)
%76 = load i32*, i32** %15
ret i32* %76
}

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"
@.str.1 = [37 x i8] c"Unexpected generic extension type %d\00"
@X509V3_R_EXTENSION_VALUE_ERROR = global i32 0
@.str.2 = [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*
store i64 0, i64* %6
%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 (i8*, ...) @ERR_asprintf_error_data(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %42
15: ; preds = %5
switch i32 %3, label %20 [
i32 1, label %16
i32 2, label %18
]
16: ; preds = %15
%17 = call i8* @string_to_hex(i8* %1, i64* %6)
br label %22
18: ; preds = %15
%19 = call i8* @generic_asn1(i8* %1, i32* %4, i64* %6)
br label %22
20: ; preds = %15
%21 = tail call i32 (i8*, ...) @ERR_asprintf_error_data(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %3)
br label %42
22: ; preds = %18, %16
%23 = phi i8* [ %17, %16 ], [ %19, %18 ]
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* @X509V3_F_V3_GENERIC_EXTENSION
%27 = load i32, i32* @X509V3_R_EXTENSION_VALUE_ERROR
%28 = call i32 @X509V3err(i32 %26, i32 %27)
%29 = call i32 (i8*, ...) @ERR_asprintf_error_data(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %42
30: ; preds = %22
%31 = call %struct.TYPE_5__* (...) @ASN1_OCTET_STRING_new()
%32 = icmp eq %struct.TYPE_5__* %31, null
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i32, i32* @X509V3_F_V3_GENERIC_EXTENSION
%35 = load i32, i32* @ERR_R_MALLOC_FAILURE
%36 = call i32 @X509V3err(i32 %34, i32 %35)
br label %42
37: ; preds = %30
%38 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %31, i64 0, i32 0
store i8* %23, i8** %38
%39 = load i64, i64* %6
%40 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %31, i64 0, i32 1
store i64 %39, i64* %40
%41 = call i32* @X509_EXTENSION_create_by_OBJ(i32* null, i32* %8, i32 %2, %struct.TYPE_5__* %31)
br label %42
42: ; preds = %37, %33, %25, %20, %10
%43 = phi i8* [ null, %25 ], [ null, %37 ], [ %23, %33 ], [ null, %20 ], [ null, %10 ]
%44 = phi %struct.TYPE_5__* [ null, %25 ], [ %31, %37 ], [ null, %33 ], [ null, %20 ], [ null, %10 ]
%45 = phi i32* [ null, %25 ], [ %41, %37 ], [ null, %33 ], [ null, %20 ], [ null, %10 ]
%46 = call i32 @ASN1_OBJECT_free(i32* %8)
%47 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_5__* %44)
%48 = call i32 @free(i8* %43)
ret i32* %45
}