user: Converte this assembly snippet into its equivalent C code:

```asm
<stable_grail_build_blocks>:
  endbr64
  push   %r15
  mov    %r8d,%eax
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  mov    %rdi,%rbx
  sub    $0x38,%rsp
  cmp    %edx,%r8d
  mov    %esi,0xc(%rsp)
  cmovg  %edx,%eax
  mov    %edx,0x10(%rsp)
  mov    %rcx,0x18(%rsp)
  nopl   0x0(%rax,%rax,1)
L22:
  lea    -0x1(%rax),%edx
  mov    %eax,%ecx
  and    %edx,%eax
  jne    L22
  mov    %ecx,0x14(%rsp)
  test   %ecx,%ecx
  jne    L30
  cmpl   $0x1,0xc(%rsp)
  lea    -0x10(%rbx),%r14
  jle    L85
  mov    0xc(%rsp),%eax
  mov    %r14,%rbp
  mov    $0x1,%r13d
  lea    -0x2(%rax),%r12d
  and    $0xfffffffe,%r12d
  add    $0x3,%r12d
  nopl   0x0(%rax)
L150:
  mov    0x10(%rbp),%rdx
  mov    0x18(%rbp),%rax
  xor    %r15d,%r15d
  mov    %rbp,%rdi
  mov    (%rax),%eax
  cmp    %eax,(%rdx)
  setg   %r15b
  lea    -0x1(%r13,%r15,1),%eax
  cltq
  lea    (%rbx,%rax,8),%rsi
  xor    %eax,%eax
  call   <stable_grail_swap1@plt>
  mov    %r13d,%eax
  lea    0x8(%rbp),%rdi
  add    $0x2,%r13d
  sub    %r15d,%eax
  add    $0x10,%rbp
  cltq
  lea    (%rbx,%rax,8),%rsi
  xor    %eax,%eax
  call   <stable_grail_swap1@plt>
  cmp    %r12d,%r13d
  jne    L150
L85:
  testb  $0x1,0xc(%rsp)
  jne    L308
L260:
  mov    %r14,(%rsp)
  mov    $0x2,%ebx
L113:
  cmp    0x10(%rsp),%ebx
  jge    L14
  nopw   0x0(%rax,%rax,1)
L288:
  mov    0xc(%rsp),%eax
  mov    %ebx,%r15d
  add    %ebx,%ebx
  sub    %ebx,%eax
  mov    %eax,0x8(%rsp)
  js     L338
  mov    %r15d,%r13d
  movslq %ebx,%rbp
  mov    (%rsp),%r12
  xor    %r14d,%r14d
  neg    %r13d
  shl    $0x3,%rbp
  nopl   0x0(%rax)
L183:
  mov    %r12,%rdi
  mov    %r13d,%ecx
  mov    %r15d,%edx
  mov    %r15d,%esi
  xor    %eax,%eax
  add    %ebx,%r14d
  add    %rbp,%r12
  call   <stable_grail_merge_left@plt>
  cmp    %r14d,0x8(%rsp)
  jge    L183
  mov    0xc(%rsp),%edx
  movslq %r14d,%rax
  sub    %r14d,%edx
L43:
  movslq %r15d,%rbp
  cmp    %edx,%r15d
  jge    L351
  mov    (%rsp),%rdi
  mov    %r15d,%ecx
  sub    %r15d,%edx
  mov    %r15d,%esi
  neg    %ecx
  lea    (%rdi,%rax,8),%rdi
  xor    %eax,%eax
  call   <stable_grail_merge_left@plt>
L129:
  shl    $0x3,%rbp
  sub    %rbp,(%rsp)
  cmp    0x10(%rsp),%ebx
  jl     L288
L14:
  mov    0xc(%rsp),%eax
  mov    0x10(%rsp),%ecx
  cltd
  lea    (%rcx,%rcx,1),%r12d
  idiv   %r12d
  mov    0xc(%rsp),%eax
  sub    %edx,%eax
  mov    %edx,%esi
  movslq %eax,%rbp
  mov    (%rsp),%rax
  mov    %rbp,%rbx
  lea    (%rax,%rbp,8),%rdi
  cmp    %edx,%ecx
  jl     L321
  mov    %ecx,%edx
  xor    %eax,%eax
  call   <stable_grail_rotate@plt>
L226:
  test   %ebx,%ebx
  jle    L298
  movslq %r12d,%rax
  mov    %r12d,%r13d
  mov    0x10(%rsp),%r14d
  sub    %rax,%rbp
  neg    %r13d
  mov    (%rsp),%rax
  movslq %r13d,%r13
  shl    $0x3,%r13
  lea    (%rax,%rbp,8),%rbp
  nopl   (%rax)
L29:
  mov    %rbp,%rdi
  mov    %r14d,%ecx
  mov    %r14d,%edx
  mov    %r14d,%esi
  xor    %eax,%eax
  sub    %r12d,%ebx
  add    %r13,%rbp
  call   <stable_grail_merge_right@plt>
  test   %ebx,%ebx
  jg     L29
L298:
  add    $0x38,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   0x0(%rax)
L351:
  mov    (%rsp),%rcx
  sub    %rbp,%rax
  mov    %r15d,%esi
  lea    (%rcx,%rax,8),%rdi
  xor    %eax,%eax
  call   <stable_grail_rotate@plt>
  jmp    L129
  nopw   0x0(%rax,%rax,1)
L338:
  mov    0xc(%rsp),%edx
  xor    %eax,%eax
  jmp    L43
L308:
  movslq 0xc(%rsp),%rax
  shl    $0x3,%rax
  lea    -0x18(%rbx,%rax,1),%rsi
  lea    -0x8(%rbx,%rax,1),%rdi
  xor    %eax,%eax
  call   <stable_grail_swap1@plt>
  jmp    L260
L321:
  sub    %ecx,%esi
  xor    %eax,%eax
  mov    %esi,%edx
  mov    %ecx,%esi
  call   <stable_grail_merge_right@plt>
  jmp    L226
L30:
  movslq 0x14(%rsp),%rax
  mov    0x18(%rsp),%rdi
  mov    %rbx,%rsi
  lea    0x0(,%rax,8),%rdx
  sub    %rdx,%rsi
  mov    %rdx,0x28(%rsp)
  call   <memcpy@plt>
  cmpl   $0x1,0xc(%rsp)
  mov    0xc(%rsp),%esi
  mov    $0x1,%eax
  jle    L174
  xchg   %ax,%ax
L232:
  mov    -0x8(%rbx,%rax,8),%rcx
  mov    (%rbx,%rax,8),%rdx
  mov    (%rdx),%edi
  cmp    %edi,(%rcx)
  setg   %cl
  movzbl %cl,%ecx
  lea    -0x1(%rcx,%rax,1),%edx
  movslq %edx,%rdx
  mov    (%rbx,%rdx,8),%rdx
  mov    %rdx,-0x18(%rbx,%rax,8)
  mov    %eax,%edx
  sub    %ecx,%edx
  movslq %edx,%rdx
  mov    (%rbx,%rdx,8),%rdx
  mov    %rdx,-0x10(%rbx,%rax,8)
  add    $0x2,%rax
  cmp    %eax,%esi
  jg     L232
L174:
  movslq 0xc(%rsp),%rax
  mov    %rax,%rdi
  shl    $0x3,%rax
  and    $0x1,%edi
  mov    %rax,0x20(%rsp)
  jne    L52
L242:
  lea    -0x10(%rbx),%rax
  cmpl   $0x2,0x14(%rsp)
  mov    $0x2,%ebx
  mov    %rax,(%rsp)
  jle    L244
  nopl   (%rax)
L149:
  mov    0xc(%rsp),%eax
  mov    %ebx,%r14d
  add    %ebx,%ebx
  sub    %ebx,%eax
  mov    %eax,0x8(%rsp)
  js     L224
  mov    %r14d,%r13d
  movslq %ebx,%rbp
  mov    (%rsp),%r12
  xor    %r15d,%r15d
  neg    %r13d
  shl    $0x3,%rbp
  mov    %r13d,%eax
  mov    %r14d,%r13d
  mov    %eax,%r14d
  nopw   0x0(%rax,%rax,1)
L163:
  mov    %r12,%rdi
  mov    %r14d,%ecx
  mov    %r13d,%edx
  mov    %r13d,%esi
  xor    %eax,%eax
  add    %ebx,%r15d
  add    %rbp,%r12
  call   <stable_grail_merge_left_with_x_buf@plt>
  cmp    %r15d,0x8(%rsp)
  jge    L163
  mov    0xc(%rsp),%edx
  mov    %r13d,%r14d
  sub    %r15d,%edx
L218:
  cmp    %edx,%r14d
  jge    L208
  mov    (%rsp),%rax
  mov    %r14d,%ecx
  movslq %r15d,%r8
  sub    %r14d,%edx
  neg    %ecx
  mov    %r14d,%esi
  lea    (%rax,%r8,8),%rdi
  xor    %eax,%eax
  call   <stable_grail_merge_left_with_x_buf@plt>
L313:
  movslq %r14d,%rdx
L64:
  shl    $0x3,%rdx
  sub    %rdx,(%rsp)
  cmp    0x14(%rsp),%ebx
  jl     L149
L244:
  mov    0x28(%rsp),%rdx
  mov    0x18(%rsp),%rsi
  mov    0x20(%rsp),%rdi
  add    (%rsp),%rdi
  call   <memcpy@plt>
  jmp    L113
  cs nopw 0x0(%rax,%rax,1)
L208:
  cmp    %r15d,0xc(%rsp)
  jle    L313
  mov    0xc(%rsp),%r9d
  movslq %r15d,%rdi
  movslq %r14d,%rdx
  mov    %rdi,%rcx
  lea    0x8(,%rdi,8),%rax
  sub    %r15d,%r9d
  sub    %rdx,%rcx
  lea    -0x1(%r9),%r10d
  shl    $0x3,%rcx
  cmp    $0x2,%r10d
  jbe    L8
  cmp    %rcx,%rax
  je     L8
  mov    (%rsp),%rdi
  lea    -0x8(%rdi,%rax,1),%r10
  add    %rdi,%rcx
  mov    %r9d,%edi
  xor    %eax,%eax
  shr    %edi
  shl    $0x4,%rdi
  nopl   (%rax)
L168:
  movdqu (%r10,%rax,1),%xmm0
  movups %xmm0,(%rcx,%rax,1)
  add    $0x10,%rax
  cmp    %rax,%rdi
  jne    L168
  mov    %r9d,%eax
  and    $0xfffffffe,%eax
  lea    (%rax,%r15,1),%r8d
  cmp    %r9d,%eax
  je     L64
  mov    (%rsp),%rsi
  movslq %r8d,%rax
  sub    %r14d,%r8d
  movslq %r8d,%r8
  mov    (%rsi,%rax,8),%rax
  mov    %rax,(%rsi,%r8,8)
  jmp    L64
L224:
  mov    0xc(%rsp),%edx
  xor    %r15d,%r15d
  jmp    L218
L8:
  mov    0xc(%rsp),%ecx
  mov    (%rsp),%rsi
  sub    $0x1,%ecx
  lea    -0x8(%rsi,%rax,1),%rax
  sub    %r15d,%ecx
  add    %rdi,%rcx
  lea    0x8(%rsi,%rcx,8),%rdi
  mov    %rdx,%rsi
  neg    %rsi
  shl    $0x3,%rsi
  nopl   0x0(%rax)
L154:
  mov    (%rax),%rcx
  add    $0x8,%rax
  mov    %rcx,-0x8(%rax,%rsi,1)
  cmp    %rdi,%rax
  jne    L154
  jmp    L64
L52:
  mov    %rax,%rsi
  mov    -0x8(%rbx,%rax,1),%rax
  mov    %rax,-0x18(%rbx,%rsi,1)
  jmp    L242
```

