// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movl %ecx, %ebx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
movq %rsi, %rdi
movl %ecx, %esi
callq 0x1d5d
testq %rax, %rax
je 0x2c42
movsbl %bl, %esi
movq %r15, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3272
movq %r15, 0x8(%rsp)
movsbl %bl, %ebp
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x3017
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x3017
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movl %ebp, %esi
callq 0x3272
movq $0x1, (%r13)
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3272
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x1f1e
testb %al, %al
je 0x2d83
testb $0x1, 0x50(%rsp)
jne 0x2cbc
movq %r15, %rdi
movl %ebp, %esi
callq 0x379a
jmp 0x2d21
testb $0x1, 0x30(%rsp)
jne 0x2cd2
movq %r12, %rdi
movl %ebp, %esi
callq 0x379a
jmp 0x2d71
testb %al, %al
jle 0x2d26
movq %r15, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %ebp, %ecx
callq 0x1e48
movq %r15, %rdi
movl %ebp, %esi
callq 0x379a
movq %r13, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x1f1e
testb %al, %al
jns 0x2d11
movq %r13, %rdi
movq %r13, %rsi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x3763
movq %r13, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x1e48
movq %r13, %rdi
jmp 0x2d74
movq %r12, %rdi
movq %r12, %rsi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x1e48
movq %r12, %rdi
movl %ebp, %esi
callq 0x379a
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x1f1e
testb %al, %al
jns 0x2d61
movq %rbx, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x3763
movq %rbx, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x1e48
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x45f8
jmp 0x2c94
leaq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdi
movl %ebp, %edx
callq 0x3017
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq