// File: /workspace/github_cpp_makefile_star2_O2/Alcaro[P]bridges/arlib/string.cpp
// Function: string::natcompare3(cstring, cstring, bool)
int string::natcompare3(cstring a, cstring b, bool case_insensitive)
{
	const uint8_t* ab = a.bytes().ptr();
	const uint8_t* bb = b.bytes().ptr();
	const uint8_t* abe = ab + a.length();
	const uint8_t* bbe = bb + b.length();
	
	int ret_zero = 0; // negative = 'a' is first
	int ret_case = 0;
	
	while (ab < abe && bb < bbe)
	{
		if (isdigit(*ab) && isdigit(*bb))
		{
			while (ab < abe && *ab == '0' &&
			       bb < bbe && *bb == '0')
			{
				ab++;
				bb++;
			}
			while (ab < abe && *ab == '0')
			{
				if (!ret_zero) ret_zero = 1;
				ab++;
			}
			while (bb < bbe && *bb == '0')
			{
				if (!ret_zero) ret_zero = -1;
				bb++;
			}
			
			const uint8_t* abs = ab;
			const uint8_t* bbs = bb;
			
			while (ab < abe && isdigit(*ab)) ab++;
			while (bb < bbe && isdigit(*bb)) bb++;
			
			if (ab-abs > bb-bbs) return 1; // a's digit sequence is longer -> a is greater
			if (ab-abs < bb-bbs) return -1;
			// same length - compare char by char (no leading zero, same length, so first difference wins)
			while (abs < ab)
			{
				if (*abs != *bbs) return *abs - *bbs;
				abs++;
				bbs++;
			}
		}
		else
		{
			uint8_t ac = *ab;
			uint8_t bc = *bb;
			if (ac != bc)
			{
				if (ret_case == 0) ret_case = ac - bc;
				if (case_insensitive) ac = toupper(ac);
				if (case_insensitive) bc = toupper(bc);
				if (ac != bc) return ac - bc;
			}
			ab++;
			bb++;
		}
	}
	
	if (bb < bbe) return -1;
	if (ab < abe) return 1;
	
	if (ret_zero) return ret_zero;
	if (ret_case) return ret_case;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq -0x10(%rsp), %rax
movq %rdi, (%rax)
movq %rsi, 0x8(%rax)
leaq -0x20(%rsp), %r9
movq %rdx, (%r9)
movq %rsi, %r10
shrq $0x38, %r10
movq %rcx, %r11
shrq $0x38, %r11
xorl $0xf, %r10d
testq %rsi, %rsi
cmovnsq %rax, %rdi
cmovsq %rsi, %r10
movl %r10d, %esi
addq %rdi, %rsi
xorl $0xf, %r11d
testq %rcx, %rcx
cmovnsq %r9, %rdx
cmovsq %rcx, %r11
movq %rcx, 0x8(%r9)
movl %r11d, %r9d
addq %rdx, %r9
xorl %ecx, %ecx
leaq 0x13589(%rip), %rax     # 0x28c90
xorl %ebp, %ebp
movq %rdx, %r14
movq %rdi, %rbx
cmpq %rsi, %rbx
jae 0x15840
cmpq %r9, %r14
jae 0x15840
movzbl (%rbx), %r13d
movb (%r13,%rax), %r11b
movb (%r14), %r15b
movzbl %r15b, %r10d
testb $0x40, %r11b
je 0x1573e
testb $0x40, (%r10,%rax)
jne 0x1579a
cmpb %r15b, %r13b
jne 0x15748
movl %ebp, %r12d
jmp 0x1577d
movl %r13d, %r12d
subl %r10d, %r12d
testl %ebp, %ebp
cmovnel %ebp, %r12d
testb %r8b, %r8b
je 0x15774
notb %r11b
orb $-0x21, %r11b
andb %r13b, %r11b
movb (%r10,%rax), %r10b
notb %r10b
orb $-0x21, %r10b
andb %r10b, %r15b
movl %r11d, %r13d
cmpb %r15b, %r13b
jne 0x15861
incq %rbx
incq %r14
incq %rdi
incq %rdx
movl %r12d, %ebp
jmp 0x1570f
incq %rbx
incq %r14
incq %rdi
incq %rdx
cmpq %rsi, %rbx
jae 0x157ba
cmpb $0x30, (%rbx)
jne 0x157ba
cmpq %r9, %r14
jae 0x157ba
cmpb $0x30, (%r14)
je 0x1578e
jmp 0x157ba
cmpl $0x1, %ecx
adcl $0x0, %ecx
incq %rbx
cmpq %rsi, %rbx
jae 0x157d5
cmpb $0x30, (%rbx)
je 0x157b1
jmp 0x157d5
cmpl $0x1, %ecx
movl $0x0, %edx
sbbl %edx, %edx
orl %edx, %ecx
incq %r14
cmpq %r9, %r14
jae 0x157e0
cmpb $0x30, (%r14)
je 0x157c6
xorl %r15d, %r15d
leaq (%rbx,%r15), %rdi
cmpq %rsi, %rdi
jae 0x157fa
movzbl (%rdi), %edx
testb $0x40, (%rdx,%rax)
je 0x157fa
incq %r15
jmp 0x157e3
xorl %r10d, %r10d
leaq (%r14,%r10), %rdx
cmpq %r9, %rdx
jae 0x15816
movzbl (%rdx), %r11d
testb $0x40, (%r11,%rax)
je 0x15816
incq %r10
jmp 0x157fd
cmpq %r10, %r15
jg 0x1587f
jl 0x15887
xorl %r10d, %r10d
leaq (%rbx,%r10), %r11
cmpq %rdi, %r11
jae 0x15709
movzbl (%r11), %r11d
movzbl (%r14,%r10), %r15d
cmpb %r15b, %r11b
jne 0x1586e
incq %r10
jmp 0x15820
xorl %r11d, %r11d
cmpq %r9, %r14
sbbl %r11d, %r11d
orl $0x1, %r11d
cmpq %r9, %r14
jb 0x15871
cmpq %rsi, %rbx
jb 0x15871
testl %ecx, %ecx
cmovnel %ecx, %ebp
movl %ebp, %r11d
jmp 0x15871
movzbl %r15b, %eax
movzbl %r13b, %r11d
subl %eax, %r11d
jmp 0x15871
subl %r15d, %r11d
movl %r11d, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %r11d
jmp 0x15871
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
jmp 0x15871
nop