// File: /workspace/github_cpp_makefile_star2_O1/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, %ebp
testb %bpl, %bpl
jle 0x3543
movq %rdx, %r15
movzbl %bpl, %r14d
xorl %eax, %eax
xorl %ecx, %ecx
orq (%rsi,%rax,8), %rcx
incq %rax
cmpq %rax, %r14
jne 0x3191
testq %rcx, %rcx
je 0x3209
testb %bpl, %bpl
movq %rdi, 0x58(%rsp)
jle 0x3231
leal (,%r14,8), %r12d
leaq 0x80(%rsp), %rdi
movq %r12, %rdx
callq 0x1070
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1070
leal (,%r14,8), %r12d
movq %rsp, %r13
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1050
movq $0x1, (%r13)
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1050
jmp 0x3239
testb %bpl, %bpl
jle 0x3543
shll $0x3, %r14d
xorl %esi, %esi
movq %r14, %rdx
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1050
movq $0x1, (%rsp)
movl %r14d, %eax
shll $0x3, %eax
leaq (%rsp,%rax), %rcx
addq $0x60, %rcx
movq %rcx, 0x40(%rsp)
leaq 0x80(%rsp,%rax), %rax
movq %rax, 0x50(%rsp)
leaq -0x1(%r14), %rax
movq %rax, 0x48(%rsp)
leaq 0x80(%rsp), %rbx
movsbl %bpl, %r12d
leaq 0x60(%rsp), %r13
movq %r14, %rax
decq %rax
testb %al, %al
js 0x329b
movl %eax, %ecx
andl $0x7f, %ecx
shll $0x3, %ecx
movq 0x80(%rsp,%rcx), %rdx
movq 0x60(%rsp,%rcx), %rsi
movb $0x1, %cl
cmpq %rsi, %rdx
ja 0x329d
jae 0x3276
xorl %ecx, %ecx
testb %al, %al
js 0x3529
testb $0x1, 0x80(%rsp)
jne 0x32ee
movq %rsp, %rdi
testb %bpl, %bpl
jle 0x3519
movq 0x50(%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 %rbx, %rsi
ja 0x32c2
movq %rsp, %rdi
jmp 0x3519
testb $0x1, 0x60(%rsp)
jne 0x3333
leaq 0x20(%rsp), %rdi
testb %bpl, %bpl
jle 0x3519
movq 0x40(%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 0x330a
jmp 0x3514
testb %cl, %cl
je 0x342b
testb %bpl, %bpl
jle 0x3399
xorl %eax, %eax
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, %r14
jne 0x3344
testb %bpl, %bpl
jle 0x3399
movq 0x50(%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 %rbx, %rsi
ja 0x3375
movq 0x48(%rsp), %rax
testb %al, %al
js 0x33f5
movl %eax, %edx
andl $0x7f, %edx
shll $0x3, %edx
movq (%rsp,%rdx), %rcx
movq 0x20(%rsp,%rdx), %rdx
cmpq %rdx, %rcx
ja 0x33f5
decq %rax
cmpq %rdx, %rcx
jae 0x339e
movq %rsp, %rdi
testb %bpl, %bpl
jle 0x3519
xorl %eax, %eax
xorl %ecx, %ecx
movq (%rsp,%rax,8), %rdx
leaq (%rdx,%rcx), %rsi
addq (%r15,%rax,8), %rsi
xorl %edi, %edi
cmpq %rdx, %rsi
setb %dil
cmovneq %rdi, %rcx
movq %rsi, (%rsp,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x33d0
movq %rsp, %rdi
testb %bpl, %bpl
jle 0x3519
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x20(%rsp,%rax,8), %rdx
addq %rcx, %rdx
xorl %esi, %esi
subq %rdx, (%rsp,%rax,8)
setb %sil
testq %rdx, %rdx
cmovneq %rsi, %rcx
incq %rax
cmpq %rax, %r14
jne 0x3405
jmp 0x32e6
testb %bpl, %bpl
jle 0x3489
xorl %eax, %eax
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, %r14
jne 0x3434
testb %bpl, %bpl
jle 0x3489
movq 0x40(%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 0x3465
movq 0x48(%rsp), %rax
testb %al, %al
js 0x34e5
movl %eax, %edx
andl $0x7f, %edx
shll $0x3, %edx
movq 0x20(%rsp,%rdx), %rcx
movq (%rsp,%rdx), %rdx
cmpq %rdx, %rcx
ja 0x34e5
decq %rax
cmpq %rdx, %rcx
jae 0x348e
leaq 0x20(%rsp), %rdi
testb %bpl, %bpl
jle 0x3519
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x20(%rsp,%rax,8), %rdx
leaq (%rdx,%rcx), %rsi
addq (%r15,%rax,8), %rsi
xorl %edi, %edi
cmpq %rdx, %rsi
setb %dil
cmovneq %rdi, %rcx
movq %rsi, 0x20(%rsp,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x34be
leaq 0x20(%rsp), %rdi
testb %bpl, %bpl
jle 0x3519
xorl %eax, %eax
xorl %ecx, %ecx
movq (%rsp,%rax,8), %rdx
addq %rcx, %rdx
xorl %esi, %esi
subq %rdx, 0x20(%rsp,%rax,8)
setb %sil
testq %rdx, %rdx
cmovneq %rsi, %rcx
incq %rax
cmpq %rax, %r14
jne 0x34f3
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x574a
jmp 0x3273
testb %bpl, %bpl
movq 0x58(%rsp), %rdi
jle 0x3543
leal (,%r14,8), %edx
movq %rsp, %rsi
callq 0x1070
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq