// File: kelwinfc[P]ci4721-Fernandez-Gyomrey/src/codegen.cpp
// Function: get_reg(symbol*)
int get_reg(symbol* s){
    
    int index = 0;
    int empty = -1;
    
    vector<register_descriptor>::iterator it = register_descriptors.begin();
    
    if ( s ){
        // Si ya esta en un registro, aprovecharlo
        for( ; it != register_descriptors.end(); ++it){
            if ( it->contains(s) )
                return index;
            else if ( it->is_free() ){
                empty = index;
            }
            index++;
        }
        
        // El simbolo no se encuentra ya en un registro
        
        // Hay algun registro vacio, usarlo
        if ( empty != -1 ){
            return empty;
        }
    }
    
    int cheapest_register = 0;
    int cost = register_descriptors[0].release_cost();
    
    it = register_descriptors.begin()+1;
    index = 0;
    for( ; it != register_descriptors.end(); ++it){
        int next_cost = it->release_cost();
        
        if ( next_cost < cost ){
            cheapest_register = index;
            cost = next_cost;
        }
        index++;
    }
    
    //TODO liberar lo que sea necesario liberar (hacer los ST respectivos)
    //TODO cargar el simbolo en un registro
    return cheapest_register;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movl $0x0, 0x44(%rsp)
movl $0xffffffff, 0x40(%rsp) # imm = 0xFFFFFFFF
leaq 0x1f990(%rip), %rdi     # 0x6e470
callq 0x4ef10
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0x4eb9a
jmp 0x4eaf8
leaq 0x1f971(%rip), %rdi     # 0x6e470
callq 0x4ef80
movq %rax, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4ef40
testb $0x1, %al
jne 0x4eb1e
jmp 0x4eb84
leaq 0x38(%rsp), %rdi
callq 0x4efb0
movq %rax, %rdi
movq 0x48(%rsp), %rsi
callq 0x4e890
testb $0x1, %al
jne 0x4eb3b
jmp 0x4eb48
movl 0x44(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x4ec6a
leaq 0x38(%rsp), %rdi
callq 0x4efb0
movq %rax, %rdi
callq 0x4e8f0
testb $0x1, %al
jne 0x4eb60
jmp 0x4eb68
movl 0x44(%rsp), %eax
movl %eax, 0x40(%rsp)
jmp 0x4eb6a
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x4efc0
jmp 0x4eaf8
cmpl $-0x1, 0x40(%rsp)
je 0x4eb98
movl 0x40(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x4ec6a
jmp 0x4eb9a
movl $0x0, 0x2c(%rsp)
leaq 0x1f8c7(%rip), %rdi     # 0x6e470
xorl %eax, %eax
movl %eax, %esi
callq 0x4efe0
movq %rax, %rdi
callq 0x4e910
movl %eax, 0x28(%rsp)
leaq 0x1f8ab(%rip), %rdi     # 0x6e470
callq 0x4ef10
movq %rax, 0x18(%rsp)
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
callq 0x4f000
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x38(%rsp)
movl $0x0, 0x44(%rsp)
leaq 0x1f874(%rip), %rdi     # 0x6e470
callq 0x4ef80
movq %rax, 0x10(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x4ef40
testb $0x1, %al
jne 0x4ec1b
jmp 0x4ec62
leaq 0x38(%rsp), %rdi
callq 0x4efb0
movq %rax, %rdi
callq 0x4e910
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x28(%rsp), %eax
jge 0x4ec4b
movl 0x44(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x4efc0
jmp 0x4ebf5
movl 0x2c(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)