assistant: ```c
void stable_grail_build_blocks(int **arr, int L, int K, int **extbuf, int LExtBuf) {
  int m, u, h, p0, p1, rest, restk, p, kbuf;
  kbuf = K < LExtBuf ? K : LExtBuf;
  while (kbuf & (kbuf - 1)) {
    kbuf &= kbuf - 1;
  }
  if (kbuf) {
    memcpy((extbuf), (arr - kbuf), (kbuf) * sizeof(int *));
    for (m = 1; m < L; m += 2) {
      u = 0;
      if ((*(*(arr + (m - 1))) - *(*(arr + m))) > 0) {
        u = 1;
      }
      arr[m - 3] = arr[m - 1 + u];
      arr[m - 2] = arr[m - u];
    }
    if (L % 2) {
      arr[L - 3] = arr[L - 1];
    }
    arr -= 2;
    for (h = 2; h < kbuf; h *= 2) {
      p0 = 0;
      p1 = L - 2 * h;
      while (p0 <= p1) {
        stable_grail_merge_left_with_x_buf(arr + p0, h, h, -h);
        p0 += 2 * h;
      }
      rest = L - p0;
      if (rest > h) {
        stable_grail_merge_left_with_x_buf(arr + p0, h, rest - h, -h);
      } else {
        for (; p0 < L; p0++) {
          arr[p0 - h] = arr[p0];
        }
      }
      arr -= h;
    }
    memcpy((arr + L), (extbuf), (kbuf) * sizeof(int *));
  } else {
    for (m = 1; m < L; m += 2) {
      u = 0;
      if ((*(*(arr + (m - 1))) - *(*(arr + m))) > 0) {
        u = 1;
      }
      stable_grail_swap1(arr + (m - 3), arr + (m - 1 + u));
      stable_grail_swap1(arr + (m - 2), arr + (m - u));
    }
    if (L % 2) {
      stable_grail_swap1(arr + (L - 1), arr + (L - 3));
    }
    arr -= 2;
    h = 2;
  }
  for (; h < K; h *= 2) {
    p0 = 0;
    p1 = L - 2 * h;
    while (p0 <= p1) {
      stable_grail_merge_left(arr + p0, h, h, -h);
      p0 += 2 * h;
    }
    rest = L - p0;
    if (rest > h) {
      stable_grail_merge_left(arr + p0, h, rest - h, -h);
    } else {
      stable_grail_rotate(arr + p0 - h, h, rest);
    }
    arr -= h;
  }
  restk = L % (2 * K);
  p = L - restk;
  if (restk <= K) {
    stable_grail_rotate(arr + p, restk, K);
  } else {
    stable_grail_merge_right(arr + p, K, restk - K, K);
  }
  while (p > 0) {
    p -= 2 * K;
    stable_grail_merge_right(arr + p, K, K, K);
  }
}
```