#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
size_t mat_to_arr(unsigned int, unsigned int, unsigned int);
long long arr_diagonal_sum(int *arr, unsigned int dism, unsigned int length) {
  long long res = 0;
  for (unsigned int i = 1; i <= dism; i++)
    res += arr[mat_to_arr(i, i, dism)];
  return res;
}


x86 assembly (clang, -O0):
<arr_diagonal_sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
ja     1174 <arr_diagonal_sum+0x64>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <mat_to_arr@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
movslq (%rax,%rcx,4),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1131 <arr_diagonal_sum+0x21>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arr_diagonal_sum>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
je     114d <arr_diagonal_sum+0x3d>
mov    %esi,%r15d
mov    %rdi,%r14
mov    $0x1,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %ebp,%esi
mov    %r15d,%edx
call   1030 <mat_to_arr@plt>
movslq (%r14,%rax,4),%rax
add    %rax,%rbx
add    $0x1,%ebp
cmp    %r15d,%ebp
jbe    1130 <arr_diagonal_sum+0x20>
jmp    114f <arr_diagonal_sum+0x3f>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @arr_diagonal_sum(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i64 0, i64* %7
store i32 1, i32* %8
br label %9
9: ; preds = %24, %3
%10 = load i32, i32* %8
%11 = load i32, i32* %5
%12 = icmp ule i32 %10, %11
br i1 %12, label %13, label %27
13: ; preds = %9
%14 = load i32*, i32** %4
%15 = load i32, i32* %8
%16 = load i32, i32* %8
%17 = load i32, i32* %5
%18 = call i64 @mat_to_arr(i32 %15, i32 %16, i32 %17)
%19 = getelementptr inbounds i32, i32* %14, i64 %18
%20 = load i32, i32* %19
%21 = sext i32 %20 to i64
%22 = load i64, i64* %7
%23 = add nsw i64 %22, %21
store i64 %23, i64* %7
br label %24
24: ; preds = %13
%25 = load i32, i32* %8
%26 = add i32 %25, 1
store i32 %26, i32* %8
br label %9
27: ; preds = %9
%28 = load i64, i64* %7
ret i64 %28
}

LLVM IR (-O2):
define i64 @arr_diagonal_sum(i32* nocapture %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %1, 0
br i1 %4, label %5, label %7
5: ; preds = %7, %3
%6 = phi i64 [ 0, %3 ], [ %14, %7 ]
ret i64 %6
7: ; preds = %3, %7
%8 = phi i32 [ %15, %7 ], [ 1, %3 ]
%9 = phi i64 [ %14, %7 ], [ 0, %3 ]
%10 = tail call i64 @mat_to_arr(i32 %8, i32 %8, i32 %1)
%11 = getelementptr inbounds i32, i32* %0, i64 %10
%12 = load i32, i32* %11
%13 = sext i32 %12 to i64
%14 = add nsw i64 %9, %13
%15 = add i32 %8, 1
%16 = icmp ugt i32 %15, %1
br i1 %16, label %5, label %7
}