// File: /workspace/github_c_makefile_star2_O3/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), %rbx
movq 0x29c42(%rip), %r15     # 0x963e0
leaq 0x29c33(%rip), %r12     # 0x963d8
movq %rbx, %rdi
callq 0x8190
xorl %r13d, %r13d
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x85f0
callq 0x6d2f0
movq 0x10(%rax), %rax
incq %rax
movq %rax, %rcx
shrq $0x3b, %rcx
shlq $0x5, %rax
negq %rcx
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rax, %rdi
callq 0x8030
movq %rax, %r15
callq 0x6d2f0
movq (%rax), %rbx
callq 0x6d2f0
cmpq %rax, %rbx
je 0x6c828
xorl %ecx, %ecx
movq %r15, %rdx
incq %rcx
movups 0x10(%rbx), %xmm0
movups 0x20(%rbx), %xmm1
movups %xmm1, 0x10(%rdx)
movups %xmm0, (%rdx)
movq (%rbx), %rbx
addq $0x20, %rdx
cmpq %rax, %rbx
jne 0x6c807
movl %ecx, %r13d
shlq $0x5, %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15,%r13)
movups %xmm0, (%r15,%r13)
leaq 0x4(%rsp), %rax
movl $0x0, (%rax)
leaq 0x18(%rsp), %r12
callq 0x6d30e
movq (%rax), %rdx
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rcx
leaq 0x4(%rsp), %r8
callq 0x8690
movl %eax, %r13d
testl %eax, %eax
je 0x6c907
cmpl $-0x1, %r13d
je 0x6c9e8
cmpl $0x3f, %r13d
je 0x6ca40
callq 0x6d424
movq %rax, %rbx
movq 0x10(%rax), %rax
addq $0x8, %rbx
testq %rax, %rax
je 0x6c8bf
movq %rbx, %rcx
xorl %edx, %edx
cmpb %r13b, 0x20(%rax)
setl %dl
cmovgeq %rax, %rcx
movq 0x10(%rax,%rdx,8), %rax
testq %rax, %rax
jne 0x6c89b
cmpq %rbx, %rcx
je 0x6c8bf
cmpb %r13b, 0x20(%rcx)
cmovleq %rcx, %rbx
callq 0x6d424
addq $0x8, %rax
cmpq %rax, %rbx
je 0x6c972
callq 0x6d424
movb %r13b, 0x18(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x6d442
movq (%rax), %rdi
testq %rdi, %rdi
je 0x6c9d6
movq 0x27697(%rip), %rax     # 0x93f90
movq (%rax), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
jmp 0x6c9d6
movslq 0x4(%rsp), %rax
shlq $0x5, %rax
movq (%r15,%rax), %rsi
movq %r12, %rdi
leaq 0xb(%rsp), %rdx
callq 0x8170
callq 0x6d32c
movq %rax, %rdi
movq %r12, %rsi
callq 0x6d34a
movq (%rax), %rbx
testq %rbx, %rbx
je 0x6c95c
movq 0x27650(%rip), %rax     # 0x93f90
movq (%rax), %rsi
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
leaq 0x29aa5(%rip), %rax     # 0x963f8
cmpq %rax, %rbx
je 0x6ca5e
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x6c9d6
callq 0x8390
jmp 0x6c9d6
movl $0x27, %edx
movq 0x2766a(%rip), %rbx     # 0x93fe8
movq %rbx, %rdi
leaq 0x11613(%rip), %rsi     # 0x7df9b
callq 0x8470
movq %rbx, %rdi
movl %r13d, %esi
callq 0x86d0
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x119e9(%rip), %rsi     # 0x7e393
callq 0x8470
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x83c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8250
movl $0x0, 0x4(%rsp)
cmpl $-0x1, %r13d
jne 0x6c84a
cmpb $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rdi
je 0x6ca27
movl $0x0, (%rdi)
movq 0x275bf(%rip), %rax     # 0x93fc0
movl (%rax), %edx
cmpl %ebp, %edx
jge 0x6ca27
xorl %ecx, %ecx
movslq %edx, %rdx
movq (%r14,%rdx,8), %rsi
movslq %ecx, %rcx
movq %rsi, (%r14,%rcx,8)
incl %edx
movl %edx, (%rax)
movl (%rdi), %ecx
incl %ecx
movl %ecx, (%rdi)
movl (%rax), %edx
cmpl %ebp, %edx
jl 0x6ca09
movq %r15, %rdi
callq 0x84f0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x275a1(%rip), %rdi     # 0x93fe8
callq 0x6c64c
movq %r15, %rdi
callq 0x84f0
movl $0x1, %edi
callq 0x84c0
movq 0x2754b(%rip), %rdi     # 0x93fb0
callq 0x6c64c
movq %r15, %rdi
callq 0x84f0
xorl %edi, %edi
callq 0x84c0
jmp 0x6ca7b
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x6ca92
callq 0x8390
movq %rbx, %rdi
callq 0x8720