// File: /workspace/github_c_makefile_star2_O2/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 0x3fad
movzbl (%r14), %r9d
cmpl $0x2b, %r9d
je 0x42dc
cmpl $0x2d, %r9d
jne 0x42e3
movl $0x1, (%rbx)
movb 0x1(%r14), %r9b
incq %r14
cmpb $0x30, %r9b
jne 0x4316
movzbl 0x1(%r14), %eax
cmpl $0x42, %eax
je 0x431e
cmpl $0x78, %eax
je 0x4306
cmpl $0x62, %eax
je 0x431e
cmpl $0x58, %eax
jne 0x43fd
addq $0x2, %r14
pushq $0x10
popq %r13
movb $0x1, %dl
xorl %ecx, %ecx
xorl %esi, %esi
jmp 0x4331
pushq $0xa
popq %r13
xorl %ecx, %ecx
jmp 0x4328
addq $0x2, %r14
pushq $0x2
popq %r13
movb $0x1, %cl
xorl %esi, %esi
xorl %edx, %edx
jmp 0x4331
incq %r14
movzbl (%r14), %r8d
cmpl $0x30, %r8d
je 0x432e
testl %r8d, %r8d
jne 0x4347
xorl %eax, %eax
jmp 0x43ee
xorl %eax, %eax
leaq 0x5e80(%rip), %r15      # 0xa1d0
xorl %edi, %edi
xorl %ebp, %ebp
xorl %r12d, %r12d
testb %r8b, %r8b
je 0x4381
movzbl %r8b, %r8d
movsbl (%r8,%r15), %r8d
testl %r8d, %r8d
js 0x438a
cmpl %r8d, %r13d
jle 0x438a
movb 0x1(%r14,%r12), %r8b
incq %r12
incl %ebp
addl $0x4, %edi
addl $0x3, %eax
jmp 0x4357
testb %cl, %cl
je 0x43a7
movl %r12d, %eax
jmp 0x43c9
leaq 0x12544(%rip), %rsi     # 0x168d5
leaq 0x12550(%rip), %rdx     # 0x168e8
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x7de8
pushq $-0x2
popq %rax
jmp 0x43ee
testb %sil, %sil
jne 0x43c9
movl %edi, %eax
testb %dl, %dl
jne 0x43c9
movl %r12d, %eax
cvtsi2sd %rax, %xmm0
mulsd 0x126c6(%rip), %xmm0    # 0x16a88
cvttsd2si %xmm0, %rax
incl %eax
testl %eax, %eax
je 0x45da
movl %r9d, 0x4(%rsp)
addl $0x3e, %eax
pushq $0x3f
popq %rcx
xorl %edx, %edx
divl %ecx
movq %rbx, %rdi
movl %eax, %esi
callq 0x3ec0
testl %eax, %eax
je 0x440e
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
incq %r14
xorl %ecx, %ecx
pushq $0x8
popq %r13
movb $0x1, %sil
jmp 0x432a
cmpb $0x30, 0x4(%rsp)
jne 0x4476
addq %r14, %r12
pushq $0x3e
popq %rcx
pushq $-0x1
popq %rax
xorl %edx, %edx
pushq $0x1
popq %rsi
xorl %edi, %edi
decq %r12
cmpq %r14, %r12
jb 0x456e
movzbl (%r12), %r8d
movsbl (%r8,%r15), %r8d
movl %r13d, %r9d
cmpl $0x2, %r9d
jb 0x4425
incl %ecx
addq %rdi, %rdi
xorl %r10d, %r10d
cmpl $0x3f, %ecx
cmoveq %rsi, %rdi
sete %r10b
cmovel %edx, %ecx
addl %r10d, %eax
testb $0x1, %r8b
je 0x446e
movq 0x10(%rbx), %r10
movslq %eax, %r11
orq %rdi, (%r10,%r11,8)
sarl %r8d
shrl %r9d
jmp 0x443e
leaq 0x8(%r12), %rcx
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movl %ebp, %eax
subq %rax, %r14
addq %r12, %r14
xorl %edi, %edi
pushq $0x9
popq %r8
movl %ebp, %eax
xorl %edx, %edx
divl %r8d
imull $-0x9, %eax, %eax
addl %eax, %r12d
cmovel %r8d, %r12d
movq 0x10(%rbx), %r9
movl %ecx, %eax
xorl %edx, %edx
divl %r8d
movl %eax, %ecx
pushq $0x1
popq %r10
cmpl %ecx, %edi
je 0x45ad
movl %r10d, %r11d
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpq %r13, %r11
je 0x44ee
movl $0x3b9aca00, %eax       # imm = 0x3B9ACA00
mulq (%r9,%r13,8)
addq %rbp, %rax
adcq $0x0, %rdx
shldq $0x1, %rax, %rdx
andq %rsi, %rax
movq %rax, (%r9,%r13,8)
incq %r13
movq %rdx, %rbp
jmp 0x44c5
testq %rbp, %rbp
je 0x44fd
movslq %r10d, %rax
incl %r10d
movq %rbp, (%r9,%rax,8)
movl %r12d, %eax
xorl %edx, %edx
xorl %r11d, %r11d
cmpl %edx, %eax
je 0x451f
imull $0xa, %r11d, %ebp
movzbl (%r14,%rdx), %r11d
movsbl (%r11,%r15), %r11d
addl %ebp, %r11d
incq %rdx
jmp 0x4505
addq %rax, %r14
movl %r11d, %eax
movl %r10d, %edx
xorl %r11d, %r11d
cmpq %r11, %rdx
je 0x4555
xorl %r12d, %r12d
addq (%r9,%r11,8), %rax
setb %r12b
shldq $0x1, %rax, %r12
movq %rax, (%r9,%r11,8)
je 0x4564
andq %rsi, %rax
movq %rax, (%r9,%r11,8)
incq %r11
movq %r12, %rax
jmp 0x452b
testq %rax, %rax
je 0x4564
movslq %r10d, %rdx
incl %r10d
movq %rax, (%r9,%rdx,8)
incl %edi
movl %r8d, %r12d
jmp 0x44b5
movq 0x10(%rbx), %rcx
cltq
xorl %edx, %edx
cmpq $0x0, (%rcx,%rax,8)
setne %dl
addl %edx, %eax
movl %eax, 0x8(%rbx)
je 0x458e
cmpl 0x4(%rbx), %eax
jle 0x4340
leaq 0x12340(%rip), %rsi     # 0x168d5
leaq 0x5ae6(%rip), %rdx      # 0xa082
leaq 0x12354(%rip), %rcx     # 0x168f7
xorl %edi, %edi
movl $0x125, %r8d            # imm = 0x125
jmp 0x45f7
movl %r10d, 0x8(%rbx)
cmpb $0x0, (%r14)
je 0x4340
leaq 0x12313(%rip), %rsi     # 0x168d5
leaq 0x5ab9(%rip), %rdx      # 0xa082
leaq 0x12327(%rip), %rcx     # 0x168f7
xorl %edi, %edi
movl $0x10a, %r8d            # imm = 0x10A
jmp 0x45f7
leaq 0x122f4(%rip), %rsi     # 0x168d5
leaq 0x5a9a(%rip), %rdx      # 0xa082
leaq 0x12308(%rip), %rcx     # 0x168f7
xorl %edi, %edi
movl $0xd0, %r8d
xorl %eax, %eax
callq 0x7de8
callq 0x1050