// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/dos11.c
// Function: bitmapAllocBlock
static uint16_t bitmapAllocBlock(
  struct mountedFS *mount
)
{
  struct DOS11data *data = &mount->dos11data;
  uint16_t map = data->bmscan / MAP_BLOCKS;
  uint16_t offset = data->bmscan % MAP_BLOCKS;

  /*
   * Iterate across all available bitmaps.
   */
  while (map < data->bitmaps) {
    uint16_t wrd = offset / 16;
    uint16_t bit = 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) {
        data->bmscan = (map * MAP_BLOCKS) + (wrd * 16);

        while (bit < 16) {
          if ((val & bits[bit]) == 0) {
            val |= bits[bit];
            data->bmbuf[MAP_BMSTART + wrd] = htole16(val);
            data->bmdirty = 1;
            return (map * MAP_BLOCKS) + (wrd * 16) + bit;
          }
          bit++;
        }
      }
      wrd++;
      bit = 0;
    }
    map++;
    offset = 0;
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x108(%rax), %eax
movl $0x3c0, %ecx            # imm = 0x3C0
cltd
idivl %ecx
movw %ax, -0x1a(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x108(%rax), %eax
movl $0x3c0, %ecx            # imm = 0x3C0
cltd
idivl %ecx
movw %dx, %ax
movw %ax, -0x1c(%rbp)
movzwl -0x1a(%rbp), %eax
movq -0x18(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jge 0x826c
movzwl -0x1c(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movw %ax, -0x1e(%rbp)
movzwl -0x1c(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movw %dx, %ax
movw %ax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movzwl -0x1a(%rbp), %esi
callq 0xb0a0
cmpl $0x0, %eax
jne 0x813d
movw $0x0, -0x2(%rbp)
jmp 0x8272
jmp 0x813f
movzwl -0x1e(%rbp), %eax
cmpl $0x3c, %eax
jge 0x8255
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x10c(%rax,%rcx,2), %edi
callq 0x6c50
movw %ax, -0x22(%rbp)
movzwl -0x22(%rbp), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x823e
movzwl -0x1a(%rbp), %eax
imull $0x3c0, %eax, %eax      # imm = 0x3C0
movzwl -0x1e(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x108(%rax)
movzwl -0x20(%rbp), %eax
cmpl $0x10, %eax
jge 0x823c
movzwl -0x22(%rbp), %eax
movzwl -0x20(%rbp), %ecx
movl %ecx, %edx
leaq 0x162f7(%rip), %rcx     # 0x1e4b0
movzwl (%rcx,%rdx,2), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x822b
movzwl -0x20(%rbp), %eax
movl %eax, %ecx
leaq 0x162df(%rip), %rax     # 0x1e4b0
movzwl (%rax,%rcx,2), %ecx
movzwl -0x22(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x22(%rbp)
movzwl -0x22(%rbp), %edi
callq 0x6c50
movw %ax, %dx
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw %dx, 0x10c(%rax,%rcx,2)
movq -0x18(%rbp), %rax
movb $0x1, 0x10a(%rax)
movzwl -0x1a(%rbp), %eax
imull $0x3c0, %eax, %eax      # imm = 0x3C0
movzwl -0x1e(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
movzwl -0x20(%rbp), %ecx
addl %ecx, %eax
movw %ax, -0x2(%rbp)
jmp 0x8272
movw -0x20(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x20(%rbp)
jmp 0x819b
jmp 0x823e
movw -0x1e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x1e(%rbp)
movw $0x0, -0x20(%rbp)
jmp 0x813f
movw -0x1a(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x1a(%rbp)
movw $0x0, -0x1c(%rbp)
jmp 0x80e9
movw $0x0, -0x2(%rbp)
movw -0x2(%rbp), %ax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)