// File: /workspace/github_c_makefile_star2_O3/cyb70289[P]tt/src/apn/integer/str.c
// Function: tt_int_from_string
int tt_int_from_string(struct tt_int *ti, const char *str)
{
	_tt_int_zero(ti);

	/* Check leading "+", "-" */
	if (*str == '-') {
		ti->sign = 1;
		str++;
	} else if (*str == '+') {
		str++;
	}

	/* Check radix: 0x -> 16, 0b -> 2, 0 -> 8, [1-9] -> 10 */
	int radix = 10;
	if (*str == '0') {
		radix = 8;
		str++;
		if (*str == 'x' || *str == 'X') {
			radix = 16;
			str++;
		} else if (*str == 'b' || *str == 'B') {
			radix = 2;
			str++;
		}
	}

	/* Skip leading 0 */
	while (*str == '0')
		str++;
	if (*str == '\0')
		return 0;

	/* Verify string */
	const char *s = str;
	while (*s) {
		int v = ascii_to_bin[(uchar)*s];
		if (v < 0 || v >= radix) {
			tt_debug("Invalid string");
			return TT_EINVAL;
		}
		s++;
	}

	/* Calculate digit buffer size in bits */
	const uint digs = s - str;
	uint bits;
	if (radix == 2)
		bits = digs;
	else if (radix == 8)
		bits = digs * 3;
	else if (radix == 16)
		bits = digs * 4;
	else
		bits = (uint)(((double)digs) * log2(10)) + 1;
	tt_assert(bits);

	/* Re-allocate buffer if required */
	const uint words = (bits+_tt_word_bits-1) / _tt_word_bits;
	int ret = _tt_int_realloc(ti, words);
	if (ret)
		return ret;

	/* String to Integer conversion */
	if (radix == 10) {
		s -= digs;	/* First digit */

		const uint len = (digs + 8) / 9;
		int lt = digs % 9;	/* Digits in top word */
		if (lt == 0)
			lt = 9;

		int msb = 1;	/* Valid words in product */
		_tt_word *product = ti->buf;
		_tt_word carry;
		_tt_word_double m;
		for (uint i = 0; i < len; i++) {
			/* Multiply product by 10^9 */
			carry = 0;
			for (uint j = 0; j < msb; j++) {
				m = product[j];
				m *= 1000000000;
				m += carry;
				carry = m >> _tt_word_bits;
				product[j] = m & ~_tt_word_top_bit;
			}
			if (carry)
				product[msb++] = carry;

			/* Pack 9-digits into one uint */
			uint dig9 = 0;
			for (int j = 0; j < lt; j++) {
				dig9 *= 10;
				dig9 += ascii_to_bin[(uchar)*s++];
			}
			lt = 9;

			/* Add 9 new decimal digits */
			carry = dig9;
			for (uint j = 0; j < msb; j++) {
				m = product[j];
				m += carry;
				carry = m >> _tt_word_bits;
				product[j] = m;
				if (carry)
					product[j] &= ~_tt_word_top_bit;
				else
					break;
			}
			if (carry)
				product[msb++] = carry;
		}
		ti->msb = msb;
		tt_assert(*s == '\0');
	} else {
		int cur_word = -1, cur_bit = _tt_word_bits-1;
		_tt_word mask = 0;

		s--;	/* Last digit */
		while (s >= str) {
			int c = ascii_to_bin[(uchar)*s];
			for (int i = radix; i > 1; i >>= 1) {
				cur_bit++;
				mask <<= 1;
				if (cur_bit == _tt_word_bits) {
					cur_bit = 0;
					mask = 1;
					cur_word++;
				}
				if (c & 1)
					ti->buf[cur_word] |= mask;
				c >>= 1;
			}
			s--;
		}

		ti->msb = cur_word + 1;
		/* Top int may be 0 */
		if (ti->buf[ti->msb-1] == 0)
			ti->msb--;
		tt_assert(ti->msb && ti->msb <= ti->_max);
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
callq 0x4a77
movzbl (%r14), %r9d
cmpl $0x2b, %r9d
je 0x4de8
cmpl $0x2d, %r9d
jne 0x4def
movl $0x1, (%rbx)
movb 0x1(%r14), %r9b
incq %r14
cmpb $0x30, %r9b
jne 0x4e1d
leaq 0x1(%r14), %r15
movzbl 0x1(%r14), %esi
movl $0x8, %ebp
movb $0x1, %cl
xorl %eax, %eax
cmpl $0x61, %esi
jg 0x4e26
cmpl $0x42, %esi
je 0x4e46
movl $0x0, %edx
cmpl $0x58, %esi
je 0x4e35
jmp 0x4e5d
movl $0xa, %ebp
xorl %eax, %eax
jmp 0x4e51
cmpl $0x62, %esi
je 0x4e46
movl $0x0, %edx
cmpl $0x78, %esi
jne 0x4e5d
addq $0x2, %r14
movl $0x10, %ebp
movb $0x1, %dl
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x4e55
addq $0x2, %r14
movl $0x2, %ebp
movb $0x1, %al
xorl %ecx, %ecx
xorl %edx, %edx
movq %r14, %r15
jmp 0x4e5d
incq %r15
movzbl (%r15), %r8d
cmpl $0x30, %r8d
je 0x4e5a
testl %r8d, %r8d
jne 0x4e73
xorl %eax, %eax
jmp 0x4f28
xorl %esi, %esi
leaq 0x6374(%rip), %r14      # 0xb1f0
xorl %edi, %edi
xorl %r12d, %r12d
xorl %r13d, %r13d
movzbl %r8b, %r8d
movsbl (%r8,%r14), %r8d
testl %r8d, %r8d
js 0x4eb6
cmpl %r8d, %ebp
jle 0x4eb6
movb 0x1(%r15,%r13), %r8b
incq %r13
incl %r12d
addl $0x4, %edi
addl $0x3, %esi
testb %r8b, %r8b
jne 0x4e84
testb %al, %al
je 0x4ed7
movl %r13d, %esi
jmp 0x4ef8
leaq 0x12a38(%rip), %rsi     # 0x178f5
leaq 0x12a44(%rip), %rdx     # 0x17908
movl $0x3, %edi
xorl %eax, %eax
callq 0x8cca
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x4f28
testb %cl, %cl
jne 0x4ef8
movl %edi, %esi
testb %dl, %dl
jne 0x4ef8
movl %r13d, %eax
cvtsi2sd %rax, %xmm0
mulsd 0x12ba7(%rip), %xmm0    # 0x17a98
cvttsd2si %xmm0, %rsi
incl %esi
testl %esi, %esi
je 0x5122
movl %r9d, 0x4(%rsp)
addl $0x3e, %esi
imulq $0x4104105, %rsi, %rax  # imm = 0x4104105
shrq $0x20, %rax
subl %eax, %esi
shrl %esi
addl %eax, %esi
shrl $0x5, %esi
movq %rbx, %rdi
callq 0x4986
testl %eax, %eax
je 0x4f37
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x30, 0x4(%rsp)
jne 0x4fd9
leaq (%r15,%r13), %rax
decq %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %r15, %rax
jb 0x4fb3
movl $0x3e, %ecx
xorl %esi, %esi
movl $0x1, %edi
xorl %r8d, %r8d
movzbl (%rax), %r9d
movsbl (%r9,%r14), %r9d
movl %ebp, %r10d
movl %edx, %r11d
incl %ecx
addq %r8, %r8
xorl %edx, %edx
cmpl $0x3f, %ecx
cmoveq %rdi, %r8
sete %dl
cmovel %esi, %ecx
addl %r11d, %edx
testb $0x1, %r9b
je 0x4f99
movq 0x10(%rbx), %r11
movslq %edx, %r12
orq %r8, (%r11,%r12,8)
sarl %r9d
movl %r10d, %r11d
shrl %r11d
cmpl $0x3, %r10d
movl %r11d, %r10d
ja 0x4f6e
decq %rax
cmpq %r15, %rax
jae 0x4f62
movq 0x10(%rbx), %rcx
movslq %edx, %rax
xorl %edx, %edx
cmpq $0x0, (%rcx,%rax,8)
setne %dl
addl %edx, %eax
movl %eax, 0x8(%rbx)
je 0x4fd4
cmpl 0x4(%rbx), %eax
jle 0x4e6c
callq 0x16d8
movl %r12d, %eax
subq %rax, %r15
addq %r13, %r15
leal 0x8(%r13), %ecx
cmpl $0x9, %ecx
jae 0x4ff5
movl $0x1, %eax
jmp 0x5110
movq 0x10(%rbx), %rcx
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
imulq $0x38e38e39, %rax, %rax # imm = 0x38E38E39
shrq $0x21, %rax
leal (%rax,%rax,8), %eax
movl %r13d, %edx
subl %eax, %edx
movl $0x9, %r10d
cmovnel %edx, %r10d
addl $0x8, %r13d
imulq $0x38e38e39, %r13, %rdi # imm = 0x38E38E39
shrq $0x21, %rdi
cmpl $0x2, %edi
movl $0x1, %r8d
cmovbl %r8d, %edi
xorl %r9d, %r9d
movl $0x1, %r11d
movl $0x0, %eax
testl %r11d, %r11d
je 0x5090
movl %r11d, %r12d
xorl %ebp, %ebp
xorl %r13d, %r13d
movl $0x3b9aca00, %eax       # imm = 0x3B9ACA00
mulq (%rcx,%rbp,8)
addq %r13, %rax
adcq $0x0, %rdx
movq %rdx, %r13
shldq $0x1, %rax, %r13
andq %rsi, %rax
movq %rax, (%rcx,%rbp,8)
incq %rbp
cmpq %rbp, %r12
jne 0x5057
testq %r13, %r13
je 0x508d
movslq %r11d, %rax
incl %r11d
movq %r13, (%rcx,%rax,8)
movl %r11d, %eax
cmpl $0x2, %r10d
cmovll %r8d, %r10d
xorl %r11d, %r11d
leal (%r11,%r11,4), %edx
movzbl (%r15), %r11d
incq %r15
movsbl (%r11,%r14), %r11d
leal (%r11,%rdx,2), %r11d
decl %r10d
jne 0x509b
movl %r11d, %edx
testl %eax, %eax
je 0x50e9
movl %eax, %r10d
xorl %r11d, %r11d
movq %rdx, %r12
xorl %edx, %edx
addq (%rcx,%r11,8), %r12
setb %dl
shldq $0x1, %r12, %rdx
movq %r12, (%rcx,%r11,8)
je 0x50fb
andq %rsi, %r12
movq %r12, (%rcx,%r11,8)
incq %r11
cmpq %r11, %r10
jne 0x50c1
jmp 0x50ee
testl %r11d, %r11d
je 0x50f9
movslq %eax, %r10
incl %eax
movq %rdx, (%rcx,%r10,8)
jmp 0x50fb
xorl %eax, %eax
incl %r9d
movl $0x9, %r10d
movl %eax, %r11d
cmpl %edi, %r9d
jne 0x5045
movl %eax, 0x8(%rbx)
cmpb $0x0, (%r15)
je 0x4e6c
callq 0x16ae
callq 0x1702