// File: /workspace/github_c_makefile_star2_O1/koendv[P]stm32duino-raspberrypi/Arduino_Tools/src/stm32flash/stm32.c
// Function: stm32_write_memory
stm32_err_t stm32_write_memory(const stm32_t *stm, uint32_t address,
			       const uint8_t data[], unsigned int len)
{
	struct port_interface *port = stm->port;
	uint8_t cs, buf[256 + 2];
	unsigned int i, aligned_len;
	stm32_err_t s_err;

	if (!len)
		return STM32_ERR_OK;

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

	/* must be 32bit aligned */
	if (address & 0x3) {
		fprintf(stderr, "Error: WRITE address must be 4 byte aligned\n");
		return STM32_ERR_UNKNOWN;
	}

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

	/* send the address and checksum */
	if (stm32_send_command(stm, stm->cmd->wm) != 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;

	aligned_len = (len + 3) & ~3;
	cs = aligned_len - 1;
	buf[0] = aligned_len - 1;
	for (i = 0; i < len; i++) {
		cs ^= data[i];
		buf[i + 1] = data[i];
	}
	/* padding data */
	for (i = len; i < aligned_len; i++) {
		cs ^= 0xFF;
		buf[i + 1] = 0xFF;
	}
	buf[aligned_len + 1] = cs;
	if (port->write(port, buf, aligned_len + 2) != PORT_ERR_OK)
		return STM32_ERR_UNKNOWN;

	s_err = stm32_get_ack_timeout(stm, STM32_BLKWRITE_TIMEOUT);
	if (s_err != STM32_ERR_OK) {
		if ((port->flags & PORT_STRETCH_W)
		    && stm->cmd->wm != STM32_CMD_WM_NS)
			stm32_warn_stretching("write");
		return STM32_ERR_UNKNOWN;
	}
	return STM32_ERR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
testl %ecx, %ecx
je 0x5ff6
movl %ecx, %r12d
cmpl $0x101, %ecx            # imm = 0x101
jae 0x600c
movl %esi, %ebp
testb $0x3, %bpl
jne 0x6013
movq %rdi, %r14
movq 0x18(%rdi), %rax
movb 0x5(%rax), %al
cmpb $-0x1, %al
je 0x601f
movq %rdx, %r13
movq 0x8(%r14), %r15
movzbl %al, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x6b78
movl $0x1, %ebx
testl %eax, %eax
jne 0x5ff8
movl %ebp, %eax
shrl $0x18, %eax
leaq 0x10(%rsp), %rsi
movb %al, (%rsi)
movl %ebp, %ecx
shrl $0x10, %ecx
movb %cl, 0x1(%rsi)
movl %ebp, %edx
shrl $0x8, %edx
movb %dl, 0x2(%rsi)
movb %bpl, 0x3(%rsi)
xorb %bpl, %cl
xorb %dl, %al
xorb %cl, %al
movb %al, 0x4(%rsi)
movl $0x5, %edx
movq %r15, %rdi
callq *0x30(%r15)
testl %eax, %eax
jne 0x5ff8
movq %r14, %rdi
xorl %esi, %esi
callq 0x602b
testl %eax, %eax
jne 0x5ff8
movq %r15, 0x8(%rsp)
leal 0x3(%r12), %eax
andl $0x3fc, %eax            # imm = 0x3FC
movq %rax, (%rsp)
leal -0x1(%rax), %r15d
leaq 0x11(%rsp), %rdi
movb %r15b, -0x1(%rdi)
movl %r12d, %ebp
movq %r13, %rsi
movq %rbp, %rdx
callq 0x21a0
xorl %eax, %eax
xorb (%r13,%rax), %r15b
incq %rax
cmpq %rax, %rbp
jne 0x5f63
movq (%rsp), %rdx
movl %edx, %r13d
cmpl %r12d, %edx
jbe 0x5fa9
leaq (%rsp,%rbp), %rdi
addq $0x11, %rdi
notl %r12d
addl %edx, %r12d
incq %r12
movl $0xff, %esi
movq %r12, %rdx
callq 0x2100
movq (%rsp), %rdx
subq %r13, %rbp
notb %r15b
incq %rbp
jne 0x5fa1
movb %r15b, 0x11(%rsp,%r13)
orl $0x2, %edx
leaq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq *0x30(%r15)
testl %eax, %eax
jne 0x5ff8
movl $0x1, %esi
movq %r14, %rdi
callq 0x602b
testl %eax, %eax
je 0x5ff6
testb $0x10, 0x8(%r15)
je 0x5ff8
movq 0x18(%r14), %rax
cmpb $0x32, 0x5(%rax)
je 0x5ff8
leaq 0x386f(%rip), %rdi      # 0x985e
callq 0x2a42
jmp 0x5ff8
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2cb5
jmp 0x6018
callq 0x2c7d
movl $0x1, %ebx
jmp 0x5ff8
callq 0x2c99
movl $0x3, %ebx
jmp 0x5ff8