// File: jakubsvehla[P]hex/src/normal_mode.cpp
// Function: NormalMode::Run()
void NormalMode::Run ()
{
  int c;
  int offset = 1;
  int jmp = 1;
  int i;
  int whence = SEEK_CUR;
  bool clear = true;
  std::stringstream buffer;

  m_Editor -> m_File -> Seek(0, whence);

  while (true) {

    m_Editor -> m_FileView -> Get(c);

    if (clear) {
      m_Editor -> m_CmdLineView -> Erase();
      m_Editor -> m_CmdLineView -> Refresh();
      m_Editor -> m_FileView -> Refresh();

      clear = false;
    }

    if (isdigit(c)) {
      buffer << (char) c;
      continue;
    }

    buffer >> jmp;

    switch (c) {

      case 'x':
        offset = 0;
        for (i = 0; i < jmp; i ++) {
          m_Editor -> m_File -> Del();
        }
        break;

      case 'r':
        offset = 0;

        try {
          if (m_Editor -> m_FileView -> GetHex(c)) {
            m_Editor -> m_File -> Put(c);
          }
        }
        catch (NotHexadecimalValue) {
          m_Editor -> m_FileView -> Beep();
        }
        break;

      case KEY_LEFT:
      case 'h':
        offset = -1;
        offset *= jmp;
        break;

      case KEY_RIGHT:
      case 'l':
        offset = 1;
        offset *= jmp;
        break;

      case KEY_DOWN:
      case 'j':
        offset = m_Editor -> m_FileView -> BytesPerLine();
        offset *= jmp;
        break;

      case KEY_UP:
      case 'k':
        offset = m_Editor -> m_FileView -> BytesPerLine();
        offset = -offset;
        offset *= jmp;
        break;

      case 'i':
        m_Editor -> m_Mode = m_Editor -> m_InsertMode;
        return;

      case ':':
        m_Editor -> m_Mode = m_Editor -> m_CmdMode;
        return;

      case ESC:
        jmp = 1;
        buffer.clear();
        buffer.str("");
        return;

      default:
        continue;
    }

    jmp = 1;
    buffer.clear();
    buffer.str("");

    if (!m_Editor -> m_File -> Seek(offset, whence)) {
      m_Editor -> m_FileView -> Beep();
    }

  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x218(%rbp)
movl $0x1, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movl $0x1, -0x1c(%rbp)
movb $0x1, -0x1d(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x31f0
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
movl -0x1c(%rbp), %edx
xorl %esi, %esi
callq 0x4110
jmp 0x575a
jmp 0x575c
jmp 0x575e
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
leaq -0xc(%rbp), %rsi
callq 0x4920
jmp 0x5778
testb $0x1, -0x1d(%rbp)
je 0x57dd
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
callq 0x4b20
jmp 0x5794
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rdi
callq 0x4b00
jmp 0x57aa
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x4b00
jmp 0x57c0
movb $0x0, -0x1d(%rbp)
jmp 0x57dd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
jmp 0x5cd9
movl -0xc(%rbp), %edi
callq 0x3270
cmpl $0x0, %eax
je 0x5801
leaq -0x198(%rbp), %rdi
movsbl -0xc(%rbp), %esi
callq 0x33b0
jmp 0x57fc
jmp 0x575c
leaq -0x1a8(%rbp), %rdi
leaq -0x14(%rbp), %rsi
callq 0x31b0
jmp 0x5813
movl -0xc(%rbp), %eax
movl %eax, -0x21c(%rbp)
subl $0x1b, %eax
je 0x5ae4
jmp 0x5827
movl -0x21c(%rbp), %eax
subl $0x3a, %eax
je 0x5abe
jmp 0x5838
movl -0x21c(%rbp), %eax
subl $0x68, %eax
je 0x59fc
jmp 0x5849
movl -0x21c(%rbp), %eax
subl $0x69, %eax
je 0x5a98
jmp 0x585a
movl -0x21c(%rbp), %eax
subl $0x6a, %eax
je 0x5a28
jmp 0x586b
movl -0x21c(%rbp), %eax
subl $0x6b, %eax
je 0x5a5c
jmp 0x587c
movl -0x21c(%rbp), %eax
subl $0x6c, %eax
je 0x5a12
jmp 0x588d
movl -0x21c(%rbp), %eax
subl $0x72, %eax
je 0x5938
jmp 0x589e
movl -0x21c(%rbp), %eax
subl $0x78, %eax
je 0x58fa
jmp 0x58ab
movl -0x21c(%rbp), %eax
subl $0x102, %eax            # imm = 0x102
je 0x5a28
jmp 0x58be
movl -0x21c(%rbp), %eax
subl $0x103, %eax            # imm = 0x103
je 0x5a5c
jmp 0x58d1
movl -0x21c(%rbp), %eax
subl $0x104, %eax            # imm = 0x104
je 0x59fc
jmp 0x58e4
movl -0x21c(%rbp), %eax
subl $0x105, %eax            # imm = 0x105
je 0x5a12
jmp 0x5bb5
movl $0x0, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x5933
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
callq 0x4370
jmp 0x5926
jmp 0x5928
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x5908
jmp 0x5bba
movq -0x218(%rbp), %rax
movl $0x0, -0x10(%rbp)
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
leaq -0xc(%rbp), %rsi
callq 0x4960
movb %al, -0x21d(%rbp)
jmp 0x595f
movb -0x21d(%rbp), %al
testb $0x1, %al
jne 0x596b
jmp 0x59dc
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
movzbl -0xc(%rbp), %esi
callq 0x4520
jmp 0x5985
jmp 0x59dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
movl -0x1b4(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x5cd9
movq -0x1b0(%rbp), %rdi
callq 0x3150
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x4b40
jmp 0x59ce
callq 0x34f0
jmp 0x59d5
jmp 0x59d7
jmp 0x5bba
jmp 0x59d7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
callq 0x34f0
jmp 0x59f7
jmp 0x5cd9
movl $0xffffffff, -0x10(%rbp) # imm = 0xFFFFFFFF
movl -0x14(%rbp), %eax
imull -0x10(%rbp), %eax
movl %eax, -0x10(%rbp)
jmp 0x5bba
movl $0x1, -0x10(%rbp)
movl -0x14(%rbp), %eax
imull -0x10(%rbp), %eax
movl %eax, -0x10(%rbp)
jmp 0x5bba
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x5440
movl %eax, -0x224(%rbp)
jmp 0x5a44
movl -0x224(%rbp), %eax
movl %eax, -0x10(%rbp)
movl -0x14(%rbp), %eax
imull -0x10(%rbp), %eax
movl %eax, -0x10(%rbp)
jmp 0x5bba
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x5440
movl %eax, -0x228(%rbp)
jmp 0x5a78
movl -0x228(%rbp), %eax
movl %eax, -0x10(%rbp)
xorl %eax, %eax
subl -0x10(%rbp), %eax
movl %eax, -0x10(%rbp)
movl -0x14(%rbp), %eax
imull -0x10(%rbp), %eax
movl %eax, -0x10(%rbp)
jmp 0x5bba
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rcx
movq 0x40(%rcx), %rcx
movq 0x8(%rax), %rax
movq %rcx, 0x28(%rax)
movl $0x1, -0x1bc(%rbp)
jmp 0x5cc4
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rcx
movq 0x38(%rcx), %rcx
movq 0x8(%rax), %rax
movq %rcx, 0x28(%rax)
movl $0x1, -0x1bc(%rbp)
jmp 0x5cc4
movl $0x1, -0x14(%rbp)
movq -0x1a8(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x1a8(%rbp,%rax), %rdi
xorl %esi, %esi
callq 0x3520
jmp 0x5b07
leaq -0x1e1(%rbp), %rdi
movq %rdi, -0x230(%rbp)
callq 0x3560
movq -0x230(%rbp), %rdx
leaq 0x2650(%rip), %rsi      # 0x8178
leaq -0x1e0(%rbp), %rdi
callq 0x5d00
jmp 0x5b36
leaq -0x1a8(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x3390
jmp 0x5b4b
leaq -0x1e0(%rbp), %rdi
callq 0x31c0
leaq -0x1e1(%rbp), %rdi
callq 0x3330
movl $0x1, -0x1bc(%rbp)
jmp 0x5cc4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
jmp 0x5ba4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x31c0
leaq -0x1e1(%rbp), %rdi
callq 0x3330
jmp 0x5cd9
jmp 0x575c
movl $0x1, -0x14(%rbp)
movq -0x1a8(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x1a8(%rbp,%rax), %rdi
xorl %esi, %esi
callq 0x3520
jmp 0x5bdd
leaq -0x209(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x3560
movq -0x238(%rbp), %rdx
leaq 0x257a(%rip), %rsi      # 0x8178
leaq -0x208(%rbp), %rdi
callq 0x5d00
jmp 0x5c0c
leaq -0x1a8(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x3390
jmp 0x5c21
leaq -0x208(%rbp), %rdi
callq 0x31c0
leaq -0x209(%rbp), %rdi
callq 0x3330
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rdi
movl -0x10(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4110
movb %al, -0x239(%rbp)
jmp 0x5c5b
movb -0x239(%rbp), %al
testb $0x1, %al
jne 0x5cbf
jmp 0x5c67
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x4b40
jmp 0x5c7d
jmp 0x5cbf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
jmp 0x5cb1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
leaq -0x208(%rbp), %rdi
callq 0x31c0
leaq -0x209(%rbp), %rdi
callq 0x3330
jmp 0x5cd9
jmp 0x575c
leaq -0x1a8(%rbp), %rdi
callq 0x3210
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
leaq -0x1a8(%rbp), %rdi
callq 0x3210
movq -0x1b0(%rbp), %rdi
callq 0x3540
movq %rax, %rdi
callq 0x3b80
nopl (%rax)