// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rdi, %r14
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x40b064
leaq 0x8(%rsp), %rsi
xorl %edi, %edi
callq 0x401060
leaq 0x16ad2(%rip), %rbp     # 0x419000
xorl %ebx, %ebx
movq %rbx, (%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x16b32(%rip)    # 0x419070
movaps %xmm0, 0x16b1b(%rip)    # 0x419060
movaps %xmm0, 0x16b04(%rip)    # 0x419050
movaps %xmm0, 0x16aed(%rip)    # 0x419040
movaps %xmm0, 0x16ad6(%rip)    # 0x419030
movaps %xmm0, 0x16abf(%rip)    # 0x419020
movaps %xmm0, 0x16aa8(%rip)    # 0x419010
movaps %xmm0, 0x16a91(%rip)    # 0x419000
leaq 0x17a8a(%rip), %rax     # 0x41a000
leaq (%rax,%r12), %r15
movzbl (%r12,%rax), %r13d
andl $0xf, %r13d
xorl %ebx, %ebx
movq 0x16af4(%rip), %rax     # 0x419080
xorl %ecx, %ecx
leaq (%rax,%rcx), %rdx
clflush (%rdx)
addq $0x400, %rcx            # imm = 0x400
cmpq $0x4000, %rcx           # imm = 0x4000
jne 0x40258e
mfence
movq 0x16ad1(%rip), %rdi     # 0x419080
movq %r15, %rsi
callq *%r14
movq 0x16ac4(%rip), %rdi     # 0x419080
mfence
movl $0xb, %r8d
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 0x4025ff
incq (%rbp,%r9,8)
addq $0x7, %r8
cmpq $0x7b, %r8
jne 0x4025c5
cmpq $0x0, (%rbp,%r13,8)
jne 0x40261f
leal 0x1(%rbx), %eax
cmpl $0x7f, %ebx
movl %eax, %ebx
jb 0x402585
movzbl %r13b, %eax
cmpq $0x1, (%rbp,%rax,8)
movq (%rsp), %rbx
adcl $0x0, %ebx
incq %r12
cmpq $0x200000, %r12         # imm = 0x200000
jne 0x402530
leaq 0x18(%rsp), %r14
xorl %edi, %edi
movq %r14, %rsi
callq 0x401060
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq 0x8(%rsp), %rax
cvtsi2sd %rax, %xmm2
subq 0x10(%rsp), %rcx
cvtsi2sd %rcx, %xmm0
divsd 0x9996(%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 0x997c(%rip), %xmm1     # 0x40c010
movsd %xmm1, (%rsp)
leaq 0x9a8d(%rip), %rdi      # 0x40c12d
movl %ebx, %esi
movb $0x2, %al
callq 0x401090
movsd (%rsp), %xmm0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq