// File: tomfunkhouser[P]gaps/pkgs/jpeg/jmemmgr.c
// Function: free_pool
METHODDEF(void)
free_pool (j_common_ptr cinfo, int pool_id)
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  small_pool_ptr shdr_ptr;
  large_pool_ptr lhdr_ptr;
  size_t space_freed;

  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */

#ifdef MEM_STATS
  if (cinfo->err->trace_level > 1)
    print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
#endif

  /* If freeing IMAGE pool, close any virtual arrays first */
  if (pool_id == JPOOL_IMAGE) {
    jvirt_sarray_ptr sptr;
    jvirt_barray_ptr bptr;

    for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
      if (sptr->b_s_open) {	/* there may be no backing store */
	sptr->b_s_open = FALSE;	/* prevent recursive close if error */
	(*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
      }
    }
    mem->virt_sarray_list = NULL;
    for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
      if (bptr->b_s_open) {	/* there may be no backing store */
	bptr->b_s_open = FALSE;	/* prevent recursive close if error */
	(*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
      }
    }
    mem->virt_barray_list = NULL;
  }

  /* Release large objects */
  lhdr_ptr = mem->large_list[pool_id];
  mem->large_list[pool_id] = NULL;

  while (lhdr_ptr != NULL) {
    large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
    space_freed = lhdr_ptr->hdr.bytes_used +
		  lhdr_ptr->hdr.bytes_left +
		  SIZEOF(large_pool_hdr);
    jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
    mem->total_space_allocated -= space_freed;
    lhdr_ptr = next_lhdr_ptr;
  }

  /* Release small objects */
  shdr_ptr = mem->small_list[pool_id];
  mem->small_list[pool_id] = NULL;

  while (shdr_ptr != NULL) {
    small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
    space_freed = shdr_ptr->hdr.bytes_used +
		  shdr_ptr->hdr.bytes_left +
		  SIZEOF(small_pool_hdr);
    jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
    mem->total_space_allocated -= space_freed;
    shdr_ptr = next_shdr_ptr;
  }
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movl %esi, 0x4c(%rsp)
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0x4c(%rsp)
jl 0xfad29
cmpl $0x2, 0x4c(%rsp)
jl 0xfad59
movq 0x50(%rsp), %rax
movq (%rax), %rax
movl $0xe, 0x28(%rax)
movl 0x4c(%rsp), %ecx
movq 0x50(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x50(%rsp), %rdi
callq *%rax
cmpl $0x1, 0x4c(%rsp)
jne 0xfae3a
movq 0x40(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0xfadbf
movq 0x20(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
je 0xfadad
movq 0x20(%rsp), %rax
movl $0x0, 0x2c(%rax)
movq 0x20(%rsp), %rax
movq 0x48(%rax), %rax
movq 0x50(%rsp), %rdi
movq 0x20(%rsp), %rsi
addq $0x38, %rsi
callq *%rax
jmp 0xfadaf
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0xfad75
movq 0x40(%rsp), %rax
movq $0x0, 0x88(%rax)
movq 0x40(%rsp), %rax
movq 0x90(%rax), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
je 0xfae2a
movq 0x18(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
je 0xfae18
movq 0x18(%rsp), %rax
movl $0x0, 0x2c(%rax)
movq 0x18(%rsp), %rax
movq 0x48(%rax), %rax
movq 0x50(%rsp), %rdi
movq 0x18(%rsp), %rsi
addq $0x38, %rsi
callq *%rax
jmp 0xfae1a
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0xfade0
movq 0x40(%rsp), %rax
movq $0x0, 0x90(%rax)
movq 0x40(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movq 0x78(%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
movq 0x40(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movq $0x0, 0x78(%rax,%rcx,8)
cmpq $0x0, 0x30(%rsp)
je 0xfaecc
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rsp), %rcx
addq 0x10(%rcx), %rax
addq $0x18, %rax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0xfb480
movq 0x28(%rsp), %rdx
movq 0x40(%rsp), %rax
movq 0x98(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x98(%rax)
movq 0x10(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0xfae61
movq 0x40(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movq 0x68(%rax,%rcx,8), %rax
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movq $0x0, 0x68(%rax,%rcx,8)
cmpq $0x0, 0x38(%rsp)
je 0xfaf5e
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rsp), %rcx
addq 0x10(%rcx), %rax
addq $0x18, %rax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0xfb430
movq 0x28(%rsp), %rdx
movq 0x40(%rsp), %rax
movq 0x98(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x98(%rax)
movq 0x8(%rsp), %rax
movq %rax, 0x38(%rsp)
jmp 0xfaef3
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)