// File: /workspace/github_c_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
testl %ecx, %ecx
je 0x5508
movl %ecx, %r13d
cmpl $0x101, %ecx            # imm = 0x101
jae 0x545c
movl %esi, %ebp
testb $0x3, %bpl
jne 0x5471
movq %rdi, %r14
movq 0x18(%rdi), %rax
movb 0x5(%rax), %al
cmpb $-0x1, %al
je 0x5491
movq %rdx, %r12
movq 0x8(%r14), %r15
movzbl %al, %esi
movq %r14, %rdi
callq 0x5132
pushq $0x1
popq %rbx
testl %eax, %eax
jne 0x550a
movl %ebp, %eax
shrl $0x18, %eax
movq %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)
pushq $0x5
popq %rdx
movq %r15, %rdi
callq *0x30(%r15)
testl %eax, %eax
jne 0x550a
movq %r14, %rdi
callq 0x5154
testl %eax, %eax
jne 0x550a
leal 0x3(%r13), %edx
andl $0x3fc, %edx            # imm = 0x3FC
leal -0x1(%rdx), %eax
movb %al, (%rsp)
movl %r13d, %ecx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x54b2
movb (%r12,%rsi), %dil
xorb %dil, %al
movb %dil, 0x1(%rsp,%rsi)
incq %rsi
jmp 0x5446
movq 0x7b8d(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3714(%rip), %rdi      # 0x8b81
pushq $0x26
jmp 0x5484
movq 0x7b78(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x375a(%rip), %rdi      # 0x8bdc
pushq $0x2c
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
pushq $0x1
jmp 0x54af
movq 0x7b58(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3767(%rip), %rdi      # 0x8c09
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
pushq $0x3
popq %rbx
jmp 0x550a
movl %edx, %esi
cmpq %rsi, %rcx
jae 0x54c5
notb %al
movb $-0x1, 0x1(%rsp,%rcx)
incq %rcx
jmp 0x54b4
movb %al, 0x1(%rsp,%rsi)
orl $0x2, %edx
movq %rsp, %rsi
movq %r15, %rdi
callq *0x30(%r15)
testl %eax, %eax
jne 0x550a
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x551e
testl %eax, %eax
je 0x5508
testb $0x10, 0x8(%r15)
je 0x550a
movq 0x18(%r14), %rax
cmpb $0x32, 0x5(%rax)
je 0x550a
leaq 0x337d(%rip), %rdi      # 0x887e
callq 0x55f7
jmp 0x550a
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq