// File: /workspace/github_c_makefile_star2_O2/vusec[P]fpvi-scsb/leak_rate_win_size/leak_rate.c
// Function: leak_run
double leak_run(leak_func_t leak)
{
    int correct;
    int no_hit;
    struct timespec before, after;

    set_cpu(EXECUTER_CORE);
    no_hit = 0;

    clock_gettime(CLOCK_REALTIME, &before);
    for(int i=0; i<SECRET_SIZE; i++)
    {
        memset(results, 0, sizeof(results));
        correct = secret[i]&LEAK_MASK;

        for(int j=0; j<MAX_RETRIES; j++)
        {
            flush(reload_buf);
            leak(reload_buf, &secret[i]);
            reload(reload_buf, results);
            if(results[correct] != 0) break;
        }

        if(results[correct] == 0)
        {   
            //printf("0x%04x ", i);
            no_hit++;
        }
    }
    clock_gettime(CLOCK_REALTIME, &after);

    double time = after.tv_sec - before.tv_sec + (after.tv_nsec - before.tv_nsec) / 1000000000.0;
    double rate = (double)((SECRET_SIZE-no_hit)*LEAK_SIZE_LOG/time)/(1024*1024);
    printf("%20.3f;%20.3f;%20d;\n",
            time,
            rate,
            no_hit
          );

    return rate;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x40b064
leaq 0x18(%rsp), %rsi
xorl %edi, %edi
callq 0x401060
leaq 0x16b32(%rip), %rbp     # 0x419000
pushq $0xb
popq %r13
xorl %ebx, %ebx
cmpq $0x200000, %r12         # imm = 0x200000
je 0x402600
movq %rbx, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x16b80(%rip)    # 0x419070
movaps %xmm0, 0x16b69(%rip)    # 0x419060
movaps %xmm0, 0x16b52(%rip)    # 0x419050
movaps %xmm0, 0x16b3b(%rip)    # 0x419040
movaps %xmm0, 0x16b24(%rip)    # 0x419030
movaps %xmm0, 0x16b0d(%rip)    # 0x419020
movaps %xmm0, 0x16af6(%rip)    # 0x419010
movaps %xmm0, 0x16adf(%rip)    # 0x419000
leaq 0x17ad8(%rip), %rax     # 0x41a000
leaq (%rax,%r12), %r15
movq %r12, 0x10(%rsp)
movzbl (%r12,%rax), %ebx
andl $0xf, %ebx
xorl %r12d, %r12d
cmpl $0x80, %r12d
je 0x4025e1
movq 0x16b30(%rip), %rax     # 0x419080
xorl %ecx, %ecx
cmpq $0x4000, %rcx           # imm = 0x4000
je 0x40256b
leaq (%rax,%rcx), %rdx
clflush (%rdx)
addq $0x400, %rcx            # imm = 0x400
jmp 0x402552
mfence
movq 0x16b0b(%rip), %rdi     # 0x419080
movq %r15, %rsi
callq *%r14
movq 0x16afe(%rip), %rdi     # 0x419080
mfence
movq %r13, %r8
cmpq $0x7b, %r8
je 0x4025ce
movl %r8d, %r9d
andl $0xf, %r9d
movl %r9d, %r10d
shll $0xa, %r10d
rdtscp
movq %rdx, %rsi
shlq $0x20, %rsi
orq %rax, %rsi
movb (%rdi,%r10), %al
rdtscp
shlq $0x20, %rdx
orq %rax, %rdx
subq %rsi, %rdx
cmpq $0x9f, %rdx
ja 0x4025c8
incq (%rbp,%r9,8)
addq $0x7, %r8
jmp 0x402588
incl %r12d
cmpq $0x0, (%rbp,%rbx,8)
je 0x40253c
xorl %eax, %eax
jmp 0x4025ec
xorl %eax, %eax
cmpq $0x0, (%rbp,%rbx,8)
sete %al
movq 0x8(%rsp), %rbx
addl %eax, %ebx
movq 0x10(%rsp), %r12
incq %r12
jmp 0x4024d4
leaq 0x28(%rsp), %r14
xorl %edi, %edi
movq %r14, %rsi
callq 0x401060
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq 0x18(%rsp), %rax
cvtsi2sd %rax, %xmm2
subq 0x20(%rsp), %rcx
cvtsi2sd %rcx, %xmm0
divsd 0x99d6(%rip), %xmm0     # 0x40c008
leal (,%rbx,4), %eax
movl $0x800000, %ecx         # imm = 0x800000
subl %eax, %ecx
cvtsi2sd %ecx, %xmm1
addsd %xmm2, %xmm0
divsd %xmm0, %xmm1
mulsd 0x99bc(%rip), %xmm1     # 0x40c010
movsd %xmm1, 0x8(%rsp)
leaq 0x9acc(%rip), %rdi      # 0x40c12d
movl %ebx, %esi
movb $0x2, %al
callq 0x401090
movsd 0x8(%rsp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq