/**
 * Copyright 1993-2015 NVIDIA Corporation.  All rights reserved.
 *
 * Please refer to the NVIDIA end user license agreement (EULA) associated
 * with this source code for terms and conditions that govern your use of
 * this software. Any use, reproduction, disclosure, or distribution of
 * this software and related documentation outside the terms of the EULA
 * is strictly prohibited.
 *
 */


// System includes
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

#define NUM_VERTICES 1024

#define SEQUENTIAL 0

#define POS(i, j) (((j) * NUM_VERTICES) + i)

#define PRINT_EDGES 0

#define PSEUDO_RANDOM_SIZE 1024

#define BLOCK_SIZE 32

#define DEBUG 1

// CUDA API error checking macro
static void handleError(cudaError_t err,
                        const char *file,
                        int line ) {
    if (err != cudaSuccess) {
        printf("%s in %s at line %d\n", cudaGetErrorString(err),
               file, line );
        exit(EXIT_FAILURE);
    }
}
#define cudaCheck( err ) (handleError(err, __FILE__, __LINE__ ))

int sampleBinomialDistribution(double succesProbability) {
    return (int)((double)rand() / (double)RAND_MAX < succesProbability);
}

void printArray(int arr[]) {
    printf("Array(");
    for (int i = 0; i < NUM_VERTICES; i++) {
        if (arr[i] > 0) {
            printf("%d:%d, ", i, arr[i]);
        }
    }
    printf(")\n");
}

void printEdges(int *adjacencyMatrix) {
    printf("Edges: \n");
    for (int i = 0; i < NUM_VERTICES; i++) {
        for (int j = 0; j < NUM_VERTICES; j++) {
            if (adjacencyMatrix[POS(i, j)] == 1) {
                printf("\t(%d, %d)\n", i, j);
            }
        }
    }
}

void star(int *adjacencyMatrix) {
    memset(adjacencyMatrix, 0, NUM_VERTICES * NUM_VERTICES);
    for (int j = 1; j < NUM_VERTICES; j++) {
        adjacencyMatrix[POS(0, j)] = 1;
        adjacencyMatrix[POS(j, 0)] = 1;
    }
}

void cycle(int *adjacencyMatrix) {
    memset(adjacencyMatrix, 0, NUM_VERTICES * NUM_VERTICES);
    for (int i = 0; i < NUM_VERTICES; i++) {
        adjacencyMatrix[POS(i, (i + 1) % NUM_VERTICES)] = 1;
        adjacencyMatrix[POS(i, (i + NUM_VERTICES - 1) % NUM_VERTICES)] = 1;
    }
}

// Skewed random graph
void randomGraph(int *adjacencyMatrix)
{
    memset(adjacencyMatrix, 0, NUM_VERTICES * NUM_VERTICES);
    int edge;
    for (int i = 0; i < NUM_VERTICES; i++) {
        for (int j = i + 1; j < NUM_VERTICES; j++) {
            double probability = (((double) i) + j) / (4 * NUM_VERTICES);
            edge = sampleBinomialDistribution(probability);
            adjacencyMatrix[POS(i, j)] = edge;
            adjacencyMatrix[POS(j, i)] = edge;
        }
    }
}

void fillPseudoRandoms(float pseudoRandoms[]) {
    for (int i = 0; i < PSEUDO_RANDOM_SIZE; i++) {
        pseudoRandoms[i] = 1. * (i % 3) / 3.;
    }
}

bool verifyMaximalIndependentSet(int *adjacencyMatrix, int* independentSet) {
    for (int i = 0; i < NUM_VERTICES; i++) {
        int neighborsInV = 0;
        for (int j = 0; j < NUM_VERTICES; j++) {
            if (i != j && adjacencyMatrix[POS(i, j)] == 1 && independentSet[j] == 1) {
                if (independentSet[i]) {
                    printf("Set is not independent as it contains neighbors %d and %d", i, j);
                    return false;
                }
              neighborsInV++;
            }
        }
        if (independentSet[i] == 0 && neighborsInV == 0) {
            printf("Set is not maximal as %d (not in set) has no neighbors in set\n", i);
            return false;
        }
    }
    return true;
}

void lfIndependentSet(int *adjacencyMatrix, int* independentSet) {
    memset(independentSet, 0, NUM_VERTICES);

    for (int i = 0; i < NUM_VERTICES; i++) {
        bool hasNeighborsInV = false;
        for (int j = 0; j < i; j++) {
            if (adjacencyMatrix[POS(i, j)] == 1 && independentSet[j] == 1) {
                hasNeighborsInV = true;
            }
        }
        if (!hasNeighborsInV) {
            independentSet[i] = 1;
        }
    }
}

