// File: /workspace/github_cpp_makefile_star2_O1/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 0x4922
leaq 0xa9fb(%rip), %rsi      # 0xf118
leaq 0x20(%rsp), %rdi
callq 0x317c
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x317c
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 0x2f9c
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
callq 0x5420
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x477a
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movl 0x18(%rbx), %edx
movq %rsp, %rdi
movl $0x1, %esi
movl $0x1, %ecx
callq 0x2f9c
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
callq 0x5420
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x47b2
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
shrq $0x2, %rax
cmpl $0x2, %eax
jl 0x48ec
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 0x48ec
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3d90
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3d1c
movq 0x48(%rsp), %rsi
addq $-0x20, %rsi
movq %rbp, %rdi
callq 0x317c
movq %rbp, %rdi
movq %r14, %rsi
callq 0x34f4
movq 0x48(%rsp), %rsi
addq $-0x40, %rsi
movq %rbp, %rdi
callq 0x325e
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4e58
movq %r15, %rdi
movq %r12, %rsi
callq 0x4fba
movl 0x80(%rsp), %eax
movl %eax, 0x38(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x4fba
movl 0xa0(%rsp), %eax
movl %eax, 0x80(%rsp)
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x48a2
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x48bf
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x48d5
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
shrq $0x2, %rax
cmpl $0x1, %eax
jg 0x47eb
movq 0x48(%rsp), %rsi
addq $-0x20, %rsi
movq 0x60(%rsp), %rdi
callq 0x460c
leaq 0x40(%rsp), %rdi
callq 0x4e84
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x4947
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x4947
movq %rdi, %r14
movl 0x18(%rbx), %edx
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1, %ecx
callq 0x2f9c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x460c
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x495e
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 0x4981
jmp 0x4989
jmp 0x4993
jmp 0x4989
jmp 0x4993
jmp 0x4989
movq %rax, %rbx
jmp 0x4a0c
movq %rax, %rbx
jmp 0x49eb
movq %rax, %rbx
jmp 0x49b8
movq %rax, %rbx
jmp 0x49d5
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x49b8
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x49d5
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x49eb
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
leaq 0x40(%rsp), %rdi
callq 0x4e84
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x4a0c
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4a23
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x21b0
nop