// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0xc7a0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xca2f
movl $0x0, -0x4(%rbp)
jmp 0xce5d
cmpq $0x10, -0x20(%rbp)
jae 0xca63
movq 0x85b3(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x5657(%rip), %rsi      # 0x1209e
movb $0x0, %al
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xce5d
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x7f, %eax
jne 0xca96
movq -0x28(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x45, %eax
jne 0xca96
movq -0x28(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x4c, %eax
jne 0xca96
movq -0x28(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x46, %eax
je 0xcac3
movq 0x8553(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x561a(%rip), %rsi      # 0x120c1
movb $0x0, %al
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xce5d
movq -0x28(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x1, %eax
je 0xcb44
movq 0x8519(%rip), %rax      # 0x14ff0
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
jne 0xcaf8
leaq 0x5610(%rip), %rax      # 0x12102
movq %rax, -0x78(%rbp)
jmp 0xcb19
movq -0x28(%rbp), %rax
movzbl 0x4(%rax), %edx
leaq 0x560a(%rip), %rax      # 0x12111
leaq 0x55fc(%rip), %rcx      # 0x1210a
cmpl $0x2, %edx
cmoveq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rdx
leaq 0x55a7(%rip), %rsi      # 0x120cf
movb $0x0, %al
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xce5d
movq -0x28(%rbp), %rax
movzbl 0x5(%rax), %eax
cmpl $0x1, %eax
je 0xcb7e
movq 0x8498(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x55b7(%rip), %rsi      # 0x12119
movb $0x0, %al
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xce5d
movq -0x28(%rbp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x1, %eax
je 0xcbab
movq 0x845e(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movq -0x28(%rbp), %rax
movzbl 0x6(%rax), %edx
leaq 0x559f(%rip), %rsi      # 0x12143
movb $0x0, %al
callq 0x10d0
cmpq $0x34, -0x20(%rbp)
jae 0xcbdf
movq 0x8437(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x55a1(%rip), %rsi      # 0x12164
movb $0x0, %al
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xce5d
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
movl $0x4, %esi
callq 0xce70
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rdi
addq $0x20, %rdi
movl $0x4, %esi
callq 0xce70
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rdi
addq $0x2e, %rdi
movl $0x2, %esi
callq 0xce70
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
addq $0x30, %rdi
movl $0x2, %esi
callq 0xce70
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
imull -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0xcc75
movq 0x83a1(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x552f(%rip), %rsi      # 0x12188
movb $0x0, %al
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x1, -0x4(%rbp)
jmp 0xce5d
movl -0x2c(%rbp), %eax
movl -0x38(%rbp), %ecx
addq %rcx, %rax
cmpq -0x20(%rbp), %rax
jbe 0xccb1
movq 0x8365(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x551c(%rip), %rsi      # 0x121b1
movb $0x0, %al
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xce5d
cmpl $0x28, -0x30(%rbp)
jae 0xcce4
movq 0x8332(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x5512(%rip), %rsi      # 0x121da
movb $0x0, %al
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xce5d
movq -0x28(%rbp), %rax
movl -0x2c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0xce4d
movq -0x40(%rbp), %rax
movl -0x30(%rbp), %ecx
imull -0x44(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
addq $0x4, %rdi
movl $0x4, %esi
callq 0xce70
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
movl $0x4, %esi
callq 0xce70
movl %eax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
addq $0xc, %rdi
movl $0x4, %esi
callq 0xce70
movl %eax, -0x5c(%rbp)
movq -0x50(%rbp), %rdi
addq $0x10, %rdi
movl $0x4, %esi
callq 0xce70
movl %eax, -0x60(%rbp)
movq -0x50(%rbp), %rdi
addq $0x14, %rdi
movl $0x4, %esi
callq 0xce70
movl %eax, -0x64(%rbp)
movl -0x58(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xce3d
cmpl $0x0, -0x64(%rbp)
jbe 0xcdd9
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl -0x5c(%rbp), %eax
movl -0x64(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0xcdd9
movq 0x8240(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movl -0x44(%rbp), %edx
leaq 0x5446(%rip), %rsi      # 0x12203
movb $0x0, %al
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xce5d
movl -0x5c(%rbp), %edi
movl -0x64(%rbp), %esi
callq 0x7c60
cmpl $0x8, -0x54(%rbp)
je 0xce3b
movl -0x60(%rbp), %eax
movl -0x64(%rbp), %ecx
addq %rcx, %rax
cmpq -0x20(%rbp), %rax
jbe 0xce26
movq 0x81f0(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
movl -0x44(%rbp), %edx
leaq 0x541b(%rip), %rsi      # 0x12228
movb $0x0, %al
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xce5d
movq -0x28(%rbp), %rdi
movl -0x60(%rbp), %eax
addq %rax, %rdi
movl -0x5c(%rbp), %esi
movl -0x64(%rbp), %edx
callq 0x7ad0
jmp 0xce3d
jmp 0xce3f
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xccf9
movq -0x28(%rbp), %rdi
callq 0x1030
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)