/*
 * William Charles Grove
 * Bitonic sort array
 * Sorting Network Implementation
 *
 * 12/1/2016
 */

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

int generate_int() {
	return rand() / 1000000;
}

void print_values(int *values, int length) {
	int j = length / 16;
	int remainder = length - (j * 16);
	int block = 0;
	for (int i = 0; i < j; ++i) {
		printf("\nBlock %d: ", block++);
		for (int index = 0; index < 16; index++) {
			printf("%d ", values[(i * 16) + index]);
		}

	}

	if (remainder > 0) {

		printf("\nBlock %d: ", block);

		for (int i = j * 16; i < (j * 16) + remainder; i++) {
			printf("%d ", values[i]);
		}

	}

}

__global__ void sn_iteration(int *value_array, int j, int k) {
	unsigned int i, x;
	i = threadIdx.x + blockDim.x * blockIdx.x;
	x = i ^ j;

	/* The threads with the lowest ids sort the array. */
	if ((x) > i) {
		if ((i & k) == 0) {
			//Sort ascending
			if (value_array[i] > value_array[x]) {
				//swap
				float temp = value_array[i];
				value_array[i] = value_array[x];
				value_array[x] = temp;
			}
		}
		if ((i & k) != 0) {
			// Sort descending
			if (value_array[i] < value_array[x]) {
				// swap
				float temp = value_array[i];
				value_array[i] = value_array[x];
				value_array[x] = temp;
			}
		}
	}
}


//Sorting network method, activates  a kernel
void sorting_network(int *values, int block_num, int thread_num, int num_vals) {
	int *value_array;
	size_t size = num_vals * sizeof(int);

	cudaMalloc((void**) &value_array, size);
	cudaMemcpy(value_array, values, size, cudaMemcpyHostToDevice);

	/*Unlike the Voronoi diagram,
	 * this process can be modeled with
	 * a one dimensional array.
	 */
	dim3 blocks(block_num, 1);
	dim3 threads(thread_num, 1);

	int j, k;
	/* Major step */
	for (k = 2; k <= num_vals; k <<= 1) {
		/* Minor step */
		for (j = k >> 1; j > 0; j = j >> 1) {
			sn_iteration<<<blocks, threads>>>(value_array, j, k);
		}
	}
	cudaMemcpy(values, value_array, size, cudaMemcpyDeviceToHost);
	cudaFree(value_array);
}

int realloc_array(int **arr, int size) {

	int *temp;

	int fsize = size * sizeof(int);

	temp = (int*) realloc(*arr, fsize);

	if (temp == NULL) {
		printf("Error in realloc!");
		return 0;
	}

	*arr = temp;

	return 1;

}

int main(int argc, char* argv[]) {

	int *values = (int*) malloc(1 * sizeof(int));
	int num_vals = 0;

	if (argc == 1) {
		printf("There were no values passed into the function.");
	}

	if (argc == 2) {

		if ((atoi(argv[1]) == 0)) {
			//It is a file name

			FILE* file = fopen(argv[1], "r");

			if (file == NULL) {
				printf("Error opening file.\n");
				return -1;
			}

			int size = 1;

			int i = 0;
			int index = -1;

			while (!feof(file)) {
				fscanf(file, "%d", &i);

				index++;
				printf("\n[%d:%d]", index, i);
				values[index] = i;

				if (index + 1 == size) {
					size *= 2;
					if (realloc_array(&values, size) == 0) {
						return -1;
					}
				}
			}
			fclose(file);

			if (realloc_array(&values, index) == 0) {
				return -1;
			}

			num_vals = index;

		} else {
			//There is a number of values to generate

			num_vals = atoi(argv[1]);

			if (realloc_array(&values, num_vals) == 0) {
				return -1;
			}

			int i;
			for (i = 0; i < num_vals; ++i) {
				values[i] = generate_int();

			}
		}
	} else {
		//There is a list of values to read in

		for (int i = 1; i < argc; i++) {

			realloc_array(&values, i);
			values[i - 1] = atoi(argv[i]);
		}

		num_vals = argc - 1;

	}

	int padding = 0;
	int block_num = num_vals / 16;
	int remainder = num_vals % 16;

	if(block_num == 0){
		block_num = 1;
	}

	if (remainder > 0) {
		block_num++;
		padding = 16 - remainder;
		realloc_array(&values, num_vals + padding);

		for (int i = num_vals; i < num_vals + padding; i++) {
			values[i] = 50;
		}
	}

	for (int i = 0; i < num_vals + padding; i++) {
		printf("%d\n", values[i]);
	}

	printf("num_val: %d\n", num_vals);
	printf("padding: %d\n", padding);
	printf("remainder: %d\n", remainder);
	printf("block num: %d\n", block_num);
	printf("size of array: %d\n", sizeof(values)/sizeof(int));

	print_values(values, num_vals);

	sorting_network(values, block_num, block_num * 16, num_vals + padding);

	print_values(values, num_vals);
}

	.file	"tmpxft_0026624a_00000000-6_sort.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z12generate_intv
	.type	_Z12generate_intv, @function
_Z12generate_intv:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	rand@PLT
	movl	$1000000, %ecx
	cltd
	idivl	%ecx
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z12generate_intv, .-_Z12generate_intv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"\nBlock %d: "
.LC1:
	.string	"%d "
	.text
	.globl	_Z12print_valuesPii
	.type	_Z12print_valuesPii, @function
_Z12print_valuesPii:
.LFB2028:
	.cfi_startproc
	endbr64
	movl	%esi, %eax
	movl	$16, %ecx
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	cltd
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	idivl	%ecx
	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
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	%rdi, %r12
	movl	%esi, %ebp
	xorl	%r13d, %r13d
	movl	%edx, 12(%rsp)
	movl	%eax, %ebx
.L5:
	movl	%r13d, %edx
	cmpl	%r13d, %ebx
	jle	.L15
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%r14d, %r14d
	call	__printf_chk@PLT
	movq	%r13, %r9
	salq	$6, %r9
	leaq	(%r12,%r9), %r15
.L6:
	movl	(%r15,%r14,4), %edx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%r14
	call	__printf_chk@PLT
	cmpq	$16, %r14
	jne	.L6
	incq	%r13
	jmp	.L5
.L15:
	cmpl	$0, 12(%rsp)
	jle	.L4
	xorl	%edx, %edx
	testl	%ebx, %ebx
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	cmovns	%ebx, %edx
	xorl	%eax, %eax
	sall	$4, %ebx
	leaq	.LC1(%rip), %r13
	movslq	%ebx, %rbx
	call	__printf_chk@PLT
.L9:
	cmpl	%ebx, %ebp
	jle	.L4
	movl	(%r12,%rbx,4), %edx
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbx
	call	__printf_chk@PLT
	jmp	.L9
.L4:
	addq	$24, %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
.LFE2028:
	.size	_Z12print_valuesPii, .-_Z12print_valuesPii
	.section	.rodata.str1.1
.LC2:
	.string	"Error in realloc!"
	.text
	.globl	_Z13realloc_arrayPPii
	.type	_Z13realloc_arrayPPii, @function
_Z13realloc_arrayPPii:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	sall	$2, %esi
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	movslq	%esi, %rsi
	call	realloc@PLT
	testq	%rax, %rax
	jne	.L17
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	xorl	%eax, %eax
	jmp	.L16
.L17:
	movq	%rax, (%rbx)
	movl	$1, %eax
.L16:
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z13realloc_arrayPPii, .-_Z13realloc_arrayPPii
	.globl	_Z34__device_stub__Z12sn_iterationPiiiPiii
	.type	_Z34__device_stub__Z12sn_iterationPiiiPiii, @function
