// File: occivink[P]pyct/monocypher.cpp
// Function: invsqrt(int*, int const*)
static int invsqrt(fe isr, const fe x)
{
    fe t0, t1, t2;

    // t0 = x^((p-5)/8)
    // Can be achieved with a simple double & add ladder,
    // but it would be slower.
    fe_sq(t0, x);
    fe_sq(t1,t0);                   fe_sq(t1, t1);  fe_mul(t1, x, t1);
    fe_mul(t0, t0, t1);
    fe_sq(t0, t0);                                  fe_mul(t0, t1, t0);
    fe_sq(t1, t0);  FOR (i, 1,   5) fe_sq(t1, t1);  fe_mul(t0, t1, t0);
    fe_sq(t1, t0);  FOR (i, 1,  10) fe_sq(t1, t1);  fe_mul(t1, t1, t0);
    fe_sq(t2, t1);  FOR (i, 1,  20) fe_sq(t2, t2);  fe_mul(t1, t2, t1);
    fe_sq(t1, t1);  FOR (i, 1,  10) fe_sq(t1, t1);  fe_mul(t0, t1, t0);
    fe_sq(t1, t0);  FOR (i, 1,  50) fe_sq(t1, t1);  fe_mul(t1, t1, t0);
    fe_sq(t2, t1);  FOR (i, 1, 100) fe_sq(t2, t2);  fe_mul(t1, t2, t1);
    fe_sq(t1, t1);  FOR (i, 1,  50) fe_sq(t1, t1);  fe_mul(t0, t1, t0);
    fe_sq(t0, t0);  FOR (i, 1,   2) fe_sq(t0, t0);  fe_mul(t0, t0, x);

    // quartic = x^((p-1)/4)
    i32 *quartic = t1;
    fe_sq (quartic, t0);
    fe_mul(quartic, quartic, x);

    i32 *check = t2;
    fe_0  (check);          int z0 = fe_isequal(x      , check);
    fe_1  (check);          int p1 = fe_isequal(quartic, check);
    fe_neg(check, check );  int m1 = fe_isequal(quartic, check);
    fe_neg(check, sqrtm1);  int ms = fe_isequal(quartic, check);

    // if quartic == -1 or sqrt(-1)
    // then  isr = x^((p-1)/4) * sqrt(-1)
    // else  isr = x^((p-1)/4)
    fe_mul(isr, t0, sqrtm1);
    fe_ccopy(isr, t0, 1 - (m1 | ms));

    WIPE_BUFFER(t0);
    WIPE_BUFFER(t1);
    WIPE_BUFFER(t2);
    return p1 | m1 | z0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x10580
leaq -0x70(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x10580
leaq -0x70(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x10580
leaq -0x70(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0xec00
leaq -0x40(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0xec00
leaq -0x40(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x10580
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0xec00
leaq -0x70(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x10580
movq $0x1, -0xa8(%rbp)
cmpq $0x5, -0xa8(%rbp)
jae 0x10e4d
leaq -0x70(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x10580
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x10e22
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0xec00
leaq -0x70(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x10580
movq $0x1, -0xb0(%rbp)
cmpq $0xa, -0xb0(%rbp)
jae 0x10ea1
leaq -0x70(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x10580
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x10e76
leaq -0x70(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0xec00
leaq -0xa0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x10580
movq $0x1, -0xb8(%rbp)
cmpq $0x14, -0xb8(%rbp)
jae 0x10efe
leaq -0xa0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x10580
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0x10ecd
leaq -0x70(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0xec00
leaq -0x70(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x10580
movq $0x1, -0xc0(%rbp)
cmpq $0xa, -0xc0(%rbp)
jae 0x10f55
leaq -0x70(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x10580
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x10f2a
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0xec00
leaq -0x70(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x10580
movq $0x1, -0xc8(%rbp)
cmpq $0x32, -0xc8(%rbp)
jae 0x10fa9
leaq -0x70(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x10580
movq -0xc8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc8(%rbp)
jmp 0x10f7e
leaq -0x70(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0xec00
leaq -0xa0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x10580
movq $0x1, -0xd0(%rbp)
cmpq $0x64, -0xd0(%rbp)
jae 0x11006
leaq -0xa0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x10580
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x10fd5
leaq -0x70(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0xec00
leaq -0x70(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x10580
movq $0x1, -0xd8(%rbp)
cmpq $0x32, -0xd8(%rbp)
jae 0x1105d
leaq -0x70(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x10580
movq -0xd8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0x11032
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0xec00
leaq -0x40(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x10580
movq $0x1, -0xe0(%rbp)
cmpq $0x2, -0xe0(%rbp)
jae 0x110b1
leaq -0x40(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x10580
movq -0xe0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe0(%rbp)
jmp 0x11086
leaq -0x40(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rdx
callq 0xec00
leaq -0x70(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x10580
movq -0xe8(%rbp), %rdi
movq -0xe8(%rbp), %rsi
movq -0x10(%rbp), %rdx
callq 0xec00
leaq -0xa0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rdi
callq 0x128c0
movq -0x10(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x132d0
movl %eax, -0xf4(%rbp)
movq -0xf0(%rbp), %rdi
callq 0x12870
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x132d0
movl %eax, -0xf8(%rbp)
movq -0xf0(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x11290
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x132d0
movl %eax, -0xfc(%rbp)
movq -0xf0(%rbp), %rdi
leaq 0xafcd(%rip), %rsi      # 0x1c150
callq 0x11290
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x132d0
movl %eax, -0x100(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq 0xafa0(%rip), %rdx      # 0x1c150
callq 0xec00
movq -0x8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movl -0xfc(%rbp), %eax
orl -0x100(%rbp), %eax
movl $0x1, %edx
subl %eax, %edx
callq 0x11220
leaq -0x40(%rbp), %rdi
movl $0x28, %esi
callq 0x3890
leaq -0x70(%rbp), %rdi
movl $0x28, %esi
callq 0x3890
leaq -0xa0(%rbp), %rdi
movl $0x28, %esi
callq 0x3890
movl -0xf8(%rbp), %eax
orl -0xfc(%rbp), %eax
orl -0xf4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)