// File: /workspace/github_c_makefile_star2_O2/orempel[P]multiboot_tool/butterfly_prog.c
// Function: butterfly_open
static int butterfly_open(struct multiboot * p_mboot)
{
    bfly_privdata_t * p_priv = (bfly_privdata_t *)p_mboot->privdata;

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

    if (butterfly_open_device(p_priv) < 0)
    {
        return -1;
    }

    if (butterfly_enter_progmode(p_priv) != 0)
    {
        fprintf(stderr, "failed to enter progmode\n");
        butterfly_close_device(p_priv);
        return -1;
    }

    p_priv->progmode_active = 1;

    uint8_t signature[3];
    if (butterfly_get_signature(p_priv, signature) != 0)
    {
        fprintf(stderr, "failed to get signature\n");
        butterfly_close_device(p_priv);
        return -1;
    }

    const avr_chipinfo_t * p_chipinfo;
    p_chipinfo = chipinfo_get_by_signature(signature);
    if (p_chipinfo == NULL)
    {
        fprintf(stderr, "failed to identify chip signature [0x%02x 0x%02x 0x%02x]\n",
                signature[0], signature[1], signature[2]);
        butterfly_close_device(p_priv);
        return -1;
    }

    p_priv->flashsize   = p_chipinfo->flashsize;
    p_priv->eepromsize  = p_chipinfo->eepromsize;

    if (butterfly_get_buffersize(p_priv, &p_priv->buffersize) != 0)
    {
        fprintf(stderr, "failed to get buffersize\n");
        butterfly_close_device(p_priv);
        return -1;
    }

    if (p_priv->twi_address != 0x00)
    {
        printf("twi address    : 0x%02x\n",
               p_priv->twi_address);
    }

    printf("device         : %-16s (sig: 0x%02x 0x%02x 0x%02x)\n",
           p_chipinfo->name, p_chipinfo->sig[0],
           p_chipinfo->sig[1], p_chipinfo->sig[2]);

    printf("flash size     : 0x%04x / %5d\n",
           p_chipinfo->flashsize, p_chipinfo->flashsize);

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

    if (p_priv->chip_erase)
    {
        if (butterfly_chiperase(p_priv) != 0)
        {
            fprintf(stderr, "failed to chip erase\n");
            butterfly_close_device(p_priv);
            return -1;
        }

        printf("chip erased    : OK\n");
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x40, %rsp
movq 0x8(%rdi), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x26e4
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2210
movl %eax, 0x44(%rbx)
testl %eax, %eax
js 0x2705
leaq 0x8(%rbx), %rsi
movl %eax, %edi
callq 0x21f0
testl %eax, %eax
js 0x2716
xorps %xmm0, %xmm0
leaq 0x4(%rsp), %r14
movups %xmm0, 0x4(%r14)
andq $0x0, 0x34(%r14)
movups %xmm0, 0x24(%r14)
movups %xmm0, 0x14(%r14)
movl $0x1, (%r14)
movl $0x8b0, 0x8(%r14)       # imm = 0x8B0
movq %r14, %rdi
movl $0x1002, %esi           # imm = 0x1002
callq 0x2230
movq %r14, %rdi
movl $0x1002, %esi           # imm = 0x1002
callq 0x20b0
movw $0x100, 0x16(%r14)      # imm = 0x100
movl 0x44(%rbx), %edi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2200
testl %eax, %eax
js 0x271f
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x22c0
movb 0x48(%rbx), %al
testb %al, %al
je 0x2582
leaq 0x4(%rsp), %rsi
movb $0x49, (%rsi)
movb %al, 0x1(%rsi)
movl 0x44(%rbx), %edi
pushq $0x2
jmp 0x258e
movl 0x44(%rbx), %edi
leaq 0x5ec9(%rip), %rsi      # 0x8455
pushq $0x1
popq %rdx
callq 0x2090
movq %rbx, %rdi
callq 0x2cae
testl %eax, %eax
jne 0x2738
movb $0x1, 0x52(%rbx)
movl 0x44(%rbx), %edi
leaq 0x5a87(%rip), %rsi      # 0x8039
pushq $0x1
popq %rdx
callq 0x2090
movl 0x44(%rbx), %edi
leaq 0x4(%rsp), %rsi
pushq $0x3
popq %rdx
callq 0x2cdf
cmpl $0x3, %eax
jne 0x274b
movb 0x6(%rsp), %al
leaq 0x1(%rsp), %rdi
movb %al, (%rdi)
movzwl 0x4(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x1(%rdi)
callq 0x2e78
testq %rax, %rax
je 0x2760
movq %rax, %r14
movl 0x14(%rax), %eax
movl %eax, 0x4e(%rbx)
movl 0x44(%rbx), %edi
leaq 0x5e52(%rip), %rsi      # 0x845e
pushq $0x1
popq %rdx
callq 0x2090
movl 0x44(%rbx), %edi
leaq 0x4(%rsp), %rsi
pushq $0x3
popq %rdx
callq 0x2cdf
cmpl $0x3, %eax
jne 0x278a
movzwl 0x5(%rsp), %eax
rolw $0x8, %ax
xorl %ebp, %ebp
cmpb $0x59, 0x4(%rsp)
cmovnel %ebp, %eax
movw %ax, 0x4c(%rbx)
movzbl 0x48(%rbx), %esi
testl %esi, %esi
je 0x265a
leaq 0x5d3d(%rip), %rdi      # 0x8390
xorl %eax, %eax
callq 0x20f0
movq %r14, %rsi
addq $0x3, %rsi
movzbl (%r14), %edx
movzbl 0x1(%r14), %ecx
movzbl 0x2(%r14), %r8d
leaq 0x5d33(%rip), %rdi      # 0x83a9
xorl %eax, %eax
callq 0x20f0
movzwl 0x14(%r14), %edx
leaq 0x5d54(%rip), %rdi      # 0x83dd
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movzwl 0x16(%r14), %edx
leaq 0x5d5e(%rip), %rdi      # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
cmpb $0x0, 0x49(%rbx)
je 0x27c6
movl 0x44(%rbx), %edi
leaq 0x5985(%rip), %rsi      # 0x8040
pushq $0x1
popq %rdx
callq 0x2090
movq %rbx, %rdi
callq 0x2cae
testl %eax, %eax
jne 0x279f
leaq 0x5e1b(%rip), %rdi      # 0x84f5
callq 0x2080
jmp 0x27c6
movq 0xa90d(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5bfc(%rip), %rdi      # 0x82f1
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x27c3
leaq 0x626c(%rip), %rdi      # 0x8978
callq 0x2240
jmp 0x27c3
leaq 0x5d14(%rip), %rdi      # 0x8431
jmp 0x2726
leaq 0x5d1d(%rip), %rdi      # 0x8443
callq 0x2240
movl 0x44(%rbx), %edi
callq 0x2140
jmp 0x27c3
movq 0xa8b9(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5bc0(%rip), %rdi      # 0x8309
jmp 0x279b
movq 0xa8a6(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5bc7(%rip), %rdi      # 0x8323
pushq $0x18
jmp 0x27b2
movq 0xa891(%rip), %rax      # 0xcff8
movq (%rax), %rdi
movzbl 0x1(%rsp), %edx
movzbl 0x2(%rsp), %ecx
movzbl 0x3(%rsp), %r8d
leaq 0x5bbb(%rip), %rsi      # 0x833c
xorl %eax, %eax
callq 0x2180
jmp 0x27bb
movq 0xa867(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5bdb(%rip), %rdi      # 0x8376
pushq $0x19
jmp 0x27b2
movq 0xa852(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5c6b(%rip), %rdi      # 0x841b
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
movq %rbx, %rdi
callq 0x2c93
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x40, %rsp
popq %rbx
popq %r14
popq %rbp
retq