// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0xa0(%rbp)
movw $0x0, -0xb2(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1314
callq 0x1230
movl $0x1, -0x4(%rbp)
jmp 0x186a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x133c
movl -0x8(%rbp), %eax
subl $0x1, %eax
movl %eax, -0xac(%rbp)
movl $0x1, -0xb0(%rbp)
jmp 0x1350
movl $0x1, -0xac(%rbp)
movl $0x2, -0xb0(%rbp)
movq -0x10(%rbp), %rax
movslq -0xac(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
leaq 0x3fa8(%rip), %rsi      # 0x531c
callq 0x10f0
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jne 0x13ab
movq -0x98(%rbp), %rsi
leaq 0x3f86(%rip), %rdi      # 0x531e
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x186a
jmp 0x13ad
movl -0xb0(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x15a0
movq -0x10(%rbp), %rax
movslq -0xb0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x154e
movq -0x10(%rbp), %rax
movslq -0xb0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %eax
addl $-0x48, %eax
movl %eax, %ecx
movq %rcx, -0x138(%rbp)
subl $0x30, %eax
ja 0x151a
movq -0x138(%rbp), %rax
leaq 0x3bf7(%rip), %rcx      # 0x5004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzwl -0xb2(%rbp), %eax
orl $0x1f, %eax
movw %ax, -0xb2(%rbp)
jmp 0x154c
movzwl -0xb2(%rbp), %eax
orl $0x1, %eax
movw %ax, -0xb2(%rbp)
jmp 0x154c
movzwl -0xb2(%rbp), %eax
orl $0x2, %eax
movw %ax, -0xb2(%rbp)
jmp 0x154c
movzwl -0xb2(%rbp), %eax
orl $0x3, %eax
movw %ax, -0xb2(%rbp)
jmp 0x154c
movzwl -0xb2(%rbp), %eax
orl $0x4, %eax
movw %ax, -0xb2(%rbp)
jmp 0x154c
movzwl -0xb2(%rbp), %eax
orl $0x8, %eax
movw %ax, -0xb2(%rbp)
jmp 0x154c
movzwl -0xb2(%rbp), %eax
orl $0x10, %eax
movw %ax, -0xb2(%rbp)
movl -0xb0(%rbp), %eax
addl $0x1, %eax
cmpl -0x8(%rbp), %eax
jge 0x1507
movl -0xb0(%rbp), %eax
addl $0x1, %eax
cmpl -0xac(%rbp), %eax
je 0x1507
movq -0x10(%rbp), %rax
movl -0xb0(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
je 0x1507
movq -0x10(%rbp), %rax
movl -0xb0(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0xb0(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa0(%rbp)
jmp 0x154c
callq 0x1230
movl $0x0, -0x4(%rbp)
jmp 0x186a
movq -0x10(%rbp), %rax
movslq -0xb0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %esi
leaq 0x3e03(%rip), %rdi      # 0x5337
movb $0x0, %al
callq 0x1070
callq 0x1230
movl $0x1, -0x4(%rbp)
jmp 0x186a
jmp 0x158c
movl -0xb0(%rbp), %eax
cmpl -0xac(%rbp), %eax
je 0x158a
movq -0x10(%rbp), %rax
movslq -0xb0(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x3de0(%rip), %rdi      # 0x5352
movb $0x0, %al
callq 0x1070
callq 0x1230
movl $0x1, -0x4(%rbp)
jmp 0x186a
jmp 0x158c
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0x13ad
cmpw $0x0, -0xb2(%rbp)
jne 0x15b3
movw $0x1f, -0xb2(%rbp)
movq -0xa8(%rbp), %rsi
movzwl -0xb2(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edx
leaq -0xe8(%rbp), %rdi
callq 0x1930
leaq -0x48(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
movl $0x34, %edx
callq 0x10b0
movq -0xa8(%rbp), %rsi
movzwl -0xb2(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edx
movl -0x18(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x30(%rax)
movups -0x48(%rbp), %xmm0
movups -0x38(%rbp), %xmm1
movups -0x28(%rbp), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x2940
movq -0x100(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x50(%rbp)
movzwl -0xb2(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x16db
cmpq $0x0, -0xa0(%rbp)
jne 0x16a7
movq -0xa8(%rbp), %rdi
leaq -0x60(%rbp), %rax
xorl %esi, %esi
xorl %ecx, %ecx
movl %ecx, %edx
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x30a0
jmp 0x16d9
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rdx
leaq -0x60(%rbp), %rax
xorl %esi, %esi
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x30a0
jmp 0x16db
movq -0xa8(%rbp), %rsi
movzwl -0xb2(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edx
movq -0x50(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x48(%rax)
movups -0x60(%rbp), %xmm0
movups %xmm0, 0x38(%rax)
movl -0x18(%rbp), %ecx
movl %ecx, 0x30(%rax)
movups -0x48(%rbp), %xmm0
movups -0x38(%rbp), %xmm1
movups -0x28(%rbp), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x118(%rbp), %rdi
callq 0x34b0
movq -0x118(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0xa8(%rbp), %rsi
movzwl -0xb2(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edx
movq -0x68(%rbp), %rcx
movq %rsp, %rax
movq %rax, -0x140(%rbp)
movq %rcx, 0x60(%rax)
movups -0x78(%rbp), %xmm0
movups %xmm0, 0x50(%rax)
movq -0x50(%rbp), %rcx
movq %rcx, 0x48(%rax)
movups -0x60(%rbp), %xmm0
movups %xmm0, 0x38(%rax)
movl -0x18(%rbp), %ecx
movl %ecx, 0x30(%rax)
movups -0x48(%rbp), %xmm0
movups -0x38(%rbp), %xmm1
movups -0x28(%rbp), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x130(%rbp), %rdi
callq 0x46b0
movq -0x130(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x1050
leaq -0x90(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x48a0
leaq -0x60(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x4820
leaq -0x78(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x4860
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
nopw %cs:(%rax,%rax)