// File: /workspace/github_c_makefile_star2_O2/orempel[P]multiboot_tool/mpmboot.c
// Function: mpm_open
static int mpm_open(struct multiboot *mboot)
{
    struct mpm_privdata *mpm = (struct mpm_privdata *)mboot->privdata;

    if (mpm->address == 0)
    {
        fprintf(stderr, "abort: no address given\n");
        return -1;
    }

    if (mpm->device == NULL)
    {
        fprintf(stderr, "abort: no device given\n");
        return -1;
    }

    if (mpm_open_device(mpm) < 0)
    {
        return -1;
    }

    if (mpm_switch_application(mpm, BOOTTYPE_BOOTLOADER))
    {
        fprintf(stderr, "failed to switch to bootloader (invalid address?)\n");
        mpm_close(mboot);
        return -1;
    }

    /* wait for watchdog and startup time */
    usleep(100000);

    char version[16 +1];
    if (mpm_read_version(mpm, (uint8_t *)version, sizeof(version) -1))
    {
        fprintf(stderr, "failed to get bootloader version\n");
        mpm_close(mboot);
        return -1;
    }

    version[16] = '\0';

    uint8_t chipinfo[8];
    if (mpm_read_chipinfo(mpm, chipinfo, sizeof(chipinfo)))
    {
        fprintf(stderr, "failed to get bootloader version\n");
        mpm_close(mboot);
        return -1;
    }

    const char *chipname = chipinfo_get_avr_name(chipinfo);

    mpm->flashpage  = chipinfo[3];
    mpm->flashsize  = (chipinfo[4] << 8) + chipinfo[5];
    mpm->eepromsize = (chipinfo[6] << 8) + chipinfo[7];

    printf("device         : %-16s (address: 0x%02X)\n",
           mpm->device, mpm->address);

    printf("version        : %-16s (sig: 0x%02x 0x%02x 0x%02x => %s)\n",
           version, chipinfo[0], chipinfo[1], chipinfo[2], chipname);

    printf("flash size     : 0x%04x / %5d   (0x%02x bytes/page)\n",
           mpm->flashsize, mpm->flashsize, mpm->flashpage);

    printf("eeprom size    : 0x%04x / %5d\n",
           mpm->eepromsize, mpm->eepromsize);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x58, %rsp
movq 0x8(%rdi), %rbx
cmpb $0x0, 0x10(%rbx)
je 0x559e
movq %rdi, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x55b3
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2210
movl %eax, 0x8(%rbx)
testl %eax, %eax
js 0x55d1
leaq 0x18(%rbx), %rsi
movl %eax, %edi
callq 0x21f0
testl %eax, %eax
js 0x55df
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movups %xmm0, 0x4(%rdx)
movups %xmm0, 0x14(%rdx)
movups %xmm0, 0x24(%rdx)
andq $0x0, 0x34(%rdx)
movl $0x1, (%rdx)
movl $0x400019b2, 0x8(%rdx)  # imm = 0x400019B2
movb $0x1, 0x17(%rdx)
movl 0x8(%rbx), %edi
xorl %esi, %esi
callq 0x2200
testl %eax, %eax
js 0x55e8
movl $0x1, 0xc(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5b11
testl %eax, %eax
jne 0x560c
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22c0
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
pushq $0x2
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x5b67
testl %eax, %eax
js 0x5578
leaq 0x8(%rsp), %rdx
movb $0x0, (%rdx)
pushq $0x2
popq %rsi
leaq 0x10(%rsp), %rcx
pushq $0x10
popq %r8
movq %rbx, %rdi
callq 0x5c7b
testl %eax, %eax
js 0x5578
xorl %eax, %eax
cmpq $0x10, %rax
je 0x5485
andb $0x7f, 0x10(%rsp,%rax)
incq %rax
jmp 0x5475
cmpb $0x0, 0x8(%rsp)
jne 0x5578
movb $0x0, 0x20(%rsp)
pushq $0x3
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x5b67
testl %eax, %eax
js 0x5578
leaq 0x7(%rsp), %rdx
movb $0x0, (%rdx)
pushq $0x3
popq %rsi
leaq 0x8(%rsp), %rcx
pushq $0x8
popq %r8
movq %rbx, %rdi
callq 0x5c7b
testl %eax, %eax
js 0x5578
cmpb $0x0, 0x7(%rsp)
jne 0x5578
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x2eb8
movb 0x3(%r14), %cl
movb %cl, 0x14(%rbx)
movzwl 0x4(%r14), %ecx
rolw $0x8, %cx
movw %cx, 0x12(%rbx)
movzwl 0x6(%r14), %ecx
rolw $0x8, %cx
movq %rax, %r15
movw %cx, 0x16(%rbx)
movq (%rbx), %rsi
movzbl 0x10(%rbx), %edx
leaq 0x3da9(%rip), %rdi      # 0x92c3
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
movzbl (%r14), %edx
movzbl 0x1(%r14), %ecx
movzbl 0x2(%r14), %r8d
leaq 0x3899(%rip), %rdi      # 0x8dd1
leaq 0x10(%rsp), %rsi
movq %r15, %r9
xorl %eax, %eax
callq 0x20f0
movzwl 0x12(%rbx), %edx
movzbl 0x14(%rbx), %ecx
leaq 0x38b5(%rip), %rdi      # 0x8e0b
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movzwl 0x16(%rbx), %edx
leaq 0x2e92(%rip), %rdi      # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x55ff
movq 0x7a79(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x3803(%rip), %rdi      # 0x8d8c
pushq $0x21
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
movq %r14, %rdi
callq 0x5624
jmp 0x55fc
movq 0x7a53(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x3726(%rip), %rdi      # 0x8cd5
pushq $0x18
jmp 0x55c6
movq 0x7a3e(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x2d2d(%rip), %rdi      # 0x82f1
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x55fc
leaq 0x33a0(%rip), %rdi      # 0x8978
callq 0x2240
jmp 0x55fc
leaq 0x2e4b(%rip), %rdi      # 0x8431
jmp 0x55ef
leaq 0x2e54(%rip), %rdi      # 0x8443
callq 0x2240
movl 0x8(%rbx), %edi
callq 0x2140
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x79e5(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x3723(%rip), %rdi      # 0x8d40
pushq $0x32
jmp 0x558b