// File: /workspace/github_c_makefile_star2_O1/ohkimur[P]the-c-programming-language-2nd-edition-solutions/chapter_5/exercise_5_12/entab.c
// Function: main
int main(int argc, char *argv[])
{
  if (!is_arg_list_valid(argc, argv))
  {
    puts("Error: invalid arguments.\n");
    return EXIT_FAILURE;
  }

  int c;
  size_t arg_pos = 1;
  size_t line_pos = 0;
  size_t tab_stop = DEFAULT_TAB_LENGTH;
  size_t nr_of_spaces = 0;
  size_t nr_of_custom_tab_stops;
  size_t initial_nr_of_custom_tab_stops = argc - 1;

  size_t custom_tab_stop = 0;
  size_t custom_line_pos_start = 0;

  for (size_t i = 1; i < argc; i++)
  {
    if (argv[i][0] == '-')
    {
      custom_line_pos_start = atoi(argv[i] + 1);
      --initial_nr_of_custom_tab_stops;
    }
    else if (argv[i][0] == '+')
    {
      custom_tab_stop = atoi(argv[i] + 1);
      --initial_nr_of_custom_tab_stops;
    }
  }

  nr_of_custom_tab_stops = initial_nr_of_custom_tab_stops;

  while ((c = getchar()) != EOF)
  {
    ++line_pos;

    if (c == ' ')
    {
      ++nr_of_spaces;

      if (nr_of_custom_tab_stops)
      {
        tab_stop = atoi(argv[arg_pos]);
      }
      else if (custom_tab_stop)
      {
        tab_stop = custom_tab_stop;
      }
      else if (initial_nr_of_custom_tab_stops)
      {
        tab_stop = 1;
      }

      if ((!custom_line_pos_start && line_pos % tab_stop == 0 && nr_of_spaces > 1) ||
          (custom_line_pos_start && nr_of_spaces == tab_stop) ||
          line_pos <= custom_line_pos_start)
      {
        putchar('\t');

        if (nr_of_custom_tab_stops)
        {
          ++arg_pos;
          --nr_of_custom_tab_stops;
        }

        nr_of_spaces = 0;
      }
    }
    else
    {
      while (nr_of_spaces)
      {
        putchar(' ');
        --nr_of_spaces;
      }

      if (c == '\n')
      {
        arg_pos = 1;
        line_pos = 0;
        nr_of_custom_tab_stops = initial_nr_of_custom_tab_stops;
      }

      putchar(c);
    }
  }

  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
movl %edi, %r15d
callq 0x1346
testl %eax, %eax
je 0x1207
leal -0x1(%r15), %eax
movslq %eax, %r13
xorl %r14d, %r14d
cmpl $0x2, %r15d
jb 0x121e
movslq %r15d, %rbx
movl $0x1, %r15d
xorl %ebp, %ebp
movq $0x0, 0x8(%rsp)
movq (%r12,%r15,8), %rdi
movzbl (%rdi), %eax
cmpl $0x2b, %eax
je 0x11eb
cmpl $0x2d, %eax
jne 0x11fd
incq %rdi
callq 0x1070
movslq %eax, %rbp
jmp 0x11fa
incq %rdi
callq 0x1070
cltq
movq %rax, 0x8(%rsp)
decq %r13
incq %r15
cmpq %r15, %rbx
jne 0x11cd
jmp 0x1229
leaq 0xdf6(%rip), %rdi       # 0x2004
callq 0x1040
movl $0x1, %r14d
jmp 0x1334
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movq %rbp, 0x10(%rsp)
callq 0x1060
cmpl $-0x1, %eax
je 0x1334
movl %eax, %ebp
movq %r12, 0x20(%rsp)
movl $0x8, %ebx
movq %r13, 0x18(%rsp)
movq %r13, %r12
xorl %r13d, %r13d
xorl %r14d, %r14d
movl $0x1, %r15d
incq %r14
cmpl $0x20, %ebp
jne 0x127c
testq %r12, %r12
je 0x12af
movq 0x20(%rsp), %rax
movq (%rax,%r15,8), %rdi
callq 0x1070
movslq %eax, %rbx
jmp 0x12ca
testq %r13, %r13
je 0x1290
movl $0x20, %edi
callq 0x1030
decq %r13
jne 0x1281
cmpl $0xa, %ebp
movl $0x1, %eax
cmoveq %rax, %r15
movl $0x0, %eax
cmoveq %rax, %r14
cmoveq 0x18(%rsp), %r12
movl %ebp, %edi
jmp 0x1319
cmpq $0x0, 0x18(%rsp)
movl $0x1, %eax
cmovneq %rax, %rbx
movq 0x8(%rsp), %rax
testq %rax, %rax
cmovneq %rax, %rbx
movq 0x10(%rsp), %rsi
incq %r13
testq %rsi, %rsi
je 0x12de
cmpq %rbx, %r13
jne 0x12fa
jmp 0x12ff
movq %r14, %rax
xorl %edx, %edx
divq %rbx
testq %rdx, %rdx
setne %al
cmpq $0x2, %r13
setb %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x12ff
cmpq %rsi, %r14
ja 0x1321
cmpq $0x1, %r12
sbbq $-0x1, %r15
subq $0x1, %r12
movl $0x0, %eax
cmovbq %rax, %r12
movl $0x9, %edi
callq 0x1030
xorl %r13d, %r13d
callq 0x1060
movl %eax, %ebp
cmpl $-0x1, %eax
jne 0x125c
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq