// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xb8, %rsp
movq %rdi, 0x48(%rsp)
xorl %edi, %edi
callq 0x40b070
movl $0x0, 0x40(%rsp)
xorl %edi, %edi
leaq 0x30(%rsp), %rsi
callq 0x401050
movl $0x0, 0x1c(%rsp)
cmpl $0x200000, 0x1c(%rsp)   # imm = 0x200000
jge 0x4027a1
leaq 0x16a5c(%rip), %rdi     # 0x419000
xorl %esi, %esi
movl $0x80, %edx
callq 0x4010a0
movslq 0x1c(%rsp), %rcx
leaq 0x17a44(%rip), %rax     # 0x41a000
movzbl (%rax,%rcx), %eax
andl $0xf, %eax
movl %eax, 0x44(%rsp)
movl $0x0, 0x18(%rsp)
cmpl $0x80, 0x18(%rsp)
jge 0x402771
movq 0x16a9c(%rip), %rax     # 0x419080
movq %rax, 0x58(%rsp)
movq $0x0, 0x50(%rsp)
cmpq $0x10, 0x50(%rsp)
jae 0x40261e
movq 0x58(%rsp), %rax
movq 0x50(%rsp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
clflush (%rax)
movq 0x50(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x50(%rsp)
jmp 0x4025f2
mfence
movq 0x48(%rsp), %rax
movq 0x16a53(%rip), %rdi     # 0x419080
movslq 0x1c(%rsp), %rcx
leaq 0x179c7(%rip), %rsi     # 0x41a000
addq %rcx, %rsi
callq *%rax
movq 0x16a3b(%rip), %rax     # 0x419080
movq %rax, 0x90(%rsp)
leaq 0x169ac(%rip), %rax     # 0x419000
movq %rax, 0x88(%rsp)
mfence
movq $0x0, 0x80(%rsp)
cmpq $0x10, 0x80(%rsp)
jae 0x40274a
imulq $0x7, 0x80(%rsp), %rax
addq $0xb, %rax
andq $0xf, %rax
movq %rax, 0x78(%rsp)
movq 0x90(%rsp), %rax
movq 0x78(%rsp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movq %rax, 0x70(%rsp)
rdtscp
movq %rax, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
shlq $0x20, %rax
orq 0xb0(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rax
movb (%rax), %al
rdtscp
movq %rax, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq 0x98(%rsp), %rax
shlq $0x20, %rax
orq 0xa0(%rsp), %rax
subq 0x68(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq $0xa0, 0x60(%rsp)
jae 0x402731
movq 0x88(%rsp), %rax
movq 0x78(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x80(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x80(%rsp)
jmp 0x40266b
movslq 0x44(%rsp), %rcx
leaq 0x168aa(%rip), %rax     # 0x419000
cmpq $0x0, (%rax,%rcx,8)
je 0x40275f
jmp 0x402771
jmp 0x402761
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x4025cf
movslq 0x44(%rsp), %rcx
leaq 0x16883(%rip), %rax     # 0x419000
cmpq $0x0, (%rax,%rcx,8)
jne 0x40278f
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x402791
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x40258f
xorl %edi, %edi
leaq 0x20(%rsp), %rsi
callq 0x401050
movq 0x20(%rsp), %rax
subq 0x30(%rsp), %rax
cvtsi2sd %rax, %xmm0
movq 0x28(%rsp), %rax
subq 0x38(%rsp), %rax
cvtsi2sd %rax, %xmm1
movsd 0x983d(%rip), %xmm2     # 0x40c010
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movl $0x200000, %eax         # imm = 0x200000
subl 0x40(%rsp), %eax
shll $0x2, %eax
cvtsi2sd %eax, %xmm0
divsd 0x10(%rsp), %xmm0
movsd 0x9809(%rip), %xmm1     # 0x40c008
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movl 0x40(%rsp), %esi
leaq 0x990d(%rip), %rdi      # 0x40c12d
movb $0x2, %al
callq 0x401080
movsd 0x8(%rsp), %xmm0
addq $0xb8, %rsp
retq
nopw %cs:(%rax,%rax)