// File: /workspace/github_c_makefile_star2_O3/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 0x7d9f
cmpl $0x64, %edi
je 0x7dd7
cmpl $0x73, %edi
jne 0x7e07
movl $0x1, 0x14(%r14)
jmp 0x7dd3
cmpl $0x61, %edi
jne 0x7e0e
movq %rsi, %rbx
leaq 0x8(%rsp), %r15
movq %rsi, %rdi
movq %r15, %rsi
movl $0x10, %edx
callq 0x21a0
movb %al, 0x8(%r14)
movq (%r15), %rcx
cmpb $0x0, (%rcx)
jne 0x7e1a
movzbl %al, %eax
addl $-0x80, %eax
cmpl $-0x80, %eax
jbe 0x7e1a
xorl %eax, %eax
jmp 0x7e41
cmpq $0x0, (%r14)
jne 0x7e37
movq 0x51ec(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x2290
movq %rax, %rcx
movq %rax, (%r14)
xorl %eax, %eax
testq %rcx, %rcx
jne 0x7e41
leaq 0x27a(%rip), %rdi       # 0x807a
callq 0x2240
jmp 0x7e3c
cmpl $0x68, %edi
jne 0x7e41
jmp 0x7e13
cmpl $0x3f, %edi
jne 0x7e41
callq 0x2e18
jmp 0x7e3c
movq 0x51d7(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x222(%rip), %rsi       # 0x804d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2180
jmp 0x7e3c
callq 0x2e36
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq