// File: /workspace/github_c_makefile_star2_O1/USArmyResearchLab[P]ARL_OpenSHMEM/test/atomic_add.c
// Function: main
int main (void)
{
	int npe, i;
	static int dest;
	static unsigned int t, tsum;
	static int pWrk[SHMEM_REDUCE_MIN_WRKDATA_SIZE];
	static long pSync[SHMEM_REDUCE_SYNC_SIZE];
	for (i = 0; i < SHMEM_REDUCE_SYNC_SIZE; i++) {
		pSync[i] = SHMEM_SYNC_VALUE;
	}

	shmem_init();
	int me = shmem_my_pe();
	int npes = shmem_n_pes();

	if (me == 0) {
		printf("# SHMEM Atomic Add Performance for variable NPES\n" \
			"# NPES\tLatency (nanoseconds)\n");
	}

	for (npe = 2; npe <= npes; npe++)
	{
		int nxtpe = me + 1;
		if (nxtpe >= npe) nxtpe -= npe;

		dest = 0;
		shmem_barrier_all();
		ctimer_start();

		if (me < npe) {
			t = ctimer();
			for (i = 0; i < NLOOP; i++) {
				shmem_int_atomic_add(&dest, nxtpe, nxtpe);
			}
			t -= ctimer();
			shmem_int_sum_to_all(&tsum, &t, 1, 0, 0, npe, pWrk, pSync);
		}

		if (me == 0) {
			unsigned int nsec = ctimer_nsec(tsum / (npe * NLOOP));
			printf("%5d %7u\n", npe, nsec);
		}

		shmem_barrier_all();

		if (me < npe) {
			if (dest != (NLOOP * me)) printf("# %d: ERROR %d %d\n",
				me,dest,NLOOP*me);
		} else {
			if (dest != 0) printf("# %d: ERROR %d\n", me, dest);
		}
	}
	shmem_finalize();

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorpd %xmm0, %xmm0
movapd %xmm0, 0x4dc7(%rip)     # 0x61d0
movapd %xmm0, 0x4daf(%rip)     # 0x61c0
movapd %xmm0, 0x4d97(%rip)     # 0x61b0
movapd %xmm0, 0x4d7f(%rip)     # 0x61a0
movapd %xmm0, 0x4d67(%rip)     # 0x6190
callq 0x1674
leaq 0x4d0b(%rip), %rax      # 0x6140
movl (%rax), %ebx
leaq 0x4d06(%rip), %rax      # 0x6144
movl (%rax), %r13d
testl %ebx, %ebx
jne 0x1451
leaq 0x2c00(%rip), %rdi      # 0x404c
callq 0x1080
cmpl $0x2, %r13d
jl 0x1651
leal 0x1(%rbx), %eax
movl %eax, -0x30(%rbp)
imull $0x64, %ebx, %eax
movl %eax, -0x2c(%rbp)
movl $0x2, %r15d
leaq 0x4cec(%rip), %r12      # 0x6160
movl %r13d, -0x34(%rbp)
cmpl %r15d, -0x30(%rbp)
movl %r15d, %r14d
movl $0x0, %eax
cmovll %eax, %r14d
movl %eax, 0x4cd2(%rip)      # 0x6160
callq 0x1668
leaq 0x4d46(%rip), %rdi      # 0x61e0
xorl %esi, %esi
callq 0x1120
cmpl %r15d, %ebx
jge 0x15de
movl -0x30(%rbp), %r13d
subl %r14d, %r13d
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x1120
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq 0x4d15(%rip), %rax      # 0x61e0
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subq 0x4d0e(%rip), %rcx      # 0x61e8
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
mulsd 0x2b1e(%rip), %xmm1     # 0x4008
mulsd 0x2b1e(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2b0e(%rip), %xmm0     # 0x4018
cvttsd2si %xmm0, %rdx
andl %ecx, %edx
orl %edx, %eax
notl %eax
movl %eax, 0x4c49(%rip)      # 0x6164
movl $0x64, %r14d
movq %r12, %rdi
movl %r13d, %esi
movl %r13d, %edx
callq 0x2f78
decl %r14d
jne 0x1521
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x1120
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq 0x4c92(%rip), %rax      # 0x61e0
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subq 0x4c8b(%rip), %rcx      # 0x61e8
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
mulsd 0x2a9b(%rip), %xmm1     # 0x4008
mulsd 0x2a9b(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2a8b(%rip), %xmm0     # 0x4018
cvttsd2si %xmm0, %rdx
andl %ecx, %edx
orl %edx, %eax
movl 0x4bc8(%rip), %ecx      # 0x6164
addl %ecx, %eax
incl %eax
movl %eax, 0x4bbe(%rip)      # 0x6164
leaq 0x4bbb(%rip), %rdi      # 0x6168
leaq 0x4bb0(%rip), %rsi      # 0x6164
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r15d, %r9d
leaq 0x4bc8(%rip), %rax      # 0x6190
pushq %rax
leaq 0x4ba0(%rip), %rax      # 0x6170
pushq %rax
callq 0x2fb8
addq $0x10, %rsp
movl -0x34(%rbp), %r13d
testl %ebx, %ebx
jne 0x1603
movl 0x4b80(%rip), %eax      # 0x6168
imull $0x64, %r15d, %ecx
xorl %edx, %edx
divl %ecx
leaq 0x2a29(%rip), %rdi      # 0x4020
movl %r15d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x10f0
callq 0x1668
movl 0x4b52(%rip), %edx      # 0x6160
cmpl %r15d, %ebx
jge 0x162d
cmpl -0x2c(%rbp), %edx
je 0x1641
leaq 0x2a0a(%rip), %rdi      # 0x4029
movl %ebx, %esi
movl -0x2c(%rbp), %ecx
xorl %eax, %eax
callq 0x10f0
jmp 0x1641
testl %edx, %edx
je 0x1641
leaq 0x2a04(%rip), %rdi      # 0x403c
movl %ebx, %esi
xorl %eax, %eax
callq 0x10f0
leal 0x1(%r15), %eax
cmpl %r13d, %r15d
movl %eax, %r15d
jne 0x1478
callq 0x1c3a
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop