user: 请从给定的汇编指令推导出C源代码:

```asm
<conservativeHeadRichardsMualemVanGenuchtenHetEvaluateV2withUpwindAndHarm_sd>:
  endbr64
  push   %r15
  movapd %xmm0,%xmm6
  movslq %r8d,%rax
  movapd %xmm0,%xmm2
  push   %r14
  mulsd  %xmm0,%xmm6
  push   %r13
  push   %r12
  mov    %esi,%r12d
  mov    %eax,%esi
  push   %rbp
  imul   %eax,%esi
  push   %rbx
  mov    %edx,%ebx
  sub    $0xf8,%rsp
  mov    0x130(%rsp),%r10
  mov    %eax,0xc(%rsp)
  mov    %r9d,0x6c(%rsp)
  mov    0x138(%rsp),%r8
  mov    (%r10,%rax,4),%eax
  mov    %edi,0x64(%rsp)
  mov    %ecx,0x30(%rsp)
  mov    0x158(%rsp),%r9
  mov    %esi,0xe0(%rsp)
  mov    0x190(%rsp),%rbp
  mov    %eax,0x60(%rsp)
  mov    0x198(%rsp),%r13
  movsd  %xmm1,(%rsp)
  movsd  %xmm6,0xd0(%rsp)
  test   %edi,%edi
  jle    L153
  mov    %r12d,%edi
  xor    %eax,%eax
  mov    %r11d,0x10(%rsp)
  movsd  %xmm0,0x18(%rsp)
  call   <assert@plt>
  movsd  0x18(%rsp),%xmm2
  mov    0x158(%rsp),%r9
  mov    0x138(%rsp),%r8
  mov    0x130(%rsp),%r10
  mov    0x10(%rsp),%r11d
L153:
  test   %ebx,%ebx
  jle    L172
  mov    0x150(%rsp),%rax
  mov    0x30(%rsp),%edi
  movl   $0x0,0xcc(%rsp)
  mov    %r10,%r15
  movl   $0x0,0x68(%rsp)
  mov    %rax,0xa0(%rsp)
  mov    0x60(%rsp),%eax
  movl   $0x0,0xbc(%rsp)
  imul   %edi,%eax
  mov    %r8,0x138(%rsp)
  movl   $0x0,0xb8(%rsp)
  mov    %r9,0x158(%rsp)
  mov    %eax,0xc8(%rsp)
  lea    -0x1(%rbx),%eax
  mov    0x150(%rsp),%rbx
  mov    %rbp,0x190(%rsp)
  lea    0x4(%rbx,%rax,4),%rax
  mov    %edi,%ebx
  mov    %r13,0x198(%rsp)
  mov    %rax,0xc0(%rsp)
  mov    %edi,%eax
  and    $0xfffffffe,%ebx
  shr    %eax
  mov    %ebx,0xe4(%rsp)
  sub    $0x1,%eax
  movsd  %xmm2,0x40(%rsp)
  mov    %rax,0xd8(%rsp)
  mov    0x150(%rsp),%rax
  mov    %rax,0x98(%rsp)
L238:
  mov    0x98(%rsp),%rax
  mov    (%rax),%ecx
  mov    0x30(%rsp),%eax
  test   %eax,%eax
  jle    L11
  cmp    $0x1,%eax
  je     L351
  mov    0xd8(%rsp),%rbx
  movslq 0x68(%rsp),%rsi
  pxor   %xmm6,%xmm6
  mov    0x1a0(%rsp),%rax
  movsd  %xmm6,0x38(%rsp)
  movapd %xmm6,%xmm2
  lea    0x1(%rbx),%rdx
  lea    (%rax,%rsi,8),%rax
  shl    $0x4,%rdx
  add    %rax,%rdx
L312:
  movsd  (%rax),%xmm0
  add    $0x10,%rax
  addsd  %xmm0,%xmm2
  movsd  -0x8(%rax),%xmm0
  addsd  %xmm0,%xmm2
  cmp    %rax,%rdx
  jne    L312
  mov    0xe4(%rsp),%eax
  cmp    %eax,0x30(%rsp)
  je     L345
L297:
  mov    0x1a0(%rsp),%rbx
  add    0x68(%rsp),%eax
  cltq
  addsd  (%rbx,%rax,8),%xmm2
L345:
  test   %r12d,%r12d
  je     L376
  movslq 0xbc(%rsp),%rax
  mov    0x1c8(%rsp),%rbx
  movq   $0x0,(%rbx,%rax,8)
  mov    0x1d0(%rsp),%rbx
  movq   $0x0,(%rbx,%rax,8)
L376:
  mov    0x168(%rsp),%rbx
  mov    0x170(%rsp),%r14
  movslq %ecx,%rax
  shl    $0x3,%rsi
  mov    0x178(%rsp),%r13
  shl    $0x3,%rax
  mov    0xcc(%rsp),%ebp
  movsd  %xmm2,0xe8(%rsp)
  add    %rax,%rbx
  add    %rax,%r14
  add    %rax,%r13
  add    0x160(%rsp),%rax
  mov    %rbx,0xa8(%rsp)
  xor    %ebx,%ebx
  mov    %rax,0xb0(%rsp)
  mov    0x188(%rsp),%rax
  add    %rsi,%rax
  mov    %rax,0x58(%rsp)
  mov    0x1a8(%rsp),%rax
  add    %rsi,%rax
  mov    %rax,0x50(%rsp)
  mov    0x1b0(%rsp),%rax
  add    %rsi,%rax
  add    0x1a0(%rsp),%rsi
  mov    %rax,0x48(%rsp)
  mov    %rbx,%rax
  mov    %ebp,%ebx
  mov    %rsi,0x70(%rsp)
  mov    %rax,%rbp
  jmp    L256
  nopw   0x0(%rax,%rax,1)
L243:
  mov    D48(%rip),%rax
  addsd  %xmm9,%xmm3
  movsd  %xmm5,0x10(%rsp)
  movq   %rax,%xmm1
  mov    D1(%rip),%rax
  movq   %rax,%xmm4
L99:
  mulsd  (%rsp),%xmm2
  movsd  %xmm1,0x28(%rsp)
  movsd  %xmm4,0x20(%rsp)
  movsd  %xmm3,0x18(%rsp)
  movapd %xmm2,%xmm0
  call   <exp@plt>
  mulsd  0x40(%rsp),%xmm0
  movsd  0x18(%rsp),%xmm3
  mov    0x50(%rsp),%rax
  mov    (%r15),%r11d
  movapd %xmm3,%xmm2
  movsd  0x20(%rsp),%xmm4
  movsd  0x28(%rsp),%xmm1
  lea    (%rbx,%r11,1),%ecx
  movslq %ecx,%rcx
  mulsd  %xmm0,%xmm2
  movsd  %xmm2,(%rax,%rbp,8)
  movsd  0x10(%rsp),%xmm2
  mov    0x48(%rsp),%rax
  mulsd  %xmm0,%xmm2
  mulsd  (%rsp),%xmm0
  mulsd  %xmm3,%xmm0
  subsd  %xmm2,%xmm0
  movsd  %xmm0,(%rax,%rbp,8)
  mov    0x1e8(%rsp),%rax
  movsd  %xmm4,(%rax,%rcx,8)
  mov    0x1f0(%rsp),%rax
  movsd  %xmm1,(%rax,%rcx,8)
  test   %r12d,%r12d
  je     L326
  mov    0xb8(%rsp),%eax
  mov    0x1c8(%rsp),%rsi
  lea    (%rax,%r11,1),%ecx
  mov    0x70(%rsp),%rax
  movslq %ecx,%rcx
  mulsd  (%rax,%rbp,8),%xmm4
  shl    $0x3,%rcx
  add    %rcx,%rsi
  add    0x1d0(%rsp),%rcx
  addsd  (%rsi),%xmm4
  movsd  %xmm4,(%rsi)
  mulsd  (%rax,%rbp,8),%xmm1
  addsd  (%rcx),%xmm1
  movsd  %xmm1,(%rcx)
L326:
  add    $0x1,%rbp
  add    0x60(%rsp),%ebx
  cmp    %ebp,0x30(%rsp)
  jle    L0
L256:
  mov    0x58(%rsp),%rax
  movsd  0x38(%rsp),%xmm5
  movsd  (%r14),%xmm3
  movsd  0x0(%r13),%xmm9
  movsd  (%rax,%rbp,8),%xmm2
  comisd %xmm2,%xmm5
  jbe    L243
  mov    0xa8(%rsp),%rax
  movapd %xmm2,%xmm5
  xorpd  D48(%rip),%xmm5
  movsd  %xmm9,0x90(%rsp)
  movsd  %xmm3,0x88(%rsp)
  movsd  %xmm2,0x80(%rsp)
  movsd  (%rax),%xmm12
  mov    0xb0(%rsp),%rax
  movsd  (%rax),%xmm4
  movapd %xmm12,%xmm1
  movsd  %xmm12,0x18(%rsp)
  subsd  D67(%rip),%xmm1
  mulsd  %xmm4,%xmm5
  movsd  %xmm4,0x78(%rsp)
  movapd %xmm5,%xmm0
  movsd  %xmm5,0x10(%rsp)
  call   <pow@plt>
  movsd  0x10(%rsp),%xmm5
  movsd  0x18(%rsp),%xmm12
  movsd  %xmm0,0x28(%rsp)
  movsd  D1(%rip),%xmm1
  movapd %xmm5,%xmm10
  mulsd  %xmm0,%xmm10
  movsd  D1(%rip),%xmm0
  divsd  %xmm12,%xmm0
  mulsd  %xmm10,%xmm5
  movsd  %xmm10,0x20(%rsp)
  addsd  D1(%rip),%xmm5
  movsd  %xmm5,0x10(%rsp)
  subsd  %xmm0,%xmm1
  xorpd  D48(%rip),%xmm1
  movapd %xmm5,%xmm0
  call   <pow@plt>
  movsd  0x10(%rsp),%xmm5
  movsd  0x18(%rsp),%xmm12
  movapd %xmm0,%xmm1
  movsd  0x78(%rsp),%xmm4
  movsd  D1(%rip),%xmm8
  divsd  %xmm5,%xmm1
  movsd  0x20(%rsp),%xmm10
  movsd  0x28(%rsp),%xmm11
  subsd  %xmm12,%xmm8
  subsd  D1(%rip),%xmm12
  movsd  0x90(%rsp),%xmm9
  movsd  D1(%rip),%xmm6
  movsd  0x88(%rsp),%xmm3
  movsd  0x80(%rsp),%xmm2
  mulsd  %xmm4,%xmm8
  xorpd  D48(%rip),%xmm4
  mulsd  %xmm12,%xmm4
  mulsd  %xmm11,%xmm4
  movapd %xmm4,%xmm5
  movsd  0x38(%rsp),%xmm4
  mulsd  %xmm0,%xmm5
  ucomisd %xmm0,%xmm4
  mulsd  %xmm1,%xmm8
  movapd %xmm10,%xmm1
  mulsd  %xmm0,%xmm1
  mulsd  %xmm10,%xmm8
  subsd  %xmm1,%xmm6
  movapd %xmm9,%xmm1
  mulsd  %xmm0,%xmm1
  movapd %xmm6,%xmm7
  mulsd  %xmm8,%xmm10
  mulsd  %xmm8,%xmm9
  mulsd  %xmm6,%xmm7
  addsd  %xmm1,%xmm3
  subsd  %xmm10,%xmm5
  movsd  %xmm9,0x10(%rsp)
  ja     L280
  sqrtsd %xmm0,%xmm0
L52:
  movsd  D149(%rip),%xmm1
  movapd %xmm7,%xmm4
  mulsd  %xmm0,%xmm4
  divsd  %xmm0,%xmm1
  addsd  %xmm0,%xmm0
  mulsd  %xmm6,%xmm0
  mulsd  %xmm5,%xmm0
  mulsd  %xmm8,%xmm1
  mulsd  %xmm7,%xmm1
  addsd  %xmm0,%xmm1
  xorpd  D48(%rip),%xmm1
  jmp    L99
L0:
  movsd  0xe8(%rsp),%xmm2
  test   %r12d,%r12d
  je     L108
L250:
  mov    0xb8(%rsp),%eax
  mov    0x1c8(%rsp),%rdx
  add    %r11d,%eax
  cltq
  shl    $0x3,%rax
  add    %rax,%rdx
  add    0x1d0(%rsp),%rax
  movsd  (%rdx),%xmm0
  divsd  %xmm2,%xmm0
  movsd  %xmm0,(%rdx)
  movsd  (%rax),%xmm0
  divsd  %xmm2,%xmm0
  movsd  %xmm0,(%rax)
L108:
  addq   $0x4,0x98(%rsp)
  mov    0x60(%rsp),%edi
  mov    0x98(%rsp),%rax
  add    %edi,0xb8(%rsp)
  mov    0xe0(%rsp),%edi
  add    %edi,0xbc(%rsp)
  mov    0x30(%rsp),%edi
  add    %edi,0x68(%rsp)
  mov    0xc8(%rsp),%edi
  add    %edi,0xcc(%rsp)
  cmp    %rax,0xc0(%rsp)
  jne    L238
  mov    0x30(%rsp),%edi
  mov    0x138(%rsp),%r8
  mov    %r15,%r10
  movsd  0x40(%rsp),%xmm2
  mov    0x158(%rsp),%r9
  mov    0x190(%rsp),%rbp
  mov    0x198(%rsp),%r13
  test   %edi,%edi
  jle    L172
  movslq %edi,%rax
  mov    0xc(%rsp),%ebx
  mov    (%r15),%esi
  pxor   %xmm4,%xmm4
  shl    $0x2,%rax
  movl   $0x0,0x50(%rsp)
  movsd  D112(%rip),%xmm5
  mov    %rax,0x70(%rsp)
  lea    -0x1(%rbx),%r12d
  mov    0x148(%rsp),%rax
  movsd  0xd0(%rsp),%xmm6
  mov    %esi,0x48(%rsp)
  mov    %rax,0x58(%rsp)
  mov    %edi,%eax
  imul   %ebx,%eax
  movl   $0x0,0x68(%rsp)
  mov    %eax,0xb8(%rsp)
  movslq 0x60(%rsp),%rax
  shl    $0x3,%rax
  mov    %rax,0x28(%rsp)
  mov    %ebx,%eax
  shr    %eax
  sub    $0x1,%eax
  add    $0x1,%rax
  shl    $0x4,%rax
  mov    %rax,0x38(%rsp)
  mov    %ebx,%eax
  and    $0xfffffffe,%eax
  mov    %eax,0x40(%rsp)
  lea    -0x1(%rdi),%eax
  add    $0x1,%rax
  mov    %rax,0x78(%rsp)
  movslq %esi,%rax
  mov    %rax,0x30(%rsp)
L177:
  mov    0xa0(%rsp),%rax
  mov    0x180(%rsp),%rbx
  movslq 0x68(%rsp),%rsi
  mov    0x1e8(%rsp),%rdi
  movslq (%rax),%rax
  shl    $0x3,%rsi
  lea    (%rbx,%rax,8),%rax
  mov    0x78(%rsp),%rbx
  add    %rsi,%rdi
  add    0x1f0(%rsp),%rsi
  mov    %rax,0x18(%rsp)
  mov    0x58(%rsp),%rax
  lea    (%rax,%rbx,4),%rbx
  mov    %rax,(%rsp)
  mov    %rbx,0x20(%rsp)
  mov    0x50(%rsp),%ebx
  nopl   0x0(%rax,%rax,1)
L42:
  mov    (%rsp),%rax
  mov    0x18(%rsp),%rcx
  mov    (%rax),%eax
  movsd  (%rcx),%xmm3
  mov    0x140(%rsp),%rcx
  lea    (%rax,%rax,1),%edx
  movapd %xmm3,%xmm0
  movslq %edx,%r15
  mov    0x4(%rcx,%r15,4),%ecx
  test   %ecx,%ecx
  js     L200
  mov    0x150(%rsp),%r14
  movslq %ecx,%rdx
  mov    0x180(%rsp),%r11
  movslq (%r14,%rdx,4),%rdx
  movsd  (%r11,%rdx,8),%xmm0
L200:
  movapd %xmm3,%xmm1
  mov    0xc(%rsp),%edx
  addsd  %xmm3,%xmm1
  mulsd  %xmm0,%xmm1
  addsd  %xmm3,%xmm0
  addsd  %xmm5,%xmm0
  divsd  %xmm0,%xmm1
  test   %edx,%edx
  jle    L282
  imul   0x6c(%rsp),%eax
  movapd %xmm6,%xmm7
  movapd %xmm2,%xmm9
  mulsd  %xmm1,%xmm7
  mulsd  %xmm1,%xmm9
  imul   %edx,%eax
  cmp    $0x1,%edx
  je     L323
  movslq %ebx,%rdx
  mov    %eax,0x10(%rsp)
  movapd %xmm7,%xmm11
  movapd %xmm4,%xmm3
  lea    0x0(%rbp,%rdx,8),%r14
  movapd %xmm9,%xmm10
  movslq %eax,%rdx
  mov    0x38(%rsp),%rax
  lea    0x0(%r13,%rdx,8),%r11
  unpcklpd %xmm11,%xmm11
  unpcklpd %xmm10,%xmm10
  xor    %edx,%edx
  movapd %xmm4,%xmm8
  cs nopw 0x0(%rax,%rax,1)
L138:
  movupd (%r9,%rdx,1),%xmm0
  movupd (%r14,%rdx,1),%xmm12
  movupd (%r11,%rdx,1),%xmm13
  add    $0x10,%rdx
  mulpd  %xmm11,%xmm0
  mulpd  %xmm10,%xmm12
  subpd  %xmm12,%xmm0
  mulpd  %xmm13,%xmm0
  addsd  %xmm0,%xmm3
  unpckhpd %xmm0,%xmm0
  addsd  %xmm0,%xmm3
  cmp    %rax,%rdx
  jne    L138
  mov    0x40(%rsp),%edx
  mov    0x10(%rsp),%eax
  mov    %edx,%r11d
  cmp    %edx,0xc(%rsp)
  je     L249
L350:
  movslq %r11d,%rdx
  add    %r11d,%eax
  mulsd  (%r9,%rdx,8),%xmm7
  lea    (%rbx,%r11,1),%edx
  cltq
  movslq %edx,%rdx
  mulsd  0x0(%rbp,%rdx,8),%xmm9
  movapd %xmm7,%xmm0
  subsd  %xmm9,%xmm0
  mulsd  0x0(%r13,%rax,8),%xmm0
  addsd  %xmm0,%xmm3
L249:
  comisd %xmm8,%xmm3
  jb     L287
L282:
  mov    0x140(%rsp),%rax
  mov    0x64(%rsp),%r11d
  mov    (%rax,%r15,4),%ecx
  mov    0x30(%rsp),%rax
  movsd  (%rdi,%rax,8),%xmm3
  movsd  (%rsi,%rax,8),%xmm7
  test   %r11d,%r11d
  jle    L97
L357:
  imul   0x60(%rsp),%ecx
  mov    0x48(%rsp),%eax
  add    %ecx,%eax
  mov    0x1c8(%rsp),%rcx
  cltq
  movsd  (%rcx,%rax,8),%xmm3
L97:
  mov    0xc(%rsp),%edx
  test   %edx,%edx
  jle    L244
L329:
  movapd %xmm6,%xmm9
  movapd %xmm6,%xmm8
  movslq %ebx,%r14
  xor    %edx,%edx
  mulsd  %xmm3,%xmm9
  mov    0x1d8(%rsp),%r15
  shl    $0x3,%r14
  mulsd  %xmm7,%xmm8
  add    %r14,%r15
  add    0x1e0(%rsp),%r14
  mulsd  %xmm1,%xmm9
  mulsd  %xmm1,%xmm8
  nopl   0x0(%rax)
L384:
  movsd  (%r9,%rdx,8),%xmm0
  movslq (%r10,%rdx,4),%rax
  mov    0x4(%r10,%rdx,4),%ecx
  mulsd  %xmm9,%xmm0
  movsd  %xmm0,(%r15,%rdx,8)
  movsd  (%r9,%rdx,8),%xmm0
  mulsd  %xmm8,%xmm0
  movsd  %xmm0,(%r14,%rdx,8)
  cmp    %ecx,%eax
  jge    L404
  nop
L24:
  cmp    %edx,(%r8,%rax,4)
  je     L308
  movq   $0x0,(%rdi,%rax,8)
  movq   $0x0,(%rsi,%rax,8)
  add    $0x1,%rax
  cmp    %eax,%ecx
  jg     L24
L404:
  lea    0x1(%rdx),%rax
  cmp    %rdx,%r12
  je     L244
L83:
  mov    %rax,%rdx
  jmp    L384
  nopl   0x0(%rax)
L308:
  movapd %xmm2,%xmm0
  mulsd  %xmm3,%xmm0
  mulsd  %xmm1,%xmm0
  movsd  %xmm0,(%rdi,%rax,8)
  movapd %xmm2,%xmm0
  mulsd  %xmm7,%xmm0
  mulsd  %xmm1,%xmm0
  movsd  %xmm0,(%rsi,%rax,8)
  add    $0x1,%rax
  cmp    %eax,%ecx
  jg     L24
  lea    0x1(%rdx),%rax
  cmp    %rdx,%r12
  jne    L83
L244:
  mov    0x28(%rsp),%rcx
  addq   $0x4,(%rsp)
  add    0xc(%rsp),%ebx
  mov    (%rsp),%rax
  add    %rcx,%rdi
  add    %rcx,%rsi
  cmp    0x20(%rsp),%rax
  jne    L42
  addq   $0x4,0xa0(%rsp)
  mov    0xc8(%rsp),%edi
  mov    0xa0(%rsp),%rax
  add    %edi,0x68(%rsp)
  mov    0x70(%rsp),%rdi
  add    %rdi,0x58(%rsp)
  mov    0xb8(%rsp),%edi
  add    %edi,0x50(%rsp)
  cmp    %rax,0xc0(%rsp)
  jne    L177
L172:
  add    $0xf8,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L287:
  test   %ecx,%ecx
  js     L282
  mov    0x30(%rsp),%rax
  movsd  (%rdi,%rax,8),%xmm3
  movsd  (%rsi,%rax,8),%xmm7
  mov    0x64(%rsp),%eax
  test   %eax,%eax
  jg     L357
  jmp    L329
L323:
  movapd %xmm4,%xmm8
  movapd %xmm4,%xmm3
  xor    %r11d,%r11d
  jmp    L350
L11:
  test   %r12d,%r12d
  je     L108
  movslq 0xbc(%rsp),%rax
  mov    0x1c8(%rsp),%rbx
  pxor   %xmm2,%xmm2
  movq   $0x0,(%rbx,%rax,8)
  mov    0x1d0(%rsp),%rbx
  movq   $0x0,(%rbx,%rax,8)
  jmp    L250
L351:
  pxor   %xmm6,%xmm6
  movslq 0x68(%rsp),%rsi
  xor    %eax,%eax
  movsd  %xmm6,0x38(%rsp)
  movapd %xmm6,%xmm2
  jmp    L297
L280:
  movsd  %xmm5,0x88(%rsp)
  movsd  %xmm7,0x80(%rsp)
  movsd  %xmm6,0x78(%rsp)
  movsd  %xmm8,0x28(%rsp)
  movsd  %xmm3,0x20(%rsp)
  movsd  %xmm2,0x18(%rsp)
  call   <sqrt@plt>
  movsd  0x78(%rsp),%xmm6
  movsd  0x88(%rsp),%xmm5
  movsd  0x80(%rsp),%xmm7
  movsd  0x28(%rsp),%xmm8
  movsd  0x20(%rsp),%xmm3
  movsd  0x18(%rsp),%xmm2
  jmp    L52
```

tool_calls: name: retrieve_data
arguments: data_tag: D48
type_infered: f32[4]
name: retrieve_data
arguments: data_tag: D1
type_infered: f64
name: retrieve_data
arguments: data_tag: D67
type_infered: f64
name: retrieve_data
arguments: data_tag: D149
type_infered: f64
name: retrieve_data
arguments: data_tag: D112
type_infered: byte[8]

tool: {"D48": [0.0,-0.0,0.0,0.0]}

tool: D1: 1

tool: D67: 2

tool: D149: 0.5

tool: {"D112": [0xa7,0x8e,0xa8,0x99,0xc2,0x57,0xf3,0x3a]}

assistant: ```c
void conservativeHeadRichardsMualemVanGenuchtenHetEvaluateV2withUpwindAndHarm_sd(const int upwindFlag, const int computeAverages, const int nSimplex, const int nPointsPerSimplex, const int nSpace, const int nQuadraturePoints_elementBoundary, const int *rowptr, const int *colind, const int *elementBoundaryElementsArray, const int *quadraturePointToElementBoundary, const int *materialTypes, const double rho, const double beta, const double *gravity, const double *alpha, const double *n_vg, const double *thetaR, const double *thetaSR, const double *KWs, const double *u, const double *gradu, const double *n_global, const double *dV, double *mass, double *dmass, double *f_avg, double *df_avg, double *a_avg, double *da_avg, double *f, double *df, double *a, double *da) {
  int i, j, k, I, matID, J, nnz;
  const int nSpace2 = nSpace * nSpace;
  register double psiC, pcBar, pcBar_n, pcBar_nM1, pcBar_nM2, onePlus_pcBar_n, sBar, sqrt_sBar, DsBar_DpsiC, thetaW, DthetaW_DpsiC, vBar, vBar2, DvBar_DpsiC, KWr, DKWr_DpsiC, rho2 = rho * rho, thetaS, rhom, drhom, m;
  int eN_upwind, ebN_global;
  register double drive, KWs_harm, KWs_left, KWs_right, krw_val, dkrw_val, vol;
  nnz = rowptr[nSpace];
  if (upwindFlag > 0)
    assert(computeAverages);
  for (i = 0; i < nSimplex; i++) {
    matID = materialTypes[i];
    vol = 0.0;
    for (j = 0; j < nPointsPerSimplex; j++)
      vol += dV[i * nPointsPerSimplex + j];
    if (computeAverages) {
      a_avg[i * nSpace2 + 0 * nSpace + 0] = 0.0;
      da_avg[i * nSpace2 + 0 * nSpace + 0] = 0.0;
    }
    for (j = 0; j < nPointsPerSimplex; j++) {
      k = i * nPointsPerSimplex + j;
      psiC = -u[k];
      m = 1.0 - 1.0 / n_vg[matID];
      thetaS = thetaR[matID] + thetaSR[matID];
      if (psiC > 0.0) {
        pcBar = alpha[matID] * psiC;
        pcBar_nM2 = pow(pcBar, n_vg[matID] - 2);
        pcBar_nM1 = pcBar_nM2 * pcBar;
        pcBar_n = pcBar_nM1 * pcBar;
        onePlus_pcBar_n = 1.0 + pcBar_n;
        sBar = pow(onePlus_pcBar_n, -m);
        DsBar_DpsiC = alpha[matID] * (1.0 - n_vg[matID]) * (sBar / onePlus_pcBar_n) * pcBar_nM1;
        vBar = 1.0 - pcBar_nM1 * sBar;
        vBar2 = vBar * vBar;
        DvBar_DpsiC = -alpha[matID] * (n_vg[matID] - 1.0) * pcBar_nM2 * sBar - pcBar_nM1 * DsBar_DpsiC;
        thetaW = thetaSR[matID] * sBar + thetaR[matID];
        DthetaW_DpsiC = thetaSR[matID] * DsBar_DpsiC;
        sqrt_sBar = sqrt(sBar);
        KWr = sqrt_sBar * vBar2;
        DKWr_DpsiC = ((0.5 / sqrt_sBar) * DsBar_DpsiC * vBar2 + 2.0 * sqrt_sBar * vBar * DvBar_DpsiC);
      } else {
        thetaW = thetaS;
        DthetaW_DpsiC = 0.0;
        KWr = 1.0;
        DKWr_DpsiC = 0.0;
      }
      rhom = rho * exp(beta * u[k]);
      drhom = beta * rhom;
      mass[k] = rhom * thetaW;
      dmass[k] = -rhom * DthetaW_DpsiC + drhom * thetaW;
      J = rowptr[0];
      a[i * nPointsPerSimplex * nnz + j * nnz + J] = KWr;
      da[i * nPointsPerSimplex * nnz + j * nnz + J] = -DKWr_DpsiC;
      if (computeAverages) {
        a_avg[i * nnz + J] += KWr * dV[k];
        da_avg[i * nnz + J] += -DKWr_DpsiC * dV[k];
      }
    }
    if (computeAverages) {
      a_avg[i * nnz + J] /= vol;
      da_avg[i * nnz + J] /= vol;
    }
  }
  for (i = 0; i < nSimplex; i++) {
    matID = materialTypes[i];
    for (j = 0; j < nPointsPerSimplex; j++) {
      k = i * nPointsPerSimplex + j;
      ebN_global = quadraturePointToElementBoundary[k];
      KWs_left = KWs[matID];
      KWs_right = KWs_left;
      if (elementBoundaryElementsArray[ebN_global * 2 + 1] >= 0)
        KWs_right = KWs[materialTypes[elementBoundaryElementsArray[ebN_global * 2 + 1]]];
      KWs_harm = 2.0 * KWs_left * KWs_right / (KWs_left + KWs_right + 1.0e-24);
      drive = 0.0;
      for (I = 0; I < nSpace; I++) {
        drive += (rho2 * KWs_harm * gravity[I] - rho * KWs_harm * gradu[k * nSpace + I]) * n_global[ebN_global * nQuadraturePoints_elementBoundary * nSpace + 0 * nSpace + I];
      }
      if (drive >= 0.0 || elementBoundaryElementsArray[ebN_global * 2 + 1] < 0)
        eN_upwind = elementBoundaryElementsArray[ebN_global * 2 + 0];
      else
        eN_upwind = elementBoundaryElementsArray[ebN_global * 2 + 1];
      J = rowptr[0];
      krw_val = a[i * nPointsPerSimplex * nnz + j * nnz + J];
      dkrw_val = da[i * nPointsPerSimplex * nnz + j * nnz + J];
      if (upwindFlag > 0) {
        krw_val = a_avg[eN_upwind * nnz + J];
      }
      for (I = 0; I < nSpace; I++) {
        f[k * nSpace + I] = rho2 * krw_val * KWs_harm * gravity[I];
        df[k * nSpace + I] = rho2 * dkrw_val * KWs_harm * gravity[I];
        for (J = rowptr[I]; J < rowptr[I + 1]; J++) {
          if (colind[J] == I) {
            a[i * nPointsPerSimplex * nnz + j * nnz + J] = rho * krw_val * KWs_harm;
            da[i * nPointsPerSimplex * nnz + j * nnz + J] = rho * dkrw_val * KWs_harm;
          } else {
            a[i * nPointsPerSimplex * nnz + j * nnz + J] = 0.0;
            da[i * nPointsPerSimplex * nnz + j * nnz + J] = 0.0;
          }
        }
      }
    }
  }
}
```