// File: /workspace/github_c_makefile_star2_O3/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 0x61fb
movl %ecx, %r13d
cmpl $0x101, %ecx            # imm = 0x101
jae 0x6211
movl %esi, %r12d
testb $0x3, %r12b
jne 0x6218
movq %rdi, %r14
movq 0x18(%rdi), %rax
movb 0x5(%rax), %al
cmpb $-0x1, %al
je 0x6224
movq %rdx, %rbp
movq 0x8(%r14), %r15
movzbl %al, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x7373
movl $0x1, %ebx
testl %eax, %eax
jne 0x61fd
movl %r12d, %eax
shrl $0x18, %eax
leaq 0x10(%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 %r15, %rdi
callq *0x30(%r15)
testl %eax, %eax
jne 0x61fd
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0x6230
testl %eax, %eax
jne 0x61fd
movq %r15, 0x8(%rsp)
leaq 0x11(%rsp), %rdi
leal 0x3(%r13), %eax
andl $0x3fc, %eax            # imm = 0x3FC
movq %rax, (%rsp)
leal -0x1(%rax), %r15d
movb %r15b, -0x1(%rdi)
movl %r13d, %r12d
movq %rbp, %rsi
movq %r12, %rdx
callq 0x21a0
xorl %eax, %eax
xorb (%rbp,%rax), %r15b
incq %rax
cmpq %rax, %r12
jne 0x5eda
movq (%rsp), %rdx
movl %edx, %ebp
cmpl %r13d, %edx
jbe 0x61ad
leaq (%rsp,%r12), %rdi
addq $0x11, %rdi
notl %r13d
addl %edx, %r13d
incq %r13
movl $0xff, %esi
movq %r13, %rdx
callq 0x2100
movq %rbp, %rcx
subq %r12, %rcx
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
decq %rcx
movzbl %r15b, %edx
movd %edx, %xmm0
movq %rcx, %xmm1
pshufd $0x44, %xmm1, %xmm2     # xmm2 = xmm1[0,1,0,1]
movl $0x10, %ecx
pcmpeqd %xmm3, %xmm3
movdqa %xmm0, %xmm1
pxor %xmm3, %xmm0
leaq (%rax,%rcx), %rdx
addq $-0x10, %rdx
addq $-0x10, %rcx
cmpq $0x10, %rdx
jne 0x5f40
negq %rcx
movq %rcx, %xmm3
pshufd $0x44, %xmm3, %xmm10    # xmm10 = xmm3[0,1,0,1]
movdqa 0x3af0(%rip), %xmm3     # 0x9a60
por %xmm10, %xmm3
movdqa 0x3af3(%rip), %xmm4     # 0x9a70
por %xmm10, %xmm4
movdqa 0x3af6(%rip), %xmm6     # 0x9a80
por %xmm10, %xmm6
movdqa 0x3af9(%rip), %xmm7     # 0x9a90
por %xmm10, %xmm7
movdqa 0x3afb(%rip), %xmm8     # 0x9aa0
por %xmm10, %xmm8
movdqa 0x3afd(%rip), %xmm9     # 0x9ab0
por %xmm10, %xmm9
movdqa 0x3aff(%rip), %xmm11    # 0x9ac0
por %xmm10, %xmm11
por 0x3b01(%rip), %xmm10    # 0x9ad0
movdqa 0x3b09(%rip), %xmm5     # 0x9ae0
pxor %xmm5, %xmm2
pxor %xmm5, %xmm10
movdqa %xmm10, %xmm12
pcmpgtd %xmm2, %xmm12
pshufd $0xa0, %xmm12, %xmm13   # xmm13 = xmm12[0,0,2,2]
pcmpeqd %xmm2, %xmm10
pshufd $0xf5, %xmm10, %xmm10   # xmm10 = xmm10[1,1,3,3]
pand %xmm13, %xmm10
pshufd $0xf5, %xmm12, %xmm12   # xmm12 = xmm12[1,1,3,3]
por %xmm10, %xmm12
pxor %xmm5, %xmm11
movdqa %xmm11, %xmm10
pcmpgtd %xmm2, %xmm10
pshufd $0xa0, %xmm10, %xmm13   # xmm13 = xmm10[0,0,2,2]
pcmpeqd %xmm2, %xmm11
pshufd $0xf5, %xmm11, %xmm11   # xmm11 = xmm11[1,1,3,3]
pand %xmm13, %xmm11
pshufd $0xf5, %xmm10, %xmm10   # xmm10 = xmm10[1,1,3,3]
por %xmm11, %xmm10
packssdw %xmm12, %xmm10
pxor %xmm5, %xmm9
movdqa %xmm9, %xmm11
pcmpgtd %xmm2, %xmm11
pshufd $0xa0, %xmm11, %xmm12   # xmm12 = xmm11[0,0,2,2]
pcmpeqd %xmm2, %xmm9
pshufd $0xf5, %xmm9, %xmm9     # xmm9 = xmm9[1,1,3,3]
pand %xmm12, %xmm9
pshufd $0xf5, %xmm11, %xmm11   # xmm11 = xmm11[1,1,3,3]
por %xmm9, %xmm11
pxor %xmm5, %xmm8
movdqa %xmm8, %xmm9
pcmpgtd %xmm2, %xmm9
pshufd $0xa0, %xmm9, %xmm12    # xmm12 = xmm9[0,0,2,2]
pcmpeqd %xmm2, %xmm8
pshufd $0xf5, %xmm8, %xmm13    # xmm13 = xmm8[1,1,3,3]
pand %xmm12, %xmm13
pshufd $0xf5, %xmm9, %xmm8     # xmm8 = xmm9[1,1,3,3]
por %xmm13, %xmm8
packssdw %xmm11, %xmm8
packssdw %xmm10, %xmm8
pxor %xmm5, %xmm7
movdqa %xmm7, %xmm9
pcmpgtd %xmm2, %xmm9
pshufd $0xa0, %xmm9, %xmm10    # xmm10 = xmm9[0,0,2,2]
pcmpeqd %xmm2, %xmm7
pshufd $0xf5, %xmm7, %xmm11    # xmm11 = xmm7[1,1,3,3]
pand %xmm10, %xmm11
pshufd $0xf5, %xmm9, %xmm7     # xmm7 = xmm9[1,1,3,3]
por %xmm11, %xmm7
pxor %xmm5, %xmm6
movdqa %xmm6, %xmm9
pcmpgtd %xmm2, %xmm9
pshufd $0xa0, %xmm9, %xmm10    # xmm10 = xmm9[0,0,2,2]
pcmpeqd %xmm2, %xmm6
pshufd $0xf5, %xmm6, %xmm6     # xmm6 = xmm6[1,1,3,3]
pand %xmm10, %xmm6
pshufd $0xf5, %xmm9, %xmm9     # xmm9 = xmm9[1,1,3,3]
por %xmm6, %xmm9
packssdw %xmm9, %xmm7
pxor %xmm5, %xmm4
movdqa %xmm4, %xmm6
pcmpgtd %xmm2, %xmm6
pshufd $0xa0, %xmm6, %xmm9     # xmm9 = xmm6[0,0,2,2]
pcmpeqd %xmm2, %xmm4
pshufd $0xf5, %xmm4, %xmm4     # xmm4 = xmm4[1,1,3,3]
pand %xmm9, %xmm4
pshufd $0xf5, %xmm6, %xmm6     # xmm6 = xmm6[1,1,3,3]
por %xmm4, %xmm6
pxor %xmm5, %xmm3
movdqa %xmm3, %xmm4
pcmpgtd %xmm2, %xmm4
pshufd $0xa0, %xmm4, %xmm5     # xmm5 = xmm4[0,0,2,2]
pcmpeqd %xmm2, %xmm3
pshufd $0xf5, %xmm3, %xmm2     # xmm2 = xmm3[1,1,3,3]
pand %xmm5, %xmm2
pshufd $0xf5, %xmm4, %xmm3     # xmm3 = xmm4[1,1,3,3]
por %xmm2, %xmm3
packssdw %xmm3, %xmm6
packssdw %xmm6, %xmm7
packsswb %xmm7, %xmm8
pand %xmm8, %xmm1
pandn %xmm0, %xmm8
por %xmm1, %xmm8
pshufd $0xee, %xmm8, %xmm0     # xmm0 = xmm8[2,3,2,3]
pxor %xmm8, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
pxor %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrld $0x10, %xmm0
pxor %xmm1, %xmm0
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
pxor %xmm0, %xmm1
movd %xmm1, %r15d
movq (%rsp), %rdx
movb %r15b, 0x11(%rsp,%rbp)
orl $0x2, %edx
leaq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq *0x30(%r15)
testl %eax, %eax
jne 0x61fd
movq 0x8(%r14), %rdi
movl $0x1, %esi
callq 0x6230
testl %eax, %eax
je 0x61fb
testb $0x10, 0x8(%r15)
je 0x61fd
movq 0x18(%r14), %rax
cmpb $0x32, 0x5(%rax)
je 0x61fd
leaq 0x367e(%rip), %rdi      # 0x9872
callq 0x2976
jmp 0x61fd
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 0x2bbd
jmp 0x621d
callq 0x2b85
movl $0x1, %ebx
jmp 0x61fd
callq 0x2ba1
movl $0x3, %ebx
jmp 0x61fd