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

```asm
<bead_sort>:
  endbr64
  push   %r12
  mov    %rdi,%r12
  push   %rbp
  mov    %esi,%ebp
  push   %rbx
  mov    (%rdi),%ebx
  cmp    $0x1,%esi
  jle    L201
  lea    -0x2(%rsi),%eax
  lea    -0x1(%rsi),%ecx
  cmp    $0x2,%eax
  jbe    L112
  mov    %ecx,%edx
  movd   %ebx,%xmm3
  lea    0x4(%rdi),%rax
  shr    $0x2,%edx
  pshufd $0x0,%xmm3,%xmm2
  sub    $0x1,%edx
  shl    $0x4,%rdx
  lea    0x14(%rdi,%rdx,1),%rdx
  nopl   (%rax)
L24:
  movdqu (%rax),%xmm0
  add    $0x10,%rax
  movdqa %xmm0,%xmm1
  pcmpgtd %xmm2,%xmm1
  pand   %xmm1,%xmm0
  pandn  %xmm2,%xmm1
  movdqa %xmm1,%xmm2
  por    %xmm0,%xmm2
  cmp    %rdx,%rax
  jne    L24
  movdqa %xmm2,%xmm1
  mov    %ecx,%edx
  psrldq $0x8,%xmm1
  and    $0xfffffffc,%edx
  movdqa %xmm1,%xmm0
  lea    0x1(%rdx),%eax
  pcmpgtd %xmm2,%xmm0
  pand   %xmm0,%xmm1
  pandn  %xmm2,%xmm0
  por    %xmm1,%xmm0
  movdqa %xmm0,%xmm2
  psrldq $0x4,%xmm2
  movdqa %xmm2,%xmm1
  pcmpgtd %xmm0,%xmm1
  pand   %xmm1,%xmm2
  pandn  %xmm0,%xmm1
  por    %xmm2,%xmm1
  movd   %xmm1,%ebx
  cmp    %ecx,%edx
  je     L167
L74:
  movslq %eax,%rdx
  lea    0x0(,%rdx,4),%rcx
  mov    (%r12,%rdx,4),%edx
  cmp    %edx,%ebx
  cmovl  %edx,%ebx
  lea    0x1(%rax),%edx
  cmp    %edx,%ebp
  jle    L167
  mov    0x4(%r12,%rcx,1),%edx
  cmp    %edx,%ebx
  cmovl  %edx,%ebx
  add    $0x2,%eax
  cmp    %eax,%ebp
  jle    L167
  mov    0x8(%r12,%rcx,1),%eax
  cmp    %eax,%ebx
  cmovl  %eax,%ebx
L167:
  mov    %ebp,%esi
  mov    $0x1,%edi
  imul   %ebx,%esi
  call   <calloc@plt>
  mov    %rax,%rdi
L140:
  mov    %r12,%rdx
  xor    %r8d,%r8d
  xor    %esi,%esi
  nopw   0x0(%rax,%rax,1)
L227:
  mov    (%rdx),%r9d
  movslq %r8d,%rcx
  xor    %eax,%eax
  add    %rdi,%rcx
  test   %r9d,%r9d
  jle    L107
L205:
  movb   $0x1,(%rcx,%rax,1)
  add    $0x1,%rax
  cmp    %eax,(%rdx)
  jg     L205
L107:
  add    $0x1,%esi
  add    $0x4,%rdx
  add    %ebx,%r8d
  cmp    %esi,%ebp
  jg     L227
L65:
  test   %ebx,%ebx
  jle    L45
  movslq %ebx,%r8
  xor    %r9d,%r9d
  nopl   0x0(%rax)
L9:
  lea    (%rdi,%r9,1),%rax
  xor    %esi,%esi
  xor    %edx,%edx
  test   %ebp,%ebp
  jle    L110
  nopl   0x0(%rax)
L176:
  movzbl (%rax),%ecx
  movb   $0x0,(%rax)
  add    %r8,%rax
  add    %ecx,%esi
  mov    %edx,%ecx
  lea    0x1(%rdx),%edx
  cmp    %edx,%ebp
  jne    L176
  mov    %ebp,%edx
  sub    %esi,%edx
  cmp    %ebp,%edx
  jge    L110
  mov    %edx,%eax
  imul   %ebx,%eax
  cltq
  add    %r9,%rax
  add    %rdi,%rax
  nopl   0x0(%rax)
L139:
  mov    %edx,%esi
  movb   $0x1,(%rax)
  add    $0x1,%edx
  add    %r8,%rax
  cmp    %esi,%ecx
  jne    L139
L110:
  add    $0x1,%r9
  cmp    %r9,%r8
  jne    L9
L45:
  test   %ebp,%ebp
  jle    L199
  lea    -0x1(%rbp),%eax
  xor    %ecx,%ecx
  lea    0x4(%r12,%rax,4),%rsi
  xchg   %ax,%ax
L96:
  test   %ebx,%ebx
  jle    L78
  movslq %ecx,%rax
  xor    %edx,%edx
  add    %rdi,%rax
  jmp    L259
  xchg   %ax,%ax
L229:
  add    $0x1,%edx
  add    $0x1,%rax
  cmp    %ebx,%edx
  je     L80
L259:
  cmpb   $0x0,(%rax)
  jne    L229
L80:
  mov    %edx,(%r12)
  add    $0x4,%r12
  add    %ebx,%ecx
  cmp    %r12,%rsi
  jne    L96
L199:
  pop    %rbx
  pop    %rbp
  pop    %r12
  jmp    1060 <free@plt>
L78:
  xor    %edx,%edx
  add    $0x4,%r12
  add    %ebx,%ecx
  mov    %edx,-0x4(%r12)
  cmp    %r12,%rsi
  jne    L96
  jmp    L199
L201:
  mov    %ebx,%esi
  mov    $0x1,%edi
  imul   %ebp,%esi
  call   <calloc@plt>
  mov    %rax,%rdi
  cmp    $0x1,%ebp
  jne    L65
  jmp    L140
  nopl   0x0(%rax)
L112:
  mov    $0x1,%eax
  jmp    L74
```

assistant: ```c
void bead_sort(int *a, int len) {
  int i, j, max, sum;
  unsigned char *beads;
  max = a[0];
  for (i = 1; i < len; i++)
    if (a[i] > max)
      max = a[i];
  beads = calloc(1, max * len);
  for (i = 0; i < len; i++)
    for (j = 0; j < a[i]; j++)
      beads[i * max + j] = 1;
  for (j = 0; j < max; j++) {
    for (sum = i = 0; i < len; i++) {
      sum += beads[i * max + j];
      beads[i * max + j] = 0;
    }
    for (i = len - sum; i < len; i++)
      beads[i * max + j] = 1;
  }
  for (i = 0; i < len; i++) {
    for (j = 0; j < max && beads[i * max + j]; j++)
      ;
    a[i] = j;
  }
  free(beads);
}
```