// File: /workspace/github_c_makefile_star2_O2/ohkimur[P]the-c-programming-language-2nd-edition-solutions/chapter_5/exercise_5_17/sort.c
// Function: parse_arg_list
int parse_arg_list(int argc, char *argv[])
{
  for (int i = 1; i < argc; ++i)
  {
    size_t arg_len = strlen(argv[i]);
    if (arg_len > 1 && argv[i][0] == '-')
    {
      for (size_t j = 1; j < arg_len; ++j)
      {
        if (isdigit(argv[i][j]) && !fields_options[i - 1][INDEX])
        {
          char field_index_str[INT_MAX_NR_OF_DIGITS];

          size_t k = 0;
          while (isdigit(argv[i][j]) && j < arg_len && k < INT_MAX_NR_OF_DIGITS)
          {
            field_index_str[k++] = argv[i][j++];
          }
          field_index_str[k] = '\0';

          fields_options[i - 1][INDEX] = atoi(field_index_str);

          --j;
          ++nr_of_fields;
        }
        else
        {
          switch (argv[i][j])
          {
          case 'n':
            comp = numcmp;
            break;

          case 'f':
            fold = 1;
            break;

          case 'd':
            directory = 1;
            break;

          case 'r':
            order = -1;
            break;

          default:
            return 0;
            break;
          }
        }
      }

      if (nr_of_fields || argc > 2)
      {
        if (!fields_options[i - 1][INDEX])
        {
          return 0;
        }

        fields_comp[i - 1] = comp;
        fields_options[i - 1][ORDER] = order;
        fields_options[i - 1][FOLD] = fold;
        fields_options[i - 1][DIRECTORY] = directory;

        comp = estrcmp;
        order = 1;
        fold = 0;
        directory = 0;
      }
    }
    else
    {
      return 0;
    }
  }

  if (nr_of_fields && nr_of_fields == argc - 1)
  {
    comp = fieldscmp;
  }
  else if (argc > 2)
  {
    return 0;
  }

  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movl %edi, %ebx
movslq %edi, %rax
movq %rax, -0x48(%rbp)
pushq $0x1
popq %r15
movq %rbx, -0x40(%rbp)
cmpq -0x48(%rbp), %r15
jge 0x14d1
movq (%r14,%r15,8), %r13
movq %r13, %rdi
callq 0x1060
movl $0x0, -0x2c(%rbp)
cmpq $0x2, %rax
jb 0x150c
cmpb $0x2d, (%r13)
jne 0x150c
movq %rax, %r12
movq %r15, %rax
shlq $0x4, %rax
leaq 0x2d3f(%rip), %rcx      # 0x40a0
addq %rcx, %rax
addq $-0x10, %rax
movq %rax, -0x38(%rbp)
pushq $0x1
popq %rbx
leaq 0xc8e(%rip), %r13       # 0x2004
cmpq %r12, %rbx
jae 0x1456
callq 0x10a0
movq (%rax), %rdx
movq (%r14,%r15,8), %rcx
movsbq (%rcx,%rbx), %rcx
testb $0x8, 0x1(%rdx,%rcx,2)
je 0x13a0
movq -0x38(%rbp), %rdx
cmpl $0x0, (%rdx)
je 0x13ec
addl $-0x64, %ecx
rorl %ecx
cmpl $0x7, %ecx
ja 0x150c
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movl $0x1, 0x2cd2(%rip)      # 0x4094
jmp 0x144e
movl $0x1, 0x2cbf(%rip)      # 0x4090
jmp 0x144e
orl $-0x1, 0x2c8e(%rip)     # 0x4068
jmp 0x144e
leaq 0x316(%rip), %rax       # 0x16f9
movq %rax, 0x2c86(%rip)      # 0x4070
jmp 0x144e
movq %rsp, %r13
movq %rsp, %rdi
addq $-0x10, %rdi
movq %rdi, %rsp
movq (%rax), %rax
movq (%r14,%r15,8), %rcx
xorl %edx, %edx
cmpq %r12, %rbx
jae 0x142c
movsbq (%rcx,%rbx), %rsi
movzwl (%rax,%rsi,2), %r8d
andl $0x800, %r8d            # imm = 0x800
je 0x142c
cmpq $0x9, %rdx
ja 0x142c
incq %rbx
movb %sil, (%rdi,%rdx)
incq %rdx
jmp 0x1402
movb $0x0, (%rdi,%rdx)
callq 0x1070
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
decq %rbx
incl 0x2c54(%rip)            # 0x4098
movq %r13, %rsp
leaq 0xbb6(%rip), %r13       # 0x2004
incq %rbx
jmp 0x1376
movq -0x40(%rbp), %rbx
cmpl $0x2, %ebx
jg 0x1468
cmpl $0x0, 0x2c32(%rip)      # 0x4098
je 0x14c9
movq -0x38(%rbp), %rdx
cmpl $0x0, (%rdx)
je 0x150c
movq 0x2bf4(%rip), %rax      # 0x4070
leaq 0x325d(%rip), %rcx      # 0x46e0
movq %rax, -0x8(%rcx,%r15,8)
movl 0x2bda(%rip), %eax      # 0x4068
movl %eax, 0x4(%rdx)
movl 0x2bf9(%rip), %eax      # 0x4090
movl %eax, 0x8(%rdx)
movl 0x2bf4(%rip), %eax      # 0x4094
movl %eax, 0xc(%rdx)
leaq -0x2fe(%rip), %rax      # 0x11ac
movq %rax, 0x2bbf(%rip)      # 0x4070
andl $0x0, 0x2bd8(%rip)      # 0x4090
movl $0x1, 0x2ba6(%rip)      # 0x4068
andl $0x0, 0x2bcb(%rip)      # 0x4094
incq %r15
jmp 0x131e
movl 0x2bc1(%rip), %eax      # 0x4098
testl %eax, %eax
setne %cl
leal -0x1(%rbx), %edx
cmpl %edx, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x14fa
leaq 0x24b(%rip), %rax       # 0x173c
movq %rax, 0x2b78(%rip)      # 0x4070
jmp 0x1506
movl $0x0, -0x2c(%rbp)
cmpl $0x2, %ebx
jg 0x150c
pushq $0x1
popq %rax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq