// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/flags.cpp
// Function: flag::Flag::parse(int*, char***, bool)
int Flag::parse(int * p_argc, char *** p_argv, const bool remove_flags) {
  int argc = *p_argc;
  char ** argv = *p_argv;

  // Grab the program name from argv[0] for --help output later.
  prog_name = argv[0];

  // Put the options into an array, as getopt expects them.
  struct option * options = new struct option[Flag::options().size() + 1];
  int x = 0;
  typedef list<struct option>::iterator iter;
  for (iter i = Flag::options().begin(), e = Flag::options().end(); i != e; ++i) {
    options[x++] = *i;
  }
  // This sentinel is needed to prevent the getopt library from segfaulting
  // when an unknown long option is seen first on the list of flags.
  struct option sentinel = {0, 0, 0, 0};
  options[x] = sentinel;

  // Parse the arguments.
  for (int c = 0, index = 0; c != -1; index = 0) {
    c = getopt_long(argc, argv, Flag::codes().c_str(), options, &index);
    switch (c) {
      case -1: break;

      case 0: {  // longopt with no short name.
        const string long_name = options[index].name;
        Flag * flag = Flag::long_names()[long_name];
        if (flag) flag -> set(optarg);
        if (flag == &FLAGS_OPT_help) {
          Flag::show_help(cout);
          delete [] options;
          exit(0);
        }
        break;
      }

      case '?': {  // unknown option.
        Flag::show_help(cerr);
        delete [] options;
        exit(1);
      }

      default: {  // short option
        if (Flag::short_names().find(c) == Flag::short_names().end()) {
          // This should never happen.
          cerr << "ERROR: failed to find a flag matching '" << c << "'" << endl;
        } else {
          Flag * flag = Flag::short_names()[c];
          if (flag) flag -> set(optarg);
        }
        break;
      }
    }
  }

  // Trim the non-argument params from argv.
  if (remove_flags) {
    for (*p_argc = 0; optind < argc; ++optind, ++(*p_argc)) {
      argv[*p_argc] = argv[optind];
    }
  }

  delete [] options;
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, 0xc(%rsp)
movq %rdi, 0x10(%rsp)
movl (%rdi), %ebp
movq (%rsi), %r14
movq (%r14), %rsi
leaq 0x29a0c(%rip), %rdi     # 0x893d8
callq 0x87a0
callq 0x602bc
movq 0x10(%rax), %rax
incq %rax
movq %rax, %rcx
shrq $0x3b, %rcx
shlq $0x5, %rax
xorl %ebx, %ebx
negq %rcx
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rax, %rdi
callq 0x8030
movq %rax, %r15
callq 0x602bc
movq (%rax), %r12
callq 0x602bc
movq %r15, %rcx
cmpq %rax, %r12
je 0x5fa35
incq %rbx
movups 0x10(%r12), %xmm0
movups 0x20(%r12), %xmm1
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
movq (%r12), %r12
addq $0x20, %rcx
jmp 0x5fa10
movl %ebx, %eax
shlq $0x5, %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15,%rax)
movups %xmm0, (%r15,%rax)
xorl %r13d, %r13d
leaq 0x18(%rsp), %r12
andl $0x0, 0x8(%rsp)
cmpl $-0x1, %r13d
je 0x5fb8d
callq 0x60322
movq (%rax), %rdx
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rcx
leaq 0x8(%rsp), %r8
callq 0x8670
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x5fa51
testl %r13d, %r13d
je 0x5faf1
cmpl $0x3f, %r13d
je 0x5fbda
callq 0x6049a
movb %r13b, 0x18(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x60b46
movq %rax, %rbx
callq 0x6049a
addq $0x8, %rax
cmpq %rax, %rbx
je 0x5fb53
callq 0x6049a
movb %r13b, 0x18(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x60516
movq (%rax), %rdi
testq %rdi, %rdi
je 0x5fa51
movq 0x274b5(%rip), %rax     # 0x86f98
movq (%rax), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
jmp 0x5fa51
movslq 0x8(%rsp), %rax
shlq $0x5, %rax
movq (%r15,%rax), %rsi
movq %r12, %rdi
leaq 0x7(%rsp), %rdx
callq 0x8150
callq 0x6038e
movq %rax, %rdi
movq %r12, %rsi
callq 0x6040a
movq (%rax), %rbx
testq %rbx, %rbx
je 0x5fb46
movq 0x2746e(%rip), %rax     # 0x86f98
movq (%rax), %rsi
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
leaq 0x298bb(%rip), %rax     # 0x893f8
cmpq %rax, %rbx
je 0x5fbf6
movq %r12, %rdi
callq 0x84b0
jmp 0x5fa51
movq 0x27496(%rip), %rdi     # 0x86ff0
leaq 0x103e0(%rip), %rsi     # 0x6ff41
callq 0x83a0
movq %rax, %rdi
movl %r13d, %esi
callq 0x86a0
movq %rax, %rdi
leaq 0x10794(%rip), %rsi     # 0x7030f
callq 0x83a0
movq %rax, %rdi
callq 0x81a0
jmp 0x5fa51
cmpb $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rdi
je 0x5fbc1
xorl %ecx, %ecx
movq 0x27426(%rip), %rax     # 0x86fc8
movl %ecx, (%rdi)
movslq (%rax), %rdx
cmpl %ebp, %edx
jge 0x5fbc1
movq (%r14,%rdx,8), %rsi
movslq %ecx, %rcx
movq %rsi, (%r14,%rcx,8)
leal 0x1(%rdx), %ecx
movl %ecx, (%rax)
movl (%rdi), %ecx
incl %ecx
jmp 0x5fba2
movq %r15, %rdi
callq 0x84d0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2740f(%rip), %rdi     # 0x86ff0
callq 0x5f8ec
movq %r15, %rdi
callq 0x84d0
pushq $0x1
popq %rdi
callq 0x8490
movq 0x273bb(%rip), %rdi     # 0x86fb8
callq 0x5f8ec
movq %r15, %rdi
callq 0x84d0
xorl %edi, %edi
callq 0x8490
jmp 0x5fc13
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x84b0
movq %rbx, %rdi
callq 0x86f0
movq %rax, %rdi
callq 0x5c505