// File: /workspace/github_c_makefile_star2_O1/ioerror[P]csidh-reference-implementation/bench.c
// Function: main
int main()
{
    clock_t t0, t1, time = 0;
    uint64_t c0, c1, cycles = 0;
    size_t bytes = 0;
    unsigned char *stack;

    private_key priv;
    public_key pub = base;
    (void) pub; /* suppress "unused variable" warning */

    printf("doing %lu iterations of%s%s%s.\n",
        its,
        val ? " validation" : "",
        val && act ? " and" : !val && !act ? " nothing" : "",
        act ? " action" : ""
    );

#ifdef X64
    __asm__ __volatile__ ("mov %%rsp, %0" : "=m"(stack));
#endif
#ifdef A64
    __asm__ __volatile__ ("mov %0, sp\n\t" : "=r"(stack)::);
#endif

    stack -= stacksz;

    for (unsigned long i = 0; i < its; ++i) {

        if (its >= 100 && i % (its / 100) == 0) {
            printf("%2lu%%", 100 * i / its);
            fflush(stdout);
            printf("\r\x1b[K");
        }

        csidh_private(&priv);

#if !defined(__OpenBSD__)
        /* spray stack */
        unsigned char canary;
        randombytes(&canary, 1);
        for (size_t j = 0; j < stacksz; ++j)
            stack[j] = canary;
#endif

        t0 = clock();   /* uses stack, but not too much */
        c0 = rdtsc();

        /**************************************/

        if (val)
            assert(validate(&pub));

        if (act)
            action(&pub, &pub, &priv);

        /**************************************/

        c1 = rdtsc();
        t1 = clock();
        cycles += c1 - c0;
        time += t1 - t0;

#if !defined(__OpenBSD__)
        /* check stack */
        assert(*stack == canary);   /* make sure we sprayed enough */
        for (size_t j = 0; j < stacksz - bytes; ++j)
            if (stack[j] != canary)
                bytes = stacksz - j;
#endif
    }

    printf("iterations: %lu\n", its);
    printf("clock cycles: %" PRIu64 " (%.1lf*10^6)\n", (uint64_t) cycles / its, 1e-6 * cycles / its);
    printf("wall-clock time: %.3lf ms\n", 1000. * time / CLOCKS_PER_SEC / its);
    printf("stack memory usage: %lu b\n", bytes);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
leaq 0x830(%rip), %rax       # 0x4280
vmovupd (%rax), %ymm0
vmovups 0x20(%rax), %ymm1
leaq 0x50(%rsp), %r14
vmovups %ymm1, 0x20(%r14)
vmovupd %ymm0, (%r14)
leaq 0xa70(%rip), %rdi       # 0x44e0
leaq 0xa89(%rip), %rdx       # 0x4500
leaq 0xa8e(%rip), %rcx       # 0x450c
leaq 0xa8c(%rip), %r8        # 0x4511
movl $0x3e8, %esi            # imm = 0x3E8
xorl %eax, %eax
vzeroupper
callq 0x1040
movq %rsp, 0x8(%rsp)
addq $-0x8000, 0x8(%rsp)     # imm = 0x8000
leaq 0x2b(%rsp), %r12
xorl %edx, %edx
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %rdx, 0x20(%rsp)
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
mulxq %rax, %rax, %rsi
rorxq $0x1, %rax, %rax
movabsq $0x1999999999999999, %rcx # imm = 0x1999999999999999
cmpq %rcx, %rax
ja 0x3b09
shrq $0x3, %rsi
leaq 0xa34(%rip), %rdi       # 0x4519
xorl %eax, %eax
callq 0x1040
movq 0x24ed(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
callq 0x1070
leaq 0xa1e(%rip), %rdi       # 0x4520
xorl %eax, %eax
callq 0x1040
movq %r12, %rdi
callq 0x30a4
movl $0x1, %esi
leaq 0x7(%rsp), %rdi
callq 0x119c
xorl %eax, %eax
movb 0x7(%rsp), %cl
movq 0x8(%rsp), %rdx
movb %cl, (%rdx,%rax)
incq %rax
cmpq $0x8000, %rax           # imm = 0x8000
jne 0x3b22
callq 0x1030
movq %rax, 0x18(%rsp)
rdtsc
movl %edx, %ebp
movl %eax, 0x14(%rsp)
movq %r14, %rdi
callq 0x311e
testb %al, %al
je 0x3cbc
movq %r14, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x355d
rdtsc
movl %edx, %r14d
movl %eax, %r12d
callq 0x1030
movq 0x8(%rsp), %rcx
movb 0x7(%rsp), %dl
cmpb %dl, (%rcx)
jne 0x3c9d
cmpq $0x8000, %rbx           # imm = 0x8000
je 0x3bb2
movl $0x8000, %esi           # imm = 0x8000
xorl %edi, %edi
cmpb %dl, (%rcx,%rdi)
cmovneq %rsi, %rbx
incq %rdi
movl $0x8000, %r8d           # imm = 0x8000
subq %rbx, %r8
decq %rsi
cmpq %r8, %rdi
jb 0x3b97
negl %ebp
shlq $0x20, %rbp
movl 0x14(%rsp), %ecx
subq %rcx, %r15
movq %r15, %rcx
addq %rbp, %rcx
movl %r12d, %edx
shlq $0x20, %r14
addq %rdx, %r14
movq %r14, %r15
addq %rcx, %r15
subq 0x18(%rsp), %r13
addq %rax, %r13
movq 0x20(%rsp), %rdx
incq %rdx
cmpq $0x3e8, %rdx            # imm = 0x3E8
leaq 0x50(%rsp), %r14
leaq 0x2b(%rsp), %r12
jne 0x3ab1
leaq 0x955(%rip), %rdi       # 0x4558
movl $0x3e8, %esi            # imm = 0x3E8
xorl %eax, %eax
callq 0x1040
movq %r15, %rdx
shrq $0x3, %rdx
movabsq $0x20c49ba5e353f7cf, %rax # imm = 0x20C49BA5E353F7CF
mulxq %rax, %rsi, %rsi
vcvtusi2sd %r15, %xmm2, %xmm0
shrq $0x4, %rsi
vmulsd 0x879(%rip), %xmm0, %xmm0 # 0x44b0
vdivsd 0x879(%rip), %xmm0, %xmm0 # 0x44b8
leaq 0x923(%rip), %rdi       # 0x4569
movb $0x1, %al
callq 0x1040
vcvtsi2sd %r13, %xmm2, %xmm0
vmovsd 0x85e(%rip), %xmm1      # 0x44b8
vmulsd %xmm1, %xmm0, %xmm0
vdivsd 0x85a(%rip), %xmm0, %xmm0 # 0x44c0
vdivsd %xmm1, %xmm0, %xmm0
leaq 0x918(%rip), %rdi       # 0x4589
movb $0x1, %al
callq 0x1040
leaq 0x925(%rip), %rdi       # 0x45a4
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1040
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8a3(%rip), %rdi       # 0x4547
leaq 0x889(%rip), %rsi       # 0x4534
leaq 0x88a(%rip), %rcx       # 0x453c
movl $0x78, %edx
callq 0x1050
leaq 0x862(%rip), %rdi       # 0x4525
leaq 0x86a(%rip), %rsi       # 0x4534
leaq 0x86b(%rip), %rcx       # 0x453c
movl $0x6a, %edx
callq 0x1050