// File: sokolcom[P]tp-blockchain-task06/src/recover.cpp
// Function: reconstruct_secret(std::vector<std::pair<uint256_t, uint256_t>, std::allocator<std::pair<uint256_t, uint256_t>>>&, unsigned short)
static uint256_t reconstruct_secret(std::vector<std::pair<uint256_t, uint256_t>> &points, const uint16_t t)
{
    const uint256_t modulo = FIELD_MODULO;

    uint256_t lagrange_polynomial = 0;
	uint256_t basis_polynomial;
    uint256_t temp, temp1;

	for (uint8_t i = 0; i < points.size(); i++)
	{
		basis_polynomial = 1;
		for (uint8_t j = 0; j < points.size(); j++)
		{
			if (j == i) continue;

            uint256_t numer = modulo - points[j].first;  // 0 - Xj
            uint256_t denom = 
                (points[i].first > points[j].first) ?
                (points[i].first - points[j].first) : modulo - (points[j].first - points[i].first);

            temp1 = inverse_modulo(denom, modulo);
            temp = numer.mulmod(temp1, modulo);
            basis_polynomial = basis_polynomial.mulmod(temp, modulo);	
		}

        temp = basis_polynomial.mulmod(points[i].second, modulo);
        lagrange_polynomial = lagrange_polynomial.addmod(temp, modulo);
	}

	return lagrange_polynomial; 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movq %rdi, -0x210(%rbp)
movw %dx, %ax
movq %rdi, -0x218(%rbp)
movq %rsi, -0x8(%rbp)
movw %ax, -0xa(%rbp)
leaq -0x30(%rbp), %rdi
leaq 0x2ff3(%rip), %rsi      # 0xf1a2
callq 0x6cb0
movq -0x210(%rbp), %rdi
movl $0x0, -0x34(%rbp)
leaq -0x34(%rbp), %rsi
callq 0xabf0
movb $0x0, -0x99(%rbp)
movzbl -0x99(%rbp), %eax
movq %rax, -0x220(%rbp)
movq -0x8(%rbp), %rdi
callq 0xc990
movq %rax, %rcx
movq -0x220(%rbp), %rax
cmpq %rcx, %rax
jae 0xc5fa
movl $0x1, -0xa0(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xacb0
movb $0x0, -0xa1(%rbp)
movzbl -0xa1(%rbp), %eax
movq %rax, -0x228(%rbp)
movq -0x8(%rbp), %rdi
callq 0xc990
movq %rax, %rcx
movq -0x228(%rbp), %rax
cmpq %rcx, %rax
jae 0xc4f3
movzbl -0xa1(%rbp), %eax
movzbl -0x99(%rbp), %ecx
cmpl %ecx, %eax
jne 0xc25e
jmp 0xc4e0
movq -0x8(%rbp), %rdi
movzbl -0xa1(%rbp), %eax
movl %eax, %esi
callq 0xc9b0
movq %rax, %rdx
leaq -0xc8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x8280
movq -0x8(%rbp), %rdi
movzbl -0x99(%rbp), %eax
movl %eax, %esi
callq 0xc9b0
movq %rax, -0x230(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0xa1(%rbp), %eax
movl %eax, %esi
callq 0xc9b0
movq -0x230(%rbp), %rdi
movq %rax, %rsi
callq 0x7e60
testb $0x1, %al
jne 0xc2c3
jmp 0xc306
movq -0x8(%rbp), %rdi
movzbl -0x99(%rbp), %eax
movl %eax, %esi
callq 0xc9b0
movq %rax, -0x238(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0xa1(%rbp), %eax
movl %eax, %esi
callq 0xc9b0
movq -0x238(%rbp), %rsi
movq %rax, %rdx
leaq -0xe8(%rbp), %rdi
callq 0x8280
jmp 0xc35e
movq -0x8(%rbp), %rdi
movzbl -0xa1(%rbp), %eax
movl %eax, %esi
callq 0xc9b0
movq %rax, -0x240(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x99(%rbp), %eax
movl %eax, %esi
callq 0xc9b0
movq -0x240(%rbp), %rsi
movq %rax, %rdx
leaq -0x108(%rbp), %rdi
callq 0x8280
leaq -0xe8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x108(%rbp), %rdx
callq 0x8280
leaq -0x128(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0xc610
movq -0x128(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
leaq -0x168(%rbp), %rax
leaq -0x30(%rbp), %rdx
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0xa7f0
movq -0x148(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x190(%rbp)
leaq -0x188(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x1a8(%rbp), %rax
leaq -0x30(%rbp), %rdx
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0xa7f0
movq -0x188(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x178(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x170(%rbp), %rax
movq %rax, -0x40(%rbp)
movb -0xa1(%rbp), %al
addb $0x1, %al
movb %al, -0xa1(%rbp)
jmp 0xc21d
movq -0x8(%rbp), %rdi
movzbl -0x99(%rbp), %eax
movl %eax, %esi
callq 0xc9b0
movq 0x20(%rax), %rcx
movq %rcx, -0x1e8(%rbp)
movq 0x28(%rax), %rcx
movq %rcx, -0x1e0(%rbp)
movq 0x30(%rax), %rcx
movq %rcx, -0x1d8(%rbp)
movq 0x38(%rax), %rax
movq %rax, -0x1d0(%rbp)
leaq -0x1c8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x1e8(%rbp), %rax
leaq -0x30(%rbp), %rdx
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0xa7f0
movq -0x210(%rbp), %rsi
movq -0x1c8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x1c0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x1b8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x1b0(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x208(%rbp), %rdi
leaq -0x78(%rbp), %rdx
leaq -0x30(%rbp), %rcx
callq 0xa9f0
movq -0x210(%rbp), %rax
movq -0x208(%rbp), %rcx
movq %rcx, (%rax)
movq -0x200(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x1f8(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x1f0(%rbp), %rcx
movq %rcx, 0x18(%rax)
movb -0x99(%rbp), %al
addb $0x1, %al
movb %al, -0x99(%rbp)
jmp 0xc1d2
movq -0x218(%rbp), %rax
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
nopw (%rax,%rax)