// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
jle 0x3b79
leaq 0x9721(%rip), %rsi      # 0xd128
leaq 0x48(%rsp), %rdi
callq 0x2ce0
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x2ce0
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movl 0x18(%rbx), %edx
leaq 0x28(%rsp), %rdi
pushq $0x1
popq %rcx
xorl %esi, %esi
callq 0x2b9c
movq %rsp, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4638
leaq 0x28(%rsp), %rdi
callq 0x4360
movl 0x18(%rbx), %edx
leaq 0x28(%rsp), %rdi
pushq $0x1
popq %rcx
movl %ecx, %esi
callq 0x2b9c
movq %rsp, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4638
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x4360
leaq 0x48(%rsp), %r15
leaq 0x68(%rsp), %r12
leaq 0xa8(%rsp), %r13
leaq 0x88(%rsp), %rbp
movq %rsp, %rbx
movq 0x50(%rsp), %rax
subq 0x48(%rsp), %rax
shrq $0x2, %rax
cmpl $0x2, %eax
jl 0x3b52
movq 0x70(%rsp), %rax
subq 0x68(%rsp), %rax
shrq $0x2, %rax
cmpl $0x2, %eax
jl 0x3b52
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3580
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3528
movq 0x8(%rsp), %rsi
addq $-0x20, %rsi
movq %rbp, %rdi
callq 0x2ce0
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2f38
movq 0x8(%rsp), %rsi
addq $-0x40, %rsi
movq %rbp, %rdi
callq 0x2d6c
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4012
movq %r15, %rdi
movq %r12, %rsi
callq 0x403e
movq %r12, %rdi
movq %r13, %rsi
callq 0x403e
movq %rbp, %rdi
callq 0x4360
movq %r13, %rdi
callq 0x4360
movq %r14, %rdi
callq 0x4360
jmp 0x3a9d
movq 0x8(%rsp), %rsi
addq $-0x20, %rsi
movq 0x20(%rsp), %rdi
callq 0x391c
movq %rsp, %rdi
callq 0x4060
leaq 0x68(%rsp), %rdi
callq 0x4360
jmp 0x3b9c
movq %rdi, %r14
movl 0x18(%rbx), %edx
leaq 0x48(%rsp), %rbx
pushq $0x1
popq %rcx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2b9c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x391c
leaq 0x48(%rsp), %rdi
callq 0x4360
movq 0x20(%rsp), %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3bc9
jmp 0x3bd3
jmp 0x3bce
jmp 0x3bd3
jmp 0x3bce
jmp 0x3bd3
movq %rax, %rbx
jmp 0x3c16
movq %rax, %rbx
jmp 0x3bfa
movq %rax, %rbx
jmp 0x3c04
movq %rax, %rbx
jmp 0x3bed
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x4360
leaq 0xa8(%rsp), %rdi
callq 0x4360
leaq 0x28(%rsp), %rdi
callq 0x4360
movq %rsp, %rdi
callq 0x4060
leaq 0x68(%rsp), %rdi
callq 0x4360
leaq 0x48(%rsp), %rdi
callq 0x4360
movq %rbx, %rdi
callq 0x21c0