// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movl -0x8(%rbp), %eax
addl $-0x3f, %eax
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x34, %eax
ja 0x2e90
movq -0x30(%rbp), %rax
leaq 0x8289(%rip), %rcx      # 0xb004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x10, %edx
callq 0x2180
movb %al, %cl
movq -0x20(%rbp), %rax
movb %cl, 0x48(%rax)
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x2dc5
movq -0x20(%rbp), %rax
movzbl 0x48(%rax), %eax
cmpl $0x1, %eax
jl 0x2dc5
movq -0x20(%rbp), %rax
movzbl 0x48(%rax), %eax
cmpl $0x7f, %eax
jle 0x2ded
movq 0xd22c(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x8327(%rip), %rsi      # 0xb101
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2ea0
jmp 0x2e99
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
je 0x2e21
movq 0xd1f5(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x8307(%rip), %rsi      # 0xb118
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2ea0
movq 0xd1a8(%rip), %rax      # 0xffd0
movq (%rax), %rdi
callq 0x2250
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x2e59
leaq 0x82e3(%rip), %rdi      # 0xb12e
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2ea0
jmp 0x2e99
movq -0x20(%rbp), %rax
movb $0x1, 0x49(%rax)
jmp 0x2e99
movq -0x20(%rbp), %rax
movb $0x1, 0x4a(%rax)
jmp 0x2e99
movq 0xd182(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x82b7(%rip), %rsi      # 0xb137
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2ea0
movl $0x1, -0x4(%rbp)
jmp 0x2ea0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)