// File: /workspace/github_c_makefile_star2_O3/Astor-Bizard[P]ProjetS5_Groupe2/src/lecture_ELF.c
// Function: main
int main(int argc, char *argv[]) {
	Elf32_Ehdr elfHeaders;
	SectionsHeadersList shList;
	ListeSymboles symbolsTable;
	Str_Reloc relocationTable;
	char* fileName;
	char* sectionToDisplay = NULL;
	FILE* f;
	int fileNamePos, i;
	uint16_t options = 0;

	if(argc < 2) {
		print_usage();
		return 1;
	}

	if(argv[1][0]=='-') {
		fileNamePos = argc-1;
		i = 1;
	}
	else {
		fileNamePos = 1;
		i = 2;
	}

	fileName = argv[fileNamePos];
	f = fopen(fileName, "r");
	if (f == NULL) {
		printf("Fichier introuvable: %s\n", fileName);
		return 1;
	}

	while (i < argc) 
	{
		if (argv[i][0] == '-') 
		{
			switch(argv[i][1]) 
			{
				case 'a':
					options = options | OPTION_ALL;
					break;
				case 'h':
					options = options | OPTION_FILE_HEADER;
					break;
				case 'S':
					options = options | OPTION_SECTION_HEADERS;
					break;
				case 'e':
					options = options | OPTION_HEADERS;
					break;
				case 's':
					options = options | OPTION_SYMS;
					break;
				case 'r':
					options = options | OPTION_RELOCS;
					break;
				case 'x':
					options = options | OPTION_SECTION_DISPLAY;
					if (i+1<argc && i+1!=fileNamePos && argv[i+1][0] != '-')
						sectionToDisplay = argv[++i];
					break;
				case 'H':
					print_usage();
					return 0;
					break;
				default:
					printf("Unrecognized option: -%c\n\n", argv[i][1]);
					print_usage();
					return 1;
					break;
			}
		}
		else 
		{
			if (i != fileNamePos) 
			{
				printf("Unrecognized option: %s\n\n", argv[i]);
				print_usage();
				return 1;
			}
		}
		i++;
	}
	if (!options)
		options = OPTION_ALL;

	elfHeaders = lecture_Headers(f, (!(options & OPTION_FILE_HEADER)));

	shList = readSectionsHeadersFromFile(f, elfHeaders, (!(options & OPTION_SECTION_HEADERS)));

	if(options & OPTION_SECTION_DISPLAY) 
	{
		if(sectionToDisplay == NULL)
			afficher_section(f, shList, 0, NULL);
		else 
			afficher_section(f, shList, 0, sectionToDisplay);
	}

	symbolsTable = lectureSymbolTab(f, elfHeaders, shList, (!(options & OPTION_SYMS)));

	relocationTable = affichage_relocation(f, elfHeaders, shList, symbolsTable, (!(options & OPTION_RELOCS)));

	fclose(f);
	free_Str_Reloc(relocationTable);
	free_SectionsHeadersList(shList);
	free_ListeSymboles(symbolsTable);
	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
cmpl $0x1, %edi
jg 0x1347
callq 0x129c
movl $0x1, %ebp
jmp 0x1655
movq %rsi, %r15
movl %edi, %r12d
movq 0x8(%rsi), %rax
movb (%rax), %bl
leal -0x1(%r12), %r13d
cmpb $0x2d, %bl
movl $0x1, %ebp
cmovnel %ebp, %r13d
movq (%rsi,%r13,8), %r14
leaq 0x2d86(%rip), %rsi      # 0x40f5
movq %r14, %rdi
callq 0x1100
testq %rax, %rax
je 0x1454
movq %rax, %r8
xorl %eax, %eax
cmpb $0x2d, %bl
setne %al
incl %eax
cmpl %r12d, %eax
jae 0x146a
xorl %ecx, %ecx
xorl %r14d, %r14d
movslq %eax, %rdx
movq (%r15,%rdx,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x13cc
movzbl 0x1(%rsi), %edx
cmpl $0x67, %edx
jle 0x13e4
cmpl $0x72, %edx
jg 0x13fc
cmpl $0x68, %edx
je 0x144a
cmpl $0x72, %edx
jne 0x1675
orl $0x8, %ecx
jmp 0x13d8
cmpl %r13d, %eax
movl %r13d, %eax
jne 0x1501
incl %eax
cmpl %r12d, %eax
jl 0x139b
jmp 0x1474
cmpl $0x60, %edx
jle 0x1436
cmpl $0x61, %edx
je 0x1444
cmpl $0x65, %edx
jne 0x1675
orl $0x3, %ecx
jmp 0x13d8
cmpl $0x73, %edx
je 0x144f
cmpl $0x78, %edx
jne 0x1675
orl $0x10, %ecx
leal 0x1(%rax), %edx
cmpl %r12d, %edx
setge %sil
cmpl %r13d, %edx
sete %dil
orb %sil, %dil
jne 0x13d8
movslq %edx, %rsi
movq (%r15,%rsi,8), %rsi
cmpb $0x2d, (%rsi)
je 0x13d8
movq %rsi, %r14
movl %edx, %eax
jmp 0x13d8
cmpl $0x53, %edx
jne 0x1669
orl $0x2, %ecx
jmp 0x13d8
movw $0x1f, %cx
jmp 0x13d8
orl $0x1, %ecx
jmp 0x13d8
orl $0x4, %ecx
jmp 0x13d8
leaq 0x2ba9(%rip), %rdi      # 0x4004
movq %r14, %rsi
xorl %eax, %eax
callq 0x1090
jmp 0x1655
movl $0x1f, %ebp
xorl %r14d, %r14d
jmp 0x1482
testw %cx, %cx
movl $0x1f, %eax
cmovnel %ecx, %eax
movzwl %ax, %ebp
movl %ebp, %edx
notl %edx
andl $0x1, %edx
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r8, %rbx
movq %r8, %rsi
callq 0x16ea
xorl %edx, %edx
testb $0x2, %bpl
sete %dl
movl 0x30(%r15), %eax
movl %eax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x2079
testb $0x10, %bpl
je 0x1533
testq %r14, %r14
je 0x1514
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
jmp 0x152e
leaq 0x2b30(%rip), %rdi      # 0x4038
xorl %eax, %eax
callq 0x1090
jmp 0x1338
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2529
xorl %edx, %edx
testb $0x4, %bpl
sete %dl
movq 0x78(%rsp), %rax
movq %rax, 0x48(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, 0x38(%rsp)
movl 0xb0(%rsp), %eax
movl %eax, 0x30(%rsp)
movups 0x80(%rsp), %xmm0
movups 0x90(%rsp), %xmm1
movups 0xa0(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x27b0
xorl %edx, %edx
testb $0x8, %bpl
sete %dl
movq 0x10(%r14), %rax
movq %rax, 0x60(%rsp)
movups (%r14), %xmm0
movups %xmm0, 0x50(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x48(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, 0x38(%rsp)
movl 0xb0(%rsp), %eax
movl %eax, 0x30(%rsp)
movups 0x80(%rsp), %xmm0
movups 0x90(%rsp), %xmm1
movups 0xa0(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0xb8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3282
movq %rbx, %rdi
callq 0x1070
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
callq 0x33cc
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x33a0
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
callq 0x33b6
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x48, %edx
jne 0x1675
callq 0x129c
jmp 0x1653
movsbl %dl, %esi
leaq 0x299e(%rip), %rdi      # 0x401d
xorl %eax, %eax
callq 0x1090
jmp 0x1338
nop