// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x25b6
movq 0xda5f(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x8e02(%rip), %rsi      # 0xb3a5
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27e7
movq -0x18(%rbp), %rdi
callq 0x2eb0
cmpl $0x0, %eax
jge 0x25d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27e7
movq -0x18(%rbp), %rdi
callq 0x2fd0
cmpl $0x0, %eax
je 0x260b
movq 0xda13(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x8dce(%rip), %rsi      # 0xb3bd
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x3040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27e7
movq -0x18(%rbp), %rax
movb $0x1, 0x52(%rax)
movq -0x18(%rbp), %rdi
leaq -0x1b(%rbp), %rsi
callq 0x3080
cmpl $0x0, %eax
je 0x2652
movq 0xd9cc(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x8da1(%rip), %rsi      # 0xb3d7
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x3040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27e7
leaq -0x1b(%rbp), %rdi
callq 0x3670
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x26a0
movq 0xd98b(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x1b(%rbp), %edx
movzbl -0x1a(%rbp), %ecx
movzbl -0x19(%rbp), %r8d
leaq 0x8d6c(%rip), %rsi      # 0xb3f0
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x3040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27e7
movq -0x28(%rbp), %rax
movw 0x14(%rax), %cx
movq -0x18(%rbp), %rax
movw %cx, 0x4e(%rax)
movq -0x28(%rbp), %rax
movw 0x16(%rax), %cx
movq -0x18(%rbp), %rax
movw %cx, 0x50(%rax)
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x4c, %rsi
callq 0x3110
cmpl $0x0, %eax
je 0x2703
movq 0xd91b(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x8d43(%rip), %rsi      # 0xb42a
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x3040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27e7
movq -0x18(%rbp), %rax
movzbl 0x48(%rax), %eax
cmpl $0x0, %eax
je 0x2726
movq -0x18(%rbp), %rax
movzbl 0x48(%rax), %esi
leaq 0x8d25(%rip), %rdi      # 0xb444
movb $0x0, %al
callq 0x20d0
movq -0x28(%rbp), %rsi
addq $0x3, %rsi
movq -0x28(%rbp), %rax
movzbl (%rax), %edx
movq -0x28(%rbp), %rax
movzbl 0x1(%rax), %ecx
movq -0x28(%rbp), %rax
movzbl 0x2(%rax), %r8d
leaq 0x8d10(%rip), %rdi      # 0xb45d
movb $0x0, %al
callq 0x20d0
movq -0x28(%rbp), %rax
movzwl 0x14(%rax), %esi
movq -0x28(%rbp), %rax
movzwl 0x14(%rax), %edx
leaq 0x8d26(%rip), %rdi      # 0xb491
movb $0x0, %al
callq 0x20d0
movq -0x28(%rbp), %rax
movzwl 0x16(%rax), %esi
movq -0x28(%rbp), %rax
movzwl 0x16(%rax), %edx
leaq 0x8d27(%rip), %rdi      # 0xb4b0
movb $0x0, %al
callq 0x20d0
movq -0x18(%rbp), %rax
cmpb $0x0, 0x49(%rax)
je 0x27e0
movq -0x18(%rbp), %rdi
callq 0x31b0
cmpl $0x0, %eax
je 0x27d2
movq 0xd849(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x8d16(%rip), %rsi      # 0xb4cf
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x3040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27e7
leaq 0x8d0c(%rip), %rdi      # 0xb4e5
movb $0x0, %al
callq 0x20d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq