// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_rpi_rp2040.c
// Function: flash_get_size
static int flash_get_size(void)
{
  uint8_t buf[128];
  int flash_size = 0;

  memset(buf, 0, sizeof(buf));

  buf[0] = FLASH_CMD_READ_SFDP;

  spi_select(0);
  spi_transfer(buf, 5 + 16, 5);
  spi_select(1);

  if (buf[0] == 'S' && buf[1] == 'F' && buf[2] == 'D' && buf[3] == 'P' && buf[8] == 0)
  {
    uint32_t w1, w2, w3;

    buf[0] = FLASH_CMD_READ_SFDP;
    buf[1] = buf[14];
    buf[2] = buf[13];
    buf[3] = buf[12];
    buf[4] = 0;

    spi_select(0);
    spi_transfer(buf, 5 + 16*4, 5);
    spi_select(1);

    w1 = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
    w2 = (buf[7] << 24) | (buf[6] << 16) | (buf[5] << 8) | buf[4];
    w3 = (buf[11] << 24) | (buf[10] << 16) | (buf[9] << 8) | buf[8];

    if ((w1 & 0x3) != 0x1)
      error_exit("4 KB erase is not supported");

    flash_cmd_sector_erase = (w1 >> 8) & 0xff;

    if (((w1 >> 17) & 0x3) != 0x0)
      error_exit("flash must support only 3-byte addressing");

    if (w1 & (1 << 22))
      flash_quad_mode = true;

    flash_cmd_read_data = (w3 >> 24) & 0xff;
    flash_wait_cycles   = (w3 >> 16) & 0x1f;

    if (w2 & 0x80000000)
      flash_size = (1ull << (w2 & 0x7fffffff)) / 8;
    else
      flash_size = (w2 + 1) / 8;
  }
  else
  {
    warning("no SFDP information found, using JEDEC ID as a fallback to detect the flash size");

    buf[0] = FLASH_CMD_READ_JEDEC_ID;

    spi_select(0);
    spi_transfer(buf, 4, 1);
    spi_select(1);

    flash_size = (1 << buf[2]);
  }

  if (flash_size < 256 || flash_size > 1024*1024*1024)
    flash_size = 0;

  return flash_size;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl $0x0, -0x84(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x60f0
movb $0x5a, -0x80(%rbp)
xorl %edi, %edi
callq 0x11cf0
leaq -0x80(%rbp), %rdi
movl $0x15, %esi
movl $0x5, %edx
callq 0x11d10
movl $0x1, %edi
callq 0x11cf0
movzbl -0x80(%rbp), %eax
cmpl $0x53, %eax
jne 0x11bce
movzbl -0x7f(%rbp), %eax
cmpl $0x46, %eax
jne 0x11bce
movzbl -0x7e(%rbp), %eax
cmpl $0x44, %eax
jne 0x11bce
movzbl -0x7d(%rbp), %eax
cmpl $0x50, %eax
jne 0x11bce
movzbl -0x78(%rbp), %eax
cmpl $0x0, %eax
jne 0x11bce
movb $0x5a, -0x80(%rbp)
movb -0x72(%rbp), %al
movb %al, -0x7f(%rbp)
movb -0x73(%rbp), %al
movb %al, -0x7e(%rbp)
movb -0x74(%rbp), %al
movb %al, -0x7d(%rbp)
movb $0x0, -0x7c(%rbp)
xorl %edi, %edi
callq 0x11cf0
leaq -0x80(%rbp), %rdi
movl $0x45, %esi
movl $0x5, %edx
callq 0x11d10
movl $0x1, %edi
callq 0x11cf0
movzbl -0x7d(%rbp), %eax
shll $0x18, %eax
movzbl -0x7e(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x7f(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x80(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x88(%rbp)
movzbl -0x79(%rbp), %eax
shll $0x18, %eax
movzbl -0x7a(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x7b(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x7c(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x8c(%rbp)
movzbl -0x75(%rbp), %eax
shll $0x18, %eax
movzbl -0x76(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x77(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x78(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x90(%rbp)
movl -0x88(%rbp), %eax
andl $0x3, %eax
cmpl $0x1, %eax
je 0x11b17
leaq 0x613f(%rip), %rdi      # 0x17c4f
movb $0x0, %al
callq 0x8970
movl -0x88(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movl %eax, 0xdbcd(%rip)      # 0x1f6f8
movl -0x88(%rbp), %eax
shrl $0x11, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x11b4a
leaq 0x6128(%rip), %rdi      # 0x17c6b
movb $0x0, %al
callq 0x8970
movl -0x88(%rbp), %eax
andl $0x400000, %eax         # imm = 0x400000
cmpl $0x0, %eax
je 0x11b61
movb $0x1, 0x70a67(%rip)     # 0x825c8
movl -0x90(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
movl %eax, 0xdb87(%rip)      # 0x1f6fc
movl -0x90(%rbp), %eax
shrl $0x10, %eax
andl $0x1f, %eax
movl %eax, 0x70a45(%rip)     # 0x825cc
movl -0x8c(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x11bba
movl -0x8c(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
shrq $0x3, %rax
movl %eax, -0x84(%rbp)
jmp 0x11bcc
movl -0x8c(%rbp), %eax
addl $0x1, %eax
shrl $0x3, %eax
movl %eax, -0x84(%rbp)
jmp 0x11c15
leaq 0x60c0(%rip), %rdi      # 0x17c95
movb $0x0, %al
callq 0x87a0
movb $-0x61, -0x80(%rbp)
xorl %edi, %edi
callq 0x11cf0
leaq -0x80(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x11d10
movl $0x1, %edi
callq 0x11cf0
movzbl -0x7e(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x84(%rbp)
cmpl $0x100, -0x84(%rbp)     # imm = 0x100
jl 0x11c2d
cmpl $0x40000000, -0x84(%rbp) # imm = 0x40000000
jle 0x11c37
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)