// File: /workspace/github_c_makefile_star2_O1/ArchLuke[P]YouSuck/Engine.c
// Function: AlphaBeta
static int AlphaBeta(int alpha, int beta, int depth, S_BOARD *pos, S_SEARCHINFO *info, int DoNull, S_MOVELIST *list, int listProvided) {
    if(depth == 0) {
        return Quiescence(alpha, beta, pos, info);
        // return EvalPosition(pos);
    }
    
    if(( info->nodes & 2047 ) == 0) {
        CheckUp(info);
    }
        
    info->nodes++;
    if(((IsRepetition(pos) && !listProvided) || pos->fiftyMove >= 100) && pos->ply) {    
        return 0;
    }
 
    if(pos->ply > MAXDEPTH - 1) {
        return EvalPosition(pos);
    }

    int InCheck = SqAttacked(pos->KingSq[pos->side],pos->side^1,pos);
    
    if(InCheck == TRUE) {
        depth++;
    }
    
    int Score = -INFINITE;
    int PvMove = NOMOVE;

    if( ProbeHashEntry(pos, &PvMove, &Score, alpha, beta, depth) == TRUE && !listProvided) {
        pos->HashTable->cut++;
        return Score;
    }

    if( DoNull && !InCheck && pos->ply && (pos->bigPce[pos->side] > 0) && depth >= 4) {
        MakeNullMove(pos);
    S_MOVELIST list2[1];    
        Score = -AlphaBeta( -beta, -beta + 1, depth-4, pos, info, FALSE, list2, FALSE);
        TakeNullMove(pos);
        if(info->stopped == TRUE) {
            return 0;
        }        
        
        if (Score >= beta && abs(Score) < ISMATE) {        
          return beta;
        }    
    }
   
    if(!listProvided)
        GenerateAllMoves(pos,list, FALSE);    
    int MoveNum = 0;
    int Legal = 0;
    int OldAlpha = alpha;
    int BestMove = NOMOVE;
    
    int BestScore = -INFINITE;
    
    Score = -INFINITE;
    
    if( PvMove != NOMOVE) {
        for(MoveNum = 0; MoveNum < list->count; ++MoveNum) {
            if( list->moves[MoveNum].move == PvMove) {
                list->moves[MoveNum].score = 2000000;
                break;
            }
        }
    }

    for(MoveNum = 0; MoveNum < list->count; ++MoveNum) {    
            
        PickNextMove(MoveNum, list);    
        if (!MakeMove(pos,list->moves[MoveNum].move))  {
            continue;
        }
        Legal++;
    S_MOVELIST list3[1];
        Score = -AlphaBeta( -beta, -alpha, depth-1, pos, info, TRUE, list3, FALSE);
        TakeMove(pos);
        
        if(info->stopped == TRUE) {
            return 0;
        }        
        if(Score > BestScore) {
            BestScore = Score;
            BestMove = list->moves[MoveNum].move;
            if(Score > alpha) {
                if(Score >= beta) {
                    if(Legal==1) {
                        info->fhf++;
                    }
                    info->fh++;    
                    
                    if(!(list->moves[MoveNum].move & MFLAGCAP)) {
                        pos->searchKillers[1][pos->ply] = pos->searchKillers[0][pos->ply];
                        pos->searchKillers[0][pos->ply] = list->moves[MoveNum].move;
                    }
                    
                    StoreHashEntry(pos, BestMove, beta, HFBETA, depth);
                    
                    return beta;
                }
                alpha = Score;
                
                if(!(list->moves[MoveNum].move & MFLAGCAP)) {
                    pos->searchHistory[pos->pieces[FROMSQ(BestMove)]][TOSQ(BestMove)] += depth;
                }
            }
        }        
    }
    
    if(Legal == 0) {
        if(InCheck) {
            return -INFINITE + pos->ply;
        } else {
            return 0;
        }
    }
    
    if(alpha != OldAlpha) {
        StoreHashEntry(pos, BestMove, BestScore, HFEXACT, depth);
    } else {
        StoreHashEntry(pos, BestMove, alpha, HFALPHA, depth);
    }
    
    return alpha;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x848, %rsp            # imm = 0x848
movl %edi, %r15d
testl %edx, %edx
je 0x4c58
movl %r9d, %r12d
movq %r8, %r14
movl %ecx, %r13d
movl %edx, %ebx
movl %esi, 0xc(%rsp)
movzwl 0x225ec(%rip), %eax     # 0x27188
testl $0x7ff, %eax            # imm = 0x7FF
jne 0x4ba8
callq 0x5408
incq 0x225d9(%rip)           # 0x27188
movslq 0x72fa(%rip), %rdi      # 0xbeb0
movslq 0x72eb(%rip), %rax      # 0xbea8
movl %edi, %ecx
subl %eax, %ecx
leal -0x1(%rdi), %edx
movb $0x1, %sil
cmpl %edx, %ecx
jge 0x4c16
movq 0x72ee(%rip), %rcx      # 0xbec0
leaq 0x70c7(%rip), %rsi      # 0xbca0
leal -0x2(%rax), %edx
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rdi
leaq (,%rax,8), %r8
leaq (%r8,%r8,2), %r8
subq %r8, %rdi
addq %rsi, %rdi
addq $0x280, %rdi            # imm = 0x280
addl $-0x1, %edx
setb %r8b
cmpq (%rdi), %rcx
setne %sil
je 0x4c16
addq $0x18, %rdi
testb %r8b, %r8b
jne 0x4bfd
testl %r12d, %r12d
setne %cl
orb %sil, %cl
cmpl $0x64, %eax
setl %al
andb %cl, %al
movl 0x727f(%rip), %ebp      # 0xbeac
testl %ebp, %ebp
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x5092
cmpl $0x40, %ebp
jl 0x4c71
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5528
movl %r15d, %edi
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x521b
movl %r15d, 0x10(%rsp)
movslq 0x7223(%rip), %rax      # 0xbea0
leaq 0x701c(%rip), %rdx      # 0xbca0
movl 0x1f8(%rdx,%rax,4), %edi
movq %rax, 0x28(%rsp)
movl %eax, %esi
xorl $0x1, %esi
callq 0x1543
movl %eax, 0x34(%rsp)
addl %eax, %ebx
movq 0x7219(%rip), %rcx      # 0xbec0
movslq 0x13472(%rip), %rsi     # 0x18120
movq %rbx, %rdi
xorl %r15d, %r15d
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
movq 0x13455(%rip), %rsi     # 0x18118
movslq %edx, %rax
leaq (%rax,%rax,2), %rdx
movl $0xffff8ad0, %ebx       # imm = 0xFFFF8AD0
cmpq %rcx, (%rsi,%rdx,8)
movq %rdi, 0x20(%rsp)
jne 0x4ce7
leaq (%rsi,%rdx,8), %rax
movl 0x8(%rax), %r15d
cmpl %edi, 0x10(%rax)
jge 0x4d60
movb $0x1, %al
testl %r12d, %r12d
setne %cl
orb %al, %cl
je 0x4d55
testl %ebp, %ebp
sete %al
testl %r13d, %r13d
sete %cl
cmpl $0x0, 0x34(%rsp)
setne %dl
orb %al, %cl
orb %dl, %cl
je 0x4dbc
testl %r12d, %r12d
jne 0x4d1f
movq %r14, %rdi
xorl %esi, %esi
callq 0x39ab
testl %r15d, %r15d
movl 0xc(%rsp), %edx
je 0x4e5c
movslq 0x800(%r14), %rax
testq %rax, %rax
jle 0x4e5c
xorl %ecx, %ecx
cmpl %r15d, (%r14,%rcx,8)
je 0x4e53
incq %rcx
cmpq %rcx, %rax
jne 0x4d3e
jmp 0x4e5c
incl 0x133d5(%rip)           # 0x18130
jmp 0x5094
incl 0x133c6(%rip)           # 0x1812c
movl 0xc(%rsi,%rdx,8), %edi
xorl %eax, %eax
cmpl $0xffff8b10, %edi       # imm = 0xFFFF8B10
movl $0x0, %r8d
cmovll %ebp, %r8d
movl 0x14(%rsi,%rdx,8), %ecx
movl %ebp, %ebx
negl %ebx
cmpl $0x74f1, %edi           # imm = 0x74F1
cmovll %r8d, %ebx
addl %edi, %ebx
cmpl $0x3, %ecx
je 0x4ce9
cmpl $0x2, %ecx
je 0x50d2
cmpl $0x1, %ecx
jne 0x4ce7
movl 0x10(%rsp), %ecx
cmpl %ecx, %ebx
jg 0x4ce7
jmp 0x50de
cmpl $0x4, 0x20(%rsp)
jl 0x4d10
leaq 0x6ed2(%rip), %rax      # 0xbca0
movq 0x28(%rsp), %rcx
cmpl $0x0, 0x25c(%rax,%rcx,4)
jle 0x4d10
callq 0x5658
movl 0xc(%rsp), %ebx
movl %ebx, %edi
negl %edi
movl $0x1, %esi
subl %ebx, %esi
movq 0x20(%rsp), %rax
leal -0x4(%rax), %edx
leaq 0x40(%rsp), %r8
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %r9d, %r9d
callq 0x4b6a
movl %eax, %ebp
callq 0x56f2
cmpl $0x1, 0x22373(%rip)     # 0x27190
movl $0x0, %eax
je 0x4e3f
movl %ebp, %eax
negl %eax
cmovnsl %eax, %ebp
cmpl %ebx, %eax
setl %al
cmpl $0x74f0, %ebp           # imm = 0x74F0
setae %r13b
orb %al, %r13b
movl %ebx, %eax
movl %eax, 0x14(%rsp)
movl %eax, %ebx
testb %r13b, %r13b
jne 0x4d10
jmp 0x5094
movl $0x1e8480, 0x4(%r14,%rcx,8) # imm = 0x1E8480
movl 0x800(%r14), %eax
testl %eax, %eax
jle 0x505e
negl %edx
movl %edx, 0x3c(%rsp)
movq 0x20(%rsp), %rcx
decl %ecx
movl %ecx, 0x38(%rsp)
xorl %r15d, %r15d
movl $0xffff8ad0, %r12d      # imm = 0xFFFF8AD0
movq $0x0, 0x18(%rsp)
xorl %ebx, %ebx
movl 0x10(%rsp), %ecx
movl %ecx, 0x28(%rsp)
cltq
movq %r15, %rdx
movl %r15d, %ecx
xorl %esi, %esi
movl 0x4(%r14,%rdx,8), %edi
cmpl %esi, %edi
cmovgl %edi, %esi
cmovgl %edx, %ecx
incq %rdx
cmpq %rdx, %rax
jne 0x4ea2
movq (%r14,%r15,8), %rax
movslq %ecx, %rcx
movq (%r14,%rcx,8), %rdx
movq %rdx, (%r14,%r15,8)
movq %rax, (%r14,%rcx,8)
movl (%r14,%r15,8), %edi
callq 0x3701
testl %eax, %eax
je 0x4f9d
movl 0x28(%rsp), %esi
negl %esi
xorl %ebp, %ebp
movl 0x3c(%rsp), %edi
movl 0x38(%rsp), %edx
movl $0x1, %ecx
leaq 0x40(%rsp), %r8
xorl %r9d, %r9d
callq 0x4b6a
movl %eax, %r13d
callq 0x48bd
cmpl $0x1, 0x22284(%rip)     # 0x27190
jne 0x4f15
xorl %eax, %eax
movl %r12d, %r13d
jmp 0x4f8b
negl %r13d
movb $0x1, %bpl
cmpl %r13d, %r12d
jge 0x4f84
movl (%r14,%r15,8), %eax
movq %rax, 0x18(%rsp)
cmpl %r13d, 0x28(%rsp)
movl 0x14(%rsp), %eax
jge 0x4f8b
cmpl 0xc(%rsp), %r13d
jge 0x4fb5
movq 0x18(%rsp), %rdx
testl $0x7c000, %edx          # imm = 0x7C000
jne 0x4f7d
movl %edx, %eax
andl $0x7f, %eax
leaq 0x6d4c(%rip), %rcx      # 0xbca0
movslq (%rcx,%rax,4), %rax
imulq $0x1e0, %rax, %rax      # imm = 0x1E0
addq %rcx, %rax
movl %edx, %ecx
shrl $0x5, %ecx
andl $0x1fc, %ecx            # imm = 0x1FC
movq 0x20(%rsp), %rdx
addl %edx, 0xc5b8(%rcx,%rax)
movl 0x14(%rsp), %eax
movl %r13d, 0x28(%rsp)
jmp 0x4f8b
movl %r12d, %r13d
movl 0x14(%rsp), %eax
testb %bpl, %bpl
je 0x50ba
movl %eax, 0x14(%rsp)
incl %ebx
movl %r13d, %r12d
incq %r15
movslq 0x800(%r14), %rax
cmpq %rax, %r15
jl 0x4e98
jmp 0x504a
testl %ebx, %ebx
jne 0x4fd1
movss 0x221d7(%rip), %xmm0    # 0x27198
addss 0x2c0b(%rip), %xmm0     # 0x7bd4
movss %xmm0, 0x221c7(%rip)    # 0x27198
movss 0x221bb(%rip), %xmm0    # 0x27194
addss 0x2bf3(%rip), %xmm0     # 0x7bd4
movss %xmm0, 0x221ab(%rip)    # 0x27194
movq 0x18(%rsp), %rdi
testl $0x7c000, %edi          # imm = 0x7C000
jne 0x5029
movslq 0x6eaf(%rip), %rax      # 0xbeac
leaq 0x6c9c(%rip), %rdx      # 0xbca0
movl 0xde18(%rdx,%rax,4), %ecx
movl %ecx, 0xdf18(%rdx,%rax,4)
movl (%r14,%r15,8), %eax
movslq 0x6e8f(%rip), %rcx      # 0xbeac
movl %eax, 0xde18(%rdx,%rcx,4)
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %r12d
movl %r12d, %esi
movl $0x2, %edx
movq 0x20(%rsp), %rcx
callq 0x578a
xorl %ebp, %ebp
movl %r12d, %eax
jmp 0x4f8b
testl %ebx, %ebx
sete %al
movq 0x20(%rsp), %rcx
movl 0x10(%rsp), %edx
movl 0x28(%rsp), %ebx
jmp 0x507a
movl $0xffff8ad0, %r12d      # imm = 0xFFFF8AD0
movq $0x0, 0x18(%rsp)
movb $0x1, %al
movl 0x10(%rsp), %edx
movl %edx, %ebx
movq 0x20(%rsp), %rcx
testb %al, %al
je 0x50a8
cmpl $0x0, 0x34(%rsp)
je 0x5092
movl $0xffff8ad0, %ebx       # imm = 0xFFFF8AD0
addl 0x6e1c(%rip), %ebx      # 0xbeac
jmp 0x5094
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl %edx, %ebx
jne 0x50be
movq 0x18(%rsp), %rdi
movl %ebx, %esi
movl $0x1, %edx
jmp 0x50cb
movl %eax, %ebx
jmp 0x5094
movq 0x18(%rsp), %rdi
movl %r12d, %esi
movl $0x3, %edx
callq 0x578a
jmp 0x5094
movl 0xc(%rsp), %ecx
cmpl %ecx, %ebx
jl 0x4ce7
xorl %eax, %eax
movl %ecx, %ebx
jmp 0x4ce9