// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
testl %edx, %edx
sete %al
cmpl $0x10000, %esi          # imm = 0x10000
setae %cl
movl $0x0, 0x4(%rsp)
orb %al, %cl
jne 0x5b83
movl %edx, %r15d
movl %esi, %r13d
cmpl $0x100000, %edx         # imm = 0x100000
setne %al
leal (%r15,%r13), %ecx
cmpl $0x10000, %ecx          # imm = 0x10000
setae %cl
testb %cl, %al
jne 0x5b83
movq %rdi, %rbp
movq 0x18(%rdi), %rax
movb 0x6(%rax), %al
cmpb $-0x1, %al
je 0x5b96
cmpl $0x100000, %r15d        # imm = 0x100000
jne 0x59c9
movq 0x20(%rbp), %rcx
testb $0x1, 0x40(%rcx)
jne 0x59be
movq 0x8(%rbp), %r15
movzbl %al, %esi
movq %rbp, %rdi
callq 0x5132
testl %eax, %eax
jne 0x5bce
movq 0x18(%rbp), %rax
cmpb $0x43, 0x6(%rax)
jne 0x5b49
pushq $0x23
popq %rdx
movq %rbp, %rdi
movl $0xff, %esi
callq 0x620a
testl %eax, %eax
je 0x5b7b
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
testb $0x10, 0x8(%r15)
je 0x5b83
leaq 0x3755(%rip), %rdi      # 0x910e
jmp 0x5b42
movl 0x1c(%rcx), %edi
callq 0x2cfc
movl %eax, %r15d
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
movq %rbp, 0x8(%rsp)
testl %r15d, %r15d
je 0x5b7b
movl $0x200, %eax            # imm = 0x200
cmpl %eax, %r15d
movl $0x200, %ebx            # imm = 0x200
cmovbl %r15d, %ebx
movq 0x8(%rbp), %r12
movq 0x18(%rbp), %rax
movzbl 0x6(%rax), %esi
movq %rbp, %rdi
callq 0x5132
testl %eax, %eax
jne 0x5bb9
movq 0x18(%rbp), %rax
cmpb $0x43, 0x6(%rax)
jne 0x5a50
leal 0x2(%rbx), %edi
callq 0x21c0
testq %rax, %rax
je 0x5b83
movq %rax, %rbp
leal -0x1(%rbx), %eax
movb %al, (%rbp)
leal (%rbx,%r13), %r14d
movl %r13d, %ecx
xorl %edx, %edx
leal (%rcx,%rdx), %esi
cmpl %r14d, %esi
jae 0x5aa8
leal (%rcx,%rdx), %esi
movb %sil, 0x1(%rbp,%rdx)
xorb %sil, %al
incq %rdx
jmp 0x5a38
leal 0x3(,%rbx,2), %edi
callq 0x21c0
testq %rax, %rax
je 0x5b83
movq %rax, %rbp
leal -0x1(%rbx), %ecx
movl %ecx, %eax
shrl $0x8, %eax
movl %ecx, %edx
rolw $0x8, %dx
xorl %ecx, %eax
movw %dx, (%rbp)
leal (%rbx,%r13), %r14d
pushq $0x2
popq %rdx
cmpl %r14d, %r13d
jae 0x5ae7
movl %r13d, %ecx
shrl $0x8, %ecx
xorl %r13d, %eax
movl %r13d, %esi
rolw $0x8, %si
xorl %ecx, %eax
movw %si, (%rbp,%rdx)
addq $0x2, %rdx
incl %r13d
jmp 0x5a83
leal 0x1(%rdx), %ecx
addl $0x2, %edx
movb %al, (%rbp,%rcx)
movq %r12, %rdi
movq %rbp, %rsi
callq *0x30(%r12)
movl %eax, %r13d
movq %rbp, %rdi
callq 0x2030
testl %r13d, %r13d
jne 0x5be6
leal (%rbx,%rbx,4), %esi
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x551e
testl %eax, %eax
je 0x5b28
jmp 0x5b33
andl $-0x2, %edx
movb %al, (%rbp,%rdx)
incq %rdx
movq %r12, %rdi
movq %rbp, %rsi
callq *0x30(%r12)
movl %eax, %r13d
movq %rbp, %rdi
callq 0x2030
testl %r13d, %r13d
jne 0x5bfb
leal (%rbx,%rbx,4), %esi
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x551e
testl %eax, %eax
jne 0x5c1c
subl %ebx, %r15d
movl %r14d, %r13d
jmp 0x59d5
testb $0x10, 0x8(%r12)
je 0x5b83
leaq 0x2df9(%rip), %rdi      # 0x893b
callq 0x55f7
jmp 0x5b83
leaq 0x15(%rsp), %rsi
orw $-0x1, (%rsi)
movb $0x0, 0x2(%rsi)
pushq $0x3
popq %rdx
movq %r15, %rdi
callq *0x30(%r15)
testl %eax, %eax
jne 0x5c57
pushq $0x23
popq %rsi
movq %rbp, %rdi
callq 0x551e
testl %eax, %eax
jne 0x5c7f
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7453(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x325b(%rip), %rdi      # 0x8e02
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
pushq $0x3
jmp 0x5c75
movq 0x7430(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3523(%rip), %rdi      # 0x90ed
pushq $0x20
jmp 0x5c0e
movq 0x741b(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x350e(%rip), %rdi      # 0x90ed
pushq $0x20
jmp 0x5c6a
movq 0x7403(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x357a(%rip), %rdi      # 0x9171
pushq $0xe
jmp 0x5c0e
movq 0x73ee(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3574(%rip), %rdi      # 0x9180
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
jmp 0x5b83
movq 0x73cd(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x356e(%rip), %rdi      # 0x919b
pushq $0x49
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
testb $0x10, 0x8(%r12)
je 0x5b83
movq 0x18(%rbp), %rax
cmpb $0x45, 0x6(%rax)
jne 0x5b3b
jmp 0x5b83
movq 0x7392(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x34b1(%rip), %rdi      # 0x9119
pushq $0x12
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
jmp 0x5b83
movq 0x736a(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x349c(%rip), %rdi      # 0x912c
pushq $0x44
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
testb $0x10, 0x8(%r15)
je 0x5b83
movq 0x18(%rbp), %rax
cmpb $0x45, 0x6(%rax)
jne 0x59b2
jmp 0x5b83