// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
leaq 0x88(%rdi), %r14
movq 0x88(%rdi), %rax
cmpq 0x90(%rdi), %rax
je 0x4d09
movq %r14, %rdi
callq 0x91bc
movq 0x190(%rbx), %rax
cmpq %rax, 0x198(%rbx)
je 0x4d09
movq %rax, 0x198(%rbx)
movq 0x60(%rbx), %rax
subq 0x58(%rbx), %rax
pushq $0x48
popq %rcx
cqto
idivq %rcx
movq %rax, %r13
movl (%rbx), %eax
movq %rax, 0x10(%rsp)
movq 0x50(%rbx), %rsi
addq $0x20, %rsi
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x3fd8
leaq 0x1c0(%rbx), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x9dfe
movq %r15, %rdi
callq 0x415a
movq %r14, %rdi
movq %r12, 0x50(%rsp)
movq %r12, %rsi
callq 0x926a
xorl %ebp, %ebp
testl %r13d, %r13d
cmovlel %ebp, %r13d
imulq $0x48, %r13, %r13
pushq $0x2
popq %r12
movl $0x0, 0xc(%rsp)
cmpq %rbp, %r13
je 0x4dd1
movq 0x58(%rbx), %rax
movq 0x88(%rbx), %rdi
cmpl $-0x1, 0x20(%rax,%rbp)
je 0x4da5
andq $0x0, 0x28(%rsp)
movq %r15, %rsi
callq 0xa03e
pushq $0x1
popq %rax
jmp 0x4dc7
andq $0x0, 0x28(%rsp)
movq %r15, %rsi
callq 0xa03e
movq (%r14), %rdi
andq $0x0, 0x28(%rsp)
movq %r15, %rsi
callq 0xa03e
movl %r12d, %eax
addl %eax, 0xc(%rsp)
addq $0x48, %rbp
jmp 0x4d7b
movq 0x88(%rbx), %rdi
leaq 0x28(%rsp), %rsi
andq $0x0, (%rsi)
callq 0xa03e
cmpl $-0x1, 0x28(%rbx)
jne 0x4dfc
movq (%r14), %rsi
movsd 0x74c1(%rip), %xmm0     # 0xc2b8
callq 0x4be4
leaq 0x190(%rbx), %rax
movq %rax, 0x48(%rsp)
xorl %edi, %edi
leaq 0x28(%rsp), %r13
leaq 0x20(%rsp), %rbp
movq %r14, 0x40(%rsp)
movq 0x58(%rbx), %rcx
movq 0x60(%rbx), %rax
subq %rcx, %rax
cqto
pushq $0x48
popq %rsi
idivq %rsi
cmpq %rdi, %rax
jbe 0x4fb9
movq %r14, %r15
movq %rdi, 0x18(%rsp)
imulq $0x48, %rdi, %r12
leaq (%rcx,%r12), %rsi
addq $0x30, %rsi
movq %r13, %rdi
callq 0x3fd8
movl 0xc(%rsp), %r14d
subl $0x1, %r14d
jb 0x4e6f
andq $0x0, 0x20(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa03e
jmp 0x4e56
movq 0x58(%rbx), %rax
movsd 0x28(%rax,%r12), %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x9296
movq %r15, %r14
movq 0x50(%rsp), %r15
movq 0x58(%rbx), %rax
movl 0x20(%rax,%r12), %eax
cmpl $-0x1, %eax
je 0x4ee2
testl %eax, %eax
movq 0x18(%rsp), %r12
jne 0x4f59
movq (%r14), %rsi
movq %r13, %rdx
movsd 0x7404(%rip), %xmm0     # 0xc2c0
callq 0x4c2c
movabsq $0x40f869f000000000, %rax # imm = 0x40F869F000000000
movq %rax, (%rsp)
movq %r15, %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0xa03e
jmp 0x4f6c
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %r12
addl %r12d, %eax
cltq
movq 0x28(%rsp), %rcx
movabsq $-0x4010000000000000, %rdx # imm = 0xBFF0000000000000
movq %rdx, (%rcx,%rax,8)
movq (%r14), %rsi
movq %r13, %rdx
movsd 0x73ae(%rip), %xmm0     # 0xc2c0
callq 0x4c2c
andq $0x0, (%rsp)
movq %r15, %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0xa03e
movabsq $0x40f869f000000000, %rax # imm = 0x40F869F000000000
movq %rax, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0xa03e
movq %r14, %r15
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movq 0x40(%rsp), %r14
jmp 0x4f6c
andq $0x0, (%rsp)
movq %r15, %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0xa03e
movq 0x10(%rsp), %rax
addl %r12d, %eax
cltq
movq 0x28(%rsp), %rcx
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rcx,%rax,8)
movl %eax, (%rsp)
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0xa230
movq %r14, %rdi
movq %r13, %rsi
callq 0x926a
movq %r13, %rdi
callq 0x415a
movq 0x18(%rsp), %rdi
incq %rdi
jmp 0x4e19
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4fd4
jmp 0x4fd4
jmp 0x4fd4
jmp 0x4fd4
jmp 0x4fd4
jmp 0x4fd4
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x415a
movq %rbx, %rdi
callq 0x22c0
nop