// File: /workspace/github_c_makefile_star2_O0/excess-c64[P]lib1541img/src/lib/1541img/zc45writer.c
// Function: writeRle
static int writeRle(uint8_t *zcfile, size_t zcfilelen, size_t *wpos,
        const uint8_t *data, uint8_t rlelen, uint8_t repcode)
{
    if (writebyte(zcfile, wpos, zcfilelen, rlelen) < 0) return -1;
    if (writebyte(zcfile, wpos, zcfilelen, repcode) < 0) return -1;
    for (int i = 0; i < 256; ++i)
    {
        uint8_t b = data[i];
        uint8_t repcnt = 0;
        for (int j = i+1; j < 256; ++j)
        {
            if (data[j] == b) ++repcnt;
            else break;
        }
        if (repcnt > 2)
        {
            if (writebyte(zcfile, wpos, zcfilelen, repcode) < 0) return -1;
            if (writebyte(zcfile, wpos, zcfilelen, repcnt+1) < 0) return -1;
            i += repcnt;
        }
        if (writebyte(zcfile, wpos, zcfilelen, b) < 0) return -1;
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl %r8d, -0x54(%rbp)
movq %rcx, %rax
movl -0x54(%rbp), %ecx
movq %rax, -0x50(%rbp)
movq %rdx, %rax
movq -0x50(%rbp), %rdx
movq %rax, -0x48(%rbp)
movq %rsi, %rax
movq -0x48(%rbp), %rsi
movq %rax, -0x40(%rbp)
movq %rdi, %r8
movq -0x40(%rbp), %rdi
movb %r9b, %al
movq %r8, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movb %cl, -0x29(%rbp)
movb %al, -0x2a(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
movzbl -0x29(%rbp), %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xc5d2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc6f6
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
movzbl -0x2a(%rbp), %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xc5f8
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc6f6
movl $0x0, -0x30(%rbp)
cmpl $0x100, -0x30(%rbp)     # imm = 0x100
jge 0xc6ef
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x31(%rbp)
movb $0x0, -0x32(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
cmpl $0x100, -0x38(%rbp)     # imm = 0x100
jge 0xc65d
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movzbl -0x31(%rbp), %ecx
cmpl %ecx, %eax
jne 0xc64e
movb -0x32(%rbp), %al
addb $0x1, %al
movb %al, -0x32(%rbp)
jmp 0xc650
jmp 0xc65d
jmp 0xc652
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xc627
movzbl -0x32(%rbp), %eax
cmpl $0x2, %eax
jle 0xc6bc
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
movzbl -0x2a(%rbp), %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xc689
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc6f6
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
movzbl -0x32(%rbp), %eax
addl $0x1, %eax
movzbl %al, %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xc6b2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc6f6
movzbl -0x32(%rbp), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
movzbl -0x31(%rbp), %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xc6df
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc6f6
jmp 0xc6e1
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xc5ff
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop