// File: Chaircrusher[P]primeratiogen/Bjorklund.cpp
// Function: Bjorklund::iter()
void Bjorklund::iter(){
    //\Bjorklund algorithm
    //\do E[k,n]. k is number of one's in sequence, and n is the length of sequence.
    int divisor = lengthOfSeq - pulseAmt; //initial amount of zero's
    if(divisor < 0) throw out_of_range{"Divisor has to be greator than 0"};
    
    remainder.push_back(pulseAmt);
    //iteration
    int index = 0; //we start algorithm from first index.
    
    while(true){
        count.push_back(std::floor(divisor / remainder[index]));
        remainder.push_back(divisor % remainder[index]);
        divisor = remainder.at(index);
        index += 1; //move to next step.
        if(remainder[index] <= 1){
            break;
        }
    }
    count.push_back(divisor);
    buildSeq(index); //place one's and zero's
    reverse(sequence.begin(), sequence.end());
    
    //position correction. some of result of algorithm is one step rotated.
    int zeroCount =0;
    if(sequence.at(0) != 1){
        do{
            zeroCount ++;
        }while(sequence.at(zeroCount) == 0);
        std::rotate(sequence.begin(), sequence.begin() + zeroCount, sequence.end());
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x128(%rbp)
movl 0x58(%rcx), %eax
subl 0x5c(%rcx), %eax
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jge 0x5ed5
movl $0x10, %edi
callq 0x2080
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x130(%rbp)
leaq 0x4218(%rip), %rsi      # 0xa0af
callq 0x2030
jmp 0x5e9e
movq -0x130(%rbp), %rdi
movq 0x9124(%rip), %rsi      # 0xefd0
movq 0x9115(%rip), %rdx      # 0xefc8
callq 0x21b0
movq -0x130(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x20a0
jmp 0x61d0
movq -0x128(%rbp), %rdi
movq %rdi, %rsi
addq $0x5c, %rsi
callq 0x6560
movl $0x0, -0x20(%rbp)
movq -0x128(%rbp), %rdi
movq %rdi, %rax
addq $0x18, %rax
movq %rax, -0x140(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x144(%rbp)
movslq -0x20(%rbp), %rsi
callq 0x6620
movq %rax, %rcx
movl -0x144(%rbp), %eax
cltd
idivl (%rcx)
movl %eax, %edi
callq 0x6600
movq -0x140(%rbp), %rdi
cvttsd2si %xmm0, %eax
movl %eax, -0x24(%rbp)
leaq -0x24(%rbp), %rsi
callq 0x65d0
movq -0x128(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, -0x134(%rbp)
movslq -0x20(%rbp), %rsi
callq 0x6620
movq -0x128(%rbp), %rdi
movq %rax, %rcx
movl -0x134(%rbp), %eax
cltd
idivl (%rcx)
movl %edx, -0x28(%rbp)
leaq -0x28(%rbp), %rsi
callq 0x65d0
movq -0x128(%rbp), %rdi
movslq -0x20(%rbp), %rsi
callq 0x6640
movq -0x128(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0xc(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movslq -0x20(%rbp), %rsi
callq 0x6620
cmpl $0x1, (%rax)
jg 0x5fad
jmp 0x5fb2
jmp 0x5eef
movq -0x128(%rbp), %rdi
addq $0x18, %rdi
leaq -0xc(%rbp), %rsi
callq 0x6560
movq -0x128(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x62c0
movq -0x128(%rbp), %rdi
addq $0x30, %rdi
movq %rdi, -0x150(%rbp)
callq 0x3680
movq -0x150(%rbp), %rdi
movl %edx, -0x40(%rbp)
movq %rax, -0x48(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
callq 0x66e0
movl %edx, -0x60(%rbp)
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %esi
movq -0x58(%rbp), %rdx
movl -0x50(%rbp), %ecx
callq 0x6680
movq -0x128(%rbp), %rdi
movl $0x0, -0x6c(%rbp)
addq $0x30, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6700
movq %rax, -0x80(%rbp)
movq %rdx, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x2fb0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
je 0x61c7
jmp 0x6071
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movq -0x128(%rbp), %rdi
addq $0x30, %rdi
movslq -0x6c(%rbp), %rsi
callq 0x6700
movq %rax, -0x90(%rbp)
movq %rdx, -0x88(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x2fb0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x6071
movq -0x128(%rbp), %rdi
addq $0x30, %rdi
movq %rdi, -0x158(%rbp)
callq 0x3680
movq -0x158(%rbp), %rdi
movl %edx, -0xa8(%rbp)
movq %rax, -0xb0(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0x98(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xa0(%rbp)
callq 0x3680
movl %edx, -0xd8(%rbp)
movq %rax, -0xe0(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0xc8(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movslq -0x6c(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x5c20
movq -0x158(%rbp), %rdi
movl %edx, -0xe8(%rbp)
movq %rax, -0xf0(%rbp)
movl -0xe8(%rbp), %eax
movl %eax, -0xb8(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0xc0(%rbp)
callq 0x66e0
movl %edx, -0x108(%rbp)
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x100(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0xf8(%rbp)
movq -0xa0(%rbp), %rdi
movl -0x98(%rbp), %esi
movq -0xc0(%rbp), %rdx
movl -0xb8(%rbp), %ecx
movq -0x100(%rbp), %r8
movl -0xf8(%rbp), %r9d
callq 0x6750
movq %rax, -0x120(%rbp)
movl %edx, -0x118(%rbp)
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0x18(%rbp), %rdi
callq 0x21c0
nopl (%rax)