; LLVM IR from simbody/simbody :: SimTKmath/Optimizers/src/IpOpt (IpTripletToDenseConverter)

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

%"class.SimTKIpopt::TripletToDenseConverter" = type { %"class.SimTKIpopt::ReferencedObject.base", i32, ptr, ptr, i32, i32, i32, i8, ptr, ptr, ptr }
%"class.SimTKIpopt::ReferencedObject.base" = type <{ ptr, i32 }>

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN10SimTKIpopt23TripletToDenseConverter19InitializeConverterEiiPKiS2_(ptr noundef nonnull align 8 dereferenceable(72) %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4) #0 align 2 {
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  store ptr %4, ptr %10, align 8, !tbaa !5
  %12 = load ptr, ptr %6, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %14, i64 4)
  %16 = extractvalue { i64, i1 } %15, 1
  %17 = extractvalue { i64, i1 } %15, 0
  %18 = select i1 %16, i64 -1, i64 %17
  %19 = call noalias noundef nonnull ptr @_Znam(i64 noundef %18) #5
  %20 = getelementptr inbounds %"class.SimTKIpopt::TripletToDenseConverter", ptr %12, i32 0, i32 2
  store ptr %19, ptr %20, align 8, !tbaa !11
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %22, i64 4)
  %24 = extractvalue { i64, i1 } %23, 1
  %25 = extractvalue { i64, i1 } %23, 0
  %26 = select i1 %24, i64 -1, i64 %25
  %27 = call noalias noundef nonnull ptr @_Znam(i64 noundef %26) #5
  %28 = getelementptr inbounds %"class.SimTKIpopt::TripletToDenseConverter", ptr %12, i32 0, i32 3
  store ptr %27, ptr %28, align 8, !tbaa !15
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %56, %5
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = load i32, ptr %8, align 4, !tbaa !9
  %32 = icmp slt i32 %30, %31
  br i1 %32, label %33, label %59

33:                                               ; preds = %29
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = load i32, ptr %11, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i32, ptr %34, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = sub nsw i32 %38, 1
  %40 = getelementptr inbounds %"class.SimTKIpopt::TripletToDenseConverter", ptr %12, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i32, ptr %41, i64 %43
  store i32 %39, ptr %44, align 4, !tbaa !9
  %45 = load ptr, ptr %10, align 8, !tbaa !5
  %46 = load i32, ptr %11, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %45, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !9
  %50 = sub nsw i32 %49, 1
  %51 = getelementptr inbounds %"class.SimTKIpopt::TripletToDenseConverter", ptr %12, i32 0, i32 3
  %52 = load ptr, ptr %51, align 8, !tbaa !15
  %53 = load i32, ptr %11, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds i32, ptr %52, i64 %54
  store i32 %50, ptr %55, align 4, !tbaa !9
  br label %56

56:                                               ; preds = %33
  %57 = load i32, ptr %11, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %11, align 4, !tbaa !9
  br label %29, !llvm.loop !16

59:                                               ; preds = %29
  %60 = load i32, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  ret i32 %60
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare { i64, i1 } @llvm.umul.with.overflow.i64(i64, i64) #2

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #3

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

attributes #0 = { mustprogress 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nobuiltin 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 #4 = { nounwind }
attributes #5 = { builtin 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"_ZTSN10SimTKIpopt23TripletToDenseConverterE", !13, i64 0, !10, i64 12, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 36, !10, i64 40, !14, i64 44, !6, i64 48, !6, i64 56, !6, i64 64}
!13 = !{!"_ZTSN10SimTKIpopt16ReferencedObjectE", !10, i64 8}
!14 = !{!"bool", !7, i64 0}
!15 = !{!12, !6, i64 24}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; 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 uwtable
define dso_local noundef i32 @_ZN10SimTKIpopt23TripletToDenseConverter19InitializeConverterEiiPKiS2_(ptr nocapture noundef nonnull writeonly align 8 dereferenceable(72) %0, i32 noundef %1, i32 noundef returned %2, ptr nocapture noundef readonly %3, ptr nocapture noundef readonly %4) local_unnamed_addr #0 align 2 {
  %6 = zext i32 %2 to i64
  %7 = icmp slt i32 %2, 0
  %8 = shl nuw nsw i64 %6, 2
  %9 = select i1 %7, i64 -1, i64 %8
  %10 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %9) #2
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  store ptr %10, ptr %11, align 8, !tbaa !5
  %12 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %9) #2
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr %12, ptr %13, align 8, !tbaa !13
  %14 = icmp sgt i32 %2, 0
  br i1 %14, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %5
  %min.iters.check = icmp ult i32 %2, 8
  br i1 %min.iters.check, label %.lr.ph.preheader18, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %6, 2147483640
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %15 = getelementptr inbounds i32, ptr %3, i64 %index
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  %wide.load = load <4 x i32>, ptr %15, align 4, !tbaa !14
  %wide.load15 = load <4 x i32>, ptr %16, align 4, !tbaa !14
  %17 = add nsw <4 x i32> %wide.load, <i32 -1, i32 -1, i32 -1, i32 -1>
  %18 = add nsw <4 x i32> %wide.load15, <i32 -1, i32 -1, i32 -1, i32 -1>
  %19 = getelementptr inbounds i32, ptr %10, i64 %index
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  store <4 x i32> %17, ptr %19, align 4, !tbaa !14
  store <4 x i32> %18, ptr %20, align 4, !tbaa !14
  %21 = getelementptr inbounds i32, ptr %4, i64 %index
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  %wide.load16 = load <4 x i32>, ptr %21, align 4, !tbaa !14
  %wide.load17 = load <4 x i32>, ptr %22, align 4, !tbaa !14
  %23 = add nsw <4 x i32> %wide.load16, <i32 -1, i32 -1, i32 -1, i32 -1>
  %24 = add nsw <4 x i32> %wide.load17, <i32 -1, i32 -1, i32 -1, i32 -1>
  %25 = getelementptr inbounds i32, ptr %12, i64 %index
  %26 = getelementptr inbounds i8, ptr %25, i64 16
  store <4 x i32> %23, ptr %25, align 4, !tbaa !14
  store <4 x i32> %24, ptr %26, align 4, !tbaa !14
  %index.next = add nuw i64 %index, 8
  %27 = icmp eq i64 %index.next, %n.vec
  br i1 %27, label %middle.block, label %vector.body, !llvm.loop !15

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %6
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader18

.lr.ph.preheader18:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader18, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader18 ]
  %28 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv
  %29 = load i32, ptr %28, align 4, !tbaa !14
  %30 = add nsw i32 %29, -1
  %31 = getelementptr inbounds i32, ptr %10, i64 %indvars.iv
  store i32 %30, ptr %31, align 4, !tbaa !14
  %32 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv
  %33 = load i32, ptr %32, align 4, !tbaa !14
  %34 = add nsw i32 %33, -1
  %35 = getelementptr inbounds i32, ptr %12, i64 %indvars.iv
  store i32 %34, ptr %35, align 4, !tbaa !14
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %6
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !19

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %5
  ret i32 %2
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { nobuiltin 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 #2 = { builtin 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, !11, i64 16}
!6 = !{!"_ZTSN10SimTKIpopt23TripletToDenseConverterE", !7, i64 0, !8, i64 12, !11, i64 16, !11, i64 24, !8, i64 32, !8, i64 36, !8, i64 40, !12, i64 44, !11, i64 48, !11, i64 56, !11, i64 64}
!7 = !{!"_ZTSN10SimTKIpopt16ReferencedObjectE", !8, i64 8}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!"bool", !9, i64 0}
!13 = !{!6, !11, i64 24}
!14 = !{!8, !8, i64 0}
!15 = distinct !{!15, !16, !17, !18}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!"llvm.loop.isvectorized", i32 1}
!18 = !{!"llvm.loop.unroll.runtime.disable"}
!19 = distinct !{!19, !16, !18, !17}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass