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

/* Type definitions */
struct TYPE_4__ {
  scalar_t__ length;
  int /*<<< orphan*/ data;
};
typedef int /*<<< orphan*/ EC_KEY;
typedef int /*<<< orphan*/ EC_GROUP;
typedef TYPE_1__ ASN1_STRING;

/* Variables and functions */
int /*<<< orphan*/ ASN1_STRING_free(TYPE_1__ *);
TYPE_1__ *ASN1_STRING_new();
int /*<<< orphan*/ EC_F_ECKEY_PARAM2TYPE;
scalar_t__ EC_GROUP_get_asn1_flag(int /*<<< orphan*/ const *);
int EC_GROUP_get_curve_name(int /*<<< orphan*/ const *);
int /*<<< orphan*/ *EC_KEY_get0_group(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_R_MISSING_PARAMETERS;
int /*<<< orphan*/ ECerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ERR_R_EC_LIB;
void *OBJ_nid2obj(int);
int V_ASN1_OBJECT;
int V_ASN1_SEQUENCE;
scalar_t__ i2d_ECParameters(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key) {
  const EC_GROUP *group;
  int nid;
  if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
    ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
    return 0;
  }
  if (EC_GROUP_get_asn1_flag(group) && (nid = EC_GROUP_get_curve_name(group))) {
    *ppval = OBJ_nid2obj(nid);
    *pptype = V_ASN1_OBJECT;
  } else {
    ASN1_STRING *pstr = NULL;
    pstr = ASN1_STRING_new();
    if (!pstr)
      return 0;
    pstr->length = i2d_ECParameters(ec_key, &pstr->data);
    if (pstr->length <= 0) {
      ASN1_STRING_free(pstr);
      ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
      return 0;
    }
    *ppval = pstr;
    *pptype = V_ASN1_SEQUENCE;
  }
  return 1;
}


x86 assembly (clang, -O0):
<eckey_param2type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b6 <eckey_param2type+0x36>
mov    -0x20(%rbp),%rdi
call   1080 <EC_KEY_get0_group@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d9 <eckey_param2type+0x59>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
call   10a0 <ECerr@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c7 <eckey_param2type+0x147>
mov    -0x28(%rbp),%rdi
call   1040 <EC_GROUP_get_asn1_flag@plt>
cmp    $0x0,%rax
je     1227 <eckey_param2type+0xa7>
mov    -0x28(%rbp),%rdi
call   1090 <EC_GROUP_get_curve_name@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     1227 <eckey_param2type+0xa7>
mov    -0x2c(%rbp),%edi
call   1050 <OBJ_nid2obj@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2dc6(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    12c0 <eckey_param2type+0x140>
movq   $0x0,-0x38(%rbp)
mov    $0x0,%al
call   1070 <ASN1_STRING_new@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1251 <eckey_param2type+0xd1>
movl   $0x0,-0x4(%rbp)
jmp    12c7 <eckey_param2type+0x147>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
add    $0x8,%rsi
call   1030 <i2d_ECParameters@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
cmpq   $0x0,(%rax)
jg     12a6 <eckey_param2type+0x126>
mov    -0x38(%rbp),%rdi
call   1060 <ASN1_STRING_free@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
call   10a0 <ECerr@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c7 <eckey_param2type+0x147>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2d08(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eckey_param2type>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     11d2 <eckey_param2type+0x52>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rdx,%rdi
call   1080 <EC_KEY_get0_group@plt>
test   %rax,%rax
je     11d2 <eckey_param2type+0x52>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <EC_GROUP_get_asn1_flag@plt>
test   %rax,%rax
je     11ed <eckey_param2type+0x6d>
mov    %rbp,%rdi
call   1090 <EC_GROUP_get_curve_name@plt>
test   %eax,%eax
je     11ed <eckey_param2type+0x6d>
mov    %eax,%edi
call   1050 <OBJ_nid2obj@plt>
mov    %rax,(%r15)
mov    0x2e10(%rip),%rax
jmp    1220 <eckey_param2type+0xa0>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   10a0 <ECerr@plt>
xor    %ebp,%ebp
jmp    122a <eckey_param2type+0xaa>
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <ASN1_STRING_new@plt>
test   %rax,%rax
je     122a <eckey_param2type+0xaa>
mov    %rax,%r12
mov    %rax,%rsi
add    $0x8,%rsi
mov    %rbx,%rdi
call   1030 <i2d_ECParameters@plt>
mov    %rax,(%r12)
test   %rax,%rax
jle    1235 <eckey_param2type+0xb5>
mov    %r12,(%r15)
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1060 <ASN1_STRING_free@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
call   10a0 <ECerr@plt>
jmp    122a <eckey_param2type+0xaa>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i64, i32 }
@EC_F_ECKEY_PARAM2TYPE = global i32 0
@EC_R_MISSING_PARAMETERS = global i32 0
@V_ASN1_OBJECT = global i32 0
@ERR_R_EC_LIB = global i32 0
@V_ASN1_SEQUENCE = global i32 0
define i32 @eckey_param2type(i32* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.TYPE_4__*
store i32* %0, i32** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
%11 = load i32*, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %3
%14 = load i32*, i32** %7
%15 = call i32* @EC_KEY_get0_group(i32* %14)
store i32* %15, i32** %8
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %13, %3
%18 = load i32, i32* @EC_F_ECKEY_PARAM2TYPE
%19 = load i32, i32* @EC_R_MISSING_PARAMETERS
%20 = call i32 @ECerr(i32 %18, i32 %19)
store i32 0, i32* %4
br label %64
21: ; preds = %13
%22 = load i32*, i32** %8
%23 = call i64 @EC_GROUP_get_asn1_flag(i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %21
%26 = load i32*, i32** %8
%27 = call i32 @EC_GROUP_get_curve_name(i32* %26)
store i32 %27, i32* %9
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i32, i32* %9
%31 = call i8* @OBJ_nid2obj(i32 %30)
%32 = load i8**, i8*** %6
store i8* %31, i8** %32
%33 = load i32, i32* @V_ASN1_OBJECT
%34 = load i32*, i32** %5
store i32 %33, i32* %34
br label %63
35: ; preds = %25, %21
store %struct.TYPE_4__* null, %struct.TYPE_4__** %10
%36 = call %struct.TYPE_4__* (...) @ASN1_STRING_new()
store %struct.TYPE_4__* %36, %struct.TYPE_4__** %10
%37 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%38 = icmp ne %struct.TYPE_4__* %37, null
br i1 %38, label %40, label %39
39: ; preds = %35
store i32 0, i32* %4
br label %64
40: ; preds = %35
%41 = load i32*, i32** %7
%42 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%43 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %42, i32 0, i32 1
%44 = call i64 @i2d_ECParameters(i32* %41, i32* %43)
%45 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%46 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %45, i32 0, i32 0
store i64 %44, i64* %46
%47 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%48 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %47, i32 0, i32 0
%49 = load i64, i64* %48
%50 = icmp sle i64 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %40
%52 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%53 = call i32 @ASN1_STRING_free(%struct.TYPE_4__* %52)
%54 = load i32, i32* @EC_F_ECKEY_PARAM2TYPE
%55 = load i32, i32* @ERR_R_EC_LIB
%56 = call i32 @ECerr(i32 %54, i32 %55)
store i32 0, i32* %4
br label %64
57: ; preds = %40
%58 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%59 = bitcast %struct.TYPE_4__* %58 to i8*
%60 = load i8**, i8*** %6
store i8* %59, i8** %60
%61 = load i32, i32* @V_ASN1_SEQUENCE
%62 = load i32*, i32** %5
store i32 %61, i32* %62
br label %63
63: ; preds = %57, %29
store i32 1, i32* %4
br label %64
64: ; preds = %63, %51, %39, %17
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i64, i32 }
@EC_F_ECKEY_PARAM2TYPE = global i32 0
@EC_R_MISSING_PARAMETERS = global i32 0
@V_ASN1_OBJECT = global i32 0
@ERR_R_EC_LIB = global i32 0
@V_ASN1_SEQUENCE = global i32 0
define i32 @eckey_param2type(i32* nocapture writeonly %0, i8** nocapture writeonly %1, i32* %2) {
%4 = icmp eq i32* %2, null
br i1 %4, label %8, label %5
5: ; preds = %3
%6 = tail call i32* @EC_KEY_get0_group(i32* %2)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %5, %3
%9 = load i32, i32* @EC_F_ECKEY_PARAM2TYPE
%10 = load i32, i32* @EC_R_MISSING_PARAMETERS
%11 = tail call i32 @ECerr(i32 %9, i32 %10)
br label %37
12: ; preds = %5
%13 = tail call i64 @EC_GROUP_get_asn1_flag(i32* %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %12
%16 = tail call i32 @EC_GROUP_get_curve_name(i32* %6)
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = tail call i8* @OBJ_nid2obj(i32 %16)
store i8* %19, i8** %1
%20 = load i32, i32* @V_ASN1_OBJECT
store i32 %20, i32* %0
br label %37
21: ; preds = %15, %12
%22 = tail call %struct.TYPE_4__* (...) @ASN1_STRING_new()
%23 = icmp eq %struct.TYPE_4__* %22, null
br i1 %23, label %37, label %24
24: ; preds = %21
%25 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %22, i64 0, i32 1
%26 = tail call i64 @i2d_ECParameters(i32* %2, i32* %25)
%27 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %22, i64 0, i32 0
store i64 %26, i64* %27
%28 = icmp slt i64 %26, 1
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = tail call i32 @ASN1_STRING_free(%struct.TYPE_4__* %22)
%31 = load i32, i32* @EC_F_ECKEY_PARAM2TYPE
%32 = load i32, i32* @ERR_R_EC_LIB
%33 = tail call i32 @ECerr(i32 %31, i32 %32)
br label %37
34: ; preds = %24
%35 = bitcast i8** %1 to %struct.TYPE_4__**
store %struct.TYPE_4__* %22, %struct.TYPE_4__** %35
%36 = load i32, i32* @V_ASN1_SEQUENCE
store i32 %36, i32* %0
br label %37
37: ; preds = %18, %34, %29, %21, %8
%38 = phi i32 [ 0, %8 ], [ 0, %21 ], [ 0, %29 ], [ 1, %34 ], [ 1, %18 ]
ret i32 %38
}