// File: boczeratul[P]StarCraftGA/ga.cpp
// Function: modifyChromosomeByRequest(int*)
void modifyChromosomeByRequest(int* chromosome) {
    int count[60];
    int toAdd[MAX_LENGTH];
    int total2Add = 0;
    
    int tempChromosome[MAX_LENGTH+1];
    int length = 0;
    
    for ( int i = 0; i < 60; ++ i )
    {
        count[i] = 0;
    }
    
    // handle more
    for ( int i = 1; i < chromosome[0]+1; ++i )
    {
        int action = chromosome[i];
        if ( count[action] < requests[action]  )  // only take care requirements
        {
            tempChromosome[length] = action;
            ++length;
            ++count[action];
        }
        else if ( requests[action] == -1 )
        {
            tempChromosome[length] = action;
            ++length;
        }
    }
//    printf("a\n");
    
    //handle less
    for ( int i = 0; i < 60; ++i )
    {
        for ( int j = 0; j < requests[i]-count[i]; ++j )
        {
            toAdd[total2Add] = i;
            ++total2Add;
        }
    }
    
    
    chromosome[0] = length+total2Add;
    
    if ( chromosome[0] > MAX_LENGTH )
    {
        generateChromosomeByRequest(chromosome);        
        return;
    }
    
//    printf("b\n");
    
    int* toAddIndex = (int*) malloc(sizeof(int)*(total2Add));
    uniformArray (toAddIndex, total2Add,  1, chromosome[0]);
    
    
    int max = 0;    
    
    for ( int i = 1; i < chromosome[0]+1; ++i )
    {
        chromosome[i] = -1;        
    }
    
//    printf("c\n");
    //fill in toAdd
    for ( int i = 0; i < total2Add; ++i )
    {
        chromosome[toAddIndex[i]] = toAdd[i];
        
        //handle crop end
        if ( toAddIndex[i] > max )
        {
            max = toAddIndex[i];        
        }
    }
    
    // the last one be request
    if ( max != 0 && chromosome[0] != max)
    {
        chromosome[chromosome[0]] = chromosome[max];
        chromosome[max] = -1;
        max = chromosome[0];
    }
    
    //fill in original
    int pivot = 0;
//    printf("d\n");
    
    for ( int i = 1; i < chromosome[0]+1; ++i )
    {
        if ( chromosome[i] == -1 )
        {
            chromosome[i] = tempChromosome[pivot];
            ++pivot;
            
            // handle crop end
            if ( i > max )
            {
                for ( int a = 1; a < requestAction[0]+1; ++a )
                {
                    if ( chromosome[i] == requestAction[a] )
                    {
                        max = i;
                        break;
                    }
                }                
            }
        }
    }
    
//    printf("e\n");
    
    chromosome[0] = max;
    
    free(toAddIndex);
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x920, %rsp            # imm = 0x920
movq %rdi, -0x8(%rbp)
movl $0x0, -0x4f4(%rbp)
movl $0x0, -0x8e4(%rbp)
movl $0x0, -0x8e8(%rbp)
cmpl $0x3c, -0x8e8(%rbp)
jge 0x2969
movslq -0x8e8(%rbp), %rax
movl $0x0, -0x100(%rbp,%rax,4)
movl -0x8e8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8e8(%rbp)
jmp 0x293d
movl $0x1, -0x8ec(%rbp)
movl -0x8ec(%rbp), %eax
movq -0x8(%rbp), %rcx
movl (%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2a4b
movq -0x8(%rbp), %rax
movslq -0x8ec(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x8f0(%rbp)
movslq -0x8f0(%rbp), %rax
movl -0x100(%rbp,%rax,4), %eax
movslq -0x8f0(%rbp), %rdx
leaq 0xb996(%rip), %rcx      # 0xe350
cmpl (%rcx,%rdx,4), %eax
jge 0x29fc
movl -0x8f0(%rbp), %ecx
movslq -0x8e4(%rbp), %rax
movl %ecx, -0x8e0(%rbp,%rax,4)
movl -0x8e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8e4(%rbp)
movslq -0x8f0(%rbp), %rax
movl -0x100(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x100(%rbp,%rax,4)
jmp 0x2a35
movslq -0x8f0(%rbp), %rcx
leaq 0xb946(%rip), %rax      # 0xe350
cmpl $-0x1, (%rax,%rcx,4)
jne 0x2a33
movl -0x8f0(%rbp), %ecx
movslq -0x8e4(%rbp), %rax
movl %ecx, -0x8e0(%rbp,%rax,4)
movl -0x8e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8e4(%rbp)
jmp 0x2a35
jmp 0x2a37
movl -0x8ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8ec(%rbp)
jmp 0x2973
movl $0x0, -0x8f4(%rbp)
cmpl $0x3c, -0x8f4(%rbp)
jge 0x2adb
movl $0x0, -0x8f8(%rbp)
movl -0x8f8(%rbp), %eax
movslq -0x8f4(%rbp), %rdx
leaq 0xb8d4(%rip), %rcx      # 0xe350
movl (%rcx,%rdx,4), %ecx
movslq -0x8f4(%rbp), %rdx
subl -0x100(%rbp,%rdx,4), %ecx
cmpl %ecx, %eax
jge 0x2ac5
movl -0x8f4(%rbp), %ecx
movslq -0x4f4(%rbp), %rax
movl %ecx, -0x4f0(%rbp,%rax,4)
movl -0x4f4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4f4(%rbp)
movl -0x8f8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8f8(%rbp)
jmp 0x2a68
jmp 0x2ac7
movl -0x8f4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8f4(%rbp)
jmp 0x2a55
movl -0x8e4(%rbp), %ecx
addl -0x4f4(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
cmpl $0xf9, (%rax)
jle 0x2b07
movq -0x8(%rbp), %rdi
callq 0x1560
jmp 0x2d62
movslq -0x4f4(%rbp), %rdi
shlq $0x2, %rdi
callq 0x10f0
movq %rax, -0x900(%rbp)
movq -0x900(%rbp), %rdi
movl -0x4f4(%rbp), %esi
movq -0x8(%rbp), %rax
movl (%rax), %ecx
movl $0x1, %edx
callq 0x2ec0
movl $0x0, -0x904(%rbp)
movl $0x1, -0x908(%rbp)
movl -0x908(%rbp), %eax
movq -0x8(%rbp), %rcx
movl (%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2b85
movq -0x8(%rbp), %rax
movslq -0x908(%rbp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl -0x908(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x908(%rbp)
jmp 0x2b4f
movl $0x0, -0x90c(%rbp)
movl -0x90c(%rbp), %eax
cmpl -0x4f4(%rbp), %eax
jge 0x2c07
movslq -0x90c(%rbp), %rax
movl -0x4f0(%rbp,%rax,4), %edx
movq -0x8(%rbp), %rax
movq -0x900(%rbp), %rcx
movslq -0x90c(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x900(%rbp), %rax
movslq -0x90c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x904(%rbp), %eax
jle 0x2bf4
movq -0x900(%rbp), %rax
movslq -0x90c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x904(%rbp)
jmp 0x2bf6
movl -0x90c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90c(%rbp)
jmp 0x2b8f
cmpl $0x0, -0x904(%rbp)
je 0x2c58
movq -0x8(%rbp), %rax
movl (%rax), %eax
cmpl -0x904(%rbp), %eax
je 0x2c58
movq -0x8(%rbp), %rax
movslq -0x904(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x8(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq (%rcx), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movslq -0x904(%rbp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x904(%rbp)
movl $0x0, -0x910(%rbp)
movl $0x1, -0x914(%rbp)
movl -0x914(%rbp), %eax
movq -0x8(%rbp), %rcx
movl (%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2d4a
movq -0x8(%rbp), %rax
movslq -0x914(%rbp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x2d34
movslq -0x910(%rbp), %rax
movl -0x8e0(%rbp,%rax,4), %edx
movq -0x8(%rbp), %rax
movslq -0x914(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x910(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x910(%rbp)
movl -0x914(%rbp), %eax
cmpl -0x904(%rbp), %eax
jle 0x2d32
movl $0x1, -0x918(%rbp)
movl -0x918(%rbp), %eax
movl 0xb569(%rip), %ecx      # 0xe250
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2d30
movq -0x8(%rbp), %rax
movslq -0x914(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movslq -0x918(%rbp), %rdx
leaq 0xb546(%rip), %rcx      # 0xe250
cmpl (%rcx,%rdx,4), %eax
jne 0x2d1d
movl -0x914(%rbp), %eax
movl %eax, -0x904(%rbp)
jmp 0x2d30
jmp 0x2d1f
movl -0x918(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x918(%rbp)
jmp 0x2cdb
jmp 0x2d32
jmp 0x2d34
jmp 0x2d36
movl -0x914(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x914(%rbp)
jmp 0x2c6c
movl -0x904(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, (%rax)
movq -0x900(%rbp), %rdi
callq 0x10d0
addq $0x920, %rsp            # imm = 0x920
popq %rbp
retq
nopl (%rax,%rax)