// File: /workspace/github_c_makefile_star2_O0/evilwombat[P]gopro-fw-tools/h4-section-patch.c
// Function: parse_firmware
int parse_firmware(unsigned char *buf, int size, struct section_info *output, unsigned int max_sections)
{
	unsigned int section_offset, num = 0;
	unsigned int global_header_crc, global_actual_crc;
	int length;
	int offset = 0;
	
	if (size < GLOBAL_HEADER_SIZE) {
		printf("Invalid firmware size: %d\n", size);
		return -1;
	}
	
	global_header_crc = read_word_le(buf, 0);
	global_actual_crc = get_global_crc(buf, size);
	
	printf("Global header CRC: %08x\n", global_header_crc);
	printf("Global actual CRC: %08x (%s)\n", global_actual_crc,
		(global_header_crc == global_actual_crc) ? "OK" : "MISMATCH!");
	
	if (global_header_crc != global_actual_crc) {
		printf("DANGER!!! Firmware global CRC does not match the CRC listed in the header!\n");
		printf("This is a bad thing. This firmware looks invalid.\n");
		return -1;
	}
	
	while (1) {
		if (num >= max_sections) {
			printf("Firmware contains more than %d sections. Something must be wrong.\n", max_sections);
			return -1;
		}
		
		offset = find_magic(buf, size, offset);
		if (offset < 0) {
			return num;
		}
	
		offset -= 28;
		output[num].header_crc = read_word_le(buf, offset);
		offset += 4;

		output[num].version = read_word_le(buf, offset);
		offset += 4;

		output[num].build_date = read_word_le(buf, offset);
		offset += 4;
		
		length = read_word_le(buf, offset);
		offset += 4;
		offset += 4;

		output[num].flags = read_word_le(buf, offset);
		offset += 4;

		output[num].magic = read_word_le(buf, offset);
		offset += 4;

		offset += 0x100 - 28;

		section_offset = offset;

		output[num].length = length;
		output[num].offset = section_offset;
		
		if (length < 0)
			continue;

		output[num].actual_crc = crc32(buf + section_offset, length);

		if (output[num].header_crc != output[num].actual_crc) {
			printf("WARNING!!! CRC MISMATCH WHILE PARSING SECTION %d\n", num);
			printf("Header CRC = %08x, Actual CRC = %08x\n",
			       output[num].header_crc, output[num].actual_crc);
			return -1;
		}
		
		offset += length;
		
		num++;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0xe0, -0x14(%rbp)
jge 0x152a
movl -0x14(%rbp), %esi
leaq 0x1c0c(%rip), %rdi      # 0x3123
movb $0x0, %al
callq 0x1060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17da
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x17f0
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x1480
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %esi
leaq 0x1bed(%rip), %rdi      # 0x313e
movb $0x0, %al
callq 0x1060
movl -0x34(%rbp), %esi
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %edi
leaq 0x1bb1(%rip), %rdx      # 0x3119
leaq 0x1ba7(%rip), %rax      # 0x3116
cmpl %edi, %ecx
cmoveq %rax, %rdx
leaq 0x1bdb(%rip), %rdi      # 0x3157
movb $0x0, %al
callq 0x1060
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
je 0x15b3
leaq 0x1be3(%rip), %rdi      # 0x3175
movb $0x0, %al
callq 0x1060
leaq 0x1c21(%rip), %rdi      # 0x31c1
movb $0x0, %al
callq 0x1060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17da
jmp 0x15b5
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jb 0x15da
movl -0x24(%rbp), %esi
leaq 0x1c2d(%rip), %rdi      # 0x31f4
movb $0x0, %al
callq 0x1060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17da
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x3c(%rbp), %edx
callq 0x1860
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x15fd
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x17da
movl -0x3c(%rbp), %eax
subl $0x1c, %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x17f0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x3c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x17f0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x3c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x17f0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movl -0x3c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x17f0
movl %eax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x17f0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
movl -0x3c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x17f0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movl -0x3c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
addl $0xe4, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x38(%rbp), %ecx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x1c(%rax)
movl -0x28(%rbp), %ecx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
cmpl $0x0, -0x38(%rbp)
jge 0x1732
jmp 0x15b5
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
addq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x2190
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x20(%rbp), %rcx
movl -0x2c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
je 0x17c3
movl -0x2c(%rbp), %esi
leaq 0x1ab3(%rip), %rdi      # 0x3237
movb $0x0, %al
callq 0x1060
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %esi
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edx
leaq 0x1ab6(%rip), %rdi      # 0x3269
movb $0x0, %al
callq 0x1060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17da
movl -0x38(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x15b5
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)