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

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

/* Variables and functions */
int /*<<< orphan*/ ASN1_INTEGER_free(TYPE_1__ *);
TYPE_1__ *ASN1_INTEGER_new();
int /*<<< orphan*/ ASN1_INTEGER_valid(TYPE_1__ *);
int /*<<< orphan*/ ASN1error(int /*<<< orphan*/);
int BN_bn2bin(int /*<<< orphan*/ const *, unsigned char *);
scalar_t__ BN_is_negative(int /*<<< orphan*/ const *);
int BN_num_bits(int /*<<< orphan*/ const *);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ ERR_R_NESTED_ASN1_ERROR;
int /*<<< orphan*/ V_ASN1_INTEGER;
int /*<<< orphan*/ V_ASN1_NEG_INTEGER;
unsigned char *realloc(unsigned char *, int);
ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) {
  ASN1_INTEGER *ret;
  int len, j;
  if (ai == NULL)
    ret = ASN1_INTEGER_new();
  else
    ret = ai;
  if (ret == NULL) {
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
    goto err;
  }
  if (!ASN1_INTEGER_valid(ret))
    goto err;
  if (BN_is_negative(bn))
    ret->type = V_ASN1_NEG_INTEGER;
  else
    ret->type = V_ASN1_INTEGER;
  j = BN_num_bits(bn);
  len = ((j == 0) ? 0 : ((j / 8) + 1));
  if (ret->length < len + 4) {
    unsigned char *new_data = realloc(ret->data, len + 4);
    if (!new_data) {
      ASN1error(ERR_R_MALLOC_FAILURE);
      goto err;
    }
    ret->data = new_data;
  }
  ret->length = BN_bn2bin(bn, ret->data);
  if (!ret->length) {
    ret->data[0] = 0;
    ret->length = 1;
  }
  return (ret);
err:
  if (ret != ai)
    ASN1_INTEGER_free(ret);
  return (NULL);
}