int main()
{
    srand(1);

    // Allocate host memory for matrices A, B and C
    size_t adjacencyMatrix_mem_size = sizeof(int) * NUM_VERTICES * NUM_VERTICES;
    int *adjacencyMatrix = (int *) malloc(adjacencyMatrix_mem_size);
    int *independentSet = (int *) malloc(NUM_VERTICES * sizeof(int));

    if (adjacencyMatrix == NULL || independentSet == NULL)
    {
        fprintf(stderr, "Failed to allocate host matrices!\n");
        exit(EXIT_FAILURE);
    }

    //randomGraph(adjacencyMatrix);
    randomGraph(adjacencyMatrix);

    if (SEQUENTIAL) {
        lfIndependentSet(adjacencyMatrix, independentSet);
    } else {
        printf("Computing result using CUDA Kernel...\n");
        
        float randoms[PSEUDO_RANDOM_SIZE];
        fillPseudoRandoms(randoms);

        // Allocate device memory
        int *d_adjacencyMatrix, *d_degrees, *d_independentSet, *d_marked, *d_removedNodes,
            *d_existsNonRemoved;

        float *d_randoms;

        /** Feel free to use other structures! **/
        cudaCheck(cudaMalloc((void **) &d_adjacencyMatrix, adjacencyMatrix_mem_size));
        cudaCheck(cudaMalloc((void **) &d_degrees, sizeof(int) * NUM_VERTICES));
        cudaCheck(cudaMalloc((void **) &d_independentSet, sizeof(int) * NUM_VERTICES));
        cudaCheck(cudaMalloc((void **) &d_marked, sizeof(int) * NUM_VERTICES));
        cudaCheck(cudaMalloc((void **) &d_removedNodes, sizeof(int) * NUM_VERTICES));
        cudaCheck(cudaMalloc((void **) &d_existsNonRemoved, sizeof(int)));

        cudaCheck(cudaMalloc((void **) &d_randoms, sizeof(float) * PSEUDO_RANDOM_SIZE));

        // copy host memory to device
        cudaCheck(cudaMemcpy(d_adjacencyMatrix, adjacencyMatrix, 
            adjacencyMatrix_mem_size, cudaMemcpyHostToDevice));

        cudaCheck(cudaMemcpy(d_randoms, &randoms, 
            sizeof(float) * PSEUDO_RANDOM_SIZE, cudaMemcpyHostToDevice));

        /*
           Run your kernel(s) here
        */
        
        // Copy result from device to host
        cudaCheck(cudaMemcpy(independentSet, d_independentSet, NUM_VERTICES * sizeof(int), 
            cudaMemcpyDeviceToHost));

        cudaFree(d_adjacencyMatrix);
        cudaFree(d_degrees);
        cudaFree(d_marked);
        cudaFree(d_independentSet);
        cudaFree(d_removedNodes);
    }


    printf("Checking computed result for correctness: ");
    
    if (PRINT_EDGES) printEdges(adjacencyMatrix);
    printArray(independentSet);

    bool correct = verifyMaximalIndependentSet(adjacencyMatrix, independentSet);

    printf("%s\n", correct ? "Result = PASS" : "Result = FAIL");

    // Clean up memory
    free(adjacencyMatrix);
    free(independentSet);

    if (correct) {
        return EXIT_SUCCESS;
    } else {
        return EXIT_FAILURE;
    }
}

	.file	"tmpxft_002c4703_00000000-6_independentSet.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2040:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2040:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/kasiaja/pwir-cuda-labs/master/lab3/independentSet.cu"
.LC1:
	.string	"%s in %s at line %d\n"
	.text
	.type	_ZL11handleError9cudaErrorPKci.constprop.0, @function
_ZL11handleError9cudaErrorPKci.constprop.0:
.LFB2179:
	.cfi_startproc
	testl	%edi, %edi
	je	.L2
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	%esi, 12(%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r8d
	movl	$2, %edi
	leaq	.LC0(%rip), %rcx
	movq	%rax, %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2179:
	.size	_ZL11handleError9cudaErrorPKci.constprop.0, .-_ZL11handleError9cudaErrorPKci.constprop.0
	.globl	_Z26sampleBinomialDistributiond
	.type	_Z26sampleBinomialDistributiond, @function
_Z26sampleBinomialDistributiond:
.LFB2028:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movsd	%xmm0, 8(%rsp)
	call	rand@PLT
	movsd	8(%rsp), %xmm0
	cvtsi2sdl	%eax, %xmm1
	xorl	%eax, %eax
	divsd	.LC2(%rip), %xmm1
	comisd	%xmm1, %xmm0
	seta	%al
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z26sampleBinomialDistributiond, .-_Z26sampleBinomialDistributiond
	.section	.rodata.str1.1
.LC3:
	.string	"Array("
.LC4:
	.string	"%d:%d, "
.LC5:
	.string	")\n"
	.text
	.globl	_Z10printArrayPi
	.type	_Z10printArrayPi, @function
_Z10printArrayPi:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	leaq	.LC4(%rip), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rdi, %rbp
	movl	$2, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%ebx, %ebx
	call	__printf_chk@PLT
.L12:
	movl	0(%rbp,%rbx,4), %ecx
	testl	%ecx, %ecx
	jle	.L11
	movl	%ebx, %edx
	movq	%r12, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L11:
	incq	%rbx
	cmpq	$1024, %rbx
	jne	.L12
	popq	%rbx
	.cfi_def_cfa_offset 24
	leaq	.LC5(%rip), %rsi
	popq	%rbp
	.cfi_def_cfa_offset 16
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z10printArrayPi, .-_Z10printArrayPi
	.section	.rodata.str1.1
.LC6:
	.string	"Edges: \n"
.LC7:
	.string	"\t(%d, %d)\n"
	.text
	.globl	_Z10printEdgesPi
	.type	_Z10printEdgesPi, @function
_Z10printEdgesPi:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC6(%rip), %rsi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdi, %r13
	movl	$2, %edi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 64
	call	__printf_chk@PLT
.L16:
	movl	%ebx, %r14d
	leaq	0(%r13,%rbx,4), %r12
	xorl	%ebp, %ebp
.L18:
	cmpl	$1, (%r12)
	jne	.L17
	movl	%ebp, %ecx
	movl	%r14d, %edx
	movq	%r15, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L17:
	incl	%ebp
	addq	$4096, %r12
	cmpl	$1024, %ebp
	jne	.L18
	incq	%rbx
	cmpq	$1024, %rbx
	jne	.L16
	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
.LFE2030:
	.size	_Z10printEdgesPi, .-_Z10printEdgesPi
	.globl	_Z4starPi
	.type	_Z4starPi, @function
_Z4starPi:
.LFB2031:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	movl	$262144, %ecx
	movq	%rdi, %rdx
	rep stosl
	movl	$1, %eax
.L23:
	movq	%rax, %rcx
	salq	$12, %rcx
	movl	$1, (%rdx,%rcx)
	movl	$1, (%rdx,%rax,4)
	incq	%rax
	cmpq	$1024, %rax
	jne	.L23
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z4starPi, .-_Z4starPi
	.globl	_Z5cyclePi
	.type	_Z5cyclePi, @function
_Z5cyclePi:
.LFB2032:
	.cfi_startproc
	endbr64
	movl	$262144, %ecx
	xorl	%eax, %eax
	movq	%rdi, %rdx
	rep stosl
.L26:
	movl	%ecx, %esi
	incl	%ecx
	movl	%ecx, %eax
	andl	$1023, %eax
	sall	$10, %eax
	addl	%esi, %eax
	cltq
	movl	$1, (%rdx,%rax,4)
	leal	1023(%rsi), %eax
	andl	$1023, %eax
	sall	$10, %eax
	addl	%esi, %eax
	cltq
	movl	$1, (%rdx,%rax,4)
	cmpl	$1024, %ecx
	jne	.L26
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z5cyclePi, .-_Z5cyclePi
	.globl	_Z11randomGraphPi
	.type	_Z11randomGraphPi, @function
_Z11randomGraphPi:
.LFB2033:
	.cfi_startproc
	endbr64
	movl	$262144, %ecx
	xorl	%eax, %eax
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 64
	rep stosl
.L31:
	movq	%rbx, %r13
	movl	%ebx, %r14d
	incq	%rbx
	leaq	0(%rbp,%r13,4), %r15
	salq	$12, %r13
	movq	%rbx, %r12
	addq	%rbp, %r13
.L29:
	cmpl	$1024, %r12d
	je	.L34
	cvtsi2sdl	%r12d, %xmm1
	cvtsi2sdl	%r14d, %xmm0
	addsd	%xmm1, %xmm0
	mulsd	.LC8(%rip), %xmm0
	call	_Z26sampleBinomialDistributiond
	movq	%r12, %rdx
	salq	$12, %rdx
	movl	%eax, (%r15,%rdx)
	movl	%eax, 0(%r13,%r12,4)
	incq	%r12
	jmp	.L29
.L34:
	cmpq	$1024, %rbx
	jne	.L31
	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
.LFE2033:
	.size	_Z11randomGraphPi, .-_Z11randomGraphPi
	.globl	_Z17fillPseudoRandomsPf
	.type	_Z17fillPseudoRandomsPf, @function
_Z17fillPseudoRandomsPf:
.LFB2034:
	.cfi_startproc
	endbr64
	movsd	.LC9(%rip), %xmm1
	xorl	%ecx, %ecx
	movl	$3, %esi
.L36:
	movl	%ecx, %eax
	cltd
	idivl	%esi
	cvtsi2sdl	%edx, %xmm0
	divsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rdi,%rcx,4)
	incq	%rcx
	cmpq	$1024, %rcx
	jne	.L36
	ret
	.cfi_endproc
