#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 */
int /*<<< orphan*/ dcopy(int, double *, int, double *, int);
int dgels(char, int, int, int, double *, int, double *, int);
int /*<<< orphan*/ dgemv(char, int, int, int, double *, int, double *, int,
                         int /*<<< orphan*/, double *, int);
int dposv(unsigned char, int, int, double *, int, double *, int);
int /*<<< orphan*/ dsyrk(unsigned char, char, int, int, int, double *, int,
                         int /*<<< orphan*/, double *, int);
double sqrt(double);
int wls(double *X, int n, int p, double *y, double *w, char method, double *XTX,
        double *sqw, double *sqwX, double *sqwy, double *coef) {
  int i, j;
  for (i = 0; i < n; i++) {
    sqw[i] = sqrt(w[i]);
    for (j = 0; j < p; j++) {
      sqwX[i + j * n] = X[i + j * n] * sqw[i];
    }
    sqwy[i] = sqw[i] * y[i];
  }
  int info;
  if (method == 'q') {
    info = dgels('n', n, p, 1, sqwX, n, sqwy, n);
    dcopy(p, sqwy, 1, coef, 1);
  } else {
    dsyrk('u', 't', p, n, 1, sqwX, n, 0, XTX, p);
    dgemv('t', n, p, 1, sqwX, n, sqwy, 1, 0, coef, 1);
    info = dposv('u', p, 1, XTX, p, coef, p);
  }
  return info;
}


