// File: /workspace/github_c_makefile_star2_O0/janet-lang[P]janet/src/core/peg.c
// Function: get_linecol_from_position
static LineCol get_linecol_from_position(PegState *s, int32_t position) {
    /* Generate if not made yet */
    if (s->linemaplen < 0) {
        int32_t newline_count = 0;
        for (const uint8_t *c = s->text_start; c < s->outer_text_end; c++) {
            if (*c == '\n') newline_count++;
        }
        int32_t *mem = janet_smalloc(sizeof(int32_t) * newline_count);
        size_t index = 0;
        for (const uint8_t *c = s->text_start; c < s->outer_text_end; c++) {
            if (*c == '\n') mem[index++] = (int32_t)(c - s->text_start);
        }
        s->linemaplen = newline_count;
        s->linemap = mem;
    }
    /* Do binary search for line. Slightly modified from classic binary search:
     * - if we find that our current character is a line break, just return immediately.
     *   a newline character is consider to be on the same line as the character before
     *   (\n is line terminator, not line separator).
     * - in the not-found case, we still want to find the greatest-indexed newline that
     *   is before position. we use that to calculate the line and column.
     * - in the case that lo = 0 and s->linemap[0] is still greater than position, we
     *   are on the first line and our column is position + 1. */
    int32_t hi = s->linemaplen; /* hi is greater than the actual line */
    int32_t lo = 0; /* lo is less than or equal to the actual line */
    LineCol ret;
    while (lo + 1 < hi) {
        int32_t mid = lo + (hi - lo) / 2;
        if (s->linemap[mid] >= position) {
            hi = mid;
        } else {
            lo = mid;
        }
    }
    /* first line case */
    if (s->linemaplen == 0 || (lo == 0 && s->linemap[0] >= position)) {
        ret.line = 1;
        ret.col = position + 1;
    } else {
        ret.line = lo + 2;
        ret.col = position - s->linemap[lo];
    }
    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x60(%rax)
jge 0x5a2cc
movl $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x5a242
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xa, %eax
jne 0x5a232
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x5a234
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x5a20f
movslq -0x18(%rbp), %rdi
shlq $0x2, %rdi
callq 0x3a870
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x5a2b6
movq -0x38(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xa, %eax
jne 0x5a2a6
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x30(%rbp)
movl %edx, (%rax,%rcx,4)
jmp 0x5a2a8
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x5a266
movl -0x18(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x60(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x10(%rbp), %rax
movl 0x60(%rax), %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
cmpl -0x3c(%rbp), %eax
jge 0x5a32a
movl -0x40(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x3c(%rbp), %eax
subl -0x40(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x48(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x14(%rbp), %eax
jl 0x5a322
movl -0x44(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x5a328
movl -0x44(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x5a2dd
movq -0x10(%rbp), %rax
cmpl $0x0, 0x60(%rax)
je 0x5a349
cmpl $0x0, -0x40(%rbp)
jne 0x5a35b
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jl 0x5a35b
movl $0x1, -0x8(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x5a379
movl -0x40(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x8(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x50(%rcx), %rcx
movslq -0x40(%rbp), %rdx
subl (%rcx,%rdx,4), %eax
movl %eax, -0x4(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)