// File: /workspace/github_c_makefile_star2_O2/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
xorl %eax, %eax
cmpq $0x64, %rax
je 0x118e
andl $0x0, (%rsp,%rax,4)
incq %rax
jmp 0x117f
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %r14d, %r14d
callq 0x1060
testb %al, %al
je 0x11f1
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x11c3
cmpl $0x9000000, %eax        # imm = 0x9000000
je 0x11c3
cmpl $0x20000000, %eax       # imm = 0x20000000
je 0x11c3
cmpl $0xa000000, %eax        # imm = 0xA000000
je 0x11c3
incl %r14d
jmp 0x1196
testl %r14d, %r14d
jle 0x11ea
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 0x1196
movl %ebx, %eax
movl $0x24, 0x4(%rsp,%rax,4)
pushq $0xa
popq %rdi
callq 0x1030
leaq 0xe0e(%rip), %rdi       # 0x2018
callq 0x1040
xorl %r12d, %r12d
leaq 0xdeb(%rip), %r14       # 0x2004
pushq $0x23
popq %rbp
xorl %r15d, %r15d
movl (%rsp,%r15,4), %ebx
cmpl $0x24, %ebx
je 0x1256
incq %r15
movq %r14, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x1050
testl %ebx, %ebx
cmovlel %r12d, %ebx
subl $0x1, %ebx
jb 0x124c
movl %ebp, %edi
callq 0x1030
jmp 0x123e
pushq $0xa
popq %rdi
callq 0x1030
jmp 0x121f
pushq $0xa
popq %rbx
movl %ebx, %edi
callq 0x1030
leaq 0xddb(%rip), %rdi       # 0x2042
callq 0x1040
leaq 0xd98(%rip), %r14       # 0x200b
leaq 0xd9a(%rip), %r15       # 0x2014
leaq 0xd8f(%rip), %r12       # 0x2010
testl %r13d, %r13d
js 0x12c9
xorl %ebp, %ebp
movl (%rsp,%rbp,4), %eax
cmpl $0x24, %eax
je 0x12bd
testl %r13d, %r13d
je 0x129f
cmpl %r13d, %eax
jge 0x12ae
movq %r15, %rdi
jmp 0x12b1
leal 0x1(%rbp), %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1050
jmp 0x12b8
movq %r12, %rdi
xorl %eax, %eax
callq 0x1050
incq %rbp
jmp 0x1288
movl %ebx, %edi
callq 0x1030
decl %r13d
jmp 0x1281
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq