// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movslq -0x38(%rbp), %rax
cmpq $0xa, %rax
jae 0x137e
movslq -0x38(%rbp), %rcx
leaq 0x5e15(%rip), %rax      # 0x7180
movq $0x0, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1356
callq 0x16c0
leaq 0x5da6(%rip), %rax      # 0x7130
movl (%rax), %eax
movl %eax, -0x40(%rbp)
leaq 0x5d9e(%rip), %rax      # 0x7134
movl (%rax), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0x13af
leaq 0x3c78(%rip), %rdi      # 0x5020
movb $0x0, %al
callq 0x10e0
movl $0x2, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jg 0x1671
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jl 0x13de
movl -0x3c(%rbp), %ecx
movl -0x48(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
xorl %eax, %eax
subl -0x40(%rbp), %eax
movl %eax, 0x5d60(%rip)      # 0x7150
callq 0x16b0
callq 0x1680
movl -0x40(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x158c
xorl %eax, %eax
movl %eax, %esi
leaq -0x18(%rbp), %rdi
callq 0x1110
movq 0x5db6(%rip), %rdx      # 0x71d0
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
subq %rdx, %rcx
cvtsi2sd %rcx, %xmm0
movq 0x5da7(%rip), %rcx      # 0x71d8
subq %rcx, %rax
cvtsi2sd %rax, %xmm1
movsd 0x3bc7(%rip), %xmm2     # 0x5008
mulsd %xmm2, %xmm1
movsd 0x3bc3(%rip), %xmm2     # 0x5010
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x3bac(%rip), %xmm1     # 0x5018
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x8(%rbp)
movq $-0x1, %rax
subq -0x8(%rbp), %rax
movl %eax, 0x5cc4(%rip)      # 0x7154
movl $0x0, -0x38(%rbp)
cmpl $0x2710, -0x38(%rbp)    # imm = 0x2710
jge 0x14c0
movl -0x48(%rbp), %esi
movl -0x48(%rbp), %edx
leaq 0x5ca3(%rip), %rdi      # 0x7150
callq 0x4230
movl %eax, -0x4c(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1497
xorl %eax, %eax
movl %eax, %esi
leaq -0x30(%rbp), %rdi
callq 0x1110
movq 0x5cfc(%rip), %rdx      # 0x71d0
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
subq %rdx, %rcx
cvtsi2sd %rcx, %xmm0
movq 0x5ced(%rip), %rcx      # 0x71d8
subq %rcx, %rax
cvtsi2sd %rax, %xmm1
movsd 0x3b0d(%rip), %xmm2     # 0x5008
mulsd %xmm2, %xmm1
movsd 0x3b09(%rip), %xmm2     # 0x5010
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x3af2(%rip), %xmm1     # 0x5018
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x20(%rbp)
movq $-0x1, %rcx
subq -0x20(%rbp), %rcx
movl 0x5c0a(%rip), %eax      # 0x7154
subq %rcx, %rax
movl %eax, 0x5c01(%rip)      # 0x7154
movl -0x3c(%rbp), %r9d
leaq 0x5bfa(%rip), %rdi      # 0x7158
leaq 0x5bef(%rip), %rsi      # 0x7154
movl $0x1, %edx
xorl %r8d, %r8d
leaq 0x5bec(%rip), %r10      # 0x7160
leaq 0x5c05(%rip), %rax      # 0x7180
movl %r8d, %ecx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x42a0
cmpl $0x0, -0x40(%rbp)
jne 0x15c3
movl 0x5bc0(%rip), %eax      # 0x7158
imull $0x2710, -0x3c(%rbp), %ecx # imm = 0x2710
xorl %edx, %edx
divl %ecx
movl %eax, %eax
movl %eax, %edi
callq 0x16a0
movl %eax, -0x50(%rbp)
movl -0x3c(%rbp), %esi
movl -0x50(%rbp), %edx
leaq 0x3ab4(%rip), %rdi      # 0x5070
movb $0x0, %al
callq 0x10e0
movl -0x40(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x1617
movl 0x5b7f(%rip), %eax      # 0x7150
cmpl -0x40(%rbp), %eax
je 0x15ed
movl -0x40(%rbp), %esi
movl 0x5b71(%rip), %edx      # 0x7150
leaq 0x3a93(%rip), %rdi      # 0x5079
movb $0x0, %al
callq 0x10e0
movl -0x4c(%rbp), %eax
xorl %ecx, %ecx
subl -0x48(%rbp), %ecx
cmpl %ecx, %eax
je 0x1615
xorl %eax, %eax
testb $0x1, %al
jne 0x1601
jmp 0x1615
movl -0x40(%rbp), %esi
movl -0x4c(%rbp), %edx
leaq 0x3a82(%rip), %rdi      # 0x5090
movb $0x0, %al
callq 0x10e0
jmp 0x1661
movl 0x5b33(%rip), %eax      # 0x7150
xorl %ecx, %ecx
subl -0x40(%rbp), %ecx
cmpl %ecx, %eax
je 0x163d
movl -0x40(%rbp), %esi
movl 0x5b21(%rip), %edx      # 0x7150
leaq 0x3a43(%rip), %rdi      # 0x5079
movb $0x0, %al
callq 0x10e0
cmpl $0x0, -0x4c(%rbp)
je 0x165f
xorl %eax, %eax
testb $0x1, %al
jne 0x164b
jmp 0x165f
movl -0x40(%rbp), %esi
movl -0x4c(%rbp), %edx
leaq 0x3a38(%rip), %rdi      # 0x5090
movb $0x0, %al
callq 0x10e0
jmp 0x1661
jmp 0x1663
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x13b6
callq 0x2050
xorl %eax, %eax
addq $0x60, %rsp
popq %rbp
retq
nop