// File: /workspace/github_c_makefile_star2_O3/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 %ebp, %ebp
movl $0x190, %edx            # imm = 0x190
xorl %esi, %esi
callq 0x1060
callq 0x1070
testb %al, %al
je 0x121a
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %r13d, %r13d
shll $0x18, %eax
cmpl $0x9ffffff, %eax        # imm = 0x9FFFFFF
jg 0x11c9
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x11d7
cmpl $0x9000000, %eax        # imm = 0x9000000
je 0x11d7
jmp 0x1207
cmpl $0x20000000, %eax       # imm = 0x20000000
je 0x11d7
cmpl $0xa000000, %eax        # imm = 0xA000000
jne 0x1207
testl %r14d, %r14d
jle 0x11fe
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 0x120a
jmp 0x1213
incl %r14d
callq 0x1070
testb %al, %al
jne 0x11af
movl (%rsp), %ebp
incl %ebx
jmp 0x1222
movl $0x1, %ebx
xorl %r13d, %r13d
movl $0x24, (%rsp,%rbx,4)
movl $0xa, %edi
callq 0x1030
leaq 0xdde(%rip), %rdi       # 0x2018
callq 0x1040
cmpl $0x24, %ebp
je 0x1289
leaq 0xdb9(%rip), %rbx       # 0x2004
xorl %r14d, %r14d
movl %ebp, %r15d
incq %r14
movq %rbx, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x1050
testl %r15d, %r15d
jle 0x1275
movl $0x23, %edi
callq 0x1030
decl %r15d
jne 0x1266
movl $0xa, %edi
callq 0x1030
movl (%rsp,%r14,4), %r15d
cmpl $0x24, %r15d
jne 0x1251
movl $0xa, %edi
callq 0x1030
leaq 0xda8(%rip), %rdi       # 0x2042
callq 0x1040
testl %r13d, %r13d
js 0x1310
leaq 0xd60(%rip), %rbx       # 0x200b
leaq 0xd62(%rip), %r14       # 0x2014
leaq 0xd57(%rip), %r15       # 0x2010
cmpl $0x24, %ebp
je 0x12fa
movl $0x1, %r12d
movl %ebp, %eax
testl %r13d, %r13d
je 0x12d5
cmpl %r13d, %eax
jge 0x12e4
movq %r14, %rdi
jmp 0x12e7
movq %rbx, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1050
jmp 0x12ee
movq %r15, %rdi
xorl %eax, %eax
callq 0x1050
movl (%rsp,%r12,4), %eax
incq %r12
cmpl $0x24, %eax
jne 0x12c6
movl $0xa, %edi
callq 0x1030
leal -0x1(%r13), %eax
testl %r13d, %r13d
movl %eax, %r13d
jg 0x12b9
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq