// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
leaq 0x8d8(%rip), %rax       # 0x4280
vmovupd (%rax), %ymm0
vmovups 0x20(%rax), %ymm1
leaq 0x60(%rsp), %r12
vmovups %ymm1, 0x20(%r12)
vmovupd %ymm0, (%r12)
leaq 0xb16(%rip), %rdi       # 0x44e0
leaq 0xb2f(%rip), %rdx       # 0x4500
leaq 0xb34(%rip), %rcx       # 0x450c
leaq 0xb32(%rip), %r8        # 0x4511
movl $0x3e8, %esi            # imm = 0x3E8
xorl %eax, %eax
vzeroupper
callq 0x1040
movq %rsp, 0x18(%rsp)
addq $-0x8000, 0x18(%rsp)    # imm = 0x8000
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
xorl %eax, %eax
cmpq $0x3e8, %rax            # imm = 0x3E8
je 0x3b48
movq %rax, 0x28(%rsp)
xorl %edx, %edx
movw $0xa, %cx
divw %cx
testw %dx, %dx
jne 0x3a59
movzwl %ax, %esi
leaq 0xae4(%rip), %rdi       # 0x4519
xorl %eax, %eax
callq 0x1040
movq 0x259d(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
callq 0x1070
leaq 0xace(%rip), %rdi       # 0x4520
xorl %eax, %eax
callq 0x1040
leaq 0x3b(%rsp), %rdi
callq 0x3060
leaq 0xf(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x11ac
xorl %eax, %eax
cmpq $0x8000, %rax           # imm = 0x8000
je 0x3a8b
movb 0xf(%rsp), %cl
movq 0x18(%rsp), %rdx
movb %cl, (%rdx,%rax)
incq %rax
jmp 0x3a72
movq %r15, 0x30(%rsp)
callq 0x1030
movq %rax, 0x20(%rsp)
rdtsc
movl %edx, %ebp
movl %eax, %r14d
movq %r12, %rdi
callq 0x30e2
testb %al, %al
je 0x3bfe
movq %r12, %rdi
movq %r12, %r13
movq %r12, %rsi
leaq 0x3b(%rsp), %rdx
callq 0x34cf
rdtsc
movl %edx, %r12d
movl %eax, %r15d
callq 0x1030
movq 0x18(%rsp), %rcx
movb (%rcx), %dl
cmpb 0xf(%rsp), %dl
jne 0x3be5
negl %ebp
shlq $0x20, %rbp
movl %r14d, %esi
movl %r15d, %edi
shlq $0x20, %r12
addq %rdi, %r12
movq 0x10(%rsp), %rdi
subq %rsi, %rdi
addq %rbp, %rdi
addq %r12, %rdi
movq %rdi, 0x10(%rsp)
movq 0x30(%rsp), %r15
subq 0x20(%rsp), %r15
addq %rax, %r15
movl $0x8000, %eax           # imm = 0x8000
xorl %esi, %esi
movq %r13, %r12
movl $0x8000, %edi           # imm = 0x8000
subq %rbx, %rdi
cmpq %rdi, %rsi
jae 0x3b3b
cmpb %dl, (%rcx,%rsi)
cmovneq %rax, %rbx
incq %rsi
decq %rax
jmp 0x3b1f
movq 0x28(%rsp), %rax
incq %rax
jmp 0x3a0c
leaq 0xa09(%rip), %rdi       # 0x4558
movl $0x3e8, %r14d           # imm = 0x3E8
movl $0x3e8, %esi            # imm = 0x3E8
xorl %eax, %eax
callq 0x1040
movq 0x10(%rsp), %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %r14
vcvtusi2sd %rcx, %xmm2, %xmm0
vmulsd 0x934(%rip), %xmm0, %xmm0 # 0x44b0
vdivsd 0x934(%rip), %xmm0, %xmm0 # 0x44b8
leaq 0x9de(%rip), %rdi       # 0x4569
movq %rax, %rsi
movb $0x1, %al
callq 0x1040
vcvtsi2sd %r15, %xmm2, %xmm0
vmovsd 0x916(%rip), %xmm1      # 0x44b8
vmulsd %xmm1, %xmm0, %xmm0
vdivsd 0x912(%rip), %xmm0, %xmm0 # 0x44c0
vdivsd %xmm1, %xmm0, %xmm0
leaq 0x9d0(%rip), %rdi       # 0x4589
movb $0x1, %al
callq 0x1040
leaq 0x9dd(%rip), %rdi       # 0x45a4
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1040
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x95b(%rip), %rdi       # 0x4547
leaq 0x941(%rip), %rsi       # 0x4534
leaq 0x942(%rip), %rcx       # 0x453c
pushq $0x78
jmp 0x3c15
leaq 0x920(%rip), %rdi       # 0x4525
leaq 0x928(%rip), %rsi       # 0x4534
leaq 0x929(%rip), %rcx       # 0x453c
pushq $0x6a
popq %rdx
callq 0x1050