user: Please show me the C implementation that matches the assembly code listed below:

```asm
<BN_to_ASN1_ENUMERATED>:
  endbr64
  push   %rbp
  mov    %rsp,%rbp
  sub    $0x30,%rsp
  mov    %rdi,-0x28(%rbp)
  mov    %rsi,-0x30(%rbp)
  cmpq   $0x0,-0x30(%rbp)
  jne    L24
  mov    $0x0,%eax
  call   <ASN1_ENUMERATED_new@plt>
  mov    %rax,-0x10(%rbp)
  jmp    L104
L24:
  mov    -0x30(%rbp),%rax
  mov    %rax,-0x10(%rbp)
L104:
  cmpq   $0x0,-0x10(%rbp)
  jne    L85
  mov    D20(%rip),%rax
  mov    (%rax),%eax
  mov    %eax,%edi
  call   <ASN1error@plt>
  jmp    L120
L85:
  mov    -0x28(%rbp),%rax
  mov    %rax,%rdi
  call   <BN_is_negative@plt>
  test   %rax,%rax
  je     L106
  mov    D28(%rip),%rax
  mov    (%rax),%edx
  mov    -0x10(%rbp),%rax
  mov    %edx,0x10(%rax)
  jmp    L36
L106:
  mov    D24(%rip),%rax
  mov    (%rax),%edx
  mov    -0x10(%rbp),%rax
  mov    %edx,0x10(%rax)
L36:
  mov    -0x28(%rbp),%rax
  mov    %rax,%rdi
  call   <BN_num_bits@plt>
  mov    %eax,-0x18(%rbp)
  cmpl   $0x0,-0x18(%rbp)
  je     L83
  mov    -0x18(%rbp),%eax
  lea    0x7(%rax),%edx
  test   %eax,%eax
  cmovs  %edx,%eax
  sar    $0x3,%eax
  add    $0x1,%eax
  jmp    L34
L83:
  mov    $0x0,%eax
L34:
  mov    %eax,-0x14(%rbp)
  mov    -0x14(%rbp),%eax
  lea    0x3(%rax),%edx
  mov    -0x10(%rbp),%rax
  mov    (%rax),%eax
  cmp    %eax,%edx
  jl     L50
  mov    -0x14(%rbp),%eax
  lea    0x4(%rax),%edx
  mov    -0x10(%rbp),%rax
  mov    0x8(%rax),%rax
  mov    %edx,%esi
  mov    %rax,%rdi
  call   <realloc@plt>
  mov    %rax,-0x8(%rbp)
  cmpq   $0x0,-0x8(%rbp)
  jne    L32
  mov    D14(%rip),%rax
  mov    (%rax),%eax
  mov    %eax,%edi
  call   <ASN1error@plt>
  jmp    L120
L32:
  mov    -0x10(%rbp),%rax
  mov    -0x8(%rbp),%rdx
  mov    %rdx,0x8(%rax)
L50:
  mov    -0x10(%rbp),%rax
  mov    0x8(%rax),%rdx
  mov    -0x28(%rbp),%rax
  mov    %rdx,%rsi
  mov    %rax,%rdi
  call   <BN_bn2bin@plt>
  mov    -0x10(%rbp),%rdx
  mov    %eax,(%rdx)
  mov    -0x10(%rbp),%rax
  mov    (%rax),%eax
  test   %eax,%eax
  jne    L115
  mov    -0x10(%rbp),%rax
  mov    0x8(%rax),%rax
  movb   $0x0,(%rax)
  mov    -0x10(%rbp),%rax
  movl   $0x1,(%rax)
L115:
  mov    -0x10(%rbp),%rax
  jmp    L86
L120:
  mov    -0x10(%rbp),%rax
  cmp    -0x30(%rbp),%rax
  je     L87
  mov    -0x10(%rbp),%rax
  mov    %rax,%rdi
  call   <ASN1_ENUMERATED_free@plt>
L87:
  mov    $0x0,%eax
L86:
  leave
  ret
```

assistant: ```c
ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai) {
  ASN1_ENUMERATED *ret;
  int len, j;
  if (ai == NULL)
    ret = ASN1_ENUMERATED_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_ENUMERATED;
  else
    ret->type = V_ASN1_ENUMERATED;
  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_ENUMERATED_free(ret);
  return (NULL);
}
```