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

```asm
<goldensun>:
  endbr64
  push   %rbp
  mov    %rsp,%rbp
  push   %r15
  push   %r14
  push   %r13
  mov    %esi,%r13d
  push   %r12
  mov    %rdi,%r12
  push   %rbx
  sub    $0x28,%rsp
  mov    %rdx,-0x50(%rbp)
  mov    %ecx,-0x44(%rbp)
  mov    %fs:0x28,%rax
  mov    %rax,-0x38(%rbp)
  xor    %eax,%eax
  lea    -0x1000(%rsp),%rax
  cmp    %rax,%rsp
  je     L38
L55:
  sub    $0x1000,%rsp
  orq    $0x0,0xff8(%rsp)
  cmp    %rax,%rsp
  jne    L55
L38:
  mov    -0x44(%rbp),%eax
  mov    %rsp,%rdi
  test   %eax,%eax
  jle    L320
  xor    %r9d,%r9d
  xor    %r14d,%r14d
  xor    %eax,%eax
  xor    %r10d,%r10d
  xor    %r15d,%r15d
  nop
L223:
  cmp    $0x8,%r9d
  je     L325
  add    $0x1,%r9d
L242:
  movslq %r9d,%rdx
  cmpb   $0x0,-0x40(%rbp,%rdx,1)
  je     L229
  lea    0x1(%r10),%edx
  cmp    %r13d,%edx
  jge    L289
  movslq %r10d,%rcx
  movslq %edx,%rdx
  movzbl (%r12,%rcx,1),%r8d
  movzbl (%r12,%rdx,1),%edx
  lea    0x2(%r10),%ecx
  mov    %r8d,%esi
  shl    $0x4,%edx
  sar    $0x4,%r8d
  add    %r8d,%edx
  cmp    %r14d,%edx
  jg     L289
  and    $0xf,%esi
  je     L182
  cmp    $0x1,%sil
  je     L312
  lea    0x1000(%rax),%r11d
  movzbl %sil,%esi
  mov    %ecx,%r10d
  sub    %edx,%r11d
L114:
  movslq %r15d,%rbx
  xor    %edx,%edx
  add    -0x50(%rbp),%rbx
L189:
  lea    (%r11,%rdx,1),%ecx
  movslq %eax,%r8
  add    $0x1,%eax
  and    $0xfff,%ecx
  and    $0xfff,%eax
  movzbl (%rdi,%rcx,1),%ecx
  mov    %cl,(%rbx,%rdx,1)
  add    $0x1,%rdx
  mov    %cl,(%rdi,%r8,1)
  cmp    %edx,%esi
  jg     L189
  xor    %ebx,%ebx
  lea    -0x1(%rsi),%edx
  test   %esi,%esi
  cmovle %ebx,%edx
  add    %esi,%r14d
  lea    0x1(%r15,%rdx,1),%r15d
L317:
  cmp    %r14d,-0x44(%rbp)
  jg     L223
L157:
  cmp    %r10d,%r13d
  jg     L36
  nopl   (%rax)
L248:
  mov    -0x38(%rbp),%rax
  sub    %fs:0x28,%rax
  jne    L14
  lea    -0x28(%rbp),%rsp
  mov    %r15d,%eax
  pop    %rbx
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  pop    %rbp
  ret
  nopl   (%rax)
L229:
  cmp    %r13d,%r10d
  jge    L289
  movslq %r10d,%rdx
  mov    -0x50(%rbp),%rbx
  movslq %r15d,%rcx
  add    $0x1,%r14d
  movzbl (%r12,%rdx,1),%edx
  add    $0x1,%r10d
  add    $0x1,%r15d
  mov    %dl,(%rbx,%rcx,1)
  movslq %eax,%rcx
  add    $0x1,%eax
  mov    %dl,(%rdi,%rcx,1)
  and    $0xfff,%eax
  jmp    L317
  nop
L325:
  cmp    %r10d,%r13d
  jle    L289
  lea    0x1(%r10),%ecx
  movslq %r10d,%r10
  movzbl (%r12,%r10,1),%r9d
  mov    %r9d,%edx
  test   %r9d,%r9d
  je     L328
  lea    (%r9,%r9,1),%esi
  not    %edx
  mov    %esi,%r9d
  shr    $0x7,%dl
  and    $0xff,%r9d
  sete   %r8b
  or     %r8d,%edx
  mov    %dl,-0x40(%rbp)
  test   %r9d,%r9d
  je     L328
  lea    (%r9,%r9,1),%r8d
  and    $0x80,%esi
  mov    %r8d,%r9d
  sete   %dl
  and    $0xff,%r9d
  sete   %sil
  or     %esi,%edx
  mov    %dl,-0x3f(%rbp)
  test   %r9d,%r9d
  je     L328
  lea    (%r9,%r9,1),%esi
  and    $0x80,%r8d
  mov    %esi,%r9d
  sete   %dl
  and    $0xff,%r9d
  sete   %r8b
  or     %r8d,%edx
  mov    %dl,-0x3e(%rbp)
  test   %r9d,%r9d
  je     L328
  lea    (%r9,%r9,1),%r8d
  and    $0x80,%esi
  mov    %r8d,%r9d
  sete   %dl
  and    $0xff,%r9d
  sete   %sil
  or     %esi,%edx
  mov    %dl,-0x3d(%rbp)
  test   %r9d,%r9d
  je     L328
  lea    (%r9,%r9,1),%esi
  and    $0x80,%r8d
  mov    %esi,%r9d
  sete   %dl
  and    $0xff,%r9d
  sete   %r8b
  or     %r8d,%edx
  mov    %dl,-0x3c(%rbp)
  test   %r9d,%r9d
  je     L328
  lea    (%r9,%r9,1),%r8d
  and    $0x80,%esi
  mov    %r8d,%r9d
  sete   %dl
  and    $0xff,%r9d
  sete   %sil
  or     %esi,%edx
  mov    %dl,-0x3b(%rbp)
  test   %r9d,%r9d
  je     L328
  lea    (%r9,%r9,1),%esi
  and    $0x80,%r8d
  mov    %esi,%r9d
  sete   %dl
  and    $0xff,%r9d
  sete   %r8b
  or     %r8d,%edx
  mov    %dl,-0x3a(%rbp)
  test   %r9d,%r9d
  je     L328
  add    %r9d,%r9d
  mov    %ecx,%r10d
  and    $0xfe,%r9d
  sete   %dl
  and    $0x80,%esi
  sete   %sil
  xor    %r9d,%r9d
  or     %esi,%edx
  mov    %dl,-0x39(%rbp)
  jmp    L242
  nopw   0x0(%rax,%rax,1)
L312:
  lea    0x3(%r10),%esi
  cmp    %r13d,%esi
  jge    L289
  movslq %ecx,%rcx
  movslq %esi,%rsi
  add    $0x4,%r10d
  movzbl (%r12,%rcx,1),%ecx
  movzbl (%r12,%rsi,1),%esi
  shl    $0x8,%ecx
  lea    0x110(%rcx,%rsi,1),%esi
L15:
  lea    0x1000(%rax),%r11d
  sub    %edx,%r11d
  jmp    L114
  cs nopw 0x0(%rax,%rax,1)
L182:
  cmp    %ecx,%r13d
  jle    L289
  movslq %ecx,%rcx
  add    $0x3,%r10d
  movzbl (%r12,%rcx,1),%esi
  add    $0x10,%esi
  jmp    L15
  cs nopw 0x0(%rax,%rax,1)
L328:
  mov    %ecx,%r10d
  jmp    L242
L36:
  and    $0xfffffffc,%r10d
  lea    0x4(%r10),%eax
  cmp    %r13d,%eax
  jge    L248
L289:
  mov    $0xffffffff,%r15d
  jmp    L248
L320:
  xor    %r10d,%r10d
  xor    %r15d,%r15d
  jmp    L157
L14:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
int goldensun(unsigned char *instream, int inLength, unsigned char *outstream, int decompressedSize) {
  int i, o = 0;
  int readBytes = 0;
  int bufferLength = 0x1000;
  unsigned char buffer[bufferLength];
  int bufferOffset = 0;
  int currentOutSize = 0;
  int currentBlock = 0;
  unsigned char expandedFlags[8];
  while (currentOutSize < decompressedSize) {
    if (currentBlock == 8) {
      if (readBytes >= inLength)
        return (-1);
      int flags = instream[readBytes++];
      if (flags < 0)
        return (-1);
      int b = 0;
      while (flags > 0) {
        int bit = (flags & 0x80) > 0;
        flags = (flags & 0x7F) << 1;
        expandedFlags[b++] = (flags == 0) || !bit;
      }
      currentBlock = 0;
    } else {
      currentBlock++;
    }
    if (expandedFlags[currentBlock]) {
      if (readBytes + 1 >= inLength) {
        return (-1);
      }
      int byte1 = instream[readBytes++];
      int byte2 = instream[readBytes++];
      if (byte2 < 0)
        return (-1);
      int disp, length;
      disp = (byte1 >> 4) + (byte2 << 4);
      if (disp > currentOutSize)
        return (-1);
      switch (byte1 & 0x0F) {
      case 0: {
        if (readBytes >= inLength)
          return (-1);
        int byte3 = instream[readBytes++];
        if (byte3 < 0)
          return (-1);
        length = byte3 + 0x10;
        break;
      }
      case 1: {
        if (readBytes + 1 >= inLength)
          return (-1);
        int byte3 = instream[readBytes++];
        int byte4 = instream[readBytes++];
        if (byte4 < 0)
          return (-1);
        length = ((byte3 << 8) + byte4) + 0x110;
        break;
      }
      default: {
        length = byte1 & 0x0F;
        break;
      }
      }
      int bufIdx = bufferOffset + bufferLength - disp;
      for (i = 0; i < length; i++) {
        unsigned char next = buffer[bufIdx % bufferLength];
        bufIdx++;
        outstream[o++] = next;
        buffer[bufferOffset] = next;
        bufferOffset = (bufferOffset + 1) % bufferLength;
      }
      currentOutSize += length;
    } else {
      if (readBytes >= inLength)
        return (-1);
      int next = instream[readBytes++];
      if (next < 0)
        return (-1);
      currentOutSize++;
      outstream[o++] = next;
      buffer[bufferOffset] = (unsigned char)next;
      bufferOffset = (bufferOffset + 1) % bufferLength;
    }
  }
  if (readBytes < inLength) {
    if ((readBytes ^ (readBytes & 3)) + 4 < inLength)
      return (-1);
  }
  return (o);
}
```