; LLVM IR from ESPboy-edu/ESPboy_tinyphysicsengine :: programs/hello.ll (TPE_bodyNonrotatingJointCollided)

; 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.TPE_Vec3 = type { i32, i32, i32 }
%struct.TPE_Body = type { ptr, i8, ptr, i8, i16, i16, i16, i8, i8 }
%struct.TPE_Joint = type { %struct.TPE_Vec3, [3 x i16], i8 }

; 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) #0

; 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 willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare dso_local { i64, i32 } @TPE_vec3Minus(i64, i32, i64, i32) #2

; Function Attrs: nounwind uwtable
declare dso_local { i64, i32 } @TPE_vec3Plus(i64, i32, i64, i32) #2

; Function Attrs: nounwind uwtable
define dso_local void @_TPE_bodyNonrotatingJointCollided(ptr noundef %0, i16 noundef signext %1, i64 %2, i32 %3, i8 noundef zeroext %4) #2 {
  %6 = alloca %struct.TPE_Vec3, align 4
  %7 = alloca { i64, i32 }, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i16, align 2
  %10 = alloca i8, align 1
  %11 = alloca %struct.TPE_Vec3, align 4
  %12 = alloca { i64, i32 }, align 4
  %13 = alloca { i64, i32 }, align 4
  %14 = alloca { i64, i32 }, align 8
  %15 = alloca i16, align 2
  %16 = alloca i32, align 4
  %17 = alloca %struct.TPE_Vec3, align 4
  %18 = alloca { i64, i32 }, align 4
  %19 = alloca { i64, i32 }, align 4
  %20 = alloca { i64, i32 }, align 8
  %21 = alloca i8, align 1
  %22 = getelementptr inbounds { i64, i32 }, ptr %7, i32 0, i32 0
  store i64 %2, ptr %22, align 4
  %23 = getelementptr inbounds { i64, i32 }, ptr %7, i32 0, i32 1
  store i32 %3, ptr %23, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %7, i64 12, i1 false)
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i16 %1, ptr %9, align 2, !tbaa !9
  store i8 %4, ptr %10, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 12, ptr %11) #3
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.TPE_Body, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !12
  %27 = load i16, ptr %9, align 2, !tbaa !9
  %28 = sext i16 %27 to i64
  %29 = getelementptr inbounds %struct.TPE_Joint, ptr %26, i64 %28
  %30 = getelementptr inbounds %struct.TPE_Joint, ptr %29, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %12, ptr align 4 %30, i64 12, i1 false)
  %31 = getelementptr inbounds { i64, i32 }, ptr %12, i32 0, i32 0
  %32 = load i64, ptr %31, align 4
  %33 = getelementptr inbounds { i64, i32 }, ptr %12, i32 0, i32 1
  %34 = load i32, ptr %33, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %13, ptr align 4 %6, i64 12, i1 false)
  %35 = getelementptr inbounds { i64, i32 }, ptr %13, i32 0, i32 0
  %36 = load i64, ptr %35, align 4
  %37 = getelementptr inbounds { i64, i32 }, ptr %13, i32 0, i32 1
  %38 = load i32, ptr %37, align 4
  %39 = call { i64, i32 } @TPE_vec3Minus(i64 %32, i32 %34, i64 %36, i32 %38)
  store { i64, i32 } %39, ptr %14, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %11, ptr align 8 %14, i64 12, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %11, i64 12, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 12, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %15) #3
  store i16 0, ptr %15, align 2, !tbaa !9
  br label %40

40:                                               ; preds = %115, %5
  %41 = load i16, ptr %15, align 2, !tbaa !9
  %42 = zext i16 %41 to i32
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.TPE_Body, ptr %43, i32 0, i32 1
  %45 = load i8, ptr %44, align 8, !tbaa !17
  %46 = zext i8 %45 to i32
  %47 = icmp slt i32 %42, %46
  br i1 %47, label %49, label %48

48:                                               ; preds = %40
  store i32 2, ptr %16, align 4
  call void @llvm.lifetime.end.p0(i64 2, ptr %15) #3
  br label %118

49:                                               ; preds = %40
  %50 = load i16, ptr %15, align 2, !tbaa !9
  %51 = zext i16 %50 to i32
  %52 = load i16, ptr %9, align 2, !tbaa !9
  %53 = sext i16 %52 to i32
  %54 = icmp ne i32 %51, %53
  br i1 %54, label %55, label %114

55:                                               ; preds = %49
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.TPE_Body, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !12
  %59 = load i16, ptr %15, align 2, !tbaa !9
  %60 = zext i16 %59 to i64
  %61 = getelementptr inbounds %struct.TPE_Joint, ptr %58, i64 %60
  %62 = getelementptr inbounds %struct.TPE_Joint, ptr %61, i32 0, i32 0
  call void @llvm.lifetime.start.p0(i64 12, ptr %17) #3
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.TPE_Body, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !12
  %66 = load i16, ptr %15, align 2, !tbaa !9
  %67 = zext i16 %66 to i64
  %68 = getelementptr inbounds %struct.TPE_Joint, ptr %65, i64 %67
  %69 = getelementptr inbounds %struct.TPE_Joint, ptr %68, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %18, ptr align 4 %69, i64 12, i1 false)
  %70 = getelementptr inbounds { i64, i32 }, ptr %18, i32 0, i32 0
  %71 = load i64, ptr %70, align 4
  %72 = getelementptr inbounds { i64, i32 }, ptr %18, i32 0, i32 1
  %73 = load i32, ptr %72, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %19, ptr align 4 %6, i64 12, i1 false)
  %74 = getelementptr inbounds { i64, i32 }, ptr %19, i32 0, i32 0
  %75 = load i64, ptr %74, align 4
  %76 = getelementptr inbounds { i64, i32 }, ptr %19, i32 0, i32 1
  %77 = load i32, ptr %76, align 4
  %78 = call { i64, i32 } @TPE_vec3Plus(i64 %71, i32 %73, i64 %75, i32 %77)
  store { i64, i32 } %78, ptr %20, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %17, ptr align 8 %20, i64 12, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %62, ptr align 4 %17, i64 12, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 12, ptr %17) #3
  %79 = load i8, ptr %10, align 1, !tbaa !11
  %80 = icmp ne i8 %79, 0
  br i1 %80, label %81, label %113

81:                                               ; preds = %55
  call void @llvm.lifetime.start.p0(i64 1, ptr %21) #3
  store i8 0, ptr %21, align 1, !tbaa !11
  br label %82

82:                                               ; preds = %109, %81
  %83 = load i8, ptr %21, align 1, !tbaa !11
  %84 = zext i8 %83 to i32
  %85 = icmp slt i32 %84, 3
  br i1 %85, label %87, label %86

86:                                               ; preds = %82
  store i32 5, ptr %16, align 4
  call void @llvm.lifetime.end.p0(i64 1, ptr %21) #3
  br label %112

87:                                               ; preds = %82
  %88 = load ptr, ptr %8, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.TPE_Body, ptr %88, i32 0, i32 0
  %90 = load ptr, ptr %89, align 8, !tbaa !12
  %91 = load i16, ptr %9, align 2, !tbaa !9
  %92 = sext i16 %91 to i64
  %93 = getelementptr inbounds %struct.TPE_Joint, ptr %90, i64 %92
  %94 = getelementptr inbounds %struct.TPE_Joint, ptr %93, i32 0, i32 1
  %95 = load i8, ptr %21, align 1, !tbaa !11
  %96 = zext i8 %95 to i64
  %97 = getelementptr inbounds [3 x i16], ptr %94, i64 0, i64 %96
  %98 = load i16, ptr %97, align 2, !tbaa !9
  %99 = load ptr, ptr %8, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.TPE_Body, ptr %99, i32 0, i32 0
  %101 = load ptr, ptr %100, align 8, !tbaa !12
  %102 = load i16, ptr %15, align 2, !tbaa !9
  %103 = zext i16 %102 to i64
  %104 = getelementptr inbounds %struct.TPE_Joint, ptr %101, i64 %103
  %105 = getelementptr inbounds %struct.TPE_Joint, ptr %104, i32 0, i32 1
  %106 = load i8, ptr %21, align 1, !tbaa !11
  %107 = zext i8 %106 to i64
  %108 = getelementptr inbounds [3 x i16], ptr %105, i64 0, i64 %107
  store i16 %98, ptr %108, align 2, !tbaa !9
  br label %109

109:                                              ; preds = %87
  %110 = load i8, ptr %21, align 1, !tbaa !11
  %111 = add i8 %110, 1
  store i8 %111, ptr %21, align 1, !tbaa !11
  br label %82, !llvm.loop !18

112:                                              ; preds = %86
  br label %113

113:                                              ; preds = %112, %55
  br label %114

114:                                              ; preds = %113, %49
  br label %115

115:                                              ; preds = %114
  %116 = load i16, ptr %15, align 2, !tbaa !9
  %117 = add i16 %116, 1
  store i16 %117, ptr %15, align 2, !tbaa !9
  br label %40, !llvm.loop !20

118:                                              ; preds = %48
  ret void
}

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 = !{!"short", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !6, i64 0}
!13 = !{!"", !6, i64 0, !7, i64 8, !6, i64 16, !7, i64 24, !10, i64 26, !10, i64 28, !10, i64 30, !7, i64 32, !7, i64 33}
!14 = !{i64 0, i64 4, !15, i64 4, i64 4, !15, i64 8, i64 4, !15}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!13, !7, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


; 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.TPE_Joint = type { %struct.TPE_Vec3, [3 x i16], i8 }
%struct.TPE_Vec3 = type { i32, i32, i32 }

; Function Attrs: nounwind uwtable
declare dso_local { i64, i32 } @TPE_vec3Minus(i64, i32, i64, i32) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local { i64, i32 } @TPE_vec3Plus(i64, i32, i64, i32) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @_TPE_bodyNonrotatingJointCollided(ptr nocapture noundef readonly %0, i16 noundef signext %1, i64 %2, i32 %3, i8 noundef zeroext %4) local_unnamed_addr #0 {
  %6 = load ptr, ptr %0, align 8, !tbaa !5
  %7 = sext i16 %1 to i64
  %8 = getelementptr inbounds %struct.TPE_Joint, ptr %6, i64 %7
  %.sroa.021.0.copyload = load i64, ptr %8, align 4
  %.sroa.222.0..sroa_idx = getelementptr inbounds i8, ptr %8, i64 8
  %.sroa.222.0.copyload = load i32, ptr %.sroa.222.0..sroa_idx, align 4
  %9 = tail call { i64, i32 } @TPE_vec3Minus(i64 %.sroa.021.0.copyload, i32 %.sroa.222.0.copyload, i64 %2, i32 %3)
  %.fca.0.extract15 = extractvalue { i64, i32 } %9, 0
  %.fca.1.extract16 = extractvalue { i64, i32 } %9, 1
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load i8, ptr %10, align 8, !tbaa !11
  %.not45 = icmp eq i8 %11, 0
  br i1 %.not45, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %.not42 = icmp eq i8 %4, 0
  %12 = zext i16 %1 to i64
  br i1 %.not42, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %18
  %13 = phi i8 [ %19, %18 ], [ %11, %.lr.ph ]
  %indvars.iv50 = phi i64 [ %indvars.iv.next51, %18 ], [ 0, %.lr.ph ]
  %.not.us = icmp eq i64 %indvars.iv50, %12
  br i1 %.not.us, label %18, label %14

14:                                               ; preds = %.lr.ph.split.us
  %15 = load ptr, ptr %0, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.TPE_Joint, ptr %15, i64 %indvars.iv50
  %.sroa.06.0.copyload.us = load i64, ptr %16, align 4
  %.sroa.27.0..sroa_idx.us = getelementptr inbounds i8, ptr %16, i64 8
  %.sroa.27.0.copyload.us = load i32, ptr %.sroa.27.0..sroa_idx.us, align 4
  %17 = tail call { i64, i32 } @TPE_vec3Plus(i64 %.sroa.06.0.copyload.us, i32 %.sroa.27.0.copyload.us, i64 %.fca.0.extract15, i32 %.fca.1.extract16)
  %.fca.0.extract.us = extractvalue { i64, i32 } %17, 0
  %.fca.1.extract.us = extractvalue { i64, i32 } %17, 1
  store i64 %.fca.0.extract.us, ptr %16, align 4
  store i32 %.fca.1.extract.us, ptr %.sroa.27.0..sroa_idx.us, align 4, !tbaa !12
  %.pre53 = load i8, ptr %10, align 8, !tbaa !11
  br label %18

18:                                               ; preds = %14, %.lr.ph.split.us
  %19 = phi i8 [ %.pre53, %14 ], [ %13, %.lr.ph.split.us ]
  %indvars.iv.next51 = add nuw nsw i64 %indvars.iv50, 1
  %20 = zext i8 %19 to i64
  %21 = icmp ult i64 %indvars.iv.next51, %20
  br i1 %21, label %.lr.ph.split.us, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %.loopexit, %18, %5
  ret void

.lr.ph.split:                                     ; preds = %.lr.ph, %.loopexit
  %22 = phi i8 [ %33, %.loopexit ], [ %11, %.lr.ph ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %.loopexit ], [ 0, %.lr.ph ]
  %.not = icmp eq i64 %indvars.iv, %12
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %.lr.ph.split
  %23 = load ptr, ptr %0, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.TPE_Joint, ptr %23, i64 %indvars.iv
  %.sroa.06.0.copyload = load i64, ptr %24, align 4
  %.sroa.27.0..sroa_idx = getelementptr inbounds i8, ptr %24, i64 8
  %.sroa.27.0.copyload = load i32, ptr %.sroa.27.0..sroa_idx, align 4
  %25 = tail call { i64, i32 } @TPE_vec3Plus(i64 %.sroa.06.0.copyload, i32 %.sroa.27.0.copyload, i64 %.fca.0.extract15, i32 %.fca.1.extract16)
  %.fca.0.extract = extractvalue { i64, i32 } %25, 0
  %.fca.1.extract = extractvalue { i64, i32 } %25, 1
  store i64 %.fca.0.extract, ptr %24, align 4
  store i32 %.fca.1.extract, ptr %.sroa.27.0..sroa_idx, align 4, !tbaa !12
  %26 = load ptr, ptr %0, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.TPE_Joint, ptr %26, i64 %7, i32 1, i64 0
  %28 = getelementptr inbounds %struct.TPE_Joint, ptr %26, i64 %indvars.iv, i32 1, i64 0
  %29 = load <2 x i16>, ptr %27, align 2, !tbaa !16
  store <2 x i16> %29, ptr %28, align 2, !tbaa !16
  %30 = getelementptr inbounds %struct.TPE_Joint, ptr %26, i64 %7, i32 1, i64 2
  %31 = load i16, ptr %30, align 2, !tbaa !16
  %32 = getelementptr inbounds %struct.TPE_Joint, ptr %26, i64 %indvars.iv, i32 1, i64 2
  store i16 %31, ptr %32, align 2, !tbaa !16
  %.pre = load i8, ptr %10, align 8, !tbaa !11
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader, %.lr.ph.split
  %33 = phi i8 [ %.pre, %.preheader ], [ %22, %.lr.ph.split ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %34 = zext i8 %33 to i64
  %35 = icmp ult i64 %indvars.iv.next, %34
  br i1 %35, label %.lr.ph.split, label %._crit_edge, !llvm.loop !14
}

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

!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, !8, i64 8, !7, i64 16, !8, i64 24, !10, i64 26, !10, i64 28, !10, i64 30, !8, i64 32, !8, i64 33}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!6, !8, i64 8}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!10, !10, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass