// File: /workspace/github_c_makefile_star2_O2/USArmyResearchLab[P]ARL_OpenSHMEM/test/atomic_swap.c
// Function: main
int main (void)
{
	int i, npe;
	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 Swap Performance for variable NPES\n" \
			"# NPES\tLatency (nanoseconds)\n");
	}

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

		int prev = 0;
		dest = -me;
		shmem_barrier_all();
		ctimer_start();

		if (me < npe) {
			t = ctimer();
			for (i = 0; i < NLOOP; i++) {
				prev = shmem_int_atomic_swap(&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);
		}

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

	return 0;
}

// Assembly:
xorl %eax, %eax
leaq 0x4e2b(%rip), %rcx      # 0x6190
cmpq $0x50, %rax
je 0x1376
andq $0x0, (%rax,%rcx)
addq $0x8, %rax
jmp 0x1365
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
callq 0x15a8
leaq 0x4dad(%rip), %rax      # 0x6140
movl (%rax), %ebx
leaq 0x4da8(%rip), %rax      # 0x6144
movl (%rax), %r15d
testl %ebx, %ebx
jne 0x13af
leaq 0x2c96(%rip), %rdi      # 0x4040
callq 0x1080
leal 0x1(%rbx), %eax
movl %eax, -0x30(%rbp)
movl %ebx, %eax
negl %eax
movl %eax, -0x2c(%rbp)
pushq $0x2
popq %r12
leaq 0x4d99(%rip), %r13      # 0x6160
movl %r15d, -0x34(%rbp)
cmpl %r15d, %r12d
jg 0x1586
cmpl %r12d, -0x30(%rbp)
movl %r12d, %r14d
movl $0x0, %eax
cmovll %eax, %r14d
movl -0x2c(%rbp), %eax
movl %eax, 0x4d73(%rip)      # 0x6160
callq 0x159c
leaq 0x4de7(%rip), %rdi      # 0x61e0
xorl %esi, %esi
callq 0x1120
cmpl %r12d, %ebx
jge 0x1530
movl -0x30(%rbp), %r15d
subl %r14d, %r15d
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x1120
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq 0x4db6(%rip), %rax      # 0x61e0
cvtsi2sd %rax, %xmm0
subq 0x4db2(%rip), %rcx      # 0x61e8
cvtsi2sd %rcx, %xmm1
mulsd 0x2bc5(%rip), %xmm1     # 0x4008
mulsd 0x2bc5(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2bb5(%rip), %xmm0     # 0x4018
cvttsd2si %xmm0, %rdx
andl %ecx, %edx
orl %edx, %eax
notl %eax
movl %eax, 0x4cf0(%rip)      # 0x6164
movl $0x2710, %r14d          # imm = 0x2710
subl $0x1, %r14d
jb 0x1490
movq %r13, %rdi
movl %r15d, %esi
movl %r15d, %edx
callq 0x2e14
jmp 0x147a
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x1120
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq 0x4d36(%rip), %rax      # 0x61e0
cvtsi2sd %rax, %xmm0
subq 0x4d32(%rip), %rcx      # 0x61e8
cvtsi2sd %rcx, %xmm1
mulsd 0x2b45(%rip), %xmm1     # 0x4008
mulsd 0x2b45(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2b35(%rip), %xmm0     # 0x4018
cvttsd2si %xmm0, %rdx
andl %ecx, %edx
orl %edx, %eax
movl 0x4c72(%rip), %ecx      # 0x6164
addl %ecx, %eax
incl %eax
movl %eax, 0x4c68(%rip)      # 0x6164
leaq 0x4c65(%rip), %rdi      # 0x6168
leaq 0x4c5a(%rip), %rsi      # 0x6164
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r12d, %r9d
leaq 0x4c74(%rip), %rax      # 0x6190
pushq %rax
leaq 0x4c4c(%rip), %rax      # 0x6170
pushq %rax
callq 0x2e64
popq %rax
popq %rcx
movl -0x34(%rbp), %r15d
testl %ebx, %ebx
jne 0x1558
movl 0x4c2e(%rip), %eax      # 0x6168
imull $0x2710, %r12d, %ecx    # imm = 0x2710
xorl %edx, %edx
divl %ecx
leaq 0x2ad4(%rip), %rdi      # 0x4020
movl %r12d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x10f0
movl 0x4c02(%rip), %edx      # 0x6160
cmpl %r12d, %ebx
jge 0x1569
cmpl %ebx, %edx
jne 0x156e
jmp 0x157e
cmpl -0x2c(%rbp), %edx
je 0x157e
leaq 0x2ab4(%rip), %rdi      # 0x4029
movl %ebx, %esi
xorl %eax, %eax
callq 0x10f0
incl %r12d
jmp 0x13cb
callq 0x1ba1
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq