#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_SCORE_NBR 2000
#define MAX_SIT_SIZE 4000

// typedef struct bestScore BestScore;

typedef struct {
    long time;
    double open;
    double high;
    double low;
    double close;
    double volume;
} Minute;

typedef struct {
    char name[128];
    long size;
    Minute* minutes;
} Coin;

typedef struct {
    double score;
    long minuteId;
    long coinId;
} Score;

typedef struct {
    // POTARD
    int nbrThreads;
    int nbrBlocks;
    int sitSize;
    int nbrScores;

    Coin* source;
    Score* scores;
    Score* bestScores;
    long nbrCoins;
    Minute* src;
    int cursorCoin;
    int cursorMinute;
    Coin** coins;
    char* result;
} Env;

Env* e;

#define STEP_SIZE 20

__global__ void compare(Env* e) {
    int workerNbr = threadIdx.x * e->nbrThreads + blockIdx.x;
    int cursorMinute = workerNbr + e->cursorMinute;
    double score = 0;
    int step = 0;
    for (int i = 1; i < e->sitSize; i++) {
        double destPourcentOpen =
            e->coins[e->cursorCoin]->minutes[cursorMinute + step].open /
            e->coins[e->cursorCoin]->minutes[cursorMinute + i].open * 1000;
        double srcPourcentOpen = e->src[0 + step].open / e->src[i].open * 1000;
        score += abs(destPourcentOpen - srcPourcentOpen);
        if (step > STEP_SIZE){
            step += 1;
        }
            
    }
    e->scores[workerNbr].score = score;
    e->scores[workerNbr].minuteId = cursorMinute;
    e->scores[workerNbr].coinId = e->cursorCoin;
}

void printBestScores() {
    for (int i = 0; i < e->nbrScores; i++) {
        // for (int i = 0; i < 2; i++) {
        printf("%.15lf %s %ld\n", e->bestScores[i].score,
               e->coins[e->bestScores[i].coinId]->name,
               e->coins[e->bestScores[i].coinId]
                   ->minutes[e->bestScores[i].minuteId]
                   .time);
    }
    printf("\n");
}

extern "C" char* bake(int sitSize, Minute* minutes) {
    e->sitSize = sitSize;
    memcpy(e->src, minutes, sizeof(Minute) * sitSize);
    for (int iBest = 0; iBest < e->nbrScores; iBest++) {
        e->bestScores[iBest].score = 999999999999;
    }
    for (e->cursorCoin = 0; e->cursorCoin < e->nbrCoins; e->cursorCoin++) {
        // printf("%s\n", e->coins[e->cursorCoin]->name);
        e->cursorMinute = 0;
        while (1) {
            compare<<<e->nbrBlocks, e->nbrThreads>>>(e);
            cudaDeviceSynchronize();
            cudaError_t error = cudaGetLastError();
            if (error != cudaSuccess) {
                printf("CUDA error: %s\n", cudaGetErrorString(error));
                exit(-1);
            }
            for (int iScore = 0; iScore < e->nbrBlocks * e->nbrThreads;
                 iScore++) {
                if (e->scores[iScore].score <=
                    e->bestScores[e->nbrScores - 1].score) {
                    // printf("%lf %lf %s\n",
                    // e->scores[iScore].score,e->coins[e->scores[iScore].coinId]->minutes[e->scores[iScore].minuteId].volume,
                    // e->coins[e->scores[iScore].coinId]->name);

                    for (int iBest = 0; iBest < e->nbrScores; iBest++) {
                        if (e->scores[iScore].score <
                            e->bestScores[iBest].score) {
                            Score tmp = e->bestScores[iBest];
                            for (int iTmp = iBest + 1; iTmp < e->nbrScores;
                                 iTmp++) {
                                Score tmp2 = e->bestScores[iTmp];
                                e->bestScores[iTmp] = tmp;
                                tmp = tmp2;
                            }
                            e->bestScores[iBest] = e->scores[iScore];
                            break;
                        }
                    }
                }
            }
            // exit(0);
            e->cursorMinute += e->nbrBlocks * e->nbrThreads;
            if (e->coins[e->cursorCoin]->size - e->cursorMinute <=
                e->nbrBlocks * e->nbrThreads) {
                break;
            }
        }
    }
    // printBestScores();
    int nbrChars = 0;
    for (int i = 0; i < e->nbrScores; i++) {
        nbrChars += sprintf(
            &e->result[nbrChars], "%lf|%s|%ld\n", e->bestScores[i].score,
            e->coins[e->bestScores[i].coinId]->name, e->bestScores[i].minuteId);
    }
    return e->result;
    // printf("%s\n", e->result);
}

extern "C" void init(int size, char* files[]) {
    cudaMallocManaged(&e, sizeof(Env));
    cudaMallocManaged(&e->coins, sizeof(void*) * size);
    cudaMallocManaged(&e->source, sizeof(Coin));
    cudaMallocManaged(&e->src, sizeof(Minute) * MAX_SIT_SIZE);
    e->result = (char*)malloc(MAX_SCORE_NBR * 1024);

    e->nbrCoins = 0;
    e->cursorCoin = 0;
    e->nbrBlocks = 256;
    e->nbrThreads = 128;
    e->nbrScores = 2000;

    e->nbrBlocks = 256;
    e->nbrThreads = 256;

    cudaMallocManaged(&e->scores, sizeof(Score) * e->nbrThreads * e->nbrBlocks);
    cudaMallocManaged(&e->bestScores, sizeof(Score) * MAX_SCORE_NBR);
    e->sitSize = 600;
    char path[128];
    for (int i = 0; i < size; i++) {
        snprintf(path, sizeof(path), "./data/%s", files[i]);
        int fd = open(path, O_RDONLY);
        if (fd < 0) {
            continue;
        }
        cudaMallocManaged(&e->coins[i], sizeof(Coin));
        struct stat buf;
        fstat(fd, &buf);
        off_t sizeAll = buf.st_size;
        cudaMallocManaged(&e->coins[i]->minutes, sizeAll);
        int res = read(fd, e->coins[i]->minutes, sizeAll);
        e->coins[i]->size = sizeAll / sizeof(Minute);
        snprintf(e->coins[i]->name, strlen(files[i]) + 1, "%s", files[i]);
        e->nbrCoins += 1;
        close(fd);
    }
}
	.file	"tmpxft_00379ba2_00000000-6_core.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2049:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2049:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%.15lf %s %ld\n"
.LC1:
	.string	"\n"
	.text
	.globl	_Z15printBestScoresv
	.type	_Z15printBestScoresv, @function
_Z15printBestScoresv:
.LFB2044:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC0(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	xorl	%ebx, %ebx
	pushq	%rcx
	.cfi_def_cfa_offset 32
.L3:
	movq	e(%rip), %rdx
	cmpl	%ebx, 12(%rdx)
	jle	.L7
	imulq	$24, %rbx, %rax
	addq	32(%rdx), %rax
	movq	64(%rdx), %rdx
	incq	%rbx
	movq	16(%rax), %rcx
	movsd	(%rax), %xmm0
	movl	$2, %edi
	movq	(%rdx,%rcx,8), %rsi
	imulq	$48, 8(%rax), %rdx
	movb	$1, %al
	addq	136(%rsi), %rdx
	movq	(%rdx), %rcx
	movq	%rsi, %rdx
	movq	%rbp, %rsi
	call	__printf_chk@PLT
	jmp	.L3
.L7:
	popq	%rdx
	.cfi_def_cfa_offset 24
	leaq	.LC1(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 16
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2044:
	.size	_Z15printBestScoresv, .-_Z15printBestScoresv
	.section	.rodata.str1.1
.LC2:
	.string	"./data/%s"
.LC3:
	.string	"%s"
	.text
	.globl	init
	.type	init, @function
init:
.LFB2046:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$1, %edx
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rsi, %r13
	movl	$80, %esi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%edi, %r12d
	leaq	e(%rip), %rdi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$312, %rsp
	.cfi_def_cfa_offset 368
	movq	%fs:40, %rax
	movq	%rax, 296(%rsp)
	xorl	%eax, %eax
	leaq	168(%rsp), %r15
	call	cudaMallocManaged@PLT
	movq	e(%rip), %rax
	movslq	%r12d, %rsi
	movl	$1, %edx
	salq	$3, %rsi
	leaq	64(%rax), %rdi
	call	cudaMallocManaged@PLT
	movl	$1, %edx
	movl	$144, %esi
	movq	e(%rip), %rax
	leaq	16(%rax), %rdi
	call	cudaMallocManaged@PLT
	movl	$1, %edx
	movl	$192000, %esi
	movq	e(%rip), %rax
	leaq	48(%rax), %rdi
	call	cudaMallocManaged@PLT
	movq	e(%rip), %rbx
	movl	$2048000, %edi
	call	malloc@PLT
	xorl	%edx, %edx
	movl	$1572864, %esi
	movq	%rax, 72(%rbx)
	xorl	%eax, %eax
	leaq	24(%rbx), %rdi
	movq	%rax, 40(%rbx)
	movabsq	$1099511628032, %rax
	movl	%edx, 56(%rbx)
	movl	$1, %edx
	movl	$2000, 12(%rbx)
	movq	%rax, (%rbx)
	xorl	%ebx, %ebx
	call	cudaMallocManaged@PLT
	movl	$1, %edx
	movl	$48000, %esi
	movq	e(%rip), %rax
	leaq	32(%rax), %rdi
	call	cudaMallocManaged@PLT
	movq	e(%rip), %rax
	movl	$600, 8(%rax)
.L9:
	cmpl	%ebx, %r12d
	jle	.L14
	movq	0(%r13,%rbx,8), %r9
	movl	$128, %ecx
	movq	%r15, %rdi
	xorl	%eax, %eax
	leaq	.LC2(%rip), %r8
	movl	$2, %edx
	movl	$128, %esi
	call	__snprintf_chk@PLT
	xorl	%esi, %esi
	movq	%r15, %rdi
	call	__open_2@PLT
	movl	%eax, %ebp
	testl	%eax, %eax
	js	.L10
	movq	e(%rip), %rax
	movl	$1, %edx
	movl	$144, %esi
	movq	64(%rax), %rax
	leaq	(%rax,%rbx,8), %rdi
	call	cudaMallocManaged@PLT
	leaq	24(%rsp), %rsi
	movl	%ebp, %edi
	call	fstat@PLT
	movq	72(%rsp), %rax
	movl	$1, %edx
	movq	%rax, 8(%rsp)
	movq	e(%rip), %rax
	movq	8(%rsp), %rsi
	movq	64(%rax), %rax
	movq	(%rax,%rbx,8), %rdi
	addq	$136, %rdi
	call	cudaMallocManaged@PLT
	movq	8(%rsp), %rdx
	orq	$-1, %rcx
	movl	%ebp, %edi
	movq	e(%rip), %rax
	movq	64(%rax), %rax
	movq	(%rax,%rbx,8), %rax
	movq	136(%rax), %rsi
	call	__read_chk@PLT
	movq	e(%rip), %rax
	movl	$48, %ecx
	xorl	%edx, %edx
	movq	0(%r13,%rbx,8), %r9
	movq	64(%rax), %rax
	movq	%r9, %rdi
	movq	(%rax,%rbx,8), %r14
	movq	8(%rsp), %rax
	movq	%r9, 8(%rsp)
	divq	%rcx
	movq	%rax, 128(%r14)
	call	strlen@PLT
	movq	8(%rsp), %r9
	movq	%r14, %rdi
	leaq	.LC3(%rip), %r8
	leaq	1(%rax), %rsi
	movl	$128, %ecx
	movl	$2, %edx
	xorl	%eax, %eax
	call	__snprintf_chk@PLT
	movq	e(%rip), %rax
	movl	%ebp, %edi
	incq	40(%rax)
	call	close@PLT
.L10:
	incq	%rbx
	jmp	.L9
.L14:
	movq	296(%rsp), %rax
	subq	%fs:40, %rax
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	addq	$312, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2046:
	.size	init, .-init
	.globl	_Z29__device_stub__Z7compareP3EnvP3Env
	.type	_Z29__device_stub__Z7compareP3EnvP3Env, @function
_Z29__device_stub__Z7compareP3EnvP3Env:
.LFB2071:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	16(%rsp), %rdx
	leaq	44(%rsp), %rsi
	leaq	32(%rsp), %rdi
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L15
	pushq	24(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z7compareP3Env(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 128
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
.L15:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L17
	call	__stack_chk_fail@PLT
.L17:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2071:
	.size	_Z29__device_stub__Z7compareP3EnvP3Env, .-_Z29__device_stub__Z7compareP3EnvP3Env
	.globl	_Z7compareP3Env
	.type	_Z7compareP3Env, @function
_Z7compareP3Env:
.LFB2072:
	.cfi_startproc
	endbr64
	jmp	_Z29__device_stub__Z7compareP3EnvP3Env
	.cfi_endproc
.LFE2072:
	.size	_Z7compareP3Env, .-_Z7compareP3Env
	.section	.rodata.str1.1
.LC5:
	.string	"CUDA error: %s\n"
.LC6:
	.string	"%lf|%s|%ld\n"
	.text
	.globl	bake
	.type	bake, @function
bake:
.LFB2045:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movslq	%edi, %rcx
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	imulq	$48, %rcx, %rcx
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	movq	e(%rip), %rax
	movsd	.LC4(%rip), %xmm0
	movl	%edi, 8(%rax)
	movq	48(%rax), %rax
	movq	%rax, %rdi
	xorl	%eax, %eax
	rep movsb
	movq	e(%rip), %rdx
	movl	12(%rdx), %esi
.L21:
	cmpl	%eax, %esi
	jle	.L42
	imulq	$24, %rax, %rcx
	addq	32(%rdx), %rcx
	incq	%rax
	movsd	%xmm0, (%rcx)
	jmp	.L21
.L42:
	movabsq	$4294967297, %rbx
	xorl	%ecx, %ecx
	movl	%ecx, 56(%rdx)
.L23:
	movq	e(%rip), %rax
	movslq	56(%rax), %rdx
	cmpq	40(%rax), %rdx
	jge	.L43
	xorl	%edx, %edx
	movl	%edx, 60(%rax)
.L35:
	movq	e(%rip), %rax
	movq	%rbx, 24(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%rbx, 12(%rsp)
	movl	28(%rsp), %ecx
	movl	(%rax), %edx
	movl	4(%rax), %eax
	movl	16(%rsp), %esi
	movl	%edx, 20(%rsp)
	movq	20(%rsp), %rdx
	movl	%eax, 8(%rsp)
	movq	8(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	movq	e(%rip), %rdi
	call	_Z29__device_stub__Z7compareP3EnvP3Env
.L24:
	call	cudaDeviceSynchronize@PLT
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	jne	.L25
	movq	e(%rip), %rax
	xorl	%r9d, %r9d
	movl	4(%rax), %esi
	imull	(%rax), %esi
	jmp	.L26
.L25:
	call	cudaGetErrorString@PLT
	movl	$2, %edi
	leaq	.LC5(%rip), %rsi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L45:
	movups	(%r8), %xmm2
	movups	%xmm2, (%rdi)
	movq	16(%r8), %rdx
	movq	%rdx, 16(%rdi)
.L28:
	incq	%r9
.L26:
	cmpl	%r9d, %esi
	jle	.L44
	movslq	12(%rax), %rdx
	movq	32(%rax), %r11
	imulq	$24, %r9, %r8
	addq	24(%rax), %r8
	movq	%rdx, %r10
	imulq	$24, %rdx, %rdx
	movsd	(%r8), %xmm1
	movsd	-24(%r11,%rdx), %xmm0
	comisd	%xmm1, %xmm0
	jb	.L28
	movq	%r11, %rcx
	xorl	%edx, %edx
.L29:
	cmpl	%edx, %r10d
	jle	.L28
	movsd	(%rcx), %xmm0
	incl	%edx
	movq	%rcx, %rdi
	leaq	24(%rcx), %rcx
	comisd	%xmm1, %xmm0
	jbe	.L29
	movslq	%edx, %rcx
	movq	8(%rdi), %rbp
	movq	16(%rdi), %r12
	imulq	$24, %rcx, %rdx
	addq	%r11, %rdx
.L31:
	cmpl	%ecx, %r10d
	jle	.L45
	movsd	(%rdx), %xmm1
	movq	8(%rdx), %r11
	movsd	%xmm0, (%rdx)
	incq	%rcx
	movq	16(%rdx), %r13
	movq	%rbp, 8(%rdx)
	addq	$24, %rdx
	movq	%r12, -8(%rdx)
	movq	%r11, %rbp
	movaps	%xmm1, %xmm0
	movq	%r13, %r12
	jmp	.L31
.L44:
	movslq	56(%rax), %r8
	movq	64(%rax), %rdi
	movl	60(%rax), %ecx
	movq	(%rdi,%r8,8), %rdi
	movq	%r8, %rdx
	addl	%esi, %ecx
	movslq	%esi, %rsi
	movq	128(%rdi), %rdi
	movl	%ecx, 60(%rax)
	movslq	%ecx, %rcx
	subq	%rcx, %rdi
	cmpq	%rsi, %rdi
	jg	.L35
	incl	%edx
	movl	%edx, 56(%rax)
	jmp	.L23
.L43:
	xorl	%ebx, %ebx
	xorl	%r12d, %r12d
	leaq	.LC6(%rip), %rbp
.L37:
	movq	e(%rip), %rcx
	movq	72(%rcx), %rax
	cmpl	%ebx, 12(%rcx)
	jle	.L46
	imulq	$24, %rbx, %rdx
	addq	32(%rcx), %rdx
	movq	64(%rcx), %rcx
	movslq	%r12d, %rdi
	movq	16(%rdx), %rsi
	movq	8(%rdx), %r9
	addq	%rax, %rdi
	movb	$1, %al
	movsd	(%rdx), %xmm0
	orq	$-1, %rdx
	incq	%rbx
	movq	(%rcx,%rsi,8), %r8
	movq	%rbp, %rcx
	movl	$2, %esi
	call	__sprintf_chk@PLT
	addl	%eax, %r12d
	jmp	.L37
.L46:
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2045:
	.size	bake, .-bake
	.section	.rodata.str1.1
.LC7:
	.string	"_Z7compareP3Env"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2074:
	.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	.LC7(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z7compareP3Env(%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
.LFE2074:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	e
	.bss
	.align 8
	.type	e, @object
	.size	e, 8
e:
	.zero	8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC4:
	.long	-1577066496
	.long	1114446484
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_SCORE_NBR 2000
#define MAX_SIT_SIZE 4000

// typedef struct bestScore BestScore;

typedef struct {
    long time;
    double open;
    double high;
    double low;
    double close;
    double volume;
} Minute;

typedef struct {
    char name[128];
    long size;
    Minute* minutes;
} Coin;

typedef struct {
    double score;
    long minuteId;
    long coinId;
} Score;

typedef struct {
    // POTARD
    int nbrThreads;
    int nbrBlocks;
    int sitSize;
    int nbrScores;

    Coin* source;
    Score* scores;
    Score* bestScores;
    long nbrCoins;
    Minute* src;
    int cursorCoin;
    int cursorMinute;
    Coin** coins;
    char* result;
} Env;

Env* e;

#define STEP_SIZE 20

__global__ void compare(Env* e) {
    int workerNbr = threadIdx.x * e->nbrThreads + blockIdx.x;
    int cursorMinute = workerNbr + e->cursorMinute;
    double score = 0;
    int step = 0;
    for (int i = 1; i < e->sitSize; i++) {
        double destPourcentOpen =
            e->coins[e->cursorCoin]->minutes[cursorMinute + step].open /
            e->coins[e->cursorCoin]->minutes[cursorMinute + i].open * 1000;
        double srcPourcentOpen = e->src[0 + step].open / e->src[i].open * 1000;
        score += abs(destPourcentOpen - srcPourcentOpen);
        if (step > STEP_SIZE){
            step += 1;
        }
            
    }
    e->scores[workerNbr].score = score;
    e->scores[workerNbr].minuteId = cursorMinute;
    e->scores[workerNbr].coinId = e->cursorCoin;
}

void printBestScores() {
    for (int i = 0; i < e->nbrScores; i++) {
        // for (int i = 0; i < 2; i++) {
        printf("%.15lf %s %ld\n", e->bestScores[i].score,
               e->coins[e->bestScores[i].coinId]->name,
               e->coins[e->bestScores[i].coinId]
                   ->minutes[e->bestScores[i].minuteId]
                   .time);
    }
    printf("\n");
}

extern "C" char* bake(int sitSize, Minute* minutes) {
    e->sitSize = sitSize;
    memcpy(e->src, minutes, sizeof(Minute) * sitSize);
    for (int iBest = 0; iBest < e->nbrScores; iBest++) {
        e->bestScores[iBest].score = 999999999999;
    }
    for (e->cursorCoin = 0; e->cursorCoin < e->nbrCoins; e->cursorCoin++) {
        // printf("%s\n", e->coins[e->cursorCoin]->name);
        e->cursorMinute = 0;
        while (1) {
            compare<<<e->nbrBlocks, e->nbrThreads>>>(e);
            hipDeviceSynchronize();
            hipError_t error = hipGetLastError();
            if (error != hipSuccess) {
                printf("CUDA error: %s\n", hipGetErrorString(error));
                exit(-1);
            }
            for (int iScore = 0; iScore < e->nbrBlocks * e->nbrThreads;
                 iScore++) {
                if (e->scores[iScore].score <=
                    e->bestScores[e->nbrScores - 1].score) {
                    // printf("%lf %lf %s\n",
                    // e->scores[iScore].score,e->coins[e->scores[iScore].coinId]->minutes[e->scores[iScore].minuteId].volume,
                    // e->coins[e->scores[iScore].coinId]->name);

                    for (int iBest = 0; iBest < e->nbrScores; iBest++) {
                        if (e->scores[iScore].score <
                            e->bestScores[iBest].score) {
                            Score tmp = e->bestScores[iBest];
                            for (int iTmp = iBest + 1; iTmp < e->nbrScores;
                                 iTmp++) {
                                Score tmp2 = e->bestScores[iTmp];
                                e->bestScores[iTmp] = tmp;
                                tmp = tmp2;
                            }
                            e->bestScores[iBest] = e->scores[iScore];
                            break;
                        }
                    }
                }
            }
            // exit(0);
            e->cursorMinute += e->nbrBlocks * e->nbrThreads;
            if (e->coins[e->cursorCoin]->size - e->cursorMinute <=
                e->nbrBlocks * e->nbrThreads) {
                break;
            }
        }
    }
    // printBestScores();
    int nbrChars = 0;
    for (int i = 0; i < e->nbrScores; i++) {
        nbrChars += sprintf(
            &e->result[nbrChars], "%lf|%s|%ld\n", e->bestScores[i].score,
            e->coins[e->bestScores[i].coinId]->name, e->bestScores[i].minuteId);
    }
    return e->result;
    // printf("%s\n", e->result);
}

extern "C" void init(int size, char* files[]) {
    hipMallocManaged(&e, sizeof(Env));
    hipMallocManaged(&e->coins, sizeof(void*) * size);
    hipMallocManaged(&e->source, sizeof(Coin));
    hipMallocManaged(&e->src, sizeof(Minute) * MAX_SIT_SIZE);
    e->result = (char*)malloc(MAX_SCORE_NBR * 1024);

    e->nbrCoins = 0;
    e->cursorCoin = 0;
    e->nbrBlocks = 256;
    e->nbrThreads = 128;
    e->nbrScores = 2000;

    e->nbrBlocks = 256;
    e->nbrThreads = 256;

    hipMallocManaged(&e->scores, sizeof(Score) * e->nbrThreads * e->nbrBlocks);
    hipMallocManaged(&e->bestScores, sizeof(Score) * MAX_SCORE_NBR);
    e->sitSize = 600;
    char path[128];
    for (int i = 0; i < size; i++) {
        snprintf(path, sizeof(path), "./data/%s", files[i]);
        int fd = open(path, O_RDONLY);
        if (fd < 0) {
            continue;
        }
        hipMallocManaged(&e->coins[i], sizeof(Coin));
        struct stat buf;
        fstat(fd, &buf);
        off_t sizeAll = buf.st_size;
        hipMallocManaged(&e->coins[i]->minutes, sizeAll);
        int res = read(fd, e->coins[i]->minutes, sizeAll);
        e->coins[i]->size = sizeAll / sizeof(Minute);
        snprintf(e->coins[i]->name, strlen(files[i]) + 1, "%s", files[i]);
        e->nbrCoins += 1;
        close(fd);
    }
}
	.text
	.file	"core.hip"
	.globl	_Z22__device_stub__compareP3Env # -- Begin function _Z22__device_stub__compareP3Env
	.type	_Z22__device_stub__compareP3Env,@function
_Z22__device_stub__compareP3Env:        # @_Z22__device_stub__compareP3Env
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$64, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	movq	%rsp, %rbx
	movq	%rax, (%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z7compareP3Env, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$80, %rsp
	.cfi_adjust_cfa_offset -80
	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	_Z22__device_stub__compareP3Env, .Lfunc_end0-_Z22__device_stub__compareP3Env
	.cfi_endproc
                                        # -- End function
	.globl	_Z15printBestScoresv            # -- Begin function _Z15printBestScoresv
	.type	_Z15printBestScoresv,@function
_Z15printBestScoresv:                   # @_Z15printBestScoresv
	.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	e(%rip), %rax
	cmpl	$0, 12(%rax)
	jle	.LBB1_3
# %bb.1:                                # %.lr.ph.preheader
	movl	$16, %ebx
	xorl	%r14d, %r14d
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	32(%rax), %rcx
	movq	64(%rax), %rax
	movsd	-16(%rcx,%rbx), %xmm0           # xmm0 = mem[0],zero
	movq	(%rcx,%rbx), %rdx
	movq	(%rax,%rdx,8), %rsi
	movq	136(%rsi), %rax
	movq	-8(%rcx,%rbx), %rcx
	leaq	(%rcx,%rcx,2), %rcx
	shlq	$4, %rcx
	movq	(%rax,%rcx), %rdx
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	incq	%r14
	movq	e(%rip), %rax
	movslq	12(%rax), %rcx
	addq	$24, %rbx
	cmpq	%rcx, %r14
	jl	.LBB1_2
.LBB1_3:                                # %._crit_edge
	movl	$10, %edi
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	putchar@PLT                     # TAILCALL
.Lfunc_end1:
	.size	_Z15printBestScoresv, .Lfunc_end1-_Z15printBestScoresv
	.cfi_endproc
                                        # -- End function
	.globl	bake                            # -- Begin function bake
	.type	bake,@function
bake:                                   # @bake
	.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
	subq	$56, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	e(%rip), %rax
	movl	%edi, 8(%rax)
	movq	48(%rax), %rax
	movslq	%edi, %rcx
	shlq	$4, %rcx
	leaq	(%rcx,%rcx,2), %rdx
	movq	%rax, %rdi
	callq	memcpy@PLT
	movq	e(%rip), %rax
	movl	12(%rax), %ecx
	testl	%ecx, %ecx
	jle	.LBB2_3
# %bb.1:                                # %.lr.ph
	movq	32(%rax), %rdx
	leaq	(,%rcx,8), %rsi
	leaq	(%rsi,%rsi,2), %rsi
	xorl	%edi, %edi
	movabsq	$4786511204640088064, %r8       # imm = 0x426D1A94A1FFE000
.LBB2_2:                                # =>This Inner Loop Header: Depth=1
	movq	%r8, (%rdx,%rdi)
	addq	$24, %rdi
	cmpq	%rdi, %rsi
	jne	.LBB2_2
.LBB2_3:                                # %._crit_edge
	movl	$0, 56(%rax)
	cmpq	$0, 40(%rax)
	jle	.LBB2_14
# %bb.4:                                # %.lr.ph72.preheader
	movabsq	$4294967296, %rbx               # imm = 0x100000000
	movl	(%rax), %ecx
	movl	4(%rax), %edx
.LBB2_5:                                # %.lr.ph72
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_6 Depth 2
                                        #       Child Loop BB2_20 Depth 3
                                        #         Child Loop BB2_23 Depth 4
                                        #         Child Loop BB2_25 Depth 4
	movl	$0, 60(%rax)
.LBB2_6:                                #   Parent Loop BB2_5 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB2_20 Depth 3
                                        #         Child Loop BB2_23 Depth 4
                                        #         Child Loop BB2_25 Depth 4
	movl	%edx, %edi
	orq	%rbx, %rdi
	movl	%ecx, %edx
	orq	%rbx, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_8
# %bb.7:                                #   in Loop: Header=BB2_6 Depth=2
	movq	e(%rip), %rdi
	callq	_Z22__device_stub__compareP3Env
.LBB2_8:                                #   in Loop: Header=BB2_6 Depth=2
	callq	hipDeviceSynchronize
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB2_18
# %bb.9:                                # %.preheader46
                                        #   in Loop: Header=BB2_6 Depth=2
	movq	e(%rip), %rax
	movl	(%rax), %ecx
	movl	4(%rax), %edx
	movl	%ecx, %r8d
	imull	%edx, %r8d
	testl	%r8d, %r8d
	jle	.LBB2_10
# %bb.19:                               # %.lr.ph66
                                        #   in Loop: Header=BB2_6 Depth=2
	xorl	%esi, %esi
.LBB2_20:                               #   Parent Loop BB2_5 Depth=1
                                        #     Parent Loop BB2_6 Depth=2
                                        # =>    This Loop Header: Depth=3
                                        #         Child Loop BB2_23 Depth 4
                                        #         Child Loop BB2_25 Depth 4
	movq	24(%rax), %r8
	movq	32(%rax), %r10
	leaq	(%rsi,%rsi,2), %rdi
	movsd	(%r8,%rdi,8), %xmm0             # xmm0 = mem[0],zero
	movslq	12(%rax), %r9
	leaq	(%r9,%r9,2), %r11
	movsd	-24(%r10,%r11,8), %xmm1         # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	jb	.LBB2_29
# %bb.21:                               #   in Loop: Header=BB2_20 Depth=3
	testl	%r9d, %r9d
	jle	.LBB2_29
# %bb.22:                               # %.lr.ph59.preheader
                                        #   in Loop: Header=BB2_20 Depth=3
	movl	%r9d, %r15d
	movl	$24, %r9d
	movl	$1, %r11d
	xorl	%r12d, %r12d
.LBB2_23:                               # %.lr.ph59
                                        #   Parent Loop BB2_5 Depth=1
                                        #     Parent Loop BB2_6 Depth=2
                                        #       Parent Loop BB2_20 Depth=3
                                        # =>      This Inner Loop Header: Depth=4
	leaq	(%r12,%r12,2), %r14
	movsd	(%r10,%r14,8), %xmm1            # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	ja	.LBB2_24
# %bb.28:                               #   in Loop: Header=BB2_23 Depth=4
	incq	%r12
	addq	$24, %r9
	incl	%r11d
	cmpq	%r15, %r12
	jne	.LBB2_23
	jmp	.LBB2_29
.LBB2_24:                               #   in Loop: Header=BB2_20 Depth=3
	leaq	(%r10,%r14,8), %rcx
	movq	16(%rcx), %rdx
	movq	%rdx, 16(%rsp)
	movupd	(%rcx), %xmm0
	movapd	%xmm0, (%rsp)
	incl	%r12d
	cmpl	%r15d, %r12d
	jge	.LBB2_27
.LBB2_25:                               # %.lr.ph63
                                        #   Parent Loop BB2_5 Depth=1
                                        #     Parent Loop BB2_6 Depth=2
                                        #       Parent Loop BB2_20 Depth=3
                                        # =>      This Inner Loop Header: Depth=4
	movq	32(%rax), %rcx
	movq	16(%rcx,%r9), %rdx
	movq	%rdx, 48(%rsp)
	movups	(%rcx,%r9), %xmm0
	movaps	%xmm0, 32(%rsp)
	movq	16(%rsp), %rdx
	movq	%rdx, 16(%rcx,%r9)
	movaps	(%rsp), %xmm0
	movups	%xmm0, (%rcx,%r9)
	movq	48(%rsp), %rcx
	movq	%rcx, 16(%rsp)
	movapd	32(%rsp), %xmm0
	movapd	%xmm0, (%rsp)
	addq	$24, %r9
	incl	%r11d
	cmpl	%r11d, 12(%rax)
	jg	.LBB2_25
# %bb.26:                               # %._crit_edge64.loopexit
                                        #   in Loop: Header=BB2_20 Depth=3
	movq	24(%rax), %r8
	movq	32(%rax), %r10
.LBB2_27:                               # %._crit_edge64
                                        #   in Loop: Header=BB2_20 Depth=3
	movq	16(%r8,%rdi,8), %rcx
	movq	%rcx, 16(%r10,%r14,8)
	movupd	(%r8,%rdi,8), %xmm0
	movupd	%xmm0, (%r10,%r14,8)
	movl	(%rax), %ecx
	movl	4(%rax), %edx
.LBB2_29:                               # %.loopexit
                                        #   in Loop: Header=BB2_20 Depth=3
	incq	%rsi
	movl	%ecx, %r8d
	imull	%edx, %r8d
	movslq	%r8d, %rdi
	cmpq	%rdi, %rsi
	jl	.LBB2_20
	jmp	.LBB2_11
.LBB2_10:                               # %.preheader46.._crit_edge67_crit_edge
                                        #   in Loop: Header=BB2_6 Depth=2
	movslq	%r8d, %rdi
.LBB2_11:                               # %._crit_edge67
                                        #   in Loop: Header=BB2_6 Depth=2
	movslq	60(%rax), %rsi
	movslq	%r8d, %r8
	addq	%rsi, %r8
	movl	%r8d, 60(%rax)
	movq	64(%rax), %r9
	movslq	56(%rax), %rsi
	movq	(%r9,%rsi,8), %r9
	movq	128(%r9), %r9
	subq	%r8, %r9
	cmpq	%rdi, %r9
	jg	.LBB2_6
# %bb.12:                               #   in Loop: Header=BB2_5 Depth=1
	incq	%rsi
	movl	%esi, 56(%rax)
	cmpq	%rsi, 40(%rax)
	jg	.LBB2_5
# %bb.13:                               # %.preheader.loopexit
	movl	12(%rax), %ecx
.LBB2_14:                               # %.preheader
	testl	%ecx, %ecx
	jle	.LBB2_17
# %bb.15:                               # %.lr.ph75.preheader
	xorl	%ebx, %ebx
	movl	$16, %r14d
	xorl	%r15d, %r15d
.LBB2_16:                               # %.lr.ph75
                                        # =>This Inner Loop Header: Depth=1
	movslq	%r15d, %r15
	movq	72(%rax), %rdi
	addq	%r15, %rdi
	movq	32(%rax), %rcx
	movq	64(%rax), %rax
	movsd	-16(%rcx,%r14), %xmm0           # xmm0 = mem[0],zero
	movq	(%rcx,%r14), %rdx
	movq	(%rax,%rdx,8), %rdx
	movq	-8(%rcx,%r14), %rcx
	movl	$.L.str.3, %esi
	movb	$1, %al
	callq	sprintf
	addl	%eax, %r15d
	incq	%rbx
	movq	e(%rip), %rax
	movslq	12(%rax), %rcx
	addq	$24, %r14
	cmpq	%rcx, %rbx
	jl	.LBB2_16
.LBB2_17:                               # %._crit_edge76
	movq	72(%rax), %rax
	addq	$56, %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
.LBB2_18:
	.cfi_def_cfa_offset 96
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.2, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$-1, %edi
	callq	exit
.Lfunc_end2:
	.size	bake, .Lfunc_end2-bake
	.cfi_endproc
                                        # -- End function
	.globl	init                            # -- Begin function init
	.type	init,@function
init:                                   # @init
	.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	$280, %rsp                      # imm = 0x118
	.cfi_def_cfa_offset 336
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movl	%edi, %ebp
	movl	$e, %edi
	movl	$80, %esi
	movl	$1, %edx
	callq	hipMallocManaged
	movq	e(%rip), %rdi
	addq	$64, %rdi
	movslq	%ebp, %r14
	leaq	(,%r14,8), %rsi
	movl	$1, %edx
	callq	hipMallocManaged
	movq	e(%rip), %rdi
	addq	$16, %rdi
	movl	$144, %esi
	movl	$1, %edx
	callq	hipMallocManaged
	movq	e(%rip), %rdi
	addq	$48, %rdi
	movl	$192000, %esi                   # imm = 0x2EE00
	movl	$1, %edx
	callq	hipMallocManaged
	movl	$2048000, %edi                  # imm = 0x1F4000
	callq	malloc
	movq	e(%rip), %rdi
	movq	%rax, 72(%rdi)
	movq	$0, 40(%rdi)
	movl	$0, 56(%rdi)
	movl	$2000, 12(%rdi)                 # imm = 0x7D0
	movabsq	$1099511628032, %rax            # imm = 0x10000000100
	movq	%rax, (%rdi)
	addq	$24, %rdi
	movl	$1572864, %esi                  # imm = 0x180000
	movl	$1, %edx
	callq	hipMallocManaged
	movq	e(%rip), %rdi
	addq	$32, %rdi
	movl	$48000, %esi                    # imm = 0xBB80
	movl	$1, %edx
	callq	hipMallocManaged
	movq	e(%rip), %rax
	movl	$600, 8(%rax)                   # imm = 0x258
	testl	%r14d, %r14d
	jle	.LBB3_5
# %bb.1:                                # %.lr.ph
	movl	%ebp, %r15d
	shlq	$3, %r15
	xorl	%r14d, %r14d
.LBB3_2:                                # =>This Inner Loop Header: Depth=1
	movq	(%rbx,%r14), %rcx
	movl	$128, %esi
	movl	$.L.str.4, %edx
	leaq	144(%rsp), %r12
	movq	%r12, %rdi
	xorl	%eax, %eax
	callq	snprintf
	movq	%r12, %rdi
	xorl	%esi, %esi
	xorl	%eax, %eax
	callq	open
	testl	%eax, %eax
	js	.LBB3_4
# %bb.3:                                #   in Loop: Header=BB3_2 Depth=1
	movl	%eax, %ebp
	movq	e(%rip), %rax
	movq	64(%rax), %rdi
	addq	%r14, %rdi
	movl	$144, %esi
	movl	$1, %edx
	callq	hipMallocManaged
	movl	%ebp, %edi
	movq	%rsp, %rsi
	callq	fstat
	movq	48(%rsp), %r12
	movq	e(%rip), %rax
	movq	64(%rax), %rax
	movq	(%rax,%r14), %rdi
	movl	$136, %eax
	addq	%rax, %rdi
	movq	%r12, %rsi
	movl	$1, %edx
	callq	hipMallocManaged
	movq	e(%rip), %rax
	movq	64(%rax), %rax
	movq	(%rax,%r14), %rax
	movq	136(%rax), %rsi
	movl	%ebp, %edi
	movq	%r12, %rdx
	callq	read
	movq	%r12, %rax
	movabsq	$-6148914691236517205, %rcx     # imm = 0xAAAAAAAAAAAAAAAB
	mulq	%rcx
	shrq	$5, %rdx
	movq	e(%rip), %rax
	movq	64(%rax), %rax
	movq	(%rax,%r14), %r12
	movq	%rdx, 128(%r12)
	movq	(%rbx,%r14), %r13
	movq	%r13, %rdi
	callq	strlen
	leaq	1(%rax), %rsi
	movl	$.L.str.5, %edx
	movq	%r12, %rdi
	movq	%r13, %rcx
	xorl	%eax, %eax
	callq	snprintf
	movq	e(%rip), %rax
	incq	40(%rax)
	movl	%ebp, %edi
	callq	close
.LBB3_4:                                #   in Loop: Header=BB3_2 Depth=1
	addq	$8, %r14
	cmpq	%r14, %r15
	jne	.LBB3_2
.LBB3_5:                                # %._crit_edge
	addq	$280, %rsp                      # imm = 0x118
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	init, .Lfunc_end3-init
	.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	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z7compareP3Env, %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_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__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	.LBB5_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
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	e,@object                       # @e
	.bss
	.globl	e
	.p2align	3, 0x0
e:
	.quad	0
	.size	e, 8

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

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%.15lf %s %ld\n"
	.size	.L.str, 15

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"CUDA error: %s\n"
	.size	.L.str.2, 16

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"%lf|%s|%ld\n"
	.size	.L.str.3, 12

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"./data/%s"
	.size	.L.str.4, 10

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%s"
	.size	.L.str.5, 3

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z7compareP3Env"
	.size	.L__unnamed_1, 16

	.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

	.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 _Z22__device_stub__compareP3Env
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym e
	.addrsig_sym _Z7compareP3Env
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z7compareP3Env         ; -- Begin function _Z7compareP3Env
	.globl	_Z7compareP3Env
	.p2align	8
	.type	_Z7compareP3Env,@function
_Z7compareP3Env:                        ; @_Z7compareP3Env
; %bb.0:
	s_load_b64 s[2:3], s[0:1], 0x0
	s_mov_b32 s4, s15
	s_waitcnt lgkmcnt(0)
	s_clause 0x2
	s_load_b32 s0, s[2:3], 0x0
	s_load_b32 s1, s[2:3], 0x3c
	s_load_b32 s8, s[2:3], 0x8
	s_waitcnt lgkmcnt(0)
	v_mad_u64_u32 v[4:5], null, s0, v0, s[4:5]
	s_cmp_gt_i32 s8, 1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v2, s1, v4
	v_ashrrev_i32_e32 v3, 31, v2
	s_cbranch_scc1 .LBB0_2
; %bb.1:                                ; %.._crit_edge_crit_edge
	s_load_b32 s4, s[2:3], 0x38
	s_mov_b32 s6, 0
	s_mov_b64 s[0:1], 0
	s_waitcnt lgkmcnt(0)
	s_ashr_i32 s5, s4, 31
	s_branch .LBB0_3
.LBB0_2:
	s_mov_b32 s6, -1
                                        ; implicit-def: $sgpr0_sgpr1
                                        ; implicit-def: $sgpr4_sgpr5
.LBB0_3:                                ; %Flow55
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 vcc_lo, exec_lo, s6
	s_cbranch_vccnz .LBB0_6
; %bb.4:                                ; %.lr.ph
	s_clause 0x2
	s_load_b32 s4, s[2:3], 0x38
	s_load_b64 s[0:1], s[2:3], 0x40
	s_load_b64 s[10:11], s[2:3], 0x30
	v_mov_b32_e32 v0, 0
	v_mov_b32_e32 v1, 0
	s_waitcnt lgkmcnt(0)
	s_ashr_i32 s5, s4, 31
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[6:7], s[4:5], 3
	s_add_u32 s0, s0, s6
	s_addc_u32 s1, s1, s7
	s_load_b64 s[6:7], s[10:11], 0x8
	s_load_b64 s[0:1], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	s_load_b64 s[0:1], s[0:1], 0x88
	s_waitcnt lgkmcnt(0)
	v_mad_i64_i32 v[7:8], null, v2, 48, s[0:1]
	s_add_i32 s1, s8, -1
	s_add_u32 s8, s10, 56
	s_addc_u32 s9, s11, 0
	global_load_b64 v[5:6], v[7:8], off offset:8
	v_add_co_u32 v7, vcc_lo, v7, 56
	v_add_co_ci_u32_e32 v8, vcc_lo, 0, v8, vcc_lo
.LBB0_5:                                ; =>This Inner Loop Header: Depth=1
	global_load_b64 v[9:10], v[7:8], off
	s_load_b64 s[10:11], s[8:9], 0x0
	s_add_i32 s1, s1, -1
	s_add_u32 s8, s8, 48
	s_addc_u32 s9, s9, 0
	s_cmp_eq_u32 s1, 0
	s_waitcnt lgkmcnt(0)
	v_div_scale_f64 v[13:14], null, s[10:11], s[10:11], s[6:7]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f64_e32 v[17:18], v[13:14]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[21:22], -v[13:14], v[17:18], 1.0
	v_fma_f64 v[17:18], v[17:18], v[21:22], v[17:18]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[21:22], -v[13:14], v[17:18], 1.0
	v_fma_f64 v[17:18], v[17:18], v[21:22], v[17:18]
	s_waitcnt vmcnt(0)
	v_div_scale_f64 v[11:12], null, v[9:10], v[9:10], v[5:6]
	v_div_scale_f64 v[23:24], vcc_lo, v[5:6], v[9:10], v[5:6]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f64_e32 v[15:16], v[11:12]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[19:20], -v[11:12], v[15:16], 1.0
	v_fma_f64 v[15:16], v[15:16], v[19:20], v[15:16]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[19:20], -v[11:12], v[15:16], 1.0
	v_fma_f64 v[15:16], v[15:16], v[19:20], v[15:16]
	v_div_scale_f64 v[19:20], s0, s[6:7], s[10:11], s[6:7]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f64 v[21:22], v[23:24], v[15:16]
	v_mul_f64 v[25:26], v[19:20], v[17:18]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[11:12], -v[11:12], v[21:22], v[23:24]
	v_fma_f64 v[13:14], -v[13:14], v[25:26], v[19:20]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_div_fmas_f64 v[11:12], v[11:12], v[15:16], v[21:22]
	s_mov_b32 vcc_lo, s0
	v_div_fmas_f64 v[13:14], v[13:14], v[17:18], v[25:26]
	v_add_co_u32 v7, vcc_lo, v7, 48
	v_add_co_ci_u32_e32 v8, vcc_lo, 0, v8, vcc_lo
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_div_fixup_f64 v[9:10], v[11:12], v[9:10], v[5:6]
	v_div_fixup_f64 v[13:14], v[13:14], s[10:11], s[6:7]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[11:12], 0x408f4000, v[13:14]
	v_fma_f64 v[9:10], 0x408f4000, v[9:10], -v[11:12]
	s_delay_alu instid0(VALU_DEP_1)
	v_add_f64 v[0:1], v[0:1], |v[9:10]|
	s_cbranch_scc0 .LBB0_5
	s_branch .LBB0_7
.LBB0_6:
	v_dual_mov_b32 v0, s0 :: v_dual_mov_b32 v1, s1
.LBB0_7:                                ; %Flow56
	s_load_b64 s[0:1], s[2:3], 0x18
	v_dual_mov_b32 v8, s5 :: v_dual_mov_b32 v7, s4
	s_waitcnt lgkmcnt(0)
	v_mad_i64_i32 v[5:6], null, v4, 24, s[0:1]
	s_clause 0x1
	global_store_b128 v[5:6], v[0:3], off
	global_store_b64 v[5:6], v[7:8], off offset:16
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z7compareP3Env
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 8
		.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 27
		.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	_Z7compareP3Env, .Lfunc_end0-_Z7compareP3Env
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 632
; NumSgprs: 18
; NumVgprs: 27
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 3
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 27
; 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
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 8
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z7compareP3Env
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z7compareP3Env.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     27
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
