#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 */
double *alloc_array_z(int, int);
size_t map(int, int, int, char);
double *diag(double *A, int arows, int acols) {
  double *C = alloc_array_z(arows, acols);
  int i;
  int j;
  for (i = 0; i < arows; i++) {
    for (j = 0; j < acols; j++) {
      if (i == j) {
        C[map(i, j, acols, 'n')] = A[i];
      }
    }
  }
  return C;
}


x86 assembly (clang, -O0):
<diag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1040 <alloc_array_z@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d7 <diag+0xb7>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11c4 <diag+0xa4>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    11b1 <diag+0x91>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x6e,%ecx
call   1030 <map@plt>
movsd  -0x30(%rbp),%xmm0
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
movsd  %xmm0,(%rax,%rcx,8)
jmp    11b6 <diag+0x96>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    115b <diag+0x3b>
jmp    11c9 <diag+0xa9>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1148 <diag+0x28>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<diag>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r15
mov    %esi,%edi
mov    %edx,0xc(%rsp)
mov    %edx,%esi
call   1040 <alloc_array_z@plt>
mov    %rax,%r12
test   %ebx,%ebx
jle    11b9 <diag+0x99>
mov    0xc(%rsp),%r13d
mov    %ebx,%r14d
xor    %ebp,%ebp
jmp    1169 <diag+0x49>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbp
cmp    %r14,%rbp
je     11b9 <diag+0x99>
cmpl   $0x0,0xc(%rsp)
jle    1160 <diag+0x40>
xor    %ebx,%ebx
jmp    1189 <diag+0x69>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbx
cmp    %ebx,%r13d
je     1160 <diag+0x40>
cmp    %rbx,%rbp
jne    1180 <diag+0x60>
movsd  (%r15,%rbp,8),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r13d,%edx
mov    $0x6e,%ecx
call   1030 <map@plt>
movsd  0x10(%rsp),%xmm0
movsd  %xmm0,(%r12,%rax,8)
jmp    1180 <diag+0x60>
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double* @diag(double* %0, i32 %1, i32 %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
store double* %0, double** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = call double* @alloc_array_z(i32 %10, i32 %11)
store double* %12, double** %7
store i32 0, i32* %8
br label %13
13: ; preds = %43, %3
%14 = load i32, i32* %8
%15 = load i32, i32* %5
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %46
17: ; preds = %13
store i32 0, i32* %9
br label %18
18: ; preds = %39, %17
%19 = load i32, i32* %9
%20 = load i32, i32* %6
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %42
22: ; preds = %18
%23 = load i32, i32* %8
%24 = load i32, i32* %9
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %38
26: ; preds = %22
%27 = load double*, double** %4
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds double, double* %27, i64 %29
%31 = load double, double* %30
%32 = load double*, double** %7
%33 = load i32, i32* %8
%34 = load i32, i32* %9
%35 = load i32, i32* %6
%36 = call i64 @map(i32 %33, i32 %34, i32 %35, i8 signext 110)
%37 = getelementptr inbounds double, double* %32, i64 %36
store double %31, double* %37
br label %38
38: ; preds = %26, %22
br label %39
39: ; preds = %38
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
br label %18
42: ; preds = %18
br label %43
43: ; preds = %42
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
br label %13
46: ; preds = %13
%47 = load double*, double** %7
ret double* %47
}

LLVM IR (-O2):
define double* @diag(double* nocapture %0, i32 %1, i32 %2) {
%4 = tail call double* @alloc_array_z(i32 %1, i32 %2)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %28
6: ; preds = %3
%7 = icmp sgt i32 %2, 0
%8 = zext i32 %1 to i64
br label %9
9: ; preds = %6, %25
%10 = phi i64 [ 0, %6 ], [ %26, %25 ]
br i1 %7, label %11, label %25
11: ; preds = %9
%12 = getelementptr inbounds double, double* %0, i64 %10
%13 = trunc i64 %10 to i32
br label %14
14: ; preds = %11, %22
%15 = phi i32 [ 0, %11 ], [ %23, %22 ]
%16 = zext i32 %15 to i64
%17 = icmp eq i64 %10, %16
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load double, double* %12
%20 = tail call i64 @map(i32 %13, i32 %15, i32 %2, i8 signext 110)
%21 = getelementptr inbounds double, double* %4, i64 %20
store double %19, double* %21
br label %22
22: ; preds = %14, %18
%23 = add nuw nsw i32 %15, 1
%24 = icmp eq i32 %23, %2
br i1 %24, label %25, label %14
25: ; preds = %22, %9
%26 = add nuw nsw i64 %10, 1
%27 = icmp eq i64 %26, %8
br i1 %27, label %28, label %9
28: ; preds = %25, %3
ret double* %4
}