int Idx_2D_to_1D_inclDiag(int idx1, int idx2, int mat_len) {
  int i = 0;
  int one_dim_index = 0;
  if (idx2 < idx1) {
    char buf[1024];
    sprintf(buf,
            "Invalid Args for %s: idx2 (=%d) < idx1 (=%d)                      "
            "         is invalid (1D!)\n",
            __FUNCTION__, idx1, idx2);
    Die(buf);
  }
  while (i < idx1) {
    one_dim_index += (mat_len - 1 - i);
    i++;
  }
  one_dim_index += (idx2);
  return one_dim_index;
}


x86 assembly (clang, -O0):
<Idx_2D_to_1D_inclDiag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    117f <Idx_2D_to_1D_inclDiag+0x5f>
lea    -0x420(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    -0x8(%rbp),%r8d
lea    0xe9d(%rip),%rsi
lea    0xef3(%rip),%rdx
mov    $0x0,%al
call   1040 <sprintf@plt>
lea    -0x420(%rbp),%rdi
mov    $0x0,%al
call   1030 <Die@plt>
jmp    1184 <Idx_2D_to_1D_inclDiag+0x64>
mov    -0x10(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11ad <Idx_2D_to_1D_inclDiag+0x8d>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
sub    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1184 <Idx_2D_to_1D_inclDiag+0x64>
mov    -0x8(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Idx_2D_to_1D_inclDiag>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
cmp    %edi,%esi
jge    1162 <Idx_2D_to_1D_inclDiag+0x42>
lea    0xec1(%rip),%rsi
lea    0xf17(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    %ebx,%ecx
mov    %ebp,%r8d
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <Die@plt>
test   %ebx,%ebx
jle    1183 <Idx_2D_to_1D_inclDiag+0x63>
lea    -0x1(%rbx),%eax
lea    -0x2(%r14),%ecx
imul   %eax,%ecx
add    %r14d,%ecx
add    $0xfffffffe,%ebx
imul   %rax,%rbx
shr    %rbx
not    %ebx
add    %ecx,%ebx
jmp    1185 <Idx_2D_to_1D_inclDiag+0x65>
xor    %ebx,%ebx
add    %ebp,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [93 x i8] c"Invalid Args for %s: idx2 (=%d) < idx1 (=%d) is invalid (1D!)\0A\00"
@__FUNCTION__.Idx_2D_to_1D_inclDiag = [22 x i8] c"Idx_2D_to_1D_inclDiag\00"
define i32 @Idx_2D_to_1D_inclDiag(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%10 = load i32, i32* %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %20
13: ; preds = %3
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = call i32 (i8*, i8*, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__FUNCTION__.Idx_2D_to_1D_inclDiag, i64 0, i64 0), i32 %15, i32 %16)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%19 = call i32 (i8*, ...) bitcast (i32 (...)* @Die to i32 (i8*, ...)*)(i8* %18)
br label %20
20: ; preds = %13, %3
br label %21
21: ; preds = %25, %20
%22 = load i32, i32* %7
%23 = load i32, i32* %4
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %34
25: ; preds = %21
%26 = load i32, i32* %6
%27 = sub nsw i32 %26, 1
%28 = load i32, i32* %7
%29 = sub nsw i32 %27, %28
%30 = load i32, i32* %8
%31 = add nsw i32 %30, %29
store i32 %31, i32* %8
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %21
34: ; preds = %21
%35 = load i32, i32* %5
%36 = load i32, i32* %8
%37 = add nsw i32 %36, %35
store i32 %37, i32* %8
%38 = load i32, i32* %8
ret i32 %38
}

LLVM IR (-O2):
@.str = [93 x i8] c"Invalid Args for %s: idx2 (=%d) < idx1 (=%d) is invalid (1D!)\0A\00"
@__FUNCTION__.Idx_2D_to_1D_inclDiag = [22 x i8] c"Idx_2D_to_1D_inclDiag\00"
define i32 @Idx_2D_to_1D_inclDiag(i32 %0, i32 %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = icmp slt i32 %1, %0
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i32 (i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([93 x i8], [93 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__FUNCTION__.Idx_2D_to_1D_inclDiag, i64 0, i64 0), i32 %0, i32 %1)
%9 = call i32 (i8*, ...) bitcast (i32 (...)* @Die to i32 (i8*, ...)*)(i8* %7)
br label %10
10: ; preds = %6, %3
%11 = icmp sgt i32 %0, 0
br i1 %11, label %12, label %25
12: ; preds = %10
%13 = add nsw i32 %0, -1
%14 = add i32 %2, -2
%15 = mul i32 %13, %14
%16 = add i32 %15, %2
%17 = zext i32 %13 to i33
%18 = add nsw i32 %0, -2
%19 = zext i32 %18 to i33
%20 = mul i33 %17, %19
%21 = lshr i33 %20, 1
%22 = trunc i33 %21 to i32
%23 = xor i32 %22, -1
%24 = add i32 %16, %23
br label %25
25: ; preds = %12, %10
%26 = phi i32 [ 0, %10 ], [ %24, %12 ]
%27 = add nsw i32 %26, %1
ret i32 %27
}