// File: /workspace/github_cpp_makefile_star2_O2/patmorin[P]rotate/permtest.cpp
// Function: main
int main(int argc, char *argv[]) {
	Index n;
    if (argc != 2) usage(argv[0]);
    std::istringstream is_n(argv[1]);
    is_n >> n;

	while (n > 0) {
		Index k = 0, cum = 1;
		while (3*cum-1 <= n) {
			k++;
			cum *= 3;
		}
		Index m = cum-1;
		std::cout << "n = " << n << ", k = " << k << ", cum = " << cum
			<< ", m = " << m << std::endl;

		n -= m;

		Index sum = 0;
		auto start = std::chrono::high_resolution_clock::now();
		for (Index i = 0; i < m; i++) {
			sum += outshuffle_perm1(i, m);
		}
		auto stop =  std::chrono::high_resolution_clock::now();
		std::chrono::duration<double> elapsed = stop - start;
		std::cout << "outshuffle1: " << elapsed.count() << " " << sum << std::endl;

		sum = 0;
		start = std::chrono::high_resolution_clock::now();
		for (Index i = 0; i < m; i++) {
			sum += outshuffle_perm2(i, m);
		}
		stop =  std::chrono::high_resolution_clock::now();
		elapsed = stop - start;
		std::cout << "outshuffle2: " << elapsed.count() << " " << sum << std::endl;

		sum = 0;
		start = std::chrono::high_resolution_clock::now();
		for (Index i = 0; i < m; i++) {
			sum += outshuffle_perm3(i, m);
		}
		stop =  std::chrono::high_resolution_clock::now();
		elapsed = stop - start;
		std::cout << "outshuffle3: " << elapsed.count() << " " << sum << std::endl;

		sum = 0;
		start = std::chrono::high_resolution_clock::now();
		for (Index i = 0; i < m; i++) {
			sum += inshuffle_perm1(i, m);
		}
		stop =  std::chrono::high_resolution_clock::now();
		elapsed = stop - start;
		std::cout << "inshuffle1: " << elapsed.count() << " " << sum << std::endl;

		sum = 0;
		start = std::chrono::high_resolution_clock::now();
		for (Index i = 0; i < m; i++) {
			sum += inshuffle_perm2(i, m);
		}
		stop =  std::chrono::high_resolution_clock::now();
		elapsed = stop - start;
		std::cout << "inshuffle2: " << elapsed.count() << " " << sum << std::endl;

		sum = 0;
		start = std::chrono::high_resolution_clock::now();
		for (Index i = 0; i < m; i++) {
			sum += inshuffle_perm3(i, m);
		}
		stop =  std::chrono::high_resolution_clock::now();
		elapsed = stop - start;
		std::cout << "inshuffle3: " << elapsed.count() << " " << sum << std::endl;

		if (m < 50) {
			for (Index i = 0; i < m-1; i++) {
				std::cout << inshuffle_perm1(i, m) << ",";
			}
			std::cout << inshuffle_perm1(m-1, m) << std::endl;

			for (Index i = 0; i < m-1; i++) {
				std::cout << inshuffle_perm2(i, m) << ",";
			}
			std::cout << inshuffle_perm2(m-1, m) << std::endl;

			for (Index i = 0; i < m-1; i++) {
				std::cout << inshuffle_perm3(i, m) << ",";
			}
			std::cout << inshuffle_perm3(m-1, m) << std::endl;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
cmpl $0x2, %edi
jne 0x1806
movq 0x8(%rsi), %rsi
leaq 0x28(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x1040
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x10f0
leaq 0x28(%rsp), %rdi
callq 0x1050
leaq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1080
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x17e8
pushq $-0x1
popq %r14
pushq $0x1
popq %rcx
movq %rcx, %r13
leaq (%rcx,%rcx,2), %rcx
leaq -0x1(%r13,%r13,2), %rdx
incq %r14
cmpq %rax, %rdx
jbe 0x12ea
movq 0x2ccb(%rip), %rdi      # 0x3fd0
leaq 0xd11(%rip), %rsi       # 0x201d
callq 0x10b0
movq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x1090
leaq 0xcfd(%rip), %rsi       # 0x2022
movq %rax, %rdi
callq 0x10b0
movq %rax, %rdi
movq %r14, %rsi
callq 0x1090
leaq 0xcea(%rip), %rsi       # 0x2029
movq %rax, %rdi
callq 0x10b0
movq %rax, %rdi
movq %r13, %rsi
callq 0x1090
leaq 0xcd9(%rip), %rsi       # 0x2032
movq %rax, %rdi
callq 0x10b0
leaq -0x1(%r13), %rbx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1070
subq %rbx, 0x10(%rsp)
callq 0x1030
leaq 0x1(%r13), %rsi
movq %rax, %r14
shrq %rsi
movq %rsi, %rcx
pushq $0x1
popq %rdi
xorl %r15d, %r15d
cmpq %rdi, %r13
je 0x13af
movq %rcx, %rax
xorl %edx, %edx
divq %r13
incq %rdi
addq %rsi, %rcx
leaq -0x1(%r15,%rdx), %r15
jmp 0x1395
callq 0x1030
movq %rax, %r12
movq 0x2c12(%rip), %rdi      # 0x3fd0
leaq 0xc74(%rip), %rsi       # 0x2039
callq 0x10b0
subq %r14, %r12
vcvtsi2sd %r12, %xmm1, %xmm0
vdivsd 0xc2e(%rip), %xmm0, %xmm0 # 0x2008
movq %rax, %rdi
callq 0x10e0
leaq 0xc85(%rip), %rsi       # 0x206e
movq %rax, %rdi
callq 0x10b0
movq %rax, %rdi
movq %r15, %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1070
callq 0x1030
movq %rbx, %rbp
shrq %rbp
movq %rax, %r14
xorl %eax, %eax
xorl %r15d, %r15d
cmpq %rax, %rbx
je 0x1438
movq %rax, %rcx
shrq %rcx
testb $0x1, %al
movl $0x0, %edx
cmoveq %rbp, %rdx
addq %rcx, %r15
incq %rax
addq %rdx, %r15
jmp 0x1417
callq 0x1030
movq %rax, %r12
movq 0x2b89(%rip), %rdi      # 0x3fd0
leaq 0xbf9(%rip), %rsi       # 0x2047
callq 0x10b0
subq %r14, %r12
vcvtsi2sd %r12, %xmm1, %xmm0
vdivsd 0xba5(%rip), %xmm0, %xmm0 # 0x2008
movq %rax, %rdi
callq 0x10e0
leaq 0xbfc(%rip), %rsi       # 0x206e
movq %rax, %rdi
callq 0x10b0
movq %rax, %rdi
movq %r15, %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1070
callq 0x1030
movq %rax, %r14
xorl %eax, %eax
xorl %r15d, %r15d
cmpq %rax, %rbx
je 0x14bb
movq %rax, %rcx
shrq %rcx
testb $0x1, %al
movl $0x0, %edx
cmoveq %rbp, %rdx
addq %rcx, %r15
incq %rax
addq %rdx, %r15
jmp 0x149a
callq 0x1030
movq %rax, %r12
movq 0x2b06(%rip), %rdi      # 0x3fd0
leaq 0xb84(%rip), %rsi       # 0x2055
callq 0x10b0
subq %r14, %r12
vcvtsi2sd %r12, %xmm1, %xmm0
vdivsd 0xb22(%rip), %xmm0, %xmm0 # 0x2008
movq %rax, %rdi
callq 0x10e0
leaq 0xb79(%rip), %rsi       # 0x206e
movq %rax, %rdi
callq 0x10b0
movq %rax, %rdi
movq %r15, %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1070
callq 0x1030
movq %rax, %r14
pushq $0x2
popq %rcx
pushq $0x1
popq %rsi
xorl %r15d, %r15d
cmpq %rsi, %r13
je 0x153c
movq %rcx, %rax
xorl %edx, %edx
divq %r13
incq %rsi
addq $0x2, %rcx
leaq -0x1(%r15,%rdx), %r15
jmp 0x1521
callq 0x1030
movq %rax, %r12
movq 0x2a85(%rip), %rdi      # 0x3fd0
leaq 0xb11(%rip), %rsi       # 0x2063
callq 0x10b0
subq %r14, %r12
vcvtsi2sd %r12, %xmm1, %xmm0
vdivsd 0xaa1(%rip), %xmm0, %xmm0 # 0x2008
movq %rax, %rdi
callq 0x10e0
leaq 0xaf8(%rip), %rsi       # 0x206e
movq %rax, %rdi
callq 0x10b0
movq %rax, %rdi
movq %r15, %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1070
callq 0x1030
movq %r13, %rsi
negq %rsi
movq %rax, %r12
pushq $0x1
popq %rax
xorl %r15d, %r15d
xorl %ecx, %ecx
cmpq %rcx, %rbx
je 0x15c3
leaq (%rsi,%rax), %rdx
cmpq %rcx, %rbp
cmovaq %rax, %rdx
incq %rcx
addq $0x2, %rax
addq %rdx, %r15
jmp 0x15a7
movq %rsi, 0x20(%rsp)
callq 0x1030
movq %rax, %r14
movq 0x29f9(%rip), %rdi      # 0x3fd0
leaq 0xa92(%rip), %rsi       # 0x2070
callq 0x10b0
subq %r12, %r14
vcvtsi2sd %r14, %xmm1, %xmm0
vdivsd 0xa15(%rip), %xmm0, %xmm0 # 0x2008
movq %rax, %rdi
callq 0x10e0
leaq 0xa6c(%rip), %rsi       # 0x206e
movq %rax, %rdi
callq 0x10b0
movq %rax, %rdi
movq %r15, %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1070
callq 0x1030
movq %rax, %r12
xorl %r15d, %r15d
xorl %eax, %eax
xorl %edx, %edx
cmpq %rax, %rbx
je 0x164a
cmpq %rax, %rbp
leaq (%r15,%rax,2), %r15
movq %r13, %rcx
cmovaq %rdx, %rcx
incq %rax
subq %rcx, %r15
incq %r15
jmp 0x162c
callq 0x1030
movq %rax, %r14
movq 0x2977(%rip), %rdi      # 0x3fd0
leaq 0xa1d(%rip), %rsi       # 0x207d
callq 0x10b0
subq %r12, %r14
vcvtsi2sd %r14, %xmm1, %xmm0
vdivsd 0x993(%rip), %xmm0, %xmm0 # 0x2008
movq %rax, %rdi
callq 0x10e0
leaq 0x9ea(%rip), %rsi       # 0x206e
movq %rax, %rdi
callq 0x10b0
movq %rax, %rdi
movq %r15, %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1070
cmpq $0x31, %rbx
ja 0x12d5
leaq -0x2(%r13), %r15
pushq $0x2
popq %r12
movq %r12, %r14
cmpq %r12, %r13
je 0x16e9
movq %r14, %rax
xorl %edx, %edx
divq %r13
leaq -0x1(%rdx), %rsi
movq 0x2904(%rip), %rdi      # 0x3fd0
callq 0x1090
leaq 0x9b2(%rip), %rsi       # 0x208a
movq %rax, %rdi
callq 0x10b0
incq %r12
addq $0x2, %r14
jmp 0x16b4
leaq 0x2(%r15,%r15), %rax
xorl %edx, %edx
divq %r13
leaq -0x1(%rdx), %rsi
movq 0x28d2(%rip), %rdi      # 0x3fd0
callq 0x1090
movq %rax, %rdi
callq 0x1070
leaq (%r15,%r15), %rax
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r12
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x1754
movq 0x20(%rsp), %rax
cmpq %r14, %rbp
leaq (%rax,%r12), %rsi
cmovaq %r12, %rsi
movq 0x2899(%rip), %rdi      # 0x3fd0
callq 0x1090
leaq 0x947(%rip), %rsi       # 0x208a
movq %rax, %rdi
callq 0x10b0
incq %r14
addq $0x2, %r12
jmp 0x171b
movq 0x18(%rsp), %rsi
movq %rsi, %r12
orq $0x1, %r12
subq %rbx, %rsi
cmpq %r15, %rbp
cmovaq %r12, %rsi
movq 0x285f(%rip), %rdi      # 0x3fd0
callq 0x1090
movq %rax, %rdi
callq 0x1070
pushq $0x1
popq %rbx
xorl %r14d, %r14d
movl $0x0, %ecx
cmpq %r14, %r15
je 0x17c2
cmpq %r14, %rbp
movq %r13, %rax
movq %rbx, %rsi
cmovaq %rcx, %rax
subq %rax, %rsi
movq 0x282b(%rip), %rdi      # 0x3fd0
callq 0x1090
leaq 0x8d9(%rip), %rsi       # 0x208a
movq %rax, %rdi
callq 0x10b0
incq %r14
addq $0x2, %rbx
jmp 0x1784
cmpq %r15, %rbp
cmovaq %rcx, %r13
subq %r13, %r12
movq 0x27fd(%rip), %rdi      # 0x3fd0
movq %r12, %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1070
jmp 0x12d5
leaq 0x48(%rsp), %rdi
callq 0x1060
xorl %eax, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsi), %rdi
callq 0x123c
jmp 0x182c
leaq 0x28(%rsp), %rdi
movq %rax, %rbx
callq 0x1050
jmp 0x1839
movq %rax, %rbx
jmp 0x1839
jmp 0x182c
jmp 0x182c
jmp 0x182c
jmp 0x182c
leaq 0x48(%rsp), %rdi
movq %rax, %rbx
callq 0x1060
movq %rbx, %rdi
callq 0x1100