#include <cuda_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <time.h>

#define ll long long
#define MAX_THREAD_PER_BLOCK 1024
#define MAX_NB_CITIES 50
#define DEBUG 1

__device__ int calc_perm_cost(ll idx, int n, ll nb_perm, int * dist, ll * fact) {
	// Test for valid idx
	if (idx < nb_perm) {
		// Resulting perm vector
		int perm[MAX_NB_CITIES + 1];

		// compute factorial code
		for (int k = 0; k < n; ++k) {
			perm[k] = idx / fact[n - 1 - k];
			idx = idx % fact[n - 1 - k];
		}

		// readjust values to obtain the permutation
		for (int k = n - 1; k > 0; --k) {
			for (int j = k - 1; j >= 0; --j) {
				if (perm[j] <= perm[k]) {
					perm[k]++;
				}
			}
		}

		// Loop path
		perm[n] = perm[0];

		// Perm cost
		int cost = 0;

		// Calc perm cost
		for (int i = 0; i < n; i++) {
			cost += dist[perm[i] * n + perm[i + 1]];
		}

		return cost;
	} else {
		return INT_MAX;
	}
}

__global__ void perm_cuda(int n, ll nb_perm, int * dist, ll * fact, int * mcost) {
	// Thread index
	int tidx = threadIdx.x;

	// Block index
	int bidx = blockIdx.x;

	// Global index
	ll idx = blockIdx.x * blockDim.x + threadIdx.x;

	// Shared dist matrix
	__shared__ int s_dist[MAX_NB_CITIES*MAX_NB_CITIES];

	// Copy global dist matrix to shared dist matrix
	int step = (n*n + blockDim.x - 1)/(blockDim.x);
	int start = tidx*step;
	for (int i = 0; i < step; i++) {
		if ((start + i) < (n*n)) {
			s_dist[start + i] = dist[start + i];
		}
	}

	__syncthreads();

	// Shared factorial
	__shared__ ll s_fact[MAX_NB_CITIES];

	// Copy global factorial to shared factorial
	if (tidx < n) {
		s_fact[tidx] = fact[tidx];
	}
	__syncthreads();

	// Shared cost
	__shared__ int s_mcost[MAX_THREAD_PER_BLOCK];

	// Minimal local cost
	s_mcost[tidx] = calc_perm_cost(idx, n, nb_perm, s_dist, s_fact);
	__syncthreads();

	// Reduce local cost to find global
	ll step_size = 1;
	int nb_threads = blockDim.x/2;

	while (nb_threads > 0) {
		if (tidx < nb_threads) {
			int fst = tidx * step_size * 2;
			int snd = fst  + step_size;

			if (s_mcost[snd] < s_mcost[fst]) {
				s_mcost[fst] = s_mcost[snd];
			}
		}

		step_size <<= 1;
		nb_threads >>= 1;

		__syncthreads();
	}

	// Put mcost from block in global
	if (tidx == 0) {
		mcost[bidx] = s_mcost[0];
	}
}

void printDeviceProps() {
	cudaSetDevice(0);
	cudaDeviceProp prop;
	cudaGetDeviceProperties(&prop, 0);

	printf("prop.name                        %s\n",       prop.name);
	printf("prop.totalGlobalMem              %zdGB\n",    prop.totalGlobalMem >> 10 >> 10 >> 10);
	printf("prop.sharedMemPerBlock           %zdKB\n",    prop.sharedMemPerBlock >> 10);
	printf("prop.regsPerBlock                %d\n",       prop.regsPerBlock);
	printf("prop.warpSize                    %d\n",       prop.warpSize);
	printf("prop.memPitch                    %zdGB\n",    prop.memPitch >> 10 >> 10 >> 10);
	printf("prop.maxThreadsPerBlock          %d\n",       prop.maxThreadsPerBlock);
	printf("prop.maxThreadsDim               %d %d %d\n", prop.maxThreadsDim[0], prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
	printf("prop.maxGridSize                 %d %d %d\n", prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2]);
	printf("prop.totalConstMem               %zdKB\n",    prop.totalConstMem >> 10);
	printf("prop.multiProcessorCount         %d\n",       prop.multiProcessorCount);
	printf("prop.l2CacheSize                 %dMB\n",     prop.l2CacheSize >> 10 >> 10);
	printf("prop.maxThreadsPerMultiProcessor %d\n",       prop.maxThreadsPerMultiProcessor);
	printf("prop.sharedMemPerMultiprocessor  %zdKB\n",    prop.sharedMemPerMultiprocessor >> 10);
	printf("prop.regsPerMultiprocessor       %d\n",       prop.regsPerMultiprocessor);
	printf("\n");
}

int * putDMatrixInDevice(int n) {
	// X coords
	int * x = (int *)malloc(n*sizeof(int));
	if (!x) {
		printf("malloc error\n");
		exit(1);
	}

	// Y coords
	int * y = (int *)malloc(n*sizeof(int));
	if (!y) {
		printf("malloc error\n");
		exit(1);
	}

	// Read
	for (int i = 0; i < n; i++) {
		scanf("%d %d", &x[i], &y[i]);
	}

	// Dist matrix on host
	int * h_dist = (int *)malloc(n*n*sizeof(int));
	if (!h_dist) {
		printf("malloc error\n");
		exit(1);
	}

	// Calc dist matrix
	int dx, dy;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			dx = x[i] - x[j];
			dy = y[i] - y[j];
			h_dist[i*n + j] = (int)sqrt(dx*dx + dy*dy);
		}
	}

	free(x);
	free(y);

	if (DEBUG) {
		// Print dist
		printf("dist\n");
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				printf("%3d ", h_dist[i*n + j]);
			}
			printf("\n");
		}
	}

	// Dist matrix on device
	int * d_dist;
	if (cudaMalloc(&d_dist, n*n*sizeof(int)) == cudaErrorMemoryAllocation) {
		printf("cudaMalloc error\n");
		exit(1);
	}

	// Copy host to device
	cudaMemcpy(d_dist, h_dist, n*n*sizeof(int), cudaMemcpyHostToDevice);

	free(h_dist);

	return d_dist;
}

ll * putFactInDevice(int n) {
	// Fact vector on host
	ll * h_fact = (ll *)malloc(n*sizeof(ll));
	if (!h_fact) {
		printf("malloc error\n");
		exit(1);
	}

	// Calc fact
	h_fact[0] = 1;
	for (int i = 1; i < n; i++) {
		h_fact[i] = i * h_fact[i - 1];
	}

	if (DEBUG) {
		// Print fact
		printf("fact ");
		for (int i = 0; i < n; i++) {
			printf("%lld ", h_fact[i]);
		}
		printf("\n");
	}

	// Fact vector on device
	ll * d_fact;
	if (cudaMalloc(&d_fact, n*sizeof(ll)) == cudaErrorMemoryAllocation) {
		printf("cudaMalloc error\n");
		exit(1);
	}

	// Copy host to device
	cudaMemcpy(d_fact, h_fact, n*sizeof(ll), cudaMemcpyHostToDevice);

	return d_fact;
}

ll factorial(int n) {
	return (n > 1 ? n*factorial(n-1) : 1);
}

int run_tsp() {
	int nb_cities;
	scanf("%d", &nb_cities);
	if (DEBUG) {
		printf("nb_cities  %d\n", nb_cities);
	}

	// Dist matrix
	int * d_dist = putDMatrixInDevice(nb_cities);

	// Fact vector
	ll * d_fact = putFactInDevice(nb_cities);

	// Number of permutations
	ll nb_perm = factorial(nb_cities);
	if (DEBUG) {
		printf("nb_perm    %lld\n", nb_perm);
	}

	// nb_blocks, nb_threads
	int nb_blocks  = (nb_perm + MAX_THREAD_PER_BLOCK - 1)/MAX_THREAD_PER_BLOCK;
	int nb_threads = (nb_perm > MAX_THREAD_PER_BLOCK ? MAX_THREAD_PER_BLOCK : nb_perm);
	if (DEBUG) {
		printf("nb_blocks  %d\n", nb_blocks);
		printf("nb_threads %d\n", nb_threads);
	}

	// Mim cost from blocks
	int * h_mcost = (int *)malloc(nb_blocks*sizeof(int));
	int * d_mcost;
	cudaMalloc(&d_mcost, nb_blocks*sizeof(int));

	// Call gpu
	clock_t start = clock();
	perm_cuda<<<nb_blocks, nb_threads>>>(nb_cities, nb_perm, d_dist, d_fact, d_mcost);
	cudaDeviceSynchronize();
	clock_t end = clock();

	// Time
	int msec = (end - start) * 1000 / CLOCKS_PER_SEC;
	printf("%ds %dms\n", msec/1000, msec%1000);

	// Copy device to host, ret
	cudaMemcpy(h_mcost, d_mcost, nb_blocks*sizeof(int), cudaMemcpyDeviceToHost);

	// Error check
	cudaError_t err = cudaGetLastError();
	if (err != cudaSuccess) {
		printf("CUDA Error: %s\n",cudaGetErrorString(err));
	}

	// Host global mim
	int global_mcost = INT_MAX;

	// Find global mim
	for (int i = 0; i < nb_blocks; i++) {
		if (h_mcost[i] < global_mcost) {
			global_mcost = h_mcost[i];
		}
	}

	return global_mcost;
}

