// File: /workspace/github_c_makefile_star2_O1/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 0x8(%rsp), %rdi
movq $0x0, (%rdi)
callq 0xa868
testq %rax, %rax
je 0xac89
movq %rax, %rbx
cmpq $0xf, 0x8(%rsp)
jbe 0xac94
cmpb $0x7f, (%rbx)
jne 0xac8d
cmpb $0x45, 0x1(%rbx)
jne 0xac8d
cmpb $0x4c, 0x2(%rbx)
jne 0xac8d
cmpb $0x46, 0x3(%rbx)
jne 0xac8d
movb 0x4(%rbx), %al
cmpb $0x1, %al
jne 0xac9b
cmpb $0x1, 0x5(%rbx)
jne 0xaca5
movzbl 0x6(%rbx), %edx
cmpl $0x1, %edx
jne 0xacac
movq 0x8(%rsp), %rax
cmpq $0x33, %rax
jbe 0xacc9
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 0xaa7a
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 0xaa99
leaq 0x2e(%rbx), %rsi
xorl %ecx, %ecx
xorl %r13d, %r13d
movzbl (%rsi), %edi
shll %cl, %edi
orl %edi, %r13d
addq $0x8, %rcx
incq %rsi
cmpq $0x10, %rcx
jne 0xaab6
leaq 0x30(%rbx), %rdi
xorl %ecx, %ecx
xorl %esi, %esi
movzbl (%rdi), %r8d
shll %cl, %r8d
orl %r8d, %esi
addq $0x8, %rcx
incq %rdi
cmpq $0x10, %rcx
jne 0xaad3
testl %edx, %edx
je 0xacd0
movl %esi, %edi
imull %r13d, %edi
movl %edx, %ecx
addq %rcx, %rdi
cmpq %rax, %rdi
ja 0xacd7
cmpl $0x27, %r13d
jbe 0xacde
movl $0x1, %ebp
testl %esi, %esi
je 0xace5
movl %esi, %eax
movq %rax, 0x18(%rsp)
leaq (%rbx,%rcx), %rax
addq $0x4, %rax
movq %rax, 0x40(%rsp)
leaq 0x8(%rbx,%rcx), %rax
movq %rax, 0x38(%rsp)
leaq 0xc(%rbx,%rcx), %rax
movq %rax, 0x30(%rsp)
leaq 0x10(%rbx,%rcx), %rax
movq %rax, 0x28(%rsp)
movq %rbx, (%rsp)
leaq (%rbx,%rcx), %rax
addq $0x14, %rax
movq %rax, 0x20(%rsp)
xorl %ebp, %ebp
xorl %edx, %edx
movl %ebp, %esi
movq 0x20(%rsp), %rax
addq %rsi, %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rsi), %r10
movq 0x30(%rsp), %rcx
leaq (%rcx,%rsi), %rdi
movq 0x38(%rsp), %rcx
leaq (%rcx,%rsi), %r8
addq 0x40(%rsp), %rsi
xorl %ecx, %ecx
xorl %ebx, %ebx
movzbl (%rsi), %r9d
shll %cl, %r9d
orl %r9d, %ebx
addq $0x8, %rcx
incq %rsi
cmpq $0x20, %rcx
jne 0xab92
xorl %ecx, %ecx
xorl %esi, %esi
movzbl (%r8), %r9d
shll %cl, %r9d
orl %r9d, %esi
addq $0x8, %rcx
incq %r8
cmpq $0x20, %rcx
jne 0xabad
xorl %ecx, %ecx
xorl %r15d, %r15d
movzbl (%rdi), %r8d
shll %cl, %r8d
orl %r8d, %r15d
addq $0x8, %rcx
incq %rdi
cmpq $0x20, %rcx
jne 0xabc9
xorl %ecx, %ecx
xorl %r14d, %r14d
movzbl (%r10), %edi
shll %cl, %edi
orl %edi, %r14d
addq $0x8, %rcx
incq %r10
cmpq $0x20, %rcx
jne 0xabe5
xorl %ecx, %ecx
xorl %r12d, %r12d
movzbl (%rax), %edi
shll %cl, %edi
orl %edi, %r12d
addq $0x8, %rcx
incq %rax
cmpq $0x20, %rcx
jne 0xac00
testb $0x2, %sil
je 0xac6d
movl %r12d, %eax
negl %eax
jae 0xac2b
cmpl %eax, %r15d
ja 0xacfe
movl %r15d, %edi
movl %r12d, %esi
movq %rdx, 0x10(%rsp)
callq 0x6edb
movq 0x10(%rsp), %rdx
cmpl $0x8, %ebx
je 0xac6d
movl %r14d, %edi
movl %r12d, %eax
addq %rdi, %rax
cmpq 0x8(%rsp), %rax
ja 0xad11
addq (%rsp), %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x6d8a
movq 0x10(%rsp), %rdx
incq %rdx
addl %r13d, %ebp
cmpq 0x18(%rsp), %rdx
jne 0xab64
movq (%rsp), %rbx
movl $0x1, %ebp
jmp 0xace5
xorl %ebp, %ebp
jmp 0xaced
callq 0x18c9
jmp 0xace3
callq 0x19da
jmp 0xace3
movzbl %al, %edi
callq 0x18eb
jmp 0xace3
callq 0x1930
jmp 0xace3
movq 0x8345(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x33d2(%rip), %rsi      # 0xe08f
xorl %eax, %eax
callq 0x10d0
jmp 0xaa63
callq 0x19b8
jmp 0xace3
callq 0x1996
jmp 0xac82
callq 0x1974
jmp 0xace3
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 0x82f3(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x3440(%rip), %rsi      # 0xe14f
jmp 0xad22
movq 0x82e0(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x3452(%rip), %rsi      # 0xe174
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x10d0
movq (%rsp), %rbx
jmp 0xace5
nopl (%rax)