/*
 * Copyright 2019 Australian National University
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either or express implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

// this macro checks for errors in cuda calls
#define Err(ans) \
  { gpucheck((ans), __FILE__, __LINE__); }
inline void gpucheck(cudaError_t code, const char *file, int line) {
  if (code != cudaSuccess) {
    fprintf(stderr, "GPU Err: %s %s %d\n", cudaGetErrorString(code), file, line);
    exit(code);
  }
}

/*
Just to save on some time I have included the below list of cuda commands. You can cut and paste directly to set up your code.
Err(cudaMallocHost(&data_h,sizeof(int) * size)); 
Err(cudaMalloc(&data_d, sizeof(int) * size));
Err(cudaMemcpy(data_h, data_d, sizeof(int) * size, cudaMemcpyDeviceToHost));
Err(cudaMemcpy(data_d, data_h, sizeof(int) * size, cudaMemcpyHostToDevice));
Err(cudaDeviceSynchronize()); // remember a kernel launch is asynchronous so you may need to do this after the last kernel.
*/

// 0-1 Knapsack problem - Eric McCreath 2019

// see https://en.wikipedia.org/wiki/Knapsack_problem
// for problem and solution I used in this code.

int n = 2500;   // number of items to select from
int *w;         // this is weight of the items (these are positive)
int *v;         // value gained by placing the item into the knapsack
int W = 100000; // the maximum weight the knapsack can take

int maxvalue = 1000; // the maximum value any item can be worth

// Objective - find a set of items to placing into the knapsack that maximizes the value while keeping the total weight less than or
// equal to W.

/* A dynamic programming approach can be used to solve the problem in psueo-polynomial time.  This works by using a 2D array, called "m",  where m[k][j] is the maximum value that can be stored in the knapsack keeping the weight less than or equal to "j" and using a subset of items which have index less than "k".  */

#define max(A, B) ((A) > (B) ? (A) : (B))

#define m(K, J) (m_array[(K) + (J) * (n + 1)])

void zeroFirstColumn(int *m_array, int n, int W) {
  for (int j = 0; j <= W; j++)
    m(0, j) = 0;
}

void displaySubSet(int *m_array, int n, int W) {
  int cw;
  cw = W;
  printf("best value is %d using : ", m(n, cw));
  for (int k = n - 1; k >= 0; k--) {
    if (m(k + 1, cw) > m(k, cw)) {
      if (W != cw)
        printf(",");
      printf("%d", k);
      cw -= w[k];
    }
  }
  printf("\n");
}

int main() {
  int *m_array;
  int j, k;

  // obtain the memory for the 2D array
  m_array = (int *)malloc(sizeof(int) * (W + 1) * (n + 1));
  if (!m_array) {
    printf("malloc error");
    exit(1);
  }

  // set up and initialize the weights with values
  srand(0);
  w = (int *)malloc(sizeof(int) * n);
  v = (int *)malloc(sizeof(int) * n);
  for (k = 0; k < n; k++) {
    w[k] = (rand() % (W - 1)) + 1;
    v[k] = (rand() % (maxvalue - 1)) + 1;
  }

  // initailize the first column to 0
  zeroFirstColumn(m_array, n, W);

  for (k = 0; k < n; k++) {
    for (j = 0; j <= W; j++) { // the next column is set based on the previous
      if (w[k] > j) {
        // If the items weight is greater than the total weight we are considering it
        // can not be added, so use the best value from the previous column for this weight
        m((k + 1), j) = m(k, j);
      } else {
        // If item "k" not add then best value us just taken
        // from the previous column (same row).
        // If item "k" is added then best value is the sum of the items value and the
        // best value from the previous column and row offset by the items weight.
        // The maximum of these two options is taken.
        m(k + 1, j) = max(m(k, j), v[k] + m(k, j - w[k]));
      }
    }
  }

  // Work out a sub set that gives the maximum value.
  displaySubSet(m_array, n, W);
}

	.file	"tmpxft_00323641_00000000-6_knapsackCUDAv1.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z15zeroFirstColumnPiii
	.type	_Z15zeroFirstColumnPiii, @function
_Z15zeroFirstColumnPiii:
.LFB2028:
	.cfi_startproc
	endbr64
	movslq	%esi, %rsi
	xorl	%eax, %eax
	leaq	4(,%rsi,4), %rcx
