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

; 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.quantum_geometric_connection_t = type { i32, i64, ptr, ptr, i8, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @geometric_create_connection(ptr noundef %0, i32 noundef %1, i64 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !11
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  br label %75

13:                                               ; preds = %3
  %14 = load i64, ptr %7, align 8, !tbaa !11
  %15 = icmp ugt i64 %14, 0
  br i1 %15, label %16, label %19

16:                                               ; preds = %13
  %17 = load i64, ptr %7, align 8, !tbaa !11
  %18 = icmp ule i64 %17, 16
  br i1 %18, label %20, label %19

19:                                               ; preds = %16, %13
  store i32 -1, ptr %4, align 4
  br label %75

20:                                               ; preds = %16
  %21 = call noalias align 32 ptr @aligned_alloc(i64 noundef 32, i64 noundef 40) #5
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %21, ptr %22, align 8, !tbaa !5
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %27, label %26

26:                                               ; preds = %20
  store i32 -2, ptr %4, align 4
  br label %75

27:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  %28 = load i64, ptr %7, align 8, !tbaa !11
  %29 = load i64, ptr %7, align 8, !tbaa !11
  %30 = mul i64 %28, %29
  %31 = load i64, ptr %7, align 8, !tbaa !11
  %32 = mul i64 %30, %31
  %33 = mul i64 %32, 8
  store i64 %33, ptr %8, align 8, !tbaa !11
  %34 = load i64, ptr %8, align 8, !tbaa !11
  %35 = call noalias align 32 ptr @aligned_alloc(i64 noundef 32, i64 noundef %34) #5
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load ptr, ptr %36, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.quantum_geometric_connection_t, ptr %37, i32 0, i32 2
  store ptr %35, ptr %38, align 8, !tbaa !13
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = load ptr, ptr %39, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.quantum_geometric_connection_t, ptr %40, i32 0, i32 2
  %42 = load ptr, ptr %41, align 8, !tbaa !13
  %43 = icmp ne ptr %42, null
  br i1 %43, label %47, label %44

44:                                               ; preds = %27
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load ptr, ptr %45, align 8, !tbaa !5
  call void @free(ptr noundef %46) #6
  store i32 -2, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %74

47:                                               ; preds = %27
  %48 = load i32, ptr %6, align 4, !tbaa !9
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = load ptr, ptr %49, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.quantum_geometric_connection_t, ptr %50, i32 0, i32 0
  store i32 %48, ptr %51, align 8, !tbaa !16
  %52 = load i64, ptr %7, align 8, !tbaa !11
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = load ptr, ptr %53, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.quantum_geometric_connection_t, ptr %54, i32 0, i32 1
  store i64 %52, ptr %55, align 8, !tbaa !17
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = load ptr, ptr %56, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.quantum_geometric_connection_t, ptr %57, i32 0, i32 4
  store i8 1, ptr %58, align 8, !tbaa !18
  %59 = load i32, ptr %6, align 4, !tbaa !9
  switch i32 %59, label %66 [
    i32 0, label %60
  ]

60:                                               ; preds = %47
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = load ptr, ptr %61, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.quantum_geometric_connection_t, ptr %62, i32 0, i32 2
  %64 = load ptr, ptr %63, align 8, !tbaa !13
  %65 = load i64, ptr %8, align 8, !tbaa !11
  call void @llvm.memset.p0.i64(ptr align 4 %64, i8 0, i64 %65, i1 false)
  br label %73

66:                                               ; preds = %47
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = load ptr, ptr %67, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.quantum_geometric_connection_t, ptr %68, i32 0, i32 2
  %70 = load ptr, ptr %69, align 8, !tbaa !13
  call void @free(ptr noundef %70) #6
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = load ptr, ptr %71, align 8, !tbaa !5
  call void @free(ptr noundef %72) #6
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %74

73:                                               ; preds = %60
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %74

74:                                               ; preds = %73, %66, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  br label %75

75:                                               ; preds = %74, %26, %19, %12
  %76 = load i32, ptr %4, align 4
  ret i32 %76
}

; Function Attrs: nounwind allocsize(1)
declare noalias ptr @aligned_alloc(i64 noundef, i64 noundef) #1

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nounwind allocsize(1) "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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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: write) }
attributes #5 = { nounwind allocsize(1) }
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, !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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 16}
!14 = !{!"quantum_geometric_connection_t", !10, i64 0, !12, i64 8, !6, i64 16, !6, i64 24, !15, i64 32, !10, i64 36}
!15 = !{!"_Bool", !7, i64 0}
!16 = !{!14, !10, i64 0}
!17 = !{!14, !12, i64 8}
!18 = !{!14, !15, i64 32}


; 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"

; Function Attrs: mustprogress nounwind willreturn memory(readwrite, argmem: write) uwtable
define dso_local range(i32 -2, 1) i32 @geometric_create_connection(ptr noundef writeonly %0, i32 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp ne ptr %0, null
  %5 = add i64 %2, -1
  %or.cond = icmp ult i64 %5, 16
  %or.cond26 = and i1 %4, %or.cond
  br i1 %or.cond26, label %6, label %20

6:                                                ; preds = %3
  %7 = tail call noalias align 32 dereferenceable_or_null(40) ptr @aligned_alloc(i64 noundef 32, i64 noundef 40) #4
  store ptr %7, ptr %0, align 8, !tbaa !5
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %20, label %8

8:                                                ; preds = %6
  %9 = shl nuw nsw i64 %2, 3
  %10 = mul nuw nsw i64 %9, %2
  %11 = mul nuw nsw i64 %10, %2
  %12 = tail call noalias align 32 ptr @aligned_alloc(i64 noundef 32, i64 noundef %11) #4
  %13 = getelementptr inbounds i8, ptr %7, i64 16
  store ptr %12, ptr %13, align 16, !tbaa !9
  %.not25 = icmp eq ptr %12, null
  br i1 %.not25, label %14, label %15

14:                                               ; preds = %8
  tail call void @free(ptr noundef nonnull %7) #5
  br label %20

15:                                               ; preds = %8
  store i32 %1, ptr %7, align 32, !tbaa !14
  %16 = getelementptr inbounds i8, ptr %7, i64 8
  store i64 %2, ptr %16, align 8, !tbaa !15
  %17 = getelementptr inbounds i8, ptr %7, i64 32
  store i8 1, ptr %17, align 32, !tbaa !16
  %cond = icmp eq i32 %1, 0
  br i1 %cond, label %18, label %19

18:                                               ; preds = %15
  tail call void @llvm.memset.p0.i64(ptr nonnull align 32 %12, i8 0, i64 %11, i1 false)
  br label %20

19:                                               ; preds = %15
  tail call void @free(ptr noundef nonnull %12) #5
  tail call void @free(ptr noundef nonnull %7) #5
  br label %20

20:                                               ; preds = %14, %19, %18, %6, %3
  %.1 = phi i32 [ -1, %3 ], [ -2, %6 ], [ 0, %18 ], [ -1, %19 ], [ -2, %14 ]
  ret i32 %.1
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized,aligned") allocsize(1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @aligned_alloc(i64 allocalign noundef, i64 noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { mustprogress nounwind willreturn memory(readwrite, argmem: write) 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 nofree nounwind willreturn allockind("alloc,uninitialized,aligned") allocsize(1) 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 #2 = { 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nounwind allocsize(1) }
attributes #5 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 16}
!10 = !{!"quantum_geometric_connection_t", !11, i64 0, !12, i64 8, !6, i64 16, !6, i64 24, !13, i64 32, !11, i64 36}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!10, !11, i64 0}
!15 = !{!10, !12, i64 8}
!16 = !{!10, !13, i64 32}


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