#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fatalx(char *, int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ getdiag(double *, double *, int);
int /*<<< orphan*/ printf(char *, ...);
void pmat(double *mat, int n) {
  int i, j;
  double *diag;
  if ((diag = (double *)calloc((n), sizeof(double))) == NULL)
    fatalx("Unable to allocate %d unit(s) for item \n", n);
  getdiag(diag, mat, n);
  printf("pmat:\n");
  for (i = 0; i < n; i++) {
    printf("diag %5d %9.3f\n", i, diag[i]);
    for (j = 0; j < n; j++) {
      if ((j % 10) == 9)
        printf("\n");
      if ((n % 10) != 0)
        printf("%9.3f ", mat[i * n + j]);
    }
    printf("\n");
  }
  printf("\n");
  printf("\n");
  free(diag);
}


x86 assembly (clang, -O0):
<pmat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1189 <pmat+0x39>
mov    -0xc(%rbp),%esi
lea    0xe7c(%rip),%rdi
call   1050 <fatalx@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <getdiag@plt>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1274 <pmat+0x124>
mov    -0x10(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe5f(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1258 <pmat+0x108>
mov    -0x14(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x9,%edx
jne    120d <pmat+0xbd>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1245 <pmat+0xf5>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
imul   -0xc(%rbp),%ecx
add    -0x14(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe02(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
jmp    124a <pmat+0xfa>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11df <pmat+0x8f>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ae <pmat+0x5e>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pmat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,%edi
mov    $0x8,%esi
call   1090 <calloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11a5 <pmat+0x35>
lea    0xe62(%rip),%rdi
mov    %ebp,%esi
call   1060 <fatalx@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <getdiag@plt>
lea    0xe87(%rip),%rdi
call   1070 <puts@plt>
test   %ebp,%ebp
jle    1281 <pmat+0x111>
mov    %ebp,%r12d
mov    $0xcccccccd,%r14d
mov    %r12,%rax
imul   %r14,%rax
shr    $0x23,%rax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebp
lea    0x0(,%r12,8),%rax
mov    %rax,(%rsp)
xor    %esi,%esi
lea    0xe43(%rip),%r13
mov    %r15,0x8(%rsp)
jmp    1221 <pmat+0xb1>
nopl   (%rax)
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    0x10(%rsp),%rsi
add    $0x1,%rsi
add    (%rsp),%rbx
cmp    %r12,%rsi
mov    0x8(%rsp),%r15
je     1281 <pmat+0x111>
movsd  (%r15,%rsi,8),%xmm0
lea    0xdfb(%rip),%rdi
mov    %rsi,0x10(%rsp)
mov    $0x1,%al
call   1080 <printf@plt>
xor    %r15d,%r15d
jmp    1249 <pmat+0xd9>
nop
add    $0x1,%r15
cmp    %r15,%r12
je     1200 <pmat+0x90>
mov    %r15d,%eax
imul   %r14,%rax
shr    $0x23,%rax
lea    (%rax,%rax,4),%eax
add    %eax,%eax
add    $0x9,%eax
cmp    %r15d,%eax
jne    126b <pmat+0xfb>
mov    $0xa,%edi
call   1040 <putchar@plt>
test   %ebp,%ebp
je     1240 <pmat+0xd0>
movsd  (%rbx,%r15,8),%xmm0
mov    %r13,%rdi
mov    $0x1,%al
call   1080 <printf@plt>
jmp    1240 <pmat+0xd0>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r15,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"Unable to allocate %d unit(s) for item \0A\00"
@.str.1 = [7 x i8] c"pmat:\0A\00"
@.str.2 = [16 x i8] c"diag %5d %9.3f\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [7 x i8] c"%9.3f \00"
define void @pmat(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca double*
store double* %0, double** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = call i64 @calloc(i32 %8, i32 8)
%10 = inttoptr i64 %9 to double*
store double* %10, double** %7
%11 = icmp eq double* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* %4
%14 = call i32 @fatalx(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %2
%16 = load double*, double** %7
%17 = load double*, double** %3
%18 = load i32, i32* %4
%19 = call i32 @getdiag(double* %16, double* %17, i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %21
21: ; preds = %64, %15
%22 = load i32, i32* %5
%23 = load i32, i32* %4
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %67
25: ; preds = %21
%26 = load i32, i32* %5
%27 = load double*, double** %7
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds double, double* %27, i64 %29
%31 = load double, double* %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %26, double %31)
store i32 0, i32* %6
br label %33
33: ; preds = %59, %25
%34 = load i32, i32* %6
%35 = load i32, i32* %4
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %62
37: ; preds = %33
%38 = load i32, i32* %6
%39 = srem i32 %38, 10
%40 = icmp eq i32 %39, 9
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = load i32, i32* %4
%45 = srem i32 %44, 10
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %58
47: ; preds = %43
%48 = load double*, double** %3
%49 = load i32, i32* %5
%50 = load i32, i32* %4
%51 = mul nsw i32 %49, %50
%52 = load i32, i32* %6
%53 = add nsw i32 %51, %52
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds double, double* %48, i64 %54
%56 = load double, double* %55
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), double %56)
br label %58
58: ; preds = %47, %43
br label %59
59: ; preds = %58
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
store i32 %61, i32* %6
br label %33
62: ; preds = %33
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %64
64: ; preds = %62
%65 = load i32, i32* %5
%66 = add nsw i32 %65, 1
store i32 %66, i32* %5
br label %21
67: ; preds = %21
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%70 = load double*, double** %7
%71 = call i32 @free(double* %70)
ret void
}

LLVM IR (-O2):
@.str = [41 x i8] c"Unable to allocate %d unit(s) for item \0A\00"
@.str.2 = [16 x i8] c"diag %5d %9.3f\0A\00"
@.str.4 = [7 x i8] c"%9.3f \00"
@str = [6 x i8] c"pmat:\00"
define void @pmat(double* %0, i32 %1) {
%3 = tail call i64 @calloc(i32 %1, i32 8)
%4 = inttoptr i64 %3 to double*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @fatalx(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %8
8: ; preds = %6, %2
%9 = tail call i32 @getdiag(double* %4, double* %0, i32 %1)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%11 = icmp sgt i32 %1, 0
br i1 %11, label %12, label %44
12: ; preds = %8
%13 = urem i32 %1, 10
%14 = icmp eq i32 %13, 0
%15 = zext i32 %1 to i64
%16 = zext i32 %1 to i64
br label %17
17: ; preds = %40, %12
%18 = phi i64 [ 0, %12 ], [ %42, %40 ]
%19 = getelementptr inbounds double, double* %4, i64 %18
%20 = load double, double* %19
%21 = trunc i64 %18 to i32
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %21, double %20)
%23 = mul nsw i64 %18, %15
br label %24
24: ; preds = %17, %37
%25 = phi i64 [ 0, %17 ], [ %38, %37 ]
%26 = trunc i64 %25 to i32
%27 = urem i32 %26, 10
%28 = icmp eq i32 %27, 9
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = tail call i32 @putchar(i32 10)
br label %31
31: ; preds = %29, %24
br i1 %14, label %37, label %32
32: ; preds = %31
%33 = add nuw nsw i64 %25, %23
%34 = getelementptr inbounds double, double* %0, i64 %33
%35 = load double, double* %34
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), double %35)
br label %37
37: ; preds = %31, %32
%38 = add nuw nsw i64 %25, 1
%39 = icmp eq i64 %38, %16
br i1 %39, label %40, label %24
40: ; preds = %37
%41 = tail call i32 @putchar(i32 10)
%42 = add nuw nsw i64 %18, 1
%43 = icmp eq i64 %42, %16
br i1 %43, label %44, label %17
44: ; preds = %40, %8
%45 = tail call i32 @putchar(i32 10)
%46 = tail call i32 @putchar(i32 10)
%47 = tail call i32 @free(double* %4)
ret void
}