// File: /workspace/github_c_makefile_star2_O3/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 0x1326
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 0x1317
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r13, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movl $0x8, %eax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
movl $0x1, %eax
movq %rax, 0x10(%rsp)
movq %r15, %r13
callq 0x1060
cmpl $-0x1, %eax
je 0x1315
movl %eax, %ebp
cmpl $0x9, %eax
je 0x1262
movl %ebp, %edi
callq 0x1030
incq %r14
cmpl $0xa, %ebp
jne 0x1239
jmp 0x1303
testq %r13, %r13
je 0x1289
movq 0x10(%rsp), %rax
movq (%rbx,%rax,8), %rdi
incq %rax
movq %rax, 0x10(%rsp)
callq 0x1070
cltq
movq %rax, 0x8(%rsp)
decq %r13
jmp 0x12ae
testq %r15, %r15
movl $0x1, %eax
movq 0x8(%rsp), %rcx
cmovneq %rax, %rcx
movq 0x18(%rsp), %rax
testq %rax, %rax
cmovneq %rax, %rcx
movq %rcx, 0x8(%rsp)
xorl %r13d, %r13d
movq 0x20(%rsp), %rcx
testq %rcx, %rcx
je 0x12ca
movl $0x1, %r12d
movq 0x8(%rsp), %rax
cmpq %rcx, %r14
jae 0x12dd
jmp 0x12e9
movq %r14, %rax
xorl %edx, %edx
movq 0x8(%rsp), %rcx
divq %rcx
movq %rcx, %rax
subq %rdx, %rax
movq %rax, %r12
testq %rax, %rax
je 0x1239
movq %r12, %rbp
movl $0x20, %edi
callq 0x1030
decq %rbp
jne 0x12ec
addq %r12, %r14
jmp 0x1239
movl $0x1, %eax
movq %rax, 0x10(%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