// File: mororo18[P]simplex-method/src/model.cpp
// Function: Model::tableau_generate()
void Model::tableau_generate(){

    if(!tableau.empty()){
        tableau.clear();
        I_index.clear();
    }

    const int cstr_qnt = cstr_vec.size();
    int n = var_qnt;

    // store the original coef value
    original_coef = main_func->coef_get();

    // insert the first row
    tableau.push_back(original_coef);

    int slack_cols = 0;

    // columns for slack variables of first row 
    for(int i = 0; i < cstr_qnt; i++){
        if(cstr_vec[i].type_id == G_EQ){
            // the two cols of the artificial variables of the geq constraint
            tableau[0].push_back(0);
            tableau[0].push_back(0);
            slack_cols += 2;
        }else {
            tableau[0].push_back(0);
            slack_cols++;
        }
    }
    
    // current solution value
    tableau[0].push_back(0);

    if(type_id == MAX)
        vec_multiply_scalar(tableau[0], MAX);

    for(int i = 0; i < cstr_vec.size(); i++){
        std::vector<double> tableau_row = cstr_vec[i].coef_get();

        // slack vars cols
        for(int j = 0; j < slack_cols; j++){
            tableau_row.push_back(0);
        }

        double cstr_value = cstr_vec[i].value;
        tableau_row.push_back(cstr_value);

        if(cstr_vec[i].type_id == EQ){
            // subtract the first row(obj_func) by the current row times BIG_M (eq_cstr)
            vec_add_vec(tableau[0], tableau_row, -BIG_M);

            // store the original coef value of the slack var
            original_coef.push_back(BIG_M);
        }else if(cstr_vec[i].type_id == G_EQ){
            //vec_multiply_scalar(cstr_cpy, G_EQ);
            tableau_row[n+i] = -1;
            n++;
            vec_add_vec(tableau[0], tableau_row, -BIG_M);

            // store the original coef value of the slack var
            original_coef.push_back(0);
            original_coef.push_back(BIG_M);
        }else
            // store the original coef value of the slack var
            original_coef.push_back(0);

        //add the identity sub-matrix 
        tableau_row[n + i] = 1;

        // store the identity sub-matrix col indexes
        I_index.push_back(n + i);
    
        tableau.push_back(tableau_row);
    }


}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq 0xd0(%rsp), %rdi
movq %rdi, 0x10(%rsp)
addq $0x88, %rdi
callq 0x10a30
testb $0x1, %al
jne 0x8d7e
movq 0x10(%rsp), %rdi
addq $0x88, %rdi
callq 0x107a0
movq 0x10(%rsp), %rdi
addq $0x190, %rdi            # imm = 0x190
callq 0x107e0
movq 0x10(%rsp), %rdi
addq $0x58, %rdi
callq 0x10a80
movq %rax, %rcx
movq 0x10(%rsp), %rax
movl %ecx, 0xcc(%rsp)
movl (%rax), %ecx
movl %ecx, 0xc8(%rsp)
movq 0x50(%rax), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x9270
movq 0x10(%rsp), %rdi
addq $0x1c0, %rdi            # imm = 0x1C0
leaq 0xb0(%rsp), %rsi
callq 0x10aa0
leaq 0xb0(%rsp), %rdi
callq 0x6bc0
movq 0x10(%rsp), %rsi
movq %rsi, %rdi
addq $0x88, %rdi
addq $0x1c0, %rsi            # imm = 0x1C0
callq 0x10ae0
movl $0x0, 0xac(%rsp)
movl $0x0, 0xa8(%rsp)
movl 0xa8(%rsp), %eax
cmpl 0xcc(%rsp), %eax
jge 0x8f0b
movq 0x10(%rsp), %rdi
addq $0x58, %rdi
movslq 0xa8(%rsp), %rsi
callq 0x10b50
cmpl $-0x1, 0x20(%rax)
jne 0x8eb1
movq 0x10(%rsp), %rdi
addq $0x88, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10780
movq %rax, %rdi
xorps %xmm0, %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0xa0(%rsp), %rsi
callq 0x10b70
movq 0x10(%rsp), %rdi
addq $0x88, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10780
movq %rax, %rdi
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
leaq 0x98(%rsp), %rsi
callq 0x10b70
movl 0xac(%rsp), %eax
addl $0x2, %eax
movl %eax, 0xac(%rsp)
jmp 0x8ef3
movq 0x10(%rsp), %rdi
addq $0x88, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10780
movq %rax, %rdi
xorps %xmm0, %xmm0
movsd %xmm0, 0x90(%rsp)
leaq 0x90(%rsp), %rsi
callq 0x10b70
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x8ef5
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
jmp 0x8e0c
movq 0x10(%rsp), %rdi
addq $0x88, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10780
movq %rax, %rdi
xorps %xmm0, %xmm0
movsd %xmm0, 0x88(%rsp)
leaq 0x88(%rsp), %rsi
callq 0x10b70
movq 0x10(%rsp), %rax
cmpl $-0x1, 0x28(%rax)
jne 0x8f71
movq 0x10(%rsp), %rdi
addq $0x88, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10780
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movsd 0x12334(%rip), %xmm0    # 0x1b2a0
callq 0x8b30
movl $0x0, 0x84(%rsp)
movq 0x10(%rsp), %rdi
movslq 0x84(%rsp), %rax
movq %rax, 0x8(%rsp)
addq $0x58, %rdi
callq 0x10a80
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x9258
movq 0x10(%rsp), %rdi
addq $0x58, %rdi
movslq 0x84(%rsp), %rsi
callq 0x10b50
movq %rax, %rsi
leaq 0x68(%rsp), %rdi
callq 0x8a60
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
cmpl 0xac(%rsp), %eax
jge 0x9026
movq $0x0, 0x58(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x10b70
jmp 0x8ffa
jmp 0x8ffc
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x8fd3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x6bc0
jmp 0x9260
movq 0x10(%rsp), %rdi
addq $0x58, %rdi
movslq 0x84(%rsp), %rsi
callq 0x10b50
movsd 0x28(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x10ba0
jmp 0x9058
movq 0x10(%rsp), %rdi
addq $0x58, %rdi
movslq 0x84(%rsp), %rsi
callq 0x10b50
cmpl $0x0, 0x20(%rax)
jne 0x90cf
movq 0x10(%rsp), %rdi
addq $0x88, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10780
movq 0x10(%rsp), %rdi
movq %rax, %rsi
leaq 0x68(%rsp), %rdx
movsd 0x1220a(%rip), %xmm0    # 0x1b2a8
callq 0x8bf0
movq 0x10(%rsp), %rdi
addq $0x1c0, %rdi            # imm = 0x1C0
movabsq $0x40f869f000000000, %rax # imm = 0x40F869F000000000
movq %rax, 0x38(%rsp)
leaq 0x38(%rsp), %rsi
callq 0x10b70
jmp 0x90ca
jmp 0x91ca
movq 0x10(%rsp), %rdi
addq $0x58, %rdi
movslq 0x84(%rsp), %rsi
callq 0x10b50
cmpl $-0x1, 0x20(%rax)
jne 0x91a5
movl 0xc8(%rsp), %eax
movl 0x84(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
leaq 0x68(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x109f0
movq 0x10(%rsp), %rdi
movabsq $-0x4010000000000000, %rcx # imm = 0xBFF0000000000000
movq %rcx, (%rax)
movl 0xc8(%rsp), %eax
incl %eax
movl %eax, 0xc8(%rsp)
addq $0x88, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10780
movq (%rsp), %rdx
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movsd 0x12152(%rip), %xmm0    # 0x1b2a8
callq 0x8bf0
movq 0x10(%rsp), %rdi
addq $0x1c0, %rdi            # imm = 0x1C0
movq $0x0, 0x30(%rsp)
leaq 0x30(%rsp), %rsi
callq 0x10b70
jmp 0x917c
movq 0x10(%rsp), %rdi
addq $0x1c0, %rdi            # imm = 0x1C0
movabsq $0x40f869f000000000, %rax # imm = 0x40F869F000000000
movq %rax, 0x28(%rsp)
leaq 0x28(%rsp), %rsi
callq 0x10b70
jmp 0x91a3
jmp 0x91c8
movq 0x10(%rsp), %rdi
addq $0x1c0, %rdi            # imm = 0x1C0
movq $0x0, 0x20(%rsp)
leaq 0x20(%rsp), %rsi
callq 0x10b70
jmp 0x91c6
jmp 0x91c8
jmp 0x91ca
movl 0xc8(%rsp), %eax
movl 0x84(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
leaq 0x68(%rsp), %rdi
callq 0x109f0
movq 0x10(%rsp), %rdi
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
addq $0x190, %rdi            # imm = 0x190
movl 0xc8(%rsp), %eax
movl 0x84(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x1c(%rsp)
leaq 0x1c(%rsp), %rsi
callq 0x10c10
jmp 0x9220
movq 0x10(%rsp), %rdi
addq $0x88, %rdi
leaq 0x68(%rsp), %rsi
callq 0x10ae0
jmp 0x9238
leaq 0x68(%rsp), %rdi
callq 0x6bc0
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x8f7c
addq $0xd8, %rsp
retq
movq 0x50(%rsp), %rdi
callq 0x2350
nopw (%rax,%rax)