#include <stdio.h>
#include <stdlib.h>

typedef struct 
{
  double re, im;
  

} complexd;

void zgeqrf_(void *m, void *n, void *q, void *m2, void *tau, void *work, void *lwork, void *info);

void zungqr_(void *m, void *n, void *n2,  void *q, void *m2, void *tau, void *work, void *lwork, void *info);

void zqr_(complexd *q, complexd *r, int *mm, int *nn)
{

  int info;
  int lwork;
  double *work;
  double *tau;
  double *qr;
  int i, j;
  int m, n;
  
  m = *mm;
  n = *nn;

  tau = malloc(2*sizeof(double)*n);
  qr = malloc(2*sizeof(double)*n*m);
  work = malloc(2*sizeof(double)*8); // dummy

  lwork = -1;
  
  zgeqrf_(&m, &n, q, &m, tau, work, &lwork, &info);
  lwork = (int)work[0];
  free(work);
  work = malloc(2*sizeof(double)*lwork);
  zgeqrf_(&m, &n, q, &m, tau, work, &lwork, &info);
  
  for (i=0;i<n*n;i++) {
    
  }

  for(i=0;i<n;i++) {
    r[n*i+i] = q[m*i+i];
    for(j=i+1;j<n;j++) {
      r[j*n+i] = q[m*j+i];
    }
  }
  lwork = -1;
  
  zungqr(&m, &n, &n,  q, &m, tau, work, &lwork, &info);
  lwork = (int)work[0];
  free(work);
  work = malloc(2*sizeof(double)*lwork);
  
  zungqr(&m, &n, &n,  q, &m, tau, work, &lwork, &info);
  free(work);
  free(tau);
  free(qr);

}