.LFE2034:
	.size	_Z17fillPseudoRandomsPf, .-_Z17fillPseudoRandomsPf
	.section	.rodata.str1.1
.LC10:
	.string	"Set is not independent as it contains neighbors %d and %d"
.LC11:
	.string	"Set is not maximal as %d (not in set) has no neighbors in set\n"
	.text
	.globl	_Z27verifyMaximalIndependentSetPiS_
	.type	_Z27verifyMaximalIndependentSetPiS_, @function
_Z27verifyMaximalIndependentSetPiS_:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	xorl	%edx, %edx
.L39:
	movl	(%rsi,%rdx,4), %r8d
	movl	%edx, %r10d
	movl	%edx, %r9d
	xorl	%eax, %eax
	xorl	%ecx, %ecx
.L43:
	cmpl	%edx, %eax
	je	.L40
	movl	%eax, %ebx
	sall	$10, %ebx
	addl	%r10d, %ebx
	movl	%ebx, %ebx
	cmpl	$1, (%rdi,%rbx,4)
	jne	.L40
	cmpl	$1, (%rsi,%rax,4)
	jne	.L40
	testl	%r8d, %r8d
	je	.L41
	movl	%eax, %ecx
	movl	%r9d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC10(%rip), %rsi
	call	__printf_chk@PLT
	jmp	.L42
.L41:
	incl	%ecx
.L40:
	incq	%rax
	cmpq	$1024, %rax
	jne	.L43
	orl	%r8d, %ecx
	jne	.L44
	movl	%r9d, %edx
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L42:
	xorl	%eax, %eax
	jmp	.L38
.L44:
	incq	%rdx
	cmpq	$1024, %rdx
	jne	.L39
	movb	$1, %al
.L38:
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2035:
	.size	_Z27verifyMaximalIndependentSetPiS_, .-_Z27verifyMaximalIndependentSetPiS_
	.globl	_Z16lfIndependentSetPiS_
	.type	_Z16lfIndependentSetPiS_, @function
_Z16lfIndependentSetPiS_:
.LFB2036:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	movq	%rdi, %r9
	movl	$256, %ecx
	movq	%rsi, %rdi
	rep stosl
	xorl	%eax, %eax
	movb	$1, %dil
.L50:
	leaq	(%r9,%rax,4), %r8
	xorl	%edx, %edx
	xorl	%ecx, %ecx
.L55:
	cmpl	%eax, %edx
	jge	.L57
	cmpl	$1, (%r8)
	jne	.L51
	cmpl	$1, (%rsi,%rdx,4)
	cmove	%edi, %ecx
.L51:
	incq	%rdx
	addq	$4096, %r8
	jmp	.L55
.L57:
	testb	%cl, %cl
	jne	.L53
	movl	$1, (%rsi,%rax,4)
.L53:
	incq	%rax
	cmpq	$1024, %rax
	jne	.L50
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z16lfIndependentSetPiS_, .-_Z16lfIndependentSetPiS_
	.section	.rodata.str1.1
.LC12:
	.string	"Result = PASS"
.LC13:
	.string	"Result = FAIL"
.LC14:
	.string	"Failed to allocate host matrices!\n"
.LC15:
	.string	"Computing result using CUDA Kernel...\n"
.LC16:
	.string	"Checking computed result for correctness: "
.LC17:
	.string	"%s\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2037:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$4096, %rsp
	.cfi_def_cfa_offset 4128
	orq	$0, (%rsp)
	subq	$64, %rsp
	.cfi_def_cfa_offset 4192
	movl	$1, %edi
	movq	%fs:40, %rax
	movq	%rax, 4152(%rsp)
	xorl	%eax, %eax
	call	srand@PLT
	movl	$4194304, %edi
	call	malloc@PLT
	movl	$4096, %edi
	movq	%rax, %r12
	call	malloc@PLT
	testq	%r12, %r12
	je	.L64
	movq	%rax, %rbp
	testq	%rax, %rax
	jne	.L59