int main() {
	if (DEBUG) {
		printDeviceProps();
	}

	int num_instances;

	scanf("%d", &num_instances);

	for (int i = 0; i < num_instances; i++) {
		printf("%d\n", run_tsp());
	}

	return 0;
} 
	.file	"tmpxft_0031bf0c_00000000-6_cuda_1_job_per_thread.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2036:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2036:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z14calc_perm_costxixPiPx
	.type	_Z14calc_perm_costxixPiPx, @function
_Z14calc_perm_costxixPiPx:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z14calc_perm_costxixPiPx, .-_Z14calc_perm_costxixPiPx
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"prop.name                        %s\n"
.LC1:
	.string	"prop.totalGlobalMem              %zdGB\n"
.LC2:
	.string	"prop.sharedMemPerBlock           %zdKB\n"
.LC3:
	.string	"prop.regsPerBlock                %d\n"
.LC4:
	.string	"prop.warpSize                    %d\n"
.LC5:
	.string	"prop.memPitch                    %zdGB\n"
.LC6:
	.string	"prop.maxThreadsPerBlock          %d\n"
.LC7:
	.string	"prop.maxThreadsDim               %d %d %d\n"
.LC8:
	.string	"prop.maxGridSize                 %d %d %d\n"
.LC9:
	.string	"prop.totalConstMem               %zdKB\n"
.LC10:
	.string	"prop.multiProcessorCount         %d\n"
.LC11:
	.string	"prop.l2CacheSize                 %dMB\n"
.LC12:
	.string	"prop.maxThreadsPerMultiProcessor %d\n"
.LC13:
	.string	"prop.sharedMemPerMultiprocessor  %zdKB\n"
.LC14:
	.string	"prop.regsPerMultiprocessor       %d\n"
.LC15:
	.string	"\n"
	.text
	.globl	_Z16printDevicePropsv
	.type	_Z16printDevicePropsv, @function
