// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, %r15
movl %edi, %r14d
callq 0x1379
testl %eax, %eax
je 0x135b
leal -0x1(%r14), %eax
movslq %eax, %r12
movslq %r14d, %rbx
pushq $0x1
popq %r14
movq $0x0, 0x28(%rsp)
movq $0x0, 0x18(%rsp)
cmpq %rbx, %r14
jae 0x120b
movq (%r15,%r14,8), %rdi
movzbl (%rdi), %eax
cmpl $0x2b, %eax
je 0x11f4
cmpl $0x2d, %eax
jne 0x1206
incq %rdi
callq 0x1070
cltq
movq %rax, 0x18(%rsp)
jmp 0x1203
incq %rdi
callq 0x1070
cltq
movq %rax, 0x28(%rsp)
decq %r12
incq %r14
jmp 0x11cd
movq %r15, 0x30(%rsp)
pushq $0x8
popq %rbp
pushq $0x1
popq %r14
movq %r14, 0x10(%rsp)
xorl %r15d, %r15d
movq %r12, 0x20(%rsp)
movq %r12, 0x8(%rsp)
movq %r15, %r13
xorl %ebx, %ebx
callq 0x1060
cmpl $-0x1, %eax
je 0x1357
movl %eax, %r12d
leaq (%r14,%r13), %r15
cmpl $0x20, %eax
jne 0x12ce
cmpq $0x0, 0x8(%rsp)
je 0x126c
movq 0x30(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1070
movslq %eax, %rbp
jmp 0x1285
cmpq $0x0, 0x20(%rsp)
pushq $0x1
popq %rax
cmovneq %rax, %rbp
movq 0x28(%rsp), %rax
testq %rax, %rax
cmovneq %rax, %rbp
cmpq $0x0, 0x18(%rsp)
je 0x12a2
cmpq %rbp, %r14
je 0x1325
cmpq 0x18(%rsp), %r15
ja 0x12c3
jmp 0x1325
movq %r15, %rax
xorl %edx, %edx
divq %rbp
testq %rdx, %rdx
setne %al
cmpq $0x2, %r14
setb %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x1325
testq %r15, %r15
je 0x1325
incq %r14
decq %rbx
jmp 0x122e
movq 0x20(%rsp), %r14
pushq $0x20
popq %r13
testq %rbx, %rbx
je 0x12e9
movl %r13d, %edi
callq 0x1030
incq %rbx
jmp 0x12d7
cmpl $0xa, %r12d
pushq $0x1
popq %rax
movq 0x10(%rsp), %rcx
cmoveq %rax, %rcx
movq %rcx, 0x10(%rsp)
movl $0x0, %ecx
cmoveq %rcx, %r15
movq 0x8(%rsp), %rcx
cmoveq %r14, %rcx
movq %rcx, 0x8(%rsp)
movq %rax, %r14
movl %r12d, %edi
callq 0x1030
jmp 0x1229
movq 0x8(%rsp), %rcx
cmpq $0x1, %rcx
sbbq $-0x1, 0x10(%rsp)
subq $0x1, %rcx
movl $0x0, %eax
cmovbq %rax, %rcx
movq %rcx, 0x8(%rsp)
pushq $0x9
popq %rdi
callq 0x1030
pushq $0x1
popq %r14
jmp 0x1229
xorl %eax, %eax
jmp 0x136a
leaq 0xca2(%rip), %rdi       # 0x2004
callq 0x1040
pushq $0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq