// File: /workspace/github_c_makefile_star2_O0/ZombiesYard[P]MPU6050-DMP-raspberry-pi/MotionSensor/inv_mpu_lib/inv_mpu.c
// Function: mpu_load_firmware(unsigned short, unsigned char const*, unsigned short, unsigned short)
uint8_t mpu_load_firmware(uint16_t length, const uint8_t *firmware,
													uint16_t start_addr, uint16_t sample_rate)
{
	uint16_t ii;
	uint16_t this_write;
	uint8_t *pgm_buf;
	uint8_t jj;
	/* Must divide evenly into st.hw->bank_size to avoid bank crossings. */
#define LOAD_CHUNK  (16)
	uint8_t cur[LOAD_CHUNK], tmp[2];
	pgm_buf = (uint8_t *)malloc(LOAD_CHUNK);

	if (st.chip_cfg.dmp_loaded)
		/* DMP should only be loaded once. */
		return 1;

	if (!firmware)
		return 1;
	for (ii = 0; ii < length; ii += this_write)
	{
		this_write = min(LOAD_CHUNK, length - ii);		
		for (jj = 0; jj < LOAD_CHUNK; jj++) pgm_buf[jj] = firmware[ii+jj];//pgm_read_byte(firmware + ii + jj);
		if (mpu_write_mem(ii, this_write, pgm_buf))
			return 1;
		if (mpu_read_mem(ii, this_write, cur))
			return 1;
		if (memcmp(pgm_buf, cur, this_write))
			return 2;
	}

	/* Set program start address. */
	tmp[0] = start_addr >> 8;
	tmp[1] = start_addr & 0xFF;
	if (i2c_write(st.hw->addr, st.reg->prgm_start_h, 2, tmp))
		return 1;

	st.chip_cfg.dmp_loaded = 1;
	st.chip_cfg.dmp_sample_rate = sample_rate;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movw %cx, %ax
movw %dx, %cx
movw %di, %dx
movw %dx, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movw %cx, -0x12(%rbp)
movw %ax, -0x14(%rbp)
movl $0x10, %edi
callq 0x10c0
movq %rax, -0x20(%rbp)
cmpb $0x0, 0x73df(%rip)      # 0xc245
je 0x4e71
movb $0x1, -0x1(%rbp)
jmp 0x4fd9
cmpq $0x0, -0x10(%rbp)
jne 0x4e81
movb $0x1, -0x1(%rbp)
jmp 0x4fd9
movw $0x0, -0x16(%rbp)
movzwl -0x16(%rbp), %eax
movzwl -0x4(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4f79
movzwl -0x4(%rbp), %ecx
movzwl -0x16(%rbp), %eax
subl %eax, %ecx
movl $0x10, %eax
cmpl %ecx, %eax
jge 0x4eb4
movl $0x10, %eax
movl %eax, -0x48(%rbp)
jmp 0x4ec1
movzwl -0x4(%rbp), %eax
movzwl -0x16(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movw %ax, -0x18(%rbp)
movb $0x0, -0x21(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0x10, %eax
jge 0x4efe
movq -0x10(%rbp), %rax
movzwl -0x16(%rbp), %ecx
movzbl -0x21(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x20(%rbp), %rax
movzbl -0x21(%rbp), %ecx
movb %dl, (%rax,%rcx)
movb -0x21(%rbp), %al
addb $0x1, %al
movb %al, -0x21(%rbp)
jmp 0x4ecc
movw -0x16(%rbp), %cx
movw -0x18(%rbp), %ax
movq -0x20(%rbp), %rdx
movzwl %cx, %edi
movzwl %ax, %esi
callq 0x4c40
cmpb $0x0, %al
je 0x4f22
movb $0x1, -0x1(%rbp)
jmp 0x4fd9
movw -0x16(%rbp), %cx
movw -0x18(%rbp), %ax
leaq -0x40(%rbp), %rdx
movzwl %cx, %edi
movzwl %ax, %esi
callq 0x4d30
cmpb $0x0, %al
je 0x4f46
movb $0x1, -0x1(%rbp)
jmp 0x4fd9
movq -0x20(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movzwl -0x18(%rbp), %eax
movl %eax, %edx
callq 0x1070
cmpl $0x0, %eax
je 0x4f64
movb $0x2, -0x1(%rbp)
jmp 0x4fd9
jmp 0x4f66
movzwl -0x18(%rbp), %ecx
movzwl -0x16(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x16(%rbp)
jmp 0x4e87
movzwl -0x12(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x42(%rbp)
movzwl -0x12(%rbp), %eax
andl $0xff, %eax
movb %al, -0x41(%rbp)
movq 0x7282(%rip), %rax      # 0xc218
movb (%rax), %sil
movq 0x7270(%rip), %rax      # 0xc210
movb 0x1d(%rax), %al
leaq -0x42(%rbp), %rcx
movl $0x2, %edx
movzbl %sil, %edi
movzbl %al, %esi
callq 0x7360
cmpl $0x0, %eax
je 0x4fc3
movb $0x1, -0x1(%rbp)
jmp 0x4fd9
movb $0x1, 0x727b(%rip)      # 0xc245
movw -0x14(%rbp), %ax
movw %ax, 0x7271(%rip)       # 0xc246
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)