// File: /workspace/github_c_makefile_star2_O1/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 0x1647
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 0x1448
movq %rax, %r8
xorl %eax, %eax
cmpb $0x2d, %bl
setne %al
incl %eax
cmpl %r12d, %eax
jge 0x145e
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 0x13e1
cmpl $0x72, %edx
jg 0x13f9
cmpl $0x68, %edx
je 0x143e
cmpl $0x72, %edx
jne 0x1667
orl $0x8, %ecx
jmp 0x13d5
cmpl %r13d, %eax
jne 0x14f3
incl %eax
cmpl %r12d, %eax
jl 0x139b
jmp 0x1463
cmpl $0x60, %edx
jle 0x142a
cmpl $0x61, %edx
je 0x1438
cmpl $0x65, %edx
jne 0x1667
orl $0x3, %ecx
jmp 0x13d5
cmpl $0x73, %edx
je 0x1443
cmpl $0x78, %edx
jne 0x1667
orl $0x10, %ecx
leal 0x1(%rax), %edx
cmpl %r12d, %edx
jge 0x13d5
cmpl %r13d, %edx
je 0x13d5
movslq %edx, %rsi
movq (%r15,%rsi,8), %rsi
cmpb $0x2d, (%rsi)
je 0x13d5
movq %rsi, %r14
movl %edx, %eax
jmp 0x13d5
cmpl $0x53, %edx
jne 0x165b
orl $0x2, %ecx
jmp 0x13d5
movw $0x1f, %cx
jmp 0x13d5
orl $0x1, %ecx
jmp 0x13d5
orl $0x4, %ecx
jmp 0x13d5
leaq 0x2bb5(%rip), %rdi      # 0x4004
movq %r14, %rsi
xorl %eax, %eax
callq 0x1090
jmp 0x1647
xorl %r14d, %r14d
xorl %ecx, %ecx
testw %cx, %cx
movl $0x1f, %r12d
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 0x16de
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 0x207b
testb $0x10, %r12b
je 0x1525
testq %r14, %r14
je 0x1506
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 0x1520
leaq 0x2b3e(%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 0x2524
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 0x2798
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 0x328d
movq %rbx, %rdi
callq 0x1070
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
callq 0x33e6
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x33a8
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
callq 0x33c7
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 0x1667
callq 0x129c
jmp 0x1645
movsbl %dl, %esi
leaq 0x29ac(%rip), %rdi      # 0x401d
xorl %eax, %eax
callq 0x1090
jmp 0x1338
nopl (%rax)