x86 assembly (clang, -O0):
<BN_to_ASN1_INTEGER>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ab <BN_to_ASN1_INTEGER+0x2b>
mov    $0x0,%al
call   1040 <ASN1_INTEGER_new@plt>
mov    %rax,-0x20(%rbp)
jmp    11b3 <BN_to_ASN1_INTEGER+0x33>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d1 <BN_to_ASN1_INTEGER+0x51>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   10a0 <ASN1error@plt>
jmp    12f4 <BN_to_ASN1_INTEGER+0x174>
mov    -0x20(%rbp),%rdi
call   1050 <ASN1_INTEGER_valid@plt>
cmp    $0x0,%eax
jne    11e8 <BN_to_ASN1_INTEGER+0x68>
jmp    12f4 <BN_to_ASN1_INTEGER+0x174>
mov    -0x10(%rbp),%rdi
call   1080 <BN_is_negative@plt>
cmp    $0x0,%rax
je     1210 <BN_to_ASN1_INTEGER+0x90>
mov    0x2dc6(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x10(%rax)
jmp    1220 <BN_to_ASN1_INTEGER+0xa0>
mov    0x2dc9(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <BN_num_bits@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1240 <BN_to_ASN1_INTEGER+0xc0>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    1251 <BN_to_ASN1_INTEGER+0xd1>
mov    -0x28(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x24(%rbp),%ecx
add    $0x4,%ecx
cmp    %ecx,%eax
jge    12ac <BN_to_ASN1_INTEGER+0x12c>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x24(%rbp),%esi
add    $0x4,%esi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12a0 <BN_to_ASN1_INTEGER+0x120>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   10a0 <ASN1error@plt>
jmp    12f4 <BN_to_ASN1_INTEGER+0x174>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
call   1060 <BN_bn2bin@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    12e7 <BN_to_ASN1_INTEGER+0x167>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1313 <BN_to_ASN1_INTEGER+0x193>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     130b <BN_to_ASN1_INTEGER+0x18b>
mov    -0x20(%rbp),%rdi
call   1090 <ASN1_INTEGER_free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BN_to_ASN1_INTEGER>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rbx
test   %rsi,%rsi
jne    11a6 <BN_to_ASN1_INTEGER+0x26>
xor    %eax,%eax
call   1040 <ASN1_INTEGER_new@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1232 <BN_to_ASN1_INTEGER+0xb2>
mov    %rbx,%rdi
call   1050 <ASN1_INTEGER_valid@plt>
test   %eax,%eax
je     124b <BN_to_ASN1_INTEGER+0xcb>
mov    %r15,%rdi
call   1080 <BN_is_negative@plt>
test   %rax,%rax
jne    11cc <BN_to_ASN1_INTEGER+0x4c>
mov    0x2e16(%rip),%rax
jmp    11d3 <BN_to_ASN1_INTEGER+0x53>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rbx)
mov    %r15,%rdi
call   1030 <BN_num_bits@plt>
test   %eax,%eax
je     11f4 <BN_to_ASN1_INTEGER+0x74>
lea    0x7(%rax),%esi
test   %eax,%eax
cmovns %eax,%esi
sar    $0x3,%esi
add    $0x5,%esi
jmp    11f9 <BN_to_ASN1_INTEGER+0x79>
mov    $0x4,%esi
mov    0x8(%rbx),%rax
cmp    %esi,(%rbx)
jge    1212 <BN_to_ASN1_INTEGER+0x92>
mov    %rax,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     123d <BN_to_ASN1_INTEGER+0xbd>
mov    %rax,0x8(%rbx)
mov    %r15,%rdi
mov    %rax,%rsi
call   1060 <BN_bn2bin@plt>
mov    %eax,(%rbx)
test   %eax,%eax
jne    125a <BN_to_ASN1_INTEGER+0xda>
mov    0x8(%rbx),%rax
movb   $0x0,(%rax)
movl   $0x1,(%rbx)
jmp    125a <BN_to_ASN1_INTEGER+0xda>
mov    0x2d9f(%rip),%rax
xor    %ebx,%ebx
jmp    1244 <BN_to_ASN1_INTEGER+0xc4>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   10a0 <ASN1error@plt>
cmp    %r14,%rbx
je     1258 <BN_to_ASN1_INTEGER+0xd8>
mov    %rbx,%rdi
call   1090 <ASN1_INTEGER_free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_7__ = type { i32, i8*, i32 }
@ERR_R_NESTED_ASN1_ERROR = global i32 0
@V_ASN1_NEG_INTEGER = global i32 0
@V_ASN1_INTEGER = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define %struct.TYPE_7__* @BN_to_ASN1_INTEGER(i32* %0, %struct.TYPE_7__* %1) {
%3 = alloca %struct.TYPE_7__*
%4 = alloca i32*
%5 = alloca %struct.TYPE_7__*
%6 = alloca %struct.TYPE_7__*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32* %0, i32** %4
store %struct.TYPE_7__* %1, %struct.TYPE_7__** %5
%10 = load %struct.TYPE_7__*, %struct.TYPE_7__** %5
%11 = icmp eq %struct.TYPE_7__* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call %struct.TYPE_7__* (...) @ASN1_INTEGER_new()
store %struct.TYPE_7__* %13, %struct.TYPE_7__** %6
br label %16
14: ; preds = %2
%15 = load %struct.TYPE_7__*, %struct.TYPE_7__** %5
store %struct.TYPE_7__* %15, %struct.TYPE_7__** %6
br label %16
16: ; preds = %14, %12
%17 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%18 = icmp eq %struct.TYPE_7__* %17, null
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @ERR_R_NESTED_ASN1_ERROR
%21 = call i32 @ASN1error(i32 %20)
br label %94
22: ; preds = %16
%23 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%24 = call i32 @ASN1_INTEGER_valid(%struct.TYPE_7__* %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %22
br label %94
27: ; preds = %22
%28 = load i32*, i32** %4
%29 = call i64 @BN_is_negative(i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i32, i32* @V_ASN1_NEG_INTEGER
%33 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%34 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %33, i32 0, i32 2
store i32 %32, i32* %34
br label %39
35: ; preds = %27
%36 = load i32, i32* @V_ASN1_INTEGER
%37 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%38 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %37, i32 0, i32 2
store i32 %36, i32* %38
br label %39
39: ; preds = %35, %31
%40 = load i32*, i32** %4
%41 = call i32 @BN_num_bits(i32* %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %39
br label %49
45: ; preds = %39
%46 = load i32, i32* %8
%47 = sdiv i32 %46, 8
%48 = add nsw i32 %47, 1
br label %49
49: ; preds = %45, %44
%50 = phi i32 [ 0, %44 ], [ %48, %45 ]
store i32 %50, i32* %7
%51 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%52 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 4
%56 = icmp slt i32 %53, %55
br i1 %56, label %57, label %73
57: ; preds = %49
%58 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%59 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %58, i32 0, i32 1
%60 = load i8*, i8** %59
%61 = load i32, i32* %7
%62 = add nsw i32 %61, 4
%63 = call i8* @realloc(i8* %60, i32 %62)
store i8* %63, i8** %9
%64 = load i8*, i8** %9
%65 = icmp ne i8* %64, null
br i1 %65, label %69, label %66
66: ; preds = %57
%67 = load i32, i32* @ERR_R_MALLOC_FAILURE
%68 = call i32 @ASN1error(i32 %67)
br label %94
69: ; preds = %57
%70 = load i8*, i8** %9
%71 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%72 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %71, i32 0, i32 1
store i8* %70, i8** %72
br label %73
73: ; preds = %69, %49
%74 = load i32*, i32** %4
%75 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%76 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %75, i32 0, i32 1
%77 = load i8*, i8** %76
%78 = call i32 @BN_bn2bin(i32* %74, i8* %77)
%79 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%80 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %79, i32 0, i32 0
store i32 %78, i32* %80
%81 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%82 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %81, i32 0, i32 0
%83 = load i32, i32* %82
%84 = icmp ne i32 %83, 0
br i1 %84, label %92, label %85
85: ; preds = %73
%86 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%87 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %86, i32 0, i32 1
%88 = load i8*, i8** %87
%89 = getelementptr inbounds i8, i8* %88, i64 0
store i8 0, i8* %89
%90 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%91 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %90, i32 0, i32 0
store i32 1, i32* %91
br label %92
92: ; preds = %85, %73
%93 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
store %struct.TYPE_7__* %93, %struct.TYPE_7__** %3
br label %102
94: ; preds = %66, %26, %19
%95 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%96 = load %struct.TYPE_7__*, %struct.TYPE_7__** %5
%97 = icmp ne %struct.TYPE_7__* %95, %96
br i1 %97, label %98, label %101
98: ; preds = %94
%99 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%100 = call i32 @ASN1_INTEGER_free(%struct.TYPE_7__* %99)
br label %101
101: ; preds = %98, %94
store %struct.TYPE_7__* null, %struct.TYPE_7__** %3
br label %102
102: ; preds = %101, %92
%103 = load %struct.TYPE_7__*, %struct.TYPE_7__** %3
ret %struct.TYPE_7__* %103
}

LLVM IR (-O2):
%struct.TYPE_7__ = type { i32, i8*, i32 }
@ERR_R_NESTED_ASN1_ERROR = global i32 0
@V_ASN1_NEG_INTEGER = global i32 0
@V_ASN1_INTEGER = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define %struct.TYPE_7__* @BN_to_ASN1_INTEGER(i32* %0, %struct.TYPE_7__* %1) {
%3 = icmp eq %struct.TYPE_7__* %1, null
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call %struct.TYPE_7__* (...) @ASN1_INTEGER_new()
%6 = icmp eq %struct.TYPE_7__* %5, null
br i1 %6, label %41, label %7
7: ; preds = %2, %4
%8 = phi %struct.TYPE_7__* [ %5, %4 ], [ %1, %2 ]
%9 = tail call i32 @ASN1_INTEGER_valid(%struct.TYPE_7__* %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %46, label %11
11: ; preds = %7
%12 = tail call i64 @BN_is_negative(i32* %0)
%13 = icmp eq i64 %12, 0
%14 = load i32, i32* @V_ASN1_INTEGER
%15 = load i32, i32* @V_ASN1_NEG_INTEGER
%16 = select i1 %13, i32 %14, i32 %15
%17 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %8, i64 0, i32 2
store i32 %16, i32* %17
%18 = tail call i32 @BN_num_bits(i32* %0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %11
%21 = sdiv i32 %18, 8
%22 = add nsw i32 %21, 5
br label %23
23: ; preds = %11, %20
%24 = phi i32 [ %22, %20 ], [ 4, %11 ]
%25 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %8, i64 0, i32 0
%26 = load i32, i32* %25
%27 = icmp slt i32 %26, %24
%28 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %8, i64 0, i32 1
%29 = load i8*, i8** %28
br i1 %27, label %30, label %34
30: ; preds = %23
%31 = tail call i8* @realloc(i8* %29, i32 %24)
%32 = icmp eq i8* %31, null
br i1 %32, label %41, label %33
33: ; preds = %30
store i8* %31, i8** %28
br label %34
34: ; preds = %23, %33
%35 = phi i8* [ %31, %33 ], [ %29, %23 ]
%36 = tail call i32 @BN_bn2bin(i32* %0, i8* %35)
store i32 %36, i32* %25
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %51
38: ; preds = %34
%39 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %8, i64 0, i32 1
%40 = load i8*, i8** %39
store i8 0, i8* %40
store i32 1, i32* %25
br label %51
41: ; preds = %30, %4
%42 = phi i32* [ @ERR_R_NESTED_ASN1_ERROR, %4 ], [ @ERR_R_MALLOC_FAILURE, %30 ]
%43 = phi %struct.TYPE_7__* [ null, %4 ], [ %8, %30 ]
%44 = load i32, i32* %42
%45 = tail call i32 @ASN1error(i32 %44)
br label %46
46: ; preds = %41, %7
%47 = phi %struct.TYPE_7__* [ %8, %7 ], [ %43, %41 ]
%48 = icmp eq %struct.TYPE_7__* %47, %1
br i1 %48, label %51, label %49
49: ; preds = %46
%50 = tail call i32 @ASN1_INTEGER_free(%struct.TYPE_7__* %47)
br label %51
51: ; preds = %46, %49, %34, %38
%52 = phi %struct.TYPE_7__* [ %8, %38 ], [ %8, %34 ], [ null, %49 ], [ null, %46 ]
ret %struct.TYPE_7__* %52
}