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

```asm
<asn1_d2i_ex_primitive>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x68,%rsp
  mov    0xa8(%rsp),%rax
  mov    0xa0(%rsp),%r10d
  mov    %rax,0x8(%rsp)
  mov    %fs:0x28,%rax
  mov    %rax,0x58(%rsp)
  xor    %eax,%eax
  movb   $0x0,0x2b(%rsp)
  test   %rdi,%rdi
  je     L195
  mov    D160(%rip),%rax
  mov    %rdx,%r15
  mov    %rsi,%r12
  mov    %rdi,%r13
  mov    D102(%rip),%rdx
  mov    %rcx,%rbp
  mov    %r8d,%ebx
  mov    %r9d,%r14d
  mov    (%rax),%eax
  mov    (%rdx),%rsi
  cmp    %rsi,(%rcx)
  je     L346
  mov    0x8(%rcx),%edx
  lea    0x38(%rsp),%r11
  mov    %edx,0x2c(%rsp)
  cmp    %eax,%edx
  je     L194
L196:
  cmp    $0xffffffff,%ebx
  je     L228
L178:
  mov    (%r12),%rax
  lea    0x2a(%rsp),%rcx
  lea    0x30(%rsp),%rdi
  xor    %edx,%edx
  sub    $0x8,%rsp
  mov    %r11,%r9
  xor    %esi,%esi
  mov    %rax,0x40(%rsp)
  movsbl %r10b,%eax
  push   0x10(%rsp)
  push   %rax
  push   %r14
  push   %rbx
  push   %r15
  lea    0x59(%rsp),%r8
  mov    %r11,0x40(%rsp)
  call   <asn1_check_tlen@plt>
  add    $0x30,%rsp
  mov    0x10(%rsp),%r11
  test   %eax,%eax
  mov    %eax,%r14d
  je     L333
  cmp    $0xffffffff,%eax
  je     L364
  mov    D156(%rip),%rax
  mov    0x2c(%rsp),%ecx
  mov    (%rax),%edx
  mov    D58(%rip),%rax
  cmp    (%rax),%ecx
  je     L116
  mov    D84(%rip),%rax
  cmp    (%rax),%ecx
  je     L116
  cmp    %edx,%ecx
  je     L396
  cmpb   $0x0,0x29(%rsp)
  mov    0x30(%rsp),%rdx
  jne    L55
  mov    0x38(%rsp),%r14
  lea    (%r14,%rdx,1),%rax
  mov    %rax,0x38(%rsp)
L135:
  mov    %rbp,%r9
  lea    0x2b(%rsp),%r8
  mov    %r14,%rsi
  mov    %r13,%rdi
  call   <asn1_ex_c2i@plt>
  test   %eax,%eax
  je     L220
  mov    0x38(%rsp),%rax
  mov    $0x1,%r14d
  mov    %rax,(%r12)
L389:
  cmpb   $0x0,0x2b(%rsp)
  je     L364
L167:
  mov    0x48(%rsp),%rdi
  test   %rdi,%rdi
  je     L364
  call   <OPENSSL_free@plt>
  nopl   0x0(%rax)
L364:
  mov    0x58(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L79
  add    $0x68,%rsp
  mov    %r14d,%eax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  cs nopw 0x0(%rax,%rax,1)
L194:
  test   %r8d,%r8d
  js     L400
  mov    D75(%rip),%rax
  mov    (%rax),%esi
  mov    D35(%rip),%rax
  mov    (%rax),%edi
  call   <ASN1err@plt>
L86:
  xor    %r14d,%r14d
  jmp    L364
  nopl   0x0(%rax)
L116:
  cmp    %edx,%ecx
  je     L396
  cmpb   $0x0,0x29(%rsp)
  je     L176
L33:
  movsbl 0x2a(%rsp),%edx
  mov    (%r12),%r14
  mov    0x30(%rsp),%rsi
  test   %dl,%dl
  jne    L12
  movq   $0x0,0x48(%rsp)
  mov    0x38(%rsp),%rax
  mov    %rax,%rdx
  add    %rsi,%rax
  sub    %r14,%rdx
  mov    %rax,0x38(%rsp)
  add    %rsi,%rdx
  jmp    L135
  nopl   (%rax)
L265:
  mov    $0xffffffff,%ebx
L400:
  test   %r10b,%r10b
  jne    L419
  mov    (%r12),%rax
  mov    %r10d,0x1c(%rsp)
  lea    0x38(%rsp),%r11
  xor    %ecx,%ecx
  lea    0x40(%rsp),%rdx
  lea    0x2c(%rsp),%rsi
  sub    $0x8,%rsp
  xor    %edi,%edi
  mov    %rax,0x40(%rsp)
  mov    %r11,%r9
  xor    %r8d,%r8d
  push   0x10(%rsp)
  push   $0x0
  push   $0x0
  push   $0xffffffffffffffff
  push   %r15
  mov    %r11,0x40(%rsp)
  call   <asn1_check_tlen@plt>
  add    $0x30,%rsp
  mov    0x10(%rsp),%r11
  mov    0x1c(%rsp),%r10d
  test   %eax,%eax
  je     L114
  mov    D215(%rip),%rax
  movzbl (%rax),%eax
  cmp    %al,0x40(%rsp)
  je     L196
  mov    D156(%rip),%rax
  mov    (%rax),%eax
  mov    %eax,0x2c(%rsp)
  jmp    L196
  nopl   0x0(%rax,%rax,1)
L346:
  mov    %r8d,0x2c(%rsp)
  lea    0x38(%rsp),%r11
  cmp    %eax,%r8d
  je     L265
L142:
  mov    D215(%rip),%rax
  movzbl (%rax),%r14d
  jmp    L178
  nopl   0x0(%rax,%rax,1)
L12:
  mov    %r11,%rdi
  call   <asn1_find_end@plt>
  test   %eax,%eax
  je     L220
  mov    0x38(%rsp),%rdx
  mov    0x2c(%rsp),%ecx
  sub    %r14,%rdx
  jmp    L135
  nopl   (%rax)
L419:
  mov    D187(%rip),%rax
  mov    (%rax),%esi
  mov    D35(%rip),%rax
  mov    (%rax),%edi
  call   <ASN1err@plt>
  jmp    L86
  nopl   0x0(%rax)
L220:
  xor    %r14d,%r14d
  jmp    L389
  nopl   0x0(%rax,%rax,1)
L333:
  mov    D0(%rip),%rax
  mov    (%rax),%esi
  mov    D35(%rip),%rax
  mov    (%rax),%edi
  call   <ASN1err@plt>
  jmp    L364
  nopl   0x0(%rax)
L396:
  mov    0x8(%rsp),%rax
  test   %rax,%rax
  je     L33
  movq   $0x0,(%rax)
  jmp    L33
  nopw   0x0(%rax,%rax,1)
L195:
  mov    D170(%rip),%rax
L160:
  mov    (%rax),%esi
  mov    D35(%rip),%rax
  mov    (%rax),%edi
  call   <ASN1err@plt>
  jmp    L86
  nopl   0x0(%rax)
L176:
  mov    D59(%rip),%rax
  jmp    L160
  nopl   0x0(%rax)
L228:
  mov    0x2c(%rsp),%ebx
  jmp    L142
  nopl   0x0(%rax)
L55:
  mov    D215(%rip),%rax
  movsbl 0x2a(%rsp),%ecx
  lea    0x40(%rsp),%r15
  mov    %r11,%rsi
  mov    $0xffffffff,%r8d
  mov    %r15,%rdi
  movq   $0x0,0x40(%rsp)
  movq   $0x0,0x50(%rsp)
  movzbl (%rax),%r9d
  movq   $0x0,0x48(%rsp)
  call   <asn1_collect@plt>
  mov    %eax,%r14d
  test   %eax,%eax
  jne    L174
  movb   $0x1,0x2b(%rsp)
  jmp    L167
  nopw   0x0(%rax,%rax,1)
L114:
  mov    D0(%rip),%rax
  mov    (%rax),%esi
  mov    D35(%rip),%rax
  mov    (%rax),%edi
  call   <ASN1err@plt>
  jmp    L86
  nopl   0x0(%rax)
L174:
  mov    0x40(%rsp),%rdx
  mov    %r15,%rdi
  lea    0x1(%rdx),%rsi
  mov    %rdx,0x8(%rsp)
  call   <BUF_MEM_grow_clean@plt>
  mov    0x8(%rsp),%rdx
  test   %eax,%eax
  mov    %eax,%r14d
  je     L273
  mov    0x48(%rsp),%r14
  mov    0x2c(%rsp),%ecx
  movq   $0x0,(%r14,%rdx,8)
  movb   $0x1,0x2b(%rsp)
  jmp    L135
L273:
  mov    D132(%rip),%rax
  mov    (%rax),%esi
  mov    D35(%rip),%rax
  mov    (%rax),%edi
  call   <ASN1err@plt>
  jmp    L364
L79:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, long inlen, const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) {
  int ret = 0, utype;
  long plen;
  char cst, inf, free_cont = 0;
  const unsigned char *p;
  BUF_MEM buf;
  const unsigned char *cont = NULL;
  long len;
  if (!pval) {
    ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
    return 0;
  }
  if (it->itype == ASN1_ITYPE_MSTRING) {
    utype = tag;
    tag = -1;
  } else
    utype = it->utype;
  if (utype == V_ASN1_ANY) {
    unsigned char oclass;
    if (tag >= 0) {
      ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
      return 0;
    }
    if (opt) {
      ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_OPTIONAL_ANY);
      return 0;
    }
    p = *in;
    ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, &p, inlen, -1, 0, 0, ctx);
    if (!ret) {
      ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
      return 0;
    }
    if (oclass != V_ASN1_UNIVERSAL)
      utype = V_ASN1_OTHER;
  }
  if (tag == -1) {
    tag = utype;
    aclass = V_ASN1_UNIVERSAL;
  }
  p = *in;
  ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, &p, inlen, tag, aclass, opt, ctx);
  if (!ret) {
    ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
    return 0;
  } else if (ret == -1)
    return -1;
  ret = 0;
  if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
    if (utype == V_ASN1_OTHER) {
      if (ctx)
        (ctx)->valid = 0;
    } else if (!cst) {
      ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_CONSTRUCTED);
      return 0;
    }
    cont = *in;
    if (inf) {
      if (!asn1_find_end(&p, plen, inf))
        goto err;
      len = p - cont;
    } else {
      len = p - cont + plen;
      p += plen;
      buf.data = NULL;
    }
  } else if (cst) {
    buf.length = 0;
    buf.max = 0;
    buf.data = NULL;
    if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL)) {
      free_cont = 1;
      goto err;
    }
    len = buf.length;
    if (!BUF_MEM_grow_clean(&buf, len + 1)) {
      ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
      return 0;
    }
    buf.data[len] = 0;
    cont = (const unsigned char *)buf.data;
    free_cont = 1;
  } else {
    cont = p;
    len = plen;
    p += plen;
  }
  if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
    goto err;
  *in = p;
  ret = 1;
err:
  if (free_cont && buf.data)
    OPENSSL_free(buf.data);
  return ret;
}
```