// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xd8, %rsp
movl $0x0, 0xd4(%rsp)
movq $0x0, 0xb8(%rsp)
movq $0x0, 0xa0(%rsp)
movq $0x0, 0x98(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x102e(%rip), %rsi      # 0x5280
movl $0x40, %edx
callq 0x1090
leaq 0x127d(%rip), %rdi      # 0x54e0
movl $0x3e8, %esi            # imm = 0x3E8
leaq 0x1291(%rip), %rdx      # 0x5500
leaq 0x1296(%rip), %rcx      # 0x550c
leaq 0x1294(%rip), %r8       # 0x5511
movb $0x0, %al
callq 0x1040
movq %rsp, 0x90(%rsp)
movq 0x90(%rsp), %rax
addq $-0x8000, %rax          # imm = 0x8000
movq %rax, 0x90(%rsp)
movq $0x0, 0x20(%rsp)
cmpq $0x3e8, 0x20(%rsp)      # imm = 0x3E8
jae 0x44bd
movq 0x20(%rsp), %rax
movl $0xa, %ecx
xorl %edx, %edx
divq %rcx
cmpq $0x0, %rdx
jne 0x430d
imulq $0x64, 0x20(%rsp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
leaq 0x1230(%rip), %rdi      # 0x5519
movb $0x0, %al
callq 0x1040
movq 0x2ce9(%rip), %rax      # 0x6fe0
movq (%rax), %rdi
callq 0x10a0
leaq 0x121a(%rip), %rdi      # 0x5520
movb $0x0, %al
callq 0x1040
leaq 0x6b(%rsp), %rdi
callq 0x34d0
leaq 0x1f(%rsp), %rdi
movl $0x1, %esi
callq 0x11d0
movq $0x0, 0x10(%rsp)
cmpq $0x8000, 0x10(%rsp)     # imm = 0x8000
jae 0x435e
movb 0x1f(%rsp), %dl
movq 0x90(%rsp), %rax
movq 0x10(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x432f
callq 0x1030
movq %rax, 0xc8(%rsp)
callq 0x4580
movq %rax, 0xb0(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x35b0
testb $0x1, %al
jne 0x4388
jmp 0x438a
jmp 0x43a9
leaq 0x1194(%rip), %rdi      # 0x5525
leaq 0x119c(%rip), %rsi      # 0x5534
movl $0x6a, %edx
leaq 0x1198(%rip), %rcx      # 0x553c
callq 0x1050
leaq 0x28(%rsp), %rsi
leaq 0x6b(%rsp), %rdx
movq %rsi, %rdi
callq 0x3af0
callq 0x4580
movq %rax, 0xa8(%rsp)
callq 0x1030
movq %rax, 0xc0(%rsp)
movq 0xa8(%rsp), %rax
subq 0xb0(%rsp), %rax
addq 0xa0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xc0(%rsp), %rax
subq 0xc8(%rsp), %rax
addq 0xb8(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x90(%rsp), %rax
movzbl (%rax), %eax
movzbl 0x1f(%rsp), %ecx
cmpl %ecx, %eax
jne 0x442b
jmp 0x444a
leaq 0x1115(%rip), %rdi      # 0x5547
leaq 0x10fb(%rip), %rsi      # 0x5534
movl $0x78, %edx
leaq 0x10f7(%rip), %rcx      # 0x553c
callq 0x1050
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rax
movl $0x8000, %ecx           # imm = 0x8000
subq 0x98(%rsp), %rcx
cmpq %rcx, %rax
jae 0x44a8
movq 0x90(%rsp), %rax
movq 0x8(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movzbl 0x1f(%rsp), %ecx
cmpl %ecx, %eax
je 0x4496
movl $0x8000, %eax           # imm = 0x8000
subq 0x8(%rsp), %rax
movq %rax, 0x98(%rsp)
jmp 0x4498
movq 0x8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x8(%rsp)
jmp 0x4453
jmp 0x44aa
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0x42ab
leaq 0x1094(%rip), %rdi      # 0x5558
movl $0x3e8, %esi            # imm = 0x3E8
movb $0x0, %al
callq 0x1040
movq 0xa0(%rsp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
vcvtusi2sdq 0xa0(%rsp), %xmm0, %xmm1
vmovsd 0xfcb(%rip), %xmm0      # 0x54c0
vmulsd %xmm1, %xmm0, %xmm0
vmovsd 0xfaf(%rip), %xmm1      # 0x54b0
vdivsd %xmm1, %xmm0, %xmm0
leaq 0x105d(%rip), %rdi      # 0x5569
movb $0x1, %al
callq 0x1040
vcvtsi2sdq 0xb8(%rsp), %xmm0, %xmm1
vmovsd 0xf8b(%rip), %xmm0      # 0x54b0
vmulsd %xmm1, %xmm0, %xmm0
vmovsd 0xf87(%rip), %xmm1      # 0x54b8
vdivsd %xmm1, %xmm0, %xmm0
vmovsd 0xf73(%rip), %xmm1      # 0x54b0
vdivsd %xmm1, %xmm0, %xmm0
leaq 0x1041(%rip), %rdi      # 0x5589
movb $0x1, %al
callq 0x1040
movq 0x98(%rsp), %rsi
leaq 0x1046(%rip), %rdi      # 0x55a4
movb $0x0, %al
callq 0x1040
movl 0xd4(%rsp), %eax
addq $0xd8, %rsp
retq
nopw %cs:(%rax,%rax)