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

```asm
<build_tree>:
  endbr64
  mov    %edx,%eax
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x1,%eax
  je     L143
  mov    %eax,%ecx
  mov    %rsi,%r8
  lea    0x1(%rdx),%ebp
  mov    %edx,%r9d
  lea    (%rsi,%rcx,4),%r11
  jmp    L70
  nopl   0x0(%rax,%rax,1)
L8:
  mov    %ebx,%r9d
L70:
  test   %r9d,%r9d
  jne    L119
  lea    -0x2(%rdx),%ecx
  mov    %rcx,%r10
  shl    $0x2,%rcx
  lea    (%r8,%rcx,1),%rsi
L96:
  sub    $0x1,%edx
  mov    %r9d,%ebx
  mov    %eax,(%rdi,%rdx,4)
  mov    %eax,(%rdi,%rcx,1)
  mov    (%rsi),%esi
  mov    (%r8,%rdx,4),%ecx
  mov    %r10d,%edx
L112:
  lea    (%rcx,%rsi,1),%r12d
  movzwl (%r11),%r10d
  and    $0xff000000,%ecx
  and    $0xff000000,%esi
  and    $0xff0000,%r12d
  or     %r12d,%r10d
  cmp    %esi,%ecx
  cmovb  %esi,%ecx
  lea    0x1000000(%r10,%rcx,1),%ecx
  mov    %ecx,(%r11)
  sub    $0x1,%eax
  je     L20
  sub    $0x4,%r11
  sub    $0x1,%ebp
  test   %r9d,%r9d
  jne    L8
L87:
  mov    %edx,%esi
  lea    -0x2(%rdx),%ecx
  sub    $0x1,%esi
  mov    %rcx,%rdx
  mov    %eax,(%rdi,%rsi,4)
  mov    %eax,(%rdi,%rcx,4)
  mov    (%r8,%rsi,4),%r9d
  mov    %eax,%esi
  mov    (%r8,%rcx,4),%ecx
  lea    (%r8,%rsi,4),%r10
  movzwl (%r10),%r11d
  lea    (%rcx,%r9,1),%esi
  and    $0xff000000,%ecx
  and    $0xff000000,%r9d
  and    $0xff0000,%esi
  or     %r11d,%esi
  cmp    %r9d,%ecx
  cmovb  %r9d,%ecx
  lea    0x1000000(%rsi,%rcx,1),%ecx
  mov    %ecx,(%r10)
  sub    $0x1,%eax
  jne    L87
L20:
  xor    %edi,%edi
  cmp    $0x2,%edx
  sete   %dil
  call   <assert@plt>
  xor    %edi,%edi
  test   %ebx,%ebx
  sete   %dil
  call   <assert@plt>
  pop    %rbx
  mov    $0x1,%edi
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  jmp    1050 <assert@plt>
  xchg   %ax,%ax
L119:
  lea    -0x1(%r9),%ecx
  mov    %rcx,%rbx
  lea    (%r8,%rcx,4),%r12
  cmp    %ebp,%edx
  jbe    L34
  lea    -0x2(%rdx),%ecx
  mov    (%r12),%r14d
  mov    %rcx,%r10
  shl    $0x2,%rcx
  lea    (%r8,%rcx,1),%rsi
  cmp    %r14d,(%rsi)
  jl     L96
L34:
  cmp    %ebp,%edx
  jae    L71
  lea    -0x2(%r9),%ecx
  mov    (%r8,%rcx,4),%esi
  mov    %rcx,%rbx
L42:
  mov    (%r12),%ecx
  jmp    L112
  nopw   0x0(%rax,%rax,1)
L71:
  lea    -0x1(%rdx),%ecx
  mov    %rcx,%r10
  lea    (%r8,%rcx,4),%r13
  cmp    $0x1,%r9d
  je     L128
  lea    -0x2(%r9),%esi
  mov    %rsi,%r14
  mov    (%r8,%rsi,4),%esi
  cmp    0x0(%r13),%esi
  jg     L128
  mov    %r14d,%ebx
  jmp    L42
  xchg   %ax,%ax
L128:
  mov    %eax,(%rdi,%rcx,4)
  mov    %r10d,%edx
  mov    0x0(%r13),%ecx
  mov    (%r12),%esi
  jmp    L112
  nopl   0x0(%rax,%rax,1)
L143:
  mov    %edx,%ebx
  jmp    L20
```

assistant: ```c
void build_tree(uint32_t *tree, uint64_t *weight, int32_t as) {
  unsigned r;
  unsigned s;
  unsigned t;
  uint64_t w1, w2;
  r = as;
  s = as;
  for (t = as - 1; t > 0; t--) {
    if (s < 1 || (r > t + 2 && weight[r - 2] < weight[s - 1])) {
      tree[r - 1] = t;
      tree[r - 2] = t;
      w1 = weight[r - 1];
      w2 = weight[r - 2];
      r -= 2;
    } else if (r < t + 2 || (s > 1 && weight[s - 2] <= weight[r - 1])) {
      w1 = weight[s - 1];
      w2 = weight[s - 2];
      s -= 2;
    } else {
      tree[r - 1] = t;
      w1 = weight[r - 1];
      w2 = weight[s - 1];
      s--;
      r--;
    }
    weight[t] = (weight[t] & 0xFFFF) + ((w1 + w2) & ~(uint64_t)0xFF00FFFF) + ((w1 & 0xFF000000) < (w2 & 0xFF000000) ? (w2 & 0xFF000000) : (w1 & 0xFF000000)) + 0x01000000;
  }
  assert(r == 2);
  assert(s == 0);
  assert(t == 0);
}
```