// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movq 0x98(%rsp), %rdi
callq 0x6a60
movq 0x90(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x70e8
movq 0x98(%rsp), %rax
movl $0x1, (%rax)
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x710e
movq 0x90(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x2b, %eax
jne 0x710c
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x710e
movl $0xa, 0x8c(%rsp)
movq 0x90(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x71d0
movl $0x8, 0x8c(%rsp)
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x78, %eax
je 0x716c
movq 0x90(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x58, %eax
jne 0x718d
movl $0x10, 0x8c(%rsp)
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x71ce
movq 0x90(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x62, %eax
je 0x71ad
movq 0x90(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x42, %eax
jne 0x71cc
movl $0x2, 0x8c(%rsp)
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x71ce
jmp 0x71d0
jmp 0x71d2
movq 0x90(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x71f8
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x71d2
movq 0x90(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x7218
movl $0x0, 0xa4(%rsp)
jmp 0x78c6
movq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
cmpb $0x0, (%rax)
je 0x72a5
movq 0x80(%rsp), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x1aeb7(%rip), %rax     # 0x22100
movsbl (%rax,%rcx), %eax
movl %eax, 0x7c(%rsp)
cmpl $0x0, 0x7c(%rsp)
jl 0x7265
movl 0x7c(%rsp), %eax
cmpl 0x8c(%rsp), %eax
jl 0x728f
movl $0x3, %edi
leaq 0x1758c(%rip), %rsi     # 0x1e7fd
leaq 0x17598(%rip), %rdx     # 0x1e810
movb $0x0, %al
callq 0xe010
movl $0xfffffffe, 0xa4(%rsp) # imm = 0xFFFFFFFE
jmp 0x78c6
movq 0x80(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x80(%rsp)
jmp 0x7228
movq 0x80(%rsp), %rax
movq 0x90(%rsp), %rcx
subq %rcx, %rax
movl %eax, 0x78(%rsp)
cmpl $0x2, 0x8c(%rsp)
jne 0x72d0
movl 0x78(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0x7335
cmpl $0x8, 0x8c(%rsp)
jne 0x72e5
imull $0x3, 0x78(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0x7333
cmpl $0x10, 0x8c(%rsp)
jne 0x72fc
movl 0x78(%rsp), %eax
shll $0x2, %eax
movl %eax, 0x74(%rsp)
jmp 0x7331
movl 0x78(%rsp), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x1766d(%rip), %xmm0    # 0x1e980
callq 0x1040
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x7333
jmp 0x7335
jmp 0x7337
cmpl $0x0, 0x74(%rsp)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x737b
xorl %edi, %edi
leaq 0x174a2(%rip), %rsi     # 0x1e7fd
leaq 0xad7c(%rip), %rdx      # 0x120de
leaq 0x174b6(%rip), %rcx     # 0x1e81f
movl $0xd0, %r8d
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x737d
movl 0x74(%rsp), %eax
addl $0x3f, %eax
subl $0x1, %eax
movl $0x3f, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, 0x70(%rsp)
movq 0x98(%rsp), %rdi
movl 0x70(%rsp), %esi
callq 0x68d0
movl %eax, 0x6c(%rsp)
cmpl $0x0, 0x6c(%rsp)
je 0x73c0
movl 0x6c(%rsp), %eax
movl %eax, 0xa4(%rsp)
jmp 0x78c6
cmpl $0xa, 0x8c(%rsp)
jne 0x76f5
movl 0x78(%rsp), %ecx
movq 0x80(%rsp), %rax
movl %ecx, %ecx
movl %ecx, %edx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, 0x80(%rsp)
movl 0x78(%rsp), %eax
addl $0x8, %eax
movl $0x9, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, 0x68(%rsp)
movl 0x78(%rsp), %eax
movl $0x9, %ecx
xorl %edx, %edx
divl %ecx
movl %edx, 0x64(%rsp)
cmpl $0x0, 0x64(%rsp)
jne 0x7422
movl $0x9, 0x64(%rsp)
movl $0x1, 0x60(%rsp)
movq 0x98(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x58(%rsp)
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x68(%rsp), %eax
jae 0x7692
movq $0x0, 0x50(%rsp)
movl $0x0, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x60(%rsp), %eax
jae 0x7514
movq 0x58(%rsp), %rax
movl 0x38(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
movl $0x3b9aca00, %edx       # imm = 0x3B9ACA00
mulq %rdx
imulq $0x3b9aca00, %rcx, %rcx # imm = 0x3B9ACA00
addq %rcx, %rdx
movq %rax, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movq 0x50(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rax
addq %rdx, %rcx
adcq $0x0, %rax
movq %rcx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rax
shldq $0x1, %rcx, %rax
movq %rax, 0x50(%rsp)
movq 0x40(%rsp), %rdx
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
andq %rax, %rdx
movq 0x58(%rsp), %rax
movl 0x38(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x7462
cmpq $0x0, 0x50(%rsp)
je 0x753a
movq 0x50(%rsp), %rdx
movq 0x58(%rsp), %rax
movl 0x60(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x60(%rsp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x0, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
cmpl 0x64(%rsp), %eax
jge 0x7599
imull $0xa, 0x34(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x80(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x80(%rsp)
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x1ab80(%rip), %rax     # 0x22100
movsbl (%rax,%rcx), %eax
addl 0x34(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x754a
movl $0x9, 0x64(%rsp)
movl 0x34(%rsp), %eax
movq %rax, 0x50(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x60(%rsp), %eax
jae 0x765a
movq 0x58(%rsp), %rax
movl 0x2c(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq 0x50(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rax
addq %rdx, %rcx
adcq $0x0, %rax
movq %rcx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rax
shldq $0x1, %rcx, %rax
movq %rax, 0x50(%rsp)
movq 0x40(%rsp), %rdx
movq 0x58(%rsp), %rax
movl 0x2c(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, 0x50(%rsp)
je 0x7646
movq 0x58(%rsp), %rax
movl 0x2c(%rsp), %ecx
movabsq $0x7fffffffffffffff, %rdx # imm = 0x7FFFFFFFFFFFFFFF
andq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x7648
jmp 0x765a
jmp 0x764a
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x75b2
cmpq $0x0, 0x50(%rsp)
je 0x7680
movq 0x50(%rsp), %rdx
movq 0x58(%rsp), %rax
movl 0x60(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x60(%rsp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x7682
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x7443
movl 0x60(%rsp), %ecx
movq 0x98(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x80(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x76ee
xorl %edi, %edi
leaq 0x1712f(%rip), %rsi     # 0x1e7fd
leaq 0xaa09(%rip), %rdx      # 0x120de
leaq 0x17143(%rip), %rcx     # 0x1e81f
movl $0x10a, %r8d            # imm = 0x10A
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x76f0
jmp 0x78bb
movl $0xffffffff, 0x28(%rsp) # imm = 0xFFFFFFFF
movl $0x3e, 0x24(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x80(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
cmpq 0x90(%rsp), %rax
jb 0x77fd
movq 0x80(%rsp), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x1a9b4(%rip), %rax     # 0x22100
movsbl (%rax,%rcx), %eax
movl %eax, 0x14(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x10(%rsp)
cmpl $0x1, 0x10(%rsp)
jle 0x77e4
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movq 0x18(%rsp), %rax
shlq %rax
movq %rax, 0x18(%rsp)
cmpl $0x3f, 0x24(%rsp)
jne 0x77a1
movl $0x0, 0x24(%rsp)
movq $0x1, 0x18(%rsp)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
movl 0x14(%rsp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x77cb
movq 0x18(%rsp), %rdx
movq 0x98(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x28(%rsp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movl 0x14(%rsp), %eax
sarl %eax
movl %eax, 0x14(%rsp)
movl 0x10(%rsp), %eax
sarl %eax
movl %eax, 0x10(%rsp)
jmp 0x775f
movq 0x80(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x80(%rsp)
jmp 0x7722
movl 0x28(%rsp), %ecx
addl $0x1, %ecx
movq 0x98(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x98(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x98(%rsp), %rcx
movl 0x8(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x7844
movq 0x98(%rsp), %rax
movl 0x8(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x7846
movq 0x98(%rsp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x8(%rcx)
movb %al, 0x7(%rsp)
je 0x7877
movq 0x98(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x98(%rsp), %rcx
cmpl 0x4(%rcx), %eax
setle %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x78b7
xorl %edi, %edi
leaq 0x16f66(%rip), %rsi     # 0x1e7fd
leaq 0xa840(%rip), %rdx      # 0x120de
leaq 0x16f7a(%rip), %rcx     # 0x1e81f
movl $0x125, %r8d            # imm = 0x125
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x78b9
jmp 0x78bb
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nopw %cs:(%rax,%rax)