// File: /workspace/github_c_makefile_star2_O3/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 %r14
pushq %rbx
subq $0x18, %rsp
stmxcsr 0x14(%rsp)
movl 0x14(%rsp), %eax
andl $-0x41, %eax
movl %eax, 0x10(%rsp)
ldmxcsr 0x10(%rsp)
stmxcsr 0xc(%rsp)
movl $0xffff7fff, %eax       # imm = 0xFFFF7FFF
andl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
ldmxcsr 0x8(%rsp)
movl $0x8000, %esi           # imm = 0x8000
xorl %edi, %edi
movl $0x3, %edx
movl $0x48022, %ecx          # imm = 0x48022
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
callq 0x401080
movq %rax, 0x16e9f(%rip)     # 0x419080
cmpq $-0x1, %rax
je 0x40244e
xorps %xmm0, %xmm0
movaps %xmm0, 0x16e7b(%rip)    # 0x419070
movaps %xmm0, 0x16e64(%rip)    # 0x419060
movaps %xmm0, 0x16e4d(%rip)    # 0x419050
movaps %xmm0, 0x16e36(%rip)    # 0x419040
movaps %xmm0, 0x16e1f(%rip)    # 0x419030
movaps %xmm0, 0x16e08(%rip)    # 0x419020
movaps %xmm0, 0x16df1(%rip)    # 0x419010
movaps %xmm0, 0x16dda(%rip)    # 0x419000
movq $0x408000, %rdi         # imm = 0x408000
movl $0x1000, %esi           # imm = 0x1000
movl $0x7, %edx
callq 0x401140
testl %eax, %eax
jne 0x40246d
movq $0x40b000, %rdi         # imm = 0x40B000
movl $0x1000, %esi           # imm = 0x1000
movl $0x7, %edx
callq 0x401140
testl %eax, %eax
jne 0x40248c
leaq 0x280(%rip), %rsi       # 0x4024e9
movl $0xb, %edi
callq 0x4010f0
cmpq $-0x1, %rax
je 0x4024ab
leaq 0x9e19(%rip), %rdi      # 0x40c09d
xorl %esi, %esi
xorl %eax, %eax
callq 0x401150
leaq 0x17d6c(%rip), %rsi     # 0x41a000
movl $0x200000, %edx         # imm = 0x200000
movl %eax, %edi
callq 0x4010c0
cmpl $0x200000, %eax         # imm = 0x200000
jne 0x4024ca
leaq 0x9e0b(%rip), %rdi      # 0x40c0bd
leaq 0x9e1a(%rip), %rsi      # 0x40c0d3
leaq 0x9e24(%rip), %rdx      # 0x40c0e4
leaq 0x9e26(%rip), %rcx      # 0x40c0ed
leaq 0x9e30(%rip), %r8       # 0x40c0fe
xorl %eax, %eax
callq 0x401090
leaq 0x9e29(%rip), %rdi      # 0x40c105
leaq 0x9e28(%rip), %rsi      # 0x40c10b
xorl %eax, %eax
callq 0x401090
movq $0x404a40, %rdi         # imm = 0x404A40
callq 0x402500
xorl %eax, %eax
callq 0x40b209
testl %eax, %eax
je 0x402322
leaq 0x9dfd(%rip), %rdi      # 0x40c105
leaq 0x9dff(%rip), %rsi      # 0x40c10e
xorl %eax, %eax
callq 0x401090
movq $0x4087ac, %rdi         # imm = 0x4087AC
callq 0x402500
leaq 0x9ddc(%rip), %rbx      # 0x40c105
leaq 0x9de2(%rip), %rsi      # 0x40c112
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
movq $0x408000, %rdi         # imm = 0x408000
callq 0x402500
leaq 0x9dc9(%rip), %rsi      # 0x40c116
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
movq $0x4049c0, %rdi         # imm = 0x4049C0
callq 0x402500
leaq 0x9daf(%rip), %rsi      # 0x40c119
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
movq $0x405998, %rdi         # imm = 0x405998
callq 0x402500
leaq 0x9d95(%rip), %rsi      # 0x40c11c
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
movq $0x403075, %rdi         # imm = 0x403075
callq 0x402500
leaq 0x9d7c(%rip), %rsi      # 0x40c120
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
leaq 0x304(%rip), %rsi       # 0x4026b9
movl $0x1, %edi
xorl %edx, %edx
callq 0x40b10b
movq %rax, %r14
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x4011c0
movq $0x40b000, %rdi         # imm = 0x40B000
callq 0x402500
movq %r14, %rdi
callq 0x40b1e4
leaq 0x9d3b(%rip), %rsi      # 0x40c124
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
leaq 0x2c7(%rip), %rsi       # 0x4026c1
movl $0x1, %edi
xorl %edx, %edx
callq 0x40b10b
movq %rax, %r14
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x4011c0
movq $0x406414, %rdi         # imm = 0x406414
callq 0x402500
movq %r14, %rdi
callq 0x40b1e4
leaq 0x9cf9(%rip), %rsi      # 0x40c127
movq %rbx, %rdi
xorl %eax, %eax
callq 0x401090
movq $0x4037e5, %rdi         # imm = 0x4037E5
callq 0x4026c9
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
retq
leaq 0x9bd6(%rip), %rdi      # 0x40c02b
leaq 0x9be8(%rip), %rsi      # 0x40c044
leaq 0x9bed(%rip), %rcx      # 0x40c050
movl $0x4f, %edx
callq 0x4010a0
leaq 0x9bf3(%rip), %rdi      # 0x40c067
leaq 0x9bc9(%rip), %rsi      # 0x40c044
leaq 0x9bce(%rip), %rcx      # 0x40c050
movl $0x54, %edx
callq 0x4010a0
leaq 0x9bd4(%rip), %rdi      # 0x40c067
leaq 0x9baa(%rip), %rsi      # 0x40c044
leaq 0x9baf(%rip), %rcx      # 0x40c050
movl $0x56, %edx
callq 0x4010a0
leaq 0x9bbe(%rip), %rdi      # 0x40c070
leaq 0x9b8b(%rip), %rsi      # 0x40c044
leaq 0x9b90(%rip), %rcx      # 0x40c050
movl $0x58, %edx
callq 0x4010a0
leaq 0x9bd9(%rip), %rdi      # 0x40c0aa
leaq 0x9b6c(%rip), %rsi      # 0x40c044
leaq 0x9b71(%rip), %rcx      # 0x40c050
movl $0x5d, %edx
callq 0x4010a0