// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, 0x20(%rsp)
movq %rdi, 0x18(%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 0x3a3f
movq %rax, %r15
movl $0x13fffb, %edi         # imm = 0x13FFFB
callq 0x30d0
testq %rax, %rax
je 0x3a3f
movq %rax, 0x10(%rsp)
movl $0x40000, %eax          # imm = 0x40000
cmpq %rax, %rbp
movl $0x40000, %edx          # imm = 0x40000
cmovbq %rbp, %rdx
testq %rbp, %rbp
cmoveq %rax, %rdx
pushq $0x1
popq %rsi
movq %r15, %rdi
movq 0x20(%rsp), %rcx
callq 0x3040
testl %eax, %eax
js 0x3a4c
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subq %rax, %rbp
movq %r15, (%r13)
movq %r15, 0x8(%r13)
leaq 0x40000(%r15), %rax
movq %rax, 0x10(%r13)
movq 0x10(%rsp), %rax
movq %rax, 0x18(%r13)
movq %rax, 0x20(%r13)
addq $0x13fffb, %rax         # imm = 0x13FFFB
movq %rax, 0x28(%r13)
xorl %eax, %eax
movq $0x0, 0x8(%rsp)
testl %eax, %eax
jne 0x3a68
movq %r13, %rdi
callq 0x4f10
testq %rbx, %rbx
je 0x395f
movl %eax, (%rbx)
cmpl $-0x3, %eax
je 0x3ac8
movq 0x18(%r13), %r14
movq 0x20(%r13), %rdi
subq %rdi, %r14
cmpq $0x3fffc, %r14          # imm = 0x3FFFC
jb 0x39ba
leal -0x3fffb(%r14), %eax
movslq %eax, %r12
pushq $0x1
popq %rsi
movq %r12, %rdx
movq 0x18(%rsp), %rcx
callq 0x3130
cmpl %eax, %r12d
jne 0x3aa1
addq %r12, 0x8(%rsp)
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 0x39e0
movq %r12, %rdx
callq 0x30f0
movq 0x8(%r13), %rax
movq %rax, (%r13)
jmp 0x39ed
cmpq $0x3fffb, %r14          # imm = 0x3FFFB
jbe 0x3acf
cmpq $0x3ffff, %r12          # imm = 0x3FFFF
ja 0x3a36
testq %rbp, %rbp
je 0x3a36
movl $0x40000, %edx          # imm = 0x40000
subq %r12, %rdx
cmpq %rdx, %rbp
cmovbq %rbp, %rdx
leaq (%r15,%r12), %rdi
pushq $0x1
popq %rsi
movq 0x20(%rsp), %rcx
callq 0x3040
testl %eax, %eax
js 0x3abd
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subq %rax, %rbp
addq (%r13), %r12
addq %rax, %r12
movq %r12, 0x10(%r13)
movl 0x30(%r13), %eax
jmp 0x3948
testq %rbx, %rbx
je 0x3a57
movl $0x1, (%rbx)
jmp 0x3a57
testq %rbx, %rbx
je 0x3a57
movl $0x2, (%rbx)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r13), %r12
movq 0x20(%r13), %rdi
subq %rdi, %r12
je 0x3a8d
pushq $0x1
popq %rsi
movq %r12, %rdx
movq 0x18(%rsp), %rcx
callq 0x3130
movslq %eax, %rcx
cmpq %r12, %rcx
jne 0x3aa1
movq 0x10(%rsp), %rdi
callq 0x3030
movq %r15, %rdi
callq 0x3030
jmp 0x3ac8
testq %rbx, %rbx
je 0x3aac
movl $0x3, (%rbx)
xorl %ecx, %ecx
testl %eax, %eax
cmovgl %eax, %ecx
movq 0x8(%rsp), %rax
addq %rcx, %rax
jmp 0x3a59
testq %rbx, %rbx
je 0x3ac8
movl $0x2, (%rbx)
movq 0x8(%rsp), %rax
jmp 0x3a59
testq %rbx, %rbx
je 0x3ada
movl $0x4, (%rbx)
movq 0x9517(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5a0d(%rip), %rdi      # 0x94f8
pushq $0x44
popq %rsi
pushq $0x1
popq %rdx
callq 0x3130
jmp 0x3ac8