// File: /workspace/github_c_makefile_star2_O3/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 0x788a
movq %rdi, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
jne 0x76e3
leaq 0x211d(%rip), %rdi      # 0x97ec
callq 0x2290
movq %rax, %rdi
movq %rax, (%rbx)
testq %rax, %rax
je 0x78da
movl $0x2, %esi
xorl %eax, %eax
callq 0x2210
movl %eax, 0xc(%rbx)
testl %eax, %eax
js 0x7891
leaq 0x10(%rsp), %rdx
movl $0x705, %esi            # imm = 0x705
movl %eax, %edi
xorl %eax, %eax
callq 0x2130
testl %eax, %eax
jne 0x789b
testb $0x1, 0x10(%rsp)
je 0x78a9
movl 0xc(%rbx), %edi
movzbl 0x8(%rbx), %edx
movl $0x703, %esi            # imm = 0x703
xorl %eax, %eax
callq 0x2130
testl %eax, %eax
js 0x78b3
movl $0x1, 0x10(%rbx)
movl 0xc(%rbx), %edi
leaq 0x10(%rsp), %rsi
movw $0x1, (%rsi)
movl $0x2, %edx
callq 0x7eb0
cmpl $0x2, %eax
jne 0x78c6
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22c0
leaq 0x8(%rsp), %rsi
movb $0x1, (%rsi)
movl 0xc(%rbx), %edi
movl $0x1, %edx
callq 0x7eb0
cmpl $0x1, %eax
jne 0x787b
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movaps %xmm0, (%rsi)
movl 0xc(%rbx), %edi
movl $0x10, %edx
callq 0x2150
cmpq $0x10, %rax
jne 0x787b
movaps 0x10(%rsp), %xmm0
andps 0x1928(%rip), %xmm0     # 0x90e0
movaps %xmm0, 0x10(%rsp)
movb $0x0, 0x20(%rsp)
leaq 0x8(%rsp), %rsi
xorl %ebp, %ebp
movq %rbx, %rdi
movl $0x8, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x7e4b
testl %eax, %eax
jne 0x78d0
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x3af9
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 0x1ad9(%rip), %rdi      # 0x92fb
xorl %eax, %eax
callq 0x20f0
movzbl (%r14), %edx
movzbl 0x1(%r14), %ecx
movzbl 0x2(%r14), %r8d
leaq 0x15a5(%rip), %rdi      # 0x8de3
leaq 0x10(%rsp), %rsi
movq %r15, %r9
xorl %eax, %eax
callq 0x20f0
movzwl 0x1a(%rbx), %edx
movzbl 0x18(%rbx), %ecx
leaq 0x15c1(%rip), %rdi      # 0x8e1d
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movzwl 0x1c(%rbx), %edx
leaq 0xb8c(%rip), %rdi       # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x78eb
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x2cb7
jmp 0x78e6
callq 0x2dd1
jmp 0x78e6
movq %rbx, %rdi
callq 0x2d96
jmp 0x78e6
leaq 0x1fe7(%rip), %rdi      # 0x9889
callq 0x2240
jmp 0x78bc
movq %rbx, %rdi
callq 0x2d72
jmp 0x78bc
leaq 0x8(%rbx), %rdi
callq 0x2d39
movl 0xc(%rbx), %edi
callq 0x2140
jmp 0x78e6
movq %r14, %rdi
callq 0x2c76
jmp 0x78e6
movq %r14, %rdi
callq 0x2cf8
jmp 0x78e6
leaq 0x799(%rip), %rdi       # 0x807a
callq 0x2240
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq