// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/dos11.c
// Function: bitmapAllocContiguous
static uint16_t bitmapAllocContiguous(
  struct mountedFS *mount,
  uint16_t count
)
{
  struct DOS11data *data = &mount->dos11data;
  uint16_t map, offset, scan = data->bmscan;
  uint16_t i;
  uint8_t first = 1;

  /*
   * Check that the request is reasonable
   */
  if (count >= data->blocks)
    return 0;

 restart:
  /*
   * Make sure that there may be sufficient space to satisfy the request.
   */
  if (((data->blocks - 1) - scan) < count)
    return 0;

  map = scan / MAP_BLOCKS;
  offset = scan % MAP_BLOCKS;

  /*
   * Iterate across all available bitmaps.
   */
  while (map < data->bitmaps) {
    uint16_t wrd = offset / 16;

    if (bitmapLoad(mount, map) == 0)
      return 0;

    /*
     * Iterate across a single bitmap.
     */
    while (wrd < MAP_LEN) {
      uint16_t val = le16toh(data->bmbuf[MAP_BMSTART + wrd]);

      if (val != 0177777) {
        if (first) {
          /*
           * Update cache of where to start scanning
           */
          data->bmscan = (map * MAP_BLOCKS) + (wrd * 16);
          first = 0;
        }

        if (count <= 16) {
          /*
           * The required blocks could fit into a single. See if that is the
           * case.
           */
          uint16_t mask = lowbits[count - 1];

          for (i = 0; i < (17 - count); i++)
            if ((val & (mask << i)) == 0) {
              /*
               * We have a fit. Allocate the space.
               */
              val |= (mask << i);
              data->bmbuf[MAP_BMSTART + wrd] = htole16(val);
              data->bmdirty = 1;
              return (map * MAP_BLOCKS) + (wrd * 16) + i;
            }
        }

        /*
         * Let's see if we can make use of the last blocks described by
         * this word.
         */
        if ((val & 0100000) == 0) {
          uint16_t base = (map * MAP_BLOCKS) + (wrd * 16);

          /*
           * See how many bits we can use
           */
          for (i = 1; i <= 16; i++)
            if ((val & highbits[i]) != 0)
              break;

          if (bitmapScan(mount, count - i, base + 16, &scan) == 0)
            goto restart;

          base = base + 16 - i;

          /*
           * Allocate the blocks
           */
          for (i = 0; i < count; i++)
            if (bitmapSetBit(mount, base + i) == 0)
              return 0;
          
          return base;
        }
      }
      wrd++;
    }
    map++;
    offset = 0;
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movw %si, %ax
movq %rdi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movw 0x108(%rax), %ax
movw %ax, -0x26(%rbp)
movb $0x1, -0x29(%rbp)
movzwl -0x12(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl (%rcx), %eax
jb 0x7779
movw $0x0, -0x2(%rbp)
jmp 0x7a64
jmp 0x777b
movq -0x20(%rbp), %rax
movl (%rax), %eax
subl $0x1, %eax
movzwl -0x26(%rbp), %ecx
subl %ecx, %eax
movzwl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jae 0x779d
movw $0x0, -0x2(%rbp)
jmp 0x7a64
movzwl -0x26(%rbp), %eax
movl $0x3c0, %ecx            # imm = 0x3C0
cltd
idivl %ecx
movw %ax, -0x22(%rbp)
movzwl -0x26(%rbp), %eax
movl $0x3c0, %ecx            # imm = 0x3C0
cltd
idivl %ecx
movw %dx, %ax
movw %ax, -0x24(%rbp)
movzwl -0x22(%rbp), %eax
movq -0x20(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jge 0x7a5e
movzwl -0x24(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movw %ax, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
movzwl -0x22(%rbp), %esi
callq 0xb0a0
cmpl $0x0, %eax
jne 0x7801
movw $0x0, -0x2(%rbp)
jmp 0x7a64
jmp 0x7803
movzwl -0x2c(%rbp), %eax
cmpl $0x3c, %eax
jge 0x7a47
movq -0x20(%rbp), %rax
movzwl -0x2c(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x10c(%rax,%rcx,2), %edi
callq 0x6c50
movw %ax, -0x2e(%rbp)
movzwl -0x2e(%rbp), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x7a36
cmpb $0x0, -0x29(%rbp)
je 0x7869
movzwl -0x22(%rbp), %eax
imull $0x3c0, %eax, %eax      # imm = 0x3C0
movzwl -0x2c(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x108(%rax)
movb $0x0, -0x29(%rbp)
movzwl -0x12(%rbp), %eax
cmpl $0x10, %eax
jg 0x793d
movzwl -0x12(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x16c49(%rip), %rax     # 0x1e4d0
movw (%rax,%rcx,2), %ax
movw %ax, -0x30(%rbp)
movw $0x0, -0x28(%rbp)
movzwl -0x28(%rbp), %eax
movzwl -0x12(%rbp), %edx
movl $0x11, %ecx
subl %edx, %ecx
cmpl %ecx, %eax
jge 0x793b
movzwl -0x2e(%rbp), %eax
movzwl -0x30(%rbp), %edx
movzwl -0x28(%rbp), %ecx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x7928
movzwl -0x30(%rbp), %eax
movzwl -0x28(%rbp), %ecx
shll %cl, %eax
movl %eax, %ecx
movzwl -0x2e(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x2e(%rbp)
movzwl -0x2e(%rbp), %edi
callq 0x6c50
movw %ax, %dx
movq -0x20(%rbp), %rax
movzwl -0x2c(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw %dx, 0x10c(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movb $0x1, 0x10a(%rax)
movzwl -0x22(%rbp), %eax
imull $0x3c0, %eax, %eax      # imm = 0x3C0
movzwl -0x2c(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
movzwl -0x28(%rbp), %ecx
addl %ecx, %eax
movw %ax, -0x2(%rbp)
jmp 0x7a64
jmp 0x792a
movw -0x28(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x28(%rbp)
jmp 0x7895
jmp 0x793d
movzwl -0x2e(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
jne 0x7a34
movzwl -0x22(%rbp), %eax
imull $0x3c0, %eax, %eax      # imm = 0x3C0
movzwl -0x2c(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
movw %ax, -0x32(%rbp)
movw $0x1, -0x28(%rbp)
movzwl -0x28(%rbp), %eax
cmpl $0x10, %eax
jg 0x79a3
movzwl -0x2e(%rbp), %eax
movzwl -0x28(%rbp), %ecx
movl %ecx, %edx
leaq 0x16b6a(%rip), %rcx     # 0x1e4f0
movzwl (%rcx,%rdx,2), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x7993
jmp 0x79a3
jmp 0x7995
movw -0x28(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x28(%rbp)
jmp 0x796c
movq -0x10(%rbp), %rdi
movzwl -0x12(%rbp), %eax
movzwl -0x28(%rbp), %ecx
subl %ecx, %eax
movw %ax, %dx
movzwl -0x32(%rbp), %eax
addl $0x10, %eax
leaq -0x26(%rbp), %rcx
movzwl %dx, %esi
movzwl %ax, %edx
callq 0xb200
cmpl $0x0, %eax
jne 0x79d4
jmp 0x777b
movzwl -0x32(%rbp), %eax
addl $0x10, %eax
movzwl -0x28(%rbp), %ecx
subl %ecx, %eax
movw %ax, -0x32(%rbp)
movw $0x0, -0x28(%rbp)
movzwl -0x28(%rbp), %eax
movzwl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jge 0x7a2a
movq -0x10(%rbp), %rdi
movzwl -0x32(%rbp), %eax
movzwl -0x28(%rbp), %ecx
addl %ecx, %eax
movzwl %ax, %esi
callq 0xb2e0
cmpl $0x0, %eax
jne 0x7a1a
movw $0x0, -0x2(%rbp)
jmp 0x7a64
jmp 0x7a1c
movw -0x28(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x28(%rbp)
jmp 0x79eb
movw -0x32(%rbp), %ax
movw %ax, -0x2(%rbp)
jmp 0x7a64
jmp 0x7a36
movw -0x2c(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x2c(%rbp)
jmp 0x7803
movw -0x22(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x22(%rbp)
movw $0x0, -0x24(%rbp)
jmp 0x77c0
movw $0x0, -0x2(%rbp)
movw -0x2(%rbp), %ax
addq $0x40, %rsp
popq %rbp
retq
nop