.L3:
	cmpl	%edx, %eax
	jg	.L6
	xorl	%esi, %esi
	incl	%eax
	movl	%esi, (%rdi)
	addq	%rcx, %rdi
	jmp	.L3
.L6:
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z15zeroFirstColumnPiii, .-_Z15zeroFirstColumnPiii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"best value is %d using : "
.LC1:
	.string	","
.LC2:
	.string	"%d"
.LC3:
	.string	"\n"
	.text
	.globl	_Z13displaySubSetPiii
	.type	_Z13displaySubSetPiii, @function
_Z13displaySubSetPiii:
.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
	leal	1(%rsi), %r14d
	movl	%r14d, %eax
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%edx, %r13d
	imull	%edx, %eax
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	movl	%r13d, %r15d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%esi, %ebx
	addl	%esi, %eax
	pushq	%rcx
	.cfi_def_cfa_offset 64
	decl	%ebx
	leaq	.LC0(%rip), %rsi
	cltq
	movslq	%ebx, %rbp
	movl	(%rdi,%rax,4), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	salq	$2, %rbp
	call	__printf_chk@PLT
.L8:
	testl	%ebx, %ebx
	js	.L13
	movl	%r14d, %eax
	imull	%r15d, %eax
	leal	1(%rax,%rbx), %edx
	addl	%ebx, %eax
	cltq
	movslq	%edx, %rdx
	movl	(%r12,%rax,4), %eax
	cmpl	%eax, (%r12,%rdx,4)
	jle	.L9
	cmpl	%r13d, %r15d
	je	.L10
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L10:
	movl	%ebx, %edx
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	w(%rip), %rax
	subl	(%rax,%rbp), %r15d
.L9:
	decl	%ebx
	subq	$4, %rbp
	jmp	.L8
