// File: /workspace/github_c_makefile_star2_O0/koendv[P]stm32duino-raspberrypi/Arduino_Tools/src/stm32flash/stm32.c
// Function: stm32_pages_erase
static stm32_err_t stm32_pages_erase(const stm32_t *stm, uint32_t spage, uint32_t pages)
{
	struct port_interface *port = stm->port;
	stm32_err_t s_err;
	port_err_t p_err;
	uint32_t pg_num;
	uint8_t pg_byte;
	uint8_t cs = 0;
	uint8_t *buf;
	int i = 0;

	/* The erase command reported by the bootloader is either 0x43, 0x44 or 0x45 */
	/* 0x44 is Extended Erase, a 2 byte based protocol and needs to be handled differently. */
	/* 0x45 is clock no-stretching version of Extended Erase for I2C port. */
	if (stm32_send_command(stm, stm->cmd->er) != STM32_ERR_OK) {
		fprintf(stderr, "Can't initiate chip mass erase!\n");
		return STM32_ERR_UNKNOWN;
	}

	/* regular erase (0x43) */
	if (stm->cmd->er == STM32_CMD_ER) {
		buf = malloc(1 + pages + 1);
		if (!buf)
			return STM32_ERR_UNKNOWN;

		buf[i++] = pages - 1;
		cs ^= (pages-1);
		for (pg_num = spage; pg_num < (pages + spage); pg_num++) {
			buf[i++] = pg_num;
			cs ^= pg_num;
		}
		buf[i++] = cs;
		p_err = port->write(port, buf, i);
		free(buf);
		if (p_err != PORT_ERR_OK) {
			fprintf(stderr, "Erase failed.\n");
			return STM32_ERR_UNKNOWN;
		}
		s_err = stm32_get_ack_timeout(stm, pages * STM32_PAGEERASE_TIMEOUT);
		if (s_err != STM32_ERR_OK) {
			if (port->flags & PORT_STRETCH_W)
				stm32_warn_stretching("erase");
			return STM32_ERR_UNKNOWN;
		}
		return STM32_ERR_OK;
	}

	/* extended erase */
	buf = malloc(2 + 2 * pages + 1);
	if (!buf)
		return STM32_ERR_UNKNOWN;

	/* Number of pages to be erased - 1, two bytes, MSB first */
	pg_byte = (pages - 1) >> 8;
	buf[i++] = pg_byte;
	cs ^= pg_byte;
	pg_byte = (pages - 1) & 0xFF;
	buf[i++] = pg_byte;
	cs ^= pg_byte;

	for (pg_num = spage; pg_num < spage + pages; pg_num++) {
		pg_byte = pg_num >> 8;
		cs ^= pg_byte;
		buf[i++] = pg_byte;
		pg_byte = pg_num & 0xFF;
		cs ^= pg_byte;
		buf[i++] = pg_byte;
	}
	buf[i++] = cs;
	p_err = port->write(port, buf, i);
	free(buf);
	if (p_err != PORT_ERR_OK) {
		fprintf(stderr, "Page-by-page erase error.\n");
		return STM32_ERR_UNKNOWN;
	}

	s_err = stm32_get_ack_timeout(stm, pages * STM32_PAGEERASE_TIMEOUT);
	if (s_err != STM32_ERR_OK) {
		fprintf(stderr, "Page-by-page erase failed. Check the maximum pages your device supports.\n");
		if ((port->flags & PORT_STRETCH_W)
		    && stm->cmd->er != STM32_CMD_EE_NS)
			stm32_warn_stretching("erase");
		return STM32_ERR_UNKNOWN;
	}

	return STM32_ERR_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movb $0x0, -0x2e(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x6(%rax), %esi
callq 0x7190
cmpl $0x0, %eax
je 0x8197
movq 0x7e76(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x428d(%rip), %rsi      # 0xc411
movb $0x0, %al
callq 0x2160
movl $0x1, -0x4(%rbp)
jmp 0x84d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x6(%rax), %eax
cmpl $0x43, %eax
jne 0x82f6
movl -0x18(%rbp), %eax
addl $0x1, %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x21b0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x81d5
movl $0x1, -0x4(%rbp)
jmp 0x84d0
movl -0x18(%rbp), %eax
subl $0x1, %eax
movb %al, %dl
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x18(%rbp), %ecx
subl $0x1, %ecx
movzbl -0x2e(%rbp), %eax
xorl %ecx, %eax
movb %al, -0x2e(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl -0x18(%rbp), %ecx
addl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jae 0x8245
movl -0x2c(%rbp), %eax
movb %al, %dl
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %ecx
movzbl -0x2e(%rbp), %eax
xorl %ecx, %eax
movb %al, -0x2e(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x8207
movb -0x2e(%rbp), %dl
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movslq -0x3c(%rbp), %rdx
callq *%rax
movl %eax, -0x28(%rbp)
movq -0x38(%rbp), %rdi
callq 0x2030
cmpl $0x0, -0x28(%rbp)
je 0x82a9
movq 0x7d64(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x41ff(%rip), %rsi      # 0xc495
movb $0x0, %al
callq 0x2160
movl $0x1, -0x4(%rbp)
jmp 0x84d0
movq -0x10(%rbp), %rdi
imull $0x5, -0x18(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x78d0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x82ea
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x82de
leaq 0x3659(%rip), %rdi      # 0xb932
callq 0x79f0
movl $0x1, -0x4(%rbp)
jmp 0x84d0
movl $0x0, -0x4(%rbp)
jmp 0x84d0
movl -0x18(%rbp), %eax
shll %eax
addl $0x2, %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x21b0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x8321
movl $0x1, -0x4(%rbp)
jmp 0x84d0
movl -0x18(%rbp), %eax
subl $0x1, %eax
shrl $0x8, %eax
movb %al, -0x2d(%rbp)
movb -0x2d(%rbp), %dl
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movzbl -0x2d(%rbp), %ecx
movzbl -0x2e(%rbp), %eax
xorl %ecx, %eax
movb %al, -0x2e(%rbp)
movl -0x18(%rbp), %eax
subl $0x1, %eax
andl $0xff, %eax
movb %al, -0x2d(%rbp)
movb -0x2d(%rbp), %dl
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movzbl -0x2d(%rbp), %ecx
movzbl -0x2e(%rbp), %eax
xorl %ecx, %eax
movb %al, -0x2e(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl -0x14(%rbp), %ecx
addl -0x18(%rbp), %ecx
cmpl %ecx, %eax
jae 0x8401
movl -0x2c(%rbp), %eax
shrl $0x8, %eax
movb %al, -0x2d(%rbp)
movzbl -0x2d(%rbp), %ecx
movzbl -0x2e(%rbp), %eax
xorl %ecx, %eax
movb %al, -0x2e(%rbp)
movb -0x2d(%rbp), %dl
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
andl $0xff, %eax
movb %al, -0x2d(%rbp)
movzbl -0x2d(%rbp), %ecx
movzbl -0x2e(%rbp), %eax
xorl %ecx, %eax
movb %al, -0x2e(%rbp)
movb -0x2d(%rbp), %dl
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x838b
movb -0x2e(%rbp), %dl
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movslq -0x3c(%rbp), %rdx
callq *%rax
movl %eax, -0x28(%rbp)
movq -0x38(%rbp), %rdi
callq 0x2030
cmpl $0x0, -0x28(%rbp)
je 0x8462
movq 0x7ba8(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x4052(%rip), %rsi      # 0xc4a4
movb $0x0, %al
callq 0x2160
movl $0x1, -0x4(%rbp)
jmp 0x84d0
movq -0x10(%rbp), %rdi
imull $0x5, -0x18(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x78d0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x84c9
movq 0x7b6d(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x4032(%rip), %rsi      # 0xc4bf
movb $0x0, %al
callq 0x2160
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x84c0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x6(%rax), %eax
cmpl $0x45, %eax
je 0x84c0
leaq 0x3477(%rip), %rdi      # 0xb932
callq 0x79f0
movl $0x1, -0x4(%rbp)
jmp 0x84d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)