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

/* Type definitions */
struct TYPE_2__ {
  unsigned char *data;
  int length;
};
typedef int /*<<< orphan*/ EC_KEY;
typedef int /*<<< orphan*/ EC_GROUP;
typedef TYPE_1__ ASN1_STRING;
typedef int /*<<< orphan*/ ASN1_OBJECT;

/* Variables and functions */
int /*<<< orphan*/ EC_GROUP_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_GROUP_new_by_curve_name(int /*<<< orphan*/);
int /*<<< orphan*/ EC_GROUP_set_asn1_flag(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/);
int /*<<< orphan*/ EC_KEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_new();
scalar_t__ EC_KEY_set_group(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_R_DECODE_ERROR;
int /*<<< orphan*/ ECerror(int /*<<< orphan*/);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ OBJ_obj2nid(int /*<<< orphan*/ *);
int /*<<< orphan*/ OPENSSL_EC_NAMED_CURVE;
int V_ASN1_OBJECT;
int V_ASN1_SEQUENCE;
int /*<<< orphan*/ *d2i_ECParameters(int /*<<< orphan*/ *,
                                     unsigned char const **, int);
EC_KEY *eckey_type2param(int ptype, void *pval) {
  EC_KEY *eckey = NULL;
  if (ptype == V_ASN1_SEQUENCE) {
    ASN1_STRING *pstr = pval;
    const unsigned char *pm = NULL;
    int pmlen;
    pm = pstr->data;
    pmlen = pstr->length;
    if (!(eckey = d2i_ECParameters(NULL, &pm, pmlen))) {
      ECerror(EC_R_DECODE_ERROR);
      goto ecerr;
    }
  } else if (ptype == V_ASN1_OBJECT) {
    ASN1_OBJECT *poid = pval;
    EC_GROUP *group;
    if ((eckey = EC_KEY_new()) == NULL) {
      ECerror(ERR_R_MALLOC_FAILURE);
      goto ecerr;
    }
    group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
    if (group == NULL)
      goto ecerr;
    EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
    if (EC_KEY_set_group(eckey, group) == 0)
      goto ecerr;
    EC_GROUP_free(group);
  } else {
    ECerror(EC_R_DECODE_ERROR);
    goto ecerr;
  }
  return eckey;
ecerr:
  if (eckey)
    EC_KEY_free(eckey);
  return NULL;
}


x86 assembly (clang, -O0):
<eckey_type2param>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%eax
jne    1214 <eckey_type2param+0x84>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rsi
call   1080 <d2i_ECParameters@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    120f <eckey_type2param+0x7f>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1060 <ECerror@plt>
jmp    12db <eckey_type2param+0x14b>
jmp    12ce <eckey_type2param+0x13e>
mov    -0xc(%rbp),%eax
mov    0x2dba(%rip),%rcx
cmp    (%rcx),%eax
jne    12b6 <eckey_type2param+0x126>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   10a0 <EC_KEY_new@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1256 <eckey_type2param+0xc6>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1060 <ECerror@plt>
jmp    12db <eckey_type2param+0x14b>
mov    -0x40(%rbp),%rdi
call   1040 <OBJ_obj2nid@plt>
mov    %eax,%edi
call   1070 <EC_GROUP_new_by_curve_name@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    127a <eckey_type2param+0xea>
jmp    12db <eckey_type2param+0x14b>
mov    -0x48(%rbp),%rdi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
call   1030 <EC_GROUP_set_asn1_flag@plt>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   10b0 <EC_KEY_set_group@plt>
cmp    $0x0,%rax
jne    12a8 <eckey_type2param+0x118>
jmp    12db <eckey_type2param+0x14b>
mov    -0x48(%rbp),%rdi
call   1050 <EC_GROUP_free@plt>
jmp    12c9 <eckey_type2param+0x139>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   1060 <ECerror@plt>
jmp    12db <eckey_type2param+0x14b>
jmp    12ce <eckey_type2param+0x13e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f7 <eckey_type2param+0x167>
cmpq   $0x0,-0x20(%rbp)
je     12ef <eckey_type2param+0x15f>
mov    -0x20(%rbp),%rdi
call   1090 <EC_KEY_free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eckey_type2param>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e1a(%rip),%rax
cmp    %edi,(%rax)
jne    11c0 <eckey_type2param+0x30>
mov    (%rbx),%rax
mov    %rax,(%rsp)
mov    0x8(%rbx),%edx
mov    %rsp,%rsi
xor    %edi,%edi
call   1080 <d2i_ECParameters@plt>
test   %rax,%rax
je     121c <eckey_type2param+0x8c>
mov    %rax,%r14
jmp    122d <eckey_type2param+0x9d>
mov    0x2e11(%rip),%rax
cmp    %edi,(%rax)
jne    121c <eckey_type2param+0x8c>
xor    %eax,%eax
call   10a0 <EC_KEY_new@plt>
test   %rax,%rax
je     1238 <eckey_type2param+0xa8>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <OBJ_obj2nid@plt>
mov    %eax,%edi
call   1070 <EC_GROUP_new_by_curve_name@plt>
test   %rax,%rax
je     1241 <eckey_type2param+0xb1>
mov    %rax,%rbx
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <EC_GROUP_set_asn1_flag@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10b0 <EC_KEY_set_group@plt>
test   %rax,%rax
je     1241 <eckey_type2param+0xb1>
mov    %rbx,%rdi
call   1050 <EC_GROUP_free@plt>
jmp    122d <eckey_type2param+0x9d>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1060 <ECerror@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d91(%rip),%rax
jmp    1223 <eckey_type2param+0x93>
mov    %r14,%rdi
call   1090 <EC_KEY_free@plt>
jmp    122a <eckey_type2param+0x9a>

LLVM IR (-O0):
%struct.TYPE_2__ = type { i8*, i32 }
@V_ASN1_SEQUENCE = global i32 0
@EC_R_DECODE_ERROR = global i32 0
@V_ASN1_OBJECT = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@OPENSSL_EC_NAMED_CURVE = global i32 0
define i32* @eckey_type2param(i32 %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.TYPE_2__*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* null, i32** %6
%12 = load i32, i32* %4
%13 = load i32, i32* @V_ASN1_SEQUENCE
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %31
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = bitcast i8* %16 to %struct.TYPE_2__*
store %struct.TYPE_2__* %17, %struct.TYPE_2__** %7
store i8* null, i8** %8
%18 = load %struct.TYPE_2__*, %struct.TYPE_2__** %7
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
%20 = load i8*, i8** %19
store i8* %20, i8** %8
%21 = load %struct.TYPE_2__*, %struct.TYPE_2__** %7
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 1
%23 = load i32, i32* %22
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = call i32* @d2i_ECParameters(i32* null, i8** %8, i32 %24)
store i32* %25, i32** %6
%26 = icmp ne i32* %25, null
br i1 %26, label %30, label %27
27: ; preds = %15
%28 = load i32, i32* @EC_R_DECODE_ERROR
%29 = call i32 @ECerror(i32 %28)
br label %68
30: ; preds = %15
br label %66
31: ; preds = %2
%32 = load i32, i32* %4
%33 = load i32, i32* @V_ASN1_OBJECT
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %62
35: ; preds = %31
%36 = load i8*, i8** %5
%37 = bitcast i8* %36 to i32*
store i32* %37, i32** %10
%38 = call i32* (...) @EC_KEY_new()
store i32* %38, i32** %6
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* @ERR_R_MALLOC_FAILURE
%42 = call i32 @ECerror(i32 %41)
br label %68
43: ; preds = %35
%44 = load i32*, i32** %10
%45 = call i32 @OBJ_obj2nid(i32* %44)
%46 = call i32* @EC_GROUP_new_by_curve_name(i32 %45)
store i32* %46, i32** %11
%47 = load i32*, i32** %11
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %50
49: ; preds = %43
br label %68
50: ; preds = %43
%51 = load i32*, i32** %11
%52 = load i32, i32* @OPENSSL_EC_NAMED_CURVE
%53 = call i32 @EC_GROUP_set_asn1_flag(i32* %51, i32 %52)
%54 = load i32*, i32** %6
%55 = load i32*, i32** %11
%56 = call i64 @EC_KEY_set_group(i32* %54, i32* %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %50
br label %68
59: ; preds = %50
%60 = load i32*, i32** %11
%61 = call i32 @EC_GROUP_free(i32* %60)
br label %65
62: ; preds = %31
%63 = load i32, i32* @EC_R_DECODE_ERROR
%64 = call i32 @ECerror(i32 %63)
br label %68
65: ; preds = %59
br label %66
66: ; preds = %65, %30
%67 = load i32*, i32** %6
store i32* %67, i32** %3
br label %75
68: ; preds = %62, %58, %49, %40, %27
%69 = load i32*, i32** %6
%70 = icmp ne i32* %69, null
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i32*, i32** %6
%73 = call i32 @EC_KEY_free(i32* %72)
br label %74
74: ; preds = %71, %68
store i32* null, i32** %3
br label %75
75: ; preds = %74, %66
%76 = load i32*, i32** %3
ret i32* %76
}

LLVM IR (-O2):
@V_ASN1_SEQUENCE = global i32 0
@EC_R_DECODE_ERROR = global i32 0
@V_ASN1_OBJECT = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@OPENSSL_EC_NAMED_CURVE = global i32 0
define i32* @eckey_type2param(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = load i32, i32* @V_ASN1_SEQUENCE
%5 = icmp eq i32 %4, %0
br i1 %5, label %6, label %19
6: ; preds = %2
%7 = bitcast i8** %3 to i8*
%8 = bitcast i8* %1 to i8**
%9 = load i8*, i8** %8
store i8* %9, i8** %3
%10 = getelementptr inbounds i8, i8* %1, i64 8
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
%13 = call i32* @d2i_ECParameters(i32* null, i8** %3, i32 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %6
br label %45
16: ; preds = %6
%17 = load i32, i32* @EC_R_DECODE_ERROR
%18 = call i32 @ECerror(i32 %17)
br label %45
19: ; preds = %2
%20 = load i32, i32* @V_ASN1_OBJECT
%21 = icmp eq i32 %20, %0
br i1 %21, label %22, label %37
22: ; preds = %19
%23 = tail call i32* (...) @EC_KEY_new()
%24 = icmp eq i32* %23, null
br i1 %24, label %40, label %25
25: ; preds = %22
%26 = bitcast i8* %1 to i32*
%27 = tail call i32 @OBJ_obj2nid(i32* %26)
%28 = tail call i32* @EC_GROUP_new_by_curve_name(i32 %27)
%29 = icmp eq i32* %28, null
br i1 %29, label %43, label %30
30: ; preds = %25
%31 = load i32, i32* @OPENSSL_EC_NAMED_CURVE
%32 = tail call i32 @EC_GROUP_set_asn1_flag(i32* %28, i32 %31)
%33 = tail call i64 @EC_KEY_set_group(i32* %23, i32* %28)
%34 = icmp eq i64 %33, 0
br i1 %34, label %43, label %35
35: ; preds = %30
%36 = tail call i32 @EC_GROUP_free(i32* %28)
br label %45
37: ; preds = %19
%38 = load i32, i32* @EC_R_DECODE_ERROR
%39 = tail call i32 @ECerror(i32 %38)
br label %45
40: ; preds = %22
%41 = load i32, i32* @ERR_R_MALLOC_FAILURE
%42 = tail call i32 @ECerror(i32 %41)
br label %45
43: ; preds = %25, %30
%44 = tail call i32 @EC_KEY_free(i32* %23)
br label %45
45: ; preds = %37, %16, %40, %35, %15, %43
%46 = phi i32* [ %23, %35 ], [ null, %43 ], [ null, %40 ], [ %13, %15 ], [ null, %16 ], [ null, %37 ]
ret i32* %46
}