// File: /workspace/github_c_makefile_star2_O0/koendv[P]stm32duino-raspberrypi/Arduino_Tools/src/stm32flash/stm32.c
// Function: stm32_read_memory
stm32_err_t stm32_read_memory(const stm32_t *stm, uint32_t address,
			      uint8_t data[], unsigned int len)
{
	struct port_interface *port = stm->port;
	uint8_t buf[5];

	if (!len)
		return STM32_ERR_OK;

	if (len > 256) {
		fprintf(stderr, "Error: READ length limit at 256 bytes\n");
		return STM32_ERR_UNKNOWN;
	}

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

	if (stm32_send_command(stm, stm->cmd->rm) != 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;

	if (stm32_send_command(stm, len - 1) != STM32_ERR_OK)
		return STM32_ERR_UNKNOWN;

	if (port->read(port, data, len) != PORT_ERR_OK)
		return STM32_ERR_UNKNOWN;

	return STM32_ERR_OK;
}

// 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)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x7454
movl $0x0, -0x4(%rbp)
jmp 0x75af
cmpl $0x100, -0x24(%rbp)     # imm = 0x100
jbe 0x7481
movq 0x8b8c(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x4a37(%rip), %rsi      # 0xbea5
movb $0x0, %al
callq 0x2160
movl $0x1, -0x4(%rbp)
jmp 0x75af
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x3(%rax), %eax
cmpl $0xff, %eax
jne 0x74b8
movq 0x8b55(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x4a27(%rip), %rsi      # 0xbecc
movb $0x0, %al
callq 0x2160
movl $0x3, -0x4(%rbp)
jmp 0x75af
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x3(%rax), %esi
callq 0x7190
cmpl $0x0, %eax
je 0x74de
movl $0x1, -0x4(%rbp)
jmp 0x75af
movl -0x14(%rbp), %eax
shrl $0x18, %eax
movb %al, -0x35(%rbp)
movl -0x14(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x34(%rbp)
movl -0x14(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, -0x33(%rbp)
movl -0x14(%rbp), %eax
andl $0xff, %eax
movb %al, -0x32(%rbp)
movzbl -0x35(%rbp), %eax
movzbl -0x34(%rbp), %ecx
xorl %ecx, %eax
movzbl -0x33(%rbp), %ecx
xorl %ecx, %eax
movzbl -0x32(%rbp), %ecx
xorl %ecx, %eax
movb %al, -0x31(%rbp)
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x30(%rbp), %rdi
leaq -0x35(%rbp), %rsi
movl $0x5, %edx
callq *%rax
cmpl $0x0, %eax
je 0x754c
movl $0x1, -0x4(%rbp)
jmp 0x75af
movq -0x10(%rbp), %rdi
callq 0x71f0
cmpl $0x0, %eax
je 0x7563
movl $0x1, -0x4(%rbp)
jmp 0x75af
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %eax
subl $0x1, %eax
movzbl %al, %esi
callq 0x7190
cmpl $0x0, %eax
je 0x7583
movl $0x1, -0x4(%rbp)
jmp 0x75af
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %ecx
movl %ecx, %edx
callq *%rax
cmpl $0x0, %eax
je 0x75a8
movl $0x1, -0x4(%rbp)
jmp 0x75af
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)