// File: /workspace/github_c_makefile_star2_O2/vusec[P]fpvi-scsb/leak_rate_win_size/leak_rate.c
// Function: main
int main(int argc, char **argv)
{
    int ret, fp;
    pthread_t tid;

    //Ensure defualt settings for FP
    _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_OFF);
    _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_OFF);

    //F+R Setup
    reload_buf   = (unsigned char *) mmap(NULL, 2*LEAK_SIZE*STRIDE, PROT_READ | PROT_WRITE,
            MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE | MAP_HUGETLB, -1, 0);
    assert(reload_buf != MAP_FAILED);
    memset(results, 0, sizeof(results));

    //Make the SMC code pages RWX
    ret = mprotect(&smc_leak, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC);
    assert(ret == 0);
    ret = mprotect(&xmc_leak, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC);
    assert(ret == 0);
    
    assert(signal(SIGSEGV, segfault_handler) != SIG_ERR);

    //Generate random secret to leak
    fp = open("/dev/urandom", O_RDONLY);
    ret = read(fp, secret, SECRET_SIZE);
    assert(ret == SECRET_SIZE);

    printf("%20s;%20s;%20s;%20s;\n", "Exception Suppr.", "Time [s]", "Leak Rate [Mb/s]", "no_hit");

    //Run Experiments
    printf("%20s;", "fr");
    leak_run(flush_reload_leak);

    if(check_tsx())
    {
        printf("%20s;", "tsx");
        leak_run(tsx_leak);
    }

    printf("%20s;", "smc");
    leak_run(smc_leak);

    printf("%20s;", "fp");
    leak_run(fp_leak);

    printf("%20s;", "md");
    leak_run(md_leak);

    printf("%20s;", "bht");
    leak_run(bht_leak);

    printf("%20s;", "xmc");
    tid = create_thread(TRIGGER_CORE, xmc_trigger_loop, NULL);
    usleep(2000);
    leak_run(xmc_leak);
    cancel_thread(tid);

    printf("%20s;", "mo");
    tid = create_thread(TRIGGER_CORE, mo_trigger_loop, NULL);
    usleep(2000);
    leak_run(mo_leak);
    cancel_thread(tid);
    
    printf("%20s;", "fault");
    leak_run_fault(fault_leak);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x10, %rsp
stmxcsr 0xc(%rsp)
movl 0xc(%rsp), %eax
andl $-0x41, %eax
movl %eax, 0x8(%rsp)
ldmxcsr 0x8(%rsp)
stmxcsr 0x4(%rsp)
movl $0xffff7fff, %eax       # imm = 0xFFFF7FFF
andl 0x4(%rsp), %eax
movl %eax, (%rsp)
ldmxcsr (%rsp)
pushq $0x3
popq %rdx
pushq $-0x1
popq %r8
movl $0x8000, %esi           # imm = 0x8000
xorl %edi, %edi
movl $0x48022, %ecx          # imm = 0x48022
xorl %r9d, %r9d
callq 0x401080
movq %rax, 0x16edb(%rip)     # 0x419080
cmpq $-0x1, %rax
je 0x40240a
xorps %xmm0, %xmm0
movaps %xmm0, 0x16eb7(%rip)    # 0x419070
movaps %xmm0, 0x16ea0(%rip)    # 0x419060
movaps %xmm0, 0x16e89(%rip)    # 0x419050
movaps %xmm0, 0x16e72(%rip)    # 0x419040
movaps %xmm0, 0x16e5b(%rip)    # 0x419030
movaps %xmm0, 0x16e44(%rip)    # 0x419020
movaps %xmm0, 0x16e2d(%rip)    # 0x419010
movaps %xmm0, 0x16e16(%rip)    # 0x419000
movq $0x408000, %rdi         # imm = 0x408000
pushq $0x7
popq %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x401140
testl %eax, %eax
jne 0x402423
movq $0x40b000, %rdi         # imm = 0x40B000
pushq $0x7
popq %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x401140
testl %eax, %eax
jne 0x40243c
leaq 0x262(%rip), %rsi       # 0x40248b
pushq $0xb
popq %rdi
callq 0x4010f0
cmpq $-0x1, %rax
je 0x402455
leaq 0x9e5b(%rip), %rdi      # 0x40c09d
xorl %esi, %esi
xorl %eax, %eax
callq 0x401150
leaq 0x17dae(%rip), %rsi     # 0x41a000
movl $0x200000, %edx         # imm = 0x200000
movl %eax, %edi
callq 0x4010c0
cmpl $0x200000, %eax         # imm = 0x200000
jne 0x40246e
leaq 0x9e4d(%rip), %rdi      # 0x40c0bd
leaq 0x9e5c(%rip), %rsi      # 0x40c0d3
leaq 0x9e66(%rip), %rdx      # 0x40c0e4
leaq 0x9e68(%rip), %rcx      # 0x40c0ed
leaq 0x9e72(%rip), %r8       # 0x40c0fe
xorl %eax, %eax
callq 0x401090
leaq 0x9e6b(%rip), %rdi      # 0x40c105
leaq 0x9e6a(%rip), %rsi      # 0x40c10b
xorl %eax, %eax
callq 0x401090
movq $0x404a00, %rdi         # imm = 0x404A00
callq 0x4024a0
xorl %eax, %eax
callq 0x40b22d
testl %eax, %eax
je 0x4022e0
leaq 0x9e3f(%rip), %rdi      # 0x40c105
leaq 0x9e41(%rip), %rsi      # 0x40c10e
xorl %eax, %eax
callq 0x401090
movq $0x4087ac, %rdi         # imm = 0x4087AC
callq 0x4024a0
leaq 0x9e1e(%rip), %rbx      # 0x40c105
leaq 0x9e24(%rip), %rsi      # 0x40c112
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
movq $0x408000, %rdi         # imm = 0x408000
callq 0x4024a0
leaq 0x9e0b(%rip), %rsi      # 0x40c116
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
movq $0x404980, %rdi         # imm = 0x404980
callq 0x4024a0
leaq 0x9df1(%rip), %rsi      # 0x40c119
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
movq $0x405958, %rdi         # imm = 0x405958
callq 0x4024a0
leaq 0x9dd7(%rip), %rsi      # 0x40c11c
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
movq $0x403035, %rdi         # imm = 0x403035
callq 0x4024a0
leaq 0x9dbe(%rip), %rsi      # 0x40c120
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
leaq 0x30c(%rip), %rsi       # 0x40267f
pushq $0x1
popq %rbp
movl %ebp, %edi
xorl %edx, %edx
callq 0x40b135
movq %rax, %r14
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x4011c0
movq $0x40b000, %rdi         # imm = 0x40B000
callq 0x4024a0
movq %r14, %rdi
callq 0x40b20a
leaq 0x9d7d(%rip), %rsi      # 0x40c124
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
leaq 0x2cf(%rip), %rsi       # 0x402687
movl %ebp, %edi
xorl %edx, %edx
callq 0x40b135
movq %rax, %r14
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x4011c0
movq $0x4063d4, %rdi         # imm = 0x4063D4
callq 0x4024a0
movq %r14, %rdi
callq 0x40b20a
leaq 0x9d3e(%rip), %rsi      # 0x40c127
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
movq $0x4037a5, %rdi         # imm = 0x4037A5
callq 0x40268f
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq
leaq 0x9c1a(%rip), %rdi      # 0x40c02b
leaq 0x9c2c(%rip), %rsi      # 0x40c044
leaq 0x9c31(%rip), %rcx      # 0x40c050
pushq $0x4f
jmp 0x402485
leaq 0x9c3d(%rip), %rdi      # 0x40c067
leaq 0x9c13(%rip), %rsi      # 0x40c044
leaq 0x9c18(%rip), %rcx      # 0x40c050
pushq $0x54
jmp 0x402485
leaq 0x9c24(%rip), %rdi      # 0x40c067
leaq 0x9bfa(%rip), %rsi      # 0x40c044
leaq 0x9bff(%rip), %rcx      # 0x40c050
pushq $0x56
jmp 0x402485
leaq 0x9c14(%rip), %rdi      # 0x40c070
leaq 0x9be1(%rip), %rsi      # 0x40c044
leaq 0x9be6(%rip), %rcx      # 0x40c050
pushq $0x58
jmp 0x402485
leaq 0x9c35(%rip), %rdi      # 0x40c0aa
leaq 0x9bc8(%rip), %rsi      # 0x40c044
leaq 0x9bcd(%rip), %rcx      # 0x40c050
pushq $0x5d
popq %rdx
callq 0x4010a0