// File: /workspace/github_cpp_makefile_star2_O3/sbingner[P]kerneldec/lzfse/src/lzfse_decode.c
// Function: lzfse_decode_file_with_scratch
size_t lzfse_decode_file_with_scratch(FILE *dst_file, FILE *src_file, size_t src_size,
                                      int *error, void *__restrict scratch_buffer) {
  lzfse_decoder_state *s = (lzfse_decoder_state *)scratch_buffer;
  memset(s, 0x00, sizeof(*s));

  uint8_t *src_buf = malloc(DECODE_SRC_CHUNK_SIZE);
  if (src_buf == NULL) {
      if (error) *error = 1;
      return 0;
  }

  uint8_t *dst_buf = malloc(DECODE_DST_CHUNK_SIZE + LZFSE_ENCODE_MAX_D_VALUE);
  if (dst_buf == NULL) {
      if (error) *error = 1;
      return 0;
  }
  size_t dst_size = DECODE_DST_CHUNK_SIZE;

  int nr = fread(src_buf, 1, src_size?(src_size<DECODE_SRC_CHUNK_SIZE?src_size:DECODE_SRC_CHUNK_SIZE):DECODE_SRC_CHUNK_SIZE, src_file);
  if (nr < 0) {
      if (error) *error = 2;
      return 0;
  }
  src_size -= nr;

  // Initialize state
  s->src = src_buf;
  s->src_begin = src_buf;
  s->src_end = s->src + DECODE_SRC_CHUNK_SIZE;
  s->dst = dst_buf;
  s->dst_begin = dst_buf;
  s->dst_end = dst_buf + DECODE_DST_CHUNK_SIZE + LZFSE_ENCODE_MAX_D_VALUE;

  size_t written = 0;

  int status;
  while (s->end_of_stream == 0) {
      // Decode a block
      int status = lzfse_decode(s);
      if (error) *error = status;

      if (status == LZFSE_STATUS_ERROR) {
          return written;
      }

      int processed_data = 0;

      size_t dst_used = s->dst - s->dst_begin;
      if (dst_used > LZFSE_ENCODE_MAX_D_VALUE) {
          int num_to_write = dst_used - LZFSE_ENCODE_MAX_D_VALUE;
          processed_data = 1;
          int nw = fwrite(s->dst_begin, 1, num_to_write, dst_file);
          if (nw != num_to_write) {
              if (error) *error = 3;
              return written + (nw>0?nw:0);
          }
          written += num_to_write;
          memmove(s->dst_begin, s->dst_begin + num_to_write, LZFSE_ENCODE_MAX_D_VALUE);
          s->dst -= num_to_write;
      }

      size_t src_used = s->src - s->src_begin;
      size_t src_unused = s->src_end - s->src;
      if (src_used > 0) {
          processed_data = 1;
          memmove((uint8_t*)s->src_begin, s->src, src_unused);
          s->src = s->src_begin;
      }
      if (!processed_data) {
          if (error) *error = 4;
          fprintf(stderr, "lzfse not making progress, aborting.  Maybe we need larger buffers?\n");
          return written;
      }
      if (src_unused < DECODE_SRC_CHUNK_SIZE && src_size > 0) {
          size_t size_wanted = DECODE_SRC_CHUNK_SIZE - src_unused;
          size_t size_to_read = src_size?(src_size<size_wanted?src_size:size_wanted):size_wanted;
          nr = fread(src_buf + src_unused, 1, size_to_read, src_file);
          if (nr < 0) {
              if (error) *error = 2;
              return written;
          }
          src_size -= nr;
          s->src_end = s->src + src_unused + nr;
      }
  }

  size_t dst_used = s->dst - s->dst_begin;
  if (dst_used > 0) {
      int nw = fwrite(s->dst_begin, 1, dst_used, dst_file);
      if (nw != dst_used) {
          if (error) *error = 3;
          return written + (nw>0?nw:0);
      }
  }
  free(dst_buf);
  free(src_buf);

  return written; // bytes written
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, 0x8(%rsp)
movq %rdi, (%rsp)
movl $0xb920, %edx           # imm = 0xB920
movq %r8, %rdi
xorl %esi, %esi
callq 0x3080
movl $0x40000, %edi          # imm = 0x40000
callq 0x30d0
testq %rax, %rax
je 0x3b30
movq %rax, %r15
movl $0x13fffb, %edi         # imm = 0x13FFFB
callq 0x30d0
testq %rax, %rax
je 0x3b30
movq %rax, %r14
movl $0x40000, %eax          # imm = 0x40000
cmpq %rax, %rbp
movl $0x40000, %edx          # imm = 0x40000
cmovbq %rbp, %rdx
testq %rbp, %rbp
cmoveq %rax, %rdx
movl $0x1, %esi
movq %r15, %rdi
movq 0x8(%rsp), %rcx
callq 0x3040
testl %eax, %eax
js 0x3b3d
movq %r15, (%r13)
movq %r15, 0x8(%r13)
leaq 0x40000(%r15), %rcx
movq %rcx, 0x10(%r13)
movq %r14, 0x18(%r13)
movq %r14, 0x20(%r13)
movq %r14, 0x10(%rsp)
addq $0x13fffb, %r14         # imm = 0x13FFFB
movq %r14, 0x28(%r13)
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subq %rax, %rbp
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x532c
testq %rbx, %rbx
je 0x3a0b
movl %eax, (%rbx)
cmpl $-0x3, %eax
je 0x3b4b
movq 0x18(%r13), %rax
movq 0x20(%r13), %rdi
subq %rdi, %rax
cmpq $0x3fffc, %rax          # imm = 0x3FFFC
jae 0x3a41
movq (%r13), %rsi
movq 0x8(%r13), %rdi
cmpq %rdi, %rsi
je 0x3b81
movq 0x10(%r13), %r12
subq %rsi, %r12
jmp 0x3a90
addl $0xfffc0005, %eax       # imm = 0xFFFC0005
movslq %eax, %r12
movl $0x1, %esi
movq %r12, %rdx
movq (%rsp), %rcx
callq 0x3130
cmpl %eax, %r12d
jne 0x3b5d
addq %r12, %r14
movq 0x20(%r13), %rdi
leaq (%rdi,%r12), %rsi
movl $0x3fffb, %edx          # imm = 0x3FFFB
callq 0x30f0
subq %r12, 0x18(%r13)
movq (%r13), %rsi
movq 0x8(%r13), %rdi
movq 0x10(%r13), %r12
subq %rsi, %r12
cmpq %rdi, %rsi
je 0x3aa0
movq %r12, %rdx
callq 0x30f0
movq 0x8(%r13), %rax
movq %rax, (%r13)
cmpq $0x3ffff, %r12          # imm = 0x3FFFF
ja 0x3aeb
testq %rbp, %rbp
je 0x3aeb
movl $0x40000, %edx          # imm = 0x40000
subq %r12, %rdx
cmpq %rdx, %rbp
cmovbq %rbp, %rdx
leaq (%r15,%r12), %rdi
movl $0x1, %esi
movq 0x8(%rsp), %rcx
callq 0x3040
testl %eax, %eax
js 0x3b74
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subq %rax, %rbp
addq (%r13), %r12
addq %rax, %r12
movq %r12, 0x10(%r13)
cmpl $0x0, 0x30(%r13)
je 0x39fc
movq 0x18(%r13), %r12
movq 0x20(%r13), %rdi
subq %rdi, %r12
je 0x3b1c
movl $0x1, %esi
movq %r12, %rdx
movq (%rsp), %rcx
callq 0x3130
movslq %eax, %rcx
cmpq %r12, %rcx
jne 0x3b5d
movq 0x10(%rsp), %rdi
callq 0x3030
movq %r15, %rdi
callq 0x3030
jmp 0x3b4b
testq %rbx, %rbx
je 0x3b48
movl $0x1, (%rbx)
jmp 0x3b48
testq %rbx, %rbx
je 0x3b48
movl $0x2, (%rbx)
xorl %r14d, %r14d
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rbx, %rbx
je 0x3b68
movl $0x3, (%rbx)
xorl %ecx, %ecx
testl %eax, %eax
cmovgl %eax, %ecx
addq %rcx, %r14
jmp 0x3b4b
testq %rbx, %rbx
je 0x3b4b
movl $0x2, (%rbx)
jmp 0x3b4b
xorl %edi, %edi
testq %rbx, %rbx
sete %dil
movq %rbx, %rsi
callq 0x3271
jmp 0x3b4b