#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*/ exit(int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ printf(char *);
void svd(int n, double *A, double *S, double *U, double *V) {
  extern void dgesvd_(char *jobu, char *jobvt, int *m, int *n, double *A,
                      int *lda, double *s, double *u, int *ldu, double *vt,
                      int *ldvt, double *work, int *lwork, int *info);
  int info, lwork;
  double wkopt;
  double *work;
  double *Vt = (double *)calloc(n * n, sizeof(double));
  char job = 'A';
  lwork = -1;
  dgesvd_(&job, &job, &n, &n, A, &n, S, U, &n, Vt, &n, &wkopt, &lwork, &info);
  lwork = (int)wkopt;
  work = (double *)calloc(lwork, sizeof(double));
  dgesvd_(&job, &job, &n, &n, A, &n, S, U, &n, Vt, &n, work, &lwork, &info);
  if (info > 0) {
    printf("DGESVD failed.\n");
    exit(1);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      V[i + j * n] = Vt[j + i * n];
    }
  }
  free(Vt);
  free(work);
}


x86 assembly (clang, -O0):
<svd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xa0,%rsp
mov    %edi,-0x24(%rbp)
mov    %rsi,-0x30(%rbp)
mov    %rdx,-0x38(%rbp)
mov    %rcx,-0x40(%rbp)
mov    %r8,-0x48(%rbp)
mov    -0x24(%rbp),%edi
imul   -0x24(%rbp),%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x68(%rbp)
movb   $0x41,-0x69(%rbp)
movl   $0xffffffff,-0x50(%rbp)
mov    -0x30(%rbp),%r8
mov    -0x38(%rbp),%r12
mov    -0x40(%rbp),%r15
mov    -0x68(%rbp),%r14
lea    -0x69(%rbp),%rsi
lea    -0x24(%rbp),%rbx
lea    -0x58(%rbp),%r11
lea    -0x50(%rbp),%r10
lea    -0x4c(%rbp),%rax
mov    %rsi,%rdi
mov    %rbx,%rdx
mov    %rbx,%rcx
mov    %rbx,%r9
mov    %r12,(%rsp)
mov    %r15,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r14,0x18(%rsp)
mov    %rbx,0x20(%rsp)
mov    %r11,0x28(%rsp)
mov    %r10,0x30(%rsp)
mov    %rax,0x38(%rsp)
call   1060 <dgesvd_@plt>
cvttsd2si -0x58(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%r8
mov    -0x38(%rbp),%r12
mov    -0x40(%rbp),%r15
mov    -0x68(%rbp),%r14
mov    -0x60(%rbp),%r11
lea    -0x69(%rbp),%rsi
lea    -0x24(%rbp),%rbx
lea    -0x50(%rbp),%r10
lea    -0x4c(%rbp),%rax
mov    %rsi,%rdi
mov    %rbx,%rdx
mov    %rbx,%rcx
mov    %rbx,%r9
mov    %r12,(%rsp)
mov    %r15,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r14,0x18(%rsp)
mov    %rbx,0x20(%rsp)
mov    %r11,0x28(%rsp)
mov    %r10,0x30(%rsp)
mov    %rax,0x38(%rsp)
call   1060 <dgesvd_@plt>
cmpl   $0x0,-0x4c(%rbp)
jle    1286 <svd+0x136>
lea    0xd89(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x70(%rbp)
mov    -0x70(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12fd <svd+0x1ad>
movl   $0x0,-0x74(%rbp)
mov    -0x74(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12ea <svd+0x19a>
mov    -0x68(%rbp),%rax
mov    -0x74(%rbp),%ecx
mov    -0x70(%rbp),%edx
imul   -0x24(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x48(%rbp),%rax
mov    -0x70(%rbp),%ecx
mov    -0x74(%rbp),%edx
imul   -0x24(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    12a0 <svd+0x150>
jmp    12ef <svd+0x19f>
mov    -0x70(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x70(%rbp)
jmp    128d <svd+0x13d>
mov    -0x68(%rbp),%rdi
call   1030 <free@plt>
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
add    $0xa0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<svd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,%r14
mov    %rcx,%rbp
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rsi,0x18(%rsp)
mov    %edi,0x14(%rsp)
imul   %edi,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r13
movb   $0x41,0xb(%rsp)
movl   $0xffffffff,0xc(%rsp)
ff
lea    0x10(%rsp),%rax
lea    0xc(%rsp),%r11
lea    0x20(%rsp),%r10
lea    0x14(%rsp),%r15
lea    0xb(%rsp),%rdi
mov    %rdi,%rsi
mov    %r15,%rdx
mov    %r15,%rcx
mov    %rbx,%r8
mov    %r15,%r9
push   %rax
push   %r11
push   %r10
push   %r15
push   %r13
push   %r15
push   %rbp
push   %r12
mov    %r12,%rbx
call   1060 <dgesvd_@plt>
add    $0x40,%rsp
cvttsd2si 0x20(%rsp),%edi
mov    %edi,0xc(%rsp)
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r12
lea    0xb(%rsp),%rdi
mov    %rdi,%rsi
mov    %r15,%rdx
mov    %r15,%rcx
mov    0x18(%rsp),%r8
mov    %r15,%r9
lea    0x10(%rsp),%rax
push   %rax
lea    0x14(%rsp),%rax
push   %rax
push   %r12
push   %r15
push   %r13
push   %r15
push   %rbp
push   %rbx
call   1060 <dgesvd_@plt>
add    $0x40,%rsp
cmpl   $0x0,0x10(%rsp)
jg     12f5 <svd+0x1a5>
mov    0x14(%rsp),%r8d
test   %r8d,%r8d
jle    12d6 <svd+0x186>
mov    %r8d,%ecx
and    $0xfffffffe,%ecx
mov    %r13,%rdx
add    $0x8,%rdx
lea    0x0(,%r8,8),%rsi
mov    %r8,%rdi
shl    $0x4,%rdi
xor    %r10d,%r10d
mov    %r14,%r9
jmp    1270 <svd+0x120>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r10
add    %rsi,%rdx
add    $0x8,%r9
cmp    %r8,%r10
je     12d6 <svd+0x186>
cmp    $0x1,%r8d
jne    1280 <svd+0x130>
xor    %eax,%eax
jmp    12b0 <svd+0x160>
nopw   0x0(%rax,%rax,1)
mov    %r9,%rbx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nop
movsd  -0x8(%rdx,%rax,8),%xmm0
movsd  %xmm0,(%rbx)
movsd  (%rdx,%rax,8),%xmm0
movsd  %xmm0,(%rbx,%rsi,1)
add    $0x2,%rax
add    %rdi,%rbx
cmp    %rax,%rcx
jne    1290 <svd+0x140>
test   $0x1,%r8b
je     1260 <svd+0x110>
mov    %r10,%rbp
imul   %r8,%rbp
add    %rax,%rbp
movsd  0x0(%r13,%rbp,8),%xmm0
imul   %r8,%rax
add    %r10,%rax
movsd  %xmm0,(%r14,%rax,8)
jmp    1260 <svd+0x110>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd04(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"DGESVD failed.\0A\00"
define void @svd(i32 %0, double* %1, double* %2, double* %3, double* %4) {
%6 = alloca i32
%7 = alloca double*
%8 = alloca double*
%9 = alloca double*
%10 = alloca double*
%11 = alloca i32
%12 = alloca i32
%13 = alloca double
%14 = alloca double*
%15 = alloca double*
%16 = alloca i8
%17 = alloca i32
%18 = alloca i32
store i32 %0, i32* %6
store double* %1, double** %7
store double* %2, double** %8
store double* %3, double** %9
store double* %4, double** %10
%19 = load i32, i32* %6
%20 = load i32, i32* %6
%21 = mul nsw i32 %19, %20
%22 = call i64 @calloc(i32 %21, i32 8)
%23 = inttoptr i64 %22 to double*
store double* %23, double** %15
store i8 65, i8* %16
store i32 -1, i32* %12
%24 = load double*, double** %7
%25 = load double*, double** %8
%26 = load double*, double** %9
%27 = load double*, double** %15
%28 = load double, double* %13
%29 = fptosi double %28 to i32
store i32 %29, i32* %12
%30 = load i32, i32* %12
%31 = call i64 @calloc(i32 %30, i32 8)
%32 = inttoptr i64 %31 to double*
store double* %32, double** %14
%33 = load double*, double** %7
%34 = load double*, double** %8
%35 = load double*, double** %9
%36 = load double*, double** %15
%37 = load double*, double** %14
%38 = load i32, i32* %11
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %5
%41 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %5
store i32 0, i32* %17
br label %44
44: ; preds = %75, %43
%45 = load i32, i32* %17
%46 = load i32, i32* %6
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %78
48: ; preds = %44
store i32 0, i32* %18
br label %49
49: ; preds = %71, %48
%50 = load i32, i32* %18
%51 = load i32, i32* %6
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %74
53: ; preds = %49
%54 = load double*, double** %15
%55 = load i32, i32* %18
%56 = load i32, i32* %17
%57 = load i32, i32* %6
%58 = mul nsw i32 %56, %57
%59 = add nsw i32 %55, %58
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds double, double* %54, i64 %60
%62 = load double, double* %61
%63 = load double*, double** %10
%64 = load i32, i32* %17
%65 = load i32, i32* %18
%66 = load i32, i32* %6
%67 = mul nsw i32 %65, %66
%68 = add nsw i32 %64, %67
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds double, double* %63, i64 %69
store double %62, double* %70
br label %71
71: ; preds = %53
%72 = load i32, i32* %18
%73 = add nsw i32 %72, 1
store i32 %73, i32* %18
br label %49
74: ; preds = %49
br label %75
75: ; preds = %74
%76 = load i32, i32* %17
%77 = add nsw i32 %76, 1
store i32 %77, i32* %17
br label %44
78: ; preds = %44
%79 = load double*, double** %15
%80 = call i32 @free(double* %79)
%81 = load double*, double** %14
%82 = call i32 @free(double* %81)
ret void
}

LLVM IR (-O2):
@str = [15 x i8] c"DGESVD failed.\00"
define void @svd(i32 %0, double* %1, double* %2, double* %3, double* nocapture writeonly %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca i8
store i32 %0, i32* %6
%11 = bitcast i32* %7 to i8*
%12 = bitcast i32* %8 to i8*
%13 = bitcast double* %9 to i8*
%14 = mul nsw i32 %0, %0
%15 = tail call i64 @calloc(i32 %14, i32 8)
%16 = inttoptr i64 %15 to double*
store i8 65, i8* %10
store i32 -1, i32* %8
%17 = load double, double* %9
%18 = fptosi double %17 to i32
store i32 %18, i32* %8
%19 = call i64 @calloc(i32 %18, i32 8)
%20 = inttoptr i64 %19 to double*
%21 = load i32, i32* %7
%22 = icmp sgt i32 %21, 0
br i1 %22, label %33, label %23
23: ; preds = %5
%24 = load i32, i32* %6
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %23
%27 = zext i32 %24 to i64
%28 = zext i32 %24 to i64
%29 = and i64 %28, 1
%30 = icmp eq i32 %24, 1
%31 = and i64 %28, 4294967294
%32 = icmp eq i64 %29, 0
br label %36
33: ; preds = %5
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %51, %26
%37 = phi i64 [ 0, %26 ], [ %52, %51 ]
%38 = mul nsw i64 %37, %27
br i1 %30, label %42, label %54
39: ; preds = %51, %23
%40 = call i32 @free(double* %16)
%41 = call i32 @free(double* %20)
ret void
42: ; preds = %54, %36
%43 = phi i64 [ 0, %36 ], [ %70, %54 ]
br i1 %32, label %51, label %44
44: ; preds = %42
%45 = add nuw nsw i64 %43, %38
%46 = getelementptr inbounds double, double* %16, i64 %45
%47 = load double, double* %46
%48 = mul nsw i64 %43, %27
%49 = add nuw nsw i64 %48, %37
%50 = getelementptr inbounds double, double* %4, i64 %49
store double %47, double* %50
br label %51
51: ; preds = %42, %44
%52 = add nuw nsw i64 %37, 1
%53 = icmp eq i64 %52, %28
br i1 %53, label %39, label %36
54: ; preds = %36, %54
%55 = phi i64 [ %70, %54 ], [ 0, %36 ]
%56 = phi i64 [ %71, %54 ], [ 0, %36 ]
%57 = add nuw nsw i64 %55, %38
%58 = getelementptr inbounds double, double* %16, i64 %57
%59 = load double, double* %58
%60 = mul nsw i64 %55, %27
%61 = add nuw nsw i64 %60, %37
%62 = getelementptr inbounds double, double* %4, i64 %61
store double %59, double* %62
%63 = or i64 %55, 1
%64 = add nuw nsw i64 %63, %38
%65 = getelementptr inbounds double, double* %16, i64 %64
%66 = load double, double* %65
%67 = mul nsw i64 %63, %27
%68 = add nuw nsw i64 %67, %37
%69 = getelementptr inbounds double, double* %4, i64 %68
store double %66, double* %69
%70 = add nuw nsw i64 %55, 2
%71 = add i64 %56, 2
%72 = icmp eq i64 %71, %31
br i1 %72, label %42, label %54
}