// File: kmyk[P]codevs-for-student-2016/Main.cpp
// Function: void beam_search<AI::think(primitive::input_t const&)::'lambda'(std::shared_ptr<photon_t>)>(std::shared_ptr<photon_t> const&, primitive::config_t const&, int, int, AI::think(primitive::input_t const&)::'lambda'(std::shared_ptr<photon_t>))
void beam_search(shared_ptr<photon_t> const & initial, config_t const & config, int beam_width, int beam_depth, F cont) {
    vector<shared_ptr<photon_t> > beam, nbeam;
    beam.push_back(initial);
    repeat (i, beam_depth) {
        if (initial->turn + i >= config.packs.size()) break;
        for (shared_ptr<photon_t> const & pho : beam) {
            step_photon_all(pho, config.packs[initial->turn + i]);
            repeat_from (x, - pack_size + 1, width) repeat (r, 4) {
                shared_ptr<photon_t> npho = pho->next[x+pack_size-1][r];
                if (not npho) continue;
                npho = cont(npho);
                if (not npho) continue;
                if (i+1 < beam_depth) {
                    nbeam.push_back(npho);
                }
            }
        }
        beam.resize(min<int>(beam_width, nbeam.size()));
        partial_sort_copy(nbeam.begin(), nbeam.end(), beam.begin(), beam.end(), compare_photon_reversed);
        nbeam.clear();
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %r8, -0x10(%rbp)
movq %r9, -0x8(%rbp)
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movl %edx, -0x24(%rbp)
movl %ecx, -0x28(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0xbec0
leaq -0x58(%rbp), %rdi
callq 0xbec0
movq -0x100(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xf680
jmp 0x10d9c
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x11123
movq -0x18(%rbp), %rdi
callq 0x76d0
movl (%rax), %eax
addl -0x68(%rbp), %eax
cltq
movq %rax, -0x108(%rbp)
movq -0x20(%rbp), %rdi
callq 0x8110
movq %rax, %rcx
movq -0x108(%rbp), %rax
cmpq %rcx, %rax
jb 0x10df4
jmp 0x11123
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1113e
leaq -0x40(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
callq 0xf400
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
callq 0xf430
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xf460
testb $0x1, %al
jne 0x10e2c
jmp 0x1104c
leaq -0x78(%rbp), %rdi
callq 0xf4a0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x18(%rbp), %rdi
callq 0x76d0
movq -0x118(%rbp), %rdi
movl (%rax), %eax
movl -0x68(%rbp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0x122f0
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x4c20
jmp 0x10e85
movl $0xfffffffe, -0x8c(%rbp) # imm = 0xFFFFFFFE
cmpl $0xa, -0x8c(%rbp)
jge 0x1103c
movl $0x0, -0x90(%rbp)
cmpl $0x4, -0x90(%rbp)
jge 0x11026
movq -0x88(%rbp), %rdi
callq 0x76d0
movq %rax, %rdi
addq $0x138, %rdi            # imm = 0x138
movl -0x8c(%rbp), %eax
addl $0x3, %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x7b30
movq %rax, %rdi
movslq -0x90(%rbp), %rsi
callq 0x7b60
movq %rax, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x7c70
leaq -0xa0(%rbp), %rdi
callq 0x7d10
testb $0x1, %al
jne 0x10f1a
movl $0xc, -0xa4(%rbp)
jmp 0x10fe5
leaq -0xc8(%rbp), %rdi
movq %rdi, -0x120(%rbp)
leaq -0xa0(%rbp), %rsi
callq 0x7c70
movq -0x120(%rbp), %rdx
leaq -0xb8(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x183f0
jmp 0x10f4d
leaq -0xa0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x7d60
leaq -0xb8(%rbp), %rdi
callq 0x78c0
leaq -0xc8(%rbp), %rdi
callq 0x78c0
leaq -0xa0(%rbp), %rdi
callq 0x7d10
testb $0x1, %al
jne 0x10fae
movl $0xc, -0xa4(%rbp)
jmp 0x10fe5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x78c0
jmp 0x11015
movl -0x68(%rbp), %eax
addl $0x1, %eax
cmpl -0x28(%rbp), %eax
jge 0x10fdb
leaq -0x58(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xf680
jmp 0x10fcb
jmp 0x10fdb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x11015
movl $0x0, -0xa4(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x78c0
movl -0xa4(%rbp), %eax
testl %eax, %eax
je 0x10fff
jmp 0x10ffd
jmp 0x11001
jmp 0x11001
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x10ea6
leaq -0xa0(%rbp), %rdi
callq 0x78c0
jmp 0x1113e
jmp 0x11028
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x10e8f
jmp 0x1103e
leaq -0x78(%rbp), %rdi
callq 0xf5c0
jmp 0x10e16
leaq -0x58(%rbp), %rdi
callq 0x11f40
movl %eax, -0xcc(%rbp)
leaq -0x24(%rbp), %rdi
leaq -0xcc(%rbp), %rsi
callq 0x6730
movslq (%rax), %rsi
leaq -0x40(%rbp), %rdi
callq 0x16270
jmp 0x11079
leaq -0x58(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0xf400
movq -0x138(%rbp), %rdi
movq %rax, -0xd8(%rbp)
callq 0xf430
movq %rax, -0xe0(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0xf400
movq -0x130(%rbp), %rdi
movq %rax, -0xe8(%rbp)
callq 0xf430
movq %rax, -0xf0(%rbp)
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
movq -0xf0(%rbp), %rcx
leaq -0xbcb0(%rip), %r8      # 0x5440
callq 0x16300
movq %rax, -0x128(%rbp)
jmp 0x110fe
movq -0x128(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x16370
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x10da3
leaq -0x58(%rbp), %rdi
callq 0xbfa0
leaq -0x40(%rbp), %rdi
callq 0xbfa0
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0xbfa0
leaq -0x40(%rbp), %rdi
callq 0xbfa0
movq -0x60(%rbp), %rdi
callq 0x22f0
nopl (%rax)