#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>

#define MAX_CHAINE 100

#define CALLOC(ptr, nr, type)   if (!(ptr = (type *) calloc((size_t)(nr), sizeof(type)))) { \
                                    printf("Erreur lors de l'allocation memoire \n") ;      \
                                    exit (-1);                                              \
                                }


#define FOPEN(fich,fichier,sens)    if ((fich=fopen(fichier,sens)) == NULL) {                   \
                                        printf("Probleme d'ouverture du fichier %s\n",fichier); \
                                        exit(-1);                                               \
                                    }

#define MIN(a, b)         (a < b ? a : b)
#define MAX(a, b)         (a > b ? a : b)

#define MAX_VALEUR         255
#define MIN_VALEUR         0

#define NBPOINTSPARLIGNES 15

#define MAX_DIM_GRID 65535
#define MAX_DIM_BLOCK 1024

#define false 0
#define true 1
#define boolean int

// Clock
#define initTimer struct timeval tv1, tv2; struct timezone tz
#define startTimer gettimeofday(&tv1, &tz)
#define stopTimer gettimeofday(&tv2, &tz)
#define tpsCalcul ((tv2.tv_sec-tv1.tv_sec)*1000000L + (tv2.tv_usec-tv1.tv_usec))


/* KERNEL CUDA */
__global__ void add_vec_scalaire_gpu(int *image, int *res, long N, int le_min, float etalement) {
    long i = (long)blockIdx.x * (long)blockDim.x + (long)threadIdx.x;
    if (i < N) {
        res[i] = ((image[i] - le_min) * etalement);
    }
}


int main(int argc, char *argv[]) {
    /*========================================================================*/
    /* Declaration de variables et allocation memoire */
    /*========================================================================*/

    if (argc < 2) {
        printf("Usage: ./CodeSequentiel <path_to_image> [<blocksize>]\n");
        exit(0);
    }

    long blocksize = 1; // default value
    if (argc == 3) {
        blocksize = atoi(argv[2]);
    }

    int i, n;

    int LE_MIN = MAX_VALEUR;
    int LE_MAX = MIN_VALEUR;

    float ETALEMENT = 0.0;

    int* image;
    int* resultat;
    int X, Y, x, y;
    int TailleImage;

    int P;

    FILE* Src, * Dst;

    char SrcFile[MAX_CHAINE];
    char DstFile[MAX_CHAINE];

    char ligne[MAX_CHAINE];

    boolean inverse = false;

    char *Chemin;

    initTimer; //

    /*========================================================================*/
    /* Recuperation des parametres                                                */
    /*========================================================================*/

    sscanf(argv[1], "%s", SrcFile);

    sprintf(DstFile, "%s.new", SrcFile);

    /*========================================================================*/
    /* Recuperation de l'endroit ou l'on travail                                */
    /*========================================================================*/

    CALLOC(Chemin, MAX_CHAINE, char);
    Chemin = getenv("PWD");
    printf("Repertoire de travail : %s \n\n", Chemin);

    /*========================================================================*/
    /* Ouverture des fichiers                                                */
    /*========================================================================*/

    printf("Operations sur les fichiers\n");

    FOPEN(Src, SrcFile, "r");
    printf("\t Fichier source ouvert (%s) \n", SrcFile);

    FOPEN(Dst, DstFile, "w");
    printf("\t Fichier destination ouvert (%s) \n", DstFile);

    /*========================================================================*/
    /* On effectue la lecture du fichier source */
    /*========================================================================*/

    printf("\t Lecture entete du fichier source ");

    for (i = 0; i < 2; i++) {
        fgets(ligne, MAX_CHAINE, Src);
        fprintf(Dst, "%s", ligne);
    }

    fscanf(Src, " %d %d\n", &X, &Y);
    fprintf(Dst, " %d %d\n", X, Y);

    fgets(ligne, MAX_CHAINE, Src);        /* Lecture du 255         */
    fprintf(Dst, "%s", ligne);

    printf(": OK \n");

    /*========================================================================*/
    /* Allocation memoire pour l'image source et l'image resultat                 */
    /*========================================================================*/

    TailleImage = X * Y;

    CALLOC(image, TailleImage, int);
    CALLOC(resultat, TailleImage, int);
    for (i = 0;i < TailleImage;i++) {
        image[i] = 0;
        resultat[i] = 0;
    }

    x = 0;
    y = 0;

    printf("\t\t Initialisation de l'image [%d ; %d] : Ok \n", X, Y);

    /*========================================================================*/
    /* Lecture du fichier pour remplir l'image source                         */
    /*========================================================================*/

    while (!feof(Src)) {
        n = fscanf(Src, "%d", &P);
        image[y+x] = P;
        LE_MIN = MIN(LE_MIN, P);
        LE_MAX = MAX(LE_MAX, P);
        x++;
        if (n == EOF || (x == X && y == Y - 1)) {
            break;
        }
        if (x == X) {
            x = 0;
            y++;
        }
    }
    fclose(Src);
    printf("\t Lecture du fichier image : Ok \n\n");

    /*========================================================================*/
    /* Calcul du facteur d'etalement                                        */
    /*========================================================================*/

    if (inverse) {
        ETALEMENT = 0.2;
    }
    else {
        ETALEMENT = (float)(MAX_VALEUR - MIN_VALEUR) / (float)(LE_MAX - LE_MIN);
    }

    /*========================================================================*/
    /* Calcul de chaque nouvelle valeur de pixel                              */
    /*========================================================================*/

    int tailleVecteur = TailleImage;
    //long blocksize = 1; // TODO can change from args

    long size = sizeof(int)*tailleVecteur;

    int *cudaVec;
    int *cudaRes;

    // Select cuda GPU device to use (if multiple device)
    cudaSetDevice(0);

    if (cudaMalloc((void **)&cudaVec, size) == cudaErrorMemoryAllocation) {
        printf("Allocation memoire qui pose probleme (cudaVec) \n");
    }
    if (cudaMalloc((void **)&cudaRes, size)  == cudaErrorMemoryAllocation) {
        printf("Allocation memoire qui pose probleme (cudaRes) \n");
    }

    long dimBlock = blocksize;
    long dimGrid = tailleVecteur/blocksize;
    if ((tailleVecteur % blocksize) != 0) {
        dimGrid++;
    }

    int res = cudaMemcpy(&cudaVec[0], &image[0], size, cudaMemcpyHostToDevice);

    printf("Copy CPU -> GPU %d \n",res);
    printf("dimBlock: %ld | dimGrid: %ld\n", dimBlock, dimGrid);

    startTimer;
    add_vec_scalaire_gpu<<<dimGrid, dimBlock>>>(cudaVec, cudaRes, tailleVecteur, LE_MIN, ETALEMENT);
    cudaDeviceSynchronize();
    stopTimer;

    printf("Duration %ld", tpsCalcul);

    cudaMemcpy(&resultat[0], &cudaRes[0], size, cudaMemcpyDeviceToHost);

    cudaFree(cudaVec);
    cudaFree(cudaRes);

    /*========================================================================*/
    /* Sauvegarde de l'image dans le fichier resultat                         */
    /*========================================================================*/

    n = 0;
    for (i = 0; i < TailleImage ; i++) {
        fprintf(Dst, "%3d ", resultat[i]);
        n++;
        if (n == NBPOINTSPARLIGNES) {
            n = 0;
            fprintf(Dst, "\n");
        }
    }

    fprintf(Dst, "\n");
    fclose(Dst);

    printf("\n");

    /*========================================================================*/
    /* Fin du programme principal        */
    /*========================================================================*/

    exit(0);
}

	.file	"tmpxft_00366581_00000000-6_CodeSequentiel.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	_Z45__device_stub__Z20add_vec_scalaire_gpuPiS_lifPiS_lif
	.type	_Z45__device_stub__Z20add_vec_scalaire_gpuPiS_lifPiS_lif, @function
