; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

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

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @is_hermitian(ptr noundef, i64 noundef) #0

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

20:                                               ; preds = %5
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %29

23:                                               ; preds = %20
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %29

26:                                               ; preds = %23
  %27 = load i64, ptr %8, align 8, !tbaa !9
  %28 = icmp eq i64 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %26, %23, %20, %5
  store i32 -1, ptr @lapack_state, align 8, !tbaa !15
  store i1 false, ptr %6, align 1
  br label %80

30:                                               ; preds = %26
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = load i64, ptr %8, align 8, !tbaa !9
  %33 = call zeroext i1 @is_hermitian(ptr noundef %31, i64 noundef %32)
  br i1 %33, label %34, label %38

34:                                               ; preds = %30
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = load i64, ptr %8, align 8, !tbaa !9
  %37 = call zeroext i1 @is_positive_definite(ptr noundef %35, i64 noundef %36)
  br i1 %37, label %39, label %38

38:                                               ; preds = %34, %30
  store i32 -1, ptr @lapack_state, align 8, !tbaa !15
  store i1 false, ptr %6, align 1
  br label %80

39:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %40 = load i64, ptr %8, align 8, !tbaa !9
  %41 = load i64, ptr %8, align 8, !tbaa !9
  %42 = mul i64 %40, %41
  %43 = mul i64 %42, 8
  %44 = call noalias ptr @malloc(i64 noundef %43) #7
  store ptr %44, ptr %12, align 8, !tbaa !5
  %45 = load ptr, ptr %12, align 8, !tbaa !5
  %46 = icmp ne ptr %45, null
  br i1 %46, label %48, label %47

47:                                               ; preds = %39
  store i32 -2, ptr @lapack_state, align 8, !tbaa !15
  store i1 false, ptr %6, align 1
  store i32 1, ptr %13, align 4
  br label %79

48:                                               ; preds = %39
  %49 = load ptr, ptr %12, align 8, !tbaa !5
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = load i64, ptr %8, align 8, !tbaa !9
  %52 = load i64, ptr %8, align 8, !tbaa !9
  %53 = load i32, ptr %11, align 4, !tbaa !13
  call void @convert_layout(ptr noundef %49, ptr noundef %50, i64 noundef %51, i64 noundef %52, i32 noundef %53, i32 noundef 102)
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #6
  %54 = load i8, ptr %10, align 1, !tbaa !11, !range !17, !noundef !18
  %55 = trunc i8 %54 to i1
  %56 = zext i1 %55 to i64
  %57 = select i1 %55, i32 76, i32 85
  %58 = trunc i32 %57 to i8
  store i8 %58, ptr %14, align 1, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #6
  %59 = load i64, ptr %8, align 8, !tbaa !9
  %60 = trunc i64 %59 to i32
  store i32 %60, ptr %15, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #6
  %61 = load i32, ptr %15, align 4, !tbaa !13
  store i32 %61, ptr %16, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #6
  %62 = load ptr, ptr %12, align 8, !tbaa !5
  call void @cpotrf_(ptr noundef %14, ptr noundef %15, ptr noundef %62, ptr noundef %16, ptr noundef %17)
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = load ptr, ptr %12, align 8, !tbaa !5
  %65 = load i64, ptr %8, align 8, !tbaa !9
  %66 = load i64, ptr %8, align 8, !tbaa !9
  %67 = mul i64 %65, %66
  %68 = mul i64 %67, 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %63, ptr align 4 %64, i64 %68, i1 false)
  %69 = load ptr, ptr %12, align 8, !tbaa !5
  call void @free(ptr noundef %69) #6
  %70 = load i32, ptr %17, align 4, !tbaa !13
  %71 = icmp ne i32 %70, 0
  br i1 %71, label %72, label %77

72:                                               ; preds = %48
  %73 = load i32, ptr %17, align 4, !tbaa !13
  %74 = icmp sgt i32 %73, 0
  %75 = zext i1 %74 to i64
  %76 = select i1 %74, i32 -4, i32 -1
  store i32 %76, ptr @lapack_state, align 8, !tbaa !15
  store i1 false, ptr %6, align 1
  store i32 1, ptr %13, align 4
  br label %78

77:                                               ; preds = %48
  store i32 0, ptr @lapack_state, align 8, !tbaa !15
  store i1 true, ptr %6, align 1
  store i32 1, ptr %13, align 4
  br label %78

78:                                               ; preds = %77, %72
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #6
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #6
  br label %79

79:                                               ; preds = %78, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  br label %80

80:                                               ; preds = %79, %38, %29
  %81 = load i1, ptr %6, align 1
  ret i1 %81
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @is_positive_definite(ptr noundef, i64 noundef) #0

declare void @cpotrf_(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #5

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { "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 #6 = { nounwind }
attributes #7 = { 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 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !14, i64 0}
!16 = !{!"", !14, i64 0, !12, i64 4, !10, i64 8, !6, i64 16}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = !{!7, !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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

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

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @is_hermitian(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @lapack_cholesky(ptr noundef %0, i64 noundef %1, ptr noundef writeonly %2, i1 noundef zeroext %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = tail call zeroext i1 @initialize_lapack()
  %11 = icmp ne ptr %0, null
  %or.cond = and i1 %11, %10
  %12 = icmp ne ptr %2, null
  %or.cond3 = and i1 %12, %or.cond
  %13 = icmp ne i64 %1, 0
  %or.cond5.not = and i1 %13, %or.cond3
  br i1 %or.cond5.not, label %15, label %14

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

15:                                               ; preds = %5
  %16 = tail call zeroext i1 @is_hermitian(ptr noundef nonnull %0, i64 noundef %1)
  br i1 %16, label %17, label %19

17:                                               ; preds = %15
  %18 = tail call zeroext i1 @is_positive_definite(ptr noundef nonnull %0, i64 noundef %1)
  br i1 %18, label %20, label %19

19:                                               ; preds = %17, %15
  store i32 -1, ptr @lapack_state, align 8, !tbaa !5
  br label %31

20:                                               ; preds = %17
  %21 = shl i64 %1, 3
  %22 = mul i64 %21, %1
  %23 = tail call noalias ptr @malloc(i64 noundef %22) #6
  %.not = icmp eq ptr %23, null
  br i1 %.not, label %24, label %25

24:                                               ; preds = %20
  store i32 -2, ptr @lapack_state, align 8, !tbaa !5
  br label %31

25:                                               ; preds = %20
  tail call void @convert_layout(ptr noundef nonnull %23, ptr noundef nonnull %0, i64 noundef %1, i64 noundef %1, i32 noundef %4, i32 noundef 102)
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %6) #7
  %26 = select i1 %3, i8 76, i8 85
  store i8 %26, ptr %6, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %7) #7
  %27 = trunc i64 %1 to i32
  store i32 %27, ptr %7, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %8) #7
  store i32 %27, ptr %8, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %9) #7
  call void @cpotrf_(ptr noundef nonnull %6, ptr noundef nonnull %7, ptr noundef nonnull %23, ptr noundef nonnull %8, ptr noundef nonnull %9) #7
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 4 %2, ptr nonnull align 4 %23, i64 %22, i1 false)
  call void @free(ptr noundef nonnull %23) #7
  %28 = load i32, ptr %9, align 4, !tbaa !14
  %.not31 = icmp eq i32 %28, 0
  %29 = icmp sgt i32 %28, 0
  %30 = select i1 %29, i32 -4, i32 -1
  %storemerge = select i1 %.not31, i32 0, i32 %30
  store i32 %storemerge, ptr @lapack_state, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %9) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %8) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %7) #7
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %6) #7
  br label %31

31:                                               ; preds = %24, %25, %19, %14
  %.2 = phi i1 [ false, %14 ], [ false, %19 ], [ %.not31, %25 ], [ false, %24 ]
  ret i1 %.2
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @is_positive_definite(ptr noundef, i64 noundef) local_unnamed_addr #0

declare void @cpotrf_(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #5

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { "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 #6 = { nounwind allocsize(0) }
attributes #7 = { 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 = !{!8, !8, i64 0}
!14 = !{!7, !7, i64 0}


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