; LLVM IR from tsotchke/quantum_geometric_tensor :: src/quantum_geometric/core (lapack_wrapper)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.anon = type { i32, i8, i64, ptr }

@lapack_state = external hidden global %struct.anon, align 8

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @initialize_lapack() #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden void @convert_layout(ptr noundef, ptr noundef, i64 noundef, i64 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare void @cgesv_(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #4

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @lapack_solve_general(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i64 noundef %4, i32 noundef %5) #0 {
  %7 = alloca i1, align 1
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = alloca i32, align 4
  %19 = alloca i32, align 4
  %20 = alloca i32, align 4
  %21 = alloca i32, align 4
  %22 = alloca ptr, align 8
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store ptr %2, ptr %10, align 8, !tbaa !5
  store i64 %3, ptr %11, align 8, !tbaa !9
  store i64 %4, ptr %12, align 8, !tbaa !9
  store i32 %5, ptr %13, align 4, !tbaa !11
  %23 = call zeroext i1 @initialize_lapack()
  br i1 %23, label %24, label %39

24:                                               ; preds = %6
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %39

27:                                               ; preds = %24
  %28 = load ptr, ptr %9, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %39

30:                                               ; preds = %27
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = icmp ne ptr %31, null
  br i1 %32, label %33, label %39

33:                                               ; preds = %30
  %34 = load i64, ptr %11, align 8, !tbaa !9
  %35 = icmp eq i64 %34, 0
  br i1 %35, label %39, label %36

36:                                               ; preds = %33
  %37 = load i64, ptr %12, align 8, !tbaa !9
  %38 = icmp eq i64 %37, 0
  br i1 %38, label %39, label %40

39:                                               ; preds = %36, %33, %30, %27, %24, %6
  store i32 -1, ptr @lapack_state, align 8, !tbaa !13
  store i1 false, ptr %7, align 1
  br label %106

40:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %41 = load i64, ptr %11, align 8, !tbaa !9
  %42 = load i64, ptr %11, align 8, !tbaa !9
  %43 = mul i64 %41, %42
  %44 = mul i64 %43, 8
  %45 = call noalias ptr @malloc(i64 noundef %44) #6
  store ptr %45, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  %46 = load i64, ptr %11, align 8, !tbaa !9
  %47 = load i64, ptr %12, align 8, !tbaa !9
  %48 = mul i64 %46, %47
  %49 = mul i64 %48, 8
  %50 = call noalias ptr @malloc(i64 noundef %49) #6
  store ptr %50, ptr %15, align 8, !tbaa !5
  %51 = load ptr, ptr %14, align 8, !tbaa !5
  %52 = icmp ne ptr %51, null
  br i1 %52, label %53, label %56

53:                                               ; preds = %40
  %54 = load ptr, ptr %15, align 8, !tbaa !5
  %55 = icmp ne ptr %54, null
  br i1 %55, label %59, label %56

56:                                               ; preds = %53, %40
  %57 = load ptr, ptr %14, align 8, !tbaa !5
  call void @free(ptr noundef %57) #5
  %58 = load ptr, ptr %15, align 8, !tbaa !5
  call void @free(ptr noundef %58) #5
  store i32 -2, ptr @lapack_state, align 8, !tbaa !13
  store i1 false, ptr %7, align 1
  store i32 1, ptr %16, align 4
  br label %105

59:                                               ; preds = %53
  %60 = load ptr, ptr %14, align 8, !tbaa !5
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  %62 = load i64, ptr %11, align 8, !tbaa !9
  %63 = load i64, ptr %11, align 8, !tbaa !9
  %64 = load i32, ptr %13, align 4, !tbaa !11
  call void @convert_layout(ptr noundef %60, ptr noundef %61, i64 noundef %62, i64 noundef %63, i32 noundef %64, i32 noundef 102)
  %65 = load ptr, ptr %15, align 8, !tbaa !5
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = load i64, ptr %11, align 8, !tbaa !9
  %68 = load i64, ptr %12, align 8, !tbaa !9
  %69 = load i32, ptr %13, align 4, !tbaa !11
  call void @convert_layout(ptr noundef %65, ptr noundef %66, i64 noundef %67, i64 noundef %68, i32 noundef %69, i32 noundef 102)
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #5
  %70 = load i64, ptr %11, align 8, !tbaa !9
  %71 = trunc i64 %70 to i32
  store i32 %71, ptr %17, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %18) #5
  %72 = load i64, ptr %12, align 8, !tbaa !9
  %73 = trunc i64 %72 to i32
  store i32 %73, ptr %18, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %19) #5
  %74 = load i32, ptr %17, align 4, !tbaa !11
  store i32 %74, ptr %19, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %20) #5
  %75 = load i32, ptr %17, align 4, !tbaa !11
  store i32 %75, ptr %20, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %21) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %22) #5
  %76 = load i64, ptr %11, align 8, !tbaa !9
  %77 = mul i64 %76, 4
  %78 = call noalias ptr @malloc(i64 noundef %77) #6
  store ptr %78, ptr %22, align 8, !tbaa !5
  %79 = load ptr, ptr %22, align 8, !tbaa !5
  %80 = icmp ne ptr %79, null
  br i1 %80, label %84, label %81

81:                                               ; preds = %59
  %82 = load ptr, ptr %15, align 8, !tbaa !5
  call void @free(ptr noundef %82) #5
  %83 = load ptr, ptr %14, align 8, !tbaa !5
  call void @free(ptr noundef %83) #5
  store i32 -2, ptr @lapack_state, align 8, !tbaa !13
  store i1 false, ptr %7, align 1
  store i32 1, ptr %16, align 4
  br label %104

84:                                               ; preds = %59
  %85 = load ptr, ptr %14, align 8, !tbaa !5
  %86 = load ptr, ptr %22, align 8, !tbaa !5
  %87 = load ptr, ptr %15, align 8, !tbaa !5
  call void @cgesv_(ptr noundef %17, ptr noundef %18, ptr noundef %85, ptr noundef %19, ptr noundef %86, ptr noundef %87, ptr noundef %20, ptr noundef %21)
  %88 = load ptr, ptr %10, align 8, !tbaa !5
  %89 = load ptr, ptr %15, align 8, !tbaa !5
  %90 = load i64, ptr %11, align 8, !tbaa !9
  %91 = load i64, ptr %12, align 8, !tbaa !9
  %92 = load i32, ptr %13, align 4, !tbaa !11
  call void @convert_layout(ptr noundef %88, ptr noundef %89, i64 noundef %90, i64 noundef %91, i32 noundef 102, i32 noundef %92)
  %93 = load ptr, ptr %22, align 8, !tbaa !5
  call void @free(ptr noundef %93) #5
  %94 = load ptr, ptr %15, align 8, !tbaa !5
  call void @free(ptr noundef %94) #5
  %95 = load ptr, ptr %14, align 8, !tbaa !5
  call void @free(ptr noundef %95) #5
  %96 = load i32, ptr %21, align 4, !tbaa !11
  %97 = icmp ne i32 %96, 0
  br i1 %97, label %98, label %103

98:                                               ; preds = %84
  %99 = load i32, ptr %21, align 4, !tbaa !11
  %100 = icmp sgt i32 %99, 0
  %101 = zext i1 %100 to i64
  %102 = select i1 %100, i32 -3, i32 -1
  store i32 %102, ptr @lapack_state, align 8, !tbaa !13
  store i1 false, ptr %7, align 1
  store i32 1, ptr %16, align 4
  br label %104

103:                                              ; preds = %84
  store i32 0, ptr @lapack_state, align 8, !tbaa !13
  store i1 true, ptr %7, align 1
  store i32 1, ptr %16, align 4
  br label %104

104:                                              ; preds = %103, %98, %81
  call void @llvm.lifetime.end.p0(i64 8, ptr %22) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %21) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #5
  br label %105

105:                                              ; preds = %104, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  br label %106

106:                                              ; preds = %105, %39
  %107 = load i1, ptr %7, align 1
  ret i1 %107
}

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind allocsize(0) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"", !12, i64 0, !15, i64 4, !10, i64 8, !6, i64 16}
!15 = !{!"_Bool", !7, i64 0}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.anon = type { i32, i8, i64, ptr }

