#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_F_ECKEY_TYPE2PARAM;
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*/ ECerr(int /*<<< orphan*/, 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))) {
      ECerr(EC_F_ECKEY_TYPE2PARAM, 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) {
      ECerr(EC_F_ECKEY_TYPE2PARAM, 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 {
    ECerr(EC_F_ECKEY_TYPE2PARAM, 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    0x2dff(%rip),%rcx
cmp    (%rcx),%eax
jne    121d <eckey_type2param+0x8d>
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   1070 <d2i_ECParameters@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1218 <eckey_type2param+0x88>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   1080 <ECerr@plt>
jmp    12f6 <eckey_type2param+0x166>
jmp    12e9 <eckey_type2param+0x159>
mov    -0xc(%rbp),%eax
mov    0x2da9(%rip),%rcx
cmp    (%rcx),%eax
jne    12c8 <eckey_type2param+0x138>
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    1268 <eckey_type2param+0xd8>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
call   1080 <ECerr@plt>
jmp    12f6 <eckey_type2param+0x166>
mov    -0x40(%rbp),%rdi
call   1040 <OBJ_obj2nid@plt>
mov    %eax,%edi
call   1060 <EC_GROUP_new_by_curve_name@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    128c <eckey_type2param+0xfc>
jmp    12f6 <eckey_type2param+0x166>
mov    -0x48(%rbp),%rdi
mov    0x2d59(%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    12ba <eckey_type2param+0x12a>
jmp    12f6 <eckey_type2param+0x166>
mov    -0x48(%rbp),%rdi
call   1050 <EC_GROUP_free@plt>
jmp    12e4 <eckey_type2param+0x154>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
mov    0x2d00(%rip),%rax
mov    (%rax),%esi
call   1080 <ECerr@plt>
jmp    12f6 <eckey_type2param+0x166>
jmp    12e9 <eckey_type2param+0x159>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1312 <eckey_type2param+0x182>
cmpq   $0x0,-0x20(%rbp)
je     130a <eckey_type2param+0x17a>
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    0x2e12(%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   1070 <d2i_ECParameters@plt>
test   %rax,%rax
je     121c <eckey_type2param+0x8c>
mov    %rax,%r14
jmp    1236 <eckey_type2param+0xa6>
mov    0x2e09(%rip),%rax
cmp    %edi,(%rax)
jne    121c <eckey_type2param+0x8c>
xor    %eax,%eax
call   10a0 <EC_KEY_new@plt>
test   %rax,%rax
je     1241 <eckey_type2param+0xb1>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <OBJ_obj2nid@plt>
mov    %eax,%edi
call   1060 <EC_GROUP_new_by_curve_name@plt>
test   %rax,%rax
je     1253 <eckey_type2param+0xc3>
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     1253 <eckey_type2param+0xc3>
mov    %rbx,%rdi
call   1050 <EC_GROUP_free@plt>
jmp    1236 <eckey_type2param+0xa6>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
call   1080 <ECerr@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    0x2d77(%rip),%rax
jmp    122c <eckey_type2param+0x9c>
mov    %r14,%rdi
call   1090 <EC_KEY_free@plt>
jmp    1233 <eckey_type2param+0xa3>

LLVM IR (-O0):
%struct.TYPE_2__ = type { i8*, i32 }
@V_ASN1_SEQUENCE = global i32 0
@EC_F_ECKEY_TYPE2PARAM = 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 %32
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 %31, label %27
27: ; preds = %15
%28 = load i32, i32* @EC_F_ECKEY_TYPE2PARAM
%29 = load i32, i32* @EC_R_DECODE_ERROR
%30 = call i32 @ECerr(i32 %28, i32 %29)
br label %71
31: ; preds = %15
br label %69
32: ; preds = %2
%33 = load i32, i32* %4
%34 = load i32, i32* @V_ASN1_OBJECT
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %64
36: ; preds = %32
%37 = load i8*, i8** %5
%38 = bitcast i8* %37 to i32*
store i32* %38, i32** %10
%39 = call i32* (...) @EC_KEY_new()
store i32* %39, i32** %6
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i32, i32* @EC_F_ECKEY_TYPE2PARAM
%43 = load i32, i32* @ERR_R_MALLOC_FAILURE
%44 = call i32 @ECerr(i32 %42, i32 %43)
br label %71
45: ; preds = %36
%46 = load i32*, i32** %10
%47 = call i32 @OBJ_obj2nid(i32* %46)
%48 = call i32* @EC_GROUP_new_by_curve_name(i32 %47)
store i32* %48, i32** %11
%49 = load i32*, i32** %11
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %52
51: ; preds = %45
br label %71
52: ; preds = %45
%53 = load i32*, i32** %11
%54 = load i32, i32* @OPENSSL_EC_NAMED_CURVE
%55 = call i32 @EC_GROUP_set_asn1_flag(i32* %53, i32 %54)
%56 = load i32*, i32** %6
%57 = load i32*, i32** %11
%58 = call i64 @EC_KEY_set_group(i32* %56, i32* %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %52
br label %71
61: ; preds = %52
%62 = load i32*, i32** %11
%63 = call i32 @EC_GROUP_free(i32* %62)
br label %68
64: ; preds = %32
%65 = load i32, i32* @EC_F_ECKEY_TYPE2PARAM
%66 = load i32, i32* @EC_R_DECODE_ERROR
%67 = call i32 @ECerr(i32 %65, i32 %66)
br label %71
68: ; preds = %61
br label %69
69: ; preds = %68, %31
%70 = load i32*, i32** %6
store i32* %70, i32** %3
br label %78
71: ; preds = %64, %60, %51, %41, %27
%72 = load i32*, i32** %6
%73 = icmp ne i32* %72, null
br i1 %73, label %74, label %77
74: ; preds = %71
%75 = load i32*, i32** %6
%76 = call i32 @EC_KEY_free(i32* %75)
br label %77
77: ; preds = %74, %71
store i32* null, i32** %3
br label %78
78: ; preds = %77, %69
%79 = load i32*, i32** %3
ret i32* %79
}

LLVM IR (-O2):
@V_ASN1_SEQUENCE = global i32 0
@EC_F_ECKEY_TYPE2PARAM = 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 %20
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 %48
16: ; preds = %6
%17 = load i32, i32* @EC_F_ECKEY_TYPE2PARAM
%18 = load i32, i32* @EC_R_DECODE_ERROR
%19 = call i32 @ECerr(i32 %17, i32 %18)
br label %48
20: ; preds = %2
%21 = load i32, i32* @V_ASN1_OBJECT
%22 = icmp eq i32 %21, %0
br i1 %22, label %23, label %38
23: ; preds = %20
%24 = tail call i32* (...) @EC_KEY_new()
%25 = icmp eq i32* %24, null
br i1 %25, label %42, label %26
26: ; preds = %23
%27 = bitcast i8* %1 to i32*
%28 = tail call i32 @OBJ_obj2nid(i32* %27)
%29 = tail call i32* @EC_GROUP_new_by_curve_name(i32 %28)
%30 = icmp eq i32* %29, null
br i1 %30, label %46, label %31
31: ; preds = %26
%32 = load i32, i32* @OPENSSL_EC_NAMED_CURVE
%33 = tail call i32 @EC_GROUP_set_asn1_flag(i32* %29, i32 %32)
%34 = tail call i64 @EC_KEY_set_group(i32* %24, i32* %29)
%35 = icmp eq i64 %34, 0
br i1 %35, label %46, label %36
36: ; preds = %31
%37 = tail call i32 @EC_GROUP_free(i32* %29)
br label %48
38: ; preds = %20
%39 = load i32, i32* @EC_F_ECKEY_TYPE2PARAM
%40 = load i32, i32* @EC_R_DECODE_ERROR
%41 = tail call i32 @ECerr(i32 %39, i32 %40)
br label %48
42: ; preds = %23
%43 = load i32, i32* @EC_F_ECKEY_TYPE2PARAM
%44 = load i32, i32* @ERR_R_MALLOC_FAILURE
%45 = tail call i32 @ECerr(i32 %43, i32 %44)
br label %48
46: ; preds = %26, %31
%47 = tail call i32 @EC_KEY_free(i32* %24)
br label %48
48: ; preds = %38, %16, %42, %36, %15, %46
%49 = phi i32* [ %24, %36 ], [ null, %46 ], [ null, %42 ], [ %13, %15 ], [ null, %16 ], [ null, %38 ]
ret i32* %49
}