// File: /workspace/github_c_makefile_star2_O2/orempel[P]multiboot_tool/butterfly_prog.c
// Function: bfly_optarg_cb
static int bfly_optarg_cb(int val, const char *arg, void *privdata)
{
    bfly_privdata_t * p_priv;

    p_priv = (bfly_privdata_t *)privdata;

    switch (val)
    {
        case 'a': /* address */
            {
                char *endptr;

                p_priv->twi_address = strtol(arg, &endptr, 16);
                if ((*endptr != '\0') ||
                    (p_priv->twi_address < 0x01) ||
                    (p_priv->twi_address > 0x7F)
                   )
                {
                    fprintf(stderr, "invalid address: '%s'\n", arg);
                    return -1;
                }
            }
            break;

        case 'd': /* device */
            if (p_priv->p_device != NULL)
            {
                fprintf(stderr, "invalid device: '%s'\n", arg);
                return -1;
            }

            p_priv->p_device = strdup(optarg);
            if (p_priv->p_device == NULL)
            {
                perror("strdup()");
                return -1;
            }
            break;

        case 'e': /* chip erase */
            p_priv->chip_erase = 1;
            break;

        case 's': /* stay in bootloader */
            p_priv->stay_in_bootloader = 1;
            break;

        case 'h':
        case '?': /* error */
            fprintf(stderr, "Usage: butterfly_prog [options]\n"
            "  -a <address>                 - optional: twi address for twiboot bridge mode\n"
            "  -d <device>                  - selects butterfly serial device\n"
            "  -e                           - executes a chip erase\n"
            "  -s                           - stay in bootloader afterwards\n"
            "  -r <flash|eeprom>:<file>     - reads flash/eeprom to file   (.bin | .hex | -)\n"
            "  -w <flash|eeprom>:<file>     - write flash/eeprom from file (.bin | .hex)\n"
            "  -n                           - disable verify after write\n"
            "  -p <0|1|2>                   - progress bar mode\n"
            "\n"
            "Example: butterfly_prog -d /dev/ttyUSB0 -w flash:code.hex\n"
            "\n");

            return -1;

        default:
            return 1;
    }

    return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rdx, %r14
pushq $0x1
popq %rax
leal -0x61(%rdi), %ecx
cmpl $0x7, %ecx
ja 0x2c01
movq %rsi, %rbx
leaq 0x544c(%rip), %rdx      # 0x8004
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
leaq 0x8(%rsp), %r15
pushq $0x10
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x21a0
movb %al, 0x48(%r14)
movq (%r15), %rcx
cmpb $0x0, (%rcx)
jne 0x2beb
movzbl %al, %eax
addl $-0x80, %eax
cmpl $-0x80, %eax
ja 0x2c67
movq 0xa406(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x5451(%rip), %rsi      # 0x804d
jmp 0x2c84
cmpl $0x73, %edi
je 0x2c62
cmpl $0x3f, %edi
jne 0x2c69
movq 0xa3e6(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5467(%rip), %rdi      # 0x8083
pushq $0x1
popq %rdx
movl $0x26d, %esi            # imm = 0x26D
callq 0x2280
jmp 0x2c8e
cmpq $0x0, (%r14)
jne 0x2c73
movq 0xa398(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x2290
movq %rax, %rcx
movq %rax, (%r14)
xorl %eax, %eax
testq %rcx, %rcx
jne 0x2c69
leaq 0x5426(%rip), %rdi      # 0x807a
callq 0x2240
jmp 0x2c8e
movb $0x1, 0x49(%r14)
jmp 0x2c67
movb $0x1, 0x4a(%r14)
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq 0xa37e(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x53e0(%rip), %rsi      # 0x8064
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2180
pushq $-0x1
popq %rax
jmp 0x2c69