#include <stdio.h>

struct City {
    int x, y;
    char name;
};

inline void GPUassert(cudaError_t code, char * file, int line, bool Abort=true)
{
    if (code != 0) {
        fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code),file,line);
        if (Abort) exit(code);
    }       
}

#define GPUerrchk(ans) { GPUassert((ans), __FILE__, __LINE__); }

#define NUMCITIES 9
#define CITYSIZE sizeof(struct City)
// #define CITYSIZE 1


__host__ __device__ void printCity(struct City c){
    printf("[x=%i, y=%i]\n", c.x, c.y);
}

__host__ __device__ void swap(struct City *a, int x, int y){
    struct City temp;
    temp = a[x];
    a[x] = a[y];
    a[y] = temp;
}

__device__ double get_distance(struct City c1, struct City c2){
    double x = double(c1.x - c2.x);
    double y = double(c1.y - c2.y);
    return sqrt((x*x) + (y*y));
}

__device__ long total_distance(struct City *path){
    long distance = 0;
    for(int i = 0; i < NUMCITIES - 1; i++){
        distance += get_distance(path[i], path[i+1]);
    }
    return (long)distance;
}

__device__ void shortest_path(struct City *path){
    int best_path_idx = 0;
    for(int i = 0; i < NUMCITIES - 1; i++){
        printCity(path[i]);
    }
}

__device__ void print_path(struct City *path){
    for(int i = 0; i < NUMCITIES; i++){
        printf("%c>", path[i].name);
    }
    printf("\n");
}

__device__ void format_path(struct City *path, char *str){
    for(int i = 0; i < NUMCITIES; i++){
        *str = path[i].name;        
        str++;
        *str = '>';
        str++;
    }
    str--; *str = 0;
}

__device__ void permutations_kernel(struct City *a, char **paths, double *distances, int i, int length, int tid, int *count) { 

    if (length == i){
        long distance = total_distance(a);
        //format_path(a, paths[count[0]]);
        count[0] = count[0] + 1;
    } else {
        for (int j = i; j < length; j++) {
            swap(a, i, j);
            // CUDA
            // permutations(a, i+1, length, tid, count);
            permutations_kernel(a, paths, distances, i+1, length, tid, count);
            swap(a, i, j);
        }
    }
}


__global__ void permute_kernel(struct City *dev_cities, char **paths, double *distances, int size) {

    int tid = threadIdx.x + blockIdx.x * blockDim.x;
    int count[1]; 
    count[0] = 0;

    struct City local_array[NUMCITIES];

    for (int i=0; i<size; i++){
        local_array[i] = dev_cities[i];  
    } 

    //swap(local_array + threadIdx.x, local_array);
    //swap(local_array, threadIdx.x, 0);
    permutations_kernel(local_array, paths, distances, 0, NUMCITIES, tid, count);

}

long factorial(int i) {
    long result = 1;
    while(i > 0) {		
        result *= i;
	i--;
    }
    return result;
}

int main(){

    struct City host_cities[NUMCITIES];
    for(int c = 0; c < NUMCITIES; c++){
        host_cities[c].name = 'A' + c;
        host_cities[c].x = rand() % 20 + 5;
        host_cities[c].y = rand() % 20 + 5;
    }

    //char host_paths [ factorial(NUMCITIES) ][ NUMCITIES*NUMCITIES ];
    char host_paths [0][0]; 
    char **device_paths;

    //double host_distances[ factorial(NUMCITIES) ];
    double host_distances[0];
    double *device_distances;

    float time;
    cudaEvent_t start, stop;
    cudaEventCreate(&start); 
    cudaEventCreate(&stop);

    struct City *device_cities; 
    cudaMalloc((void**) &device_cities, sizeof(host_cities));
    //cudaMalloc((void**) &device_paths, sizeof(host_paths));
    cudaMalloc((void**) &device_paths, sizeof(char) * NUMCITIES * NUMCITIES * factorial(NUMCITIES));
    cudaMalloc((void**) &device_distances, sizeof(host_distances));

    GPUerrchk(cudaMemcpy(device_distances, host_distances, sizeof(host_distances), cudaMemcpyHostToDevice));
    GPUerrchk(cudaMemcpy(device_cities, host_cities, sizeof(host_cities), cudaMemcpyHostToDevice));
    //GPUerrchk(cudaMemcpy(device_paths, host_paths, sizeof(host_paths), cudaMemcpyHostToDevice));
    GPUerrchk(cudaMemcpy(device_paths, host_paths, sizeof(char) * NUMCITIES * NUMCITIES * factorial(NUMCITIES), cudaMemcpyHostToDevice));   

    cudaEventRecord(start,0);
    permute_kernel<<<1, NUMCITIES>>>(device_cities, device_paths, device_distances, NUMCITIES);
    cudaEventRecord(stop,0);

    GPUerrchk(cudaPeekAtLastError());
    GPUerrchk(cudaDeviceSynchronize());

    cudaEventElapsedTime( &time, start, stop );
    printf("\nTiempo de Ejecucion: %f mSeg\n\n", time);

    cudaEventDestroy( start );
    cudaEventDestroy( stop );

    cudaFree(device_cities);

    return 0;
}



	.file	"tmpxft_0035cdca_00000000-6_traveler.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2040:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2040:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata._Z9GPUassert9cudaErrorPcib.str1.1,"aMS",@progbits,1
.LC0:
	.string	"GPUassert: %s %s %d\n"
	.section	.text._Z9GPUassert9cudaErrorPcib,"axG",@progbits,_Z9GPUassert9cudaErrorPcib,comdat
	.weak	_Z9GPUassert9cudaErrorPcib
	.type	_Z9GPUassert9cudaErrorPcib, @function
_Z9GPUassert9cudaErrorPcib:
.LFB2027:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L8
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	%edi, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movl	%edx, 12(%rsp)
	movq	%rsi, (%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r9d
	movq	(%rsp), %r8
	leaq	.LC0(%rip), %rdx
	movq	stderr(%rip), %rdi
	movq	%rax, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	testb	%bpl, %bpl
	je	.L2
	movl	%ebx, %edi
	call	exit@PLT
.L2:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
.L8:
	.cfi_restore 3
	.cfi_restore 6
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9GPUassert9cudaErrorPcib, .-_Z9GPUassert9cudaErrorPcib
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"[x=%i, y=%i]\n"
	.text
	.globl	_Z9printCity4City
	.type	_Z9printCity4City, @function
_Z9printCity4City:
.LFB2028:
	.cfi_startproc
	endbr64
	movq	%rdi, -24(%rsp)
	movl	-20(%rsp), %ecx
	movl	$2, %edi
	xorl	%eax, %eax
	movl	-24(%rsp), %edx
	leaq	.LC1(%rip), %rsi
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z9printCity4City, .-_Z9printCity4City
	.globl	_Z4swapP4Cityii
	.type	_Z4swapP4Cityii, @function
_Z4swapP4Cityii:
.LFB2029:
	.cfi_startproc
	endbr64
	movslq	%esi, %rsi
	movslq	%edx, %rdx
	imulq	$12, %rdx, %rdx
	imulq	$12, %rsi, %rsi
	addq	%rdi, %rsi
	addq	%rdx, %rdi
	movq	(%rdi), %rdx
	movb	8(%rsi), %al
	movq	(%rsi), %rcx
	movq	%rdx, (%rsi)
	movl	8(%rdi), %edx
	movl	%edx, 8(%rsi)
	movq	%rcx, (%rdi)
	movb	%al, 8(%rdi)
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z4swapP4Cityii, .-_Z4swapP4Cityii
	.globl	_Z12get_distance4CityS_
	.type	_Z12get_distance4CityS_, @function
_Z12get_distance4CityS_:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$56, %rsp
	.cfi_def_cfa_offset 64
	movq	%rdi, 16(%rsp)
	movl	$1, 44(%rsp)
	movl	44(%rsp), %edi
	movl	%esi, 24(%rsp)
	movq	%rdx, (%rsp)
	movl	%ecx, 8(%rsp)
	call	exit@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z12get_distance4CityS_, .-_Z12get_distance4CityS_
	.globl	_Z14total_distanceP4City
	.type	_Z14total_distanceP4City, @function
_Z14total_distanceP4City:
.LFB2031:
	.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
.LFE2031:
	.size	_Z14total_distanceP4City, .-_Z14total_distanceP4City
	.globl	_Z13shortest_pathP4City
	.type	_Z13shortest_pathP4City, @function
_Z13shortest_pathP4City:
.LFB2032:
	.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
.LFE2032:
	.size	_Z13shortest_pathP4City, .-_Z13shortest_pathP4City
	.globl	_Z10print_pathP4City
	.type	_Z10print_pathP4City, @function
_Z10print_pathP4City:
.LFB2183:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	_Z13shortest_pathP4City
	.cfi_endproc
.LFE2183:
	.size	_Z10print_pathP4City, .-_Z10print_pathP4City
	.globl	_Z11format_pathP4CityPc
	.type	_Z11format_pathP4CityPc, @function
_Z11format_pathP4CityPc:
.LFB2034:
	.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
.LFE2034:
	.size	_Z11format_pathP4CityPc, .-_Z11format_pathP4CityPc
	.globl	_Z19permutations_kernelP4CityPPcPdiiiPi
	.type	_Z19permutations_kernelP4CityPPcPdiiiPi, @function
_Z19permutations_kernelP4CityPPcPdiiiPi:
.LFB2035:
	.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
.LFE2035:
	.size	_Z19permutations_kernelP4CityPPcPdiiiPi, .-_Z19permutations_kernelP4CityPPcPdiiiPi
	.globl	_Z9factoriali
	.type	_Z9factoriali, @function
_Z9factoriali:
.LFB2036:
	.cfi_startproc
	endbr64
	movslq	%edi, %rdx
	movl	$1, %eax
.L27:
	testl	%edi, %edi
	jle	.L29
	imulq	%rdx, %rax
	decl	%edi
	decq	%rdx
	jmp	.L27
.L29:
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z9factoriali, .-_Z9factoriali
	.globl	_Z44__device_stub__Z14permute_kernelP4CityPPcPdiP4CityPPcPdi
	.type	_Z44__device_stub__Z14permute_kernelP4CityPPcPdiP4CityPPcPdi, @function
_Z44__device_stub__Z14permute_kernelP4CityPPcPdiP4CityPPcPdi:
.LFB2062:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 4(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L30
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z14permute_kernelP4CityPPcPdi(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L30:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
	call	__stack_chk_fail@PLT
.L32:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2062:
	.size	_Z44__device_stub__Z14permute_kernelP4CityPPcPdiP4CityPPcPdi, .-_Z44__device_stub__Z14permute_kernelP4CityPPcPdiP4CityPPcPdi
	.globl	_Z14permute_kernelP4CityPPcPdi
	.type	_Z14permute_kernelP4CityPPcPdi, @function
_Z14permute_kernelP4CityPPcPdi:
.LFB2063:
	.cfi_startproc
	endbr64
	jmp	_Z44__device_stub__Z14permute_kernelP4CityPPcPdiP4CityPPcPdi
	.cfi_endproc
.LFE2063:
	.size	_Z14permute_kernelP4CityPPcPdi, .-_Z14permute_kernelP4CityPPcPdi
	.section	.rodata.str1.1
.LC2:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/alfonsokim/go-traveler/master/traveler.cu"
.LC3:
	.string	"\nTiempo de Ejecucion: %f mSeg\n\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2037:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movl	$20, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movb	$65, %bpl
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$200, %rsp
	.cfi_def_cfa_offset 240
	movq	%fs:40, %rax
	movq	%rax, 184(%rsp)
	xorl	%eax, %eax
	leaq	76(%rsp), %rbx
	movq	%rbx, %r13
.L36:
	movb	%bpl, 8(%rbx)
	incl	%ebp
	addq	$12, %rbx
	call	rand@PLT
	cltd
	idivl	%r12d
	addl	$5, %edx
	movl	%edx, -12(%rbx)
	call	rand@PLT
	cltd
	idivl	%r12d
	addl	$5, %edx
	movl	%edx, -8(%rbx)
	cmpb	$74, %bpl
	jne	.L36
	leaq	24(%rsp), %rdi
	leaq	.LC2(%rip), %rbx
	call	cudaEventCreate@PLT
	leaq	32(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	40(%rsp), %rdi
	movl	$108, %esi
	call	cudaMalloc@PLT
	movl	$9, %edi
	call	_Z9factoriali
	leaq	8(%rsp), %rdi
	imulq	$81, %rax, %rbp
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	xorl	%esi, %esi
	leaq	16(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rdi
	xorl	%edx, %edx
	leaq	72(%rsp), %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$1, %ecx
	movl	$145, %edx
	movq	%rbx, %rsi
	movl	%eax, %edi
	call	_Z9GPUassert9cudaErrorPcib
	movq	40(%rsp), %rdi
	movl	$1, %ecx
	movq	%r13, %rsi
	movl	$108, %edx
	call	cudaMemcpy@PLT
	movl	$1, %ecx
	movl	$146, %edx
	movq	%rbx, %rsi
	movl	%eax, %edi
	call	_Z9GPUassert9cudaErrorPcib
	movq	8(%rsp), %rdi
	leaq	75(%rsp), %rsi
	movq	%rbp, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$1, %ecx
	movl	$148, %edx
	movq	%rbx, %rsi
	movl	%eax, %edi
	call	_Z9GPUassert9cudaErrorPcib
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1, %ecx
	movabsq	$4294967305, %rdx
	movl	$1, %esi
	movabsq	$4294967297, %rdi
	movl	$1, 68(%rsp)
	movq	%rdx, 60(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L37
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rsi
	movl	$9, %ecx
	movq	40(%rsp), %rdi
	call	_Z44__device_stub__Z14permute_kernelP4CityPPcPdiP4CityPPcPdi
.L37:
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	call	cudaPeekAtLastError@PLT
	movl	$1, %ecx
	movl	$154, %edx
	movq	%rbx, %rsi
	movl	%eax, %edi
	call	_Z9GPUassert9cudaErrorPcib
	call	cudaDeviceSynchronize@PLT
	movl	$1, %ecx
	movl	$155, %edx
	movq	%rbx, %rsi
	movl	%eax, %edi
	call	_Z9GPUassert9cudaErrorPcib
	movq	32(%rsp), %rdx
	movq	24(%rsp), %rsi
	leaq	60(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	60(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	24(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	32(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L38
	call	__stack_chk_fail@PLT
.L38:
	addq	$200, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2037:
	.size	main, .-main
	.section	.rodata.str1.1
.LC4:
	.string	"_Z14permute_kernelP4CityPPcPdi"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2065:
	.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	.LC4(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z14permute_kernelP4CityPPcPdi(%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
.LFE2065:
	.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>

struct City {
    int x, y;
    char name;
};

inline void GPUassert(hipError_t code, char * file, int line, bool Abort=true)
{
    if (code != 0) {
        fprintf(stderr, "GPUassert: %s %s %d\n", hipGetErrorString(code),file,line);
        if (Abort) exit(code);
    }       
}

#define GPUerrchk(ans) { GPUassert((ans), __FILE__, __LINE__); }

#define NUMCITIES 9
#define CITYSIZE sizeof(struct City)
// #define CITYSIZE 1


__host__ __device__ void printCity(struct City c){
    printf("[x=%i, y=%i]\n", c.x, c.y);
}

__host__ __device__ void swap(struct City *a, int x, int y){
    struct City temp;
    temp = a[x];
    a[x] = a[y];
    a[y] = temp;
}

__device__ double get_distance(struct City c1, struct City c2){
    double x = double(c1.x - c2.x);
    double y = double(c1.y - c2.y);
    return sqrt((x*x) + (y*y));
}

__device__ long total_distance(struct City *path){
    long distance = 0;
    for(int i = 0; i < NUMCITIES - 1; i++){
        distance += get_distance(path[i], path[i+1]);
    }
    return (long)distance;
}

__device__ void shortest_path(struct City *path){
    int best_path_idx = 0;
    for(int i = 0; i < NUMCITIES - 1; i++){
        printCity(path[i]);
    }
}

__device__ void print_path(struct City *path){
    for(int i = 0; i < NUMCITIES; i++){
        printf("%c>", path[i].name);
    }
    printf("\n");
}

__device__ void format_path(struct City *path, char *str){
    for(int i = 0; i < NUMCITIES; i++){
        *str = path[i].name;        
        str++;
        *str = '>';
        str++;
    }
    str--; *str = 0;
}

__device__ void permutations_kernel(struct City *a, char **paths, double *distances, int i, int length, int tid, int *count) { 

    if (length == i){
        long distance = total_distance(a);
        //format_path(a, paths[count[0]]);
        count[0] = count[0] + 1;
    } else {
        for (int j = i; j < length; j++) {
            swap(a, i, j);
            // CUDA
            // permutations(a, i+1, length, tid, count);
            permutations_kernel(a, paths, distances, i+1, length, tid, count);
            swap(a, i, j);
        }
    }
}


__global__ void permute_kernel(struct City *dev_cities, char **paths, double *distances, int size) {

    int tid = threadIdx.x + blockIdx.x * blockDim.x;
    int count[1]; 
    count[0] = 0;

    struct City local_array[NUMCITIES];

    for (int i=0; i<size; i++){
        local_array[i] = dev_cities[i];  
    } 

    //swap(local_array + threadIdx.x, local_array);
    //swap(local_array, threadIdx.x, 0);
    permutations_kernel(local_array, paths, distances, 0, NUMCITIES, tid, count);

}

long factorial(int i) {
    long result = 1;
    while(i > 0) {		
        result *= i;
	i--;
    }
    return result;
}

int main(){

    struct City host_cities[NUMCITIES];
    for(int c = 0; c < NUMCITIES; c++){
        host_cities[c].name = 'A' + c;
        host_cities[c].x = rand() % 20 + 5;
        host_cities[c].y = rand() % 20 + 5;
    }

    //char host_paths [ factorial(NUMCITIES) ][ NUMCITIES*NUMCITIES ];
    char host_paths [0][0]; 
    char **device_paths;

    //double host_distances[ factorial(NUMCITIES) ];
    double host_distances[0];
    double *device_distances;

    float time;
    hipEvent_t start, stop;
    hipEventCreate(&start); 
    hipEventCreate(&stop);

    struct City *device_cities; 
    hipMalloc((void**) &device_cities, sizeof(host_cities));
    //cudaMalloc((void**) &device_paths, sizeof(host_paths));
    hipMalloc((void**) &device_paths, sizeof(char) * NUMCITIES * NUMCITIES * factorial(NUMCITIES));
    hipMalloc((void**) &device_distances, sizeof(host_distances));

    GPUerrchk(hipMemcpy(device_distances, host_distances, sizeof(host_distances), hipMemcpyHostToDevice));
    GPUerrchk(hipMemcpy(device_cities, host_cities, sizeof(host_cities), hipMemcpyHostToDevice));
    //GPUerrchk(cudaMemcpy(device_paths, host_paths, sizeof(host_paths), cudaMemcpyHostToDevice));
    GPUerrchk(hipMemcpy(device_paths, host_paths, sizeof(char) * NUMCITIES * NUMCITIES * factorial(NUMCITIES), hipMemcpyHostToDevice));   

    hipEventRecord(start,0);
    permute_kernel<<<1, NUMCITIES>>>(device_cities, device_paths, device_distances, NUMCITIES);
    hipEventRecord(stop,0);

    GPUerrchk(hipPeekAtLastError());
    GPUerrchk(hipDeviceSynchronize());

    hipEventElapsedTime( &time, start, stop );
    printf("\nTiempo de Ejecucion: %f mSeg\n\n", time);

    hipEventDestroy( start );
    hipEventDestroy( stop );

    hipFree(device_cities);

    return 0;
}



	.text
	.file	"traveler.hip"
	.globl	_Z9printCity4City               # -- Begin function _Z9printCity4City
	.type	_Z9printCity4City,@function
_Z9printCity4City:                      # @_Z9printCity4City
	.cfi_startproc
# %bb.0:
	movq	%rdi, %rsi
	movq	%rdi, %rdx
	shrq	$32, %rdx
	movl	$.L.str, %edi
                                        # kill: def $esi killed $esi killed $rsi
                                        # kill: def $edx killed $edx killed $rdx
	xorl	%eax, %eax
	jmp	printf                          # TAILCALL
.Lfunc_end0:
	.size	_Z9printCity4City, .Lfunc_end0-_Z9printCity4City
	.cfi_endproc
                                        # -- End function
	.globl	_Z4swapP4Cityii                 # -- Begin function _Z4swapP4Cityii
	.type	_Z4swapP4Cityii,@function
_Z4swapP4Cityii:                        # @_Z4swapP4Cityii
	.cfi_startproc
# %bb.0:
	movslq	%esi, %rax
	leaq	(%rax,%rax,2), %rax
	movl	8(%rdi,%rax,4), %ecx
	movl	%ecx, -8(%rsp)
	movq	(%rdi,%rax,4), %rcx
	movq	%rcx, -16(%rsp)
	movslq	%edx, %rcx
	leaq	(%rcx,%rcx,2), %rcx
	movl	8(%rdi,%rcx,4), %edx
	movl	%edx, 8(%rdi,%rax,4)
	movq	(%rdi,%rcx,4), %rdx
	movq	%rdx, (%rdi,%rax,4)
	movl	-8(%rsp), %eax
	movl	%eax, 8(%rdi,%rcx,4)
	movq	-16(%rsp), %rax
	movq	%rax, (%rdi,%rcx,4)
	retq
.Lfunc_end1:
	.size	_Z4swapP4Cityii, .Lfunc_end1-_Z4swapP4Cityii
	.cfi_endproc
                                        # -- End function
	.globl	_Z29__device_stub__permute_kernelP4CityPPcPdi # -- Begin function _Z29__device_stub__permute_kernelP4CityPPcPdi
	.type	_Z29__device_stub__permute_kernelP4CityPPcPdi,@function
_Z29__device_stub__permute_kernelP4CityPPcPdi: # @_Z29__device_stub__permute_kernelP4CityPPcPdi
	.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	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z14permute_kernelP4CityPPcPdi, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	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_end2:
	.size	_Z29__device_stub__permute_kernelP4CityPPcPdi, .Lfunc_end2-_Z29__device_stub__permute_kernelP4CityPPcPdi
	.cfi_endproc
                                        # -- End function
	.globl	_Z9factoriali                   # -- Begin function _Z9factoriali
	.type	_Z9factoriali,@function
_Z9factoriali:                          # @_Z9factoriali
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB3_1
# %bb.2:                                # %.lr.ph.preheader
	movl	%edi, %ecx
	movl	$1, %eax
.LBB3_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	imulq	%rcx, %rax
	leaq	-1(%rcx), %rdx
	cmpq	$1, %rcx
	movq	%rdx, %rcx
	ja	.LBB3_3
# %bb.4:                                # %._crit_edge
	retq
.LBB3_1:
	movl	$1, %eax
	retq
.Lfunc_end3:
	.size	_Z9factoriali, .Lfunc_end3-_Z9factoriali
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$184, %rsp
	.cfi_def_cfa_offset 208
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movb	$65, %bl
	movl	$8, %r14d
.LBB4_1:                                # =>This Inner Loop Header: Depth=1
	movb	%bl, 64(%rsp,%r14)
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$35, %rcx
	addl	%edx, %ecx
	shll	$2, %ecx
	leal	(%rcx,%rcx,4), %ecx
	negl	%ecx
	addl	%ecx, %eax
	addl	$5, %eax
	movl	%eax, 56(%rsp,%r14)
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$35, %rcx
	addl	%edx, %ecx
	shll	$2, %ecx
	leal	(%rcx,%rcx,4), %ecx
	negl	%ecx
	addl	%ecx, %eax
	addl	$5, %eax
	movl	%eax, 60(%rsp,%r14)
	incb	%bl
	addq	$12, %r14
	cmpq	$116, %r14
	jne	.LBB4_1
# %bb.2:                                # %_Z9factoriali.exit
	leaq	32(%rsp), %rdi
	callq	hipEventCreate
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rdi
	movl	$108, %esi
	callq	hipMalloc
	leaq	40(%rsp), %rdi
	movl	$29393280, %esi                 # imm = 0x1C08180
	callq	hipMalloc
	leaq	48(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	hipMalloc
	movq	(%rbx), %rdi
	leaq	8(%rsp), %rsi
	xorl	%edx, %edx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB4_3
# %bb.5:                                # %_Z9GPUassert10hipError_tPcib.exit
	movq	16(%rsp), %rdi
	leaq	64(%rsp), %rsi
	movl	$108, %edx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB4_6
# %bb.7:                                # %_Z9GPUassert10hipError_tPcib.exit9
	movq	40(%rsp), %rdi
	leaq	8(%rsp), %rsi
	movl	$29393280, %edx                 # imm = 0x1C08180
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB4_8
# %bb.9:                                # %_Z9GPUassert10hipError_tPcib.exit16
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movabsq	$4294967297, %rdi               # imm = 0x100000001
	leaq	8(%rdi), %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_11
# %bb.10:
	movq	16(%rsp), %rdi
	movq	40(%rsp), %rsi
	movq	48(%rsp), %rdx
	movl	$9, %ecx
	callq	_Z29__device_stub__permute_kernelP4CityPPcPdi
.LBB4_11:
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	callq	hipPeekAtLastError
	testl	%eax, %eax
	jne	.LBB4_12
# %bb.13:                               # %_Z9GPUassert10hipError_tPcib.exit18
	callq	hipDeviceSynchronize
	testl	%eax, %eax
	jne	.LBB4_14
# %bb.15:                               # %_Z9GPUassert10hipError_tPcib.exit20
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdx
	leaq	60(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	cvtss2sd	(%rbx), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	movq	32(%rsp), %rdi
	callq	hipEventDestroy
	movq	24(%rsp), %rdi
	callq	hipEventDestroy
	movq	16(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$184, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.LBB4_3:
	.cfi_def_cfa_offset 208
	movl	%eax, %ebx
	movq	stderr(%rip), %r14
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.3, %esi
	movl	$.L.str.1, %ecx
	movq	%r14, %rdi
	movq	%rax, %rdx
	movl	$147, %r8d
	jmp	.LBB4_4
.LBB4_6:
	movl	%eax, %ebx
	movq	stderr(%rip), %r14
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.3, %esi
	movl	$.L.str.1, %ecx
	movq	%r14, %rdi
	movq	%rax, %rdx
	movl	$148, %r8d
	jmp	.LBB4_4
.LBB4_8:
	movl	%eax, %ebx
	movq	stderr(%rip), %r14
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.3, %esi
	movl	$.L.str.1, %ecx
	movq	%r14, %rdi
	movq	%rax, %rdx
	movl	$150, %r8d
	jmp	.LBB4_4
.LBB4_12:
	movl	%eax, %ebx
	movq	stderr(%rip), %r14
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.3, %esi
	movl	$.L.str.1, %ecx
	movq	%r14, %rdi
	movq	%rax, %rdx
	movl	$156, %r8d
	jmp	.LBB4_4
.LBB4_14:
	movl	%eax, %ebx
	movq	stderr(%rip), %r14
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.3, %esi
	movl	$.L.str.1, %ecx
	movq	%r14, %rdi
	movq	%rax, %rdx
	movl	$157, %r8d
.LBB4_4:
	xorl	%eax, %eax
	callq	fprintf
	movl	%ebx, %edi
	callq	exit
.Lfunc_end4:
	.size	main, .Lfunc_end4-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	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z14permute_kernelP4CityPPcPdi, %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_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__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	.LBB6_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
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"[x=%i, y=%i]\n"
	.size	.L.str, 14

	.type	_Z14permute_kernelP4CityPPcPdi,@object # @_Z14permute_kernelP4CityPPcPdi
	.section	.rodata,"a",@progbits
	.globl	_Z14permute_kernelP4CityPPcPdi
	.p2align	3, 0x0
_Z14permute_kernelP4CityPPcPdi:
	.quad	_Z29__device_stub__permute_kernelP4CityPPcPdi
	.size	_Z14permute_kernelP4CityPPcPdi, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/alfonsokim/go-traveler/master/traveler.hip"
	.size	.L.str.1, 105

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"\nTiempo de Ejecucion: %f mSeg\n\n"
	.size	.L.str.2, 32

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"GPUassert: %s %s %d\n"
	.size	.L.str.3, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z14permute_kernelP4CityPPcPdi"
	.size	.L__unnamed_1, 31

	.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

	.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 _Z29__device_stub__permute_kernelP4CityPPcPdi
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z14permute_kernelP4CityPPcPdi
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.p2align	2                               ; -- Begin function _Z19permutations_kernelP4CityPPcPdiiiPi
	.type	_Z19permutations_kernelP4CityPPcPdiiiPi,@function
_Z19permutations_kernelP4CityPPcPdiiiPi: ; @_Z19permutations_kernelP4CityPPcPdiiiPi
; %bb.0:
	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	s_mov_b32 s0, s33
	s_mov_b32 s33, s32
	s_or_saveexec_b32 s1, -1
	scratch_store_b32 off, v58, s33 offset:64 ; 4-byte Folded Spill
	s_mov_b32 exec_lo, s1
	v_writelane_b32 v58, s0, 5
	s_addk_i32 s32, 0x50
	s_clause 0x9
	scratch_store_b32 off, v40, s33 offset:36
	; meta instruction
	scratch_store_b32 off, v41, s33 offset:32
	; meta instruction
	scratch_store_b32 off, v42, s33 offset:28
	; meta instruction
	scratch_store_b32 off, v43, s33 offset:24
	; meta instruction
	scratch_store_b32 off, v44, s33 offset:20
	; meta instruction
	scratch_store_b32 off, v45, s33 offset:16
	; meta instruction
	scratch_store_b32 off, v46, s33 offset:12
	; meta instruction
	scratch_store_b32 off, v47, s33 offset:8
	; meta instruction
	scratch_store_b32 off, v56, s33 offset:4
	; meta instruction
	scratch_store_b32 off, v57, s33
	v_writelane_b32 v58, s34, 0
	v_writelane_b32 v58, s35, 1
	v_writelane_b32 v58, s36, 2
	v_writelane_b32 v58, s30, 3
	v_writelane_b32 v58, s31, 4
	v_dual_mov_b32 v41, v4 :: v_dual_mov_b32 v40, v3
	v_dual_mov_b32 v43, v1 :: v_dual_mov_b32 v42, v0
	s_mov_b32 s0, exec_lo
	v_cmpx_ne_u32_e32 9, v2
	s_xor_b32 s34, exec_lo, s0
	s_cbranch_execz .LBB0_5
; %bb.1:                                ; %.preheader
	s_mov_b32 s35, exec_lo
	v_cmpx_gt_i32_e32 9, v2
	s_cbranch_execz .LBB0_4
; %bb.2:                                ; %.lr.ph
	v_mad_u64_u32 v[45:46], null, v2, 12, v[42:43]
	v_mad_i64_i32 v[56:57], null, v2, 12, v[42:43]
	v_add_nc_u32_e32 v44, 1, v2
	v_add_nc_u32_e32 v47, -1, v2
	s_mov_b32 s36, 0
.LBB0_3:                                ; =>This Inner Loop Header: Depth=1
	flat_load_b32 v3, v[56:57] offset:8
	flat_load_b96 v[0:2], v[45:46]
	s_getpc_b64 s[0:1]
	s_add_u32 s0, s0, _Z19permutations_kernelP4CityPPcPdiiiPi@rel32@lo+4
	s_addc_u32 s1, s1, _Z19permutations_kernelP4CityPPcPdiiiPi@rel32@hi+12
	s_waitcnt vmcnt(1) lgkmcnt(1)
	flat_store_b32 v[45:46], v3 offset:8
	flat_load_b64 v[3:4], v[56:57]
	s_waitcnt vmcnt(0) lgkmcnt(0)
	flat_store_b64 v[45:46], v[3:4]
	flat_store_b96 v[56:57], v[0:2]
	v_dual_mov_b32 v0, v42 :: v_dual_mov_b32 v1, v43
	v_mov_b32_e32 v2, v44
	v_dual_mov_b32 v3, v40 :: v_dual_mov_b32 v4, v41
	s_swappc_b64 s[30:31], s[0:1]
	flat_load_b32 v3, v[56:57] offset:8
	flat_load_b96 v[0:2], v[45:46]
	v_add_nc_u32_e32 v47, 1, v47
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_lt_i32_e32 vcc_lo, 7, v47
	s_or_b32 s36, vcc_lo, s36
	s_waitcnt vmcnt(1) lgkmcnt(1)
	flat_store_b32 v[45:46], v3 offset:8
	flat_load_b64 v[3:4], v[56:57]
	s_waitcnt vmcnt(0) lgkmcnt(0)
	flat_store_b64 v[45:46], v[3:4]
	flat_store_b96 v[56:57], v[0:2]
	v_add_co_u32 v56, s0, v56, 12
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v57, s0, 0, v57, s0
	s_and_not1_b32 exec_lo, exec_lo, s36
	s_cbranch_execnz .LBB0_3
.LBB0_4:                                ; %Flow3
	s_or_b32 exec_lo, exec_lo, s35
                                        ; implicit-def: $vgpr40_vgpr41
.LBB0_5:                                ; %Flow4
	s_and_not1_saveexec_b32 s0, s34
	s_cbranch_execz .LBB0_7
; %bb.6:                                ; %_Z14total_distanceP4City.exit
	flat_load_b32 v0, v[40:41]
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_add_nc_u32_e32 v0, 1, v0
	flat_store_b32 v[40:41], v0
.LBB0_7:                                ; %.loopexit
	s_or_b32 exec_lo, exec_lo, s0
	s_clause 0x9
	scratch_load_b32 v57, off, s33
	scratch_load_b32 v56, off, s33 offset:4
	scratch_load_b32 v47, off, s33 offset:8
	scratch_load_b32 v46, off, s33 offset:12
	scratch_load_b32 v45, off, s33 offset:16
	scratch_load_b32 v44, off, s33 offset:20
	scratch_load_b32 v43, off, s33 offset:24
	scratch_load_b32 v42, off, s33 offset:28
	scratch_load_b32 v41, off, s33 offset:32
	scratch_load_b32 v40, off, s33 offset:36
	v_readlane_b32 s30, v58, 3
	v_readlane_b32 s31, v58, 4
	v_readlane_b32 s36, v58, 2
	v_readlane_b32 s35, v58, 1
	v_readlane_b32 s34, v58, 0
	v_readlane_b32 s0, v58, 5
	s_or_saveexec_b32 s1, -1
	scratch_load_b32 v58, off, s33 offset:64 ; 4-byte Folded Reload
	s_mov_b32 exec_lo, s1
	s_addk_i32 s32, 0xffb0
	s_mov_b32 s33, s0
	s_waitcnt vmcnt(0) lgkmcnt(0)
	s_setpc_b64 s[30:31]
.Lfunc_end0:
	.size	_Z19permutations_kernelP4CityPPcPdiiiPi, .Lfunc_end0-_Z19permutations_kernelP4CityPPcPdiiiPi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Function info:
; codeLenInByte = 640
; NumSgprs: 39
; NumVgprs: 59
; ScratchSize: 80
; MemoryBound: 0
	.text
	.protected	_Z14permute_kernelP4CityPPcPdi ; -- Begin function _Z14permute_kernelP4CityPPcPdi
	.globl	_Z14permute_kernelP4CityPPcPdi
	.p2align	8
	.type	_Z14permute_kernelP4CityPPcPdi,@function
_Z14permute_kernelP4CityPPcPdi:         ; @_Z14permute_kernelP4CityPPcPdi
; %bb.0:
	s_load_b64 s[0:1], s[0:1], 0x4
	v_and_b32_e32 v1, 0x3ff, v0
	s_load_b32 s4, s[2:3], 0x18
	v_bfe_u32 v2, v0, 10, 10
	v_bfe_u32 v0, v0, 20, 10
	s_movk_i32 s32, 0x80
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s0, s0, 16
	s_cmp_lt_i32 s4, 1
	s_mul_i32 s0, s0, s1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v1, s0, v1
	v_mad_u32_u24 v1, v2, s1, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_add_lshl_u32 v0, v1, v0, 2
	v_mov_b32_e32 v1, 0
	ds_store_b32 v0, v1
	s_cbranch_scc1 .LBB1_3
; %bb.1:                                ; %.lr.ph.preheader
	s_load_b64 s[0:1], s[2:3], 0x0
	v_mov_b32_e32 v2, 16
.LBB1_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_waitcnt lgkmcnt(0)
	global_load_b96 v[3:5], v1, s[0:1]
	v_add_nc_u32_e32 v6, 8, v2
	s_add_i32 s4, s4, -1
	s_add_u32 s0, s0, 12
	s_addc_u32 s1, s1, 0
	s_cmp_eq_u32 s4, 0
	s_waitcnt vmcnt(0)
	s_clause 0x1
	scratch_store_b32 v6, v5, off
	scratch_store_b64 v2, v[3:4], off
	v_add_nc_u32_e32 v2, 12, v2
	s_cbranch_scc0 .LBB1_2
.LBB1_3:                                ; %._crit_edge
	v_cmp_ne_u32_e32 vcc_lo, -1, v0
	s_mov_b64 s[0:1], src_private_base
	s_mov_b64 s[2:3], src_shared_base
	v_dual_mov_b32 v1, s1 :: v_dual_mov_b32 v2, 0
	v_dual_cndmask_b32 v3, 0, v0 :: v_dual_mov_b32 v0, 16
	v_cndmask_b32_e64 v4, 0, s3, vcc_lo
	s_getpc_b64 s[2:3]
	s_add_u32 s2, s2, _Z19permutations_kernelP4CityPPcPdiiiPi@rel32@lo+4
	s_addc_u32 s3, s3, _Z19permutations_kernelP4CityPPcPdiiiPi@rel32@hi+12
	s_delay_alu instid0(SALU_CYCLE_1)
	s_swappc_b64 s[30:31], s[2:3]
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z14permute_kernelP4CityPPcPdi
		.amdhsa_group_segment_fixed_size 4096
		.amdhsa_private_segment_fixed_size 208
		.amdhsa_kernarg_size 28
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 1
		.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 1
		.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 2
		.amdhsa_next_free_vgpr 59
		.amdhsa_next_free_sgpr 37
		.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_end1:
	.size	_Z14permute_kernelP4CityPPcPdi, .Lfunc_end1-_Z14permute_kernelP4CityPPcPdi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 252
; NumSgprs: 39
; NumVgprs: 59
; ScratchSize: 208
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 4096 bytes/workgroup (compile time only)
; SGPRBlocks: 4
; VGPRBlocks: 7
; NumSGPRsForWavesPerEU: 39
; NumVGPRsForWavesPerEU: 59
; 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: 2
	.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:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 4096
    .kernarg_segment_align: 8
    .kernarg_segment_size: 28
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z14permute_kernelP4CityPPcPdi
    .private_segment_fixed_size: 208
    .sgpr_count:     39
    .sgpr_spill_count: 0
    .symbol:         _Z14permute_kernelP4CityPPcPdi.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: true
    .vgpr_count:     59
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
