// File: /workspace/github_cpp_makefile_star2_O1/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
movzwl (%rsi), %eax
testq %rax, %rax
je 0x14621
movl (%rdx,%rax,4), %r8d
xorl %ecx, %ecx
cmpl (%rdx,%rcx,4), %r8d
jbe 0x14623
incq %rcx
cmpw %cx, %ax
jne 0x1460f
movl %eax, %ecx
jmp 0x14623
xorl %ecx, %ecx
cmpw %ax, %cx
jne 0x14632
incl %eax
movw %ax, (%rsi)
jmp 0x149b2
movzwl %cx, %r8d
movl (%rdx,%rax,4), %r9d
movl %r8d, %ecx
shll $0x4, %ecx
orl %eax, %ecx
cmpl (%rdx,%r8,4), %r9d
jne 0x1467f
cmpl $0x11, %ecx
jg 0x14784
cmpl $0x1, %ecx
je 0x14801
cmpl $0x2, %ecx
je 0x148aa
cmpl $0x3, %ecx
jne 0x149b2
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0x55, %ecx
shrl $0x8, %eax
andl $-0x56, %eax
jmp 0x1493f
cmpl $0x11, %ecx
jg 0x147b2
cmpl $0x1, %ecx
je 0x14832
cmpl $0x2, %ecx
je 0x148c6
cmpl $0x3, %ecx
jne 0x149af
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0x8001, %ecx           # imm = 0x8001
movl %eax, %r8d
andl $0x2, %r8d
leal (%rcx,%r8,2), %ecx
movl %eax, %r8d
andl $0x4, %r8d
leal (%rcx,%r8,4), %ecx
movl %eax, %r8d
andl $0x8, %r8d
leal (%rcx,%r8,8), %ecx
movl %eax, %r8d
andl $0x10, %r8d
shll $0x4, %r8d
orl %ecx, %r8d
movl %eax, %r9d
andl $0x20, %r9d
shll $0x5, %r9d
orl %r8d, %r9d
movl %eax, %ecx
andl $0x40, %ecx
shll $0x6, %ecx
orl %r9d, %ecx
movl %eax, %r8d
shll $0x7, %r8d
andl $0x4000, %r8d           # imm = 0x4000
movl %eax, %r9d
shrl $0x7, %r9d
andl $0x2, %r9d
orl %r8d, %r9d
movl %eax, %r8d
shrl $0x6, %r8d
andl $0x8, %r8d
orl %r9d, %r8d
movl %eax, %r9d
shrl $0x5, %r9d
andl $0x20, %r9d
orl %r8d, %r9d
movl %eax, %r8d
shrl $0x4, %r8d
andl $0x80, %r8d
orl %r9d, %r8d
movl %eax, %r9d
shrl $0x3, %r9d
andl $0x200, %r9d            # imm = 0x200
orl %r8d, %r9d
orl %ecx, %r9d
movl %eax, %ecx
shrl $0x2, %ecx
andl $0x800, %ecx            # imm = 0x800
shrl %eax
andl $0x2000, %eax           # imm = 0x2000
orl %ecx, %eax
orl %r9d, %eax
movw %ax, (%rdi)
movl 0xc(%rdx), %eax
movq 0x4(%rdx), %rcx
movq %rcx, 0x8(%rdx)
movl (%rdx), %ecx
movl %eax, (%rdx)
movl %ecx, 0x4(%rdx)
jmp 0x149af
cmpl $0x12, %ecx
je 0x14860
cmpl $0x13, %ecx
je 0x14931
cmpl $0x23, %ecx
jne 0x149b2
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0xf, %ecx
shrl $0x8, %eax
andl $-0x10, %eax
jmp 0x1493f
cmpl $0x12, %ecx
je 0x1487a
cmpl $0x13, %ecx
je 0x14946
cmpl $0x23, %ecx
jne 0x149af
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0xf00f, %ecx           # imm = 0xF00F
movl %eax, %r8d
shll $0x4, %r8d
andl $0xf00, %r8d            # imm = 0xF00
orl %ecx, %r8d
shrl $0x4, %eax
andl $0xf0, %eax
orl %r8d, %eax
movw %ax, (%rdi)
rolq $0x20, 0x8(%rdx)
jmp 0x149af
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0x1, %ecx
movl %eax, %edx
shrl $0x2, %edx
andl $0x6, %edx
orl %ecx, %edx
movl %eax, %ecx
shrl $0x4, %ecx
andl $0x18, %ecx
orl %edx, %ecx
movl %eax, %edx
shrl $0x6, %edx
andl $0x60, %edx
orl %ecx, %edx
shrl $0x8, %eax
andl $-0x80, %eax
jmp 0x148c2
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0x9999, %ecx           # imm = 0x9999
movl $0x2222, %r8d           # imm = 0x2222
movl %eax, %r9d
andl %r8d, %r9d
leal (%rcx,%r9,2), %ecx
shrl %eax
andl %r8d, %eax
orl %ecx, %eax
movw %ax, (%rdi)
rolq $0x20, (%rdx)
jmp 0x149af
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0x3, %ecx
movl %eax, %edx
shrl $0x4, %edx
andl $0x3c, %edx
orl %ecx, %edx
shrl $0x8, %eax
andl $-0x40, %eax
jmp 0x148c2
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0xc3c3, %ecx           # imm = 0xC3C3
movl $0xc0c, %r8d            # imm = 0xC0C
movl %eax, %r9d
andl %r8d, %r9d
leal (%rcx,%r9,4), %ecx
shrl $0x2, %eax
andl %r8d, %eax
orl %ecx, %eax
movw %ax, (%rdi)
rolq $0x20, 0x4(%rdx)
jmp 0x149af
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0x5, %ecx
movl %eax, %edx
shrl $0x4, %edx
andl $0x5a, %edx
orl %ecx, %edx
shrl $0x8, %eax
andl $-0x60, %eax
orl %edx, %eax
jmp 0x14941
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0x8181, %ecx           # imm = 0x8181
movl $0x202, %r8d            # imm = 0x202
movl %eax, %r9d
andl %r8d, %r9d
leal (%rcx,%r9,2), %ecx
movl %eax, %r9d
andl $0x404, %r9d            # imm = 0x404
leal (%rcx,%r9,4), %ecx
movl $0x808, %r9d            # imm = 0x808
movl %eax, %r10d
andl %r9d, %r10d
leal (%rcx,%r10,8), %ecx
movl %eax, %r10d
shrl $0x3, %r10d
andl %r8d, %r10d
orl %ecx, %r10d
movl %eax, %ecx
shrl $0x2, %ecx
andl %r9d, %ecx
orl %r10d, %ecx
shrl %eax
andl $0x2020, %eax           # imm = 0x2020
orl %ecx, %eax
movw %ax, (%rdi)
movl 0x8(%rdx), %eax
movq (%rdx), %rcx
movl %eax, (%rdx)
movq %rcx, 0x4(%rdx)
jmp 0x149af
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0x33, %ecx
shrl $0x8, %eax
andl $-0x34, %eax
orl %ecx, %eax
movw %ax, (%rdi)
jmp 0x149b2
movzwl (%rdi), %eax
movl %eax, %ecx
andl $0xc003, %ecx           # imm = 0xC003
movl %eax, %r8d
andl $0xc, %r8d
leal (%rcx,%r8,4), %ecx
movl %eax, %r8d
andl $0x30, %r8d
shll $0x4, %r8d
orl %ecx, %r8d
movl %eax, %ecx
shll $0x6, %ecx
andl $0x3000, %ecx           # imm = 0x3000
orl %r8d, %ecx
movl %eax, %r8d
shrl $0x6, %r8d
andl $0xc, %r8d
orl %ecx, %r8d
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xc0, %ecx
orl %r8d, %ecx
shrl $0x2, %eax
andl $0xc00, %eax            # imm = 0xC00
orl %ecx, %eax
movw %ax, (%rdi)
movl 0xc(%rdx), %eax
movq 0x4(%rdx), %rcx
movl %eax, 0x4(%rdx)
movq %rcx, 0x8(%rdx)
incw (%rsi)
popq %rbp
retq