// File: /workspace/github_c_makefile_star2_O1/ohkimur[P]the-c-programming-language-2nd-edition-solutions/chapter_1/exercise_1_13/histogram.c
// Function: main
int main(void)
{
  int histogram[BUFFER];
  int histogram_length = 0;

  int max_word_count = 0;

  // Initialize the histogram array with 0
  int i;
  for (i = 0; i < BUFFER; ++i)
  {
    histogram[i] = 0;
  }

  // Count the words length and store in histogram array at the
  // specific index
  char c;
  int word_count_index = 0;
  while ((c = getchar()))
  {
    if (c == ' ' || c == '\t' || c == '\n' || c == EOF)
    {
      if (word_count_index > 0)
      {
        ++histogram[word_count_index - 1];

        if (histogram[word_count_index - 1] > max_word_count)
        {
          max_word_count = histogram[word_count_index - 1];
        }

        if (histogram_length < word_count_index - 1)
        {
          histogram_length = word_count_index - 1;
        }

        word_count_index = 0;
      }
      if (c == EOF)
      {
        break;
      }
    }
    else
    {
      ++word_count_index;
    }
  }

  // Add in the histogram array a end of useful data char
  histogram[histogram_length + 1] = '$';

  putchar('\n');

  int column_index = 0;
  int line_index = 0;

  // Print horizontal histogram
  printf("Horizontal Histogram\n--------------------\n");

  while (histogram[column_index] != '$')
  {
    printf("%3d: \t", column_index + 1);

    for (line_index = 0; line_index < histogram[column_index]; ++line_index)
    {
      putchar('#');
    }

    putchar('\n');

    ++column_index;
  }

  putchar('\n');

  // Print a vertical histogram
  printf("Vertical Histogram\n------------------\n");

  for (line_index = max_word_count; line_index >= 0; --line_index)
  {
    column_index = 0;
    while (histogram[column_index] != '$')
    {
      if (line_index == 0)
      {
        printf("%2d ", column_index + 1);
      }
      else if (histogram[column_index] >= line_index)
      {
        printf("## ");
      }
      else
      {
        printf("   ");
      }

      ++column_index;
    }

    putchar('\n');
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rsp, %rdi
xorl %r13d, %r13d
movl $0x190, %edx            # imm = 0x190
xorl %esi, %esi
callq 0x1060
callq 0x1070
testb %al, %al
je 0x1218
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %r13d, %r13d
shll $0x18, %eax
cmpl $0x9ffffff, %eax        # imm = 0x9FFFFFF
jg 0x11ca
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x11d8
cmpl $0x9000000, %eax        # imm = 0x9000000
je 0x11d8
jmp 0x1208
cmpl $0x20000000, %eax       # imm = 0x20000000
je 0x11d8
cmpl $0xa000000, %eax        # imm = 0xA000000
jne 0x1208
testl %r14d, %r14d
jle 0x11ff
decl %r14d
movl (%rsp,%r14,4), %ecx
leal 0x1(%rcx), %edx
movl %edx, (%rsp,%r14,4)
cmpl %r13d, %ecx
cmovgel %edx, %r13d
cmpl %r14d, %ebx
cmovgl %ebx, %r14d
movl %r14d, %ebx
xorl %r14d, %r14d
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x120b
jmp 0x1214
incl %r14d
callq 0x1070
testb %al, %al
jne 0x11b0
incl %ebx
jmp 0x121d
movl $0x1, %ebx
movl $0x24, (%rsp,%rbx,4)
movl $0xa, %edi
callq 0x1030
leaq 0xde3(%rip), %rdi       # 0x2018
callq 0x1040
movl (%rsp), %ebp
cmpl $0x24, %ebp
je 0x1281
leaq 0xdbb(%rip), %rbx       # 0x2004
xorl %r14d, %r14d
incq %r14
movq %rbx, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x1050
testl %ebp, %ebp
jle 0x126e
movl $0x23, %edi
callq 0x1030
decl %ebp
jne 0x1260
movl $0xa, %edi
callq 0x1030
movl (%rsp,%r14,4), %ebp
cmpl $0x24, %ebp
jne 0x124c
movl $0xa, %edi
callq 0x1030
leaq 0xdb0(%rip), %rdi       # 0x2042
callq 0x1040
testl %r13d, %r13d
js 0x130b
movl (%rsp), %ebp
leaq 0xd65(%rip), %rbx       # 0x200b
leaq 0xd67(%rip), %r14       # 0x2014
leaq 0xd5c(%rip), %r15       # 0x2010
cmpl $0x24, %ebp
je 0x12f5
movl $0x1, %r12d
movl %ebp, %eax
testl %r13d, %r13d
je 0x12d0
cmpl %r13d, %eax
jge 0x12df
movq %r14, %rdi
jmp 0x12e2
movq %rbx, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1050
jmp 0x12e9
movq %r15, %rdi
xorl %eax, %eax
callq 0x1050
movl (%rsp,%r12,4), %eax
incq %r12
cmpl $0x24, %eax
jne 0x12c1
movl $0xa, %edi
callq 0x1030
leal -0x1(%r13), %eax
testl %r13d, %r13d
movl %eax, %r13d
jg 0x12b4
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq