; LLVM IR from csviragh/robotsim :: src/utilities (math_utils)

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

@IsInsidePolygon.Inside = external hidden global i8, align 1
@IsInsidePolygon.p1x = external hidden global double, align 8
@IsInsidePolygon.p1y = external hidden global double, align 8
@IsInsidePolygon.p2x = external hidden global double, align 8
@IsInsidePolygon.p2y = external hidden global double, align 8
@IsInsidePolygon.max = external hidden global double, align 8
@IsInsidePolygon.min = external hidden global double, align 8
@IsInsidePolygon.xints = external hidden global double, align 8

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

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

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

11:                                               ; preds = %3
  store i1 false, ptr %4, align 1
  br label %127

12:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i8 0, ptr @IsInsidePolygon.Inside, align 1, !tbaa !11
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = getelementptr inbounds double, ptr %13, i64 0
  %15 = load double, ptr %14, align 8, !tbaa !13
  store double %15, ptr @IsInsidePolygon.p1x, align 8, !tbaa !13
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds double, ptr %16, i64 1
  %18 = load double, ptr %17, align 8, !tbaa !13
  store double %18, ptr @IsInsidePolygon.p1y, align 8, !tbaa !13
  store double 0.000000e+00, ptr @IsInsidePolygon.xints, align 8, !tbaa !13
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %121, %12
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = icmp sle i32 %20, %21
  br i1 %22, label %23, label %124

23:                                               ; preds = %19
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load i32, ptr %8, align 4, !tbaa !9
  %26 = mul nsw i32 2, %25
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = mul nsw i32 2, %27
  %29 = srem i32 %26, %28
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds double, ptr %24, i64 %30
  %32 = load double, ptr %31, align 8, !tbaa !13
  store double %32, ptr @IsInsidePolygon.p2x, align 8, !tbaa !13
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = mul nsw i32 2, %34
  %36 = add nsw i32 %35, 1
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = mul nsw i32 2, %37
  %39 = srem i32 %36, %38
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds double, ptr %33, i64 %40
  %42 = load double, ptr %41, align 8, !tbaa !13
  store double %42, ptr @IsInsidePolygon.p2y, align 8, !tbaa !13
  %43 = load double, ptr @IsInsidePolygon.p2y, align 8, !tbaa !13
  %44 = load double, ptr @IsInsidePolygon.p1y, align 8, !tbaa !13
  %45 = fcmp ogt double %43, %44
  br i1 %45, label %46, label %49

46:                                               ; preds = %23
  %47 = load double, ptr @IsInsidePolygon.p2y, align 8, !tbaa !13
  store double %47, ptr @IsInsidePolygon.max, align 8, !tbaa !13
  %48 = load double, ptr @IsInsidePolygon.p1y, align 8, !tbaa !13
  store double %48, ptr @IsInsidePolygon.min, align 8, !tbaa !13
  br label %52

49:                                               ; preds = %23
  %50 = load double, ptr @IsInsidePolygon.p1y, align 8, !tbaa !13
  store double %50, ptr @IsInsidePolygon.max, align 8, !tbaa !13
  %51 = load double, ptr @IsInsidePolygon.p2y, align 8, !tbaa !13
  store double %51, ptr @IsInsidePolygon.min, align 8, !tbaa !13
  br label %52

52:                                               ; preds = %49, %46
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds double, ptr %53, i64 1
  %55 = load double, ptr %54, align 8, !tbaa !13
  %56 = load double, ptr @IsInsidePolygon.min, align 8, !tbaa !13
  %57 = fcmp ogt double %55, %56
  br i1 %57, label %58, label %118

58:                                               ; preds = %52
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds double, ptr %59, i64 1
  %61 = load double, ptr %60, align 8, !tbaa !13
  %62 = load double, ptr @IsInsidePolygon.max, align 8, !tbaa !13
  %63 = fcmp ole double %61, %62
  br i1 %63, label %64, label %117

64:                                               ; preds = %58
  %65 = load double, ptr @IsInsidePolygon.p2x, align 8, !tbaa !13
  %66 = load double, ptr @IsInsidePolygon.p1x, align 8, !tbaa !13
  %67 = fcmp ogt double %65, %66
  br i1 %67, label %68, label %71

68:                                               ; preds = %64
  %69 = load double, ptr @IsInsidePolygon.p2x, align 8, !tbaa !13
  store double %69, ptr @IsInsidePolygon.max, align 8, !tbaa !13
  %70 = load double, ptr @IsInsidePolygon.p1x, align 8, !tbaa !13
  store double %70, ptr @IsInsidePolygon.min, align 8, !tbaa !13
  br label %74

71:                                               ; preds = %64
  %72 = load double, ptr @IsInsidePolygon.p1x, align 8, !tbaa !13
  store double %72, ptr @IsInsidePolygon.max, align 8, !tbaa !13
  %73 = load double, ptr @IsInsidePolygon.p2x, align 8, !tbaa !13
  store double %73, ptr @IsInsidePolygon.min, align 8, !tbaa !13
  br label %74

74:                                               ; preds = %71, %68
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = getelementptr inbounds double, ptr %75, i64 0
  %77 = load double, ptr %76, align 8, !tbaa !13
  %78 = load double, ptr @IsInsidePolygon.max, align 8, !tbaa !13
  %79 = fcmp ole double %77, %78
  br i1 %79, label %80, label %116

80:                                               ; preds = %74
  %81 = load double, ptr @IsInsidePolygon.p1y, align 8, !tbaa !13
  %82 = load double, ptr @IsInsidePolygon.p2y, align 8, !tbaa !13
  %83 = fcmp une double %81, %82
  br i1 %83, label %84, label %100

84:                                               ; preds = %80
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = getelementptr inbounds double, ptr %85, i64 1
  %87 = load double, ptr %86, align 8, !tbaa !13
  %88 = load double, ptr @IsInsidePolygon.p1y, align 8, !tbaa !13
  %89 = fsub double %87, %88
  %90 = load double, ptr @IsInsidePolygon.p2x, align 8, !tbaa !13
  %91 = load double, ptr @IsInsidePolygon.p1x, align 8, !tbaa !13
  %92 = fsub double %90, %91
  %93 = fmul double %89, %92
  %94 = load double, ptr @IsInsidePolygon.p2y, align 8, !tbaa !13
  %95 = load double, ptr @IsInsidePolygon.p1y, align 8, !tbaa !13
  %96 = fsub double %94, %95
  %97 = fdiv double %93, %96
  %98 = load double, ptr @IsInsidePolygon.p1x, align 8, !tbaa !13
  %99 = fadd double %97, %98
  store double %99, ptr @IsInsidePolygon.xints, align 8, !tbaa !13
  br label %100

100:                                              ; preds = %84, %80
  %101 = load double, ptr @IsInsidePolygon.p1x, align 8, !tbaa !13
  %102 = load double, ptr @IsInsidePolygon.p2x, align 8, !tbaa !13
  %103 = fcmp oeq double %101, %102
  br i1 %103, label %110, label %104

104:                                              ; preds = %100
  %105 = load ptr, ptr %5, align 8, !tbaa !5
  %106 = getelementptr inbounds double, ptr %105, i64 0
  %107 = load double, ptr %106, align 8, !tbaa !13
  %108 = load double, ptr @IsInsidePolygon.xints, align 8, !tbaa !13
  %109 = fcmp ole double %107, %108
  br i1 %109, label %110, label %115

110:                                              ; preds = %104, %100
  %111 = load i8, ptr @IsInsidePolygon.Inside, align 1, !tbaa !11, !range !15, !noundef !16
  %112 = trunc i8 %111 to i1
  %113 = xor i1 %112, true
  %114 = zext i1 %113 to i8
  store i8 %114, ptr @IsInsidePolygon.Inside, align 1, !tbaa !11
  br label %115

115:                                              ; preds = %110, %104
  br label %116

116:                                              ; preds = %115, %74
  br label %117

117:                                              ; preds = %116, %58
  br label %118

118:                                              ; preds = %117, %52
  %119 = load double, ptr @IsInsidePolygon.p2x, align 8, !tbaa !13
  store double %119, ptr @IsInsidePolygon.p1x, align 8, !tbaa !13
  %120 = load double, ptr @IsInsidePolygon.p2y, align 8, !tbaa !13
  store double %120, ptr @IsInsidePolygon.p1y, align 8, !tbaa !13
  br label %121

121:                                              ; preds = %118
  %122 = load i32, ptr %8, align 4, !tbaa !9
  %123 = add nsw i32 %122, 1
  store i32 %123, ptr %8, align 4, !tbaa !9
  br label %19, !llvm.loop !17

124:                                              ; preds = %19
  %125 = load i8, ptr @IsInsidePolygon.Inside, align 1, !tbaa !11, !range !15, !noundef !16
  %126 = trunc i8 %125 to i1
  store i1 %126, ptr %4, align 1
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %127

127:                                              ; preds = %124, %11
  %128 = load i1, ptr %4, align 1
  ret i1 %128
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !18}
!18 = !{!"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"

@IsInsidePolygon.Inside = external hidden local_unnamed_addr global i8, align 1
@IsInsidePolygon.p1x = external hidden local_unnamed_addr global double, align 8
@IsInsidePolygon.p1y = external hidden local_unnamed_addr global double, align 8
@IsInsidePolygon.p2x = external hidden local_unnamed_addr global double, align 8
@IsInsidePolygon.p2y = external hidden local_unnamed_addr global double, align 8
@IsInsidePolygon.max = external hidden local_unnamed_addr global double, align 8
@IsInsidePolygon.min = external hidden local_unnamed_addr global double, align 8
@IsInsidePolygon.xints = external hidden local_unnamed_addr global double, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(write, argmem: read, inaccessiblemem: none) uwtable
define dso_local zeroext i1 @IsInsidePolygon(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp slt i32 %2, 3
  br i1 %4, label %57, label %5

5:                                                ; preds = %3
  store i8 0, ptr @IsInsidePolygon.Inside, align 1, !tbaa !5
  %6 = load double, ptr %1, align 8, !tbaa !9
  store double %6, ptr @IsInsidePolygon.p1x, align 8, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %1, i64 8
  %8 = load double, ptr %7, align 8, !tbaa !9
  store double %8, ptr @IsInsidePolygon.p1y, align 8, !tbaa !9
  store double 0.000000e+00, ptr @IsInsidePolygon.xints, align 8, !tbaa !9
  %9 = shl nuw nsw i32 %2, 1
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load double, ptr %10, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %5, %51
  %13 = phi double [ 0.000000e+00, %5 ], [ %52, %51 ]
  %14 = phi double [ %6, %5 ], [ %21, %51 ]
  %15 = phi double [ %8, %5 ], [ %26, %51 ]
  %.031 = phi i32 [ 0, %5 ], [ %54, %51 ]
  %16 = phi i8 [ 0, %5 ], [ %53, %51 ]
  %17 = shl nuw nsw i32 %.031, 1
  %18 = urem i32 %17, %9
  %19 = zext nneg i32 %18 to i64
  %20 = getelementptr inbounds double, ptr %1, i64 %19
  %21 = load double, ptr %20, align 8, !tbaa !9
  store double %21, ptr @IsInsidePolygon.p2x, align 8, !tbaa !9
  %22 = or disjoint i32 %17, 1
  %23 = urem i32 %22, %9
  %24 = zext nneg i32 %23 to i64
  %25 = getelementptr inbounds double, ptr %1, i64 %24
  %26 = load double, ptr %25, align 8, !tbaa !9
  store double %26, ptr @IsInsidePolygon.p2y, align 8, !tbaa !9
  %27 = fcmp ogt double %26, %15
  %. = select i1 %27, double %26, double %15
  store double %., ptr @IsInsidePolygon.max, align 8, !tbaa !9
  %storemerge = select i1 %27, double %15, double %26
  store double %storemerge, ptr @IsInsidePolygon.min, align 8, !tbaa !9
  %28 = fcmp ule double %11, %storemerge
  %29 = fcmp ugt double %11, %.
  %or.cond = or i1 %28, %29
  br i1 %or.cond, label %51, label %30

30:                                               ; preds = %12
  %31 = fcmp ogt double %21, %14
  %.30 = select i1 %31, double %21, double %14
  store double %.30, ptr @IsInsidePolygon.max, align 8, !tbaa !9
  %storemerge27 = select i1 %31, double %14, double %21
  store double %storemerge27, ptr @IsInsidePolygon.min, align 8, !tbaa !9
  %32 = load double, ptr %0, align 8, !tbaa !9
  %33 = fcmp ugt double %32, %.30
  br i1 %33, label %51, label %34

34:                                               ; preds = %30
  %35 = fcmp une double %15, %26
  br i1 %35, label %36, label %43

36:                                               ; preds = %34
  %37 = fsub double %11, %15
  %38 = fsub double %21, %14
  %39 = fmul double %37, %38
  %40 = fsub double %26, %15
  %41 = fdiv double %39, %40
  %42 = fadd double %14, %41
  store double %42, ptr @IsInsidePolygon.xints, align 8, !tbaa !9
  br label %43

43:                                               ; preds = %36, %34
  %44 = phi double [ %42, %36 ], [ %13, %34 ]
  %45 = fcmp oeq double %14, %21
  br i1 %45, label %49, label %46

46:                                               ; preds = %43
  %47 = load double, ptr %0, align 8, !tbaa !9
  %48 = fcmp ugt double %47, %44
  br i1 %48, label %51, label %49

49:                                               ; preds = %46, %43
  %50 = xor i8 %16, 1
  store i8 %50, ptr @IsInsidePolygon.Inside, align 1, !tbaa !5
  br label %51

51:                                               ; preds = %46, %49, %30, %12
  %52 = phi double [ %44, %46 ], [ %44, %49 ], [ %13, %30 ], [ %13, %12 ]
  %53 = phi i8 [ %16, %46 ], [ %50, %49 ], [ %16, %30 ], [ %16, %12 ]
  store double %21, ptr @IsInsidePolygon.p1x, align 8, !tbaa !9
  store double %26, ptr @IsInsidePolygon.p1y, align 8, !tbaa !9
  %54 = add nuw i32 %.031, 1
  %exitcond.not = icmp eq i32 %.031, %2
  br i1 %exitcond.not, label %55, label %12, !llvm.loop !11

55:                                               ; preds = %51
  %56 = trunc nuw i8 %53 to i1
  br label %57

57:                                               ; preds = %3, %55
  %.017 = phi i1 [ %56, %55 ], [ false, %3 ]
  ret i1 %.017
}

attributes #0 = { nofree norecurse nosync nounwind memory(write, argmem: read, inaccessiblemem: none) 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, !6, i64 0}
!6 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass