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

/* Type definitions */
struct TYPE_3__ {
  int length;
  unsigned char *data;
  int /*<<< orphan*/ type;
};
typedef int /*<<< orphan*/ EC_KEY;
typedef int /*<<< orphan*/ EC_GROUP;
typedef int /*<<< orphan*/ BIGNUM;
typedef TYPE_1__ ASN1_INTEGER;

/* Variables and functions */
int ASN1_object_size(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ BN_clear_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int BN_num_bits(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_GROUP_get_order(int /*<<< orphan*/ const *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_get0_group(int /*<<< orphan*/ const *);
int /*<<< orphan*/ V_ASN1_INTEGER;
int /*<<< orphan*/ V_ASN1_SEQUENCE;
int i2d_ASN1_INTEGER(TYPE_1__ *, int /*<<< orphan*/ *);
int ECDSA_size(const EC_KEY *r) {
  int ret, i;
  ASN1_INTEGER bs;
  BIGNUM *order = NULL;
  unsigned char buf[4];
  const EC_GROUP *group;
  if (r == NULL)
    return 0;
  group = EC_KEY_get0_group(r);
  if (group == NULL)
    return 0;
  if ((order = BN_new()) == NULL)
    return 0;
  if (!EC_GROUP_get_order(group, order, NULL)) {
    BN_clear_free(order);
    return 0;
  }
  i = BN_num_bits(order);
  bs.length = (i + 7) / 8;
  bs.data = buf;
  bs.type = V_ASN1_INTEGER;
  buf[0] = 0xff;
  i = i2d_ASN1_INTEGER(&bs, NULL);
  i += i;
  ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
  BN_clear_free(order);
  return (ret);
}


x86 assembly (clang, -O0):
<ECDSA_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119b <ECDSA_size+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    1285 <ECDSA_size+0x115>
mov    -0x10(%rbp),%rdi
call   1060 <EC_KEY_get0_group@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11bf <ECDSA_size+0x4f>
movl   $0x0,-0x4(%rbp)
jmp    1285 <ECDSA_size+0x115>
mov    $0x0,%al
call   1030 <BN_new@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11e0 <ECDSA_size+0x70>
movl   $0x0,-0x4(%rbp)
jmp    1285 <ECDSA_size+0x115>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <EC_GROUP_get_order@plt>
cmp    $0x0,%eax
jne    120f <ECDSA_size+0x9f>
mov    -0x38(%rbp),%rdi
call   1090 <BN_clear_free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1285 <ECDSA_size+0x115>
mov    -0x38(%rbp),%rdi
call   1040 <BN_num_bits@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x7,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x30(%rbp)
lea    -0x3c(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movb   $0xff,-0x3c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <i2d_ASN1_INTEGER@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%edi
call   1080 <ASN1_object_size@plt>
mov    %eax,-0x14(%rbp)
mov    -0x38(%rbp),%rdi
call   1090 <BN_clear_free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ECDSA_size>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
xor    %ebx,%ebx
test   %rdi,%rdi
je     121b <ECDSA_size+0xab>
call   1060 <EC_KEY_get0_group@plt>
test   %rax,%rax
je     121b <ECDSA_size+0xab>
mov    %rax,%r15
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <BN_new@plt>
test   %rax,%rax
je     121b <ECDSA_size+0xab>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %r15,%rdi
mov    %rax,%rsi
xor    %edx,%edx
call   1050 <EC_GROUP_get_order@plt>
test   %eax,%eax
je     1213 <ECDSA_size+0xa3>
mov    %r14,%rdi
call   1040 <BN_num_bits@plt>
lea    0x7(%rax),%ecx
add    $0xe,%eax
test   %ecx,%ecx
cmovns %ecx,%eax
sar    $0x3,%eax
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movb   $0xff,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1070 <i2d_ASN1_INTEGER@plt>
lea    (%rax,%rax,1),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%edi
call   1080 <ASN1_object_size@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1090 <BN_clear_free@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i8*, i32 }
@V_ASN1_INTEGER = global i32 0
@V_ASN1_SEQUENCE = global i32 0
define i32 @ECDSA_size(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.TYPE_3__
%7 = alloca i32*
%8 = alloca [4 x i8]
%9 = alloca i32*
store i32* %0, i32** %3
store i32* null, i32** %7
%10 = load i32*, i32** %3
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %2
br label %53
13: ; preds = %1
%14 = load i32*, i32** %3
%15 = call i32* @EC_KEY_get0_group(i32* %14)
store i32* %15, i32** %9
%16 = load i32*, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 0, i32* %2
br label %53
19: ; preds = %13
%20 = call i32* (...) @BN_new()
store i32* %20, i32** %7
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 0, i32* %2
br label %53
23: ; preds = %19
%24 = load i32*, i32** %9
%25 = load i32*, i32** %7
%26 = call i32 @EC_GROUP_get_order(i32* %24, i32* %25, i32* null)
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i32*, i32** %7
%30 = call i32 @BN_clear_free(i32* %29)
store i32 0, i32* %2
br label %53
31: ; preds = %23
%32 = load i32*, i32** %7
%33 = call i32 @BN_num_bits(i32* %32)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 7
%36 = sdiv i32 %35, 8
%37 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %6, i32 0, i32 0
store i32 %36, i32* %37
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%39 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %6, i32 0, i32 1
store i8* %38, i8** %39
%40 = load i32, i32* @V_ASN1_INTEGER
%41 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %6, i32 0, i32 2
store i32 %40, i32* %41
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
store i8 -1, i8* %42
%43 = call i32 @i2d_ASN1_INTEGER(%struct.TYPE_3__* %6, i32* null)
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = load i32, i32* %5
%46 = add nsw i32 %45, %44
store i32 %46, i32* %5
%47 = load i32, i32* %5
%48 = load i32, i32* @V_ASN1_SEQUENCE
%49 = call i32 @ASN1_object_size(i32 1, i32 %47, i32 %48)
store i32 %49, i32* %4
%50 = load i32*, i32** %7
%51 = call i32 @BN_clear_free(i32* %50)
%52 = load i32, i32* %4
store i32 %52, i32* %2
br label %53
53: ; preds = %31, %28, %22, %18, %12
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i8*, i32 }
@V_ASN1_INTEGER = global i32 0
@V_ASN1_SEQUENCE = global i32 0
define i32 @ECDSA_size(i32* %0) {
%2 = alloca %struct.TYPE_3__
%3 = alloca [4 x i8]
%4 = bitcast %struct.TYPE_3__* %2 to i8*
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%6 = icmp eq i32* %0, null
br i1 %6, label %31, label %7
7: ; preds = %1
%8 = tail call i32* @EC_KEY_get0_group(i32* %0)
%9 = icmp eq i32* %8, null
br i1 %9, label %31, label %10
10: ; preds = %7
%11 = tail call i32* (...) @BN_new()
%12 = icmp eq i32* %11, null
br i1 %12, label %31, label %13
13: ; preds = %10
%14 = tail call i32 @EC_GROUP_get_order(i32* %8, i32* %11, i32* null)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @BN_clear_free(i32* %11)
br label %31
18: ; preds = %13
%19 = tail call i32 @BN_num_bits(i32* %11)
%20 = add nsw i32 %19, 7
%21 = sdiv i32 %20, 8
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %2, i64 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %2, i64 0, i32 1
store i8* %5, i8** %23
%24 = load i32, i32* @V_ASN1_INTEGER
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %2, i64 0, i32 2
store i32 %24, i32* %25
store i8 -1, i8* %5
%26 = call i32 @i2d_ASN1_INTEGER(%struct.TYPE_3__* %2, i32* null)
%27 = shl nsw i32 %26, 1
%28 = load i32, i32* @V_ASN1_SEQUENCE
%29 = call i32 @ASN1_object_size(i32 1, i32 %27, i32 %28)
%30 = call i32 @BN_clear_free(i32* %11)
br label %31
31: ; preds = %10, %7, %1, %18, %16
%32 = phi i32 [ %29, %18 ], [ 0, %16 ], [ 0, %1 ], [ 0, %7 ], [ 0, %10 ]
ret i32 %32
}