// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%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 0x3bfa
movq %rax, %r14
movl $0x13fffb, %edi         # imm = 0x13FFFB
callq 0x30d0
testq %rax, %rax
je 0x3bfa
movq %rax, %r15
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 %r14, %rdi
movq 0x28(%rsp), %rcx
callq 0x3040
testl %eax, %eax
js 0x3c07
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subq %rax, %rbp
movq %r14, (%r13)
movq %r14, 0x8(%r13)
movq %r14, 0x10(%rsp)
leaq 0x40000(%r14), %rax
movq %rax, 0x10(%r13)
movq %r15, 0x18(%r13)
movq %r15, 0x20(%r13)
movq %r15, 0x30(%rsp)
addq $0x13fffb, %r15         # imm = 0x13FFFB
movq %r15, 0x28(%r13)
xorl %r14d, %r14d
movq %rbx, 0x18(%rsp)
cmpl $0x0, 0x30(%r13)
jne 0x3c27
movq %r13, %rdi
callq 0x53dc
testq %rbx, %rbx
je 0x3a8c
movl %eax, (%rbx)
xorl %ecx, %ecx
cmpl $-0x3, %eax
jne 0x3a9f
movq %r14, %r15
testb %cl, %cl
jne 0x3a72
jmp 0x3c15
movq %rbp, 0x8(%rsp)
movq 0x18(%r13), %rbp
movq 0x20(%r13), %rdi
subq %rdi, %rbp
cmpq $0x3fffc, %rbp          # imm = 0x3FFFC
jb 0x3b32
movq %r14, %rbx
addl $0xfffc0005, %ebp       # imm = 0xFFFC0005
movslq %ebp, %r14
movl $0x1, %esi
movq %r14, %rdx
movq 0x20(%rsp), %rcx
callq 0x3130
movq %rax, %r12
cmpl %r12d, %r14d
jne 0x3afc
addq %r14, %rbx
movq 0x20(%r13), %rdi
leaq (%rdi,%r14), %rsi
movl $0x3fffb, %edx          # imm = 0x3FFFB
callq 0x30f0
subq %r14, 0x18(%r13)
movq %rbx, %r14
jmp 0x3b1f
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x3b0c
movl $0x3, (%rax)
testl %r12d, %r12d
movl $0x0, %r15d
cmovgl %r12d, %r15d
movq %rbx, %r14
addq %rbx, %r15
movl $0x1, %ecx
cmpl %r12d, %ebp
movq 0x18(%rsp), %rbx
jne 0x3bc2
movq 0x8(%rsp), %rbp
movq (%r13), %rsi
movq 0x8(%r13), %rdi
movq 0x10(%r13), %r12
subq %rsi, %r12
cmpq %rdi, %rsi
je 0x3b60
movq %r12, %rdx
callq 0x30f0
movq 0x8(%r13), %rax
movq %rax, (%r13)
movl $0x1, %ecx
testl %ecx, %ecx
je 0x3be2
movb $0x1, %cl
cmpq $0x3ffff, %r12          # imm = 0x3FFFF
ja 0x3a96
testq %rbp, %rbp
je 0x3a96
movl $0x40000, %edx          # imm = 0x40000
subq %r12, %rdx
cmpq %rdx, %rbp
cmovbq %rbp, %rdx
movq 0x10(%rsp), %rax
leaq (%rax,%r12), %rdi
movl $0x1, %esi
movq 0x28(%rsp), %rcx
callq 0x3040
testl %eax, %eax
setns %cl
js 0x3bce
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subq %rax, %rbp
addq (%r13), %r12
addq %rax, %r12
movq %r12, 0x10(%r13)
jmp 0x3a96
xorl %ecx, %ecx
movq 0x8(%rsp), %rbp
jmp 0x3a96
testq %rbx, %rbx
je 0x3a93
movl $0x2, (%rbx)
jmp 0x3a93
xorl %edi, %edi
testq %rbx, %rbx
sete %dil
movq %rbx, %rsi
callq 0x327d
xorl %ecx, %ecx
jmp 0x3a93
testq %rbx, %rbx
je 0x3c12
movl $0x1, (%rbx)
jmp 0x3c12
testq %rbx, %rbx
je 0x3c12
movl $0x2, (%rbx)
xorl %r15d, %r15d
movq %r15, %rax
addq $0x38, %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 0x3c6a
movl $0x1, %esi
movq %r12, %rdx
movq 0x20(%rsp), %rcx
callq 0x3130
movslq %eax, %rcx
cmpq %r12, %rcx
je 0x3c65
testq %rbx, %rbx
je 0x3c59
movl $0x3, (%rbx)
xorl %r15d, %r15d
testl %eax, %eax
cmovgl %eax, %r15d
addq %r14, %r15
cmpq %r12, %rcx
jne 0x3c15
movq 0x30(%rsp), %rdi
callq 0x3030
movq 0x10(%rsp), %rdi
callq 0x3030
movq %r14, %r15
jmp 0x3c15