// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x40, %rsp
movq 0x8(%rdi), %r14
movq (%r14), %rdi
testq %rdi, %rdi
je 0x32ef
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2210
movl %eax, 0x44(%r14)
testl %eax, %eax
js 0x32f9
leaq 0x44(%r14), %rbx
leaq 0x8(%r14), %r15
movl %eax, %edi
movq %r15, %rsi
callq 0x21f0
testl %eax, %eax
js 0x330a
xorps %xmm0, %xmm0
leaq 0x4(%rsp), %r12
movups %xmm0, 0x4(%r12)
movups %xmm0, 0x24(%r12)
movups %xmm0, 0x14(%r12)
movq $0x0, 0x34(%r12)
movl $0x1, (%r12)
movl $0x8b0, 0x8(%r12)       # imm = 0x8B0
movq %r12, %rdi
movl $0x1002, %esi           # imm = 0x1002
callq 0x2230
movq %r12, %rdi
movl $0x1002, %esi           # imm = 0x1002
callq 0x20b0
movw $0x100, 0x16(%r12)      # imm = 0x100
movl (%rbx), %edi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2200
testl %eax, %eax
js 0x3317
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x22c0
movb 0x48(%r14), %al
testb %al, %al
je 0x314b
leaq 0x4(%rsp), %rsi
movb $0x49, (%rsi)
movb %al, 0x1(%rsi)
movl (%rbx), %edi
movl $0x2, %edx
jmp 0x3159
movl (%rbx), %edi
leaq 0x5301(%rip), %rsi      # 0x8455
movl $0x1, %edx
callq 0x2090
movl (%rbx), %edi
leaq 0x4(%rsp), %rsi
movl $0x1, %edx
callq 0x38d2
cmpl $0x1, %eax
jne 0x3325
cmpb $0xd, 0x4(%rsp)
jne 0x3325
movb $0x1, 0x52(%r14)
movl 0x44(%r14), %edi
leaq 0x4ea6(%rip), %rsi      # 0x8039
movl $0x1, %edx
callq 0x2090
movl 0x44(%r14), %edi
leaq 0x4(%rsp), %rsi
movl $0x3, %edx
callq 0x38d2
cmpl $0x3, %eax
jne 0x3332
movb 0x6(%rsp), %al
leaq 0x1(%rsp), %rdi
movb %al, (%rdi)
movzwl 0x4(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x1(%rdi)
callq 0x3abc
testq %rax, %rax
je 0x3344
movq %rax, %r12
movl 0x14(%rax), %eax
movl %eax, 0x4e(%r14)
movl 0x44(%r14), %edi
leaq 0x526a(%rip), %rsi      # 0x845e
movl $0x1, %edx
callq 0x2090
movl 0x44(%r14), %edi
leaq 0x4(%rsp), %rsi
movl $0x3, %edx
callq 0x38d2
cmpl $0x3, %eax
jne 0x3381
movzwl 0x5(%rsp), %eax
rolw $0x8, %ax
xorl %ebp, %ebp
cmpb $0x59, 0x4(%rsp)
cmovnel %ebp, %eax
movw %ax, 0x4c(%r14)
movzbl 0x48(%r14), %esi
testl %esi, %esi
je 0x3249
leaq 0x514e(%rip), %rdi      # 0x8390
xorl %eax, %eax
callq 0x20f0
movq %r12, %rsi
addq $0x3, %rsi
movzbl (%r12), %edx
movzbl 0x1(%r12), %ecx
movzbl 0x2(%r12), %r8d
leaq 0x5141(%rip), %rdi      # 0x83a9
xorl %eax, %eax
callq 0x20f0
movzwl 0x14(%r12), %edx
leaq 0x5161(%rip), %rdi      # 0x83dd
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movzwl 0x16(%r12), %edx
leaq 0x516a(%rip), %rdi      # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
cmpb $0x0, 0x49(%r14)
je 0x33a0
movl (%rbx), %edi
leaq 0x4d91(%rip), %rsi      # 0x8040
movl $0x1, %edx
callq 0x2090
movl (%rbx), %edi
leaq 0x4(%rsp), %rsi
movl $0x1, %edx
callq 0x38d2
cmpl $0x1, %eax
jne 0x3393
cmpb $0xd, 0x4(%rsp)
jne 0x3393
leaq 0x5210(%rip), %rdi      # 0x84f5
callq 0x2080
jmp 0x33a0
callq 0x23e5
jmp 0x339b
leaq 0x568a(%rip), %rdi      # 0x898a
callq 0x2240
jmp 0x339b
movq %rbx, %rdi
callq 0x23cd
jmp 0x339b
leaq 0x5125(%rip), %rdi      # 0x8443
callq 0x2240
jmp 0x3378
movq %rbx, %rdi
movq %r15, %rsi
callq 0x22e0
jmp 0x339b
leaq 0x4(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2321
jmp 0x339b
movq 0x9cad(%rip), %rax      # 0xcff8
movq (%rax), %rdi
movzbl 0x1(%rsp), %edx
movzbl 0x2(%rsp), %ecx
movzbl 0x3(%rsp), %r8d
leaq 0x4fd7(%rip), %rsi      # 0x833c
xorl %eax, %eax
callq 0x2180
movl (%rbx), %edi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2200
movl (%rbx), %edi
callq 0x2140
jmp 0x339b
leaq 0x4(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2362
jmp 0x339b
movq %r14, %rdi
callq 0x23a3
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq