// File: /workspace/github_c_makefile_star2_O1/ohkimur[P]the-c-programming-language-2nd-edition-solutions/chapter_5/exercise_5_12/detab.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;
  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)
  {
    if (c == '\t')
    {
      if (nr_of_custom_tab_stops)
      {
        tab_stop = atoi(argv[arg_pos++]);
        --nr_of_custom_tab_stops;
      }
      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)
      {
        if (line_pos >= custom_line_pos_start)
        {
          nr_of_spaces = tab_stop;
        }
        else
        {
          nr_of_spaces = 1;
        }
      }
      else
      {
        nr_of_spaces = tab_stop - line_pos % tab_stop;
      }

      while (nr_of_spaces)
      {
        putchar(' ');
        ++line_pos;
        --nr_of_spaces;
      }
    }
    else
    {
      putchar(c);
      ++line_pos;

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

  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movl %edi, %r14d
callq 0x1313
testl %eax, %eax
je 0x11fa
leal -0x1(%r14), %eax
movslq %eax, %r15
cmpl $0x2, %r14d
jb 0x1210
movslq %r14d, %r14
movl $0x1, %r12d
xorl %r13d, %r13d
xorl %ebp, %ebp
movq (%rbx,%r12,8), %rdi
movzbl (%rdi), %eax
cmpl $0x2b, %eax
je 0x11e2
cmpl $0x2d, %eax
jne 0x11f0
incq %rdi
callq 0x1070
movslq %eax, %r13
jmp 0x11ed
incq %rdi
callq 0x1070
movslq %eax, %rbp
decq %r15
incq %r12
cmpq %r12, %r14
jne 0x11c4
jmp 0x1215
leaq 0xe03(%rip), %rdi       # 0x2004
callq 0x1040
movl $0x1, %eax
jmp 0x1304
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r13, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movl $0x8, %eax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
movl $0x1, %eax
movq %rax, 0x8(%rsp)
movq %r15, %r13
callq 0x1060
cmpl $-0x1, %eax
je 0x1302
movl %eax, %ebp
cmpl $0x9, %eax
je 0x1262
movl %ebp, %edi
callq 0x1030
incq %r14
cmpl $0xa, %ebp
jne 0x1239
jmp 0x12f0
testq %r13, %r13
je 0x1285
movq 0x8(%rsp), %rax
movq (%rbx,%rax,8), %rdi
incq %rax
movq %rax, 0x8(%rsp)
callq 0x1070
movslq %eax, %r12
decq %r13
jmp 0x12a5
testq %r15, %r15
movl $0x1, %eax
movq 0x10(%rsp), %r12
cmovneq %rax, %r12
movq 0x18(%rsp), %rax
testq %rax, %rax
cmovneq %rax, %r12
xorl %r13d, %r13d
movq 0x20(%rsp), %rax
testq %rax, %rax
movq %r12, 0x10(%rsp)
je 0x12c2
cmpq %rax, %r14
movl $0x1, %eax
cmovbq %rax, %r12
jmp 0x12cd
movq %r14, %rax
xorl %edx, %edx
divq %r12
subq %rdx, %r12
movq %r12, %rbp
testq %r12, %r12
je 0x1239
movl $0x20, %edi
callq 0x1030
decq %rbp
jne 0x12d9
addq %r12, %r14
jmp 0x1239
movl $0x1, %eax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
jmp 0x1236
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq