// File: jaudoux[P]countTags/src/optionparser.h
// Function: option::Parser::workhorse(bool, option::Descriptor const*, int, char const**, option::Parser::Action&, bool, bool, int)
inline bool Parser::workhorse(bool gnu, const Descriptor usage[], int numargs, const char** args, Action& action,
                              bool single_minus_longopt, bool print_errors, int min_abbr_len)
{
  // protect against NULL pointer
  if (args == 0)
    numargs = 0;

  int nonops = 0;

  while (numargs != 0 && *args != 0)
  {
    const char* param = *args; // param can be --long-option, -srto or non-option argument

    // in POSIX mode the first non-option argument terminates the option list
    // a lone minus character is a non-option argument
    if (param[0] != '-' || param[1] == 0)
    {
      if (gnu)
      {
        ++nonops;
        ++args;
        if (numargs > 0)
          --numargs;
        continue;
      }
      else
        break;
    }

    // -- terminates the option list. The -- itself is skipped.
    if (param[1] == '-' && param[2] == 0)
    {
      shift(args, nonops);
      ++args;
      if (numargs > 0)
        --numargs;
      break;
    }

    bool handle_short_options;
    const char* longopt_name;
    if (param[1] == '-') // if --long-option
    {
      handle_short_options = false;
      longopt_name = param + 2;
    }
    else
    {
      handle_short_options = true;
      longopt_name = param + 1; //for testing a potential -long-option
    }

    bool try_single_minus_longopt = single_minus_longopt;
    bool have_more_args = (numargs > 1 || numargs < 0); // is referencing argv[1] valid?

    do // loop over short options in group, for long options the body is executed only once
    {
      int idx;

      const char* optarg;

      /******************** long option **********************/
      if (handle_short_options == false || try_single_minus_longopt)
      {
        idx = 0;
        while (usage[idx].longopt != 0 && !streq(usage[idx].longopt, longopt_name))
          ++idx;

        if (usage[idx].longopt == 0 && min_abbr_len > 0) // if we should try to match abbreviated long options
        {
          int i1 = 0;
          while (usage[i1].longopt != 0 && !streqabbr(usage[i1].longopt, longopt_name, min_abbr_len))
            ++i1;
          if (usage[i1].longopt != 0)
          { // now test if the match is unambiguous by checking for another match
            int i2 = i1 + 1;
            while (usage[i2].longopt != 0 && !streqabbr(usage[i2].longopt, longopt_name, min_abbr_len))
              ++i2;

            if (usage[i2].longopt == 0) // if there was no second match it's unambiguous, so accept i1 as idx
              idx = i1;
          }
        }

        // if we found something, disable handle_short_options (only relevant if single_minus_longopt)
        if (usage[idx].longopt != 0)
          handle_short_options = false;

        try_single_minus_longopt = false; // prevent looking for longopt in the middle of shortopt group

        optarg = longopt_name;
        while (*optarg != 0 && *optarg != '=')
          ++optarg;
        if (*optarg == '=') // attached argument
          ++optarg;
        else
          // possibly detached argument
          optarg = (have_more_args ? args[1] : 0);
      }

      /************************ short option ***********************************/
      if (handle_short_options)
      {
        if (*++param == 0) // point at the 1st/next option character
          break; // end of short option group

        idx = 0;
        while (usage[idx].shortopt != 0 && !instr(*param, usage[idx].shortopt))
          ++idx;

        if (param[1] == 0) // if the potential argument is separate
          optarg = (have_more_args ? args[1] : 0);
        else
          // if the potential argument is attached
          optarg = param + 1;
      }

      const Descriptor* descriptor = &usage[idx];

      if (descriptor->shortopt == 0) /**************  unknown option ********************/
      {
        // look for dummy entry (shortopt == "" and longopt == "") to use as Descriptor for unknown options
        idx = 0;
        while (usage[idx].shortopt != 0 && (usage[idx].shortopt[0] != 0 || usage[idx].longopt[0] != 0))
          ++idx;
        descriptor = (usage[idx].shortopt == 0 ? 0 : &usage[idx]);
      }

      if (descriptor != 0)
      {
        Option option(descriptor, param, optarg);
        switch (descriptor->check_arg(option, print_errors))
        {
          case ARG_ILLEGAL:
            return false; // fatal
          case ARG_OK:
            // skip one element of the argument vector, if it's a separated argument
            if (optarg != 0 && have_more_args && optarg == args[1])
            {
              shift(args, nonops);
              if (numargs > 0)
                --numargs;
              ++args;
            }

            // No further short options are possible after an argument
            handle_short_options = false;

            break;
          case ARG_IGNORE:
          case ARG_NONE:
            option.arg = 0;
            break;
        }

        if (!action.perform(option))
          return false;
      }

    } while (handle_short_options);

    shift(args, nonops);
    ++args;
    if (numargs > 0)
      --numargs;

  } // while

  if (numargs > 0 && *args == 0) // It's a bug in the caller if numargs is greater than the actual number
    numargs = 0; // of arguments, but as a service to the user we fix this if we spot it.

  if (numargs < 0) // if we don't know the number of remaining non-option arguments
  { // we need to count them
    numargs = 0;
    while (args[numargs] != 0)
      ++numargs;
  }

  return action.finished(numargs + nonops, args - nonops);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rcx, -0xb0(%rbp)
movl %edx, %eax
movq -0xb0(%rbp), %rdx
movl %eax, -0xa4(%rbp)
movq %rsi, %rax
movl -0xa4(%rbp), %esi
movq %rax, -0xa0(%rbp)
movl %edi, %eax
movq -0xa0(%rbp), %rdi
movb %r9b, %cl
movb %al, %r9b
movl 0x18(%rbp), %eax
movb 0x10(%rbp), %al
andb $0x1, %r9b
movb %r9b, -0x2(%rbp)
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
andb $0x1, %cl
movb %cl, -0x29(%rbp)
andb $0x1, %al
movb %al, -0x2a(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x5b06
movl $0x0, -0x14(%rbp)
movl $0x0, -0x30(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x14(%rbp)
movb %al, -0xb1(%rbp)
je 0x5b2c
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
setne %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x5b3b
jmp 0x6163
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x5b5f
movq -0x38(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x5b90
testb $0x1, -0x2(%rbp)
je 0x5b8b
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x20(%rbp)
cmpl $0x0, -0x14(%rbp)
jle 0x5b89
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x5b0d
jmp 0x6163
movq -0x38(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2d, %eax
jne 0x5bd6
movq -0x38(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x0, %eax
jne 0x5bd6
movq -0x20(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x6280
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x20(%rbp)
cmpl $0x0, -0x14(%rbp)
jle 0x5bd1
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x6163
movq -0x38(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2d, %eax
jne 0x5bf5
movb $0x0, -0x39(%rbp)
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x48(%rbp)
jmp 0x5c05
movb $0x1, -0x39(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movb -0x29(%rbp), %al
andb $0x1, %al
movb %al, -0x49(%rbp)
movb $0x1, %al
cmpl $0x1, -0x14(%rbp)
movb %al, -0xb2(%rbp)
jg 0x5c28
cmpl $0x0, -0x14(%rbp)
setl %al
movb %al, -0xb2(%rbp)
movb -0xb2(%rbp), %al
andb $0x1, %al
movb %al, -0x4a(%rbp)
movb -0x39(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x5c4a
testb $0x1, -0x49(%rbp)
je 0x5e82
movl $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rcx
movslq -0x50(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, 0x10(%rcx)
movb %al, -0xb3(%rbp)
je 0x5c93
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x62f0
xorb $-0x1, %al
movb %al, -0xb3(%rbp)
movb -0xb3(%rbp), %al
testb $0x1, %al
jne 0x5c9f
jmp 0x5caa
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x5c51
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x10(%rax)
jne 0x5dd2
cmpl $0x0, 0x18(%rbp)
jle 0x5dd2
movl $0x0, -0x5c(%rbp)
movq -0x10(%rbp), %rcx
movslq -0x5c(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, 0x10(%rcx)
movb %al, -0xb4(%rbp)
je 0x5d1b
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x48(%rbp), %rsi
movslq 0x18(%rbp), %rdx
callq 0x6370
xorb $-0x1, %al
movb %al, -0xb4(%rbp)
movb -0xb4(%rbp), %al
testb $0x1, %al
jne 0x5d27
jmp 0x5d32
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x5cd5
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x10(%rax)
je 0x5dd0
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rcx
movslq -0x60(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, 0x10(%rcx)
movb %al, -0xb5(%rbp)
je 0x5d9b
movq -0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x48(%rbp), %rsi
movslq 0x18(%rbp), %rdx
callq 0x6370
xorb $-0x1, %al
movb %al, -0xb5(%rbp)
movb -0xb5(%rbp), %al
testb $0x1, %al
jne 0x5da7
jmp 0x5db2
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x5d55
movq -0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x10(%rax)
jne 0x5dce
movl -0x5c(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x5dd0
jmp 0x5dd2
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x10(%rax)
je 0x5dec
movb $0x0, -0x39(%rbp)
movb $0x0, -0x49(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0xb6(%rbp)
je 0x5e1f
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3d, %eax
setne %al
movb %al, -0xb6(%rbp)
movb -0xb6(%rbp), %al
testb $0x1, %al
jne 0x5e2b
jmp 0x5e39
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x5df8
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3d, %eax
jne 0x5e53
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x5e80
testb $0x1, -0x4a(%rbp)
je 0x5e6a
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xc0(%rbp)
jmp 0x5e75
xorl %eax, %eax
movq %rax, -0xc0(%rbp)
jmp 0x5e75
movq -0xc0(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x5e82
testb $0x1, -0x39(%rbp)
je 0x5f58
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x5ea9
jmp 0x6137
movl $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rcx
movslq -0x50(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, 0x8(%rcx)
movb %al, -0xc1(%rbp)
je 0x5ef7
movq -0x38(%rbp), %rax
movb (%rax), %al
movq -0x10(%rbp), %rcx
movslq -0x50(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq 0x8(%rcx), %rsi
movsbl %al, %edi
callq 0x6430
xorb $-0x1, %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x5f03
jmp 0x5f0e
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x5eb0
movq -0x38(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x5f4a
testb $0x1, -0x4a(%rbp)
je 0x5f32
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xd0(%rbp)
jmp 0x5f3d
xorl %eax, %eax
movq %rax, -0xd0(%rbp)
jmp 0x5f3d
movq -0xd0(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x5f56
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x5f58
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x604c
movl $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rcx
movslq -0x50(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, 0x8(%rcx)
movb %al, -0xd1(%rbp)
je 0x5ff0
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0xd2(%rbp)
jne 0x5fe4
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0xd2(%rbp)
movb -0xd2(%rbp), %al
movb %al, -0xd1(%rbp)
movb -0xd1(%rbp), %al
testb $0x1, %al
jne 0x5ffc
jmp 0x600a
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x5f81
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
jne 0x602b
xorl %eax, %eax
movq %rax, -0xe0(%rbp)
jmp 0x6041
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x612b
movq -0x68(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x58(%rbp), %rcx
leaq -0x98(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x6490
movq -0xf0(%rbp), %rdi
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movzbl -0x2a(%rbp), %esi
andl $0x1, %esi
callq *%rax
movl %eax, %ecx
movq %rcx, -0xe8(%rbp)
subl $0x3, %eax
ja 0x610c
movq -0xe8(%rbp), %rax
leaq 0x8f9a(%rip), %rcx      # 0xf044
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x0, -0x1(%rbp)
jmp 0x61cc
cmpq $0x0, -0x58(%rbp)
je 0x60fe
testb $0x1, -0x4a(%rbp)
je 0x60fe
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x60fe
movq -0x20(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x6280
cmpl $0x0, -0x14(%rbp)
jle 0x60f2
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x20(%rbp)
movb $0x0, -0x39(%rbp)
jmp 0x610c
movq $0x0, -0x78(%rbp)
movq -0x28(%rbp), %rdi
movq (%rdi), %rax
leaq -0x98(%rbp), %rsi
callq *(%rax)
testb $0x1, %al
jne 0x6129
movb $0x0, -0x1(%rbp)
jmp 0x61cc
jmp 0x612b
jmp 0x612d
testb $0x1, -0x39(%rbp)
jne 0x5c33
movq -0x20(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x6280
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x20(%rbp)
cmpl $0x0, -0x14(%rbp)
jle 0x615e
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x5b0d
cmpl $0x0, -0x14(%rbp)
jle 0x617a
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x617a
movl $0x0, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x61a3
movl $0x0, -0x14(%rbp)
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x61a1
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x6187
jmp 0x61a3
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl -0x30(%rbp), %esi
movq -0x20(%rbp), %rdx
movslq -0x30(%rbp), %rcx
xorl %eax, %eax
subq %rcx, %rax
shlq $0x3, %rax
addq %rax, %rdx
movq (%rdi), %rax
callq *0x8(%rax)
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xf0, %rsp
popq %rbp
retq
nopw (%rax,%rax)