.L13:
	popq	%rdx
	.cfi_def_cfa_offset 56
	leaq	.LC3(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 48
	movl	$2, %edi
	popq	%rbp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	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
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z13displaySubSetPiii, .-_Z13displaySubSetPiii
	.section	.rodata.str1.1
.LC4:
	.string	"malloc error"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	movl	W(%rip), %eax
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	leal	1(%rax), %edi
	movl	n(%rip), %eax
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movslq	%edi, %rdi
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	incl	%eax
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	cltq
	imulq	%rax, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	testq	%rax, %rax
	jne	.L15
	movl	$2, %edi
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L15:
	xorl	%edi, %edi
	movq	%rax, %rbx
	call	srand@PLT
	movslq	n(%rip), %rbp
	salq	$2, %rbp
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	%rbp, %rdi
	xorl	%ebp, %ebp
	movq	%rax, w(%rip)
	call	malloc@PLT
	movq	%rax, v(%rip)
.L16:
	movl	n(%rip), %r8d
	cmpl	%ebp, %r8d
	jle	.L24
	call	rand@PLT
	movl	W(%rip), %ecx
	cltd
	decl	%ecx
	idivl	%ecx
	movq	w(%rip), %rax
	incl	%edx
	movl	%edx, (%rax,%rbp,4)
	call	rand@PLT
	movl	maxvalue(%rip), %esi
	cltd
	leal	-1(%rsi), %ecx
	idivl	%ecx
	movq	v(%rip), %rax
	incl	%edx
	movl	%edx, (%rax,%rbp,4)
	incq	%rbp
	jmp	.L16
.L24:
	movl	W(%rip), %r9d
	movl	%r8d, %esi
	movq	%rbx, %rdi
	leal	1(%r8), %r13d
	movl	%r9d, %edx
	call	_Z15zeroFirstColumnPiii
	movslq	%r8d, %rax
	movq	w(%rip), %r10
	movq	v(%rip), %r11
	leaq	4(,%rax,4), %rbp
	xorl	%eax, %eax
.L18:
	movl	%eax, %r12d
	cmpl	%eax, %r8d
	jle	.L21
	leaq	(%rbx,%rax,4), %rsi
	xorl	%ecx, %ecx
.L22:
	cmpl	%ecx, %r9d
	jl	.L25
	movl	(%r10,%rax,4), %r14d
	movl	(%rsi), %edi
	cmpl	%ecx, %r14d
	jg	.L19
	movl	%ecx, %edx
	subl	%r14d, %edx
	imull	%r13d, %edx
	addl	%r12d, %edx
	movslq	%edx, %rdx
	movl	(%rbx,%rdx,4), %edx
	addl	(%r11,%rax,4), %edx
	cmpl	%edx, %edi
	cmovl	%edx, %edi
.L19:
	movl	%edi, 4(%rsi)
	incl	%ecx
	addq	%rbp, %rsi
	jmp	.L22
.L25:
	incq	%rax
	jmp	.L18
.L21:
	movq	%rbx, %rdi
	movl	%r9d, %edx
	movl	%r8d, %esi
	call	_Z13displaySubSetPiii
	popq	%rbx
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	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	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2056:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2056:
	.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
	.globl	maxvalue
	.data
	.align 4
	.type	maxvalue, @object
	.size	maxvalue, 4
maxvalue:
	.long	1000
	.globl	W
	.align 4
	.type	W, @object
	.size	W, 4
W:
	.long	100000
	.globl	v
	.bss
	.align 8
	.type	v, @object
	.size	v, 8
v:
	.zero	8
	.globl	w
	.align 8
	.type	w, @object
	.size	w, 8
w:
	.zero	8
	.globl	n
	.data
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.long	2500
	.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:

/*
 * Copyright 2019 Australian National University
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either or express implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

// this macro checks for errors in cuda calls
#define Err(ans) \
  { gpucheck((ans), __FILE__, __LINE__); }
inline void gpucheck(hipError_t code, const char *file, int line) {
  if (code != hipSuccess) {
    fprintf(stderr, "GPU Err: %s %s %d\n", hipGetErrorString(code), file, line);
    exit(code);
  }
}

/*
Just to save on some time I have included the below list of cuda commands. You can cut and paste directly to set up your code.
Err(cudaMallocHost(&data_h,sizeof(int) * size)); 
Err(cudaMalloc(&data_d, sizeof(int) * size));
Err(cudaMemcpy(data_h, data_d, sizeof(int) * size, cudaMemcpyDeviceToHost));
Err(cudaMemcpy(data_d, data_h, sizeof(int) * size, cudaMemcpyHostToDevice));
Err(cudaDeviceSynchronize()); // remember a kernel launch is asynchronous so you may need to do this after the last kernel.
*/

// 0-1 Knapsack problem - Eric McCreath 2019

// see https://en.wikipedia.org/wiki/Knapsack_problem
// for problem and solution I used in this code.

int n = 2500;   // number of items to select from
int *w;         // this is weight of the items (these are positive)
int *v;         // value gained by placing the item into the knapsack
int W = 100000; // the maximum weight the knapsack can take

int maxvalue = 1000; // the maximum value any item can be worth

// Objective - find a set of items to placing into the knapsack that maximizes the value while keeping the total weight less than or
// equal to W.

/* A dynamic programming approach can be used to solve the problem in psueo-polynomial time.  This works by using a 2D array, called "m",  where m[k][j] is the maximum value that can be stored in the knapsack keeping the weight less than or equal to "j" and using a subset of items which have index less than "k".  */

#define max(A, B) ((A) > (B) ? (A) : (B))

#define m(K, J) (m_array[(K) + (J) * (n + 1)])

void zeroFirstColumn(int *m_array, int n, int W) {
  for (int j = 0; j <= W; j++)
    m(0, j) = 0;
}

void displaySubSet(int *m_array, int n, int W) {
  int cw;
  cw = W;
  printf("best value is %d using : ", m(n, cw));
  for (int k = n - 1; k >= 0; k--) {
    if (m(k + 1, cw) > m(k, cw)) {
      if (W != cw)
        printf(",");
      printf("%d", k);
      cw -= w[k];
    }
  }
  printf("\n");
}

int main() {
  int *m_array;
  int j, k;

  // obtain the memory for the 2D array
  m_array = (int *)malloc(sizeof(int) * (W + 1) * (n + 1));
  if (!m_array) {
    printf("malloc error");
    exit(1);
  }

  // set up and initialize the weights with values
  srand(0);
  w = (int *)malloc(sizeof(int) * n);
  v = (int *)malloc(sizeof(int) * n);
  for (k = 0; k < n; k++) {
    w[k] = (rand() % (W - 1)) + 1;
    v[k] = (rand() % (maxvalue - 1)) + 1;
  }

  // initailize the first column to 0
  zeroFirstColumn(m_array, n, W);

  for (k = 0; k < n; k++) {
    for (j = 0; j <= W; j++) { // the next column is set based on the previous
      if (w[k] > j) {
        // If the items weight is greater than the total weight we are considering it
        // can not be added, so use the best value from the previous column for this weight
        m((k + 1), j) = m(k, j);
      } else {
        // If item "k" not add then best value us just taken
        // from the previous column (same row).
        // If item "k" is added then best value is the sum of the items value and the
        // best value from the previous column and row offset by the items weight.
        // The maximum of these two options is taken.
        m(k + 1, j) = max(m(k, j), v[k] + m(k, j - w[k]));
      }
    }
  }

  // Work out a sub set that gives the maximum value.
  displaySubSet(m_array, n, W);
}

	.text
	.file	"knapsackCUDAv1.hip"
	.globl	_Z15zeroFirstColumnPiii         # -- Begin function _Z15zeroFirstColumnPiii
	.type	_Z15zeroFirstColumnPiii,@function
_Z15zeroFirstColumnPiii:                # @_Z15zeroFirstColumnPiii
	.cfi_startproc
# %bb.0:
                                        # kill: def $edx killed $edx def $rdx
	testl	%edx, %edx
	js	.LBB0_3
# %bb.1:                                # %.lr.ph
	movslq	%esi, %rax
	leaq	4(,%rax,4), %rax
	incl	%edx
.LBB0_2:                                # =>This Inner Loop Header: Depth=1
	movl	$0, (%rdi)
	addq	%rax, %rdi
	decq	%rdx
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z15zeroFirstColumnPiii, .Lfunc_end0-_Z15zeroFirstColumnPiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z13displaySubSetPiii           # -- Begin function _Z13displaySubSetPiii
	.type	_Z13displaySubSetPiii,@function
_Z13displaySubSetPiii:                  # @_Z13displaySubSetPiii
	.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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%edx, %ebx
	movl	%esi, %r15d
	movq	%rdi, %r14
	leal	1(%r15), %ebp
	movl	%ebp, %eax
	imull	%edx, %eax
	addl	%esi, %eax
	cltq
	movl	(%rdi,%rax,4), %esi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	testl	%r15d, %r15d
	jle	.LBB1_7
# %bb.1:                                # %.lr.ph.preheader
	movl	%r15d, %r15d
	movl	%ebx, %r12d
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%r12d, %eax
	imull	%ebp, %eax
	leal	(%r15,%rax), %ecx
	movslq	%ecx, %rcx
	movl	(%r14,%rcx,4), %ecx
	cltq
	leaq	(%r14,%rax,4), %rax
	cmpl	-4(%rax,%r15,4), %ecx
	jle	.LBB1_6
# %bb.3:                                #   in Loop: Header=BB1_2 Depth=1
	cmpl	%ebx, %r12d
	je	.LBB1_5
# %bb.4:                                #   in Loop: Header=BB1_2 Depth=1
	movl	$44, %edi
	callq	putchar@PLT
.LBB1_5:                                #   in Loop: Header=BB1_2 Depth=1
	leal	-1(%r15), %esi
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	movq	w(%rip), %rax
	subl	-4(%rax,%r15,4), %r12d
.LBB1_6:                                #   in Loop: Header=BB1_2 Depth=1
	decq	%r15
	leaq	1(%r15), %rax
	cmpq	$1, %rax
	ja	.LBB1_2
.LBB1_7:                                # %._crit_edge
	movl	$10, %edi
	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
	jmp	putchar@PLT                     # TAILCALL
.Lfunc_end1:
	.size	_Z13displaySubSetPiii, .Lfunc_end1-_Z13displaySubSetPiii
	.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
	movslq	W(%rip), %rax
	incq	%rax
	movslq	n(%rip), %rdi
	incq	%rdi
	imulq	%rax, %rdi
	shlq	$2, %rdi
	callq	malloc
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	testq	%rax, %rax
	je	.LBB2_19
# %bb.1:
	xorl	%edi, %edi
	callq	srand
	movslq	n(%rip), %r14
	leaq	(,%r14,4), %r15
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, w(%rip)
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, v(%rip)
	testq	%r14, %r14
	jle	.LBB2_4
# %bb.2:                                # %.lr.ph.preheader
	xorl	%r15d, %r15d
.LBB2_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	movl	W(%rip), %ecx
	decl	%ecx
	cltd
	idivl	%ecx
	incl	%edx
	movq	w(%rip), %rax
	movl	%edx, (%rax,%r15,4)
	callq	rand
	movl	maxvalue(%rip), %ecx
	decl	%ecx
	cltd
	idivl	%ecx
	incl	%edx
	movq	v(%rip), %rax
	movl	%edx, (%rax,%r15,4)
	incq	%r15
	movslq	n(%rip), %r14
	cmpq	%r14, %r15
	jl	.LBB2_3
.LBB2_4:                                # %._crit_edge
	movl	W(%rip), %edx
	testl	%edx, %edx
	js	.LBB2_7
# %bb.5:                                # %.lr.ph.i
	movslq	%r14d, %rax
	leaq	4(,%rax,4), %rax
	leaq	1(%rdx), %rcx
	movq	8(%rsp), %rsi                   # 8-byte Reload
.LBB2_6:                                # =>This Inner Loop Header: Depth=1
	movl	$0, (%rsi)
	addq	%rax, %rsi
	decq	%rcx
	jne	.LBB2_6
.LBB2_7:                                # %_Z15zeroFirstColumnPiii.exit
	testl	%r14d, %r14d
	jle	.LBB2_18
# %bb.8:                                # %.preheader.lr.ph
	movq	w(%rip), %rax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	%r14d, %ecx
	leaq	(,%rcx,4), %rsi
	movq	v(%rip), %rdi
	leal	1(%rdx), %r8d
	leaq	4(,%rcx,4), %r9
	xorq	$-4, %rsi
	xorl	%r10d, %r10d
.LBB2_9:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_12 Depth 2
	testl	%edx, %edx
	js	.LBB2_10
# %bb.11:                               # %.lr.ph49
                                        #   in Loop: Header=BB2_9 Depth=1
	movq	16(%rsp), %rax                  # 8-byte Reload
	movslq	(%rax,%r10,4), %r15
	leaq	1(%r10), %r11
	movq	8(%rsp), %rax                   # 8-byte Reload
	leaq	(%rax,%r10,4), %r12
	movq	%rsi, %r13
	imulq	%r15, %r13
	xorl	%ebp, %ebp
.LBB2_12:                               #   Parent Loop BB2_9 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpq	%r15, %rbp
	jge	.LBB2_14
# %bb.13:                               #   in Loop: Header=BB2_12 Depth=2
	movl	(%r12), %ebx
	jmp	.LBB2_15
.LBB2_14:                               #   in Loop: Header=BB2_12 Depth=2
	movl	(%r12,%r13), %ebx
	addl	(%rdi,%r10,4), %ebx
	movl	(%r12), %eax
	cmpl	%ebx, %eax
	cmovgl	%eax, %ebx
.LBB2_15:                               #   in Loop: Header=BB2_12 Depth=2
	movl	%ebx, 4(%r12)
	incq	%rbp
	addq	%r9, %r12
	cmpq	%rbp, %r8
	jne	.LBB2_12
# %bb.16:                               #   in Loop: Header=BB2_9 Depth=1
	movq	%r11, %r10
	jmp	.LBB2_17
.LBB2_10:                               # %.preheader.._crit_edge50_crit_edge
                                        #   in Loop: Header=BB2_9 Depth=1
	incq	%r10
	movq	%r10, %r11
.LBB2_17:                               # %._crit_edge50
                                        #   in Loop: Header=BB2_9 Depth=1
	cmpq	%rcx, %r11
	jne	.LBB2_9
.LBB2_18:                               # %._crit_edge52
	movq	8(%rsp), %rdi                   # 8-byte Reload
	movl	%r14d, %esi
                                        # kill: def $edx killed $edx killed $rdx
	callq	_Z13displaySubSetPiii
	xorl	%eax, %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
.LBB2_19:
	.cfi_def_cfa_offset 80
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.type	n,@object                       # @n
	.data
	.globl	n
	.p2align	2, 0x0
n:
	.long	2500                            # 0x9c4
	.size	n, 4

	.type	w,@object                       # @w
	.bss
	.globl	w
	.p2align	3, 0x0
w:
	.quad	0
	.size	w, 8

	.type	v,@object                       # @v
	.globl	v
	.p2align	3, 0x0
v:
	.quad	0
	.size	v, 8

	.type	W,@object                       # @W
	.data
	.globl	W
	.p2align	2, 0x0
W:
	.long	100000                          # 0x186a0
	.size	W, 4

	.type	maxvalue,@object                # @maxvalue
	.globl	maxvalue
	.p2align	2, 0x0
maxvalue:
	.long	1000                            # 0x3e8
	.size	maxvalue, 4

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"best value is %d using : "
	.size	.L.str, 26

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"malloc error"
	.size	.L.str.4, 13

	.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 __hip_cuid_

	.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_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
