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

typedef struct MapFileList {
	char* filename;
	struct MapFileList* next;
}MapFileList;

typedef struct MapInputList{
	char* key;
	char* value;
	struct MapInputList* next;
}MapInputList;

typedef enum InputFormat{TextInputFormat,KeyValueInputFormat,SequenceFileInputFormat} input_format;

typedef struct MapReduceSpec{
	MapFileList* map_file_list;
	MapInputList* map_input_list;
	unsigned map_block_num;
	unsigned map_thread_num;
	input_format map_input_format;
}MapReduceSpec;

void init_map_file_list(MapFileList* list){
	list->filename=NULL;
	list->next=NULL;
}

void free_map_file_list(MapFileList* list){
	MapFileList* del;
	MapFileList* tmp;
	del=list;
	tmp=list->next;
	while(tmp){
		if(del->filename!=NULL)
			free(del->filename);
		free(del);
		del=tmp;
		tmp=tmp->next;
	}
	if(del->filename!=NULL)
		free(del->filename);
	free(del);	
}

void init_map_input_list(MapInputList* list){
	list->key=NULL;
	list->value=NULL;
	list->next=NULL;
}

void free_map_input_list(MapInputList* list){
	MapInputList* del;
	MapInputList* tmp;
	del=list;
	tmp=list->next;
	while(tmp){
		if(del->key!=NULL)
			free(del->key);
		if(del->value!=NULL)
			free(del->value);
		free(del);
		del=tmp;
		tmp=tmp->next;
	}
	if(del->key!=NULL)
		free(del->key);
	if(del->value!=NULL)
		free(del->value);
	free(del);	
}

void init_mapreduce_spec(MapReduceSpec* spec){
	spec->map_file_list=NULL;
	spec->map_input_list=NULL;
	spec->map_block_num=4;
	spec->map_thread_num=128;
	spec->map_input_format=TextInputFormat;
}

void free_spec(MapReduceSpec* spec){
	free_map_file_list(spec->map_file_list);
	free_map_input_list(spec->map_input_list);
	free(spec);
}

void map_input_split(MapReduceSpec* spec){
	MapFileList* file_list_entry;
	size_t buffer_size=(size_t)256*1024*1024;
	size_t buffer_used=0;

	MapInputList* input_list_entry;

	FILE* pFile;
	file_list_entry=spec->map_file_list;
	
	input_list_entry=(MapInputList*)malloc(sizeof(MapInputList));
	init_map_input_list(input_list_entry);
	spec->map_input_list=input_list_entry;
	
	if(spec->map_input_format==TextInputFormat){
		size_t file_size;
		while(file_list_entry->filename!=NULL){
			pFile=fopen(file_list_entry->filename,"rb");
			if (pFile==NULL) {fputs ("File error\n",stderr); exit (1);}
			fseek (pFile , 0 , SEEK_END);
			file_size = ftell (pFile);
			rewind (pFile);
			if(buffer_used+file_size<=buffer_size){
				ssize_t result=0;
				size_t len = 0;
				while (result!= -1) {
					input_list_entry->key=(char*)malloc(10);
					sprintf(input_list_entry->key,"%d",(int)ftell(pFile));
					printf("%s ",input_list_entry->key);
					result=getline(&(input_list_entry->value), &len, pFile);
					printf("%s \n",input_list_entry->value);
					input_list_entry->next=(MapInputList*)malloc(sizeof(MapInputList));
					input_list_entry=input_list_entry->next;
					init_map_input_list(input_list_entry);
				}
				buffer_used=buffer_used+file_size;
			}
			else
				printf("Buffer full!!\n");
			file_list_entry=file_list_entry->next;
			fclose(pFile);
		}
	}


}