_Z34__device_stub__Z12sn_iterationPiiiPiii:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	32(%rsp), %rdi
	movl	%esi, 4(%rsp)
	leaq	44(%rsp), %rsi
	movl	%edx, (%rsp)
	leaq	16(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L20
	pushq	24(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z12sn_iterationPiii(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 144
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L20:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z34__device_stub__Z12sn_iterationPiiiPiii, .-_Z34__device_stub__Z12sn_iterationPiiiPiii
	.globl	_Z12sn_iterationPiii
	.type	_Z12sn_iterationPiii, @function
_Z12sn_iterationPiii:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z12sn_iterationPiiiPiii
	.cfi_endproc
.LFE2057:
	.size	_Z12sn_iterationPiii, .-_Z12sn_iterationPiii
	.globl	_Z15sorting_networkPiiii
	.type	_Z15sorting_networkPiiii, @function
_Z15sorting_networkPiiii:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	%esi, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movl	%edx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movslq	%ecx, %rbx
	movq	%rbx, %r13
	salq	$2, %rbx
	movq	%rbx, %rsi
	subq	$48, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%r14d, 16(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 20(%rsp)
	movq	%rax, 32(%rsp)
	movl	%ebp, 28(%rsp)
	movl	$2, %ebp
.L26:
	cmpl	%r13d, %ebp
	jg	.L33
	movl	%ebp, %r14d
.L28:
	sarl	%r14d
	testl	%r14d, %r14d
	jle	.L34
	movl	36(%rsp), %ecx
	movq	28(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	16(%rsp), %rdi
	movl	24(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L28
	movq	8(%rsp), %rdi
	movl	%ebp, %edx
	movl	%r14d, %esi
	call	_Z34__device_stub__Z12sn_iterationPiiiPiii
	jmp	.L28
.L34:
	addl	%ebp, %ebp
	jmp	.L26
.L33:
	movq	8(%rsp), %rsi
	movq	%r12, %rdi
	movl	$2, %ecx
	movq	%rbx, %rdx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L31
	call	__stack_chk_fail@PLT
.L31:
	addq	$48, %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
.LFE2029:
	.size	_Z15sorting_networkPiiii, .-_Z15sorting_networkPiiii
	.section	.rodata.str1.1
.LC3:
	.string	"There were no values passed into the function."
.LC4:
	.string	"r"
.LC5:
	.string	"Error opening file.\n"
.LC6:
	.string	"%d"
.LC7:
	.string	"\n[%d:%d]"
.LC8:
	.string	"%d\n"
.LC9:
	.string	"num_val: %d\n"
.LC10:
	.string	"padding: %d\n"
.LC11:
	.string	"remainder: %d\n"
.LC12:
	.string	"block num: %d\n"
.LC13:
	.string	"size of array: %d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.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
	movq	%rsi, %r12
	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
	movl	$4, %edi
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movq	%rax, 32(%rsp)
	cmpl	$1, %ebx
	jne	.L36
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L38:
	movl	$1, %ebp
	leaq	32(%rsp), %r13
	jmp	.L37
.L36:
	cmpl	$2, %ebx
	jne	.L38
	movq	8(%r12), %rbp
	movq	%rbp, %rdi
	call	atoi@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.L39
	movq	%rbp, %rdi
	leaq	.LC4(%rip), %rsi
	call	fopen@PLT
	movq	%rax, %rbp
	testq	%rax, %rax
	jne	.L40
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L47
.L40:
	xorl	%eax, %eax
	xorl	%ebx, %ebx
	movl	$1, %r12d
	movl	%eax, 28(%rsp)
	leaq	.LC6(%rip), %r14
.L42:
	movq	%rbp, %rdi
	movl	%ebx, %r13d
	call	feof@PLT
	testl	%eax, %eax
	jne	.L70
	leaq	28(%rsp), %rdx
	movq	%r14, %rsi
	movq	%rbp, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movl	28(%rsp), %ecx
	movl	%r13d, %edx
	xorl	%eax, %eax
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	32(%rsp), %rax
	movl	28(%rsp), %edx
	movl	%edx, (%rax,%rbx,4)
	leal	1(%rbx), %eax
	cmpl	%eax, %r12d
	jne	.L43
	addl	%r12d, %r12d
	leaq	32(%rsp), %rdi
	movl	%r12d, %esi
	call	_Z13realloc_arrayPPii
	testl	%eax, %eax
	je	.L47
.L43:
	incq	%rbx
	jmp	.L42
.L70:
	decl	%ebx
	movq	%rbp, %rdi
	call	fclose@PLT
	leaq	32(%rsp), %rdi
	movl	%ebx, %esi
	call	_Z13realloc_arrayPPii
	testl	%eax, %eax
	jne	.L46
	jmp	.L47
.L39:
	leaq	32(%rsp), %rdi
	movl	%eax, %esi
	xorl	%ebp, %ebp
	call	_Z13realloc_arrayPPii
	testl	%eax, %eax
	jne	.L48
.L47:
	orl	$-1, %eax
	jmp	.L35
.L48:
	cmpl	%ebp, %ebx
	jle	.L46
	movq	32(%rsp), %rax
	leaq	(%rax,%rbp,4), %r12
	incq	%rbp
	call	_Z12generate_intv
	movl	%eax, (%r12)
	jmp	.L48
.L37:
	movl	%ebp, %esi
	cmpl	%ebp, %ebx
	jle	.L71
	movq	%r13, %rdi
	call	_Z13realloc_arrayPPii
	movq	(%r12,%rbp,8), %rdi
	movq	32(%rsp), %rax
	leaq	-4(%rax,%rbp,4), %r14
	incq	%rbp
	call	atoi@PLT
	movl	%eax, (%r14)
	jmp	.L37
.L71:
	decl	%ebx
.L46:
	movl	%ebx, %eax
	movl	$16, %ecx
	cltd
	idivl	%ecx
	movl	%edx, %r14d
	movl	%eax, %ebp
	testl	%eax, %eax
	jne	.L52
	movl	$1, %ebp
.L52:
	xorl	%r12d, %r12d
	testl	%r14d, %r14d
	jle	.L53
	movl	$16, %r12d
	leaq	32(%rsp), %rdi
	incl	%ebp
	subl	%r14d, %r12d
	leal	(%rbx,%r12), %r13d
	movl	%r13d, %esi
	call	_Z13realloc_arrayPPii
	movq	32(%rsp), %rdx
	movslq	%ebx, %rax
.L54:
	movl	$50, (%rdx,%rax,4)
	incq	%rax
	cmpl	%eax, %r13d
	jg	.L54
.L53:
	xorl	%r15d, %r15d
.L55:
	leal	(%rbx,%r12), %ecx
	movq	32(%rsp), %r13
	cmpl	%r15d, %ecx
	jle	.L72
	movl	0(%r13,%r15,4), %edx
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%r15
	call	__printf_chk@PLT
	jmp	.L55
.L72:
	movl	%ebx, %edx
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	movl	%ecx, 12(%rsp)
	call	__printf_chk@PLT
	movl	%r12d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC10(%rip), %rsi
	call	__printf_chk@PLT
	movl	%r14d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC11(%rip), %rsi
	call	__printf_chk@PLT
	movl	%ebp, %edx
	leaq	.LC12(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$2, %edx
	leaq	.LC13(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	%ebx, %esi
	movq	%r13, %rdi
	call	_Z12print_valuesPii
	movl	12(%rsp), %ecx
	movl	%ebp, %edx
	movl	%ebp, %esi
	sall	$4, %edx
	movq	%r13, %rdi
	call	_Z15sorting_networkPiiii
	movl	%ebx, %esi
	movq	%r13, %rdi
	call	_Z12print_valuesPii
	xorl	%eax, %eax
.L35:
	movq	40(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L57
	call	__stack_chk_fail@PLT
.L57:
	addq	$56, %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
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC14:
	.string	"_Z12sn_iterationPiii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.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	.LC14(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z12sn_iterationPiii(%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
.LFE2059:
	.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:

/*
 * William Charles Grove
 * Bitonic sort array
 * Sorting Network Implementation
 *
 * 12/1/2016
 */


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

int generate_int() {
	return rand() / 1000000;
}

void print_values(int *values, int length) {
	int j = length / 16;
	int remainder = length - (j * 16);
	int block = 0;
	for (int i = 0; i < j; ++i) {
		printf("\nBlock %d: ", block++);
		for (int index = 0; index < 16; index++) {
			printf("%d ", values[(i * 16) + index]);
		}

	}

	if (remainder > 0) {

		printf("\nBlock %d: ", block);

		for (int i = j * 16; i < (j * 16) + remainder; i++) {
			printf("%d ", values[i]);
		}

	}

}

__global__ void sn_iteration(int *value_array, int j, int k) {
	unsigned int i, x;
	i = threadIdx.x + blockDim.x * blockIdx.x;
	x = i ^ j;

	/* The threads with the lowest ids sort the array. */
	if ((x) > i) {
		if ((i & k) == 0) {
			//Sort ascending
			if (value_array[i] > value_array[x]) {
				//swap
				float temp = value_array[i];
				value_array[i] = value_array[x];
				value_array[x] = temp;
			}
		}
		if ((i & k) != 0) {
			// Sort descending
			if (value_array[i] < value_array[x]) {
				// swap
				float temp = value_array[i];
				value_array[i] = value_array[x];
				value_array[x] = temp;
			}
		}
	}
}


//Sorting network method, activates  a kernel
void sorting_network(int *values, int block_num, int thread_num, int num_vals) {
	int *value_array;
	size_t size = num_vals * sizeof(int);

	hipMalloc((void**) &value_array, size);
	hipMemcpy(value_array, values, size, hipMemcpyHostToDevice);

	/*Unlike the Voronoi diagram,
	 * this process can be modeled with
	 * a one dimensional array.
	 */
	dim3 blocks(block_num, 1);
	dim3 threads(thread_num, 1);

	int j, k;
	/* Major step */
	for (k = 2; k <= num_vals; k <<= 1) {
		/* Minor step */
		for (j = k >> 1; j > 0; j = j >> 1) {
			sn_iteration<<<blocks, threads>>>(value_array, j, k);
		}
	}
	hipMemcpy(values, value_array, size, hipMemcpyDeviceToHost);
	hipFree(value_array);
}

int realloc_array(int **arr, int size) {

	int *temp;

	int fsize = size * sizeof(int);

	temp = (int*) realloc(*arr, fsize);

	if (temp == NULL) {
		printf("Error in realloc!");
		return 0;
	}

	*arr = temp;

	return 1;

}

int main(int argc, char* argv[]) {

	int *values = (int*) malloc(1 * sizeof(int));
	int num_vals = 0;

	if (argc == 1) {
		printf("There were no values passed into the function.");
	}

	if (argc == 2) {

		if ((atoi(argv[1]) == 0)) {
			//It is a file name

			FILE* file = fopen(argv[1], "r");

			if (file == NULL) {
				printf("Error opening file.\n");
				return -1;
			}

			int size = 1;

			int i = 0;
			int index = -1;

			while (!feof(file)) {
				fscanf(file, "%d", &i);

				index++;
				printf("\n[%d:%d]", index, i);
				values[index] = i;

				if (index + 1 == size) {
					size *= 2;
					if (realloc_array(&values, size) == 0) {
						return -1;
					}
				}
			}
			fclose(file);

			if (realloc_array(&values, index) == 0) {
				return -1;
			}

			num_vals = index;

		} else {
			//There is a number of values to generate

			num_vals = atoi(argv[1]);

			if (realloc_array(&values, num_vals) == 0) {
				return -1;
			}

			int i;
			for (i = 0; i < num_vals; ++i) {
				values[i] = generate_int();

			}
		}
	} else {
		//There is a list of values to read in

		for (int i = 1; i < argc; i++) {

			realloc_array(&values, i);
			values[i - 1] = atoi(argv[i]);
		}

		num_vals = argc - 1;

	}

	int padding = 0;
	int block_num = num_vals / 16;
	int remainder = num_vals % 16;

	if(block_num == 0){
		block_num = 1;
	}

	if (remainder > 0) {
		block_num++;
		padding = 16 - remainder;
		realloc_array(&values, num_vals + padding);

		for (int i = num_vals; i < num_vals + padding; i++) {
			values[i] = 50;
		}
	}

	for (int i = 0; i < num_vals + padding; i++) {
		printf("%d\n", values[i]);
	}

	printf("num_val: %d\n", num_vals);
	printf("padding: %d\n", padding);
	printf("remainder: %d\n", remainder);
	printf("block num: %d\n", block_num);
	printf("size of array: %d\n", sizeof(values)/sizeof(int));

	print_values(values, num_vals);

	sorting_network(values, block_num, block_num * 16, num_vals + padding);

	print_values(values, num_vals);
}

	.text
	.file	"sort.hip"
	.globl	_Z12generate_intv               # -- Begin function _Z12generate_intv
	.type	_Z12generate_intv,@function
_Z12generate_intv:                      # @_Z12generate_intv
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	rand
	cltq
	imulq	$1125899907, %rax, %rax         # imm = 0x431BDE83
	movq	%rax, %rcx
	shrq	$63, %rcx
	sarq	$50, %rax
	addl	%ecx, %eax
                                        # kill: def $eax killed $eax killed $rax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z12generate_intv, .Lfunc_end0-_Z12generate_intv
	.cfi_endproc
                                        # -- End function
	.globl	_Z12print_valuesPii             # -- Begin function _Z12print_valuesPii
	.type	_Z12print_valuesPii,@function
_Z12print_valuesPii:                    # @_Z12print_valuesPii
	.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	$24, %rsp
	.cfi_def_cfa_offset 80
	.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	%esi, %ebx
	movq	%rdi, %r14
	leal	15(%rbx), %r12d
	testl	%esi, %esi
	cmovnsl	%esi, %r12d
	movl	%r12d, %ebp
	andl	$-16, %ebp
	xorl	%esi, %esi
	cmpl	$16, %ebx
	jl	.LBB1_6
# %bb.1:                                # %.lr.ph.preheader
	movl	%ebp, 12(%rsp)                  # 4-byte Spill
	sarl	$4, %r12d
	movl	%r12d, %r13d
	movq	%r14, 16(%rsp)                  # 8-byte Spill
	xorl	%r15d, %r15d
.LBB1_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_3 Depth 2
	movl	$.L.str, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	xorl	%ebp, %ebp
.LBB1_3:                                #   Parent Loop BB1_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%r14,%rbp,4), %esi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%rbp
	cmpq	$16, %rbp
	jne	.LBB1_3
# %bb.4:                                #   in Loop: Header=BB1_2 Depth=1
	incq	%r15
	addq	$64, %r14
	cmpq	%r13, %r15
	jne	.LBB1_2
# %bb.5:
	movl	%r12d, %esi
	movq	16(%rsp), %r14                  # 8-byte Reload
	movl	12(%rsp), %ebp                  # 4-byte Reload
.LBB1_6:                                # %._crit_edge
	subl	%ebp, %ebx
	jle	.LBB1_9
# %bb.7:
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	movslq	%ebp, %rax
	leaq	(%r14,%rax,4), %r14
	xorl	%r15d, %r15d
.LBB1_8:                                # =>This Inner Loop Header: Depth=1
	movl	(%r14,%r15,4), %esi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpl	%r15d, %ebx
	jne	.LBB1_8
.LBB1_9:                                # %.loopexit
	addq	$24, %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_end1:
	.size	_Z12print_valuesPii, .Lfunc_end1-_Z12print_valuesPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z27__device_stub__sn_iterationPiii # -- Begin function _Z27__device_stub__sn_iterationPiii
	.type	_Z27__device_stub__sn_iterationPiii,@function
_Z27__device_stub__sn_iterationPiii:    # @_Z27__device_stub__sn_iterationPiii
	.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	$96, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rcx
	movl	%esi, (%rcx)
	movq	%rsp, %rsi
	movl	%edx, (%rsi)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%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	$_Z12sn_iterationPiii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$112, %rsp
	.cfi_adjust_cfa_offset -112
	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	_Z27__device_stub__sn_iterationPiii, .Lfunc_end2-_Z27__device_stub__sn_iterationPiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z15sorting_networkPiiii        # -- Begin function _Z15sorting_networkPiiii
	.type	_Z15sorting_networkPiiii,@function
_Z15sorting_networkPiiii:               # @_Z15sorting_networkPiiii
	.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	$24, %rsp
	.cfi_def_cfa_offset 80
	.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	%ecx, %ebp
	movl	%edx, 12(%rsp)                  # 4-byte Spill
	movl	%esi, %r15d
	movq	%rdi, %r14
	movslq	%ecx, %r13
	leaq	(,%r13,4), %rbx
	movq	%rsp, %r12
	movq	%r12, %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	movq	(%r12), %rdi
	movq	%r14, 16(%rsp)                  # 8-byte Spill
	movq	%r14, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	cmpl	$2, %r13d
	jl	.LBB3_7
# %bb.1:                                # %.lr.ph29.preheader
	movl	%r15d, %r15d
	btsq	$32, %r15
	movl	12(%rsp), %r12d                 # 4-byte Reload
	btsq	$32, %r12
	movl	$2, %r14d
.LBB3_2:                                # %.lr.ph29
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_3 Depth 2
	movl	%r14d, %r13d
	sarl	%r13d
	testl	%r13d, %r13d
	jle	.LBB3_6
.LBB3_3:                                # %.lr.ph
                                        #   Parent Loop BB3_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%r15, %rdi
	movl	$1, %esi
	movq	%r12, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_5
# %bb.4:                                #   in Loop: Header=BB3_3 Depth=2
	movq	(%rsp), %rdi
	movl	%r13d, %esi
	movl	%r14d, %edx
	callq	_Z27__device_stub__sn_iterationPiii
.LBB3_5:                                #   in Loop: Header=BB3_3 Depth=2
	movl	%r13d, %eax
	shrl	%eax
	cmpl	$1, %r13d
	movl	%eax, %r13d
	ja	.LBB3_3
.LBB3_6:                                # %._crit_edge
                                        #   in Loop: Header=BB3_2 Depth=1
	addl	%r14d, %r14d
	cmpl	%ebp, %r14d
	jle	.LBB3_2
.LBB3_7:                                # %._crit_edge30
	movq	(%rsp), %rsi
	movq	16(%rsp), %rdi                  # 8-byte Reload
	movq	%rbx, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	(%rsp), %rdi
	callq	hipFree
	addq	$24, %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_end3:
	.size	_Z15sorting_networkPiiii, .Lfunc_end3-_Z15sorting_networkPiiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z13realloc_arrayPPii           # -- Begin function _Z13realloc_arrayPPii
	.type	_Z13realloc_arrayPPii,@function
_Z13realloc_arrayPPii:                  # @_Z13realloc_arrayPPii
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	shll	$2, %esi
	movq	(%rdi), %rdi
	movslq	%esi, %rsi
	callq	realloc
	testq	%rax, %rax
	je	.LBB4_1
# %bb.2:
	movq	%rax, (%rbx)
	movl	$1, %ebx
	jmp	.LBB4_3
.LBB4_1:
	xorl	%ebx, %ebx
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
.LBB4_3:
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z13realloc_arrayPPii, .Lfunc_end4-_Z13realloc_arrayPPii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.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	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %r14
	movl	%edi, %ebx
	movl	$4, %edi
	callq	malloc
	movq	%rax, (%rsp)
	cmpl	$2, %ebx
	je	.LBB5_4
# %bb.1:
	cmpl	$1, %ebx
	jne	.LBB5_21
# %bb.2:                                # %.thread125
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB5_3
.LBB5_4:
	movq	8(%r14), %r14
	movq	%r14, %rdi
	callq	atoi
	testl	%eax, %eax
	je	.LBB5_5
# %bb.16:
	movl	%eax, %ebx
	movq	%rsp, %rdi
	movl	%eax, %esi
	callq	_Z13realloc_arrayPPii
	testl	%eax, %eax
	je	.LBB5_17
# %bb.18:                               # %.preheader
	testl	%ebx, %ebx
	jle	.LBB5_14
# %bb.19:                               # %.lr.ph
	movq	(%rsp), %r14
	movl	%ebx, %r15d
	xorl	%r12d, %r12d
.LBB5_20:                               # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$1125899907, %rax, %rax         # imm = 0x431BDE83
	movq	%rax, %rcx
	shrq	$63, %rcx
	sarq	$50, %rax
	addl	%ecx, %eax
	movl	%eax, (%r14,%r12,4)
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB5_20
	jmp	.LBB5_14
.LBB5_21:
	jle	.LBB5_3
# %bb.22:                               # %.lr.ph97.preheader
	movl	%ebx, %r13d
	movl	$1, %r15d
	movq	%rsp, %r12
.LBB5_23:                               # %.lr.ph97
                                        # =>This Inner Loop Header: Depth=1
	movq	%r12, %rdi
	movl	%r15d, %esi
	callq	_Z13realloc_arrayPPii
	movq	(%r14,%r15,8), %rdi
	callq	atoi
	movq	(%rsp), %rcx
	movl	%eax, -4(%rcx,%r15,4)
	incq	%r15
	cmpq	%r15, %r13
	jne	.LBB5_23
.LBB5_3:                                # %._crit_edge98
	decl	%ebx
.LBB5_14:                               # %.loopexit88
	leal	15(%rbx), %eax
	testl	%ebx, %ebx
	movl	%ebx, %ecx
	cmovsl	%eax, %ecx
	movl	%ecx, %edx
	andl	$-16, %edx
	movl	%ebx, %r15d
	subl	%edx, %r15d
	sarl	$4, %ecx
	cmpl	$31, %eax
	movl	$1, %r14d
	cmovael	%ecx, %r14d
	testl	%r15d, %r15d
	movl	%r15d, 20(%rsp)                 # 4-byte Spill
	jle	.LBB5_15
# %bb.24:                               # %.lr.ph101
	incl	%r14d
	movl	$16, %ebp
	subl	%r15d, %ebp
	leal	(%rbx,%rbp), %r12d
	movq	%rsp, %r13
	movq	%r13, %rdi
	movl	%r12d, %esi
	callq	_Z13realloc_arrayPPii
	movq	(%r13), %rax
	movslq	%ebx, %rcx
	movslq	%r12d, %rdx
.LBB5_25:                               # =>This Inner Loop Header: Depth=1
	movl	$50, (%rax,%rcx,4)
	incq	%rcx
	cmpq	%rdx, %rcx
	jl	.LBB5_25
	jmp	.LBB5_26
.LBB5_15:
	xorl	%ebp, %ebp
.LBB5_26:                               # %.loopexit
	movl	%ebp, %eax
	movq	(%rsp), %r12
	addl	%ebx, %eax
	movl	%eax, 16(%rsp)                  # 4-byte Spill
	jle	.LBB5_29
# %bb.27:                               # %.lr.ph104
	movl	16(%rsp), %r13d                 # 4-byte Reload
	xorl	%r15d, %r15d
.LBB5_28:                               # =>This Inner Loop Header: Depth=1
	movl	(%r12,%r15,4), %esi
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpq	%r15, %r13
	jne	.LBB5_28
.LBB5_29:                               # %._crit_edge105
	xorl	%r13d, %r13d
	movl	$.L.str.9, %edi
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.10, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.11, %edi
	movl	20(%rsp), %esi                  # 4-byte Reload
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.12, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.13, %edi
	movl	$2, %esi
	xorl	%eax, %eax
	callq	printf
	movq	%r12, %rdi
	movl	%ebx, %esi
	callq	_Z12print_valuesPii
	movl	%r14d, %edx
	shll	$4, %edx
	movq	%r12, %rdi
	movl	%r14d, %esi
	movl	16(%rsp), %ecx                  # 4-byte Reload
	callq	_Z15sorting_networkPiiii
	movq	%r12, %rdi
	movl	%ebx, %esi
	callq	_Z12print_valuesPii
.LBB5_30:
	movl	%r13d, %eax
	addq	$24, %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
.LBB5_5:
	.cfi_def_cfa_offset 80
	movl	$.L.str.4, %esi
	movq	%r14, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB5_6
# %bb.7:
	movq	%rax, %r14
	movl	$0, 12(%rsp)
	movq	%rax, %rdi
	callq	feof
	movl	$-1, %r13d
	movl	$-1, %ebx
	testl	%eax, %eax
	jne	.LBB5_13
# %bb.8:                                # %.lr.ph94.preheader
	movl	$1, %ebp
	leaq	12(%rsp), %r15
	movq	%rsp, %r12
	xorl	%ebx, %ebx
.LBB5_9:                                # %.lr.ph94
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.6, %esi
	movq	%r14, %rdi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	12(%rsp), %edx
	movl	$.L.str.7, %edi
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
	movl	12(%rsp), %eax
	movq	(%rsp), %rcx
	movl	%eax, (%rcx,%rbx,4)
	leal	1(%rbx), %eax
	cmpl	%ebp, %eax
	jne	.LBB5_11
# %bb.10:                               #   in Loop: Header=BB5_9 Depth=1
	addl	%ebp, %ebp
	movq	%r12, %rdi
	movl	%ebp, %esi
	callq	_Z13realloc_arrayPPii
	testl	%eax, %eax
	je	.LBB5_30
.LBB5_11:                               #   in Loop: Header=BB5_9 Depth=1
	movq	%r14, %rdi
	callq	feof
	incq	%rbx
	testl	%eax, %eax
	je	.LBB5_9
# %bb.12:                               # %._crit_edge.loopexit
	decl	%ebx
.LBB5_13:                               # %._crit_edge
	movq	%r14, %rdi
	callq	fclose
	movq	%rsp, %rdi
	movl	%ebx, %esi
	callq	_Z13realloc_arrayPPii
	testl	%eax, %eax
	jne	.LBB5_14
	jmp	.LBB5_30
.LBB5_6:                                # %.thread
	movl	$.Lstr, %edi
	callq	puts@PLT
.LBB5_17:
	movl	$-1, %r13d
	jmp	.LBB5_30
.Lfunc_end5:
	.size	main, .Lfunc_end5-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	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z12sn_iterationPiii, %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_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__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	.LBB7_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
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"\nBlock %d: "
	.size	.L.str, 12

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"%d "
	.size	.L.str.1, 4

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

	.type	.L.str.2,@object                # @.str.2
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.2:
	.asciz	"Error in realloc!"
	.size	.L.str.2, 18

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"There were no values passed into the function."
	.size	.L.str.3, 47

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"r"
	.size	.L.str.4, 2

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

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"\n[%d:%d]"
	.size	.L.str.7, 9

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

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"num_val: %d\n"
	.size	.L.str.9, 13

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"padding: %d\n"
	.size	.L.str.10, 13

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

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"block num: %d\n"
	.size	.L.str.12, 15

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"size of array: %d\n"
	.size	.L.str.13, 19

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z12sn_iterationPiii"
	.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,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Error opening file."
	.size	.Lstr, 20

	.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 _Z27__device_stub__sn_iterationPiii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z12sn_iterationPiii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z12sn_iterationPiii    ; -- Begin function _Z12sn_iterationPiii
	.globl	_Z12sn_iterationPiii
	.p2align	8
	.type	_Z12sn_iterationPiii,@function
_Z12sn_iterationPiii:                   ; @_Z12sn_iterationPiii
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x1c
	s_load_b64 s[2:3], s[0:1], 0x8
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[4:5], null, s15, s4, v[0:1]
	v_xor_b32_e32 v0, s2, v4
	s_mov_b32 s2, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_u32_e64 v0, v4
	s_cbranch_execz .LBB0_7
; %bb.1:
	s_load_b64 s[0:1], s[0:1], 0x0
	v_mov_b32_e32 v5, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[4:5]
	v_dual_mov_b32 v1, v5 :: v_dual_and_b32 v4, s3, v4
	v_lshlrev_b64 v[5:6], 2, v[0:1]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v0, vcc_lo, s0, v2
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v3, vcc_lo
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v2, vcc_lo, s0, v5
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v6, vcc_lo
	s_mov_b32 s0, 0
	s_clause 0x1
	global_load_b32 v5, v[0:1], off
	global_load_b32 v6, v[2:3], off
	s_mov_b32 s1, exec_lo
	v_cmpx_ne_u32_e32 0, v4
	s_xor_b32 s1, exec_lo, s1
	s_cbranch_execz .LBB0_3
; %bb.2:
	s_waitcnt vmcnt(0)
	v_cmp_lt_i32_e32 vcc_lo, v5, v6
	s_and_b32 s0, vcc_lo, exec_lo
.LBB0_3:                                ; %Flow
	s_and_not1_saveexec_b32 s1, s1
	s_cbranch_execz .LBB0_5
; %bb.4:
	s_waitcnt vmcnt(0)
	v_cmp_gt_i32_e32 vcc_lo, v5, v6
	s_and_not1_b32 s0, s0, exec_lo
	s_and_b32 s2, vcc_lo, exec_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 s0, s0, s2
.LBB0_5:                                ; %Flow47
	s_or_b32 exec_lo, exec_lo, s1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_b32 exec_lo, exec_lo, s0
	s_cbranch_execz .LBB0_7
; %bb.6:                                ; %.sink.split
	s_waitcnt vmcnt(1)
	v_cvt_f32_i32_e32 v4, v5
	s_delay_alu instid0(VALU_DEP_1)
	v_cvt_i32_f32_e32 v4, v4
	s_waitcnt vmcnt(0)
	s_clause 0x1
	global_store_b32 v[0:1], v6, off
	global_store_b32 v[2:3], v4, off
.LBB0_7:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z12sn_iterationPiii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.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 0
		.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 7
		.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	_Z12sn_iterationPiii, .Lfunc_end0-_Z12sn_iterationPiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 296
; NumSgprs: 18
; NumVgprs: 7
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 7
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; 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:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z12sn_iterationPiii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z12sn_iterationPiii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     7
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
