// File: /workspace/github_c_makefile_star2_O1/erikdubbelboer[P]discnt/src/sparkline.c
// Function: sparklineRenderRange
sds sparklineRenderRange(sds output, struct sequence *seq, int rows, int offset, int len, int flags) {
    int j;
    double relmax = seq->max - seq->min;
    int steps = charset_len*rows;
    int row = 0;
    char *chars = zmalloc(len);
    int loop = 1;
    int opt_fill = flags & SPARKLINE_FILL;
    int opt_log = flags & SPARKLINE_LOG_SCALE;

    if (opt_log) {
        relmax = log(relmax+1);
    } else if (relmax == 0) {
        relmax = 1;
    }

    while(loop) {
        loop = 0;
        memset(chars,' ',len);
        for (j = 0; j < len; j++) {
            struct sample *s = &seq->samples[j+offset];
            double relval = s->value - seq->min;
            int step;

            if (opt_log) relval = log(relval+1);
            step = (int) (relval*steps)/relmax;
            if (step < 0) step = 0;
            if (step >= steps) step = steps-1;

            if (row < rows) {
                /* Print the character needed to create the sparkline */
                int charidx = step-((rows-row-1)*charset_len);
                loop = 1;
                if (charidx >= 0 && charidx < charset_len) {
                    chars[j] = opt_fill ? charset_fill[charidx] :
                                          charset[charidx];
                } else if(opt_fill && charidx >= charset_len) {
                    chars[j] = '|';
                }
            } else {
                /* Labels spacing */
                if (seq->labels && row-rows < label_margin_top) {
                    loop = 1;
                    break;
                }
                /* Print the label if needed. */
                if (s->label) {
                    int label_len = strlen(s->label);
                    int label_char = row - rows - label_margin_top;

                    if (label_len > label_char) {
                        loop = 1;
                        chars[j] = s->label[label_char];
                    }
                }
            }
        }
        if (loop) {
            row++;
            output = sdscatlen(output,chars,len);
            output = sdscatlen(output,"\n",1);
        }
    }
    zfree(chars);
    return output;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r9d, %ebx
movl %ecx, %ebp
movq %rsi, %r15
movq %rdi, 0x28(%rsp)
movsd 0x18(%rsi), %xmm0
subsd 0x10(%rsi), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rdx, 0x30(%rsp)
leal (%rdx,%rdx,2), %eax
movq %rax, 0x50(%rsp)
movslq %r8d, %rdi
movq %rdi, 0x38(%rsp)
callq 0x1fea5
movl %ebx, 0xc(%rsp)
movq %rax, 0x18(%rsp)
testb $0x2, %bl
jne 0x30efa
xorpd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movapd %xmm1, %xmm0
jne 0x30f0d
jp 0x30f0d
movsd 0x24278(%rip), %xmm0    # 0x55170
jmp 0x30f0d
movsd 0x10(%rsp), %xmm0
addsd 0x24268(%rip), %xmm0    # 0x55170
callq 0x123c0
movq 0x30(%rsp), %rax
notl %eax
movq %rax, 0x60(%rsp)
movq 0x50(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x70(%rsp)
decl %eax
movl %eax, 0x48(%rsp)
movslq %ebp, %rax
shlq $0x4, %rax
orq $0x8, %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x20(%rsp)
movsd %xmm0, 0x10(%rsp)
movq 0x18(%rsp), %rdi
movl $0x20, %esi
movq 0x38(%rsp), %rbx
movq %rbx, %rdx
callq 0x12380
testl %ebx, %ebx
jle 0x310b4
movq 0x60(%rsp), %rax
movq 0x20(%rsp), %rcx
addl %ecx, %eax
movl %eax, 0x44(%rsp)
cltq
movq %rax, 0x68(%rsp)
movl %ecx, %eax
notl %eax
addl 0x30(%rsp), %eax
leal (%rax,%rax,2), %eax
negl %eax
movl %eax, 0x4c(%rsp)
xorl %r13d, %r13d
movl $0x1, %r12d
movq 0x58(%rsp), %r14
movl 0xc(%rsp), %edx
movq 0x8(%r15), %rbx
movsd -0x8(%rbx,%r14), %xmm0
subsd 0x10(%r15), %xmm0
testb $0x2, %dl
je 0x30fd0
addsd 0x241a9(%rip), %xmm0    # 0x55170
callq 0x123c0
movl 0xc(%rsp), %edx
movq 0x20(%rsp), %rax
cmpl 0x30(%rsp), %eax
jge 0x31028
mulsd 0x70(%rsp), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
divsd 0x10(%rsp), %xmm0
cvttsd2si %xmm0, %eax
testl %eax, %eax
jg 0x30ff9
xorl %eax, %eax
cmpl 0x50(%rsp), %eax
jl 0x31003
movl 0x48(%rsp), %eax
addl 0x4c(%rsp), %eax
cmpl $0x2, %eax
ja 0x31061
testb $0x1, %dl
movl %eax, %eax
leaq 0x27873(%rip), %rbp     # 0x5888b
leaq 0x27870(%rip), %rcx     # 0x5888f
cmoveq %rcx, %rbp
addq %rax, %rbp
jmp 0x3105c
setg %al
cmpl $0x0, 0x4(%r15)
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x31089
movq (%rbx,%r14), %rbp
movb $0x1, %bl
testq %rbp, %rbp
je 0x31091
movq %rbp, %rdi
callq 0x12250
movl 0xc(%rsp), %edx
cmpl %eax, 0x44(%rsp)
jge 0x31091
addq 0x68(%rsp), %rbp
movb (%rbp), %cl
jmp 0x31075
movb $0x1, %bl
movl $0x1, %r13d
testb $0x1, %dl
je 0x31091
movb $0x7c, %cl
cmpl $0x2, %eax
jle 0x31091
movq 0x18(%rsp), %rax
movb %cl, -0x1(%rax,%r12)
movb $0x1, %bl
movl $0x1, %r13d
jmp 0x31091
movl $0x1, %r13d
xorl %ebx, %ebx
testb %bl, %bl
je 0x310ac
leaq 0x1(%r12), %rax
addq $0x10, %r14
cmpq 0x38(%rsp), %r12
movq %rax, %r12
jl 0x30fa9
testl %r13d, %r13d
sete %bl
jmp 0x310b6
movb $0x1, %bl
testb %bl, %bl
jne 0x310f3
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x1e66d
movl $0x1, %edx
movq %rax, %rdi
leaq 0x22c9f(%rip), %rsi     # 0x53d88
callq 0x1e66d
movq %rax, 0x28(%rsp)
testb %bl, %bl
je 0x30f50
movq 0x18(%rsp), %rdi
callq 0x1ffff
movq 0x28(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq