// File: Alcaro[P]bridges/solver.cpp
// Function: (anonymous namespace)::solver<((anonymous namespace)::op_t)0>::count_bridges(unsigned short, unsigned char&, unsigned char&, bool&)
void count_bridges(uint16_t index, uint8_t& min, uint8_t& max, bool& has_doubles)
	{
		if (LIKELY((possibilities[index]&0xF000) == 0))
		{
			uint16_t flags = possibilities[index];
			
			//check how many bridges exit from each tile
			//keep only the lowest and highest bits
			// (the <<8s would be unneeded if '0 or 2 but not 1' was impossible, but I don't think it is)
			uint16_t flags_min = (flags & ~(flags<<4 | flags<<8));
			uint16_t flags_max = flags;
			
			min = popcount_mid(flags_min | flags_min>>4);
			max = popcount_mid(flags_max | flags_max>>4);
			has_doubles = (flags & (flags>>8));
			
			return;
		}
		
		min = 0;
		max = 0;
		has_doubles = false;
		
		uint16_t ni = index;
		do {
			uint16_t flags = possibilities[ni] & ~0xF000;
			
			//check how many bridges exit from each tile
			//keep only the lowest and highest bits
			uint16_t flags_min = (flags & ~(flags<<4 | flags<<8));
			uint16_t flags_max = flags;
			
			min += popcount_mid(flags_min | flags_min>>4);
			max += popcount_mid(flags_max | flags_max>>4);
			has_doubles |= (flags & (flags>>8));
			
			ni = nextjoined[ni];
		} while (ni != index);
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movw %si, %ax
movq %rdi, -0x68(%rbp)
movw %ax, -0x6a(%rbp)
movq %rdx, -0x78(%rbp)
movq %rcx, -0x80(%rbp)
movq %r8, -0x88(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq 0x4e28(%rax), %rax
movzwl -0x6a(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x0, %eax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x56134
jmp 0x56278
movq -0xa0(%rbp), %rcx
movq 0x4e28(%rcx), %rax
movzwl -0x6a(%rbp), %edx
movw (%rax,%rdx,2), %ax
movw %ax, -0x8a(%rbp)
movzwl -0x8a(%rbp), %eax
movzwl -0x8a(%rbp), %edx
shll $0x4, %edx
movzwl -0x8a(%rbp), %esi
shll $0x8, %esi
orl %esi, %edx
xorl $-0x1, %edx
andl %edx, %eax
movw %ax, -0x8c(%rbp)
movw -0x8a(%rbp), %ax
movw %ax, -0x8e(%rbp)
movzwl -0x8c(%rbp), %eax
movzwl -0x8c(%rbp), %edx
sarl $0x4, %edx
orl %edx, %eax
movq %rcx, -0x8(%rbp)
movw %ax, -0xa(%rbp)
movabsq $0x4332322132212110, %rax # imm = 0x4332322132212110
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movzwl -0xa(%rbp), %ecx
sarl $0x2, %ecx
andl $0x3c, %ecx
movl %ecx, %ecx
shrq %cl, %rax
movq -0x18(%rbp), %rdx
movzwl -0xa(%rbp), %ecx
sarl $0x6, %ecx
andl $0x3c, %ecx
movl %ecx, %ecx
shrq %cl, %rdx
movq -0xa0(%rbp), %rcx
addq %rdx, %rax
andq $0xf, %rax
movb %al, %dl
movq -0x78(%rbp), %rax
movb %dl, (%rax)
movzwl -0x8e(%rbp), %eax
movzwl -0x8e(%rbp), %edx
sarl $0x4, %edx
orl %edx, %eax
movq %rcx, -0x20(%rbp)
movw %ax, -0x22(%rbp)
movabsq $0x4332322132212110, %rax # imm = 0x4332322132212110
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movzwl -0x22(%rbp), %ecx
sarl $0x2, %ecx
andl $0x3c, %ecx
movl %ecx, %ecx
shrq %cl, %rax
movq -0x30(%rbp), %rdx
movzwl -0x22(%rbp), %ecx
sarl $0x6, %ecx
andl $0x3c, %ecx
movl %ecx, %ecx
shrq %cl, %rdx
movq %rdx, %rcx
addq %rcx, %rax
andq $0xf, %rax
movb %al, %cl
movq -0x80(%rbp), %rax
movb %cl, (%rax)
movzwl -0x8a(%rbp), %eax
movzwl -0x8a(%rbp), %ecx
sarl $0x8, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
setne %cl
movq -0x88(%rbp), %rax
andb $0x1, %cl
movb %cl, (%rax)
jmp 0x5642f
movq -0x78(%rbp), %rax
movb $0x0, (%rax)
movq -0x80(%rbp), %rax
movb $0x0, (%rax)
movq -0x88(%rbp), %rax
movb $0x0, (%rax)
movw -0x6a(%rbp), %ax
movw %ax, -0x90(%rbp)
movq -0xa0(%rbp), %rax
movq 0x4e28(%rax), %rcx
movzwl -0x90(%rbp), %edx
movzwl (%rcx,%rdx,2), %ecx
andl $0xffff0fff, %ecx       # imm = 0xFFFF0FFF
movw %cx, -0x92(%rbp)
movzwl -0x92(%rbp), %ecx
movzwl -0x92(%rbp), %edx
shll $0x4, %edx
movzwl -0x92(%rbp), %esi
shll $0x8, %esi
orl %esi, %edx
xorl $-0x1, %edx
andl %edx, %ecx
movw %cx, -0x94(%rbp)
movw -0x92(%rbp), %cx
movw %cx, -0x96(%rbp)
movzwl -0x94(%rbp), %ecx
movzwl -0x94(%rbp), %edx
sarl $0x4, %edx
orl %edx, %ecx
movq %rax, -0x38(%rbp)
movw %cx, -0x3a(%rbp)
movabsq $0x4332322132212110, %rcx # imm = 0x4332322132212110
movq %rcx, -0x48(%rbp)
movq -0x48(%rbp), %rdx
movzwl -0x3a(%rbp), %ecx
sarl $0x2, %ecx
andl $0x3c, %ecx
movl %ecx, %ecx
shrq %cl, %rdx
movq %rdx, -0xb0(%rbp)
movq -0x48(%rbp), %rdx
movzwl -0x3a(%rbp), %ecx
sarl $0x6, %ecx
andl $0x3c, %ecx
movl %ecx, %ecx
shrq %cl, %rdx
movq -0xb0(%rbp), %rcx
addq %rdx, %rcx
andq $0xf, %rcx
movl %ecx, %esi
movq -0x78(%rbp), %rcx
movzbl (%rcx), %edx
addl %esi, %edx
movb %dl, (%rcx)
movzwl -0x96(%rbp), %ecx
movzwl -0x96(%rbp), %edx
sarl $0x4, %edx
orl %edx, %ecx
movq %rax, -0x50(%rbp)
movw %cx, -0x52(%rbp)
movabsq $0x4332322132212110, %rcx # imm = 0x4332322132212110
movq %rcx, -0x60(%rbp)
movq -0x60(%rbp), %rdx
movzwl -0x52(%rbp), %ecx
sarl $0x2, %ecx
andl $0x3c, %ecx
movl %ecx, %ecx
shrq %cl, %rdx
movq %rdx, -0xa8(%rbp)
movq -0x60(%rbp), %rdx
movzwl -0x52(%rbp), %ecx
sarl $0x6, %ecx
andl $0x3c, %ecx
movl %ecx, %ecx
shrq %cl, %rdx
movq -0xa8(%rbp), %rcx
addq %rdx, %rcx
andq $0xf, %rcx
movl %ecx, %esi
movq -0x80(%rbp), %rcx
movzbl (%rcx), %edx
addl %esi, %edx
movb %dl, (%rcx)
movzwl -0x92(%rbp), %esi
movzwl -0x92(%rbp), %ecx
sarl $0x8, %ecx
andl %ecx, %esi
movq -0x88(%rbp), %rcx
movb (%rcx), %dl
andb $0x1, %dl
movzbl %dl, %edx
orl %esi, %edx
cmpl $0x0, %edx
setne %dl
andb $0x1, %dl
movb %dl, (%rcx)
movzwl -0x90(%rbp), %ecx
movw 0x8(%rax,%rcx,2), %ax
movw %ax, -0x90(%rbp)
movzwl -0x90(%rbp), %eax
movzwl -0x6a(%rbp), %ecx
cmpl %ecx, %eax
jne 0x5629b
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)