#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
 
#define  MAX_EDGE  100000000
 

__global__ void BFS(int* off,int* edge,int* current,int* size,int N,int E,int* c_arr,int* c_size,int* dist){
    int id = blockIdx.x*blockDim.x+threadIdx.x;
    if(id < *size){
        // printf("inside kernel %d %d\n",id,*size);
        int node = current[id];
        //extend this node
        int start = off[node];
        int end = E;
        if(node!=N-1)
            end = off[node+1];
        
        while(start<end){
            // add in list atomically in c_arr
            int child = edge[start];
            // printf("c %d \n",child);
            if ( dist[child] < 0){
                dist[child] = dist[node] + 1;
                int index = atomicAdd(c_size,1);
                c_arr[index]= child;
            }
            start++;  
        }

        // printf("s %d\n",*c_size);
    }
}


int main(){

    // set start node same as destination
    int startNode;
    scanf("%d",&startNode);

    FILE* fgraph = fopen("graph.txt","r");
    int N,edge_size;
    fscanf(fgraph,"%d %d\n",&N,&edge_size);


    int* H_offset = (int*)malloc(sizeof(int)*N);
    int* H_edges  = (int*)malloc(sizeof(int)*edge_size);


    for(int i=0;i<edge_size;i++){
        fscanf(fgraph,"%d",&H_edges[i]);
    }

    for(int i=0;i<N;i++){
        fscanf(fgraph,"%d",&H_offset[i]);
    }

    
    
    fclose(fgraph);
    printf("completed input\n");

    int* H_current_node = (int*)malloc(sizeof(int)*edge_size);
    H_current_node[0]=startNode;
    int* H_c_size = (int*)malloc(sizeof(int));
    *H_c_size = 1;
    int* H_visited = (int*)malloc(sizeof(int)*N);
    memset(H_visited,-1,sizeof(int)*N);
    H_visited[startNode]=0;

    int* a0 = (int*)malloc(sizeof(int));
    *a0=0;

    int* a1 = (int*)malloc(sizeof(int));
    *a1=1;

    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    int* D_offset;
    int* D_edges;
    int* D_visited;

    int* D_current_node1;
    int* D_c_size1;
    int* D_current_node2;
    int* D_c_size2;

    cudaMalloc(&D_offset,sizeof(int)*N);
    cudaMalloc(&D_visited,sizeof(int)*N);
    cudaMalloc(&D_edges,sizeof(int)*edge_size);
    cudaMalloc(&D_current_node1,sizeof(int)*edge_size);
    cudaMalloc(&D_c_size1,sizeof(int));
    cudaMalloc(&D_current_node2,sizeof(int)*edge_size);
    cudaMalloc(&D_c_size2,sizeof(int));


    cudaMemcpy(D_offset,H_offset,sizeof(int)*N,cudaMemcpyHostToDevice);
    cudaMemcpy(D_edges,H_edges,sizeof(int)*edge_size,cudaMemcpyHostToDevice);
    cudaMemcpy(D_current_node1,H_current_node,sizeof(int)*edge_size,cudaMemcpyHostToDevice);
    cudaMemcpy(D_visited,H_visited,sizeof(int)*N,cudaMemcpyHostToDevice);
    cudaMemcpy(D_c_size1,a1,sizeof(int),cudaMemcpyHostToDevice);
    cudaMemcpy(D_c_size2,a0,sizeof(int),cudaMemcpyHostToDevice);



    int i=1;
    cudaEventRecord(start);
    while(*H_c_size>0){
        int numThreads = 512;
        int numBlocks = (*H_c_size+numThreads-1)/numThreads;


        if(i%2==1){
            //use array 1
            BFS<<<numBlocks,numThreads>>>(D_offset,D_edges,D_current_node1,D_c_size1,N,edge_size,D_current_node2,D_c_size2,D_visited);

            cudaMemcpy(H_c_size,D_c_size2, sizeof(int),cudaMemcpyDeviceToHost);
            // reset the index
            cudaMemcpy(D_c_size1,a0,sizeof(int),cudaMemcpyHostToDevice);
          
        }
        else{
            //use array 2
            BFS<<<numBlocks,numThreads>>>(D_offset,D_edges,D_current_node2,D_c_size2,N,edge_size,D_current_node1,D_c_size1,D_visited);
            
            cudaMemcpy(H_c_size,D_c_size1, sizeof(int),cudaMemcpyDeviceToHost);
            //reset index
            cudaMemcpy(D_c_size2,a0,sizeof(int),cudaMemcpyHostToDevice);

        }
        i++;
        
    }
    cudaEventRecord(stop);
    
    cudaMemcpy(H_visited,D_visited, sizeof(int)*N,cudaMemcpyDeviceToHost);

    cudaEventSynchronize(stop);
    float milliseconds = 0;
    cudaEventElapsedTime(&milliseconds, start, stop);

    int max_level = 0;
    for(int i=0;i<N;i++){
        if(H_visited[i]>max_level){
            max_level = H_visited[i];
        }
        // printf("%d, %d\n",i,H_visited[i]);
    }
    
    printf("max-level: %d\n",max_level);
    printf("time: %f\n",milliseconds);

    FILE* fptr = fopen("Hx.txt","w");
    int count_unreachable = 0;
    int farthest_val = 0;
    int farthest_node = 0;
    for(int i=0;i<N;i++){
        if(H_visited[i]==-1)
            count_unreachable++;
        if(H_visited[i]>=farthest_val){
            farthest_val = H_visited[i];
            farthest_node = i;
        }

        fprintf(fptr,"%d ",H_visited[i]);
    }
    fclose(fptr);

    printf("unreachable %d , %f\n",count_unreachable,count_unreachable/(N*1.0));
    printf("farthest node: %d  val: %d\n",farthest_node,farthest_val);
    return 0;
}

	.file	"tmpxft_0020518b_00000000-6_bfs_hx.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z36__device_stub__Z3BFSPiS_S_S_iiS_S_S_PiS_S_S_iiS_S_S_
	.type	_Z36__device_stub__Z3BFSPiS_S_S_iiS_S_S_PiS_S_S_iiS_S_S_, @function
_Z36__device_stub__Z3BFSPiS_S_S_iiS_S_S_PiS_S_S_iiS_S_S_:
.LFB2052:
	.cfi_startproc
	endbr64
	subq	$216, %rsp
	.cfi_def_cfa_offset 224
	movq	224(%rsp), %rax
	movq	%rdi, 56(%rsp)
	leaq	80(%rsp), %rdi
	movq	%rsi, 48(%rsp)
	leaq	92(%rsp), %rsi
	movq	%rax, 16(%rsp)
	movq	232(%rsp), %rax
	movq	%rdx, 40(%rsp)
	leaq	64(%rsp), %rdx
	movq	%rax, 8(%rsp)
	movq	240(%rsp), %rax
	movq	%rcx, 32(%rsp)
	leaq	72(%rsp), %rcx
	movl	%r8d, 28(%rsp)
	movl	%r9d, 24(%rsp)
	movq	%rax, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 200(%rsp)
	xorl	%eax, %eax
	leaq	56(%rsp), %rax
	movl	$1, 88(%rsp)
	movq	%rax, 128(%rsp)
	leaq	48(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	40(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	28(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 176(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 184(%rsp)
	movq	%rsp, %rax
	movq	%rax, 192(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 80(%rsp)
	movq	%rax, 92(%rsp)
	movl	$1, 100(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	72(%rsp)
	.cfi_def_cfa_offset 232
	leaq	_Z3BFSPiS_S_S_iiS_S_S_(%rip), %rdi
	pushq	72(%rsp)
	.cfi_def_cfa_offset 240
	movq	108(%rsp), %rcx
	movl	116(%rsp), %r8d
	movq	96(%rsp), %rsi
	movl	104(%rsp), %edx
	leaq	144(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 232
	popq	%rdx
	.cfi_def_cfa_offset 224
.L2:
	movq	200(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$216, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2052:
	.size	_Z36__device_stub__Z3BFSPiS_S_S_iiS_S_S_PiS_S_S_iiS_S_S_, .-_Z36__device_stub__Z3BFSPiS_S_S_iiS_S_S_PiS_S_S_iiS_S_S_
	.globl	_Z3BFSPiS_S_S_iiS_S_S_
	.type	_Z3BFSPiS_S_S_iiS_S_S_, @function
_Z3BFSPiS_S_S_iiS_S_S_:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z36__device_stub__Z3BFSPiS_S_S_iiS_S_S_PiS_S_S_iiS_S_S_
	.cfi_endproc
.LFE2053:
	.size	_Z3BFSPiS_S_S_iiS_S_S_, .-_Z3BFSPiS_S_S_iiS_S_S_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d"
.LC1:
	.string	"r"
.LC2:
	.string	"graph.txt"
.LC3:
	.string	"%d %d\n"
.LC4:
	.string	"completed input\n"
.LC6:
	.string	"max-level: %d\n"
.LC7:
	.string	"time: %f\n"
.LC8:
	.string	"w"
.LC9:
	.string	"Hx.txt"
.LC10:
	.string	"%d "
.LC11:
	.string	"unreachable %d , %f\n"
.LC12:
	.string	"farthest node: %d  val: %d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC0(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%r15, %rdi
	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
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$152, %rsp
	.cfi_def_cfa_offset 208
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	28(%rsp), %rsi
	call	__isoc23_scanf@PLT
	leaq	.LC1(%rip), %rsi
	leaq	.LC2(%rip), %rdi
	call	fopen@PLT
	leaq	36(%rsp), %rcx
	leaq	32(%rsp), %rdx
	leaq	.LC3(%rip), %rsi
	movq	%rax, %rdi
	movq	%rax, %rbx
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movslq	32(%rsp), %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movslq	36(%rsp), %rdi
	movq	%rax, %r14
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, 8(%rsp)
	movq	%rax, %r12
.L9:
	cmpl	%ebp, 36(%rsp)
	jle	.L28
	movq	%r12, %rdx
	movq	%r15, %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	incl	%ebp
	addq	$4, %r12
	jmp	.L9
.L28:
	movq	%r14, %r12
	xorl	%ebp, %ebp
.L11:
	cmpl	%ebp, 32(%rsp)
	jle	.L29
	movq	%r12, %rdx
	movq	%r15, %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	incl	%ebp
	addq	$4, %r12
	jmp	.L11
.L29:
	movq	%rbx, %rdi
	call	fclose@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movslq	36(%rsp), %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movslq	28(%rsp), %r15
	movl	$4, %edi
	movq	%rax, %r13
	movl	%r15d, (%rax)
	call	malloc@PLT
	movslq	32(%rsp), %r12
	movl	$1, (%rax)
	movq	%rax, %rbp
	salq	$2, %r12
	movq	%r12, %rdi
	call	malloc@PLT
	movq	%r12, %rdx
	orl	$-1, %esi
	movq	%r12, %rcx
	movq	%rax, %rdi
	movq	%rax, %rbx
	call	__memset_chk@PLT
	xorl	%ecx, %ecx
	movl	$4, %edi
	movl	%ecx, (%rbx,%r15,4)
	call	malloc@PLT
	xorl	%esi, %esi
	movl	$4, %edi
	movl	%esi, (%rax)
	movq	%rax, %r12
	call	malloc@PLT
	leaq	40(%rsp), %rdi
	movl	$1, (%rax)
	movq	%rax, %r15
	call	cudaEventCreate@PLT
	leaq	48(%rsp), %rdi
	call	cudaEventCreate@PLT
	movslq	32(%rsp), %rsi
	leaq	56(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	32(%rsp), %rsi
	leaq	72(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	36(%rsp), %rsi
	leaq	64(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	36(%rsp), %rsi
	leaq	80(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	leaq	88(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	movslq	36(%rsp), %rsi
	leaq	96(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	leaq	104(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	movslq	32(%rsp), %rdx
	movq	56(%rsp), %rdi
	movq	%r14, %rsi
	movl	$1, %ecx
	movl	$1, %r14d
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	36(%rsp), %rdx
	movq	8(%rsp), %rsi
	movl	$1, %ecx
	movq	64(%rsp), %rdi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	36(%rsp), %rdx
	movq	80(%rsp), %rdi
	movq	%r13, %rsi
	movabsq	$4294967808, %r13
	movl	$1, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	32(%rsp), %rdx
	movq	72(%rsp), %rdi
	movq	%rbx, %rsi
	movl	$1, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movq	88(%rsp), %rdi
	movl	$1, %ecx
	movq	%r15, %rsi
	movl	$4, %edx
	call	cudaMemcpy@PLT
	movq	104(%rsp), %rdi
	movl	$1, %ecx
	movq	%r12, %rsi
	movl	$4, %edx
	call	cudaMemcpy@PLT
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
.L13:
	movl	0(%rbp), %eax
	testl	%eax, %eax
	jle	.L30
	addl	$511, %eax
	movq	%r13, 124(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	sarl	$9, %eax
	movq	%r13, %rdx
	movl	$1, %ecx
	movl	$1, 132(%rsp)
	movl	%eax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 116(%rsp)
	movq	112(%rsp), %rdi
	movl	120(%rsp), %esi
	testl	$1, %r14d
	je	.L14
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L15
	pushq	%rdx
	.cfi_def_cfa_offset 216
	pushq	80(%rsp)
	.cfi_def_cfa_offset 224
	pushq	120(%rsp)
	.cfi_def_cfa_offset 232
	pushq	120(%rsp)
	.cfi_def_cfa_offset 240
	movl	68(%rsp), %r9d
	movl	64(%rsp), %r8d
	movq	120(%rsp), %rcx
	movq	112(%rsp), %rdx
	movq	96(%rsp), %rsi
	movq	88(%rsp), %rdi
	call	_Z36__device_stub__Z3BFSPiS_S_S_iiS_S_S_PiS_S_S_iiS_S_S_
	addq	$32, %rsp
	.cfi_def_cfa_offset 208
.L15:
	movq	104(%rsp), %rsi
	movl	$2, %ecx
	movl	$4, %edx
	movq	%rbp, %rdi
	call	cudaMemcpy@PLT
	movq	88(%rsp), %rdi
	movl	$1, %ecx
	movq	%r12, %rsi
	movl	$4, %edx
	jmp	.L27
.L14:
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L17
	pushq	%rax
	.cfi_def_cfa_offset 216
	pushq	80(%rsp)
	.cfi_def_cfa_offset 224
	pushq	104(%rsp)
	.cfi_def_cfa_offset 232
	pushq	104(%rsp)
	.cfi_def_cfa_offset 240
	movl	68(%rsp), %r9d
	movl	64(%rsp), %r8d
	movq	136(%rsp), %rcx
	movq	128(%rsp), %rdx
	movq	96(%rsp), %rsi
	movq	88(%rsp), %rdi
	call	_Z36__device_stub__Z3BFSPiS_S_S_iiS_S_S_PiS_S_S_iiS_S_S_
	addq	$32, %rsp
	.cfi_def_cfa_offset 208
.L17:
	movq	88(%rsp), %rsi
	movl	$2, %ecx
	movl	$4, %edx
	movq	%rbp, %rdi
	call	cudaMemcpy@PLT
	movq	104(%rsp), %rdi
	movl	$1, %ecx
	movq	%r12, %rsi
	movl	$4, %edx
.L27:
	call	cudaMemcpy@PLT
	incl	%r14d
	jmp	.L13
.L30:
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movslq	32(%rsp), %rdx
	movq	72(%rsp), %rsi
	movq	%rbx, %rdi
	movl	$2, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movq	48(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	48(%rsp), %rdx
	movq	40(%rsp), %rsi
	leaq	124(%rsp), %rdi
	movl	$0x00000000, 124(%rsp)
	call	cudaEventElapsedTime@PLT
	movl	32(%rsp), %esi
	xorl	%eax, %eax
	xorl	%edx, %edx
.L19:
	cmpl	%eax, %esi
	jle	.L31
	movl	(%rbx,%rax,4), %ecx
	cmpl	%ecx, %edx
	cmovl	%ecx, %edx
	incq	%rax
	jmp	.L19
.L31:
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%ebp, %ebp
	call	__printf_chk@PLT
	movl	$2, %edi
	movb	$1, %al
	xorl	%r14d, %r14d
	leaq	.LC7(%rip), %rsi
	cvtss2sd	124(%rsp), %xmm0
	xorl	%r13d, %r13d
	xorl	%r12d, %r12d
	call	__printf_chk@PLT
	leaq	.LC9(%rip), %rdi
	leaq	.LC8(%rip), %rsi
	call	fopen@PLT
	leaq	.LC10(%rip), %r15
	movq	%rax, %rdi
.L21:
	cmpl	%ebp, 32(%rsp)
	jle	.L32
	movl	(%rbx,%rbp,4), %ecx
	cmpl	$-1, %ecx
	jne	.L22
	incl	%r12d
.L22:
	cmpl	%r13d, %ecx
	jl	.L23
	movl	%ebp, %r14d
	movl	%ecx, %r13d
.L23:
	movq	%r15, %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	movq	%rdi, 8(%rsp)
	call	__fprintf_chk@PLT
	incq	%rbp
	movq	8(%rsp), %rdi
	jmp	.L21
.L32:
	call	fclose@PLT
	cvtsi2sdl	32(%rsp), %xmm1
	movl	%r12d, %edx
	movb	$1, %al
	cvtsi2sdl	%r12d, %xmm0
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	divsd	%xmm1, %xmm0
	call	__printf_chk@PLT
	xorl	%eax, %eax
	movl	%r13d, %ecx
	movl	%r14d, %edx
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	addq	$152, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	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
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC13:
	.string	"_Z3BFSPiS_S_S_iiS_S_S_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.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	.LC13(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z3BFSPiS_S_S_iiS_S_S_(%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
.LFE2055:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <stdio.h>
#include <stdlib.h>
#include <hip/hip_runtime.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
 
#define  MAX_EDGE  100000000
 

__global__ void BFS(int* off,int* edge,int* current,int* size,int N,int E,int* c_arr,int* c_size,int* dist){
    int id = blockIdx.x*blockDim.x+threadIdx.x;
    if(id < *size){
        // printf("inside kernel %d %d\n",id,*size);
        int node = current[id];
        //extend this node
        int start = off[node];
        int end = E;
        if(node!=N-1)
            end = off[node+1];
        
        while(start<end){
            // add in list atomically in c_arr
            int child = edge[start];
            // printf("c %d \n",child);
            if ( dist[child] < 0){
                dist[child] = dist[node] + 1;
                int index = atomicAdd(c_size,1);
                c_arr[index]= child;
            }
            start++;  
        }

        // printf("s %d\n",*c_size);
    }
}


int main(){

    // set start node same as destination
    int startNode;
    scanf("%d",&startNode);

    FILE* fgraph = fopen("graph.txt","r");
    int N,edge_size;
    fscanf(fgraph,"%d %d\n",&N,&edge_size);


    int* H_offset = (int*)malloc(sizeof(int)*N);
    int* H_edges  = (int*)malloc(sizeof(int)*edge_size);


    for(int i=0;i<edge_size;i++){
        fscanf(fgraph,"%d",&H_edges[i]);
    }

    for(int i=0;i<N;i++){
        fscanf(fgraph,"%d",&H_offset[i]);
    }

    
    
    fclose(fgraph);
    printf("completed input\n");

    int* H_current_node = (int*)malloc(sizeof(int)*edge_size);
    H_current_node[0]=startNode;
    int* H_c_size = (int*)malloc(sizeof(int));
    *H_c_size = 1;
    int* H_visited = (int*)malloc(sizeof(int)*N);
    memset(H_visited,-1,sizeof(int)*N);
    H_visited[startNode]=0;

    int* a0 = (int*)malloc(sizeof(int));
    *a0=0;

    int* a1 = (int*)malloc(sizeof(int));
    *a1=1;

    hipEvent_t start, stop;
    hipEventCreate(&start);
    hipEventCreate(&stop);
    int* D_offset;
    int* D_edges;
    int* D_visited;

    int* D_current_node1;
    int* D_c_size1;
    int* D_current_node2;
    int* D_c_size2;

    hipMalloc(&D_offset,sizeof(int)*N);
    hipMalloc(&D_visited,sizeof(int)*N);
    hipMalloc(&D_edges,sizeof(int)*edge_size);
    hipMalloc(&D_current_node1,sizeof(int)*edge_size);
    hipMalloc(&D_c_size1,sizeof(int));
    hipMalloc(&D_current_node2,sizeof(int)*edge_size);
    hipMalloc(&D_c_size2,sizeof(int));


    hipMemcpy(D_offset,H_offset,sizeof(int)*N,hipMemcpyHostToDevice);
    hipMemcpy(D_edges,H_edges,sizeof(int)*edge_size,hipMemcpyHostToDevice);
    hipMemcpy(D_current_node1,H_current_node,sizeof(int)*edge_size,hipMemcpyHostToDevice);
    hipMemcpy(D_visited,H_visited,sizeof(int)*N,hipMemcpyHostToDevice);
    hipMemcpy(D_c_size1,a1,sizeof(int),hipMemcpyHostToDevice);
    hipMemcpy(D_c_size2,a0,sizeof(int),hipMemcpyHostToDevice);



    int i=1;
    hipEventRecord(start);
    while(*H_c_size>0){
        int numThreads = 512;
        int numBlocks = (*H_c_size+numThreads-1)/numThreads;


        if(i%2==1){
            //use array 1
            BFS<<<numBlocks,numThreads>>>(D_offset,D_edges,D_current_node1,D_c_size1,N,edge_size,D_current_node2,D_c_size2,D_visited);

            hipMemcpy(H_c_size,D_c_size2, sizeof(int),hipMemcpyDeviceToHost);
            // reset the index
            hipMemcpy(D_c_size1,a0,sizeof(int),hipMemcpyHostToDevice);
          
        }
        else{
            //use array 2
            BFS<<<numBlocks,numThreads>>>(D_offset,D_edges,D_current_node2,D_c_size2,N,edge_size,D_current_node1,D_c_size1,D_visited);
            
            hipMemcpy(H_c_size,D_c_size1, sizeof(int),hipMemcpyDeviceToHost);
            //reset index
            hipMemcpy(D_c_size2,a0,sizeof(int),hipMemcpyHostToDevice);

        }
        i++;
        
    }
    hipEventRecord(stop);
    
    hipMemcpy(H_visited,D_visited, sizeof(int)*N,hipMemcpyDeviceToHost);

    hipEventSynchronize(stop);
    float milliseconds = 0;
    hipEventElapsedTime(&milliseconds, start, stop);

    int max_level = 0;
    for(int i=0;i<N;i++){
        if(H_visited[i]>max_level){
            max_level = H_visited[i];
        }
        // printf("%d, %d\n",i,H_visited[i]);
    }
    
    printf("max-level: %d\n",max_level);
    printf("time: %f\n",milliseconds);

    FILE* fptr = fopen("Hx.txt","w");
    int count_unreachable = 0;
    int farthest_val = 0;
    int farthest_node = 0;
    for(int i=0;i<N;i++){
        if(H_visited[i]==-1)
            count_unreachable++;
        if(H_visited[i]>=farthest_val){
            farthest_val = H_visited[i];
            farthest_node = i;
        }

        fprintf(fptr,"%d ",H_visited[i]);
    }
    fclose(fptr);

    printf("unreachable %d , %f\n",count_unreachable,count_unreachable/(N*1.0));
    printf("farthest node: %d  val: %d\n",farthest_node,farthest_val);
    return 0;
}

	.text
	.file	"bfs_hx.hip"
	.globl	_Z18__device_stub__BFSPiS_S_S_iiS_S_S_ # -- Begin function _Z18__device_stub__BFSPiS_S_S_iiS_S_S_
	.type	_Z18__device_stub__BFSPiS_S_S_iiS_S_S_,@function
_Z18__device_stub__BFSPiS_S_S_iiS_S_S_: # @_Z18__device_stub__BFSPiS_S_S_iiS_S_S_
	.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	$176, %rsp
	.cfi_def_cfa_offset 224
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	12(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	8(%rsp), %r8
	movl	%r9d, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	224(%rsp), %rax
	movq	%rax, 48(%rbx)
	leaq	232(%rsp), %rax
	movq	%rax, 56(%rbx)
	leaq	240(%rsp), %rax
	movq	%rax, 64(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%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	$_Z3BFSPiS_S_S_iiS_S_S_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$192, %rsp
	.cfi_adjust_cfa_offset -192
	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	_Z18__device_stub__BFSPiS_S_S_iiS_S_S_, .Lfunc_end0-_Z18__device_stub__BFSPiS_S_S_iiS_S_S_
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	leaq	68(%rsp), %rsi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	$.L.str.1, %edi
	movl	$.L.str.2, %esi
	callq	fopen
	movq	%rax, %rbx
	movq	%rsp, %r14
	leaq	4(%rsp), %r15
	movl	$.L.str.3, %esi
	movq	%rax, %rdi
	movq	%r14, %rdx
	movq	%r15, %rcx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movslq	(%r14), %r14
	leaq	(,%r14,4), %rdi
	callq	malloc
	movq	%rax, %r12
	movslq	(%r15), %r15
	leaq	(,%r15,4), %rdi
	callq	malloc
	testq	%r15, %r15
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	jle	.LBB1_4
# %bb.1:                                # %.lr.ph.preheader
	movq	%rax, %r14
	xorl	%r15d, %r15d
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str, %esi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r15
	movslq	4(%rsp), %rax
	addq	$4, %r14
	cmpq	%rax, %r15
	jl	.LBB1_2
# %bb.3:                                # %.preheader.loopexit
	movl	(%rsp), %r14d
.LBB1_4:                                # %.preheader
	testl	%r14d, %r14d
	jle	.LBB1_7
# %bb.5:                                # %.lr.ph100.preheader
	movq	%r12, %r14
	xorl	%r15d, %r15d
.LBB1_6:                                # %.lr.ph100
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str, %esi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r15
	movslq	(%rsp), %rax
	addq	$4, %r14
	cmpq	%rax, %r15
	jl	.LBB1_6
.LBB1_7:                                # %.lr.ph103.preheader
	movq	%rbx, %rdi
	callq	fclose
	movl	$.Lstr, %edi
	callq	puts@PLT
	movslq	4(%rsp), %rdi
	shlq	$2, %rdi
	callq	malloc
	movq	%rax, 112(%rsp)                 # 8-byte Spill
	movslq	68(%rsp), %rbx
	movl	%ebx, (%rax)
	movl	$4, %edi
	callq	malloc
	movq	%rax, %r14
	movl	$1, %r13d
	movl	%r13d, (%rax)
	movslq	(%rsp), %r15
	shlq	$2, %r15
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %rbp
	movq	%rax, %rdi
	movl	$255, %esi
	movq	%r15, %rdx
	callq	memset@PLT
	movq	%r12, 96(%rsp)                  # 8-byte Spill
	xorl	%r12d, %r12d
	movl	%r12d, (%rbp,%rbx,4)
	movl	$4, %edi
	callq	malloc
	movq	%rax, %rbx
	movl	%r12d, (%rax)
	movl	$4, %edi
	callq	malloc
	movq	%rax, 104(%rsp)                 # 8-byte Spill
	movl	%r13d, (%rax)
	leaq	88(%rsp), %rdi
	callq	hipEventCreate
	leaq	56(%rsp), %rdi
	callq	hipEventCreate
	movslq	(%rsp), %rsi
	shlq	$2, %rsi
	leaq	48(%rsp), %rdi
	callq	hipMalloc
	movslq	(%rsp), %rsi
	shlq	$2, %rsi
	leaq	16(%rsp), %rdi
	callq	hipMalloc
	movslq	4(%rsp), %rsi
	shlq	$2, %rsi
	leaq	40(%rsp), %rdi
	callq	hipMalloc
	movslq	4(%rsp), %rsi
	shlq	$2, %rsi
	leaq	32(%rsp), %rdi
	callq	hipMalloc
	leaq	8(%rsp), %r15
	movl	$4, %esi
	movq	%r15, %rdi
	callq	hipMalloc
	movslq	4(%rsp), %rsi
	shlq	$2, %rsi
	leaq	80(%rsp), %rdi
	callq	hipMalloc
	leaq	72(%rsp), %r12
	movl	$4, %esi
	movq	%r12, %rdi
	callq	hipMalloc
	leaq	48(%rsp), %rax
	movq	(%rax), %rdi
	movslq	(%rsp), %rdx
	shlq	$2, %rdx
	movq	96(%rsp), %rsi                  # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	40(%rsp), %rax
	movq	(%rax), %rdi
	movslq	4(%rsp), %rdx
	shlq	$2, %rdx
	movq	24(%rsp), %rsi                  # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	32(%rsp), %rax
	movq	(%rax), %rdi
	movslq	4(%rsp), %rdx
	shlq	$2, %rdx
	movq	112(%rsp), %rsi                 # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	16(%rsp), %rax
	movq	(%rax), %rdi
	movslq	(%rsp), %rdx
	shlq	$2, %rdx
	movq	%rbp, 24(%rsp)                  # 8-byte Spill
	movq	%rbp, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r15), %rdi
	movl	$4, %edx
	movq	104(%rsp), %rsi                 # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%r12, %r15
	movq	(%r12), %rdi
	movl	$4, %edx
	movq	%rbx, %r12
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	88(%rsp), %rax
	movq	(%rax), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movabsq	$4294967296, %rax               # imm = 0x100000000
	leaq	512(%rax), %rbp
	movl	$1, %ebx
.LBB1_8:                                # %.lr.ph103
                                        # =>This Inner Loop Header: Depth=1
	addl	$511, %r13d                     # imm = 0x1FF
	shrl	$9, %r13d
	movabsq	$4294967296, %rax               # imm = 0x100000000
	orq	%rax, %r13
	movq	%r13, %rdi
	movl	$1, %esi
	movq	%rbp, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testb	$1, %bl
	jne	.LBB1_9
# %bb.11:                               #   in Loop: Header=BB1_8 Depth=1
	leaq	8(%rsp), %rcx
	movq	%r15, %r13
	testl	%eax, %eax
	jne	.LBB1_13
# %bb.12:                               #   in Loop: Header=BB1_8 Depth=1
	movq	48(%rsp), %rdi
	movq	40(%rsp), %rsi
	movq	80(%rsp), %rdx
	movq	72(%rsp), %rcx
	movl	(%rsp), %r8d
	movl	4(%rsp), %r9d
	subq	$8, %rsp
	.cfi_adjust_cfa_offset 8
	pushq	24(%rsp)
	.cfi_adjust_cfa_offset 8
	pushq	24(%rsp)
	.cfi_adjust_cfa_offset 8
	pushq	56(%rsp)
	.cfi_adjust_cfa_offset 8
	callq	_Z18__device_stub__BFSPiS_S_S_iiS_S_S_
	addq	$32, %rsp
	.cfi_adjust_cfa_offset -32
	leaq	8(%rsp), %rcx
	movq	%r15, %r13
	jmp	.LBB1_13
.LBB1_9:                                #   in Loop: Header=BB1_8 Depth=1
	movq	%r15, %rcx
	leaq	8(%rsp), %r13
	testl	%eax, %eax
	jne	.LBB1_13
# %bb.10:                               #   in Loop: Header=BB1_8 Depth=1
	movq	48(%rsp), %rdi
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdx
	movq	8(%rsp), %rcx
	movl	(%rsp), %r8d
	movl	4(%rsp), %r9d
	subq	$8, %rsp
	.cfi_adjust_cfa_offset 8
	pushq	24(%rsp)
	.cfi_adjust_cfa_offset 8
	pushq	88(%rsp)
	.cfi_adjust_cfa_offset 8
	pushq	104(%rsp)
	.cfi_adjust_cfa_offset 8
	callq	_Z18__device_stub__BFSPiS_S_S_iiS_S_S_
	addq	$32, %rsp
	.cfi_adjust_cfa_offset -32
	movq	%r15, %rcx
	leaq	8(%rsp), %r13
.LBB1_13:                               #   in Loop: Header=BB1_8 Depth=1
	movq	(%rcx), %rsi
	movl	$4, %edx
	movq	%r14, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	(%r13), %rdi
	movl	$4, %edx
	movq	%r12, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	incl	%ebx
	movl	(%r14), %r13d
	testl	%r13d, %r13d
	jg	.LBB1_8
# %bb.14:                               # %._crit_edge104
	movq	56(%rsp), %rdi
	xorl	%r14d, %r14d
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	16(%rsp), %rsi
	movslq	(%rsp), %rdx
	shlq	$2, %rdx
	movq	24(%rsp), %r13                  # 8-byte Reload
	movq	%r13, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	56(%rsp), %rdi
	callq	hipEventSynchronize
	leaq	64(%rsp), %rdi
	movl	$0, (%rdi)
	movq	88(%rsp), %rsi
	movq	56(%rsp), %rdx
	callq	hipEventElapsedTime
	movl	(%rsp), %eax
	movl	$0, %esi
	testl	%eax, %eax
	jle	.LBB1_17
# %bb.15:                               # %.lr.ph108.preheader
	xorl	%ecx, %ecx
	xorl	%esi, %esi
.LBB1_16:                               # %.lr.ph108
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r13,%rcx,4), %edx
	cmpl	%esi, %edx
	cmovgl	%edx, %esi
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_16
.LBB1_17:                               # %._crit_edge109
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	cvtss2sd	64(%rsp), %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.7, %edi
	movl	$.L.str.8, %esi
	callq	fopen
	movq	%rax, %r12
	cmpl	$0, (%rsp)
	movl	$0, %ebp
	movl	$0, %r15d
	jle	.LBB1_20
# %bb.18:                               # %.lr.ph115.preheader
	xorl	%ebx, %ebx
	xorl	%r15d, %r15d
	xorl	%ebp, %ebp
	xorl	%r14d, %r14d
.LBB1_19:                               # %.lr.ph115
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r13,%rbx,4), %edx
	xorl	%eax, %eax
	cmpl	$-1, %edx
	sete	%al
	addl	%eax, %r14d
	cmpl	%ebp, %edx
	cmovgl	%edx, %ebp
	cmovgel	%ebx, %r15d
	movl	$.L.str.9, %esi
	movq	%r12, %rdi
	xorl	%eax, %eax
	callq	fprintf
	incq	%rbx
	movslq	(%rsp), %rax
	cmpq	%rax, %rbx
	jl	.LBB1_19
.LBB1_20:                               # %._crit_edge116
	movq	%r12, %rdi
	callq	fclose
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r14d, %xmm0
	cvtsi2sdl	(%rsp), %xmm1
	divsd	%xmm1, %xmm0
	movl	$.L.str.10, %edi
	movl	%r14d, %esi
	movb	$1, %al
	callq	printf
	movl	$.L.str.11, %edi
	movl	%r15d, %esi
	movl	%ebp, %edx
	xorl	%eax, %eax
	callq	printf
	xorl	%eax, %eax
	addq	$120, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z3BFSPiS_S_S_iiS_S_S_, %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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z3BFSPiS_S_S_iiS_S_S_,@object  # @_Z3BFSPiS_S_S_iiS_S_S_
	.section	.rodata,"a",@progbits
	.globl	_Z3BFSPiS_S_S_iiS_S_S_
	.p2align	3, 0x0
_Z3BFSPiS_S_S_iiS_S_S_:
	.quad	_Z18__device_stub__BFSPiS_S_S_iiS_S_S_
	.size	_Z3BFSPiS_S_S_iiS_S_S_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%d"
	.size	.L.str, 3

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"graph.txt"
	.size	.L.str.1, 10

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

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"max-level: %d\n"
	.size	.L.str.5, 15

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"time: %f\n"
	.size	.L.str.6, 10

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Hx.txt"
	.size	.L.str.7, 7

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"w"
	.size	.L.str.8, 2

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

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"unreachable %d , %f\n"
	.size	.L.str.10, 21

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"farthest node: %d  val: %d\n"
	.size	.L.str.11, 28

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z3BFSPiS_S_S_iiS_S_S_"
	.size	.L__unnamed_1, 23

	.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	"completed input"
	.size	.Lstr, 16

	.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 _Z18__device_stub__BFSPiS_S_S_iiS_S_S_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z3BFSPiS_S_S_iiS_S_S_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z3BFSPiS_S_S_iiS_S_S_  ; -- Begin function _Z3BFSPiS_S_S_iiS_S_S_
	.globl	_Z3BFSPiS_S_S_iiS_S_S_
	.p2align	8
	.type	_Z3BFSPiS_S_S_iiS_S_S_,@function
_Z3BFSPiS_S_S_iiS_S_S_:                 ; @_Z3BFSPiS_S_S_iiS_S_S_
; %bb.0:
	s_clause 0x1
	s_load_b256 s[4:11], s[0:1], 0x0
	s_load_b32 s2, s[0:1], 0x4c
	s_waitcnt lgkmcnt(0)
	s_load_b32 s3, s[10:11], 0x0
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	s_waitcnt lgkmcnt(0)
	v_cmpx_gt_i32_e64 s3, v1
	s_cbranch_execz .LBB0_10
; %bb.1:
	v_ashrrev_i32_e32 v2, 31, v1
	s_load_b64 s[2:3], s[0:1], 0x20
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	v_add_co_u32 v0, vcc_lo, s8, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s9, v1, vcc_lo
	global_load_b32 v5, v[0:1], off
	s_waitcnt lgkmcnt(0)
	s_add_i32 s2, s2, -1
	v_mov_b32_e32 v8, s3
	s_waitcnt vmcnt(0)
	v_ashrrev_i32_e32 v6, 31, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[3:4], 2, v[5:6]
	v_add_co_u32 v1, vcc_lo, s4, v3
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v2, vcc_lo, s5, v4, vcc_lo
	v_cmp_ne_u32_e32 vcc_lo, s2, v5
	global_load_b32 v0, v[1:2], off
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_3
; %bb.2:
	global_load_b32 v8, v[1:2], off offset:4
.LBB0_3:
	s_or_b32 exec_lo, exec_lo, s2
	s_waitcnt vmcnt(0)
	v_cmp_lt_i32_e32 vcc_lo, v0, v8
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_10
; %bb.4:                                ; %.lr.ph
	s_clause 0x1
	s_load_b64 s[2:3], s[0:1], 0x38
	s_load_b128 s[8:11], s[0:1], 0x28
	v_ashrrev_i32_e32 v1, 31, v0
	v_mov_b32_e32 v9, 0
	s_mov_b32 s1, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[1:2], 2, v[0:1]
	v_add_co_u32 v1, vcc_lo, s6, v1
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v2, vcc_lo, s7, v2, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v3, vcc_lo, s2, v3
	v_add_co_ci_u32_e32 v4, vcc_lo, s3, v4, vcc_lo
.LBB0_5:                                ; =>This Inner Loop Header: Depth=1
	global_load_b32 v5, v[1:2], off
	s_mov_b32 s0, exec_lo
	s_waitcnt vmcnt(0)
	v_ashrrev_i32_e32 v6, 31, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[6:7], 2, v[5:6]
	v_add_co_u32 v6, vcc_lo, s2, v6
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v7, vcc_lo, s3, v7, vcc_lo
	global_load_b32 v10, v[6:7], off
	s_waitcnt vmcnt(0)
	v_cmpx_gt_i32_e32 0, v10
	s_cbranch_execz .LBB0_9
; %bb.6:                                ;   in Loop: Header=BB0_5 Depth=1
	global_load_b32 v11, v[3:4], off
	s_mov_b32 s4, exec_lo
	s_mov_b32 s5, exec_lo
	v_mbcnt_lo_u32_b32 v10, s4, 0
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v11, 1, v11
	global_store_b32 v[6:7], v11, off
                                        ; implicit-def: $vgpr6
	v_cmpx_eq_u32_e32 0, v10
	s_cbranch_execz .LBB0_8
; %bb.7:                                ;   in Loop: Header=BB0_5 Depth=1
	s_bcnt1_i32_b32 s4, s4
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mov_b32_e32 v6, s4
	global_atomic_add_u32 v6, v9, v6, s[10:11] glc
.LBB0_8:                                ;   in Loop: Header=BB0_5 Depth=1
	s_or_b32 exec_lo, exec_lo, s5
	s_waitcnt vmcnt(0)
	v_readfirstlane_b32 s4, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v6, s4, v10
	v_ashrrev_i32_e32 v7, 31, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[6:7], 2, v[6:7]
	v_add_co_u32 v6, vcc_lo, s8, v6
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v7, vcc_lo, s9, v7, vcc_lo
	global_store_b32 v[6:7], v5, off
.LBB0_9:                                ; %Flow
                                        ;   in Loop: Header=BB0_5 Depth=1
	s_or_b32 exec_lo, exec_lo, s0
	v_add_nc_u32_e32 v0, 1, v0
	v_add_co_u32 v1, s0, v1, 4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e64 v2, s0, 0, v2, s0
	v_cmp_ge_i32_e32 vcc_lo, v0, v8
	s_or_b32 s1, vcc_lo, s1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s1
	s_cbranch_execnz .LBB0_5
.LBB0_10:                               ; %.loopexit
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z3BFSPiS_S_S_iiS_S_S_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 320
		.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 12
		.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	_Z3BFSPiS_S_S_iiS_S_S_, .Lfunc_end0-_Z3BFSPiS_S_S_iiS_S_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 548
; NumSgprs: 18
; NumVgprs: 12
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 12
; Occupancy: 16
; WaveLimiterHint : 1
; 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
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .offset:         36
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         40
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         48
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         56
        .size:           8
        .value_kind:     global_buffer
      - .offset:         64
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         68
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         72
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         76
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         78
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         80
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         82
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         84
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         86
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         112
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         120
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         128
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 320
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z3BFSPiS_S_S_iiS_S_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z3BFSPiS_S_S_iiS_S_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     12
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
