// File: /workspace/github_c_makefile_star2_O2/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 0x12f5
callq 0x124c
pushq $0x1
popq %rbp
jmp 0x15d6
movq %rsi, %r15
movl %edi, %r12d
movq 0x8(%rsi), %rax
movb (%rax), %bl
leal -0x1(%r12), %r13d
cmpb $0x2d, %bl
pushq $0x1
popq %rbp
cmovnel %ebp, %r13d
movq (%rsi,%r13,8), %r14
leaq 0x2dda(%rip), %rsi      # 0x40f5
movq %r14, %rdi
callq 0x1100
testq %rax, %rax
je 0x1467
movq %rax, %r8
xorl %eax, %eax
cmpb $0x2d, %bl
setne %al
incl %eax
xorl %r14d, %r14d
xorl %ecx, %ecx
cmpl %r12d, %eax
jge 0x13d9
movslq %eax, %rdx
movq (%r15,%rdx,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x13aa
movzbl 0x1(%rsi), %edx
cmpl $0x53, %edx
je 0x13ca
cmpl $0x61, %edx
je 0x13bf
cmpl $0x65, %edx
je 0x13d4
cmpl $0x68, %edx
je 0x13ba
cmpl $0x72, %edx
je 0x13cf
cmpl $0x73, %edx
je 0x13c5
cmpl $0x78, %edx
jne 0x15ea
orl $0x10, %ecx
leal 0x1(%rax), %edx
cmpl %r12d, %edx
setge %sil
cmpl %r13d, %edx
sete %dil
orb %sil, %dil
jne 0x13b6
movslq %edx, %rsi
movq (%r15,%rsi,8), %rsi
cmpb $0x2d, (%rsi)
je 0x13b6
movq %rsi, %r14
movl %edx, %eax
jmp 0x13b6
cmpl %r13d, %eax
movl %r13d, %eax
jne 0x147d
incl %eax
jmp 0x133e
orl $0x1, %ecx
jmp 0x13b6
movw $0x1f, %cx
jmp 0x13b6
orl $0x4, %ecx
jmp 0x13b6
orl $0x2, %ecx
jmp 0x13b6
orl $0x8, %ecx
jmp 0x13b6
orl $0x3, %ecx
jmp 0x13b6
testw %cx, %cx
pushq $0x1f
popq %r12
cmovnel %ecx, %r12d
movzwl %r12w, %ebp
movl %ebp, %edx
notl %edx
andl $0x1, %edx
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r8, %rbx
movq %r8, %rsi
callq 0x166a
xorl %edx, %edx
testb $0x2, %r12b
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 0x1fa5
testb $0x10, %r12b
je 0x14b4
testq %r14, %r14
je 0x1495
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 0x14af
leaq 0x2b96(%rip), %rdi      # 0x4004
movq %r14, %rsi
xorl %eax, %eax
callq 0x1090
jmp 0x15d6
leaq 0x2bb4(%rip), %rdi      # 0x4038
xorl %eax, %eax
callq 0x1090
callq 0x124c
jmp 0x15d6
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 0x2418
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 0x26b0
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 0x30b6
movq %rbx, %rdi
callq 0x1070
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
callq 0x31ec
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x31c0
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
callq 0x31d6
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 0x15f6
callq 0x124c
jmp 0x15d4
movsbl %dl, %esi
leaq 0x2a1d(%rip), %rdi      # 0x401d
xorl %eax, %eax
callq 0x1090
jmp 0x148b