// File: /workspace/github_cpp_makefile_star2_O3/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 %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
cmovnsl %r10d, %esi
xorl $0xf, %r11d
testq %rcx, %rcx
cmovnsq %r9, %rdx
movq %rcx, 0x8(%r9)
cmovnsl %r11d, %ecx
testl %esi, %esi
setne %bl
testl %ecx, %ecx
setne %al
movl %ebx, %r10d
andb %al, %r10b
xorl %r9d, %r9d
movl $0x0, %r11d
cmpb $0x1, %r10b
jne 0x1a88d
addq %rdi, %rsi
addq %rdx, %rcx
xorl %r11d, %r11d
leaq 0x1560b(%rip), %r10     # 0x2fd50
xorl %r9d, %r9d
movzbl (%rdi), %ebx
movb (%rbx,%r10), %r14b
movb (%rdx), %al
movzbl %al, %r15d
testb $0x40, %r14b
je 0x1a762
testb $0x40, (%r15,%r10)
jne 0x1a772
cmpb %al, %bl
jne 0x1a83d
movl %r11d, %ebp
jmp 0x1a86d
cmpq %rsi, %rdi
jae 0x1a799
cmpb $0x30, (%rdi)
jne 0x1a799
cmpq %rcx, %rdx
jae 0x1a799
cmpb $0x30, (%rdx)
jne 0x1a799
incq %rdi
incq %rdx
jmp 0x1a772
cmpl $0x1, %r9d
adcl $0x0, %r9d
incq %rdi
cmpq %rsi, %rdi
jae 0x1a7b6
cmpb $0x30, (%rdi)
je 0x1a78e
jmp 0x1a7b6
cmpl $0x1, %r9d
movl $0x0, %eax
sbbl %eax, %eax
orl %eax, %r9d
incq %rdx
cmpq %rcx, %rdx
jae 0x1a7c0
cmpb $0x30, (%rdx)
je 0x1a7a5
movq %rdi, %rbx
cmpq %rsi, %rdi
jae 0x1a7dd
movq %rdi, %rbx
movzbl (%rbx), %eax
testb $0x40, (%rax,%r10)
je 0x1a7dd
incq %rbx
cmpq %rsi, %rbx
jb 0x1a7cb
movq %rdx, %r14
cmpq %rcx, %rdx
jae 0x1a7fb
movq %rdx, %r14
movzbl (%r14), %eax
testb $0x40, (%rax,%r10)
je 0x1a7fb
incq %r14
cmpq %rcx, %r14
jb 0x1a7e8
movq %rbx, %r15
subq %rdi, %r15
movq %r14, %rax
subq %rdx, %rax
cmpq %rax, %r15
jg 0x1a8c0
jl 0x1a8c7
cmpq %rbx, %rdi
jae 0x1a835
xorl %r12d, %r12d
movzbl (%rdi,%r12), %eax
movzbl (%rdx,%r12), %ebp
cmpb %bpl, %al
jne 0x1a8ab
incq %r12
cmpq %r12, %r15
jne 0x1a81e
movq %r14, %rdx
movq %rbx, %rdi
jmp 0x1a876
movl %ebx, %ebp
subl %r15d, %ebp
testl %r11d, %r11d
cmovnel %r11d, %ebp
testb %r8b, %r8b
je 0x1a869
notb %r14b
orb $-0x21, %r14b
andb %bl, %r14b
movb (%r15,%r10), %r11b
notb %r11b
orb $-0x21, %r11b
andb %r11b, %al
movl %r14d, %ebx
cmpb %al, %bl
jne 0x1a8b6
incq %rdi
incq %rdx
movl %ebp, %r11d
cmpq %rcx, %rdx
setb %al
cmpq %rsi, %rdi
setb %bl
jae 0x1a88d
cmpq %rcx, %rdx
jb 0x1a748
movzbl %al, %ecx
movl %ecx, %eax
negl %eax
orl $0x1, %eax
testb %cl, %cl
jne 0x1a8ad
testb %bl, %bl
jne 0x1a8ad
testl %r9d, %r9d
cmovnel %r9d, %r11d
movl %r11d, %eax
jmp 0x1a8ad
subl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movzbl %al, %ecx
movzbl %bl, %eax
subl %ecx, %eax
jmp 0x1a8ad
movl $0x1, %eax
jmp 0x1a8ad
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1a8ad