void add_input(char *path,MapReduceSpec* spec){
	MapFileList* plist;
	plist=(MapFileList*)malloc(sizeof(MapFileList));
	spec->map_file_list=plist;
	struct dirent* entry = NULL;
	DIR *pDir;
	pDir=opendir(path);
	while((entry=readdir(pDir))!=NULL){
		if(entry->d_type==DT_REG){
			plist->filename=(char*)malloc(strlen(path)+strlen(entry->d_name)+1);
			strcpy(plist->filename,path);
		       	strcat(plist->filename,entry->d_name);
			plist->next=(MapFileList*)malloc(sizeof(MapFileList));
			plist=plist->next;
		}
	}       
	map_input_split(spec);	
}

int main(int argc, char **argv){
	MapReduceSpec* spec=(MapReduceSpec*)malloc(sizeof(MapReduceSpec));
	add_input(argv[1],spec);
	free(spec);
}

	.file	"tmpxft_002b4641_00000000-6_test1.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
	.globl	_Z18init_map_file_listP11MapFileList
	.type	_Z18init_map_file_listP11MapFileList, @function
_Z18init_map_file_listP11MapFileList:
.LFB2027:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	movq	%rax, (%rdi)
	movq	%rax, 8(%rdi)
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z18init_map_file_listP11MapFileList, .-_Z18init_map_file_listP11MapFileList
	.globl	_Z18free_map_file_listP11MapFileList
	.type	_Z18free_map_file_listP11MapFileList, @function
_Z18free_map_file_listP11MapFileList:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rdi, %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 32
	movq	8(%rdi), %rbp
.L4:
	movq	(%rbx), %rdi
	testq	%rbp, %rbp
	je	.L16
	testq	%rdi, %rdi
	je	.L5
	call	free@PLT
.L5:
	movq	%rbx, %rdi
	movq	%rbp, %rbx
	call	free@PLT
	movq	8(%rbp), %rbp
	jmp	.L4
.L16:
	testq	%rdi, %rdi
	je	.L7
	call	free@PLT
.L7:
	popq	%rax
	.cfi_def_cfa_offset 24
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z18free_map_file_listP11MapFileList, .-_Z18free_map_file_listP11MapFileList
	.globl	_Z19init_map_input_listP12MapInputList
	.type	_Z19init_map_input_listP12MapInputList, @function
_Z19init_map_input_listP12MapInputList:
.LFB2029:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	movq	%rax, (%rdi)
	movq	%rax, 8(%rdi)
	movq	%rax, 16(%rdi)
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z19init_map_input_listP12MapInputList, .-_Z19init_map_input_listP12MapInputList
	.globl	_Z19free_map_input_listP12MapInputList
	.type	_Z19free_map_input_listP12MapInputList, @function
_Z19free_map_input_listP12MapInputList:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rdi, %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 32
	movq	16(%rdi), %rbp
.L19:
	movq	(%rbx), %rdi
	testq	%rbp, %rbp
	je	.L38
	testq	%rdi, %rdi
	je	.L20
	call	free@PLT
.L20:
	movq	8(%rbx), %rdi
	testq	%rdi, %rdi
	je	.L21
	call	free@PLT
.L21:
	movq	%rbx, %rdi
	movq	%rbp, %rbx
	call	free@PLT
	movq	16(%rbp), %rbp
	jmp	.L19
.L38:
	testq	%rdi, %rdi
	je	.L23
	call	free@PLT
.L23:
	movq	8(%rbx), %rdi
	testq	%rdi, %rdi
	je	.L24
	call	free@PLT
.L24:
	popq	%rax
	.cfi_def_cfa_offset 24
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z19free_map_input_listP12MapInputList, .-_Z19free_map_input_listP12MapInputList
	.globl	_Z19init_mapreduce_specP13MapReduceSpec
	.type	_Z19init_mapreduce_specP13MapReduceSpec, @function
