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

```asm
<rearrange_relocs>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x28,%rsp
  mov    %rdx,(%rsp)
  mov    %rcx,0x10(%rsp)
  test   %rdi,%rdi
  je     L114
  mov    %rdi,%r13
  mov    %rsi,%r14
  mov    $0x0,%esi
  jmp    L111
L80:
  movslq 0x8(%rbx),%rax
  shr    $0x3,%rax
  test   %eax,%eax
  jle    L16
  lea    -0x1(%rax),%eax
  lea    0x8(,%rax,8),%rsi
  mov    $0x0,%edx
  jmp    L65
L183:
  add    $0x8,%rdx
  cmp    %rsi,%rdx
  je     L16
L65:
  mov    %r13,%rax
  add    0x10(%rbx),%rax
  add    %rdx,%rax
  mov    (%rax),%rcx
  cmp    %rcx,%r14
  ja     L183
  cmp    %rcx,(%rsp)
  jbe    L183
  add    0x10(%rsp),%rcx
  mov    %rcx,(%rax)
  jmp    L183
L156:
  mov    0x10(%rsp),%rax
  add    %rax,0x0(%rbp)
L109:
  add    $0x10,%r12
  cmp    0x8(%rsp),%r12
  je     L16
L107:
  mov    %r13,%rbp
  add    0x10(%rbx),%rbp
  add    %r12,%rbp
  mov    0x0(%rbp),%rax
  cmp    %rax,%r14
  ja     L109
  cmp    %rax,(%rsp)
  jbe    L109
  mov    0x1c(%rbx),%esi
  mov    %r13,%rdi
  call   <get_s_header@plt>
  mov    %rax,0x18(%rsp)
  mov    0x8(%rbp),%edi
  call   <ELF64_R_SYM@plt>
  mov    0x18(%rsp),%rcx
  mov    %r13,%rdx
  add    0x10(%rcx),%rdx
  imul   0x18(%rcx),%eax
  cltq
  add    %rdx,%rax
  mov    (%rax),%rdx
  cmp    %rdx,0x0(%rbp)
  jne    L156
  add    0x10(%rsp),%rdx
  mov    %rdx,(%rax)
  jmp    L156
L16:
  mov    %r15d,%esi
L111:
  lea    0x1(%rsi),%r15d
  mov    %r13,%rdi
  call   <get_s_header@plt>
  mov    %rax,%rbx
  test   %rax,%rax
  je     L23
  mov    (%rbx),%rax
  mov    D4(%rip),%rdx
  cmp    (%rdx),%rax
  je     L80
  mov    D14(%rip),%rdx
  cmp    (%rdx),%rax
  jne    L16
  movslq 0x8(%rbx),%rax
  shr    $0x4,%rax
  test   %eax,%eax
  jle    L16
  lea    -0x1(%rax),%eax
  add    $0x1,%rax
  shl    $0x4,%rax
  mov    %rax,0x8(%rsp)
  mov    $0x0,%r12d
  jmp    L107
L23:
  mov    $0x0,%eax
L22:
  add    $0x28,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L114:
  mov    $0x1,%eax
  jmp    L22
```

assistant: ```c
int rearrange_relocs(char *elf, void *base, void *top, size_t n) {
  if (!elf)
    return 1;
  int sh_num;
  sh_num = 0;
  Elf64_Shdr *shdr;
  Elf64_Shdr *linked;
  while ((shdr = get_s_header(elf, sh_num++))) {
    int num, i;
    unsigned long long addr;
    if (shdr->sh_type == SHT_RELA) {
      num = shdr->sh_size / sizeof(Elf64_Rela);
      Elf64_Rela *rel;
      for (i = 0; i < num; i++) {
        addr = ((unsigned long long)elf + shdr->sh_offset + (i * sizeof(Elf64_Rela)));
        rel = (Elf64_Rela *)addr;
        if ((unsigned long long)rel->r_offset >= (unsigned long long)base && (unsigned long long)rel->r_offset < (unsigned long long)top) {
          rel->r_offset += n;
        }
      }
    } else if (shdr->sh_type == SHT_REL) {
      num = shdr->sh_size / sizeof(Elf64_Rel);
      Elf64_Rel *rel;
      for (i = 0; i < num; i++) {
        addr = ((unsigned long long)elf + shdr->sh_offset + (i * sizeof(Elf64_Rel)));
        rel = (Elf64_Rel *)addr;
        if ((unsigned long long)rel->r_offset >= (unsigned long long)base && (unsigned long long)rel->r_offset < (unsigned long long)top) {
          linked = get_s_header(elf, shdr->sh_link);
          unsigned long long sym_addr;
          Elf64_Sym *sym;
          int sym_ind = ELF64_R_SYM(rel->r_info);
          sym_addr = (unsigned long long)elf + linked->sh_offset;
          sym_addr += linked->sh_entsize * sym_ind;
          sym = (Elf64_Sym *)sym_addr;
          if (rel->r_offset == sym->st_value)
            sym->st_value += n;
          rel->r_offset += n;
        }
      }
    }
  }
  return 0;
}
```