// File: jliesch[P]ofxget/pugixml/pugixml.cpp
// Function: unsigned int pugi::impl::(anonymous namespace)::string_to_integer<unsigned int>(char const*, unsigned int, unsigned int)
PUGI__FN PUGI__UNSIGNED_OVERFLOW U string_to_integer(const char_t* value, U minv, U maxv)
	{
		U result = 0;
		const char_t* s = value;

		while (PUGI__IS_CHARTYPE(*s, ct_space))
			s++;

		bool negative = (*s == '-');

		s += (*s == '+' || *s == '-');

		bool overflow = false;

		if (s[0] == '0' && (s[1] | ' ') == 'x')
		{
			s += 2;

			// since overflow detection relies on length of the sequence skip leading zeros
			while (*s == '0')
				s++;

			const char_t* start = s;

			for (;;)
			{
				if (static_cast<unsigned>(*s - '0') < 10)
					result = result * 16 + (*s - '0');
				else if (static_cast<unsigned>((*s | ' ') - 'a') < 6)
					result = result * 16 + ((*s | ' ') - 'a' + 10);
				else
					break;

				s++;
			}

			size_t digits = static_cast<size_t>(s - start);

			overflow = digits > sizeof(U) * 2;
		}
		else
		{
			// since overflow detection relies on length of the sequence skip leading zeros
			while (*s == '0')
				s++;

			const char_t* start = s;

			for (;;)
			{
				if (static_cast<unsigned>(*s - '0') < 10)
					result = result * 10 + (*s - '0');
				else
					break;

				s++;
			}

			size_t digits = static_cast<size_t>(s - start);

			PUGI__STATIC_ASSERT(sizeof(U) == 8 || sizeof(U) == 4 || sizeof(U) == 2);

			const size_t max_digits10 = sizeof(U) == 8 ? 20 : sizeof(U) == 4 ? 10 : 5;
			const char_t max_lead = sizeof(U) == 8 ? '1' : sizeof(U) == 4 ? '4' : '6';
			const size_t high_bit = sizeof(U) * 8 - 1;

			overflow = digits >= max_digits10 && !(digits == max_digits10 && (*start < max_lead || (*start == max_lead && result >> high_bit)));
		}

		if (negative)
		{
			// Workaround for crayc++ CC-3059: Expected no overflow in routine.
		#ifdef _CRAYC
			return (overflow || result > ~minv + 1) ? minv : ~result + 1;
		#else
			return (overflow || result > 0 - minv) ? minv : 0 - result;
		#endif
		}
		else
			return (overflow || result > maxv) ? maxv : result;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x1be53(%rip), %rax     # 0x6b0d0
movzbl (%rax,%rcx), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x4f297
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x4f26d
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
sete %al
andb $0x1, %al
movb %al, -0x29(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x2b, %ecx
movb %al, -0x69(%rbp)
je 0x4f2ca
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
sete %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x28(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movb $0x0, -0x2a(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x4f3be
movq -0x28(%rbp), %rax
movsbl 0x1(%rax), %eax
orl $0x20, %eax
cmpl $0x78, %eax
jne 0x4f3be
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x4f32e
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x4f314
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
subl $0x30, %eax
cmpl $0xa, %eax
jae 0x4f35c
movl -0x1c(%rbp), %eax
shll $0x4, %eax
movq -0x28(%rbp), %rcx
movsbl (%rcx), %ecx
subl $0x30, %ecx
addl %ecx, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4f38f
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
orl $0x20, %eax
subl $0x61, %eax
cmpl $0x6, %eax
jae 0x4f38b
movl -0x1c(%rbp), %eax
shll $0x4, %eax
movq -0x28(%rbp), %rcx
movsbl (%rcx), %ecx
orl $0x20, %ecx
subl $0x61, %ecx
addl $0xa, %ecx
addl %ecx, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4f38d
jmp 0x4f39d
jmp 0x4f38f
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x4f336
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x40(%rbp)
cmpq $0x8, -0x40(%rbp)
seta %al
andb $0x1, %al
movb %al, -0x2a(%rbp)
jmp 0x4f49e
jmp 0x4f3c0
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x4f3da
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x4f3c0
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
subl $0x30, %eax
cmpl $0xa, %eax
jae 0x4f406
imull $0xa, -0x1c(%rbp), %eax
movq -0x28(%rbp), %rcx
movsbl (%rcx), %ecx
subl $0x30, %ecx
addl %ecx, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4f408
jmp 0x4f416
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x4f3e2
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x50(%rbp)
movq $0xa, -0x58(%rbp)
movb $0x34, -0x59(%rbp)
movq $0x1f, -0x68(%rbp)
xorl %eax, %eax
cmpq $0xa, -0x50(%rbp)
movb %al, -0x6a(%rbp)
jb 0x4f496
xorl %eax, %eax
cmpq $0xa, -0x50(%rbp)
movb %al, -0x6b(%rbp)
jne 0x4f48e
movq -0x48(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x34, %ecx
movb %al, -0x6c(%rbp)
jl 0x4f488
movq -0x48(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x34, %ecx
movb %al, -0x6d(%rbp)
jne 0x4f482
movl -0x1c(%rbp), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x6d(%rbp)
movb -0x6d(%rbp), %al
movb %al, -0x6c(%rbp)
movb -0x6c(%rbp), %al
movb %al, -0x6b(%rbp)
movb -0x6b(%rbp), %al
xorb $-0x1, %al
movb %al, -0x6a(%rbp)
movb -0x6a(%rbp), %al
andb $0x1, %al
movb %al, -0x2a(%rbp)
testb $0x1, -0x29(%rbp)
je 0x4f4ce
testb $0x1, -0x2a(%rbp)
jne 0x4f4b6
movl -0x1c(%rbp), %eax
xorl %ecx, %ecx
subl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jbe 0x4f4be
movl -0x14(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x4f4c6
xorl %eax, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x4f4f0
testb $0x1, -0x2a(%rbp)
jne 0x4f4dc
movl -0x1c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jbe 0x4f4e4
movl -0x18(%rbp), %eax
movl %eax, -0x78(%rbp)
jmp 0x4f4ea
movl -0x1c(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopw %cs:(%rax,%rax)