// File: /workspace/github_c_makefile_star2_O1/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
movl %edi, %ebx
cmpl $0x2, %edi
setge %al
jl 0x158c
movq %rsi, %r14
movb %al, -0x29(%rbp)
movq 0x8(%rsi), %r12
movq %r12, %rdi
callq 0x1060
cmpq $0x2, %rax
jb 0x1586
movq %rax, %r15
movl %ebx, %eax
movq %rax, -0x48(%rbp)
movq %r14, -0x50(%rbp)
leaq 0x8(%r14), %r13
movl $0x1, %eax
movq %rax, -0x38(%rbp)
movq %rbx, -0x40(%rbp)
cmpb $0x2d, (%r12)
jne 0x1586
callq 0x10a0
leaq 0xc77(%rip), %rsi       # 0x2004
movq %rax, %r12
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
leaq 0x2d01(%rip), %rax      # 0x40a0
movq %rcx, -0x58(%rbp)
leaq (%rcx,%rax), %r14
addq $-0x10, %r14
movl $0x1, %ebx
movb $0x1, %al
movq (%r12), %rdx
movq (%r13), %rcx
movsbq (%rcx,%rbx), %rcx
testb $0x8, 0x1(%rdx,%rcx,2)
je 0x13cc
cmpl $0x0, (%r14)
je 0x1423
addl $-0x64, %ecx
rorl %ecx
cmpl $0x7, %ecx
ja 0x157a
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
movl $0x1, 0x2ca7(%rip)      # 0x4094
jmp 0x14b0
movl $0x1, 0x2c94(%rip)      # 0x4090
jmp 0x14b0
movl $0xffffffff, 0x2c5d(%rip) # imm = 0xFFFFFFFF
jmp 0x14b0
leaq 0x3b0(%rip), %rax       # 0x17c7
movq %rax, 0x2c52(%rip)      # 0x4070
jmp 0x14b0
movq %rsp, -0x60(%rbp)
movq %rsp, %rdi
addq $-0x10, %rdi
movq %rdi, %rsp
cmpq %r15, %rbx
jae 0x148e
movq (%r12), %rax
movq (%r13), %rcx
movsbq (%rcx,%rbx), %rdx
movzwl (%rax,%rdx,2), %esi
andl $0x800, %esi            # imm = 0x800
je 0x148e
leaq (%rcx,%rbx), %rsi
incq %rsi
xorl %r8d, %r8d
leaq 0x1(%r8), %rcx
leaq (%r8,%rbx), %r9
incq %r9
movb %dl, (%rdi,%r8)
cmpq %r15, %r9
jae 0x1489
movsbq (%rsi,%r8), %rdx
movzwl (%rax,%rdx,2), %r9d
andl $0x800, %r9d            # imm = 0x800
je 0x1489
cmpq $0x9, %r8
movq %rcx, %r8
jb 0x1459
addq %rcx, %rbx
jmp 0x1490
xorl %ecx, %ecx
movb $0x0, (%rdi,%rcx)
callq 0x1070
movl %eax, (%r14)
decq %rbx
incl 0x2bf3(%rip)            # 0x4098
movq -0x60(%rbp), %rsp
leaq 0xb54(%rip), %rsi       # 0x2004
incq %rbx
cmpq %r15, %rbx
setb %al
jb 0x13b2
movq -0x40(%rbp), %rbx
cmpl $0x2, %ebx
jne 0x14d1
cmpl $0x0, 0x2bc9(%rip)      # 0x4098
je 0x1542
leaq 0x2bc8(%rip), %rax      # 0x40a0
movq -0x58(%rbp), %rcx
cmpl $0x0, -0x10(%rcx,%rax)
je 0x1586
movq 0x2b82(%rip), %rax      # 0x4070
movq -0x38(%rbp), %rcx
leaq 0x31e7(%rip), %rdx      # 0x46e0
movq %rax, -0x8(%rdx,%rcx,8)
movl 0x2b64(%rip), %eax      # 0x4068
movl %eax, 0x4(%r14)
movl 0x2b82(%rip), %eax      # 0x4090
movl %eax, 0x8(%r14)
movl 0x2b7c(%rip), %eax      # 0x4094
movl %eax, 0xc(%r14)
leaq -0x377(%rip), %rax      # 0x11ac
movq %rax, 0x2b46(%rip)      # 0x4070
movl $0x1, 0x2b34(%rip)      # 0x4068
xorl %eax, %eax
movl %eax, 0x2b54(%rip)      # 0x4090
movl %eax, 0x2b52(%rip)      # 0x4094
movq -0x38(%rbp), %rcx
incq %rcx
cmpq -0x48(%rbp), %rcx
movq -0x50(%rbp), %rax
je 0x158c
setb -0x29(%rbp)
movq %rcx, -0x38(%rbp)
leaq (%rax,%rcx,8), %r13
movq (%r13), %r12
movq %r12, %rdi
callq 0x1060
movq %rax, %r15
cmpq $0x2, %rax
jae 0x1376
jmp 0x1586
testb $0x1, %al
je 0x14bf
movq -0x40(%rbp), %rbx
testb $0x1, -0x29(%rbp)
jne 0x15ba
movl 0x2b06(%rip), %eax      # 0x4098
testl %eax, %eax
setne %cl
leal -0x1(%rbx), %edx
cmpl %edx, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x15b5
leaq 0x25e(%rip), %rax       # 0x180a
movq %rax, 0x2abd(%rip)      # 0x4070
jmp 0x15be
cmpl $0x2, %ebx
jle 0x15be
xorl %eax, %eax
jmp 0x15c3
movl $0x1, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq