// File: /workspace/github_c_makefile_star2_O2/openshmem-org[P]openshmem-examples/c/cpi.c
// Function: main
int
main(int argc, char *argv[])
{
    struct utsname u;
    int myid, numprocs, i;
    double h, sum;
    struct timeval startwtime, endwtime;
    double *pWrkR;              /* symmetric reduction workspace */

    uname(&u);

    shmem_init();
    numprocs = shmem_n_pes();
    myid = shmem_my_pe();

    printf("PE %d on host \"%s\"\n", myid, u.nodename);

    if (myid == 0) {
        if (argc > 1)
            n = atoi(argv[1]);  /* # rectangles on command line */
        else
            n = 10000;          /* default # of rectangles */

        gettimeofday(&startwtime, NULL);
    }

    /* reduction of 1 value: size the workspace accordingly */
    {
        const long pWrkRSize = max2(1 / 2 + 1, SHMEM_REDUCE_MIN_WRKDATA_SIZE);

        pWrkR = (double *) shmem_malloc(pWrkRSize * sizeof(*pWrkR));
    }

    /* initialize sync arrays */
    for (i = 0; i < SHMEM_BCAST_SYNC_SIZE; i += 1) {
        pSyncB[i] = SHMEM_SYNC_VALUE;
    }
    for (i = 0; i < SHMEM_REDUCE_SYNC_SIZE; i += 1) {
        pSyncR[i] = SHMEM_SYNC_VALUE;
    }

    shmem_barrier_all();

    /* -=- set up done -=- */

    /* send "n" out to everyone */
    shmem_broadcast32(&n, &n, 1, 0, 0, 0, numprocs, pSyncB);

    /* do partial computation */
    h = 1.0 / (double) n;
    sum = 0.0;
    /* A slightly better approach starts from large i and works back */
    for (i = myid + 1; i <= n; i += numprocs) {
        const double x = h * ((double) i - 0.5);
        sum += f(x);
    }
    mypi = h * sum;

    /* wait for everyone to finish */
    shmem_barrier_all();

    /* add up partial pi computations into PI */
    shmem_double_sum_to_all(&pi, &mypi, 1, 0, 0, numprocs, pWrkR, pSyncR);

    /* "master" PE summarizes */
    if (myid == 0) {
        double elapsed;
        gettimeofday(&endwtime, NULL);
        elapsed = (endwtime.tv_sec - startwtime.tv_sec) * 1000.0;   /* sec to ms */
        elapsed += (endwtime.tv_usec - startwtime.tv_usec) / 1000.0;    /* us to ms */
        printf("pi is approximately %.16f, Error is %.16f\n",
               pi, fabs(pi - PI25DT));
        printf("run time = %f ms\n", elapsed);
        fflush(stdout);
    }

    shmem_free(pWrkR);

    shmem_finalize();

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x32(%rsp), %rdi
callq 0x1040
callq 0x10e0
callq 0x1070
movl %eax, %r15d
callq 0x10a0
movl %eax, %r14d
leaq 0x73(%rsp), %rdx
leaq 0xdf3(%rip), %rdi       # 0x2040
movl %eax, %esi
xorl %eax, %eax
callq 0x1030
testl %r14d, %r14d
jne 0x1280
movl $0x2710, %eax           # imm = 0x2710
cmpl $0x2, %ebp
jl 0x126e
movq 0x8(%rbx), %rdi
callq 0x10b0
movl %eax, 0x2e3c(%rip)      # 0x40b0
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x1050
pushq $0x8
popq %rdi
callq 0x10c0
movq %rax, %rbx
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x2e29(%rip)     # 0x40c0
xorl %eax, %eax
leaq 0x2e30(%rip), %r13      # 0x40d0
cmpq $0x18, %rax
je 0x12b1
orq $-0x1, (%rax,%r13)
addq $0x8, %rax
jmp 0x12a0
callq 0x10d0
leaq 0x2e03(%rip), %rax      # 0x40c0
leaq 0x2dec(%rip), %rdi      # 0x40b0
pushq $0x1
popq %rdx
movq %rdi, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %r15
callq 0x1060
popq %rax
popq %rcx
movl 0x2dce(%rip), %eax      # 0x40b0
cvtsi2sd %eax, %xmm2
movsd 0xd1a(%rip), %xmm1      # 0x2008
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
leal 0x1(%r14), %ecx
xorpd %xmm2, %xmm2
movsd 0xd0a(%rip), %xmm3      # 0x2010
movsd 0xd0a(%rip), %xmm4      # 0x2018
cmpl %eax, %ecx
jg 0x1337
cvtsi2sd %ecx, %xmm5
addsd %xmm3, %xmm5
mulsd %xmm0, %xmm5
mulsd %xmm5, %xmm5
addsd %xmm1, %xmm5
movapd %xmm4, %xmm6
divsd %xmm5, %xmm6
addsd %xmm6, %xmm2
addl %r15d, %ecx
jmp 0x130e
mulsd %xmm2, %xmm0
movsd %xmm0, 0x2da5(%rip)     # 0x40e8
leaq 0x2d9e(%rip), %r12      # 0x40e8
callq 0x10d0
leaq 0x2d9a(%rip), %rdi      # 0x40f0
pushq $0x1
popq %rdx
movq %r12, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r15d, %r9d
pushq %r13
pushq %rbx
callq 0x1080
popq %rax
popq %rcx
testl %r14d, %r14d
jne 0x1408
leaq 0x20(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x1050
movq (%r14), %rax
subq 0x10(%rsp), %rax
movq 0x8(%r14), %rcx
cvtsi2sd %rax, %xmm0
movsd 0xc81(%rip), %xmm1      # 0x2020
subq 0x18(%rsp), %rcx
cvtsi2sd %rcx, %xmm2
mulsd %xmm1, %xmm0
divsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x2d2d(%rip), %xmm0     # 0x40f0
movsd 0xc5d(%rip), %xmm1      # 0x2028
addsd %xmm0, %xmm1
andpd 0xc59(%rip), %xmm1      # 0x2030
leaq 0xc76(%rip), %rdi       # 0x2054
movb $0x2, %al
callq 0x1030
leaq 0xc93(%rip), %rdi       # 0x207f
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x1030
movq 0x2bf8(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
callq 0x1100
movq %rbx, %rdi
callq 0x10f0
callq 0x1090
xorl %eax, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq