// File: /workspace/github_c_makefile_star2_O1/koendv[P]stm32duino-raspberrypi/Arduino_Tools/src/stm32flash/stm32.c
// Function: stm32_erase_memory
stm32_err_t stm32_erase_memory(const stm32_t *stm, uint32_t spage, uint32_t pages)
{
	uint32_t n;
	stm32_err_t s_err;

	if (!pages || spage > STM32_MAX_PAGES ||
	    ((pages != STM32_MASS_ERASE) && ((spage + pages) > STM32_MAX_PAGES)))
		return STM32_ERR_OK;

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

	if (pages == STM32_MASS_ERASE) {
		/*
		 * Not all chips support mass erase.
		 * Mass erase can be obtained executing a "readout protect"
		 * followed by "readout un-protect". This method is not
		 * suggested because can hang the target if a debug SWD/JTAG
		 * is connected. When the target enters in "readout
		 * protection" mode it will consider the debug connection as
		 * a tentative of intrusion and will hang.
		 * Erasing the flash page-by-page is the safer way to go.
		 */
		if (!(stm->dev->flags & F_NO_ME))
			return stm32_mass_erase(stm);

		pages = flash_addr_to_page_ceil(stm->dev->fl_end);
	}

	/*
	 * Some device, like STM32L152, cannot erase more than 512 pages in
	 * one command. Split the call.
	 */
	while (pages) {
		n = (pages <= 512) ? pages : 512;
		s_err = stm32_pages_erase(stm, spage, n);
		if (s_err != STM32_ERR_OK)
			return s_err;
		spage += n;
		pages -= n;
	}
	return STM32_ERR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testl %edx, %edx
sete %al
cmpl $0x10000, %esi          # imm = 0x10000
setae %cl
xorl %r13d, %r13d
orb %al, %cl
jne 0x6655
movl %edx, %r12d
movl %esi, %r15d
cmpl $0x100000, %edx         # imm = 0x100000
setne %al
leal (%r12,%r15), %ecx
cmpl $0x10000, %ecx          # imm = 0x10000
setae %cl
testb %cl, %al
jne 0x6655
movq %rdi, %rbx
movq 0x18(%rdi), %rax
movb 0x6(%rax), %al
cmpb $-0x1, %al
je 0x6667
leaq 0x18(%rbx), %rcx
movq %rcx, 0x10(%rsp)
cmpl $0x100000, %r12d        # imm = 0x100000
jne 0x63bf
movq 0x20(%rbx), %rcx
testb $0x1, 0x40(%rcx)
jne 0x63b4
movq 0x8(%rbx), %r14
movzbl %al, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x6b78
testl %eax, %eax
jne 0x6674
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpb $0x43, 0x6(%rax)
jne 0x6623
movl $0x23, %edx
movq %rbx, %rdi
movl $0xff, %esi
callq 0x6b78
testl %eax, %eax
je 0x6652
movl $0x1, %r13d
testb $0x10, 0x8(%r14)
je 0x6655
leaq 0x3d44(%rip), %rdi      # 0xa0ee
callq 0x2a42
jmp 0x6655
movl 0x1c(%rcx), %edi
callq 0x3a50
movl %eax, %r12d
testl %r12d, %r12d
je 0x6655
movq %rbx, 0x8(%rsp)
movl $0x200, %eax            # imm = 0x200
cmpl %eax, %r12d
movl $0x200, %ebp            # imm = 0x200
cmovbl %r12d, %ebp
movq 0x8(%rbx), %r13
movq 0x18(%rbx), %rax
movzbl 0x6(%rax), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x6b78
testl %eax, %eax
jne 0x6577
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpb $0x43, 0x6(%rax)
jne 0x64c0
leal 0x2(%rbp), %edi
callq 0x21c0
testq %rax, %rax
je 0x657c
movq %rax, %r14
leal -0x1(%rbp), %eax
movb %al, (%r14)
leal (%r15,%rbp), %ecx
movl $0x1, %edx
cmpl %r15d, %ecx
jbe 0x646a
movl %r12d, %ecx
movl $0x200, %edx            # imm = 0x200
cmpq %rdx, %rcx
cmovaeq %rdx, %rcx
negq %rcx
movl $0x1, %edx
movl %r15d, %esi
movb %sil, (%r14,%rdx)
leaq (%rcx,%rdx), %rdi
incq %rdi
incq %rdx
xorb %sil, %al
incb %sil
cmpq $0x1, %rdi
jne 0x6450
movl %edx, %ecx
incl %edx
movb %al, (%r14,%rcx)
movq %r13, %rdi
movq %r14, %rsi
movq %r13, %rbx
callq *0x30(%r13)
movl %eax, %r13d
movq %r14, %rdi
callq 0x2030
testl %r13d, %r13d
jne 0x659d
leal (%rbp,%rbp,4), %esi
movq 0x8(%rsp), %rdi
callq 0x602b
testl %eax, %eax
je 0x656b
movl $0x1, %r13d
testb $0x10, 0x8(%rbx)
jne 0x65e9
xorl %eax, %eax
jmp 0x6570
movq %r13, 0x18(%rsp)
leal 0x3(,%rbp,2), %edi
callq 0x21c0
movl $0x1, %r13d
testq %rax, %rax
je 0x6582
movq %rax, %r14
leal -0x1(%rbp), %ecx
movl %ecx, %eax
shrl $0x8, %eax
xorl %ecx, %eax
rolw $0x8, %cx
movw %cx, (%r14)
leal (%r15,%rbp), %ecx
movl $0x2, %edx
cmpl %r15d, %ecx
movq 0x18(%rsp), %rdi
jbe 0x6535
movl $0x2, %edx
movl %ebp, %ecx
movl %r15d, %esi
movl %esi, %r8d
shrl $0x8, %r8d
xorl %esi, %eax
xorl %r8d, %eax
movl %esi, %r8d
rolw $0x8, %r8w
movw %r8w, (%r14,%rdx)
addq $0x2, %rdx
incl %esi
decl %ecx
jne 0x6512
movl %edx, %ecx
orl $0x1, %edx
movb %al, (%r14,%rcx)
movq %r14, %rsi
callq *0x30(%rdi)
movl %eax, %r13d
movq %r14, %rdi
callq 0x2030
testl %r13d, %r13d
jne 0x65ad
leal (%rbp,%rbp,4), %esi
movq %rbx, %rdi
callq 0x602b
testl %eax, %eax
jne 0x65b4
xorl %r13d, %r13d
movb $0x1, %al
jmp 0x6584
xorl %r13d, %r13d
movb $0x1, %al
movq 0x8(%rsp), %rbx
jmp 0x6584
callq 0x2dd3
movl $0x1, %r13d
xorl %eax, %eax
testb %al, %al
je 0x6655
addl %ebp, %r15d
subl %ebp, %r12d
jne 0x63cd
jmp 0x6652
callq 0x2e0b
movl $0x1, %r13d
jmp 0x64b9
callq 0x2def
jmp 0x65df
movq 0x7a3d(%rip), %rax      # 0xdff8
movq (%rax), %rcx
movl $0x49, %esi
movl $0x1, %edx
leaq 0x3bac(%rip), %rdi      # 0xa17b
callq 0x22b0
movq 0x18(%rsp), %rax
testb $0x10, 0x8(%rax)
jne 0x65fa
xorl %eax, %eax
movl $0x1, %r13d
jmp 0x6584
leaq 0x332b(%rip), %rdi      # 0x991b
callq 0x2a42
jmp 0x64b9
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpb $0x45, 0x6(%rax)
movl $0x1, %r13d
je 0x6582
leaq 0x3302(%rip), %rdi      # 0x991b
callq 0x2a42
jmp 0x6582
leaq 0x21(%rsp), %rsi
movw $0xffff, (%rsi)         # imm = 0xFFFF
movb $0x0, 0x2(%rsi)
movl $0x3, %edx
movq %r14, %rdi
callq *0x30(%r14)
testl %eax, %eax
jne 0x667b
movl $0x23, %esi
movq %rbx, %rdi
callq 0x602b
testl %eax, %eax
jne 0x6688
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2eb9
movl $0x3, %r13d
jmp 0x6655
callq 0x2e27
jmp 0x6680
callq 0x2e43
movl $0x1, %r13d
jmp 0x6655
leaq 0x24(%rsp), %rbx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x2e65
movl (%rbx), %r13d
jmp 0x6655