_Z19init_mapreduce_specP13MapReduceSpec:
.LFB2031:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	xorl	%edx, %edx
	movq	%rax, (%rdi)
	movq	%rax, 8(%rdi)
	movabsq	$549755813892, %rax
	movq	%rax, 16(%rdi)
	movl	%edx, 24(%rdi)
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z19init_mapreduce_specP13MapReduceSpec, .-_Z19init_mapreduce_specP13MapReduceSpec
	.globl	_Z9free_specP13MapReduceSpec
	.type	_Z9free_specP13MapReduceSpec, @function
_Z9free_specP13MapReduceSpec:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	call	_Z18free_map_file_listP11MapFileList
	movq	8(%rbx), %rdi
	call	_Z19free_map_input_listP12MapInputList
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE2032:
	.size	_Z9free_specP13MapReduceSpec, .-_Z9free_specP13MapReduceSpec
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"rb"
.LC1:
	.string	"File error\n"
.LC2:
	.string	"%d"
.LC3:
	.string	"%s "
.LC4:
	.string	"%s \n"
.LC5:
	.string	"Buffer full!!\n"
	.text
	.globl	_Z15map_input_splitP13MapReduceSpec
	.type	_Z15map_input_splitP13MapReduceSpec, @function
_Z15map_input_splitP13MapReduceSpec:
.LFB2033:
	.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
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	(%rdi), %r13
	movl	$24, %edi
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	xorl	%ecx, %ecx
	cmpl	$0, 24(%rbp)
	movq	%rcx, (%rax)
	movq	%rcx, 8(%rax)
	movq	%rcx, 16(%rax)
	movq	%rax, 8(%rbp)
	jne	.L42
	movq	%rax, %rbx
	xorl	%r14d, %r14d
.L43:
	movq	0(%r13), %rdi
	testq	%rdi, %rdi
	je	.L42
	leaq	.LC0(%rip), %rsi
	call	fopen@PLT
	movq	%rax, %rbp
	testq	%rax, %rax
	jne	.L44
	movq	stderr(%rip), %rsi
	leaq	.LC1(%rip), %rdi
	call	fputs@PLT
	movl	$1, %edi
	call	exit@PLT
.L44:
	movl	$2, %edx
	xorl	%esi, %esi
	movq	%rax, %rdi
	call	fseek@PLT
	movq	%rbp, %rdi
	call	ftell@PLT
	movq	%rbp, %rdi
	movq	%rax, %r12
	call	rewind@PLT
	addq	%r14, %r12
	cmpq	$268435456, %r12
	ja	.L45
	xorl	%edx, %edx
	movq	%rdx, (%rsp)
.L46:
	movl	$10, %edi
	movq	%rbx, %r15
	call	malloc@PLT
	movq	%rbp, %rdi
	movq	%rax, (%rbx)
	call	ftell@PLT
	movq	(%rbx), %rdi
	orq	$-1, %rdx
	leaq	.LC2(%rip), %rcx
	movl	%eax, %r8d
	movl	$2, %esi
	xorl	%eax, %eax
	call	__sprintf_chk@PLT
	movq	(%rbx), %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC3(%rip), %rsi
	call	__printf_chk@PLT
	leaq	8(%rbx), %rdi
	movq	%rsp, %rsi
	movq	%rbp, %rdx
	call	getline@PLT
	movq	8(%rbx), %rdx
	movl	$2, %edi
	leaq	.LC4(%rip), %rsi
	movq	%rax, %r14
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$24, %edi
	call	malloc@PLT
	movq	%rax, 16(%r15)
	movq	%rax, %rbx
	xorl	%eax, %eax
	incq	%r14
	movq	%rax, (%rbx)
	movq	%rax, 8(%rbx)
	movq	%rax, 16(%rbx)
	jne	.L46
	jmp	.L47
.L45:
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	movq	%r14, %r12
	call	__printf_chk@PLT
.L47:
	movq	%rbp, %rdi
	movq	8(%r13), %r13
	movq	%r12, %r14
	call	fclose@PLT
	jmp	.L43
.L42:
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L50
	call	__stack_chk_fail@PLT
.L50:
	addq	$24, %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	_Z15map_input_splitP13MapReduceSpec, .-_Z15map_input_splitP13MapReduceSpec
	.globl	_Z9add_inputPcP13MapReduceSpec
	.type	_Z9add_inputPcP13MapReduceSpec, @function
_Z9add_inputPcP13MapReduceSpec:
.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
	movq	%rsi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	movl	$16, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	pushq	%rdx
	.cfi_def_cfa_offset 64
	call	malloc@PLT
	movq	%r12, %rdi
	movq	%rax, 0(%r13)
	movq	%rax, %rbp
	call	opendir@PLT
	movq	%rax, %r14
.L56:
	movq	%r14, %rdi
	call	readdir@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	je	.L60
	cmpb	$8, 18(%rbx)
	jne	.L56
	movq	%r12, %rdi
	addq	$19, %rbx
	call	strlen@PLT
	movq	%rbx, %rdi
	movq	%rax, %r15
	call	strlen@PLT
	leaq	1(%r15,%rax), %r15
	movq	%r15, %rdi
	call	malloc@PLT
	movq	%r15, %rdx
	movq	%r12, %rsi
	movq	%rax, 0(%rbp)
	movq	%rax, %rdi
	call	__strcpy_chk@PLT
	movq	%rbx, %rsi
	movq	%rax, %rdi
	call	strcat@PLT
	movl	$16, %edi
	call	malloc@PLT
	movq	%rax, 8(%rbp)
	movq	%rax, %rbp
	jmp	.L56
.L60:
	popq	%rax
	.cfi_def_cfa_offset 56
	movq	%r13, %rdi
	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
	jmp	_Z15map_input_splitP13MapReduceSpec
	.cfi_endproc
.LFE2034:
	.size	_Z9add_inputPcP13MapReduceSpec, .-_Z9add_inputPcP13MapReduceSpec
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsi, %rbp
	movl	$32, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	pushq	%rax
	.cfi_def_cfa_offset 32
	call	malloc@PLT
	movq	8(%rbp), %rdi
	movq	%rax, %rbx
	movq	%rax, %rsi
	call	_Z9add_inputPcP13MapReduceSpec
	movq	%rbx, %rdi
	call	free@PLT
	popq	%rdx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2035:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2061:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2061:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct MapFileList {
	char* filename;
	struct MapFileList* next;
}MapFileList;

typedef struct MapInputList{
	char* key;
	char* value;
	struct MapInputList* next;
}MapInputList;

typedef enum InputFormat{TextInputFormat,KeyValueInputFormat,SequenceFileInputFormat} input_format;

typedef struct MapReduceSpec{
	MapFileList* map_file_list;
	MapInputList* map_input_list;
	unsigned map_block_num;
	unsigned map_thread_num;
	input_format map_input_format;
}MapReduceSpec;

void init_map_file_list(MapFileList* list){
	list->filename=NULL;
	list->next=NULL;
}

void free_map_file_list(MapFileList* list){
	MapFileList* del;
	MapFileList* tmp;
	del=list;
	tmp=list->next;
	while(tmp){
		if(del->filename!=NULL)
			free(del->filename);
		free(del);
		del=tmp;
		tmp=tmp->next;
	}
	if(del->filename!=NULL)
		free(del->filename);
	free(del);	
}

void init_map_input_list(MapInputList* list){
	list->key=NULL;
	list->value=NULL;
	list->next=NULL;
}

void free_map_input_list(MapInputList* list){
	MapInputList* del;
	MapInputList* tmp;
	del=list;
	tmp=list->next;
	while(tmp){
		if(del->key!=NULL)
			free(del->key);
		if(del->value!=NULL)
			free(del->value);
		free(del);
		del=tmp;
		tmp=tmp->next;
	}
	if(del->key!=NULL)
		free(del->key);
	if(del->value!=NULL)
		free(del->value);
	free(del);	
}