.L64:
	movq	stderr(%rip), %rdi
	leaq	.LC14(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L59:
	movq	%r12, %rdi
	leaq	56(%rsp), %rbx
	call	_Z11randomGraphPi
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	call	_Z17fillPseudoRandomsPf
	movq	%rsp, %rdi
	movl	$4194304, %esi
	call	cudaMalloc@PLT
	movl	$175, %esi
	movl	%eax, %edi
	call	_ZL11handleError9cudaErrorPKci.constprop.0
	leaq	8(%rsp), %rdi
	movl	$4096, %esi
	call	cudaMalloc@PLT
	movl	$176, %esi
	movl	%eax, %edi
	call	_ZL11handleError9cudaErrorPKci.constprop.0
	leaq	16(%rsp), %rdi
	movl	$4096, %esi
	call	cudaMalloc@PLT
	movl	$177, %esi
	movl	%eax, %edi
	call	_ZL11handleError9cudaErrorPKci.constprop.0
	leaq	24(%rsp), %rdi
	movl	$4096, %esi
	call	cudaMalloc@PLT
	movl	$178, %esi
	movl	%eax, %edi
	call	_ZL11handleError9cudaErrorPKci.constprop.0
	leaq	32(%rsp), %rdi
	movl	$4096, %esi
	call	cudaMalloc@PLT
	movl	$179, %esi
	movl	%eax, %edi
	call	_ZL11handleError9cudaErrorPKci.constprop.0
	leaq	40(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	movl	$180, %esi
	movl	%eax, %edi
	call	_ZL11handleError9cudaErrorPKci.constprop.0
	leaq	48(%rsp), %rdi
	movl	$4096, %esi
	call	cudaMalloc@PLT
	movl	$182, %esi
	movl	%eax, %edi
	call	_ZL11handleError9cudaErrorPKci.constprop.0
	movq	(%rsp), %rdi
	movl	$1, %ecx
	movq	%r12, %rsi
	movl	$4194304, %edx
	call	cudaMemcpy@PLT
	movl	$185, %esi
	movl	%eax, %edi
	call	_ZL11handleError9cudaErrorPKci.constprop.0
	movq	48(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$4096, %edx
	call	cudaMemcpy@PLT
	movl	$188, %esi
	movl	%eax, %edi
	call	_ZL11handleError9cudaErrorPKci.constprop.0
	movq	16(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbp, %rdi
	movl	$4096, %edx
	call	cudaMemcpy@PLT
	movl	$196, %esi
	movl	%eax, %edi
	call	_ZL11handleError9cudaErrorPKci.constprop.0
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbp, %rdi
	call	_Z10printArrayPi
	movq	%rbp, %rsi
	movq	%r12, %rdi
	call	_Z27verifyMaximalIndependentSetPiS_
	leaq	.LC13(%rip), %rdx
	movl	$2, %edi
	leaq	.LC17(%rip), %rsi
	testb	%al, %al
	movl	%eax, %ebx
	leaq	.LC12(%rip), %rax
	cmovne	%rax, %rdx
	xorl	%eax, %eax
	xorl	$1, %ebx
	call	__printf_chk@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movzbl	%bl, %eax
	movq	4152(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L62
	call	__stack_chk_fail@PLT
.L62:
	addq	$4160, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2037:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2063:
	.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
.LFE2063:
	.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.cst8,"aM",@progbits,8
	.align 8
.LC2:
	.long	-4194304
	.long	1105199103
	.align 8
.LC8:
	.long	0
	.long	1060110336
	.align 8
.LC9:
	.long	0
	.long	1074266112
	.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 1993-2015 NVIDIA Corporation.  All rights reserved.
 *
 * Please refer to the NVIDIA end user license agreement (EULA) associated
 * with this source code for terms and conditions that govern your use of
 * this software. Any use, reproduction, disclosure, or distribution of
 * this software and related documentation outside the terms of the EULA
 * is strictly prohibited.
 *
 */


// System includes

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

#define NUM_VERTICES 1024

#define SEQUENTIAL 0

#define POS(i, j) (((j) * NUM_VERTICES) + i)

#define PRINT_EDGES 0

#define PSEUDO_RANDOM_SIZE 1024

#define BLOCK_SIZE 32

#define DEBUG 1

// CUDA API error checking macro
static void handleError(hipError_t err,
                        const char *file,
                        int line ) {
    if (err != hipSuccess) {
        printf("%s in %s at line %d\n", hipGetErrorString(err),
               file, line );
        exit(EXIT_FAILURE);
    }
}
#define cudaCheck( err ) (handleError(err, __FILE__, __LINE__ ))

int sampleBinomialDistribution(double succesProbability) {
    return (int)((double)rand() / (double)RAND_MAX < succesProbability);
}

void printArray(int arr[]) {
    printf("Array(");
    for (int i = 0; i < NUM_VERTICES; i++) {
        if (arr[i] > 0) {
            printf("%d:%d, ", i, arr[i]);
        }
    }
    printf(")\n");
}

void printEdges(int *adjacencyMatrix) {
    printf("Edges: \n");
    for (int i = 0; i < NUM_VERTICES; i++) {
        for (int j = 0; j < NUM_VERTICES; j++) {
            if (adjacencyMatrix[POS(i, j)] == 1) {
                printf("\t(%d, %d)\n", i, j);
            }
        }
    }
}

void star(int *adjacencyMatrix) {
    memset(adjacencyMatrix, 0, NUM_VERTICES * NUM_VERTICES);
    for (int j = 1; j < NUM_VERTICES; j++) {
        adjacencyMatrix[POS(0, j)] = 1;
        adjacencyMatrix[POS(j, 0)] = 1;
    }
}

void cycle(int *adjacencyMatrix) {
    memset(adjacencyMatrix, 0, NUM_VERTICES * NUM_VERTICES);
    for (int i = 0; i < NUM_VERTICES; i++) {
        adjacencyMatrix[POS(i, (i + 1) % NUM_VERTICES)] = 1;
        adjacencyMatrix[POS(i, (i + NUM_VERTICES - 1) % NUM_VERTICES)] = 1;
    }
}

// Skewed random graph
void randomGraph(int *adjacencyMatrix)
{
    memset(adjacencyMatrix, 0, NUM_VERTICES * NUM_VERTICES);
    int edge;
    for (int i = 0; i < NUM_VERTICES; i++) {
        for (int j = i + 1; j < NUM_VERTICES; j++) {
            double probability = (((double) i) + j) / (4 * NUM_VERTICES);
            edge = sampleBinomialDistribution(probability);
            adjacencyMatrix[POS(i, j)] = edge;
            adjacencyMatrix[POS(j, i)] = edge;
        }
    }
}

void fillPseudoRandoms(float pseudoRandoms[]) {
    for (int i = 0; i < PSEUDO_RANDOM_SIZE; i++) {
        pseudoRandoms[i] = 1. * (i % 3) / 3.;
    }
}

bool verifyMaximalIndependentSet(int *adjacencyMatrix, int* independentSet) {
    for (int i = 0; i < NUM_VERTICES; i++) {
        int neighborsInV = 0;
        for (int j = 0; j < NUM_VERTICES; j++) {
            if (i != j && adjacencyMatrix[POS(i, j)] == 1 && independentSet[j] == 1) {
                if (independentSet[i]) {
                    printf("Set is not independent as it contains neighbors %d and %d", i, j);
                    return false;
                }
              neighborsInV++;
            }
        }
        if (independentSet[i] == 0 && neighborsInV == 0) {
            printf("Set is not maximal as %d (not in set) has no neighbors in set\n", i);
            return false;
        }
    }
    return true;
}

void lfIndependentSet(int *adjacencyMatrix, int* independentSet) {
    memset(independentSet, 0, NUM_VERTICES);

    for (int i = 0; i < NUM_VERTICES; i++) {
        bool hasNeighborsInV = false;
        for (int j = 0; j < i; j++) {
            if (adjacencyMatrix[POS(i, j)] == 1 && independentSet[j] == 1) {
                hasNeighborsInV = true;
            }
        }
        if (!hasNeighborsInV) {
            independentSet[i] = 1;
        }
    }
}

int main()
{
    srand(1);

    // Allocate host memory for matrices A, B and C
    size_t adjacencyMatrix_mem_size = sizeof(int) * NUM_VERTICES * NUM_VERTICES;
    int *adjacencyMatrix = (int *) malloc(adjacencyMatrix_mem_size);
    int *independentSet = (int *) malloc(NUM_VERTICES * sizeof(int));

    if (adjacencyMatrix == NULL || independentSet == NULL)
    {
        fprintf(stderr, "Failed to allocate host matrices!\n");
        exit(EXIT_FAILURE);
    }

    //randomGraph(adjacencyMatrix);
    randomGraph(adjacencyMatrix);

    if (SEQUENTIAL) {
        lfIndependentSet(adjacencyMatrix, independentSet);
    } else {
        printf("Computing result using CUDA Kernel...\n");
        
        float randoms[PSEUDO_RANDOM_SIZE];
        fillPseudoRandoms(randoms);

        // Allocate device memory
        int *d_adjacencyMatrix, *d_degrees, *d_independentSet, *d_marked, *d_removedNodes,
            *d_existsNonRemoved;

        float *d_randoms;

        /** Feel free to use other structures! **/
        cudaCheck(hipMalloc((void **) &d_adjacencyMatrix, adjacencyMatrix_mem_size));
        cudaCheck(hipMalloc((void **) &d_degrees, sizeof(int) * NUM_VERTICES));
        cudaCheck(hipMalloc((void **) &d_independentSet, sizeof(int) * NUM_VERTICES));
        cudaCheck(hipMalloc((void **) &d_marked, sizeof(int) * NUM_VERTICES));
        cudaCheck(hipMalloc((void **) &d_removedNodes, sizeof(int) * NUM_VERTICES));
        cudaCheck(hipMalloc((void **) &d_existsNonRemoved, sizeof(int)));

        cudaCheck(hipMalloc((void **) &d_randoms, sizeof(float) * PSEUDO_RANDOM_SIZE));

        // copy host memory to device
        cudaCheck(hipMemcpy(d_adjacencyMatrix, adjacencyMatrix, 
            adjacencyMatrix_mem_size, hipMemcpyHostToDevice));

        cudaCheck(hipMemcpy(d_randoms, &randoms, 
            sizeof(float) * PSEUDO_RANDOM_SIZE, hipMemcpyHostToDevice));

        /*
           Run your kernel(s) here
        */
        
        // Copy result from device to host
        cudaCheck(hipMemcpy(independentSet, d_independentSet, NUM_VERTICES * sizeof(int), 
            hipMemcpyDeviceToHost));

        hipFree(d_adjacencyMatrix);
        hipFree(d_degrees);
        hipFree(d_marked);
        hipFree(d_independentSet);
        hipFree(d_removedNodes);
    }


    printf("Checking computed result for correctness: ");
    
    if (PRINT_EDGES) printEdges(adjacencyMatrix);
    printArray(independentSet);

    bool correct = verifyMaximalIndependentSet(adjacencyMatrix, independentSet);

    printf("%s\n", correct ? "Result = PASS" : "Result = FAIL");

    // Clean up memory
    free(adjacencyMatrix);
    free(independentSet);

    if (correct) {
        return EXIT_SUCCESS;
    } else {
        return EXIT_FAILURE;
    }
}

	.text
	.file	"independentSet.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z26sampleBinomialDistributiond
.LCPI0_0:
	.quad	0x41dfffffffc00000              # double 2147483647
	.text
	.globl	_Z26sampleBinomialDistributiond
	.type	_Z26sampleBinomialDistributiond,@function
_Z26sampleBinomialDistributiond:        # @_Z26sampleBinomialDistributiond
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI0_0(%rip), %xmm0
	xorl	%eax, %eax
	movsd	(%rsp), %xmm1                   # 8-byte Reload
                                        # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	seta	%al
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z26sampleBinomialDistributiond, .Lfunc_end0-_Z26sampleBinomialDistributiond
	.cfi_endproc
                                        # -- End function
	.globl	_Z10printArrayPi                # -- Begin function _Z10printArrayPi
	.type	_Z10printArrayPi,@function
_Z10printArrayPi:                       # @_Z10printArrayPi
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rdi, %rbx
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	xorl	%r14d, %r14d
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%r14,4), %edx
	testl	%edx, %edx
	jle	.LBB1_3
# %bb.2:                                #   in Loop: Header=BB1_1 Depth=1
	movl	$.L.str.1, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
.LBB1_3:                                #   in Loop: Header=BB1_1 Depth=1
	incq	%r14
	cmpq	$1024, %r14                     # imm = 0x400
	jne	.LBB1_1
# %bb.4:
	movl	$.Lstr, %edi
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	puts@PLT                        # TAILCALL
.Lfunc_end1:
	.size	_Z10printArrayPi, .Lfunc_end1-_Z10printArrayPi
	.cfi_endproc
                                        # -- End function
	.globl	_Z10printEdgesPi                # -- Begin function _Z10printEdgesPi
	.type	_Z10printEdgesPi,@function
_Z10printEdgesPi:                       # @_Z10printEdgesPi
	.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
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	xorl	%r14d, %r14d
.LBB2_1:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_2 Depth 2
	movq	%rbx, %r12
	xorl	%r15d, %r15d
.LBB2_2:                                #   Parent Loop BB2_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpl	$1, (%r12)
	jne	.LBB2_4
# %bb.3:                                #   in Loop: Header=BB2_2 Depth=2
	movl	$.L.str.4, %edi
	movl	%r14d, %esi
	movl	%r15d, %edx
	xorl	%eax, %eax
	callq	printf
.LBB2_4:                                #   in Loop: Header=BB2_2 Depth=2
	incq	%r15
	addq	$4096, %r12                     # imm = 0x1000
	cmpq	$1024, %r15                     # imm = 0x400
	jne	.LBB2_2
# %bb.5:                                #   in Loop: Header=BB2_1 Depth=1
	incq	%r14
	addq	$4, %rbx
	cmpq	$1024, %r14                     # imm = 0x400
	jne	.LBB2_1
# %bb.6:
	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_end2:
	.size	_Z10printEdgesPi, .Lfunc_end2-_Z10printEdgesPi
	.cfi_endproc
                                        # -- End function
	.globl	_Z4starPi                       # -- Begin function _Z4starPi
	.type	_Z4starPi,@function
_Z4starPi:                              # @_Z4starPi
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movl	$1048576, %edx                  # imm = 0x100000
	xorl	%esi, %esi
	callq	memset@PLT
	addq	$4096, %rbx                     # imm = 0x1000
	movq	$-4092, %rax                    # imm = 0xF004
	movl	$1, %ecx
	movq	%rbx, %rdx
.LBB3_1:                                # =>This Inner Loop Header: Depth=1
	movl	%ecx, (%rdx)
	movl	%ecx, (%rbx,%rax)
	addq	$4096, %rdx                     # imm = 0x1000
	addq	$4, %rax
	jne	.LBB3_1
# %bb.2:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z4starPi, .Lfunc_end3-_Z4starPi
	.cfi_endproc
                                        # -- End function
	.globl	_Z5cyclePi                      # -- Begin function _Z5cyclePi
	.type	_Z5cyclePi,@function
_Z5cyclePi:                             # @_Z5cyclePi
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rdi, %rbx
	xorl	%r14d, %r14d
	movl	$1048576, %edx                  # imm = 0x100000
	xorl	%esi, %esi
	callq	memset@PLT
	movl	$1047552, %eax                  # imm = 0xFFC00
	movw	$1, %cx
	movw	$1023, %dx                      # imm = 0x3FF
	movl	$1023, %esi                     # imm = 0x3FF
	movl	$1, %edi
.LBB4_1:                                # =>This Inner Loop Header: Depth=1
	movl	%ecx, %r8d
	andl	%esi, %r8d
	shll	$10, %r8d
	addl	%r14d, %r8d
	movl	%edx, %r9d
	andl	%esi, %r9d
	shll	$10, %r9d
	addl	%r14d, %r9d
	leal	1(%r14), %r10d
	movl	%edi, (%rbx,%r8,4)
	movl	%edi, (%rbx,%r9,4)
	addl	$1024, %eax                     # imm = 0x400
	incl	%ecx
	incl	%edx
	movl	%r10d, %r14d
	cmpl	$2096128, %eax                  # imm = 0x1FFC00
	jne	.LBB4_1
# %bb.2:
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z5cyclePi, .Lfunc_end4-_Z5cyclePi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z11randomGraphPi
.LCPI5_0:
	.quad	0x3f30000000000000              # double 2.44140625E-4
.LCPI5_1:
	.quad	0x41dfffffffc00000              # double 2147483647
	.text
	.globl	_Z11randomGraphPi
	.type	_Z11randomGraphPi,@function
_Z11randomGraphPi:                      # @_Z11randomGraphPi
	.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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %r12
	xorl	%r14d, %r14d
	movl	$1048576, %edx                  # imm = 0x100000
	xorl	%esi, %esi
	callq	memset@PLT
	leaq	4(%r12), %r15
	addq	$4096, %r12                     # imm = 0x1000
	movsd	.LCPI5_0(%rip), %xmm0           # xmm0 = mem[0],zero
	xorl	%r13d, %r13d
.LBB5_2:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB5_4 Depth 2
	cmpq	$1022, %r13                     # imm = 0x3FE
	ja	.LBB5_1
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB5_2 Depth=1
	movl	$1, %ebp
	movq	%r12, %rbx
.LBB5_4:                                #   Parent Loop BB5_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%r14,%rbp), %eax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%eax, %xmm1
	mulsd	%xmm0, %xmm1
	movsd	%xmm1, (%rsp)                   # 8-byte Spill
	callq	rand
	movsd	.LCPI5_1(%rip), %xmm1           # xmm1 = mem[0],zero
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	%xmm1, %xmm0
	xorl	%eax, %eax
	movsd	(%rsp), %xmm1                   # 8-byte Reload
                                        # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	movsd	.LCPI5_0(%rip), %xmm0           # xmm0 = mem[0],zero
	seta	%al
	movl	%eax, (%rbx)
	movl	%eax, -4(%r15,%rbp,4)
	addq	$4096, %rbx                     # imm = 0x1000
	leaq	1(,%rbp), %rax
	addq	%r13, %rax
	incq	%rbp
	cmpq	$1024, %rax                     # imm = 0x400
	jne	.LBB5_4
.LBB5_1:                                # %.loopexit
                                        #   in Loop: Header=BB5_2 Depth=1
	incq	%r13
	movl	$4100, %eax                     # imm = 0x1004
	addq	%rax, %r15
	addq	$2, %r14
	addq	%rax, %r12
	cmpq	$1024, %r13                     # imm = 0x400
	jne	.LBB5_2
# %bb.5:
	addq	$8, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	_Z11randomGraphPi, .Lfunc_end5-_Z11randomGraphPi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z17fillPseudoRandomsPf
.LCPI6_0:
	.quad	0x4008000000000000              # double 3
	.text
	.globl	_Z17fillPseudoRandomsPf
	.type	_Z17fillPseudoRandomsPf,@function
_Z17fillPseudoRandomsPf:                # @_Z17fillPseudoRandomsPf
	.cfi_startproc
# %bb.0:
	xorl	%eax, %eax
	movl	$65535, %ecx                    # imm = 0xFFFF
	movsd	.LCPI6_0(%rip), %xmm0           # xmm0 = mem[0],zero
.LBB6_1:                                # =>This Inner Loop Header: Depth=1
	movl	%eax, %edx
	andl	%ecx, %edx
	imull	$43691, %edx, %edx              # imm = 0xAAAB
	shrl	$17, %edx
	leal	(%rdx,%rdx,2), %edx
	movl	%eax, %esi
	subl	%edx, %esi
	andl	%ecx, %esi
	xorps	%xmm1, %xmm1
	cvtsi2sd	%esi, %xmm1
	divsd	%xmm0, %xmm1
	cvtsd2ss	%xmm1, %xmm1
	movss	%xmm1, (%rdi,%rax,4)
	incq	%rax
	cmpq	$1024, %rax                     # imm = 0x400
	jne	.LBB6_1
# %bb.2:
	retq
.Lfunc_end6:
	.size	_Z17fillPseudoRandomsPf, .Lfunc_end6-_Z17fillPseudoRandomsPf
	.cfi_endproc
                                        # -- End function
	.globl	_Z27verifyMaximalIndependentSetPiS_ # -- Begin function _Z27verifyMaximalIndependentSetPiS_
	.type	_Z27verifyMaximalIndependentSetPiS_,@function
_Z27verifyMaximalIndependentSetPiS_:    # @_Z27verifyMaximalIndependentSetPiS_
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rsi, %rax
	xorl	%esi, %esi
	xorl	%ebx, %ebx
.LBB7_1:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB7_2 Depth 2
	movq	%rdi, %r8
	xorl	%edx, %edx
	xorl	%ecx, %ecx
.LBB7_2:                                #   Parent Loop BB7_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpq	%rdx, %rsi
	je	.LBB7_7
# %bb.3:                                #   in Loop: Header=BB7_2 Depth=2
	cmpl	$1, (%r8)
	jne	.LBB7_7
# %bb.4:                                #   in Loop: Header=BB7_2 Depth=2
	cmpl	$1, (%rax,%rdx,4)
	jne	.LBB7_7
# %bb.5:                                #   in Loop: Header=BB7_2 Depth=2
	cmpl	$0, (%rax,%rsi,4)
	jne	.LBB7_12
# %bb.6:                                #   in Loop: Header=BB7_2 Depth=2
	incl	%ecx
.LBB7_7:                                #   in Loop: Header=BB7_2 Depth=2
	incq	%rdx
	addq	$4096, %r8                      # imm = 0x1000
	cmpq	$1024, %rdx                     # imm = 0x400
	jne	.LBB7_2
# %bb.8:                                #   in Loop: Header=BB7_1 Depth=1
	orl	(%rax,%rsi,4), %ecx
	je	.LBB7_9
# %bb.10:                               #   in Loop: Header=BB7_1 Depth=1
	cmpq	$1023, %rsi                     # imm = 0x3FF
	leaq	1(%rsi), %rcx
	setae	%bl
	addq	$4, %rdi
	movq	%rcx, %rsi
	cmpq	$1024, %rcx                     # imm = 0x400
	jne	.LBB7_1
	jmp	.LBB7_11
.LBB7_12:
	movl	$.L.str.5, %edi
                                        # kill: def $esi killed $esi killed $rsi
                                        # kill: def $edx killed $edx killed $rdx
	xorl	%eax, %eax
	callq	printf
.LBB7_11:                               # %.thread34
	andb	$1, %bl
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB7_9:
	.cfi_def_cfa_offset 16
	movl	$.L.str.6, %edi
                                        # kill: def $esi killed $esi killed $rsi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB7_11
.Lfunc_end7:
	.size	_Z27verifyMaximalIndependentSetPiS_, .Lfunc_end7-_Z27verifyMaximalIndependentSetPiS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z16lfIndependentSetPiS_        # -- Begin function _Z16lfIndependentSetPiS_
	.type	_Z16lfIndependentSetPiS_,@function
_Z16lfIndependentSetPiS_:               # @_Z16lfIndependentSetPiS_
	.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, %r14
	xorl	%r15d, %r15d
	movl	$1024, %edx                     # imm = 0x400
	movq	%rsi, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
.LBB8_1:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB8_3 Depth 2
	testq	%r15, %r15
	je	.LBB8_7
# %bb.2:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB8_1 Depth=1
	xorl	%eax, %eax
	movq	%r14, %rcx
	xorl	%edx, %edx
.LBB8_3:                                # %.lr.ph
                                        #   Parent Loop BB8_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpl	$1, (%rcx)
	jne	.LBB8_5
# %bb.4:                                #   in Loop: Header=BB8_3 Depth=2
	movl	(%rbx,%rax,4), %esi
	cmpl	$1, %esi
	movzbl	%dl, %edx
	cmovel	%esi, %edx
.LBB8_5:                                #   in Loop: Header=BB8_3 Depth=2
	incq	%rax
	addq	$4096, %rcx                     # imm = 0x1000
	cmpq	%rax, %r15
	jne	.LBB8_3
# %bb.6:                                # %._crit_edge
                                        #   in Loop: Header=BB8_1 Depth=1
	testb	$1, %dl
	jne	.LBB8_8
.LBB8_7:                                # %._crit_edge.thread
                                        #   in Loop: Header=BB8_1 Depth=1
	movl	$1, (%rbx,%r15,4)
.LBB8_8:                                #   in Loop: Header=BB8_1 Depth=1
	incq	%r15
	addq	$4, %r14
	cmpq	$1024, %r15                     # imm = 0x400
	jne	.LBB8_1
# %bb.9:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end8:
	.size	_Z16lfIndependentSetPiS_, .Lfunc_end8-_Z16lfIndependentSetPiS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI9_0:
	.quad	0x4008000000000000              # double 3
	.text
	.globl	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	$4168, %rsp                     # imm = 0x1048
	.cfi_def_cfa_offset 4224
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$1, %edi
	callq	srand
	movl	$4194304, %edi                  # imm = 0x400000
	callq	malloc
	movq	%rax, %rbx
	movl	$4096, %edi                     # imm = 0x1000
	callq	malloc
	testq	%rbx, %rbx
	je	.LBB9_5
# %bb.1:
	movq	%rax, %r14
	testq	%rax, %rax
	je	.LBB9_5
# %bb.2:
	movq	%rbx, %rdi
	callq	_Z11randomGraphPi
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	xorl	%eax, %eax
	movsd	.LCPI9_0(%rip), %xmm0           # xmm0 = mem[0],zero
	xorl	%ecx, %ecx
.LBB9_3:                                # =>This Inner Loop Header: Depth=1
	movzwl	%ax, %edx
	imull	$43691, %edx, %edx              # imm = 0xAAAB
	shrl	$17, %edx
	leal	(%rdx,%rdx,2), %edx
	movl	%eax, %esi
	subl	%edx, %esi
	movzwl	%si, %edx
	xorps	%xmm1, %xmm1
	cvtsi2sd	%edx, %xmm1
	divsd	%xmm0, %xmm1
	cvtsd2ss	%xmm1, %xmm1
	movss	%xmm1, 64(%rsp,%rcx)
	addq	$4, %rcx
	incl	%eax
	cmpq	$4096, %rcx                     # imm = 0x1000
	jne	.LBB9_3
# %bb.4:                                # %_Z17fillPseudoRandomsPf.exit
	leaq	56(%rsp), %r15
	movl	$4194304, %esi                  # imm = 0x400000
	movq	%r15, %rdi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$177, %esi
	callq	_ZL11handleError10hipError_tPKci
	leaq	16(%rsp), %rdi
	movl	$4096, %esi                     # imm = 0x1000
	callq	hipMalloc
	movl	%eax, %edi
	movl	$178, %esi
	callq	_ZL11handleError10hipError_tPKci
	leaq	48(%rsp), %r13
	movl	$4096, %esi                     # imm = 0x1000
	movq	%r13, %rdi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$179, %esi
	callq	_ZL11handleError10hipError_tPKci
	leaq	8(%rsp), %rdi
	movl	$4096, %esi                     # imm = 0x1000
	callq	hipMalloc
	movl	%eax, %edi
	movl	$180, %esi
	callq	_ZL11handleError10hipError_tPKci
	leaq	40(%rsp), %r12
	movl	$4096, %esi                     # imm = 0x1000
	movq	%r12, %rdi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$181, %esi
	callq	_ZL11handleError10hipError_tPKci
	leaq	32(%rsp), %rdi
	movl	$4, %esi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$182, %esi
	callq	_ZL11handleError10hipError_tPKci
	leaq	24(%rsp), %rbp
	movl	$4096, %esi                     # imm = 0x1000
	movq	%rbp, %rdi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$184, %esi
	callq	_ZL11handleError10hipError_tPKci
	movq	(%r15), %rdi
	movl	$4194304, %edx                  # imm = 0x400000
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$188, %esi
	callq	_ZL11handleError10hipError_tPKci
	movq	(%rbp), %rdi
	leaq	64(%rsp), %rsi
	movl	$4096, %edx                     # imm = 0x1000
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$191, %esi
	callq	_ZL11handleError10hipError_tPKci
	movq	(%r13), %rsi
	movl	$4096, %edx                     # imm = 0x1000
	movq	%r14, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$199, %esi
	callq	_ZL11handleError10hipError_tPKci
	movq	(%r15), %rdi
	callq	hipFree
	leaq	16(%rsp), %rax
	movq	(%rax), %rdi
	callq	hipFree
	leaq	8(%rsp), %rax
	movq	(%rax), %rdi
	callq	hipFree
	movq	(%r13), %rdi
	callq	hipFree
	movq	(%r12), %rdi
	callq	hipFree
	movl	$.L.str.10, %edi
	xorl	%eax, %eax
	callq	printf
	movq	%r14, %rdi
	callq	_Z10printArrayPi
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	_Z27verifyMaximalIndependentSetPiS_
	movl	%eax, %ebp
	movl	$.L.str.12, %eax
	movl	$.L.str.13, %edi
	testb	%bpl, %bpl
	cmovneq	%rax, %rdi
	callq	puts@PLT
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	xorb	$1, %bpl
	movzbl	%bpl, %eax
	addq	$4168, %rsp                     # imm = 0x1048
	.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
.LBB9_5:
	.cfi_def_cfa_offset 4224
	movq	stderr(%rip), %rcx
	movl	$.L.str.7, %edi
	movl	$34, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end9:
	.size	main, .Lfunc_end9-main
	.cfi_endproc
                                        # -- End function
	.type	_ZL11handleError10hipError_tPKci,@function # -- Begin function _ZL11handleError10hipError_tPKci
_ZL11handleError10hipError_tPKci:       # @_ZL11handleError10hipError_tPKci
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB10_2
# %bb.1:
	retq
.LBB10_2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%esi, %ebx
	callq	hipGetErrorString
	movl	$.L.str.14, %edi
	movl	$.L.str.9, %edx
	movq	%rax, %rsi
	movl	%ebx, %ecx
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end10:
	.size	_ZL11handleError10hipError_tPKci, .Lfunc_end10-_ZL11handleError10hipError_tPKci
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Array("
	.size	.L.str, 7

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"\t(%d, %d)\n"
	.size	.L.str.4, 11

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Set is not independent as it contains neighbors %d and %d"
	.size	.L.str.5, 58

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Set is not maximal as %d (not in set) has no neighbors in set\n"
	.size	.L.str.6, 63

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Failed to allocate host matrices!\n"
	.size	.L.str.7, 35

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/kasiaja/pwir-cuda-labs/master/lab3/independentSet.hip"
	.size	.L.str.9, 116

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Checking computed result for correctness: "
	.size	.L.str.10, 43

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Result = PASS"
	.size	.L.str.12, 14

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Result = FAIL"
	.size	.L.str.13, 14

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"%s in %s at line %d\n"
	.size	.L.str.14, 21

	.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	")"
	.size	.Lstr, 2

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Edges: "
	.size	.Lstr.1, 8

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Computing result using CUDA Kernel..."
	.size	.Lstr.2, 38

	.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
