// File: /workspace/github_cpp_makefile_star2_O3/simap[P]ecctools/lib/uECC.c
// Function: uECC_vli_modInv(unsigned long*, unsigned long const*, unsigned long const*, signed char)
uECC_VLI_API void uECC_vli_modInv(uECC_word_t *result,
                                  const uECC_word_t *input,
                                  const uECC_word_t *mod,
                                  wordcount_t num_words) {
    uECC_word_t a[uECC_MAX_WORDS], b[uECC_MAX_WORDS], u[uECC_MAX_WORDS], v[uECC_MAX_WORDS];
    cmpresult_t cmpResult;

    if (uECC_vli_isZero(input, num_words)) {
        uECC_vli_clear(result, num_words);
        return;
    }

    uECC_vli_set(a, input, num_words);
    uECC_vli_set(b, mod, num_words);
    uECC_vli_clear(u, num_words);
    u[0] = 1;
    uECC_vli_clear(v, num_words);
    while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) {
        if (EVEN(a)) {
            uECC_vli_rshift1(a, num_words);
            vli_modInv_update(u, mod, num_words);
        } else if (EVEN(b)) {
            uECC_vli_rshift1(b, num_words);
            vli_modInv_update(v, mod, num_words);
        } else if (cmpResult > 0) {
            uECC_vli_sub(a, a, b, num_words);
            uECC_vli_rshift1(a, num_words);
            if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) {
                uECC_vli_add(u, u, mod, num_words);
            }
            uECC_vli_sub(u, u, v, num_words);
            vli_modInv_update(u, mod, num_words);
        } else {
            uECC_vli_sub(b, b, a, num_words);
            uECC_vli_rshift1(b, num_words);
            if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) {
                uECC_vli_add(v, v, mod, num_words);
            }
            uECC_vli_sub(v, v, u, num_words);
            vli_modInv_update(v, mod, num_words);
        }
    }
    uECC_vli_set(result, u, num_words);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %ecx, %ebx
