// File: hitc8494[P]Matrix/matrix.cpp
// Function: Matrix::upperTriangular()
Matrix Matrix::upperTriangular(){
  if( _m == _n ){
    Matrix result = *this;
    //*****For each leading 1...
    for( uint i = 0; i < _m; i++ ){
      //*****Put a leading nonzero in the current row.
      if( result._data[i][i] == 0 ){
	for( uint j = i+1; j < _m; j++ ){
	  if( result._data[j][i] != 0 ){
	    result.SWITCHRows( i+1, j+1 );
	    break;
	  }
	}
      }
      if( result._data[i][i] == 0 ){ //All 0s in the column. Not possible.
	_error = 1;
	return *this;
      }
      //*****Multiply current row by 1/value to give it a leading 1.
      double value = result._data[i][i];
      result.MULTIPLYRow( 1/value, i+1 );
      //*****Clean the column so the leading 1 is the only number in the column.
      Matrix res( result, i );
      for( uint j = 0; j < _m; j++ ){
	if( i != j ){
	  double coeff = result._data[j][i];
	  result.ADDToRow( (res*-coeff), j+1 );
	}
      }
    }
    return result;
  }
  else {
    _error = 1;
    return *this;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x120(%rbp)
movq %rdi, %rax
movq %rax, -0x118(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x110(%rbp)
movl 0x18(%rcx), %eax
cmpl 0x1c(%rcx), %eax
jne 0x3b6e
movq -0x110(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x2be0
movl $0x0, -0x54(%rbp)
movq -0x110(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpl 0x18(%rcx), %eax
jae 0x3b3f
leaq -0x50(%rbp), %rdi
movl -0x54(%rbp), %eax
movl %eax, %esi
callq 0x5ba0
movq %rax, %rdi
movl -0x54(%rbp), %eax
movl %eax, %esi
callq 0x6360
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x398a
jp 0x398a
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movq -0x110(%rbp), %rcx
movl -0x58(%rbp), %eax
cmpl 0x18(%rcx), %eax
jae 0x3988
leaq -0x50(%rbp), %rdi
movl -0x58(%rbp), %eax
movl %eax, %esi
callq 0x5ba0
movq %rax, %rdi
movl -0x54(%rbp), %eax
movl %eax, %esi
callq 0x6360
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3953
jp 0x3953
jmp 0x397b
movl -0x54(%rbp), %esi
incl %esi
movl -0x58(%rbp), %edx
incl %edx
leaq -0x50(%rbp), %rdi
callq 0x3600
jmp 0x3968
jmp 0x3988
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x3b63
jmp 0x397d
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x3918
jmp 0x398a
leaq -0x50(%rbp), %rdi
movl -0x54(%rbp), %eax
movl %eax, %esi
callq 0x5ba0
movq %rax, %rdi
movl -0x54(%rbp), %eax
movl %eax, %esi
callq 0x6360
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x39d9
jp 0x39d9
movq -0x110(%rbp), %rsi
movq -0x120(%rbp), %rdi
movb $0x1, 0x31(%rsi)
callq 0x2be0
jmp 0x39cd
movl $0x1, -0x68(%rbp)
jmp 0x3b58
movl -0x54(%rbp), %eax
movl %eax, %esi
leaq -0x50(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x5ba0
movq %rax, %rdi
movl -0x54(%rbp), %eax
movl %eax, %esi
callq 0x6360
movq -0x128(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x70(%rbp), %xmm1
movsd 0xb5f0(%rip), %xmm0     # 0xf008
divsd %xmm1, %xmm0
movl -0x54(%rbp), %esi
incl %esi
callq 0x37c0
movq -0x128(%rbp), %rsi
movl -0x54(%rbp), %edx
leaq -0xb0(%rbp), %rdi
callq 0x2b30
jmp 0x3a3e
movl $0x0, -0xb4(%rbp)
movq -0x110(%rbp), %rcx
movl -0xb4(%rbp), %eax
cmpl 0x18(%rcx), %eax
jae 0x3b25
movl -0x54(%rbp), %eax
cmpl -0xb4(%rbp), %eax
je 0x3b0f
movl -0xb4(%rbp), %eax
movl %eax, %esi
leaq -0x50(%rbp), %rdi
callq 0x5ba0
movq %rax, %rdi
movl -0x54(%rbp), %eax
movl %eax, %esi
callq 0x6360
movsd (%rax), %xmm0
movsd %xmm0, -0xc0(%rbp)
movaps 0xb572(%rip), %xmm1     # 0xf010
movsd -0xc0(%rbp), %xmm0
pxor %xmm1, %xmm0
movlpd %xmm0, -0x108(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
leaq -0x108(%rbp), %rdx
callq 0x32a0
jmp 0x3ace
movl -0xb4(%rbp), %edx
addl $0x1, %edx
leaq -0x50(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x3410
leaq -0x100(%rbp), %rdi
callq 0x2c30
jmp 0x3b0f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x2c30
jmp 0x3b63
jmp 0x3b11
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x3a48
leaq -0xb0(%rbp), %rdi
callq 0x2c30
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x38d2
movq -0x120(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x2be0
jmp 0x3b51
movl $0x1, -0x68(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x2c30
jmp 0x3b85
leaq -0x50(%rbp), %rdi
callq 0x2c30
jmp 0x3b95
movq -0x110(%rbp), %rsi
movq -0x120(%rbp), %rdi
movb $0x1, 0x31(%rsi)
callq 0x2be0
movq -0x118(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x22a0
nop