// File: night-shift[P]Srl/src/lib/fpconv/fpconv.c
// Function: emit_digits
static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
{
    int exp = absv(K + ndigits - 1);

    int max_trailing_zeros = 7;

    if(neg) {
        max_trailing_zeros -= 1;
    }

    /* write plain integer */
    if(K >= 0 && (exp < (ndigits + max_trailing_zeros))) {

        memcpy(dest, digits, ndigits);
        memset(dest + ndigits, '0', K);

        return ndigits + K;
    }

    /* write decimal w/o scientific notation */
    if(K < 0 && (K > -7 || exp < 4)) {
        int offset = ndigits - absv(K);
        /* fp < 1.0 -> write leading zero */
        if(offset <= 0) {
            offset = -offset;
            dest[0] = '0';
            dest[1] = '.';
            memset(dest + 2, '0', offset);
            memcpy(dest + offset + 2, digits, ndigits);

            return ndigits + 2 + offset;

        /* fp > 1.0 */
        } else {
            memcpy(dest, digits, offset);
            dest[offset] = '.';
            memcpy(dest + offset + 1, digits + offset, ndigits - offset);

            return ndigits + 1;
        }
    }

    /* write decimal w/ scientific notation */
    ndigits = minv(ndigits, 18 - neg);

    int idx = 0;
    dest[idx++] = digits[0];

    if(ndigits > 1) {
        dest[idx++] = '.';
        memcpy(dest + idx, digits + 1, ndigits - 1);
        idx += ndigits - 1;
    }

    dest[idx++] = 'e';

    char sign = K + ndigits - 1 < 0 ? '-' : '+';
    dest[idx++] = sign;

    int cent = 0;

    if(exp > 99) {
        cent = exp / 100;
        dest[idx++] = cent + '0';
        exp -= cent * 100;
    }
    if(exp > 9) {
        int dec = exp / 10;
        dest[idx++] = dec + '0';
        exp -= dec * 10;

    } else if(cent) {
        dest[idx++] = '0';
    }

    dest[idx++] = exp % 10 + '0';

    return idx;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movb %r8b, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
andb $0x1, %al
movb %al, -0x25(%rbp)
movl -0x24(%rbp), %eax
addl -0x14(%rbp), %eax
subl $0x1, %eax
cmpl $0x0, %eax
jge 0x681ce
movl -0x24(%rbp), %ecx
addl -0x14(%rbp), %ecx
subl $0x1, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, -0x48(%rbp)
jmp 0x681da
movl -0x24(%rbp), %eax
addl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl $0x7, -0x30(%rbp)
testb $0x1, -0x25(%rbp)
je 0x681f6
movl -0x30(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x24(%rbp)
jl 0x68241
movl -0x2c(%rbp), %eax
movl -0x14(%rbp), %ecx
addl -0x30(%rbp), %ecx
cmpl %ecx, %eax
jge 0x68241
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x35360
movq -0x20(%rbp), %rdi
movslq -0x14(%rbp), %rax
addq %rax, %rdi
movslq -0x24(%rbp), %rdx
movl $0x30, %esi
callq 0x343e0
movl -0x14(%rbp), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x684eb
cmpl $0x0, -0x24(%rbp)
jge 0x68335
cmpl $-0x7, -0x24(%rbp)
jg 0x6825b
cmpl $0x4, -0x2c(%rbp)
jge 0x68335
movl -0x14(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x68271
xorl %eax, %eax
subl -0x24(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x68277
movl -0x24(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x4c(%rbp), %eax
movl -0x50(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jg 0x682e2
xorl %eax, %eax
subl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
movb $0x30, (%rax)
movq -0x20(%rbp), %rax
movb $0x2e, 0x1(%rax)
movq -0x20(%rbp), %rdi
addq $0x2, %rdi
movslq -0x34(%rbp), %rdx
movl $0x30, %esi
callq 0x343e0
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rax
addq %rax, %rdi
addq $0x2, %rdi
movq -0x10(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x35360
movl -0x14(%rbp), %eax
addl $0x2, %eax
addl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x684eb
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movslq -0x34(%rbp), %rdx
callq 0x35360
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
movb $0x2e, (%rax,%rcx)
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rax
addq %rax, %rdi
addq $0x1, %rdi
movq -0x10(%rbp), %rsi
movslq -0x34(%rbp), %rax
addq %rax, %rsi
movl -0x14(%rbp), %eax
subl -0x34(%rbp), %eax
movslq %eax, %rdx
callq 0x35360
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x684eb
movl -0x14(%rbp), %eax
movb -0x25(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %edx
movl $0x12, %ecx
subl %edx, %ecx
cmpl %ecx, %eax
jge 0x68354
movl -0x14(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0x68366
movb -0x25(%rbp), %al
andb $0x1, %al
movzbl %al, %ecx
movl $0x12, %eax
subl %ecx, %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x14(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movb (%rax), %dl
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x38(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
cmpl $0x1, -0x14(%rbp)
jle 0x683d7
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movb $0x2e, (%rax,%rcx)
movq -0x20(%rbp), %rdi
movslq -0x38(%rbp), %rax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
addq $0x1, %rsi
movl -0x14(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdx
callq 0x35360
movl -0x14(%rbp), %eax
subl $0x1, %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movb $0x65, (%rax,%rcx)
movl -0x24(%rbp), %edx
addl -0x14(%rbp), %edx
subl $0x1, %edx
movl $0x2b, %eax
movl $0x2d, %ecx
cmpl $0x0, %edx
cmovll %ecx, %eax
movb %al, -0x39(%rbp)
movb -0x39(%rbp), %dl
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x38(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl $0x0, -0x40(%rbp)
cmpl $0x63, -0x2c(%rbp)
jle 0x68465
movl -0x2c(%rbp), %eax
movl $0x64, %ecx
cltd
idivl %ecx
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
addl $0x30, %eax
movb %al, %dl
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x38(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
imull $0x64, -0x40(%rbp), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x9, -0x2c(%rbp)
jle 0x684a4
movl -0x2c(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
addl $0x30, %eax
movb %al, %dl
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x38(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
imull $0xa, -0x44(%rbp), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
jmp 0x684c2
cmpl $0x0, -0x40(%rbp)
je 0x684c0
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movb $0x30, (%rax,%rcx)
jmp 0x684c2
movl -0x2c(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
addl $0x30, %edx
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x38(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)