// File: /workspace/github_c_makefile_star2_O2/koendv[P]stm32duino-raspberrypi/Arduino_Tools/src/stm32flash/stm32.c
// Function: stm32_crc_memory
stm32_err_t stm32_crc_memory(const stm32_t *stm, uint32_t address,
			     uint32_t length, uint32_t *crc)
{
	struct port_interface *port = stm->port;
	uint8_t buf[5];

	if ((address & 0x3) || (length & 0x3)) {
		fprintf(stderr, "Start and end addresses must be 4 byte aligned\n");
		return STM32_ERR_UNKNOWN;
	}

	if (stm->cmd->crc == STM32_CMD_ERR) {
		fprintf(stderr, "Error: CRC command not implemented in bootloader.\n");
		return STM32_ERR_NO_CMD;
	}

	if (stm32_send_command(stm, stm->cmd->crc) != STM32_ERR_OK)
		return STM32_ERR_UNKNOWN;

	buf[0] = address >> 24;
	buf[1] = (address >> 16) & 0xFF;
	buf[2] = (address >> 8) & 0xFF;
	buf[3] = address & 0xFF;
	buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3];
	if (port->write(port, buf, 5) != PORT_ERR_OK)
		return STM32_ERR_UNKNOWN;

	if (stm32_get_ack(stm) != STM32_ERR_OK)
		return STM32_ERR_UNKNOWN;

	buf[0] = length >> 24;
	buf[1] = (length >> 16) & 0xFF;
	buf[2] = (length >> 8) & 0xFF;
	buf[3] = length & 0xFF;
	buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3];
	if (port->write(port, buf, 5) != PORT_ERR_OK)
		return STM32_ERR_UNKNOWN;

	if (stm32_get_ack(stm) != STM32_ERR_OK)
		return STM32_ERR_UNKNOWN;

	if (stm32_get_ack(stm) != STM32_ERR_OK)
		return STM32_ERR_UNKNOWN;

	if (port->read(port, buf, 5) != PORT_ERR_OK)
		return STM32_ERR_UNKNOWN;

	if (buf[4] != (buf[0] ^ buf[1] ^ buf[2] ^ buf[3]))
		return STM32_ERR_UNKNOWN;

	*crc = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
	return STM32_ERR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %eax
orl %esi, %eax
testb $0x3, %al
jne 0x5ffa
movq %rdi, %r14
movq 0x18(%rdi), %rax
movb 0xb(%rax), %al
cmpb $-0x1, %al
je 0x601a
movq %rcx, %rbx
movl %edx, %r12d
movl %esi, %r13d
movq 0x8(%r14), %r15
movzbl %al, %esi
movq %r14, %rdi
callq 0x5132
pushq $0x1
popq %rbp
testl %eax, %eax
jne 0x6039
movl %r13d, %eax
shrl $0x18, %eax
leaq 0x3(%rsp), %rsi
movb %al, (%rsi)
movl %r13d, %ecx
shrl $0x10, %ecx
movb %cl, 0x1(%rsi)
movl %r13d, %edx
shrl $0x8, %edx
movb %dl, 0x2(%rsi)
movb %r13b, 0x3(%rsi)
xorb %r13b, %cl
xorb %dl, %al
xorb %cl, %al
movb %al, 0x4(%rsi)
pushq $0x5
popq %rdx
movq %r15, %rdi
callq *0x30(%r15)
testl %eax, %eax
jne 0x6039
movq %r14, %rdi
callq 0x5154
testl %eax, %eax
jne 0x6039
movl %r12d, %eax
shrl $0x18, %eax
leaq 0x3(%rsp), %rsi
movb %al, (%rsi)
movl %r12d, %ecx
shrl $0x10, %ecx
movb %cl, 0x1(%rsi)
movl %r12d, %edx
shrl $0x8, %edx
movb %dl, 0x2(%rsi)
movb %r12b, 0x3(%rsi)
xorb %r12b, %cl
xorb %dl, %al
xorb %cl, %al
movb %al, 0x4(%rsi)
pushq $0x5
popq %rdx
movq %r15, %rdi
callq *0x30(%r15)
testl %eax, %eax
jne 0x6039
movq %r14, %rdi
callq 0x5154
testl %eax, %eax
jne 0x6039
movq %r14, %rdi
callq 0x5154
testl %eax, %eax
jne 0x6039
leaq 0x3(%rsp), %rsi
pushq $0x5
popq %rdx
movq %r15, %rdi
callq *0x28(%r15)
testl %eax, %eax
jne 0x6039
movzbl 0x7(%rsp), %edi
movzbl 0x3(%rsp), %esi
movzbl 0x4(%rsp), %eax
movl %eax, %r8d
xorl %esi, %r8d
movzbl 0x5(%rsp), %ecx
movzbl 0x6(%rsp), %edx
movl %ecx, %r9d
xorl %edx, %r9d
xorl %r8d, %r9d
cmpl %edi, %r9d
jne 0x6039
shll $0x18, %esi
shll $0x10, %eax
orl %esi, %eax
shll $0x8, %ecx
orl %eax, %ecx
orl %edx, %ecx
movl %ecx, (%rbx)
xorl %ebp, %ebp
jmp 0x6039
movq 0x6fef(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x2e5e(%rip), %rdi      # 0x8e69
pushq $0x2f
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
pushq $0x1
jmp 0x6038
movq 0x6fcf(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x2e6e(%rip), %rdi      # 0x8e99
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
pushq $0x3
popq %rbp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq