// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movl $0x0, -0x4(%rbp)
movl $0x0, -0x1a4(%rbp)
movl $0x0, -0x1a8(%rbp)
movl $0x0, -0x1ac(%rbp)
cmpl $0x64, -0x1ac(%rbp)
jge 0x11bc
movslq -0x1ac(%rbp), %rax
movl $0x0, -0x1a0(%rbp,%rax,4)
movl -0x1ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ac(%rbp)
jmp 0x1190
movl $0x0, -0x1b4(%rbp)
callq 0x1050
movb %al, -0x1ad(%rbp)
cmpb $0x0, %al
je 0x12b4
movsbl -0x1ad(%rbp), %eax
cmpl $0x20, %eax
je 0x120d
movsbl -0x1ad(%rbp), %eax
cmpl $0x9, %eax
je 0x120d
movsbl -0x1ad(%rbp), %eax
cmpl $0xa, %eax
je 0x120d
movsbl -0x1ad(%rbp), %eax
cmpl $-0x1, %eax
jne 0x12a0
cmpl $0x0, -0x1b4(%rbp)
jle 0x1290
movl -0x1b4(%rbp), %eax
subl $0x1, %eax
cltq
movl -0x1a0(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x1a0(%rbp,%rax,4)
movl -0x1b4(%rbp), %eax
subl $0x1, %eax
cltq
movl -0x1a0(%rbp,%rax,4), %eax
cmpl -0x1a8(%rbp), %eax
jle 0x1264
movl -0x1b4(%rbp), %eax
subl $0x1, %eax
cltq
movl -0x1a0(%rbp,%rax,4), %eax
movl %eax, -0x1a8(%rbp)
movl -0x1a4(%rbp), %eax
movl -0x1b4(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1286
movl -0x1b4(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1a4(%rbp)
movl $0x0, -0x1b4(%rbp)
movsbl -0x1ad(%rbp), %eax
cmpl $-0x1, %eax
jne 0x129e
jmp 0x12b4
jmp 0x12af
movl -0x1b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b4(%rbp)
jmp 0x11c6
movl -0x1a4(%rbp), %eax
addl $0x1, %eax
cltq
movl $0x24, -0x1a0(%rbp,%rax,4)
movl $0xa, %edi
callq 0x1030
movl $0x0, -0x1b8(%rbp)
movl $0x0, -0x1bc(%rbp)
leaq 0xd15(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1040
movslq -0x1b8(%rbp), %rax
cmpl $0x24, -0x1a0(%rbp,%rax,4)
je 0x1374
movl -0x1b8(%rbp), %esi
addl $0x1, %esi
leaq 0xd18(%rip), %rdi       # 0x202f
movb $0x0, %al
callq 0x1040
movl $0x0, -0x1bc(%rbp)
movl -0x1bc(%rbp), %eax
movslq -0x1b8(%rbp), %rcx
cmpl -0x1a0(%rbp,%rcx,4), %eax
jge 0x1359
movl $0x23, %edi
callq 0x1030
movl -0x1bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1bc(%rbp)
jmp 0x1328
movl $0xa, %edi
callq 0x1030
movl -0x1b8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b8(%rbp)
jmp 0x12f6
movl $0xa, %edi
callq 0x1030
leaq 0xcb1(%rip), %rdi       # 0x2036
movb $0x0, %al
callq 0x1040
movl -0x1a8(%rbp), %eax
movl %eax, -0x1bc(%rbp)
cmpl $0x0, -0x1bc(%rbp)
jl 0x1447
movl $0x0, -0x1b8(%rbp)
movslq -0x1b8(%rbp), %rax
cmpl $0x24, -0x1a0(%rbp,%rax,4)
je 0x1429
cmpl $0x0, -0x1bc(%rbp)
jne 0x13e2
movl -0x1b8(%rbp), %esi
addl $0x1, %esi
leaq 0xc84(%rip), %rdi       # 0x205d
movb $0x0, %al
callq 0x1040
jmp 0x1418
movslq -0x1b8(%rbp), %rax
movl -0x1a0(%rbp,%rax,4), %eax
cmpl -0x1bc(%rbp), %eax
jl 0x1408
leaq 0xc63(%rip), %rdi       # 0x2062
movb $0x0, %al
callq 0x1040
jmp 0x1416
leaq 0xc57(%rip), %rdi       # 0x2066
movb $0x0, %al
callq 0x1040
jmp 0x1418
movl -0x1b8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b8(%rbp)
jmp 0x13af
movl $0xa, %edi
callq 0x1030
movl -0x1bc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1bc(%rbp)
jmp 0x1398
xorl %eax, %eax
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq