// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq %rdi, %r14
leaq 0x10(%rsp), %rdi
movq $0x0, (%rdi)
callq 0x9d8c
testq %rax, %rax
je 0xa1bc
movq %rax, %rbx
movq 0x10(%rsp), %rax
cmpq $0xf, %rax
jbe 0xa1c7
cmpb $0x7f, (%rbx)
jne 0xa1c0
cmpb $0x45, 0x1(%rbx)
jne 0xa1c0
cmpb $0x4c, 0x2(%rbx)
jne 0xa1c0
cmpb $0x46, 0x3(%rbx)
jne 0xa1c0
movb 0x4(%rbx), %cl
cmpb $0x1, %cl
jne 0xa1ce
cmpb $0x1, 0x5(%rbx)
jne 0xa1d8
movzbl 0x6(%rbx), %edx
cmpl $0x1, %edx
jne 0xa1df
cmpq $0x33, %rax
jbe 0xa201
leaq 0x18(%rbx), %rdx
xorl %ecx, %ecx
xorl %esi, %esi
movzbl (%rdx), %edi
shll %cl, %edi
orl %edi, %esi
addq $0x8, %rcx
incq %rdx
cmpq $0x20, %rcx
jne 0x9fb9
movl %esi, (%r14)
leaq 0x20(%rbx), %rsi
xorl %ecx, %ecx
xorl %edx, %edx
movzbl (%rsi), %edi
shll %cl, %edi
orl %edi, %edx
addq $0x8, %rcx
incq %rsi
cmpq $0x20, %rcx
jne 0x9fd8
testl %edx, %edx
je 0xa208
movzbl 0x2e(%rbx), %ecx
movzbl 0x2f(%rbx), %edi
shll $0x8, %edi
movzwl 0x30(%rbx), %esi
movq %rdi, 0x8(%rsp)
addl %ecx, %edi
movl %esi, %r8d
imull %edi, %r8d
movl %edx, %edx
addq %rdx, %r8
cmpq %rax, %r8
ja 0xa20f
cmpl $0x27, %edi
jbe 0xa216
movl $0x1, %ebp
testl %esi, %esi
je 0xa21d
movl %edi, %eax
movq %rax, 0x40(%rsp)
movl %esi, %eax
movq %rax, 0x38(%rsp)
leaq (%rbx,%rdx), %r8
addq $0x4, %r8
leaq (%rbx,%rdx), %r9
addq $0x8, %r9
movq 0x8(%rsp), %rax
addl %ecx, %eax
movq %rax, 0x8(%rsp)
leaq (%rbx,%rdx), %r10
addq $0xc, %r10
leaq (%rbx,%rdx), %r14
addq $0x10, %r14
movq %rbx, (%rsp)
leaq (%rbx,%rdx), %rbp
addq $0x14, %rbp
xorl %edx, %edx
movq %r8, %rax
xorl %ecx, %ecx
xorl %ebx, %ebx
movzbl (%rax), %esi
shll %cl, %esi
orl %esi, %ebx
addq $0x8, %rcx
incq %rax
cmpq $0x20, %rcx
jne 0xa084
xorl %ecx, %ecx
movq %r9, %rdi
xorl %eax, %eax
movzbl (%rdi), %esi
shll %cl, %esi
orl %esi, %eax
addq $0x8, %rcx
incq %rdi
cmpq $0x20, %rcx
jne 0xa09f
xorl %ecx, %ecx
movq %r10, %rdi
xorl %r15d, %r15d
movzbl (%rdi), %esi
shll %cl, %esi
orl %esi, %r15d
addq $0x8, %rcx
incq %rdi
cmpq $0x20, %rcx
jne 0xa0bb
xorl %ecx, %ecx
movq %r14, %rdi
xorl %r13d, %r13d
movzbl (%rdi), %esi
shll %cl, %esi
orl %esi, %r13d
addq $0x8, %rcx
incq %rdi
cmpq $0x20, %rcx
jne 0xa0d8
xorl %ecx, %ecx
movq %rbp, %rdi
xorl %r12d, %r12d
movzbl (%rdi), %esi
shll %cl, %esi
orl %esi, %r12d
addq $0x8, %rcx
incq %rdi
cmpq $0x20, %rcx
jne 0xa0f5
testb $0x2, %al
je 0xa18d
movl %r12d, %eax
negl %eax
jae 0xa11e
cmpl %eax, %r15d
ja 0xa236
movl %r15d, %edi
movl %r12d, %esi
movq %rdx, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %r9, 0x20(%rsp)
movq %r10, 0x18(%rsp)
callq 0x6caf
movq 0x18(%rsp), %r10
movq 0x20(%rsp), %r9
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %rdx
cmpl $0x8, %ebx
je 0xa18d
movl %r13d, %edi
movl %r12d, %eax
addq %rdi, %rax
cmpq 0x10(%rsp), %rax
ja 0xa249
addq (%rsp), %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x6b71
movq 0x18(%rsp), %r10
movq 0x20(%rsp), %r9
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %rdx
incq %rdx
addq 0x40(%rsp), %r8
movq 0x8(%rsp), %rax
addq %rax, %r9
addq %rax, %r10
addq %rax, %r14
addq %rax, %rbp
cmpq 0x38(%rsp), %rdx
jne 0xa07d
movq (%rsp), %rbx
movl $0x1, %ebp
jmp 0xa21d
xorl %ebp, %ebp
jmp 0xa225
callq 0x18c9
jmp 0xa21b
callq 0x19da
jmp 0xa21b
movzbl %cl, %edi
callq 0x18eb
jmp 0xa21b
callq 0x1930
jmp 0xa21b
movq 0x8e12(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x3e97(%rip), %rsi      # 0xe087
xorl %eax, %eax
callq 0x10d0
movq 0x10(%rsp), %rax
jmp 0x9fa7
callq 0x19b8
jmp 0xa21b
callq 0x1996
jmp 0xa1b5
callq 0x1974
jmp 0xa21b
callq 0x1952
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x1030
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8dbb(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x3f00(%rip), %rsi      # 0xe147
jmp 0xa25a
movq 0x8da8(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x3f12(%rip), %rsi      # 0xe16c
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x10d0
movq (%rsp), %rbx
jmp 0xa21d
nopl (%rax)