// File: /workspace/github_c_makefile_star2_O1/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
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r14
callq 0x4a46
movzbl (%rbx), %eax
cmpl $0x2b, %eax
je 0x4dd1
cmpl $0x2d, %eax
jne 0x4dd4
movl $0x1, (%r14)
incq %rbx
movb (%rbx), %dl
cmpb $0x30, %dl
movq %r14, 0x20(%rsp)
jne 0x4e0a
leaq 0x1(%rbx), %rcx
movzbl 0x1(%rbx), %eax
movl $0x8, %r8d
movb $0x1, %dil
xorl %esi, %esi
cmpl $0x61, %eax
jg 0x4e14
cmpl $0x42, %eax
je 0x4e37
movl $0x0, %r9d
cmpl $0x58, %eax
je 0x4e24
jmp 0x4e4c
movl $0xa, %r8d
xorl %esi, %esi
jmp 0x4e44
cmpl $0x62, %eax
je 0x4e37
movl $0x0, %r9d
cmpl $0x78, %eax
jne 0x4e4c
addq $0x2, %rbx
movl $0x10, %r8d
movb $0x1, %r9b
xorl %esi, %esi
xorl %edi, %edi
jmp 0x4e49
addq $0x2, %rbx
movl $0x2, %r8d
movb $0x1, %sil
xorl %edi, %edi
xorl %r9d, %r9d
movq %rbx, %rcx
movl %ecx, %r14d
negl %r14d
xorl %ebp, %ebp
movzbl (%rcx,%rbp), %eax
cmpl $0x30, %eax
jne 0x4e65
incq %rbp
decl %r14d
jmp 0x4e54
testl %eax, %eax
jne 0x4e70
xorl %eax, %eax
jmp 0x4f61
movl %r9d, 0x8(%rsp)
movl %edi, 0xc(%rsp)
movl %esi, 0x4(%rsp)
movb %dl, 0x3(%rsp)
movq %rcx, 0x10(%rsp)
leaq (%rcx,%rbp), %r13
leaq 0x635f(%rip), %r12      # 0xb1f0
movq %r13, 0x18(%rsp)
movzbl (%r13), %eax
testq %rax, %rax
je 0x4eea
movsbl (%rax,%r12), %eax
testl %eax, %eax
setns %cl
cmpl %eax, %r8d
setg %bl
andb %cl, %bl
je 0x4eb9
incq %r13
jmp 0x4edf
movl $0x3, %edi
leaq 0x12a30(%rip), %rsi     # 0x178f5
leaq 0x12a3c(%rip), %rdx     # 0x17908
xorl %eax, %eax
movq %r12, %r15
movl %r8d, %r12d
callq 0x8c46
movl %r12d, %r8d
movq %r15, %r12
testb %bl, %bl
jne 0x4e96
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x4f61
movq 0x10(%rsp), %rsi
negl %esi
addq %r13, %rsi
subq %rbp, %rsi
cmpb $0x0, 0x4(%rsp)
jne 0x4f2c
cmpb $0x0, 0xc(%rsp)
je 0x4f0a
leal (%rsi,%rsi,2), %esi
jmp 0x4f2c
cmpb $0x0, 0x8(%rsp)
je 0x4f16
shll $0x2, %esi
jmp 0x4f2c
movl %esi, %eax
cvtsi2sd %rax, %xmm0
mulsd 0x12b73(%rip), %xmm0    # 0x17a98
cvttsd2si %xmm0, %rsi
incl %esi
testl %esi, %esi
je 0x517b
movl %r8d, 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 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x4952
testl %eax, %eax
je 0x4f70
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x30, 0x3(%rsp)
jne 0x501d
decq %r13
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %r14
cmpq %r14, %r13
jb 0x4ff4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x3e, %ecx
xorl %edx, %edx
movl $0x1, %esi
xorl %edi, %edi
movzbl (%r13), %r8d
movsbl (%r8,%r12), %r8d
movl 0x4(%rsp), %r9d
movl %eax, %r10d
incl %ecx
addq %rdi, %rdi
xorl %eax, %eax
cmpl $0x3f, %ecx
cmoveq %rsi, %rdi
sete %al
cmovel %edx, %ecx
addl %r10d, %eax
testb $0x1, %r8b
je 0x4fda
movq 0x10(%rbx), %r10
movslq %eax, %r11
orq %rdi, (%r10,%r11,8)
sarl %r8d
movl %r9d, %r10d
shrl %r10d
cmpl $0x3, %r9d
movl %r10d, %r9d
ja 0x4faf
decq %r13
cmpq %r14, %r13
jae 0x4fa0
leal 0x1(%rax), %ecx
movl %ecx, 0x8(%rbx)
movq 0x10(%rbx), %rdx
cltq
cmpq $0x0, (%rdx,%rax,8)
cmovnel %ecx, %eax
movl %eax, 0x8(%rbx)
testl %eax, %eax
je 0x5018
cmpl 0x4(%rbx), %eax
jle 0x4e69
callq 0x172c
movq %r13, %rax
movq 0x10(%rsp), %r10
subq %r10, %rax
subq %rbp, %rax
movl %eax, %eax
movq %r13, %rcx
subq %rax, %rcx
movl %r13d, %eax
subl %r10d, %eax
subl %ebp, %eax
addl $0x8, %eax
movl $0x1, %r9d
cmpl $0x9, %eax
jae 0x5051
movq %rbx, %rbp
jmp 0x5169
movq %rbx, %r8
movq 0x10(%rbx), %rsi
addl %r13d, %r14d
imulq $0x38e38e39, %r14, %rax # imm = 0x38E38E39
shrq $0x21, %rax
leal (%rax,%rax,8), %eax
movl %r13d, %edx
subl %eax, %edx
subl %r10d, %edx
subl %ebp, %edx
movl $0x9, %ebx
cmovnel %edx, %ebx
movabsq $0x7fffffffffffffff, %rdi # imm = 0x7FFFFFFFFFFFFFFF
subl %r10d, %r13d
subl %ebp, %r13d
movq %r8, %rbp
addl $0x8, %r13d
imulq $0x38e38e39, %r13, %r8  # imm = 0x38E38E39
shrq $0x21, %r8
cmpl $0x2, %r8d
movl $0x1, %r10d
cmovbl %r10d, %r8d
xorl %r11d, %r11d
testl %r9d, %r9d
je 0x50e5
movl %r9d, %r14d
xorl %r15d, %r15d
xorl %r13d, %r13d
movl $0x3b9aca00, %eax       # imm = 0x3B9ACA00
mulq (%rsi,%r15,8)
addq %r13, %rax
adcq $0x0, %rdx
shldq $0x1, %rax, %rdx
andq %rdi, %rax
movq %rax, (%rsi,%r15,8)
incq %r15
movq %rdx, %r13
cmpq %r15, %r14
jne 0x50bc
jmp 0x50e7
xorl %edx, %edx
testq %rdx, %rdx
je 0x50f6
movslq %r9d, %rax
incl %r9d
movq %rdx, (%rsi,%rax,8)
cmpl $0x2, %ebx
cmovll %r10d, %ebx
xorl %eax, %eax
leal (%rax,%rax,4), %eax
movzbl (%rcx), %edx
incq %rcx
movsbl (%rdx,%r12), %edx
leal (%rdx,%rax,2), %eax
decl %ebx
jne 0x50ff
movl %eax, %eax
testl %r9d, %r9d
je 0x5149
movl %r9d, %edx
xorl %ebx, %ebx
movq %rax, %r14
xorl %eax, %eax
addq (%rsi,%rbx,8), %r14
setb %al
shldq $0x1, %r14, %rax
movq %r14, (%rsi,%rbx,8)
je 0x5147
andq %rdi, %r14
movq %r14, (%rsi,%rbx,8)
incq %rbx
cmpl %ebx, %edx
jne 0x5120
jmp 0x5149
xorl %eax, %eax
testq %rax, %rax
je 0x5158
movslq %r9d, %rdx
incl %r9d
movq %rax, (%rsi,%rdx,8)
incl %r11d
movl $0x9, %ebx
cmpl %r8d, %r11d
jne 0x50ae
movl %r9d, 0x8(%rbp)
cmpb $0x0, (%rcx)
je 0x4e69
callq 0x1702
callq 0x1756