// File: snaury[P]t-digest-simple/main.cpp
// Function: main
int main() {
    std::random_device seed;
    std::default_random_engine rng(seed());
    std::uniform_real_distribution<double> dist(0.0, 1.0);

    std::vector<double> data;
    std::cout << "Generating random data..." << std::endl;
    for (size_t i = 0; i < num_values; ++i) {
        data.push_back(dist(rng));
    }

    std::cout << "Computing the digest..." << std::endl;
    auto t0 = std::chrono::high_resolution_clock::now();
    MergingTDigest digest(100);
    for (auto value : data) {
        digest.add(value);
    }
    digest.compress();
    auto t1 = std::chrono::high_resolution_clock::now();
    auto dt = std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0);
    auto nsperop = dt / (double)num_values;
    std::cout << nsperop.count() << "ns per value" << std::endl;
    std::cout << digest.size() << " centroids" << std::endl;
    std::cout << "p50 = " << digest.quantile(0.5) << std::endl;
    std::cout << "p95 = " << digest.quantile(0.95) << std::endl;
    std::cout << "p99 = " << digest.quantile(0.99) << std::endl;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1520, %rsp           # imm = 0x1520
movl $0x0, -0x4(%rbp)
leaq -0x1390(%rbp), %rdi
movq %rdi, -0x1478(%rbp)
callq 0x2940
movq -0x1478(%rbp), %rdi
callq 0x29e0
movl %eax, -0x146c(%rbp)
jmp 0x2419
movl -0x146c(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x1398(%rbp), %rdi
callq 0x2a00
jmp 0x2431
leaq -0x13b8(%rbp), %rdi
xorps %xmm0, %xmm0
movsd 0x6bc5(%rip), %xmm1     # 0x9008
callq 0x2a30
jmp 0x244a
leaq -0x13d0(%rbp), %rdi
callq 0x2a60
movq 0xab7b(%rip), %rdi      # 0xcfd8
leaq 0x6c04(%rip), %rsi      # 0x9068
callq 0x2150
movq %rax, -0x1480(%rbp)
jmp 0x2472
movq -0x1480(%rbp), %rdi
movq 0xab48(%rip), %rsi      # 0xcfc8
callq 0x2180
jmp 0x2487
movq $0x0, -0x13d8(%rbp)
cmpq $0x989680, -0x13d8(%rbp) # imm = 0x989680
jae 0x2529
leaq -0x13b8(%rbp), %rdi
leaq -0x1398(%rbp), %rsi
callq 0x2ab0
movsd %xmm0, -0x1488(%rbp)
jmp 0x24c0
movsd -0x1488(%rbp), %xmm0
movsd %xmm0, -0x13e0(%rbp)
leaq -0x13d0(%rbp), %rdi
leaq -0x13e0(%rbp), %rsi
callq 0x2a80
jmp 0x24e5
jmp 0x24e7
movq -0x13d8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x13d8(%rbp)
jmp 0x2492
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
jmp 0x2926
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
jmp 0x291a
movq 0xaaa8(%rip), %rdi      # 0xcfd8
leaq 0x6b4b(%rip), %rsi      # 0x9082
callq 0x2150
movq %rax, -0x1490(%rbp)
jmp 0x2545
movq -0x1490(%rbp), %rdi
movq 0xaa75(%rip), %rsi      # 0xcfc8
callq 0x2180
jmp 0x255a
callq 0x2030
movq %rax, -0x13e8(%rbp)
leaq -0x1420(%rbp), %rdi
movsd 0x6a9b(%rip), %xmm0     # 0x9010
movl $0x800, %esi            # imm = 0x800
callq 0x2ae0
jmp 0x2581
leaq -0x13d0(%rbp), %rax
movq %rax, -0x1428(%rbp)
movq -0x1428(%rbp), %rdi
callq 0x2b40
movq %rax, -0x1430(%rbp)
movq -0x1428(%rbp), %rdi
callq 0x2b70
movq %rax, -0x1438(%rbp)
leaq -0x1430(%rbp), %rdi
leaq -0x1438(%rbp), %rsi
callq 0x2ba0
testb $0x1, %al
jne 0x25ce
jmp 0x2634
leaq -0x1430(%rbp), %rdi
callq 0x2be0
movsd (%rax), %xmm0
movsd %xmm0, -0x1440(%rbp)
movsd -0x1440(%rbp), %xmm0
leaq -0x1420(%rbp), %rdi
movl $0x1, %esi
callq 0x2c00
jmp 0x2601
jmp 0x2603
leaq -0x1430(%rbp), %rdi
callq 0x2c50
jmp 0x25b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
leaq -0x1420(%rbp), %rdi
callq 0x33c0
jmp 0x291a
leaq -0x1420(%rbp), %rdi
callq 0x2c70
jmp 0x2642
callq 0x2030
movq %rax, -0x1448(%rbp)
leaq -0x1448(%rbp), %rdi
leaq -0x13e8(%rbp), %rsi
callq 0x3050
movq %rax, -0x1498(%rbp)
jmp 0x266a
movq -0x1498(%rbp), %rax
movq %rax, -0x1458(%rbp)
leaq -0x1458(%rbp), %rdi
callq 0x3020
movq %rax, -0x14a0(%rbp)
jmp 0x268d
movq -0x14a0(%rbp), %rax
movq %rax, -0x1450(%rbp)
movabsq $0x416312d000000000, %rax # imm = 0x416312D000000000
movq %rax, -0x1468(%rbp)
leaq -0x1450(%rbp), %rdi
leaq -0x1468(%rbp), %rsi
callq 0x30a0
movsd %xmm0, -0x14a8(%rbp)
jmp 0x26c9
movsd -0x14a8(%rbp), %xmm0
movsd %xmm0, -0x1460(%rbp)
leaq -0x1460(%rbp), %rdi
callq 0x30f0
movsd %xmm0, -0x14b0(%rbp)
jmp 0x26ef
movsd -0x14b0(%rbp), %xmm0
movq 0xa8da(%rip), %rdi      # 0xcfd8
callq 0x2270
movq %rax, -0x14b8(%rbp)
jmp 0x270c
movq -0x14b8(%rbp), %rdi
leaq 0x6980(%rip), %rsi      # 0x909a
callq 0x2150
movq %rax, -0x14c0(%rbp)
jmp 0x2728
movq -0x14c0(%rbp), %rdi
movq 0xa892(%rip), %rsi      # 0xcfc8
callq 0x2180
jmp 0x273d
leaq -0x1420(%rbp), %rdi
callq 0x3110
movq %rax, -0x14c8(%rbp)
jmp 0x2752
movq -0x14c8(%rbp), %rsi
movq 0xa878(%rip), %rdi      # 0xcfd8
callq 0x2050
movq %rax, -0x14d0(%rbp)
jmp 0x276e
movq -0x14d0(%rbp), %rdi
leaq 0x692b(%rip), %rsi      # 0x90a7
callq 0x2150
movq %rax, -0x14d8(%rbp)
jmp 0x278a
movq -0x14d8(%rbp), %rdi
movq 0xa830(%rip), %rsi      # 0xcfc8
callq 0x2180
jmp 0x279f
movq 0xa832(%rip), %rdi      # 0xcfd8
leaq 0x6905(%rip), %rsi      # 0x90b2
callq 0x2150
movq %rax, -0x14e0(%rbp)
jmp 0x27bb
leaq -0x1420(%rbp), %rdi
movsd 0x684e(%rip), %xmm0     # 0x9018
callq 0x3140
movsd %xmm0, -0x14e8(%rbp)
jmp 0x27d9
movsd -0x14e8(%rbp), %xmm0
movq -0x14e0(%rbp), %rdi
callq 0x2270
movq %rax, -0x14f0(%rbp)
jmp 0x27f6
movq -0x14f0(%rbp), %rdi
movq 0xa7c4(%rip), %rsi      # 0xcfc8
callq 0x2180
jmp 0x280b
movq 0xa7c6(%rip), %rdi      # 0xcfd8
leaq 0x68a0(%rip), %rsi      # 0x90b9
callq 0x2150
movq %rax, -0x14f8(%rbp)
jmp 0x2827
leaq -0x1420(%rbp), %rdi
movsd 0x67ea(%rip), %xmm0     # 0x9020
callq 0x3140
movsd %xmm0, -0x1500(%rbp)
jmp 0x2845
movsd -0x1500(%rbp), %xmm0
movq -0x14f8(%rbp), %rdi
callq 0x2270
movq %rax, -0x1508(%rbp)
jmp 0x2862
movq -0x1508(%rbp), %rdi
movq 0xa758(%rip), %rsi      # 0xcfc8
callq 0x2180
jmp 0x2877
movq 0xa75a(%rip), %rdi      # 0xcfd8
leaq 0x683b(%rip), %rsi      # 0x90c0
callq 0x2150
movq %rax, -0x1510(%rbp)
jmp 0x2893
leaq -0x1420(%rbp), %rdi
movsd 0x6786(%rip), %xmm0     # 0x9028
callq 0x3140
movsd %xmm0, -0x1518(%rbp)
jmp 0x28b1
movsd -0x1518(%rbp), %xmm0
movq -0x1510(%rbp), %rdi
callq 0x2270
movq %rax, -0x1520(%rbp)
jmp 0x28ce
movq -0x1520(%rbp), %rdi
movq 0xa6ec(%rip), %rsi      # 0xcfc8
callq 0x2180
jmp 0x28e3
movl $0x0, -0x4(%rbp)
leaq -0x1420(%rbp), %rdi
callq 0x33c0
leaq -0x13d0(%rbp), %rdi
callq 0x33e0
leaq -0x1390(%rbp), %rdi
callq 0x3440
movl -0x4(%rbp), %eax
addq $0x1520, %rsp           # imm = 0x1520
popq %rbp
retq
leaq -0x13d0(%rbp), %rdi
callq 0x33e0
leaq -0x1390(%rbp), %rdi
callq 0x3440
movq -0x13a0(%rbp), %rdi
callq 0x2240
nop