// File: /workspace/github_c_makefile_star2_O2/jflatow[P]jfdb/test/../src/db.h
// Function: JFDB_region_map_gaps
static inline void JFDB_region_map_gaps(JFDB_RegionMap *map, JFDB_Region *regions) {
  // initialize the counters and the min, clear the regions
  uint32_t block = 0, count = 0, size;
  JFDB_Region *minMax = &regions[0];
  memset(regions, 0, JFDB_NUM_GAPS * sizeof(JFDB_Region));
  for (size_t i = 0; i < map->size; i++) {
    uint64_t word = map->data[i];
    if (word == 0) {
      count += 64;
    } else if (word == -1) {
      // skip bits
    } else {
      for (int b = ffsll(word), pos = b; pos < 64; pos += (b = ffsll(word >> pos))) {
        if (b) {
          // found the next bit, add leading zeroes (end region)
          // position is right after the bit
          count += b - 1;
          // possibly find / replace min
          if ((size = count * JFDB_BLOCK_SIZE) > minMax->size) {
            minMax->size = size;
            minMax->block = block + map->begin;
            minMax = &regions[0];
            for (int j = 1; j < JFDB_NUM_GAPS; j++)
              if (regions[j].size < minMax->size)
                minMax = &regions[j];
          }
          // find the next zero bit, reset the count (begin region)
          // guaranteed to be a zero since we shifted at least one in
          // position right before the zero
          pos += ffsll(~(word >> pos)) - 1;
          block = 64 * i + pos;
          count = 0;
        } else {
          // add the trailing zeroes to the count
          count += 64 - pos;
          break;
        }
      }
    }
  }
  // possibly find / replace the min with the trailing region
  if ((size = count * JFDB_BLOCK_SIZE) > minMax->size) {
    minMax->size = size;
    minMax->block = block + map->begin;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsi)
movups %xmm0, 0x20(%rsi)
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
leaq 0x8(%rsi), %rdx
xorl %ebx, %ebx
movabsq $0x7, %r8
movq %rsi, %r9
xorl %r10d, %r10d
xorl %r11d, %r11d
movl 0x4(%rdi), %eax
cmpq %rax, %r10
jae 0x180e
movq 0x8(%rdi,%r10,8), %r14
cmpq $-0x1, %r14
je 0x1806
testq %r14, %r14
je 0x1803
bsfq %r14, %r12
incl %r12d
movl %r10d, %r15d
shll $0x6, %r15d
movl %r12d, %ecx
cmpl $0x3f, %ecx
jg 0x1806
testl %r12d, %r12d
je 0x1801
addl %r12d, %ebx
shll $0x8, %ebx
addl $0xffffff00, %ebx       # imm = 0xFFFFFF00
cmpl 0x4(%r9), %ebx
jbe 0x17cf
movl %ebx, 0x4(%r9)
addl (%rdi), %r11d
movl %r11d, (%r9)
movq %rdx, %rax
movq %r8, %r11
movq %rsi, %r9
subq $0x1, %r11
jb 0x17cf
movl 0x4(%rax), %ebp
movq %rax, %rbx
cmpl 0x4(%r9), %ebp
jb 0x17c6
movq %r9, %rbx
addq $0x8, %rax
movq %rbx, %r9
jmp 0x17b1
movq %r14, %rax
shrq %cl, %rax
notq %rax
bsfq %rax, %rax
addl %ecx, %eax
leal (%rax,%r15), %r11d
movq %r14, %r13
movl %eax, %ecx
shrq %cl, %r13
bsfq %r13, %r12
incl %r12d
xorl %ebx, %ebx
testq %r13, %r13
cmovel %ebx, %r12d
addl %r12d, %eax
movl %eax, %ecx
jmp 0x1782
subl %ecx, %ebx
addl $0x40, %ebx
incq %r10
jmp 0x174d
shll $0x8, %ebx
cmpl 0x4(%r9), %ebx
jbe 0x1821
movl %ebx, 0x4(%r9)
addl (%rdi), %r11d
movl %r11d, (%r9)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq