// File: /workspace/github_c_makefile_star2_O2/mikecat[P]x86_interpreter/read_elf.c
// Function: read_elf
int read_elf(uint32_t* eip_value, const char* filename) {
	size_t filesize = 0;
	uint8_t* filedata = read_whole_file(&filesize, filename);
	uint32_t sh_offset, sh_ent_size, sh_num, sh_size;
	uint8_t* sheader;
	uint32_t i;
	if (filedata == 0) return 0;
	if (filesize < 16) {
		fprintf(stderr, "file size too small for ELF ident\n");
		free(filedata); return 0;
	}
	if (filedata[0] != 0x7f || filedata[1] != 'E' || filedata[2] != 'L' || filedata[3] != 'F') {
		fprintf(stderr, "not ELF file\n");
		free(filedata); return 0;
	}
	if (filedata[4] != 1) {
		fprintf(stderr, "only 32-bit ELF file is supported, but this is %s\n",
			filedata[4] == 0 ? "invalid" : filedata[4] == 2 ? "64-bit" : "unknown");
		free(filedata); return 0;
	}
	if (filedata[5] != 1) {
		fprintf(stderr, "only little endian ELF file is supported\n");
		free(filedata); return 0;
	}
	if (filedata[6] != 1) {
		fprintf(stderr, "warning: unknown ELF version %"PRIu8"\n", filedata[6]);
	}
	if (filesize < 52) {
		fprintf(stderr, "file size too small for ELF header\n");
		free(filedata); return 0;
	}
	*eip_value = read_num(filedata + 24, 4);
	sh_offset = read_num(filedata + 32, 4);
	sh_ent_size = read_num(filedata + 46, 2);
	sh_num = read_num(filedata + 48, 2);
	sh_size = (uint32_t)sh_ent_size * (uint32_t)sh_num;
	if (sh_offset == 0) {
		fprintf(stderr, "warning: no section header table in ELF\n");
		free(filedata); return 1;
	}
	if ((uint64_t)sh_offset + sh_size > filesize) {
		fprintf(stderr, "ELF section header table is out of file\n");
		free(filedata); return 0;
	}
	if (sh_ent_size < 40) {
		fprintf(stderr, "ELF section header entry size too small\n");
		free(filedata); return 0;
	}
	sheader = filedata + sh_offset;
	for (i = 0; i < sh_num; i++) {
		uint8_t* ent = sheader + sh_ent_size * i;
		uint32_t type = read_num(ent + 4, 4);
		uint32_t flags = read_num(ent + 8, 4);
		uint32_t addr = read_num(ent + 12, 4);
		uint32_t offset = read_num(ent + 16, 4);
		uint32_t size = read_num(ent + 20, 4);
		if (flags & 2) { /* メモリにロードする */
			if (size > 0 && UINT32_MAX - addr < size - 1) {
				fprintf(stderr, "ELF section %"PRIu32" out of address space\n", i);
				free(filedata); return 0;
			}
			dmemory_allocate(addr, size);
			if (type != 8) { /* SHT_NOBITSでない */
				if ((uint64_t)offset + size > filesize) {
					fprintf(stderr, "ELF section %"PRIu32" data is out of file\n", i);
					free(filedata); return 0;
				}
				dmemory_write(filedata + offset, addr, size);
			}
		}
	}
	free(filedata);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
leaq 0x10(%rsp), %rdi
andq $0x0, (%rdi)
callq 0x8bbc
xorl %ebx, %ebx
testq %rax, %rax
je 0x9039
movq %rax, %r13
movq 0x10(%rsp), %r15
cmpq $0xf, %r15
jbe 0x8f89
cmpb $0x7f, (%r13)
jne 0x8f71
cmpb $0x45, 0x1(%r13)
jne 0x8f71
cmpb $0x4c, 0x2(%r13)
jne 0x8f71
cmpb $0x46, 0x3(%r13)
jne 0x8f71
movb 0x4(%r13), %al
cmpb $0x1, %al
jne 0x8fa1
cmpb $0x1, 0x5(%r13)
jne 0x8fde
movzbl 0x6(%r13), %edx
cmpl $0x1, %edx
jne 0x8ff3
cmpq $0x33, %r15
jbe 0x9015
leaq 0x18(%r13), %rdi
pushq $0x4
popq %rbx
movl %ebx, %esi
callq 0x90e5
movl %eax, (%r14)
leaq 0x20(%r13), %rdi
movl %ebx, %esi
callq 0x90e5
movl %eax, %ebx
leaq 0x2e(%r13), %rdi
pushq $0x2
popq %r14
movl %r14d, %esi
callq 0x90e5
movl %eax, %r12d
leaq 0x30(%r13), %rdi
movl %r14d, %esi
callq 0x90e5
testl %ebx, %ebx
je 0x904a
movq %r13, 0x8(%rsp)
movl %eax, %ecx
imull %r12d, %ecx
movl %ebx, %ebp
addq %rbp, %rcx
cmpq %r15, %rcx
ja 0x906e
cmpl $0x27, %r12d
jbe 0x9081
addq 0x8(%rsp), %rbp
movl %eax, %eax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
xorl %r14d, %r14d
movl %r12d, 0x1c(%rsp)
pushq $0x4
popq %r15
cmpq %r14, 0x28(%rsp)
je 0x8f5f
movq %rax, 0x30(%rsp)
movl %eax, %ebx
leaq (%rbx,%rbp), %rdi
addq $0x4, %rdi
movl %r15d, %esi
callq 0x90e5
movl %eax, 0x24(%rsp)
leaq (%rbx,%rbp), %rdi
addq $0x8, %rdi
movl %r15d, %esi
callq 0x90e5
movl %eax, %r13d
leaq (%rbx,%rbp), %rdi
addq $0xc, %rdi
movl %r15d, %esi
callq 0x90e5
movl %eax, %r12d
leaq (%rbx,%rbp), %rdi
addq $0x10, %rdi
movl %r15d, %esi
callq 0x90e5
movl %eax, 0x20(%rsp)
leaq (%rbx,%rbp), %rdi
addq $0x14, %rdi
movl %r15d, %esi
callq 0x90e5
testb $0x2, %r13b
je 0x8f4a
movl %eax, %ebx
negl %eax
setb %cl
cmpl %eax, %r12d
seta %al
andb %cl, %al
cmpb $0x1, %al
je 0x90ab
movl %r12d, %edi
movl %ebx, %esi
callq 0x5e70
cmpl $0x8, 0x24(%rsp)
je 0x8f4a
movl 0x20(%rsp), %edi
movl %ebx, %eax
addq %rdi, %rax
cmpq 0x10(%rsp), %rax
ja 0x90be
addq 0x8(%rsp), %rdi
movl %r12d, %esi
movl %ebx, %edx
callq 0x5d3e
incq %r14
movl 0x1c(%rsp), %r12d
movq 0x30(%rsp), %rax
addl %r12d, %eax
jmp 0x8e89
movq 0x8(%rsp), %rdi
callq 0x1030
pushq $0x1
popq %rbx
jmp 0x9039
movq 0x9080(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x4013(%rip), %rdi      # 0xcf95
pushq $0xd
jmp 0x9028
movq 0x9068(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3fd8(%rip), %rdi      # 0xcf72
pushq $0x22
jmp 0x9028
movq 0x9050(%rip), %rcx      # 0x11ff8
movq (%rcx), %rdi
cmpb $0x2, %al
leaq 0x402a(%rip), %rcx      # 0xcfde
leaq 0x402a(%rip), %rsi      # 0xcfe5
cmoveq %rcx, %rsi
testb %al, %al
leaq 0x400e(%rip), %rdx      # 0xcfd6
cmovneq %rsi, %rdx
leaq 0x3fd0(%rip), %rsi      # 0xcfa3
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x10d0
jmp 0x9031
movq 0x9013(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x3ffe(%rip), %rdi      # 0xcfed
pushq $0x29
jmp 0x9028
movq 0x8ffe(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x4013(%rip), %rsi      # 0xd017
xorl %eax, %eax
callq 0x10d0
movq 0x10(%rsp), %r15
jmp 0x8dfd
movq 0x8fdc(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x4012(%rip), %rdi      # 0xd038
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
movq %r13, %rdi
callq 0x1030
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8fa7(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x4001(%rip), %rdi      # 0xd05c
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
movq %r13, %rdi
jmp 0x8f64
movq 0x8f83(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x4006(%rip), %rdi      # 0xd085
jmp 0x9092
movq 0x8f70(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x401c(%rip), %rdi      # 0xd0ae
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
movq 0x8(%rsp), %rdi
callq 0x1030
xorl %ebx, %ebx
jmp 0x9039
movq 0x8f46(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x401b(%rip), %rsi      # 0xd0d7
jmp 0x90cf
movq 0x8f33(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x402d(%rip), %rsi      # 0xd0fc
xorl %ebx, %ebx
movl %r14d, %edx
xorl %eax, %eax
callq 0x10d0
movq 0x8(%rsp), %rdi
jmp 0x9034