_Z16printDevicePropsv:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	xorl	%edi, %edi
	subq	$1040, %rsp
	.cfi_def_cfa_offset 1056
	movq	%fs:40, %rax
	movq	%rax, 1032(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rbx
	call	cudaSetDevice@PLT
	xorl	%esi, %esi
	movq	%rbx, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movq	%rbx, %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC0(%rip), %rsi
	call	__printf_chk@PLT
	movq	288(%rsp), %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	shrq	$30, %rdx
	call	__printf_chk@PLT
	movq	296(%rsp), %rdx
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	shrq	$10, %rdx
	call	__printf_chk@PLT
	movl	304(%rsp), %edx
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	308(%rsp), %edx
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	312(%rsp), %rdx
	leaq	.LC5(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	shrq	$30, %rdx
	call	__printf_chk@PLT
	movl	320(%rsp), %edx
	leaq	.LC6(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	332(%rsp), %r8d
	xorl	%eax, %eax
	movl	328(%rsp), %ecx
	movl	324(%rsp), %edx
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	344(%rsp), %r8d
	xorl	%eax, %eax
	movl	340(%rsp), %ecx
	movl	336(%rsp), %edx
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	352(%rsp), %rdx
	leaq	.LC9(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	shrq	$10, %rdx
	call	__printf_chk@PLT
	movl	388(%rsp), %edx
	leaq	.LC10(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	616(%rsp), %edx
	movl	$2, %edi
	leaq	.LC11(%rip), %rsi
	sarl	$20, %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	624(%rsp), %edx
	leaq	.LC12(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	640(%rsp), %rdx
	leaq	.LC13(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	shrq	$10, %rdx
	call	__printf_chk@PLT
	movl	648(%rsp), %edx
	leaq	.LC14(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	xorl	%eax, %eax
	movl	$2, %edi
	leaq	.LC15(%rip), %rsi
	call	__printf_chk@PLT
	movq	1032(%rsp), %rax
	subq	%fs:40, %rax
	je	.L5
	call	__stack_chk_fail@PLT
.L5:
	addq	$1040, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z16printDevicePropsv, .-_Z16printDevicePropsv
	.section	.rodata.str1.1
.LC16:
	.string	"malloc error\n"
.LC17:
	.string	"%d %d"
.LC18:
	.string	"dist\n"
.LC19:
	.string	"%3d "
.LC20:
	.string	"cudaMalloc error\n"
	.text
	.globl	_Z18putDMatrixInDevicei
	.type	_Z18putDMatrixInDevicei, @function
_Z18putDMatrixInDevicei:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edi, %ebx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	movslq	%edi, %rax
	leaq	0(,%rax,4), %r15
	movq	%rax, 16(%rsp)
	movq	%r15, %rdi
	call	malloc@PLT
	testq	%rax, %rax
	jne	.L9
.L10:
	leaq	.LC16(%rip), %rsi
	jmp	.L30
.L9:
	movq	%r15, %rdi
	movq	%rax, %r13
	call	malloc@PLT
	movq	%rax, %r12
	testq	%rax, %rax
	je	.L10
	movq	%r13, %rbp
	movq	%rax, %rdx
	xorl	%r14d, %r14d
.L11:
	cmpl	%ebx, %r14d
	jge	.L31
	movq	%rbp, %rsi
	leaq	.LC17(%rip), %rdi
	xorl	%eax, %eax
	incl	%r14d
	movq	%rdx, 8(%rsp)
	addq	$4, %rbp
	call	__isoc23_scanf@PLT
	movq	8(%rsp), %rdx
	addq	$4, %rdx
	jmp	.L11
.L31:
	movl	%ebx, %eax
	imull	%ebx, %eax
	cltq
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movq	%rax, %rbp
	testq	%rax, %rax
	je	.L10
	movq	%rax, %r14
	movq	%rax, %rdi
	xorl	%esi, %esi
.L13:
	cmpl	%esi, %ebx
	jle	.L32
	xorl	%edx, %edx
.L14:
	movl	0(%r13,%rsi,4), %eax
	movl	(%r12,%rsi,4), %ecx
	movq	%rdi, 40(%rsp)
	subl	0(%r13,%rdx,4), %eax
	subl	(%r12,%rdx,4), %ecx
	movq	%rsi, 32(%rsp)
	imull	%ecx, %ecx
	movq	%rdx, 24(%rsp)
	imull	%eax, %eax
	addl	%ecx, %eax
	cvtsi2sdl	%eax, %xmm0
	call	sqrt@PLT
	movq	24(%rsp), %rdx
	movq	40(%rsp), %rdi
	cvttsd2sil	%xmm0, %eax
	movq	32(%rsp), %rsi
	movl	%eax, (%rdi,%rdx,4)
	incq	%rdx
	cmpq	%rdx, 16(%rsp)
	jne	.L14
	incq	%rsi
	addq	%r15, %rdi
	jmp	.L13
.L32:
	movq	%r13, %rdi
	call	free@PLT
	movq	%r12, %rdi
	xorl	%r12d, %r12d
	call	free@PLT
	leaq	.LC18(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L15:
	cmpl	%ebx, %r12d
	jge	.L33
	leaq	(%r15,%r14), %r13
.L16:
	movl	(%r14), %edx
	leaq	.LC19(%rip), %rsi
	xorl	%eax, %eax
	addq	$4, %r14
	movl	$2, %edi
	call	__printf_chk@PLT
	cmpq	%r14, %r13
	jne	.L16
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incl	%r12d
	call	__printf_chk@PLT
	jmp	.L15
.L33:
	movq	8(%rsp), %rsi
	leaq	48(%rsp), %rdi
	call	cudaMalloc@PLT
	cmpl	$2, %eax
	jne	.L18
	leaq	.LC20(%rip), %rsi
.L30:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L18:
	movq	8(%rsp), %rdx
	movq	48(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	call	cudaMemcpy@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	48(%rsp), %rax
	movq	56(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L19
	call	__stack_chk_fail@PLT
.L19:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z18putDMatrixInDevicei, .-_Z18putDMatrixInDevicei
	.section	.rodata.str1.1
.LC21:
	.string	"fact "
.LC22:
	.string	"%lld "
	.text
	.globl	_Z15putFactInDevicei
	.type	_Z15putFactInDevicei, @function
_Z15putFactInDevicei:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movslq	%edi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movq	%rbp, %r12
	salq	$3, %rbp
	movq	%rbp, %rdi
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	leaq	.LC16(%rip), %rsi
	testq	%rax, %rax
	je	.L43
	movq	$1, (%rax)
	movq	%rax, %rbx
	xorl	%eax, %eax
.L36:
	incq	%rax
	cmpl	%eax, %r12d
	jle	.L44
	movq	-8(%rbx,%rax,8), %rdx
	imulq	%rax, %rdx
	movq	%rdx, (%rbx,%rax,8)
	jmp	.L36
.L44:
	leaq	.LC21(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%r13d, %r13d
	call	__printf_chk@PLT
	leaq	.LC22(%rip), %r14
.L38:
	cmpl	%r13d, %r12d
	jle	.L45
	movq	(%rbx,%r13,8), %rdx
	movq	%r14, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%r13
	call	__printf_chk@PLT
	jmp	.L38
.L45:
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rsp, %rdi
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	cmpl	$2, %eax
	jne	.L40
	leaq	.LC20(%rip), %rsi
.L43:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L40:
	movq	(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rdx
	movq	%rbx, %rsi
	call	cudaMemcpy@PLT
	movq	(%rsp), %rax
	movq	8(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L41
	call	__stack_chk_fail@PLT
.L41:
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z15putFactInDevicei, .-_Z15putFactInDevicei
	.globl	_Z9factoriali
	.type	_Z9factoriali, @function
_Z9factoriali:
.LFB2031:
	.cfi_startproc
	endbr64
	movslq	%edi, %rdx
	movl	$1, %eax
.L48:
	cmpl	$1, %edi
	jle	.L46
	imulq	%rdx, %rax
	decl	%edi
	decq	%rdx
	jmp	.L48
.L46:
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z9factoriali, .-_Z9factoriali
	.globl	_Z34__device_stub__Z9perm_cudaixPiPxS_ixPiPxS_
	.type	_Z34__device_stub__Z9perm_cudaixPiPxS_ixPiPxS_, @function
_Z34__device_stub__Z9perm_cudaixPiPxS_ixPiPxS_:
.LFB2058:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movl	%edi, 44(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	44(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L49
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z9perm_cudaixPiPxS_(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 192
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L49:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L51
	call	__stack_chk_fail@PLT
.L51:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2058:
	.size	_Z34__device_stub__Z9perm_cudaixPiPxS_ixPiPxS_, .-_Z34__device_stub__Z9perm_cudaixPiPxS_ixPiPxS_
	.globl	_Z9perm_cudaixPiPxS_
	.type	_Z9perm_cudaixPiPxS_, @function
_Z9perm_cudaixPiPxS_:
.LFB2059:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z9perm_cudaixPiPxS_ixPiPxS_
	.cfi_endproc
.LFE2059:
	.size	_Z9perm_cudaixPiPxS_, .-_Z9perm_cudaixPiPxS_
	.section	.rodata.str1.1
.LC23:
	.string	"%d"
.LC24:
	.string	"nb_cities  %d\n"
.LC25:
	.string	"nb_perm    %lld\n"
.LC26:
	.string	"nb_blocks  %d\n"
.LC27:
	.string	"nb_threads %d\n"
.LC28:
	.string	"%ds %dms\n"
.LC29:
	.string	"CUDA Error: %s\n"
	.text
	.globl	_Z7run_tspv
	.type	_Z7run_tspv, @function
_Z7run_tspv:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC23(%rip), %rdi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	$1024, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	36(%rsp), %rsi
	call	__isoc23_scanf@PLT
	movl	36(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC24(%rip), %rsi
	call	__printf_chk@PLT
	movl	36(%rsp), %edi
	call	_Z18putDMatrixInDevicei
	movl	36(%rsp), %edi
	movq	%rax, 16(%rsp)
	call	_Z15putFactInDevicei
	movl	36(%rsp), %edi
	leaq	.LC25(%rip), %rsi
	movq	%rax, 24(%rsp)
	call	_Z9factoriali
	movl	$2, %edi
	movq	%rax, %rbx
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1024, %ecx
	movl	$2, %edi
	leaq	1023(%rbx), %rax
	cqto
	leaq	.LC26(%rip), %rsi
	idivq	%rcx
	cmpq	%r12, %rbx
	cmovle	%rbx, %r12
	movq	%rax, %rbp
	movl	%eax, %edx
	movl	%eax, %r15d
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%r12d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC27(%rip), %rsi
	call	__printf_chk@PLT
	movslq	%ebp, %rax
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movq	8(%rsp), %rsi
	leaq	40(%rsp), %rdi
	movq	%rax, %r13
	call	cudaMalloc@PLT
	call	clock@PLT
	movl	%r12d, 60(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%rax, %r14
	movl	%ebp, 48(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movl	68(%rsp), %ecx
	movq	%rax, 52(%rsp)
	movq	60(%rsp), %rdx
	movq	48(%rsp), %rdi
	movl	56(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L55
	movq	40(%rsp), %r8
	movq	24(%rsp), %rcx
	movq	%rbx, %rsi
	movq	16(%rsp), %rdx
	movl	36(%rsp), %edi
	call	_Z34__device_stub__Z9perm_cudaixPiPxS_ixPiPxS_
.L55:
	call	cudaDeviceSynchronize@PLT
	call	clock@PLT
	movl	$1000, %ecx
	movl	$1000, %esi
	movl	$2, %edi
	subq	%r14, %rax
	cqto
	idivq	%rcx
	cltd
	idivl	%esi
	leaq	.LC28(%rip), %rsi
	movl	%edx, %ecx
	movl	%eax, %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	8(%rsp), %rdx
	movq	40(%rsp), %rsi
	movq	%r13, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L56
	call	cudaGetErrorString@PLT
	leaq	.LC29(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L56:
	xorl	%edx, %edx
	movl	$2147483647, %eax
.L57:
	cmpl	%edx, %r15d
	jle	.L64
	movl	0(%r13,%rdx,4), %ecx
	cmpl	%ecx, %eax
	cmovg	%ecx, %eax
	incq	%rdx
	jmp	.L57
.L64:
	movq	72(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L59
	call	__stack_chk_fail@PLT
.L59:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z7run_tspv, .-_Z7run_tspv
	.section	.rodata.str1.1
.LC30:
	.string	"%d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC30(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	xorl	%ebx, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	_Z16printDevicePropsv
	leaq	4(%rsp), %rsi
	leaq	.LC23(%rip), %rdi
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
.L66:
	cmpl	%ebx, 4(%rsp)
	jle	.L70
	call	_Z7run_tspv
	movq	%rbp, %rsi
	movl	$2, %edi
	incl	%ebx
	movl	%eax, %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L66
.L70:
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L68
	call	__stack_chk_fail@PLT
.L68:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2033:
	.size	main, .-main
	.section	.rodata.str1.1
.LC31:
	.string	"_Z9perm_cudaixPiPxS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2061:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC31(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z9perm_cudaixPiPxS_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2061:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <time.h>

#define ll long long
#define MAX_THREAD_PER_BLOCK 1024
#define MAX_NB_CITIES 50
#define DEBUG 1

__device__ int calc_perm_cost(ll idx, int n, ll nb_perm, int * dist, ll * fact) {
	// Test for valid idx
	if (idx < nb_perm) {
		// Resulting perm vector
		int perm[MAX_NB_CITIES + 1];

		// compute factorial code
		for (int k = 0; k < n; ++k) {
			perm[k] = idx / fact[n - 1 - k];
			idx = idx % fact[n - 1 - k];
		}

		// readjust values to obtain the permutation
		for (int k = n - 1; k > 0; --k) {
			for (int j = k - 1; j >= 0; --j) {
				if (perm[j] <= perm[k]) {
					perm[k]++;
				}
			}
		}

		// Loop path
		perm[n] = perm[0];

		// Perm cost
		int cost = 0;

		// Calc perm cost
		for (int i = 0; i < n; i++) {
			cost += dist[perm[i] * n + perm[i + 1]];
		}

		return cost;
	} else {
		return INT_MAX;
	}
}

__global__ void perm_cuda(int n, ll nb_perm, int * dist, ll * fact, int * mcost) {
	// Thread index
	int tidx = threadIdx.x;

	// Block index
	int bidx = blockIdx.x;

	// Global index
	ll idx = blockIdx.x * blockDim.x + threadIdx.x;

	// Shared dist matrix
	__shared__ int s_dist[MAX_NB_CITIES*MAX_NB_CITIES];

	// Copy global dist matrix to shared dist matrix
	int step = (n*n + blockDim.x - 1)/(blockDim.x);
	int start = tidx*step;
	for (int i = 0; i < step; i++) {
		if ((start + i) < (n*n)) {
			s_dist[start + i] = dist[start + i];
		}
	}

	__syncthreads();

	// Shared factorial
	__shared__ ll s_fact[MAX_NB_CITIES];

	// Copy global factorial to shared factorial
	if (tidx < n) {
		s_fact[tidx] = fact[tidx];
	}
	__syncthreads();

	// Shared cost
	__shared__ int s_mcost[MAX_THREAD_PER_BLOCK];

	// Minimal local cost
	s_mcost[tidx] = calc_perm_cost(idx, n, nb_perm, s_dist, s_fact);
	__syncthreads();

	// Reduce local cost to find global
	ll step_size = 1;
	int nb_threads = blockDim.x/2;

	while (nb_threads > 0) {
		if (tidx < nb_threads) {
			int fst = tidx * step_size * 2;
			int snd = fst  + step_size;

			if (s_mcost[snd] < s_mcost[fst]) {
				s_mcost[fst] = s_mcost[snd];
			}
		}

		step_size <<= 1;
		nb_threads >>= 1;

		__syncthreads();
	}

	// Put mcost from block in global
	if (tidx == 0) {
		mcost[bidx] = s_mcost[0];
	}
}

void printDeviceProps() {
	hipSetDevice(0);
	hipDeviceProp_t prop;
	hipGetDeviceProperties(&prop, 0);

	printf("prop.name                        %s\n",       prop.name);
	printf("prop.totalGlobalMem              %zdGB\n",    prop.totalGlobalMem >> 10 >> 10 >> 10);
	printf("prop.sharedMemPerBlock           %zdKB\n",    prop.sharedMemPerBlock >> 10);
	printf("prop.regsPerBlock                %d\n",       prop.regsPerBlock);
	printf("prop.warpSize                    %d\n",       prop.warpSize);
	printf("prop.memPitch                    %zdGB\n",    prop.memPitch >> 10 >> 10 >> 10);
	printf("prop.maxThreadsPerBlock          %d\n",       prop.maxThreadsPerBlock);
	printf("prop.maxThreadsDim               %d %d %d\n", prop.maxThreadsDim[0], prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
	printf("prop.maxGridSize                 %d %d %d\n", prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2]);
	printf("prop.totalConstMem               %zdKB\n",    prop.totalConstMem >> 10);
	printf("prop.multiProcessorCount         %d\n",       prop.multiProcessorCount);
	printf("prop.l2CacheSize                 %dMB\n",     prop.l2CacheSize >> 10 >> 10);
	printf("prop.maxThreadsPerMultiProcessor %d\n",       prop.maxThreadsPerMultiProcessor);
	printf("prop.sharedMemPerMultiprocessor  %zdKB\n",    prop.sharedMemPerMultiprocessor >> 10);
	printf("prop.regsPerMultiprocessor       %d\n",       prop.regsPerMultiprocessor);
	printf("\n");
}

int * putDMatrixInDevice(int n) {
	// X coords
	int * x = (int *)malloc(n*sizeof(int));
	if (!x) {
		printf("malloc error\n");
		exit(1);
	}

	// Y coords
	int * y = (int *)malloc(n*sizeof(int));
	if (!y) {
		printf("malloc error\n");
		exit(1);
	}

	// Read
	for (int i = 0; i < n; i++) {
		scanf("%d %d", &x[i], &y[i]);
	}

	// Dist matrix on host
	int * h_dist = (int *)malloc(n*n*sizeof(int));
	if (!h_dist) {
		printf("malloc error\n");
		exit(1);
	}

	// Calc dist matrix
	int dx, dy;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			dx = x[i] - x[j];
			dy = y[i] - y[j];
			h_dist[i*n + j] = (int)sqrt(dx*dx + dy*dy);
		}
	}

	free(x);
	free(y);

	if (DEBUG) {
		// Print dist
		printf("dist\n");
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				printf("%3d ", h_dist[i*n + j]);
			}
			printf("\n");
		}
	}

	// Dist matrix on device
	int * d_dist;
	if (hipMalloc(&d_dist, n*n*sizeof(int)) == hipErrorOutOfMemory) {
		printf("hipMalloc error\n");
		exit(1);
	}

	// Copy host to device
	hipMemcpy(d_dist, h_dist, n*n*sizeof(int), hipMemcpyHostToDevice);

	free(h_dist);

	return d_dist;
}

ll * putFactInDevice(int n) {
	// Fact vector on host
	ll * h_fact = (ll *)malloc(n*sizeof(ll));
	if (!h_fact) {
		printf("malloc error\n");
		exit(1);
	}

	// Calc fact
	h_fact[0] = 1;
	for (int i = 1; i < n; i++) {
		h_fact[i] = i * h_fact[i - 1];
	}

	if (DEBUG) {
		// Print fact
		printf("fact ");
		for (int i = 0; i < n; i++) {
			printf("%lld ", h_fact[i]);
		}
		printf("\n");
	}

	// Fact vector on device
	ll * d_fact;
	if (hipMalloc(&d_fact, n*sizeof(ll)) == hipErrorOutOfMemory) {
		printf("hipMalloc error\n");
		exit(1);
	}

	// Copy host to device
	hipMemcpy(d_fact, h_fact, n*sizeof(ll), hipMemcpyHostToDevice);

	return d_fact;
}

ll factorial(int n) {
	return (n > 1 ? n*factorial(n-1) : 1);
}

int run_tsp() {
	int nb_cities;
	scanf("%d", &nb_cities);
	if (DEBUG) {
		printf("nb_cities  %d\n", nb_cities);
	}

	// Dist matrix
	int * d_dist = putDMatrixInDevice(nb_cities);

	// Fact vector
	ll * d_fact = putFactInDevice(nb_cities);

	// Number of permutations
	ll nb_perm = factorial(nb_cities);
	if (DEBUG) {
		printf("nb_perm    %lld\n", nb_perm);
	}

	// nb_blocks, nb_threads
	int nb_blocks  = (nb_perm + MAX_THREAD_PER_BLOCK - 1)/MAX_THREAD_PER_BLOCK;
	int nb_threads = (nb_perm > MAX_THREAD_PER_BLOCK ? MAX_THREAD_PER_BLOCK : nb_perm);
	if (DEBUG) {
		printf("nb_blocks  %d\n", nb_blocks);
		printf("nb_threads %d\n", nb_threads);
	}

	// Mim cost from blocks
	int * h_mcost = (int *)malloc(nb_blocks*sizeof(int));
	int * d_mcost;
	hipMalloc(&d_mcost, nb_blocks*sizeof(int));

	// Call gpu
	clock_t start = clock();
	perm_cuda<<<nb_blocks, nb_threads>>>(nb_cities, nb_perm, d_dist, d_fact, d_mcost);
	hipDeviceSynchronize();
	clock_t end = clock();

	// Time
	int msec = (end - start) * 1000 / CLOCKS_PER_SEC;
	printf("%ds %dms\n", msec/1000, msec%1000);

	// Copy device to host, ret
	hipMemcpy(h_mcost, d_mcost, nb_blocks*sizeof(int), hipMemcpyDeviceToHost);

	// Error check
	hipError_t err = hipGetLastError();
	if (err != hipSuccess) {
		printf("CUDA Error: %s\n",hipGetErrorString(err));
	}

	// Host global mim
	int global_mcost = INT_MAX;

	// Find global mim
	for (int i = 0; i < nb_blocks; i++) {
		if (h_mcost[i] < global_mcost) {
			global_mcost = h_mcost[i];
		}
	}

	return global_mcost;
}

int main() {
	if (DEBUG) {
		printDeviceProps();
	}

	int num_instances;

	scanf("%d", &num_instances);

	for (int i = 0; i < num_instances; i++) {
		printf("%d\n", run_tsp());
	}

	return 0;
} 
	.text
	.file	"cuda_1_job_per_thread.hip"
	.globl	_Z24__device_stub__perm_cudaixPiPxS_ # -- Begin function _Z24__device_stub__perm_cudaixPiPxS_
	.type	_Z24__device_stub__perm_cudaixPiPxS_,@function
_Z24__device_stub__perm_cudaixPiPxS_:   # @_Z24__device_stub__perm_cudaixPiPxS_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$144, %rsp
	.cfi_def_cfa_offset 192
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	12(%rsp), %rax
	movl	%edi, (%rax)
	leaq	56(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	48(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	40(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	32(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z9perm_cudaixPiPxS_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z24__device_stub__perm_cudaixPiPxS_, .Lfunc_end0-_Z24__device_stub__perm_cudaixPiPxS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z16printDevicePropsv           # -- Begin function _Z16printDevicePropsv
	.type	_Z16printDevicePropsv,@function
_Z16printDevicePropsv:                  # @_Z16printDevicePropsv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$1472, %rsp                     # imm = 0x5C0
	.cfi_def_cfa_offset 1488
	.cfi_offset %rbx, -16
	xorl	%edi, %edi
	callq	hipSetDevice
	movq	%rsp, %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movl	$.L.str, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	288(%rbx), %rsi
	shrq	$30, %rsi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	movq	296(%rbx), %rsi
	shrq	$10, %rsi
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	movl	304(%rbx), %esi
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	movl	308(%rbx), %esi
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	movq	312(%rbx), %rsi
	shrq	$30, %rsi
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	movl	320(%rbx), %esi
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	movl	324(%rbx), %esi
	movl	328(%rbx), %edx
	movl	332(%rbx), %ecx
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	movl	336(%rbx), %esi
	movl	340(%rbx), %edx
	movl	344(%rbx), %ecx
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
	movq	352(%rbx), %rsi
	shrq	$10, %rsi
	movl	$.L.str.9, %edi
	xorl	%eax, %eax
	callq	printf
	movl	388(%rbx), %esi
	movl	$.L.str.10, %edi
	xorl	%eax, %eax
	callq	printf
	movl	616(%rbx), %esi
	sarl	$20, %esi
	movl	$.L.str.11, %edi
	xorl	%eax, %eax
	callq	printf
	movl	624(%rbx), %esi
	movl	$.L.str.12, %edi
	xorl	%eax, %eax
	callq	printf
	movq	640(%rbx), %rsi
	shrq	$10, %rsi
	movl	$.L.str.13, %edi
	xorl	%eax, %eax
	callq	printf
	movl	648(%rbx), %esi
	movl	$.L.str.14, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$10, %edi
	callq	putchar@PLT
	addq	$1472, %rsp                     # imm = 0x5C0
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z16printDevicePropsv, .Lfunc_end1-_Z16printDevicePropsv
	.cfi_endproc
                                        # -- End function
	.globl	_Z18putDMatrixInDevicei         # -- Begin function _Z18putDMatrixInDevicei
	.type	_Z18putDMatrixInDevicei,@function
_Z18putDMatrixInDevicei:                # @_Z18putDMatrixInDevicei
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%edi, 4(%rsp)                   # 4-byte Spill
	movslq	%edi, %r14
	shlq	$2, %r14
	movq	%r14, %rdi
	callq	malloc
	testq	%rax, %rax
	je	.LBB2_1
# %bb.3:
	movq	%rax, %r12
	movq	%r14, %rdi
	callq	malloc
	testq	%rax, %rax
	je	.LBB2_1
# %bb.4:                                # %.preheader69
	movq	%rax, %r13
	movl	4(%rsp), %eax                   # 4-byte Reload
	movl	%eax, %ebp
	testl	%eax, %eax
	jle	.LBB2_7
# %bb.5:                                # %.lr.ph.preheader
	movq	%rbp, %rbx
	movq	%r12, %r14
	movq	%r13, %r15
.LBB2_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.17, %edi
	movq	%r14, %rsi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	__isoc23_scanf
	addq	$4, %r15
	addq	$4, %r14
	decq	%rbx
	jne	.LBB2_6
.LBB2_7:                                # %._crit_edge
	movl	4(%rsp), %ebx                   # 4-byte Reload
	movl	%ebx, %r14d
	imull	%r14d, %r14d
	shlq	$2, %r14
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	testq	%rax, %rax
	je	.LBB2_1
# %bb.8:                                # %.preheader68
	movq	%r14, 24(%rsp)                  # 8-byte Spill
	testl	%ebx, %ebx
	jle	.LBB2_21
# %bb.9:                                # %.preheader67.lr.ph
	xorl	%eax, %eax
	xorpd	%xmm1, %xmm1
	xorl	%ebx, %ebx
.LBB2_10:                               # %.lr.ph72
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_11 Depth 2
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movl	%eax, %eax
	movq	8(%rsp), %rcx                   # 8-byte Reload
	leaq	(%rcx,%rax,4), %r15
	xorl	%r14d, %r14d
.LBB2_11:                               #   Parent Loop BB2_10 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%r12,%rbx,4), %eax
	subl	(%r12,%r14,4), %eax
	movl	(%r13,%rbx,4), %ecx
	subl	(%r13,%r14,4), %ecx
	imull	%eax, %eax
	imull	%ecx, %ecx
	addl	%eax, %ecx
	xorps	%xmm0, %xmm0
	cvtsi2sd	%ecx, %xmm0
	ucomisd	%xmm1, %xmm0
	jb	.LBB2_18
# %bb.12:                               #   in Loop: Header=BB2_11 Depth=2
	sqrtsd	%xmm0, %xmm0
	jmp	.LBB2_19
.LBB2_18:                               # %call.sqrt
                                        #   in Loop: Header=BB2_11 Depth=2
	callq	sqrt
	xorpd	%xmm1, %xmm1
.LBB2_19:                               # %.split
                                        #   in Loop: Header=BB2_11 Depth=2
	cvttsd2si	%xmm0, %eax
	movl	%eax, (%r15,%r14,4)
	incq	%r14
	cmpq	%r14, %rbp
	jne	.LBB2_11
# %bb.20:                               # %._crit_edge73
                                        #   in Loop: Header=BB2_10 Depth=1
	incq	%rbx
	movq	32(%rsp), %rax                  # 8-byte Reload
	addl	4(%rsp), %eax                   # 4-byte Folded Reload
	cmpq	%rbp, %rbx
	jne	.LBB2_10
# %bb.13:                               # %._crit_edge75
	movq	%r12, %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	4(%rsp), %r13d                  # 4-byte Reload
	testl	%r13d, %r13d
	jle	.LBB2_22
# %bb.14:                               # %.preheader.lr.ph
	xorl	%ebx, %ebx
	xorl	%r14d, %r14d
.LBB2_15:                               # %.lr.ph77
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_16 Depth 2
	movl	%ebx, %eax
	movq	8(%rsp), %rcx                   # 8-byte Reload
	leaq	(%rcx,%rax,4), %r15
	xorl	%r12d, %r12d
.LBB2_16:                               #   Parent Loop BB2_15 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%r15,%r12,4), %esi
	movl	$.L.str.19, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r12
	cmpq	%r12, %rbp
	jne	.LBB2_16
# %bb.17:                               # %._crit_edge78
                                        #   in Loop: Header=BB2_15 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r14
	addl	%r13d, %ebx
	cmpq	%rbp, %r14
	jne	.LBB2_15
	jmp	.LBB2_22
.LBB2_21:                               # %._crit_edge80.critedge
	movq	%r12, %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
	movl	$.Lstr.3, %edi
	callq	puts@PLT
.LBB2_22:                               # %._crit_edge80
	leaq	16(%rsp), %rdi
	movq	24(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rsi
	callq	hipMalloc
	cmpl	$2, %eax
	je	.LBB2_23
# %bb.24:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %r14                   # 8-byte Reload
	movq	%r14, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%r14, %rdi
	callq	free
	movq	16(%rsp), %rax
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB2_1:
	.cfi_def_cfa_offset 96
	movl	$.Lstr.5, %edi
	jmp	.LBB2_2
.LBB2_23:
	movl	$.Lstr.6, %edi
.LBB2_2:
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z18putDMatrixInDevicei, .Lfunc_end2-_Z18putDMatrixInDevicei
	.cfi_endproc
                                        # -- End function
	.globl	_Z15putFactInDevicei            # -- Begin function _Z15putFactInDevicei
	.type	_Z15putFactInDevicei,@function
_Z15putFactInDevicei:                   # @_Z15putFactInDevicei
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%edi, %ebp
	movslq	%edi, %rbx
	shlq	$3, %rbx
	movq	%rbx, %rdi
	callq	malloc
	testq	%rax, %rax
	je	.LBB3_1
# %bb.3:
	movq	%rax, %r14
	movq	$1, (%rax)
	movl	%ebp, %r15d
	cmpl	$2, %ebp
	jl	.LBB3_6
# %bb.4:                                # %.lr.ph.preheader
	movq	(%r14), %rax
	movl	$1, %ecx
.LBB3_5:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	imulq	%rcx, %rax
	movq	%rax, (%r14,%rcx,8)
	incq	%rcx
	cmpq	%rcx, %r15
	jne	.LBB3_5
.LBB3_6:                                # %._crit_edge
	movl	$.L.str.21, %edi
	xorl	%eax, %eax
	callq	printf
	testl	%ebp, %ebp
	jle	.LBB3_9
# %bb.7:                                # %.lr.ph25.preheader
	xorl	%r12d, %r12d
.LBB3_8:                                # %.lr.ph25
                                        # =>This Inner Loop Header: Depth=1
	movq	(%r14,%r12,8), %rsi
	movl	$.L.str.22, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB3_8
.LBB3_9:                                # %._crit_edge26
	movl	$10, %edi
	callq	putchar@PLT
	leaq	8(%rsp), %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	cmpl	$2, %eax
	je	.LBB3_10
# %bb.11:
	movq	8(%rsp), %rdi
	movq	%r14, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rax
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB3_1:
	.cfi_def_cfa_offset 64
	movl	$.Lstr.5, %edi
	jmp	.LBB3_2
.LBB3_10:
	movl	$.Lstr.6, %edi
.LBB3_2:
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end3:
	.size	_Z15putFactInDevicei, .Lfunc_end3-_Z15putFactInDevicei
	.cfi_endproc
                                        # -- End function
	.globl	_Z9factoriali                   # -- Begin function _Z9factoriali
	.type	_Z9factoriali,@function
_Z9factoriali:                          # @_Z9factoriali
	.cfi_startproc
# %bb.0:
	movl	$1, %eax
	cmpl	$2, %edi
	jl	.LBB4_3
# %bb.1:                                # %tailrecurse.preheader
	movl	%edi, %ecx
.LBB4_2:                                # %tailrecurse
                                        # =>This Inner Loop Header: Depth=1
	imulq	%rcx, %rax
	leaq	-1(%rcx), %rdx
	cmpq	$2, %rcx
	movq	%rdx, %rcx
	ja	.LBB4_2
.LBB4_3:                                # %tailrecurse._crit_edge
	retq
.Lfunc_end4:
	.size	_Z9factoriali, .Lfunc_end4-_Z9factoriali
	.cfi_endproc
                                        # -- End function
	.globl	_Z7run_tspv                     # -- Begin function _Z7run_tspv
	.type	_Z7run_tspv,@function
_Z7run_tspv:                            # @_Z7run_tspv
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	leaq	4(%rsp), %rbx
	movl	$.L.str.23, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	(%rbx), %esi
	movl	$.L.str.24, %edi
	xorl	%eax, %eax
	callq	printf
	movl	(%rbx), %edi
	callq	_Z18putDMatrixInDevicei
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	movl	(%rbx), %edi
	callq	_Z15putFactInDevicei
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	(%rbx), %eax
	movl	$1, %r13d
	cmpl	$2, %eax
	jl	.LBB5_2
.LBB5_1:                                # %tailrecurse.i
                                        # =>This Inner Loop Header: Depth=1
	imulq	%rax, %r13
	leaq	-1(%rax), %rcx
	cmpq	$2, %rax
	movq	%rcx, %rax
	ja	.LBB5_1
.LBB5_2:                                # %_Z9factoriali.exit
	movl	$.L.str.25, %edi
	movq	%r13, %rsi
	xorl	%eax, %eax
	callq	printf
	leaq	1023(%r13), %rax
	leaq	2046(%r13), %rbx
	testq	%rax, %rax
	cmovnsq	%rax, %rbx
	sarq	$10, %rbx
	movl	$1024, %r12d                    # imm = 0x400
	cmpq	%r12, %r13
	cmovlq	%r13, %r12
	movl	$.L.str.26, %edi
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.27, %edi
	movl	%r12d, %esi
	xorl	%eax, %eax
	callq	printf
	movslq	%ebx, %r15
	leaq	(,%r15,4), %rbp
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, %r14
	leaq	8(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	callq	clock
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movl	%r15d, %edi
	btsq	$32, %rdi
	movl	%r12d, %edx
	btsq	$32, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB5_4
# %bb.3:
	movl	4(%rsp), %edi
	movq	8(%rsp), %r8
	movq	%r13, %rsi
	movq	24(%rsp), %rdx                  # 8-byte Reload
	movq	16(%rsp), %rcx                  # 8-byte Reload
	callq	_Z24__device_stub__perm_cudaixPiPxS_
.LBB5_4:
	callq	hipDeviceSynchronize
	callq	clock
	subq	32(%rsp), %rax                  # 8-byte Folded Reload
	movabsq	$2361183241434822607, %rcx      # imm = 0x20C49BA5E353F7CF
	imulq	%rcx
	movq	%rdx, %rax
	shrq	$63, %rax
	sarq	$7, %rdx
	addq	%rax, %rdx
	movslq	%edx, %rdx
	imulq	$274877907, %rdx, %rsi          # imm = 0x10624DD3
	movq	%rsi, %rax
	shrq	$63, %rax
	sarq	$38, %rsi
	addl	%eax, %esi
	imull	$1000, %esi, %eax               # imm = 0x3E8
	subl	%eax, %edx
	movl	$.L.str.28, %edi
                                        # kill: def $esi killed $esi killed $rsi
                                        # kill: def $edx killed $edx killed $rdx
	xorl	%eax, %eax
	callq	printf
	movq	8(%rsp), %rsi
	movq	%r14, %rdi
	movq	%rbp, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	callq	hipGetLastError
	testl	%eax, %eax
	je	.LBB5_6
# %bb.5:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.29, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
.LBB5_6:
	testl	%ebx, %ebx
	jle	.LBB5_7
# %bb.9:                                # %.lr.ph.preheader
	movl	%ebx, %ecx
	movl	$2147483647, %eax               # imm = 0x7FFFFFFF
	xorl	%edx, %edx
.LBB5_10:                               # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r14,%rdx,4), %esi
	cmpl	%eax, %esi
	cmovll	%esi, %eax
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB5_10
	jmp	.LBB5_8
.LBB5_7:
	movl	$2147483647, %eax               # imm = 0x7FFFFFFF
.LBB5_8:                                # %._crit_edge
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	_Z7run_tspv, .Lfunc_end5-_Z7run_tspv
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	callq	_Z16printDevicePropsv
	leaq	12(%rsp), %rbx
	movl	$.L.str.23, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	cmpl	$0, (%rbx)
	jle	.LBB6_3
# %bb.1:                                # %.lr.ph.preheader
	xorl	%ebx, %ebx
.LBB6_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	_Z7run_tspv
	movl	$.L.str.30, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
	incl	%ebx
	cmpl	12(%rsp), %ebx
	jl	.LBB6_2
.LBB6_3:                                # %._crit_edge
	xorl	%eax, %eax
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end6:
	.size	main, .Lfunc_end6-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB7_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB7_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z9perm_cudaixPiPxS_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end7:
	.size	__hip_module_ctor, .Lfunc_end7-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB8_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB8_2:
	retq
.Lfunc_end8:
	.size	__hip_module_dtor, .Lfunc_end8-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z9perm_cudaixPiPxS_,@object    # @_Z9perm_cudaixPiPxS_
	.section	.rodata,"a",@progbits
	.globl	_Z9perm_cudaixPiPxS_
	.p2align	3, 0x0
_Z9perm_cudaixPiPxS_:
	.quad	_Z24__device_stub__perm_cudaixPiPxS_
	.size	_Z9perm_cudaixPiPxS_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"prop.name                        %s\n"
	.size	.L.str, 37

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"prop.totalGlobalMem              %zdGB\n"
	.size	.L.str.1, 40

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"prop.sharedMemPerBlock           %zdKB\n"
	.size	.L.str.2, 40

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"prop.regsPerBlock                %d\n"
	.size	.L.str.3, 37

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"prop.warpSize                    %d\n"
	.size	.L.str.4, 37

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"prop.memPitch                    %zdGB\n"
	.size	.L.str.5, 40

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"prop.maxThreadsPerBlock          %d\n"
	.size	.L.str.6, 37

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"prop.maxThreadsDim               %d %d %d\n"
	.size	.L.str.7, 43

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"prop.maxGridSize                 %d %d %d\n"
	.size	.L.str.8, 43

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"prop.totalConstMem               %zdKB\n"
	.size	.L.str.9, 40

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"prop.multiProcessorCount         %d\n"
	.size	.L.str.10, 37

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"prop.l2CacheSize                 %dMB\n"
	.size	.L.str.11, 39

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"prop.maxThreadsPerMultiProcessor %d\n"
	.size	.L.str.12, 37

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"prop.sharedMemPerMultiprocessor  %zdKB\n"
	.size	.L.str.13, 40

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"prop.regsPerMultiprocessor       %d\n"
	.size	.L.str.14, 37

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"%d %d"
	.size	.L.str.17, 6

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"%3d "
	.size	.L.str.19, 5

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	"fact "
	.size	.L.str.21, 6

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"%lld "
	.size	.L.str.22, 6

	.type	.L.str.23,@object               # @.str.23
.L.str.23:
	.asciz	"%d"
	.size	.L.str.23, 3

	.type	.L.str.24,@object               # @.str.24
.L.str.24:
	.asciz	"nb_cities  %d\n"
	.size	.L.str.24, 15

	.type	.L.str.25,@object               # @.str.25
.L.str.25:
	.asciz	"nb_perm    %lld\n"
	.size	.L.str.25, 17

	.type	.L.str.26,@object               # @.str.26
.L.str.26:
	.asciz	"nb_blocks  %d\n"
	.size	.L.str.26, 15

	.type	.L.str.27,@object               # @.str.27
.L.str.27:
	.asciz	"nb_threads %d\n"
	.size	.L.str.27, 15

	.type	.L.str.28,@object               # @.str.28
.L.str.28:
	.asciz	"%ds %dms\n"
	.size	.L.str.28, 10

	.type	.L.str.29,@object               # @.str.29
.L.str.29:
	.asciz	"CUDA Error: %s\n"
	.size	.L.str.29, 16

	.type	.L.str.30,@object               # @.str.30
.L.str.30:
	.asciz	"%d\n"
	.size	.L.str.30, 4

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z9perm_cudaixPiPxS_"
	.size	.L__unnamed_1, 21

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr.3,@object                 # @str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr.3:
	.asciz	"dist"
	.size	.Lstr.3, 5

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"malloc error"
	.size	.Lstr.5, 13

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"hipMalloc error"
	.size	.Lstr.6, 16

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z24__device_stub__perm_cudaixPiPxS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z9perm_cudaixPiPxS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z9perm_cudaixPiPxS_    ; -- Begin function _Z9perm_cudaixPiPxS_
	.globl	_Z9perm_cudaixPiPxS_
	.p2align	8
	.type	_Z9perm_cudaixPiPxS_,@function
_Z9perm_cudaixPiPxS_:                   ; @_Z9perm_cudaixPiPxS_
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x34
	s_load_b32 s10, s[0:1], 0x0
	s_mov_b32 s8, s15
	s_waitcnt lgkmcnt(0)
	s_and_b32 s9, s2, 0xffff
	s_mul_i32 s11, s10, s10
	v_cvt_f32_u32_e32 v1, s9
	s_sub_i32 s3, 0, s9
	s_add_i32 s4, s11, s9
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_add_i32 s12, s4, -1
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_u32_f32_e32 v1, v1
	v_readfirstlane_b32 s2, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_i32 s3, s3, s2
	s_mul_hi_u32 s3, s2, s3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	s_add_i32 s13, s2, s3
	s_load_b256 s[0:7], s[0:1], 0x8
	s_mul_hi_u32 s13, s12, s13
	s_mul_i32 s14, s13, s9
	s_delay_alu instid0(SALU_CYCLE_1)
	s_sub_i32 s12, s12, s14
	s_add_i32 s14, s13, 1
	s_sub_i32 s15, s12, s9
	s_cmp_ge_u32 s12, s9
	s_cselect_b32 s13, s14, s13
	s_cselect_b32 s12, s15, s12
	s_add_i32 s14, s13, 1
	s_cmp_ge_u32 s12, s9
	s_cselect_b32 s12, s14, s13
	s_mov_b32 s13, 0
	s_cmp_lt_i32 s12, 1
	s_cbranch_scc1 .LBB0_5
; %bb.1:                                ; %.lr.ph.preheader
	v_mul_lo_u32 v1, s12, v0
	v_mov_b32_e32 v2, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[2:3], 2, v[1:2]
	v_lshlrev_b32_e32 v4, 2, v1
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s2, v2
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v3, vcc_lo, s3, v3, vcc_lo
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	v_add_nc_u32_e32 v5, s13, v1
	s_mov_b32 s2, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s11, v5
	s_cbranch_execz .LBB0_4
; %bb.3:                                ;   in Loop: Header=BB0_2 Depth=1
	global_load_b32 v5, v[2:3], off
	s_waitcnt vmcnt(0)
	ds_store_b32 v4, v5
.LBB0_4:                                ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s2
	v_add_co_u32 v2, vcc_lo, v2, 4
	v_add_co_ci_u32_e32 v3, vcc_lo, 0, v3, vcc_lo
	v_add_nc_u32_e32 v4, 4, v4
	s_add_i32 s13, s13, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_ge_i32 s13, s12
	s_cbranch_scc0 .LBB0_2
.LBB0_5:                                ; %._crit_edge
	s_waitcnt lgkmcnt(0)
	s_mov_b32 s2, exec_lo
	s_barrier
	buffer_gl0_inv
	v_cmpx_gt_i32_e64 s10, v0
	s_cbranch_execz .LBB0_7
; %bb.6:
	v_lshlrev_b32_e32 v3, 3, v0
	global_load_b64 v[1:2], v3, s[4:5]
	s_waitcnt vmcnt(0)
	ds_store_b64 v3, v[1:2] offset:14096
.LBB0_7:
	s_or_b32 exec_lo, exec_lo, s2
	v_mad_u64_u32 v[1:2], null, s8, s9, v[0:1]
	v_mov_b32_e32 v2, 0
	v_bfrev_b32_e32 v3, -2
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_cmp_gt_i64_e32 vcc_lo, s[0:1], v[1:2]
	s_and_saveexec_b32 s1, vcc_lo
	s_cbranch_execz .LBB0_22
; %bb.8:
	s_cmp_gt_i32 s10, 0
	s_cselect_b32 s2, -1, 0
	s_cmp_lt_i32 s10, 1
	s_cbranch_scc1 .LBB0_11
; %bb.9:                                ; %.lr.ph.i.preheader
	v_mov_b32_e32 v3, 16
	s_lshl_b32 s0, s10, 3
	s_mov_b32 s4, s10
	s_add_i32 s3, s0, 0x3708
.LBB0_10:                               ; %.lr.ph.i
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mov_b32_e32 v4, s3
	v_ashrrev_i32_e32 v6, 31, v2
	s_add_i32 s4, s4, -1
	s_add_i32 s3, s3, -8
	s_cmp_eq_u32 s4, 0
	ds_load_b64 v[4:5], v4
	v_add_co_u32 v7, vcc_lo, v1, v6
	v_add_co_ci_u32_e32 v8, vcc_lo, v2, v6, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_xor_b32_e32 v13, v7, v6
	v_xor_b32_e32 v12, v8, v6
	s_waitcnt lgkmcnt(0)
	v_ashrrev_i32_e32 v7, 31, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_add_co_u32 v8, vcc_lo, v4, v7
	v_add_co_ci_u32_e32 v9, vcc_lo, v5, v7, vcc_lo
	v_xor_b32_e32 v14, v6, v7
	v_xor_b32_e32 v15, v8, v7
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_xor_b32_e32 v16, v9, v7
	v_cvt_f32_u32_e32 v6, v15
	v_sub_co_u32 v8, vcc_lo, 0, v15
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cvt_f32_u32_e32 v7, v16
	v_sub_co_ci_u32_e32 v9, vcc_lo, 0, v16, vcc_lo
	v_fmac_f32_e32 v6, 0x4f800000, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v6, v6
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v6, 0x5f7ffffc, v6
	v_mul_f32_e32 v7, 0x2f800000, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_trunc_f32_e32 v7, v7
	v_fmac_f32_e32 v6, 0xcf800000, v7
	v_cvt_u32_f32_e32 v7, v7
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cvt_u32_f32_e32 v6, v6
	v_mul_lo_u32 v10, v8, v7
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_mul_hi_u32 v11, v8, v6
	v_mul_lo_u32 v18, v9, v6
	v_mul_lo_u32 v17, v8, v6
	v_add_nc_u32_e32 v10, v11, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_4)
	v_mul_hi_u32 v11, v6, v17
	v_mul_hi_u32 v19, v7, v17
	v_mul_lo_u32 v17, v7, v17
	v_add_nc_u32_e32 v10, v10, v18
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_4)
	v_mul_lo_u32 v18, v6, v10
	v_mul_hi_u32 v20, v6, v10
	v_mul_hi_u32 v21, v7, v10
	v_mul_lo_u32 v10, v7, v10
	v_add_co_u32 v11, vcc_lo, v11, v18
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v18, vcc_lo, 0, v20, vcc_lo
	v_add_co_u32 v11, vcc_lo, v11, v17
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v11, vcc_lo, v18, v19, vcc_lo
	v_add_co_ci_u32_e32 v17, vcc_lo, 0, v21, vcc_lo
	v_add_co_u32 v10, vcc_lo, v11, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v11, vcc_lo, 0, v17, vcc_lo
	v_add_co_u32 v6, vcc_lo, v6, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v7, vcc_lo, v7, v11, vcc_lo
	v_mul_lo_u32 v10, v8, v6
	v_mul_hi_u32 v11, v8, v6
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_mul_lo_u32 v8, v8, v7
	v_mul_lo_u32 v9, v9, v6
	v_mul_hi_u32 v17, v7, v10
	v_mul_lo_u32 v18, v7, v10
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v8, v11, v8
	v_mul_hi_u32 v10, v6, v10
	v_add_nc_u32_e32 v8, v8, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_4)
	v_mul_lo_u32 v9, v6, v8
	v_mul_hi_u32 v11, v6, v8
	v_mul_hi_u32 v19, v7, v8
	v_mul_lo_u32 v8, v7, v8
	v_add_co_u32 v9, vcc_lo, v10, v9
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v10, vcc_lo, 0, v11, vcc_lo
	v_add_co_u32 v9, vcc_lo, v9, v18
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v9, vcc_lo, v10, v17, vcc_lo
	v_add_co_ci_u32_e32 v10, vcc_lo, 0, v19, vcc_lo
	v_add_co_u32 v8, vcc_lo, v9, v8
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v9, vcc_lo, 0, v10, vcc_lo
	v_add_co_u32 v8, vcc_lo, v6, v8
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v17, vcc_lo, v7, v9, vcc_lo
	v_mul_hi_u32 v18, v13, v8
	v_mad_u64_u32 v[6:7], null, v12, v8, 0
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[8:9], null, v13, v17, 0
	v_mad_u64_u32 v[10:11], null, v12, v17, 0
	v_add_co_u32 v8, vcc_lo, v18, v8
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v9, vcc_lo, 0, v9, vcc_lo
	v_add_co_u32 v6, vcc_lo, v8, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v6, vcc_lo, v9, v7, vcc_lo
	v_add_co_ci_u32_e32 v7, vcc_lo, 0, v11, vcc_lo
	v_add_co_u32 v8, vcc_lo, v6, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v9, vcc_lo, 0, v7, vcc_lo
	v_mul_lo_u32 v10, v16, v8
	v_mad_u64_u32 v[6:7], null, v15, v8, 0
	s_delay_alu instid0(VALU_DEP_3)
	v_mul_lo_u32 v18, v15, v9
	v_add_co_u32 v11, vcc_lo, v8, 1
	v_add_co_ci_u32_e32 v20, vcc_lo, 0, v9, vcc_lo
	v_add_co_u32 v17, s0, v8, 2
	v_sub_co_u32 v6, vcc_lo, v13, v6
	v_add3_u32 v7, v7, v18, v10
	v_add_co_ci_u32_e64 v19, s0, 0, v9, s0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_nc_u32_e32 v10, v12, v7
	v_sub_co_ci_u32_e64 v10, s0, v10, v16, vcc_lo
	v_sub_co_ci_u32_e32 v7, vcc_lo, v12, v7, vcc_lo
	v_cmp_ge_u32_e32 vcc_lo, v6, v15
	v_sub_co_u32 v13, s0, v6, v15
	v_cndmask_b32_e64 v6, 0, -1, vcc_lo
	v_subrev_co_ci_u32_e64 v10, vcc_lo, 0, v10, s0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_ge_u32_e32 vcc_lo, v13, v15
	v_cmp_ge_u32_e64 s0, v10, v16
	v_cndmask_b32_e64 v12, 0, -1, vcc_lo
	v_cmp_ge_u32_e32 vcc_lo, v7, v16
	v_cndmask_b32_e64 v13, 0, -1, vcc_lo
	v_cmp_eq_u32_e32 vcc_lo, v7, v16
	v_cndmask_b32_e64 v7, 0, -1, s0
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cndmask_b32_e32 v6, v13, v6, vcc_lo
	v_cmp_eq_u32_e32 vcc_lo, v10, v16
	v_cmp_ne_u32_e64 s0, 0, v6
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v7, v7, v12, vcc_lo
	v_cmp_ne_u32_e32 vcc_lo, 0, v7
	v_dual_cndmask_b32 v6, v11, v17 :: v_dual_cndmask_b32 v7, v20, v19
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cndmask_b32_e64 v6, v8, v6, s0
	v_cndmask_b32_e64 v7, v9, v7, s0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_xor_b32_e32 v6, v6, v14
	v_xor_b32_e32 v7, v7, v14
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_sub_co_u32 v8, vcc_lo, v6, v14
	v_sub_co_ci_u32_e32 v6, vcc_lo, v7, v14, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v9, v8, v5
	scratch_store_b32 v3, v8, off
	v_add_nc_u32_e32 v3, 4, v3
	v_mul_lo_u32 v7, v6, v4
	v_mad_u64_u32 v[5:6], null, v8, v4, 0
	v_add3_u32 v4, v6, v9, v7
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_sub_co_u32 v1, vcc_lo, v1, v5
	v_sub_co_ci_u32_e32 v2, vcc_lo, v2, v4, vcc_lo
	s_cbranch_scc0 .LBB0_10
.LBB0_11:                               ; %.preheader.i
	s_cmp_lt_i32 s10, 2
	s_cbranch_scc1 .LBB0_18
; %bb.12:                               ; %.lr.ph49.i.preheader
	s_lshl_b32 s0, s10, 2
	v_add_nc_u32_e64 v1, 16, -4
	s_add_i32 s0, s0, 16
	s_mov_b32 s3, s10
	s_add_i32 s0, s0, -8
.LBB0_13:                               ; %.lr.ph49.i
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_14 Depth 2
	s_lshl_b32 s4, s3, 2
	s_mov_b32 s5, 1
	v_add_nc_u32_e32 v2, s4, v1
	s_mov_b32 s4, s0
	scratch_load_b32 v3, v2, off
.LBB0_14:                               ;   Parent Loop BB0_13 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	scratch_load_b32 v4, off, s4
	s_mov_b32 s11, exec_lo
	s_waitcnt vmcnt(0)
	v_cmpx_le_i32_e64 v4, v3
	s_cbranch_execz .LBB0_16
; %bb.15:                               ;   in Loop: Header=BB0_14 Depth=2
	v_add_nc_u32_e32 v3, 1, v3
	scratch_store_b32 v2, v3, off
.LBB0_16:                               ;   in Loop: Header=BB0_14 Depth=2
	s_or_b32 exec_lo, exec_lo, s11
	s_add_i32 s5, s5, 1
	s_add_i32 s4, s4, -4
	s_cmp_eq_u32 s3, s5
	s_cbranch_scc0 .LBB0_14
; %bb.17:                               ; %.loopexit.i
                                        ;   in Loop: Header=BB0_13 Depth=1
	s_add_i32 s4, s3, -1
	s_add_i32 s0, s0, -4
	s_cmp_gt_i32 s3, 2
	s_mov_b32 s3, s4
	s_cbranch_scc1 .LBB0_13
.LBB0_18:                               ; %._crit_edge.i
	scratch_load_b32 v1, off, off offset:16
	s_lshl_b32 s0, s10, 2
	s_and_not1_b32 vcc_lo, exec_lo, s2
	s_add_i32 s0, s0, 16
	s_waitcnt vmcnt(0)
	scratch_store_b32 off, v1, s0
	s_cbranch_vccnz .LBB0_21
; %bb.19:                               ; %.lr.ph53.i.preheader
	v_or_b32_e64 v2, 16, 4
	v_mov_b32_e32 v3, 0
	s_mov_b32 s0, s10
.LBB0_20:                               ; %.lr.ph53.i
                                        ; =>This Inner Loop Header: Depth=1
	v_mul_lo_u32 v4, v1, s10
	scratch_load_b32 v1, v2, off
	v_add_nc_u32_e32 v2, 4, v2
	s_add_i32 s0, s0, -1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	s_cmp_eq_u32 s0, 0
	s_waitcnt vmcnt(0)
	v_lshlrev_b32_e32 v5, 2, v1
	v_lshl_add_u32 v4, v4, 2, v5
	ds_load_b32 v4, v4
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v3, v4, v3
	s_cbranch_scc0 .LBB0_20
	s_branch .LBB0_22
.LBB0_21:
	v_mov_b32_e32 v3, 0
.LBB0_22:                               ; %Flow100
	s_or_b32 exec_lo, exec_lo, s1
	v_lshlrev_b32_e32 v1, 2, v0
	s_cmp_lt_u32 s9, 2
	ds_store_b32 v1, v3 offset:10000
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_28
; %bb.23:                               ; %.lr.ph53
	v_lshlrev_b32_e32 v1, 1, v0
	s_mov_b32 s0, 1
.LBB0_24:                               ; =>This Inner Loop Header: Depth=1
	s_mov_b32 s1, s9
	s_lshr_b32 s9, s9, 1
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_u32_e64 s9, v0
	s_cbranch_execz .LBB0_27
; %bb.25:                               ;   in Loop: Header=BB0_24 Depth=1
	v_mul_lo_u32 v2, v1, s0
	s_delay_alu instid0(VALU_DEP_1)
	v_add_lshl_u32 v4, v2, s0, 2
	v_lshlrev_b32_e32 v3, 2, v2
	ds_load_b32 v2, v4 offset:10000
	ds_load_b32 v4, v3 offset:10000
	s_waitcnt lgkmcnt(0)
	v_cmp_lt_i32_e32 vcc_lo, v2, v4
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_27
; %bb.26:                               ;   in Loop: Header=BB0_24 Depth=1
	v_add_nc_u32_e32 v3, 0x2710, v3
	ds_store_b32 v3, v2
.LBB0_27:                               ;   in Loop: Header=BB0_24 Depth=1
	s_or_b32 exec_lo, exec_lo, s2
	s_lshl_b32 s0, s0, 1
	s_cmp_gt_u32 s1, 3
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_24
.LBB0_28:                               ; %._crit_edge54
	s_mov_b32 s0, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_30
; %bb.29:
	v_mov_b32_e32 v0, 0
	s_ashr_i32 s9, s8, 31
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[0:1], s[8:9], 2
	s_add_u32 s0, s6, s0
	ds_load_b32 v1, v0 offset:10000
	s_addc_u32 s1, s7, s1
	s_waitcnt lgkmcnt(0)
	global_store_b32 v0, v1, s[0:1]
.LBB0_30:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z9perm_cudaixPiPxS_
		.amdhsa_group_segment_fixed_size 14496
		.amdhsa_private_segment_fixed_size 224
		.amdhsa_kernarg_size 296
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 1
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 22
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z9perm_cudaixPiPxS_, .Lfunc_end0-_Z9perm_cudaixPiPxS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1904
; NumSgprs: 18
; NumVgprs: 22
; ScratchSize: 224
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 14496 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 22
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 1
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .offset:         0
        .size:           4
        .value_kind:     by_value
      - .offset:         8
        .size:           8
        .value_kind:     by_value
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         32
        .size:           8
        .value_kind:     global_buffer
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         44
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         52
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         54
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         56
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         58
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         60
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         62
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         104
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 14496
    .kernarg_segment_align: 8
    .kernarg_segment_size: 296
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z9perm_cudaixPiPxS_
    .private_segment_fixed_size: 224
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z9perm_cudaixPiPxS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     22
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