void init_mapreduce_spec(MapReduceSpec* spec){
	spec->map_file_list=NULL;
	spec->map_input_list=NULL;
	spec->map_block_num=4;
	spec->map_thread_num=128;
	spec->map_input_format=TextInputFormat;
}

void free_spec(MapReduceSpec* spec){
	free_map_file_list(spec->map_file_list);
	free_map_input_list(spec->map_input_list);
	free(spec);
}

void map_input_split(MapReduceSpec* spec){
	MapFileList* file_list_entry;
	size_t buffer_size=(size_t)256*1024*1024;
	size_t buffer_used=0;

	MapInputList* input_list_entry;

	FILE* pFile;
	file_list_entry=spec->map_file_list;
	
	input_list_entry=(MapInputList*)malloc(sizeof(MapInputList));
	init_map_input_list(input_list_entry);
	spec->map_input_list=input_list_entry;
	
	if(spec->map_input_format==TextInputFormat){
		size_t file_size;
		while(file_list_entry->filename!=NULL){
			pFile=fopen(file_list_entry->filename,"rb");
			if (pFile==NULL) {fputs ("File error\n",stderr); exit (1);}
			fseek (pFile , 0 , SEEK_END);
			file_size = ftell (pFile);
			rewind (pFile);
			if(buffer_used+file_size<=buffer_size){
				ssize_t result=0;
				size_t len = 0;
				while (result!= -1) {
					input_list_entry->key=(char*)malloc(10);
					sprintf(input_list_entry->key,"%d",(int)ftell(pFile));
					printf("%s ",input_list_entry->key);
					result=getline(&(input_list_entry->value), &len, pFile);
					printf("%s \n",input_list_entry->value);
					input_list_entry->next=(MapInputList*)malloc(sizeof(MapInputList));
					input_list_entry=input_list_entry->next;
					init_map_input_list(input_list_entry);
				}
				buffer_used=buffer_used+file_size;
			}
			else
				printf("Buffer full!!\n");
			file_list_entry=file_list_entry->next;
			fclose(pFile);
		}
	}


}

void add_input(char *path,MapReduceSpec* spec){
	MapFileList* plist;
	plist=(MapFileList*)malloc(sizeof(MapFileList));
	spec->map_file_list=plist;
	struct dirent* entry = NULL;
	DIR *pDir;
	pDir=opendir(path);
	while((entry=readdir(pDir))!=NULL){
		if(entry->d_type==DT_REG){
			plist->filename=(char*)malloc(strlen(path)+strlen(entry->d_name)+1);
			strcpy(plist->filename,path);
		       	strcat(plist->filename,entry->d_name);
			plist->next=(MapFileList*)malloc(sizeof(MapFileList));
			plist=plist->next;
		}
	}       
	map_input_split(spec);	
}

int main(int argc, char **argv){
	MapReduceSpec* spec=(MapReduceSpec*)malloc(sizeof(MapReduceSpec));
	add_input(argv[1],spec);
	free(spec);
}

	.text
	.file	"test1.hip"
	.globl	_Z18init_map_file_listP11MapFileList # -- Begin function _Z18init_map_file_listP11MapFileList
	.type	_Z18init_map_file_listP11MapFileList,@function
_Z18init_map_file_listP11MapFileList:   # @_Z18init_map_file_listP11MapFileList
	.cfi_startproc
# %bb.0:
	xorps	%xmm0, %xmm0
	movups	%xmm0, (%rdi)
	retq
.Lfunc_end0:
	.size	_Z18init_map_file_listP11MapFileList, .Lfunc_end0-_Z18init_map_file_listP11MapFileList
	.cfi_endproc
                                        # -- End function
	.globl	_Z18free_map_file_listP11MapFileList # -- Begin function _Z18free_map_file_listP11MapFileList
	.type	_Z18free_map_file_listP11MapFileList,@function
_Z18free_map_file_listP11MapFileList:   # @_Z18free_map_file_listP11MapFileList
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rdi, %r14
	movq	8(%rdi), %rax
	testq	%rax, %rax
	je	.LBB1_1
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%rax, %rbx
	movq	(%r14), %rdi
	testq	%rdi, %rdi
	je	.LBB1_4
# %bb.3:                                #   in Loop: Header=BB1_2 Depth=1
	callq	free
.LBB1_4:                                #   in Loop: Header=BB1_2 Depth=1
	movq	%r14, %rdi
	callq	free
	movq	8(%rbx), %rax
	movq	%rbx, %r14
	testq	%rax, %rax
	jne	.LBB1_2
	jmp	.LBB1_5
.LBB1_1:
	movq	%r14, %rbx
.LBB1_5:                                # %._crit_edge
	movq	(%rbx), %rdi
	testq	%rdi, %rdi
	je	.LBB1_7
# %bb.6:
	callq	free
.LBB1_7:
	movq	%rbx, %rdi
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	free                            # TAILCALL
.Lfunc_end1:
	.size	_Z18free_map_file_listP11MapFileList, .Lfunc_end1-_Z18free_map_file_listP11MapFileList
	.cfi_endproc
                                        # -- End function
	.globl	_Z19init_map_input_listP12MapInputList # -- Begin function _Z19init_map_input_listP12MapInputList
	.type	_Z19init_map_input_listP12MapInputList,@function
_Z19init_map_input_listP12MapInputList: # @_Z19init_map_input_listP12MapInputList
	.cfi_startproc
# %bb.0:
	xorps	%xmm0, %xmm0
	movups	%xmm0, (%rdi)
	movq	$0, 16(%rdi)
	retq
.Lfunc_end2:
	.size	_Z19init_map_input_listP12MapInputList, .Lfunc_end2-_Z19init_map_input_listP12MapInputList
	.cfi_endproc
                                        # -- End function
	.globl	_Z19free_map_input_listP12MapInputList # -- Begin function _Z19free_map_input_listP12MapInputList
	.type	_Z19free_map_input_listP12MapInputList,@function
_Z19free_map_input_listP12MapInputList: # @_Z19free_map_input_listP12MapInputList
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rdi, %r14
	movq	16(%rdi), %rax
	testq	%rax, %rax
	je	.LBB3_1
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%rax, %rbx
	movq	(%r14), %rdi
	testq	%rdi, %rdi
	je	.LBB3_4
# %bb.3:                                #   in Loop: Header=BB3_2 Depth=1
	callq	free
.LBB3_4:                                #   in Loop: Header=BB3_2 Depth=1
	movq	8(%r14), %rdi
	testq	%rdi, %rdi
	je	.LBB3_6
# %bb.5:                                #   in Loop: Header=BB3_2 Depth=1
	callq	free
.LBB3_6:                                #   in Loop: Header=BB3_2 Depth=1
	movq	%r14, %rdi
	callq	free
	movq	16(%rbx), %rax
	movq	%rbx, %r14
	testq	%rax, %rax
	jne	.LBB3_2
	jmp	.LBB3_7
.LBB3_1:
	movq	%r14, %rbx
.LBB3_7:                                # %._crit_edge
	movq	(%rbx), %rdi
	testq	%rdi, %rdi
	je	.LBB3_9
# %bb.8:
	callq	free
.LBB3_9:
	movq	8(%rbx), %rdi
	testq	%rdi, %rdi
	je	.LBB3_11
# %bb.10:
	callq	free
.LBB3_11:
	movq	%rbx, %rdi
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	free                            # TAILCALL
.Lfunc_end3:
	.size	_Z19free_map_input_listP12MapInputList, .Lfunc_end3-_Z19free_map_input_listP12MapInputList
	.cfi_endproc
                                        # -- End function
	.globl	_Z19init_mapreduce_specP13MapReduceSpec # -- Begin function _Z19init_mapreduce_specP13MapReduceSpec
	.type	_Z19init_mapreduce_specP13MapReduceSpec,@function
_Z19init_mapreduce_specP13MapReduceSpec: # @_Z19init_mapreduce_specP13MapReduceSpec
	.cfi_startproc
# %bb.0:
	xorps	%xmm0, %xmm0
	movups	%xmm0, (%rdi)
	movabsq	$549755813892, %rax             # imm = 0x8000000004
	movq	%rax, 16(%rdi)
	movl	$0, 24(%rdi)
	retq
.Lfunc_end4:
	.size	_Z19init_mapreduce_specP13MapReduceSpec, .Lfunc_end4-_Z19init_mapreduce_specP13MapReduceSpec
	.cfi_endproc
                                        # -- End function
	.globl	_Z9free_specP13MapReduceSpec    # -- Begin function _Z9free_specP13MapReduceSpec
	.type	_Z9free_specP13MapReduceSpec,@function
_Z9free_specP13MapReduceSpec:           # @_Z9free_specP13MapReduceSpec
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	callq	_Z18free_map_file_listP11MapFileList
	movq	8(%rbx), %rdi
	callq	_Z19free_map_input_listP12MapInputList
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	free                            # TAILCALL
.Lfunc_end5:
	.size	_Z9free_specP13MapReduceSpec, .Lfunc_end5-_Z9free_specP13MapReduceSpec
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z15map_input_splitP13MapReduceSpec
.LCPI6_0:
	.zero	16
	.text
	.globl	_Z15map_input_splitP13MapReduceSpec
	.type	_Z15map_input_splitP13MapReduceSpec,@function
_Z15map_input_splitP13MapReduceSpec:    # @_Z15map_input_splitP13MapReduceSpec
	.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	$24, %rsp
	.cfi_def_cfa_offset 80
	.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, %rbx
	movq	(%rdi), %rbp
	movl	$1, %edi
	movl	$24, %esi
	callq	calloc@PLT
	movq	%rax, 8(%rbx)
	cmpl	$0, 24(%rbx)
	jne	.LBB6_10
# %bb.1:                                # %.preheader
	movq	(%rbp), %rdi
	testq	%rdi, %rdi
	je	.LBB6_10
# %bb.2:                                # %.lr.ph.preheader
	movq	%rax, %r14
	leaq	16(%rsp), %rbx
	xorl	%r13d, %r13d
.LBB6_3:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB6_6 Depth 2
	movl	$.L.str, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB6_11
# %bb.4:                                #   in Loop: Header=BB6_3 Depth=1
	movq	%rax, %r15
	movq	%rax, %rdi
	xorl	%esi, %esi
	movl	$2, %edx
	callq	fseek
	movq	%r15, %rdi
	callq	ftell
	movq	%rax, %r12
	movq	%r15, %rdi
	callq	rewind
	addq	%r13, %r12
	cmpq	$268435456, %r12                # imm = 0x10000000
	ja	.LBB6_8
# %bb.5:                                #   in Loop: Header=BB6_3 Depth=1
	movq	%rbp, 8(%rsp)                   # 8-byte Spill
	movq	$0, 16(%rsp)
.LBB6_6:                                #   Parent Loop BB6_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%r14, %rbp
	movl	$10, %edi
	callq	malloc
	movq	%rax, %r14
	movq	%rax, (%rbp)
	movq	%r15, %rdi
	callq	ftell
	movl	$.L.str.2, %esi
	movq	%r14, %rdi
	movl	%eax, %edx
	xorl	%eax, %eax
	callq	sprintf
	movq	(%rbp), %rsi
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	leaq	8(%rbp), %rdi
	movq	%rbx, %rsi
	movq	%r15, %rdx
	callq	getline
	movq	%rax, %r13
	movq	8(%rbp), %rsi
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$24, %edi
	callq	malloc
	movq	%rax, %r14
	movq	%rax, 16(%rbp)
	xorps	%xmm0, %xmm0
	movups	%xmm0, (%rax)
	movq	$0, 16(%rax)
	cmpq	$-1, %r13
	jne	.LBB6_6
# %bb.7:                                #   in Loop: Header=BB6_3 Depth=1
	movq	%r12, %r13
	movq	8(%rsp), %rbp                   # 8-byte Reload
	jmp	.LBB6_9
.LBB6_8:                                #   in Loop: Header=BB6_3 Depth=1
	movl	$.Lstr, %edi
	callq	puts@PLT
.LBB6_9:                                #   in Loop: Header=BB6_3 Depth=1
	movq	8(%rbp), %rbp
	movq	%r15, %rdi
	callq	fclose
	movq	(%rbp), %rdi
	testq	%rdi, %rdi
	jne	.LBB6_3
.LBB6_10:                               # %.loopexit
	addq	$24, %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
.LBB6_11:
	.cfi_def_cfa_offset 80
	movq	stderr(%rip), %rsi
	movl	$.L.str.1, %edi
	callq	fputs
	movl	$1, %edi
	callq	exit
.Lfunc_end6:
	.size	_Z15map_input_splitP13MapReduceSpec, .Lfunc_end6-_Z15map_input_splitP13MapReduceSpec
	.cfi_endproc
                                        # -- End function
	.globl	_Z9add_inputPcP13MapReduceSpec  # -- Begin function _Z9add_inputPcP13MapReduceSpec
	.type	_Z9add_inputPcP13MapReduceSpec,@function
_Z9add_inputPcP13MapReduceSpec:         # @_Z9add_inputPcP13MapReduceSpec
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movq	%rdi, %r14
	movl	$16, %edi
	callq	malloc
	movq	%rax, %r15
	movq	%rax, (%rbx)
	movq	%r14, %rdi
	callq	opendir
	movq	%rax, %r12
	movq	%rax, %rdi
	callq	readdir
	testq	%rax, %rax
	je	.LBB7_5
# %bb.1:                                # %.lr.ph.preheader
	movq	%rax, %r13
.LBB7_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	cmpb	$8, 18(%r13)
	jne	.LBB7_4
# %bb.3:                                #   in Loop: Header=BB7_2 Depth=1
	movq	%r14, %rdi
	callq	strlen
	movq	%rax, %rbp
	addq	$19, %r13
	movq	%r13, %rdi
	callq	strlen
	leaq	(%rax,%rbp), %rdi
	incq	%rdi
	callq	malloc
	movq	%rax, (%r15)
	movq	%rax, %rdi
	movq	%r14, %rsi
	callq	strcpy
	movq	(%r15), %rdi
	movq	%r13, %rsi
	callq	strcat
	movl	$16, %edi
	callq	malloc
	movq	%rax, 8(%r15)
	movq	%rax, %r15
.LBB7_4:                                #   in Loop: Header=BB7_2 Depth=1
	movq	%r12, %rdi
	callq	readdir
	movq	%rax, %r13
	testq	%rax, %rax
	jne	.LBB7_2
.LBB7_5:                                # %._crit_edge
	movq	%rbx, %rdi
	addq	$8, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	_Z15map_input_splitP13MapReduceSpec # TAILCALL
.Lfunc_end7:
	.size	_Z9add_inputPcP13MapReduceSpec, .Lfunc_end7-_Z9add_inputPcP13MapReduceSpec
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rsi, %rbx
	movl	$32, %edi
	callq	malloc
	movq	%rax, %r14
	movq	8(%rbx), %rdi
	movq	%rax, %rsi
	callq	_Z9add_inputPcP13MapReduceSpec
	movq	%r14, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end8:
	.size	main, .Lfunc_end8-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"rb"
	.size	.L.str, 3

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"File error\n"
	.size	.L.str.1, 12

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

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

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

	.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	"Buffer full!!"
	.size	.Lstr, 14

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