testb %bl, %bl
jle 0x33f3
movq %rdx, %r14
movzbl %bl, %ebp
xorl %ecx, %ecx
xorl %eax, %eax
orq (%rsi,%rcx,8), %rax
incq %rcx
cmpq %rcx, %rbp
jne 0x30ec
leal (,%rbp,8), %r15d
testq %rax, %rax
je 0x3405
movq %rdi, 0x58(%rsp)
leaq 0x80(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
callq 0x1070
leaq 0x60(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1070
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1050
leaq 0x10(%rsp), %rax
movq $0x1, (%rax)
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movq %r15, 0x50(%rsp)
movq %r15, %rdx
callq 0x1050
leal (,%rbp,8), %eax
leaq (%rsp,%rax), %rcx
addq $0x60, %rcx
movq %rcx, 0x8(%rsp)
leaq (%rsp,%rax), %r15
addq $0x80, %r15
leaq -0x1(%rbp), %rax
movq %rax, (%rsp)
movsbl %bl, %ebx
movq %rbp, %rax
decq %rax
testb %al, %al
js 0x33df
movl %eax, %edx
andl $0x7f, %edx
shll $0x3, %edx
movq 0x80(%rsp,%rdx), %rcx
movq 0x60(%rsp,%rdx), %rdx
cmpq %rdx, %rcx
je 0x3190
testb $0x1, 0x80(%rsp)
jne 0x31f2
movq %r15, %rax
xorl %ecx, %ecx
movq -0x8(%rax), %rdx
movq %rdx, %rsi
shrq %rsi
orq %rcx, %rsi
movq %rsi, -0x8(%rax)
leaq -0x8(%rax), %rsi
shlq $0x3f, %rdx
movq %rsi, %rax
movq %rdx, %rcx
cmpq %r12, %rsi
ja 0x31c4
leaq 0x10(%rsp), %rdi
jmp 0x33d0
testb $0x1, 0x60(%rsp)
jne 0x3229
movq 0x8(%rsp), %rax
xorl %ecx, %ecx
movq -0x8(%rax), %rdx
movq %rdx, %rsi
shrq %rsi
orq %rcx, %rsi
movq %rsi, -0x8(%rax)
leaq -0x8(%rax), %rsi
shlq $0x3f, %rdx
movq %rsi, %rax
movq %rdx, %rcx
cmpq %r13, %rsi
ja 0x3200
jmp 0x33cb
xorl %eax, %eax
cmpq %rdx, %rcx
jbe 0x3301
xorl %ecx, %ecx
movq 0x60(%rsp,%rax,8), %rdx
addq %rcx, %rdx
xorl %esi, %esi
subq %rdx, 0x80(%rsp,%rax,8)
setb %sil
testq %rdx, %rdx
cmovneq %rsi, %rcx
incq %rax
cmpq %rax, %rbp
jne 0x3236
movq %r15, %rax
xorl %ecx, %ecx
movq -0x8(%rax), %rdx
movq %rdx, %rsi
shrq %rsi
orq %rcx, %rsi
movq %rsi, -0x8(%rax)
leaq -0x8(%rax), %rsi
shlq $0x3f, %rdx
movq %rsi, %rax
movq %rdx, %rcx
cmpq %r12, %rsi
ja 0x3260
movq (%rsp), %rax
testb %al, %al
js 0x32d6
movl %eax, %edx
andl $0x7f, %edx
shll $0x3, %edx
movq 0x10(%rsp,%rdx), %rcx
movq 0x30(%rsp,%rdx), %rdx
cmpq %rdx, %rcx
ja 0x32d6
decq %rax
cmpq %rdx, %rcx
jae 0x3288
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x10(%rsp,%rax,8), %rdx
leaq (%rdx,%rcx), %rsi
addq (%r14,%rax,8), %rsi
xorl %edi, %edi
cmpq %rdx, %rsi
setb %dil
cmovneq %rdi, %rcx
movq %rsi, 0x10(%rsp,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x32af
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x30(%rsp,%rax,8), %rdx
addq %rcx, %rdx
xorl %esi, %esi
subq %rdx, 0x10(%rsp,%rax,8)
setb %sil
testq %rdx, %rdx
cmovneq %rsi, %rcx
incq %rax
cmpq %rax, %rbp
jne 0x32da
jmp 0x31e8
xorl %ecx, %ecx
movq 0x80(%rsp,%rax,8), %rdx
addq %rcx, %rdx
xorl %esi, %esi
subq %rdx, 0x60(%rsp,%rax,8)
setb %sil
testq %rdx, %rdx
cmovneq %rsi, %rcx
incq %rax
cmpq %rax, %rbp
jne 0x3303
movq 0x8(%rsp), %rax
xorl %ecx, %ecx
movq -0x8(%rax), %rdx
movq %rdx, %rsi
shrq %rsi
orq %rcx, %rsi
movq %rsi, -0x8(%rax)
leaq -0x8(%rax), %rsi
shlq $0x3f, %rdx
movq %rsi, %rax
movq %rdx, %rcx
cmpq %r13, %rsi
ja 0x332f
movq (%rsp), %rax
testb %al, %al
js 0x33a5
movl %eax, %edx
andl $0x7f, %edx
shll $0x3, %edx
movq 0x30(%rsp,%rdx), %rcx
movq 0x10(%rsp,%rdx), %rdx
cmpq %rdx, %rcx
ja 0x33a5
decq %rax
cmpq %rdx, %rcx
jae 0x3357
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x30(%rsp,%rax,8), %rdx
leaq (%rdx,%rcx), %rsi
addq (%r14,%rax,8), %rsi
xorl %edi, %edi
cmpq %rdx, %rsi
setb %dil
cmovneq %rdi, %rcx
movq %rsi, 0x30(%rsp,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x337e
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x10(%rsp,%rax,8), %rdx
addq %rcx, %rdx
xorl %esi, %esi
subq %rdx, 0x30(%rsp,%rax,8)
setb %sil
testq %rdx, %rdx
cmovneq %rsi, %rcx
incq %rax
cmpq %rax, %rbp
jne 0x33a9
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x557c
jmp 0x318d
leaq 0x10(%rsp), %rsi
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rdx
callq 0x1070
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %esi, %esi
movq %r15, %rdx
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1050