_Z45__device_stub__Z20add_vec_scalaire_gpuPiS_lifPiS_lif:
.LFB2052:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 4(%rsp)
	leaq	40(%rsp), %rcx
	movss	%xmm0, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%rsp, %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z20add_vec_scalaire_gpuPiS_lif(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L2:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2052:
	.size	_Z45__device_stub__Z20add_vec_scalaire_gpuPiS_lifPiS_lif, .-_Z45__device_stub__Z20add_vec_scalaire_gpuPiS_lifPiS_lif
	.globl	_Z20add_vec_scalaire_gpuPiS_lif
	.type	_Z20add_vec_scalaire_gpuPiS_lif, @function
_Z20add_vec_scalaire_gpuPiS_lif:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z45__device_stub__Z20add_vec_scalaire_gpuPiS_lifPiS_lif
	.cfi_endproc
.LFE2053:
	.size	_Z20add_vec_scalaire_gpuPiS_lif, .-_Z20add_vec_scalaire_gpuPiS_lif
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Usage: ./CodeSequentiel <path_to_image> [<blocksize>]\n"
.LC1:
	.string	"%s"
.LC2:
	.string	"%s.new"
.LC3:
	.string	"Erreur lors de l'allocation memoire \n"
.LC4:
	.string	"PWD"
.LC5:
	.string	"Repertoire de travail : %s \n\n"
.LC6:
	.string	"Operations sur les fichiers\n"
.LC7:
	.string	"r"
.LC8:
	.string	"Probleme d'ouverture du fichier %s\n"
.LC9:
	.string	"\t Fichier source ouvert (%s) \n"
.LC10:
	.string	"w"
.LC11:
	.string	"\t Fichier destination ouvert (%s) \n"
.LC12:
	.string	"\t Lecture entete du fichier source "
.LC13:
	.string	" %d %d\n"
.LC14:
	.string	": OK \n"
.LC15:
	.string	"\t\t Initialisation de l'image [%d ; %d] : Ok \n"
.LC16:
	.string	"%d"
.LC17:
	.string	"\t Lecture du fichier image : Ok \n\n"
.LC18:
	.string	"Allocation memoire qui pose probleme (cudaVec) \n"
.LC19:
	.string	"Allocation memoire qui pose probleme (cudaRes) \n"
.LC20:
	.string	"Copy CPU -> GPU %d \n"
.LC21:
	.string	"dimBlock: %ld | dimGrid: %ld\n"
.LC23:
	.string	"Duration %ld"
.LC24:
	.string	"%3d "
.LC25:
	.string	"\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
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$456, %rsp
	.cfi_def_cfa_offset 512
	movq	%fs:40, %rax
	movq	%rax, 440(%rsp)
	xorl	%eax, %eax
	cmpl	$1, %edi
	jg	.L9
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	jmp	.L45
.L9:
	movq	$1, 24(%rsp)
	movq	%rsi, %r12
	cmpl	$3, %edi
	jne	.L10
	movq	16(%rsi), %rdi
	call	atoi@PLT
	cltq
	movq	%rax, 24(%rsp)
.L10:
	movq	8(%r12), %rdi
	leaq	140(%rsp), %rbp
	leaq	.LC1(%rip), %r12
	xorl	%eax, %eax
	movq	%rbp, %rdx
	movq	%r12, %rsi
	leaq	240(%rsp), %r13
	call	__isoc23_sscanf@PLT
	movl	$2, %esi
	movq	%r13, %rdi
	xorl	%eax, %eax
	movq	%rbp, %r8
	leaq	.LC2(%rip), %rcx
	movl	$100, %edx
	call	__sprintf_chk@PLT
	movl	$1, %esi
	movl	$100, %edi
	call	calloc@PLT
	testq	%rax, %rax
	jne	.L11
.L14:
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L43:
	orl	$-1, %edi
.L42:
	call	exit@PLT
.L11:
	leaq	.LC4(%rip), %rdi
	call	getenv@PLT
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC7(%rip), %rsi
	movq	%rbp, %rdi
	call	fopen@PLT
	movq	%rbp, %rdx
	movq	%rax, 16(%rsp)
	testq	%rax, %rax
	je	.L44
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC10(%rip), %rsi
	movq	%r13, %rdi
	call	fopen@PLT
	movq	%r13, %rdx
	movq	%rax, 8(%rsp)
	testq	%rax, %rax
	jne	.L13
.L44:
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L43
.L13:
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	340(%rsp), %rbp
	movq	16(%rsp), %rcx
	movl	$100, %edx
	movl	$100, %esi
	movq	%rbp, %rdi
	leaq	.LC13(%rip), %r13
	call	__fgets_chk@PLT
	movq	8(%rsp), %rdi
	movq	%r12, %rdx
	movq	%rbp, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	16(%rsp), %rcx
	movl	$100, %edx
	movq	%rbp, %rdi
	movl	$100, %esi
	call	__fgets_chk@PLT
	movq	8(%rsp), %rdi
	movq	%r12, %rdx
	movq	%rbp, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	16(%rsp), %rdi
	movq	%r13, %rsi
	xorl	%eax, %eax
	leaq	56(%rsp), %rcx
	leaq	52(%rsp), %rdx
	call	__isoc23_fscanf@PLT
	movl	56(%rsp), %r8d
	movl	52(%rsp), %ecx
	xorl	%eax, %eax
	movq	8(%rsp), %rdi
	movq	%r13, %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movq	16(%rsp), %rcx
	movl	$100, %edx
	movq	%rbp, %rdi
	movl	$100, %esi
	call	__fgets_chk@PLT
	movq	8(%rsp), %rdi
	movq	%r12, %rdx
	movq	%rbp, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	leaq	.LC14(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	52(%rsp), %r12d
	movl	56(%rsp), %r14d
	movl	$4, %esi
	movl	%r12d, %eax
	imull	%r14d, %eax
	movl	%eax, 36(%rsp)
	cltq
	movq	%rax, %rdi
	movq	%rax, 40(%rsp)
	call	calloc@PLT
	movq	%rax, %r13
	testq	%rax, %rax
	je	.L14
	movq	40(%rsp), %rdi
	movl	$4, %esi
	call	calloc@PLT
	movq	%rax, %rbp
	xorl	%eax, %eax
	testq	%rbp, %rbp
	je	.L14
.L15:
	cmpl	%eax, 36(%rsp)
	jle	.L46
	xorl	%edx, %edx
	movl	%edx, 0(%r13,%rax,4)
	movl	%edx, 0(%rbp,%rax,4)
	incq	%rax
	jmp	.L15
.L46:
	movl	%r14d, %ecx
	movl	%r12d, %edx
	leaq	.LC15(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	xorl	%r15d, %r15d
	xorl	%ebx, %ebx
	xorl	%r12d, %r12d
	call	__printf_chk@PLT
	movl	$255, %r14d
.L17:
	movq	16(%rsp), %rdi
	call	feof@PLT
	testl	%eax, %eax
	jne	.L18
	movq	16(%rsp), %rdi
	leaq	60(%rsp), %rdx
	leaq	.LC16(%rip), %rsi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	leal	(%rbx,%r15), %edx
	movl	%eax, %esi
	movl	60(%rsp), %eax
	movslq	%edx, %rdx
	cmpl	%eax, %r14d
	movl	%eax, 0(%r13,%rdx,4)
	cmovg	%eax, %r14d
	cmpl	%eax, %r12d
	cmovl	%eax, %r12d
	incl	%ebx
	incl	%esi
	je	.L18
	cmpl	%ebx, 52(%rsp)
	jne	.L17
	movl	56(%rsp), %eax
	decl	%eax
	cmpl	%r15d, %eax
	je	.L18
	incl	%r15d
	xorl	%ebx, %ebx
	jmp	.L17
.L18:
	movq	16(%rsp), %rdi
	call	fclose@PLT
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	40(%rsp), %rax
	xorl	%edi, %edi
	salq	$2, %rax
	movq	%rax, 16(%rsp)
	call	cudaSetDevice@PLT
	movq	16(%rsp), %rsi
	leaq	64(%rsp), %rdi
	call	cudaMalloc@PLT
	cmpl	$2, %eax
	jne	.L21
	leaq	.LC18(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L21:
	movq	16(%rsp), %rsi
	leaq	72(%rsp), %rdi
	call	cudaMalloc@PLT
	cmpl	$2, %eax
	jne	.L22
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L22:
	movslq	36(%rsp), %rax
	movq	64(%rsp), %rdi
	movl	$1, %ecx
	movq	%r13, %rsi
	leaq	84(%rsp), %r13
	cqto
	movq	%rax, 40(%rsp)
	idivq	24(%rsp)
	cmpq	$1, %rdx
	movq	16(%rsp), %rdx
	sbbq	$-1, %rax
	movq	%rax, %r15
	call	cudaMemcpy@PLT
	leaq	.LC20(%rip), %rsi
	movl	$2, %edi
	movl	%eax, %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	24(%rsp), %rdx
	movq	%r15, %rcx
	xorl	%eax, %eax
	leaq	.LC21(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	104(%rsp), %rdi
	movq	%r13, %rsi
	call	gettimeofday@PLT
	movl	24(%rsp), %eax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	%r15d, 92(%rsp)
	movl	%eax, 120(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 124(%rsp)
	movl	128(%rsp), %ecx
	movq	%rax, 96(%rsp)
	movq	120(%rsp), %rdx
	movq	92(%rsp), %rdi
	movl	100(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	subl	%r14d, %r12d
	movq	40(%rsp), %rdx
	movq	72(%rsp), %rsi
	movl	%r14d, %ecx
	cvtsi2ssl	%r12d, %xmm1
	movss	.LC22(%rip), %xmm0
	movq	64(%rsp), %rdi
	divss	%xmm1, %xmm0
	call	_Z45__device_stub__Z20add_vec_scalaire_gpuPiS_lifPiS_lif
.L24:
	call	cudaDeviceSynchronize@PLT
	movq	%r13, %rsi
	leaq	120(%rsp), %rdi
	xorl	%ebx, %ebx
	call	gettimeofday@PLT
	movq	120(%rsp), %rdx
	xorl	%eax, %eax
	xorl	%r12d, %r12d
	subq	104(%rsp), %rdx
	leaq	.LC23(%rip), %rsi
	movl	$2, %edi
	imulq	$1000000, %rdx, %rdx
	addq	128(%rsp), %rdx
	subq	112(%rsp), %rdx
	call	__printf_chk@PLT
	movq	16(%rsp), %rdx
	movq	72(%rsp), %rsi
	movq	%rbp, %rdi
	movl	$2, %ecx
	leaq	.LC24(%rip), %r13
	call	cudaMemcpy@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
.L25:
	cmpl	%ebx, 36(%rsp)
	jle	.L47
	movl	0(%rbp,%rbx,4), %ecx
	movq	8(%rsp), %rdi
	movq	%r13, %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	incl	%r12d
	call	__fprintf_chk@PLT
	cmpl	$15, %r12d
	jne	.L26
	movq	8(%rsp), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	xorl	%r12d, %r12d
	leaq	.LC25(%rip), %rdx
	call	__fprintf_chk@PLT
.L26:
	incq	%rbx
	jmp	.L25
.L47:
	movq	8(%rsp), %rdi
	leaq	.LC25(%rip), %rbx
	movl	$2, %esi
	xorl	%eax, %eax
	movq	%rbx, %rdx
	call	__fprintf_chk@PLT
	movq	8(%rsp), %rdi
	call	fclose@PLT
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
.L45:
	call	__printf_chk@PLT
	xorl	%edi, %edi
	jmp	.L42
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC26:
	.string	"_Z20add_vec_scalaire_gpuPiS_lif"
	.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	.LC26(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z20add_vec_scalaire_gpuPiS_lif(%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
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC22:
	.long	1132396544
	.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 <stdlib.h>
#include <stdio.h>
#include <sys/time.h>

#define MAX_CHAINE 100

#define CALLOC(ptr, nr, type)   if (!(ptr = (type *) calloc((size_t)(nr), sizeof(type)))) { \
                                    printf("Erreur lors de l'allocation memoire \n") ;      \
                                    exit (-1);                                              \
                                }


#define FOPEN(fich,fichier,sens)    if ((fich=fopen(fichier,sens)) == NULL) {                   \
                                        printf("Probleme d'ouverture du fichier %s\n",fichier); \
                                        exit(-1);                                               \
                                    }

#define MIN(a, b)         (a < b ? a : b)
#define MAX(a, b)         (a > b ? a : b)

#define MAX_VALEUR         255
#define MIN_VALEUR         0

#define NBPOINTSPARLIGNES 15

#define MAX_DIM_GRID 65535
#define MAX_DIM_BLOCK 1024

#define false 0
#define true 1
#define boolean int

// Clock
#define initTimer struct timeval tv1, tv2; struct timezone tz
#define startTimer gettimeofday(&tv1, &tz)
#define stopTimer gettimeofday(&tv2, &tz)
#define tpsCalcul ((tv2.tv_sec-tv1.tv_sec)*1000000L + (tv2.tv_usec-tv1.tv_usec))


/* KERNEL CUDA */
__global__ void add_vec_scalaire_gpu(int *image, int *res, long N, int le_min, float etalement) {
    long i = (long)blockIdx.x * (long)blockDim.x + (long)threadIdx.x;
    if (i < N) {
        res[i] = ((image[i] - le_min) * etalement);
    }
}


int main(int argc, char *argv[]) {
    /*========================================================================*/
    /* Declaration de variables et allocation memoire */
    /*========================================================================*/

    if (argc < 2) {
        printf("Usage: ./CodeSequentiel <path_to_image> [<blocksize>]\n");
        exit(0);
    }

    long blocksize = 1; // default value
    if (argc == 3) {
        blocksize = atoi(argv[2]);
    }

    int i, n;

    int LE_MIN = MAX_VALEUR;
    int LE_MAX = MIN_VALEUR;

    float ETALEMENT = 0.0;

    int* image;
    int* resultat;
    int X, Y, x, y;
    int TailleImage;

    int P;

    FILE* Src, * Dst;

    char SrcFile[MAX_CHAINE];
    char DstFile[MAX_CHAINE];

    char ligne[MAX_CHAINE];

    boolean inverse = false;

    char *Chemin;

    initTimer; //

    /*========================================================================*/
    /* Recuperation des parametres                                                */
    /*========================================================================*/

    sscanf(argv[1], "%s", SrcFile);

    sprintf(DstFile, "%s.new", SrcFile);

    /*========================================================================*/
    /* Recuperation de l'endroit ou l'on travail                                */
    /*========================================================================*/

    CALLOC(Chemin, MAX_CHAINE, char);
    Chemin = getenv("PWD");
    printf("Repertoire de travail : %s \n\n", Chemin);

    /*========================================================================*/
    /* Ouverture des fichiers                                                */
    /*========================================================================*/

    printf("Operations sur les fichiers\n");

    FOPEN(Src, SrcFile, "r");
    printf("\t Fichier source ouvert (%s) \n", SrcFile);

    FOPEN(Dst, DstFile, "w");
    printf("\t Fichier destination ouvert (%s) \n", DstFile);

    /*========================================================================*/
    /* On effectue la lecture du fichier source */
    /*========================================================================*/

    printf("\t Lecture entete du fichier source ");

    for (i = 0; i < 2; i++) {
        fgets(ligne, MAX_CHAINE, Src);
        fprintf(Dst, "%s", ligne);
    }

    fscanf(Src, " %d %d\n", &X, &Y);
    fprintf(Dst, " %d %d\n", X, Y);

    fgets(ligne, MAX_CHAINE, Src);        /* Lecture du 255         */
    fprintf(Dst, "%s", ligne);

    printf(": OK \n");

    /*========================================================================*/
    /* Allocation memoire pour l'image source et l'image resultat                 */
    /*========================================================================*/

    TailleImage = X * Y;

    CALLOC(image, TailleImage, int);
    CALLOC(resultat, TailleImage, int);
    for (i = 0;i < TailleImage;i++) {
        image[i] = 0;
        resultat[i] = 0;
    }

    x = 0;
    y = 0;

    printf("\t\t Initialisation de l'image [%d ; %d] : Ok \n", X, Y);

    /*========================================================================*/
    /* Lecture du fichier pour remplir l'image source                         */
    /*========================================================================*/

    while (!feof(Src)) {
        n = fscanf(Src, "%d", &P);
        image[y+x] = P;
        LE_MIN = MIN(LE_MIN, P);
        LE_MAX = MAX(LE_MAX, P);
        x++;
        if (n == EOF || (x == X && y == Y - 1)) {
            break;
        }
        if (x == X) {
            x = 0;
            y++;
        }
    }
    fclose(Src);
    printf("\t Lecture du fichier image : Ok \n\n");

    /*========================================================================*/
    /* Calcul du facteur d'etalement                                        */
    /*========================================================================*/

    if (inverse) {
        ETALEMENT = 0.2;
    }
    else {
        ETALEMENT = (float)(MAX_VALEUR - MIN_VALEUR) / (float)(LE_MAX - LE_MIN);
    }

    /*========================================================================*/
    /* Calcul de chaque nouvelle valeur de pixel                              */
    /*========================================================================*/

    int tailleVecteur = TailleImage;
    //long blocksize = 1; // TODO can change from args

    long size = sizeof(int)*tailleVecteur;

    int *cudaVec;
    int *cudaRes;

    // Select cuda GPU device to use (if multiple device)
    hipSetDevice(0);

    if (hipMalloc((void **)&cudaVec, size) == hipErrorOutOfMemory) {
        printf("Allocation memoire qui pose probleme (cudaVec) \n");
    }
    if (hipMalloc((void **)&cudaRes, size)  == hipErrorOutOfMemory) {
        printf("Allocation memoire qui pose probleme (cudaRes) \n");
    }

    long dimBlock = blocksize;
    long dimGrid = tailleVecteur/blocksize;
    if ((tailleVecteur % blocksize) != 0) {
        dimGrid++;
    }

    int res = hipMemcpy(&cudaVec[0], &image[0], size, hipMemcpyHostToDevice);

    printf("Copy CPU -> GPU %d \n",res);
    printf("dimBlock: %ld | dimGrid: %ld\n", dimBlock, dimGrid);

    startTimer;
    add_vec_scalaire_gpu<<<dimGrid, dimBlock>>>(cudaVec, cudaRes, tailleVecteur, LE_MIN, ETALEMENT);
    hipDeviceSynchronize();
    stopTimer;

    printf("Duration %ld", tpsCalcul);

    hipMemcpy(&resultat[0], &cudaRes[0], size, hipMemcpyDeviceToHost);

    hipFree(cudaVec);
    hipFree(cudaRes);

    /*========================================================================*/
    /* Sauvegarde de l'image dans le fichier resultat                         */
    /*========================================================================*/

    n = 0;
    for (i = 0; i < TailleImage ; i++) {
        fprintf(Dst, "%3d ", resultat[i]);
        n++;
        if (n == NBPOINTSPARLIGNES) {
            n = 0;
            fprintf(Dst, "\n");
        }
    }

    fprintf(Dst, "\n");
    fclose(Dst);

    printf("\n");

    /*========================================================================*/
    /* Fin du programme principal        */
    /*========================================================================*/

    exit(0);
}

	.text
	.file	"CodeSequentiel.hip"
	.globl	_Z35__device_stub__add_vec_scalaire_gpuPiS_lif # -- Begin function _Z35__device_stub__add_vec_scalaire_gpuPiS_lif
	.type	_Z35__device_stub__add_vec_scalaire_gpuPiS_lif,@function
_Z35__device_stub__add_vec_scalaire_gpuPiS_lif: # @_Z35__device_stub__add_vec_scalaire_gpuPiS_lif
	.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	$128, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	movq	%rsp, %rcx
	movss	%xmm0, (%rcx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%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	$_Z20add_vec_scalaire_gpuPiS_lif, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	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	_Z35__device_stub__add_vec_scalaire_gpuPiS_lif, .Lfunc_end0-_Z35__device_stub__add_vec_scalaire_gpuPiS_lif
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x437f0000                      # float 255
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$456, %rsp                      # imm = 0x1C8
	.cfi_def_cfa_offset 512
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$1, %edi
	jg	.LBB1_3
# %bb.1:
	movl	$.Lstr.7, %edi
	callq	puts@PLT
.LBB1_2:
	xorl	%edi, %edi
	callq	exit
.LBB1_3:
	movq	%rsi, %rbx
	movl	$1, %r14d
	cmpl	$3, %edi
	jne	.LBB1_5
# %bb.4:
	movq	16(%rbx), %rdi
	callq	atoi
	movslq	%eax, %r14
.LBB1_5:
	movq	8(%rbx), %rdi
	leaq	240(%rsp), %rbx
	movl	$.L.str.1, %esi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	__isoc23_sscanf
	leaq	128(%rsp), %rdi
	movl	$.L.str.2, %esi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	sprintf
	movl	$.L.str.4, %edi
	callq	getenv
	movl	$.L.str.5, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.L.str.7, %esi
	movq	%rbx, %rdi
	callq	fopen
	testq	%rax, %rax
	jne	.LBB1_7
# %bb.6:
	leaq	240(%rsp), %rsi
	jmp	.LBB1_9
.LBB1_7:
	movq	%rax, %r13
	leaq	240(%rsp), %rsi
	movl	$.L.str.9, %edi
	xorl	%eax, %eax
	callq	printf
	leaq	128(%rsp), %rdi
	movl	$.L.str.10, %esi
	callq	fopen
	testq	%rax, %rax
	jne	.LBB1_10
# %bb.8:
	leaq	128(%rsp), %rsi
.LBB1_9:
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
.LBB1_13:
	movl	$-1, %edi
	callq	exit
.LBB1_10:                               # %.critedge
	movq	%rax, %rbx
	movq	%r14, 80(%rsp)                  # 8-byte Spill
	leaq	128(%rsp), %rsi
	movl	$.L.str.11, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.12, %edi
	xorl	%eax, %eax
	callq	printf
	leaq	352(%rsp), %r14
	movq	%r14, %rdi
	movl	$100, %esi
	movq	%r13, %rdx
	callq	fgets
	movq	%r14, %rdi
	movq	%rbx, %rsi
	callq	fputs@PLT
	movq	%r14, %rdi
	movl	$100, %esi
	movq	%r13, %rdx
	callq	fgets
	movq	%r14, %rdi
	movq	%rbx, %rsi
	callq	fputs@PLT
	leaq	44(%rsp), %r15
	leaq	40(%rsp), %r12
	movl	$.L.str.13, %esi
	movq	%r13, %rdi
	movq	%r15, %rdx
	movq	%r12, %rcx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%r15), %edx
	movl	(%r12), %ecx
	movl	$.L.str.13, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movq	%r14, %rdi
	movl	$100, %esi
	movq	%r13, %rdx
	callq	fgets
	movq	%r14, %rdi
	movq	%rbx, %rsi
	callq	fputs@PLT
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	(%r15), %ebp
	movl	(%r12), %r14d
	movl	%r14d, %r15d
	imull	%ebp, %r15d
	movslq	%r15d, %r12
	movl	$4, %esi
	movq	%r12, %rdi
	callq	calloc
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	testq	%rax, %rax
	je	.LBB1_12
# %bb.11:
	movl	$4, %esi
	movq	%r12, %rdi
	callq	calloc
	testq	%rax, %rax
	jne	.LBB1_14
.LBB1_12:
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	jmp	.LBB1_13
.LBB1_14:                               # %.preheader
	movq	%r12, 48(%rsp)                  # 8-byte Spill
	movl	%r15d, 32(%rsp)                 # 4-byte Spill
	movq	%rax, 64(%rsp)                  # 8-byte Spill
	movq	%rbx, 72(%rsp)                  # 8-byte Spill
	xorl	%r12d, %r12d
	movl	$.L.str.15, %edi
	movl	%ebp, %esi
	movl	%r14d, %edx
	xorl	%eax, %eax
	callq	printf
	movq	%r13, %rdi
	callq	feof
	movl	$255, %ebp
	testl	%eax, %eax
	jne	.LBB1_20
# %bb.15:                               # %.lr.ph122.preheader
	xorl	%r12d, %r12d
	xorl	%r15d, %r15d
	xorl	%r14d, %r14d
.LBB1_16:                               # %.lr.ph122
                                        # =>This Inner Loop Header: Depth=1
	movl	%r12d, %ebx
	movl	$.L.str.16, %esi
	movq	%r13, %rdi
	leaq	36(%rsp), %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	36(%rsp), %r12d
	leal	(%r15,%r14), %ecx
	movslq	%ecx, %rcx
	movq	56(%rsp), %rdx                  # 8-byte Reload
	movl	%r12d, (%rdx,%rcx,4)
	cmpl	%r12d, %ebp
	cmovgel	%r12d, %ebp
	cmpl	%r12d, %ebx
	cmovgl	%ebx, %r12d
	cmpl	$-1, %eax
	je	.LBB1_20
# %bb.17:                               #   in Loop: Header=BB1_16 Depth=1
	incl	%r15d
	movl	44(%rsp), %eax
	movl	%r15d, %ebx
	cmpl	%eax, %r15d
	jne	.LBB1_19
# %bb.18:                               #   in Loop: Header=BB1_16 Depth=1
	movl	40(%rsp), %ecx
	decl	%ecx
	xorl	%ebx, %ebx
	cmpl	%ecx, %r14d
	je	.LBB1_20
.LBB1_19:                               #   in Loop: Header=BB1_16 Depth=1
	xorl	%ecx, %ecx
	cmpl	%eax, %r15d
	sete	%cl
	addl	%ecx, %r14d
	movq	%r13, %rdi
	callq	feof
	movl	%ebx, %r15d
	testl	%eax, %eax
	je	.LBB1_16
.LBB1_20:                               # %._crit_edge123
	movq	%r13, %rdi
	callq	fclose
	movl	$.Lstr.4, %edi
	callq	puts@PLT
	movq	48(%rsp), %rax                  # 8-byte Reload
	leaq	(,%rax,4), %rbx
	xorl	%edi, %edi
	callq	hipSetDevice
	leaq	24(%rsp), %rdi
	movq	%rbx, 8(%rsp)                   # 8-byte Spill
	movq	%rbx, %rsi
	callq	hipMalloc
	cmpl	$2, %eax
	jne	.LBB1_22
# %bb.21:
	movl	$.Lstr.5, %edi
	callq	puts@PLT
.LBB1_22:
	leaq	16(%rsp), %rdi
	movq	8(%rsp), %rsi                   # 8-byte Reload
	callq	hipMalloc
	cmpl	$2, %eax
	movq	72(%rsp), %rbx                  # 8-byte Reload
	movq	80(%rsp), %r15                  # 8-byte Reload
	jne	.LBB1_24
# %bb.23:
	movl	$.Lstr.6, %edi
	callq	puts@PLT
.LBB1_24:
	movq	48(%rsp), %r13                  # 8-byte Reload
	movq	%r13, %rax
	cqto
	idivq	%r15
	movq	%rax, %r14
	cmpq	$1, %rdx
	sbbq	$-1, %r14
	movq	24(%rsp), %rdi
	movq	56(%rsp), %rsi                  # 8-byte Reload
	movq	8(%rsp), %rdx                   # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$.L.str.20, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.21, %edi
	movq	%r15, %rsi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	printf
	leaq	96(%rsp), %rdi
	leaq	88(%rsp), %rsi
	callq	gettimeofday
	movl	%r14d, %edi
	btsq	$32, %rdi
	movl	%r15d, %edx
	btsq	$32, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_26
# %bb.25:
	subl	%ebp, %r12d
	cvtsi2ss	%r12d, %xmm1
	movss	.LCPI1_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	divss	%xmm1, %xmm0
	movq	24(%rsp), %rdi
	movq	16(%rsp), %rsi
	movq	%r13, %rdx
	movl	%ebp, %ecx
	callq	_Z35__device_stub__add_vec_scalaire_gpuPiS_lif
.LBB1_26:
	callq	hipDeviceSynchronize
	leaq	112(%rsp), %r14
	leaq	88(%rsp), %rsi
	movq	%r14, %rdi
	callq	gettimeofday
	movq	(%r14), %rax
	movq	8(%r14), %rsi
	subq	96(%rsp), %rax
	imulq	$1000000, %rax, %rax            # imm = 0xF4240
	subq	104(%rsp), %rsi
	addq	%rax, %rsi
	movl	$.L.str.22, %edi
	xorl	%eax, %eax
	callq	printf
	movq	16(%rsp), %rsi
	movq	64(%rsp), %r12                  # 8-byte Reload
	movq	%r12, %rdi
	movq	8(%rsp), %rdx                   # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	testl	%r13d, %r13d
	jle	.LBB1_31
# %bb.27:                               # %.lr.ph134.preheader
	movl	32(%rsp), %r14d                 # 4-byte Reload
	xorl	%r15d, %r15d
	xorl	%ebp, %ebp
.LBB1_28:                               # %.lr.ph134
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r12,%r15,4), %edx
	movl	$.L.str.23, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	fprintf
	incl	%ebp
	cmpl	$15, %ebp
	jne	.LBB1_30
# %bb.29:                               #   in Loop: Header=BB1_28 Depth=1
	movl	$10, %edi
	movq	%rbx, %rsi
	callq	fputc@PLT
	xorl	%ebp, %ebp
.LBB1_30:                               #   in Loop: Header=BB1_28 Depth=1
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB1_28
.LBB1_31:                               # %._crit_edge135
	movl	$10, %edi
	movq	%rbx, %rsi
	callq	fputc@PLT
	movq	%rbx, %rdi
	callq	fclose
	movl	$10, %edi
	callq	putchar@PLT
	jmp	.LBB1_2
.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	$_Z20add_vec_scalaire_gpuPiS_lif, %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	_Z20add_vec_scalaire_gpuPiS_lif,@object # @_Z20add_vec_scalaire_gpuPiS_lif
	.section	.rodata,"a",@progbits
	.globl	_Z20add_vec_scalaire_gpuPiS_lif
	.p2align	3, 0x0
_Z20add_vec_scalaire_gpuPiS_lif:
	.quad	_Z35__device_stub__add_vec_scalaire_gpuPiS_lif
	.size	_Z20add_vec_scalaire_gpuPiS_lif, 8

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

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%s.new"
	.size	.L.str.2, 7

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"PWD"
	.size	.L.str.4, 4

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Repertoire de travail : %s \n\n"
	.size	.L.str.5, 30

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

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Probleme d'ouverture du fichier %s\n"
	.size	.L.str.8, 36

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"\t Fichier source ouvert (%s) \n"
	.size	.L.str.9, 31

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

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"\t Fichier destination ouvert (%s) \n"
	.size	.L.str.11, 36

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"\t Lecture entete du fichier source "
	.size	.L.str.12, 36

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

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"\t\t Initialisation de l'image [%d ; %d] : Ok \n"
	.size	.L.str.15, 46

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

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"Copy CPU -> GPU %d \n"
	.size	.L.str.20, 21

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	"dimBlock: %ld | dimGrid: %ld\n"
	.size	.L.str.21, 30

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"Duration %ld"
	.size	.L.str.22, 13

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

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z20add_vec_scalaire_gpuPiS_lif"
	.size	.L__unnamed_1, 32

	.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	"Operations sur les fichiers"
	.size	.Lstr, 28

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	": OK "
	.size	.Lstr.1, 6

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"Erreur lors de l'allocation memoire "
	.size	.Lstr.3, 37

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"\t Lecture du fichier image : Ok \n"
	.size	.Lstr.4, 34

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"Allocation memoire qui pose probleme (cudaVec) "
	.size	.Lstr.5, 48

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"Allocation memoire qui pose probleme (cudaRes) "
	.size	.Lstr.6, 48

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"Usage: ./CodeSequentiel <path_to_image> [<blocksize>]"
	.size	.Lstr.7, 54

	.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 _Z35__device_stub__add_vec_scalaire_gpuPiS_lif
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z20add_vec_scalaire_gpuPiS_lif
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z20add_vec_scalaire_gpuPiS_lif ; -- Begin function _Z20add_vec_scalaire_gpuPiS_lif
	.globl	_Z20add_vec_scalaire_gpuPiS_lif
	.p2align	8
	.type	_Z20add_vec_scalaire_gpuPiS_lif,@function
_Z20add_vec_scalaire_gpuPiS_lif:        ; @_Z20add_vec_scalaire_gpuPiS_lif
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x2c
	s_load_b64 s[2:3], s[0:1], 0x10
	v_mov_b32_e32 v1, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[2:3], null, s4, s15, v[0:1]
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_i64_e32 vcc_lo, s[2:3], v[2:3]
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b128 s[4:7], s[0:1], 0x0
	v_lshlrev_b64 v[0:1], 2, v[2:3]
	s_load_b64 s[0:1], s[0:1], 0x18
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v2, vcc_lo, s4, v0
	v_add_co_ci_u32_e32 v3, vcc_lo, s5, v1, vcc_lo
	v_add_co_u32 v0, vcc_lo, s6, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v1, vcc_lo
	global_load_b32 v2, v[2:3], off
	s_waitcnt vmcnt(0)
	v_subrev_nc_u32_e32 v2, s0, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_f32_i32_e32 v2, v2
	v_mul_f32_e32 v2, s1, v2
	s_delay_alu instid0(VALU_DEP_1)
	v_cvt_i32_f32_e32 v2, v2
	global_store_b32 v[0:1], v2, off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z20add_vec_scalaire_gpuPiS_lif
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.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 4
		.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	_Z20add_vec_scalaire_gpuPiS_lif, .Lfunc_end0-_Z20add_vec_scalaire_gpuPiS_lif
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 176
; NumSgprs: 18
; NumVgprs: 4
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 4
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           8
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         28
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z20add_vec_scalaire_gpuPiS_lif
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z20add_vec_scalaire_gpuPiS_lif.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     4
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
