// File: drummyfish[P]flowchart/src/program.cpp
// Function: program::string_from_image_area[abi:cxx11](image*, coord_2d, coord_2d, bool&)
string program::string_from_image_area(image *img, coord_2d c1, coord_2d c2, bool &error)
  {
    string result = "";

    int max_y, min_y, max_x, min_x;
    unsigned int i,j;
    unsigned char r,g,b;
    bool end_it;

    end_it = false;
    min_y = c2.y;

    for (j = c1.y; j <= c2.y; j++)
      {
        for (i = c1.x; i <= c2.x; i++)
          {
            img->get_pixel(i,j,r,g,b);

            if (is_black(r,g,b))
              {
                end_it = true;
                min_y = j;
              }
          }

        if (end_it)
          break;
      }

    max_y = c1.y;
    end_it = false;

    for (j = c2.y - 1; j >= c1.y; j--)
      {
        for (i = c1.x; i <= c2.x; i++)
          {
            img->get_pixel(i,j,r,g,b);

            if (is_black(r,g,b))
              {
                end_it = true;
                max_y = j;
              }
          }

        if (end_it)
          break;
      }

    if (max_y - min_y > (FONT_HEIGHT - 1) || max_y - min_y < 0)   // in this cases there cannot be a valid text
      {
        // SET ERROR HEREEEEEEEEEEEEEEEEEEE LATER

        return result;
      }

    min_x = c2.x;
    end_it = false;

    for (i = c1.x; i <= c2.x; i++)
      {
        for (j = min_y; ((int) j) <= max_y; j++)
          {
            img->get_pixel(i,j,r,g,b);

            if (is_black(r,g,b))
              {
                end_it = true;
                min_x = i;
              }
          }

        if (end_it)
          break;
      }

    max_x = c1.x;
    end_it = false;

    for (i = c2.x - 1; i >= c1.x; i--)
      {
        for (j = min_y; ((int) j) <= max_y; j++)
          {
            img->get_pixel(i,j,r,g,b);

            if (is_black(r,g,b))
              {
                end_it = true;
                max_x = i;
              }
          }

        if (end_it)
          break;
      }

    // now try to read the string at possitions it could possibly appear:

    coord_2d coord_from, coord_to;   // range of possible top left corner of the string, will be brute force-tested
    coord_2d current_coord;
    int x_limit, y_difference;
    bool success;

    y_difference = max_y - min_y;

    x_limit = saturate(max_x + FONT_WIDTH - 1,c1.x,c2.x);  // to what maximum x the sring should be read

    coord_to.x = min_x;
    coord_to.y = min_y;

    coord_from.y = saturate(coord_to.y - (FONT_HEIGHT - y_difference - 1),c1.y,c2.y);
    coord_from.x = saturate(min_x - FONT_WIDTH + 1,c1.x,c2.x);

    for (j = coord_from.y; j <= coord_to.y; j++)
      for (i = coord_from.x; i <= coord_to.x; i++)
        {
          current_coord.x = i;
          current_coord.y = j;

          result = this->string_from_image_position(img,current_coord,x_limit,success);

          if (success)
            return result;
        }

    return result;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0xc0(%rbp)
movq %rdi, %rax
movq %rax, -0xd0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rcx, -0x10(%rbp)
movq %r8, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xc8(%rbp)
movb $0x0, -0x31(%rbp)
leaq -0x32(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x3470
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rdx
leaq 0x7398(%rip), %rsi      # 0x1f460
callq 0x15680
jmp 0x180cf
leaq -0x32(%rbp), %rdi
callq 0x3270
movb $0x0, -0x60(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x14(%rbp), %eax
ja 0x181a9
movl -0x10(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x18(%rbp), %eax
ja 0x18191
movq -0x28(%rbp), %rdi
movl -0x58(%rbp), %esi
movl -0x5c(%rbp), %edx
leaq -0x5d(%rbp), %rcx
leaq -0x5e(%rbp), %r8
leaq -0x5f(%rbp), %r9
callq 0x15f80
jmp 0x18123
movzbl -0x5d(%rbp), %eax
cmpl $0x0, %eax
jne 0x18181
movzbl -0x5e(%rbp), %eax
cmpl $0x0, %eax
jne 0x18181
movzbl -0x5f(%rbp), %eax
cmpl $0x0, %eax
jne 0x18181
movb $0x1, -0x60(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x18181
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x32(%rbp), %rdi
callq 0x3270
jmp 0x184e6
movq -0xc0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x3160
jmp 0x184e6
jmp 0x18183
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x180fa
testb $0x1, -0x60(%rbp)
je 0x18199
jmp 0x181a9
jmp 0x1819b
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x180e8
movl -0xc(%rbp), %eax
movl %eax, -0x48(%rbp)
movb $0x0, -0x60(%rbp)
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jb 0x18236
movl -0x10(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x18(%rbp), %eax
ja 0x18221
movq -0x28(%rbp), %rdi
movl -0x58(%rbp), %esi
movl -0x5c(%rbp), %edx
leaq -0x5d(%rbp), %rcx
leaq -0x5e(%rbp), %r8
leaq -0x5f(%rbp), %r9
callq 0x15f80
jmp 0x181ef
movzbl -0x5d(%rbp), %eax
cmpl $0x0, %eax
jne 0x18214
movzbl -0x5e(%rbp), %eax
cmpl $0x0, %eax
jne 0x18214
movzbl -0x5f(%rbp), %eax
cmpl $0x0, %eax
jne 0x18214
movb $0x1, -0x60(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x18216
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x181ca
testb $0x1, -0x60(%rbp)
je 0x18229
jmp 0x18236
jmp 0x1822b
movl -0x5c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x181bc
movl -0x48(%rbp), %eax
subl -0x4c(%rbp), %eax
cmpl $0x7, %eax
jg 0x1824c
movl -0x48(%rbp), %eax
subl -0x4c(%rbp), %eax
cmpl $0x0, %eax
jge 0x1825c
movb $0x1, -0x31(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x184c4
movl -0x18(%rbp), %eax
movl %eax, -0x54(%rbp)
movb $0x0, -0x60(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x18(%rbp), %eax
ja 0x182e6
movl -0x4c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x48(%rbp), %eax
jg 0x182d1
movq -0x28(%rbp), %rdi
movl -0x58(%rbp), %esi
movl -0x5c(%rbp), %edx
leaq -0x5d(%rbp), %rcx
leaq -0x5e(%rbp), %r8
leaq -0x5f(%rbp), %r9
callq 0x15f80
jmp 0x1829f
movzbl -0x5d(%rbp), %eax
cmpl $0x0, %eax
jne 0x182c4
movzbl -0x5e(%rbp), %eax
cmpl $0x0, %eax
jne 0x182c4
movzbl -0x5f(%rbp), %eax
cmpl $0x0, %eax
jne 0x182c4
movb $0x1, -0x60(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0x182c6
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1827a
testb $0x1, -0x60(%rbp)
je 0x182d9
jmp 0x182e6
jmp 0x182db
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x1826c
movl -0x10(%rbp), %eax
movl %eax, -0x50(%rbp)
movb $0x0, -0x60(%rbp)
movl -0x18(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x10(%rbp), %eax
jb 0x18373
movl -0x4c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x48(%rbp), %eax
jg 0x1835e
movq -0x28(%rbp), %rdi
movl -0x58(%rbp), %esi
movl -0x5c(%rbp), %edx
leaq -0x5d(%rbp), %rcx
leaq -0x5e(%rbp), %r8
leaq -0x5f(%rbp), %r9
callq 0x15f80
jmp 0x1832c
movzbl -0x5d(%rbp), %eax
cmpl $0x0, %eax
jne 0x18351
movzbl -0x5e(%rbp), %eax
cmpl $0x0, %eax
jne 0x18351
movzbl -0x5f(%rbp), %eax
cmpl $0x0, %eax
jne 0x18351
movb $0x1, -0x60(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x18353
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x18307
testb $0x1, -0x60(%rbp)
je 0x18366
jmp 0x18373
jmp 0x18368
movl -0x58(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x182f9
movl -0x48(%rbp), %eax
movl -0x4c(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x84(%rbp)
movl -0x50(%rbp), %edi
addl $0x6, %edi
movl -0x10(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0x1cac0
movl %eax, -0xd4(%rbp)
jmp 0x1839a
movl -0xd4(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
movl -0x84(%rbp), %edx
movl %eax, %ecx
movl %edx, %eax
leal -0x7(%rax,%rcx), %edi
movl -0xc(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x1cac0
movl %eax, -0xd8(%rbp)
jmp 0x183d3
movl -0xd8(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x54(%rbp), %edi
addl $-0x6, %edi
movl -0x10(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0x1cac0
movl %eax, -0xdc(%rbp)
jmp 0x183f5
movl -0xdc(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x70(%rbp), %eax
ja 0x184b9
movl -0x6c(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x74(%rbp), %eax
ja 0x184a9
movq -0xc8(%rbp), %rsi
movl -0x58(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x28(%rbp), %rdx
movq -0x7c(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x80(%rbp), %r8d
movq -0xb0(%rbp), %rcx
leaq -0xa8(%rbp), %rdi
leaq -0x85(%rbp), %r9
callq 0x18660
jmp 0x18464
movq -0xc0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x32f0
leaq -0xa8(%rbp), %rdi
callq 0x3160
testb $0x1, -0x85(%rbp)
je 0x18499
movb $0x1, -0x31(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x184c4
jmp 0x1849b
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x18416
jmp 0x184ab
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x18404
movb $0x1, -0x31(%rbp)
movl $0x1, -0x64(%rbp)
testb $0x1, -0x31(%rbp)
jne 0x184d6
movq -0xc0(%rbp), %rdi
callq 0x3160
movq -0xd0(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x3460
nop