// File: /workspace/github_c_makefile_star2_O0/openshmem-org[P]openshmem-examples/c++/buffon.cxx
// Function: main
int
main()
{
    const double a = 1.0;
    const double b = 1.0;
    const double l = 1.0;
    const int master = 0;
    double pdf_estimate;
    double pi_error;
    double pi_estimate;
    int process_num;
    int process_rank;
    double random_value;
    int seed;
    int trial_num = 100000;
    int trial_total;

    //
    // Initialize SHMEM.
    //
    shmem_init();
    //
    // Get the number of processes.
    //
    process_num = shmem_n_pes();
    //
    // Get the rank of this process.
    //
    process_rank = shmem_my_pe();
    //
    // The master process prints a message.
    //
    if (process_rank == master) {
        std::cout << "\n";
        std::cout << "BUFFON_LAPLACE - Master process:\n";
        std::cout << "  C++ version\n";
        std::cout << "\n";
        std::cout << "  A SHMEM example program to estimate PI\n";
        std::cout << "  using the Buffon-Laplace needle experiment.\n";
        std::cout << "  On a grid of cells of  width A and height B,\n";
        std::cout << "  a needle of length L is dropped at random.\n";
        std::cout << "  We count the number of times it crosses\n";
        std::cout << "  at least one grid line, and use this to estimate \n";
        std::cout << "  the value of PI.\n";
        std::cout << "\n";
        std::cout << "  The number of processes is " << process_num << "\n";
        std::cout << "\n";
        std::cout << "  Cell width A =    " << a << "\n";
        std::cout << "  Cell height B =   " << b << "\n";
        std::cout << "  Needle length L = " << l << "\n";
    }
    //
    // added barrier here to force output sequence
    //
    shmem_barrier_all();
    //
    // Each process sets a random number seed.
    //
    seed = 123456789 + process_rank * 100;
    srand(seed);
    //
    // Just to make sure that we're all doing different things, have each
    // process print out its rank, seed value, and a first test random value.
    //
    random_value = (double) rand() / (double) RAND_MAX;

    std::cout << "  " << std::setw(8) << process_rank
     << "  " << std::setw(12) << seed
     << "  " << std::setw(14) << random_value << "\n";
    //
    // Each process now carries out TRIAL_NUM trials, and then
    // sends the value back to the master process.
    //
    hit_num = buffon_laplace_simulate(a, b, l, trial_num);

    //
    // initialize sync buffer for reduction
    //
    for (int i = 0; i < SHMEM_BCAST_SYNC_SIZE; ++i) {
        pSync[i] = SHMEM_SYNC_VALUE;
    }
    shmem_barrier_all();

    shmem_int_sum_to_all(&hit_total, &hit_num, 1, 0, 0, process_num, pWrk,
                         pSync);

    //
    // The master process can now estimate PI.
    //
    if (process_rank == master) {
        trial_total = trial_num * process_num;

        pdf_estimate = (double) hit_total / (double) trial_total;

        if (hit_total == 0) {
            pi_estimate = r8_huge();
        }
        else {
            pi_estimate = l * (2.0 * (a + b) - l) / (a * b * pdf_estimate);
        }

        pi_error = r8_abs(PI - pi_estimate);

        std::cout << "\n";
        std::cout <<
            "    Trials      Hits    Estimated PDF       Estimated Pi        Error\n";
        std::cout << "\n";
        std::cout << "  " << std::setw(8) << trial_total
         << "  " << std::setw(8) << hit_total
         << "  " << std::setw(16) << pdf_estimate
         << "  " << std::setw(16) << pi_estimate
         << "  " << std::setw(16) << pi_error << "\n";
    }
    //
    // Shut down
    //
    if (process_rank == master) {
        std::cout << "\n";
        std::cout << "BUFFON_LAPLACE - Master process:\n";
        std::cout << "  Normal end of execution.\n";
    }

    shmem_finalize();

    return 0;
}

// Assembly:
subq $0xd8, %rsp
movl $0x0, 0xd4(%rsp)
movsd 0xb86(%rip), %xmm0      # 0x2020
movsd %xmm0, 0xc8(%rsp)
movsd 0xb75(%rip), %xmm0      # 0x2020
movsd %xmm0, 0xc0(%rsp)
movsd 0xb64(%rip), %xmm0      # 0x2020
movsd %xmm0, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
movl $0x186a0, 0x80(%rsp) # imm = 0x186A0
callq 0x1120
callq 0x1080
movl %eax, 0x94(%rsp)
callq 0x10b0
movl %eax, 0x90(%rsp)
cmpl $0x0, 0x90(%rsp)
jne 0x16c4
movq 0x2acb(%rip), %rdi      # 0x3fd8
leaq 0xc6a(%rip), %rsi       # 0x217e
callq 0x1090
movq 0x2ab8(%rip), %rdi      # 0x3fd8
leaq 0xb11(%rip), %rsi       # 0x2038
callq 0x1090
movq 0x2aa5(%rip), %rdi      # 0x3fd8
leaq 0xb20(%rip), %rsi       # 0x205a
callq 0x1090
movq 0x2a92(%rip), %rdi      # 0x3fd8
leaq 0xc31(%rip), %rsi       # 0x217e
callq 0x1090
movq 0x2a7f(%rip), %rdi      # 0x3fd8
leaq 0xb09(%rip), %rsi       # 0x2069
callq 0x1090
movq 0x2a6c(%rip), %rdi      # 0x3fd8
leaq 0xb20(%rip), %rsi       # 0x2093
callq 0x1090
movq 0x2a59(%rip), %rdi      # 0x3fd8
leaq 0xb3c(%rip), %rsi       # 0x20c2
callq 0x1090
movq 0x2a46(%rip), %rdi      # 0x3fd8
leaq 0xb59(%rip), %rsi       # 0x20f2
callq 0x1090
movq 0x2a33(%rip), %rdi      # 0x3fd8
leaq 0xb74(%rip), %rsi       # 0x2120
callq 0x1090
movq 0x2a20(%rip), %rdi      # 0x3fd8
leaq 0xb8c(%rip), %rsi       # 0x214b
callq 0x1090
movq 0x2a0d(%rip), %rdi      # 0x3fd8
leaq 0xbae(%rip), %rsi       # 0x2180
callq 0x1090
movq 0x29fa(%rip), %rdi      # 0x3fd8
leaq 0xb99(%rip), %rsi       # 0x217e
callq 0x1090
movq 0x29e7(%rip), %rdi      # 0x3fd8
leaq 0xb9c(%rip), %rsi       # 0x2194
callq 0x1090
movq %rax, %rdi
movl 0x94(%rsp), %esi
callq 0x10f0
movq %rax, %rdi
leaq 0xb68(%rip), %rsi       # 0x217e
callq 0x1090
movq 0x29b6(%rip), %rdi      # 0x3fd8
leaq 0xb55(%rip), %rsi       # 0x217e
callq 0x1090
movq 0x29a3(%rip), %rdi      # 0x3fd8
leaq 0xb76(%rip), %rsi       # 0x21b2
callq 0x1090
movq %rax, %rdi
movsd 0x9d4(%rip), %xmm0      # 0x2020
callq 0x1110
movq %rax, %rdi
leaq 0xb23(%rip), %rsi       # 0x217e
callq 0x1090
movq 0x2971(%rip), %rdi      # 0x3fd8
leaq 0xb59(%rip), %rsi       # 0x21c7
callq 0x1090
movq %rax, %rdi
movsd 0x9a2(%rip), %xmm0      # 0x2020
callq 0x1110
movq %rax, %rdi
leaq 0xaf1(%rip), %rsi       # 0x217e
callq 0x1090
movq 0x293f(%rip), %rdi      # 0x3fd8
leaq 0xb3c(%rip), %rsi       # 0x21dc
callq 0x1090
movq %rax, %rdi
movsd 0x970(%rip), %xmm0      # 0x2020
callq 0x1110
movq %rax, %rdi
leaq 0xabf(%rip), %rsi       # 0x217e
callq 0x1090
callq 0x10c0
imull $0x64, 0x90(%rsp), %eax
addl $0x75bcd15, %eax        # imm = 0x75BCD15
movl %eax, 0x84(%rsp)
movl 0x84(%rsp), %edi
callq 0x1070
callq 0x1040
cvtsi2sd %eax, %xmm0
movsd 0x90e(%rip), %xmm1      # 0x2008
divsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
movq 0x28ca(%rip), %rdi      # 0x3fd8
leaq 0xaaf(%rip), %rsi       # 0x21c4
callq 0x1090
movq %rax, 0x40(%rsp)
movl $0x8, %edi
callq 0x1ae0
movq 0x40(%rsp), %rdi
movl %eax, 0x78(%rsp)
movl 0x78(%rsp), %esi
callq 0x10a0
movq %rax, %rdi
movl 0x90(%rsp), %esi
callq 0x10f0
movq %rax, %rdi
leaq 0xa70(%rip), %rsi       # 0x21c4
callq 0x1090
movq %rax, 0x48(%rsp)
movl $0xc, %edi
callq 0x1ae0
movq 0x48(%rsp), %rdi
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %esi
callq 0x10a0
movq %rax, %rdi
movl 0x84(%rsp), %esi
callq 0x10f0
movq %rax, %rdi
leaq 0xa31(%rip), %rsi       # 0x21c4
callq 0x1090
movq %rax, 0x50(%rsp)
movl $0xe, %edi
callq 0x1ae0
movq 0x50(%rsp), %rdi
movl %eax, 0x70(%rsp)
movl 0x70(%rsp), %esi
callq 0x10a0
movq %rax, %rdi
movsd 0x88(%rsp), %xmm0
callq 0x1110
movq %rax, %rdi
leaq 0x9aa(%rip), %rsi       # 0x217e
callq 0x1090
movl 0x80(%rsp), %edi
movsd 0x838(%rip), %xmm2      # 0x2020
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x1270
movl %eax, 0x28eb(%rip)      # 0x40e4
movl $0x0, 0x6c(%rsp)
cmpl $0x2, 0x6c(%rsp)
jge 0x1829
movslq 0x6c(%rsp), %rcx
leaq 0x28bc(%rip), %rax      # 0x40d0
movq $-0x1, (%rax,%rcx,8)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x1801
callq 0x10c0
movl 0x94(%rsp), %r9d
leaq 0x28a3(%rip), %rdi      # 0x40e0
leaq 0x28a0(%rip), %rsi      # 0x40e4
movl $0x1, %edx
xorl %r8d, %r8d
leaq 0x2871(%rip), %r10      # 0x40c4
leaq 0x2876(%rip), %rax      # 0x40d0
movl %r8d, %ecx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x10d0
cmpl $0x0, 0x90(%rsp)
jne 0x1a8e
movl 0x80(%rsp), %eax
imull 0x94(%rsp), %eax
movl %eax, 0x7c(%rsp)
cvtsi2sdl 0x284c(%rip), %xmm0 # 0x40e0
cvtsi2sdl 0x7c(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
cmpl $0x0, 0x2832(%rip)      # 0x40e0
jne 0x18c0
callq 0x1470
movsd %xmm0, 0x98(%rsp)
jmp 0x18e6
movsd 0x758(%rip), %xmm1      # 0x2020
mulsd 0xa8(%rsp), %xmm1
movsd 0x74f(%rip), %xmm0      # 0x2028
divsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x742(%rip), %xmm0      # 0x2030
subsd 0x98(%rsp), %xmm0
callq 0x1420
movsd %xmm0, 0xa0(%rsp)
movq 0x26cc(%rip), %rdi      # 0x3fd8
leaq 0x86b(%rip), %rsi       # 0x217e
callq 0x1090
movq 0x26b9(%rip), %rdi      # 0x3fd8
leaq 0x8cb(%rip), %rsi       # 0x21f1
callq 0x1090
movq 0x26a6(%rip), %rdi      # 0x3fd8
leaq 0x845(%rip), %rsi       # 0x217e
callq 0x1090
movq 0x2693(%rip), %rdi      # 0x3fd8
leaq 0x878(%rip), %rsi       # 0x21c4
callq 0x1090
movq %rax, 0x18(%rsp)
movl $0x8, %edi
callq 0x1ae0
movq 0x18(%rsp), %rdi
movl %eax, 0x68(%rsp)
movl 0x68(%rsp), %esi
callq 0x10a0
movq %rax, %rdi
movl 0x7c(%rsp), %esi
callq 0x10f0
movq %rax, %rdi
leaq 0x83c(%rip), %rsi       # 0x21c4
callq 0x1090
movq %rax, 0x20(%rsp)
movl $0x8, %edi
callq 0x1ae0
movq 0x20(%rsp), %rdi
movl %eax, 0x64(%rsp)
movl 0x64(%rsp), %esi
callq 0x10a0
movq %rax, %rdi
movl 0x2729(%rip), %esi      # 0x40e0
callq 0x10f0
movq %rax, %rdi
leaq 0x7fe(%rip), %rsi       # 0x21c4
callq 0x1090
movq %rax, 0x28(%rsp)
movl $0x10, %edi
callq 0x1ae0
movq 0x28(%rsp), %rdi
movl %eax, 0x60(%rsp)
movl 0x60(%rsp), %esi
callq 0x10a0
movq %rax, %rdi
movsd 0xa8(%rsp), %xmm0
callq 0x1110
movq %rax, %rdi
leaq 0x7bd(%rip), %rsi       # 0x21c4
callq 0x1090
movq %rax, 0x30(%rsp)
movl $0x10, %edi
callq 0x1ae0
movq 0x30(%rsp), %rdi
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %esi
callq 0x10a0
movq %rax, %rdi
movsd 0x98(%rsp), %xmm0
callq 0x1110
movq %rax, %rdi
leaq 0x77c(%rip), %rsi       # 0x21c4
callq 0x1090
movq %rax, 0x38(%rsp)
movl $0x10, %edi
callq 0x1ae0
movq 0x38(%rsp), %rdi
movl %eax, 0x58(%rsp)
movl 0x58(%rsp), %esi
callq 0x10a0
movq %rax, %rdi
movsd 0xa0(%rsp), %xmm0
callq 0x1110
movq %rax, %rdi
leaq 0x6f5(%rip), %rsi       # 0x217e
callq 0x1090
cmpl $0x0, 0x90(%rsp)
jne 0x1ad1
movq 0x2539(%rip), %rdi      # 0x3fd8
leaq 0x6d8(%rip), %rsi       # 0x217e
callq 0x1090
movq 0x2526(%rip), %rdi      # 0x3fd8
leaq 0x57f(%rip), %rsi       # 0x2038
callq 0x1090
movq 0x2513(%rip), %rdi      # 0x3fd8
leaq 0x76c(%rip), %rsi       # 0x2238
callq 0x1090
callq 0x1050
xorl %eax, %eax
addq $0xd8, %rsp
retq