// File: /workspace/github_cpp_makefile_star2_O3/permutationlock[P]aes_math/lib/galois_field.cpp
// Function: GaloisPolynomial::inverse() const
GaloisPolynomial GaloisPolynomial::inverse() const{
    // Return 0 on 0
    if(_polynomial.size()<1) return GaloisPolynomial(Polynomial(0,_polynomial.getPrime(),1));
    Polynomial p1 = _modulus;
    Polynomial p2 = _polynomial;
    vector<Polynomial> aux;
    aux.push_back(Polynomial(0,_polynomial.getPrime(),1));
    aux.push_back(Polynomial(1,_polynomial.getPrime(),1));
    
    // Divide until we hit 1
    while(p1.size()>1 && p2.size()>1){
        Polynomial quot = p1/p2;
        Polynomial rem = p1%p2;
        Polynomial a_n = aux.back();
        a_n*=quot;
        a_n+=aux[aux.size()-2];
        aux.push_back(a_n);
        p1 = p2;
        p2 = rem;
    }
    
    return GaloisPolynomial(aux.back());
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %rbx
movq 0x8(%rsi), %rax
subq (%rsi), %rax
shrq $0x2, %rax
testl %eax, %eax
movq %rdi, 0x60(%rsp)
jle 0x47b4
leaq 0xab69(%rip), %rsi      # 0xf118
leaq 0x20(%rsp), %rdi
callq 0x2ffc
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x2ffc
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movl 0x18(%rbx), %edx
movq %rsp, %rdi
xorl %esi, %esi
movl $0x1, %ecx
callq 0x2e1c
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
callq 0x52ca
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x460c
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movl 0x18(%rbx), %edx
movq %rsp, %rdi
movl $0x1, %esi
movl $0x1, %ecx
callq 0x2e1c
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
callq 0x52ca
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4644
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
shrq $0x2, %rax
cmpl $0x2, %eax
jl 0x477e
movq %rsp, %r14
leaq 0x20(%rsp), %r15
leaq 0x68(%rsp), %r12
leaq 0x88(%rsp), %r13
leaq 0xa8(%rsp), %rbp
leaq 0x40(%rsp), %rbx
movq 0x70(%rsp), %rax
subq 0x68(%rsp), %rax
shrq $0x2, %rax
cmpl $0x2, %eax
jl 0x477e
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3c26
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3bb2
movq 0x48(%rsp), %rsi
addq $-0x20, %rsi
movq %rbp, %rdi
callq 0x2ffc
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3376
movq 0x48(%rsp), %rsi
addq $-0x40, %rsi
movq %rbp, %rdi
callq 0x30e0
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4cea
movq %r15, %rdi
movq %r12, %rsi
callq 0x4e52
movl 0x80(%rsp), %eax
movl %eax, 0x38(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x4e52
movl 0xa0(%rsp), %eax
movl %eax, 0x80(%rsp)
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x4734
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x4751
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4767
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
shrq $0x2, %rax
cmpl $0x1, %eax
jg 0x467d
movq 0x48(%rsp), %rsi
addq $-0x20, %rsi
movq 0x60(%rsp), %rdi
callq 0x449e
leaq 0x40(%rsp), %rdi
callq 0x4d16
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x47d9
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x47d9
movq %rdi, %r14
movl 0x18(%rbx), %edx
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1, %ecx
callq 0x2e1c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x449e
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x47f0
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x60(%rsp), %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4813
jmp 0x481b
jmp 0x4825
jmp 0x481b
jmp 0x4825
jmp 0x481b
movq %rax, %rbx
jmp 0x489e
movq %rax, %rbx
jmp 0x487d
movq %rax, %rbx
jmp 0x484a
movq %rax, %rbx
jmp 0x4867
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x484a
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x4867
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x487d
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
leaq 0x40(%rsp), %rdi
callq 0x4d16
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x489e
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x48b5
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x21b0
nop