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

using namespace std;

#define DEBUG_LEVEL 0

#define ELEMENTS_PER_BUY_OPTION 2
#define STORE_ID_OFFSET 0
#define PRICE_OFFSET 1

#define NUM_PRODUCTS 30000
#define NUM_BUY_OPTIONS 1024 // Debe ser igual al número de threads.

#define NUM_THREADS 1024 // El número mínimo de threads es 32 (por el tamaño de warp) y el maximo 1024.

void initAllProductsBuyOptions(unsigned int *all_products_buy_options);

void printAllProductsAllBuyOptions(unsigned int *all_products_buy_options);
void getBestBuyOptions(unsigned int *all_products_buy_options, unsigned int *best_buy_options);
void printBestBuyOptions(unsigned int *best_buy_options);

bool areResultsValid(unsigned int *all_products_buy_options, unsigned int *best_buy_options);

__global__ void KernelMarketplace(unsigned int *total_buy_options, unsigned int *best_buy_options)
{
    __shared__ unsigned int tmp_best_buy_options[NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION];

    unsigned int thread_id = threadIdx.x;
    unsigned int shared_thread_buy_option = thread_id * ELEMENTS_PER_BUY_OPTION;
    unsigned int global_thread_buy_option = ( blockIdx.x * blockDim.x + thread_id ) * ELEMENTS_PER_BUY_OPTION;

    tmp_best_buy_options[shared_thread_buy_option + STORE_ID_OFFSET] = total_buy_options[global_thread_buy_option + STORE_ID_OFFSET];
    tmp_best_buy_options[shared_thread_buy_option + PRICE_OFFSET] = total_buy_options[global_thread_buy_option + PRICE_OFFSET];
    __syncthreads();

    for (unsigned int stride = 2; stride <= blockDim.x; stride *= 2)
    {
        if (thread_id % stride == 0)
        {
            unsigned int next_buy_option_position = shared_thread_buy_option + stride;

            if (tmp_best_buy_options[shared_thread_buy_option + PRICE_OFFSET] > tmp_best_buy_options[next_buy_option_position + PRICE_OFFSET])
            {
                tmp_best_buy_options[shared_thread_buy_option + STORE_ID_OFFSET] = tmp_best_buy_options[next_buy_option_position + STORE_ID_OFFSET];
                tmp_best_buy_options[shared_thread_buy_option + PRICE_OFFSET] = tmp_best_buy_options[next_buy_option_position + PRICE_OFFSET];
            }
        }
        __syncthreads();
    }

    if (thread_id == 0)
    {
        best_buy_options[blockIdx.x * ELEMENTS_PER_BUY_OPTION + STORE_ID_OFFSET] = tmp_best_buy_options[STORE_ID_OFFSET];
        best_buy_options[blockIdx.x * ELEMENTS_PER_BUY_OPTION + PRICE_OFFSET] = tmp_best_buy_options[PRICE_OFFSET];
    }
}

int main(int argc, char** argv)
{
    // Buy options in host and device
    unsigned int *host_all_products_buy_options = (unsigned int *) malloc( NUM_PRODUCTS * NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int) );
    unsigned int *best_buy_options = (unsigned int *) malloc( NUM_PRODUCTS * ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int) );
    unsigned int *seq_best_buy_options = (unsigned int *) malloc( NUM_PRODUCTS * ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int) );
    unsigned int *device_all_products_buy_options;
    unsigned int *device_best_buy_options;

    // Metadata
    unsigned int buy_option_size = ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int);
    unsigned int num_total_buy_options = NUM_PRODUCTS * NUM_BUY_OPTIONS;
    unsigned int total_buy_options_size = num_total_buy_options * buy_option_size;
    unsigned int best_buy_options_size = NUM_PRODUCTS * buy_option_size;

    // Benchmarking
    float elapsed_time;
    cudaEvent_t start;
    cudaEvent_t stop;
    float seq_elapsed_time;
    cudaEvent_t seq_start;
    cudaEvent_t seq_stop;

    initAllProductsBuyOptions(host_all_products_buy_options);

    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    cudaEventCreate(&seq_start);
    cudaEventCreate(&seq_stop);

    // Obtener Memoria en el device
    cudaMalloc( (unsigned int**) &device_all_products_buy_options, total_buy_options_size );
    cudaMalloc( (unsigned int**) &device_best_buy_options, best_buy_options_size );

    // Copiar datos desde el host en el device
    cudaMemcpy(device_all_products_buy_options, host_all_products_buy_options, total_buy_options_size, cudaMemcpyHostToDevice);

    cudaEventRecord(start, 0);

    // Ejecutar el kernel (número de bloques = número de productos). Un bloque por cada producto y todos los threads por cada bloque.
    KernelMarketplace<<<NUM_PRODUCTS, NUM_THREADS>>>(device_all_products_buy_options, device_best_buy_options);

    // Obtener el resultado parcial desde el host
    cudaMemcpy(best_buy_options, device_best_buy_options, best_buy_options_size, cudaMemcpyDeviceToHost);

    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);

    // Liberar Memoria del device
    cudaFree(device_all_products_buy_options);
    cudaFree(device_best_buy_options);

    cudaEventElapsedTime(&elapsed_time, start, stop);

    printf("\nKERNEL MARKETPLACE\n");
    printf("Number buy options per product: %d\n", NUM_BUY_OPTIONS);
    printf("Number of blocks (products): %d\n", NUM_PRODUCTS);
    printf("Number of Threads: %d\n", NUM_THREADS);
    printf("Vector Size: %d\n", num_total_buy_options);
    printf("Total time %4.6f milseg\n", elapsed_time);
    printf("Bandwidth %4.3f GB/s\n", (num_total_buy_options * ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int)) / (1000000 * elapsed_time));

    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    if (DEBUG_LEVEL >= 2)
    {
        printAllProductsAllBuyOptions(host_all_products_buy_options);
    }

    if (DEBUG_LEVEL >= 1)
    {
        printBestBuyOptions(best_buy_options);
    }

    if ( areResultsValid( host_all_products_buy_options, best_buy_options ) )
    {
        printf ("TEST PASS\n");
    }
    else
    {
        printf ("TEST FAIL\n");
    }

    cudaEventRecord(seq_start, 0);

    getBestBuyOptions(host_all_products_buy_options, seq_best_buy_options);

    cudaEventRecord(seq_stop, 0);
    cudaEventSynchronize(seq_stop);

    cudaEventElapsedTime(&seq_elapsed_time, seq_start, seq_stop);

    printf("\nSEQUENCIAL\n");
    printf("Elapsed time %4.6f milseg\n", seq_elapsed_time);

    cudaEventDestroy(seq_start);
    cudaEventDestroy(seq_stop);
}

void initAllProductsBuyOptions(unsigned int *all_products_buy_options)
{
    for(unsigned int product_iteration = 0; product_iteration < NUM_PRODUCTS; ++product_iteration)
    {
        unsigned int current_product_position = product_iteration * NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION;

        for(unsigned int buy_option_iteration = 0; buy_option_iteration < NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION; buy_option_iteration += ELEMENTS_PER_BUY_OPTION)
        {
            unsigned int current_product_store_position = current_product_position + buy_option_iteration + STORE_ID_OFFSET;
            unsigned int current_product_price_position = current_product_position + buy_option_iteration + PRICE_OFFSET;

            // Set the current product buy option to the store with the same id as the current iteration in order to do not duplicate buy options.
            all_products_buy_options[current_product_store_position] = buy_option_iteration/2;

            // Set the price with a random value between 1 and 1000.
            all_products_buy_options[current_product_price_position] = rand() % 99999 + 1;
        }
    }
}

bool areResultsValid(unsigned int *all_products_buy_options, unsigned int *best_buy_options_by_device)
{
   unsigned int *best_buy_options_by_host = (unsigned int *) malloc( NUM_PRODUCTS * ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int) );

   getBestBuyOptions(all_products_buy_options, best_buy_options_by_host);

   if (DEBUG_LEVEL >= 1)
   {
       printBestBuyOptions(best_buy_options_by_host);
   }

   for (unsigned int product_iteration = 0; product_iteration < NUM_PRODUCTS * ELEMENTS_PER_BUY_OPTION; product_iteration += ELEMENTS_PER_BUY_OPTION)
   {
       unsigned int current_product_store_position = product_iteration + STORE_ID_OFFSET;
       unsigned int current_product_price_position = product_iteration + PRICE_OFFSET;

       unsigned int best_store_by_device = best_buy_options_by_device[current_product_store_position];
       unsigned int best_price_by_device = best_buy_options_by_device[current_product_price_position];

       unsigned int best_store_by_host = best_buy_options_by_host[current_product_store_position];
       unsigned int best_price_by_host = best_buy_options_by_host[current_product_price_position];

       if (best_price_by_device != best_price_by_host)
       {
           if (DEBUG_LEVEL >= 1)
           {
               printf("FAILED IN product: %d\n", product_iteration);
               printf("\tbest_store_by_device: %d\n", best_store_by_device);
               printf("\tbest_store_by_host: %d\n", best_store_by_host);
               printf("\tbest_price_by_device: %d\n", best_price_by_device);
               printf("\tbest_price_by_host: %d\n", best_price_by_host);
           }

           return false;
       }
   }

   return true;
}

void printAllProductsAllBuyOptions(unsigned int *all_products_buy_options)
{
    cout << "All products buy options:" << endl;
    for (unsigned int i = 0; i < NUM_PRODUCTS; ++i){
        cout << endl << "\tproduct_id: " << i << endl;
        for (unsigned int j = 0; j < NUM_BUY_OPTIONS*2; j += 2){
            cout << "Buy option:" << endl;
            cout << "\tstore_id: " << all_products_buy_options[i*NUM_BUY_OPTIONS*2+j] << endl;
            cout << "\tprice: " << all_products_buy_options[i*NUM_BUY_OPTIONS*2+j+1] << endl;
        }
    }
}

void getBestBuyOptions(unsigned int *all_products_buy_options, unsigned int *best_buy_options)
{
    for(unsigned int product_iteration = 0; product_iteration < NUM_PRODUCTS; ++product_iteration)
    {
        unsigned int current_product_position = product_iteration * NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION;
        unsigned int current_product_store_position = current_product_position + STORE_ID_OFFSET;
        unsigned int current_product_price_position = current_product_position + PRICE_OFFSET;

        unsigned int best_store = all_products_buy_options[current_product_store_position];
        unsigned int best_price = all_products_buy_options[current_product_price_position];

        for(unsigned int product_to_compare = ELEMENTS_PER_BUY_OPTION; product_to_compare < NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION; product_to_compare += ELEMENTS_PER_BUY_OPTION)
        {
            if (all_products_buy_options[current_product_position + product_to_compare + PRICE_OFFSET] < best_price)
            {
               best_store = all_products_buy_options[product_iteration * NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION + product_to_compare + STORE_ID_OFFSET];
               best_price = all_products_buy_options[product_iteration * NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION + product_to_compare + PRICE_OFFSET];
            }
        }
        best_buy_options[product_iteration * ELEMENTS_PER_BUY_OPTION + STORE_ID_OFFSET] = best_store;
        best_buy_options[product_iteration * ELEMENTS_PER_BUY_OPTION + PRICE_OFFSET] = best_price;
    }
}

void printBestBuyOptions(unsigned int *best_buy_options)
{
    cout << endl <<"Best products buy options:" << endl;
    for (unsigned int i = 0; i < NUM_PRODUCTS * ELEMENTS_PER_BUY_OPTION; i += ELEMENTS_PER_BUY_OPTION)
    {
        cout << "Best buy option for product_id: " << i / ELEMENTS_PER_BUY_OPTION << endl;
        cout << "\tstore_id: " << best_buy_options[i + STORE_ID_OFFSET] << endl;
        cout << "\tprice: " << best_buy_options[i + PRICE_OFFSET] << endl;
    }
}

	.file	"tmpxft_0022a1f9_00000000-6_main.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3643:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3643:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z25initAllProductsBuyOptionsPj
	.type	_Z25initAllProductsBuyOptionsPj, @function
_Z25initAllProductsBuyOptionsPj:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$99999, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
.L4:
	movq	%rbp, %rbx
	xorl	%r12d, %r12d
	salq	$13, %rbx
	addq	%r13, %rbx
.L3:
	movl	%r12d, %eax
	addl	$2, %r12d
	addq	$8, %rbx
	shrl	%eax
	movl	%eax, -8(%rbx)
	call	rand@PLT
	cltd
	idivl	%r14d
	incl	%edx
	movl	%edx, -4(%rbx)
	cmpl	$2048, %r12d
	jne	.L3
	incq	%rbp
	cmpq	$30000, %rbp
	jne	.L4
	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
.LFE3636:
	.size	_Z25initAllProductsBuyOptionsPj, .-_Z25initAllProductsBuyOptionsPj
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"All products buy options:"
.LC1:
	.string	"\tproduct_id: "
.LC2:
	.string	"Buy option:"
.LC3:
	.string	"\tstore_id: "
.LC4:
	.string	"\tprice: "
	.text
	.globl	_Z29printAllProductsAllBuyOptionsPj
	.type	_Z29printAllProductsAllBuyOptionsPj, @function
_Z29printAllProductsAllBuyOptionsPj:
.LFB3638:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC0(%rip), %rsi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	leaq	.LC1(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdi, %r13
	leaq	_ZSt4cout(%rip), %rdi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	pushq	%rdx
	.cfi_def_cfa_offset 64
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E@PLT
.L11:
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	movq	%rbp, %rbx
	xorl	%r12d, %r12d
	salq	$13, %rbx
	leaq	.LC2(%rip), %r15
	call	_ZNSolsEPFRSoS_E@PLT
	movq	%r14, %rsi
	addq	%r13, %rbx
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rbp, %rsi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E@PLT
.L10:
	movq	%r15, %rsi
	leaq	_ZSt4cout(%rip), %rdi
	addl	$2, %r12d
	addq	$8, %rbx
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E@PLT
	leaq	.LC3(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	-8(%rbx), %esi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E@PLT
	leaq	.LC4(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	-4(%rbx), %esi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E@PLT
	cmpl	$2048, %r12d
	jne	.L10
	incq	%rbp
	cmpq	$30000, %rbp
	jne	.L11
	popq	%rax
	.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
.LFE3638:
	.size	_Z29printAllProductsAllBuyOptionsPj, .-_Z29printAllProductsAllBuyOptionsPj
	.globl	_Z17getBestBuyOptionsPjS_
	.type	_Z17getBestBuyOptionsPjS_, @function
_Z17getBestBuyOptionsPjS_:
.LFB3639:
	.cfi_startproc
	endbr64
	addq	$12, %rdi
	xorl	%eax, %eax
.L18:
	movl	-12(%rdi), %r9d
	movl	-8(%rdi), %ecx
	movq	%rdi, %rdx
	movl	$2, %r8d
.L17:
	movl	(%rdx), %r10d
	cmpl	%ecx, %r10d
	jnb	.L16
	movl	-4(%rdx), %r9d
	movl	%r10d, %ecx
.L16:
	addl	$2, %r8d
	addq	$8, %rdx
	cmpl	$2048, %r8d
	jne	.L17
	movl	%r9d, (%rsi,%rax)
	addq	$8192, %rdi
	movl	%ecx, 4(%rsi,%rax)
	addq	$8, %rax
	cmpq	$240000, %rax
	jne	.L18
	ret
	.cfi_endproc
.LFE3639:
	.size	_Z17getBestBuyOptionsPjS_, .-_Z17getBestBuyOptionsPjS_
	.globl	_Z15areResultsValidPjS_
	.type	_Z15areResultsValidPjS_, @function
_Z15areResultsValidPjS_:
.LFB3637:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rdi, %rbp
	movl	$240000, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rsi, %rbx
	pushq	%rcx
	.cfi_def_cfa_offset 32
	call	malloc@PLT
	movq	%rbp, %rdi
	movq	%rax, %rsi
	movq	%rax, %r11
	call	_Z17getBestBuyOptionsPjS_
	movl	$4, %edx
.L23:
	movl	(%r11,%rdx), %eax
	cmpl	%eax, (%rbx,%rdx)
	jne	.L24
	addq	$8, %rdx
	cmpq	$240004, %rdx
	jne	.L23
	movb	$1, %al
	jmp	.L21
.L24:
	xorl	%eax, %eax
.L21:
	popq	%rdx
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3637:
	.size	_Z15areResultsValidPjS_, .-_Z15areResultsValidPjS_
	.section	.rodata.str1.1
.LC5:
	.string	"Best products buy options:"
.LC6:
	.string	"Best buy option for product_id: "
	.text
	.globl	_Z19printBestBuyOptionsPj
	.type	_Z19printBestBuyOptionsPj, @function
_Z19printBestBuyOptionsPj:
.LFB3640:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC6(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	leaq	_ZSt4cout(%rip), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rdi, %rbp
	movq	%r12, %rdi
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 48
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	call	_ZNSolsEPFRSoS_E@PLT
	leaq	.LC5(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E@PLT
.L28:
	movq	%r13, %rsi
	movq	%r12, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%ebx, %esi
	movq	%rax, %rdi
	shrl	%esi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E@PLT
	leaq	.LC3(%rip), %rsi
	movq	%r12, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	0(%rbp,%rbx,4), %esi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E@PLT
	leaq	.LC4(%rip), %rsi
	movq	%r12, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	4(%rbp,%rbx,4), %esi
	addq	$2, %rbx
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E@PLT
	cmpq	$60000, %rbx
	jne	.L28
	popq	%rax
	.cfi_def_cfa_offset 40
	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
.LFE3640:
	.size	_Z19printBestBuyOptionsPj, .-_Z19printBestBuyOptionsPj
	.globl	_Z39__device_stub__Z17KernelMarketplacePjS_PjS_
	.type	_Z39__device_stub__Z17KernelMarketplacePjS_PjS_, @function
_Z39__device_stub__Z17KernelMarketplacePjS_PjS_:
.LFB3665:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movq	%rsi, (%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L31
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z17KernelMarketplacePjS_(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L31:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L33
	call	__stack_chk_fail@PLT
.L33:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3665:
	.size	_Z39__device_stub__Z17KernelMarketplacePjS_PjS_, .-_Z39__device_stub__Z17KernelMarketplacePjS_PjS_
	.globl	_Z17KernelMarketplacePjS_
	.type	_Z17KernelMarketplacePjS_, @function
_Z17KernelMarketplacePjS_:
.LFB3666:
	.cfi_startproc
	endbr64
	jmp	_Z39__device_stub__Z17KernelMarketplacePjS_PjS_
	.cfi_endproc
.LFE3666:
	.size	_Z17KernelMarketplacePjS_, .-_Z17KernelMarketplacePjS_
	.section	.rodata.str1.1
.LC7:
	.string	"\nKERNEL MARKETPLACE\n"
.LC8:
	.string	"Number buy options per product: %d\n"
.LC9:
	.string	"Number of blocks (products): %d\n"
.LC10:
	.string	"Number of Threads: %d\n"
.LC11:
	.string	"Vector Size: %d\n"
.LC12:
	.string	"Total time %4.6f milseg\n"
.LC15:
	.string	"Bandwidth %4.3f GB/s\n"
.LC16:
	.string	"TEST PASS\n"
.LC17:
	.string	"TEST FAIL\n"
.LC18:
	.string	"\nSEQUENCIAL\n"
.LC19:
	.string	"Elapsed time %4.6f milseg\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	$245760000, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$80, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$240000, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	movl	$240000, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r12
	call	_Z25initAllProductsBuyOptionsPj
	leaq	16(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	24(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	32(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	40(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	%rsp, %rdi
	movl	$245760000, %esi
	call	cudaMalloc@PLT
	leaq	8(%rsp), %rdi
	movl	$240000, %esi
	call	cudaMalloc@PLT
	movq	(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$245760000, %edx
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	$4194305, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$268437331, %edi
	salq	$10, %rdx
	movl	$1, %ecx
	movl	$1, %esi
	salq	$4, %rdi
	movq	%rdx, 60(%rsp)
	movl	$1, 68(%rsp)
	movq	%rdi, 48(%rsp)
	movl	$1, 56(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L37
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdi
	call	_Z39__device_stub__Z17KernelMarketplacePjS_PjS_
.L37:
	movq	8(%rsp), %rsi
	movl	$2, %ecx
	movl	$240000, %edx
	movq	%rbp, %rdi
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	24(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rsi
	leaq	48(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1024, %edx
	leaq	.LC8(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$30000, %edx
	leaq	.LC9(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$1024, %edx
	leaq	.LC10(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$30720000, %edx
	leaq	.LC11(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	48(%rsp), %xmm0
	call	__printf_chk@PLT
	movl	$2, %edi
	movb	$1, %al
	movss	.LC13(%rip), %xmm1
	mulss	48(%rsp), %xmm1
	leaq	.LC15(%rip), %rsi
	movss	.LC14(%rip), %xmm0
	divss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	16(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	24(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	call	_Z15areResultsValidPjS_
	leaq	.LC16(%rip), %rsi
	testb	%al, %al
	jne	.L42
	leaq	.LC17(%rip), %rsi
.L42:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	%r12, %rsi
	movq	%rbx, %rdi
	call	_Z17getBestBuyOptionsPjS_
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	40(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	leaq	60(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC18(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	60(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	40(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L40
	call	__stack_chk_fail@PLT
.L40:
	addq	$80, %rsp
	.cfi_def_cfa_offset 32
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3635:
	.size	main, .-main
	.section	.rodata.str1.1
.LC20:
	.string	"_Z17KernelMarketplacePjS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3668:
	.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	.LC20(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z17KernelMarketplacePjS_(%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
.LFE3668:
	.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
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC13:
	.long	1232348160
	.align 4
.LC14:
	.long	1298817024
	.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 <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

#define DEBUG_LEVEL 0

#define ELEMENTS_PER_BUY_OPTION 2
#define STORE_ID_OFFSET 0
#define PRICE_OFFSET 1

#define NUM_PRODUCTS 30000
#define NUM_BUY_OPTIONS 1024 // Debe ser igual al número de threads.

#define NUM_THREADS 1024 // El número mínimo de threads es 32 (por el tamaño de warp) y el maximo 1024.

void initAllProductsBuyOptions(unsigned int *all_products_buy_options);

void printAllProductsAllBuyOptions(unsigned int *all_products_buy_options);
void getBestBuyOptions(unsigned int *all_products_buy_options, unsigned int *best_buy_options);
void printBestBuyOptions(unsigned int *best_buy_options);

bool areResultsValid(unsigned int *all_products_buy_options, unsigned int *best_buy_options);

__global__ void KernelMarketplace(unsigned int *total_buy_options, unsigned int *best_buy_options)
{
    __shared__ unsigned int tmp_best_buy_options[NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION];

    unsigned int thread_id = threadIdx.x;
    unsigned int shared_thread_buy_option = thread_id * ELEMENTS_PER_BUY_OPTION;
    unsigned int global_thread_buy_option = ( blockIdx.x * blockDim.x + thread_id ) * ELEMENTS_PER_BUY_OPTION;

    tmp_best_buy_options[shared_thread_buy_option + STORE_ID_OFFSET] = total_buy_options[global_thread_buy_option + STORE_ID_OFFSET];
    tmp_best_buy_options[shared_thread_buy_option + PRICE_OFFSET] = total_buy_options[global_thread_buy_option + PRICE_OFFSET];
    __syncthreads();

    for (unsigned int stride = 2; stride <= blockDim.x; stride *= 2)
    {
        if (thread_id % stride == 0)
        {
            unsigned int next_buy_option_position = shared_thread_buy_option + stride;

            if (tmp_best_buy_options[shared_thread_buy_option + PRICE_OFFSET] > tmp_best_buy_options[next_buy_option_position + PRICE_OFFSET])
            {
                tmp_best_buy_options[shared_thread_buy_option + STORE_ID_OFFSET] = tmp_best_buy_options[next_buy_option_position + STORE_ID_OFFSET];
                tmp_best_buy_options[shared_thread_buy_option + PRICE_OFFSET] = tmp_best_buy_options[next_buy_option_position + PRICE_OFFSET];
            }
        }
        __syncthreads();
    }

    if (thread_id == 0)
    {
        best_buy_options[blockIdx.x * ELEMENTS_PER_BUY_OPTION + STORE_ID_OFFSET] = tmp_best_buy_options[STORE_ID_OFFSET];
        best_buy_options[blockIdx.x * ELEMENTS_PER_BUY_OPTION + PRICE_OFFSET] = tmp_best_buy_options[PRICE_OFFSET];
    }
}

int main(int argc, char** argv)
{
    // Buy options in host and device
    unsigned int *host_all_products_buy_options = (unsigned int *) malloc( NUM_PRODUCTS * NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int) );
    unsigned int *best_buy_options = (unsigned int *) malloc( NUM_PRODUCTS * ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int) );
    unsigned int *seq_best_buy_options = (unsigned int *) malloc( NUM_PRODUCTS * ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int) );
    unsigned int *device_all_products_buy_options;
    unsigned int *device_best_buy_options;

    // Metadata
    unsigned int buy_option_size = ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int);
    unsigned int num_total_buy_options = NUM_PRODUCTS * NUM_BUY_OPTIONS;
    unsigned int total_buy_options_size = num_total_buy_options * buy_option_size;
    unsigned int best_buy_options_size = NUM_PRODUCTS * buy_option_size;

    // Benchmarking
    float elapsed_time;
    hipEvent_t start;
    hipEvent_t stop;
    float seq_elapsed_time;
    hipEvent_t seq_start;
    hipEvent_t seq_stop;

    initAllProductsBuyOptions(host_all_products_buy_options);

    hipEventCreate(&start);
    hipEventCreate(&stop);
    hipEventCreate(&seq_start);
    hipEventCreate(&seq_stop);

    // Obtener Memoria en el device
    hipMalloc( (unsigned int**) &device_all_products_buy_options, total_buy_options_size );
    hipMalloc( (unsigned int**) &device_best_buy_options, best_buy_options_size );

    // Copiar datos desde el host en el device
    hipMemcpy(device_all_products_buy_options, host_all_products_buy_options, total_buy_options_size, hipMemcpyHostToDevice);

    hipEventRecord(start, 0);

    // Ejecutar el kernel (número de bloques = número de productos). Un bloque por cada producto y todos los threads por cada bloque.
    KernelMarketplace<<<NUM_PRODUCTS, NUM_THREADS>>>(device_all_products_buy_options, device_best_buy_options);

    // Obtener el resultado parcial desde el host
    hipMemcpy(best_buy_options, device_best_buy_options, best_buy_options_size, hipMemcpyDeviceToHost);

    hipEventRecord(stop, 0);
    hipEventSynchronize(stop);

    // Liberar Memoria del device
    hipFree(device_all_products_buy_options);
    hipFree(device_best_buy_options);

    hipEventElapsedTime(&elapsed_time, start, stop);

    printf("\nKERNEL MARKETPLACE\n");
    printf("Number buy options per product: %d\n", NUM_BUY_OPTIONS);
    printf("Number of blocks (products): %d\n", NUM_PRODUCTS);
    printf("Number of Threads: %d\n", NUM_THREADS);
    printf("Vector Size: %d\n", num_total_buy_options);
    printf("Total time %4.6f milseg\n", elapsed_time);
    printf("Bandwidth %4.3f GB/s\n", (num_total_buy_options * ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int)) / (1000000 * elapsed_time));

    hipEventDestroy(start);
    hipEventDestroy(stop);

    if (DEBUG_LEVEL >= 2)
    {
        printAllProductsAllBuyOptions(host_all_products_buy_options);
    }

    if (DEBUG_LEVEL >= 1)
    {
        printBestBuyOptions(best_buy_options);
    }

    if ( areResultsValid( host_all_products_buy_options, best_buy_options ) )
    {
        printf ("TEST PASS\n");
    }
    else
    {
        printf ("TEST FAIL\n");
    }

    hipEventRecord(seq_start, 0);

    getBestBuyOptions(host_all_products_buy_options, seq_best_buy_options);

    hipEventRecord(seq_stop, 0);
    hipEventSynchronize(seq_stop);

    hipEventElapsedTime(&seq_elapsed_time, seq_start, seq_stop);

    printf("\nSEQUENCIAL\n");
    printf("Elapsed time %4.6f milseg\n", seq_elapsed_time);

    hipEventDestroy(seq_start);
    hipEventDestroy(seq_stop);
}

void initAllProductsBuyOptions(unsigned int *all_products_buy_options)
{
    for(unsigned int product_iteration = 0; product_iteration < NUM_PRODUCTS; ++product_iteration)
    {
        unsigned int current_product_position = product_iteration * NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION;

        for(unsigned int buy_option_iteration = 0; buy_option_iteration < NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION; buy_option_iteration += ELEMENTS_PER_BUY_OPTION)
        {
            unsigned int current_product_store_position = current_product_position + buy_option_iteration + STORE_ID_OFFSET;
            unsigned int current_product_price_position = current_product_position + buy_option_iteration + PRICE_OFFSET;

            // Set the current product buy option to the store with the same id as the current iteration in order to do not duplicate buy options.
            all_products_buy_options[current_product_store_position] = buy_option_iteration/2;

            // Set the price with a random value between 1 and 1000.
            all_products_buy_options[current_product_price_position] = rand() % 99999 + 1;
        }
    }
}

bool areResultsValid(unsigned int *all_products_buy_options, unsigned int *best_buy_options_by_device)
{
   unsigned int *best_buy_options_by_host = (unsigned int *) malloc( NUM_PRODUCTS * ELEMENTS_PER_BUY_OPTION * sizeof(unsigned int) );

   getBestBuyOptions(all_products_buy_options, best_buy_options_by_host);

   if (DEBUG_LEVEL >= 1)
   {
       printBestBuyOptions(best_buy_options_by_host);
   }

   for (unsigned int product_iteration = 0; product_iteration < NUM_PRODUCTS * ELEMENTS_PER_BUY_OPTION; product_iteration += ELEMENTS_PER_BUY_OPTION)
   {
       unsigned int current_product_store_position = product_iteration + STORE_ID_OFFSET;
       unsigned int current_product_price_position = product_iteration + PRICE_OFFSET;

       unsigned int best_store_by_device = best_buy_options_by_device[current_product_store_position];
       unsigned int best_price_by_device = best_buy_options_by_device[current_product_price_position];

       unsigned int best_store_by_host = best_buy_options_by_host[current_product_store_position];
       unsigned int best_price_by_host = best_buy_options_by_host[current_product_price_position];

       if (best_price_by_device != best_price_by_host)
       {
           if (DEBUG_LEVEL >= 1)
           {
               printf("FAILED IN product: %d\n", product_iteration);
               printf("\tbest_store_by_device: %d\n", best_store_by_device);
               printf("\tbest_store_by_host: %d\n", best_store_by_host);
               printf("\tbest_price_by_device: %d\n", best_price_by_device);
               printf("\tbest_price_by_host: %d\n", best_price_by_host);
           }

           return false;
       }
   }

   return true;
}

void printAllProductsAllBuyOptions(unsigned int *all_products_buy_options)
{
    cout << "All products buy options:" << endl;
    for (unsigned int i = 0; i < NUM_PRODUCTS; ++i){
        cout << endl << "\tproduct_id: " << i << endl;
        for (unsigned int j = 0; j < NUM_BUY_OPTIONS*2; j += 2){
            cout << "Buy option:" << endl;
            cout << "\tstore_id: " << all_products_buy_options[i*NUM_BUY_OPTIONS*2+j] << endl;
            cout << "\tprice: " << all_products_buy_options[i*NUM_BUY_OPTIONS*2+j+1] << endl;
        }
    }
}

void getBestBuyOptions(unsigned int *all_products_buy_options, unsigned int *best_buy_options)
{
    for(unsigned int product_iteration = 0; product_iteration < NUM_PRODUCTS; ++product_iteration)
    {
        unsigned int current_product_position = product_iteration * NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION;
        unsigned int current_product_store_position = current_product_position + STORE_ID_OFFSET;
        unsigned int current_product_price_position = current_product_position + PRICE_OFFSET;

        unsigned int best_store = all_products_buy_options[current_product_store_position];
        unsigned int best_price = all_products_buy_options[current_product_price_position];

        for(unsigned int product_to_compare = ELEMENTS_PER_BUY_OPTION; product_to_compare < NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION; product_to_compare += ELEMENTS_PER_BUY_OPTION)
        {
            if (all_products_buy_options[current_product_position + product_to_compare + PRICE_OFFSET] < best_price)
            {
               best_store = all_products_buy_options[product_iteration * NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION + product_to_compare + STORE_ID_OFFSET];
               best_price = all_products_buy_options[product_iteration * NUM_BUY_OPTIONS * ELEMENTS_PER_BUY_OPTION + product_to_compare + PRICE_OFFSET];
            }
        }
        best_buy_options[product_iteration * ELEMENTS_PER_BUY_OPTION + STORE_ID_OFFSET] = best_store;
        best_buy_options[product_iteration * ELEMENTS_PER_BUY_OPTION + PRICE_OFFSET] = best_price;
    }
}

void printBestBuyOptions(unsigned int *best_buy_options)
{
    cout << endl <<"Best products buy options:" << endl;
    for (unsigned int i = 0; i < NUM_PRODUCTS * ELEMENTS_PER_BUY_OPTION; i += ELEMENTS_PER_BUY_OPTION)
    {
        cout << "Best buy option for product_id: " << i / ELEMENTS_PER_BUY_OPTION << endl;
        cout << "\tstore_id: " << best_buy_options[i + STORE_ID_OFFSET] << endl;
        cout << "\tprice: " << best_buy_options[i + PRICE_OFFSET] << endl;
    }
}

	.text
	.file	"main.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z32__device_stub__KernelMarketplacePjS_ # -- Begin function _Z32__device_stub__KernelMarketplacePjS_
	.type	_Z32__device_stub__KernelMarketplacePjS_,@function
_Z32__device_stub__KernelMarketplacePjS_: # @_Z32__device_stub__KernelMarketplacePjS_
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.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	16(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%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	$_Z17KernelMarketplacePjS_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z32__device_stub__KernelMarketplacePjS_, .Lfunc_end0-_Z32__device_stub__KernelMarketplacePjS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x49742400                      # float 1.0E+6
.LCPI1_1:
	.long	0x4d6a6000                      # float 2.4576E+8
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.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	$64, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$245760000, %edi                # imm = 0xEA60000
	callq	malloc
	movq	%rax, %rbx
	movl	$240000, %edi                   # imm = 0x3A980
	callq	malloc
	movq	%rax, %r15
	movl	$240000, %edi                   # imm = 0x3A980
	callq	malloc
	movq	%rax, %r14
	movq	%rbx, %rdi
	callq	_Z25initAllProductsBuyOptionsPj
	leaq	40(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rdi
	callq	hipEventCreate
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	leaq	48(%rsp), %r13
	movl	$245760000, %esi                # imm = 0xEA60000
	movq	%r13, %rdi
	callq	hipMalloc
	leaq	32(%rsp), %rdi
	movl	$240000, %esi                   # imm = 0x3A980
	callq	hipMalloc
	movq	(%r13), %rdi
	movl	$245760000, %edx                # imm = 0xEA60000
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r12), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movabsq	$4294968320, %rdx               # imm = 0x100000400
	leaq	28976(%rdx), %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_2
# %bb.1:
	movq	48(%rsp), %rdi
	movq	32(%rsp), %rsi
	callq	_Z32__device_stub__KernelMarketplacePjS_
.LBB1_2:
	movq	32(%rsp), %rsi
	movl	$240000, %edx                   # imm = 0x3A980
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	16(%rsp), %rdi
	callq	hipEventSynchronize
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rsi
	movq	16(%rsp), %rdx
	leaq	60(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventElapsedTime
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.L.str.1, %edi
	movl	$1024, %esi                     # imm = 0x400
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.2, %edi
	movl	$30000, %esi                    # imm = 0x7530
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.3, %edi
	movl	$1024, %esi                     # imm = 0x400
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.4, %edi
	movl	$30720000, %esi                 # imm = 0x1D4C000
	xorl	%eax, %eax
	callq	printf
	cvtss2sd	(%r12), %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	movss	(%r12), %xmm0                   # xmm0 = mem[0],zero,zero,zero
	mulss	.LCPI1_0(%rip), %xmm0
	movss	.LCPI1_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	divss	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	movq	40(%rsp), %rdi
	callq	hipEventDestroy
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	movq	%rbx, %rdi
	movq	%r15, %rsi
	callq	_Z15areResultsValidPjS_
	movl	$.Lstr.2, %ecx
	movl	$.Lstr.1, %edi
	testb	%al, %al
	cmovneq	%rcx, %rdi
	callq	puts@PLT
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	_Z17getBestBuyOptionsPjS_
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	24(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	56(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx), %xmm0
	movl	$.L.str.10, %edi
	movb	$1, %al
	callq	printf
	movq	24(%rsp), %rdi
	callq	hipEventDestroy
	movq	8(%rsp), %rdi
	callq	hipEventDestroy
	xorl	%eax, %eax
	addq	$64, %rsp
	.cfi_def_cfa_offset 48
	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_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z25initAllProductsBuyOptionsPj # -- Begin function _Z25initAllProductsBuyOptionsPj
	.type	_Z25initAllProductsBuyOptionsPj,@function
_Z25initAllProductsBuyOptionsPj:        # @_Z25initAllProductsBuyOptionsPj
	.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	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %rbx
	addq	$4, %rbx
	xorl	%r14d, %r14d
.LBB2_1:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_2 Depth 2
	movq	$-2, %r15
	xorl	%ebp, %ebp
.LBB2_2:                                #   Parent Loop BB2_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%ebp, 4(%rbx,%r15,4)
	callq	rand
	cltq
	imulq	$703694479, %rax, %rcx          # imm = 0x29F1868F
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$46, %rcx
	addl	%edx, %ecx
	imull	$99999, %ecx, %ecx              # imm = 0x1869F
	negl	%ecx
	addl	%ecx, %eax
	incl	%eax
	movl	%eax, 8(%rbx,%r15,4)
	addq	$2, %r15
	incl	%ebp
	cmpq	$2046, %r15                     # imm = 0x7FE
	jb	.LBB2_2
# %bb.3:                                #   in Loop: Header=BB2_1 Depth=1
	incq	%r14
	addq	$8192, %rbx                     # imm = 0x2000
	cmpq	$30000, %r14                    # imm = 0x7530
	jne	.LBB2_1
# %bb.4:
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.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_end2:
	.size	_Z25initAllProductsBuyOptionsPj, .Lfunc_end2-_Z25initAllProductsBuyOptionsPj
	.cfi_endproc
                                        # -- End function
	.globl	_Z15areResultsValidPjS_         # -- Begin function _Z15areResultsValidPjS_
	.type	_Z15areResultsValidPjS_,@function
_Z15areResultsValidPjS_:                # @_Z15areResultsValidPjS_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	movq	%rdi, %r15
	movl	$240000, %edi                   # imm = 0x3A980
	callq	malloc
	movq	%rax, %r14
	movq	%r15, %rdi
	movq	%rax, %rsi
	callq	_Z17getBestBuyOptionsPjS_
	xorl	%eax, %eax
.LBB3_1:                                # =>This Inner Loop Header: Depth=1
	movl	4(%rbx,%rax,4), %ecx
	movl	4(%r14,%rax,4), %edx
	cmpq	$59997, %rax                    # imm = 0xEA5D
	ja	.LBB3_3
# %bb.2:                                #   in Loop: Header=BB3_1 Depth=1
	addq	$2, %rax
	cmpl	%edx, %ecx
	je	.LBB3_1
.LBB3_3:                                # %.critedge
	cmpl	%edx, %ecx
	sete	%al
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z15areResultsValidPjS_, .Lfunc_end3-_Z15areResultsValidPjS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z17getBestBuyOptionsPjS_       # -- Begin function _Z17getBestBuyOptionsPjS_
	.type	_Z17getBestBuyOptionsPjS_,@function
_Z17getBestBuyOptionsPjS_:              # @_Z17getBestBuyOptionsPjS_
	.cfi_startproc
# %bb.0:
	leaq	12(%rdi), %rax
	xorl	%ecx, %ecx
.LBB4_1:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_2 Depth 2
	movq	%rcx, %r8
	shlq	$13, %r8
	movl	(%rdi,%r8), %edx
	movl	4(%rdi,%r8), %r8d
	xorl	%r9d, %r9d
.LBB4_2:                                #   Parent Loop BB4_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%rax,%r9,4), %r10d
	cmpl	%r8d, %r10d
	jae	.LBB4_4
# %bb.3:                                #   in Loop: Header=BB4_2 Depth=2
	movl	-4(%rax,%r9,4), %edx
	movl	%r10d, %r8d
.LBB4_4:                                #   in Loop: Header=BB4_2 Depth=2
	addq	$2, %r9
	cmpq	$2046, %r9                      # imm = 0x7FE
	jb	.LBB4_2
# %bb.5:                                #   in Loop: Header=BB4_1 Depth=1
	movl	%edx, (%rsi,%rcx,8)
	movl	%r8d, 4(%rsi,%rcx,8)
	incq	%rcx
	addq	$8192, %rax                     # imm = 0x2000
	cmpq	$30000, %rcx                    # imm = 0x7530
	jne	.LBB4_1
# %bb.6:
	retq
.Lfunc_end4:
	.size	_Z17getBestBuyOptionsPjS_, .Lfunc_end4-_Z17getBestBuyOptionsPjS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z29printAllProductsAllBuyOptionsPj # -- Begin function _Z29printAllProductsAllBuyOptionsPj
	.type	_Z29printAllProductsAllBuyOptionsPj,@function
_Z29printAllProductsAllBuyOptionsPj:    # @_Z29printAllProductsAllBuyOptionsPj
	.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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	$_ZSt4cout, %r12d
	movl	$_ZSt4cout, %edi
	movl	$.L.str.11, %esi
	movl	$25, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r12, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	addq	$4, %rbx
	xorl	%r14d, %r14d
.LBB5_1:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB5_2 Depth 2
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r12, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	%rax, %r15
	movl	$.L.str.12, %esi
	movl	$13, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	%r15, %rdi
	movq	%r14, %rsi
	callq	_ZNSo9_M_insertImEERSoT_
	movq	%rax, %r15
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%r15, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r15, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	$-2, %r13
.LBB5_2:                                #   Parent Loop BB5_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	$_ZSt4cout, %edi
	movl	$.L.str.13, %esi
	movl	$11, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r12, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$_ZSt4cout, %edi
	movl	$.L.str.14, %esi
	movl	$11, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	4(%rbx,%r13,4), %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertImEERSoT_
	movq	%rax, %r15
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%r15, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r15, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$_ZSt4cout, %edi
	movl	$.L.str.15, %esi
	movl	$8, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	8(%rbx,%r13,4), %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertImEERSoT_
	movq	%rax, %r15
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%r15, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r15, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	addq	$2, %r13
	cmpq	$2046, %r13                     # imm = 0x7FE
	jb	.LBB5_2
# %bb.3:                                #   in Loop: Header=BB5_1 Depth=1
	incq	%r14
	addq	$8192, %rbx                     # imm = 0x2000
	cmpq	$30000, %r14                    # imm = 0x7530
	jne	.LBB5_1
# %bb.4:
	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_end5:
	.size	_Z29printAllProductsAllBuyOptionsPj, .Lfunc_end5-_Z29printAllProductsAllBuyOptionsPj
	.cfi_endproc
                                        # -- End function
	.globl	_Z19printBestBuyOptionsPj       # -- Begin function _Z19printBestBuyOptionsPj
	.type	_Z19printBestBuyOptionsPj,@function
_Z19printBestBuyOptionsPj:              # @_Z19printBestBuyOptionsPj
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movq	_ZSt4cout(%rip), %rax
	movl	$_ZSt4cout, %ecx
	movq	-24(%rax), %rdi
	addq	%rcx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	%rax, %r14
	movl	$.L.str.16, %esi
	movl	$26, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	(%r14), %rax
	movq	-24(%rax), %rdi
	addq	%r14, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r14, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	$-2, %r12
	xorl	%r14d, %r14d
.LBB6_1:                                # =>This Inner Loop Header: Depth=1
	movl	$_ZSt4cout, %edi
	movl	$.L.str.17, %esi
	movl	$32, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$_ZSt4cout, %edi
	movq	%r14, %rsi
	callq	_ZNSo9_M_insertImEERSoT_
	movq	%rax, %r15
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%r15, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r15, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$_ZSt4cout, %edi
	movl	$.L.str.14, %esi
	movl	$11, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	8(%rbx,%r12,4), %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertImEERSoT_
	movq	%rax, %r15
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%r15, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r15, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$_ZSt4cout, %edi
	movl	$.L.str.15, %esi
	movl	$8, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	12(%rbx,%r12,4), %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertImEERSoT_
	movq	%rax, %r15
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%r15, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r15, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	addq	$2, %r12
	incq	%r14
	cmpq	$59998, %r12                    # imm = 0xEA5E
	jb	.LBB6_1
# %bb.2:
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end6:
	.size	_Z19printBestBuyOptionsPj, .Lfunc_end6-_Z19printBestBuyOptionsPj
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB7_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB7_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z17KernelMarketplacePjS_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end7:
	.size	__hip_module_ctor, .Lfunc_end7-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB8_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB8_2:
	retq
.Lfunc_end8:
	.size	__hip_module_dtor, .Lfunc_end8-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z17KernelMarketplacePjS_,@object # @_Z17KernelMarketplacePjS_
	.section	.rodata,"a",@progbits
	.globl	_Z17KernelMarketplacePjS_
	.p2align	3, 0x0
_Z17KernelMarketplacePjS_:
	.quad	_Z32__device_stub__KernelMarketplacePjS_
	.size	_Z17KernelMarketplacePjS_, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"Number buy options per product: %d\n"
	.size	.L.str.1, 36

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Number of blocks (products): %d\n"
	.size	.L.str.2, 33

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Number of Threads: %d\n"
	.size	.L.str.3, 23

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Total time %4.6f milseg\n"
	.size	.L.str.5, 25

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Bandwidth %4.3f GB/s\n"
	.size	.L.str.6, 22

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Elapsed time %4.6f milseg\n"
	.size	.L.str.10, 27

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"All products buy options:"
	.size	.L.str.11, 26

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"\tproduct_id: "
	.size	.L.str.12, 14

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Buy option:"
	.size	.L.str.13, 12

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"\tstore_id: "
	.size	.L.str.14, 12

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"\tprice: "
	.size	.L.str.15, 9

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"Best products buy options:"
	.size	.L.str.16, 27

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"Best buy option for product_id: "
	.size	.L.str.17, 33

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z17KernelMarketplacePjS_"
	.size	.L__unnamed_1, 26

	.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	"\nKERNEL MARKETPLACE"
	.size	.Lstr, 20

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"TEST FAIL"
	.size	.Lstr.1, 10

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"TEST PASS"
	.size	.Lstr.2, 10

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"\nSEQUENCIAL"
	.size	.Lstr.3, 12

	.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 _Z32__device_stub__KernelMarketplacePjS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z17KernelMarketplacePjS_
	.addrsig_sym _ZSt4cout
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z17KernelMarketplacePjS_ ; -- Begin function _Z17KernelMarketplacePjS_
	.globl	_Z17KernelMarketplacePjS_
	.p2align	8
	.type	_Z17KernelMarketplacePjS_,@function
_Z17KernelMarketplacePjS_:              ; @_Z17KernelMarketplacePjS_
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x1c
	s_load_b128 s[0:3], s[0:1], 0x0
	v_mov_b32_e32 v2, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	s_mul_i32 s5, s15, s4
	s_cmp_lt_u32 s4, 2
	v_add_lshl_u32 v1, s5, v0, 1
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v1, vcc_lo, s0, v1
	v_add_co_ci_u32_e32 v2, vcc_lo, s1, v2, vcc_lo
	s_mov_b32 s0, 2
	global_load_b64 v[2:3], v[1:2], off
	v_lshlrev_b32_e32 v1, 3, v0
	s_waitcnt vmcnt(0)
	ds_store_b64 v1, v[2:3]
	s_waitcnt vmcnt(0) lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
.LBB0_1:                                ; =>This Inner Loop Header: Depth=1
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_6
; %bb.2:                                ; %.lr.ph
                                        ;   in Loop: Header=BB0_1 Depth=1
	s_add_i32 s1, s0, -1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_and_b32_e32 v2, s1, v0
	s_mov_b32 s1, exec_lo
	v_cmpx_eq_u32_e32 0, v2
	s_cbranch_execz .LBB0_5
; %bb.3:                                ;   in Loop: Header=BB0_1 Depth=1
	v_lshl_add_u32 v2, s0, 2, v1
	ds_load_b32 v3, v1 offset:4
	ds_load_b32 v4, v2 offset:4
	s_waitcnt lgkmcnt(0)
	v_cmp_gt_u32_e32 vcc_lo, v3, v4
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_5
; %bb.4:                                ;   in Loop: Header=BB0_1 Depth=1
	ds_load_b32 v3, v2
	s_waitcnt lgkmcnt(0)
	ds_store_b32 v1, v3
	ds_load_b32 v2, v2 offset:4
	s_waitcnt lgkmcnt(0)
	ds_store_b32 v1, v2 offset:4
.LBB0_5:                                ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s1
	s_lshl_b32 s0, s0, 1
	s_waitcnt lgkmcnt(0)
	s_cmp_gt_u32 s0, s4
	s_branch .LBB0_1
.LBB0_6:                                ; %._crit_edge
	s_mov_b32 s1, 0
	s_mov_b32 s0, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_8
; %bb.7:
	v_mov_b32_e32 v2, 0
	s_lshl_b32 s0, s15, 1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[0:1], s[0:1], 2
	s_add_u32 s0, s2, s0
	ds_load_b64 v[0:1], v2
	s_addc_u32 s1, s3, s1
	s_waitcnt lgkmcnt(0)
	global_store_b64 v2, v[0:1], s[0:1]
.LBB0_8:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z17KernelMarketplacePjS_
		.amdhsa_group_segment_fixed_size 8192
		.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 5
		.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	_Z17KernelMarketplacePjS_, .Lfunc_end0-_Z17KernelMarketplacePjS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 332
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 8192 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; 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
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .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: 8192
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z17KernelMarketplacePjS_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z17KernelMarketplacePjS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
