// File: /workspace/github_c_makefile_star2_O2/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 0x12fc
testl %eax, %eax
je 0x12de
leal -0x1(%r14), %eax
movslq %eax, %r12
movslq %r14d, %r14
pushq $0x1
popq %r15
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpq %r14, %r15
jae 0x11f6
movq (%rbx,%r15,8), %rdi
movzbl (%rdi), %eax
cmpl $0x2b, %eax
je 0x11e3
cmpl $0x2d, %eax
jne 0x11f1
incq %rdi
callq 0x1070
movslq %eax, %rbp
jmp 0x11ee
incq %rdi
callq 0x1070
movslq %eax, %r13
decq %r12
incq %r15
jmp 0x11c0
movq %rbp, 0x20(%rsp)
movq %r13, 0x10(%rsp)
pushq $0x8
popq %r13
pushq $0x1
popq %rax
xorl %r14d, %r14d
pushq $0x20
popq %rbp
movq %rax, (%rsp)
movq %r12, 0x8(%rsp)
callq 0x1060
cmpl $-0x1, %eax
je 0x12da
movl %eax, %r15d
cmpl $0x9, %eax
je 0x1242
movl %r15d, %edi
callq 0x1030
incq %r14
cmpl $0xa, %r15d
jne 0x1216
jmp 0x12cb
movq 0x8(%rsp), %r15
testq %r15, %r15
je 0x1268
movq (%rsp), %rax
movq (%rbx,%rax,8), %rdi
incq %rax
movq %rax, (%rsp)
callq 0x1070
movslq %eax, %r13
decq %r15
jmp 0x1281
testq %r12, %r12
pushq $0x1
popq %rax
cmovneq %rax, %r13
movq 0x10(%rsp), %rax
testq %rax, %rax
cmovneq %rax, %r13
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
testq %rax, %rax
movq %r13, 0x18(%rsp)
movq %r15, 0x8(%rsp)
je 0x12a1
cmpq %rax, %r14
pushq $0x1
popq %rax
cmovbq %rax, %r13
jmp 0x12ac
movq %r14, %rax
xorl %edx, %edx
divq %r13
subq %rdx, %r13
movq %r13, %r15
subq $0x1, %r15
jb 0x12be
movl %ebp, %edi
callq 0x1030
jmp 0x12af
addq %r13, %r14
movq 0x18(%rsp), %r13
jmp 0x1216
pushq $0x1
popq %rax
movq %rax, (%rsp)
xorl %r14d, %r14d
jmp 0x1211
xorl %eax, %eax
jmp 0x12ed
leaq 0xd1f(%rip), %rdi       # 0x2004
callq 0x1040
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq