// File: /workspace/github_c_makefile_star2_O1/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 0x299ca(%rip), %r15     # 0x963e0
leaq 0x299bb(%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 0x6d56e
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 0x6d56e
movq (%rax), %rbx
callq 0x6d56e
cmpq %rax, %rbx
je 0x6caa0
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 0x6ca7f
movl %ecx, %r13d
shlq $0x5, %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15,%r13)
movups %xmm0, (%r15,%r13)
leaq 0x8(%rsp), %r13
movl $0x0, (%r13)
callq 0x6d58c
movq (%rax), %rdx
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rcx
movq %r13, %r8
callq 0x8690
movl %eax, %ebx
cmpl $-0x1, %eax
je 0x6cc4d
testl %ebx, %ebx
je 0x6cb79
cmpl $0x3f, %ebx
je 0x6ccb6
callq 0x6d6a0
movq %rax, %r12
movq 0x10(%rax), %rcx
addq $0x8, %r12
movq %r12, %rax
testq %rcx, %rcx
je 0x6cb24
movq %r12, %rax
xorl %edx, %edx
cmpb %bl, 0x20(%rcx)
setl %dl
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x6cb0e
cmpq %r12, %rax
je 0x6cb30
cmpb %bl, 0x20(%rax)
cmovleq %rax, %r12
callq 0x6d6a0
addq $0x8, %rax
cmpq %rax, %r12
je 0x6cbe9
callq 0x6d6a0
movb %bl, 0x18(%rsp)
movq %rax, %rdi
leaq 0x18(%rsp), %rsi
callq 0x6d6be
movq (%rax), %rdi
testq %rdi, %rdi
je 0x6cc4d
movq 0x27425(%rip), %rax     # 0x93f90
movq (%rax), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
jmp 0x6cc4d
movslq 0x8(%rsp), %rax
shlq $0x5, %rax
movq (%r15,%rax), %rsi
leaq 0x18(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x8170
callq 0x6d5aa
movq %rax, %rdi
leaq 0x18(%rsp), %rsi
callq 0x6d5c8
movq (%rax), %r12
testq %r12, %r12
je 0x6cbc3
movq 0x273da(%rip), %rax     # 0x93f90
movq (%rax), %rsi
movq (%r12), %rax
movq %r12, %rdi
callq *0x18(%rax)
leaq 0x2982e(%rip), %rax     # 0x963f8
cmpq %rax, %r12
je 0x6ccd4
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x6cc4d
callq 0x8390
jmp 0x6cc4d
movl $0x27, %edx
movq 0x273f3(%rip), %r12     # 0x93fe8
movq %r12, %rdi
leaq 0x1134c(%rip), %rsi     # 0x7df4b
callq 0x8470
movq %r12, %rdi
movl %ebx, %esi
callq 0x86d0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x11723(%rip), %rsi     # 0x7e343
callq 0x8470
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x83c0
movsbl %al, %esi
movq %r12, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8250
movl $0x0, 0x8(%rsp)
cmpl $-0x1, %ebx
jne 0x6cabf
cmpb $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rdi
je 0x6cc9d
movl $0x0, (%rdi)
movq 0x27349(%rip), %rax     # 0x93fc0
movl (%rax), %edx
cmpl %ebp, %edx
jge 0x6cc9d
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 0x6cc7f
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 0x2732b(%rip), %rdi     # 0x93fe8
callq 0x6c8c4
movq %r15, %rdi
callq 0x84f0
movl $0x1, %edi
callq 0x84c0
movq 0x272d5(%rip), %rdi     # 0x93fb0
callq 0x6c8c4
movq %r15, %rdi
callq 0x84f0
xorl %edi, %edi
callq 0x84c0
jmp 0x6ccf1
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x6cd08
callq 0x8390
movq %rbx, %rdi
callq 0x8720