// File: vsklad[P]cgen/./bal/cnf/cnf/cnfclauses.hpp
// Function: bal::ca_insert_last_literal_sorted(unsigned short&, unsigned short&, unsigned int*)
inline void ca_insert_last_literal_sorted(uint16_t& flags, clause_size_t& literals_size, literalid_t* const literals) {
        clause_size_t index = 0;
        while (index < literals_size) {
            if (literals[literals_size] <= literals[index]) {
                break;
            };
            index++;
        };
        if (index == literals_size) {
            // new literal is the last one, nothing else to do
            literals_size++;
        } else if (literals[literals_size] == literals[index]) {
            // nothing to insert, transform flags
            switch((index << 4) | literals_size) {
                case 0x01: // l0 = l1
                    flags = (flags & 0x0001) | (flags & 0x0018) >> 2 |
                    (flags & 0x0180) >> 4 | (flags & 0x1800) >> 6 | (flags & 0x8000) >> 8;
                    break;
                case 0x02: // l0 = l2
                    flags = (flags & 0x0005) | (flags & 0x05A0) >> 4 | (flags & 0xA000) >> 8;
                    break;
                case 0x03: // l0 = l3
                    flags = (flags & 0x0055) | (flags & 0xAA00) >> 8;
                    break;
                case 0x12: // l1 = l2
                    flags = (flags & 0x0003) | (flags & 0x03C0) >> 4 | (flags & 0xC000) >> 8;
                    break;
                case 0x13: // l1 = l3
                    flags = (flags & 0x0033) | (flags & 0xCC00) >> 8;
                    break;
                case 0x23: // l2 = l3
                    flags = (flags & 0x000F) | (flags & 0xF000) >> 8;
                    break;
            };
        } else {
            // insert into index position and update flags
            switch((index << 4) | literals_size) {
                case 0x01: // l1 = l0
                    flags = (flags & 0x9999) | (flags & 0x2222) << 1 | (flags & 0x4444) >> 1; \
                    std::swap(literals[0], literals[1]);
                    break;
                case 0x02: // l2 = l0
                    flags = (flags & 0x8181) |
                    (flags & 0x0202) << 1 | (flags & 0x0404) << 2 | (flags & 0x0808) << 3 |
                    (flags & 0x1010) >> 3 | (flags & 0x2020) >> 2 | (flags & 0x4040) >> 1;
                    std::swap(literals[1], literals[2]);
                    std::swap(literals[0], literals[1]);
                    break;
                case 0x03: // l3 = l0
                    flags = (flags & 0x8001) |
                    (flags & 0x0002) << 1 | (flags & 0x0004) << 2 |
                    (flags & 0x0008) << 3 | (flags & 0x0010) << 4 |
                    (flags & 0x0020) << 5 | (flags & 0x0040) << 6 |
                    (flags & 0x0080) << 7 | (flags & 0x0100) >> 7 |
                    (flags & 0x0200) >> 6 | (flags & 0x0400) >> 5 |
                    (flags & 0x0800) >> 4 | (flags & 0x1000) >> 3 |
                    (flags & 0x2000) >> 2 | (flags & 0x4000) >> 1;
                    
                    std::swap(literals[2], literals[3]);
                    std::swap(literals[1], literals[2]);
                    std::swap(literals[0], literals[1]);
                    break;
                case 0x12: // l2 = l1
                    flags = (flags & 0xC3C3) | (flags & 0x0C0C) << 2 | (flags & 0x3030) >> 2;
                    std::swap(literals[1], literals[2]);
                    break;
                case 0x13: // l3 = l1
                    flags = (flags & 0xC003) |
                    (flags & 0x000C) << 2 | (flags & 0x0030) << 4 | (flags & 0x00C0) << 6 |
                    (flags & 0x0300) >> 6 | (flags & 0x0C00) >> 4 | (flags & 0x3000) >> 2;
                    std::swap(literals[2], literals[3]);
                    std::swap(literals[1], literals[2]);
                    break;
                case 0x23: // l3 -> l2
                    flags = (flags & 0xF00F) | (flags & 0x00F0) << 4 | (flags & 0x0F00) >> 4;
                    std::swap(literals[2], literals[3]);
                    break;
            };
            literals_size++;
        };
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movw $0x0, -0x1a(%rbp)
movzwl -0x1a(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl (%rcx), %ecx
cmpl %ecx, %eax
jge 0x188b4
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl (%rcx), %ecx
movl (%rax,%rcx,4), %eax
movq -0x18(%rbp), %rcx
movzwl -0x1a(%rbp), %edx
cmpl (%rcx,%rdx,4), %eax
ja 0x188a6
jmp 0x188b4
movw -0x1a(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x1a(%rbp)
jmp 0x1887a
movzwl -0x1a(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl (%rcx), %ecx
cmpl %ecx, %eax
jne 0x188d6
movq -0x10(%rbp), %rax
movw (%rax), %cx
addw $0x1, %cx
movw %cx, (%rax)
jmp 0x18e44
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl (%rcx), %ecx
movl (%rax,%rcx,4), %eax
movq -0x18(%rbp), %rcx
movzwl -0x1a(%rbp), %edx
cmpl (%rcx,%rdx,4), %eax
jne 0x18a7d
movzwl -0x1a(%rbp), %eax
shll $0x4, %eax
movq -0x10(%rbp), %rcx
movzwl (%rcx), %ecx
orl %ecx, %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x28(%rbp)
subl $0x22, %eax
ja 0x18a78
movq -0x28(%rbp), %rax
leaq 0x4550b(%rip), %rcx     # 0x5de2c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0x1, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x18, %ecx
sarl $0x2, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x180, %ecx            # imm = 0x180
sarl $0x4, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x1800, %ecx           # imm = 0x1800
sarl $0x6, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x8000, %ecx           # imm = 0x8000
sarl $0x8, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
jmp 0x18a78
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0x5, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x5a0, %ecx            # imm = 0x5A0
sarl $0x4, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xa000, %ecx           # imm = 0xA000
sarl $0x8, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
jmp 0x18a78
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0x55, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xaa00, %ecx           # imm = 0xAA00
sarl $0x8, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
jmp 0x18a78
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0x3, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x3c0, %ecx            # imm = 0x3C0
sarl $0x4, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xc000, %ecx           # imm = 0xC000
sarl $0x8, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
jmp 0x18a78
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0x33, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xcc00, %ecx           # imm = 0xCC00
sarl $0x8, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
jmp 0x18a78
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0xf, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xf000, %ecx           # imm = 0xF000
sarl $0x8, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
jmp 0x18e42
movzwl -0x1a(%rbp), %eax
shll $0x4, %eax
movq -0x10(%rbp), %rcx
movzwl (%rcx), %ecx
orl %ecx, %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x22, %eax
ja 0x18e34
movq -0x30(%rbp), %rax
leaq 0x452f7(%rip), %rcx     # 0x5dda0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0x9999, %eax           # imm = 0x9999
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x2222, %ecx           # imm = 0x2222
shll %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x4444, %ecx           # imm = 0x4444
sarl %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x114e0
jmp 0x18e34
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0x8181, %eax           # imm = 0x8181
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x202, %ecx            # imm = 0x202
shll %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x404, %ecx            # imm = 0x404
shll $0x2, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x808, %ecx            # imm = 0x808
shll $0x3, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x1010, %ecx           # imm = 0x1010
sarl $0x3, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x2020, %ecx           # imm = 0x2020
sarl $0x2, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x4040, %ecx           # imm = 0x4040
sarl %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
movq -0x18(%rbp), %rdi
addq $0x4, %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x114e0
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x114e0
jmp 0x18e34
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0x8001, %eax           # imm = 0x8001
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x2, %ecx
shll %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x4, %ecx
shll $0x2, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x8, %ecx
shll $0x3, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x10, %ecx
shll $0x4, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x20, %ecx
shll $0x5, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x40, %ecx
shll $0x6, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x80, %ecx
shll $0x7, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x100, %ecx            # imm = 0x100
sarl $0x7, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x200, %ecx            # imm = 0x200
sarl $0x6, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x400, %ecx            # imm = 0x400
sarl $0x5, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x800, %ecx            # imm = 0x800
sarl $0x4, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x1000, %ecx           # imm = 0x1000
sarl $0x3, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x2000, %ecx           # imm = 0x2000
sarl $0x2, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x4000, %ecx           # imm = 0x4000
sarl %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
addq $0xc, %rsi
callq 0x114e0
movq -0x18(%rbp), %rdi
addq $0x4, %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x114e0
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x114e0
jmp 0x18e34
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0xc3c3, %eax           # imm = 0xC3C3
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xc0c, %ecx            # imm = 0xC0C
shll $0x2, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x3030, %ecx           # imm = 0x3030
sarl $0x2, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
movq -0x18(%rbp), %rdi
addq $0x4, %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x114e0
jmp 0x18e34
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0xc003, %eax           # imm = 0xC003
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xc, %ecx
shll $0x2, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x30, %ecx
shll $0x4, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xc0, %ecx
shll $0x6, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x300, %ecx            # imm = 0x300
sarl $0x6, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xc00, %ecx            # imm = 0xC00
sarl $0x4, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x3000, %ecx           # imm = 0x3000
sarl $0x2, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
addq $0xc, %rsi
callq 0x114e0
movq -0x18(%rbp), %rdi
addq $0x4, %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x114e0
jmp 0x18e34
movq -0x8(%rbp), %rax
movzwl (%rax), %eax
andl $0xf00f, %eax           # imm = 0xF00F
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xf0, %ecx
shll $0x4, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0xf00, %ecx            # imm = 0xF00
sarl $0x4, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
addq $0xc, %rsi
callq 0x114e0
movq -0x10(%rbp), %rax
movw (%rax), %cx
addw $0x1, %cx
movw %cx, (%rax)
jmp 0x18e44
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)