@lapack_state = external hidden local_unnamed_addr global %struct.anon, align 8

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @initialize_lapack() local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden void @convert_layout(ptr noundef, ptr noundef, i64 noundef, i64 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare void @cgesv_(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @lapack_solve_general(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i64 noundef %4, i32 noundef %5) local_unnamed_addr #0 {
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = tail call zeroext i1 @initialize_lapack()
  %13 = icmp ne ptr %0, null
  %or.cond = and i1 %13, %12
  %14 = icmp ne ptr %1, null
  %or.cond3 = and i1 %14, %or.cond
  %15 = icmp ne ptr %2, null
  %or.cond5 = and i1 %15, %or.cond3
  %or.cond5.not = xor i1 %or.cond5, true
  %16 = icmp eq i64 %3, 0
  %or.cond7 = or i1 %16, %or.cond5.not
  %17 = icmp eq i64 %4, 0
  %or.cond9 = or i1 %17, %or.cond7
  br i1 %or.cond9, label %18, label %19

18:                                               ; preds = %6
  store i32 -1, ptr @lapack_state, align 8, !tbaa !5
  br label %40

19:                                               ; preds = %6
  %20 = shl i64 %3, 3
  %21 = mul i64 %20, %3
  %22 = tail call noalias ptr @malloc(i64 noundef %21) #5
  %23 = mul i64 %20, %4
  %24 = tail call noalias ptr @malloc(i64 noundef %23) #5
  %25 = icmp ne ptr %22, null
  %26 = icmp ne ptr %24, null
  %or.cond11 = and i1 %25, %26
  br i1 %or.cond11, label %28, label %27

27:                                               ; preds = %19
  tail call void @free(ptr noundef %22) #6
  tail call void @free(ptr noundef %24) #6
  store i32 -2, ptr @lapack_state, align 8, !tbaa !5
  br label %40

28:                                               ; preds = %19
  tail call void @convert_layout(ptr noundef nonnull %22, ptr noundef %0, i64 noundef %3, i64 noundef %3, i32 noundef %5, i32 noundef 102)
  tail call void @convert_layout(ptr noundef nonnull %24, ptr noundef nonnull %1, i64 noundef %3, i64 noundef %4, i32 noundef %5, i32 noundef 102)
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %7) #6
  %29 = trunc i64 %3 to i32
  store i32 %29, ptr %7, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %8) #6
  %30 = trunc i64 %4 to i32
  store i32 %30, ptr %8, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %9) #6
  store i32 %29, ptr %9, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %10) #6
  store i32 %29, ptr %10, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %11) #6
  %31 = shl i64 %3, 2
  %32 = tail call noalias ptr @malloc(i64 noundef %31) #5
  %.not = icmp eq ptr %32, null
  br i1 %.not, label %33, label %34

33:                                               ; preds = %28
  tail call void @free(ptr noundef nonnull %24) #6
  tail call void @free(ptr noundef nonnull %22) #6
  br label %39

34:                                               ; preds = %28
  call void @cgesv_(ptr noundef nonnull %7, ptr noundef nonnull %8, ptr noundef nonnull %22, ptr noundef nonnull %9, ptr noundef nonnull %32, ptr noundef nonnull %24, ptr noundef nonnull %10, ptr noundef nonnull %11) #6
  call void @convert_layout(ptr noundef nonnull %2, ptr noundef nonnull %24, i64 noundef %3, i64 noundef %4, i32 noundef 102, i32 noundef %5)
  call void @free(ptr noundef nonnull %32) #6
  call void @free(ptr noundef nonnull %24) #6
  call void @free(ptr noundef nonnull %22) #6
  %35 = load i32, ptr %11, align 4, !tbaa !13
  %.not57 = icmp eq i32 %35, 0
  br i1 %.not57, label %39, label %36

36:                                               ; preds = %34
  %37 = icmp sgt i32 %35, 0
  %38 = select i1 %37, i32 -3, i32 -1
  br label %39

39:                                               ; preds = %34, %36, %33
  %.sink = phi i32 [ %38, %36 ], [ -2, %33 ], [ 0, %34 ]
  %.0 = phi i1 [ false, %36 ], [ false, %33 ], [ true, %34 ]
  store i32 %.sink, ptr @lapack_state, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %11) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %10) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %9) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %8) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %7) #6
  br label %40

40:                                               ; preds = %27, %39, %18
  %.2 = phi i1 [ false, %18 ], [ %.0, %39 ], [ false, %27 ]
  ret i1 %.2
}

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { nounwind allocsize(0) }
attributes #6 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 4, !11, i64 8, !12, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"_Bool", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!7, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass