// File: /workspace/github_c_makefile_star2_O1/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 %rbx
subq $0x58, %rsp
movq 0x8(%rdi), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x335b
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2200
movl %eax, 0x44(%rbx)
testl %eax, %eax
js 0x3362
leaq 0x44(%rbx), %r14
leaq 0x8(%rbx), %rsi
movl %eax, %edi
callq 0x21e0
testl %eax, %eax
js 0x3370
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r15
movaps %xmm0, (%r15)
movups %xmm0, 0x2c(%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
movl $0x1, (%r15)
movl $0x8b0, 0x8(%r15)       # imm = 0x8B0
movq %r15, %rdi
movl $0x1002, %esi           # imm = 0x1002
callq 0x2220
movq %r15, %rdi
movl $0x1002, %esi           # imm = 0x1002
callq 0x20b0
movw $0x100, 0x16(%r15)      # imm = 0x100
movl (%r14), %edi
xorl %esi, %esi
movq %r15, %rdx
callq 0x21f0
movl %eax, %r15d
testl %eax, %eax
js 0x337a
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x22b0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %r15d, %r15d
js 0x33d2
movb 0x48(%rbx), %al
testb %al, %al
je 0x31ba
leaq 0x10(%rsp), %rsi
movb $0x49, (%rsi)
movb %al, 0x1(%rsi)
movl (%r14), %edi
movl $0x2, %edx
jmp 0x31c9
movl (%r14), %edi
leaq 0x5291(%rip), %rsi      # 0x8455
movl $0x1, %edx
callq 0x2090
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
movl $0x1, %edx
callq 0x38d7
cmpl $0x1, %eax
jne 0x3387
cmpb $0xd, 0x10(%rsp)
jne 0x3387
movb $0x1, 0x52(%rbx)
movl 0x44(%rbx), %edi
leaq 0x4e37(%rip), %rsi      # 0x8039
movl $0x1, %edx
callq 0x2090
movl 0x44(%rbx), %edi
leaq 0x10(%rsp), %rsi
movl $0x3, %edx
callq 0x38d7
cmpl $0x3, %eax
jne 0x3394
movb 0x12(%rsp), %al
leaq 0xd(%rsp), %rdi
movb %al, (%rdi)
movzwl 0x10(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x1(%rdi)
callq 0x3ad8
testq %rax, %rax
je 0x33a6
movq %rax, %r15
movzwl 0x14(%rax), %eax
movw %ax, 0x4e(%rbx)
movzwl 0x16(%r15), %eax
movw %ax, 0x50(%rbx)
movl 0x44(%rbx), %edi
leaq 0x51f3(%rip), %rsi      # 0x845e
movl $0x1, %edx
callq 0x2090
movl 0x44(%rbx), %edi
leaq 0x10(%rsp), %rsi
movl $0x3, %edx
callq 0x38d7
cmpl $0x3, %eax
jne 0x32a3
movzwl 0x11(%rsp), %ecx
rolw $0x8, %cx
xorl %edx, %edx
cmpb $0x59, 0x10(%rsp)
cmovnel %edx, %ecx
movw %cx, 0x4c(%rbx)
cmpl $0x3, %eax
jne 0x33b8
movzbl 0x48(%rbx), %esi
testl %esi, %esi
je 0x32c2
leaq 0x50d5(%rip), %rdi      # 0x8390
xorl %eax, %eax
callq 0x20f0
movq %r15, %rsi
addq $0x3, %rsi
movzbl (%r15), %edx
movzbl 0x1(%r15), %ecx
movzbl 0x2(%r15), %r8d
leaq 0x50cb(%rip), %rdi      # 0x83a9
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
movzwl 0x14(%r15), %edx
leaq 0x50ea(%rip), %rdi      # 0x83dd
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movzwl 0x16(%r15), %edx
leaq 0x50f4(%rip), %rdi      # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
cmpb $0x0, 0x49(%rbx)
je 0x33d2
movl (%r14), %edi
leaq 0x4d1b(%rip), %rsi      # 0x8040
movl $0x1, %edx
callq 0x2090
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
movl $0x1, %edx
callq 0x38d7
cmpl $0x1, %eax
jne 0x33c5
cmpb $0xd, 0x10(%rsp)
jne 0x33c5
leaq 0x51a1(%rip), %rdi      # 0x84f5
callq 0x2080
jmp 0x33d2
callq 0x244a
jmp 0x33cd
leaq 0x560f(%rip), %rdi      # 0x8978
callq 0x2230
jmp 0x33cd
movq %r14, %rdi
callq 0x2432
jmp 0x33cd
movq %r14, %rdi
callq 0x22d0
jmp 0x3190
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22e9
jmp 0x33d2
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x232e
jmp 0x33cd
leaq 0xd(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x23e2
jmp 0x33cd
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2373
jmp 0x33cd
movq %rbx, %rdi
callq 0x23b8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq