// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x38, %rsp
movl $0x0, 0x34(%rsp)
movl %edi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
stmxcsr 0x14(%rsp)
movl 0x14(%rsp), %eax
andl $-0x41, %eax
orl $0x0, %eax
movl %eax, 0x10(%rsp)
ldmxcsr 0x10(%rsp)
stmxcsr 0xc(%rsp)
movl 0xc(%rsp), %eax
andl $0xffff7fff, %eax       # imm = 0xFFFF7FFF
orl $0x0, %eax
movl %eax, 0x8(%rsp)
ldmxcsr 0x8(%rsp)
xorl %eax, %eax
movl %eax, %r9d
movl $0x8000, %esi           # imm = 0x8000
movl $0x3, %edx
movl $0x48022, %ecx          # imm = 0x48022
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq %r9, %rdi
callq 0x401070
movq %rax, 0x16e60(%rip)     # 0x419080
movq $-0x1, %rax
cmpq %rax, 0x16e52(%rip)     # 0x419080
je 0x402232
jmp 0x402251
leaq 0x9df2(%rip), %rdi      # 0x40c02b
leaq 0x9e04(%rip), %rsi      # 0x40c044
movl $0x4f, %edx
leaq 0x9e04(%rip), %rcx      # 0x40c050
callq 0x401090
leaq 0x16da8(%rip), %rdi     # 0x419000
xorl %esi, %esi
movl $0x80, %edx
callq 0x4010a0
movq $0x408000, %rdi         # imm = 0x408000
movl $0x1000, %esi           # imm = 0x1000
movl $0x7, %edx
callq 0x401150
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jne 0x402287
jmp 0x4022a6
leaq 0x9dd9(%rip), %rdi      # 0x40c067
leaq 0x9daf(%rip), %rsi      # 0x40c044
movl $0x54, %edx
leaq 0x9daf(%rip), %rcx      # 0x40c050
callq 0x401090
movq $0x40b000, %rdi         # imm = 0x40B000
movl $0x1000, %esi           # imm = 0x1000
movl $0x7, %edx
callq 0x401150
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jne 0x4022c9
jmp 0x4022e8
leaq 0x9d97(%rip), %rdi      # 0x40c067
leaq 0x9d6d(%rip), %rsi      # 0x40c044
movl $0x56, %edx
leaq 0x9d6d(%rip), %rcx      # 0x40c050
callq 0x401090
movl $0xb, %edi
leaq 0x24c(%rip), %rsi       # 0x402540
callq 0x4010f0
movq $-0x1, %rcx
cmpq %rcx, %rax
je 0x402307
jmp 0x402326
leaq 0x9d62(%rip), %rdi      # 0x40c070
leaq 0x9d2f(%rip), %rsi      # 0x40c044
movl $0x58, %edx
leaq 0x9d2f(%rip), %rcx      # 0x40c050
callq 0x401090
leaq 0x9d70(%rip), %rdi      # 0x40c09d
xorl %esi, %esi
movb $0x0, %al
callq 0x401160
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %edi
leaq 0x17cbb(%rip), %rsi     # 0x41a000
movl $0x200000, %edx         # imm = 0x200000
callq 0x4010c0
movl %eax, 0x24(%rsp)
cmpl $0x200000, 0x24(%rsp)   # imm = 0x200000
jne 0x40235f
jmp 0x40237e
leaq 0x9d44(%rip), %rdi      # 0x40c0aa
leaq 0x9cd7(%rip), %rsi      # 0x40c044
movl $0x5d, %edx
leaq 0x9cd7(%rip), %rcx      # 0x40c050
callq 0x401090
leaq 0x9d38(%rip), %rdi      # 0x40c0bd
leaq 0x9d47(%rip), %rsi      # 0x40c0d3
leaq 0x9d51(%rip), %rdx      # 0x40c0e4
leaq 0x9d53(%rip), %rcx      # 0x40c0ed
leaq 0x9d5d(%rip), %r8       # 0x40c0fe
movb $0x0, %al
callq 0x401080
leaq 0x9d56(%rip), %rdi      # 0x40c105
leaq 0x9d55(%rip), %rsi      # 0x40c10b
movb $0x0, %al
callq 0x401080
movq $0x404d00, %rdi         # imm = 0x404D00
callq 0x402560
movb $0x0, %al
callq 0x40b2e0
cmpl $0x0, %eax
je 0x4023f6
leaq 0x9d29(%rip), %rdi      # 0x40c105
leaq 0x9d2b(%rip), %rsi      # 0x40c10e
movb $0x0, %al
callq 0x401080
movq $0x4087ac, %rdi         # imm = 0x4087AC
callq 0x402560
leaq 0x9d08(%rip), %rdi      # 0x40c105
leaq 0x9d0e(%rip), %rsi      # 0x40c112
movb $0x0, %al
callq 0x401080
movq $0x408000, %rdi         # imm = 0x408000
callq 0x402560
leaq 0x9ce7(%rip), %rdi      # 0x40c105
leaq 0x9cf1(%rip), %rsi      # 0x40c116
movb $0x0, %al
callq 0x401080
movq $0x404c80, %rdi         # imm = 0x404C80
callq 0x402560
leaq 0x9cc6(%rip), %rdi      # 0x40c105
leaq 0x9cd3(%rip), %rsi      # 0x40c119
movb $0x0, %al
callq 0x401080
movq $0x405c58, %rdi         # imm = 0x405C58
callq 0x402560
leaq 0x9ca5(%rip), %rdi      # 0x40c105
leaq 0x9cb5(%rip), %rsi      # 0x40c11c
movb $0x0, %al
callq 0x401080
movq $0x403305, %rdi         # imm = 0x403305
callq 0x402560
leaq 0x9c84(%rip), %rdi      # 0x40c105
leaq 0x9c98(%rip), %rsi      # 0x40c120
movb $0x0, %al
callq 0x401080
movl $0x1, %edi
leaq 0x3a5(%rip), %rsi       # 0x402840
xorl %eax, %eax
movl %eax, %edx
callq 0x40b190
movq %rax, 0x18(%rsp)
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x4011c0
movq $0x40b000, %rdi         # imm = 0x40B000
callq 0x402560
movq 0x18(%rsp), %rdi
callq 0x40b2a0
leaq 0x9c35(%rip), %rdi      # 0x40c105
leaq 0x9c4d(%rip), %rsi      # 0x40c124
movb $0x0, %al
callq 0x401080
movl $0x1, %edi
leaq 0x366(%rip), %rsi       # 0x402850
xorl %eax, %eax
movl %eax, %edx
callq 0x40b190
movq %rax, 0x18(%rsp)
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x4011c0
movq $0x4066d4, %rdi         # imm = 0x4066D4
callq 0x402560
movq 0x18(%rsp), %rdi
callq 0x40b2a0
leaq 0x9be6(%rip), %rdi      # 0x40c105
leaq 0x9c01(%rip), %rsi      # 0x40c127
movb $0x0, %al
callq 0x401080
movq $0x403a75, %rdi         # imm = 0x403A75
callq 0x402860
xorl %eax, %eax
addq $0x38, %rsp
retq