// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x1400
cmpl $0x0, %eax
jne 0x11cf
leaq 0xe46(%rip), %rdi       # 0x2004
callq 0x1040
movl $0x1, -0x4(%rbp)
jmp 0x13f1
movq $0x1, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x8, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl -0x8(%rbp), %eax
subl $0x1, %eax
cltq
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x1, -0x60(%rbp)
movq -0x60(%rbp), %rax
movslq -0x8(%rbp), %rcx
cmpq %rcx, %rax
jae 0x12b1
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x1261
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x1, %rdi
callq 0x1070
cltq
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x129e
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2b, %eax
jne 0x129c
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x1, %rdi
callq 0x1070
cltq
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x129e
jmp 0x12a0
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1213
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
callq 0x1060
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x13ea
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpl $0x20, -0x14(%rbp)
jne 0x139e
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x130c
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1070
cltq
movq %rax, -0x30(%rbp)
jmp 0x1330
cmpq $0x0, -0x50(%rbp)
je 0x131d
movq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x132e
cmpq $0x0, -0x48(%rbp)
je 0x132c
movq $0x1, -0x30(%rbp)
jmp 0x132e
jmp 0x1330
cmpq $0x0, -0x58(%rbp)
jne 0x1350
movq -0x28(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x30(%rbp)
cmpq $0x0, %rdx
jne 0x1350
cmpq $0x1, -0x38(%rbp)
ja 0x136b
cmpq $0x0, -0x58(%rbp)
je 0x1361
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
je 0x136b
movq -0x28(%rbp), %rax
cmpq -0x58(%rbp), %rax
ja 0x139c
movl $0x9, %edi
callq 0x1030
cmpq $0x0, -0x40(%rbp)
je 0x1394
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
jmp 0x13e5
jmp 0x13a0
cmpq $0x0, -0x38(%rbp)
je 0x13bf
movl $0x20, %edi
callq 0x1030
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x13a0
cmpl $0xa, -0x14(%rbp)
jne 0x13dd
movq $0x1, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x14(%rbp), %edi
callq 0x1030
jmp 0x12b9
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)