#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_R_EXTENSION_NAME_ERROR;
int /*<<< orphan*/ X509V3_R_EXTENSION_VALUE_ERROR;
int /*<<< orphan*/ X509V3error(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))) {
    X509V3error(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) {
    X509V3error(X509V3_R_EXTENSION_VALUE_ERROR);
    ERR_asprintf_error_data("value=%s", value);
    goto err;
  }
  if (!(oct = ASN1_OCTET_STRING_new())) {
    X509V3error(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    1220 <v3_generic_extension+0x80>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1080 <X509V3error@plt>
mov    -0x8(%rbp),%rsi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1030 <ERR_asprintf_error_data@plt>
jmp    130e <v3_generic_extension+0x16e>
cmpl   $0x1,-0x18(%rbp)
jne    1240 <v3_generic_extension+0xa0>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <string_to_hex@plt>
mov    %rax,-0x28(%rbp)
jmp    127f <v3_generic_extension+0xdf>
cmpl   $0x2,-0x18(%rbp)
jne    1264 <v3_generic_extension+0xc4>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10c0 <generic_asn1@plt>
mov    %rax,-0x28(%rbp)
jmp    127a <v3_generic_extension+0xda>
mov    -0x18(%rbp),%esi
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1030 <ERR_asprintf_error_data@plt>
jmp    130e <v3_generic_extension+0x16e>
jmp    127f <v3_generic_extension+0xdf>
cmpq   $0x0,-0x28(%rbp)
jne    12af <v3_generic_extension+0x10f>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   1080 <X509V3error@plt>
mov    -0x10(%rbp),%rsi
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1030 <ERR_asprintf_error_data@plt>
jmp    130e <v3_generic_extension+0x16e>
mov    $0x0,%al
call   1060 <ASN1_OCTET_STRING_new@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    12d7 <v3_generic_extension+0x137>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1080 <X509V3error@plt>
jmp    130e <v3_generic_extension+0x16e>
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   10a0 <X509_EXTENSION_create_by_OBJ@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   10b0 <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     11e7 <v3_generic_extension+0x47>
cmp    $0x2,%ebp
je     1204 <v3_generic_extension+0x64>
cmp    $0x1,%ebp
jne    1250 <v3_generic_extension+0xb0>
mov    %rsp,%rsi
mov    %r12,%rdi
call   1090 <string_to_hex@plt>
jmp    1212 <v3_generic_extension+0x72>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1080 <X509V3error@plt>
lea    0xe04(%rip),%rdi
xor    %r13d,%r13d
mov    %rbx,%rsi
jmp    1280 <v3_generic_extension+0xe0>
mov    %rsp,%rdx
mov    %r12,%rdi
mov    %r13,%rsi
call   10c0 <generic_asn1@plt>
mov    %rax,%r13
test   %rax,%rax
je     1265 <v3_generic_extension+0xc5>
xor    %eax,%eax
call   1060 <ASN1_OCTET_STRING_new@plt>
test   %rax,%rax
je     12b5 <v3_generic_extension+0x115>
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   10a0 <X509_EXTENSION_create_by_OBJ@plt>
mov    %rax,%rbx
jmp    128b <v3_generic_extension+0xeb>
lea    0xdb1(%rip),%rdi
xor    %r13d,%r13d
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <ERR_asprintf_error_data@plt>
jmp    1287 <v3_generic_extension+0xe7>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1080 <X509V3error@plt>
lea    0xdb3(%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   10b0 <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    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   1080 <X509V3error@plt>
jmp    1287 <v3_generic_extension+0xe7>

LLVM IR (-O0):
%struct.TYPE_5__ = type { i8*, i64 }
@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 %24, label %19
19: ; preds = %5
%20 = load i32, i32* @X509V3_R_EXTENSION_NAME_ERROR
%21 = call i32 @X509V3error(i32 %20)
%22 = load i8*, i8** %6
%23 = call i32 (i8*, ...) @ERR_asprintf_error_data(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %66
24: ; preds = %5
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %7
%29 = call i8* @string_to_hex(i8* %28, i64* %12)
store i8* %29, i8** %11
br label %41
30: ; preds = %24
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 2
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i8*, i8** %7
%35 = load i32*, i32** %10
%36 = call i8* @generic_asn1(i8* %34, i32* %35, i64* %12)
store i8* %36, i8** %11
br label %40
37: ; preds = %30
%38 = load i32, i32* %9
%39 = call i32 (i8*, ...) @ERR_asprintf_error_data(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %38)
br label %66
40: ; preds = %33
br label %41
41: ; preds = %40, %27
%42 = load i8*, i8** %11
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i32, i32* @X509V3_R_EXTENSION_VALUE_ERROR
%46 = call i32 @X509V3error(i32 %45)
%47 = load i8*, i8** %7
%48 = call i32 (i8*, ...) @ERR_asprintf_error_data(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %47)
br label %66
49: ; preds = %41
%50 = call %struct.TYPE_5__* (...) @ASN1_OCTET_STRING_new()
store %struct.TYPE_5__* %50, %struct.TYPE_5__** %14
%51 = icmp ne %struct.TYPE_5__* %50, null
br i1 %51, label %55, label %52
52: ; preds = %49
%53 = load i32, i32* @ERR_R_MALLOC_FAILURE
%54 = call i32 @X509V3error(i32 %53)
br label %66
55: ; preds = %49
%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, %52, %44, %37, %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_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 %14
10: ; preds = %5
%11 = load i32, i32* @X509V3_R_EXTENSION_NAME_ERROR
%12 = tail call i32 @X509V3error(i32 %11)
%13 = 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 %39
14: ; preds = %5
switch i32 %3, label %19 [
i32 1, label %15
i32 2, label %17
]
15: ; preds = %14
%16 = call i8* @string_to_hex(i8* %1, i64* %6)
br label %21
17: ; preds = %14
%18 = call i8* @generic_asn1(i8* %1, i32* %4, i64* %6)
br label %21
19: ; preds = %14
%20 = 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 %39
21: ; preds = %17, %15
%22 = phi i8* [ %16, %15 ], [ %18, %17 ]
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i32, i32* @X509V3_R_EXTENSION_VALUE_ERROR
%26 = call i32 @X509V3error(i32 %25)
%27 = 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 %39
28: ; preds = %21
%29 = call %struct.TYPE_5__* (...) @ASN1_OCTET_STRING_new()
%30 = icmp eq %struct.TYPE_5__* %29, null
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @ERR_R_MALLOC_FAILURE
%33 = call i32 @X509V3error(i32 %32)
br label %39
34: ; preds = %28
%35 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %29, i64 0, i32 0
store i8* %22, i8** %35
%36 = load i64, i64* %6
%37 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %29, i64 0, i32 1
store i64 %36, i64* %37
%38 = call i32* @X509_EXTENSION_create_by_OBJ(i32* null, i32* %8, i32 %2, %struct.TYPE_5__* %29)
br label %39
39: ; preds = %34, %31, %24, %19, %10
%40 = phi i8* [ null, %24 ], [ null, %34 ], [ %22, %31 ], [ null, %19 ], [ null, %10 ]
%41 = phi %struct.TYPE_5__* [ null, %24 ], [ %29, %34 ], [ null, %31 ], [ null, %19 ], [ null, %10 ]
%42 = phi i32* [ null, %24 ], [ %38, %34 ], [ null, %31 ], [ null, %19 ], [ null, %10 ]
%43 = call i32 @ASN1_OBJECT_free(i32* %8)
%44 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_5__* %41)
%45 = call i32 @free(i8* %40)
ret i32* %42
}