// File: /workspace/github_c_makefile_star2_O0/ArchLuke[P]YouSuck/Engine.c
// Function: TrapSearch
static int TrapSearch(S_BOARD *pos, S_SEARCHINFO *info)
{
    printf("trap search starting \n");
    S_MOVELIST list[1];
    S_MOVELIST captures[1];
    int counter;
    int depth;
    int index;

    int bestMove=NOMOVE;
    int pvMove=NOMOVE;
    int bestScore=-INFINITE;
    int score=-INFINITE;
    int trapScore=-INFINITE;
    int bestTrapScore=-INFINITE;
    int trap=NOMOVE;

    index=pos->posKey % pos->HashTable->numEntries;
    if( pos->HashTable->pTable[index].posKey == pos->posKey ) {
        bestMove=pos->HashTable->pTable[index].move;    
        bestScore=pos->HashTable->pTable[index].score;
        if(bestScore>500)
            return -INFINITE;
        depth=pos->HashTable->pTable[index].depth;
        pvMove=bestMove;
        trapScore=CheckCaptures(pos, pvMove, captures, bestScore);
        if (trapScore>bestTrapScore)
        {

            bestTrapScore=trapScore;
            trap=pvMove;
        }
        GenerateAllMoves(pos,list, FALSE);
    }else{
        return -INFINITE;
    }
    
    int count=list->count;
    for(counter=0;counter <= count;counter++)
    {
        if(info->stopped==TRUE)
            break;
        PopMove(list, pvMove);
        score=AlphaBeta(-INFINITE, INFINITE, depth, pos, info, TRUE, list, TRUE);
        
        if( pos->HashTable->pTable[index].posKey == pos->posKey ) {
            pvMove=pos->HashTable->pTable[index].move;
        }else
		break;
        if (score+THRESHOLD>bestScore)
        {
            //check captures
            trapScore=CheckCaptures(pos, pvMove, captures, bestScore);
            if(trapScore>bestTrapScore)
            {

                bestTrapScore=trapScore;
                trap=pvMove;
            }                
        }else
            break;
            
    }
    trapMove=trap;
    printf("trap search exitting with trapscore %d and trap move %s \n", bestTrapScore, PrMove(trapMove));
    return bestTrapScore;

}

// Assembly:
subq $0x1078, %rsp           # imm = 0x1078
movq %rdi, 0x1068(%rsp)
movq %rsi, 0x1060(%rsp)
leaq 0x585d(%rip), %rdi      # 0xbe2b
movb $0x0, %al
callq 0x1060
movl $0x0, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0xffff8ad0, 0x28(%rsp) # imm = 0xFFFF8AD0
movl $0xffff8ad0, 0x24(%rsp) # imm = 0xFFFF8AD0
movl $0xffff8ad0, 0x20(%rsp) # imm = 0xFFFF8AD0
movl $0xffff8ad0, 0x1c(%rsp) # imm = 0xFFFF8AD0
movl $0x0, 0x18(%rsp)
movq 0x1068(%rsp), %rax
movq 0x220(%rax), %rax
movq 0x1068(%rsp), %rcx
movslq 0xc480(%rcx), %rcx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x34(%rsp)
movq 0x1068(%rsp), %rax
movq 0xc478(%rax), %rax
movslq 0x34(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x1068(%rsp), %rcx
cmpq 0x220(%rcx), %rax
jne 0x6742
movq 0x1068(%rsp), %rax
movq 0xc478(%rax), %rax
movslq 0x34(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, 0x30(%rsp)
movq 0x1068(%rsp), %rax
movq 0xc478(%rax), %rax
movslq 0x34(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, 0x28(%rsp)
cmpl $0x1f4, 0x28(%rsp)      # imm = 0x1F4
jle 0x66c7
movl $0xffff8ad0, 0x1074(%rsp) # imm = 0xFFFF8AD0
jmp 0x68c5
movq 0x1068(%rsp), %rax
movq 0xc478(%rax), %rax
movslq 0x34(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, 0x38(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x1068(%rsp), %rdi
movl 0x2c(%rsp), %esi
leaq 0x40(%rsp), %rdx
movl 0x28(%rsp), %ecx
callq 0x7fe0
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jle 0x6729
movl 0x20(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x1068(%rsp), %rdi
leaq 0x850(%rsp), %rsi
xorl %edx, %edx
callq 0x45e0
jmp 0x6752
movl $0xffff8ad0, 0x1074(%rsp) # imm = 0xFFFF8AD0
jmp 0x68c5
movl 0x1050(%rsp), %eax
movl %eax, 0x14(%rsp)
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x14(%rsp), %eax
jg 0x6888
movq 0x1060(%rsp), %rax
cmpl $0x1, 0x20(%rax)
jne 0x6786
jmp 0x6888
leaq 0x850(%rsp), %rdi
movl 0x2c(%rsp), %esi
callq 0x83a0
movl 0x38(%rsp), %edx
movq 0x1068(%rsp), %rcx
movq 0x1060(%rsp), %r8
leaq 0x850(%rsp), %rax
movl $0xffff8ad0, %edi       # imm = 0xFFFF8AD0
movl $0x7530, %esi           # imm = 0x7530
movl $0x1, %r9d
movq %rax, (%rsp)
movl $0x1, 0x8(%rsp)
callq 0x68e0
movl %eax, 0x24(%rsp)
movq 0x1068(%rsp), %rax
movq 0xc478(%rax), %rax
movslq 0x34(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x1068(%rsp), %rcx
cmpq 0x220(%rcx), %rax
jne 0x682b
movq 0x1068(%rsp), %rax
movq 0xc478(%rax), %rax
movslq 0x34(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, 0x2c(%rsp)
jmp 0x682d
jmp 0x6888
movl 0x24(%rsp), %eax
addl $0x32, %eax
cmpl 0x28(%rsp), %eax
jle 0x6874
movq 0x1068(%rsp), %rdi
movl 0x2c(%rsp), %esi
leaq 0x40(%rsp), %rdx
movl 0x28(%rsp), %ecx
callq 0x7fe0
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jle 0x6872
movl 0x20(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0x6876
jmp 0x6888
jmp 0x6878
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x6765
movl 0x18(%rsp), %eax
movl %eax, 0x9022(%rip)      # 0xf8b4
movl 0x1c(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x9014(%rip), %edi      # 0xf8b4
callq 0x6040
movl 0x10(%rsp), %esi
movq %rax, %rdx
leaq 0x558f(%rip), %rdi      # 0xbe42
movb $0x0, %al
callq 0x1060
movl 0x1c(%rsp), %eax
movl %eax, 0x1074(%rsp)
movl 0x1074(%rsp), %eax
addq $0x1078, %rsp           # imm = 0x1078
retq
nopw %cs:(%rax,%rax)