// File: /workspace/github_cpp_makefile_star2_O1/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 %rcx, 0x8(%r9)
movzbl 0xf(%rax), %r10d
movl %r10d, %r11d
xorl $0xf, %r11d
testb %r10b, %r10b
cmovnsq %rax, %rdi
movzbl 0xf(%r9), %eax
cmovnsl %r11d, %esi
movl %eax, %r10d
xorl $0xf, %r10d
testb %al, %al
cmovnsq %r9, %rdx
cmovnsl %r10d, %ecx
testl %ecx, %ecx
setne %r14b
xorl %r9d, %r9d
testl %esi, %esi
setne %bpl
je 0x1a7e1
movl $0x0, %ebx
testl %ecx, %ecx
je 0x1a7e3
addq %rdi, %rsi
addq %rdx, %rcx
xorl %ebx, %ebx
leaq 0x156ca(%rip), %r10     # 0x2fd10
xorl %r9d, %r9d
movzbl (%rdi), %r14d
movb (%r14,%r10), %r12b
testb $0x40, %r12b
je 0x1a662
movzbl (%rdx), %r11d
testb $0x40, (%r11,%r10)
jne 0x1a66e
movb (%rdx), %r15b
cmpb %r15b, %r14b
jne 0x1a68a
movl %ebx, %ebp
jmp 0x1a6ce
cmpq %rsi, %rdi
jae 0x1a6dc
cmpb $0x30, (%rdi)
jne 0x1a6dc
cmpq %rcx, %rdx
jae 0x1a6dc
cmpb $0x30, (%rdx)
jne 0x1a6dc
incq %rdi
incq %rdx
jmp 0x1a66e
movzbl %r15b, %r13d
movl %r14d, %ebp
subl %r13d, %ebp
testl %ebx, %ebx
cmovnel %ebx, %ebp
notb %r12b
orb $-0x21, %r12b
movzbl %r12b, %ebx
testb %r8b, %r8b
movl $0xff, %r11d
cmovel %r11d, %ebx
je 0x1a6c2
movb (%r13,%r10), %r11b
notb %r11b
orb $-0x21, %r11b
andb %r11b, %r15b
andl %r14d, %ebx
cmpb %r15b, %bl
jne 0x1a771
incq %rdi
incq %rdx
movb $0x1, %r14b
jmp 0x1a77e
movq %rdi, %r14
cmpq %rsi, %rdi
jae 0x1a6fa
cmpb $0x30, (%r14)
jne 0x1a6fa
cmpl $0x1, %r9d
adcl $0x0, %r9d
incq %r14
cmpq %rsi, %r14
jb 0x1a6e4
movq %rdx, %r15
cmpq %rcx, %rdx
jae 0x1a71e
cmpb $0x30, (%r15)
jne 0x1a71e
cmpl $0x1, %r9d
movl $0x0, %edx
sbbl %edx, %edx
orl %edx, %r9d
incq %r15
cmpq %rcx, %r15
jb 0x1a702
movq %r14, %rdi
cmpq %rsi, %r14
jae 0x1a73b
movq %r14, %rdi
movzbl (%rdi), %edx
testb $0x40, (%rdx,%r10)
je 0x1a73b
incq %rdi
cmpq %rsi, %rdi
jb 0x1a729
movq %r15, %rdx
cmpq %rcx, %r15
jae 0x1a759
movq %r15, %rdx
movzbl (%rdx), %r11d
testb $0x40, (%r11,%r10)
je 0x1a759
incq %rdx
cmpq %rcx, %rdx
jb 0x1a746
movq %rdi, %r12
subq %r14, %r12
movq %rdx, %r11
subq %r15, %r11
cmpq %r11, %r12
jle 0x1a787
movl $0x1, %eax
jmp 0x1a7bf
movzbl %r15b, %r11d
movzbl %bl, %eax
subl %r11d, %eax
xorl %r14d, %r14d
movl %ebp, %ebx
testb %r14b, %r14b
jne 0x1a7c6
jmp 0x1a800
jge 0x1a790
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1a7bf
movb $0x1, %bpl
cmpq %rdi, %r14
jae 0x1a7c1
movl %eax, -0x24(%rsp)
xorl %r13d, %r13d
movzbl (%r14,%r13), %eax
movzbl (%r15,%r13), %r11d
cmpb %r11b, %al
jne 0x1a7bc
incq %r13
cmpq %r13, %r12
jne 0x1a79f
movl -0x24(%rsp), %eax
jmp 0x1a7c1
subl %r11d, %eax
xorl %ebp, %ebp
testb %bpl, %bpl
je 0x1a800
cmpq %rcx, %rdx
setb %r14b
cmpq %rsi, %rdi
setb %bpl
jae 0x1a7e3
cmpq %rcx, %rdx
jb 0x1a649
jmp 0x1a7e3
xorl %ebx, %ebx
movzbl %r14b, %ecx
movl %ecx, %eax
negl %eax
orl $0x1, %eax
testb %cl, %cl
jne 0x1a800
testb %bpl, %bpl
jne 0x1a800
testl %r9d, %r9d
cmovnel %r9d, %ebx
movl %ebx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop