// File: /workspace/github_c_makefile_star2_O1/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
movl $0x1, %eax
cmpl $0x63, %edi
jle 0x7d53
cmpl $0x64, %edi
je 0x7d8b
cmpl $0x73, %edi
jne 0x7dbb
movl $0x1, 0x14(%r14)
jmp 0x7d87
cmpl $0x61, %edi
jne 0x7dc2
movq %rsi, %rbx
leaq 0x8(%rsp), %r15
movq %rsi, %rdi
movq %r15, %rsi
movl $0x10, %edx
callq 0x2190
movb %al, 0x8(%r14)
movq (%r15), %rcx
cmpb $0x0, (%rcx)
jne 0x7dce
movzbl %al, %eax
addl $-0x80, %eax
cmpl $-0x80, %eax
jbe 0x7dce
xorl %eax, %eax
jmp 0x7df5
cmpq $0x0, (%r14)
jne 0x7deb
movq 0x5238(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x2280
movq %rax, %rcx
movq %rax, (%r14)
xorl %eax, %eax
testq %rcx, %rcx
jne 0x7df5
leaq 0x2c6(%rip), %rdi       # 0x807a
callq 0x2230
jmp 0x7df0
cmpl $0x68, %edi
jne 0x7df5
jmp 0x7dc7
cmpl $0x3f, %edi
jne 0x7df5
callq 0x2e5f
jmp 0x7df0
movq 0x5223(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x26e(%rip), %rsi       # 0x804d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2170
jmp 0x7df0
callq 0x2e7d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq