#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define HANDLE_ERROR(err) (HandleError( err, __FILE__, __LINE__ ))
#define THREADS_PER_BLOCK 256


static void HandleError(cudaError_t err, const char *file, int line) {
    if (err != cudaSuccess) {
        printf("%s in %s at line %d\n", cudaGetErrorString(err), file, line);
        exit(EXIT_FAILURE);
    }
}

__global__ void dot(int N, float *a, float *b, float *c) {
    __shared__ float cache[THREADS_PER_BLOCK];      // buffer of shared memory
    int tid = threadIdx.x + blockIdx.x * blockDim.x;
    int cacheIndex = threadIdx.x;

    float temp = 0;
    while (tid < N) {
        temp += a[tid] * b[tid];
        tid += blockDim.x * gridDim.x;
    }

    cache[cacheIndex] = temp;   // clear shared memory buffer

    __syncthreads();

    int i = blockDim.x/2;
    while (i != 0) {
        if (cacheIndex < i)
            cache[cacheIndex] += cache[cacheIndex + i];
        __syncthreads();
        i /= 2;
    }

    if (cacheIndex == 0)
        c[blockIdx.x] = cache[0];
}

void cuda_dot(int N, float *a, float *b, float *result) {
    int blocksPerGrid = min(32, (N+THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK);
    float *partial_c, c;
    float *dev_a, *dev_b, *dev_partial_c;

    partial_c =(float*) malloc (blocksPerGrid*sizeof(float));

    // allocate the memory on the GPU
    HANDLE_ERROR(cudaMalloc((void **) &dev_a, N * sizeof(float)));
    HANDLE_ERROR(cudaMalloc((void **) &dev_b, N * sizeof(float)));
    HANDLE_ERROR(cudaMalloc((void **) &c, sizeof(float)));
    HANDLE_ERROR(cudaMalloc((void **) &dev_partial_c, blocksPerGrid*sizeof(float)));

    // copy the arrays 'a' and 'b' to the GPU
    HANDLE_ERROR(cudaMemcpy(dev_a, a, N * sizeof(float), cudaMemcpyHostToDevice));
    HANDLE_ERROR(cudaMemcpy(dev_b, b, N * sizeof(float), cudaMemcpyHostToDevice));

    dot<<<blocksPerGrid, THREADS_PER_BLOCK>>>(N, dev_a, dev_b, dev_partial_c);

    // copy the array 'c' back from the GPU to the CPU
    HANDLE_ERROR(cudaMemcpy(partial_c, dev_partial_c, blocksPerGrid*sizeof(float), cudaMemcpyDeviceToHost));

    c = 0;
    for (int i=0; i<blocksPerGrid; i++) {
        c += partial_c[i];
    }

    memcpy(result, &c, sizeof(float));

    // free the memory allocated on the GPU
    cudaFree(dev_a);
    cudaFree(dev_b);
    cudaFree(dev_partial_c);

    delete [] partial_c;
}



struct Tensor {
    int shape[4];
    float* vector;
    int sz;
};


struct Tensor get_tensor(char* file_name) {
    FILE *f;
    if ((f = fopen(file_name, "rb")) == NULL) {
        printf("Error: file opening in %s", file_name);
        exit(0);
    }
    fseek(f, 0, SEEK_END);
    int file_sz = ftell(f);
    fseek(f, 0, SEEK_SET);

    int shape[4] = {-1, -1, -1, -1};
    float* vector = (float *) malloc(file_sz - 16);  // 16 for shape

    fread(shape, 4, 4, f);
    fread(vector, file_sz - 16, 1, f);

    printf("Read file: %d bytes (%d, %d, %d, %d) shape from %s\n",
           file_sz, shape[0], shape[1], shape[2], shape[3], file_name);

    struct Tensor tensor;
    tensor.vector = vector;
    memcpy(tensor.shape, shape, 16);
    tensor.sz = file_sz;
    return tensor;
}


void write_tensor(struct Tensor tensor, char* file_name) {
    FILE *fp = fopen(file_name, "wb");
    fwrite(tensor.shape, 4, 4, fp);
    fwrite(tensor.vector, tensor.sz - 16, 1, fp);
    fclose(fp);
    printf("Write tensor: %s\n", file_name);
}


struct Padding {
    int top;
    int bottom;
    int left;
    int right;
};


struct Padding get_same_padding_in_tf(int oh, int ow, int kh, int kw) {
    // strides: 1 (default)
    // padding: same (default)
    int p_vertical = max(- 1 + kh, 0);
    int p_horizontal = max(- 1 + kw, 0);
    struct Padding pad;
    pad.top = p_vertical / 2;
    pad.bottom = p_vertical - pad.top;
    pad.left = p_horizontal / 2;
    pad.right = p_horizontal - pad.left;
    return pad;
}


float* get_input_patch(struct Tensor input, struct Padding pad,
                       int _n, int _h, int _w, int kh, int kw,
                       int i0, int i1, int i2) {
    // input.shape: (N=1, H, W, C=IC)
    // --> (kh, kw, ic)
    int h = input.shape[1];
    int w = input.shape[2];
    int ic = input.shape[3];
    int patch_sz = kh * kw * ic;
    float *patch = (float *) calloc(patch_sz, sizeof(float));

    int center_h = kh / 2;
    int center_w = kw / 2;

    int input_base_idx = i0 * _n + i1 * (_h - center_h) + i2 * (_w - center_w);
    int patch_base_idx = 0;
    int size = kw * ic;
    int end_h = kh;

    if (pad.top != 0 && _h - center_h < 0) {
        input_base_idx += i1 * pad.top;
        patch_base_idx += kw * ic * pad.top;
        end_h -= pad.top;
    } else if (pad.bottom != 0 && _h - center_h + kh > h) {
        end_h -= pad.bottom;
    }
    if (pad.left != 0 && _w - center_w < 0){
        input_base_idx += i2 * pad.left;
        patch_base_idx += ic * pad.left;
        size -= ic * pad.left;
    } else if (pad.right != 0 && _w - center_w + kw > w) {
        size -= ic * pad.right;
    }
    int input_start_idx, patch_start_idx;
    for (int _kh = 0; _kh < end_h; _kh++) {
        input_start_idx = input_base_idx + i1 * _kh;
        patch_start_idx = patch_base_idx + kw * ic * _kh;
        memcpy(patch + patch_start_idx, input.vector + input_start_idx, size * sizeof(float));
    }
    return patch;
}


void einsum_hwi_hwoi_to_o(int* shape, float* v_hwi, float* v_hwoi, float* v_o) {
    // shape: h, w, o, i
    int h = shape[0];
    int w = shape[1];
    int o = shape[2];
    int i = shape[3];
    int hwi_idx, hwoi_idx;
    for (int _h = 0; _h < h; _h++) {
        for (int _w = 0; _w < w; _w++) {
            for (int _o = 0; _o < o; _o++) {
                hwi_idx = (w * i) * _h + i * _w;
                hwoi_idx = (w * o * i) * _h + (o * i) * _w + i * _o;

                float dot_product_ret = 0;
                cuda_dot(i, v_hwi + hwi_idx, v_hwoi + hwoi_idx, &dot_product_ret);
                v_o[_o] += dot_product_ret;
            }
        }
    }
}


struct Tensor conv2d(struct Tensor input, struct Tensor kernel) {
    // input.shape: (N, H, W, C=IC)
    // kernel.shape: (KH, KW, OC, IC=C)
    // output.shape: (N, H, W, OC)
    int output_sz = input.shape[0] * input.shape[1] * input.shape[2] * kernel.shape[2] * 4;
    struct Tensor out;
    out.vector = (float *) malloc(output_sz);
    int shape[4] = {input.shape[0], input.shape[1], input.shape[2], kernel.shape[2]};
    memcpy(out.shape, shape, 16);
    out.sz = output_sz + 16;

    int i2 = input.shape[3];
    int i1 = input.shape[2] * i2;
    int i0 = input.shape[1] * i1;

    int o2 = kernel.shape[2];
    int o1 = input.shape[2] * o2;
    int o0 = input.shape[1] * o1;

    struct Padding pad = get_same_padding_in_tf(out.shape[1], out.shape[2], kernel.shape[0], kernel.shape[1]);

    float *patch_hwi;  // kh * kw * ic
    float *v_o;

    int start_odx;
    int oc = kernel.shape[2];
    for (int _n = 0; _n < input.shape[0]; _n++) {  // N
        for (int _h = 0; _h < input.shape[1]; _h++) { // H
            for (int _w = 0; _w < input.shape[2]; _w++) { // W

                start_odx = o0 * _n + o1 * _h + o2 * _w;

                v_o = (float *) calloc(oc, sizeof(float));

                patch_hwi = get_input_patch(input, pad, _n, _h, _w, kernel.shape[0], kernel.shape[1], i0, i1, i2);
                einsum_hwi_hwoi_to_o(kernel.shape, patch_hwi, kernel.vector, v_o);

                memcpy(out.vector + start_odx, v_o, oc * sizeof(float));

                free(patch_hwi);
                free(v_o);
            }
        }
    }
    return out;
}


int main (int argc, char* argv[]) {

    clock_t start, end;
    float elapsed_time;

    struct Tensor tensor_in = get_tensor(argv[1]);
    struct Tensor tensor_ke = get_tensor(argv[2]);

    struct Tensor tensor_ot;

    start = clock();
    tensor_ot = conv2d(tensor_in, tensor_ke);
    end = clock();
    elapsed_time = (float) (end - start) / CLOCKS_PER_SEC;
    printf("Elapsed time: %f \n", elapsed_time);

    write_tensor(tensor_ot, "output_tensor.bin");

    free(tensor_in.vector);
    free(tensor_ke.vector);
    free(tensor_ot.vector);
    return 0;
}


	.file	"tmpxft_002b0780_00000000-6_convolution.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2038:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2038:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/dongkwan-kim/naively-quantized-conv/master/code/prob4/convolution.cu"
.LC1:
	.string	"%s in %s at line %d\n"
	.text
	.type	_ZL11HandleError9cudaErrorPKci.constprop.0, @function
_ZL11HandleError9cudaErrorPKci.constprop.0:
.LFB2180:
	.cfi_startproc
	testl	%edi, %edi
	je	.L2
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	%esi, 12(%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r8d
	movl	$2, %edi
	leaq	.LC0(%rip), %rcx
	movq	%rax, %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2180:
	.size	_ZL11HandleError9cudaErrorPKci.constprop.0, .-_ZL11HandleError9cudaErrorPKci.constprop.0
	.section	.rodata.str1.1
.LC2:
	.string	"rb"
.LC3:
	.string	"Error: file opening in %s"
.LC4:
	.string	"Read file: %d bytes (%d, %d, %d, %d) shape from %s\n"
	.text
	.globl	_Z10get_tensorPc
	.type	_Z10get_tensorPc, @function
_Z10get_tensorPc:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r15
	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
	movq	%rsi, %r12
	leaq	.LC2(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%r12, %rdi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L9
	movl	$2, %edi
	movq	%r12, %rdx
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L9:
	movq	%rax, %rbx
	movl	$2, %edx
	xorl	%esi, %esi
	movq	%rax, %rdi
	call	fseek@PLT
	movq	%rbx, %rdi
	call	ftell@PLT
	xorl	%edx, %edx
	xorl	%esi, %esi
	movq	%rbx, %rdi
	movq	%rax, %rbp
	movl	%eax, 12(%rsp)
	call	fseek@PLT
	leal	-16(%rbp), %r13d
	movq	$-1, 24(%rsp)
	movq	$-1, 32(%rsp)
	movslq	%r13d, %r13
	movq	%r13, %rdi
	call	malloc@PLT
	leaq	24(%rsp), %rdi
	movq	%rbx, %r8
	movl	$4, %ecx
	movq	%rax, %r14
	movl	$4, %edx
	movl	$16, %esi
	call	__fread_chk@PLT
	movq	%rbx, %r8
	movl	$1, %ecx
	movq	%r13, %rdx
	movq	%r13, %rsi
	movq	%r14, %rdi
	call	__fread_chk@PLT
	pushq	%r12
	.cfi_def_cfa_offset 120
	movl	44(%rsp), %eax
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	pushq	%rax
	.cfi_def_cfa_offset 128
	movl	48(%rsp), %r9d
	xorl	%eax, %eax
	movl	44(%rsp), %r8d
	movl	40(%rsp), %ecx
	movl	28(%rsp), %edx
	call	__printf_chk@PLT
	movups	40(%rsp), %xmm0
	movq	%r14, 16(%r15)
	movl	%ebp, 24(%r15)
	movups	%xmm0, (%r15)
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	movq	%r15, %rax
	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
.LFE2029:
	.size	_Z10get_tensorPc, .-_Z10get_tensorPc
	.section	.rodata.str1.1
.LC5:
	.string	"wb"
.LC6:
	.string	"Write tensor: %s\n"
	.text
	.globl	_Z12write_tensor6TensorPc
	.type	_Z12write_tensor6TensorPc, @function
_Z12write_tensor6TensorPc:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC5(%rip), %rsi
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	pushq	%rax
	.cfi_def_cfa_offset 32
	call	fopen@PLT
	leaq	32(%rsp), %rdi
	movl	$4, %edx
	movl	$4, %esi
	movq	%rax, %rcx
	movq	%rax, %rbx
	call	fwrite@PLT
	movl	56(%rsp), %eax
	movq	48(%rsp), %rdi
	movq	%rbx, %rcx
	movl	$1, %edx
	leal	-16(%rax), %esi
	movslq	%esi, %rsi
	call	fwrite@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	%rbp, %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rsi
	call	__printf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z12write_tensor6TensorPc, .-_Z12write_tensor6TensorPc
	.globl	_Z22get_same_padding_in_tfiiii
	.type	_Z22get_same_padding_in_tfiiii, @function
_Z22get_same_padding_in_tfiiii:
.LFB2031:
	.cfi_startproc
	endbr64
	testl	%edx, %edx
	movl	$1, %eax
	cmovle	%eax, %edx
	decl	%edx
	testl	%ecx, %ecx
	cmovle	%eax, %ecx
	movl	%edx, %eax
	sarl	%eax
	decl	%ecx
	subl	%eax, %edx
	movl	%ecx, %esi
	salq	$32, %rdx
	sarl	%esi
	orq	%rdx, %rax
	subl	%esi, %ecx
	movl	%esi, %edx
	salq	$32, %rcx
	orq	%rcx, %rdx
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z22get_same_padding_in_tfiiii, .-_Z22get_same_padding_in_tfiiii
	.globl	_Z15get_input_patch6Tensor7Paddingiiiiiiii
	.type	_Z15get_input_patch6Tensor7Paddingiiiiiiii, @function
_Z15get_input_patch6Tensor7Paddingiiiiiiii:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	%ecx, %r15d
	movq	%rsi, %r11
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	sarq	$32, %r11
	movq	%rsi, %r14
	movl	$4, %esi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%r8d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%r9d, %r12d
	movq	%rdi, %r9
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	sarq	$32, %r9
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%r9d, %ebp
	movl	%edx, %ebx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 56(%rsp)
	movl	160(%rsp), %edi
	movl	140(%rsp), %ecx
	movl	132(%rsp), %eax
	movq	%r9, 48(%rsp)
	imull	%r12d, %edi
	movq	%r11, 40(%rsp)
	movl	%eax, 16(%rsp)
	movl	136(%rsp), %eax
	movl	%ecx, 36(%rsp)
	imull	%ecx, %edi
	movl	%eax, 24(%rsp)
	movslq	%edi, %rdi
	leaq	0(,%rdi,4), %rax
	movq	%rax, 8(%rsp)
	call	calloc@PLT
	movl	$2, %esi
	movl	36(%rsp), %ecx
	imull	168(%rsp), %ebx
	movq	%rax, %r8
	movl	%r12d, %eax
	movq	40(%rsp), %r11
	movq	48(%rsp), %r9
	cltd
	idivl	%esi
	subl	%eax, %r15d
	movl	160(%rsp), %eax
	cltd
	idivl	%esi
	movl	184(%rsp), %edx
	subl	%eax, %r13d
	movl	176(%rsp), %eax
	imull	%r13d, %edx
	imull	%r15d, %eax
	addl	%ebx, %eax
	movl	160(%rsp), %ebx
	addl	%edx, %eax
	imull	%ecx, %ebx
	testl	%r15d, %r15d
	jns	.L16
	movq	56(%rsp), %r10
	testl	%r10d, %r10d
	je	.L16
	movl	176(%rsp), %edx
	movl	%ebx, %ebp
	subl	%r10d, %r12d
	imull	%r10d, %ebp
	imull	%r10d, %edx
	addl	%edx, %eax
	jmp	.L17
.L16:
	testl	%r9d, %r9d
	je	.L17
	movl	16(%rsp), %esi
	addl	%r12d, %r15d
	xorl	%ebp, %ebp
	cmpl	%esi, %r15d
	jle	.L17
	subl	%r9d, %r12d
.L17:
	testl	%r13d, %r13d
	jns	.L18
	testl	%r14d, %r14d
	je	.L18
	movl	184(%rsp), %edx
	imull	%r14d, %edx
	imull	%ecx, %r14d
	addl	%edx, %eax
	movl	%ebx, %edx
	addl	%r14d, %ebp
	subl	%r14d, %edx
	jmp	.L19
.L18:
	movl	%ebx, %edx
	testl	%r11d, %r11d
	je	.L19
	movl	24(%rsp), %esi
	addl	160(%rsp), %r13d
	cmpl	%esi, %r13d
	jle	.L19
	imull	%r11d, %ecx
	subl	%ecx, %edx
.L19:
	movslq	%edx, %r13
	movslq	176(%rsp), %r14
	cltq
	movslq	%ebx, %rbx
	leaq	0(,%r13,4), %rsi
	movslq	%ebp, %rbp
	salq	$2, %rbx
	xorl	%r13d, %r13d
	movq	%rsi, 16(%rsp)
	salq	$2, %r14
	leaq	0(,%rax,4), %r15
	salq	$2, %rbp
.L20:
	cmpl	%r13d, %r12d
	jle	.L32
	movq	8(%rsp), %rcx
	cmpq	%rbp, 8(%rsp)
	leaq	(%r8,%rbp), %rdi
	movq	%r8, 24(%rsp)
	movq	144(%rsp), %rsi
	movq	16(%rsp), %rdx
	cmovb	%rbp, %rcx
	incl	%r13d
	addq	%r15, %rsi
	addq	%r14, %r15
	subq	%rbp, %rcx
	addq	%rbx, %rbp
	call	__memcpy_chk@PLT
	movq	24(%rsp), %r8
	jmp	.L20
.L32:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	movq	%r8, %rax
	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
.LFE2032:
	.size	_Z15get_input_patch6Tensor7Paddingiiiiiiii, .-_Z15get_input_patch6Tensor7Paddingiiiiiiii
	.globl	_Z27__device_stub__Z3dotiPfS_S_iPfS_S_
	.type	_Z27__device_stub__Z3dotiPfS_S_iPfS_S_, @function
_Z27__device_stub__Z3dotiPfS_S_iPfS_S_:
.LFB2060:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movl	%edi, 28(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movq	%rcx, (%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	28(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%rsp, %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L33
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z3dotiPfS_S_(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L33:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L35
	call	__stack_chk_fail@PLT
.L35:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2060:
	.size	_Z27__device_stub__Z3dotiPfS_S_iPfS_S_, .-_Z27__device_stub__Z3dotiPfS_S_iPfS_S_
	.globl	_Z3dotiPfS_S_
	.type	_Z3dotiPfS_S_, @function
_Z3dotiPfS_S_:
.LFB2061:
	.cfi_startproc
	endbr64
	jmp	_Z27__device_stub__Z3dotiPfS_S_iPfS_S_
	.cfi_endproc
.LFE2061:
	.size	_Z3dotiPfS_S_, .-_Z3dotiPfS_S_
	.globl	_Z8cuda_dotiPfS_S_
	.type	_Z8cuda_dotiPfS_S_, @function
_Z8cuda_dotiPfS_S_:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rsi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rdx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rcx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%edi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	$32, %ebx
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	cmpl	$8192, %edi
	jg	.L39
	leal	255(%rdi), %eax
	movl	$256, %ecx
	cltd
	idivl	%ecx
	movl	%eax, %ebx
.L39:
	movslq	%ebx, %rax
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	leaq	24(%rsp), %rdi
	movq	%rax, %rbp
	movslq	%r12d, %rax
	salq	$2, %rax
	movq	%rax, %rsi
	movq	%rax, (%rsp)
	call	cudaMalloc@PLT
	movl	$54, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movq	(%rsp), %rsi
	leaq	32(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	$55, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	leaq	20(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	movl	$56, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movq	8(%rsp), %rsi
	leaq	40(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	$57, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movq	(%rsp), %rdx
	movq	24(%rsp), %rdi
	movq	%r15, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$60, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movq	(%rsp), %rdx
	movq	32(%rsp), %rdi
	movq	%r14, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$61, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	movl	%ebx, 48(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movl	$16777217, %edx
	movl	$1, %ecx
	movq	%rax, 52(%rsp)
	movq	48(%rsp), %rdi
	salq	$8, %rdx
	movl	56(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L40
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%r12d, %edi
	movq	24(%rsp), %rsi
	call	_Z27__device_stub__Z3dotiPfS_S_iPfS_S_
.L40:
	movq	8(%rsp), %rdx
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbp, %rdi
	call	cudaMemcpy@PLT
	movl	$66, %esi
	movl	%eax, %edi
	call	_ZL11HandleError9cudaErrorPKci.constprop.0
	xorl	%eax, %eax
	xorl	%edx, %edx
	xorps	%xmm0, %xmm0
	movl	$0x00000000, 20(%rsp)
.L41:
	cmpl	%eax, %ebx
	jle	.L55
	addss	0(%rbp,%rax,4), %xmm0
	movb	$1, %dl
	incq	%rax
	jmp	.L41
.L55:
	testb	%dl, %dl
	je	.L43
	movss	%xmm0, 20(%rsp)
.L43:
	movl	20(%rsp), %eax
	movq	24(%rsp), %rdi
	movl	%eax, 0(%r13)
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	testq	%rbp, %rbp
	je	.L38
	movq	%rbp, %rdi
	call	_ZdaPv@PLT
.L38:
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L45
	call	__stack_chk_fail@PLT
.L45:
	addq	$88, %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
.LFE2028:
	.size	_Z8cuda_dotiPfS_S_, .-_Z8cuda_dotiPfS_S_
	.globl	_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_
	.type	_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_, @function
_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%r15d, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	xorl	%r14d, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rcx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movl	4(%rdi), %r10d
	movl	8(%rdi), %r13d
	movq	%rsi, 40(%rsp)
	movq	%rdx, 48(%rsp)
	movl	%r10d, %r11d
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	movl	(%rdi), %eax
	movl	12(%rdi), %edi
	imull	%edi, %r11d
	movl	%eax, 20(%rsp)
	movl	%r11d, %eax
	imull	%r13d, %eax
	movl	%eax, 32(%rsp)
	movl	%edi, %eax
	imull	%r13d, %eax
	movl	%eax, 36(%rsp)
	movslq	%edi, %rax
	salq	$2, %rax
	movq	%rax, 24(%rsp)
	leaq	100(%rsp), %rax
	movq	%rax, 56(%rsp)
.L57:
	movl	20(%rsp), %eax
	cmpl	%eax, %r12d
	jge	.L65
	movl	%r15d, %eax
	movl	%r14d, %r8d
	xorl	%r9d, %r9d
.L61:
	cmpl	%r10d, %r9d
	jge	.L59
	movq	40(%rsp), %rsi
	movslq	%r8d, %rdx
	movq	48(%rsp), %rcx
	leaq	(%rsi,%rdx,4), %rbx
	movslq	%eax, %rdx
	movq	%rbx, 8(%rsp)
	leaq	(%rcx,%rdx,4), %rdx
	xorl	%ebx, %ebx
.L60:
	cmpl	%ebx, %r13d
	jle	.L66
	movq	56(%rsp), %rcx
	movq	8(%rsp), %rsi
	movl	%eax, 92(%rsp)
	movl	%r8d, 88(%rsp)
	movl	%r11d, 84(%rsp)
	movl	%r10d, 80(%rsp)
	movl	%r9d, 76(%rsp)
	movq	%rdx, 64(%rsp)
	movl	%edi, 72(%rsp)
	movl	$0x00000000, 100(%rsp)
	call	_Z8cuda_dotiPfS_S_
	movss	0(%rbp,%rbx,4), %xmm0
	addss	100(%rsp), %xmm0
	movq	24(%rsp), %rax
	movq	64(%rsp), %rdx
	movl	88(%rsp), %r8d
	movl	84(%rsp), %r11d
	movss	%xmm0, 0(%rbp,%rbx,4)
	addq	%rax, %rdx
	movl	80(%rsp), %r10d
	incq	%rbx
	movl	92(%rsp), %eax
	movl	76(%rsp), %r9d
	movl	72(%rsp), %edi
	jmp	.L60
.L66:
	movl	36(%rsp), %ecx
	incl	%r9d
	addl	%edi, %r8d
	addl	%ecx, %eax
	jmp	.L61
.L59:
	movl	32(%rsp), %eax
	incl	%r12d
	addl	%r11d, %r14d
	addl	%eax, %r15d
	jmp	.L57
.L65:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L62
	call	__stack_chk_fail@PLT
.L62:
	addq	$120, %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
.LFE2033:
	.size	_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_, .-_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_
	.globl	_Z6conv2d6TensorS_
	.type	_Z6conv2d6TensorS_, @function
_Z6conv2d6TensorS_:
.LFB2034:
	.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
	movq	%rdi, %rbx
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movl	180(%rsp), %ebp
	movl	176(%rsp), %r13d
	movl	184(%rsp), %r12d
	movl	216(%rsp), %r15d
	movl	%ebp, %r14d
	imull	%r13d, %r14d
	imull	%r12d, %r14d
	imull	%r15d, %r14d
	sall	$2, %r14d
	movslq	%r14d, %rax
	addl	$16, %r14d
	movq	%rax, %rdi
	movq	%rax, 24(%rsp)
	call	malloc@PLT
	movl	%r13d, %edi
	movl	%r14d, 24(%rbx)
	movl	%r12d, %esi
	movq	%rax, 32(%rsp)
	movl	188(%rsp), %r14d
	movq	%rax, 16(%rbx)
	movq	%rbp, %rax
	salq	$32, %rax
	orq	%rax, %rdi
	movq	%r15, %rax
	salq	$32, %rax
	movq	%rdi, (%rbx)
	shrq	$32, %rdi
	orq	%rax, %rsi
	movl	%r14d, %eax
	imull	%r12d, %eax
	movq	%rsi, 8(%rbx)
	movl	%eax, 64(%rsp)
	imull	%ebp, %eax
	movl	%eax, 84(%rsp)
	movl	%r15d, %eax
	imull	%r12d, %eax
	movl	%eax, 68(%rsp)
	imull	%ebp, %eax
	movl	%eax, 104(%rsp)
	movl	212(%rsp), %eax
	movl	%eax, 40(%rsp)
	movl	208(%rsp), %eax
	movl	40(%rsp), %ecx
	movl	%eax, %edx
	movl	%eax, 44(%rsp)
	call	_Z22get_same_padding_in_tfiiii
	xorl	%ecx, %ecx
	movq	%rdx, 96(%rsp)
	movslq	%r15d, %rdx
	movq	%rdx, 48(%rsp)
	salq	$2, %rdx
	movq	%rdx, 56(%rsp)
	movl	%ecx, 80(%rsp)
	movl	%ecx, 4(%rsp)
	movq	%rax, 88(%rsp)
.L68:
	cmpl	%r13d, 4(%rsp)
	jge	.L72
	xorl	%eax, %eax
	movl	80(%rsp), %r11d
	movl	%eax, 8(%rsp)
.L73:
	cmpl	%ebp, 8(%rsp)
	jge	.L70
	xorl	%edx, %edx
	movslq	%r11d, %r10
	movl	%edx, 12(%rsp)
	leaq	0(,%r10,4), %r15
.L71:
	cmpl	%r12d, 12(%rsp)
	jge	.L75
	movq	48(%rsp), %rdi
	movl	$4, %esi
	movl	%r11d, 108(%rsp)
	call	calloc@PLT
	movq	96(%rsp), %rsi
	movl	%r13d, 176(%rsp)
	movq	%rax, 16(%rsp)
	movl	%ebp, 180(%rsp)
	movl	%r12d, 184(%rsp)
	pushq	%r14
	.cfi_def_cfa_offset 184
	movl	72(%rsp), %eax
	pushq	%rax
	.cfi_def_cfa_offset 192
	movl	100(%rsp), %eax
	pushq	%rax
	.cfi_def_cfa_offset 200
	movl	64(%rsp), %eax
	pushq	%rax
	.cfi_def_cfa_offset 208
	subq	$32, %rsp
	.cfi_def_cfa_offset 240
	movups	240(%rsp), %xmm0
	movups	256(%rsp), %xmm1
	movl	108(%rsp), %r9d
	movl	76(%rsp), %r8d
	movq	152(%rsp), %rdi
	movl	72(%rsp), %ecx
	movups	%xmm0, (%rsp)
	movl	68(%rsp), %edx
	movups	%xmm1, 16(%rsp)
	call	_Z15get_input_patch6Tensor7Paddingiiiiiiii
	movq	%rax, 136(%rsp)
	addq	$64, %rsp
	.cfi_def_cfa_offset 176
	movq	16(%rsp), %rcx
	movq	%rax, %rsi
	movq	224(%rsp), %rdx
	leaq	208(%rsp), %rdi
	call	_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_
	movq	24(%rsp), %rcx
	cmpq	%r15, 24(%rsp)
	movq	32(%rsp), %rax
	movq	56(%rsp), %rdx
	cmovb	%r15, %rcx
	movq	16(%rsp), %rsi
	leaq	(%rax,%r15), %rdi
	subq	%r15, %rcx
	call	__memcpy_chk@PLT
	movq	72(%rsp), %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	free@PLT
	movq	56(%rsp), %rax
	incl	12(%rsp)
	movl	108(%rsp), %r11d
	addq	%rax, %r15
	jmp	.L71
.L75:
	movl	68(%rsp), %eax
	incl	8(%rsp)
	addl	%eax, %r11d
	jmp	.L73
.L70:
	movl	104(%rsp), %edx
	incl	4(%rsp)
	addl	%edx, 80(%rsp)
	jmp	.L68
.L72:
	addq	$120, %rsp
	.cfi_def_cfa_offset 56
	movq	%rbx, %rax
	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
.LFE2034:
	.size	_Z6conv2d6TensorS_, .-_Z6conv2d6TensorS_
	.section	.rodata.str1.1
.LC9:
	.string	"Elapsed time: %f \n"
.LC10:
	.string	"output_tensor.bin"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rsi, %rbx
	subq	$112, %rsp
	.cfi_def_cfa_offset 128
	movq	8(%rsi), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	_Z10get_tensorPc
	movq	16(%rbx), %rsi
	leaq	40(%rsp), %rdi
	call	_Z10get_tensorPc
	call	clock@PLT
	leaq	72(%rsp), %rdi
	subq	$64, %rsp
	.cfi_def_cfa_offset 192
	movups	104(%rsp), %xmm1
	movups	120(%rsp), %xmm2
	movq	%rax, %rbx
	movups	72(%rsp), %xmm3
	movups	88(%rsp), %xmm4
	movups	%xmm1, 32(%rsp)
	movups	%xmm2, 48(%rsp)
	movups	%xmm3, (%rsp)
	movups	%xmm4, 16(%rsp)
	call	_Z6conv2d6TensorS_
	addq	$64, %rsp
	.cfi_def_cfa_offset 128
	call	clock@PLT
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	subq	%rbx, %rax
	cvtsi2ssq	%rax, %xmm0
	movb	$1, %al
	divss	.LC8(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	subq	$32, %rsp
	.cfi_def_cfa_offset 160
	leaq	.LC10(%rip), %rdi
	movups	104(%rsp), %xmm5
	movups	120(%rsp), %xmm6
	movups	%xmm5, (%rsp)
	movups	%xmm6, 16(%rsp)
	call	_Z12write_tensor6TensorPc
	movq	56(%rsp), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 128
	call	free@PLT
	movq	56(%rsp), %rdi
	call	free@PLT
	movq	88(%rsp), %rdi
	call	free@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L77
	call	__stack_chk_fail@PLT
.L77:
	addq	$112, %rsp
	.cfi_def_cfa_offset 16
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2035:
	.size	main, .-main
	.section	.rodata.str1.1
.LC11:
	.string	"_Z3dotiPfS_S_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2063:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC11(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z3dotiPfS_S_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2063:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC8:
	.long	1232348160
	.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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define HANDLE_ERROR(err) (HandleError( err, __FILE__, __LINE__ ))
#define THREADS_PER_BLOCK 256


static void HandleError(hipError_t err, const char *file, int line) {
    if (err != hipSuccess) {
        printf("%s in %s at line %d\n", hipGetErrorString(err), file, line);
        exit(EXIT_FAILURE);
    }
}

__global__ void dot(int N, float *a, float *b, float *c) {
    __shared__ float cache[THREADS_PER_BLOCK];      // buffer of shared memory
    int tid = threadIdx.x + blockIdx.x * blockDim.x;
    int cacheIndex = threadIdx.x;

    float temp = 0;
    while (tid < N) {
        temp += a[tid] * b[tid];
        tid += blockDim.x * gridDim.x;
    }

    cache[cacheIndex] = temp;   // clear shared memory buffer

    __syncthreads();

    int i = blockDim.x/2;
    while (i != 0) {
        if (cacheIndex < i)
            cache[cacheIndex] += cache[cacheIndex + i];
        __syncthreads();
        i /= 2;
    }

    if (cacheIndex == 0)
        c[blockIdx.x] = cache[0];
}

void cuda_dot(int N, float *a, float *b, float *result) {
    int blocksPerGrid = min(32, (N+THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK);
    float *partial_c, c;
    float *dev_a, *dev_b, *dev_partial_c;

    partial_c =(float*) malloc (blocksPerGrid*sizeof(float));

    // allocate the memory on the GPU
    HANDLE_ERROR(hipMalloc((void **) &dev_a, N * sizeof(float)));
    HANDLE_ERROR(hipMalloc((void **) &dev_b, N * sizeof(float)));
    HANDLE_ERROR(hipMalloc((void **) &c, sizeof(float)));
    HANDLE_ERROR(hipMalloc((void **) &dev_partial_c, blocksPerGrid*sizeof(float)));

    // copy the arrays 'a' and 'b' to the GPU
    HANDLE_ERROR(hipMemcpy(dev_a, a, N * sizeof(float), hipMemcpyHostToDevice));
    HANDLE_ERROR(hipMemcpy(dev_b, b, N * sizeof(float), hipMemcpyHostToDevice));

    dot<<<blocksPerGrid, THREADS_PER_BLOCK>>>(N, dev_a, dev_b, dev_partial_c);

    // copy the array 'c' back from the GPU to the CPU
    HANDLE_ERROR(hipMemcpy(partial_c, dev_partial_c, blocksPerGrid*sizeof(float), hipMemcpyDeviceToHost));

    c = 0;
    for (int i=0; i<blocksPerGrid; i++) {
        c += partial_c[i];
    }

    memcpy(result, &c, sizeof(float));

    // free the memory allocated on the GPU
    hipFree(dev_a);
    hipFree(dev_b);
    hipFree(dev_partial_c);

    delete [] partial_c;
}



struct Tensor {
    int shape[4];
    float* vector;
    int sz;
};


struct Tensor get_tensor(char* file_name) {
    FILE *f;
    if ((f = fopen(file_name, "rb")) == NULL) {
        printf("Error: file opening in %s", file_name);
        exit(0);
    }
    fseek(f, 0, SEEK_END);
    int file_sz = ftell(f);
    fseek(f, 0, SEEK_SET);

    int shape[4] = {-1, -1, -1, -1};
    float* vector = (float *) malloc(file_sz - 16);  // 16 for shape

    fread(shape, 4, 4, f);
    fread(vector, file_sz - 16, 1, f);

    printf("Read file: %d bytes (%d, %d, %d, %d) shape from %s\n",
           file_sz, shape[0], shape[1], shape[2], shape[3], file_name);

    struct Tensor tensor;
    tensor.vector = vector;
    memcpy(tensor.shape, shape, 16);
    tensor.sz = file_sz;
    return tensor;
}


void write_tensor(struct Tensor tensor, char* file_name) {
    FILE *fp = fopen(file_name, "wb");
    fwrite(tensor.shape, 4, 4, fp);
    fwrite(tensor.vector, tensor.sz - 16, 1, fp);
    fclose(fp);
    printf("Write tensor: %s\n", file_name);
}


struct Padding {
    int top;
    int bottom;
    int left;
    int right;
};


struct Padding get_same_padding_in_tf(int oh, int ow, int kh, int kw) {
    // strides: 1 (default)
    // padding: same (default)
    int p_vertical = max(- 1 + kh, 0);
    int p_horizontal = max(- 1 + kw, 0);
    struct Padding pad;
    pad.top = p_vertical / 2;
    pad.bottom = p_vertical - pad.top;
    pad.left = p_horizontal / 2;
    pad.right = p_horizontal - pad.left;
    return pad;
}


float* get_input_patch(struct Tensor input, struct Padding pad,
                       int _n, int _h, int _w, int kh, int kw,
                       int i0, int i1, int i2) {
    // input.shape: (N=1, H, W, C=IC)
    // --> (kh, kw, ic)
    int h = input.shape[1];
    int w = input.shape[2];
    int ic = input.shape[3];
    int patch_sz = kh * kw * ic;
    float *patch = (float *) calloc(patch_sz, sizeof(float));

    int center_h = kh / 2;
    int center_w = kw / 2;

    int input_base_idx = i0 * _n + i1 * (_h - center_h) + i2 * (_w - center_w);
    int patch_base_idx = 0;
    int size = kw * ic;
    int end_h = kh;

    if (pad.top != 0 && _h - center_h < 0) {
        input_base_idx += i1 * pad.top;
        patch_base_idx += kw * ic * pad.top;
        end_h -= pad.top;
    } else if (pad.bottom != 0 && _h - center_h + kh > h) {
        end_h -= pad.bottom;
    }
    if (pad.left != 0 && _w - center_w < 0){
        input_base_idx += i2 * pad.left;
        patch_base_idx += ic * pad.left;
        size -= ic * pad.left;
    } else if (pad.right != 0 && _w - center_w + kw > w) {
        size -= ic * pad.right;
    }
    int input_start_idx, patch_start_idx;
    for (int _kh = 0; _kh < end_h; _kh++) {
        input_start_idx = input_base_idx + i1 * _kh;
        patch_start_idx = patch_base_idx + kw * ic * _kh;
        memcpy(patch + patch_start_idx, input.vector + input_start_idx, size * sizeof(float));
    }
    return patch;
}


void einsum_hwi_hwoi_to_o(int* shape, float* v_hwi, float* v_hwoi, float* v_o) {
    // shape: h, w, o, i
    int h = shape[0];
    int w = shape[1];
    int o = shape[2];
    int i = shape[3];
    int hwi_idx, hwoi_idx;
    for (int _h = 0; _h < h; _h++) {
        for (int _w = 0; _w < w; _w++) {
            for (int _o = 0; _o < o; _o++) {
                hwi_idx = (w * i) * _h + i * _w;
                hwoi_idx = (w * o * i) * _h + (o * i) * _w + i * _o;

                float dot_product_ret = 0;
                cuda_dot(i, v_hwi + hwi_idx, v_hwoi + hwoi_idx, &dot_product_ret);
                v_o[_o] += dot_product_ret;
            }
        }
    }
}


struct Tensor conv2d(struct Tensor input, struct Tensor kernel) {
    // input.shape: (N, H, W, C=IC)
    // kernel.shape: (KH, KW, OC, IC=C)
    // output.shape: (N, H, W, OC)
    int output_sz = input.shape[0] * input.shape[1] * input.shape[2] * kernel.shape[2] * 4;
    struct Tensor out;
    out.vector = (float *) malloc(output_sz);
    int shape[4] = {input.shape[0], input.shape[1], input.shape[2], kernel.shape[2]};
    memcpy(out.shape, shape, 16);
    out.sz = output_sz + 16;

    int i2 = input.shape[3];
    int i1 = input.shape[2] * i2;
    int i0 = input.shape[1] * i1;

    int o2 = kernel.shape[2];
    int o1 = input.shape[2] * o2;
    int o0 = input.shape[1] * o1;

    struct Padding pad = get_same_padding_in_tf(out.shape[1], out.shape[2], kernel.shape[0], kernel.shape[1]);

    float *patch_hwi;  // kh * kw * ic
    float *v_o;

    int start_odx;
    int oc = kernel.shape[2];
    for (int _n = 0; _n < input.shape[0]; _n++) {  // N
        for (int _h = 0; _h < input.shape[1]; _h++) { // H
            for (int _w = 0; _w < input.shape[2]; _w++) { // W

                start_odx = o0 * _n + o1 * _h + o2 * _w;

                v_o = (float *) calloc(oc, sizeof(float));

                patch_hwi = get_input_patch(input, pad, _n, _h, _w, kernel.shape[0], kernel.shape[1], i0, i1, i2);
                einsum_hwi_hwoi_to_o(kernel.shape, patch_hwi, kernel.vector, v_o);

                memcpy(out.vector + start_odx, v_o, oc * sizeof(float));

                free(patch_hwi);
                free(v_o);
            }
        }
    }
    return out;
}


int main (int argc, char* argv[]) {

    clock_t start, end;
    float elapsed_time;

    struct Tensor tensor_in = get_tensor(argv[1]);
    struct Tensor tensor_ke = get_tensor(argv[2]);

    struct Tensor tensor_ot;

    start = clock();
    tensor_ot = conv2d(tensor_in, tensor_ke);
    end = clock();
    elapsed_time = (float) (end - start) / CLOCKS_PER_SEC;
    printf("Elapsed time: %f \n", elapsed_time);

    write_tensor(tensor_ot, "output_tensor.bin");

    free(tensor_in.vector);
    free(tensor_ke.vector);
    free(tensor_ot.vector);
    return 0;
}


	.text
	.file	"convolution.hip"
	.globl	_Z18__device_stub__dotiPfS_S_   # -- Begin function _Z18__device_stub__dotiPfS_S_
	.type	_Z18__device_stub__dotiPfS_S_,@function
_Z18__device_stub__dotiPfS_S_:          # @_Z18__device_stub__dotiPfS_S_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	4(%rsp), %rax
	movl	%edi, (%rax)
	leaq	40(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	32(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	24(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%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	$_Z3dotiPfS_S_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z18__device_stub__dotiPfS_S_, .Lfunc_end0-_Z18__device_stub__dotiPfS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z8cuda_dotiPfS_S_              # -- Begin function _Z8cuda_dotiPfS_S_
	.type	_Z8cuda_dotiPfS_S_,@function
_Z8cuda_dotiPfS_S_:                     # @_Z8cuda_dotiPfS_S_
	.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	$56, %rsp
	.cfi_def_cfa_offset 112
	.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	%rcx, 48(%rsp)                  # 8-byte Spill
	movq	%rdx, 40(%rsp)                  # 8-byte Spill
	movq	%rsi, 32(%rsp)                  # 8-byte Spill
	movl	%edi, %r15d
	movl	$32, %eax
	cmpl	$8192, %edi                     # imm = 0x2000
	jg	.LBB1_2
# %bb.1:
	leal	255(%r15), %ecx
	leal	510(%r15), %eax
	testl	%ecx, %ecx
	cmovnsl	%ecx, %eax
	sarl	$8, %eax
.LBB1_2:
	movslq	%eax, %r13
	leaq	(,%r13,4), %r12
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r14
	movslq	%r15d, %rbx
	shlq	$2, %rbx
	leaq	24(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$56, %esi
	callq	_ZL11HandleError10hipError_tPKci
	leaq	16(%rsp), %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$57, %esi
	callq	_ZL11HandleError10hipError_tPKci
	leaq	4(%rsp), %rdi
	movl	$4, %esi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$58, %esi
	callq	_ZL11HandleError10hipError_tPKci
	leaq	8(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$59, %esi
	callq	_ZL11HandleError10hipError_tPKci
	movq	(%rbp), %rdi
	movq	32(%rsp), %rsi                  # 8-byte Reload
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$62, %esi
	callq	_ZL11HandleError10hipError_tPKci
	leaq	16(%rsp), %rax
	movq	(%rax), %rdi
	movq	40(%rsp), %rsi                  # 8-byte Reload
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$63, %esi
	callq	_ZL11HandleError10hipError_tPKci
	movl	%r13d, %ebx
	movq	%rbx, %rdi
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$256, %rdx                      # imm = 0x100
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_4
# %bb.3:
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rcx
	movl	%r15d, %edi
	callq	_Z18__device_stub__dotiPfS_S_
.LBB1_4:
	movq	8(%rsp), %rsi
	movq	%r14, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$68, %esi
	callq	_ZL11HandleError10hipError_tPKci
	movl	$0, 4(%rsp)
	xorps	%xmm0, %xmm0
	testl	%ebx, %ebx
	jle	.LBB1_8
# %bb.5:                                # %.lr.ph.preheader
	xorl	%eax, %eax
.LBB1_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	addss	(%r14,%rax,4), %xmm0
	incq	%rax
	cmpq	%rax, %rbx
	jne	.LBB1_6
# %bb.7:                                # %._crit_edge
	movss	%xmm0, 4(%rsp)
.LBB1_8:
	movq	48(%rsp), %rax                  # 8-byte Reload
	movss	%xmm0, (%rax)
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	testq	%r14, %r14
	je	.LBB1_10
# %bb.9:
	movq	%r14, %rdi
	callq	_ZdaPv
.LBB1_10:
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z8cuda_dotiPfS_S_, .Lfunc_end1-_Z8cuda_dotiPfS_S_
	.cfi_endproc
                                        # -- End function
	.type	_ZL11HandleError10hipError_tPKci,@function # -- Begin function _ZL11HandleError10hipError_tPKci
_ZL11HandleError10hipError_tPKci:       # @_ZL11HandleError10hipError_tPKci
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB2_2
# %bb.1:
	retq
.LBB2_2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%esi, %ebx
	callq	hipGetErrorString
	movl	$.L.str.8, %edi
	movl	$.L.str, %edx
	movq	%rax, %rsi
	movl	%ebx, %ecx
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end2:
	.size	_ZL11HandleError10hipError_tPKci, .Lfunc_end2-_ZL11HandleError10hipError_tPKci
	.cfi_endproc
                                        # -- End function
	.globl	_Z10get_tensorPc                # -- Begin function _Z10get_tensorPc
	.type	_Z10get_tensorPc,@function
_Z10get_tensorPc:                       # @_Z10get_tensorPc
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.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, %rax
	movq	%rdi, %rbx
	movl	$.L.str.1, %esi
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	movq	%rax, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB3_2
# %bb.1:
	movq	%rax, %r12
	movq	%rax, %rdi
	xorl	%esi, %esi
	movl	$2, %edx
	callq	fseek
	movq	%r12, %rdi
	callq	ftell
	movq	%rax, %r15
	movq	%r12, %rdi
	xorl	%esi, %esi
	xorl	%edx, %edx
	callq	fseek
	leal	-16(%r15), %eax
	movslq	%eax, %r14
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %r13
	pcmpeqd	%xmm0, %xmm0
	leaq	16(%rsp), %rbp
	movdqa	%xmm0, (%rbp)
	movl	$4, %esi
	movl	$4, %edx
	movq	%rbp, %rdi
	movq	%r12, %rcx
	callq	fread
	movl	$1, %edx
	movq	%r13, %rdi
	movq	%r14, %rsi
	movq	%r12, %rcx
	callq	fread
	movl	(%rbp), %edx
	movl	4(%rbp), %ecx
	movl	8(%rbp), %r8d
	movl	12(%rbp), %r9d
	movq	8(%rsp), %rax                   # 8-byte Reload
	movq	%rax, (%rsp)
	movl	$.L.str.3, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	movq	%r13, 16(%rbx)
	movaps	(%rbp), %xmm0
	movups	%xmm0, (%rbx)
	movl	%r15d, 24(%rbx)
	movq	%rbx, %rax
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB3_2:
	.cfi_def_cfa_offset 96
	movl	$.L.str.2, %edi
	movq	8(%rsp), %rsi                   # 8-byte Reload
	xorl	%eax, %eax
	callq	printf
	xorl	%edi, %edi
	callq	exit
.Lfunc_end3:
	.size	_Z10get_tensorPc, .Lfunc_end3-_Z10get_tensorPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z12write_tensor6TensorPc       # -- Begin function _Z12write_tensor6TensorPc
	.type	_Z12write_tensor6TensorPc,@function
_Z12write_tensor6TensorPc:              # @_Z12write_tensor6TensorPc
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	$.L.str.4, %esi
	callq	fopen
	movq	%rax, %r14
	leaq	32(%rsp), %r15
	movl	$4, %esi
	movl	$4, %edx
	movq	%r15, %rdi
	movq	%rax, %rcx
	callq	fwrite
	movq	16(%r15), %rdi
	movslq	24(%r15), %rsi
	addq	$-16, %rsi
	movl	$1, %edx
	movq	%r14, %rcx
	callq	fwrite
	movq	%r14, %rdi
	callq	fclose
	movl	$.L.str.5, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	printf                          # TAILCALL
.Lfunc_end4:
	.size	_Z12write_tensor6TensorPc, .Lfunc_end4-_Z12write_tensor6TensorPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z22get_same_padding_in_tfiiii  # -- Begin function _Z22get_same_padding_in_tfiiii
	.type	_Z22get_same_padding_in_tfiiii,@function
_Z22get_same_padding_in_tfiiii:         # @_Z22get_same_padding_in_tfiiii
	.cfi_startproc
# %bb.0:
                                        # kill: def $edx killed $edx def $rdx
	cmpl	$2, %edx
	movl	$1, %esi
	cmovll	%esi, %edx
	leal	-1(%rdx), %edi
	cmpl	$2, %ecx
	cmovgel	%ecx, %esi
	decl	%esi
	movl	%edi, %eax
	shrl	%eax
	subl	%eax, %edi
	movl	%esi, %edx
	shrl	%edx
	subl	%edx, %esi
	shlq	$32, %rdi
	orq	%rdi, %rax
	shlq	$32, %rsi
	orq	%rsi, %rdx
	retq
.Lfunc_end5:
	.size	_Z22get_same_padding_in_tfiiii, .Lfunc_end5-_Z22get_same_padding_in_tfiiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z15get_input_patch6Tensor7Paddingiiiiiiii # -- Begin function _Z15get_input_patch6Tensor7Paddingiiiiiiii
	.type	_Z15get_input_patch6Tensor7Paddingiiiiiiii,@function
_Z15get_input_patch6Tensor7Paddingiiiiiiii: # @_Z15get_input_patch6Tensor7Paddingiiiiiiii
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%r9d, %ebp
	movl	%r8d, %r15d
	movl	%ecx, %r13d
	movl	%edx, %ebx
	movq	%rsi, 32(%rsp)                  # 8-byte Spill
	movq	%rdi, %r12
	movl	128(%rsp), %r14d
	movl	100(%rsp), %eax
	movl	%eax, 20(%rsp)                  # 4-byte Spill
	movl	104(%rsp), %eax
	movl	%eax, 16(%rsp)                  # 4-byte Spill
	movl	108(%rsp), %eax
	movl	%eax, 8(%rsp)                   # 4-byte Spill
	imull	%r14d, %eax
	movl	%eax, 12(%rsp)                  # 4-byte Spill
	imull	%r9d, %eax
	movslq	%eax, %rdi
	movl	$4, %esi
	callq	calloc
	movl	152(%rsp), %esi
	movl	144(%rsp), %edi
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	movl	%ebp, %eax
	shrl	$31, %eax
	addl	%ebp, %eax
	sarl	%eax
	movl	%r14d, %ecx
	shrl	$31, %ecx
	addl	%r14d, %ecx
	sarl	%ecx
	imull	136(%rsp), %ebx
	subl	%eax, %r13d
	movl	%r13d, %edx
	imull	%edi, %edx
	addl	%ebx, %edx
	subl	%ecx, %r15d
	movl	%r15d, %eax
	imull	%esi, %eax
	addl	%edx, %eax
	testl	%r12d, %r12d
	je	.LBB6_3
# %bb.1:
	testl	%r13d, %r13d
	jns	.LBB6_3
# %bb.2:
	movl	%r12d, %ecx
	imull	%edi, %ecx
	addl	%ecx, %eax
	movl	12(%rsp), %r8d                  # 4-byte Reload
	movl	%r8d, %ecx
	imull	%r12d, %ecx
	jmp	.LBB6_4
.LBB6_3:
	shrq	$32, %r12
	addl	%ebp, %r13d
	xorl	%ecx, %ecx
	cmpl	20(%rsp), %r13d                 # 4-byte Folded Reload
	movl	$0, %edx
	cmovgl	%r12d, %edx
	testq	%r12, %r12
	cmovel	%ecx, %edx
	movl	%edx, %r12d
	movl	12(%rsp), %r8d                  # 4-byte Reload
.LBB6_4:
	subl	%r12d, %ebp
	movq	32(%rsp), %rdx                  # 8-byte Reload
	testl	%edx, %edx
	je	.LBB6_7
# %bb.5:
	testl	%r15d, %r15d
	jns	.LBB6_7
# %bb.6:
	imull	%edx, %esi
	addl	%esi, %eax
	movl	8(%rsp), %esi                   # 4-byte Reload
	imull	%edx, %esi
	addl	%esi, %ecx
	movl	%r8d, %edx
	subl	%esi, %edx
	jmp	.LBB6_11
.LBB6_7:
	shrq	$32, %rdx
	testl	%edx, %edx
	je	.LBB6_8
# %bb.9:
	addl	%r14d, %r15d
	cmpl	16(%rsp), %r15d                 # 4-byte Folded Reload
	jle	.LBB6_8
# %bb.10:
	subl	%edx, %r14d
	movl	8(%rsp), %edx                   # 4-byte Reload
	imull	%r14d, %edx
	jmp	.LBB6_11
.LBB6_8:
	movl	%r8d, %edx
.LBB6_11:
	testl	%ebp, %ebp
	jle	.LBB6_14
# %bb.12:                               # %.lr.ph
	leaq	96(%rsp), %rsi
	movslq	%edx, %r14
	shlq	$2, %r14
	movslq	%edi, %r12
	movslq	%eax, %rbx
	movslq	%r8d, %r13
	movslq	%ecx, %rax
	movl	%ebp, %ebp
	movq	24(%rsp), %rcx                  # 8-byte Reload
	leaq	(%rcx,%rax,4), %r15
	shlq	$2, %rbx
	addq	16(%rsi), %rbx
	shlq	$2, %r13
	shlq	$2, %r12
.LBB6_13:                               # =>This Inner Loop Header: Depth=1
	movq	%r15, %rdi
	movq	%rbx, %rsi
	movq	%r14, %rdx
	callq	memcpy@PLT
	addq	%r13, %r15
	addq	%r12, %rbx
	decq	%rbp
	jne	.LBB6_13
.LBB6_14:                               # %._crit_edge
	movq	24(%rsp), %rax                  # 8-byte Reload
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end6:
	.size	_Z15get_input_patch6Tensor7Paddingiiiiiiii, .Lfunc_end6-_Z15get_input_patch6Tensor7Paddingiiiiiiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_ # -- Begin function _Z20einsum_hwi_hwoi_to_oPiPfS0_S0_
	.type	_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_,@function
_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_:     # @_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_
	.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	$88, %rsp
	.cfi_def_cfa_offset 144
	.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, 56(%rsp)                  # 8-byte Spill
	movl	(%rdi), %eax
	movl	%eax, 48(%rsp)                  # 4-byte Spill
	testl	%eax, %eax
	jle	.LBB7_9
# %bb.1:                                # %.preheader42.lr.ph
	movq	%rcx, %rbp
	movq	%rdx, %r15
	movl	4(%rdi), %eax
	movl	8(%rdi), %esi
	movl	12(%rdi), %r13d
	movl	%esi, %ecx
	imull	%r13d, %ecx
	movl	%ecx, 52(%rsp)                  # 4-byte Spill
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	imull	%eax, %ecx
	movl	%ecx, 44(%rsp)                  # 4-byte Spill
	xorl	%eax, %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	leaq	12(%rsp), %rbx
	movl	$0, 4(%rsp)                     # 4-byte Folded Spill
	movq	%rsi, 72(%rsp)                  # 8-byte Spill
.LBB7_2:                                # %.preheader42
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB7_4 Depth 2
                                        #       Child Loop BB7_6 Depth 3
	cmpl	$0, 24(%rsp)                    # 4-byte Folded Reload
	jle	.LBB7_8
# %bb.3:                                # %.preheader.lr.ph
                                        #   in Loop: Header=BB7_2 Depth=1
	movl	4(%rsp), %eax                   # 4-byte Reload
                                        # kill: def $eax killed $eax def $rax
	imull	24(%rsp), %eax                  # 4-byte Folded Reload
	movq	%rax, 64(%rsp)                  # 8-byte Spill
	movq	16(%rsp), %rax                  # 8-byte Reload
                                        # kill: def $eax killed $eax killed $rax
	movl	%eax, 8(%rsp)                   # 4-byte Spill
	xorl	%eax, %eax
	movq	%rax, 32(%rsp)                  # 8-byte Spill
.LBB7_4:                                # %.preheader
                                        #   Parent Loop BB7_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB7_6 Depth 3
	testl	%esi, %esi
	jle	.LBB7_7
# %bb.5:                                # %.lr.ph
                                        #   in Loop: Header=BB7_4 Depth=2
	movq	64(%rsp), %rax                  # 8-byte Reload
	movq	32(%rsp), %rcx                  # 8-byte Reload
	addl	%ecx, %eax
	imull	%r13d, %eax
	cltq
	movq	56(%rsp), %rcx                  # 8-byte Reload
	leaq	(%rcx,%rax,4), %rax
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	movl	8(%rsp), %eax                   # 4-byte Reload
	movl	%eax, %r12d
	xorl	%r14d, %r14d
.LBB7_6:                                #   Parent Loop BB7_2 Depth=1
                                        #     Parent Loop BB7_4 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movl	$0, 12(%rsp)
	movslq	%r12d, %r12
	leaq	(%r15,%r12,4), %rdx
	movl	%r13d, %edi
	movq	80(%rsp), %rsi                  # 8-byte Reload
	movq	%rbx, %rcx
	callq	_Z8cuda_dotiPfS_S_
	movq	72(%rsp), %rsi                  # 8-byte Reload
	movss	12(%rsp), %xmm0                 # xmm0 = mem[0],zero,zero,zero
	addss	(%rbp,%r14,4), %xmm0
	movss	%xmm0, (%rbp,%r14,4)
	incq	%r14
	addl	%r13d, %r12d
	cmpq	%r14, %rsi
	jne	.LBB7_6
.LBB7_7:                                # %._crit_edge
                                        #   in Loop: Header=BB7_4 Depth=2
	movq	32(%rsp), %rcx                  # 8-byte Reload
	incq	%rcx
	movl	8(%rsp), %eax                   # 4-byte Reload
	addl	52(%rsp), %eax                  # 4-byte Folded Reload
	movl	%eax, 8(%rsp)                   # 4-byte Spill
	movq	%rcx, %rax
	movq	%rcx, 32(%rsp)                  # 8-byte Spill
	cmpq	24(%rsp), %rcx                  # 8-byte Folded Reload
	jne	.LBB7_4
.LBB7_8:                                # %._crit_edge45
                                        #   in Loop: Header=BB7_2 Depth=1
	movl	4(%rsp), %ecx                   # 4-byte Reload
	incl	%ecx
	movq	16(%rsp), %rax                  # 8-byte Reload
	addl	44(%rsp), %eax                  # 4-byte Folded Reload
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	%ecx, 4(%rsp)                   # 4-byte Spill
	cmpl	48(%rsp), %ecx                  # 4-byte Folded Reload
	jne	.LBB7_2
.LBB7_9:                                # %._crit_edge47
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end7:
	.size	_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_, .Lfunc_end7-_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_
	.cfi_endproc
                                        # -- End function
	.globl	_Z6conv2d6TensorS_              # -- Begin function _Z6conv2d6TensorS_
	.type	_Z6conv2d6TensorS_,@function
_Z6conv2d6TensorS_:                     # @_Z6conv2d6TensorS_
	.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	$184, %rsp
	.cfi_def_cfa_offset 240
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %r15
	movl	240(%rsp), %ebp
	movl	244(%rsp), %r12d
	movl	248(%rsp), %r14d
	movslq	280(%rsp), %r13
	movl	%r13d, %eax
	imull	%r14d, %eax
	movl	%ebp, %ebx
	imull	%r12d, %ebx
	imull	%eax, %ebx
	leal	(,%rbx,4), %eax
	movslq	%eax, %rdi
	callq	malloc
	movq	%rax, 176(%rsp)                 # 8-byte Spill
	movq	%rax, 16(%r15)
	movl	%ebp, (%r15)
	movl	%r12d, 72(%rsp)                 # 4-byte Spill
	movl	%r12d, 4(%r15)
	movl	%r14d, 8(%r15)
	movl	%r13d, 12(%r15)
	leal	16(,%rbx,4), %eax
	movq	%r15, 128(%rsp)                 # 8-byte Spill
	movl	%eax, 24(%r15)
	movl	272(%rsp), %r15d
	movl	276(%rsp), %edx
	cmpl	$2, %r15d
	movl	$1, %eax
	movl	$1, %ecx
	cmovgel	%r15d, %ecx
	cmpl	$2, %edx
	movl	%edx, 124(%rsp)                 # 4-byte Spill
	cmovgel	%edx, %eax
	movl	%ebp, 104(%rsp)                 # 4-byte Spill
	testl	%ebp, %ebp
	jle	.LBB8_9
# %bb.1:                                # %.preheader38.lr.ph
	leaq	272(%rsp), %rsi
	leaq	240(%rsp), %rdx
	movl	12(%rdx), %edx
	movl	%edx, 120(%rsp)                 # 4-byte Spill
	imull	%r14d, %edx
	movl	%edx, 116(%rsp)                 # 4-byte Spill
	movl	%edx, %edi
	movl	72(%rsp), %edx                  # 4-byte Reload
	imull	%edx, %edi
	movl	%edi, 112(%rsp)                 # 4-byte Spill
	decl	%ecx
	decl	%eax
	movl	%ecx, %edi
	shrl	%edi
	subl	%edi, %ecx
	movl	%eax, %r8d
	shrl	%r8d
	subl	%r8d, %eax
	shlq	$32, %rcx
	orq	%rcx, %rdi
	movq	%rdi, 160(%rsp)                 # 8-byte Spill
	shlq	$32, %rax
	orq	%rax, %r8
	movq	%r8, 152(%rsp)                  # 8-byte Spill
	movq	16(%rsi), %rax
	movq	%rax, 144(%rsp)                 # 8-byte Spill
	leaq	(,%r13,4), %rax
	movq	%rax, 136(%rsp)                 # 8-byte Spill
	movl	%r14d, %eax
	imull	%r13d, %eax
	movl	%eax, 108(%rsp)                 # 4-byte Spill
	imull	%edx, %eax
	movl	%eax, 100(%rsp)                 # 4-byte Spill
	xorl	%eax, %eax
	movq	%rax, 88(%rsp)                  # 8-byte Spill
	xorl	%ecx, %ecx
	movq	%r14, 168(%rsp)                 # 8-byte Spill
.LBB8_2:                                # %.preheader38
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB8_4 Depth 2
                                        #       Child Loop BB8_6 Depth 3
	movl	%ecx, 80(%rsp)                  # 4-byte Spill
	cmpl	$0, 72(%rsp)                    # 4-byte Folded Reload
	jle	.LBB8_8
# %bb.3:                                # %.preheader.lr.ph
                                        #   in Loop: Header=BB8_2 Depth=1
	xorl	%ecx, %ecx
	movq	88(%rsp), %rax                  # 8-byte Reload
                                        # kill: def $eax killed $eax killed $rax
	movl	%eax, 76(%rsp)                  # 4-byte Spill
.LBB8_4:                                # %.preheader
                                        #   Parent Loop BB8_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB8_6 Depth 3
	movl	%ecx, 84(%rsp)                  # 4-byte Spill
	testl	%r14d, %r14d
	jle	.LBB8_7
# %bb.5:                                # %.lr.ph
                                        #   in Loop: Header=BB8_4 Depth=2
	movl	76(%rsp), %eax                  # 4-byte Reload
	movl	%eax, %ebp
	xorl	%ebx, %ebx
.LBB8_6:                                #   Parent Loop BB8_2 Depth=1
                                        #     Parent Loop BB8_4 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movl	$4, %esi
	movq	%r13, %rdi
	callq	calloc
	movq	%rax, %r12
	leaq	240(%rsp), %rax
	movups	(%rax), %xmm0
	movups	16(%rax), %xmm1
	movups	%xmm1, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	120(%rsp), %eax                 # 4-byte Reload
	movl	%eax, 56(%rsp)
	movl	116(%rsp), %eax                 # 4-byte Reload
	movl	%eax, 48(%rsp)
	movl	112(%rsp), %eax                 # 4-byte Reload
	movl	%eax, 40(%rsp)
	movl	124(%rsp), %eax                 # 4-byte Reload
	movl	%eax, 32(%rsp)
	movq	160(%rsp), %rdi                 # 8-byte Reload
	movq	152(%rsp), %rsi                 # 8-byte Reload
	movl	80(%rsp), %edx                  # 4-byte Reload
	movl	84(%rsp), %ecx                  # 4-byte Reload
	movl	%ebx, %r8d
	movl	%r15d, %r9d
	callq	_Z15get_input_patch6Tensor7Paddingiiiiiiii
	movl	%r15d, %r14d
	movq	%rax, %r15
	leaq	272(%rsp), %rdi
	movq	%rax, %rsi
	movq	144(%rsp), %rdx                 # 8-byte Reload
	movq	%r12, %rcx
	callq	_Z20einsum_hwi_hwoi_to_oPiPfS0_S0_
	movslq	%ebp, %rbp
	movq	176(%rsp), %rax                 # 8-byte Reload
	leaq	(%rax,%rbp,4), %rdi
	movq	%r12, %rsi
	movq	136(%rsp), %rdx                 # 8-byte Reload
	callq	memcpy@PLT
	movq	%r15, %rdi
	movl	%r14d, %r15d
	callq	free
	movq	%r12, %rdi
	callq	free
	movq	168(%rsp), %r14                 # 8-byte Reload
	incq	%rbx
	addl	%r13d, %ebp
	cmpq	%rbx, %r14
	jne	.LBB8_6
.LBB8_7:                                # %._crit_edge
                                        #   in Loop: Header=BB8_4 Depth=2
	movl	84(%rsp), %ecx                  # 4-byte Reload
	incl	%ecx
	movl	76(%rsp), %eax                  # 4-byte Reload
	addl	108(%rsp), %eax                 # 4-byte Folded Reload
	movl	%eax, 76(%rsp)                  # 4-byte Spill
	cmpl	72(%rsp), %ecx                  # 4-byte Folded Reload
	jne	.LBB8_4
.LBB8_8:                                # %._crit_edge41
                                        #   in Loop: Header=BB8_2 Depth=1
	movl	80(%rsp), %ecx                  # 4-byte Reload
	incl	%ecx
	movq	88(%rsp), %rax                  # 8-byte Reload
	addl	100(%rsp), %eax                 # 4-byte Folded Reload
	movq	%rax, 88(%rsp)                  # 8-byte Spill
	cmpl	104(%rsp), %ecx                 # 4-byte Folded Reload
	jne	.LBB8_2
.LBB8_9:                                # %._crit_edge43
	movq	128(%rsp), %rax                 # 8-byte Reload
	addq	$184, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end8:
	.size	_Z6conv2d6TensorS_, .Lfunc_end8-_Z6conv2d6TensorS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI9_0:
	.long	0x49742400                      # float 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$192, %rsp
	.cfi_def_cfa_offset 240
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %r14
	movq	8(%rsi), %rsi
	leaq	160(%rsp), %rbx
	movq	%rbx, %rdi
	callq	_Z10get_tensorPc
	movq	16(%r14), %rsi
	leaq	128(%rsp), %r14
	movq	%r14, %rdi
	callq	_Z10get_tensorPc
	callq	clock
	movq	%rax, %r12
	movups	(%r14), %xmm0
	movups	16(%r14), %xmm1
	movups	%xmm1, 48(%rsp)
	movups	%xmm0, 32(%rsp)
	movups	(%rbx), %xmm0
	movups	16(%rbx), %xmm1
	movups	%xmm1, 16(%rsp)
	movups	%xmm0, (%rsp)
	leaq	96(%rsp), %r13
	movq	%r13, %rdi
	callq	_Z6conv2d6TensorS_
	movups	(%r13), %xmm0
	movaps	%xmm0, 64(%rsp)
	movq	16(%r13), %r15
	movq	24(%r13), %r13
	callq	clock
	subq	%r12, %rax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	divss	.LCPI9_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	movq	%r15, 80(%rsp)
	movq	%r13, 88(%rsp)
	movaps	64(%rsp), %xmm0
	movaps	80(%rsp), %xmm1
	movups	%xmm1, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$.L.str.7, %edi
	callq	_Z12write_tensor6TensorPc
	movq	16(%rbx), %rdi
	callq	free
	movq	16(%r14), %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$192, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end9:
	.size	main, .Lfunc_end9-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	.LBB10_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB10_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z3dotiPfS_S_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end10:
	.size	__hip_module_ctor, .Lfunc_end10-__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	.LBB11_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
.LBB11_2:
	retq
.Lfunc_end11:
	.size	__hip_module_dtor, .Lfunc_end11-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z3dotiPfS_S_,@object           # @_Z3dotiPfS_S_
	.section	.rodata,"a",@progbits
	.globl	_Z3dotiPfS_S_
	.p2align	3, 0x0
_Z3dotiPfS_S_:
	.quad	_Z18__device_stub__dotiPfS_S_
	.size	_Z3dotiPfS_S_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/dongkwan-kim/naively-quantized-conv/master/code/prob4/convolution.hip"
	.size	.L.str, 132

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"rb"
	.size	.L.str.1, 3

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Error: file opening in %s"
	.size	.L.str.2, 26

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Read file: %d bytes (%d, %d, %d, %d) shape from %s\n"
	.size	.L.str.3, 52

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Write tensor: %s\n"
	.size	.L.str.5, 18

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

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"output_tensor.bin"
	.size	.L.str.7, 18

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

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z3dotiPfS_S_"
	.size	.L__unnamed_1, 14

	.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 _Z18__device_stub__dotiPfS_S_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z3dotiPfS_S_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z3dotiPfS_S_           ; -- Begin function _Z3dotiPfS_S_
	.globl	_Z3dotiPfS_S_
	.p2align	8
	.type	_Z3dotiPfS_S_,@function
_Z3dotiPfS_S_:                          ; @_Z3dotiPfS_S_
; %bb.0:
	s_clause 0x3
	s_load_b32 s3, s[0:1], 0x2c
	s_load_b32 s10, s[0:1], 0x0
	s_load_b64 s[8:9], s[0:1], 0x18
	s_load_b128 s[4:7], s[0:1], 0x8
	s_add_u32 s0, s0, 32
	s_mov_b32 s2, s15
	s_addc_u32 s1, s1, 0
	v_mov_b32_e32 v3, 0
	s_mov_b32 s11, exec_lo
	s_waitcnt lgkmcnt(0)
	s_and_b32 s3, s3, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s2, s3, v[0:1]
	v_cmpx_gt_i32_e64 s10, v1
	s_cbranch_execz .LBB0_4
; %bb.1:                                ; %.lr.ph
	s_load_b32 s1, s[0:1], 0x0
	v_mov_b32_e32 v3, 0
	s_mov_b32 s0, 0
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s1, s1, s3
.LBB0_2:                                ; =>This Inner Loop Header: Depth=1
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[4:5], 2, v[1:2]
	v_add_nc_u32_e32 v1, s1, v1
	v_add_co_u32 v6, vcc_lo, s4, v4
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v7, vcc_lo, s5, v5, vcc_lo
	v_add_co_u32 v4, vcc_lo, s6, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s7, v5, vcc_lo
	v_cmp_le_i32_e32 vcc_lo, s10, v1
	global_load_b32 v2, v[6:7], off
	global_load_b32 v4, v[4:5], off
	s_or_b32 s0, vcc_lo, s0
	s_waitcnt vmcnt(0)
	v_fmac_f32_e32 v3, v2, v4
	s_and_not1_b32 exec_lo, exec_lo, s0
	s_cbranch_execnz .LBB0_2
; %bb.3:                                ; %Flow43
	s_or_b32 exec_lo, exec_lo, s0
.LBB0_4:                                ; %Flow44
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 exec_lo, exec_lo, s11
	v_lshlrev_b32_e32 v1, 2, v0
	s_cmp_lt_u32 s3, 2
	ds_store_b32 v1, v3
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_8
.LBB0_5:                                ; %.lr.ph30
                                        ; =>This Inner Loop Header: Depth=1
	s_lshr_b32 s0, s3, 1
	s_mov_b32 s1, exec_lo
	v_cmpx_gt_u32_e64 s0, v0
	s_cbranch_execz .LBB0_7
; %bb.6:                                ;   in Loop: Header=BB0_5 Depth=1
	v_lshl_add_u32 v2, s0, 2, v1
	ds_load_b32 v2, v2
	ds_load_b32 v3, v1
	s_waitcnt lgkmcnt(0)
	v_add_f32_e32 v2, v2, v3
	ds_store_b32 v1, v2
.LBB0_7:                                ;   in Loop: Header=BB0_5 Depth=1
	s_or_b32 exec_lo, exec_lo, s1
	s_cmp_gt_u32 s3, 3
	s_mov_b32 s3, s0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_5
.LBB0_8:                                ; %._crit_edge31
	s_mov_b32 s3, 0
	s_mov_b32 s0, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_10
; %bb.9:
	v_mov_b32_e32 v0, 0
	s_lshl_b64 s[0:1], s[2:3], 2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_u32 s0, s8, s0
	s_addc_u32 s1, s9, s1
	ds_load_b32 v1, v0
	s_waitcnt lgkmcnt(0)
	global_store_b32 v0, v1, s[0:1]
.LBB0_10:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z3dotiPfS_S_
		.amdhsa_group_segment_fixed_size 1024
		.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 8
		.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	_Z3dotiPfS_S_, .Lfunc_end0-_Z3dotiPfS_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 412
; NumSgprs: 18
; NumVgprs: 8
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 1024 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 8
; 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:
      - .offset:         0
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           4
        .value_kind:     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: 1024
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z3dotiPfS_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z3dotiPfS_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     8
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
