// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
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 0x75f7
movl $0x0, -0x4(%rbp)
jmp 0x78c4
cmpl $0x100, -0x24(%rbp)     # imm = 0x100
jbe 0x7624
movq 0x89e9(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x4894(%rip), %rsi      # 0xbea5
movb $0x0, %al
callq 0x2160
movl $0x1, -0x4(%rbp)
jmp 0x78c4
movl -0x14(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x7653
movq 0x89ba(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x48c0(%rip), %rsi      # 0xbf00
movb $0x0, %al
callq 0x2160
movl $0x1, -0x4(%rbp)
jmp 0x78c4
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x5(%rax), %eax
cmpl $0xff, %eax
jne 0x768a
movq 0x8983(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x48b6(%rip), %rsi      # 0xbf2d
movb $0x0, %al
callq 0x2160
movl $0x3, -0x4(%rbp)
jmp 0x78c4
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x5(%rax), %esi
callq 0x7190
cmpl $0x0, %eax
je 0x76b0
movl $0x1, -0x4(%rbp)
jmp 0x78c4
movl -0x14(%rbp), %eax
shrl $0x18, %eax
movb %al, -0x140(%rbp)
movl -0x14(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x13f(%rbp)
movl -0x14(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, -0x13e(%rbp)
movl -0x14(%rbp), %eax
andl $0xff, %eax
movb %al, -0x13d(%rbp)
movzbl -0x140(%rbp), %eax
movzbl -0x13f(%rbp), %ecx
xorl %ecx, %eax
movzbl -0x13e(%rbp), %ecx
xorl %ecx, %eax
movzbl -0x13d(%rbp), %ecx
xorl %ecx, %eax
movb %al, -0x13c(%rbp)
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x30(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movl $0x5, %edx
callq *%rax
cmpl $0x0, %eax
je 0x773f
movl $0x1, -0x4(%rbp)
jmp 0x78c4
movq -0x10(%rbp), %rdi
callq 0x71f0
cmpl $0x0, %eax
je 0x7759
movl $0x1, -0x4(%rbp)
jmp 0x78c4
movl -0x24(%rbp), %eax
addl $0x3, %eax
andl $-0x4, %eax
movl %eax, -0x148(%rbp)
movl -0x148(%rbp), %eax
subl $0x1, %eax
movb %al, -0x31(%rbp)
movl -0x148(%rbp), %eax
subl $0x1, %eax
movb %al, -0x140(%rbp)
movl $0x0, -0x144(%rbp)
movl -0x144(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x77df
movq -0x20(%rbp), %rax
movl -0x144(%rbp), %ecx
movzbl (%rax,%rcx), %ecx
movzbl -0x31(%rbp), %eax
xorl %ecx, %eax
movb %al, -0x31(%rbp)
movq -0x20(%rbp), %rax
movl -0x144(%rbp), %ecx
movb (%rax,%rcx), %cl
movl -0x144(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movb %cl, -0x140(%rbp,%rax)
movl -0x144(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x144(%rbp)
jmp 0x778d
movl -0x24(%rbp), %eax
movl %eax, -0x144(%rbp)
movl -0x144(%rbp), %eax
cmpl -0x148(%rbp), %eax
jae 0x7826
movzbl -0x31(%rbp), %eax
xorl $0xff, %eax
movb %al, -0x31(%rbp)
movl -0x144(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movb $-0x1, -0x140(%rbp,%rax)
movl -0x144(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x144(%rbp)
jmp 0x77e8
movb -0x31(%rbp), %cl
movl -0x148(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movb %cl, -0x140(%rbp,%rax)
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x30(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movl -0x148(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movl %ecx, %edx
callq *%rax
cmpl $0x0, %eax
je 0x786b
movl $0x1, -0x4(%rbp)
jmp 0x78c4
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x78d0
movl %eax, -0x14c(%rbp)
cmpl $0x0, -0x14c(%rbp)
je 0x78bd
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x78b4
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x5(%rax), %eax
cmpl $0x32, %eax
je 0x78b4
leaq 0x3fc6(%rip), %rdi      # 0xb875
callq 0x79f0
movl $0x1, -0x4(%rbp)
jmp 0x78c4
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq