// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x13f0
cmpl $0x0, %eax
jne 0x11cf
leaq 0xe46(%rip), %rdi       # 0x2004
callq 0x1040
movl $0x1, -0x4(%rbp)
jmp 0x13da
movq $0x1, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x8, -0x30(%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 0x12a9
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x1259
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 0x1296
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2b, %eax
jne 0x1294
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 0x1296
jmp 0x1298
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x120b
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
callq 0x1060
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x13d3
cmpl $0x9, -0x14(%rbp)
jne 0x139a
cmpq $0x0, -0x40(%rbp)
je 0x1303
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movq (%rax,%rcx,8), %rdi
callq 0x1070
cltq
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1327
cmpq $0x0, -0x50(%rbp)
je 0x1314
movq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x1325
cmpq $0x0, -0x48(%rbp)
je 0x1323
movq $0x1, -0x30(%rbp)
jmp 0x1325
jmp 0x1327
cmpq $0x0, -0x58(%rbp)
je 0x134c
movq -0x28(%rbp), %rax
cmpq -0x58(%rbp), %rax
jb 0x1342
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x134a
movq $0x1, -0x38(%rbp)
jmp 0x136b
movq -0x30(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x30(%rbp)
movq -0x68(%rbp), %rax
subq %rdx, %rax
movq %rax, -0x38(%rbp)
jmp 0x136d
cmpq $0x0, -0x38(%rbp)
je 0x1398
movl $0x20, %edi
callq 0x1030
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x136d
jmp 0x13ce
movl -0x14(%rbp), %edi
callq 0x1030
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpl $0xa, -0x14(%rbp)
jne 0x13cc
movq $0x1, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x13ce
jmp 0x12b1
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)