// File: /workspace/github_c_makefile_star2_O0/rsa9000[P]regfanalysistools/regfwalk.c
// Function: regf_parse_fheader
static int regf_parse_fheader(FILE *fp)
{
	uint8_t buf[0x200];
	const struct regf_fhdr *fhdr = (void *)buf;
	const uint32_t *p32;
	uint32_t cs;
	int i, res;

	if (fseek(fp, 0, SEEK_SET)) {
		fprintf(stderr, "%s: could not seek to header position: %s\n",
			__func__, strerror(errno));
		return -1;
	}

	res = fread(buf, 1, sizeof(buf), fp);
	if (res != sizeof(buf)) {
		fprintf(stderr, "%s: could not read file header page: %s\n",
			__func__, strerror(errno));
		return -1;
	}

	if (strncmp(fhdr->sign, "regf", 4) != 0) {
		fprintf(stderr, "%s: unexpected signature '%.4s' expect 'regf'\n",
			__func__, fhdr->sign);
		return -1;
	}

	cs = 0;
	for (i = 0, p32 = (void *)buf; i < 0x200/sizeof(uint32_t); ++i, ++p32)
		cs ^= *p32;

	if (cs) {
		fprintf(stderr, "%s: header checksum verification failed\n",
			__func__);
		return -1;
	}

	printf("fhdr.seqno{pri,sec} = 0x%08X, 0x%08X\n", fhdr->seqno_pri, fhdr->seqno_sec);
	printf("fhdr.timestamp = 0x%016"PRIX64"\n", fhdr->timestamp);
	printf("fhdr.logfile = %s\n", fhdr->is_log ? "true" : "false");

	if (fhdr->seqno_pri != fhdr->seqno_sec) {
		fprintf(stderr, "%s: primary/secondary seqno missmatch!\n",
			__func__);
		return -1;
	}

	st.version = fhdr->ver_maj << 4 | fhdr->ver_min;
	st.root_off = fhdr->root_off;
	st.data_sz = fhdr->data_sz;

	printf("fhdr.version = %u.%u\n", fhdr->ver_maj, fhdr->ver_min);

	printf("fhdr.root_off = 0x%08X\n", fhdr->root_off);
	printf("fhdr.data_sz = 0x%08X\n", fhdr->data_sz);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x10(%rbp)
leaq -0x210(%rbp), %rax
movq %rax, -0x218(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1120
cmpl $0x0, %eax
je 0x1589
movq 0x7ab0(%rip), %rax      # 0x8ff8
movq (%rax), %rax
movq %rax, -0x238(%rbp)
callq 0x1030
movl (%rax), %edi
callq 0x1170
movq -0x238(%rbp), %rdi
movq %rax, %rcx
leaq 0x4c0b(%rip), %rsi      # 0x617a
leaq 0x4c2f(%rip), %rdx      # 0x61a5
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x182b
leaq -0x210(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1060
movl %eax, -0x22c(%rbp)
movslq -0x22c(%rbp), %rax
cmpq $0x200, %rax            # imm = 0x200
je 0x1600
movq 0x7a39(%rip), %rax      # 0x8ff8
movq (%rax), %rax
movq %rax, -0x240(%rbp)
callq 0x1030
movl (%rax), %edi
callq 0x1170
movq -0x240(%rbp), %rdi
movq %rax, %rcx
leaq 0x4bd2(%rip), %rsi      # 0x61b8
leaq 0x4bb8(%rip), %rdx      # 0x61a5
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x182b
movq -0x218(%rbp), %rdi
leaq 0x4bd3(%rip), %rsi      # 0x61e1
movl $0x4, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x164f
movq 0x79d4(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
movq -0x218(%rbp), %rcx
leaq 0x4bb1(%rip), %rsi      # 0x61e6
leaq 0x4b69(%rip), %rdx      # 0x61a5
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x182b
movl $0x0, -0x224(%rbp)
movl $0x0, -0x228(%rbp)
leaq -0x210(%rbp), %rax
movq %rax, -0x220(%rbp)
movslq -0x228(%rbp), %rax
cmpq $0x80, %rax
jae 0x16b8
movq -0x220(%rbp), %rax
movl (%rax), %eax
xorl -0x224(%rbp), %eax
movl %eax, -0x224(%rbp)
movl -0x228(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x228(%rbp)
movq -0x220(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x220(%rbp)
jmp 0x1671
cmpl $0x0, -0x224(%rbp)
je 0x16ec
movq 0x7930(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x4b43(%rip), %rsi      # 0x6215
leaq 0x4acc(%rip), %rdx      # 0x61a5
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x182b
movq -0x218(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x218(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x4b37(%rip), %rdi      # 0x623e
movb $0x0, %al
callq 0x10a0
movq -0x218(%rbp), %rax
movq 0x10(%rax), %rsi
leaq 0x4b44(%rip), %rdi      # 0x6264
movb $0x0, %al
callq 0x10a0
movq -0x218(%rbp), %rax
movl 0x20(%rax), %ecx
leaq 0x4b5f(%rip), %rsi      # 0x6297
leaq 0x4b53(%rip), %rax      # 0x6292
cmpl $0x0, %ecx
cmovneq %rax, %rsi
leaq 0x4b32(%rip), %rdi      # 0x627f
movb $0x0, %al
callq 0x10a0
movq -0x218(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x218(%rbp), %rcx
cmpl 0x8(%rcx), %eax
je 0x1795
movq 0x7887(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x4b22(%rip), %rsi      # 0x629d
leaq 0x4a23(%rip), %rdx      # 0x61a5
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x182b
movq -0x218(%rbp), %rax
movl 0x18(%rax), %eax
shll $0x4, %eax
movq -0x218(%rbp), %rcx
orl 0x1c(%rcx), %eax
movl %eax, 0x7956(%rip)      # 0x9108
movq -0x218(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, 0x794a(%rip)      # 0x910c
movq -0x218(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, 0x793e(%rip)      # 0x9110
movq -0x218(%rbp), %rax
movl 0x18(%rax), %esi
movq -0x218(%rbp), %rax
movl 0x1c(%rax), %edx
leaq 0x4ad8(%rip), %rdi      # 0x62c5
movb $0x0, %al
callq 0x10a0
movq -0x218(%rbp), %rax
movl 0x28(%rax), %esi
leaq 0x4ad6(%rip), %rdi      # 0x62db
movb $0x0, %al
callq 0x10a0
movq -0x218(%rbp), %rax
movl 0x2c(%rax), %esi
leaq 0x4ad6(%rip), %rdi      # 0x62f3
movb $0x0, %al
callq 0x10a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nopw (%rax,%rax)