// File: /workspace/github_c_makefile_star2_O3/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 %esi, %ebx
movl %edi, %ebp
testl %edx, %edx
je 0x4c19
movl %r9d, %r13d
movq %r8, %r14
movl %edx, %r15d
movl %ecx, 0x10(%rsp)
movq 0x2263c(%rip), %rax     # 0x27188
testl $0x7ff, %eax            # imm = 0x7FF
jne 0x4b5f
callq 0x5313
movq 0x22629(%rip), %rax     # 0x27188
incq %rax
movq %rax, 0x2261f(%rip)     # 0x27188
movslq 0x7340(%rip), %rdi      # 0xbeb0
movslq 0x7331(%rip), %rax      # 0xbea8
movl %edi, %ecx
subl %eax, %ecx
leal -0x1(%rdi), %edx
movb $0x1, %sil
cmpl %edx, %ecx
jge 0x4bd0
movq 0x7334(%rip), %rcx      # 0xbec0
leaq 0x710d(%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 0x4bd0
addq $0x18, %rdi
testb %r8b, %r8b
jne 0x4bb7
movl %ebp, 0x14(%rsp)
testl %r13d, %r13d
setne %cl
orb %sil, %cl
cmpl $0x64, %eax
setl %al
andb %cl, %al
movl 0x72c0(%rip), %r12d     # 0xbeac
testl %r12d, %r12d
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x4f5e
cmpl $0x40, %r12d
jl 0x4c33
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5433
movl %ebp, %edi
movl %ebx, %esi
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5136
movl %ebx, 0xc(%rsp)
movslq 0x7262(%rip), %rax      # 0xbea0
leaq 0x705b(%rip), %rdx      # 0xbca0
movl 0x1f8(%rdx,%rax,4), %edi
movq %rax, 0x28(%rsp)
movl %eax, %esi
xorl $0x1, %esi
callq 0x1547
movq %rax, 0x38(%rsp)
addl %r15d, %eax
movq %rax, 0x18(%rsp)
movq 0x7251(%rip), %rcx      # 0xbec0
movslq 0x134aa(%rip), %rsi     # 0x18120
xorl %ebx, %ebx
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
movq 0x13491(%rip), %rax     # 0x18118
movslq %edx, %rdx
leaq (%rdx,%rdx,2), %rdx
cmpq %rcx, (%rax,%rdx,8)
jne 0x4cfd
leaq (%rax,%rdx,8), %rcx
movl 0x8(%rcx), %ebx
movq 0x18(%rsp), %rsi
cmpl %esi, 0x10(%rcx)
jl 0x4cfd
incl 0x13481(%rip)           # 0x1812c
movl 0xc(%rax,%rdx,8), %ecx
xorl %esi, %esi
cmpl $0xffff8b10, %ecx       # imm = 0xFFFF8B10
cmovll %r12d, %esi
movl 0x14(%rax,%rdx,8), %eax
movl %r12d, %ebp
negl %ebp
cmpl $0x74f1, %ecx           # imm = 0x74F1
cmovll %esi, %ebp
addl %ecx, %ebp
cmpl $0x3, %eax
je 0x4cf4
cmpl $0x2, %eax
je 0x4cea
cmpl $0x1, %eax
jne 0x4cfd
movl 0x14(%rsp), %eax
cmpl %eax, %ebp
movl %eax, %ebp
jle 0x4cf4
jmp 0x4cfd
movl 0xc(%rsp), %eax
cmpl %eax, %ebp
movl %eax, %ebp
jl 0x4cfd
testl %r13d, %r13d
je 0x4f84
testl %r12d, %r12d
sete %al
cmpl $0x0, 0x10(%rsp)
sete %cl
cmpl $0x0, 0x38(%rsp)
setne %dl
orb %al, %cl
orb %dl, %cl
je 0x4d58
testl %r13d, %r13d
jne 0x4d28
movq %r14, %rdi
xorl %esi, %esi
callq 0x3966
movl 0x800(%r14), %eax
testl %ebx, %ebx
je 0x4de9
testl %eax, %eax
jle 0x4f4a
xorl %ecx, %ecx
cmpl %ebx, (%r14,%rcx,8)
je 0x4de0
incq %rcx
cmpq %rcx, %rax
jne 0x4d41
jmp 0x4de9
cmpl $0x4, %r15d
jl 0x4d19
leaq 0x6f3b(%rip), %rax      # 0xbca0
movq 0x28(%rsp), %rcx
cmpl $0x0, 0x25c(%rax,%rcx,4)
jle 0x4d19
callq 0x5563
movl 0xc(%rsp), %r12d
movl %r12d, %edi
negl %edi
movl $0x1, %esi
subl %r12d, %esi
addl $-0x4, %r15d
leaq 0x40(%rsp), %r8
xorl %ebp, %ebp
movl %r15d, %edx
xorl %ecx, %ecx
xorl %r9d, %r9d
callq 0x4b1b
movl %eax, %r15d
callq 0x55f8
cmpl $0x1, 0x223de(%rip)     # 0x27190
je 0x4f60
movl %r15d, %eax
negl %eax
cmovnsl %eax, %r15d
cmpl %r12d, %eax
jl 0x4d19
cmpl $0x74f0, %r15d          # imm = 0x74F0
jae 0x4d19
movl 0xc(%rsp), %ebp
jmp 0x4f60
movl $0x1e8480, 0x4(%r14,%rcx,8) # imm = 0x1E8480
testl %eax, %eax
jle 0x4f4a
movl 0xc(%rsp), %ecx
negl %ecx
movl %ecx, 0x28(%rsp)
movq 0x18(%rsp), %rcx
decl %ecx
movl %ecx, 0x34(%rsp)
xorl %r13d, %r13d
movl $0xffff8ad0, 0x10(%rsp) # imm = 0xFFFF8AD0
movq $0x0, 0x20(%rsp)
xorl %ebx, %ebx
movl 0x14(%rsp), %ebp
cltq
movq %r13, %rdx
movl %r13d, %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 0x4e2a
movslq %ecx, %rax
movq (%r14,%r13,8), %rcx
movq (%r14,%rax,8), %rdx
movq %rdx, (%r14,%r13,8)
movq %rcx, (%r14,%rax,8)
movl (%r14,%r13,8), %edi
callq 0x36cf
testl %eax, %eax
je 0x4f0d
movl %ebp, %r12d
movl %ebp, %esi
negl %esi
xorl %ebp, %ebp
movl 0x28(%rsp), %edi
movl 0x34(%rsp), %edx
movl $0x1, %ecx
leaq 0x40(%rsp), %r8
xorl %r9d, %r9d
callq 0x4b1b
movl %eax, %r15d
callq 0x4889
cmpl $0x1, 0x222fb(%rip)     # 0x27190
je 0x4f60
negl %r15d
movl 0x10(%rsp), %eax
cmpl %r15d, %eax
jge 0x4f00
movl (%r14,%r13,8), %edi
cmpl %r15d, %r12d
movq %rdi, 0x20(%rsp)
jge 0x4f03
movl 0xc(%rsp), %ebp
cmpl %ebp, %r15d
jge 0x4f8c
testl $0x7c000, %edi          # imm = 0x7C000
jne 0x4efb
movl %edi, %eax
andl $0x7f, %eax
leaq 0x6dca(%rip), %rcx      # 0xbca0
movslq (%rcx,%rax,4), %rax
imulq $0x1e0, %rax, %rax      # imm = 0x1E0
addq %rcx, %rax
movl %edi, %ecx
shrl $0x5, %ecx
andl $0x1fc, %ecx            # imm = 0x1FC
movq 0x18(%rsp), %rdx
addl %edx, 0xc5b8(%rcx,%rax)
movl %r15d, %ebp
jmp 0x4f06
movl %eax, %r15d
movl %r12d, %ebp
incl %ebx
movl %r15d, 0x10(%rsp)
incq %r13
movslq 0x800(%r14), %rax
cmpq %rax, %r13
jl 0x4e20
testl %ebx, %ebx
je 0x4f4a
movl 0x14(%rsp), %r14d
cmpl %r14d, %ebp
jne 0x4f74
movq 0x20(%rsp), %rdi
movl %r14d, %esi
movl $0x1, %edx
movq 0x18(%rsp), %rcx
callq 0x568b
movl %r14d, %ebp
jmp 0x4f60
cmpl $0x0, 0x38(%rsp)
je 0x4f5e
movl $0xffff8ad0, %ebp       # imm = 0xFFFF8AD0
addl 0x6f50(%rip), %ebp      # 0xbeac
jmp 0x4f60
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rdi
movl 0x10(%rsp), %esi
movl $0x3, %edx
jmp 0x4ffd
incl 0x131a6(%rip)           # 0x18130
jmp 0x4f60
testl %ebx, %ebx
jne 0x4fa8
movss 0x22200(%rip), %xmm0    # 0x27198
addss 0x2c34(%rip), %xmm0     # 0x7bd4
movss %xmm0, 0x221f0(%rip)    # 0x27198
movss 0x221e4(%rip), %xmm0    # 0x27194
addss 0x2c1c(%rip), %xmm0     # 0x7bd4
movss %xmm0, 0x221d4(%rip)    # 0x27194
testl $0x7c000, %edi          # imm = 0x7C000
jne 0x4ff6
movslq 0x6edd(%rip), %rax      # 0xbeac
leaq 0x6cca(%rip), %rdx      # 0xbca0
movl 0xde18(%rdx,%rax,4), %ecx
movl %ecx, 0xdf18(%rdx,%rax,4)
movl (%r14,%r13,8), %eax
movslq 0x6ebd(%rip), %rcx      # 0xbeac
movl %eax, 0xde18(%rdx,%rcx,4)
movl %ebp, %esi
movl $0x2, %edx
movq 0x18(%rsp), %rcx
callq 0x568b
jmp 0x4f60