x86 assembly (clang, -O0):
<wls>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x58,%rsp
mov    %r9b,%al
mov    0x30(%rbp),%r9
mov    0x28(%rbp),%r9
mov    0x20(%rbp),%r9
mov    0x18(%rbp),%r9
mov    0x10(%rbp),%r9
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %al,-0x29(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    125a <wls+0xfa>
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1080 <sqrt@plt>
mov    0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1225 <wls+0xc5>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%edx
imul   -0x14(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
mov    0x20(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%edx
imul   -0x14(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11ce <wls+0x6e>
mov    0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
mov    0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    119c <wls+0x3c>
movsbl -0x29(%rbp),%eax
cmp    $0x71,%eax
jne    12b4 <wls+0x154>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    0x20(%rbp),%r8
mov    -0x14(%rbp),%r9d
mov    0x28(%rbp),%r10
mov    -0x14(%rbp),%eax
mov    $0x6e,%edi
mov    $0x1,%ecx
mov    %r10,(%rsp)
mov    %eax,0x8(%rsp)
call   1050 <dgels@plt>
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
mov    0x28(%rbp),%rsi
mov    0x30(%rbp),%rcx
mov    $0x1,%r8d
mov    %r8d,%edx
call   1030 <dcopy@plt>
jmp    1365 <wls+0x205>
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    0x20(%rbp),%r9
mov    -0x14(%rbp),%r11d
mov    0x10(%rbp),%r10
mov    -0x18(%rbp),%eax
mov    $0x75,%edi
mov    $0x74,%esi
mov    $0x1,%r8d
xor    %ebx,%ebx
mov    %r11d,(%rsp)
movl   $0x0,0x8(%rsp)
mov    %r10,0x10(%rsp)
mov    %eax,0x18(%rsp)
call   1070 <dsyrk@plt>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    0x20(%rbp),%r8
mov    -0x14(%rbp),%r9d
mov    0x28(%rbp),%r10
mov    0x30(%rbp),%rax
mov    $0x74,%edi
mov    $0x1,%ecx
xor    %r11d,%r11d
mov    %r10,(%rsp)
movl   $0x1,0x8(%rsp)
movl   $0x0,0x10(%rsp)
mov    %rax,0x18(%rsp)
movl   $0x1,0x20(%rsp)
call   1060 <dgemv@plt>
mov    -0x18(%rbp),%esi
mov    0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8d
mov    0x30(%rbp),%r9
mov    -0x18(%rbp),%eax
mov    $0x75,%edi
mov    $0x1,%edx
mov    %eax,(%rsp)
call   1040 <dposv@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<wls>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r9d,0xc(%rsp)
mov    %rdi,0x10(%rsp)
mov    0x98(%rsp),%r10
mov    %rsi,(%rsp)
test   %esi,%esi
jle    12f3 <wls+0x193>
mov    %rcx,%r9
mov    0x88(%rsp),%rsi
mov    (%rsp),%r11d
mov    %edx,%eax
mov    %rax,0x40(%rsp)
mov    %eax,%edi
and    $0xfffffffe,%edi
mov    0x90(%rsp),%r12
lea    (%r12,%r11,8),%rbp
mov    %r11,%r14
shl    $0x4,%r14
mov    0x10(%rsp),%r15
lea    (%r15,%r11,8),%rbx
xor    %r13d,%r13d
xorpd  %xmm1,%xmm1
mov    %rdx,0x38(%rsp)
mov    %r8,0x30(%rsp)
mov    %rcx,0x28(%rsp)
mov    %r11,0x20(%rsp)
mov    %rdi,0x18(%rsp)
jmp    1219 <wls+0xb9>
cs nopw 0x0(%rax,%rax,1)
nop
mulsd  (%r9,%r13,8),%xmm0
movsd  %xmm0,(%r10,%r13,8)
add    $0x1,%r13
add    $0x8,%rbp
add    $0x8,%rbx
add    $0x8,%r15
add    $0x8,%r12
cmp    %r11,%r13
je     12f3 <wls+0x193>
movsd  (%r8,%r13,8),%xmm0
ucomisd %xmm1,%xmm0
jb     1230 <wls+0xd0>
sqrtsd %xmm0,%xmm0
jmp    1262 <wls+0x102>
nopl   0x0(%rax,%rax,1)
call   1080 <sqrt@plt>
xorpd  %xmm1,%xmm1
mov    0x18(%rsp),%rdi
mov    0x20(%rsp),%r11
mov    0x88(%rsp),%rsi
mov    0x28(%rsp),%r9
mov    0x30(%rsp),%r8
mov    0x98(%rsp),%r10
mov    0x38(%rsp),%rdx
movsd  %xmm0,(%rsi,%r13,8)
test   %edx,%edx
jle    11f0 <wls+0x90>
cmp    $0x1,%edx
jne    1280 <wls+0x120>
xor    %eax,%eax
jmp    12bf <wls+0x15f>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %ecx,%ecx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mulsd  (%r15,%rcx,1),%xmm0
movsd  %xmm0,(%r12,%rcx,1)
movsd  (%rbx,%rcx,1),%xmm0
mulsd  (%rsi,%r13,8),%xmm0
movsd  %xmm0,0x0(%rbp,%rcx,1)
add    $0x2,%rax
movsd  (%rsi,%r13,8),%xmm0
add    %r14,%rcx
cmp    %rax,%rdi
jne    1290 <wls+0x130>
testb  $0x1,0x40(%rsp)
je     11f0 <wls+0x90>
imul   %r11,%rax
add    %r13,%rax
mov    0x10(%rsp),%rcx
mulsd  (%rcx,%rax,8),%xmm0
mov    0x90(%rsp),%rcx
movsd  %xmm0,(%rcx,%rax,8)
movsd  (%rsi,%r13,8),%xmm0
jmp    11f0 <wls+0x90>
mov    0xa0(%rsp),%r13
cmpb   $0x71,0xc(%rsp)
jne    1354 <wls+0x1f4>
mov    $0x6e,%edi
mov    (%rsp),%rsi
mov    %rdx,%rbx
mov    $0x1,%ecx
mov    0x90(%rsp),%r8
mov    %esi,%r9d
push   %rsi
push   %r10
mov    %r10,%rbp
call   1050 <dgels@plt>
add    $0x10,%rsp
mov    %eax,%r14d
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
mov    %r13,%rcx
mov    $0x1,%r8d
call   1030 <dcopy@plt>
mov    %r14d,%eax
add    $0x48,%rsp
jmp    13d8 <wls+0x278>
mov    0x80(%rsp),%r12
mov    $0x75,%edi
mov    $0x74,%esi
mov    %rdx,%rbx
mov    (%rsp),%rbp
mov    %ebp,%ecx
mov    $0x1,%r8d
mov    0x90(%rsp),%r15
mov    %r15,%r9
push   %rdx
push   %r12
push   $0x0
push   %rbp
mov    %r10,%r14
call   1070 <dsyrk@plt>
add    $0x18,%rsp
mov    $0x74,%edi
mov    %ebp,%esi
mov    %ebx,%edx
mov    $0x1,%ecx
mov    %r15,%r8
mov    %ebp,%r9d
push   $0x1
push   %r13
push   $0x0
push   $0x1
push   %r14
call   1060 <dgemv@plt>
add    $0x28,%rsp
mov    $0x75,%edi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %r12,%rcx
mov    %ebx,%r8d
mov    %r13,%r9
push   %rbx
call   1040 <dposv@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @wls(double* %0, i32 %1, i32 %2, double* %3, double* %4, i8 signext %5, double* %6, double* %7, double* %8, double* %9, double* %10) {
%12 = alloca double*
%13 = alloca i32
%14 = alloca i32
%15 = alloca double*
%16 = alloca double*
%17 = alloca i8
%18 = alloca double*
%19 = alloca double*
%20 = alloca double*
%21 = alloca double*
%22 = alloca double*
%23 = alloca i32
%24 = alloca i32
%25 = alloca i32
store double* %0, double** %12
store i32 %1, i32* %13
store i32 %2, i32* %14
store double* %3, double** %15
store double* %4, double** %16
store i8 %5, i8* %17
store double* %6, double** %18
store double* %7, double** %19
store double* %8, double** %20
store double* %9, double** %21
store double* %10, double** %22
store i32 0, i32* %23
br label %26
26: ; preds = %88, %11
%27 = load i32, i32* %23
%28 = load i32, i32* %13
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %91
30: ; preds = %26
%31 = load double*, double** %16
%32 = load i32, i32* %23
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds double, double* %31, i64 %33
%35 = load double, double* %34
%36 = call double @sqrt(double %35)
%37 = load double*, double** %19
%38 = load i32, i32* %23
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds double, double* %37, i64 %39
store double %36, double* %40
store i32 0, i32* %24
br label %41
41: ; preds = %69, %30
%42 = load i32, i32* %24
%43 = load i32, i32* %14
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %72
45: ; preds = %41
%46 = load double*, double** %12
%47 = load i32, i32* %23
%48 = load i32, i32* %24
%49 = load i32, i32* %13
%50 = mul nsw i32 %48, %49
%51 = add nsw i32 %47, %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds double, double* %46, i64 %52
%54 = load double, double* %53
%55 = load double*, double** %19
%56 = load i32, i32* %23
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds double, double* %55, i64 %57
%59 = load double, double* %58
%60 = fmul double %54, %59
%61 = load double*, double** %20
%62 = load i32, i32* %23
%63 = load i32, i32* %24
%64 = load i32, i32* %13
%65 = mul nsw i32 %63, %64
%66 = add nsw i32 %62, %65
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds double, double* %61, i64 %67
store double %60, double* %68
br label %69
69: ; preds = %45
%70 = load i32, i32* %24
%71 = add nsw i32 %70, 1
store i32 %71, i32* %24
br label %41
72: ; preds = %41
%73 = load double*, double** %19
%74 = load i32, i32* %23
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds double, double* %73, i64 %75
%77 = load double, double* %76
%78 = load double*, double** %15
%79 = load i32, i32* %23
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds double, double* %78, i64 %80
%82 = load double, double* %81
%83 = fmul double %77, %82
%84 = load double*, double** %21
%85 = load i32, i32* %23
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds double, double* %84, i64 %86
store double %83, double* %87
br label %88
88: ; preds = %72
%89 = load i32, i32* %23
%90 = add nsw i32 %89, 1
store i32 %90, i32* %23
br label %26
91: ; preds = %26
%92 = load i8, i8* %17
%93 = sext i8 %92 to i32
%94 = icmp eq i32 %93, 113
br i1 %94, label %95, label %107
95: ; preds = %91
%96 = load i32, i32* %13
%97 = load i32, i32* %14
%98 = load double*, double** %20
%99 = load i32, i32* %13
%100 = load double*, double** %21
%101 = load i32, i32* %13
%102 = call i32 @dgels(i8 signext 110, i32 %96, i32 %97, i32 1, double* %98, i32 %99, double* %100, i32 %101)
store i32 %102, i32* %25
%103 = load i32, i32* %14
%104 = load double*, double** %21
%105 = load double*, double** %22
%106 = call i32 @dcopy(i32 %103, double* %104, i32 1, double* %105, i32 1)
br label %128
107: ; preds = %91
%108 = load i32, i32* %14
%109 = load i32, i32* %13
%110 = load double*, double** %20
%111 = load i32, i32* %13
%112 = load double*, double** %18
%113 = load i32, i32* %14
%114 = call i32 @dsyrk(i8 zeroext 117, i8 signext 116, i32 %108, i32 %109, i32 1, double* %110, i32 %111, i32 0, double* %112, i32 %113)
%115 = load i32, i32* %13
%116 = load i32, i32* %14
%117 = load double*, double** %20
%118 = load i32, i32* %13
%119 = load double*, double** %21
%120 = load double*, double** %22
%121 = call i32 @dgemv(i8 signext 116, i32 %115, i32 %116, i32 1, double* %117, i32 %118, double* %119, i32 1, i32 0, double* %120, i32 1)
%122 = load i32, i32* %14
%123 = load double*, double** %18
%124 = load i32, i32* %14
%125 = load double*, double** %22
%126 = load i32, i32* %14
%127 = call i32 @dposv(i8 zeroext 117, i32 %122, i32 1, double* %123, i32 %124, double* %125, i32 %126)
store i32 %127, i32* %25
br label %128
128: ; preds = %107, %95
%129 = load i32, i32* %25
ret i32 %129
}

LLVM IR (-O2):
define i32 @wls(double* nocapture %0, i32 %1, i32 %2, double* nocapture %3, double* nocapture %4, i8 signext %5, double* %6, double* nocapture %7, double* %8, double* %9, double* %10) {
%12 = icmp sgt i32 %1, 0
br i1 %12, label %13, label %71
13: ; preds = %11
%14 = icmp sgt i32 %2, 0
%15 = zext i32 %1 to i64
%16 = zext i32 %1 to i64
%17 = zext i32 %2 to i64
%18 = and i64 %17, 1
%19 = icmp eq i32 %2, 1
%20 = and i64 %17, 4294967294
%21 = icmp eq i64 %18, 0
br label %22
22: ; preds = %13, %63
%23 = phi i64 [ 0, %13 ], [ %69, %63 ]
%24 = getelementptr inbounds double, double* %4, i64 %23
%25 = load double, double* %24
%26 = tail call double @sqrt(double %25)
%27 = getelementptr inbounds double, double* %7, i64 %23
store double %26, double* %27
br i1 %14, label %28, label %63
28: ; preds = %22
br i1 %19, label %51, label %29
29: ; preds = %28, %29
%30 = phi double [ %48, %29 ], [ %26, %28 ]
%31 = phi i64 [ %47, %29 ], [ 0, %28 ]
%32 = phi i64 [ %49, %29 ], [ 0, %28 ]
%33 = mul nsw i64 %31, %15
%34 = add nuw nsw i64 %33, %23
%35 = getelementptr inbounds double, double* %0, i64 %34
%36 = load double, double* %35
%37 = fmul double %36, %30
%38 = getelementptr inbounds double, double* %8, i64 %34
store double %37, double* %38
%39 = or i64 %31, 1
%40 = load double, double* %27
%41 = mul nsw i64 %39, %15
%42 = add nuw nsw i64 %41, %23
%43 = getelementptr inbounds double, double* %0, i64 %42
%44 = load double, double* %43
%45 = fmul double %44, %40
%46 = getelementptr inbounds double, double* %8, i64 %42
store double %45, double* %46
%47 = add nuw nsw i64 %31, 2
%48 = load double, double* %27
%49 = add i64 %32, 2
%50 = icmp eq i64 %49, %20
br i1 %50, label %51, label %29
51: ; preds = %29, %28
%52 = phi double [ undef, %28 ], [ %48, %29 ]
%53 = phi double [ %26, %28 ], [ %48, %29 ]
%54 = phi i64 [ 0, %28 ], [ %47, %29 ]
br i1 %21, label %63, label %55
55: ; preds = %51
%56 = mul nsw i64 %54, %15
%57 = add nuw nsw i64 %56, %23
%58 = getelementptr inbounds double, double* %0, i64 %57
%59 = load double, double* %58
%60 = fmul double %59, %53
%61 = getelementptr inbounds double, double* %8, i64 %57
store double %60, double* %61
%62 = load double, double* %27
br label %63
63: ; preds = %55, %51, %22
%64 = phi double [ %26, %22 ], [ %52, %51 ], [ %62, %55 ]
%65 = getelementptr inbounds double, double* %3, i64 %23
%66 = load double, double* %65
%67 = fmul double %64, %66
%68 = getelementptr inbounds double, double* %9, i64 %23
store double %67, double* %68
%69 = add nuw nsw i64 %23, 1
%70 = icmp eq i64 %69, %16
br i1 %70, label %71, label %22
71: ; preds = %63, %11
%72 = icmp eq i8 %5, 113
br i1 %72, label %73, label %76
73: ; preds = %71
%74 = tail call i32 @dgels(i8 signext 110, i32 %1, i32 %2, i32 1, double* %8, i32 %1, double* %9, i32 %1)
%75 = tail call i32 @dcopy(i32 %2, double* %9, i32 1, double* %10, i32 1)
br label %80
76: ; preds = %71
%77 = tail call i32 @dsyrk(i8 zeroext 117, i8 signext 116, i32 %2, i32 %1, i32 1, double* %8, i32 %1, i32 0, double* %6, i32 %2)
%78 = tail call i32 @dgemv(i8 signext 116, i32 %1, i32 %2, i32 1, double* %8, i32 %1, double* %9, i32 1, i32 0, double* %10, i32 1)
%79 = tail call i32 @dposv(i8 zeroext 117, i32 %2, i32 1, double* %6, i32 %2, double* %10, i32 %2)
br label %80
80: ; preds = %76, %73
%81 = phi i32 [ %74, %73 ], [ %79, %76 ]
ret i32 %81
}