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

/* Type definitions */
struct TYPE_6__ {
  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*/ 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 (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    119b <BN_to_ASN1_INTEGER+0x2b>
mov    $0x0,%al
call   1040 <ASN1_INTEGER_new@plt>
mov    %rax,-0x20(%rbp)
jmp    11a3 <BN_to_ASN1_INTEGER+0x33>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c1 <BN_to_ASN1_INTEGER+0x51>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1090 <ASN1error@plt>
jmp    12cd <BN_to_ASN1_INTEGER+0x15d>
mov    -0x10(%rbp),%rdi
call   1070 <BN_is_negative@plt>
cmp    $0x0,%rax
je     11e9 <BN_to_ASN1_INTEGER+0x79>
mov    0x2ded(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x10(%rax)
jmp    11f9 <BN_to_ASN1_INTEGER+0x89>
mov    0x2df0(%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    1219 <BN_to_ASN1_INTEGER+0xa9>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    122a <BN_to_ASN1_INTEGER+0xba>
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    1285 <BN_to_ASN1_INTEGER+0x115>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x24(%rbp),%esi
add    $0x4,%esi
call   1060 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1279 <BN_to_ASN1_INTEGER+0x109>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1090 <ASN1error@plt>
jmp    12cd <BN_to_ASN1_INTEGER+0x15d>
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   1050 <BN_bn2bin@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    12c0 <BN_to_ASN1_INTEGER+0x150>
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    12ec <BN_to_ASN1_INTEGER+0x17c>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     12e4 <BN_to_ASN1_INTEGER+0x174>
mov    -0x20(%rbp),%rdi
call   1080 <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    1196 <BN_to_ASN1_INTEGER+0x26>
xor    %eax,%eax
call   1040 <ASN1_INTEGER_new@plt>
mov    %rax,%rbx
test   %rax,%rax
je     121b <BN_to_ASN1_INTEGER+0xab>
mov    %r15,%rdi
call   1070 <BN_is_negative@plt>
test   %rax,%rax
jne    11ac <BN_to_ASN1_INTEGER+0x3c>
mov    0x2e36(%rip),%rax
jmp    11b3 <BN_to_ASN1_INTEGER+0x43>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rbx)
mov    %r15,%rdi
call   1030 <BN_num_bits@plt>
test   %eax,%eax
je     11d4 <BN_to_ASN1_INTEGER+0x64>
lea    0x7(%rax),%esi
test   %eax,%eax
cmovns %eax,%esi
sar    $0x3,%esi
add    $0x5,%esi
jmp    11d9 <BN_to_ASN1_INTEGER+0x69>
mov    $0x4,%esi
mov    0x8(%rbx),%rax
cmp    %esi,(%rbx)
jge    11f2 <BN_to_ASN1_INTEGER+0x82>
mov    %rax,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     1212 <BN_to_ASN1_INTEGER+0xa2>
mov    %rax,0x8(%rbx)
mov    %r15,%rdi
mov    %rax,%rsi
call   1050 <BN_bn2bin@plt>
mov    %eax,(%rbx)
test   %eax,%eax
jne    123a <BN_to_ASN1_INTEGER+0xca>
mov    0x8(%rbx),%rax
movb   $0x0,(%rax)
movl   $0x1,(%rbx)
jmp    123a <BN_to_ASN1_INTEGER+0xca>
mov    0x2dcf(%rip),%rax
jmp    1224 <BN_to_ASN1_INTEGER+0xb4>
mov    0x2db6(%rip),%rax
xor    %ebx,%ebx
mov    (%rax),%edi
call   1090 <ASN1error@plt>
cmp    %r14,%rbx
je     1238 <BN_to_ASN1_INTEGER+0xc8>
mov    %rbx,%rdi
call   1080 <ASN1_INTEGER_free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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