// File: /workspace/github_c_makefile_star2_O1/orempel[P]multiboot_tool/twiboot.c
// Function: twi_open
static int twi_open(struct multiboot *mboot)
{
    struct twi_privdata *twi = (struct twi_privdata *)mboot->privdata;

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

    if (twi->device == NULL)
    {
        twi->device = strdup(TWI_DEFAULT_DEVICE);
        if (twi->device == NULL)
        {
            perror("strdup()");
            return -1;
        }
    }

    if (twi_open_device(twi) != 0)
    {
        return -1;
    }

    if (twi_switch_application(twi, BOOTTYPE_BOOTLOADER))
    {
        fprintf(stderr, "failed to switch to bootloader (invalid address?): %s\n",
                strerror(errno));

        twi_close(mboot);
        return -1;
    }

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

    char version[16 +1];
    if (twi_read_version(twi, version, sizeof(version) -1))
    {
        fprintf(stderr, "failed to get bootloader version: %s\n",
                strerror(errno));

        twi_close(mboot);
        return -1;
    }

    version[16] = '\0';

    uint8_t chipinfo[8];
    if (twi_read_memory(twi, chipinfo, sizeof(chipinfo), MEMTYPE_CHIPINFO, 0x0000))
    {
        fprintf(stderr, "failed to get chipinfo: %s\n", strerror(errno));
        twi_close(mboot);
        return -1;
    }

    const char *chipname = chipinfo_get_avr_name(chipinfo);

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

    printf("device         : %-16s (address: 0x%02X)\n",
           twi->device, twi->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",
           twi->flashsize, twi->flashsize, twi->pagesize);

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

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movq 0x8(%rdi), %rbx
cmpb $0x0, 0x8(%rbx)
je 0x78ad
movq %rdi, %r14
cmpq $0x0, (%rbx)
jne 0x7704
leaq 0x208e(%rip), %rdi      # 0x9781
callq 0x2280
movq %rax, (%rbx)
testq %rax, %rax
je 0x78fd
movq (%rbx), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x2200
movl %eax, 0xc(%rbx)
testl %eax, %eax
js 0x78b4
leaq 0x10(%rsp), %rdx
movl $0x705, %esi            # imm = 0x705
movl %eax, %edi
xorl %eax, %eax
callq 0x2130
testl %eax, %eax
jne 0x78be
testb $0x1, 0x10(%rsp)
je 0x78cc
movl 0xc(%rbx), %edi
movzbl 0x8(%rbx), %edx
movl $0x703, %esi            # imm = 0x703
xorl %eax, %eax
callq 0x2130
testl %eax, %eax
js 0x78d6
movl $0x1, 0x10(%rbx)
leaq 0x10(%rsp), %rsi
movw $0x1, (%rsi)
movl 0xc(%rbx), %edi
movl $0x2, %edx
callq 0x7e64
cmpl $0x2, %eax
jne 0x78e9
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22b0
leaq 0x8(%rsp), %rsi
movb $0x1, (%rsi)
movl 0xc(%rbx), %edi
movl $0x1, %edx
callq 0x7e64
cmpl $0x1, %eax
jne 0x789e
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movaps %xmm0, (%rsi)
movl 0xc(%rbx), %edi
movl $0x10, %edx
callq 0x2150
cmpq $0x10, %rax
jne 0x789e
xorl %eax, %eax
andb $0x7f, 0x10(%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x77d2
movb $0x0, 0x20(%rsp)
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
movl $0x8, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x7dff
testl %eax, %eax
jne 0x78f3
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x3b1f
movb 0x3(%r14), %cl
movb %cl, 0x18(%rbx)
movzwl 0x4(%r14), %ecx
rolw $0x8, %cx
movw %cx, 0x1a(%rbx)
movzwl 0x6(%r14), %ecx
rolw $0x8, %cx
movq %rax, %r15
movw %cx, 0x1c(%rbx)
movq (%rbx), %rsi
movzbl 0x8(%rbx), %edx
leaq 0x1a80(%rip), %rdi      # 0x92c3
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
movzbl (%r14), %edx
movzbl 0x1(%r14), %ecx
movzbl 0x2(%r14), %r8d
leaq 0x1570(%rip), %rdi      # 0x8dd1
leaq 0x10(%rsp), %rsi
movq %r15, %r9
xorl %eax, %eax
callq 0x20f0
movzwl 0x1a(%rbx), %edx
movzbl 0x18(%rbx), %ecx
leaq 0x158c(%rip), %rdi      # 0x8e0b
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movzwl 0x1c(%rbx), %edx
leaq 0xb69(%rip), %rdi       # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x790e
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x2cfe
jmp 0x7909
callq 0x2e18
jmp 0x7909
movq %rbx, %rdi
callq 0x2ddd
jmp 0x7909
leaq 0x1f59(%rip), %rdi      # 0x981e
callq 0x2230
jmp 0x78df
movq %rbx, %rdi
callq 0x2db9
jmp 0x78df
leaq 0x8(%rbx), %rdi
callq 0x2d80
movl 0xc(%rbx), %edi
callq 0x2140
jmp 0x7909
movq %r14, %rdi
callq 0x2cbd
jmp 0x7909
movq %r14, %rdi
callq 0x2d3f
jmp 0x7909
leaq 0x776(%rip), %rdi       # 0x807a
callq 0x2230
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq