// File: /workspace/github_c_makefile_star2_O0/moefh[P]hed/src/editor.c
// Function: hed_set_cursor_pos
void hed_set_cursor_pos(struct hed_editor *editor, size_t pos, size_t visible_len_after)
{
  struct hed_screen *scr = &editor->screen;
  struct hed_file *file = editor->file;
  size_t n_page_lines = get_num_displayed_file_lines(editor);
  size_t last_line = file->data_len / 16 + (file->data_len % 16 != 0);

  if (pos >= file->data_len)
    pos = (file->data_len == 0) ? 0 : file->data_len-1;
  if (pos + visible_len_after >= file->data_len)
    visible_len_after = file->data_len - pos;
  
  file->cursor_pos = pos;

  // If the cursor or 'visible_len_after' bytes after it are not visible,
  // center the screen vertically around the cursor
  if (! (file->cursor_pos / 16 >= file->top_line
         && (file->cursor_pos+visible_len_after) / 16 >= file->top_line
         && file->cursor_pos / 16 <= file->top_line + n_page_lines
         && (file->cursor_pos+visible_len_after) / 16 <= file->top_line + n_page_lines)) {
    if (file->cursor_pos / 16 < n_page_lines/2)
      file->top_line = 0;
    else {
      file->top_line = file->cursor_pos / 16 - n_page_lines/2;
      if (file->top_line + n_page_lines > last_line)
        file->top_line = last_line - n_page_lines;
    }
  }
  scr->redraw_needed = true;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq 0x40(%rsp), %rax
addq $0x108, %rax            # imm = 0x108
movq %rax, 0x28(%rsp)
movq 0x40(%rsp), %rax
movq 0x628(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x40(%rsp), %rdi
callq 0x6610
cltq
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
shrq $0x4, %rax
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rcx
andq $0xf, %rcx
cmpq $0x0, %rcx
setne %cl
andb $0x1, %cl
movzbl %cl, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
movq 0x20(%rsp), %rcx
cmpq 0x18(%rcx), %rax
jb 0x64d8
movq 0x20(%rsp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x64bc
xorl %eax, %eax
movq %rax, 0x8(%rsp)
jmp 0x64ce
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
subq $0x1, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
addq 0x30(%rsp), %rax
movq 0x20(%rsp), %rcx
cmpq 0x18(%rcx), %rax
jb 0x6500
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
subq 0x38(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x38(%rax)
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
shrq $0x4, %rax
movq 0x20(%rsp), %rcx
cmpq 0x40(%rcx), %rax
jb 0x6588
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
addq 0x30(%rsp), %rax
shrq $0x4, %rax
movq 0x20(%rsp), %rcx
cmpq 0x40(%rcx), %rax
jb 0x6588
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
shrq $0x4, %rax
movq 0x20(%rsp), %rcx
movq 0x40(%rcx), %rcx
addq 0x18(%rsp), %rcx
cmpq %rcx, %rax
ja 0x6588
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
addq 0x30(%rsp), %rax
shrq $0x4, %rax
movq 0x20(%rsp), %rcx
movq 0x40(%rcx), %rcx
addq 0x18(%rsp), %rcx
cmpq %rcx, %rax
jbe 0x65fe
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
shrq $0x4, %rax
movq 0x18(%rsp), %rcx
shrq %rcx
cmpq %rcx, %rax
jae 0x65b1
movq 0x20(%rsp), %rax
movq $0x0, 0x40(%rax)
jmp 0x65fc
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rcx
shrq $0x4, %rcx
movq 0x18(%rsp), %rax
shrq %rax
subq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x20(%rsp), %rax
movq 0x40(%rax), %rax
addq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
jbe 0x65fa
movq 0x10(%rsp), %rcx
subq 0x18(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x40(%rax)
jmp 0x65fc
jmp 0x65fe
movq 0x28(%rsp), %rax
movb $0x1, 0xd(%rax)
addq $0x48, %rsp
retq
nopl (%rax)