#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();
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*/ ECerror(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) {
    ECerror(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);
      ECerror(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   1090 <EC_KEY_get0_group@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d0 <eckey_param2type+0x50>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1040 <ECerror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <eckey_param2type+0x135>
mov    -0x28(%rbp),%rdi
call   1050 <EC_GROUP_get_asn1_flag@plt>
cmp    $0x0,%rax
je     121e <eckey_param2type+0x9e>
mov    -0x28(%rbp),%rdi
call   10a0 <EC_GROUP_get_curve_name@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     121e <eckey_param2type+0x9e>
mov    -0x2c(%rbp),%edi
call   1060 <OBJ_nid2obj@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2dd7(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    12ae <eckey_param2type+0x12e>
movq   $0x0,-0x38(%rbp)
mov    $0x0,%al
call   1080 <ASN1_STRING_new@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1248 <eckey_param2type+0xc8>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <eckey_param2type+0x135>
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     1294 <eckey_param2type+0x114>
mov    -0x38(%rbp),%rdi
call   1070 <ASN1_STRING_free@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1040 <ECerror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <eckey_param2type+0x135>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2d22(%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   1090 <EC_KEY_get0_group@plt>
test   %rax,%rax
je     11d2 <eckey_param2type+0x52>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <EC_GROUP_get_asn1_flag@plt>
test   %rax,%rax
je     11e4 <eckey_param2type+0x64>
mov    %rbp,%rdi
call   10a0 <EC_GROUP_get_curve_name@plt>
test   %eax,%eax
je     11e4 <eckey_param2type+0x64>
mov    %eax,%edi
call   1060 <OBJ_nid2obj@plt>
mov    %rax,(%r15)
mov    0x2e18(%rip),%rax
jmp    1217 <eckey_param2type+0x97>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1040 <ECerror@plt>
xor    %ebp,%ebp
jmp    1221 <eckey_param2type+0xa1>
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <ASN1_STRING_new@plt>
test   %rax,%rax
je     1221 <eckey_param2type+0xa1>
mov    %rax,%r12
mov    %rax,%rsi
add    $0x8,%rsi
mov    %rbx,%rdi
call   1030 <i2d_ECParameters@plt>
mov    %rax,(%r12)
test   %rax,%rax
jle    122c <eckey_param2type+0xac>
mov    %r12,(%r15)
mov    0x2db1(%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   1070 <ASN1_STRING_free@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1040 <ECerror@plt>
jmp    1221 <eckey_param2type+0xa1>

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

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