// File: /workspace/github_c_makefile_star2_O2/orempel[P]multiboot_tool/twiboot.c
// Function: twi_optarg_cb
static int twi_optarg_cb(int val, const char *arg, void *privdata)
{
    struct twi_privdata *twi = (struct twi_privdata *)privdata;

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

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

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

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

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

        case 'h':
        case '?': /* error */
                fprintf(stderr, "Usage: twiboot [options]\n"
                    "  -a <address>                 - selects i2c address (0x01 - 0x7F)\n"
                    "  -d <device>                  - selects i2c device  (default: /dev/i2c-0)\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: twiboot -a 0x22 -w flash:blmc.hex -w eeprom:blmc_eeprom.hex\n"
                    "\n");
                return -1;

        default:
            return 1;
    }

    return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rdx, %r14
cmpl $0x61, %edi
je 0x6fbf
cmpl $0x64, %edi
je 0x6ff3
cmpl $0x73, %edi
je 0x6fb5
cmpl $0x3f, %edi
je 0x7023
cmpl $0x68, %edi
je 0x7023
pushq $0x1
jmp 0x707d
movl $0x1, 0x14(%r14)
jmp 0x6fec
movq %rsi, %rbx
leaq 0x8(%rsp), %r15
pushq $0x10
popq %rdx
movq %rsi, %rdi
movq %r15, %rsi
callq 0x21a0
movb %al, 0x8(%r14)
movq (%r15), %rcx
cmpb $0x0, (%rcx)
jne 0x7043
movzbl %al, %eax
addl $-0x80, %eax
cmpl $-0x80, %eax
jbe 0x7043
xorl %eax, %eax
jmp 0x707e
cmpq $0x0, (%r14)
jne 0x7059
movq 0x5fd0(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x2290
movq %rax, %rcx
movq %rax, (%r14)
xorl %eax, %eax
testq %rcx, %rcx
jne 0x707e
leaq 0x105e(%rip), %rdi      # 0x807a
callq 0x2240
jmp 0x707b
movq 0x5fce(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x2503(%rip), %rdi      # 0x9537
pushq $0x1
popq %rdx
movl $0x238, %esi            # imm = 0x238
callq 0x2280
jmp 0x707b
movq 0x5fae(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0xff9(%rip), %rsi       # 0x804d
movq %rbx, %rdx
jmp 0x7074
movq 0x5f98(%rip), %rax      # 0xcff8
movq (%rax), %rdi
movq 0x5f66(%rip), %rax      # 0xcfd0
movq (%rax), %rdx
leaq 0xff0(%rip), %rsi       # 0x8064
xorl %eax, %eax
callq 0x2180
pushq $-0x1
popq %rax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq