// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
cmpl $0x1, %edi
movq %rdi, -0x38(%rbp)
jle 0x1514
movq %rsi, %r15
movl %edi, %eax
movq %rax, -0x40(%rbp)
movl $0x1, %ebx
movl %edi, %eax
xorl $0x2, %eax
movl %eax, -0x2c(%rbp)
movq (%r15,%rbx,8), %r13
movq %r13, %rdi
callq 0x1060
cmpq $0x2, %rax
jb 0x1541
cmpb $0x2d, (%r13)
jne 0x1541
movq %rax, %r12
callq 0x10a0
movq %rax, %r13
movq %rbx, %rcx
shlq $0x4, %rcx
leaq 0x2d3b(%rip), %rax      # 0x40a0
movq %rcx, -0x48(%rbp)
leaq (%rcx,%rax), %rsi
addq $-0x10, %rsi
movl $0x1, %r14d
leaq 0xc86(%rip), %rdx       # 0x2004
movq %rsi, -0x50(%rbp)
movq (%r13), %rcx
movq (%r15,%rbx,8), %rax
movsbq (%rax,%r14), %rax
testb $0x8, 0x1(%rcx,%rax,2)
je 0x139b
cmpl $0x0, (%rsi)
je 0x13f2
addl $-0x64, %eax
rorl %eax
cmpl $0x7, %eax
ja 0x1541
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
movl $0x1, 0x2cd8(%rip)      # 0x4094
jmp 0x1482
movl $0x1, 0x2cc5(%rip)      # 0x4090
jmp 0x1482
movl $0xffffffff, 0x2c8e(%rip) # imm = 0xFFFFFFFF
jmp 0x1482
leaq 0x377(%rip), %rax       # 0x175d
movq %rax, 0x2c83(%rip)      # 0x4070
jmp 0x1482
movq %rsp, -0x58(%rbp)
movq %rsp, %rdi
addq $-0x10, %rdi
movq %rdi, %rsp
cmpq %r12, %r14
jae 0x145d
movq (%r13), %rax
movq (%r15,%rbx,8), %rcx
movsbq (%rcx,%r14), %rdx
movzwl (%rax,%rdx,2), %esi
andl $0x800, %esi            # imm = 0x800
je 0x145d
leaq (%rcx,%r14), %rsi
incq %rsi
xorl %r8d, %r8d
leaq 0x1(%r8), %rcx
leaq (%r8,%r14), %r9
incq %r9
movb %dl, (%rdi,%r8)
cmpq %r12, %r9
jae 0x1458
movsbq (%rsi,%r8), %rdx
movzwl (%rax,%rdx,2), %r9d
andl $0x800, %r9d            # imm = 0x800
je 0x1458
cmpq $0x9, %r8
movq %rcx, %r8
jb 0x1428
addq %rcx, %r14
jmp 0x145f
xorl %ecx, %ecx
movb $0x0, (%rdi,%rcx)
callq 0x1070
movq -0x50(%rbp), %rsi
movl %eax, (%rsi)
decq %r14
incl 0x2c21(%rip)            # 0x4098
movq -0x58(%rbp), %rsp
leaq 0xb82(%rip), %rdx       # 0x2004
incq %r14
cmpq %r12, %r14
jb 0x1382
movl 0x2c04(%rip), %eax      # 0x4098
movl -0x2c(%rbp), %ecx
orl %eax, %ecx
je 0x1505
leaq 0x2bfe(%rip), %rcx      # 0x40a0
movq -0x48(%rbp), %rdx
cmpl $0x0, -0x10(%rdx,%rcx)
je 0x1541
movq 0x2bb8(%rip), %rcx      # 0x4070
leaq 0x3221(%rip), %rdx      # 0x46e0
movq %rcx, -0x8(%rdx,%rbx,8)
movl 0x2b9e(%rip), %ecx      # 0x4068
movl %ecx, 0x4(%rsi)
movl 0x2bbd(%rip), %ecx      # 0x4090
movl %ecx, 0x8(%rsi)
movl 0x2bb8(%rip), %ecx      # 0x4094
movl %ecx, 0xc(%rsi)
leaq -0x33a(%rip), %rcx      # 0x11ac
movq %rcx, 0x2b83(%rip)      # 0x4070
movl $0x1, 0x2b71(%rip)      # 0x4068
xorl %ecx, %ecx
movl %ecx, 0x2b91(%rip)      # 0x4090
movl %ecx, 0x2b8f(%rip)      # 0x4094
incq %rbx
cmpq -0x40(%rbp), %rbx
jne 0x132b
jmp 0x151a
movl 0x2b7e(%rip), %eax      # 0x4098
testl %eax, %eax
setne %cl
movq -0x38(%rbp), %rsi
leal -0x1(%rsi), %edx
cmpl %edx, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x1545
leaq 0x268(%rip), %rax       # 0x17a0
movq %rax, 0x2b31(%rip)      # 0x4070
jmp 0x154c
xorl %eax, %eax
jmp 0x1551
xorl %eax, %eax
cmpl $0x2, %esi
jg 0x1551
movl $0x1, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq