// File: /workspace/github_c_makefile_star2_O1/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 0x69c9
movq %rdi, %r15
movq 0x18(%rdi), %rax
movb 0xb(%rax), %al
cmpb $-0x1, %al
je 0x69d5
movq %rcx, %rbx
movl %edx, %r12d
movl %esi, %r13d
movq 0x8(%r15), %r14
movzbl %al, %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x6b78
movl $0x1, %ebp
testl %eax, %eax
jne 0x69df
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)
movl $0x5, %edx
movq %r14, %rdi
callq *0x30(%r14)
testl %eax, %eax
jne 0x69df
movq %r15, %rdi
xorl %esi, %esi
callq 0x602b
testl %eax, %eax
jne 0x69df
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)
movl $0x5, %edx
movq %r14, %rdi
callq *0x30(%r14)
testl %eax, %eax
jne 0x69df
movq %r15, %rdi
xorl %esi, %esi
callq 0x602b
testl %eax, %eax
jne 0x69df
movq %r15, %rdi
xorl %esi, %esi
callq 0x602b
testl %eax, %eax
jne 0x69df
leaq 0x3(%rsp), %rsi
movl $0x5, %edx
movq %r14, %rdi
callq *0x28(%r14)
testl %eax, %eax
jne 0x69df
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 0x69df
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 0x69df
callq 0x2f0d
movl $0x1, %ebp
jmp 0x69df
callq 0x2f29
movl $0x3, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq